All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/6] sched_ext: selftest infrastructure fixes
@ 2026-10-14 13:13 Tao Cui
  2026-10-14 13:13 ` [PATCH v2 1/6] selftests/sched_ext: Fix reload_loop error-path use-after-free Tao Cui
                   ` (6 more replies)
  0 siblings, 7 replies; 10+ messages in thread
From: Tao Cui @ 2026-10-14 13:13 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui,
	cui.tao

From: Tao Cui <cuitao@kylinos.cn>

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/20261009134104.296891-1-cui.tao@linux.dev/

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(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2 1/6] selftests/sched_ext: Fix reload_loop error-path use-after-free
  2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
@ 2026-10-14 13:13 ` Tao Cui
  2026-10-14 13:13 ` [PATCH v2 2/6] sched_ext: Add SCX_ECODE_RSN_CGROUP_OFFLINE to user_exit_info.h Tao Cui
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Tao Cui @ 2026-10-14 13:13 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui,
	cui.tao

From: Tao Cui <cuitao@kylinos.cn>

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.

Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 tools/testing/selftests/sched_ext/reload_loop.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/sched_ext/reload_loop.c b/tools/testing/selftests/sched_ext/reload_loop.c
index 49297b83d748..a00f3964dba0 100644
--- a/tools/testing/selftests/sched_ext/reload_loop.c
+++ b/tools/testing/selftests/sched_ext/reload_loop.c
@@ -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");
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 2/6] sched_ext: Add SCX_ECODE_RSN_CGROUP_OFFLINE to user_exit_info.h
  2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
  2026-10-14 13:13 ` [PATCH v2 1/6] selftests/sched_ext: Fix reload_loop error-path use-after-free Tao Cui
@ 2026-10-14 13:13 ` Tao Cui
  2026-10-14 13:13 ` [PATCH v2 3/6] selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF Tao Cui
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Tao Cui @ 2026-10-14 13:13 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui,
	cui.tao

From: Tao Cui <cuitao@kylinos.cn>

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.

Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 tools/sched_ext/include/scx/user_exit_info.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/sched_ext/include/scx/user_exit_info.h b/tools/sched_ext/include/scx/user_exit_info.h
index 56a02b549aef..cf27ac2447ef 100644
--- a/tools/sched_ext/include/scx/user_exit_info.h
+++ b/tools/sched_ext/include/scx/user_exit_info.h
@@ -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,
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 3/6] selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF
  2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
  2026-10-14 13:13 ` [PATCH v2 1/6] selftests/sched_ext: Fix reload_loop error-path use-after-free Tao Cui
  2026-10-14 13:13 ` [PATCH v2 2/6] sched_ext: Add SCX_ECODE_RSN_CGROUP_OFFLINE to user_exit_info.h Tao Cui
@ 2026-10-14 13:13 ` Tao Cui
  2026-10-14 13:28   ` sashiko-bot
  2026-10-14 13:13 ` [PATCH v2 4/6] selftests/sched_ext: Add SCX_ASSERT_ALIVE and adopt it Tao Cui
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 10+ messages in thread
From: Tao Cui @ 2026-10-14 13:13 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui,
	cui.tao

