Skip to contentSkip to conversation

tools/sched_ext: robustness fixes from cold review

PATCH In review Tao Cui 4 participants 15 messages Nov 10 – Nov 11 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

cover tools/sched_ext: robustness fixes from cold review

From: Tao Cui <[email protected]>

Hello,

This series fixes nine robustness issues in the example schedulers scx_pair, scx_flatcg, scx_qmap and scx_userland.

scx_flatcg resets its stats baseline on restart (stale counters print as u64 underflow garbage) and rejects a stats interval that puts nanosleep in an error loop (a negative or non-finite -i busy-spins at 100% CPU). scx_pair stops aborting the scheduler when the dispatching cgroup goes away mid-dispatch, gates attach on an interrupted initialization instead of running with a half-built cgrp_q_arr, resets pair_id on restart, and cleans up a dead busy-check whose fall-through clears another cgroup's busy marker. scx_qmap documents the options it actually accepts and resets its hier stats baseline on restart. scx_userland bounds the batch against the dispatch buffer instead of overflowing it 13ms in, and rejects batch size 0, which livelocks the machine until the stall watchdog fires.

Verified in qemu on tj/sched_ext for-next (5b740a72b55c), same kernel and config with and without each patch, comparing stock and patched binaries: a scx_flatcg restart prints six u64-underflow garbage counters on stock and zeros with patch 1; a non-finite -i busy-spins ~17k EINVAL lines in 3s on stock and is rejected at parse time with patch 2; SIGINT storms abort 20/30 interrupted inits on stock with the scheduler never loading and exit 30/30 clean with patch 4; a scx_pair restart aborts 7ms in with a stale pair_id on stock and runs clean for its full window with patch 5; an oversized scx_userland batch aborts 13ms in with "dispatch buffer overflow" on stock and survives 25s with patch 9; and batch size 0, which livelocks the machine until the stall watchdog fires (spinner progress collapses to ~1%), is rejected immediately with patch 8. Beyond the A/B pairs: the scx_pair cgid-cache churn window is too narrow to drive (40 rounds clean on both binaries; patch 3 is graded by inspection), the qmap synopsis diff shows exactly the three added options (patch 6), and the hier stats baseline was verified with a clang-23.1.5-built scx_qmap run through restart cycles (patch 7, hier stats clean throughout).

Based on sched_ext/for-next (5b740a72b55c).

Tao Cui (9):
tools/sched_ext: scx_flatcg: Reset the stats baseline on restart tools/sched_ext: scx_flatcg: Validate the stats interval tools/sched_ext: scx_pair: Don't abort when the dispatching cgroup
goes away
tools/sched_ext: scx_pair: Don't attach after an interrupted
initialization
tools/sched_ext: scx_pair: Reset pair_id on restart tools/sched_ext: scx_qmap: Add the missing options to the usage
synopsis
tools/sched_ext: scx_qmap: Reset the hier stats baseline on restart tools/sched_ext: scx_userland: Check dispatch buffer slots before
inserting
tools/sched_ext: scx_userland: Reject batch size 0

 tools/sched_ext/scx_flatcg.c       |  7 ++++++-
 tools/sched_ext/scx_pair.bpf.c     | 12 ++++++++++--
 tools/sched_ext/scx_pair.c         | 14 ++++++++++++--
 tools/sched_ext/scx_qmap.c         |  3 ++-
 tools/sched_ext/scx_userland.bpf.c |  8 ++++++++
 tools/sched_ext/scx_userland.c     |  5 +++++
 6 files changed, 43 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/9 tools/sched_ext: scx_flatcg: Reset the stats baseline on restart

From: Tao Cui <[email protected]>

last_stats holds the previous accumulator snapshot for the stats deltas and is initialized before the restart: label. A hotplug fail-safe reload starts the scheduler with fresh counters while last_stats still carries the previous generation's snapshots, so the first stats interval prints u64-underflow garbage.

Reset it together with the rest of the reload state. last_cpu_sum and last_cpu_idle anchor to /proc/stat which persists across reloads and are intentionally left alone.

Fixes a4103eacc2ab ("sched_ext: Add a cgroup scheduler which uses flattened hierarchy")
---
 tools/sched_ext/scx_flatcg.c | 1 +
 1 file changed, 1 insertion(+)
