sched_ext: Reenqueue IMMED tasks when a higher class wins the pick
Subsystems (3)
RFC PATCH 1/1 sched_ext: Reenqueue IMMED tasks when a higher class wins the pick
This is the SCX_ENQ_IMMED fix extracted from the ops.cpu_acquire/release() RFC, as requested by Tejun:
https://lore.kernel.org/all/[email protected]/
SCX_ENQ_IMMED (commit 98d709cba319 ("sched_ext: Implement SCX_ENQ_IMMED")) relies on rq->next_class to detect when a higher scheduling class is about to take the CPU: rq_is_open() rejects IMMED insertions while next_class is above ext, and wakeup_preempt_scx() schedules a reenqueue when a wakeup raises next_class.
Several paths make a higher-class task runnable or let it win the pick without passing through wakeup_preempt_scx(), leaving IMMED tasks stranded on the local DSQ until that task yields:
1) sched_setscheduler() or a PI boost promoting a queued task to RT or DL:
sched_change_end() does not call wakeup_preempt() for non-running tasks, switched_to_rt() only calls resched_curr(), and switched_to_dl() calls wakeup_preempt_dl() directly.
2) DL replenishment, CFS bandwidth unthrottling (in partial mode), and RT group unthrottling enqueue tasks without going through wakeup_preempt().
3) When the ext dl_server picks an SCX task, rq->next_class becomes ext even though RT tasks are queued; once the server exhausts its budget, RT takes over without a wakeup.
4) Under core scheduling, a forced-idle pick resets rq->next_class to idle while tasks remain on the local DSQ. A later RT wakeup on that CPU goes through wakeup_preempt_idle() instead of wakeup_preempt_scx().
With proxy execution enabled, scx_proxy_reenqueue_retry() already covers all of these: __schedule() sets rq->next_class from the picked task right before calling it, so it reenqueues IMMED tasks whenever a higher class wins the pick, whether or not that task went through wakeup_preempt(). Without proxy execution (CONFIG_SCHED_PROXY_EXEC=n, or sched_proxy_exec=0), nothing does.
Patch 1 adds scx_settle_pick() to __schedule(), after proxy resolution, on the path which every pick reaches, independent of proxy execution. When SCX is enabled, nr_immed is non-zero and rq->next_class (the donor's class under proxy execution) is above ext, it schedules a deferred local DSQ reenqueue. rq_is_open() is false when the reenqueue runs after the context switch, so IMMED tasks are returned to the BPF scheduler. Core-sched siblings apply remote picks in their own __schedule(), so they are covered too. The IMMED check in scx_proxy_reenqueue_retry() becomes redundant and is removed.
A forced idle CPU still doesn't reenqueue IMMED tasks. This matches rq_is_open(), which returns true while the CPU is forced idle, as rq->next_class is idle. The IMMED task at the head of the local DSQ waits for the next core pick.
Patch 2 adds the immed_strand selftest covering the core-scheduling forced idle case 4). It enables CONFIG_SCHED_CORE in the selftest config because forced idle is the only case above that strands an IMMED task deterministically without other non-default options. The promotion in 1), DL replenishment and the dl_server case 3) reschedule the CPU right away and only strand a task when racing with an insertion. CFS and RT group unthrottling need CONFIG_CFS_BANDWIDTH or CONFIG_RT_GROUP_SCHED. The test is skipped without SMT siblings or core scheduling. Without patch 1 and with proxy execution disabled (sched_proxy_exec=0), immed_strand fails with:
immed_strand: CPU 3: inserts=1 reenqs=0 hazards=1 strands=1 max=200419us Expected bss->nr_strands == 0 (1 == 0)
The patches were written with the help of an AI coding assistant.
Kuba Piecuch (2):
sched_ext: Reenqueue IMMED tasks when a higher class wins the pick selftests/sched_ext: Add immed_strand test
kernel/sched/core.c | 1 + kernel/sched/ext/ext.c | 53 ++- kernel/sched/ext/ext.h | 13 + tools/testing/selftests/sched_ext/Makefile | 1 + tools/testing/selftests/sched_ext/config | 1 + .../selftests/sched_ext/immed_strand.bpf.c | 174 ++++++++ .../selftests/sched_ext/immed_strand.c | 383 ++++++++++++++++++ 7 files changed, 617 insertions(+), 9 deletions(-) create mode 100644 tools/testing/selftests/sched_ext/immed_strand.bpf.c create mode 100644 tools/testing/selftests/sched_ext/immed_strand.c
base-commit: 97e1c7d467d07734f8f5c8c64730e1f7c415bd46
Signature · 1 line
2.56.1.839.g22a8290db9-goog
RFC PATCH 1/2 sched_ext: Reenqueue IMMED tasks when a higher class wins the pick
SCX_ENQ_IMMED relies on rq->next_class to tell whether a higher class task is about to take the CPU. rq_is_open() refuses IMMED insertions while next_class is above ext and wakeup_preempt_scx() reenqueues IMMED tasks when a higher class task raises next_class. However, a higher class task can take the CPU without going through wakeup_preempt(), leaving IMMED tasks on the local DSQ until it gives the CPU up:
- sched_setscheduler() or a PI boost promoting a queued task to RT or DL. sched_change_end() doesn't call wakeup_preempt() for tasks which aren't running, switched_to_rt() only calls resched_curr() and switched_to_dl() calls wakeup_preempt_dl() directly.
- DL replenishment, CFS bandwidth unthrottling (in partial mode) and RT group unthrottling make tasks runnable without wakeup_preempt().
- When the ext dl_server picks an SCX task, next_class becomes ext even though RT tasks are queued. Once the server runs out of budget, RT takes over without a wakeup.
- With core scheduling, a forced idle pick resets next_class to idle while tasks remain on the local DSQ. An RT task waking up on the CPU then goes through wakeup_preempt_idle() instead of wakeup_preempt_scx().
With proxy execution enabled, scx_proxy_reenqueue_retry() already catches these. __schedule() sets rq->next_class from the picked task right before calling it, so it reenqueues IMMED tasks whenever a higher class wins the pick, whether or not that task went through wakeup_preempt(). Without proxy execution (CONFIG_SCHED_PROXY_EXEC=n or sched_proxy_exec=0), nothing does.
Rather than chasing every path which can make a higher class task runnable, check once the pick is final, whether or not proxy execution is enabled. Add scx_settle_pick(), called by __schedule() after proxy resolution, on the path which every pick reaches. If rq->next_class is above SCX and the local DSQ has IMMED tasks, schedule a reenqueue. It runs right after the context switch. rq_is_open() is false by then, so the IMMED tasks go back to the BPF scheduler. With proxy execution, rq->next_class is the class of the donor, so a higher class donor running an SCX lock owner counts too. Core-sched siblings apply a pick made by another CPU in their own __schedule(), so they're covered as well.
This makes the IMMED check in scx_proxy_reenqueue_retry() redundant, so remove it. The reenqueue from wakeup_preempt_scx() stays, as it kicks in earlier for the common case. When no BPF scheduler is loaded, the new hook is a static branch.
--- kernel/sched/core.c | 1 + kernel/sched/ext/ext.c | 53 +++++++++++++++++++++++++++++++++++------- kernel/sched/ext/ext.h | 13 +++++++++++ 3 files changed, 58 insertions(+), 9 deletions(-)
kernel/sched/core.c +1 -0
@@ -7340,6 +7340,7 @@ static void __sched notrace __schedule(int sched_mode)clear_tsk_need_resched (prev);clear_preempt_need_resched (); keep_resched: +scx_settle_pick (rq); rq->last_seen_need_resched_ns =0 ; is_switch = prev != next;
kernel/sched/ext/ext.c +44 -9
76 lines not on this page · raw message
kernel/sched/ext/ext.h +13 -0
@@ -26,6 +26,7 @@ void scx_proxy_donor_start(struct rq *rq);void scx_proxy_reenqueue_retry (struct rq *rq,struct task_struct *next);void init_sched_ext_class (void );void __scx_update_idle (struct rq *rq,bool idle,bool do_notify); +void __scx_settle_pick (struct rq *rq);static inline bool scx_allow_proxy_exec (const struct task_struct *p) {
@@ -41,6 +42,17 @@ static inline void scx_update_idle(struct rq *rq, bool idle, bool do_notify)__scx_update_idle (rq, idle, do_notify); } +/* +* Called by __schedule() once the pick, including proxy resolution, is final. +* A pick which lands in a class above SCX can leave %SCX_ENQ_IMMED tasks behind +* on the local DSQ. See __scx_settle_pick(). +*/ +static inline void scx_settle_pick (struct rq *rq) +{ +if (scx_enabled ()) +__scx_settle_pick (rq); +} +static inline u32 scx_cpuperf_target (s32 cpu) {if (scx_enabled ())
@@ -80,6 +92,7 @@ static inline void scx_proxy_reenqueue_retry(struct rq *rq, struct task_struct *next) {}static inline void init_sched_ext_class (void ) {}static inline void scx_update_idle (struct rq *rq,bool idle,bool do_notify) {} +static inline void scx_settle_pick (struct rq *rq) {}#endif /* CONFIG_SCHED_CLASS_EXT */
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sched_ext: missed scx_settle_pick() on core-sched sibling in pick_next_task() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sched_ext: missed scx_settle_pick() on core-sched sibling in pick_next_task()
--
commit 6268089dba82c4845b6bc4c92ec88a503f333074
Author: Kuba Piecuch <[email protected]>
sched_ext: Reenqueue IMMED tasks when a higher class wins the pick
This commit adds scx_settle_pick() in __schedule() after task selection and proxy resolution complete so that SCX_ENQ_IMMED tasks on the local DSQ are reenqueued whenever rq->next_class ends up above ext_sched_class, replacing the proxy-only check in scx_proxy_reenqueue_retry().
kernel/sched/core.c static void __sched notrace __schedule(int sched_mode) · line 7395
Quoted 12 lines
kernel/sched/core.c +1
-0@@ -7389,6 +7389,7 @@ static void __sched notrace __schedule(int sched_mode)clear_tsk_need_resched (prev);clear_preempt_need_resched (); keep_resched: +scx_settle_pick (rq); rq->last_seen_need_resched_ns =0 ; is_switch = prev != next;
[Severity: Medium]
With CONFIG_SCHED_CORE, can a sibling CPU miss scx_settle_pick() and leave SCX_ENQ_IMMED tasks stranded on its local DSQ when cookie matching falls back to its currently running higher-class task?
During a core-wide pick in pick_next_task(), pick_task(rq_i, rf) on a sibling running a higher-class task with core_cookie C can run ext_server_pick_task() -> do_pick_task_scx(). That lowers rq_i->next_class to &ext_sched_class via rq_modified_begin() and allows ops.dispatch() to insert an SCX_ENQ_IMMED task with a different cookie onto rq_i->scx.local_dsq. If another sibling's task with cookie C wins max and sched_core_find(rq_i, C) selects rq_i->curr again, pick_next_task() skips rescheduling rq_i:
kernel/sched/core.c:pick_next_task() {
...
if (rq_i->curr == rq_i->core_pick) {
rq_i->core_pick = NULL;
rq_i->core_dl_server = NULL;
continue;
}
resched_curr(rq_i);
...
}Because rq_i never enters __schedule(), rq_i->next_class is not restored to rq_i->curr->sched_class and scx_settle_pick(rq_i) never runs, leaving the IMMED task on rq_i->scx.local_dsq while the higher-class task continues running and causing subsequent rq_is_open(rq_i, ...) checks to see rq_i->next_class == &ext_sched_class.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH 2/2 selftests/sched_ext: Add immed_strand test
In reply to Kuba PiecuchAdd a test which strands an SCX_ENQ_IMMED task behind a higher class task that takes the CPU without going through wakeup_preempt_scx().
Under core scheduling, an IMMED task gets forced idle by an RT task with a different cookie running on the SMT sibling. The forced idle pick resets rq->next_class to idle with the IMMED task still on the local DSQ. An RT task sharing the sibling's cookie then wakes up on the forced idle CPU, goes through wakeup_preempt_idle() and wins the pick.
The scheduler runs in partial mode and always inserts the test task into the test CPU's local DSQ with SCX_ENQ_IMMED. When the task gets reenqueued, it's parked and moved back with IMMED on the next dispatch. The time the IMMED task spends on the local DSQ while a higher class task runs is tracked through ops.running(), the reenqueue and the sched_switch tracepoint. The test requires the RT task to be picked with the IMMED task queued, the IMMED task never to wait for more than 5ms and the scheduler not to exit.
The test doesn't cover the other ways a higher class task can take the CPU without wakeup_preempt_scx(): sched_setscheduler() or PI boosting of a queued task, DL replenishment, bandwidth unthrottling and the ext dl_server running out of budget. They reschedule the CPU right away and only strand IMMED tasks when racing with an insertion.
Enable CONFIG_SCHED_CORE in the selftest config, as the test relies on core-sched forced idle. It is skipped without SMT siblings or core scheduling.
--- tools/testing/selftests/sched_ext/Makefile | 1 + tools/testing/selftests/sched_ext/config | 1 + .../selftests/sched_ext/immed_strand.bpf.c | 174 ++++++++ .../selftests/sched_ext/immed_strand.c | 383 ++++++++++++++++++ 4 files changed, 559 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/immed_strand.bpf.c create mode 100644 tools/testing/selftests/sched_ext/immed_strand.c
tools/testing/selftests/sched_ext/Makefile +1 -0
@@ -178,6 +178,7 @@ auto-test-targets := \ enq_select_cpu \ exit \ hotplug \ + immed_strand \ init_enable_count \ kick \ maximal \
tools/testing/selftests/sched_ext/config +1 -0
@@ -3,6 +3,7 @@ CONFIG_CGROUPS=y CONFIG_CGROUP_SCHED=y CONFIG_CPUSETS=y CONFIG_EXT_GROUP_SCHED=y +CONFIG_SCHED_CORE=y CONFIG_BPF=y CONFIG_BPF_SYSCALL=y CONFIG_DEBUG_INFO=y
tools/testing/selftests/sched_ext/immed_strand.bpf.c new file +174 -0
174 lines not on this page · raw message
tools/testing/selftests/sched_ext/immed_strand.c new file +383 -0
383 lines not on this page · raw message
Signature · 1 line
2.56.1.839.g22a8290db9-goog