Skip to content

Commit 5a5a0c1

Browse files
mykyta5Kernel Patches Daemon
authored andcommitted
selftests/bpf: add bpf_wq tests
Add bpf_wq selftests to verify: * BPF program using non-constant offset of struct bpf_wq is rejected * BPF program using map with no BTF for storing struct bpf_wq is rejected Signed-off-by: Mykyta Yatsenko <[email protected]> Tested-by: Eduard Zingerman <[email protected]>
1 parent a25031e commit 5a5a0c1

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

tools/testing/selftests/bpf/prog_tests/wq.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,47 @@ void serial_test_failures_wq(void)
3838
{
3939
RUN_TESTS(wq_failures);
4040
}
41+
42+
static void test_failure_map_no_btf(void)
43+
{
44+
struct wq *skel = NULL;
45+
char log[8192];
46+
const struct bpf_insn *insns;
47+
size_t insn_cnt;
48+
int ret, err, map_fd;
49+
LIBBPF_OPTS(bpf_prog_load_opts, opts, .log_size = sizeof(log), .log_buf = log,
50+
.log_level = 2);
51+
52+
skel = wq__open();
53+
if (!ASSERT_OK_PTR(skel, "skel_open"))
54+
return;
55+
56+
err = bpf_object__prepare(skel->obj);
57+
if (!ASSERT_OK(err, "skel__prepare"))
58+
goto out;
59+
60+
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "map_no_btf", sizeof(__u32), sizeof(__u64), 100,
61+
NULL);
62+
if (!ASSERT_GT(map_fd, -1, "map create"))
63+
goto out;
64+
65+
err = bpf_map__reuse_fd(skel->maps.array, map_fd);
66+
if (!ASSERT_OK(err, "map reuse fd"))
67+
goto out;
68+
69+
insns = bpf_program__insns(skel->progs.test_map_no_btf);
70+
insn_cnt = bpf_program__insn_cnt(skel->progs.test_map_no_btf);
71+
ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
72+
73+
ASSERT_NEQ(ret, 0, "prog load failed");
74+
ASSERT_HAS_SUBSTR(log, "map 'map_no_btf' has to have BTF in order to use bpf_wq",
75+
"log complains no map BTF");
76+
out:
77+
wq__destroy(skel);
78+
}
79+
80+
void test_wq_custom(void)
81+
{
82+
if (test__start_subtest("test_failure_map_no_btf"))
83+
test_failure_map_no_btf();
84+
}

tools/testing/selftests/bpf/progs/wq.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,20 @@ long test_call_lru_sleepable(void *ctx)
187187

188188
return test_elem_callback(&lru, &key, wq_callback);
189189
}
190+
191+
SEC("tc")
192+
long test_map_no_btf(void *ctx)
193+
{
194+
struct elem *val;
195+
struct bpf_wq *wq;
196+
int key = 42;
197+
198+
val = bpf_map_lookup_elem(&array, &key);
199+
if (!val)
200+
return -2;
201+
202+
wq = &val->w;
203+
if (bpf_wq_init(wq, &array, 0) != 0)
204+
return -3;
205+
return 0;
206+
}

tools/testing/selftests/bpf/progs/wq_failures.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,26 @@ long test_wrong_wq_pointer_offset(void *ctx)
142142

143143
return -22;
144144
}
145+
146+
SEC("tc")
147+
__log_level(2)
148+
__failure
149+
__msg(": (85) call bpf_wq_init#")
150+
__msg("R1 doesn't have constant offset. bpf_wq has to be at the constant offset")
151+
long test_bad_wq_off(void *ctx)
152+
{
153+
struct elem *val;
154+
struct bpf_wq *wq;
155+
int key = 42;
156+
u64 unknown;
157+
158+
val = bpf_map_lookup_elem(&array, &key);
159+
if (!val)
160+
return -2;
161+
162+
unknown = bpf_get_prandom_u32();
163+
wq = &val->w + unknown;
164+
if (bpf_wq_init(wq, &array, 0) != 0)
165+
return -3;
166+
return 0;
167+
}

0 commit comments

Comments
 (0)