tools/sched_ext/scx_flatcg.c +1 -0
index a223bff3746a..4c3c2cca2b85 100644
@@ -140,6 +140,7 @@ int main(int argc, char **argv)
 	signal(SIGINT, sigint_handler);
 	signal(SIGTERM, sigint_handler);
 restart:
+	memset(last_stats, 0, sizeof(last_stats));
 	optind = 1;
 	skel = SCX_OPS_OPEN(flatcg_ops, scx_flatcg);
 
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
author
#2 lore raw

PATCH 2/9 tools/sched_ext: scx_flatcg: Validate the stats interval

In reply to Tao Cui

From: Tao Cui <[email protected]>

The interval is parsed with strtod() and split into a timespec with a float cast in the middle: a negative value produces a negative tv_sec or tv_nsec (for -0.5 the seconds truncate to zero and the nanoseconds go negative), and any value that doesn't survive the float round-trip (e.g. 2^24 + 1) produces tv_nsec >= 1e9. Either way nanosleep() returns EINVAL, which is never checked, and the stats loop starts busy-spinning at 100% CPU instead of sampling at the requested interval.

Reject negative intervals up front and compute the nsec part from the double rather than through a float so the rounding hole is gone.

Fixes a4103eacc2ab ("sched_ext: Add a cgroup scheduler which uses flattened hierarchy")
---
 tools/sched_ext/scx_flatcg.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/sched_ext/scx_flatcg.c +5 -1
index 4c3c2cca2b85..bac6535fa2f0 100644
@@ -158,8 +158,12 @@ int main(int argc, char **argv)
 			break;
 		case 'i':
 			v = strtod(optarg, NULL);
+			if (!(v >= 0) || v >= 1e18) {
+				fprintf(stderr, "Invalid interval %f\n", v);
+				exit(1);
+			}
 			intv_ts.tv_sec = v;
-			intv_ts.tv_nsec = (v - (float)intv_ts.tv_sec) * 1000000000;
+			intv_ts.tv_nsec = (v - intv_ts.tv_sec) * 1000000000;
 			break;
 		case 'f':
 			skel->rodata->fifo_sched = true;
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
author
#3 lore raw

PATCH 3/9 tools/sched_ext: scx_pair: Don't abort when the dispatching cgroup goes away

In reply to Tao Cui

From: Tao Cui <[email protected]>

try_dispatch() caches the cgroup id in pairc->cgid, drops pairc->lock and then looks the cgroup back up in cgrp_q_idx_hash. pair_cgroup_exit() deletes that hash entry when the cgroup is removed, so a plain rmdir of the cgroup that is currently being dispatched, a normal event for short-lived containers or cgroup churn, hits the lookup failure and scx_bpf_error() takes the whole scheduler down.

The expiry path already knows how to re-pick a cgroup; make the fast path tolerate the disappearance too. Mark the pair as drained on lookup failure and return, letting the next dispatch expire the batch and pick the next cgroup instead of aborting.

The enqueue side is left for a follow-up: a lookup miss is reachable there in principle (css offline runs under the cgroup mutex alone, and a sleeping-task migrate-out does not take the rq lock), but handling it gracefully needs somewhere to put the task, which on the enqueue path means a fallback queue; that is a larger change than this fix scopes. The dispatch path, where the miss costs one skipped batch, is the cheap half.

Fixes f0262b102c7c ("tools/sched_ext: add scx_pair scheduler")
---
 tools/sched_ext/scx_pair.bpf.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/sched_ext/scx_pair.bpf.c +10 -2
index 0d61b7b812db..d38d3a078ade 100644
@@ -478,8 +478,16 @@ static int try_dispatch(s32 cpu)
 	/* again, it'd be better to do all these with the lock held, oh well */
 	vptr = bpf_map_lookup_elem(&cgrp_q_idx_hash, &cgid);
 	if (!vptr) {
-		scx_bpf_error("failed to lookup q_idx for cgroup[%llu]", cgid);
-		return -ENOENT;
+		/*
+		 * @cgid can disappear from under us: pair_cgroup_exit()
+		 * drops the hash entry when the cgroup is going away.
+		 * Mark the pair as drained and let the next dispatch
+		 * expire it instead of taking the whole scheduler down.
+		 */
+		bpf_spin_lock(&pairc->lock);
+		pairc->draining = true;
+		bpf_spin_unlock(&pairc->lock);
+		goto out_maybe_kick;
 	}
 	q_idx = *vptr;
 
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
#4 lore raw

