Skip to content

Commit bceca96

Browse files
Yonghong SongKernel Patches Daemon
Yonghong Song
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add struct_ops prog private stack tests
Add three tests for struct_ops using private stack. ./test_progs -t struct_ops_private_stack #336/1 struct_ops_private_stack/private_stack:OK #336/2 struct_ops_private_stack/private_stack_fail:OK #336/3 struct_ops_private_stack/private_stack_recur:OK #336 struct_ops_private_stack:OK The following is a snippet of a struct_ops check_member() implementation: u32 moff = __btf_member_bit_offset(t, member) / 8; switch (moff) { case offsetof(struct bpf_testmod_ops3, test_1): prog->aux->priv_stack_requested = true; prog->aux->recursion_detected = test_1_recursion_detected; fallthrough; default: break; } return 0; The first test is with nested two different callback functions where the first prog has more than 512 byte stack size (including subprogs) with private stack enabled. The second test is a negative test where the second prog has more than 512 byte stack size without private stack enabled. The third test is the same callback function recursing itself. At run time, the jit trampoline recursion check kicks in to prevent the recursion. The recursion_detected() callback function is implemented by the bpf_testmod, the following message in dmesg bpf_testmod: oh no, recursing into test_1, recursion_misses 1 demonstrates the callback function is indeed triggered when recursion miss happens. Signed-off-by: Yonghong Song <[email protected]>
1 parent 5972c65 commit bceca96

File tree

6 files changed

+389
-0
lines changed

6 files changed

+389
-0
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,39 @@ __bpf_kfunc void bpf_testmod_ctx_release(struct bpf_testmod_ctx *ctx)
245245
call_rcu(&ctx->rcu, testmod_free_cb);
246246
}
247247

248+
static struct bpf_testmod_ops3 *st_ops3;
249+
250+
static int bpf_testmod_test_3(void)
251+
{
252+
return 0;
253+
}
254+
255+
static int bpf_testmod_test_4(void)
256+
{
257+
return 0;
258+
}
259+
260+
static struct bpf_testmod_ops3 __bpf_testmod_ops3 = {
261+
.test_1 = bpf_testmod_test_3,
262+
.test_2 = bpf_testmod_test_4,
263+
};
264+
265+
static void bpf_testmod_test_struct_ops3(void)
266+
{
267+
if (st_ops3)
268+
st_ops3->test_1();
269+
}
270+
271+
__bpf_kfunc void bpf_testmod_ops3_call_test_1(void)
272+
{
273+
st_ops3->test_1();
274+
}
275+
276+
__bpf_kfunc void bpf_testmod_ops3_call_test_2(void)
277+
{
278+
st_ops3->test_2();
279+
}
280+
248281
struct bpf_testmod_btf_type_tag_1 {
249282
int a;
250283
};
@@ -382,6 +415,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
382415

383416
(void)trace_bpf_testmod_test_raw_tp_null(NULL);
384417

418+
bpf_testmod_test_struct_ops3();
419+
385420
struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) +
386421
sizeof(int)), GFP_KERNEL);
387422
if (struct_arg3 != NULL) {
@@ -586,6 +621,8 @@ BTF_ID_FLAGS(func, bpf_kfunc_trusted_num_test, KF_TRUSTED_ARGS)
586621
BTF_ID_FLAGS(func, bpf_kfunc_rcu_task_test, KF_RCU)
587622
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)
588623
BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE)
624+
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1)
625+
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_2)
589626
BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
590627

591628
BTF_ID_LIST(bpf_testmod_dtor_ids)
@@ -1096,6 +1133,10 @@ static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
10961133
.is_valid_access = bpf_testmod_ops_is_valid_access,
10971134
};
10981135

