selftests/bpf: Add more 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 <yatsenko@meta.com>
Tested-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20251010164606.147298-2-mykyta.yatsenko5@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Mykyta Yatsenko
2025-10-10 17:46:05 +01:00
committed by Alexei Starovoitov
parent 5f8d411729
commit bca2b74ea9
3 changed files with 96 additions and 0 deletions

View File

@@ -38,3 +38,59 @@ void serial_test_failures_wq(void)
{
RUN_TESTS(wq_failures);
}
static void test_failure_map_no_btf(void)
{
struct wq *skel = NULL;
char log[8192];
const struct bpf_insn *insns;
size_t insn_cnt;
int ret, err, map_fd;
LIBBPF_OPTS(bpf_prog_load_opts, opts, .log_size = sizeof(log), .log_buf = log,
.log_level = 2);
skel = wq__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
return;
err = bpf_object__prepare(skel->obj);
if (!ASSERT_OK(err, "skel__prepare"))
goto out;
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "map_no_btf", sizeof(__u32), sizeof(__u64), 100,
NULL);
if (!ASSERT_GT(map_fd, -1, "map create"))
goto out;
err = bpf_map__reuse_fd(skel->maps.array, map_fd);
if (!ASSERT_OK(err, "map reuse fd")) {
close(map_fd);
goto out;
}
insns = bpf_program__insns(skel->progs.test_map_no_btf);
if (!ASSERT_OK_PTR(insns, "insns ptr"))
goto out;
insn_cnt = bpf_program__insn_cnt(skel->progs.test_map_no_btf);
if (!ASSERT_GT(insn_cnt, 0u, "insn cnt"))
goto out;
ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
if (!ASSERT_LT(ret, 0, "prog load failed")) {
if (ret > 0)
close(ret);
goto out;
}
ASSERT_HAS_SUBSTR(log, "map 'map_no_btf' has to have BTF in order to use bpf_wq",
"log complains no map BTF");
out:
wq__destroy(skel);
}
void test_wq_custom(void)
{
if (test__start_subtest("test_failure_map_no_btf"))
test_failure_map_no_btf();
}

View File

@@ -187,3 +187,20 @@ long test_call_lru_sleepable(void *ctx)
return test_elem_callback(&lru, &key, wq_callback);
}
SEC("tc")
long test_map_no_btf(void *ctx)
{
struct elem *val;
struct bpf_wq *wq;
int key = 42;
val = bpf_map_lookup_elem(&array, &key);
if (!val)
return -2;
wq = &val->w;
if (bpf_wq_init(wq, &array, 0) != 0)
return -3;
return 0;
}

View File

@@ -142,3 +142,26 @@ long test_wrong_wq_pointer_offset(void *ctx)
return -22;
}
SEC("tc")
__log_level(2)
__failure
__msg(": (85) call bpf_wq_init#")
__msg("R1 doesn't have constant offset. bpf_wq has to be at the constant offset")
long test_bad_wq_off(void *ctx)
{
struct elem *val;
struct bpf_wq *wq;
int key = 42;
u64 unknown;
val = bpf_map_lookup_elem(&array, &key);
if (!val)
return -2;
unknown = bpf_get_prandom_u32();
wq = &val->w + unknown;
if (bpf_wq_init(wq, &array, 0) != 0)
return -3;
return 0;
}