Re: [PATCH 3/9] tools/sched_ext: scx_pair: Don't abort when the dispatching cgroup goes away

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched_ext: scx_pair: leaked active_mask bit in try_dispatch() stalls pair --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched_ext: scx_pair: leaked active_mask bit in try_dispatch() stalls pair
--

commit 0bc135e247824dc3899acf0f6cf608c43ee6328b
Author: Tao Cui <[email protected]>

tools/sched_ext: scx_pair: Don't abort when the dispatching cgroup goes away

This commit changes try_dispatch() in scx_pair to mark the CPU pair as draining and return instead of calling scx_bpf_error() when looking up the current cgroup ID in cgrp_q_idx_hash fails.

Quoted 11 lines
tools/sched_ext/scx_pair.bpf.c +1 -2
index 0d61b7b812db6..d38d3a078adee 100644
@@ -478,8 +478,16 @@ static int try_dispatch(s32 cpu)
 	/* again, it'd be better to do all these with the lock held, oh well */
 	vptr = bpf_map_lookup_elem(&cgrp_q_idx_hash, &cgid);
 	if (!vptr) {
-		scx_bpf_error("failed to lookup q_idx for cgroup[%llu]", cgid);
-		return -ENOENT;
+		/*

tools/sched_ext/scx_pair.bpf.c static int try_dispatch(s32 cpu) · line 491

Quoted 10 lines
tools/sched_ext/scx_pair.bpf.c +9 -0
+		 * @cgid can disappear from under us: pair_cgroup_exit()
+		 * drops the hash entry when the cgroup is going away.
+		 * Mark the pair as drained and let the next dispatch
+		 * expire it instead of taking the whole scheduler down.
+		 */
+		bpf_spin_lock(&pairc->lock);
+		pairc->draining = true;
+		bpf_spin_unlock(&pairc->lock);
+		goto out_maybe_kick;
 	}

[Severity: Medium]
Does this path need to clear in_pair_mask from pairc->active_mask and return -EAGAIN, like the empty-cgroup path just below?

Before dropping pairc->lock to look up cgid in cgrp_q_idx_hash, try_dispatch() sets pairc->active_mask |= in_pair_mask. Jumping to out_maybe_kick returns 0 without dispatching a task, so pair_dispatch() stops retrying and the current CPU goes idle with its bit still set in pairc->active_mask. When the sibling CPU enters try_dispatch() and sees pairc->draining set, it checks pairc->active_mask, assumes this idle CPU is still running a task from the old cgroup, and bails out without switching to the next cgroup in top_q, stalling the pair.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#5 lore raw

PATCH 4/9 tools/sched_ext: scx_pair: Don't attach after an interrupted initialization

In reply to Tao Cui

From: Tao Cui <[email protected]>

The cgrp_q_arr initialization loop checks exit_req and breaks early so that Ctrl-C doesn't make the user wait through all MAX_CGRPS iterations
- but the break falls straight into SCX_OPS_ATTACH(). Attaching with part of the inner queue maps missing lets ops.cgroup_init() hand out slots for cgroups whose inner map was never created, and the first task in such a cgroup aborts the scheduler with an scx_bpf_error(). On a busy system the 4096-iteration loop takes long enough that hitting it with Ctrl-C is likely, so the intended responsive interrupt turns into a scheduler crash dump.

Exit cleanly when interrupted mid-initialization instead of attaching.

Fixes f0262b102c7c ("tools/sched_ext: add scx_pair scheduler")
---
 tools/sched_ext/scx_pair.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
tools/sched_ext/scx_pair.c +10 -1
index 00f595b58f97..35c8a1a07f56 100644
@@ -161,8 +161,17 @@ int main(int argc, char **argv)
 	printf("\n");
 
 	/*
-	 * Fully initialized, attach and run.
+	 * Fully initialized, attach and run. An interrupt during the
+	 * initialization loop leaves the tail of cgrp_q_arr without inner
+	 * maps; attaching in that state lets ops.cgroup_init() hand out
+	 * uninitialized slots and abort the scheduler on the first cgroup
+	 * task.
 	 */
