selftests/bpf: Wait for wq callback before checking ok_sleepable
Subsystems (3)
PATCH v2 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 checking wq_cb_run. wq_cb_sleepable() now increments wq_cb_run after setting ok_sleepable and test loops with sched_yield() until wq_cb_run is set before checking.
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 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 | 5 +++-- tools/testing/selftests/bpf/progs/wq.c | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/wq.c +3 -2
@@ -28,8 +28,9 @@ void serial_test_wq(void)ASSERT_OK (err,"test_run" );ASSERT_EQ (topts.retval,0 ,"test_run" ); -usleep (50 );/* 10 usecs should be enough, but give it extra */ - +/* wait for wq callback to finish */ +while (!READ_ONCE (wq_skel->bss->wq_cb_run)) +sched_yield ();ASSERT_EQ (wq_skel->bss->ok_sleepable, (1 <<1 ),"ok_sleepable" ); clean_up:wq__destroy (wq_skel);
tools/testing/selftests/bpf/progs/wq.c +6 -4
@@ -52,6 +52,7 @@ struct {__u32 ok;__u32 ok_sleepable; +__u32 wq_cb_run;static int test_elem_callback (void *map,int *key,int (callback_fn )(void *map,int *key,void *value))
@@ -130,11 +131,12 @@ static int wq_cb_sleepable(void *map, int *key, void *value)struct elem *data = (struct elem *)value;int offset = data->ok_offset; -if (*key != offset) -return 0 ; +if (*key == offset) { +bpf_kfunc_call_test_sleepable (); + ok_sleepable |= (1 << offset); + } -bpf_kfunc_call_test_sleepable (); - ok_sleepable |= (1 << offset); + wq_cb_run++;return 0 ; }
---
base-commit: a30c772627b2ef4bc715332b1b04938a914556cd
change-id: 20261005-wq-poll-fix-f46c47dbaf03
Best regards,
Signature · 1 line
Jonathan Bastari Thamrin <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/bpf: missing error jump before spin loop in serial_test_wq() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: missing error jump before spin loop in serial_test_wq() --
commit b67542c75142d5bd693ad5ed6fa8a0252549f42f
Author: Jonathan Bastari Thamrin <[email protected]>
selftests/bpf: Wait for wq callback before checking ok_sleepable
This commit replaces the 50us sleep in serial_test_wq() with a spin loop waiting for a wq_cb_run counter to be incremented by wq_cb_sleepable() before checking ok_sleepable.
tools/testing/selftests/bpf/prog_tests/wq.c void serial_test_wq(void) · line 33
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/wq.c +3
-2@@ -28,8 +28,9 @@ void serial_test_wq(void)ASSERT_OK (err,"test_run" );ASSERT_EQ (topts.retval,0 ,"test_run" ); -usleep (50 );/* 10 usecs should be enough, but give it extra */ - +/* wait for wq callback to finish */ +while (!READ_ONCE (wq_skel->bss->wq_cb_run)) +sched_yield ();
[Severity: Medium]
Can serial_test_wq() hang indefinitely here if bpf_prog_test_run_opts() fails or if test_syscall_array_sleepable() -> test_elem_callback() in tools/testing/selftests/bpf/progs/wq.c returns a non-zero error in topts.retval before bpf_wq_start() schedules wq_cb_sleepable()?
Since ASSERT_OK() and ASSERT_EQ() only record the failure and evaluate to false without returning or jumping to clean_up, execution falls through into the while (!READ_ONCE(wq_skel->bss->wq_cb_run)) loop even when wq_cb_sleepable() was never queued.
Should the assertions jump to clean_up on failure before entering the wait loop, for example:
if (!ASSERT_OK(err, "test_run") || !ASSERT_EQ(topts.retval, 0, "test_run")) goto clean_up;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Review
Quoted 8 lines · On Sun, Nov 01, 2026 at 06:36 PM Jonathan Bastari Thamrin via B4 Relay…
(quoted diff) +3
-2ASSERT_OK(err, "test_run"); ASSERT_EQ(topts.retval, 0, "test_run"); - usleep(50); /* 10 usecs should be enough, but give it extra */ - + /* wait for wq callback to finish */ + while (!READ_ONCE(wq_skel->bss->wq_cb_run)) + sched_yield();
This loop can spin forever.
The two ASSERTs above it don't stop the test. When retval != 0 nothing starts the wq and wq_cb_run stays 0.
what goes wrong today with the patch ------------------------------- ------ -------------- test_run fails or retval != 0 FAIL hang the callback doesn't run FAIL hang the callback gets a wrong key FAIL FAIL
The second row is what this test is for.
The watchdog doesn't help. watchdog_timer_func() sends one SIGSEGV. crash_handler() prints the stack and returns. The loop goes on.
Waiting for the callback is right. Waiting forever is not. Keep the loop bounded, like prog_tests/ksock_wq.c.
Then there is no need for wq_cb_run and for the changes in progs/wq.c.
Something like this (not even compile tested):
tools/testing/selftests/bpf/prog_tests/wq.c +7 -5
@@ -7,7 +7,7 @@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 +25,13 @@ 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; +/* The callback runs in a kworker. Fail instead of a hang if it doesn't. */ +for (i =0 ; i <10000 && !READ_ONCE (wq_skel->bss->ok_sleepable); i++) +usleep (1000 );ASSERT_EQ (wq_skel->bss->ok_sleepable, (1 <<1 ),"ok_sleepable" ); clean_up:wq__destroy (wq_skel);
pw-bot: cr