sched: Make proxy execution compatible with sched_ext
Subsystems (4)
Andrea Righi
reviewerPATCH v12 1/1 sched: Make proxy execution compatible with sched_ext
This series enables proxy execution with sched_ext and is based on early work by John Stultz [1].
Background
==========
Proxy execution (proxy-exec) lets a waiting task ("donor") donate its scheduling context to a mutex owner, so the owner can run while the donor stays eligible on the runqueue.
Currently, proxy-exec and sched_ext are mutually exclusive at build time: we can't enable CONFIG_SCHED_PROXY_EXEC=y and CONFIG_SCHED_CLASS_EXT=y in the same kernel.
This restriction can be problematic for Linux distributions and for anyone who wants to ship one kernel and choose features at runtime.
Why are they mutually exclusive?
================================
sched_ext schedulers drive dispatch through their own interfaces. A proxy-exec handoff can run a task that the BPF scheduler never dispatched through that path. sched_ext callbacks then observe a "current" task that does not match what the BPF side considers running, so kfuncs and helper state can see an inconsistent view of the executing task.
sched_ext also tracks runnable work through Dispatch Queues (DSQs) and BPF chosen dispatch rules, while the core scheduler still maintains classic per-CPU runqueues and pick paths. A proxy handoff can therefore switch the CPU to a task that the BPF scheduler never inserted or ordered through its DSQ interface.
DSQ state, vtime, and "who is running" bookkeeping inside the BPF program can then disagree with what the core actually executes, so helpers and kfuncs that assume their dispatched task is current may observe stale or inconsistent state.
Design: supporting proxy-exec with sched_ext
============================================
Provide proxy-exec in sched_ext as optional per-scheduler capability: a BPF scheduler can set the ops flag SCX_OPS_ENQ_BLOCKED to keep mutex-blocked donors runnable and receive them through ops.enqueue() with SCX_ENQ_BLOCKED set.
This flag requires an ops.enqueue() callback and gives BPF control over whether, where, and in what order to dispatch each donor. The core then walks the mutex-owner chain and, when needed, migrates the donor's scheduling context to the owner's rq before executing the owner.
Without the SCX_OPS_ENQ_BLOCKED flag, mutex waiters block normally and do not participate in proxy-exec while owned by that scheduler.
Once BPF dispatches a donor, proxy execution may move its scheduling context to the mutex owner's rq. This move is distinct from BPF placement: wake_cpu continues to identify the donor's callback CPU, while task_cpu() identifies the proxy CPU. When the donor wakes, the normal wakeup path uses wake_cpu as prev_cpu when returning the task to the BPF scheduler, which can then choose its next placement normally.
The donor-to-owner handoff is modeled like a "function call" from the scheduler's perspective. The donor remains the running scheduling entity selected by BPF: its scheduling context, runtime and slice are consumed while the core temporarily invokes the mutex owner's code to make the critical section progress. It is not a scheduler-visible switch to the owner.
Accordingly, the donor remains the scheduling context presented to sched_ext, while the mutex owner is treated solely as the execution context selected internally by the core scheduler. Scheduling state is accounted against rq->donor where appropriate, while rq->curr identifies the execution context. The internal owner substitution does not generate synthetic sched_ext callbacks for a task that BPF did not dispatch.
Scheduler-facing current-task kfuncs follow the same rule:scx_bpf_task_running(), scx_bpf_cpu_curr() and scx_bpf_cid_curr() report the donor selected by the scheduler rather than the mutex owner whose code happens to execute on its behalf.
The sched_ext callback bookkeeping is adjusted accordingly. A blocked donor enters a tracked ops.running()/ops.stopping() session only after proxy resolution succeeds, and ops.stopping() is only called for tasks that entered such a session. This state is independent of physical rq->curr execution, which may refer to the mutex owner instead of the donor.
NOHZ CFS bandwidth checks also follow rq->donor rather than rq->curr and use the number of queued FAIR tasks when deciding whether the tick can stop. This keeps bandwidth enforcement active for a constrained FAIR donor even when its proxy owner remains runnable in another scheduling class.
Scheduler ownership changes need special handling because a donor may already be blocked when a root scheduler is enabled, when a task moves between root and sub-schedulers, or when a class transition moves it into or out of EXT. Start every scheduling-class or BPF-scheduler ownership change from clean task state:
fully deactivate a retained donor before the transition and let the incoming scheduler apply its own admission policy the next time the task blocks. This conservative rule leaves retaining proxy state across compatible RT/DL PI transitions as a future improvement.
Remote DSQ transfers recheck proxy-sensitive state after locking the source runqueue. If a transfer races with proxy execution, the task is parked on the source runqueue's reject DSQ and returned to its owning BPF scheduler after proxy resolution settles. Ordinary migration-disabled tasks still reach the normal validation path, where an illegal BPF-directed migration is reported. If an affinity migration is pending, the task remains parked until the affinity machinery completes the operation. This preserves scheduler placement and sub-scheduler capability rules without migrating an active execution context.
scx_qmap has been modified with a -X option to enable queueing mutex-blocked tasks for proxy-exec. Blocked donors receive a fresh slice, are inserted at the head of their current cid's local DSQ and request immediate preemption. This is an intentionally aggressive policy for making proxy-exec easy to observe. With SCX_OPS_ALWAYS_ENQ_IMMED, a blocked donor which is returned through ops.enqueue() with SCX_ENQ_REENQ falls back to the shared DSQ, avoiding a loop which would repeatedly return it from the same local DSQ. A new statistic reports direct blocked-donor dispatch attempts.
A new kselftest (enq_blocked) is also introduced to validate proxy-exec with sched_ext. The test creates a priority inversion with a low-priority owner (nice +19), a high-priority donor (nice -20) and one nice 0 contender per available CPU. It exercises a same-CPU topology and a cross-CPU topology, where the donor and owner run on different CPUs. Each configuration runs with SCX_OPS_ENQ_BLOCKED first disabled and then enabled, counts blocked-donor enqueues by CPU and reports mutex hold/wait-time deltas. Access to the mutex is provided by a loadable kernel module built via TEST_GEN_MODS_DIR, with the test responsible for its lifecycle.
Example kselftest run:
===== START =====
TEST: enq_blocked
DESCRIPTION: Verify proxy donor admission under CPU-wide contention OUTPUT:
[topology=same-cpu SCX_OPS_ENQ_BLOCKED=disabled]
proxy_exec=enabled
donor_cpu=0
owner_cpu=0
nr_contenders=16
measured_trials=10
owner_nice=19
donor_nice=-20
contender_nice=0 mutex_hold_avg_ns=263587544 (263.587 ms, samples=10) mutex_wait_avg_ns=263597934 (263.597 ms, samples=10) nr_blocked_enqueues=0 nr_blocked_enqueues_donor_cpu=0 nr_blocked_enqueues_owner_cpu=0 nr_blocked_enqueues_other_cpu=0 nr_blocked_wakeups=0
[topology=same-cpu SCX_OPS_ENQ_BLOCKED=enabled]
proxy_exec=enabled
donor_cpu=0
owner_cpu=0
nr_contenders=16
measured_trials=10
owner_nice=19
donor_nice=-20
contender_nice=0 mutex_hold_avg_ns=251789178 (251.789 ms, samples=10) mutex_wait_avg_ns=209808599 (209.808 ms, samples=10) nr_blocked_enqueues=50 nr_blocked_enqueues_donor_cpu=50 nr_blocked_enqueues_owner_cpu=0 nr_blocked_enqueues_other_cpu=0 nr_blocked_wakeups=0
[topology=same-cpu delta: enabled - disabled]
mutex_hold_delta_ns=-11798366 (-4.48%)
mutex_wait_delta_ns=-53789335 (-20.41%)
[topology=cross-cpu SCX_OPS_ENQ_BLOCKED=disabled]
proxy_exec=enabled
donor_cpu=0
owner_cpu=1
nr_contenders=16
measured_trials=10
owner_nice=19
donor_nice=-20
contender_nice=0 mutex_hold_avg_ns=246794454 (246.794 ms, samples=10) mutex_wait_avg_ns=247298497 (247.298 ms, samples=10) nr_blocked_enqueues=0 nr_blocked_enqueues_donor_cpu=0 nr_blocked_enqueues_owner_cpu=0 nr_blocked_enqueues_other_cpu=0 nr_blocked_wakeups=0
[topology=cross-cpu SCX_OPS_ENQ_BLOCKED=enabled]
proxy_exec=enabled
donor_cpu=0
owner_cpu=1
nr_contenders=16
measured_trials=10
owner_nice=19
donor_nice=-20
contender_nice=0 mutex_hold_avg_ns=215486809 (215.486 ms, samples=10) mutex_wait_avg_ns=215498090 (215.498 ms, samples=10) nr_blocked_enqueues=80 nr_blocked_enqueues_donor_cpu=20 nr_blocked_enqueues_owner_cpu=60 nr_blocked_enqueues_other_cpu=0 nr_blocked_wakeups=0
[topology=cross-cpu delta: enabled - disabled]
mutex_hold_delta_ns=-31307645 (-12.69%)
mutex_wait_delta_ns=-31800407 (-12.86%)
ok 1 enq_blocked #
===== END =====
=============================
RESULTS:
PASSED: 1 SKIPPED: 0 FAILED: 0
References
==========
[1] https://lore.kernel.org/all/[email protected]
Git tree: git://git.kernel.org/pub/scm/linux/kernel/git/arighi/linux.git scx-proxy-exec
Changes in v12:
- Preserve a fresh re-enqueue reason when ops.enqueue() synchronously rejects a task again and clear the consumed reason only after the task leaves the reject DSQ (sashiko)
- Immediately process tasks rejected after becoming migration-disabled during an rq-lock handoff, leaving only active proxy contexts parked (sashiko)
- Use the donor scheduling context for protected-slice admission, rescue preemption, slice save/restore and bypass handling, and charge rescue runtime to the rescued task's scheduler (sashiko)
- Refresh the NOHZ tick dependency after rq->donor changes in __schedule(), preventing a stale constrained FAIR donor from keeping the tick enabled for an incoming RT task (sashiko)
- Clarify that ops.running()/ops.stopping() track the donor's scheduling context including blocked donors after successful proxy resolution (sashiko)
- Restore the change that leaves mutex proxy-lock scopes before calling proxy_resched_idle() (required to avoid scheduling-class callbacks under the extra raw spinlocks)
- Dequeue waking proxy donors before replacing rq->donor references avoiding callbacks under blocked_lock and transient donor re-enqueueing
- Harden scx_qmap blocked-donor fast paths against missing eligible self cids
- Move SCX_TASK_ENQ_WAKEUP to bit 16, leaving room for task-state and re-enqueue-reason expansion
- Link to v11: https://lore.kernel.org/all/[email protected]/
Changes in v11:
- End retained proxy execution before every scheduling-class or BPF-scheduler ownership change, passing the incoming class to sched_change_begin() and removing the prepare_switch() class callback (Tejun Heo)
- Document the conservative class-transition rule and leave retaining proxy state across compatible RT/DL PI transitions for future work (Tejun Heo)
- Split reject-DSQ code movement from its generalization and carry the re-enqueue reason directly in p->scx.flags (Tejun Heo)
- Use one SCX_TASK_REENQ_PROXY reason for all proxy-raced remote transfers, clarify why proxy migration differs from BPF-directed migration and keep tasks parked while affinity migration is pending (Tejun Heo)
- Factor local-DSQ diversion cleanup into a shared helper, clear HEAD, IMMED, PREEMPT and carried-slice state for proxy rejection (Tejun Heo)
- Explain the scx_qmap SCX_OPS_ALWAYS_ENQ_IMMED re-enqueue fallback (Tejun Heo)
- Rename the blocked-donor command-line option to -X, since -B is now taken for the rescue bandwidth setting
- Drop the SCHED_FLAG_KEEP_PARAMS preparatory fix (this is addressed separately by https://lore.kernel.org/all/[email protected])
- Link to v10: https://lore.kernel.org/all/[email protected]/
Changes in v10:
- Check constrained FAIR proxy donors before the RT fast paths in sched_can_stop_tick(), so a throttled RT owner cannot bypass bandwidth enforcement (sashiko)
- Add a preparatory fix that sets DEQUEUE_CLASS only when SCHED_FLAG_KEEP_PARAMS permits the scheduling class to change, preventing class-transition callbacks from running without an actual transition (sashiko)
- Update try_to_block_task() comment to describe the sched_ext blocked-donor admission check (sashiko)
- Link to v9: https://lore.kernel.org/all/[email protected]/
Changes in v9:
- Add an incoming-class prepare_switch() callback and use it to block retained donors before transitions into sched_ext (Tejun Heo)
- Generalize the per-rq reject DSQ and use it for proxy-raced remote transfers instead of bouncing tasks to a global DSQ (Tejun Heo)
- Restrict migration-disabled warning exception to blocked donors whose scheduling context was actually proxy-migrated (sashiko)
- Keep ops.tick() and other donor accounting inside a tracked ops.running()/ops.stopping() session (sashiko)
- Prevent BPF-directed migration of an active proxy donor and use the donor as the current scheduling context during restore (sashiko)
- Update sched_fair_update_stop_tick() to use the number of queued FAIR tasks (sashiko)
- Fold the temporary local-DSQ donor placement into the final BPF-controlled admission change
- Link to v8: https://lore.kernel.org/all/[email protected]/
Changes in v8:
- Rework remote-DSQ migration check so the locked re-check handles only proxy-exec state changes (Tejun Heo)
- Use the current-donor helper when preventing migration of an active donor (sashiko)
- Handle blocked-donor reactivation into scx schedulers without SCX_OPS_ENQ_BLOCKED (John Stultz)
- Document sched_ext callback behavior under proxy execution (sashiko)
- Avoid redispatching a rejected blocked donor to the same local DSQ in scx_qmap (sashiko)
- Add a preparatory fix to avoid false migration warnings when proxy-exec moves a migration-disabled donor
- Skip the enq_blocked kselftest when proxy execution is unavailable
- Link to v7: https://lore.kernel.org/all/[email protected]/
Changes in v7:
- Move the remote CPU check out of the lockless remote-DSQ scan and perform it only after locking the source rq, retain the locked migration recheck immediately before moving the task (sashiko)
- Complete the scheduler-context conversion from rq->curr to rq->donor for capability revocation and preemption checks, make scx_bpf_task_running(), scx_bpf_cpu_curr() and scx_bpf_cid_curr() expose the donor selected by the scheduler (sashiko)
- Handle SCX_ENQ_REENQ before scx_qmap's blocked-donor fast path to avoid an infinite reject/re-enqueue loop (sashiko)
- Add scx_qmap stat for SCX_ENQ_BLOCKED dispatches
- Link to v6: https://lore.kernel.org/all/[email protected]/
Changes in v6:
- Add a scheduler-core fix to make NOHZ CFS bandwidth checks follow the proxy donor instead of the physical execution context (sashiko)
- Block retained donors when entering sched_ext through PI de-boosting or global scheduler activation (sashiko)
- Distinguish ordinary wakeup activations from retained-donor admissions so SCX_ENQ_BLOCKED is not reported for normal wakeups; extend the selftest to detect this regression (sashiko)
- Validate unexpected-CPU blocked-donor enqueues in both same-CPU and cross-CPU test topologies (sashiko)
- Link to v5: https://lore.kernel.org/all/[email protected]/
Changes in v5:
- Split retained-donor deactivation and sched_ext's default rejection into preparatory patches so the scheduler-core changes can be routed separately
- Drop the proxy destination query kfuncs and the preparatory mutex lock-scope change (John Stultz)
- Use p->is_blocked instead of task_is_blocked() to fix a WARN triggered during scx_pair testing (John Stultz)
- Rename SCX_TASK_IS_RUNNING to SCX_TASK_RUN_TRACKED and document that it tracks an ops.running()/stopping() session rather than physical rq->curr execution
- Keep a proxy-migrated blocked donor on the owner's rq until wakeup instead of allowing BPF-directed migration to pull it back to wake_cpu and cause repeated donor migration
- Extend kselftest to cover same-CPU and cross-CPU proxy-exec switches
- Make scx_qmap insert blocked donors at the head of their current cid's local DSQ and request immediate preemption
- Link to v4: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- Harden scx_bpf_task_proxy_cpu() locking and blocked-state validation, return -ENOENT for unrunnable owners and drop unnecessary donor-affinity checks (K Prateek Nayak, sashiko)
- Reschedule remote CPUs after donor deactivation, handle sched_setscheduler() admission and assert scheduler-change locking (sashiko)
- Avoid a potential KCSAN data-race report in the lockless blocked-donor migration check by using rcu_access_pointer() for rq->donor (sashiko)
- Fix tick dependency updates for incoming EXT contexts and keep the tick enabled for blocked donors (sashiko)
- Dump EXT donors in scx_dump_state() (sashiko)
- Reordered the preparatory sched_ext changes so real running-state tracking is established before donor-based accounting, and split the proxy destination query kfuncs into a separate patch
- Add compatibility wrappers for the proxy CPU/cid kfuncs and document their results as scheduling hints
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Dropped the core restrictions on proxy-migrating migration-disabled and single-CPU donors: proxy execution moves the scheduling context, not the task's execution context. (Peter Zijlstra, K Prateek Nayak)
- Dropped the sched_ext-specific put_prev_task()/set_next_task() exception and fixed ops.running()/ops.stopping() pairing inside sched_ext instead; track a real running transition even when either callback is absent. (Peter Zijlstra)
- Dropped the kf_tasks[] nesting and nested ops.runnable() patches from v2; extensive proxy-exec testing did not reproduce task-op re-entry, so retain the existing non-nesting invariant.
- Replaced scx_bpf_task_is_blocked() with SCX_ENQ_BLOCKED in ops.enqueue() flags, identifying blocked-donor admission directly at enqueue time.
- Expanded the curr/donor description to cover mixed scheduling classes and fixed local preemption to expire rq->donor's slice. (Aiqun Maria Yu)
- Allow inactive blocked donors to be placed on a remote local DSQ while preventing normal migration of an active rq donor. (Aiqun Maria Yu)
- Deactivate retained donors when ownership changes to a root or sub-scheduler without SCX_OPS_ENQ_BLOCKED, and extend the selftest to cover attaching a scheduler after the donor blocks. (K Prateek Nayak)
- Harden remote DSQ consumption by rejecting active tasks and rechecking migration eligibility after locking the source rq. Fall back to the global DSQ without treating an eligibility change during the lock handoff as a BPF scheduler error.
- scx_qmap has a command line option (-B) to enable blocked-donor queueing
- Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Rebased onto sched_ext/for-7.3 and adapted the series to the split sched_ext implementation and cid-form scheduler interfaces.
- Replaced the global sched_proxy_exec_scx boot-time opt-in with the per-scheduler SCX_OPS_ENQ_BLOCKED capability, allowing BPF to control donor admission and ordering through ops.enqueue().
- Added scx_bpf_task_is_blocked(), scx_bpf_task_proxy_cpu(), and scx_bpf_task_proxy_cid(); enforce CPU/cid API separation for cid-form schedulers.
- Added proxy exec support to scx_qmap, including optional owner-cid steering, affinity validation, and fallback to the donor's current cid.
- Added a kselftest with a kernel mutex test and a three-task priority inversion workload, test is executed with blocked task admission disabled and enabled, validates the behavior, and reports hold/wait-time deltas.
- Link to v1: https://lore.kernel.org/all/[email protected]/
Andrea Righi (17):
sched/core: Drop mutex locks before proxy rescheduling sched/core: Dequeue waking proxy donors before reset
sched: Make NOHZ CFS bandwidth checks follow proxy donor sched/core: Avoid false migration warning for proxy donors
sched: Pass next class to sched_change_begin()
sched: Add helper to block retained proxy donors
sched: Add sched_ext hooks for proxy execution
sched_ext: Block proxy donors across scheduler transitions
sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
sched_ext: Move reject DSQ draining into core
sched_ext: Generalize the reject DSQ reenqueue path
sched_ext: Handle proxy-exec races in remote DSQ transfers
sched_ext: Split curr|donor references properly
sched_ext: Delegate proxy donor admission to BPF schedulers
sched_ext: Add selftest for blocked donor admission
sched_ext: scx_qmap: Add proxy execution support
sched: Allow enabling proxy exec with sched_ext
Documentation/scheduler/sched-ext.rst | 13 + include/linux/sched/ext.h | 4 + init/Kconfig | 2 - kernel/sched/core.c | 146 +++- kernel/sched/ext/ext.c | 574 ++++++++++--- kernel/sched/ext/ext.h | 6 + kernel/sched/ext/internal.h | 37 +- kernel/sched/ext/sub.c | 70 +- kernel/sched/ext/sub.h | 13 +- kernel/sched/fair.c | 12 +- kernel/sched/sched.h | 18 +- kernel/sched/syscalls.c | 4 +- tools/sched_ext/include/scx/compat.h | 1 + tools/sched_ext/include/scx/enum_defs.autogen.h | 4 + tools/sched_ext/include/scx/enums.autogen.bpf.h | 3 + tools/sched_ext/include/scx/enums.autogen.h | 1 + tools/sched_ext/scx_qmap.bpf.c | 40 +- tools/sched_ext/scx_qmap.c | 13 +- tools/sched_ext/scx_qmap.h | 1 + tools/testing/selftests/sched_ext/.gitignore | 4 + tools/testing/selftests/sched_ext/Makefile | 2 + tools/testing/selftests/sched_ext/config | 2 + .../testing/selftests/sched_ext/enq_blocked.bpf.c | 116 +++ tools/testing/selftests/sched_ext/enq_blocked.c | 917 +++++++++++++++++++++ tools/testing/selftests/sched_ext/enq_blocked.h | 28 + .../selftests/sched_ext/test_modules/Makefile | 13 + .../sched_ext/test_modules/scx_enq_blocked_test.c | 195 +++++ 27 files changed, 2024 insertions(+), 215 deletions(-) create mode 100644 tools/testing/selftests/sched_ext/enq_blocked.bpf.c create mode 100644 tools/testing/selftests/sched_ext/enq_blocked.c create mode 100644 tools/testing/selftests/sched_ext/enq_blocked.h create mode 100644 tools/testing/selftests/sched_ext/test_modules/Makefile create mode 100644 tools/testing/selftests/sched_ext/test_modules/scx_enq_blocked_test.c
Andrea Righi
reviewerPATCH 1/17 sched/core: Drop mutex locks before proxy rescheduling
find_proxy_task() can call proxy_resched_idle() from return expressions inside mutex wait_lock and blocked task lock guard scopes. Return expressions are evaluated before scoped guards are released, so proxy_resched_idle() invokes scheduling-class callbacks while both raw spinlocks are held.
Route these cases through a common label outside the guard scopes. The common label is required because a direct return would evaluate proxy_resched_idle() before guard cleanup and run the class callbacks under the proxy-exec locks again.
This is a preparatory change to support proxy execution with sched_ext.
--- kernel/sched/core.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/sched/core.c +6 -4
@@ -6955,7 +6955,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)if (!READ_ONCE (owner->on_rq) || owner->se.sched_delayed) {/* XXX Don't handle blocked owners/delayed dequeue yet */ if (curr_in_chain) -return proxy_resched_idle (rq); + goto resched_idle;__clear_task_blocked_on (p,NULL ); goto deactivate; }
@@ -6967,7 +6967,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)* and leave that CPU to sort things out. */ if (curr_in_chain) -return proxy_resched_idle (rq); + goto resched_idle; goto migrate_task; }
@@ -6980,7 +6980,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)* case we should end up back in find_proxy_task(), this time * hopefully with all relevant tasks already enqueued. */ -return proxy_resched_idle (rq); + goto resched_idle; }/*
@@ -7017,7 +7017,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)* So schedule rq->idle so that ttwu_runnable() can get the rq * lock and mark owner as running. */ -return proxy_resched_idle (rq); + goto resched_idle; }/* * OK, now we're absolutely sure @owner is on this
@@ -7029,6 +7029,8 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)WARN_ON_ONCE (owner && !owner->on_rq);return owner; +resched_idle: +return proxy_resched_idle (rq); deactivate:proxy_deactivate (rq, p);return NULL ;
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 2/17 sched/core: Dequeue waking proxy donors before reset
In reply to Andrea Righiproxy_needs_return() resets an active donor while holding blocked_lock. proxy_reset_donor() invokes scheduling-class callbacks, adding an unnecessary raw-spinlock nesting. It also presents the waking donor to put_prev_task() as still runnable immediately before block_task() removes it from the runqueue.
Split block_task() so the waking donor can first be dequeued from its scheduling class. Release blocked_lock, dequeue the donor while its generic on_rq state still prevents migration, replace all donor references, and only then complete the generic runqueue removal. This follows the normal sleep ordering and avoids transiently re-enqueuing the waking donor.
This is a preparatory change to support proxy execution with sched_ext.
--- kernel/sched/core.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)
kernel/sched/core.c +24 -7
@@ -2253,7 +2253,8 @@ void deactivate_task(struct rq *rq, struct task_struct *p, int flags)dequeue_task (rq, p, flags); } -static void block_task (struct rq *rq,struct task_struct *p,unsigned long task_state) +static bool dequeue_block_task (struct rq *rq,struct task_struct *p, +unsigned long task_state) {int flags =DEQUEUE_NOCLOCK ;
@@ -2274,9 +2275,15 @@ static void block_task(struct rq *rq, struct task_struct *p, unsigned long task_* * Where __schedule() and ttwu() have matching control dependencies. * -* After this, schedule() must not care about p->state any more. +* Once the caller invokes __block_task(), schedule() must not care about +* p->state any more. */ -if (dequeue_task (rq, p,DEQUEUE_SLEEP | flags)) +return dequeue_task (rq, p,DEQUEUE_SLEEP | flags); +} + +static void block_task (struct rq *rq,struct task_struct *p,unsigned long task_state) +{ +if (dequeue_block_task (rq, p, task_state))__block_task (rq, p); }
@@ -3775,6 +3782,8 @@ static inline void proxy_reset_donor(struct rq *rq)*/ static inline bool proxy_needs_return (struct rq *rq,struct task_struct *p) { +bool reset_donor = false; +/* * Typically per __set_task_cpu(), task_cpu(p) == p->wake_cpu. *
@@ -3798,11 +3807,19 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)if (task_current (rq, p))return false; -/* If we're return migrating the rq->donor, switch it out for idle */ -if (task_current_donor (rq, p)) - proxy_reset_donor(rq); + reset_donor = task_current_donor(rq, p); + } + +if (reset_donor) { +bool dequeued =dequeue_block_task (rq, p,TASK_WAKING ); + +/* Keep on_rq set until all donor references have been replaced. */ +proxy_reset_donor (rq); +if (dequeued) +__block_task (rq, p); + }else { +block_task (rq, p,TASK_WAKING ); } -block_task (rq, p,TASK_WAKING );return true; }#else /* !CONFIG_SCHED_PROXY_EXEC */
Signature · 1 line
2.55.0
Tejun Heo
reviewerRe: [PATCH 02/17] sched/core: Dequeue waking proxy donors before reset
kernel/sched/core.c static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p) · line 3821
Quoted 9 lines · Andrea Righi
kernel/sched/core.c +9
-0+if (reset_donor) { +bool dequeued =dequeue_block_task (rq, p,TASK_WAKING ); + +/* Keep on_rq set until all donor references have been replaced. */ +proxy_reset_donor (rq); +if (dequeued) +__block_task (rq, p); + }else { +block_task (rq, p,TASK_WAKING );
nitpick, would the following be easier to follow?
bool dequeued = dequeue_block_task(rq, p, TASK_WAKING);
/* Keep on_rq set until all donor references have been replaced. */
if (reset_donor)
proxy_reset_donor(rq);
if (dequeued)
__block_task(rq, p);Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerPATCH v2 1/1 sched/core: Dequeue waking proxy donors before reset
proxy_needs_return() resets an active donor while holding blocked_lock. proxy_reset_donor() invokes scheduling-class callbacks, adding an unnecessary raw-spinlock nesting. It also presents the waking donor to put_prev_task() as still runnable immediately before block_task() removes it from the runqueue.
Split block_task() so the waking donor can first be dequeued from its scheduling class. Release blocked_lock, dequeue the donor while its generic on_rq state still prevents migration, replace all donor references, and only then complete the generic runqueue removal. This follows the normal sleep ordering and avoids transiently re-enqueuing the waking donor.
This is a preparatory change to support proxy execution with sched_ext.
---
Changes in v2:
- Simplify the proxy_needs_return() control flow (Tejun Heo)
kernel/sched/core.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)
kernel/sched/core.c +23 -7
@@ -2253,7 +2253,8 @@ void deactivate_task(struct rq *rq, struct task_struct *p, int flags)dequeue_task (rq, p, flags); } -static void block_task (struct rq *rq,struct task_struct *p,unsigned long task_state) +static bool dequeue_block_task (struct rq *rq,struct task_struct *p, +unsigned long task_state) {int flags =DEQUEUE_NOCLOCK ;
@@ -2274,9 +2275,15 @@ static void block_task(struct rq *rq, struct task_struct *p, unsigned long task_* * Where __schedule() and ttwu() have matching control dependencies. * -* After this, schedule() must not care about p->state any more. +* Once the caller invokes __block_task(), schedule() must not care about +* p->state any more. */ -if (dequeue_task (rq, p,DEQUEUE_SLEEP | flags)) +return dequeue_task (rq, p,DEQUEUE_SLEEP | flags); +} + +static void block_task (struct rq *rq,struct task_struct *p,unsigned long task_state) +{ +if (dequeue_block_task (rq, p, task_state))__block_task (rq, p); }
@@ -3775,6 +3782,9 @@ static inline void proxy_reset_donor(struct rq *rq)*/ static inline bool proxy_needs_return (struct rq *rq,struct task_struct *p) { +bool reset_donor = false; +bool dequeued; +/* * Typically per __set_task_cpu(), task_cpu(p) == p->wake_cpu. *
@@ -3798,11 +3808,17 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)if (task_current (rq, p))return false; -/* If we're return migrating the rq->donor, switch it out for idle */ -if (task_current_donor (rq, p)) - proxy_reset_donor(rq); + reset_donor = task_current_donor(rq, p); } -block_task (rq, p,TASK_WAKING ); + + dequeued =dequeue_block_task (rq, p,TASK_WAKING ); + +/* Keep on_rq set until all donor references have been replaced. */ +if (reset_donor) +proxy_reset_donor (rq); + +if (dequeued) +__block_task (rq, p);return true; }#else /* !CONFIG_SCHED_PROXY_EXEC */
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 3/17 sched: Make NOHZ CFS bandwidth checks follow proxy donor
In reply to Andrea RighiProxy execution separates the scheduling context in rq->donor from the physical execution context in rq->curr. sched_can_stop_tick() checks the latter for CFS bandwidth constraints and only does so when nr_running is one.
A retained proxy donor keeps both the donor and mutex owner queued. The check therefore misses a constrained FAIR donor and may stop the tick while its runtime still needs to be enforced.
Check the selected donor instead and remove the nr_running restriction. The donor being a queued FAIR task is sufficient to require bandwidth accounting regardless of other runnable tasks.
--- kernel/sched/core.c | 35 ++++++++++++++++++----------------- kernel/sched/fair.c | 12 +++++++----- 2 files changed, 25 insertions(+), 22 deletions(-)
kernel/sched/core.c +18 -17
@@ -1420,11 +1420,8 @@ static void nohz_csd_func(void *info)#endif /* CONFIG_NO_HZ_COMMON */ #ifdef CONFIG_NO_HZ_FULL -static inline bool __need_bw_check (struct rq *rq,struct task_struct *p) +static inline bool __need_bw_check (struct task_struct *p) { -if (rq->nr_running !=1 ) -return false; -if (p->sched_class != &fair_sched_class)return false;
@@ -1442,6 +1439,14 @@ bool sched_can_stop_tick(struct rq *rq)if (rq->dl.dl_nr_running)return false; +/* +* The selected scheduling context can be a constrained FAIR donor even +* when rq->curr is an RT task. Check it before the RT fast paths below, +* which may report that the tick can stop for a throttled RT context. +*/ +if (__need_bw_check (rq->donor) &&cfs_task_bw_constrained (rq->donor)) +return false; +/* * If there are more than one RR tasks, we need the tick to affect the * actual RR behaviour.
@@ -1472,18 +1477,6 @@ bool sched_can_stop_tick(struct rq *rq)if (rq->cfs.h_nr_queued >1 )return false; -/* -* If there is one task and it has CFS runtime bandwidth constraints -* and it's on the cpu now we don't want to stop the tick. -* This check prevents clearing the bit if a newly enqueued task here is -* dequeued by migrating while the constrained task continues to run. -* E.g. going from 2->1 without going through pick_next_task(). -*/ -if (__need_bw_check (rq, rq->curr)) { -if (cfs_task_bw_constrained (rq->curr)) -return false; - } - return true; }#endif /* CONFIG_NO_HZ_FULL */
@@ -7105,7 +7098,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)*/ static void __sched notrace __schedule(int sched_mode) { -struct task_struct *prev, *next; +struct task_struct *prev, *next, *tick_donor;/* * On PREEMPT_RT kernel, SM_RTLOCK_WAIT is noted * as a preemption by schedule_debug() and RCU.
@@ -7159,6 +7152,7 @@ static void __sched notrace __schedule(int sched_mode) rq->clock_update_flags <<=1 ;update_rq_clock (rq); rq->clock_update_flags =RQCF_UPDATED ; + tick_donor = rq->donor; switch_count = &prev->nivcsw;
@@ -7234,6 +7228,13 @@ static void __sched notrace __schedule(int sched_mode)clear_tsk_need_resched (prev);clear_preempt_need_resched (); keep_resched: +/* +* Enqueue and dequeue updates can evaluate the outgoing donor. Refresh +* the dependency after selecting a different scheduling context. +*/ +if (rq->donor != tick_donor) +sched_update_tick_dependency (rq); + rq->last_seen_need_resched_ns =0 ; is_switch = prev != next;
kernel/sched/fair.c +7 -5
@@ -7570,14 +7570,16 @@ static void sched_fair_update_stop_tick(struct rq *rq, struct task_struct *p)if (!tick_nohz_full_cpu (cpu))return ; -if (rq->nr_running !=1 ) +if (rq->cfs.h_nr_queued !=1 )return ;/* -* We know there is only one task runnable and we've just picked it. The -* normal enqueue path will have cleared TICK_DEP_BIT_SCHED if we will -* be otherwise able to stop the tick. Just need to check if we are using -* bandwidth control. +* We know there is only one FAIR task queued and we've just picked it. +* The normal enqueue path will have cleared TICK_DEP_BIT_SCHED if we +* will be otherwise able to stop the tick. With proxy execution, its +* lock owner may remain runnable in another scheduling class, so +* rq->nr_running can be greater than one. Just need to check if we are +* using bandwidth control. */ if (cfs_task_bw_constrained (p))tick_nohz_dep_set_cpu (cpu,TICK_DEP_BIT_SCHED );
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 4/17 sched/core: Avoid false migration warning for proxy donors
In reply to Andrea RighiProxy execution can move a blocked donor's scheduling context to the lock owner's CPU even when the donor is migration-disabled. The donor does not execute there, and its original execution CPU remains recorded in wake_cpu.
set_task_cpu() warns unconditionally for migration-disabled tasks, so a subsequent proxy migration or the wakeup path returning the donor home triggers a false positive. Moving a blocked scheduling context does not violate the migration-disabled execution context.
Exclude blocked proxy donors from the warning. The proxy wakeup path restores an executable placement before clearing the blocked state.
This is a preparatory fix for enabling proxy execution together with sched_ext.
--- kernel/sched/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/sched/core.c +8 -1
@@ -3352,6 +3352,8 @@ void relax_compatible_cpus_allowed_ptr(struct task_struct *p)void set_task_cpu (struct task_struct *p,unsigned int new_cpu) {unsigned int state =READ_ONCE (p->__state); +bool proxy_migrated =sched_proxy_exec () && p->is_blocked && +task_cpu (p) != p->wake_cpu;/* * We should never call set_task_cpu() on a blocked task,
@@ -3387,7 +3389,12 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)*/ WARN_ON_ONCE (!cpu_online (new_cpu)); -WARN_ON_ONCE (is_migration_disabled (p)); +/* +* Proxy execution can move a blocked task's scheduling context to any +* CPU without moving its migration-disabled execution context. The +* wakeup path will return the task to a CPU where it can execute. +*/ +WARN_ON_ONCE (is_migration_disabled (p) && !proxy_migrated);trace_sched_migrate_task (p, new_cpu);
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 5/17 sched: Pass next class to sched_change_begin()
In reply to Andrea Righisched_change_begin() currently only receives the task whose scheduling state is being changed. It cannot distinguish a class transition from a same-class update before recording the task queueing state.
Pass the incoming scheduling class to sched_change_begin() and update all callers. This allows transition handling to run before the normal dequeue and class change.
This is a preparatory change to support proxy execution with sched_ext.
--- kernel/sched/core.c | 12 +++++++----- kernel/sched/ext/ext.c | 7 ++++--- kernel/sched/ext/sub.c | 9 ++++++--- kernel/sched/sched.h | 9 ++++++--- kernel/sched/syscalls.c | 4 ++-- 5 files changed, 25 insertions(+), 16 deletions(-)
kernel/sched/core.c +7 -5
@@ -2803,7 +2803,7 @@ void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctxstatic void do_set_cpus_allowed (struct task_struct *p,struct affinity_context *ctx) { -scoped_guard (sched_change, p,DEQUEUE_SAVE ) +scoped_guard (sched_change, p, p->sched_class,DEQUEUE_SAVE ) p->sched_class->set_cpus_allowed(p, ctx); }
@@ -7746,7 +7746,7 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)if (prev_class != next_class) queue_flag |=DEQUEUE_CLASS ; - scoped_guard (sched_change, p, queue_flag) { + scoped_guard (sched_change, p, next_class, queue_flag) {/* * Boosting condition are: * 1. -rt task is running and holds mutex A
@@ -8423,7 +8423,7 @@ int migrate_task_to(struct task_struct *p, int target_cpu)void sched_setnuma (struct task_struct *p,int nid) {guard (task_rq_lock)(p); -scoped_guard (sched_change, p,DEQUEUE_SAVE ) +scoped_guard (sched_change, p, p->sched_class,DEQUEUE_SAVE ) p->numa_preferred_nid = nid; }#endif /* CONFIG_NUMA_BALANCING */
@@ -9541,7 +9541,7 @@ void sched_move_task(struct task_struct *tsk, bool for_autogroup)CLASS (task_rq_lock, rq_guard)(tsk); rq = rq_guard.rq; - scoped_guard (sched_change, tsk, queue_flags) { + scoped_guard (sched_change, tsk, tsk->sched_class, queue_flags) {sched_change_group (tsk);if (!for_autogroup)scx_cgroup_move_task (tsk);
@@ -11245,7 +11245,9 @@ static inline void sched_mm_cid_fork(struct task_struct *t) { } static DEFINE_PER_CPU(struct sched_change_ctx, sched_change_ctx); -struct sched_change_ctx *sched_change_begin (struct task_struct *p,unsigned int flags) +struct sched_change_ctx * +sched_change_begin (struct task_struct *p,const struct sched_class *next_class, +unsigned int flags) {struct sched_change_ctx *ctx =this_cpu_ptr (&sched_change_ctx);struct rq *rq =task_rq (p);
kernel/sched/ext/ext.c +4 -3
@@ -6158,7 +6158,8 @@ void scx_bypass(struct scx_sched *sch, bool bypass)scx_task_slice_ended (rq, p);/* cycling deq/enq is enough, see the function comment */ - scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { +scoped_guard (sched_change, p, p->sched_class, + DEQUEUE_SAVE | DEQUEUE_MOVE) {/* nothing */ ; } }
@@ -6440,7 +6441,7 @@ static void scx_root_disable(struct scx_sched *sch)if (old_class != new_class) queue_flags |=DEQUEUE_CLASS ; - scoped_guard (sched_change, p, queue_flags) { + scoped_guard (sched_change, p, new_class, queue_flags) { p->sched_class = new_class; }
@@ -7801,7 +7802,7 @@ static void scx_root_enable_workfn(struct kthread_work *work)if (old_class != new_class) queue_flags |=DEQUEUE_CLASS ; - scoped_guard (sched_change, p, queue_flags) { + scoped_guard (sched_change, p, new_class, queue_flags) {scx_set_task_slice (p,READ_ONCE (sch->slice_dfl)); p->sched_class = new_class; }
kernel/sched/ext/sub.c +6 -3
@@ -1257,7 +1257,8 @@ static void scx_rehome_task(struct scx_sched *to, struct task_struct *p)lockdep_assert_held (&p->pi_lock);lockdep_assert_rq_held (task_rq (p)); - scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { +scoped_guard (sched_change, p, p->sched_class, + DEQUEUE_SAVE | DEQUEUE_MOVE) {scx_disable_and_exit_task (scx_task_sched (p), p);scx_set_task_state (p,SCX_TASK_INIT_BEGIN );scx_set_task_state (p,SCX_TASK_INIT );
@@ -1287,7 +1288,8 @@ static void scx_punt_task(struct scx_sched *to, struct task_struct *p)lockdep_assert_rq_held (task_rq (p));WARN_ON_ONCE (!READ_ONCE (to->bypass_depth)); - scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { +scoped_guard (sched_change, p, p->sched_class, + DEQUEUE_SAVE | DEQUEUE_MOVE) {scx_disable_and_exit_task (scx_task_sched (p), p);scx_set_task_sched (p, to); }
@@ -1941,7 +1943,8 @@ void scx_sub_enable_workfn(struct kthread_work *work)if (!(p->scx.flags &SCX_TASK_SUB_INIT ))continue ; - scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { +scoped_guard (sched_change, p, p->sched_class, + DEQUEUE_SAVE | DEQUEUE_MOVE) {/* * $p must be either READY or ENABLED. If ENABLED, * __scx_disabled_and_exit_task() first disables and
kernel/sched/sched.h +6 -3
@@ -4223,13 +4223,16 @@ struct sched_change_ctx {bool running; }; -struct sched_change_ctx *sched_change_begin (struct task_struct *p,unsigned int flags); +struct sched_change_ctx * +sched_change_begin (struct task_struct *p,const struct sched_class *next_class, +unsigned int flags);void sched_change_end (struct sched_change_ctx *ctx); DEFINE_CLASS(sched_change, struct sched_change_ctx *,sched_change_end (_T), -sched_change_begin (p, flags), - struct task_struct *p, unsigned int flags) +sched_change_begin (p, next_class, flags), + struct task_struct *p, const struct sched_class *next_class, + unsigned int flags)DEFINE_CLASS_IS_UNCONDITIONAL (sched_change)
kernel/sched/syscalls.c +2 -2
@@ -85,7 +85,7 @@ void set_user_nice(struct task_struct *p, long nice)return ; } - scoped_guard (sched_change, p, DEQUEUE_SAVE) { + scoped_guard (sched_change, p, p->sched_class, DEQUEUE_SAVE) { p->static_prio =NICE_TO_PRIO (nice);set_load_weight (p, true); old_prio = p->prio;
@@ -678,7 +678,7 @@ int __sched_setscheduler(struct task_struct *p,if (prev_class != next_class) queue_flags |=DEQUEUE_CLASS ; - scoped_guard (sched_change, p, queue_flags) { + scoped_guard (sched_change, p, next_class, queue_flags) {if (!(attr->sched_flags &SCHED_FLAG_KEEP_PARAMS )) {__setscheduler_params (p, attr);
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 6/17 sched: Add helper to block retained proxy donors
In reply to Andrea RighiScheduler ownership changes may need to turn a mutex-blocked task retained on the runqueue for proxy execution back into a normally blocked task.
Add sched_proxy_block_task() to perform that transition while holding p->pi_lock and the task's runqueue lock. Reset an active donor to the physical current task before blocking it. If DELAY_DEQUEUE keeps a FAIR donor queued, the immediately following sched_change_begin() completes the dequeue through switching_from_fair() before recording its queued state.
This is a preparatory change to support proxy execution with sched_ext.
--- kernel/sched/core.c | 28 ++++++++++++++++++++++++++++ kernel/sched/sched.h | 6 ++++++ 2 files changed, 34 insertions(+)
kernel/sched/core.c +28 -0
@@ -6813,6 +6813,34 @@ static void proxy_deactivate(struct rq *rq, struct task_struct *donor)block_task (rq, donor, state); } +/* +* Remove a retained proxy donor before changing its scheduler ownership. +* The caller holds p->pi_lock, so p cannot wake and migrate if block_task() +* drops it from the runqueue. If DELAY_DEQUEUE keeps a blocked fair task +* queued, switching_from_fair() completes the dequeue in the immediately +* following sched_change_begin(). +*/ +void sched_proxy_block_task (struct rq *rq,struct task_struct *p) +{ +unsigned long state =READ_ONCE (p->__state); + +lockdep_assert_held (&p->pi_lock); +lockdep_assert_rq_held (rq); + +if (!p->is_blocked || !task_on_rq_queued (p)) +return ; +if (WARN_ON_ONCE (state ==TASK_RUNNING )) +return ; + +if (task_current_donor (rq, p)) +proxy_reset_donor (rq); + +if (!p->se.sched_delayed) +block_task (rq, p, state); + +WARN_ON_ONCE (task_on_rq_queued (p) && !p->se.sched_delayed); +} +static inline void proxy_release_rq_lock (struct rq *rq,struct rq_flags *rf)__releases (__rq_lockp (rq)) {
kernel/sched/sched.h +6 -0
@@ -2492,6 +2492,12 @@ static inline bool task_is_blocked(struct task_struct *p)return !!p->blocked_on; } +#ifdef CONFIG_SCHED_PROXY_EXEC +void sched_proxy_block_task (struct rq *rq,struct task_struct *p); +#else +static inline void sched_proxy_block_task (struct rq *rq,struct task_struct *p) {} +#endif +static inline int task_on_cpu (struct rq *rq,struct task_struct *p) {return p->on_cpu;
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 7/17 sched: Add sched_ext hooks for proxy execution
In reply to Andrea RighiProxy execution splits the scheduling context (the donor) from the execution context (the lock owner). sched_ext needs to observe that split at three points in __schedule():
- whether a blocked EXT task can be retained on the runqueue as a donor,
- when a donor's scheduling context starts driving a lock owner,
- once proxy resolution has settled for the current pick.
Introduce scx_allow_proxy_exec(), scx_proxy_donor_start() and scx_proxy_resolved(), and add their call sites in __schedule(). The implementations are empty here and are filled in by the sched_ext changes that follow, so that all the sched core changes needed by proxy execution stay together in the preparatory patches.
SCHED_PROXY_EXEC still depends on !SCHED_CLASS_EXT, so the new hooks are
inert: they are compiled out with CONFIG_SCHED_CLASS_EXT=n and unreachable otherwise.
This is a preparatory change to support proxy execution with sched_ext. No functional change.
--- kernel/sched/core.c | 12 +++++++----- kernel/sched/ext/ext.c | 13 +++++++++++++ kernel/sched/ext/ext.h | 6 ++++++ 3 files changed, 26 insertions(+), 5 deletions(-)
kernel/sched/core.c +7 -5
@@ -7208,13 +7208,12 @@ static void __sched notrace __schedule(int sched_mode) } }else if (!preempt && prev_state) {/* -* We pass task_is_blocked() as the should_block arg -* in order to keep mutex-blocked tasks on the runqueue -* for slection with proxy-exec (without proxy-exec -* task_is_blocked() will always be false). +* Keep mutex-blocked tasks on the runqueue for proxy execution +* only when their scheduling class allows it. Without proxy +* execution, task_is_blocked() always returns false. */ try_to_block_task (rq, prev, &prev_state, - !task_is_blocked (prev)); + !task_is_blocked (prev) || !scx_allow_proxy_exec (prev)); switch_count = &prev->nvcsw; }
@@ -7235,6 +7234,7 @@ static void __sched notrace __schedule(int sched_mode) }if (next == rq->idle) {zap_balance_callbacks (rq); +scx_proxy_resolved (rq); goto keep_resched; } }
@@ -7255,6 +7255,8 @@ static void __sched notrace __schedule(int sched_mode) donor->sched_class->put_prev_task (rq, donor, donor); donor->sched_class->set_next_task (rq, donor, true); } +scx_proxy_donor_start (rq); +scx_proxy_resolved (rq); }else {rq_set_donor (rq, next); }
kernel/sched/ext/ext.c +13 -0
@@ -24,6 +24,11 @@DEFINE_RAW_SPINLOCK (scx_sched_lock); +bool scx_allow_proxy_exec (const struct task_struct *p) +{ +return true; +} +/* * NOTE: sched_ext is in the process of growing multiple scheduler support and * scx_root usage is in a transitional state. Naked dereferences are safe if the
@@ -1078,6 +1083,10 @@ static void schedule_deferred_locked(struct rq *rq)schedule_deferred (rq); } +void scx_proxy_resolved (struct rq *rq) +{ +} +void schedule_dsq_reenq (struct scx_sched *sch,struct scx_dispatch_q *dsq,u64 reenq_flags,struct rq *locked_rq) {
@@ -3061,6 +3070,10 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) } } +void scx_proxy_donor_start (struct rq *rq) +{ +} +static enum scx_cpu_preempt_reason preempt_reason_from_class (const struct sched_class *class) {
kernel/sched/ext/ext.h +6 -0
@@ -20,6 +20,9 @@ void scx_rq_deactivate(struct rq *rq);int scx_check_setscheduler (struct task_struct *p,int policy);bool task_should_scx (int policy);bool scx_allow_ttwu_queue (const struct task_struct *p); +bool scx_allow_proxy_exec (const struct task_struct *p); +void scx_proxy_donor_start (struct rq *rq); +void scx_proxy_resolved (struct rq *rq);void init_sched_ext_class (void );static inline u32 scx_cpuperf_target (s32 cpu)
@@ -54,6 +57,9 @@ static inline void scx_rq_deactivate(struct rq *rq) {}static inline int scx_check_setscheduler (struct task_struct *p,int policy) {return 0 ; }static inline bool task_on_scx (const struct task_struct *p) {return false; }static inline bool scx_allow_ttwu_queue (const struct task_struct *p) {return true; } +static inline bool scx_allow_proxy_exec (const struct task_struct *p) {return true; } +static inline void scx_proxy_donor_start (struct rq *rq) {} +static inline void scx_proxy_resolved (struct rq *rq) {}static inline void init_sched_ext_class (void ) {}#endif /* CONFIG_SCHED_CLASS_EXT */
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 8/17 sched_ext: Block proxy donors across scheduler transitions
In reply to Andrea RighiProxy execution retains mutex-blocked donors on the runqueue so their scheduling context can execute a lock owner. sched_ext cannot safely retain such donors unless the BPF scheduler explicitly participates in their admission and ordering.
Make sched_ext reject retained donors by default. Implement scx_allow_proxy_exec() to force blocked EXT tasks through the regular block path in schedule().
Also fully deactivate any retained proxy donor in sched_change_begin() before changing its scheduling class. This prevents sched_setscheduler(), PI transitions and sched_ext activation from carrying an existing proxy session into the new class.
Some RT/DL PI transitions could usefully preserve the proxy session. For example, an RT waiter can boost a FAIR donor which is itself blocked on a non-PI mutex; retaining the donor would allow the boosted context to keep proxy-executing that mutex owner. Doing so safely requires defining which scheduling classes can carry retained proxy state across a transition. Keep the conservative reset for now and leave compatible RT/DL PI chains for future work.
This is a preparatory change to support proxy execution with sched_ext.
--- kernel/sched/core.c | 9 +++++++++ kernel/sched/ext/ext.c | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/sched/core.c +9 -0
@@ -11295,6 +11295,15 @@ sched_change_begin(struct task_struct *p, const struct sched_class *next_class, flags |=DEQUEUE_NOCLOCK ; } +/* +* Don't carry retained proxy state across scheduling class changes. +* Compatible RT/DL PI transitions could preserve the session so that a +* boosted donor continues proxy-executing its lock owner. Defining which +* class transitions can safely retain that state is left for future work. +*/ +if ((flags &DEQUEUE_CLASS ) && next_class != p->sched_class) +sched_proxy_block_task (rq, p); +if ((flags &DEQUEUE_CLASS ) && p->sched_class->switching_from) p->sched_class->switching_from (rq, p);
kernel/sched/ext/ext.c +1 -1
@@ -26,7 +26,7 @@ DEFINE_RAW_SPINLOCK(scx_sched_lock);bool scx_allow_proxy_exec (const struct task_struct *p) { -return true; +return p->sched_class != &ext_sched_class; }/*
Signature · 1 line
2.55.0
Tejun Heo
reviewerRe: [PATCH 08/17] sched_ext: Block proxy donors across scheduler transitions
Hello,
kernel/sched/core.c sched_change_begin(struct task_struct *p, const struct sched_class *next_class, · line 11306
Quoted 13 lines · Andrea Righi
kernel/sched/core.c +9
-0@@ -11295,6 +11295,15 @@ sched_change_begin(struct task_struct *p, const struct sched_class *next_class, flags |=DEQUEUE_NOCLOCK ; } +/* +* Don't carry retained proxy state across scheduling class changes. +* Compatible RT/DL PI transitions could preserve the session so that a +* boosted donor continues proxy-executing its lock owner. Defining which +* class transitions can safely retain that state is left for future work. +*/ +if ((flags &DEQUEUE_CLASS ) && next_class != p->sched_class) +sched_proxy_block_task (rq, p); +
I suppose this prevents RT PI propagation through existing proxy execution chain regardless of sched class in use? That sounds pretty convoluted and overlapping anyway but John does this look okay to you?
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerRe: [PATCH 08/17] sched_ext: Block proxy donors across scheduler transitions
Hi Tejun,
Quoted 7 lines · Tejun Heo
Hello,
Quoted 4 lines · Andrea Righi
kernel/sched/core.c +0
-0@@ -11295,6 +11295,15 @@ sched_change_begin(struct task_struct *p, const struct sched_class *next_class, flags |=DEQUEUE_NOCLOCK ; }
Quoted 13 lines
kernel/sched/core.c sched_change_begin(struct task_struct *p, const struct sched_class *next_class, · line 11306
Quoted 9 lines
kernel/sched/core.c +9
-0+/* +* Don't carry retained proxy state across scheduling class changes. +* Compatible RT/DL PI transitions could preserve the session so that a +* boosted donor continues proxy-executing its lock owner. Defining which +* class transitions can safely retain that state is left for future work. +*/ +if ((flags &DEQUEUE_CLASS ) && next_class != p->sched_class) +sched_proxy_block_task (rq, p); +I suppose this prevents RT PI propagation through existing proxy execution chain regardless of sched class in use? That sounds pretty convoluted and overlapping anyway but John does this look okay to you?
rt_mutex PI propagation itself still takes place, what this prevents is carrying the boosted scheduling context through an existing proxy chain when the boost changes scheduling class. Same-class RT priority changes are preserved.
The reset was intended as a conservative way to avoid carrying retained state into or out of sched_ext, but this also affects fair/RT/DL proxy transitions.
Maybe we can narrow the condition to transitions into or out of sched_ext, like:
#ifdef CONFIG_SCHED_CLASS_EXT
/*
* Don't carry retained proxy state into or out of sched_ext. The BPF
* scheduler must explicitly admit blocked donors and cannot inherit a
* proxy session owned by another scheduling class.
*/
if ((flags & DEQUEUE_CLASS) &&
(p->sched_class == &ext_sched_class || next_class == &ext_sched_class)) sched_proxy_block_task(rq, p);
#endif
This would preserve proxy execution across fair/RT/DL PI transitions while still ending it before entering or leaving sched_ext. WDYT?
Thanks,
-Andrea
Andrea Righi
reviewerPATCH 9/17 sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
In reply to Andrea RighiWith proxy execution, pick_next_task() can select a blocked task as the scheduling context before find_proxy_task() resolves the execution context.
From the BPF scheduler perspective, that donor is running while its scheduling context drives the lock owner; ops.tick() and other accounting must therefore remain enclosed by a matching ops.running()/ops.stopping() session.
In this scenario, the session boundaries do not always match physical task switches. Keep the "running" session open when the same donor continues on the same CPU. When proxy execution migrates a donor's scheduling context to another CPU, end its running session on the source CPU and start a new session on the destination CPU only after proxy resolution succeeds. This prevents a failed resolution from exposing a provisional ops.running() event.
Track these sessions with a new SCX_TASK_RUN_TRACKED flag. The explicit running-state tracking is also required by later donor-based accounting:
it prevents an EXT owner executing for a non-EXT donor from being treated as the active EXT scheduling context when it is dequeued.
This is a preparatory change for enabling proxy execution together with sched_ext.
--- include/linux/sched/ext.h | 1 + kernel/sched/ext/ext.c | 65 ++++++++++++++----- kernel/sched/ext/internal.h | 6 ++ .../sched_ext/include/scx/enum_defs.autogen.h | 1 + 4 files changed, 57 insertions(+), 16 deletions(-)
include/linux/sched/ext.h +1 -0
@@ -104,6 +104,7 @@ enum scx_ent_flags {SCX_TASK_SUB_INIT =1 <<4 ,/* task being initialized for a sub sched */ SCX_TASK_IMMED =1 <<5 ,/* task is on local DSQ with %SCX_ENQ_IMMED */ SCX_TASK_PROTECTED =1 <<6 ,/* slice and DSQ head position protected */ +SCX_TASK_RUN_TRACKED =1 <<7 ,/* task is in an ops.running()/stopping() session */ /* * Bits 8 to 10 are used to carry task state:
kernel/sched/ext/ext.c +49 -16
104 lines not on this page · raw message
kernel/sched/ext/internal.h +6 -0
@@ -449,6 +449,12 @@ struct sched_ext_ops {* Therefore, always use scx_bpf_task_cpu(@p) to determine the * target CPU the task is going to use. * +* Under proxy execution, the BPF scheduler continues to observe the +* donor as the current scheduling context. A blocked donor enters a +* ->running()/->stopping() session while its scheduling context drives +* the lock owner. The lock owner executing on its behalf is intentionally +* not reported through these callbacks. +* * See ->runnable() for explanation on the task state notifiers. */ void (*running)(struct task_struct *p);
tools/sched_ext/include/scx/enum_defs.autogen.h +1 -0
@@ -101,6 +101,7 @@#define HAVE_SCX_TASK_SUB_INIT #define HAVE_SCX_TASK_IMMED #define HAVE_SCX_TASK_PROTECTED +#define HAVE_SCX_TASK_RUN_TRACKED #define HAVE_SCX_TASK_STATE_SHIFT #define HAVE_SCX_TASK_STATE_BITS #define HAVE_SCX_TASK_STATE_MASK
Signature · 1 line
2.55.0
Tejun Heo
reviewerRe: [PATCH 09/17] sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
Hello,
Quoted 10 lines · Andrea Righi
kernel/sched/ext/ext.c +2
-1+static void set_next_task_scx (struct rq *rq,struct task_struct *p,bool first) +{if (p->scx.flags &SCX_TASK_QUEUED ) {/* * Core-sched might decide to execute @p before it is @@ -3024,9 +3037,14 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) p->se.exec_start =rq_clock_task (rq); -/* see dequeue_task_scx() on why we skip when !QUEUED */
kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 3047
Quoted 10 lines
kernel/sched/ext/ext.c +8
-2-if (SCX_HAS_OP (sch, running) && (p->scx.flags &SCX_TASK_QUEUED )) -SCX_CALL_OP_TASK (sch, running, rq, p); +/* +* See dequeue_task_scx() for why we skip when !QUEUED. On a normal +* scheduling transition, defer starting a blocked donor's session until +* proxy resolution succeeds. A restore follows an already resolved +* scheduling context and can start the session immediately. +*/ +if ((p->scx.flags &SCX_TASK_QUEUED ) && (!p->is_blocked || !first)) +scx_start_task_running (rq, p);
nit: can we do !(p->is_blocked && first) instead? I think that matches what's being expressed better.
Also, can you expand the comment? I find it a bit difficult to follow. So, when we're actually switching to a donor, we skip running transition because proxy resolution might give us nothing or it may have to run on a differnet CPU and so on, so we do running transition after proxy resolution is done. For SAVE/RESTORE, this doesn't apply as they're always matched regardless of donor state. Is that the right reading?
kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 3098
Quoted 12 lines
kernel/sched/ext/ext.c +6
-0clr_task_runnable (p, true);@@ -3072,6 +3090,13 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)void scx_proxy_donor_start (struct rq *rq) { +struct task_struct *donor = rq->donor; + +lockdep_assert_rq_held (rq); + +if (donor->sched_class == &ext_sched_class && + (donor->scx.flags &SCX_TASK_QUEUED ))
another nit: no need for line break.
kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 3100
Quoted 2 lines
kernel/sched/ext/ext.c +1
-0+scx_start_task_running (rq, donor); }
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerRe: [PATCH 09/17] sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
Quoted 27 lines · Tejun Heo
Hello,
Quoted 10 lines · Andrea Righi
kernel/sched/ext/ext.c +2
-1+static void set_next_task_scx (struct rq *rq,struct task_struct *p,bool first) +{if (p->scx.flags &SCX_TASK_QUEUED ) {/* * Core-sched might decide to execute @p before it is @@ -3024,9 +3037,14 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) p->se.exec_start =rq_clock_task (rq); -/* see dequeue_task_scx() on why we skip when !QUEUED */ kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 3047
Quoted 10 lines
kernel/sched/ext/ext.c +8
-2-if (SCX_HAS_OP (sch, running) && (p->scx.flags &SCX_TASK_QUEUED )) -SCX_CALL_OP_TASK (sch, running, rq, p); +/* +* See dequeue_task_scx() for why we skip when !QUEUED. On a normal +* scheduling transition, defer starting a blocked donor's session until +* proxy resolution succeeds. A restore follows an already resolved +* scheduling context and can start the session immediately. +*/ +if ((p->scx.flags &SCX_TASK_QUEUED ) && (!p->is_blocked || !first)) +scx_start_task_running (rq, p);nit: can we do !(p->is_blocked && first) instead? I think that matches what's being expressed better.
Quoted 6 lines
Also, can you expand the comment? I find it a bit difficult to follow. So, when we're actually switching to a donor, we skip running transition because proxy resolution might give us nothing or it may have to run on a differnet CPU and so on, so we do running transition after proxy resolution is done. For SAVE/RESTORE, this doesn't apply as they're always matched regardless of donor state. Is that the right reading?
Yes, that's the intended behavior. When @first is set, a blocked task is only a provisional donor: proxy-exec may fail or migrate the donor to another CPU, so ops.running() is deferred until resolution succeeds. When !@first, this is the restore side of a SAVE/RESTORE cycle, the matching dequeue already issued ops.stopping(), so ops.running() must be restarted regardless of the donor state. I'll change the condition and expand the comment.
Quoted 5 lines
Quoted 5 lines
kernel/sched/ext/ext.c +0
-0clr_task_runnable (p, true);@@ -3072,6 +3090,13 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
Quoted 10 lines
kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 3098
Quoted 8 lines
kernel/sched/ext/ext.c +6
-0void scx_proxy_donor_start (struct rq *rq) { +struct task_struct *donor = rq->donor; + +lockdep_assert_rq_held (rq); + +if (donor->sched_class == &ext_sched_class && + (donor->scx.flags &SCX_TASK_QUEUED ))another nit: no need for line break.
Ack.
Thanks,
-Andrea
Quoted 8 lines
kernel/sched/ext/ext.c static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) · line 3100
Quoted 3 lines
kernel/sched/ext/ext.c +1
-0+scx_start_task_running (rq, donor); }Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerPATCH v2 1/1 sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
In reply to Tejun HeoWith proxy execution, pick_next_task() can select a blocked task as the scheduling context before find_proxy_task() resolves the execution context.
From the BPF scheduler perspective, that donor is running while its scheduling context drives the lock owner; ops.tick() and other accounting must therefore remain enclosed by a matching ops.running()/ops.stopping() session.
In this scenario, the session boundaries do not always match physical task switches. Keep the "running" session open when the same donor continues on the same CPU. When proxy execution migrates a donor's scheduling context to another CPU, end its running session on the source CPU and start a new session on the destination CPU only after proxy resolution succeeds. This prevents a failed resolution from exposing a provisional ops.running() event.
Track these sessions with a new SCX_TASK_RUN_TRACKED flag. The explicit running-state tracking is also required by later donor-based accounting:
it prevents an EXT owner executing for a non-EXT donor from being treated as the active EXT scheduling context when it is dequeued.
This is a preparatory change for enabling proxy execution together with sched_ext.
---
Changes in v2:
- Simplify the donor checks and clarify normal selection versus SAVE/RESTORE handling (Tejun Heo)
include/linux/sched/ext.h | 1 + kernel/sched/ext/ext.c | 70 ++++++++++++++----- kernel/sched/ext/internal.h | 6 ++ .../sched_ext/include/scx/enum_defs.autogen.h | 1 + 4 files changed, 62 insertions(+), 16 deletions(-)
include/linux/sched/ext.h +1 -0
@@ -104,6 +104,7 @@ enum scx_ent_flags {SCX_TASK_SUB_INIT =1 <<4 ,/* task being initialized for a sub sched */ SCX_TASK_IMMED =1 <<5 ,/* task is on local DSQ with %SCX_ENQ_IMMED */ SCX_TASK_PROTECTED =1 <<6 ,/* slice and DSQ head position protected */ +SCX_TASK_RUN_TRACKED =1 <<7 ,/* task is in an ops.running()/stopping() session */ /* * Bits 8 to 10 are used to carry task state:
kernel/sched/ext/ext.c +54 -16
109 lines not on this page · raw message
kernel/sched/ext/internal.h +6 -0
@@ -449,6 +449,12 @@ struct sched_ext_ops {* Therefore, always use scx_bpf_task_cpu(@p) to determine the * target CPU the task is going to use. * +* Under proxy execution, the BPF scheduler continues to observe the +* donor as the current scheduling context. A blocked donor enters a +* ->running()/->stopping() session while its scheduling context drives +* the lock owner. The lock owner executing on its behalf is intentionally +* not reported through these callbacks. +* * See ->runnable() for explanation on the task state notifiers. */ void (*running)(struct task_struct *p);
tools/sched_ext/include/scx/enum_defs.autogen.h +1 -0
@@ -101,6 +101,7 @@#define HAVE_SCX_TASK_SUB_INIT #define HAVE_SCX_TASK_IMMED #define HAVE_SCX_TASK_PROTECTED +#define HAVE_SCX_TASK_RUN_TRACKED #define HAVE_SCX_TASK_STATE_SHIFT #define HAVE_SCX_TASK_STATE_BITS #define HAVE_SCX_TASK_STATE_MASK
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 10/17 sched_ext: Move reject DSQ draining into core
In reply to Andrea RighiThe reject DSQ drain will be generalized for placement failures outside sub-scheduler capability rejection. Move the drain into the core sched_ext implementation first so the subsequent behavioral changes can be reviewed separately. No functional change.
--- kernel/sched/ext/ext.c | 49 ++++++++++++++++++++++++++++++++++++++++++ kernel/sched/ext/sub.c | 45 -------------------------------------- kernel/sched/ext/sub.h | 2 -- 3 files changed, 49 insertions(+), 47 deletions(-)
kernel/sched/ext/ext.c +49 -0
@@ -4668,6 +4668,55 @@ static void process_deferred_reenq_users(struct rq *rq) } } +#ifdef CONFIG_EXT_SUB_SCHED +/* +* Drain @rq->scx.reject_dsq, reenqueueing each task so the BPF re-decides +* from p->scx.reenq_reason_*. +* +* A task can be re-rejected repeatedly. The reenqueue is bounded per task in +* scx_do_enqueue_task(), which ejects the owning sub past SCX_REENQ_MAX_REPEAT. +* Rejection can't happen for root. +*/ +static void scx_reenq_reject (struct rq *rq) +{ +LIST_HEAD (tasks); +struct task_struct *p, *n; + +lockdep_assert_rq_held (rq); + +if (!scx_has_subs () ||list_empty (&rq->scx.reject_dsq.list)) +return ; + +/* +* Move to a private list so a task re-rejected by the +* scx_do_enqueue_task() below isn't revisited this round. +*/ +list_for_each_entry_safe (p, n, &rq->scx.reject_dsq.list, scx.dsq_list.node) { +/* migration_pending tasks should have bypassed to local DSQ */ +if (WARN_ON_ONCE (p->migration_pending)) +continue ; + +scx_dispatch_dequeue (rq, p); + +if (WARN_ON_ONCE (p->scx.flags &SCX_TASK_REENQ_REASON_MASK )) + p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; + p->scx.flags |=SCX_TASK_REENQ_CAP ; + +list_add_tail (&p->scx.dsq_list.node, &tasks); + } + +list_for_each_entry_safe (p, n, &tasks, scx.dsq_list.node) { +list_del_init (&p->scx.dsq_list.node); + +scx_do_enqueue_task (rq, p,SCX_ENQ_REENQ ,-1 ); + + p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; + } +} +#else +static void scx_reenq_reject (struct rq *rq) {} +#endif +static void run_deferred (struct rq *rq) {process_ddsp_deferred_locals (rq);
kernel/sched/ext/sub.c +0 -45
51 lines not on this page · raw message
kernel/sched/ext/sub.h +0 -2
@@ -37,7 +37,6 @@ void scx_discard_stale_ecaps_syncs(void);struct scx_dispatch_q *scx_resolve_local_dsq (struct scx_sched *sch,struct rq *rq,struct task_struct *p,u64 *enq_flags);bool scx_task_reenq_on_cap_revoke (struct rq *rq,struct task_struct *p); -void scx_reenq_reject (struct rq *rq);void scx_rescue_charge (struct rq *rq,s64 delta_exec);void scx_rescue_end (struct rq *rq);bool scx_rescue_keep (struct rq *rq,struct task_struct *p);
@@ -93,7 +92,6 @@ static inline void scx_discard_ecaps_to_sync(s32 cpu, struct scx_sched_pcpu *pcpstatic inline void scx_discard_stale_ecaps_syncs (void ) {}static inline struct scx_dispatch_q *scx_resolve_local_dsq (struct scx_sched *sch,struct rq *rq,struct task_struct *p,u64 *enq_flags) {return &rq->scx.local_dsq; }static inline bool scx_task_reenq_on_cap_revoke (struct rq *rq,struct task_struct *p) {return false; } -static inline void scx_reenq_reject (struct rq *rq) {}static inline void scx_rescue_charge (struct rq *rq,s64 delta_exec) {}static inline void scx_rescue_end (struct rq *rq) {}static inline bool scx_rescue_keep (struct rq *rq,struct task_struct *p) {return false; }
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 11/17 sched_ext: Generalize the reject DSQ reenqueue path
In reply to Andrea RighiThe reject DSQ is currently specific to sub-scheduler cap failures. Its storage and initialization depend on CONFIG_EXT_SUB_SCHED, and the drain path assumes every rejected task was rejected for SCX_TASK_REENQ_CAP.
Other transient placement failures need the same ability to park a task on its source rq and return it to the owning BPF scheduler. Make the reject DSQ unconditional and carry the reenqueue reason directly in p->scx.flags from the rejection site.
A reenqueue reason remains valid while ops.enqueue() runs. If the resulting placement is rejected again, the rejection path replaces the carried reason and the new reason remains attached to the task on the reject DSQ. Clear the reason only when the task was not rejected again.
This is a preparatory change to support proxy execution with sched_ext.
--- kernel/sched/ext/ext.c | 57 ++++++++++++++++++++++++------------------ kernel/sched/ext/sub.c | 4 +++ kernel/sched/sched.h | 2 +- 3 files changed, 38 insertions(+), 25 deletions(-)
kernel/sched/ext/ext.c +33 -24
@@ -1598,11 +1598,10 @@ static void scx_dispatch_enqueue(struct scx_sched *sch, struct rq *rq, struct scx_dispatch_q *dsq, struct task_struct *p, u64 slice, u64 vtime, u64 enq_flags) { - bool is_rq_owned = false; + bool is_rq_owned = dsq_is_rq_owned(dsq);if (dsq->id ==SCX_DSQ_LOCAL ) { dsq =scx_resolve_local_dsq (sch, rq, p, &enq_flags); - is_rq_owned = true; }WARN_ON_ONCE (p->scx.dsq || !list_empty (&p->scx.dsq_list.node));
@@ -1807,6 +1806,9 @@ void scx_dispatch_dequeue(struct rq *rq, struct task_struct *p) } p->scx.dsq =NULL ; +if (dsq->id ==SCX_DSQ_REJECT ) + p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; +if (!is_rq_owned)raw_spin_unlock (&dsq->lock); }
@@ -2105,6 +2107,17 @@ void scx_do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,scx_dispatch_enqueue (sch, rq, dsq, p,0 ,0 , enq_flags); } +/* +* Clear the reason consumed by ops.enqueue() unless the resulting placement +* produced a fresh rejection. A rejected task must retain its new reason +* while it remains parked on @rq's reject DSQ. +*/ +static void scx_finish_reenqueue (struct rq *rq,struct task_struct *p) +{ +if (p->scx.dsq != &rq->scx.reject_dsq) + p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; +} +static bool task_runnable (const struct task_struct *p) {return !list_empty (&p->scx.runnable_node);
@@ -3202,7 +3215,7 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,if (p->scx.flags &SCX_TASK_IMMED ) { p->scx.flags |=SCX_TASK_REENQ_PREEMPTED ;scx_do_enqueue_task (rq, p,SCX_ENQ_REENQ ,-1 ); - p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; +scx_finish_reenqueue (rq, p); }else {u64 enq_flags =0 ;
@@ -4487,7 +4500,7 @@ static u32 reenq_local(struct scx_sched *sch, struct rq *rq, u64 reenq_flags)scx_do_enqueue_task (rq, p,SCX_ENQ_REENQ ,-1 ); - p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; +scx_finish_reenqueue (rq, p); nr_enqueued++; }
@@ -4605,7 +4618,7 @@ static void reenq_user(struct rq *rq, struct scx_dispatch_q *dsq, u64 reenq_flagscx_do_enqueue_task (task_rq, p,SCX_ENQ_REENQ ,-1 ); - p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; +scx_finish_reenqueue (task_rq, p);if (!(++nr_enqueued %SCX_TASK_ITER_BATCH )) {scx_rq_lock_drop (locked_rq);
@@ -4668,14 +4681,14 @@ static void process_deferred_reenq_users(struct rq *rq) } } -#ifdef CONFIG_EXT_SUB_SCHED /* -* Drain @rq->scx.reject_dsq, reenqueueing each task so the BPF re-decides -* from p->scx.reenq_reason_*. +* Drain @rq->scx.reject_dsq and reenqueue each task so that its owning BPF +* scheduler chooses placement again. * -* A task can be re-rejected repeatedly. The reenqueue is bounded per task in -* scx_do_enqueue_task(), which ejects the owning sub past SCX_REENQ_MAX_REPEAT. -* Rejection can't happen for root. +* A task can be re-rejected repeatedly. Reenqueues are bounded per task by +* SCX_REENQ_MAX_REPEAT in scx_do_enqueue_task(), which ejects the owning +* scheduler. The private list below prevents a task from being revisited in +* the same round. */ static void scx_reenq_reject (struct rq *rq) {
@@ -4684,23 +4697,22 @@ static void scx_reenq_reject(struct rq *rq)lockdep_assert_rq_held (rq); -if (!scx_has_subs () ||list_empty (&rq->scx.reject_dsq.list)) +if (list_empty (&rq->scx.reject_dsq.list))return ;/* -* Move to a private list so a task re-rejected by the +* Move tasks to a private list so a task re-rejected by * scx_do_enqueue_task() below isn't revisited this round. */ list_for_each_entry_safe (p, n, &rq->scx.reject_dsq.list, scx.dsq_list.node) { +u32 reason = p->scx.flags &SCX_TASK_REENQ_REASON_MASK ; +/* migration_pending tasks should have bypassed to local DSQ */ -if (WARN_ON_ONCE (p->migration_pending)) -continue ; +WARN_ON_ONCE (p->migration_pending); +WARN_ON_ONCE (!reason);scx_dispatch_dequeue (rq, p); - -if (WARN_ON_ONCE (p->scx.flags &SCX_TASK_REENQ_REASON_MASK )) - p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; - p->scx.flags |=SCX_TASK_REENQ_CAP ; + p->scx.flags |= reason;list_add_tail (&p->scx.dsq_list.node, &tasks); }
@@ -4710,12 +4722,9 @@ static void scx_reenq_reject(struct rq *rq)scx_do_enqueue_task (rq, p,SCX_ENQ_REENQ ,-1 ); - p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK; +scx_finish_reenqueue (rq, p); } } -#else -static void scx_reenq_reject (struct rq *rq) {} -#endif static void run_deferred (struct rq *rq) {
@@ -8775,8 +8784,8 @@ void __init init_sched_ext_class(void)/* local_dsq's sch will be set during scx_root_enable() */ BUG_ON (scx_init_dsq (&rq->scx.local_dsq,SCX_DSQ_LOCAL ,NULL )); -#ifdef CONFIG_EXT_SUB_SCHED BUG_ON (scx_init_dsq (&rq->scx.reject_dsq,SCX_DSQ_REJECT ,NULL )); +#ifdef CONFIG_EXT_SUB_SCHED scx_rescue_init (rq);#endif
kernel/sched/ext/sub.c +4 -0
@@ -752,6 +752,10 @@ struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *r p->scx.reenq_reason_caps = missing; p->scx.reenq_reason_cid = cid; +WARN_ON_ONCE ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) && + !(*enq_flags &SCX_ENQ_REENQ )); + p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; + p->scx.flags |=SCX_TASK_REENQ_CAP ;return &rq->scx.reject_dsq; }
kernel/sched/sched.h +1 -1
@@ -807,8 +807,8 @@ struct scx_rq_rescue {struct scx_rq {struct scx_dispatch_q local_dsq; +struct scx_dispatch_q reject_dsq;/* staging for rejected tasks */ #ifdef CONFIG_EXT_SUB_SCHED -struct scx_dispatch_q reject_dsq;/* staging for cap-rejected tasks */ struct scx_rq_rescue rescue;#endif struct list_head runnable_list;/* runnable tasks on this rq */
Signature · 1 line
2.55.0
Tejun Heo
reviewerRe: [PATCH 11/17] sched_ext: Generalize the reject DSQ reenqueue path
Hello, Andrea.
kernel/sched/ext/ext.c void scx_dispatch_dequeue(struct rq *rq, struct task_struct *p) · line 1810
Quoted 6 lines · Andrea Righi
kernel/sched/ext/ext.c +2
-0@@ -1807,6 +1806,9 @@ void scx_dispatch_dequeue(struct rq *rq, struct task_struct *p) } p->scx.dsq =NULL ; +if (dsq->id ==SCX_DSQ_REJECT ) + p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ;
Two things bother me here. scx_dispatch_dequeue() is a DSQ unlink primitive and managing the reason lifetime doesn't seem to belong in it. And it leads to the following in scx_reenq_reject():
Quoted 2 lines
kernel/sched/ext/ext.c +1
-0list_for_each_entry_safe (p, n, &rq->scx.reject_dsq.list, scx.dsq_list.node) { +u32 reason = p->scx.flags &SCX_TASK_REENQ_REASON_MASK ;
...
kernel/sched/ext/ext.c static void scx_reenq_reject(struct rq *rq) · line 4715
Quoted 6 lines
kernel/sched/ext/ext.c +1
-4scx_dispatch_dequeue (rq, p); - -if (WARN_ON_ONCE (p->scx.flags &SCX_TASK_REENQ_REASON_MASK )) - p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; - p->scx.flags |=SCX_TASK_REENQ_CAP ; + p->scx.flags |= reason;
where the drain clears the reason just to immediately put it back.
Can't we keep scx_dispatch_dequeue() oblivious of the reason and instead clear it explicitly where its lifetime actually ends? ie. Clear directly in dequeue_task_scx() after the DSQ removal and in the SCX_DEQ_CORE_SCHED_EXEC path of set_next_task_scx(), and keep the conditional helper for the reenqueue sites, maybe renamed to scx_maybe_clear_reenq_reason() as clearing is conditional only there. Then the drain doesn't need to save and restore anything.
Thanks.
--
tejun
Andrea Righi
reviewerRe: [PATCH 11/17] sched_ext: Generalize the reject DSQ reenqueue path
Hi Tejun,
Quoted 26 lines · Tejun Heo
Hello, Andrea.
kernel/sched/ext/ext.c void scx_dispatch_dequeue(struct rq *rq, struct task_struct *p) · line 1810
Quoted 6 lines · Andrea Righi
kernel/sched/ext/ext.c +2
-0@@ -1807,6 +1806,9 @@ void scx_dispatch_dequeue(struct rq *rq, struct task_struct *p) } p->scx.dsq =NULL ; +if (dsq->id ==SCX_DSQ_REJECT ) + p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ;Two things bother me here.
scx_dispatch_dequeue()is a DSQ unlink primitive and managing the reason lifetime doesn't seem to belong in it. And it leads to the following inscx_reenq_reject():Quoted 2 lines
kernel/sched/ext/ext.c +1
-0list_for_each_entry_safe (p, n, &rq->scx.reject_dsq.list, scx.dsq_list.node) { +u32 reason = p->scx.flags &SCX_TASK_REENQ_REASON_MASK ;...
kernel/sched/ext/ext.c static void scx_reenq_reject(struct rq *rq) · line 4715
Quoted 6 lines
kernel/sched/ext/ext.c +1
-4scx_dispatch_dequeue (rq, p); - -if (WARN_ON_ONCE (p->scx.flags &SCX_TASK_REENQ_REASON_MASK )) - p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; - p->scx.flags |=SCX_TASK_REENQ_CAP ; + p->scx.flags |= reason;where the drain clears the reason just to immediately put it back.
Quoted 7 lines
Can't we keep
scx_dispatch_dequeue()oblivious of the reason and instead clear it explicitly where its lifetime actually ends? ie. Clear directly indequeue_task_scx()after the DSQ removal and in theSCX_DEQ_CORE_SCHED_EXECpath ofset_next_task_scx(), and keep the conditional helper for the reenqueue sites, maybe renamed toscx_maybe_clear_reenq_reason()as clearing is conditional only there. Then the drain doesn't need to save and restore anything.
Yes, that makes sense. The reason lifetime belongs to the reenqueue transaction, ratehr than the generic DSQ unlink primitive. I'll keep scx_dispatch_dequeue() oblivious to it, clear it explicitly from dequeue_task_scx() and the core-sched execution path, and rename the conditional reenqueue helper.
Thanks for lookinga at this series!
-Andrea
Andrea Righi
reviewerPATCH 12/17 sched_ext: Handle proxy-exec races in remote DSQ transfers
In reply to Andrea RighiWithout proxy execution, the DSQ lock and holding_cpu handshake ensure that a task cannot be dequeued or start running during an rq-lock handoff without clearing holding_cpu.
Proxy execution is an exception: a task can start physically executing as a lock owner while its scheduling context remains on a DSQ; its on-CPU or migration-disabled state can therefore change without clearing holding_cpu.
Recheck these states after acquiring the source rq lock. If the transfer can no longer proceed, park the task on the source rq's reject DSQ and re-enqueue it through its owning scheduler. This preserves the BPF scheduler's placement policy and keeps descendant tasks within their sub-scheduler's cap grants.
Implement the scx_proxy_resolved() hook to drain the parked tasks once proxy resolution has settled and the outgoing owner has switched out.
Without this change and proxy execution enabled, stress-ng --pipeherd can trigger this race and migrate an active execution context, leading to sleeping-while-atomic warnings and subsequent lockdep corruption.
This is a preparatory change to support proxy execution with sched_ext.
--- include/linux/sched/ext.h | 2 + kernel/sched/ext/ext.c | 168 +++++++++++++++--- kernel/sched/ext/internal.h | 8 + kernel/sched/ext/sub.c | 4 +- kernel/sched/sched.h | 1 + .../sched_ext/include/scx/enum_defs.autogen.h | 1 + 6 files changed, 161 insertions(+), 23 deletions(-)
include/linux/sched/ext.h +2 -0
@@ -137,6 +137,7 @@ enum scx_ent_flags {* IMMED reenqueued due to failed ENQ_IMMED * PREEMPTED preempted while running * CAP sub-sched cap miss, see p->scx.reenq_reason_* +* PROXY proxy state prevented a remote DSQ transfer */ SCX_TASK_REENQ_REASON_SHIFT =12 , SCX_TASK_REENQ_REASON_BITS = 3,
@@ -147,6 +148,7 @@ enum scx_ent_flags {SCX_TASK_REENQ_IMMED =2 <<SCX_TASK_REENQ_REASON_SHIFT ,SCX_TASK_REENQ_PREEMPTED =3 <<SCX_TASK_REENQ_REASON_SHIFT ,SCX_TASK_REENQ_CAP =4 <<SCX_TASK_REENQ_REASON_SHIFT , +SCX_TASK_REENQ_PROXY =5 <<SCX_TASK_REENQ_REASON_SHIFT ,/* iteration cursor, not a task */ SCX_TASK_CURSOR = 1 << 31,
kernel/sched/ext/ext.c +148 -20
268 lines not on this page · raw message
kernel/sched/ext/internal.h +8 -0
@@ -1758,6 +1758,14 @@ enum scx_enq_flags { SCX_ENQ_SLICE_DFL = 1LLU << 62, /* carried slice is a default refill */ }; +/* Strip priority and carried slice state when diverting from a local DSQ. */ +static inline void scx_prepare_dsq_divert (struct task_struct *p,u64 *enq_flags) +{ + *enq_flags &= ~(SCX_ENQ_IMMED |SCX_ENQ_PREEMPT |SCX_ENQ_HEAD | +SCX_ENQ_APPLY_SLICE |SCX_ENQ_SLICE_DFL ); + p->scx.flags &= ~SCX_TASK_IMMED ; +} +enum scx_deq_flags {/* expose select DEQUEUE_* flags as enums */ SCX_DEQ_SLEEP =DEQUEUE_SLEEP ,
kernel/sched/ext/sub.c +1 -3
@@ -735,9 +735,7 @@ struct scx_dispatch_q *scx_resolve_local_dsq(struct scx_sched *sch, struct rq *r* or HEAD - a diversion has no priority and IMMED is not allowed on * non-local DSQs. Strip the enq and task flags along with the slice. */ - *enq_flags &= ~(SCX_ENQ_IMMED |SCX_ENQ_PREEMPT |SCX_ENQ_HEAD | -SCX_ENQ_APPLY_SLICE |SCX_ENQ_SLICE_DFL ); - p->scx.flags &= ~SCX_TASK_IMMED ; +scx_prepare_dsq_divert (p, enq_flags);/* the enqueuer opted for rescue instead of rejection and reenqueue */ if ((*enq_flags &SCX_ENQ_RESCUE ) &&likely (scx_rescue_bw_1024)) {
kernel/sched/sched.h +1 -0
@@ -788,6 +788,7 @@ enum scx_rq_flags {SCX_RQ_BAL_CB_PENDING =1 <<6 ,/* must queue a cb after dispatching */ SCX_RQ_SUB_IDLE_RENOTIFY =1 <<7 ,/* sub-scheds are owed update_idle() */ SCX_RQ_ROOT_IDLE_RENOTIFY =1 <<8 ,/* the root is owed update_idle() */ +SCX_RQ_PROXY_REENQ =1 <<9 ,/* proxy-rejected tasks need reenqueue */ SCX_RQ_IN_WAKEUP =1 <<16 , SCX_RQ_IN_DISPATCH = 1 << 17,
tools/sched_ext/include/scx/enum_defs.autogen.h +1 -0
@@ -119,6 +119,7 @@#define HAVE_SCX_TASK_REENQ_IMMED #define HAVE_SCX_TASK_REENQ_PREEMPTED #define HAVE_SCX_TASK_REENQ_CAP +#define HAVE_SCX_TASK_REENQ_PROXY #define HAVE_SCX_TASK_CURSOR #define HAVE_SCX_ECODE_RSN_HOTPLUG #define HAVE_SCX_ECODE_RSN_CGROUP_OFFLINE
Signature · 1 line
2.55.0
Tejun Heo
reviewerRe: [PATCH 12/17] sched_ext: Handle proxy-exec races in remote DSQ transfers
Hello,
Quoted 5 lines · Andrea Righi
+/*
+ * Proxy resolution happens beforerq->curris switched. Queue deferred work
+ * on the rq so that an outgoing proxy owner has clearedon_cpuby the time
+ *reject_dsqis drained.
+ */
This doesn't make sense to me. Deferred operatoins can only execute after the scheduling operations are complete no matter when they were queued. I can see why you'd need this to kick rejected tasks that can't be queued yet.
Quoted 15 lines
void
scx_proxy_resolved(struct rq *rq)
{kernel/sched/ext/ext.c +4
-2+lockdep_assert_rq_held (rq); + +if (rq->scx.flags &SCX_RQ_PROXY_REENQ ) +schedule_deferred_locked (rq); } void schedule_dsq_reenq(struct scx_sched *sch, struct scx_dispatch_q *dsq,@@ -1530,12 +1539,17 @@ static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq,call_task_dequeue (sch, rq, p,0 );/* -* Only local inserts get the wakeup treatment below. Rejects kick the -* deferred reenq and rescue parks are paced by the rescue timer.
kernel/sched/ext/ext.c static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq, · line 1550
Quoted 10 lines
kernel/sched/ext/ext.c +7
-1+* Only local inserts get the wakeup treatment below. Proxy-active tasks +* and rescuees remain parked until their respective resolution paths. +* Other rejects can be reenqueued immediately. */ if (unlikely (dsq->id !=SCX_DSQ_LOCAL )) { -if (dsq->id ==SCX_DSQ_REJECT ) +if (dsq->id ==SCX_DSQ_REJECT ) { +if ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) == +SCX_TASK_REENQ_PROXY ) + rq->scx.flags |=SCX_RQ_PROXY_REENQ ;
and if my reading above is correct, this wouldn't be necessary, right?
Quoted 5 lines
kernel/sched/ext/ext.c +1
-0schedule_deferred_locked (rq); + }return ; }
...
Quoted 1 line
+static bool
task_proxy_move_active(struct task_struct *p)
How about task_proxy_running_or_donating()
Quoted 1 line
+static bool
task_move_proxy_raced(struct task_struct *p)
and task_proxy_unsafe_to_move() instead?
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2599
Quoted 7 lines
kernel/sched/ext/ext.c +7
-0+/* +* Park a task whose remote transfer raced with proxy execution. Reenqueueing +* from the source rq makes the task's owning scheduler choose its placement +* again and preserves sub-scheduler containment. +*/ +static void scx_reject_task (struct scx_sched *sch,struct rq *rq, +struct task_struct *p,u64 enq_flags)
and scx_proxy_reject_task().
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2603
Quoted 4 lines
kernel/sched/ext/ext.c +4
-0+{ +lockdep_assert_rq_held (rq); +WARN_ON_ONCE ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) && + !(enq_flags &SCX_ENQ_REENQ ));
In the previous patch, is it possible to clear reason before reenqueueing it and get rid of overwrite cases or does that quite not work out?
Quoted 16 lines
kernel/sched/ext/ext.c +9
-0+ p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; + + p->scx.holding_cpu =-1 ; + p->scx.flags |=SCX_TASK_REENQ_PROXY ; +scx_prepare_dsq_divert (p, &enq_flags); + +scx_dispatch_enqueue (sch, rq, &rq->scx.reject_dsq, p,0 ,0 , enq_flags); +} +/** * unlink_dsq_and_switch_rq_lock() - Unlink task and switch to its rq lock * @p: target task @@ -2596,6 +2667,20 @@ static bool consume_remote_task(struct scx_sched *sch, struct rq *this_rq, struct scx_dispatch_q *dsq, struct rq *src_rq) { if (unlink_dsq_and_switch_rq_lock(p, dsq, this_rq, src_rq)) {
kernel/sched/ext/ext.c static bool consume_remote_task(struct scx_sched *sch, struct rq *this_rq, · line 2679
Quoted 10 lines
kernel/sched/ext/ext.c +10
-0+/* +* Proxy execution may have changed @p's running or +* migration-disabled state while switching rq locks without +* clearing holding_cpu. Park it on the source rq and let its +* owning scheduler choose its placement again. +*/ +if (unlikely (task_move_proxy_raced (p))) { + p->scx.dsq =NULL ; +scx_reject_task (sch, src_rq, p, + enq_flags |SCX_ENQ_CLEAR_OPSS );
No need for line break.
Quoted 17 lines
kernel/sched/ext/ext.c +5
-0+switch_rq_lock (src_rq, this_rq); +return false; + } +move_remote_task_to_local_dsq (sch, p, enq_flags, src_rq, this_rq);return true; }else {@@ -2626,6 +2711,7 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch, struct scx_dispatch_q *dst_dsq) {struct rq *src_rq =task_rq (p), *dst_rq; +bool proxy_raced;BUG_ON (src_dsq->id ==SCX_DSQ_LOCAL );lockdep_assert_held (&src_dsq->lock);@@ -2633,6 +2719,19 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch,
kernel/sched/ext/ext.c static struct rq *move_task_between_dsqs(struct scx_sched *sch, · line 2729
Quoted 10 lines
kernel/sched/ext/ext.c +8
-0if (dst_dsq->id ==SCX_DSQ_LOCAL ) { dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq); +/* +* Unlike the rq-lock handoff paths, @src_rq has been locked +* throughout this operation. Only active proxy state can race the +* move here; let the enforcing check below diagnose an ordinary +* migration-disabled task. +*/ + proxy_raced = src_rq != dst_rq &&task_proxy_move_active (p); +if (unlikely (proxy_raced)) {
I don't know what bouncing through the local var buys. Out of curiosity, if task_move_proxy_raced() is used here, does something break or is it just to avoid unnecessary tests?
Quoted 19 lines
kernel/sched/ext/ext.c +7
-0+dispatch_dequeue_locked (p, src_dsq); +raw_spin_unlock (&src_dsq->lock); +scx_reject_task (sch, src_rq, p, enq_flags); +return src_rq; + }if (src_rq != dst_rq &&unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { dst_dsq =find_global_dsq (sch,task_cpu (p));@@ -2788,7 +2887,9 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq,/* task_rq couldn't have changed if we're still the holding cpu */ if (likely (p->scx.holding_cpu ==raw_smp_processor_id ()) && !WARN_ON_ONCE (src_rq !=task_rq (p))) { +bool proxy_raced = src_rq != dst_rq &&task_move_proxy_raced (p);bool fallback = false; +/* * If @p is staying on the same rq, there's no need to go * through the full deactivate/activate cycle. Optimize by
kernel/sched/ext/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2902
Quoted 6 lines
kernel/sched/ext/ext.c +1
-2@@ -2798,9 +2899,13 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, p->scx.holding_cpu =-1 ;scx_dispatch_enqueue (sch, dst_rq, &dst_rq->scx.local_dsq, p, slice, vtime, enq_flags |SCX_ENQ_APPLY_SLICE ); - }else if (unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { - p->scx.holding_cpu =-1 ; + }else if (unlikely (proxy_raced)) {
Ditto, I don't know what the bouncing through proxy_raced buysk. Alos, in this else clause src_rq != dst_rq is already established.
kernel/sched/ext/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2906
Quoted 4 lines
kernel/sched/ext/ext.c +3
-0fallback = true; +scx_reject_task (sch, src_rq, p, enq_flags); + }else if (unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, + true))) {
No need for line break.
kernel/sched/ext/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2908
Quoted 2 lines
kernel/sched/ext/ext.c +2
-0+ fallback = true; + p->scx.holding_cpu =-1 ;
fallback = true and p->scx.holding_cpu = -1 lines are swapped compared to the original code making the diff look a bit confusing.
Quoted 32 lines
kernel/sched/ext/ext.c +0
-0static void scx_reenq_reject (struct rq *rq) {LIST_HEAD (tasks);kernel/sched/ext/ext.c +11
-4struct task_struct *p, *n; +bool proxy_pending = false;lockdep_assert_rq_held (rq); -if (list_empty (&rq->scx.reject_dsq.list)) +if (list_empty (&rq->scx.reject_dsq.list)) { + rq->scx.flags &= ~SCX_RQ_PROXY_REENQ ;return ; + }/* -* Move tasks to a private list so a task re-rejected by +* Move ready tasks to a private list so a task re-rejected by * scx_do_enqueue_task() below isn't revisited this round. */ list_for_each_entry_safe (p, n, &rq->scx.reject_dsq.list, scx.dsq_list.node) {u32 reason = p->scx.flags &SCX_TASK_REENQ_REASON_MASK ; -/* migration_pending tasks should have bypassed to local DSQ */ -WARN_ON_ONCE (p->migration_pending);WARN_ON_ONCE (!reason); +/* +* The affinity machinery owns placement while a migration is +* pending and will dequeue and reactivate @p as necessary. Don't +* return it to BPF in the meantime. This isn't a proxy-resolution +* state and thus doesn't contribute to @proxy_pending. +*/
kernel/sched/ext/ext.c static void process_deferred_reenq_users(struct rq *rq) · line 4835
Quoted 10 lines
kernel/sched/ext/ext.c +10
-0+if (p->migration_pending) { +WARN_ON_ONCE (reason !=SCX_TASK_REENQ_PROXY ); +continue ; + } + +if (reason ==SCX_TASK_REENQ_PROXY && + (task_on_cpu (rq, p) ||task_current_donor (rq, p))) { + proxy_pending = true; +continue ; + }
Can you structure the code so that it's:
if (proxy_exec_enabled() && reason == SCX_TASK_REENQ_PROXY) {
...
else {
WARN_ON_ONCE(p->migration_pending);
...
}kernel/sched/ext/ext.c static void process_deferred_reenq_users(struct rq *rq) · line 4846
Quoted 10 lines
kernel/sched/ext/ext.c +4
-0scx_dispatch_dequeue (rq, p); p->scx.flags |= reason;list_add_tail (&p->scx.dsq_list.node, &tasks); } +if (proxy_pending) + rq->scx.flags |=SCX_RQ_PROXY_REENQ ; +else + rq->scx.flags &= ~SCX_RQ_PROXY_REENQ ;
Hmm... this looks unconventional. Shouldn't the cosumer of the flag - scx_proxy_resolved() - clear it?
kernel/sched/ext/internal.h enum scx_enq_flags { · line 1762
Quoted 10 lines
kernel/sched/ext/internal.h +2
-0@@ -1758,6 +1758,14 @@ enum scx_enq_flags { SCX_ENQ_SLICE_DFL = 1LLU << 62, /* carried slice is a default refill */ }; +/* Strip priority and carried slice state when diverting from a local DSQ. */ +static inline void scx_prepare_dsq_divert (struct task_struct *p,u64 *enq_flags)
How about scx_divert_strip_flags()?
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerRe: [PATCH 12/17] sched_ext: Handle proxy-exec races in remote DSQ transfers
Hi Tejun,
Quoted 9 lines · Tejun Heo
Hello,
Quoted 5 lines · Andrea Righi
+/*
+ * Proxy resolution happens beforerq->curris switched. Queue deferred work
+ * on the rq so that an outgoing proxy owner has clearedon_cpuby the time
+ *reject_dsqis drained.
+ */
Quoted 3 lines
This doesn't make sense to me. Deferred operatoins can only execute after the scheduling operations are complete no matter when they were queued. I can see why you'd need this to kick rejected tasks that can't be queued yet.
You're right, this comment comes from an earlier version where proxy-active rejects did not schedule an initial deferred drain and the hook was used to arrange the post-switch drain.
The rejection path has changed to schedule deferred work for every reject, so queueing it from this hook is not what provides the post-switch ordering.
But the hook is still needed as a retry notification. When the initial drain runs, the rejected task may still be on_cpu or remain the active donor if the scheduling operation retained or selected the same proxy execution context. In that case, scx_reenq_reject() must leave the task parked and record that another attempt is needed. A later proxy resolution then queues that attempt.
The flag also records the outstanding drain independently of the balance callback used to schedule it. If proxy re-pick calls zap_balance_callbacks() before that callback runs, scx_proxy_resolved() uses the flag to queue replacement deferred work.
I'll rewrite the comment to describe this retry role.
Quoted 13 lines
Quoted 13 lines
void
scx_proxy_resolved(struct rq *rq)
{kernel/sched/ext/ext.c +4
-0+lockdep_assert_rq_held (rq); + +if (rq->scx.flags &SCX_RQ_PROXY_REENQ ) +schedule_deferred_locked (rq); } void schedule_dsq_reenq(struct scx_sched *sch, struct scx_dispatch_q *dsq,@@ -1530,12 +1539,17 @@ static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq,call_task_dequeue (sch, rq, p,0 );
Quoted 15 lines
kernel/sched/ext/ext.c static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq, · line 1550
Quoted 13 lines
kernel/sched/ext/ext.c +7
-3/* -* Only local inserts get the wakeup treatment below. Rejects kick the -* deferred reenq and rescue parks are paced by the rescue timer. +* Only local inserts get the wakeup treatment below. Proxy-active tasks +* and rescuees remain parked until their respective resolution paths. +* Other rejects can be reenqueued immediately. */ if (unlikely (dsq->id !=SCX_DSQ_LOCAL )) { -if (dsq->id ==SCX_DSQ_REJECT ) +if (dsq->id ==SCX_DSQ_REJECT ) { +if ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) == +SCX_TASK_REENQ_PROXY ) + rq->scx.flags |=SCX_RQ_PROXY_REENQ ;and if my reading above is correct, this wouldn't be necessary, right?
The flag isn't needed to provide the post-switch ordering, but it is needed to record that reject_dsq still needs another drain.
scx_reenq_reject() sets it when a proxy-rejected task remains on_cpu or continues to be the active donor, scx_proxy_resolved() then can use it to queue another attempt.
It also preserves the outstanding drain if proxy re-pick calls zap_balance_callbacks() before the initially queued callback runs. In that case, the task is already parked on reject_dsq, but the flag lets scx_proxy_resolved() queue replacement deferred work.
Quoted 10 lines
Quoted 6 lines
kernel/sched/ext/ext.c +1
-0schedule_deferred_locked (rq); + }return ; }...
Quoted 1 line
+static bool
task_proxy_move_active(struct task_struct *p)How about
task_proxy_running_or_donating()
Ack.
Quoted 4 lines
Quoted 2 lines
+static bool
task_move_proxy_raced(struct task_struct *p)and
task_proxy_unsafe_to_move()instead?
Ack.
Quoted 10 lines
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2599
Quoted 8 lines
kernel/sched/ext/ext.c +7
-0+/* +* Park a task whose remote transfer raced with proxy execution. Reenqueueing +* from the source rq makes the task's owning scheduler choose its placement +* again and preserves sub-scheduler containment. +*/ +static void scx_reject_task (struct scx_sched *sch,struct rq *rq, +struct task_struct *p,u64 enq_flags)and
scx_proxy_reject_task().
Ack.
Quoted 8 lines
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2603
Quoted 5 lines
kernel/sched/ext/ext.c +4
-0+{ +lockdep_assert_rq_held (rq); +WARN_ON_ONCE ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) && + !(enq_flags &SCX_ENQ_REENQ ));In the previous patch, is it possible to clear reason before reenqueueing it and get rid of overwrite cases or does that quite not work out?
Yes, I think we can get rid of the overwrite cases. There is one constraint,
though: the reason must remain set while ops.enqueue() runs, because BPF reads it from p->scx.flags.
I'll move the clearing into scx_do_enqueue_task() so that it happens immediately after ops.enqueue() returns and before resolving any direct dispatch requested by the callback. A rejection caused by that new placement will then see a clear reason field and can set its own reason without overwriting the previous one.
For paths which bypass ops.enqueue(), the reason can be cleared before the kernel placement. With that lifetime enforced in scx_do_enqueue_task(), scx_finish_reenqueue() and the overwrite handling in the previous patch can go away.
Quoted 21 lines
Quoted 21 lines
kernel/sched/ext/ext.c +13
-0+ p->scx.flags &= ~SCX_TASK_REENQ_REASON_MASK ; + + p->scx.holding_cpu =-1 ; + p->scx.flags |=SCX_TASK_REENQ_PROXY ; +scx_prepare_dsq_divert (p, &enq_flags); + +scx_dispatch_enqueue (sch, rq, &rq->scx.reject_dsq, p,0 ,0 , enq_flags); +} +/** * unlink_dsq_and_switch_rq_lock() - Unlink task and switch to its rq lock * @p: target task @@ -2596,6 +2667,20 @@ static bool consume_remote_task(struct scx_sched *sch, struct rq *this_rq, struct scx_dispatch_q *dsq, struct rq *src_rq) { if (unlink_dsq_and_switch_rq_lock(p, dsq, this_rq, src_rq)) { +/* +* Proxy execution may have changed @p's running or +* migration-disabled state while switching rq locks without +* clearing holding_cpu. Park it on the source rq and let its
Quoted 8 lines
kernel/sched/ext/ext.c static bool consume_remote_task(struct scx_sched *sch, struct rq *this_rq, · line 2679
Quoted 6 lines
kernel/sched/ext/ext.c +6
-0+* owning scheduler choose its placement again. +*/ +if (unlikely (task_move_proxy_raced (p))) { + p->scx.dsq =NULL ; +scx_reject_task (sch, src_rq, p, + enq_flags |SCX_ENQ_CLEAR_OPSS );No need for line break.
Ack.
Quoted 18 lines
Quoted 18 lines
kernel/sched/ext/ext.c +5
-0+switch_rq_lock (src_rq, this_rq); +return false; + } +move_remote_task_to_local_dsq (sch, p, enq_flags, src_rq, this_rq);return true; }else {@@ -2626,6 +2711,7 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch, struct scx_dispatch_q *dst_dsq) {struct rq *src_rq =task_rq (p), *dst_rq; +bool proxy_raced;BUG_ON (src_dsq->id ==SCX_DSQ_LOCAL );lockdep_assert_held (&src_dsq->lock);@@ -2633,6 +2719,19 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch,
Quoted 14 lines
kernel/sched/ext/ext.c static struct rq *move_task_between_dsqs(struct scx_sched *sch, · line 2729
Quoted 10 lines
kernel/sched/ext/ext.c +8
-0if (dst_dsq->id ==SCX_DSQ_LOCAL ) { dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq); +/* +* Unlike the rq-lock handoff paths, @src_rq has been locked +* throughout this operation. Only active proxy state can race the +* move here; let the enforcing check below diagnose an ordinary +* migration-disabled task. +*/ + proxy_raced = src_rq != dst_rq &&task_proxy_move_active (p); +if (unlikely (proxy_raced)) {I don't know what bouncing through the local var buys. Out of curiosity, if
task_move_proxy_raced()is used here, does something break or is it just to avoid unnecessary tests?
Yeah, the local variable doesn't buy anything, I'll remove it.
Using task_move_proxy_raced() (aka task_proxy_unsafe_to_move() after the rename) would change the behavior for an ordinary migration-disabled task. This path holds src_rq, so that state isn't a lock-handoff race and should still reach task_can_run_on_remote_rq(..., true), which diagnoses the invalid BPF-directed migration. Treating it as a proxy rejection instead could hide that error and cause repeated reenqueues.
Quoted 21 lines
Quoted 21 lines
kernel/sched/ext/ext.c +7
-0+dispatch_dequeue_locked (p, src_dsq); +raw_spin_unlock (&src_dsq->lock); +scx_reject_task (sch, src_rq, p, enq_flags); +return src_rq; + }if (src_rq != dst_rq &&unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { dst_dsq =find_global_dsq (sch,task_cpu (p));@@ -2788,7 +2887,9 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq,/* task_rq couldn't have changed if we're still the holding cpu */ if (likely (p->scx.holding_cpu ==raw_smp_processor_id ()) && !WARN_ON_ONCE (src_rq !=task_rq (p))) { +bool proxy_raced = src_rq != dst_rq &&task_move_proxy_raced (p);bool fallback = false; +/* * If @p is staying on the same rq, there's no need to go * through the full deactivate/activate cycle. Optimize by @@ -2798,9 +2899,13 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, p->scx.holding_cpu =-1 ;
Quoted 8 lines
kernel/sched/ext/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2902
Quoted 5 lines
kernel/sched/ext/ext.c +1
-2scx_dispatch_enqueue (sch, dst_rq, &dst_rq->scx.local_dsq, p, slice, vtime, enq_flags |SCX_ENQ_APPLY_SLICE ); - }else if (unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, true))) { - p->scx.holding_cpu =-1 ; + }else if (unlikely (proxy_raced)) {Ditto, I don't know what the bouncing through
proxy_racedbuysk. Alos, in this else clausesrc_rq!=dst_rqis already established.
Ack.
Quoted 7 lines
kernel/sched/ext/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2906
Quoted 5 lines
kernel/sched/ext/ext.c +3
-0fallback = true; +scx_reject_task (sch, src_rq, p, enq_flags); + }else if (unlikely (!task_can_run_on_remote_rq (sch, p, dst_rq, + true))) {No need for line break.
Ack.
Quoted 6 lines
kernel/sched/ext/ext.c static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, · line 2908
Quoted 3 lines
kernel/sched/ext/ext.c +2
-0+ fallback = true; + p->scx.holding_cpu =-1 ;fallback = true and
p->scx.holding_cpu= -1 lines are swapped compared to the original code making the diff look a bit confusing.
Ack.
Quoted 46 lines
Quoted 33 lines
kernel/sched/ext/ext.c +0
-0static void scx_reenq_reject (struct rq *rq) {LIST_HEAD (tasks);kernel/sched/ext/ext.c +11
-4struct task_struct *p, *n; +bool proxy_pending = false;lockdep_assert_rq_held (rq); -if (list_empty (&rq->scx.reject_dsq.list)) +if (list_empty (&rq->scx.reject_dsq.list)) { + rq->scx.flags &= ~SCX_RQ_PROXY_REENQ ;return ; + }/* -* Move tasks to a private list so a task re-rejected by +* Move ready tasks to a private list so a task re-rejected by * scx_do_enqueue_task() below isn't revisited this round. */ list_for_each_entry_safe (p, n, &rq->scx.reject_dsq.list, scx.dsq_list.node) {u32 reason = p->scx.flags &SCX_TASK_REENQ_REASON_MASK ; -/* migration_pending tasks should have bypassed to local DSQ */ -WARN_ON_ONCE (p->migration_pending);WARN_ON_ONCE (!reason); +/* +* The affinity machinery owns placement while a migration is +* pending and will dequeue and reactivate @p as necessary. Don't +* return it to BPF in the meantime. This isn't a proxy-resolution +* state and thus doesn't contribute to @proxy_pending. +*/ kernel/sched/ext/ext.c static void process_deferred_reenq_users(struct rq *rq) · line 4835
Quoted 10 lines
kernel/sched/ext/ext.c +10
-0+if (p->migration_pending) { +WARN_ON_ONCE (reason !=SCX_TASK_REENQ_PROXY ); +continue ; + } + +if (reason ==SCX_TASK_REENQ_PROXY && + (task_on_cpu (rq, p) ||task_current_donor (rq, p))) { + proxy_pending = true; +continue ; + }Can you structure the code so that it's:
Quoted 7 lines
if (proxy_exec_enabled() && reason == SCX_TASK_REENQ_PROXY) {
...
else {
WARN_ON_ONCE(p->migration_pending);
...
}Ack.
Quoted 6 lines
Quoted 6 lines
kernel/sched/ext/ext.c +0
-0scx_dispatch_dequeue (rq, p); p->scx.flags |= reason;list_add_tail (&p->scx.dsq_list.node, &tasks); }
Quoted 7 lines
kernel/sched/ext/ext.c static void process_deferred_reenq_users(struct rq *rq) · line 4846
Quoted 4 lines
kernel/sched/ext/ext.c +4
-0+if (proxy_pending) + rq->scx.flags |=SCX_RQ_PROXY_REENQ ; +else + rq->scx.flags &= ~SCX_RQ_PROXY_REENQ ;Hmm... this looks unconventional. Shouldn't the cosumer of the flag -
scx_proxy_resolved()- clear it?
Agreed. I'll make scx_proxy_resolved() clear the flag before scheduling the deferred work.
Quoted 9 lines
Quoted 9 lines
kernel/sched/ext/internal.h +0
-0@@ -1758,6 +1758,14 @@ enum scx_enq_flags { SCX_ENQ_SLICE_DFL = 1LLU << 62, /* carried slice is a default refill */ };
Quoted 4 lines
kernel/sched/ext/internal.h enum scx_enq_flags { · line 1762
Quoted 2 lines
kernel/sched/ext/internal.h +2
-0+/* Strip priority and carried slice state when diverting from a local DSQ. */ +static inline void scx_prepare_dsq_divert (struct task_struct *p,u64 *enq_flags)How about
scx_divert_strip_flags()?
Ack.
Thanks tons for the review!
-Andrea
Tejun Heo
reviewerRe: [PATCH 12/17] sched_ext: Handle proxy-exec races in remote DSQ transfers
Hello,
Quoted 19 lines · Andrea Righi
Quoted 18 lines
Quoted 6 lines
kernel/sched/ext/ext.c +0
-2@@ -1530,12 +1539,17 @@ static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq,call_task_dequeue (sch, rq, p,0 );/* -* Only local inserts get the wakeup treatment below. Rejects kick the -* deferred reenq and rescue parks are paced by the rescue timer. kernel/sched/ext/ext.c static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq, · line 1550
Quoted 10 lines
kernel/sched/ext/ext.c +7
-1+* Only local inserts get the wakeup treatment below. Proxy-active tasks +* and rescuees remain parked until their respective resolution paths. +* Other rejects can be reenqueued immediately. */ if (unlikely (dsq->id !=SCX_DSQ_LOCAL )) { -if (dsq->id ==SCX_DSQ_REJECT ) +if (dsq->id ==SCX_DSQ_REJECT ) { +if ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) == +SCX_TASK_REENQ_PROXY ) + rq->scx.flags |=SCX_RQ_PROXY_REENQ ;and if my reading above is correct, this wouldn't be necessary, right?
Quoted 2 lines
The flag isn't needed to provide the post-switch ordering, but it is needed to record that
reject_dsqstill needs another drain.
But wouldn't it be able to use the same schedule_deferred_locked() call like SCX_DSQ_REJECT case so that the PROXY_REENQ flag is only used for retry cases (and maybe renamed accordingly)?
...
Quoted 4 lines
It also preserves the outstanding drain if proxy re-pick calls
zap_balance_callbacks()before the initially queued callback runs. In that case, the task is already parked onreject_dsq, but the flag letsscx_proxy_resolved()queue replacement deferred work.
I think this is a generic problem with core scheduling. SCX assumes that deferred scheduilng always runs but core-sched can zap them. We probably need to address this directly using a similar but generic deferred work pending flag.
Quoted 8 lines
Quoted 7 lines
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2603
Quoted 4 lines
kernel/sched/ext/ext.c +4
-0+{ +lockdep_assert_rq_held (rq); +WARN_ON_ONCE ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) && + !(enq_flags &SCX_ENQ_REENQ ));In the previous patch, is it possible to clear reason before reenqueueing it and get rid of overwrite cases or does that quite not work out?
Quoted 8 lines
Yes, I think we can get rid of the overwrite cases. There is one constraint,
though: the reason must remain set whileops.enqueue()runs, because BPF reads it fromp->scx.flags.I'll move the clearing into
scx_do_enqueue_task()so that it happens immediately afterops.enqueue()returns and before resolving any direct dispatch requested by the callback. A rejection caused by that new placement will then see a clear reason field and can set its own reason without overwriting the previous one.
I haven't really thought through it so please do whatever that makes the code least ugly.
Quoted 17 lines
Quoted 16 lines
kernel/sched/ext/ext.c static struct rq *move_task_between_dsqs(struct scx_sched *sch, · line 2729
Quoted 12 lines
kernel/sched/ext/ext.c +8
-0@@ -2633,6 +2719,19 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch,if (dst_dsq->id ==SCX_DSQ_LOCAL ) { dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq); +/* +* Unlike the rq-lock handoff paths, @src_rq has been locked +* throughout this operation. Only active proxy state can race the +* move here; let the enforcing check below diagnose an ordinary +* migration-disabled task. +*/ + proxy_raced = src_rq != dst_rq &&task_proxy_move_active (p); +if (unlikely (proxy_raced)) {I don't know what bouncing through the local var buys. Out of curiosity, if
task_move_proxy_raced()is used here, does something break or is it just to avoid unnecessary tests?
Quoted 8 lines
Yeah, the local variable doesn't buy anything, I'll remove it.
Using
task_move_proxy_raced()(akatask_proxy_unsafe_to_move()after the rename) would change the behavior for an ordinary migration-disabled task. This path holdssrc_rq, so that state isn't a lock-handoff race and should still reachtask_can_run_on_remote_rq(..., true), which diagnoses the invalid BPF-directed migration. Treating it as a proxy rejection instead could hide that error and cause repeated reenqueues.
That sounds like something which is worth noting in the comment. It's subtle that this site needs a different set of conditions.
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerRe: [PATCH 12/17] sched_ext: Handle proxy-exec races in remote DSQ transfers
Quoted 22 lines · Tejun Heo
Hello,
Quoted 19 lines · Andrea Righi
Quoted 18 lines
Quoted 6 lines
kernel/sched/ext/ext.c +0
-2@@ -1530,12 +1539,17 @@ static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq,call_task_dequeue (sch, rq, p,0 );/* -* Only local inserts get the wakeup treatment below. Rejects kick the -* deferred reenq and rescue parks are paced by the rescue timer. kernel/sched/ext/ext.c static void rq_owned_post_enq(struct scx_sched *sch, struct rq *rq, · line 1550
Quoted 10 lines
kernel/sched/ext/ext.c +7
-1+* Only local inserts get the wakeup treatment below. Proxy-active tasks +* and rescuees remain parked until their respective resolution paths. +* Other rejects can be reenqueued immediately. */ if (unlikely (dsq->id !=SCX_DSQ_LOCAL )) { -if (dsq->id ==SCX_DSQ_REJECT ) +if (dsq->id ==SCX_DSQ_REJECT ) { +if ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) == +SCX_TASK_REENQ_PROXY ) + rq->scx.flags |=SCX_RQ_PROXY_REENQ ;and if my reading above is correct, this wouldn't be necessary, right?
Quoted 6 lines
Quoted 2 lines
The flag isn't needed to provide the post-switch ordering, but it is needed to record that
reject_dsqstill needs another drain.But wouldn't it be able to use the same
schedule_deferred_locked()call likeSCX_DSQ_REJECTcase so that thePROXY_REENQflag is only used for retry cases (and maybe renamed accordingly)?
Yes. The initial insertion into reject_dsq can use schedule_deferred_locked() without setting a proxy-specific flag. Then we can set the flag only when scx_reenq_reject() actually skips a task because it is still running or donating. How about renaming to SCX_RQ_PROXY_RETRY to refelct the new meaning?
Quoted 11 lines
...
Quoted 4 lines
It also preserves the outstanding drain if proxy re-pick calls
zap_balance_callbacks()before the initially queued callback runs. In that case, the task is already parked onreject_dsq, but the flag letsscx_proxy_resolved()queue replacement deferred work.I think this is a generic problem with core scheduling. SCX assumes that deferred scheduilng always runs but core-sched can zap them. We probably need to address this directly using a similar but generic deferred work pending flag.
Agreed. This isn't specific to proxy-rejected tasks, any sched_ext deferred work queued through a balance callback can be lost if core scheduling zaps the callback.
Maybe we can introduce a new generic sched_ext deferred work pending flag, kept set until the deferred work actually runs and we can use it to rearm it after callback cancellation?
Probably something to do for another patch series...
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2603
Quoted 5 lines
kernel/sched/ext/ext.c +4
-0+{ +lockdep_assert_rq_held (rq); +WARN_ON_ONCE ((p->scx.flags &SCX_TASK_REENQ_REASON_MASK ) && + !(enq_flags &SCX_ENQ_REENQ ));In the previous patch, is it possible to clear reason before reenqueueing it and get rid of overwrite cases or does that quite not work out?
Yes, I think we can get rid of the overwrite cases. There is one constraint,
though: the reason must remain set whileops.enqueue()runs, because BPF reads it fromp->scx.flags.
Quoted 7 lines
Quoted 4 lines
I'll move the clearing into
scx_do_enqueue_task()so that it happens immediately afterops.enqueue()returns and before resolving any direct dispatch requested by the callback. A rejection caused by that new placement will then see a clear reason field and can set its own reason without overwriting the previous one.I haven't really thought through it so please do whatever that makes the code least ugly.
Ok, probably moving the clearing into scx_do_enqueue_task(), after ops.enqueue() returns but before resolving the resulting placement is the least ugly way... This keeps the reason visible to BPF while ensuring that a subsequent rejection starts with a clear reason field.
Quoted 27 lines
Quoted 18 lines
Quoted 17 lines
kernel/sched/ext/ext.c static struct rq *move_task_between_dsqs(struct scx_sched *sch, · line 2729
Quoted 13 lines
kernel/sched/ext/ext.c +8
-0@@ -2633,6 +2719,19 @@ static struct rq *move_task_between_dsqs(struct scx_sched *sch,if (dst_dsq->id ==SCX_DSQ_LOCAL ) { dst_rq = container_of(dst_dsq, struct rq, scx.local_dsq); +/* +* Unlike the rq-lock handoff paths, @src_rq has been locked +* throughout this operation. Only active proxy state can race the +* move here; let the enforcing check below diagnose an ordinary +* migration-disabled task. +*/ + proxy_raced = src_rq != dst_rq &&task_proxy_move_active (p); +if (unlikely (proxy_raced)) {I don't know what bouncing through the local var buys. Out of curiosity, if
task_move_proxy_raced()is used here, does something break or is it just to avoid unnecessary tests?Quoted 8 lines
Yeah, the local variable doesn't buy anything, I'll remove it.
Using
task_move_proxy_raced()(akatask_proxy_unsafe_to_move()after the rename) would change the behavior for an ordinary migration-disabled task. This path holdssrc_rq, so that state isn't a lock-handoff race and should still reachtask_can_run_on_remote_rq(..., true), which diagnoses the invalid BPF-directed migration. Treating it as a proxy rejection instead could hide that error and cause repeated reenqueues.
Quoted 2 lines
That sounds like something which is worth noting in the comment. It's subtle that this site needs a different set of conditions.
Agreed. I'll remove the local variable and expand the comment.
Thanks for all the help! Really appreciated!
-Andrea
Tejun Heo
reviewerRe: [PATCH 12/17] sched_ext: Handle proxy-exec races in remote DSQ transfers
Quoted 8 lines · Andrea Righi
Quoted 3 lines
But wouldn't it be able to use the same
schedule_deferred_locked()call likeSCX_DSQ_REJECTcase so that thePROXY_REENQflag is only used for retry cases (and maybe renamed accordingly)?Yes. The initial insertion into
reject_dsqcan useschedule_deferred_locked()without setting a proxy-specific flag. Then we can set the flag only whenscx_reenq_reject()actually skips a task because it is still running or donating. How about renaming toSCX_RQ_PROXY_RETRYto refelct the new meaning?
Sounds good to me.
Quoted 9 lines
Quoted 4 lines
I think this is a generic problem with core scheduling. SCX assumes that deferred scheduilng always runs but core-sched can zap them. We probably need to address this directly using a similar but generic deferred work pending flag.
Agreed. This isn't specific to proxy-rejected tasks, any
sched_extdeferred work queued through a balance callback can be lost if core scheduling zaps the callback.
Quoted 5 lines
Maybe we can introduce a new generic
sched_extdeferred work pending flag, kept set until the deferred work actually runs and we can use it to rearm it after callback cancellation?Probably something to do for another patch series...
Yeap.
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerPATCH 13/17 sched_ext: Split curr|donor references properly
In reply to Andrea RighiWith proxy execution, the task selected by the scheduler and the task physically executing can differ. A blocked mutex waiter donates its scheduling context to the lock owner:
D -----------------> M -------------> O ----------------> T [donor] blocked on [mutex] owned by [owner] preempted by [task]
\_________________________________^
donates scheduling context
where:
D = blocked donor
M = mutex
O = mutex owner
T = competing runnable task
During a proxy execution switch, D supplies the scheduling class, priority, and runtime budget, while O supplies the execution context: O is the task whose code physically executes. T is a competing runnable task which may preempt the D/O proxy execution.
Consider FAIR and EXT tasks with sched_ext running in partial mode. FAIR can be replaced with a higher scheduling class such as RT or deadline without changing the class interaction described here. The possible combinations are:
1. D is EXT, O is EXT, T is EXT
D can interrupt T according to BPF scheduling policy. O executes with D's EXT priority and runtime budget, while T waits in EXT.
2. D is EXT, O is EXT, T is FAIR
D is visible to the BPF scheduler, but cannot preempt T because EXT is below FAIR. Once T stops, BPF can dispatch D and O executes with D's EXT priority and runtime budget. If T becomes runnable again, it preempts the D/O proxy execution.
3. D is EXT, O is FAIR, T is EXT
This cannot represent T preempting O because EXT is below FAIR.
4. D is EXT, O is FAIR, T is FAIR
D cannot boost O above T because EXT is below FAIR. O and T continue competing under FAIR. Once O releases M, D wakes and resumes normal EXT scheduling.
5. D is FAIR, O is EXT, T is EXT
D preempts T as the higher-class scheduling context. O executes with D's FAIR priority and runtime budget, while T waits in EXT. D is not visible to the BPF scheduler.
6. D is FAIR, O is EXT, T is FAIR
D competes with T according to its FAIR deadline. When D is selected, O executes with D's FAIR priority and runtime budget. D is not visible to the BPF scheduler.
7. D is FAIR, O is FAIR, T is EXT
This cannot represent T preempting O because EXT is below FAIR.
8. D is FAIR, O is FAIR, T is FAIR
O, T, and D all have FAIR scheduling contexts. D remains runnable as a blocked proxy donor. When CFS selects D, O executes using D's FAIR scheduling context. When CFS selects O, O executes using its own FAIR context, and when CFS selects T, T executes normally. D is not visible to the BPF scheduler.
Thus, sched_ext policy and accounting must generally use rq->donor, the scheduler-selected task which supplies the scheduling context, rather than rq->curr, the task whose code physically executes. Without proxy execution they are the same task.
On nohz_full CPUs, a blocked proxy donor must retain the scheduler tick even when it has an infinite slice. Otherwise, a full dynticks CPU could stop the tick while rq->curr and rq->donor differ, violating assumptions made by the remote NOHZ tick path.
This is a conservative compromise that keeps the change local to sched_ext, at the cost of a periodic tick while a blocked proxy donor is selected. Allowing blocked proxy donors to run tickless would require making the core scheduler's remote tick handling aware that rq->curr and rq->donor can differ.
Moreover, extend scx_dump_state() to report both contexts. Each CPU record now includes a donor= line. If an EXT donor differs from rq->curr, also emit its detailed task record. The existing '*' marker continues to identify rq->curr, while the donor= line identifies the otherwise unmarked donor record.
Note that at this point in the series, CONFIG_SCHED_PROXY_EXEC still depends on !CONFIG_SCHED_CLASS_EXT, so proxy execution and sched_ext cannot be enabled together. The scheduling changes are therefore preparatory. A later patch removes this restriction.
--- Documentation/scheduler/sched-ext.rst | 13 +++ kernel/sched/ext/ext.c | 128 +++++++++++++++++--------- kernel/sched/ext/sub.c | 5 +- kernel/sched/ext/sub.h | 11 ++- 4 files changed, 104 insertions(+), 53 deletions(-)
Documentation/scheduler/sched-ext.rst +13 -0
@@ -488,6 +488,19 @@ and edge cases, to name a few examples: class, in which case it will exit the tick-dispatch loop even though it is runnable and has a non-zero slice. +* Under proxy execution, sched_ext continues to observe the donor as the + current scheduling context. Accordingly, ``ops.running()`` and + ``ops.stopping()`` report when the donor's scheduling context becomes active + and inactive, even when the donor is blocked and a lock owner executes on its + behalf. The physical execution context is intentionally not reported through + these callbacks. + + A blocked donor enters a running session only after proxy resolution finds + an execution context. The session remains active if only the physical + execution context changes while the donor remains the same. Running sessions + are tracked so that ``ops.running()`` and ``ops.stopping()`` remain paired + and are not emitted recursively. + See the "Scheduling Cycle" section for a more detailed description of how a freshly woken up task gets on a CPU.
kernel/sched/ext/ext.c +82 -46
342 lines not on this page · raw message
kernel/sched/ext/sub.c +3 -2
@@ -285,7 +285,8 @@ void scx_rescue_charge(struct rq *rq, s64 delta_exec) rq->scx.rescue.budget -= delta_exec;/* per-cpu usage average feeds the overload victim pick */ - pcpu =per_cpu_ptr (scx_task_sched (rq->curr)->pcpu,cpu_of (rq)); + pcpu =per_cpu_ptr (scx_task_sched (rq->scx.rescue.curr)->pcpu, +cpu_of (rq)); pcpu->rescue_avg =scx_rescue_decay_avg (pcpu) + delta_exec;if (!scx_rescue_slice_remaining (rq))
@@ -557,7 +558,7 @@ static void scx_rescue_timerfn(struct timer_list *timer)scx_rescue_admit (rq, p, slice);scx_move_local_task_to_local_dsq (scx_task_sched (p), p,SCX_ENQ_IGNORE_CAPS , &rq->scx.rescue.dsq, rq); -if (sched_class_above (&ext_sched_class, rq->curr->sched_class)) +if (sched_class_above (&ext_sched_class, rq->donor->sched_class))resched_curr (rq); }else if (p->scx.dsq && rq->scx.rescue.budget >2 * scx_rescue_quantum_ns) {/*
kernel/sched/ext/sub.h +6 -5
@@ -167,17 +167,18 @@ static inline u64 scx_caps_for_task(struct task_struct *p)return SCX_CAP_ENQ ; } -/* the cap @sch needs to preempt @rq's current task, 0 if none */ -static inline u64 scx_caps_for_preempt (struct scx_sched *sch,struct rq *rq,u64 enq_flags) +/* the cap @sch needs to preempt @rq's current scheduling context, 0 if none */ +static inline u64 scx_caps_for_preempt (struct scx_sched *sch,struct rq *rq, +u64 enq_flags) { -struct task_struct *curr = rq->curr; +struct task_struct *donor = rq->donor;/* a kernel-forced placement preempts regardless of caps */ if (unlikely (enq_flags &SCX_ENQ_IGNORE_CAPS ))return 0 ;/* a non-ext task can't be preempted by ext, own-subtree needs no cap */ -if (curr->sched_class != &ext_sched_class || -scx_is_descendant (scx_task_sched (curr), sch)) +if (donor->sched_class != &ext_sched_class || +scx_is_descendant (scx_task_sched (donor), sch))return 0 ;return SCX_CAP_PREEMPT ; }
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 14/17 sched_ext: Delegate proxy donor admission to BPF schedulers
In reply to Andrea RighiProxy execution keeps a mutex-blocked donor runnable so that its scheduling context can execute the mutex owner.
Define SCX_OPS_ENQ_BLOCKED as the admission contract for retained donors. Schedulers without the flag block EXT donors normally. Schedulers with the flag continue to own blocked donors and receive them through ops.enqueue() with SCX_ENQ_BLOCKED, allowing BPF to choose their DSQ, CPU and ordering.
After BPF dispatches a donor, proxy execution may move its context to the rq of the mutex owner. This move is distinct from BPF placement:wake_cpu continues to identify the callback CPU of the donor, while task_cpu() identifies the proxy CPU. A proxy-migrated donor returns through the full wakeup activation path when the mutex is released.
Do not carry a retained proxy session across BPF scheduler ownership changes. Before root activation, parent-to-child takeover, rehome or punt, fully deactivate a retained donor unconditionally. The incoming scheduler then starts with clean task state and applies its own admission policy the next time the task blocks.
A retained donor that wakes on its callback rq does not pass through enqueue_task_scx() again. Track full wakeup enqueueing with SCX_TASK_ENQ_WAKEUP so wakeup_preempt_scx() can request dispatch reconsideration only for this retained-donor case.
Blocked-donor enqueueing takes precedence over the exiting and migration-disabled local-DSQ fallbacks, so that an opted-in scheduler sees every eligible donor request.
--- include/linux/sched/ext.h | 1 + kernel/sched/ext/ext.c | 141 ++++++++++++++++-- kernel/sched/ext/internal.h | 23 ++- kernel/sched/ext/sub.c | 9 +- tools/sched_ext/include/scx/compat.h | 1 + .../sched_ext/include/scx/enum_defs.autogen.h | 2 + .../sched_ext/include/scx/enums.autogen.bpf.h | 3 + tools/sched_ext/include/scx/enums.autogen.h | 1 + 8 files changed, 161 insertions(+), 20 deletions(-)
include/linux/sched/ext.h +1 -0
@@ -105,6 +105,7 @@ enum scx_ent_flags {SCX_TASK_IMMED =1 <<5 ,/* task is on local DSQ with %SCX_ENQ_IMMED */ SCX_TASK_PROTECTED =1 <<6 ,/* slice and DSQ head position protected */ SCX_TASK_RUN_TRACKED =1 <<7 ,/* task is in an ops.running()/stopping() session */ +SCX_TASK_ENQ_WAKEUP =1 <<16 ,/* wakeup enqueue awaiting wakeup_preempt() */ /* * Bits 8 to 10 are used to carry task state:
kernel/sched/ext/ext.c +125 -16
205 lines not on this page · raw message
kernel/sched/ext/internal.h +22 -1
@@ -215,6 +215,19 @@ enum scx_ops_flags {*/ SCX_OPS_TID_TO_TASK =1LLU <<8 , +/* +* If set, mutex-blocked tasks remain runnable as proxy donors and are +* passed to ops.enqueue() with %SCX_ENQ_BLOCKED. The BPF scheduler controls +* when donors are dispatched and whether they should preempt other work. +* +* If clear, mutex-blocked tasks are removed from the runqueue normally +* and cannot donate their scheduling context through proxy execution. +* +* For blocked donors, this flag takes precedence over +* %SCX_OPS_ENQ_EXITING and %SCX_OPS_ENQ_MIGRATION_DISABLED. +*/ +SCX_OPS_ENQ_BLOCKED =1LLU <<9 , +SCX_OPS_ALL_FLAGS =SCX_OPS_KEEP_BUILTIN_IDLE |SCX_OPS_ENQ_LAST | SCX_OPS_ENQ_EXITING |
@@ -223,7 +236,8 @@ enum scx_ops_flags {SCX_OPS_SWITCH_PARTIAL |SCX_OPS_BUILTIN_IDLE_PER_NODE |SCX_OPS_ALWAYS_ENQ_IMMED | -SCX_OPS_TID_TO_TASK , +SCX_OPS_TID_TO_TASK | +SCX_OPS_ENQ_BLOCKED ,/* high 8 bits are internal, don't include in SCX_OPS_ALL_FLAGS */ __SCX_OPS_INTERNAL_MASK = 0xffLLU << 56,
@@ -1746,6 +1760,12 @@ enum scx_enq_flags {*/ SCX_ENQ_LAST =1LLU <<41 , +/* +* The task is blocked on a mutex and is being kept runnable as a proxy +* donor. Only passed to ops.enqueue() when %SCX_OPS_ENQ_BLOCKED is set. +*/ +SCX_ENQ_BLOCKED =1LLU <<42 , +/* high 8 bits are internal */ __SCX_ENQ_INTERNAL_MASK = 0xffLLU << 56,
@@ -2055,6 +2075,7 @@ struct task_struct *scx_task_iter_next_locked(struct scx_task_iter *iter);bool scx_set_task_slice (struct task_struct *p,u64 slice);void scx_task_slice_ended (struct rq *rq,struct task_struct *p);void scx_task_unlink_from_dsq (struct task_struct *p,struct scx_dispatch_q *dsq); +void scx_prepare_task_sched_change (struct task_struct *p);void scx_dispatch_dequeue (struct rq *rq,struct task_struct *p);void scx_do_enqueue_task (struct rq *rq,struct task_struct *p,u64 enq_flags,int sticky_cpu);
kernel/sched/ext/sub.c +6 -3
@@ -1215,8 +1215,9 @@ static void scx_rehome_task(struct scx_sched *to, struct task_struct *p)lockdep_assert_held (&p->pi_lock);lockdep_assert_rq_held (task_rq (p)); +scx_prepare_task_sched_change (p);scoped_guard (sched_change, p, p->sched_class, - DEQUEUE_SAVE | DEQUEUE_MOVE) { + DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) {scx_disable_and_exit_task (scx_task_sched (p), p);scx_set_task_state (p,SCX_TASK_INIT_BEGIN );scx_set_task_state (p,SCX_TASK_INIT );
@@ -1246,8 +1247,9 @@ static void scx_punt_task(struct scx_sched *to, struct task_struct *p)lockdep_assert_rq_held (task_rq (p));WARN_ON_ONCE (!READ_ONCE (to->bypass_depth)); +scx_prepare_task_sched_change (p);scoped_guard (sched_change, p, p->sched_class, - DEQUEUE_SAVE | DEQUEUE_MOVE) { + DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) {scx_disable_and_exit_task (scx_task_sched (p), p);scx_set_task_sched (p, to); }
@@ -1901,8 +1903,9 @@ void scx_sub_enable_workfn(struct kthread_work *work)if (!(p->scx.flags &SCX_TASK_SUB_INIT ))continue ; +scx_prepare_task_sched_change (p);scoped_guard (sched_change, p, p->sched_class, - DEQUEUE_SAVE | DEQUEUE_MOVE) { + DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) {/* * $p must be either READY or ENABLED. If ENABLED, * __scx_disabled_and_exit_task() first disables and
tools/sched_ext/include/scx/compat.h +1 -0
@@ -117,6 +117,7 @@ static inline bool __COMPAT_struct_has_field(const char *type, const char *field#define SCX_OPS_ALLOW_QUEUED_WAKEUP SCX_OPS_FLAG(SCX_OPS_ALLOW_QUEUED_WAKEUP)#define SCX_OPS_BUILTIN_IDLE_PER_NODE SCX_OPS_FLAG(SCX_OPS_BUILTIN_IDLE_PER_NODE)#define SCX_OPS_ALWAYS_ENQ_IMMED SCX_OPS_FLAG(SCX_OPS_ALWAYS_ENQ_IMMED) +#define SCX_OPS_ENQ_BLOCKED SCX_OPS_FLAG(SCX_OPS_ENQ_BLOCKED)#define SCX_PICK_IDLE_FLAG (name) __COMPAT_ENUM_OR_ZERO("scx_pick_idle_cpu_flags", #name)
tools/sched_ext/include/scx/enum_defs.autogen.h +2 -0
@@ -85,6 +85,7 @@#define HAVE_SCX_ENQ_RESCUE #define HAVE_SCX_ENQ_REENQ #define HAVE_SCX_ENQ_LAST +#define HAVE_SCX_ENQ_BLOCKED #define HAVE___SCX_ENQ_INTERNAL_MASK #define HAVE_SCX_ENQ_CLEAR_OPSS #define HAVE_SCX_ENQ_DSQ_PRIQ
@@ -102,6 +103,7 @@#define HAVE_SCX_TASK_IMMED #define HAVE_SCX_TASK_PROTECTED #define HAVE_SCX_TASK_RUN_TRACKED +#define HAVE_SCX_TASK_ENQ_WAKEUP #define HAVE_SCX_TASK_STATE_SHIFT #define HAVE_SCX_TASK_STATE_BITS #define HAVE_SCX_TASK_STATE_MASK
tools/sched_ext/include/scx/enums.autogen.bpf.h +3 -0
@@ -133,6 +133,9 @@ const volatile u64 __SCX_ENQ_REENQ __weak;const volatile u64 __SCX_ENQ_LAST__weak ;#define SCX_ENQ_LAST __SCX_ENQ_LAST +const volatile u64 __SCX_ENQ_BLOCKED__weak ; +#define SCX_ENQ_BLOCKED __SCX_ENQ_BLOCKED +const volatile u64 __SCX_ENQ_CLEAR_OPSS__weak ;#define SCX_ENQ_CLEAR_OPSS __SCX_ENQ_CLEAR_OPSS
tools/sched_ext/include/scx/enums.autogen.h +1 -0
@@ -48,6 +48,7 @@SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_RESCUE ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_REENQ ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_LAST ); \ +SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_BLOCKED ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_CLEAR_OPSS ); \SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_DSQ_PRIQ ); \SCX_ENUM_SET (skel, scx_deq_flags,SCX_DEQ_SCHED_CHANGE ); \
Signature · 1 line
2.55.0
Tejun Heo
reviewerRe: [PATCH 14/17] sched_ext: Delegate proxy donor admission to BPF schedulers
kernel/sched/ext/ext.c DEFINE_RAW_SPINLOCK(scx_sched_lock); · line 38
Quoted 13 lines · Andrea Righi
bool
scx_allow_proxy_exec(const struct task_struct *p){kernel/sched/ext/ext.c +10
-1-return p->sched_class != &ext_sched_class; +struct scx_sched *sch; + +if (p->sched_class != &ext_sched_class) +return true; + +/* +* scx_enabled() may change while __schedule() holds only @p's rq lock. +* Once @p is associated with a scheduler, use that scheduler's policy +* even while the global enable state is transitioning. +*/
I'm not sure this comment is necessary. scx_task_sched() is stable while holding the task's rq lock.
kernel/sched/ext/ext.c void scx_do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags, · line 2090
Quoted 6 lines
kernel/sched/ext/ext.c +2
-0@@ -2059,19 +2086,25 @@ void scx_do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,if (p->scx.ddsp_dsq_id !=SCX_DSQ_INVALID ) goto direct; + enq_blocked = (sch->ops.flags &SCX_OPS_ENQ_BLOCKED ) && + p->is_blocked && !(enq_flags &SCX_ENQ_WAKEUP );
Why not just test directly in the if statement? It's not like the test result is used anywhere else. Is the intention giving the test result an intuitive name? I guess is_blocked && WAKEUP is the condition is the donor gaining execution back? Might be worthwhile to add a comment.
kernel/sched/ext/ext.c void scx_do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags, · line 2096
Quoted 6 lines
kernel/sched/ext/ext.c +6
-0+if (enq_blocked) { + enq_flags |=SCX_ENQ_BLOCKED ; + }else { +/* see %SCX_OPS_ENQ_EXITING */ +if (!(sch->ops.flags &SCX_OPS_ENQ_EXITING ) && +unlikely (p->flags &PF_EXITING )) {
While at it, can you swap the order? This is ordered this way because OPS testing used to be static_key but now that these are regular tests, it makes more sense to test the unlikely one first, or maybe that belongs in a separate patch.
kernel/sched/ext/ext.c static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_ · line 2223
Quoted 10 lines
kernel/sched/ext/ext.c +5
-1@@ -2183,8 +2216,17 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_int sticky_cpu = p->scx.sticky_cpu;u64 enq_flags = core_enq_flags | rq->scx.remote_activate_enq_flags; -if (enq_flags &ENQUEUE_WAKEUP ) +/* +* p->is_blocked is cleared after wakeup_preempt(), so remember whether +* this is a full wakeup activation. If wakeup_preempt_scx() isn't called, +* set_next_task_scx() or a subsequent non-wakeup enqueue clears the flag. +*/
I can't make heads or tails of this comment. This doesn't seem to explain what TASK_ENQ_WAKEUP is used for but just goes into how it's managed.
Quoted 19 lines
kernel/sched/ext/ext.c +10
-1+if (enq_flags &ENQUEUE_WAKEUP ) { rq->scx.flags |=SCX_RQ_IN_WAKEUP ; + p->scx.flags |=SCX_TASK_ENQ_WAKEUP ; + }else { + p->scx.flags &= ~SCX_TASK_ENQ_WAKEUP ; + }/* * Restoring the current scheduling context will be immediately followed @@ -2399,10 +2441,30 @@ static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl/* * Preemption between SCX tasks is implemented by resetting the victim * task's slice to 0 and triggering reschedule on the target CPU. -* Nothing to do. +* +* A mutex waiter can remain on-rq as a proxy donor while logically +* blocked. If it wakes without having been proxy-migrated, +* ttwu_runnable() calls here without another enqueue_task_scx(). Request +* rescheduling so that ops.dispatch() can reconsider the task after
kernel/sched/ext/ext.c static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl · line 2457
Quoted 10 lines
kernel/sched/ext/ext.c +8
-1+* ttwu_runnable() clears is_blocked. +* +* A proxy-migrated donor instead returns through the full activation +* path, which calls enqueue_task_scx() before arriving here. +* SCX_TASK_ENQ_WAKEUP records that the enqueue already happened and an +* additional reschedule isn't needed. */ -if (p->sched_class == &ext_sched_class) +if (p->sched_class == &ext_sched_class) { +bool enq_wakeup = p->scx.flags &SCX_TASK_ENQ_WAKEUP ;
Ditto with bouncing test result.
kernel/sched/ext/ext.c static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl · line 2459
Quoted 2 lines
kernel/sched/ext/ext.c +2
-0+ + p->scx.flags &= ~SCX_TASK_ENQ_WAKEUP ;
I'm not a big fan of SCX_TASK_ENQ_WAKEUP. This is a roundabout way to detect owner -> donor case, right? Doesn't the caller already know? If so, can't it just pass in that as a wake_flag?
kernel/sched/ext/ext.c static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl · line 2465
Quoted 6 lines
kernel/sched/ext/ext.c +6
-0+if (!enq_wakeup && p->is_blocked) { +struct scx_sched *sch =scx_task_sched (p); + +if (sch && (sch->ops.flags &SCX_OPS_ENQ_BLOCKED )) +resched_curr (rq); + }
It'd nice if we can gate the above behind proxy enabled.
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2583
Quoted 6 lines
kernel/sched/ext/ext.c +2
-0@@ -2517,6 +2579,20 @@ static bool task_can_run_on_remote_rq(struct scx_sched *sch,WARN_ON_ONCE (task_cpu (p) == cpu); +/* +* A blocked donor may be moved normally to select a new callback rq.
What's "callback" rq?
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2593
Quoted 10 lines
kernel/sched/ext/ext.c +10
-0+* set_task_cpu() updates wake_cpu and makes the destination rq its new +* callback home. +* +* proxy_set_task_cpu() instead preserves wake_cpu when moving a donor to +* its lock owner's CPU. Keep such a donor on the proxy rq until it wakes; +* otherwise normal BPF placement may repeatedly pull it back to its +* callback rq only for proxy execution to move it to the owner again. +*/ +if (sched_proxy_exec () && p->is_blocked && +task_cpu (p) != p->wake_cpu)
No need for line break. Can you elaborate the scenario this scenario is needed for? Is this the exact condition? Let's say a donor is running the owner on the same CPU, so task_cpu(p) == p->wake_cpu. Wouldn't you still want to block scx from moving it to another CPU? What am I missing?
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerRe: [PATCH 14/17] sched_ext: Delegate proxy donor admission to BPF schedulers
Hi Tejun,
Quoted 17 lines · Tejun Heo
kernel/sched/ext/ext.c DEFINE_RAW_SPINLOCK(scx_sched_lock); · line 38
Quoted 13 lines · Andrea Righi
bool
scx_allow_proxy_exec(const struct task_struct *p){kernel/sched/ext/ext.c +10
-1-return p->sched_class != &ext_sched_class; +struct scx_sched *sch; + +if (p->sched_class != &ext_sched_class) +return true; + +/* +* scx_enabled() may change while __schedule() holds only @p's rq lock. +* Once @p is associated with a scheduler, use that scheduler's policy +* even while the global enable state is transitioning. +*/ I'm not sure this comment is necessary.
scx_task_sched()is stable while holding the task's rq lock.
That's right, the comment is unnecessary, I'll remove it.
Quoted 5 lines
Quoted 5 lines
kernel/sched/ext/ext.c +0
-0@@ -2059,19 +2086,25 @@ void scx_do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,if (p->scx.ddsp_dsq_id !=SCX_DSQ_INVALID ) goto direct;
Quoted 7 lines
kernel/sched/ext/ext.c void scx_do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags, · line 2090
Quoted 2 lines
kernel/sched/ext/ext.c +2
-0+ enq_blocked = (sch->ops.flags &SCX_OPS_ENQ_BLOCKED ) && + p->is_blocked && !(enq_flags &SCX_ENQ_WAKEUP );Why not just test directly in the if statement? It's not like the test result is used anywhere else. Is the intention giving the test result an intuitive name? I guess
is_blocked&& WAKEUP is the condition is the donor gaining execution back? Might be worthwhile to add a comment.
Right, the local var was only intended to give the condition a name. I'll inline it and add a comment explaining the WAKEUP exclusion.
Quoted 12 lines
kernel/sched/ext/ext.c void scx_do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags, · line 2096
Quoted 7 lines
kernel/sched/ext/ext.c +6
-0+if (enq_blocked) { + enq_flags |=SCX_ENQ_BLOCKED ; + }else { +/* see %SCX_OPS_ENQ_EXITING */ +if (!(sch->ops.flags &SCX_OPS_ENQ_EXITING ) && +unlikely (p->flags &PF_EXITING )) {While at it, can you swap the order? This is ordered this way because OPS testing used to be
static_keybut now that these are regular tests, it makes more sense to test the unlikely one first, or maybe that belongs in a separate patch.
Ack. I'll test PF_EXITING first. Since this patch already moves that condition to handle blocked-donor admission ahead of it, maybe we can fold this into the same patch.
Quoted 5 lines
Quoted 5 lines
kernel/sched/ext/ext.c +0
-0@@ -2183,8 +2216,17 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_int sticky_cpu = p->scx.sticky_cpu;u64 enq_flags = core_enq_flags | rq->scx.remote_activate_enq_flags;
Quoted 9 lines
kernel/sched/ext/ext.c static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int core_enq_ · line 2223
Quoted 6 lines
kernel/sched/ext/ext.c +5
-1-if (enq_flags &ENQUEUE_WAKEUP ) +/* +* p->is_blocked is cleared after wakeup_preempt(), so remember whether +* this is a full wakeup activation. If wakeup_preempt_scx() isn't called, +* set_next_task_scx() or a subsequent non-wakeup enqueue clears the flag. +*/ I can't make heads or tails of this comment. This doesn't seem to explain what
TASK_ENQ_WAKEUPis used for but just goes into how it's managed.
The distinction it was trying to carry is between a retained on-rq donor wakeup, which reaches wakeup_preempt_scx() through ttwu_runnable() without another ops.enqueue() and a full wakeup activation, which has already enqueued the task.
We can remove SCX_TASK_ENQ_WAKEUP, pass WF_ON_RQ directly from ttwu_runnable() and replace the old lifecycle comment with one next to the check explaining that WF_ON_RQ identifies the no-enqueue path, which needs resched_curr(), so that BPF can reconsider the task after is_blocked is cleared.
Quoted 33 lines
Quoted 20 lines
kernel/sched/ext/ext.c +10
-1+if (enq_flags &ENQUEUE_WAKEUP ) { rq->scx.flags |=SCX_RQ_IN_WAKEUP ; + p->scx.flags |=SCX_TASK_ENQ_WAKEUP ; + }else { + p->scx.flags &= ~SCX_TASK_ENQ_WAKEUP ; + }/* * Restoring the current scheduling context will be immediately followed @@ -2399,10 +2441,30 @@ static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl/* * Preemption between SCX tasks is implemented by resetting the victim * task's slice to 0 and triggering reschedule on the target CPU. -* Nothing to do. +* +* A mutex waiter can remain on-rq as a proxy donor while logically +* blocked. If it wakes without having been proxy-migrated, +* ttwu_runnable() calls here without another enqueue_task_scx(). Request +* rescheduling so that ops.dispatch() can reconsider the task after kernel/sched/ext/ext.c static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl · line 2457
Quoted 10 lines
kernel/sched/ext/ext.c +8
-1+* ttwu_runnable() clears is_blocked. +* +* A proxy-migrated donor instead returns through the full activation +* path, which calls enqueue_task_scx() before arriving here. +* SCX_TASK_ENQ_WAKEUP records that the enqueue already happened and an +* additional reschedule isn't needed. */ -if (p->sched_class == &ext_sched_class) +if (p->sched_class == &ext_sched_class) { +bool enq_wakeup = p->scx.flags &SCX_TASK_ENQ_WAKEUP ;Ditto with bouncing test result.
Quoted 6 lines
kernel/sched/ext/ext.c static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl · line 2459
Quoted 2 lines
kernel/sched/ext/ext.c +2
-0+ + p->scx.flags &= ~SCX_TASK_ENQ_WAKEUP ;I'm not a big fan of
SCX_TASK_ENQ_WAKEUP. This is a roundabout way to detect owner -> donor case, right? Doesn't the caller already know? If so, can't it just pass in that as awake_flag?
Ok, as mentioned earlier we can pass WF_ON_RQ to wakeup_preempt_scx() to distinguish the two cases.
Quoted 9 lines
kernel/sched/ext/ext.c static void wakeup_preempt_scx(struct rq *rq, struct task_struct *p, int wake_fl · line 2465
Quoted 7 lines
kernel/sched/ext/ext.c +6
-0+if (!enq_wakeup && p->is_blocked) { +struct scx_sched *sch =scx_task_sched (p); + +if (sch && (sch->ops.flags &SCX_OPS_ENQ_BLOCKED )) +resched_curr (rq); + }It'd nice if we can gate the above behind proxy enabled.
Ack.
Quoted 9 lines
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2583
Quoted 7 lines
kernel/sched/ext/ext.c +2
-0@@ -2517,6 +2579,20 @@ static bool task_can_run_on_remote_rq(struct scx_sched *sch,WARN_ON_ONCE (task_cpu (p) == cpu); +/* +* A blocked donor may be moved normally to select a new callback rq. What's "callback" rq?
Poor terminology... I meant the rq associated with the donor before proxy execution moved its scheduling context. I'll rephrase this.
Quoted 16 lines
kernel/sched/ext/ext.c static bool task_can_run_on_remote_rq(struct scx_sched *sch, · line 2593
Quoted 11 lines
kernel/sched/ext/ext.c +10
-0+* set_task_cpu() updates wake_cpu and makes the destination rq its new +* callback home. +* +* proxy_set_task_cpu() instead preserves wake_cpu when moving a donor to +* its lock owner's CPU. Keep such a donor on the proxy rq until it wakes; +* otherwise normal BPF placement may repeatedly pull it back to its +* callback rq only for proxy execution to move it to the owner again. +*/ +if (sched_proxy_exec () && p->is_blocked && +task_cpu (p) != p->wake_cpu)No need for line break. Can you elaborate the scenario this scenario is needed for? Is this the exact condition? Let's say a donor is running the owner on the same CPU, so
task_cpu(p)==p->wake_cpu. Wouldn't you still want to block scx from moving it to another CPU? What am I missing?
The condition is intended to identify a completed proxy migration, not all blocked donors.
For example, suppose BPF places donor D on CPU0 while its mutex owner O runs on CPU1, proxy-exec moves D to CPU1 (its scheduling context) using proxy_set_task_cpu(), which preserves D->wake_cpu == CPU0.
Then we have:
- task_cpu(D) = CPU1
- wake_cpu(D) = CPU0
If D is subsequently put on a shared DSQ, then it could be consumed by CPU0 and move it back, only for proxy execution to move it to CPU1 again. The check prevents that ping-pong while D remains blocked.
In the same-CPU case, while D is actively proxy-running O on CPU0, D is rq->donor and has already been removed from its DSQ, so another CPU cannot consume and move it. A remote-transfer race is also rejected by task_proxy_move_active (aka task_proxy_running_or_donating() after the rename).
If D is later preempted and re-enqueued while still blocked, it is no longer active and BPF may place it elsewhere. Suppose BPF moves it to CPU1. The normal migration updates both task_cpu(D) and wake_cpu(D) to CPU1. When CPU1 selects D, proxy execution finds O on CPU0 and moves D's scheduling context back to CPU0 while preserving wake_cpu(D) == CPU1. The resulting mismatch then prevents further BPF-directed moves while D remains blocked.
So there are 3 cases:
- task_proxy_move_active() prevents moving a donor that is currently running or donating,
- task_cpu(D) != D->wake_cpu prevents moving an inactive donor whose scheduling context has already been moved to its owner's CPU by proxy exec,
- an inactive donor that has not yet been proxy-migrated remains under BPF placement control.
Thanks,
-Andrea
Tejun Heo
reviewerRe: [PATCH 14/17] sched_ext: Delegate proxy donor admission to BPF schedulers
Hello,
On Mon, Aug 17, 2026 at 09:02:26PM +0200, Andrea Righi wrote:
...
Quoted 7 lines
So there are 3 cases:
-task_proxy_move_active()prevents moving a donor that is currently running or donating,
-task_cpu(D)!=D->wake_cpuprevents moving an inactive donor whose scheduling context has already been moved to its owner's CPU by proxy exec,
- an inactive donor that has not yet been proxy-migrated remains under BPF placement control.
I think this mostly stems from my not understanding the full picture, so while proxy execution is in progress - the donor's context is running the owner - the execution cannot leave the owner's last CPU? That *seems* to be the behavior assumed by the core code. It's a bit odd that the owner becomes essentially CPU-pinned.
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerPATCH 15/17 sched_ext: Add selftest for blocked donor admission
In reply to Andrea RighiSCX_OPS_ENQ_BLOCKED allows BPF schedulers to receive blocked proxy donors through ops.enqueue(). SCX_ENQ_BLOCKED identifies blocked-donor admission requests. Add selftest coverage for this interface.
Exercise a priority inversion using a weighted-vruntime BPF scheduler. A nice +19 owner holds a shared mutex, a nice -20 donor blocks on it, and nice 0 CPU contenders, one per allowed CPU, keep the system busy. Test both a same-CPU topology and a cross-CPU topology with the donor and owner on different CPUs.
Treat blocked donors according to the normal BPF ordering policy and assign the default slice on every enqueue, as for other tasks. Run each CPU placement configuration with SCX_OPS_ENQ_BLOCKED first disabled and then enabled, count blocked-donor enqueues by CPU and report average mutex hold and wait times. Verify that full wakeups are never reported as blocked-donor admissions by checking that SCX_ENQ_WAKEUP and SCX_ENQ_BLOCKED are not set together.
Proxy execution coverage requires CONFIG_SCHED_PROXY_EXEC=y, which the selftest config selects. Access to the kernel mutex is provided via a loadable kernel module, built through TEST_GEN_MODS_DIR and managed by the test.
--- tools/testing/selftests/sched_ext/.gitignore | 4 + tools/testing/selftests/sched_ext/Makefile | 2 + tools/testing/selftests/sched_ext/config | 2 + .../selftests/sched_ext/enq_blocked.bpf.c | 116 +++ .../testing/selftests/sched_ext/enq_blocked.c | 917 ++++++++++++++++++ .../testing/selftests/sched_ext/enq_blocked.h | 28 + .../selftests/sched_ext/test_modules/Makefile | 13 + .../test_modules/scx_enq_blocked_test.c | 195 ++++ 8 files changed, 1277 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/enq_blocked.bpf.c create mode 100644 tools/testing/selftests/sched_ext/enq_blocked.c create mode 100644 tools/testing/selftests/sched_ext/enq_blocked.h create mode 100644 tools/testing/selftests/sched_ext/test_modules/Makefile create mode 100644 tools/testing/selftests/sched_ext/test_modules/scx_enq_blocked_test.c
tools/testing/selftests/sched_ext/.gitignore +4 -0
@@ -4,3 +4,7 @@ !Makefile !.gitignore !config +!test_modules/ +!test_modules/scx_enq_blocked_test.c +!test_modules/Makefile +test_modules/*.mod.c
tools/testing/selftests/sched_ext/Makefile +2 -0
@@ -5,6 +5,7 @@ include ../../../scripts/Makefile.arch include ../../../scripts/Makefile.include TEST_GEN_PROGS := runner +TEST_GEN_MODS_DIR := test_modules # override lib.mk's default rules OVERRIDE_TARGETS := 1
@@ -164,6 +165,7 @@ all_test_bpfprogs := $(foreach prog,$(wildcard *.bpf.c),$(INCLUDE_DIR)/$(patsubs auto-test-targets := \ create_dsq \ dequeue \ + enq_blocked \ enq_last_no_enq_fails \ ddsp_bogus_dsq_fail \ ddsp_vtimelocal_fail \
tools/testing/selftests/sched_ext/config +2 -0
@@ -6,3 +6,5 @@ CONFIG_BPF=y CONFIG_BPF_SYSCALL=y CONFIG_DEBUG_INFO=y CONFIG_DEBUG_INFO_BTF=y +CONFIG_EXPERT=y +CONFIG_SCHED_PROXY_EXEC=y
tools/testing/selftests/sched_ext/enq_blocked.bpf.c new file +116 -0
116 lines not on this page · raw message
tools/testing/selftests/sched_ext/enq_blocked.c new file +917 -0
917 lines not on this page · raw message
tools/testing/selftests/sched_ext/enq_blocked.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES */ +#ifndef __ENQ_BLOCKED_H +#define __ENQ_BLOCKED_H + +#include <linux/ioctl.h> +#include <linux/types.h> + +struct enq_blocked_stats { +__u64 hold_time_ns; +__u64 wait_time_ns; +__u64 nr_holds; +__u64 nr_waits; +}; + +#define ENQ_BLOCKED_IOCTL_OWNER _IO('s', 1) +#define ENQ_BLOCKED_IOCTL_DONOR _IO('s', 2) +#define ENQ_BLOCKED_IOCTL_RESET_STATS _IO('s', 3) +#define ENQ_BLOCKED_IOCTL_GET_STATS _IOR('s', 4, struct enq_blocked_stats) +#define ENQ_BLOCKED_IOCTL_PREP_ATTACH _IO('s', 5) +#define ENQ_BLOCKED_IOCTL_ATTACH_DONE _IO('s', 6) +#define ENQ_BLOCKED_IOCTL_DONOR_STATE _IO('s', 7) +#define ENQ_BLOCKED_IOCTL_PROXY_SUPPORTED _IO('s', 8) + +#define ENQ_BLOCKED_DONOR_SLEEPING (1U << 0) +#define ENQ_BLOCKED_DONOR_ON_RQ (1U << 1) + +#endif /* __ENQ_BLOCKED_H */
tools/testing/selftests/sched_ext/test_modules/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES + +TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= $(if $(O),$(O),$(realpath ../../../../..)) + +obj-m += scx_enq_blocked_test.o + +all: + +$(Q)$(MAKE) -C $(KDIR) M=$(TESTMODS_DIR) modules + +clean: + +$(Q)$(MAKE) -C $(KDIR) M=$(TESTMODS_DIR) clean
tools/testing/selftests/sched_ext/test_modules/scx_enq_blocked_test.c new file +195 -0
195 lines not on this page · raw message
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 16/17 sched_ext: scx_qmap: Add proxy execution support
In reply to Andrea RighiAdd a -X option to opt scx_qmap into queueing mutex-blocked tasks for proxy execution. Without the option, SCX_OPS_ENQ_BLOCKED remains clear and mutex waiters block normally. With -X, blocked donors are passed to qmap_enqueue() with SCX_ENQ_BLOCKED.
When scx_qmap receives a blocked donor, dispatch it directly to the local DSQ of its current cid with a fresh slice and SCX_ENQ_PREEMPT. This places the donor at the head of the DSQ and requests an immediate reschedule, allowing the core proxy-exec path to run the mutex owner using the donor's scheduling context as soon as the donor is selected.
The blocked policy is intentionally unfair and can strongly prioritize tasks using contended mutexes, but scx_qmap is a demo scheduler and such aggressive behavior makes proxy-exec support easy to observe. Count these dispatch attempts in nr_enq_blocked and report their per-interval delta.
--- tools/sched_ext/scx_qmap.bpf.c | 40 +++++++++++++++++++++++++++++++++- tools/sched_ext/scx_qmap.c | 13 ++++++++--- tools/sched_ext/scx_qmap.h | 1 + 3 files changed, 50 insertions(+), 4 deletions(-)
tools/sched_ext/scx_qmap.bpf.c +39 -1
@@ -454,7 +454,8 @@ void BPF_STRUCT_OPS(qmap_enqueue, struct task_struct *p, u64 enq_flags)* * Force it onto its first allowed cid's local DSQ. If we hold that cid * it runs. Otherwise the insert carries SCX_ENQ_RESCUE and the kernel -* diverts the task to its rescue path. +* diverts the task to its rescue path. Do this before the blocked-donor +* fast paths, which also require an eligible self cid to make progress. */ if (!cmask_intersects (&taskc->cpus_allowed, &qa.self_cids.mask)) {s32 c =cmask_next_set_wrap (&taskc->cpus_allowed,0 );
@@ -468,6 +469,43 @@ void BPF_STRUCT_OPS(qmap_enqueue, struct task_struct *p, u64 enq_flags) } } +/* +* SCX_OPS_ALWAYS_ENQ_IMMED makes the local insertion below implicitly +* carry SCX_ENQ_IMMED. If the CPU can't run the blocked donor immediately, +* the core returns it through ops.enqueue() with SCX_ENQ_REENQ. Inserting +* it into the same local DSQ would repeat the IMMED handback until the +* scheduler is ejected. Move reenqueued blocked donors to the shared DSQ, +* which doesn't carry SCX_ENQ_IMMED, so another CPU can consume them. +*/ +if ((enq_flags & (SCX_ENQ_BLOCKED |SCX_ENQ_REENQ )) == + (SCX_ENQ_BLOCKED |SCX_ENQ_REENQ )) { + taskc->force_local = false; +scx_bpf_dsq_insert (p,SHARED_DSQ ,0 , enq_flags); + cid =cmask_next_and2_set_wrap (&taskc->cpus_allowed, + &qa.idle_cids.mask, + &qa.self_cids.mask,0 ); +if (cid <scx_bpf_nr_cids ()) +scx_bpf_kick_cid (cid,SCX_KICK_IDLE ); +return ; + } + +/* +* Insert a blocked mutex donor at the head of its current cid's local +* DSQ with a fresh slice and %SCX_ENQ_PREEMPT, requesting an immediate +* reschedule. Once selected, the core proxy-exec path can immediately +* run the mutex owner using the donor's scheduling context. +* +* This policy is intentionally unfair and can strongly prioritize tasks +* using contended mutexes; scx_qmap is a demonstration scheduler and +* this behavior makes proxy-exec support easy to observe. +*/ +if (enq_flags &SCX_ENQ_BLOCKED ) { +__sync_fetch_and_add (&qa.nr_enq_blocked,1 ); +scx_bpf_dsq_insert (p,SCX_DSQ_LOCAL_ON |scx_bpf_task_cid (p), + slice_ns, enq_flags |SCX_ENQ_PREEMPT ); +return ; + } +/* * Fault injection: deliberately dispatch one of our own tasks to a cid * we don't hold. The inserts carry SCX_ENQ_RESCUE and divert to the
tools/sched_ext/scx_qmap.c +10 -3
@@ -46,7 +46,7 @@ const char help_fmt[] ="See the top-of-file comment in .bpf.c for the design.\n" "\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]\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" "\n" " -s SLICE_US Override slice duration\n"
@@ -65,6 +65,7 @@ const char help_fmt[] =" -S Suppress qmap-specific debug dump\n" " -p Switch only tasks on SCHED_EXT policy instead of all\n" " -I Turn on SCX_OPS_ALWAYS_ENQ_IMMED\n" +" -X Turn on SCX_OPS_ENQ_BLOCKED\n" " -F COUNT IMMED stress: force every COUNT'th enqueue to a busy local DSQ (use with -I)\n" " -C MODE cid-override test (shuffle|bad-dup|bad-range|bad-mono)\n" " -i SEC Stats interval, seconds (default 5)\n"
@@ -107,6 +108,7 @@ struct hier_prev {u64 nr_dsps[MAX_SUB_SCHEDS ];u64 nr_reenq_cap;u64 nr_reenq_immed; +u64 nr_enq_blocked;u64 nr_inject_attempts;u64 nr_rescue_dsp; };
@@ -190,14 +192,16 @@ static void print_hier(struct qmap_arena *qa, struct hier_prev *prev, u64 own_cg }format_cid_ranges (qa,CID_SHARED , ranges,sizeof (ranges)); -printf ("hier : nsub=%llu excl=%u shared=%s rr=%s reenq cap/immed +%llu/+%llu inj=+%llu rescue=+%llu\n" , +printf ("hier : nsub=%llu excl=%u shared=%s rr=%s reenq cap/immed +%llu/+%llu blocked=+%llu inj=+%llu rescue=+%llu\n" , (unsigned long long )qa->nr_sub_scheds, qa->part.nr_excl, ranges, rr, (unsigned long long )(qa->nr_reenq_cap - prev->nr_reenq_cap), (unsigned long long )(qa->nr_reenq_immed - prev->nr_reenq_immed), + (unsigned long long )(qa->nr_enq_blocked - prev->nr_enq_blocked), (unsigned long long )(qa->nr_inject_attempts - prev->nr_inject_attempts), (unsigned long long )(qa->nr_rescue_dsp - prev->nr_rescue_dsp)); prev->nr_reenq_cap = qa->nr_reenq_cap; prev->nr_reenq_immed = qa->nr_reenq_immed; + prev->nr_enq_blocked = qa->nr_enq_blocked; prev->nr_inject_attempts = qa->nr_inject_attempts; prev->nr_rescue_dsp = qa->nr_rescue_dsp;
@@ -262,7 +266,7 @@ int main(int argc, char **argv) skel->rodata->max_tasks =16384 ;while ((opt =getopt (argc, argv, -"s:e:t:T:l:b:N:PMHc:d:D:SpIF:C:i:R:J:B:q:vh" )) !=-1 ) { +"s:e:t:T:l:b:N:PMHc:d:D:SpIXF:C:i:R:J:B:q:vh" )) !=-1 ) {switch (opt) {case 's' : skel->rodata->slice_ns =strtoull (optarg,NULL ,0 ) *1000 ;
@@ -323,6 +327,9 @@ int main(int argc, char **argv)case 'I' : skel->struct_ops.qmap_ops->flags |=SCX_OPS_ALWAYS_ENQ_IMMED ;break ; +case 'X' : + skel->struct_ops.qmap_ops->flags |=SCX_OPS_ENQ_BLOCKED ; +break ;case 'F' : skel->rodata->immed_stress_nth =strtoul (optarg,NULL ,0 );break ;
tools/sched_ext/scx_qmap.h +1 -0
@@ -174,6 +174,7 @@ struct qmap_arena {/* bpf -> userspace: stats */ u64 nr_reenq_cap;/* SCX_TASK_REENQ_CAP bounces */ u64 nr_reenq_immed;/* SCX_TASK_REENQ_IMMED bounces */ +u64 nr_enq_blocked;/* SCX_ENQ_BLOCKED dispatches */ u64 nr_inject_attempts;/* fault-injection: dispatches to an unheld cid */ u64 nr_rescue_dsp;/* SCX_ENQ_RESCUE dispatch attempts */ u32 inject_mode;/* fault-injection mode (QMAP_INJ_*) */
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 17/17 sched: Allow enabling proxy exec with sched_ext
In reply to Andrea RighiNow that sched_ext can handle proxy donors and BPF schedulers can opt in to blocked-donor enqueueing with SCX_OPS_ENQ_BLOCKED, remove the !SCHED_CLASS_EXT dependency from SCHED_PROXY_EXEC and allow both options to be enabled together.
--- init/Kconfig | 2 -- 1 file changed, 2 deletions(-)
init/Kconfig +0 -2
@@ -934,8 +934,6 @@ config SCHED_PROXY_EXEC bool "Proxy Execution" # Avoid some build failures w/ PREEMPT_RT until it can be fixed depends on !PREEMPT_RT - # Need to investigate how to inform sched_ext of split contexts - depends on !SCHED_CLASS_EXT # Not particularly useful until we get to multi-rq proxying depends on EXPERT help
Signature · 1 line
2.55.0