selftests/bpf: add checks for bpf_wq_set_callback()
authorBenjamin Tissoires <bentiss@kernel.org>
Sat, 20 Apr 2024 09:09:14 +0000 (11:09 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 24 Apr 2024 02:46:57 +0000 (19:46 -0700)
We assign the callback and set everything up.
The actual tests of these callbacks will be done when bpf_wq_start() is
available.

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

index 785b91b629be925cc2cc0e2ce0785c3a889c37b9..b80b39f76034f3ff2a969c0bd00cc2945829181f 100644 (file)
@@ -471,4 +471,9 @@ 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_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;
+#define bpf_wq_set_callback(timer, cb, flags) \
+       bpf_wq_set_callback_impl(timer, cb, flags, NULL)
 #endif
index 39ad96a18123f626c311aaa2fdf8213594b7d67d..eb2b78552ca299055341d9e9edad2bdcb509fcab 100644 (file)
@@ -494,6 +494,10 @@ __bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused
        return arg;
 }
 
+__bpf_kfunc void bpf_kfunc_call_test_sleepable(void)
+{
+}
+
 BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids)
 BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
 BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -520,6 +524,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS | KF_RCU)
 BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
 BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
 BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset)
+BTF_ID_FLAGS(func, bpf_kfunc_call_test_sleepable, KF_SLEEPABLE)
 BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids)
 
 static int bpf_testmod_ops_init(struct btf *btf)
index 7c664dd6105972fa1aa15c276172d246809bbf27..ce5cd763561caf74f2bf078f07f4cb6ed25e365f 100644 (file)
@@ -96,6 +96,7 @@ void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;
 void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;
 
 void bpf_kfunc_call_test_destructive(void) __ksym;
+void bpf_kfunc_call_test_sleepable(void) __ksym;
 
 void bpf_kfunc_call_test_offset(struct prog_test_ref_kfunc *p);
 struct prog_test_member *bpf_kfunc_call_memb_acquire(void);
index ed2fe26e14eff4c2168b0993f2e3159a1beaf178..dc301b52f91c788fc3f9a2d42e1868a4e876a3b8 100644 (file)
@@ -49,7 +49,8 @@ struct {
        __type(value, struct elem);
 } lru SEC(".maps");
 
-static int test_elem_callback(void *map, int *key)
+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;
@@ -66,10 +67,14 @@ static int test_elem_callback(void *map, int *key)
        if (bpf_wq_init(wq, map, 0) != 0)
                return -3;
 
+       if (bpf_wq_set_callback(wq, callback_fn, 0))
+               return -4;
+
        return 0;
 }
 
-static int test_hmap_elem_callback(void *map, int *key)
+static int test_hmap_elem_callback(void *map, int *key,
+               int (callback_fn)(void *map, int *key, struct bpf_wq *wq))
 {
        struct hmap_elem init = {}, *val;
        struct bpf_wq *wq;
@@ -85,6 +90,28 @@ static int test_hmap_elem_callback(void *map, int *key)
        if (bpf_wq_init(wq, map, 0) != 0)
                return -3;
 
+       if (bpf_wq_set_callback(wq, callback_fn, 0))
+               return -4;
+
+       return 0;
+}
+
+__u32 ok;
+__u32 ok_sleepable;
+
+/* callback for non sleepable workqueue */
+static int wq_callback(void *map, int *key, struct bpf_wq *work)
+{
+       bpf_kfunc_common_test();
+       ok |= (1 << *key);
+       return 0;
+}
+
+/* callback for sleepable workqueue */
+static int wq_cb_sleepable(void *map, int *key, struct bpf_wq *work)
+{
+       bpf_kfunc_call_test_sleepable();
+       ok_sleepable |= (1 << *key);
        return 0;
 }
 
@@ -95,7 +122,7 @@ long test_call_array_sleepable(void *ctx)
 {
        int key = 0;
 
-       return test_elem_callback(&array, &key);
+       return test_elem_callback(&array, &key, wq_cb_sleepable);
 }
 
 SEC("syscall")
@@ -105,7 +132,7 @@ long test_syscall_array_sleepable(void *ctx)
 {
        int key = 1;
 
-       return test_elem_callback(&array, &key);
+       return test_elem_callback(&array, &key, wq_cb_sleepable);
 }
 
 SEC("tc")
@@ -115,7 +142,7 @@ long test_call_hash_sleepable(void *ctx)
 {
        int key = 2;
 
-       return test_hmap_elem_callback(&hmap, &key);
+       return test_hmap_elem_callback(&hmap, &key, wq_callback);
 }
 
 SEC("tc")
@@ -125,7 +152,7 @@ long test_call_hash_malloc_sleepable(void *ctx)
 {
        int key = 3;
 
-       return test_hmap_elem_callback(&hmap_malloc, &key);
+       return test_hmap_elem_callback(&hmap_malloc, &key, wq_callback);
 }
 
 SEC("tc")
@@ -135,5 +162,5 @@ long test_call_lru_sleepable(void *ctx)
 {
        int key = 4;
 
-       return test_elem_callback(&lru, &key);
+       return test_elem_callback(&lru, &key, wq_callback);
 }
index db7015c7d541a183ea81888882db0731742451b4..4cbdb425f223d3354619a612c486432b321e59ed 100644 (file)
@@ -27,6 +27,20 @@ struct {
        __type(value, struct elem);
 } lru SEC(".maps");
 
+/* callback for non sleepable workqueue */
+static int wq_callback(void *map, int *key, struct bpf_wq *work)
+{
+       bpf_kfunc_common_test();
+       return 0;
+}
+
+/* callback for sleepable workqueue */
+static int wq_cb_sleepable(void *map, int *key, struct bpf_wq *work)
+{
+       bpf_kfunc_call_test_sleepable();
+       return 0;
+}
+
 SEC("tc")
 /* test that bpf_wq_init takes a map as a second argument
  */
@@ -76,3 +90,55 @@ long test_wq_init_wrong_map(void *ctx)
 
        return 0;
 }
+
+SEC("?tc")
+__log_level(2)
+__failure
+/* check that the first argument of bpf_wq_set_callback()
+ * is a correct bpf_wq pointer.
+ */
+__msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */
+__msg("arg#0 doesn't point to a map value")
+long test_wrong_wq_pointer(void *ctx)
+{
+       int key = 0;
+       struct bpf_wq *wq;
+
+       wq = bpf_map_lookup_elem(&array, &key);
+       if (!wq)
+               return 1;
+
+       if (bpf_wq_init(wq, &array, 0))
+               return 2;
+
+       if (bpf_wq_set_callback((void *)&wq, wq_callback, 0))
+               return 3;
+
+       return -22;
+}
+
+SEC("?tc")
+__log_level(2)
+__failure
+/* check that the first argument of bpf_wq_set_callback()
+ * is a correct bpf_wq pointer.
+ */
+__msg(": (85) call bpf_wq_set_callback_impl#") /* anchor message */
+__msg("off 1 doesn't point to 'struct bpf_wq' that is at 0")
+long test_wrong_wq_pointer_offset(void *ctx)
+{
+       int key = 0;
+       struct bpf_wq *wq;
+
+       wq = bpf_map_lookup_elem(&array, &key);
+       if (!wq)
+               return 1;
+
+       if (bpf_wq_init(wq, &array, 0))
+               return 2;
+
+       if (bpf_wq_set_callback((void *)wq + 1, wq_cb_sleepable, 0))
+               return 3;
+
+       return -22;
+}