+	if (exit_req) {
+		scx_pair__destroy(skel);
+		return 1;
+	}
+
 	link = SCX_OPS_ATTACH(skel, pair_ops, scx_pair);
 
 	while (!exit_req && !UEI_EXITED(skel, uei)) {
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
author
#6 lore raw

PATCH 5/9 tools/sched_ext: scx_pair: Reset pair_id on restart

In reply to Tao Cui

From: Tao Cui <[email protected]>

pair_id is initialized to 0 before the restart: label, so a reload triggered by an ops restart (e.g. a hotplug event with the scheduler providing no cpu_online/cpu_offline callbacks) jumps back to the label with pair_id still holding the previous run's counter. Since f546c77038ab ("tools/sched_ext: scx_pair: fix pair_ctx indexing for CPU pairs") pair_id is a dense index into the pair_ctx array, so the second load starts pairing CPUs past the end of the array and the scheduler can never come up again.

Initialize pair_id after the label so every reload starts from a clean counter.

Fixes f546c77038ab ("tools/sched_ext: scx_pair: fix pair_ctx indexing for CPU pairs")
---
 tools/sched_ext/scx_pair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/sched_ext/scx_pair.c +2 -1
index 35c8a1a07f56..f15093e002f0 100644
@@ -48,12 +48,13 @@ int main(int argc, char **argv)
 	struct bpf_link *link;
 	__u64 seq = 0, ecode;
 	__s32 stride, i, opt, outer_fd;
-	__u32 pair_id = 0;
+	__u32 pair_id;
 
 	libbpf_set_print(libbpf_print_fn);
 	signal(SIGINT, sigint_handler);
 	signal(SIGTERM, sigint_handler);
 restart:
+	pair_id = 0;
 	optind = 1;
 	skel = SCX_OPS_OPEN(pair_ops, scx_pair);
 
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
author
#7 lore raw

PATCH 6/9 tools/sched_ext: scx_qmap: Add the missing options to the usage synopsis

In reply to Tao Cui

From: Tao Cui <[email protected]>

-C, -B and -q are described in the option list but missing from the usage synopsis. Add them.

---
 tools/sched_ext/scx_qmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/sched_ext/scx_qmap.c +1 -1
index fe2f206f5c8a..f44802247069 100644
@@ -47,7 +47,7 @@ const char help_fmt[] =
 "\n"
 "Usage: %s [-s SLICE_US] [-e COUNT] [-t COUNT] [-T COUNT] [-l COUNT] [-b COUNT]\n"
 "       [-N COUNT] [-P] [-M] [-H] [-c CG_PATH] [-d PID] [-D LEN] [-S] [-p] [-I] [-X]\n"
-"       [-F COUNT] [-i SEC] [-R MS] [-J MODE] [-v]\n"
+"       [-F COUNT] [-i SEC] [-R MS] [-C MODE] [-B PPT] [-q US] [-J MODE] [-v]\n"
 "\n"
 "  -s SLICE_US   Override slice duration\n"
 "  -e COUNT      Trigger scx_bpf_error() after COUNT enqueues\n"
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
author
#8 lore raw

PATCH 7/9 tools/sched_ext: scx_qmap: Reset the hier stats baseline on restart

In reply to Tao Cui

From: Tao Cui <[email protected]>

hprev holds the previous per-participant counter snapshots for the hier stats deltas and is initialized before the restart: label, so a hotplug-triggered fail-safe reload leaves it holding snapshots from the previous generation while the scheduler's own counters start from zero; the first stats interval prints u64-underflow garbage.

Reset it together with optind on reload.

Fixes e9151ed5c944 ("tools/sched_ext: scx_qmap - Expand hierarchical sub-scheduling")
---
 tools/sched_ext/scx_qmap.c | 1 +
 1 file changed, 1 insertion(+)
tools/sched_ext/scx_qmap.c +1 -0
index f44802247069..414afb817e0a 100644
@@ -292,6 +292,7 @@ int main(int argc, char **argv)
 	}
 restart:
 	optind = 1;
