Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Benjamin Tissoires <bentiss@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	 Martin KaFai Lau <martin.lau@linux.dev>,
	 Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	 John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	 Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	 Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
	 Shuah Khan <shuah@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org,
	Benjamin Tissoires <bentiss@kernel.org>
Subject: [PATCH bpf-next v2 14/16] selftests/bpf: add checks for bpf_wq_set_callback()
Date: Sat, 20 Apr 2024 11:09:14 +0200	[thread overview]
Message-ID: <20240420-bpf_wq-v2-14-6c986a5a741f@kernel.org> (raw)
In-Reply-To: <20240420-bpf_wq-v2-0-6c986a5a741f@kernel.org>

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>

---

changes in v2:
- removed mark_precise checks
- adapted without BPF_F_WQ_SLEEPABLE
---
 tools/testing/selftests/bpf/bpf_experimental.h     |  5 ++
 .../selftests/bpf/bpf_testmod/bpf_testmod.c        |  5 ++
 .../selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h  |  1 +
 tools/testing/selftests/bpf/progs/wq.c             | 41 +++++++++++---
 tools/testing/selftests/bpf/progs/wq_failures.c    | 66 ++++++++++++++++++++++
 5 files changed, 111 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
index 785b91b629be..b80b39f76034 100644
--- a/tools/testing/selftests/bpf/bpf_experimental.h
+++ b/tools/testing/selftests/bpf/bpf_experimental.h
@@ -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
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
index 39ad96a18123..eb2b78552ca2 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
@@ -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)
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h
index 7c664dd61059..ce5cd763561c 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h
@@ -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);
diff --git a/tools/testing/selftests/bpf/progs/wq.c b/tools/testing/selftests/bpf/progs/wq.c
index f92466eb8fb1..f746296d32b1 100644
--- a/tools/testing/selftests/bpf/progs/wq.c
+++ b/tools/testing/selftests/bpf/progs/wq.c
@@ -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;
 }
 
@@ -96,7 +123,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")
@@ -107,7 +134,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")
@@ -118,7 +145,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")
@@ -130,7 +157,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")
@@ -141,5 +168,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);
 }
diff --git a/tools/testing/selftests/bpf/progs/wq_failures.c b/tools/testing/selftests/bpf/progs/wq_failures.c
index db7015c7d541..4cbdb425f223 100644
--- a/tools/testing/selftests/bpf/progs/wq_failures.c
+++ b/tools/testing/selftests/bpf/progs/wq_failures.c
@@ -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;
+}

-- 
2.44.0


  parent reply	other threads:[~2024-04-20  9:10 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-20  9:09 [PATCH bpf-next v2 00/16] Introduce bpf_wq Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 01/16] bpf: make timer data struct more generic Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 02/16] bpf: replace bpf_timer_init with a generic helper Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 03/16] bpf: replace bpf_timer_set_callback " Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 04/16] bpf: replace bpf_timer_cancel_and_free " Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 05/16] bpf: add support for bpf_wq user type Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 06/16] tools: sync include/uapi/linux/bpf.h Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 07/16] bpf: verifier: bail out if the argument is not a map Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 08/16] bpf: add support for KF_ARG_PTR_TO_WORKQUEUE Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 09/16] bpf: allow struct bpf_wq to be embedded in arraymaps and hashmaps Benjamin Tissoires
2024-04-24  2:51   ` Alexei Starovoitov
2024-04-20  9:09 ` [PATCH bpf-next v2 10/16] selftests/bpf: add bpf_wq tests Benjamin Tissoires
2024-04-24  2:56   ` Alexei Starovoitov
2024-04-24 21:07     ` Alexei Starovoitov
2024-04-20  9:09 ` [PATCH bpf-next v2 11/16] bpf: wq: add bpf_wq_init Benjamin Tissoires
2024-04-24  2:55   ` Alexei Starovoitov
2024-04-24 15:06     ` Alexei Starovoitov
2024-04-24 16:14       ` Alexei Starovoitov
2024-04-25  8:50         ` Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 12/16] selftests/bpf: wq: add bpf_wq_init() checks Benjamin Tissoires
2024-04-25  0:28   ` Andrii Nakryiko
2024-04-25  8:53     ` Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 13/16] bpf: wq: add bpf_wq_set_callback_impl Benjamin Tissoires
2024-04-24  2:56   ` Alexei Starovoitov
2024-04-20  9:09 ` Benjamin Tissoires [this message]
2024-04-20  9:09 ` [PATCH bpf-next v2 15/16] bpf: add bpf_wq_start Benjamin Tissoires
2024-04-20  9:09 ` [PATCH bpf-next v2 16/16] selftests/bpf: wq: add bpf_wq_start() checks Benjamin Tissoires
2024-04-24  3:00 ` [PATCH bpf-next v2 00/16] Introduce bpf_wq patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240420-bpf_wq-v2-14-6c986a5a741f@kernel.org \
    --to=bentiss@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=sdf@google.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox