sched: Fix SCX_ENQ_CPU_SELECTED for queued wakeups
Subsystems (3)
v2 cover sched: Fix SCX_ENQ_CPU_SELECTED for queued wakeups
SCX_ENQ_CPU_SELECTED is meant to tell ops.enqueue() whether ops.select_cpu() ran for a wakeup. However, wakeups delivered through the ttwu wakelist never have the flag set, because sched_ttwu_pending() drops WF_RQ_SELECTED. Schedulers that set SCX_OPS_ALLOW_QUEUED_WAKEUP therefore cannot tell whether ops.select_cpu() was skipped, and SCX_EV_SELECT_CPU_FALLBACK is undercounted.
Patch 1 carries WF_RQ_SELECTED across the wakelist the same way WF_MIGRATED already is. Patch 2 adds a selftest. In a 4-vCPU VM, without patch 1 the test reports that all of the roughly 100k queued wakeups in a 2s run had ops.select_cpu() called but SCX_ENQ_CPU_SELECTED clear. With patch 1 there are none.
Patch 1 changes kernel/sched/core.c and include/linux/sched.h, and Tejun has acked it, so this resend is rebased onto tip/sched/urgent.
The bug was found, and the series was drafted, with the help of an AI coding agent.
Resend of v2:
- Rebase onto tip/sched/urgent (f4eb2ace3efd); no code or commit-message changes.
Changes in v2:
- Patch 1: Reword the description to distinguish the wakelist path before ->select_task_rq() from the one after it, and fix the ttwu_do_activate() name (Andrea). Add Acked-by from Tejun and Reviewed-by from Andrea.
- Patch 2: Give each ping-pong task its own two CPUs and dispatch to SCX_DSQ_LOCAL. The waker and the wakee then always run on different CPUs, so the test no longer depends on tasks spreading out on their own (Sashiko). Skip on systems with fewer than four CPUs.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/r/[email protected]
Kuba Piecuch (2):
sched/core: Preserve WF_RQ_SELECTED across queued wakeups selftests/sched_ext: Test SCX_ENQ_CPU_SELECTED with queued wakeups
include/linux/sched.h | 6 +- kernel/sched/core.c | 10 +- tools/testing/selftests/sched_ext/Makefile | 1 + .../sched_ext/enq_cpu_selected.bpf.c | 111 ++++++++++ .../selftests/sched_ext/enq_cpu_selected.c | 194 ++++++++++++++++++ 5 files changed, 319 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/sched_ext/enq_cpu_selected.bpf.c create mode 100644 tools/testing/selftests/sched_ext/enq_cpu_selected.c
base-commit: f4eb2ace3efdd8b6e3b0946bbd316166edb1a51e
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH RESEND v2 1/2 sched/core: Preserve WF_RQ_SELECTED across queued wakeups
ENQUEUE_RQ_SELECTED tells a sched_class that ->select_task_rq() was called for a task wakeup. sched_ext exposes this flag to BPF schedulers as SCX_ENQ_CPU_SELECTED.
When a BPF scheduler sets SCX_OPS_ALLOW_QUEUED_WAKEUP, a wakeup can be delivered through the ttwu wakelist in two ways:
1) from the p->on_cpu path, before ->select_task_rq() is called. In this case WF_RQ_SELECTED is not set and SCX_ENQ_CPU_SELECTED is correctly clear;
2) from ttwu_queue(), after ->select_task_rq() has been called. In this case WF_RQ_SELECTED is set in wake_flags, but sched_ttwu_pending() only carries WF_MIGRATED across the wakelist, so the flag is dropped and ops.enqueue() is wrongly told that ops.select_cpu() was skipped. This also results in undercounting SCX_EV_SELECT_CPU_FALLBACK events.
Fix case 2 by preserving WF_RQ_SELECTED across the wakelist in the same way WF_MIGRATED is, so that ttwu_do_activate() passes ENQUEUE_RQ_SELECTED to activate_task(). Case 1 is unaffected.
--- include/linux/sched.h | 6 ++++-- kernel/sched/core.c | 10 +++++++++- 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/sched.h +4 -2
@@ -1008,7 +1008,7 @@ struct task_struct {/* Unserialized, strictly 'current' */ /* -* This field must not be in the scheduler word above due to wakelist +* These fields must not be in the scheduler word above due to wakelist * queueing no longer being serialized by p->on_cpu. However: * * p->XXX = X; ttwu()
@@ -1018,9 +1018,11 @@ struct task_struct {* p->on_rq = 0; p->sched_remote_wakeup = Y; * * guarantees all stores of 'current' are visible before -* ->sched_remote_wakeup gets used, so it can be in this word. +* ->sched_remote_wakeup and ->sched_rq_selected get used, so they can +* be in this word. */ unsigned sched_remote_wakeup:1; + unsigned sched_rq_selected:1;#ifdef CONFIG_RT_MUTEXES unsigned sched_rt_mutex:1;#endif
kernel/sched/core.c +9 -1
@@ -4000,13 +4000,20 @@ void sched_ttwu_pending(void *arg)update_rq_clock (rq);llist_for_each_entry_safe (p, t, llist, wake_entry.llist) { +int wake_flags =0 ; +if (WARN_ON_ONCE (p->on_cpu))smp_cond_load_acquire (&p->on_cpu, !VAL );if (WARN_ON_ONCE (task_cpu (p) !=cpu_of (rq)))set_task_cpu (p,cpu_of (rq)); -ttwu_do_activate (rq, p, p->sched_remote_wakeup ?WF_MIGRATED :0 , &rf); +if (p->sched_remote_wakeup) + wake_flags |=WF_MIGRATED ; +if (p->sched_rq_selected) + wake_flags |=WF_RQ_SELECTED ; + +ttwu_do_activate (rq, p, wake_flags, &rf); }/*
@@ -4050,6 +4057,7 @@ static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flagsstruct rq *rq =cpu_rq (cpu); p->sched_remote_wakeup = !!(wake_flags &WF_MIGRATED ); + p->sched_rq_selected = !!(wake_flags &WF_RQ_SELECTED );WRITE_ONCE (rq->ttwu_pending,1 );#ifdef CONFIG_SMP
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Tejun Heo
reviewerRe: [PATCH RESEND v2 1/2] sched/core: Preserve WF_RQ_SELECTED across queued wakeups
Hello, Peter.
On Mon, Nov 23, 2026 at 05:18:01PM +0000, Kuba Piecuch wrote:
...
Quoted 3 lines
Fix case 2 by preserving
WF_RQ_SELECTEDacross the wakelist in the same wayWF_MIGRATEDis, so thatttwu_do_activate()passesENQUEUE_RQ_SELECTEDtoactivate_task(). Case 1 is unaffected.
Like Tao's ENQUEUE_RQ_SELECTED patch, this one only affects sched_ext. Can I take it through sched_ext too?
Thanks.
--
tejun
PATCH RESEND v2 2/2 selftests/sched_ext: Test SCX_ENQ_CPU_SELECTED with queued wakeups
In reply to Kuba PiecuchAdd a test that checks that SCX_ENQ_CPU_SELECTED on a wakeup ops.enqueue() is set if and only if ops.select_cpu() ran for that wakeup. The scheduler sets SCX_OPS_ALLOW_QUEUED_WAKEUP so that queued wakeups are covered too.
ops.select_cpu() records in task storage that it ran and on which CPU. ops.enqueue() checks that record against SCX_ENQ_CPU_SELECTED. Pairs of tasks ping-pong over pipes to generate the wakeups. Each task is affined to its own two CPUs, so ops.select_cpu() is still called, but the waker and the wakee never share a CPU and the wakee's CPU has nothing else to run, which ttwu_queue_cond() requires for queueing within an LLC. Tasks stay on prev_cpu and are dispatched to the local DSQ. An enqueue on a CPU other than the one that ran ops.select_cpu() is counted as a queued wakeup. The test is skipped if there are fewer than four CPUs or if no queued wakeups are seen.
This test was written with the help of an AI coding assistant.
--- tools/testing/selftests/sched_ext/Makefile | 1 + .../sched_ext/enq_cpu_selected.bpf.c | 111 ++++++++++ .../selftests/sched_ext/enq_cpu_selected.c | 194 ++++++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/enq_cpu_selected.bpf.c create mode 100644 tools/testing/selftests/sched_ext/enq_cpu_selected.c
tools/testing/selftests/sched_ext/Makefile +1 -0
@@ -175,6 +175,7 @@ auto-test-targets := \ ddsp_vtimelocal_fail \ dsp_local_on \ enable_cmask \ + enq_cpu_selected \ enq_select_cpu \ exit \ hotplug \
tools/testing/selftests/sched_ext/enq_cpu_selected.bpf.c new file +111 -0
111 lines not on this page · raw message
tools/testing/selftests/sched_ext/enq_cpu_selected.c new file +194 -0
194 lines not on this page · raw message
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
[email protected]
AI reviewRe: [PATCH RESEND v2 2/2] selftests/sched_ext: Test SCX_ENQ_CPU_SELECTED with queued wakeups
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] selftests/sched_ext: pipe fd leak on error paths in spawn_pair() - [Low] selftests/sched_ext: missing…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] selftests/sched_ext: pipe fd leak on error paths in spawn_pair()
- [Low] selftests/sched_ext: missing skeleton cleanup on load failure in setup()
--
commit 53693d287118813912c899a1ad2d7a5cd66987f6
Author: Kuba Piecuch <[email protected]>
selftests/sched_ext: Test SCX_ENQ_CPU_SELECTED with queued wakeups
This commit adds a sched_ext selftest verifying that SCX_ENQ_CPU_SELECTED is set on wakeup ops.enqueue() calls if and only if ops.select_cpu() ran for that wakeup, including queued wakeups.
Quoted 6 lines
tools/testing/selftests/sched_ext/enq_cpu_selected.c new file +0
-0@@ -0,0 +1,194 @@
[ ... ]
Quoted 6 lines
tools/testing/selftests/sched_ext/enq_cpu_selected.c new file +6
-0+static enum scx_test_status setup (void **ctx) +{ +struct enq_cpu_selected *skel; + + nr_pairs =setup_masks (); +SCX_FAIL_IF (nr_pairs <0 ,"Failed to get CPU affinity" );
tools/testing/selftests/sched_ext/enq_cpu_selected.c line 68
Quoted 10 lines
tools/testing/selftests/sched_ext/enq_cpu_selected.c new file +10
-0+if (!nr_pairs) { +fprintf (stderr,"Needs at least %d CPUs, skipping\n" , +2 *CPUS_PER_TASK ); +return SCX_TEST_SKIP ; + } + + skel =enq_cpu_selected__open (); +SCX_FAIL_IF (!skel,"Failed to open" ); +SCX_ENUM_INIT (skel); +SCX_FAIL_IF (enq_cpu_selected__load (skel),"Failed to load skel" );
[Severity: Low]
Does this leak skel when enq_cpu_selected__load() fails? run_test() skips test->cleanup() whenever test->setup() does not return SCX_TEST_PASS, so the opened skeleton stays allocated across subsequent tests in the runner process.
[ ... ]
Quoted 6 lines
tools/testing/selftests/sched_ext/enq_cpu_selected.c new file +6
-0+static int spawn_pair (pid_t *pids,const cpu_set_t *pair_masks) +{ +int a2b[2 ], b2a[2 ]; + +if (pipe (a2b) ||pipe (b2a)) +return -errno;
tools/testing/selftests/sched_ext/enq_cpu_selected.c line 109
Quoted 10 lines
tools/testing/selftests/sched_ext/enq_cpu_selected.c new file +10
-0+ + pids[0 ] =fork (); +if (pids[0 ] <0 ) +return -errno; +if (!pids[0 ]) +pingpong (b2a[0 ], a2b[1 ], true, &pair_masks[0 ]); + + pids[1 ] =fork (); +if (pids[1 ] <0 ) +return -errno;
[Severity: Low]
Can these error paths leak the a2b and b2a pipe file descriptors? If pipe(b2a) fails, a2b[0..1] remain open, and if either fork() fails, all four descriptors remain open in the multi-test runner process.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2