sched_ext: selftest infrastructure fixes
Subsystems (2)
v2 cover sched_ext: selftest infrastructure fixes
From: Tao Cui <[email protected]>
The sched_ext selftests pass vacuously or hang in several failure
scenarios: a scheduler that dies mid-test freezes the state the test asserts on, EXIT_KIND() silently maps missing BTF enumerators to SCX_EXIT_NONE, waits for scheduler exit are unbounded, attach failures in the reload stress test are swallowed, and an error path in reload_loop destroys the skeleton under a still-running thread.
This is v2 of the series posted as [1]; it applies the review feedback from Tejun (and the sashiko bot findings, which overlapped):
- P1: use SCX_FAIL() inside the error branch.
- P2: name ebeca1f930ea in the description (and fix the wrong attribution the v1 description carried).
- P3: the v1 premise was wrong - struct scx_exit_info carries the enum, so the BTF always has the type and the enumerators the tests use exist on all sched_ext kernels. The patch is kept but
reshaped: EXIT_KIND() is now an alias of SCX_KIND_VAL() (single implementation), the remaining kind != EXIT_KIND(SCX_EXIT_NONE) sites are converted to UEI_EXITED(), invalid_one() resolves error_kind before spawning the gated worker, and the v1 <unistd.h> include is not needed here.
- P4: SCX_ASSERT_ALIVE() prints uei.msg too.
- P5: split in two. exit.c now destroys the link and the skeleton on its timeout path (a scheduler stuck unexited no longer stays attached and makes every later test fail with EBUSY); reload_loop drops its dead reporting arm and the always-on EBUSY printf, and the failure bar is now "at least one successful attach per thread". The two #defines became an enum and stray blank lines after setup() are gone.
Base: tj/sched_ext for-next at 8ef2c4fe917a ("Merge branch 'for-7.3-fixes' into for-next"), rebased after the review round. v1 was based on the then-tip f55a5ae74ca6; the rebase is a pure replay (the only selftest-side upstream change in between is a dequeue_remote.c build fix outside the files touched here).
Testing: full sched_ext selftest suite (QEMU/KVM x86_64, 4 vcpus, serial console) on a kernel built from this base: a runner built from this series and a runner built from the plain base pass identically, 38 passed / 2 skipped / 0 failed each (the skips are
environment-dependent: enq_blocked requires PROXY_EXEC and nohz_tick requires NOHZ_FULL; kick_coalesce passes since the base kernel has lazy preemption). On top of that, A/B checks: a scheduler whose exit point never fires wedges a pre-series runner forever in exit's unbounded wait, while with patch 5 the runner reports "Timed out waiting for scheduler to exit" after ~10s, destroys the link and the skeleton, and every subsequent test still attaches and passes (the stuck A/B ran on the f55a5ae74ca6 pre-rebase base with identical series content; the remaining A/Bs were run the same way on the v1 base and their code paths are unchanged here); a synthetic BTF without the SCX_EXIT_UNREG_BPF enumerator fails loudly with patch 3 and passes silently without it; an injected scheduler death reports kind, exit code and uei.msg through SCX_ASSERT_ALIVE(); under reload stress with in-flight teardown, patch 6 classifies the observed 1125-1233 EBUSY failures and passes as long as each thread completed at least one attach.
[1] https://lore.kernel.org/all/[email protected]/
Tao Cui (6):
selftests/sched_ext: Fix reload_loop error-path use-after-free
sched_ext: Add SCX_ECODE_RSN_CGROUP_OFFLINE to user_exit_info.h selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF selftests/sched_ext: Add SCX_ASSERT_ALIVE and adopt it selftests/sched_ext: Bound exit waits selftests/sched_ext: Report reload_loop attach failures
tools/sched_ext/include/scx/user_exit_info.h | 1 + .../selftests/sched_ext/allowed_cpus.c | 2 +- .../selftests/sched_ext/cgroup_nr_cpus.c | 2 +- .../selftests/sched_ext/create_dsq.bpf.c | 8 +++ .../testing/selftests/sched_ext/create_dsq.c | 2 + .../selftests/sched_ext/cyclic_kick_wait.c | 2 +- .../testing/selftests/sched_ext/enq_blocked.c | 2 +- tools/testing/selftests/sched_ext/exit.c | 8 ++- tools/testing/selftests/sched_ext/hotplug.c | 12 ++-- .../sched_ext/init_enable_count.bpf.c | 8 +++ .../selftests/sched_ext/init_enable_count.c | 13 +++++ tools/testing/selftests/sched_ext/kick.c | 8 ++- .../testing/selftests/sched_ext/maximal.bpf.c | 6 +- tools/testing/selftests/sched_ext/maximal.c | 2 + tools/testing/selftests/sched_ext/nohz_tick.c | 6 +- tools/testing/selftests/sched_ext/prog_run.c | 7 ++- .../testing/selftests/sched_ext/reload_loop.c | 49 +++++++++++++--- tools/testing/selftests/sched_ext/scx_test.h | 57 +++++++++++++++++-- 18 files changed, 162 insertions(+), 33 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/6 selftests/sched_ext: Fix reload_loop error-path use-after-free
From: Tao Cui <[email protected]>
If the second pthread_create() fails, run() returns and the runner calls cleanup, which destroys the skeleton while the first reload thread is still running do_reload_loop() and dereferencing its maps - a use-after-free on the error path.
Set force_exit and join the first thread before failing.
--- tools/testing/selftests/sched_ext/reload_loop.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/sched_ext/reload_loop.c +5 -1
@@ -53,7 +53,11 @@ static enum scx_test_status run(void *ctx)SCX_FAIL_IF (err,"Failed to create thread 0" ); err =pthread_create (&threads[1 ],NULL , do_reload_loop,NULL ); -SCX_FAIL_IF (err,"Failed to create thread 1" ); +if (err) { + force_exit = true; +pthread_join (threads[0 ],NULL ); +SCX_FAIL ("Failed to create thread 1" ); + }SCX_FAIL_IF (pthread_join (threads[0 ], &ret),"thread 0 failed" );SCX_FAIL_IF (pthread_join (threads[1 ], &ret),"thread 1 failed" );
Signature · 1 line
2.53.0
PATCH v2 2/6 sched_ext: Add SCX_ECODE_RSN_CGROUP_OFFLINE to user_exit_info.h
In reply to Tao CuiFrom: Tao Cui <[email protected]>
The kernel-side scx_exit_code enum gained SCX_ECODE_RSN_CGROUP_OFFLINE in commit ebeca1f930ea ("sched_ext: Introduce cgroup sub-sched support"), which exits a scheduler when its cgroup goes offline, but the tools-side user_exit_info.h was not updated. Userspace cannot decode exit codes carrying this reason.
Add the missing enum value.
--- tools/sched_ext/include/scx/user_exit_info.h | 1 + 1 file changed, 1 insertion(+)
tools/sched_ext/include/scx/user_exit_info.h +1 -0
@@ -52,6 +52,7 @@enum scx_exit_code {/* Reasons */ SCX_ECODE_RSN_HOTPLUG =1LLU <<32 , +SCX_ECODE_RSN_CGROUP_OFFLINE =2LLU <<32 ,/* Actions */ SCX_ECODE_ACT_RESTART =1LLU <<48 ,
Signature · 1 line
2.53.0
PATCH v2 3/6 selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF
In reply to Tao CuiFrom: Tao Cui <[email protected]>
EXIT_KIND() resolved scx_exit_kind values through __COMPAT_ENUM_OR_ZERO(), which maps a missing enumerator to 0 (== SCX_EXIT_NONE). struct scx_exit_info carries the enum itself, so the BTF of any loaded scheduler always has the type, and every enumerator the tests resolve today exists on all sched_ext kernels: the fallback got the value right, and the single comparison that could pass vacuously was the kick.c error poll.
Still, a silently-zeroed lookup is a trap. EXIT_KIND() is now an alias of SCX_KIND_VAL(), which fails the test loudly when the enumerator is missing. Convert the remainingkind != EXIT_KIND(SCX_EXIT_NONE) checks (allowed_cpus, cgroup_nr_cpus, cyclic_kick_wait and enq_blocked) to UEI_EXITED() so they keep their semantics without a BTF lookup, and hoist kick.c invalid_one()'s error_kind resolution above the gated-worker spawn so a failing lookup cannot bypass its cleanup path. The <unistd.h> include that the v1 posting added is not needed here; compat.h already pulls it in.
--- .../selftests/sched_ext/allowed_cpus.c | 2 +- .../selftests/sched_ext/cgroup_nr_cpus.c | 2 +- .../selftests/sched_ext/cyclic_kick_wait.c | 2 +- .../testing/selftests/sched_ext/enq_blocked.c | 2 +- tools/testing/selftests/sched_ext/kick.c | 8 ++++--- tools/testing/selftests/sched_ext/nohz_tick.c | 6 ++--- tools/testing/selftests/sched_ext/scx_test.h | 22 ++++++++++++++----- 7 files changed, 28 insertions(+), 16 deletions(-)
tools/testing/selftests/sched_ext/allowed_cpus.c +1 -1
@@ -149,7 +149,7 @@ static enum scx_test_status run(void *ctx)/* Just sleeping is fine, plenty of scheduling events happening. */ sleep (1 ); -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) { +if (UEI_EXITED (skel, uei)) {SCX_ERR ("Scheduler exited unexpectedly" ); goto out; }
tools/testing/selftests/sched_ext/cgroup_nr_cpus.c +1 -1
@@ -400,7 +400,7 @@ static enum scx_test_status run(void *arg) !wait_nr_cpus(&ctx, ctx.leaf, nr_child, "leaf after cpuset disable")) goto out; -if (ctx.skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) { +if (UEI_EXITED (ctx.skel, uei)) {SCX_ERR ("Scheduler exited unexpectedly" ); goto out; }
tools/testing/selftests/sched_ext/cyclic_kick_wait.c +1 -1
@@ -152,7 +152,7 @@ static enum scx_test_status run(void *ctx)sleep (5 ); -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) { +if (UEI_EXITED (skel, uei)) {SCX_ERR ("Scheduler exited unexpectedly (kind=%llu code=%lld)" , (unsigned long long )skel->data->uei.kind, (long long )skel->data->uei.exit_code);
tools/testing/selftests/sched_ext/enq_blocked.c +1 -1
@@ -844,7 +844,7 @@ static enum scx_test_status run_one(bool enq_blocked, bool cross_cpu, } } -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) { +if (UEI_EXITED (skel, uei)) {SCX_ERR ("Scheduler exited unexpectedly (kind=%llu code=%lld)" , (unsigned long long )skel->data->uei.kind, (long long )skel->data->uei.exit_code);
tools/testing/selftests/sched_ext/kick.c +5 -3
@@ -97,7 +97,7 @@ static bool wait_for_state(struct kick *skel, u32 wanted)for (i =0 ; i <WAIT_LOOPS ; i++) {if (__atomic_load_n (&skel->bss->state, __ATOMIC_ACQUIRE) == wanted)return true; -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) +if (UEI_EXITED (skel, uei))return false;usleep (1000 ); }
@@ -111,7 +111,7 @@ static bool wait_for_counter(struct kick *skel, const u64 *counter, u64 wanted)for (i =0 ; i <WAIT_LOOPS ; i++) {if (__atomic_load_n (counter, __ATOMIC_ACQUIRE) >= wanted)return true; -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) +if (UEI_EXITED (skel, uei))return false;usleep (1000 ); }
@@ -421,7 +421,9 @@ static enum scx_test_status invalid_one(struct kick_ctx *ctx, u32 scenario)enum scx_test_status ret =SCX_TEST_FAIL ;int cpu = ctx->target_cpu;int i; +u64 error_kind; + error_kind =EXIT_KIND (SCX_EXIT_ERROR ); victim =scx_test_spawn_gated_worker (cpu, false);if (victim.pid <0 )return SCX_TEST_FAIL ;
@@ -440,7 +442,7 @@ static enum scx_test_status invalid_one(struct kick_ctx *ctx, u32 scenario) goto out;for (i =0 ; i <WAIT_LOOPS ; i++) { -if (skel->data->uei.kind ==EXIT_KIND (SCX_EXIT_ERROR )) { +if (UEI_EXITED (skel, uei) == error_kind) { ret =SCX_TEST_PASS ;break ; }
tools/testing/selftests/sched_ext/nohz_tick.c +3 -3
@@ -165,7 +165,7 @@ static bool wait_for_counter(struct nohz_tick *skel, const u64 *counter, u64 valfor (elapsed =0 ; elapsed < timeout_ms; elapsed++) {if (__atomic_load_n (counter, __ATOMIC_RELAXED) >= value)return true; -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) +if (UEI_EXITED (skel, uei))return false;usleep (1000 ); }
@@ -182,7 +182,7 @@ static bool wait_for_tick_stop(struct nohz_tick *skel, const u64 *counter, int tu64 curr;usleep (1000 ); -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) +if (UEI_EXITED (skel, uei))return false; curr =__atomic_load_n (counter, __ATOMIC_RELAXED);if (curr == prev) {
@@ -461,7 +461,7 @@ static enum scx_test_status run(void *ctx_ptr)scx_test_stop_gated_worker (&challenger); check_exit: -if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_NONE )) { +if (UEI_EXITED (skel, uei)) {SCX_ERR ("Scheduler exited unexpectedly (kind=%llu code=%lld)" , (unsigned long long )skel->data->uei.kind, (long long )skel->data->uei.exit_code);
tools/testing/selftests/sched_ext/scx_test.h +16 -6
@@ -18,8 +18,6 @@ enum scx_test_status { SCX_TEST_FAIL, }; -#define EXIT_KIND (__ent) __COMPAT_ENUM_OR_ZERO("scx_exit_kind", #__ent) -struct scx_test {/** * name - The name of the testcase.
@@ -119,13 +117,25 @@ void scx_test_register(struct scx_test *test); (s64 )__val; \ }) +/* +* SCX_KIND_VAL - Resolve an scx_exit_kind enumerator from kernel BTF. +* +* Unlike __COMPAT_ENUM_OR_ZERO(), a missing enumerator fails the test +* loudly instead of silently resolving to 0 (== SCX_EXIT_NONE), which +* would make the comparison vacuous. +*/ #define SCX_KIND_VAL (__kind) ({ \ - u64 __val = 0; \ - bool __found = false; \ + u64 __val = 0; \ \ - __found = __COMPAT_read_enum("scx_exit_kind", #__kind, &__val); \ - SCX_ASSERT(__found); \ + if (!__COMPAT_read_enum("scx_exit_kind", #__kind, &__val)) \ + SCX_FAIL("scx_exit_kind.%s not found in kernel BTF", #__kind); \ __val; \ }) +/* +* EXIT_KIND - Alias of SCX_KIND_VAL() for resolving scx_exit_kind +* enumerator values, kept for readability at the EXIT_KIND() call sites. +*/ +#define EXIT_KIND (__ent) SCX_KIND_VAL(__ent) +#endif // # __SCX_TEST_H__
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 3/6] selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/sched_ext: EXIT_KIND() early return leaks bpf_link and breaks test suite --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/sched_ext: EXIT_KIND() early return leaks bpf_link and breaks test suite --
commit dc88ff2863ae13f6a94ae129f38a8ba739ac1964
Author: Tao Cui <[email protected]>
selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF
EXIT_KIND() resolved scx_exit_kind values through __COMPAT_ENUM_OR_ZERO(), which maps a missing enumerator to 0 (== SCX_EXIT_NONE). struct scx_exit_info carries the enum itself, so the BTF of any loaded scheduler always has the type, and every enumerator the tests resolve today exists on all sched_ext kernels: the fallback got the value right, and the single comparison that could pass vacuously was the kick.c error poll.
Quoted 4 lines
tools/testing/selftests/sched_ext/scx_test.h +0
-0
[ ... ]
Quoted 20 lines
tools/testing/selftests/sched_ext/scx_test.h +10
-4@@ -119,13 +117,25 @@ void scx_test_register(struct scx_test *test); (s64 )__val; \ }) +/* +* SCX_KIND_VAL - Resolve an scx_exit_kind enumerator from kernel BTF. +* +* Unlike __COMPAT_ENUM_OR_ZERO(), a missing enumerator fails the test +* loudly instead of silently resolving to 0 (== SCX_EXIT_NONE), which +* would make the comparison vacuous. +*/ #define SCX_KIND_VAL (__kind) ({ \ - u64 __val = 0; \ - bool __found = false; \ + u64 __val = 0; \ \ - __found = __COMPAT_read_enum("scx_exit_kind", #__kind, &__val); \ - SCX_ASSERT(__found); \ + if (!__COMPAT_read_enum("scx_exit_kind", #__kind, &__val)) \ + SCX_FAIL("scx_exit_kind.%s not found in kernel BTF", #__kind); \
tools/testing/selftests/sched_ext/scx_test.h void scx_test_register(struct scx_test *test); · line 141
Quoted 10 lines
tools/testing/selftests/sched_ext/scx_test.h +6
-0__val; \ }) +/* +* EXIT_KIND - Alias of SCX_KIND_VAL() for resolving scx_exit_kind +* enumerator values, kept for readability at the EXIT_KIND() call sites. +*/ +#define EXIT_KIND (__ent) SCX_KIND_VAL(__ent) +#endif // # __SCX_TEST_H__
[Severity: Medium]
When EXIT_KIND() fails to resolve the enum, SCX_FAIL() triggers a hidden return SCX_TEST_FAIL. Does this early return leak resources and break the test suite?
For example, in tools/testing/selftests/sched_ext/exit.c:run():
link = bpf_map__attach_struct_ops(skel->maps.exit_ops);
if (!link) {
SCX_ERR("Failed to attach scheduler");
exit__destroy(skel);
return SCX_TEST_FAIL;
}
/* Assumes uei.kind is written last */
while (skel->data->uei.kind == EXIT_KIND(SCX_EXIT_NONE))
sched_yield();
SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG_BPF));
SCX_EQ(skel->data->uei.exit_code, tc);
sprintf(buf, "%d", tc);
SCX_ASSERT(!strcmp(skel->data->uei.msg, buf));
bpf_link__destroy(link);
exit__destroy(skel);If EXIT_KIND() fails inside the SCX_EQ() assertion or the while loop, it returns early and bypasses bpf_link__destroy() and exit__destroy(), leaking both the BPF link and skeleton.
Since runner.c executes tests serially in a single process, could the open BPF link keep the scheduler active in the kernel, causing all subsequent tests to fail with -EBUSY?
This issue also appears to affect multiple other tests, such as ddsp_bogus_dsq_fail.c, prog_run.c, and enq_select_cpu.c, where EXIT_KIND() is used inside assertions while a struct bpf_link *link is held. While follow-up patches in this series refactor loops to use SCX_TIMED_WAIT_UEI, they do not seem to resolve these specific assertion leaks.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/6 selftests/sched_ext: Add SCX_ASSERT_ALIVE and adopt it
In reply to Tao CuiFrom: Tao Cui <[email protected]>
A scheduler that dies mid-test freezes the state that the test is about to assert on, so the assertions can pass vacuously. Add SCX_ASSERT_ALIVE(), which fails the test with the recorded exit kind, exit code and message, and use it in maximal, create_dsq and init_enable_count. Existing open-coded checks keep their goto-based cleanup and are not converted. init_enable_count also clears the uei between its two attach cycles, since the clean unregister of the first cycle would otherwise trip the check of the second.
--- .../selftests/sched_ext/create_dsq.bpf.c | 8 ++++++++ tools/testing/selftests/sched_ext/create_dsq.c | 2 ++ .../sched_ext/init_enable_count.bpf.c | 8 ++++++++ .../selftests/sched_ext/init_enable_count.c | 13 +++++++++++++ .../testing/selftests/sched_ext/maximal.bpf.c | 6 +++++- tools/testing/selftests/sched_ext/maximal.c | 2 ++ tools/testing/selftests/sched_ext/scx_test.h | 18 ++++++++++++++++++ 7 files changed, 56 insertions(+), 1 deletion(-)
tools/testing/selftests/sched_ext/create_dsq.bpf.c +8 -0
@@ -12,6 +12,8 @@ char _license[] SEC("license") = "GPL";u32 nr_lifecycle_tests; +UEI_DEFINE (uei); +void BPF_STRUCT_OPS (create_dsq_exit_task ,struct task_struct *p,struct scx_exit_task_args *args) {
@@ -84,10 +86,16 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(create_dsq_init)return 0 ; } +void BPF_STRUCT_OPS (create_dsq_exit ,struct scx_exit_info *ei) +{ +UEI_RECORD (uei, ei); +} + SEC(".struct_ops.link")struct sched_ext_ops create_dsq_ops = { .init_task = (void *) create_dsq_init_task, .exit_task = (void *) create_dsq_exit_task, .init = (void *) create_dsq_init, + .exit = (void *) create_dsq_exit, .name ="create_dsq" , };
tools/testing/selftests/sched_ext/create_dsq.c +2 -0
@@ -35,6 +35,8 @@ static enum scx_test_status run(void *ctx)return SCX_TEST_FAIL ; } +SCX_ASSERT_ALIVE (skel, uei); +bpf_link__destroy (link);SCX_EQ (skel->bss->nr_lifecycle_tests,1024 );
tools/testing/selftests/sched_ext/init_enable_count.bpf.c +8 -0
@@ -15,6 +15,8 @@ char _license[] SEC("license") = "GPL";u64 init_task_cnt, exit_task_cnt, enable_cnt, disable_cnt;u64 init_fork_cnt, init_transition_cnt; +UEI_DEFINE (uei); +s32 BPF_STRUCT_OPS_SLEEPABLE (cnt_init_task ,struct task_struct *p,struct scx_init_task_args *args) {
@@ -43,11 +45,17 @@ void BPF_STRUCT_OPS(cnt_disable, struct task_struct *p)__sync_fetch_and_add (&disable_cnt,1 ); } +void BPF_STRUCT_OPS (cnt_exit ,struct scx_exit_info *ei) +{ +UEI_RECORD (uei, ei); +} + SEC(".struct_ops.link")struct sched_ext_ops init_enable_count_ops = { .init_task = (void *) cnt_init_task, .exit_task = (void *) cnt_exit_task, .enable = (void *) cnt_enable, .disable = (void *) cnt_disable, + .exit = (void *) cnt_exit, .name ="init_enable_count" , };
tools/testing/selftests/sched_ext/init_enable_count.c +13 -0
@@ -5,6 +5,7 @@* Copyright (c) 2023 Tejun Heo <[email protected]> */ #include <signal.h> +#include <string.h> #include <stdio.h> #include <unistd.h> #include <sched.h>
@@ -72,7 +73,17 @@ static enum scx_test_status run_test(bool global)close (pipe_fds[1 ]);signal (SIGCHLD ,SIG_DFL ); +SCX_ASSERT_ALIVE (skel, uei); +bpf_link__destroy (link); + +/* +* The clean unregister above is recorded in uei and would +* otherwise trip the liveness check of the second cycle, which +* reuses this skeleton. +*/ +memset (&skel->data->uei,0 ,sizeof (skel->data->uei)); +SCX_GE (skel->bss->init_task_cnt, num_pre_forks);SCX_GE (skel->bss->exit_task_cnt, num_pre_forks);
@@ -124,6 +135,8 @@ static enum scx_test_status run_test(bool global) status); } +SCX_ASSERT_ALIVE (skel, uei); +bpf_link__destroy (link);SCX_GE (skel->bss->init_task_cnt,2 * num_children);
tools/testing/selftests/sched_ext/maximal.bpf.c +5 -1
@@ -14,6 +14,8 @@ char _license[] SEC("license") = "GPL";#define DSQ_ID 0 +UEI_DEFINE (uei); +s32 BPF_STRUCT_OPS (maximal_select_cpu ,struct task_struct *p,s32 prev_cpu,u64 wake_flags) {
@@ -132,7 +134,9 @@ s32 BPF_STRUCT_OPS_SLEEPABLE(maximal_init) }void BPF_STRUCT_OPS (maximal_exit ,struct scx_exit_info *info) -{} +{ +UEI_RECORD (uei, info); +} SEC(".struct_ops.link")struct sched_ext_ops maximal_ops = {
tools/testing/selftests/sched_ext/maximal.c +2 -0
@@ -35,6 +35,8 @@ static enum scx_test_status run(void *ctx) link =bpf_map__attach_struct_ops (skel->maps.maximal_ops);SCX_FAIL_IF (!link,"Failed to attach scheduler" ); +SCX_ASSERT_ALIVE (skel, uei); +bpf_link__destroy (link);return SCX_TEST_PASS ;
tools/testing/selftests/sched_ext/scx_test.h +18 -0
@@ -108,6 +108,24 @@ void scx_test_register(struct scx_test *test);#define SCX_ASSERT (_x) SCX_FAIL_IF(!(_x), "Expected %s to be true (%lu)", \ #_x, (u64)(_x)) +/* +* SCX_ASSERT_ALIVE - Fail the test if the scheduler under test exited +* on its own between attach and this check, e.g. from a runtime error. +* A scheduler that dies mid-test freezes whatever state the test is +* about to assert on, which can make the assertions pass vacuously. +* uei.msg carries the actual error text, so print it alongside the +* recorded kind and exit code. +*/ +#define SCX_ASSERT_ALIVE (__skel, __uei) \ + do { \ + if (UEI_EXITED(__skel, __uei)) \ + SCX_FAIL("Scheduler exited unexpectedly" \ + " (kind=%llu code=%lld msg='%s')", \ + (unsigned long long)(__skel)->data->__uei.kind, \ + (long long)(__skel)->data->__uei.exit_code, \ + (__skel)->data->__uei.msg); \ + } while (0) +#define SCX_ECODE_VAL (__ecode) ({ \ u64 __val = 0; \ bool __found = false; \
Signature · 1 line
2.53.0
PATCH v2 5/6 selftests/sched_ext: Bound exit waits
In reply to Tao CuiFrom: Tao Cui <[email protected]>
exit, prog_run and hotplug wait for the scheduler under test to exit with no time bound; a scheduler that never exits hangs the runner instead of failing the test. Bound the waits to ~10s (iteration-based) with the new SCX_TIMED_WAIT_UEI() helper, which stretches on a loaded system instead of failing early.
exit.c returns straight from its timeout path, which would leave the link and the skeleton alive and the unexited scheduler attached for the rest of the runner run - every later test would then fail to attach with EBUSY and bury the real failure. Destroy both before failing, like prog_run and hotplug already destroy the link.
--- tools/testing/selftests/sched_ext/exit.c | 8 ++++++-- tools/testing/selftests/sched_ext/hotplug.c | 12 ++++++++---- tools/testing/selftests/sched_ext/prog_run.c | 7 +++++-- tools/testing/selftests/sched_ext/scx_test.h | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-)
tools/testing/selftests/sched_ext/exit.c +6 -2
@@ -43,8 +43,12 @@ static enum scx_test_status run(void *ctx) }/* Assumes uei.kind is written last */ -while (skel->data->uei.kind ==EXIT_KIND (SCX_EXIT_NONE )) -sched_yield (); +if (!SCX_TIMED_WAIT_UEI (skel, uei,10000 )) { +SCX_ERR ("Timed out waiting for scheduler to exit" ); +bpf_link__destroy (link); +exit__destroy (skel); +return SCX_TEST_FAIL ; + }SCX_EQ (skel->data->uei.kind,EXIT_KIND (SCX_EXIT_UNREG_BPF ));SCX_EQ (skel->data->uei.exit_code, tc);
tools/testing/selftests/sched_ext/hotplug.c +8 -4
@@ -85,8 +85,10 @@ static enum scx_test_status test_hotplug(bool onlining, bool cbs_defined)if (toggle_online_status (onlining ?1 :0 )) goto out_destroy_link; -while (!UEI_EXITED (skel, uei)) -sched_yield (); +if (!SCX_TIMED_WAIT_UEI (skel, uei,10000 )) { +SCX_ERR ("Timed out waiting for scheduler to exit" ); + goto out_destroy_link; + }SCX_EQ (skel->data->uei.kind, kind);SCX_EQ (UEI_REPORT (skel, uei), code);
@@ -130,8 +132,10 @@ static enum scx_test_status test_hotplug_attach(void) goto out_destroy_link;SCX_ASSERT (link); -while (!UEI_EXITED (skel, uei)) -sched_yield (); +if (!SCX_TIMED_WAIT_UEI (skel, uei,10000 )) { +SCX_ERR ("Timed out waiting for scheduler to exit" ); + goto out_destroy_link; + } kind =SCX_KIND_VAL (SCX_EXIT_UNREG_KERN ); code = SCX_ECODE_VAL(SCX_ECODE_ACT_RESTART) |
tools/testing/selftests/sched_ext/prog_run.c +5 -2
@@ -55,8 +55,11 @@ static enum scx_test_status run(void *ctx) }/* Assumes uei.kind is written last */ -while (skel->data->uei.kind ==EXIT_KIND (SCX_EXIT_NONE )) -sched_yield (); +if (!SCX_TIMED_WAIT_UEI (skel, uei,10000 )) { +SCX_ERR ("Timed out waiting for scheduler to exit" ); + status =SCX_TEST_FAIL ; + goto out; + }if (skel->data->uei.kind !=EXIT_KIND (SCX_EXIT_UNREG_BPF )) { SCX_ERR("Unexpected exit kind: %llu",
tools/testing/selftests/sched_ext/scx_test.h +17 -0
@@ -126,6 +126,23 @@ void scx_test_register(struct scx_test *test); (__skel)->data->__uei.msg); \ }while (0 ) +/* +* SCX_TIMED_WAIT_UEI - Wait for the scheduler under test to exit, i.e. +* its uei to be filled in, for up to @__ms iterations at ~1ms each. +* Returns true if the scheduler exited and false on timeout, so a +* scheduler that never exits fails the test instead of hanging it. +* The bound is iteration-based rather than wall-clock and stretches +* on a loaded system instead of failing early. + */ +#define SCX_TIMED_WAIT_UEI (__skel, __uei, __ms) \ + ({ \ + s64 __left = (__ms); \ + \ + while (__left-- > 0 && !UEI_EXITED(__skel, __uei)) \ + usleep(1000); \ + UEI_EXITED(__skel, __uei) != 0; \ + }) +#define SCX_ECODE_VAL (__ecode) ({ \ u64 __val = 0; \ bool __found = false; \
Signature · 1 line
2.53.0
PATCH v2 6/6 selftests/sched_ext: Report reload_loop attach failures
In reply to Tao CuiFrom: Tao Cui <[email protected]>
reload_loop skips bpf_map__attach_struct_ops() failures silently:
measured on a loaded host, 1125-1233 of 2048 iterations failed, all with EBUSY (the single scheduler slot changing hands between the two threads) - the test passed while roughly half of its iterations never ran.
Count the failures per thread, recording any errno other than EBUSY separately. Fail the test on an unexpected errno or when a thread failed every attach; a thread that completed at least one attach has exercised the reload path, so ordinary EBUSY no longer prints on every run. The iteration and thread counts move from #defines to an enum, and two stray blank lines after setup() are dropped.
--- .../testing/selftests/sched_ext/reload_loop.c | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-)
tools/testing/selftests/sched_ext/reload_loop.c +36 -7
@@ -4,6 +4,7 @@* Copyright (c) 2024 David Vernet <[email protected]> */ #include <bpf/bpf.h> +#include <errno.h> #include <pthread.h> #include <scx/common.h> #include <sys/wait.h>
@@ -11,8 +12,14 @@#include "maximal.bpf.skel.h" #include "scx_test.h" +enum { +RELOAD_ITERS =1024 , +RELOAD_THREADS =2 , +}; +static struct maximal *skel; -static pthread_t threads[2 ]; +static pthread_t threads[RELOAD_THREADS ]; +static int unexpected_errno;bool force_exit = false;
@@ -31,22 +38,33 @@ static enum scx_test_status setup(void **ctx)static void *do_reload_loop (void *arg) { -u32 i; +u32 i, failures =0 ; -for (i =0 ; i <1024 && !force_exit; i++) { +for (i =0 ; i <RELOAD_ITERS && !force_exit; i++) {struct bpf_link *link; + errno =0 ; link =bpf_map__attach_struct_ops (skel->maps.maximal_ops); -if (link) -bpf_link__destroy (link); +if (!link) { +int e = errno; + +if (e !=EBUSY ) +__atomic_store_n (&unexpected_errno, e, + __ATOMIC_RELAXED); + failures++; +continue ; + } + +bpf_link__destroy (link); } -return NULL ; +return (void *)(unsigned long )failures; }static enum scx_test_status run (void *ctx) { -int err; +unsigned long failures[RELOAD_THREADS ] = {}; +int err, i;void *ret; err =pthread_create (&threads[0 ],NULL , do_reload_loop,NULL );
@@ -60,7 +78,18 @@ static enum scx_test_status run(void *ctx) }SCX_FAIL_IF (pthread_join (threads[0 ], &ret),"thread 0 failed" ); + failures[0 ] = (unsigned long )ret; +SCX_FAIL_IF (pthread_join (threads[1 ], &ret),"thread 1 failed" ); + failures[1 ] = (unsigned long )ret; + +SCX_FAIL_IF (unexpected_errno,"unexpected attach errno %d" , + unexpected_errno); + +for (i =0 ; i <RELOAD_THREADS ; i++) +SCX_FAIL_IF (failures[i] ==RELOAD_ITERS , +"thread %d: every attach failed (%lu EBUSY)" , + i, failures[i]);return SCX_TEST_PASS ; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 6/6] selftests/sched_ext: Report reload_loop attach failures
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] bpf: flaky reload_loop test due to strict iteration limit --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf: flaky reload_loop test due to strict iteration limit
--
commit 8a9da64f2647d703dd026bef834ba09325868dbc
Author: Tao Cui <[email protected]>
selftests/sched_ext: Report reload_loop attach failures
This commit updates the reload_loop test to record the number of EBUSY attach failures per thread. It fails the test if an unexpected errno is encountered or if any thread fails to attach the link on every iteration.
Quoted 4 lines
tools/testing/selftests/sched_ext/reload_loop.c +0
-0
[ ... ]
Quoted 13 lines
tools/testing/selftests/sched_ext/reload_loop.c +3
-2@@ -31,22 +38,33 @@ static enum scx_test_status setup(void **ctx)static void *do_reload_loop (void *arg) { -u32 i; +u32 i, failures =0 ; -for (i =0 ; i <1024 && !force_exit; i++) { +for (i =0 ; i <RELOAD_ITERS && !force_exit; i++) {struct bpf_link *link; + errno =0 ; link =bpf_map__attach_struct_ops (skel->maps.maximal_ops);
tools/testing/selftests/sched_ext/reload_loop.c static enum scx_test_status setup(void **ctx) · line 55
Quoted 10 lines
tools/testing/selftests/sched_ext/reload_loop.c +8
-2-if (link) -bpf_link__destroy (link); +if (!link) { +int e = errno; + +if (e !=EBUSY ) +__atomic_store_n (&unexpected_errno, e, + __ATOMIC_RELAXED); + failures++; +continue ;
[Severity: Medium]
Because this loop does not yield the CPU (e.g., via sched_yield() or usleep()) when receiving -EBUSY, can a thread rapidly spin through all 1024 iterations while the other thread holds the link or is preempted?
Quoted 8 lines
tools/testing/selftests/sched_ext/reload_loop.c +4
-1+ } + +bpf_link__destroy (link); } -return NULL ; +return (void *)(unsigned long )failures; }
[ ... ]
Quoted 6 lines
tools/testing/selftests/sched_ext/reload_loop.c +2
-0@@ -60,7 +78,18 @@ static enum scx_test_status run(void *ctx) }SCX_FAIL_IF (pthread_join (threads[0 ], &ret),"thread 0 failed" ); + failures[0 ] = (unsigned long )ret; +
tools/testing/selftests/sched_ext/reload_loop.c static enum scx_test_status run(void *ctx) · line 92
Quoted 10 lines
tools/testing/selftests/sched_ext/reload_loop.c +9
-0SCX_FAIL_IF (pthread_join (threads[1 ], &ret),"thread 1 failed" ); + failures[1 ] = (unsigned long )ret; + +SCX_FAIL_IF (unexpected_errno,"unexpected attach errno %d" , + unexpected_errno); + +for (i =0 ; i <RELOAD_THREADS ; i++) +SCX_FAIL_IF (failures[i] ==RELOAD_ITERS , +"thread %d: every attach failed (%lu EBUSY)" , + i, failures[i]);
[Severity: Medium]
Could this assertion cause spurious test failures on loaded systems?
Since 1024 syscalls returning -EBUSY execute extremely fast, if one thread successfully attaches the link and is temporarily preempted before destroying it, the other thread can easily exhaust its RELOAD_ITERS and hit this failure condition.
Quoted 3 lines
tools/testing/selftests/sched_ext/reload_loop.c +0
-0return SCX_TEST_PASS ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Tejun Heo
reviewerQuoted 7 lines
Tao Cui (6):
selftests/sched_ext: Fixreload_looperror-path use-after-free
sched_ext: AddSCX_ECODE_RSN_CGROUP_OFFLINEtouser_exit_info.hselftests/sched_ext: Fail loudly whenEXIT_KINDis missing from BTF selftests/sched_ext: AddSCX_ASSERT_ALIVEand adopt it selftests/sched_ext: Bound exit waits selftests/sched_ext: Reportreload_loopattach failures
Applied 1-6 to sched_ext/for-7.4 with description wording fixed up in patches 2, 3 and 6.
Thanks.
Signature · 1 line
tejun