+	memset(&hprev, 0, sizeof(hprev));
 	skel = SCX_OPS_CID_OPEN(qmap_ops, scx_qmap);
 
 	skel->rodata->slice_ns = skel->rodata->__SCX_SLICE_DFL;
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
author
#9 lore raw

PATCH 8/9 tools/sched_ext: scx_userland: Check dispatch buffer slots before inserting

In reply to Tao Cui

From: Tao Cui <[email protected]>

userland_dispatch() pops up to MAX_ENQUEUED_TASKS pids from the dispatched map and inserts each task into the global DSQ without consulting scx_bpf_dispatch_nr_slots(). The kernel's dispatch buffer holds 32 slots against MAX_ENQUEUED_TASKS (4096), so a single
full dispatch round overflows it long before that many tasks
pile up between dispatch rounds the insert past the buffer limit aborts the whole scheduler. Every other tools scheduler that loops on scx_bpf_dsq_insert() (scx_central, scx_qmap) checks the remaining slot count; scx_userland is the odd one out.

Break out when the buffer is exhausted and leave the remaining tasks queued for the next dispatch round.

Fixes cc4448d0856d ("tools/sched_ext: add scx_userland scheduler")
---
 tools/sched_ext/scx_userland.bpf.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/sched_ext/scx_userland.bpf.c +8 -0
index f29862b89386..0a6daad84528 100644
@@ -233,6 +233,14 @@ void BPF_STRUCT_OPS(userland_dispatch, s32 cpu, struct task_struct *prev)
 		s32 pid;
 		struct task_struct *p;
 
+		/*
+		 * The dispatch buffer is limited. When it runs out, leave the
+		 * remaining tasks queued for the next dispatch round instead
+		 * of inserting past the limit and aborting the scheduler.
+		 */
+		if (!scx_bpf_dispatch_nr_slots())
+			break;
+
 		if (bpf_map_pop_elem(&dispatched, &pid))
 			break;
 
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
#10 lore raw

Re: [PATCH 8/9] tools/sched_ext: scx_userland: Check dispatch buffer slots before inserting

TL;DR: Commit message only: the sentence explaining when the dispatch buffer overflows is garbled (two sentences run together, stray indent); the review asks for a rewrite. No code issues raised.
Quoted 1 line

tools/sched_ext: scx_userland: Check dispatch buffer slots before inserting

TL;DR: Commit message only: the sentence explaining when the dispatch buffer overflows is garbled (two sentences run together, stray indent); the review asks for a rewrite. No code issues raised.

Quoted 12 lines

tools/sched_ext: scx_userland: Check dispatch buffer slots before inserting

userland_dispatch() pops up to MAX_ENQUEUED_TASKS pids from the dispatched map and inserts each task into the global DSQ without consulting scx_bpf_dispatch_nr_slots(). The kernel's dispatch buffer holds 32 slots against MAX_ENQUEUED_TASKS (4096), so a single
full dispatch round overflows it long before that many tasks
pile up between dispatch rounds the insert past the buffer limit aborts the whole scheduler. Every other tools scheduler that loops on scx_bpf_dsq_insert() (scx_central, scx_qmap) checks the remaining slot count; scx_userland is the odd one out.

Quoted 5 lines

Break out when the buffer is exhausted and leave the remaining tasks queued for the next dispatch round.

Fixes cc4448d0856d ("tools/sched_ext: add scx_userland scheduler")

This isn't a bug, but the sentence explaining when the overflow happens looks garbled. It reads as two sentences run together:

"so a single
full dispatch round overflows it long before that many tasks
pile up between dispatch rounds the insert past the buffer limit aborts the whole scheduler."

The second line also has a stray three-space indent.

From the code, userland_dispatch() pops pids from the dispatched queue and calls scx_bpf_dsq_insert() for each one. Each insert from ops.dispatch() goes through scx_dsq_insert_commit(), which calls scx_error(sch, "dispatch buffer overflow") once
dspc->cursor >= sch->dsp_max_batch. Since scx_userland does not set ops.dispatch_max_batch, dsp_max_batch is SCX_DSP_DFL_MAX_BATCH (32).