From: Tao Cui <cuitao@kylinos.cn>

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 remaining
`kind != 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.

Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 .../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(-)

diff --git a/tools/testing/selftests/sched_ext/allowed_cpus.c b/tools/testing/selftests/sched_ext/allowed_cpus.c
index 773699d120ea..76311728a6b1 100644
--- a/tools/testing/selftests/sched_ext/allowed_cpus.c
+++ b/tools/testing/selftests/sched_ext/allowed_cpus.c
@@ -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;
 	}
diff --git a/tools/testing/selftests/sched_ext/cgroup_nr_cpus.c b/tools/testing/selftests/sched_ext/cgroup_nr_cpus.c
index 4973f7080353..1475a8b3f05f 100644
--- a/tools/testing/selftests/sched_ext/cgroup_nr_cpus.c
+++ b/tools/testing/selftests/sched_ext/cgroup_nr_cpus.c
@@ -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;
 	}
diff --git a/tools/testing/selftests/sched_ext/cyclic_kick_wait.c b/tools/testing/selftests/sched_ext/cyclic_kick_wait.c
index c2e5aa9de715..178d1d75f332 100644
--- a/tools/testing/selftests/sched_ext/cyclic_kick_wait.c
+++ b/tools/testing/selftests/sched_ext/cyclic_kick_wait.c
@@ -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);
diff --git a/tools/testing/selftests/sched_ext/enq_blocked.c b/tools/testing/selftests/sched_ext/enq_blocked.c
index 26204548bbf0..1d5e090b54bc 100644
--- a/tools/testing/selftests/sched_ext/enq_blocked.c
+++ b/tools/testing/selftests/sched_ext/enq_blocked.c
@@ -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);
diff --git a/tools/testing/selftests/sched_ext/kick.c b/tools/testing/selftests/sched_ext/kick.c
index 7f01602c75b2..40c91d54c1eb 100644
--- a/tools/testing/selftests/sched_ext/kick.c
+++ b/tools/testing/selftests/sched_ext/kick.c
@@ -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;
 		}
diff --git a/tools/testing/selftests/sched_ext/nohz_tick.c b/tools/testing/selftests/sched_ext/nohz_tick.c
index df40976b9166..e19a97034b7f 100644
--- a/tools/testing/selftests/sched_ext/nohz_tick.c
+++ b/tools/testing/selftests/sched_ext/nohz_tick.c
@@ -165,7 +165,7 @@ static bool wait_for_counter(struct nohz_tick *skel, const u64 *counter, u64 val
 	for (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 t
 		u64 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);
diff --git a/tools/testing/selftests/sched_ext/scx_test.h b/tools/testing/selftests/sched_ext/scx_test.h
index 90b8d6915bb7..319e67f19b6d 100644
--- a/tools/testing/selftests/sched_ext/scx_test.h
+++ b/tools/testing/selftests/sched_ext/scx_test.h
@@ -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__
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 4/6] selftests/sched_ext: Add SCX_ASSERT_ALIVE and adopt it
  2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
                   ` (2 preceding siblings ...)
  2026-10-14 13:13 ` [PATCH v2 3/6] selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF Tao Cui
@ 2026-10-14 13:13 ` Tao Cui
  2026-10-14 13:13 ` [PATCH v2 5/6] selftests/sched_ext: Bound exit waits Tao Cui
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Tao Cui @ 2026-10-14 13:13 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui,
	cui.tao

From: Tao Cui <cuitao@kylinos.cn>

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.

Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 .../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(-)

diff --git a/tools/testing/selftests/sched_ext/create_dsq.bpf.c b/tools/testing/selftests/sched_ext/create_dsq.bpf.c
index 680cc4b6d8c7..24dacd1aab0c 100644
--- a/tools/testing/selftests/sched_ext/create_dsq.bpf.c
+++ b/tools/testing/selftests/sched_ext/create_dsq.bpf.c
@@ -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",
 };
diff --git a/tools/testing/selftests/sched_ext/create_dsq.c b/tools/testing/selftests/sched_ext/create_dsq.c
index 422e6532ee71..84143d5710f4 100644
--- a/tools/testing/selftests/sched_ext/create_dsq.c
+++ b/tools/testing/selftests/sched_ext/create_dsq.c
@@ -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);
diff --git a/tools/testing/selftests/sched_ext/init_enable_count.bpf.c b/tools/testing/selftests/sched_ext/init_enable_count.bpf.c
index 5eb9edb1837d..c0a5dd8fe183 100644
--- a/tools/testing/selftests/sched_ext/init_enable_count.bpf.c
+++ b/tools/testing/selftests/sched_ext/init_enable_count.bpf.c
@@ -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",
 };
diff --git a/tools/testing/selftests/sched_ext/init_enable_count.c b/tools/testing/selftests/sched_ext/init_enable_count.c
index 44577e30e764..c4a57038a1d6 100644
--- a/tools/testing/selftests/sched_ext/init_enable_count.c
+++ b/tools/testing/selftests/sched_ext/init_enable_count.c
@@ -5,6 +5,7 @@
  * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
  */
 #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);
diff --git a/tools/testing/selftests/sched_ext/maximal.bpf.c b/tools/testing/selftests/sched_ext/maximal.bpf.c
index 04a369078aac..9ea664e2ff70 100644
--- a/tools/testing/selftests/sched_ext/maximal.bpf.c
+++ b/tools/testing/selftests/sched_ext/maximal.bpf.c
@@ -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 = {
diff --git a/tools/testing/selftests/sched_ext/maximal.c b/tools/testing/selftests/sched_ext/maximal.c
index 1dc369224670..6c6b80e0446e 100644
--- a/tools/testing/selftests/sched_ext/maximal.c
+++ b/tools/testing/selftests/sched_ext/maximal.c
@@ -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;
diff --git a/tools/testing/selftests/sched_ext/scx_test.h b/tools/testing/selftests/sched_ext/scx_test.h
index 319e67f19b6d..9e102d7f2893 100644
--- a/tools/testing/selftests/sched_ext/scx_test.h
+++ b/tools/testing/selftests/sched_ext/scx_test.h
@@ -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;							\
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 5/6] selftests/sched_ext: Bound exit waits
  2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
                   ` (3 preceding siblings ...)
  2026-10-14 13:13 ` [PATCH v2 4/6] selftests/sched_ext: Add SCX_ASSERT_ALIVE and adopt it Tao Cui
@ 2026-10-14 13:13 ` Tao Cui
  2026-10-14 13:13 ` [PATCH v2 6/6] selftests/sched_ext: Report reload_loop attach failures Tao Cui
  2026-10-14 18:29 ` [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tejun Heo
  6 siblings, 0 replies; 10+ messages in thread
From: Tao Cui @ 2026-10-14 13:13 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui,
	cui.tao

From: Tao Cui <cuitao@kylinos.cn>

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.

Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 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(-)

diff --git a/tools/testing/selftests/sched_ext/exit.c b/tools/testing/selftests/sched_ext/exit.c
index 01b17092d5c8..fd6f1c9ab2db 100644
--- a/tools/testing/selftests/sched_ext/exit.c
+++ b/tools/testing/selftests/sched_ext/exit.c
@@ -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);
diff --git a/tools/testing/selftests/sched_ext/hotplug.c b/tools/testing/selftests/sched_ext/hotplug.c
index 10b8d42bd89b..3c07686a93c8 100644
--- a/tools/testing/selftests/sched_ext/hotplug.c
+++ b/tools/testing/selftests/sched_ext/hotplug.c
@@ -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) |
diff --git a/tools/testing/selftests/sched_ext/prog_run.c b/tools/testing/selftests/sched_ext/prog_run.c
index 1129ec2aaddc..369c744ac06e 100644
--- a/tools/testing/selftests/sched_ext/prog_run.c
+++ b/tools/testing/selftests/sched_ext/prog_run.c
@@ -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",
diff --git a/tools/testing/selftests/sched_ext/scx_test.h b/tools/testing/selftests/sched_ext/scx_test.h
index 9e102d7f2893..8490b01fff74 100644
--- a/tools/testing/selftests/sched_ext/scx_test.h
+++ b/tools/testing/selftests/sched_ext/scx_test.h
@@ -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;							\
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 6/6] selftests/sched_ext: Report reload_loop attach failures
  2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
                   ` (4 preceding siblings ...)
  2026-10-14 13:13 ` [PATCH v2 5/6] selftests/sched_ext: Bound exit waits Tao Cui