1136+
static const struct bpf_verifier_ops bpf_testmod_verifier_ops3 = {
1137+
.is_valid_access = bpf_testmod_ops_is_valid_access,
1138+
};
1139+
10991140
static int bpf_dummy_reg(void *kdata, struct bpf_link *link)
11001141
{
11011142
struct bpf_testmod_ops *ops = kdata;
@@ -1175,6 +1216,68 @@ struct bpf_struct_ops bpf_testmod_ops2 = {
11751216
.owner = THIS_MODULE,
11761217
};
11771218

1219+
static int st_ops3_reg(void *kdata, struct bpf_link *link)
1220+
{
1221+
int err = 0;
1222+
1223+
mutex_lock(&st_ops_mutex);
1224+
if (st_ops3) {
1225+
pr_err("st_ops has already been registered\n");
1226+
err = -EEXIST;
1227+
goto unlock;
1228+
}
1229+
st_ops3 = kdata;
1230+
1231+
unlock:
1232+
mutex_unlock(&st_ops_mutex);
1233+
return err;
1234+
}
1235+
1236+
static void st_ops3_unreg(void *kdata, struct bpf_link *link)
1237+
{
1238+
mutex_lock(&st_ops_mutex);
1239+
st_ops3 = NULL;
1240+
mutex_unlock(&st_ops_mutex);
1241+
}
1242+
1243+
static void test_1_recursion_detected(struct bpf_prog *prog)
1244+
{
1245+
struct bpf_prog_stats *stats;
1246+
1247+
stats = this_cpu_ptr(prog->stats);
1248+
printk("bpf_testmod: oh no, recursing into test_1, recursion_misses %llu",
1249+
u64_stats_read(&stats->misses));
1250+
}
1251+
1252+
static int st_ops3_check_member(const struct btf_type *t,
1253+
const struct btf_member *member,
1254+
const struct bpf_prog *prog)
1255+
{
1256+
u32 moff = __btf_member_bit_offset(t, member) / 8;
1257+
1258+
switch (moff) {
1259+
case offsetof(struct bpf_testmod_ops3, test_1):
1260+
prog->aux->priv_stack_requested = true;
1261+
prog->aux->recursion_detected = test_1_recursion_detected;
1262+
fallthrough;
1263+
default:
1264+
break;
1265+
}
1266+
return 0;
1267+
}
1268+
1269+
struct bpf_struct_ops bpf_testmod_ops3 = {
1270+
.verifier_ops = &bpf_testmod_verifier_ops3,
1271+
.init = bpf_testmod_ops_init,
1272+
.init_member = bpf_testmod_ops_init_member,
1273+
.reg = st_ops3_reg,
1274+
.unreg = st_ops3_unreg,
1275+
.check_member = st_ops3_check_member,
1276+
.cfi_stubs = &__bpf_testmod_ops3,
1277+
.name = "bpf_testmod_ops3",
1278+
.owner = THIS_MODULE,
1279+
};
1280+
11781281
static int bpf_test_mod_st_ops__test_prologue(struct st_ops_args *args)
11791282
{
11801283
return 0;
@@ -1333,6 +1436,7 @@ static int bpf_testmod_init(void)
13331436
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_testmod_kfunc_set);
13341437
ret = ret ?: register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops);
13351438
ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2);
1439+
ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops3, bpf_testmod_ops3);
13361440
ret = ret ?: register_bpf_struct_ops(&testmod_st_ops, bpf_testmod_st_ops);
13371441
ret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,
13381442
ARRAY_SIZE(bpf_testmod_dtors),

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ struct bpf_testmod_ops2 {
9494
int (*test_1)(void);
9595
};
9696