So the scheduler aborts once more than 32 pids (31 if dispatch_user_scheduler() also inserted the usersched task in the same call) have built up in the dispatched queue between two ops.dispatch() calls.

Could the sentence be rewritten to say that directly?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/43092548483

author
#11 lore raw

PATCH 9/9 tools/sched_ext: scx_userland: Reject batch size 0

In reply to Tao Cui

From: Tao Cui <[email protected]>

-b 0 is accepted silently and leaves batch_size at zero, so dispatch_batch() never moves a task out of the vruntime list into the dispatched queue: tasks pile up on the vruntime list while nr_scheduled stays positive and ops.update_idle() keeps kicking cpus; the system-wide livelock ends when the stall watchdog kills the scheduler.

Reject a zero batch size at option parsing.

Fixes cc4448d0856d ("tools/sched_ext: add scx_userland scheduler")
---
 tools/sched_ext/scx_userland.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/sched_ext/scx_userland.c +5 -0
index b16b6db5f365..cbd511685449 100644
@@ -364,6 +364,11 @@ static void pre_bootstrap(int argc, char **argv)
 		}
 	}
 
+	if (!batch_size) {
+		fprintf(stderr, "Invalid batch size %s\n", optarg);
+		exit(1);
+	}
+
 	/*
 	 * It's not always safe to allocate in a user space scheduler, as an
 	 * enqueued task could hold a lock that we require in order to be able
Signature · 3 lines
2.53.0

base-commit: 5b740a72b55c4267ba4d8010f17dde26b26f52d3
#12 lore raw

Re: [PATCH 9/9] tools/sched_ext: scx_userland: Reject batch size 0

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] tools/sched_ext: NULL optarg passed to fprintf() in pre_bootstrap() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] tools/sched_ext: NULL optarg passed to fprintf() in pre_bootstrap()
--

commit 0b3057c6e52c8c9370246141c70182a7501b6c0e
Author: Tao Cui <[email protected]>

tools/sched_ext: scx_userland: Reject batch size 0

This commit rejects a zero batch size in pre_bootstrap() to prevent a livelock when -b 0 is passed on the command line.

tools/sched_ext/scx_userland.c static void pre_bootstrap(int argc, char **argv) · line 370

Quoted 12 lines
tools/sched_ext/scx_userland.c +4 -0
index b16b6db5f3650..cbd511685449f 100644
@@ -364,6 +364,11 @@ static void pre_bootstrap(int argc, char **argv)
 		}
 	}
 
+	if (!batch_size) {
+		fprintf(stderr, "Invalid batch size %s\n", optarg);
+		exit(1);
+	}

[Severity: Low]
Does this pass a NULL optarg to fprintf()? Because this check runs after the while ((opt = getopt(...)) != -1) loop finishes, the final getopt() call has already reset optarg to NULL, which prints "(null)" on glibc and causes a NULL pointer dereference on libcs such as musl. Checking !batch_size inside case 'b' while optarg still points to the argument (or printing batch_size with %u) would avoid this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
reviewer
#13 lore raw
In reply to Tao Cui
Quoted 6 lines

Tao Cui (9):
tools/sched_ext: scx_flatcg: Reset the stats baseline on restart tools/sched_ext: scx_flatcg: Validate the stats interval tools/sched_ext: scx_pair: Don't abort when the dispatching cgroup
goes away
tools/sched_ext: scx_pair: Don't attach after an interrupted

Quoted 8 lines

initialization
tools/sched_ext: scx_pair: Reset pair_id on restart tools/sched_ext: scx_qmap: Add the missing options to the usage
synopsis
tools/sched_ext: scx_qmap: Reset the hier stats baseline on restart tools/sched_ext: scx_userland: Check dispatch buffer slots before
inserting
tools/sched_ext: scx_userland: Reject batch size 0

Applied 1, 5, 6, 7 and 8 to sched_ext/for-7.5, with a garbled sentence in 8's description fixed. I'm skipping 2, 3, 4 and 9 as they don't really make a meaningful difference.

Thanks.

--
tejun

A message that is not in the archive (sent off-list or not yet received): CA+XhMqzQXn4r57b+CP-mryhat3rtg3bD2BaRLqjjqwEj1eJ-EA@mail.gmail.com

author
#15 lore raw

Hello, David,

Quoted 69 lines · David CARLIER

Hi Tao

On Tue, 10 Nov 2026 at 12:50, Tao Cui <[email protected] <mailto:[email protected]>> wrote:

From: Tao Cui <[email protected] <mailto:[email protected]>>

Hello,

This series fixes nine robustness issues in the example schedulers scx_pair, scx_flatcg, scx_qmap and scx_userland.

scx_flatcg resets its stats baseline on restart (stale counters print as u64 underflow garbage) and rejects a stats interval that puts nanosleep in an error loop (a negative or non-finite -i busy-spins at 100% CPU). scx_pair stops aborting the scheduler when the dispatching cgroup goes away mid-dispatch, gates attach on an interrupted initialization instead of running with a half-built cgrp_q_arr, resets pair_id on restart, and cleans up a dead busy-check whose fall-through clears another cgroup's busy marker. scx_qmap documents the options it actually accepts and resets its hier stats baseline on restart. scx_userland bounds the batch against the dispatch buffer instead of overflowing it 13ms in, and rejects batch size 0, which livelocks the machine until the stall watchdog fires.

Verified in qemu on tj/sched_ext for-next (5b740a72b55c), same kernel and config with and without each patch, comparing stock and patched binaries: a scx_flatcg restart prints six u64-underflow garbage counters on stock and zeros with patch 1; a non-finite -i busy-spins ~17k EINVAL lines in 3s on stock and is rejected at parse time with patch 2; SIGINT storms abort 20/30 interrupted inits on stock with the scheduler never loading and exit 30/30 clean with patch 4; a scx_pair restart aborts 7ms in with a stale pair_id on stock and runs clean for its full window with patch 5; an oversized scx_userland batch aborts 13ms in with "dispatch buffer overflow" on stock and survives 25s with patch 9; and batch size 0, which livelocks the machine until the stall watchdog fires (spinner progress collapses to ~1%), is rejected immediately with patch 8. Beyond the A/B pairs: the scx_pair cgid-cache churn window is too narrow to drive (40 rounds clean on both binaries; patch 3 is graded by inspection), the qmap synopsis diff shows exactly the three added options (patch 6), and the hier stats baseline was verified with a clang-23.1.5-built scx_qmap run through restart cycles (patch 7, hier stats clean throughout).

Based on sched_ext/for-next (5b740a72b55c).

Tao Cui (9):
  tools/sched_ext: scx_flatcg: Reset the stats baseline on restart tools/sched_ext: scx_flatcg: Validate the stats interval tools/sched_ext: scx_pair: Don't abort when the dispatching cgroup goes away
  tools/sched_ext: scx_pair: Don't attach after an interrupted initialization
  tools/sched_ext: scx_pair: Reset pair_id on restart tools/sched_ext: scx_qmap: Add the missing options to the usage synopsis
  tools/sched_ext: scx_qmap: Reset the hier stats baseline on restart tools/sched_ext: scx_userland: Check dispatch buffer slots before inserting
  tools/sched_ext: scx_userland: Reject batch size 0

 tools/sched_ext/scx_flatcg.c       |  7 ++++++- tools/sched_ext/scx_pair.bpf.c     | 12 ++++++++++-- tools/sched_ext/scx_pair.c         | 14 ++++++++++++-- tools/sched_ext/scx_qmap.c         |  3 ++- tools/sched_ext/scx_userland.bpf.c |  8 ++++++++ tools/sched_ext/scx_userland.c     |  5 +++++ 6 files changed, 43 insertions(+), 6 deletions(-)

Quoted 8 lines

--
2.53.0

I might be missing something, but the cover letter mentions a scx_pair busy-check cleanup that isn't in the series, and the verification paragraph swaps patches 8 and 9 it seems.

hope it helps.

You're right on both counts. Thanks for the careful read.

The busy-check sentence is a leftover. The series originally included that cleanup, but I dropped it before posting because it became clear the path it removed has no observable behavioral difference (the would-be hijacking update always fails with -EBUSY anyway). I neglected to update the cover letter accordingly.

The patch 8/9 swap is also just an editing mistake from the same late rewrite of the verification paragraph.

Thanks,
Tao