@ 2026-10-14 13:13 ` Tao Cui
  2026-10-14 13:24   ` sashiko-bot
  2026-10-14 18:29 ` [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tejun Heo
  6 siblings, 1 reply; 10+ messages in thread
From: Tao Cui @ 2026-10-14 13:13 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui,
	cui.tao

From: Tao Cui <cuitao@kylinos.cn>

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.

Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 .../testing/selftests/sched_ext/reload_loop.c | 43 ++++++++++++++++---
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/sched_ext/reload_loop.c b/tools/testing/selftests/sched_ext/reload_loop.c
index a00f3964dba0..7d8880fa1a03 100644
--- a/tools/testing/selftests/sched_ext/reload_loop.c
+++ b/tools/testing/selftests/sched_ext/reload_loop.c
@@ -4,6 +4,7 @@
  * Copyright (c) 2024 David Vernet <dvernet@meta.com>
  */
 #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;
 }
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 6/6] selftests/sched_ext: Report reload_loop attach failures
  2026-10-14 13:13 ` [PATCH v2 6/6] selftests/sched_ext: Report reload_loop attach failures Tao Cui
@ 2026-10-14 13:24   ` sashiko-bot
  0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-10-14 13:24 UTC (permalink / raw)
  To: Tao Cui; +Cc: sched-ext

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 <cuitao@kylinos.cn>

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.

> diff --git a/tools/testing/selftests/sched_ext/reload_loop.c b/tools/testing/selftests/sched_ext/reload_loop.c
> index a00f3964dba05..7d8880fa1a032 100644
> --- a/tools/testing/selftests/sched_ext/reload_loop.c
> +++ b/tools/testing/selftests/sched_ext/reload_loop.c

[ ... ]

> @@ -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;

[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?

> +		}
> +
> +		bpf_link__destroy(link);
>  	}
>  
> -	return NULL;
> +	return (void *)(unsigned long)failures;
>  }

[ ... ]

> @@ -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]);

[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.

>  
>  	return SCX_TEST_PASS;
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261014131325.793640-1-cui.tao@linux.dev?part=6

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 3/6] selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF
  2026-10-14 13:13 ` [PATCH v2 3/6] selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF Tao Cui
