Skip to contentSkip to conversation

selftests/bpf: Wait for wq callback before checking ok_sleepable

PATCH Applied Jonathan Bastari Thamrin 2 participants 2 messages Nov 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 selftests/bpf: Wait for wq callback before checking ok_sleepable

wq test fails with ok_sleepable reading 0 instead of 2 as workqueue callback hasn't executed before check.

This happens as callback runs later on a kworker and 50us isn't always enough.

Change from 50us sleep to waiting for callback by polling ok_sleepable every 1ms for up to 10s. If the callback never runs, the test fails instead of hanging, similar to ksock_wq.c. If test_run fails, it skips the wait and goes to clean_up since the callback was never queued.

Before (usleep(50)): 3 fails out of 5000 runs
Diagnosing issue (usleep(0)): 23 out of 100 runs
After: 0 out of 2000 runs
Tested using vmtest on x86_64, test pinned to one CPU with yes load

---
Changes in v3:
- Go back to bounded polling on ok_sleepable, similar to
ksock_wq.c, as unbounded wait in v2 can hang if callback never
runs (Alexei)
- Skip the wait if test_run fails, and raise limit to 10s (Alexei)
- Drop the wq_cb_run flag and the progs/wq.c changes
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Wait for callback via a wq_cb_run flag instead of polling with a timeout, similar to test_struct_ops_assoc.c (Kumar)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 tools/testing/selftests/bpf/prog_tests/wq.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/wq.c +9 -5
index 97926e35aaf2..a4f71bc34fa6 100644
@@ -4,10 +4,13 @@
 #include "wq.skel.h"
 #include "wq_failures.skel.h"
 
+#define CALLBACK_WAIT_RETRIES 10000
+#define CALLBACK_WAIT_US 1000
+
 void serial_test_wq(void)
 {
 	struct wq *wq_skel = NULL;
-	int err, prog_fd;
+	int err, prog_fd, i;
 
 	LIBBPF_OPTS(bpf_test_run_opts, topts);
 
@@ -25,11 +28,12 @@ void serial_test_wq(void)
 
 	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 */
+	if (!ASSERT_OK(err, "test_run") || !ASSERT_EQ(topts.retval, 0, "test_run"))
+		goto clean_up;
 
+	/* wait for wq callback to finish and fail instead of hanging if it never runs */
+	for (i = 0; i < CALLBACK_WAIT_RETRIES && !READ_ONCE(wq_skel->bss->ok_sleepable); i++)
+		usleep(CALLBACK_WAIT_US);
 	ASSERT_EQ(wq_skel->bss->ok_sleepable, (1 << 1), "ok_sleepable");
 clean_up:
 	wq__destroy(wq_skel);

---
base-commit: cd5bc55288d4fd999595c8f2af6a5408cbe92a52
change-id: 20261102-wq-poll-fix-e3902f46512c

Best regards,

Signature · 1 line
Jonathan Bastari Thamrin <[email protected]>
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Tue, 03 Nov 2026 10:21:38 +0700 you wrote:

From: Jonathan Bastari Thamrin <[email protected]>

wq test fails with ok_sleepable reading 0 instead of 2 as workqueue callback hasn't executed before check.

This happens as callback runs later on a kworker and 50us isn't always enough.

[...]

Here is the summary with links:
- [bpf-next,v3] selftests/bpf: Wait for wq callback before checking ok_sleepable

    https://git.kernel.org/bpf/bpf-next/c/f2b22071bf84

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html