97+
struct bpf_testmod_ops3 {
98+
int (*test_1)(void);
99+
int (*test_2)(void);
100+
};
101+
97102
struct st_ops_args {
98103
u64 a;
99104
};
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "struct_ops_private_stack.skel.h"
5+
#include "struct_ops_private_stack_fail.skel.h"
6+
#include "struct_ops_private_stack_recur.skel.h"
7+
8+
static void test_private_stack(void)
9+
{
10+
struct struct_ops_private_stack *skel;
11+
struct bpf_link *link;
12+
int err;
13+
14+
skel = struct_ops_private_stack__open();
15+
if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack__open"))
16+
return;
17+
18+
if (skel->data->skip) {
19+
test__skip();
20+
goto cleanup;
21+
}
22+
23+
err = struct_ops_private_stack__load(skel);
24+
if (!ASSERT_OK(err, "struct_ops_private_stack__load"))
25+
goto cleanup;
26+
27+
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
28+
if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
29+
goto cleanup;
30+
31+
ASSERT_OK(trigger_module_test_read(256), "trigger_read");
32+
33+
ASSERT_EQ(skel->bss->val_i, 3, "val_i");
34+
ASSERT_EQ(skel->bss->val_j, 8, "val_j");
35+
36+
bpf_link__destroy(link);
37+
38+
cleanup:
39+
struct_ops_private_stack__destroy(skel);
40+
}
41+
42+
static void test_private_stack_fail(void)
43+
{
44+
struct struct_ops_private_stack_fail *skel;
45+
int err;
46+
47+
skel = struct_ops_private_stack_fail__open();
48+
if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack_fail__open"))
49+
return;
50+
51+
if (skel->data->skip) {
52+
test__skip();
53+
goto cleanup;
54+
}
55+
56+
err = struct_ops_private_stack_fail__load(skel);
57+
if (!ASSERT_ERR(err, "struct_ops_private_stack_fail__load"))
58+
goto cleanup;
59+
return;
60+
61+
cleanup:
62+
struct_ops_private_stack_fail__destroy(skel);
63+
}
64+
65+
static void test_private_stack_recur(void)
66+
{
67+
struct struct_ops_private_stack_recur *skel;
68+
struct bpf_link *link;
69+
int err;
70+
71+
skel = struct_ops_private_stack_recur__open();
72+
if (!ASSERT_OK_PTR(skel, "struct_ops_private_stack_recur__open"))
73+
return;
74+
75+
if (skel->data->skip) {
76+
test__skip();
77+
goto cleanup;
78+
}
79+
80+
err = struct_ops_private_stack_recur__load(skel);
81+
if (!ASSERT_OK(err, "struct_ops_private_stack_recur__load"))
82+
goto cleanup;
83+
84+
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
85+
if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
86+
goto cleanup;
87+
88+
ASSERT_OK(trigger_module_test_read(256), "trigger_read");
89+
90+
ASSERT_EQ(skel->bss->val_j, 3, "val_j");
91+
92+
bpf_link__destroy(link);
93+
94+
cleanup:
95+
struct_ops_private_stack_recur__destroy(skel);
96+
}
97+
98+
void test_struct_ops_private_stack(void)
99+
{
100+
if (test__start_subtest("private_stack"))
101+
test_private_stack();
102+
if (test__start_subtest("private_stack_fail"))
103+
test_private_stack_fail();
104+
if (test__start_subtest("private_stack_recur"))
105+
test_private_stack_recur();
106+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include "../bpf_testmod/bpf_testmod.h"
7+
8+
char _license[] SEC("license") = "GPL";
9+
10+
#if defined(__TARGET_ARCH_x86)
11+
bool skip __attribute((__section__(".data"))) = false;
12+
#else
13+
bool skip = true;
14+
#endif
15+
16+
void bpf_testmod_ops3_call_test_2(void) __ksym;
17+
18+
int val_i, val_j;
19+
20+
__noinline static int subprog2(int *a, int *b)
21+
{
22+
return val_i + a[10] + b[20];
23+
}
24+
25+
__noinline static int subprog1(int *a)
26+
{
27+
/* stack size 200 bytes */
28+
int b[50] = {};
29+
30+
b[20] = 2;
31+
return subprog2(a, b);
32+
}
33+
34+
35+
SEC("struct_ops")
36+
int BPF_PROG(test_1)
37+
{
38+
/* stack size 400 bytes */
39+
int a[100] = {};
40+
41+
a[10] = 1;
42+
val_i = subprog1(a);
43+
bpf_testmod_ops3_call_test_2();
44+
return 0;
45+
}
46+
47+
SEC("struct_ops")
48+
int BPF_PROG(test_2)
49+
{
50+
/* stack size 200 bytes */
51+
int a[50] = {};
52+
53+
a[10] = 3;
54+
val_j = subprog1(a);
55+
return 0;
56+
}
57+
58+
SEC(".struct_ops")
59+
struct bpf_testmod_ops3 testmod_1 = {
60+
.test_1 = (void *)test_1,
61+
.test_2 = (void *)test_2,
62+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include "../bpf_testmod/bpf_testmod.h"
7+
8+
char _license[] SEC("license") = "GPL";
9+
10+
#if defined(__TARGET_ARCH_x86)
11+
bool skip __attribute((__section__(".data"))) = false;
12+
#else
13+
bool skip = true;
14+
#endif
15+
16+
void bpf_testmod_ops3_call_test_2(void) __ksym;
17+
18+
int val_i, val_j;
19+
20+
__noinline static int subprog2(int *a, int *b)
21+
{
22+
return val_i + a[10] + b[20];
23+
}
24+
25+
__noinline static int subprog1(int *a)
26+
{
27+
/* stack size 200 bytes */
28+
int b[50] = {};
29+
30+
b[20] = 2;
31+
return subprog2(a, b);
32+
}
33+
34+
35+
SEC("struct_ops")
36+
int BPF_PROG(test_1)
37+
{
38+
/* stack size 100 bytes */
39+
int a[25] = {};
40+
41+
a[10] = 1;
42+
val_i = subprog1(a);
43+
bpf_testmod_ops3_call_test_2();
44+
return 0;
45+
}
46+
47+
SEC("struct_ops")
48+
int BPF_PROG(test_2)
49+
{
50+
/* stack size 400 bytes */
51+
int a[100] = {};
52+
53+
a[10] = 3;
54+
val_j = subprog1(a);
55+
return 0;
56+
}
57+
58+
SEC(".struct_ops")
59+
struct bpf_testmod_ops3 testmod_1 = {
60+
.test_1 = (void *)test_1,
61+
.test_2 = (void *)test_2,
62+
};

0 commit comments

Comments
 (0)