selftests/bpf: wq: add bpf_wq_start() checks
authorBenjamin Tissoires <bentiss@kernel.org>
Sat, 20 Apr 2024 09:09:16 +0000 (11:09 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 24 Apr 2024 02:46:57 +0000 (19:46 -0700)
Allows to test if allocation/free works

Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
Link: https://lore.kernel.org/r/20240420-bpf_wq-v2-16-6c986a5a741f@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/bpf_experimental.h
tools/testing/selftests/bpf/prog_tests/wq.c
tools/testing/selftests/bpf/progs/wq.c

index b80b39f76034f3ff2a969c0bd00cc2945829181f..93c5a6c446b384623095441c3b43a412759c998b 100644 (file)
@@ -471,6 +471,7 @@ extern struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) __
 extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;
 
 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
+extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
 extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,
                int (callback_fn)(void *map, int *key, struct bpf_wq *wq),
                unsigned int flags__k, void *aux__ign) __ksym;
index 26ab69796103d7cb4f8c01b7deba6858feb894a4..8a4a91d944ccc76c01639738daa4161287c68f46 100644 (file)
@@ -6,9 +6,31 @@
 
 void serial_test_wq(void)
 {
+       struct wq *wq_skel = NULL;
+       int err, prog_fd;
+
        LIBBPF_OPTS(bpf_test_run_opts, topts);
 
        RUN_TESTS(wq);
+
+       /* re-run the success test to check if the timer was actually executed */
+
+       wq_skel = wq__open_and_load();
+       if (!ASSERT_OK_PTR(wq_skel, "wq_skel_load"))
+               return;
+
+       err = wq__attach(wq_skel);
+       if (!ASSERT_OK(err, "wq_attach"))
+               return;
+
+       prog_fd = bpf_program__fd(wq_skel->progs.test_syscall_array_sleepable);
+       err = bpf_prog_test_run_opts(prog_fd, &topts);
+       ASSERT_OK(err, "test_run");
+       ASSERT_EQ(topts.retval, 0, "test_run");
+
+       usleep(50); /* 10 usecs should be enough, but give it extra */
+
+       ASSERT_EQ(wq_skel->bss->ok_sleepable, (1 << 1), "ok_sleepable");
 }
 
 void serial_test_failures_wq(void)
index dc301b52f91c788fc3f9a2d42e1868a4e876a3b8..49e712acbf60042f91e2ff4f49cd7920d358426f 100644 (file)
@@ -49,12 +49,19 @@ struct {
        __type(value, struct elem);
 } lru SEC(".maps");
 
+__u32 ok;
+__u32 ok_sleepable;
+
 static int test_elem_callback(void *map, int *key,
                int (callback_fn)(void *map, int *key, struct bpf_wq *wq))
 {
        struct elem init = {}, *val;
        struct bpf_wq *wq;
 
+       if ((ok & (1 << *key) ||
+           (ok_sleepable & (1 << *key))))
+               return -22;
+
        if (map == &lru &&
            bpf_map_update_elem(map, key, &init, 0))
                return -1;
@@ -70,6 +77,9 @@ static int test_elem_callback(void *map, int *key,
        if (bpf_wq_set_callback(wq, callback_fn, 0))
                return -4;
 
+       if (bpf_wq_start(wq, 0))
+               return -5;
+
        return 0;
 }
 
@@ -79,6 +89,10 @@ static int test_hmap_elem_callback(void *map, int *key,
        struct hmap_elem init = {}, *val;
        struct bpf_wq *wq;
 
+       if ((ok & (1 << *key) ||
+           (ok_sleepable & (1 << *key))))
+               return -22;
+
        if (bpf_map_update_elem(map, key, &init, 0))
                return -1;
 
@@ -93,12 +107,12 @@ static int test_hmap_elem_callback(void *map, int *key,
        if (bpf_wq_set_callback(wq, callback_fn, 0))
                return -4;
 
+       if (bpf_wq_start(wq, 0))
+               return -5;
+
        return 0;
 }
 
-__u32 ok;
-__u32 ok_sleepable;
-
 /* callback for non sleepable workqueue */
 static int wq_callback(void *map, int *key, struct bpf_wq *work)
 {