@ 2026-10-14 13:28   ` sashiko-bot
  0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-10-14 13:28 UTC (permalink / raw)
  To: Tao Cui; +Cc: sched-ext

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 <cuitao@kylinos.cn>
    
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.

> diff --git a/tools/testing/selftests/sched_ext/scx_test.h b/tools/testing/selftests/sched_ext/scx_test.h
> index 90b8d6915bb7e..319e67f19b6dd 100644
> --- a/tools/testing/selftests/sched_ext/scx_test.h
> +++ b/tools/testing/selftests/sched_ext/scx_test.h
[ ... ]
> @@ -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__

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261014131325.793640-1-cui.tao@linux.dev?part=3

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 0/6] sched_ext: selftest infrastructure fixes
  2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
                   ` (5 preceding siblings ...)
  2026-10-14 13:13 ` [PATCH v2 6/6] selftests/sched_ext: Report reload_loop attach failures Tao Cui
@ 2026-10-14 18:29 ` Tejun Heo
  6 siblings, 0 replies; 10+ messages in thread
From: Tejun Heo @ 2026-10-14 18:29 UTC (permalink / raw)
  To: Tao Cui; +Cc: David Vernet, Andrea Righi, Changwoo Min, sched-ext, Tao Cui

> 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

Applied 1-6 to sched_ext/for-7.4 with description wording fixed up in
patches 2, 3 and 6.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-10-14 18:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-14 13:13 [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tao Cui
2026-10-14 13:13 ` [PATCH v2 1/6] selftests/sched_ext: Fix reload_loop error-path use-after-free Tao Cui
2026-10-14 13:13 ` [PATCH v2 2/6] sched_ext: Add SCX_ECODE_RSN_CGROUP_OFFLINE to user_exit_info.h Tao Cui
2026-10-14 13:13 ` [PATCH v2 3/6] selftests/sched_ext: Fail loudly when EXIT_KIND is missing from BTF Tao Cui
2026-10-14 13:28   ` sashiko-bot
2026-10-14 13:13 ` [PATCH v2 4/6] selftests/sched_ext: Add SCX_ASSERT_ALIVE and adopt it Tao Cui
2026-10-14 13:13 ` [PATCH v2 5/6] selftests/sched_ext: Bound exit waits Tao Cui
2026-10-14 13:13 ` [PATCH v2 6/6] selftests/sched_ext: Report reload_loop attach failures Tao Cui
2026-10-14 13:24   ` sashiko-bot
2026-10-14 18:29 ` [PATCH v2 0/6] sched_ext: selftest infrastructure fixes Tejun Heo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.