Skip to content

Commit cd259b5

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 #333/1 struct_ops_private_stack/private_stack:OK #333/2 struct_ops_private_stack/private_stack_fail:OK #333/3 struct_ops_private_stack/private_stack_recur:OK #333 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->use_priv_stack = true; 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. Signed-off-by: Yonghong Song <[email protected]>
1 parent 9c5aa7e commit cd259b5

File tree

6 files changed

+379
-0
lines changed

6 files changed

+379
-0
lines changed

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

Lines changed: 94 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
};
@@ -380,6 +413,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
380413

381414
(void)bpf_testmod_test_arg_ptr_to_struct(&struct_arg1_2);
382415

416+
bpf_testmod_test_struct_ops3();
417+
383418
struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) +
384419
sizeof(int)), GFP_KERNEL);
385420
if (struct_arg3 != NULL) {
@@ -584,6 +619,8 @@ BTF_ID_FLAGS(func, bpf_kfunc_trusted_num_test, KF_TRUSTED_ARGS)
584619
BTF_ID_FLAGS(func, bpf_kfunc_rcu_task_test, KF_RCU)
585620
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)
586621
BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE)
622+
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1)
623+
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_2)
587624
BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
588625

589626
BTF_ID_LIST(bpf_testmod_dtor_ids)
@@ -1094,6 +1131,10 @@ static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
10941131
.is_valid_access = bpf_testmod_ops_is_valid_access,
10951132
};
10961133

1134+
static const struct bpf_verifier_ops bpf_testmod_verifier_ops3 = {
1135+
.is_valid_access = bpf_testmod_ops_is_valid_access,
1136+
};
1137+
10971138
static int bpf_dummy_reg(void *kdata, struct bpf_link *link)
10981139
{
10991140
struct bpf_testmod_ops *ops = kdata;
@@ -1173,6 +1214,58 @@ struct bpf_struct_ops bpf_testmod_ops2 = {
11731214
.owner = THIS_MODULE,
11741215
};
11751216

1217+
static int st_ops3_reg(void *kdata, struct bpf_link *link)
1218+
{
1219+
int err = 0;
1220+
1221+
mutex_lock(&st_ops_mutex);
1222+
if (st_ops3) {
1223+
pr_err("st_ops has already been registered\n");
1224+
err = -EEXIST;
1225+
goto unlock;
1226+
}
1227+
st_ops3 = kdata;
1228+
1229+
unlock:
1230+
mutex_unlock(&st_ops_mutex);
1231+
return err;
1232+
}
1233+
1234+
static void st_ops3_unreg(void *kdata, struct bpf_link *link)
1235+
{
1236+
mutex_lock(&st_ops_mutex);
1237+
st_ops3 = NULL;
1238+
mutex_unlock(&st_ops_mutex);
1239+
}
1240+
1241+
static int st_ops3_check_member(const struct btf_type *t,
1242+
const struct btf_member *member,
1243+
const struct bpf_prog *prog)
1244+
{
1245+
u32 moff = __btf_member_bit_offset(t, member) / 8;
1246+
1247+
switch (moff) {
1248+
case offsetof(struct bpf_testmod_ops3, test_1):
1249+
prog->aux->use_priv_stack = true;
1250+
fallthrough;
1251+
default:
1252+
break;
1253+
}
1254+
return 0;
1255+
}
1256+
1257+
struct bpf_struct_ops bpf_testmod_ops3 = {
1258+
.verifier_ops = &bpf_testmod_verifier_ops3,
1259+
.init = bpf_testmod_ops_init,
1260+
.init_member = bpf_testmod_ops_init_member,
1261+
.reg = st_ops3_reg,
1262+
.unreg = st_ops3_unreg,
1263+
.check_member = st_ops3_check_member,
1264+
.cfi_stubs = &__bpf_testmod_ops3,
1265+
.name = "bpf_testmod_ops3",
1266+
.owner = THIS_MODULE,
1267+
};
1268+
11761269
static int bpf_test_mod_st_ops__test_prologue(struct st_ops_args *args)
11771270
{
11781271
return 0;
@@ -1331,6 +1424,7 @@ static int bpf_testmod_init(void)
13311424
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_testmod_kfunc_set);
13321425
ret = ret ?: register_bpf_struct_ops(&bpf_bpf_testmod_ops, bpf_testmod_ops);
13331426
ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops2, bpf_testmod_ops2);
1427+
ret = ret ?: register_bpf_struct_ops(&bpf_testmod_ops3, bpf_testmod_ops3);
13341428
ret = ret ?: register_bpf_struct_ops(&testmod_st_ops, bpf_testmod_st_ops);
13351429
ret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,
13361430
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)