sched: Make proxy execution compatible with sched_ext
Subsystems (3)
Andrea Righi
reviewerPATCH v4 1/1 sched: Make proxy execution compatible with sched_ext
This series enables using 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.
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.
The sched_ext callback bookkeeping is adjusted accordingly. Blocked proxy donors do not generate spurious ops.running() callbacks and ops.stopping() is only called after a real running transition. The core also drops mutex locks before proxy rescheduling invokes scheduling-class callbacks, so callbacks can safely inspect the proxy chain.
Scheduler ownership changes need special handling because a donor may already be blocked when a root scheduler is enabled or when a task moves between root and sub-schedulers. If the incoming scheduler does not set SCX_OPS_ENQ_BLOCKED, the retained donor is removed from the runqueue and stays on the normal mutex wait path. Otherwise it remains eligible for BPF admission.
The mutex owner's CPU can be queried from BPF using scx_bpf_task_proxy_cpu(p) or scx_bpf_task_proxy_cid(p), with p being the mutex-blocked task. It's up to the BPF scheduler to decide how to use this information. For example, it may inspect the task currently running at that location, steer the donor there, or request preemption to expedite the proxy-exec handoff. Schedulers that do not need this information can keep the donor on its current CPU/cid and let the core perform the handoff.
scx_qmap has been modified with a -B option to enable queueing mutex-blocked tasks for proxy-exec.
A new kselftest (enq_blocked) is also introduced to validate proxy-exec with sched_ext. The test creates a three-task priority inversion on one CPU: a low-priority owner (nice +19) holds a kernel mutex, a high-priority donor (nice -20) blocks on it and a nice 0 contender competes for the CPU. The test runs with SCX_OPS_ENQ_BLOCKED first disabled and then enabled. In the enabled run it checks that blocked donors reach ops.enqueue() with SCX_ENQ_BLOCKED and that scx_bpf_task_proxy_cpu() reports the shared CPU. 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:
$ sudo tools/testing/selftests/sched_ext/runner -t enq_blocked ===== START ===== TEST: enq_blocked DESCRIPTION: Verify BPF-driven proxy donor admission OUTPUT:
[SCX_OPS_ENQ_BLOCKED=disabled]
proxy_exec=enabled
owner_nice=19
donor_nice=-20
contender_nice=0
mutex_hold_avg_ns=254084719 (254.084 ms, samples=10)
mutex_wait_avg_ns=254095120 (254.095 ms, samples=10)
nr_blocked_enqueues=0[SCX_OPS_ENQ_BLOCKED=enabled]
proxy_exec=enabled
owner_nice=19
donor_nice=-20
contender_nice=0
mutex_hold_avg_ns=228884734 (228.884 ms, samples=10)
mutex_wait_avg_ns=207903720 (207.903 ms, samples=10)
nr_blocked_enqueues=51[delta: enabled - disabled]
mutex_hold_delta_ns=-25199985 (-9.92%)
mutex_wait_delta_ns=-46191400 (-18.18%) 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 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 (9):
sched/core: Drop mutex locks before proxy rescheduling
sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
sched_ext: Split curr|donor references properly
sched_ext: Fix TOCTOU race in consume_remote_task()
sched_ext: Delegate proxy donor admission to BPF schedulers
sched_ext: Add proxy destination query kfuncs
sched_ext: Add selftest for blocked donor admission
sched_ext: scx_qmap: Add proxy execution support
sched: Allow enabling proxy exec with sched_ext
John Stultz (1):
sched_ext: Handle blocked donor migration with proxy execution
include/linux/sched/ext.h | 2 + init/Kconfig | 2 - kernel/sched/core.c | 81 ++- kernel/sched/ext/ext.c | 279 +++++++- kernel/sched/ext/ext.h | 6 + kernel/sched/ext/internal.h | 25 +- kernel/sched/ext/sub.c | 12 +- kernel/sched/sched.h | 8 + kernel/sched/syscalls.c | 3 + tools/sched_ext/include/scx/common.bpf.h | 2 + tools/sched_ext/include/scx/compat.bpf.h | 18 + tools/sched_ext/include/scx/compat.h | 1 + tools/sched_ext/include/scx/enum_defs.autogen.h | 1 + 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 | 13 + tools/sched_ext/scx_qmap.c | 9 +- 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 | 109 +++ tools/testing/selftests/sched_ext/enq_blocked.c | 733 +++++++++++++++++++++ tools/testing/selftests/sched_ext/enq_blocked.h | 27 + .../selftests/sched_ext/test_modules/Makefile | 13 + .../sched_ext/test_modules/scx_enq_blocked_test.c | 193 ++++++ 25 files changed, 1501 insertions(+), 48 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/10 sched/core: Drop mutex locks before proxy rescheduling
find_proxy_task() can call proxy_resched_idle() while holding a mutex wait_lock and the blocked task lock. Guard cleanup does not run until after the return expression is evaluated.
proxy_resched_idle() invokes scheduling-class callbacks through put_prev_set_next_task(). Calling those callbacks with the mutex locks held prevents them from safely inspecting the proxy chain, as doing so may require acquiring the same locks.
This is a preparatory change to support proxy execution in sched_ext. It leaves the guard scope before calling proxy_resched_idle() so sched_ext callbacks can safely inspect the proxy chain.
--- kernel/sched/core.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/sched/core.c +6 -4
@@ -6923,7 +6923,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; }
@@ -6935,7 +6935,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; }
@@ -6948,7 +6948,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; }/*
@@ -6985,7 +6985,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
@@ -6997,6 +6997,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
Re: [PATCH 01/10] sched/core: Drop mutex locks before proxy rescheduling
Quoted 5 lines · Andrea Righi
find_proxy_task()can callproxy_resched_idle()while holding a mutexwait_lockand the blocked task lock. Guard cleanup does not run until after the return expression is evaluated.
Quoted 8 lines
proxy_resched_idle()invokes scheduling-class callbacks throughput_prev_set_next_task(). Calling those callbacks with the mutex locks held prevents them from safely inspecting the proxy chain, as doing so may require acquiring the same locks.This is a preparatory change to support proxy execution in
sched_ext. It leaves the guard scope before callingproxy_resched_idle()sosched_extcallbacks can safely inspect the proxy chain.
Thanks again for sending these out!
So, I wanted to understand more the importance of the sched_ext logic being able to inspect the proxy chain?
I sort of roughly understand the argument that some sched_ext schedulers may not want to keep blocked tasks on the rq (effectively what the "Delegate proxy donor admission to BPF schedulers" patch does), but that doesn't require deep inspection.
This change seems to be needed for "Add proxy destination query kfuncs", where the commit message sounds reasonable, but as I think more on it, it doesn't look correct (I'll comment more on the patch), and I'm still confused a bit as to the purpose.
So can you describe the premise and a bit more detail as to why this is important?
thanks
-john
Andrea Righi
reviewerRe: [PATCH 01/10] sched/core: Drop mutex locks before proxy rescheduling
Hi John,
Quoted 25 lines · John Stultz
Quoted 5 lines · Andrea Righi
find_proxy_task()can callproxy_resched_idle()while holding a mutexwait_lockand the blocked task lock. Guard cleanup does not run until after the return expression is evaluated.Quoted 8 lines
proxy_resched_idle()invokes scheduling-class callbacks throughput_prev_set_next_task(). Calling those callbacks with the mutex locks held prevents them from safely inspecting the proxy chain, as doing so may require acquiring the same locks.This is a preparatory change to support proxy execution in
sched_ext. It leaves the guard scope before callingproxy_resched_idle()sosched_extcallbacks can safely inspect the proxy chain.Thanks again for sending these out!
So, I wanted to understand more the importance of the
sched_extlogic being able to inspect the proxy chain?I sort of roughly understand the argument that some
sched_extschedulers may not want to keep blocked tasks on the rq (effectively what the "Delegate proxy donor admission to BPF schedulers" patch does), but that doesn't require deep inspection.
Quoted 7 lines
This change seems to be needed for "Add proxy destination query kfuncs", where the commit message sounds reasonable, but as I think more on it, it doesn't look correct (I'll comment more on the patch), and I'm still confused a bit as to the purpose.
So can you describe the premise and a bit more detail as to why this is important?
Inspecting the proxy chain is not strictly required. A BPF scheduler can just accept a blocked donor, enqueue it on its current CPU and let the core proxy-exec path resolve and "virtually" migrate it as needed to the owner's CPU.
The motivation for providing a scx_bpf_task_proxy_cpu() kfunc is purely for optimization reasons. By knowing the owner's CPU, a scheduler could make more informed admission decisions. For example, it can inspect the task currently running on the target CPU, determine whether the donor should take precedence over the running task and kick that CPU to trigger a preemption. Without this information, the scheduler can only enqueue the donor and wait until the task running on the owner's CPU releases it (potentially adding up to one time slice of latency).
That said, I don't see a correctness dependency on exposing this information. If the latency-policy use case is not strong enough to justify the API, I'm happy to drop the query kfuncs and this preparatory lock-scope change for now, the blocked-donor admission support can stand on its own.
Thanks,
-Andrea
Re: [PATCH 01/10] sched/core: Drop mutex locks before proxy rescheduling
Quoted 4 lines · Andrea Righi
Inspecting the proxy chain is not strictly required. A BPF scheduler can just accept a blocked donor, enqueue it on its current CPU and let the core proxy-exec path resolve and "virtually" migrate it as needed to the owner's CPU.
Quoted 8 lines
The motivation for providing a
scx_bpf_task_proxy_cpu()kfunc is purely for optimization reasons. By knowing the owner's CPU, a scheduler could make more informed admission decisions. For example, it can inspect the task currently running on the target CPU, determine whether the donor should take precedence over the running task and kick that CPU to trigger a preemption. Without this information, the scheduler can only enqueue the donor and wait until the task running on the owner's CPU releases it (potentially adding up to one time slice of latency).
So... I suspect I'm missing a subtlety of scx, but in the non-scx case, if a blocked task is important enough to be selected on the current cpu, the find_proxy_task() logic will walk the chain and do the proxy-migration if needed and mark resched, so then that cpu can evaluate which of the currently running task or the migrated donor is important enough to run.
I worry the difficulty (with my poor understanding of the optimization) is that it seems like its evaluting the preemption on admission (I assume this means the point when we keep the blocked_on task on the rq? I may be totally off base here), is that if a very important task was breifly running at that moment, you might not allow the blocked_on task to be enqueued. And at that point the donor is effectively sleeping and no proxying can happen until it wakes (which may be only when the lock owner eventually runs and releases the lock). In this way the optimization might make a call in that instant that results in *many* time slices of latency (particularly if there are lots other unimportant tasks on that cpu).
By letting the proxy logic handle the proxy migration and rescheduling, the blocked_on tasks are just in the same pool of selectable tasks on that cpu and the scheduler on that cpu gets to decide what is the most important thing to run next.
Now, I can see the benefit of potentially saving the resched kick on the target cpu when we do a proxy-migration - it is an interesting idea I should think more on to see how we might do that better in the core logic.
Quoted 4 lines
That said, I don't see a correctness dependency on exposing this information. If the latency-policy use case is not strong enough to justify the API, I'm happy to drop the query kfuncs and this preparatory lock-scope change for now, the blocked-donor admission support can stand on its own.
Yeah. If that's the case I wonder if it might be worth waiting a bit on these optimizations until after the rest of the core proxy logic makes it upstream and settles a bit?
It might also help make sure the demand for this optimization/interface is strong before we start adding interfaces prematurely.
Really, I'd like it to all be transparent to the class schedulers, but given the complexities, just having the ability for the scx bpf schedulers to disable keeping blocked_on tasks on the rq (effectively disabling scx proxy donation) seems like a good initial step.
Again, I am really excited about your work here! I've sadly really not had any time since Dec to look into sched_ext details, and I've had to tell a few folks who are interested in both sched_ext and proxy_exec that they should turn proxy_exec off if they want to use sched_ext. So I'm very eager to have a better answer there!
Thanks for all your great work here!
-john
Andrea Righi
reviewerRe: [PATCH 01/10] sched/core: Drop mutex locks before proxy rescheduling
Quoted 14 lines · John Stultz
Quoted 4 lines · Andrea Righi
Inspecting the proxy chain is not strictly required. A BPF scheduler can just accept a blocked donor, enqueue it on its current CPU and let the core proxy-exec path resolve and "virtually" migrate it as needed to the owner's CPU.
Quoted 8 lines
The motivation for providing a
scx_bpf_task_proxy_cpu()kfunc is purely for optimization reasons. By knowing the owner's CPU, a scheduler could make more informed admission decisions. For example, it can inspect the task currently running on the target CPU, determine whether the donor should take precedence over the running task and kick that CPU to trigger a preemption. Without this information, the scheduler can only enqueue the donor and wait until the task running on the owner's CPU releases it (potentially adding up to one time slice of latency).
Quoted 6 lines
So... I suspect I'm missing a subtlety of scx, but in the non-scx case, if a blocked task is important enough to be selected on the current cpu, the
find_proxy_task()logic will walk the chain and do the proxy-migration if needed and mark resched, so then that cpu can evaluate which of the currently running task or the migrated donor is important enough to run.
With scx if a resched is triggered balance_one() does this:
if (prev is runnable and prev->scx.slice > 0)
keep running prev;
We need to set the prev->scx.slice to 0 to select the new task. That means, if the donor is "more important" than the task running in the owner's CPU we need to zero it's time slice. And This can be done via scx_bpf_kick_cpu(cpu, SCX_KICK_PREEMPT).
That's the reason why I was considering introducing the scx_bpf_task_proxy_cpu() kfunc.
However, I agree that this is adding too much complexity and we can ignore this for now.
Moreover, let's also consider the following:
- if donor and owner are on the same CPU everything is already working as intended without scx_bpf_task_proxy_cpu()
- if donor and owner are on different CPUs, something like this should happen (D = donor, O = owner, C = contending task):
CPU0 CPU1 ---- ---- D blocks C runs
ops.enqueue(D, SCX_ENQ_BLOCKED)
[D inserted into CPU0 local DSQ]
D briefly yields to idle
D selected from local DSQ
D migrates ---------------------> ops.enqueue(D, SCX_ENQ_BLOCKED)
[D enters CPU1 local DSQ]
resched, but C retains its slice C continues
C's slice expires ops.enqueue(C, 0)
[C is returned to the BPF policy]
D selected as donor
O proxy-runs using D's slice O releases mutex
If we want to handle the preemption when the donor's CPU != owner's CPU we can just use SCX_ENQ_PREEMPT when scx_bpf_cpu_curr(scx_bpf_task_cpu(p)) is an SCX task.
So the BPF scheduler could do something like this:
ops.enqueue(p, enq_flags) {
if (enq_flags & SCX_ENQ_BLOCKED) { // p is a donor
struct task_struct *curr = scx_bpf_cpu_curr(cpu);
if ((curr && task_is_scx(curr) && is_more_important(p, curr))
enq_flags |= SCX_ENQ_PREEMPT; // curr->scx.slice to 0 + resched
scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu, 0, flags);
}Therefore we don't really need scx_bpf_task_proxy_cpu().
Quoted 23 lines
I worry the difficulty (with my poor understanding of the optimization) is that it seems like its evaluting the preemption on admission (I assume this means the point when we keep the
blocked_ontask on the rq? I may be totally off base here), is that if a very important task was breifly running at that moment, you might not allow theblocked_ontask to be enqueued. And at that point the donor is effectively sleeping and no proxying can happen until it wakes (which may be only when the lock owner eventually runs and releases the lock). In this way the optimization might make a call in that instant that results in *many* time slices of latency (particularly if there are lots other unimportant tasks on that cpu).By letting the proxy logic handle the proxy migration and rescheduling, the
blocked_ontasks are just in the same pool of selectable tasks on that cpu and the scheduler on that cpu gets to decide what is the most important thing to run next.Now, I can see the benefit of potentially saving the resched kick on the target cpu when we do a proxy-migration - it is an interesting idea I should think more on to see how we might do that better in the core logic.
Quoted 8 lines
Quoted 4 lines
That said, I don't see a correctness dependency on exposing this information. If the latency-policy use case is not strong enough to justify the API, I'm happy to drop the query kfuncs and this preparatory lock-scope change for now, the blocked-donor admission support can stand on its own.
Yeah. If that's the case I wonder if it might be worth waiting a bit on these optimizations until after the rest of the core proxy logic makes it upstream and settles a bit?
Agreed. I've already dropped that in my local tree. Let's keep it simple for now. :)
Quoted 10 lines
It might also help make sure the demand for this optimization/interface is strong before we start adding interfaces prematurely.
Really, I'd like it to all be transparent to the class schedulers, but given the complexities, just having the ability for the scx bpf schedulers to disable keeping
blocked_ontasks on the rq (effectively disabling scx proxy donation) seems like a good initial step.
Quoted 8 lines
Again, I am really excited about your work here! I've sadly really not had any time since Dec to look into
sched_extdetails, and I've had to tell a few folks who are interested in bothsched_extandproxy_execthat they should turnproxy_execoff if they want to usesched_ext. So I'm very eager to have a better answer there!Thanks for all your great work here!
-john
Thank you!
-Andrea
Andrea Righi
reviewerPATCH 2/10 sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
In reply to Andrea RighiWith proxy-exec, pick_next_task() can return a task with blocked_on set (a proxy donor). put_prev_set_next_task() then calls set_next_task_scx() on this "ghost" task even though the task only provides scheduling context and never actually runs.
Calling ops.running() for such a donor produces a spurious running event. Simply suppressing ops.running() is not sufficient because the following put_prev_task_scx() would still invoke ops.stopping(), resulting in an unpaired stopping event.
Introduce SCX_TASK_IS_RUNNING to track whether a task entered a real running transition. Set and clear the flag independently of ops.running() and ops.stopping(), as the callbacks are independently optional. Invoke ops.running() only for non-blocked tasks and invoke ops.stopping() only after a real running transition. This keeps the callbacks paired for proxy donors while preserving stopping notifications for schedulers which only implement ops.stopping().
This is a preparatory change for enabling proxy execution together with sched_ext. 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.
--- include/linux/sched/ext.h | 2 ++ kernel/sched/ext/ext.c | 33 +++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-)
include/linux/sched/ext.h +2 -0
@@ -102,6 +102,8 @@ 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_IS_RUNNING = 1 << 6, /* entered a real running transition */ +/* * Bits 8 to 10 are used to carry task state: *
kernel/sched/ext/ext.c +25 -8
@@ -1989,9 +1989,13 @@ static bool dequeue_task_scx(struct rq *rq, struct task_struct *p, int core_deq_* information meaningful to the BPF scheduler and can be suppressed by * skipping the callbacks if the task is !QUEUED. */ -if (SCX_HAS_OP (sch, stopping) &&task_current (rq, p)) { -update_curr_scx (rq); -SCX_CALL_OP_TASK (sch, stopping, rq, p, false); +if (task_current (rq, p) && + (p->scx.flags &SCX_TASK_IS_RUNNING )) { +if (SCX_HAS_OP (sch, stopping)) { +update_curr_scx (rq); +SCX_CALL_OP_TASK (sch, stopping, rq, p, false); + } + p->scx.flags &= ~SCX_TASK_IS_RUNNING ; }if (SCX_HAS_OP (sch, quiescent) && !task_on_rq_migrating (p))
@@ -2686,9 +2690,17 @@ 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 */ -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. A blocked proxy +* donor is also skipped because it provides scheduling context but never +* runs itself. +*/ +if ((p->scx.flags &SCX_TASK_QUEUED ) && !task_is_blocked (p)) { +if (SCX_HAS_OP (sch, running)) +SCX_CALL_OP_TASK (sch, running, rq, p); + + p->scx.flags |=SCX_TASK_IS_RUNNING ; + }clr_task_runnable (p, true);
@@ -2791,8 +2803,13 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,update_curr_scx (rq);/* see dequeue_task_scx() on why we skip when !QUEUED */ -if (SCX_HAS_OP (sch, stopping) && (p->scx.flags &SCX_TASK_QUEUED )) -SCX_CALL_OP_TASK (sch, stopping, rq, p, true); +if ((p->scx.flags &SCX_TASK_QUEUED ) && + (p->scx.flags &SCX_TASK_IS_RUNNING )) { +if (SCX_HAS_OP (sch, stopping)) +SCX_CALL_OP_TASK (sch, stopping, rq, p, true); + + p->scx.flags &= ~SCX_TASK_IS_RUNNING ; + }if (p->scx.flags &SCX_TASK_QUEUED ) {set_task_runnable (rq, p);
Signature · 1 line
2.55.0
Re: [PATCH 02/10] sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
Quoted 11 lines · Andrea Righi
With proxy-exec,
pick_next_task()can return a task withblocked_onset (a proxy donor).put_prev_set_next_task()then callsset_next_task_scx()on this "ghost" task even though the task only provides scheduling context and never actually runs.Calling
ops.running()for such a donor produces a spurious running event. Simply suppressingops.running()is not sufficient because the followingput_prev_task_scx()would still invokeops.stopping(), resulting in an unpaired stopping event.
Quoted 7 lines
Introduce
SCX_TASK_IS_RUNNINGto track whether a task entered a real running transition. Set and clear the flag independently ofops.running()andops.stopping(), as the callbacks are independently optional. Invokeops.running()only for non-blocked tasks and invokeops.stopping()only after a real running transition. This keeps the callbacks paired for proxy donors while preserving stopping notifications for schedulers which only implementops.stopping().
It took me a while to understand this.
It seems you're wanting to distinguish normal task selection and execution (without proxy) from just task selection for proxy-donation (where it doesn't run).
I think what makes it confusing is that TASK_IS_RUNNING is not set for the case when the task is running (rq->curr) as a lock-owning proxy for a waiting donor.
Would it maybe make it easier to follow if the flag was TASK_BLOCKED_DONOR? And the logic was flipped a bit?
That might more clearly cover the case you intend here without extra edge cases that you'll have to explain (well, you're running but you're not the donor and running... ).
thanks
-john
Andrea Righi
reviewerRe: [PATCH 02/10] sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
Quoted 30 lines · John Stultz
Quoted 11 lines · Andrea Righi
With proxy-exec,
pick_next_task()can return a task withblocked_onset (a proxy donor).put_prev_set_next_task()then callsset_next_task_scx()on this "ghost" task even though the task only provides scheduling context and never actually runs.Calling
ops.running()for such a donor produces a spurious running event. Simply suppressingops.running()is not sufficient because the followingput_prev_task_scx()would still invokeops.stopping(), resulting in an unpaired stopping event.Quoted 7 lines
Introduce
SCX_TASK_IS_RUNNINGto track whether a task entered a real running transition. Set and clear the flag independently ofops.running()andops.stopping(), as the callbacks are independently optional. Invokeops.running()only for non-blocked tasks and invokeops.stopping()only after a real running transition. This keeps the callbacks paired for proxy donors while preserving stopping notifications for schedulers which only implementops.stopping().It took me a while to understand this.
It seems you're wanting to distinguish normal task selection and execution (without proxy) from just task selection for proxy-donation (where it doesn't run).
I think what makes it confusing is that
TASK_IS_RUNNINGis not set for the case when the task is running (rq->curr) as a lock-owning proxy for a waiting donor.
Quoted 6 lines
Would it maybe make it easier to follow if the flag was
TASK_BLOCKED_DONOR? And the logic was flipped a bit?That might more clearly cover the case you intend here without extra edge cases that you'll have to explain (well, you're running but you're not the donor and running... ).
I agree that SCX_TASK_IS_RUNNING is confusing, because it doesn't describe really well the rq->curr/physical execution.
What the flag records is whether a task entered the sched_ext running/stopping state. With proxy-exec, the selected scheduling context (rq->donor) and the physical execution context (rq->curr) can differ, which creates two relevant cases:
1. a blocked EXT donor goes through set_next_task_scx() even though its mutex owner executes instead. We must suppress ops.running() for the donor and remember that it did not enter the running state, so that a later ops.stopping() is also suppressed,
2. an EXT mutex owner can execute as rq->curr for a non-EXT donor. The owner is not a blocked donor, but it did not go through set_next_task_scx(), so it must not receive ops.stopping() if it is subsequently dequeued.
So, I don't think changing the flag to SCX_TASK_BLOCKED_DONOR captures the required state. SCX_TASK_IS_RUNNING handles both cases by recording whether the task entered the sched_ext ops.running/stopping() state, independently of whether either callback is implemented.
How about renaming it to SCX_TASK_IN_RUNNING_TRANSITION and explicitly documenting that this is the sched_ext callback state, not physical rq->curr execution? Something like:
/* entered the SCX ops.running/stopping() state, not necessarily rq->curr */ SCX_TASK_IN_RUNNING_TRANSITION = 1 << 6,
Any other ideas for a more clear name?
Thanks,
-Andrea
Andrea Righi
reviewerRe: [PATCH 02/10] sched_ext: Fix ops.running/stopping() pairing for proxy-exec donors
Quoted 60 lines · Andrea Righi
Quoted 30 lines · John Stultz
Quoted 11 lines · Andrea Righi
With proxy-exec,
pick_next_task()can return a task withblocked_onset (a proxy donor).put_prev_set_next_task()then callsset_next_task_scx()on this "ghost" task even though the task only provides scheduling context and never actually runs.Calling
ops.running()for such a donor produces a spurious running event. Simply suppressingops.running()is not sufficient because the followingput_prev_task_scx()would still invokeops.stopping(), resulting in an unpaired stopping event.Quoted 7 lines
Introduce
SCX_TASK_IS_RUNNINGto track whether a task entered a real running transition. Set and clear the flag independently ofops.running()andops.stopping(), as the callbacks are independently optional. Invokeops.running()only for non-blocked tasks and invokeops.stopping()only after a real running transition. This keeps the callbacks paired for proxy donors while preserving stopping notifications for schedulers which only implementops.stopping().It took me a while to understand this.
It seems you're wanting to distinguish normal task selection and execution (without proxy) from just task selection for proxy-donation (where it doesn't run).
I think what makes it confusing is that
TASK_IS_RUNNINGis not set for the case when the task is running (rq->curr) as a lock-owning proxy for a waiting donor.Quoted 6 lines
Would it maybe make it easier to follow if the flag was
TASK_BLOCKED_DONOR? And the logic was flipped a bit?That might more clearly cover the case you intend here without extra edge cases that you'll have to explain (well, you're running but you're not the donor and running... ).
I agree that
SCX_TASK_IS_RUNNINGis confusing, because it doesn't describe really well the rq->curr/physical execution.What the flag records is whether a task entered the
sched_extrunning/stopping state. With proxy-exec, the selected scheduling context (rq->donor) and the physical execution context (rq->curr) can differ, which creates two relevant cases:1. a blocked EXT donor goes through
set_next_task_scx()even though its mutex owner executes instead. We must suppressops.running()for the donor and remember that it did not enter the running state, so that a laterops.stopping()is also suppressed,2. an EXT mutex owner can execute as
rq->currfor a non-EXT donor. The owner is not a blocked donor, but it did not go throughset_next_task_scx(), so it must not receiveops.stopping()if it is subsequently dequeued.So, I don't think changing the flag to
SCX_TASK_BLOCKED_DONORcaptures the required state.SCX_TASK_IS_RUNNINGhandles both cases by recording whether the task entered thesched_extops.running/stopping() state, independently of whether either callback is implemented.
Quoted 8 lines
How about renaming it to
SCX_TASK_IN_RUNNING_TRANSITIONand explicitly documenting that this is thesched_extcallback state, not physicalrq->currexecution? Something like:/* entered the SCX ops.running/stopping() state, not necessarily
rq->curr*/SCX_TASK_IN_RUNNING_TRANSITION= 1 << 6,Any other ideas for a more clear name?
Or perhaps SCX_TASK_RUN_TRACKED (I think I prefer this one). I did other changes to the series, so I'll post a new version with this rename as well. Then we can continue the discussion in the new thread.
Thanks,
-Andrea
Andrea Righi
reviewerPATCH 3/10 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.
--- kernel/sched/ext/ext.c | 53 ++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 17 deletions(-)
kernel/sched/ext/ext.c +36 -17
@@ -1149,17 +1149,24 @@ static void touch_core_sched_dispatch(struct rq *rq, struct task_struct *p)static void update_curr_scx (struct rq *rq) { -struct task_struct *curr = rq->curr; +struct task_struct *donor;s64 delta_exec; +/* +* update_curr_scx() is selected through rq->donor->sched_class, not +* rq->curr->sched_class, so @donor is always an EXT task here. If an EXT +* owner executes for a FAIR donor, FAIR's update_curr() runs instead. +*/ + donor = rq->donor; + delta_exec =update_curr_common (rq);if (unlikely (delta_exec <=0 ))return ; -if (curr->scx.slice !=SCX_SLICE_INF ) { - curr->scx.slice -=min_t (u64, curr->scx.slice, delta_exec); -if (!curr->scx.slice) -touch_core_sched (rq, curr); +if (donor->scx.slice !=SCX_SLICE_INF ) { + donor->scx.slice -=min_t (u64, donor->scx.slice, delta_exec); +if (!donor->scx.slice) +touch_core_sched (rq, donor); }dl_server_update (&rq->ext_server, delta_exec);
@@ -1320,9 +1327,9 @@ static void local_dsq_post_enq(struct scx_sched *sch, struct scx_dispatch_q *dsqif (rq->scx.flags &SCX_RQ_IN_BALANCE )return ; -if ((enq_flags &SCX_ENQ_PREEMPT ) && p != rq->curr && - rq->curr->sched_class == &ext_sched_class) { - rq->curr->scx.slice =0 ; +if ((enq_flags &SCX_ENQ_PREEMPT ) && p != rq->donor && + rq->donor->sched_class == &ext_sched_class) { + rq->donor->scx.slice =0 ;resched_curr (rq); } }
@@ -2470,7 +2477,8 @@ static void dispatch_to_local_dsq(struct scx_sched *sch, struct rq *rq, }/* if the destination CPU is idle, wake it up */ -if (!fallback &&sched_class_above (p->sched_class, dst_rq->curr->sched_class)) +if (!fallback &&sched_class_above (p->sched_class, + dst_rq->donor->sched_class))resched_curr (dst_rq); }
@@ -2678,6 +2686,7 @@ static int balance_one(struct rq *rq, struct task_struct *prev)static void set_next_task_scx (struct rq *rq,struct task_struct *p,bool first) {struct scx_sched *sch =scx_task_sched (p); +bool can_stop_tick;if (p->scx.flags &SCX_TASK_QUEUED ) {/*
@@ -2703,6 +2712,7 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first) }clr_task_runnable (p, true); + can_stop_tick = p->scx.slice ==SCX_SLICE_INF && !task_is_blocked (p);/* * @p is getting newly scheduled or got kicked after someone updated its
@@ -2713,7 +2723,7 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)* nohz. In the future, we might want to add a mechanism to update * load_avgs periodically on tick-stopped CPUs. */ -if (p->scx.slice ==SCX_SLICE_INF ) { +if (can_stop_tick) {if (!(rq->scx.flags &SCX_RQ_CAN_STOP_TICK )) {/* * Bypass mode always assigns finite slices, so @p
@@ -2734,7 +2744,8 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)/* * @rq still references the outgoing scheduling context. A finite -* slice is sufficient by itself to require the tick. +* slice or a blocked proxy donor is sufficient by itself to require +* the tick. */ if (tick_nohz_full_cpu (cpu_of (rq)))tick_nohz_dep_set_cpu (cpu_of (rq),TICK_DEP_BIT_SCHED );
@@ -2907,7 +2918,7 @@ static struct task_struct *first_local_task(struct rq *rq)static struct task_struct *do_pick_task_scx (struct rq *rq,struct rq_flags *rf,bool force_scx) { -struct task_struct *prev = rq->curr; +struct task_struct *prev = rq->donor;bool keep_prev;struct task_struct *p;
@@ -4060,14 +4071,14 @@ static void run_deferred(struct rq *rq)#ifdef CONFIG_NO_HZ_FULL bool scx_can_stop_tick (struct rq *rq) { -struct task_struct *p = rq->curr; +struct task_struct *p = rq->donor;struct scx_sched *sch =scx_task_sched (p);if (p->sched_class != &ext_sched_class)return true;/* -* @rq->curr may still reference an outgoing EXT task after it has been +* @rq->donor may still reference an outgoing EXT task after it has been * dequeued. If no EXT tasks are accounted on @rq, ignore its stale * slice state. If another task is dispatched from a DSQ, * set_next_task_scx() will update the dependency for the incoming task.
@@ -4081,7 +4092,8 @@ bool scx_can_stop_tick(struct rq *rq)/* * @rq can dispatch from different DSQs, so we can't tell whether it * needs the tick or not by looking at nr_running. Allow stopping ticks -* iff the BPF scheduler indicated so. See set_next_task_scx(). +* iff set_next_task_scx() determined that the selected scheduling context +* can run tickless. */ return rq->scx.flags &SCX_RQ_CAN_STOP_TICK ; }
@@ -6047,6 +6059,9 @@ static void scx_dump_cpu(struct scx_sched *sch, struct seq_buf *s,dump_line (&ns," curr=%s[%d] class=%ps" , rq->curr->comm, rq->curr->pid, rq->curr->sched_class); +dump_line (&ns," donor=%s[%d] class=%ps" , + rq->donor->comm, rq->donor->pid, + rq->donor->sched_class);if (!cpumask_empty (rq->scx.cpus_to_kick))dump_line (&ns," cpus_to_kick : %*pb" ,cpumask_pr_args (rq->scx.cpus_to_kick));
@@ -6090,6 +6105,10 @@ static void scx_dump_cpu(struct scx_sched *sch, struct seq_buf *s,if (rq->curr->sched_class == &ext_sched_class && (dump_all_tasks ||scx_task_on_sched (sch, rq->curr)))scx_dump_task (sch, s, dctx, rq, rq->curr,'*' ); +if (rq->donor != rq->curr && + rq->donor->sched_class == &ext_sched_class && + (dump_all_tasks ||scx_task_on_sched (sch, rq->donor))) +scx_dump_task (sch, s, dctx, rq, rq->donor,' ' );list_for_each_entry (p, &rq->scx.runnable_list, scx.runnable_node)if (dump_all_tasks ||scx_task_on_sched (sch, p))
@@ -7518,7 +7537,7 @@ static bool kick_one_cpu(s32 cpu, struct rq *this_rq, unsigned long *ksyncs)unsigned long flags;raw_spin_rq_lock_irqsave (rq, flags); - cur_class = rq->curr->sched_class; + cur_class = rq->donor->sched_class;/* * During CPU hotplug, a CPU may depend on kicking itself to make
@@ -7530,7 +7549,7 @@ static bool kick_one_cpu(s32 cpu, struct rq *this_rq, unsigned long *ksyncs) !sched_class_above(cur_class, &ext_sched_class)) {if (cpumask_test_cpu (cpu, this_scx->cpus_to_preempt)) {if (cur_class == &ext_sched_class) - rq->curr->scx.slice =0 ; + rq->donor->scx.slice =0 ;cpumask_clear_cpu (cpu, this_scx->cpus_to_preempt); }
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 4/10 sched_ext: Fix TOCTOU race in consume_remote_task()
In reply to Andrea RighiWhen pulling a task from a non-local DSQ, scx_consume_dispatch_q() checks whether the task can run on the destination rq via task_can_run_on_remote_rq(). However, it then drops the destination rq lock and locks the source rq in consume_remote_task() -> unlink_dsq_and_lock_src_rq(). During this window, the task might become migration disabled, making it invalid to migrate to the destination rq.
With proxy execution enabled, a mutex-intensive workload such as stress-ng --pipeherd 0 can trigger this race when a donor becomes active on its source rq during the unlocked window. Migrating the active execution context can make it resume with IRQ and preemption state from the wrong scheduling path, triggering sleeping-while-atomic warnings and subsequent lockdep corruption.
Re-evaluate task_can_run_on_remote_rq() after locking the source rq. Use a non-enforcing check because eligibility may have changed during the lock handoff independently of BPF policy; reporting that transient race through scx_error() would unnecessarily abort the scheduler. If the task can no longer migrate, clear its DSQ association, reset the holding CPU, and enqueue it to the global DSQ instead. Normal consumption filtering then skips it on ineligible CPUs while allowing an eligible CPU to pick it without forcing it onto the source local DSQ.
Also reject tasks which are currently executing. Although an on-CPU task should normally not be available for remote DSQ consumption, checking it explicitly prevents a stale or racy candidate from reaching deactivate_task() and set_task_cpu().
While the destination rq lock is dropped, clear the tracked rq state and restore it after reacquiring the lock. Otherwise, a nested ops.dequeue() callback can attempt to restore an rq which is no longer locked.
Closing the race and preserving rq tracking across the lock handoff are prerequisites for correctly supporting proxy execution with sched_ext.
--- kernel/sched/ext/ext.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-)
kernel/sched/ext/ext.c +31 -4
@@ -2145,8 +2145,10 @@ static void move_remote_task_to_local_dsq(struct task_struct *p, u64 enq_flags,* - The BPF scheduler is bypassed while the rq is offline and we can always say * no to the BPF scheduler initiated migrations while offline. * -* The caller must ensure that @p and @rq are on different CPUs. -* If enforce == true, caller must hold @p's rq lock. +* The caller must ensure that @p and @rq are on different CPUs. If @enforce is +* true, report violations attributable to BPF-directed migrations. The caller +* must hold @p's rq lock to avoid reporting a transient race as a scheduler +* error. */ static bool task_can_run_on_remote_rq(struct scx_sched *sch, struct task_struct *p, struct rq *rq,
@@ -2164,6 +2166,10 @@ static bool task_can_run_on_remote_rq(struct scx_sched *sch,WARN_ON_ONCE (task_cpu (p) == cpu); +/* Don't migrate a task which is running on a CPU. */ +if (task_on_cpu (task_rq (p), p)) +return false; +/* * If @p has migration disabled, @p->cpus_ptr is updated to contain only * the pinned CPU in migrate_disable_switch() while @p is being switched
@@ -2257,11 +2263,32 @@ static bool unlink_dsq_and_switch_rq_lock(struct task_struct *p, !WARN_ON_ONCE (src_rq !=task_rq (p)); } -static bool consume_remote_task (struct rq *this_rq, +static bool consume_remote_task (struct scx_sched *sch,struct rq *this_rq,struct task_struct *p,u64 enq_flags,struct scx_dispatch_q *dsq,struct rq *src_rq) {if (unlink_dsq_and_switch_rq_lock (p, dsq, this_rq, src_rq)) { +/* +* Eligibility may have changed while switching rq locks. This is a +* kernel-side race, not an invalid BPF placement request, so don't +* abort the scheduler on failure. Fall back to the global DSQ, where +* normal consumption filters can select an eligible CPU without +* forcing the task onto the source local DSQ. +*/ +if (unlikely (!task_can_run_on_remote_rq (sch, p, this_rq, false))) { + p->scx.dsq =NULL ; + p->scx.holding_cpu =-1 ; +scx_dispatch_enqueue (sch, src_rq, +find_global_dsq (sch,task_cpu (p)), p, + enq_flags |SCX_ENQ_CLEAR_OPSS | +SCX_ENQ_GDSQ_FALLBACK ); +if (sched_class_above (p->sched_class, + src_rq->donor->sched_class)) +resched_curr (src_rq); +switch_rq_lock (src_rq, this_rq); +return false; + } +move_remote_task_to_local_dsq (p, enq_flags, src_rq, this_rq);return true; }else {
@@ -2377,7 +2404,7 @@ bool scx_consume_dispatch_q(struct scx_sched *sch, struct rq *rq, }if (task_can_run_on_remote_rq (sch, p, rq, false)) { -if (likely (consume_remote_task (rq, p, enq_flags, dsq, task_rq))) +if (likely (consume_remote_task (sch, rq, p, enq_flags, dsq, task_rq)))return true; goto retry; }
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 5/10 sched_ext: Handle blocked donor migration with proxy execution
In reply to Andrea RighiFrom: John Stultz <[email protected]>
With proxy execution enabled, mutex-blocked donors stay runnable so their scheduling context can execute the lock owner.
sched_ext may normally relocate a queued blocked donor. set_task_cpu() updates both task_cpu() and wake_cpu, making the destination rq the donor's new callback home. This remains valid if the donor was previously proxy-migrated: the BPF scheduler is intentionally selecting a new return destination, which the next proxy migration will preserve. The usual migration-disabled, affinity, and rq-online checks apply.
An active donor cannot be moved normally because the source rq still references it through rq->donor for scheduling-class operations and runtime accounting. Moving it would associate the task with a different rq while leaving those source-rq references in place. The core proxy migration path avoids this by switching the rq donor to idle before moving the scheduling context.
Allow normal sched_ext migration of blocked donors unless the donor is active on its rq. In that case, defer migration until it is switched out or leave it to the proxy machinery.
Keep blocked donors on the local DSQ when they are put so they remain visible to the proxy pick path.
--- kernel/sched/ext/ext.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
kernel/sched/ext/ext.c +29 -0
@@ -2170,6 +2170,19 @@ static bool task_can_run_on_remote_rq(struct scx_sched *sch,if (task_on_cpu (task_rq (p), p))return false; +/* +* A blocked donor may be moved normally to select a new callback rq. +* set_task_cpu() updates wake_cpu and makes the destination rq its new +* callback home, even if the donor was previously proxy-migrated. +* +* Don't move an active donor while the source rq still references it for +* scheduling and accounting. The migration can be retried after the donor +* is switched out. +*/ +if (task_is_blocked (p) && +rcu_access_pointer (task_rq (p)->donor) == p) +return false; +/* * If @p has migration disabled, @p->cpus_ptr is updated to contain only * the pinned CPU in migrate_disable_switch() while @p is being switched
@@ -2852,6 +2865,22 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,if (p->scx.flags &SCX_TASK_QUEUED ) {set_task_runnable (rq, p); +/* +* Mutex-blocked donors stay queued on the runqueue under proxy +* execution, but the donor never runs as itself, proxy-exec +* walks the blocked_on chain on the next __schedule() and runs +* the lock owner in its place. +* +* Put the donor on the local DSQ directly so pick_next_task() +* can still see it. find_proxy_task() will either run the chain +* owner or deactivate the donor so the wakeup path can return it +* and let BPF make a new dispatch decision once it is unblocked. +*/ +if (task_is_blocked (p)) { +scx_dispatch_enqueue (sch, rq, &rq->scx.local_dsq, p,0 ); + goto switch_class; + } +/* * If @p has slice left and is being put, @p is getting * preempted by a higher priority scheduler class or core-sched
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 6/10 sched_ext: Delegate proxy donor admission to BPF schedulers
In reply to Andrea RighiProxy execution keeps a blocked donor runnable so its scheduling context can execute the mutex owner. Dispatching sched_ext donors on a local DSQ bypasses the BPF scheduler ordering policy and can give donors more CPU priority than intended to perform the proxy execution handoff.
Add SCX_OPS_ENQ_BLOCKED as an explicit proxy execution capability. Tasks owned by schedulers without the flag block normally. Schedulers with the flag receive blocked donors through ops.enqueue() with SCX_ENQ_BLOCKED set in enq_flags and can apply their own admission policy.
From a high-level perspective, the resulting flow for a BPF scheduler with SCX_OPS_ENQ_BLOCKED is:
D ------ blocked on -----> M ------ owned by -----> O
[donor] [mutex] [owner]
|
| ops.enqueue(D, SCX_ENQ_BLOCKED)
| BPF dispatches D to CPUi
v
+-----------------+
| CPUi local DSQ |
+-------+---------+
|
| pick_next_task() selects D
v
+-----------------+
| proxy exec | move D to O's rq
+-------+---------+
|
| run O using D's scheduling context
v
rq->curr = O
rq->donor = D
|
| O releases M
v
+-----------------+
| proxy exec | return D to CPUi via wakeup
+-----------------+Scheduler ownership can change after a donor has already blocked. Since sched_change preserves queued state, handle both iterator-driven moves to root or sub-schedulers and sched_setscheduler() transitions into EXT. Before entering a scheduler without the flag, deactivate the retained donor. It remains on the mutex wait path and wakes normally when the mutex becomes available.
The global sched_ext enable state can change while __schedule() holds the runqueue lock. Once an EXT task has an assigned scheduler, consult it directly so the task cannot retain a donor during the transition unless the scheduler opted in. Tasks without an assigned scheduler keep the generic proxy execution behavior.
The preparation helpers require and assert that p->pi_lock and p's rq lock are held before updating rq state.
The donor starts associated with its original CPU. A BPF scheduler may dispatch it directly into that CPU local DSQ to let the core resolve the mutex owner and execute it with the donor scheduling context.
Reschedule a retained donor when its mutex wakes it so ops.dispatch() can reconsider the now-unblocked task. Make SCX_OPS_ENQ_BLOCKED override the exiting and migration-disabled enqueue fallbacks so opted-in schedulers receive all eligible donor requests.
--- kernel/sched/core.c | 43 ++++++- kernel/sched/ext/ext.c | 107 +++++++++++++++--- kernel/sched/ext/ext.h | 6 + kernel/sched/ext/internal.h | 23 +++- kernel/sched/ext/sub.c | 12 +- kernel/sched/sched.h | 6 + kernel/sched/syscalls.c | 3 + tools/sched_ext/include/scx/compat.h | 1 + .../sched_ext/include/scx/enum_defs.autogen.h | 1 + .../sched_ext/include/scx/enums.autogen.bpf.h | 3 + tools/sched_ext/include/scx/enums.autogen.h | 1 + 11 files changed, 185 insertions(+), 21 deletions(-)
kernel/sched/core.c +42 -1
@@ -6764,6 +6764,45 @@ 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 after block_task() +* drops it from the runqueue. +* +* Unlike the regular schedule() path, this must leave @p fully dequeued. +* DELAY_DEQUEUE may keep a blocked fair task queued with sched_delayed set, +* which would let the following sched_change preserve and re-enqueue it under +* the new scheduler. Complete an existing or newly-created delayed dequeue +* before returning. +*/ +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_resched_idle (rq); +/* Kick the execution context if @rq is remote. */ +resched_curr (rq); + } + +if (!p->se.sched_delayed) +block_task (rq, p, state); +if (p->se.sched_delayed) +dequeue_task (rq, p,DEQUEUE_SLEEP |DEQUEUE_DELAYED | +DEQUEUE_NOCLOCK ); + +WARN_ON_ONCE (task_on_rq_queued (p)); +WARN_ON_ONCE (p->se.sched_delayed); +} +static inline void proxy_release_rq_lock (struct rq *rq,struct rq_flags *rf)__releases (__rq_lockp (rq)) {
@@ -7006,6 +7045,7 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)proxy_migrate_task (rq, rf, p, owner_cpu); return NULL; } +#else /* SCHED_PROXY_EXEC */ static struct task_struct *find_proxy_task (struct rq *rq,struct task_struct *donor,struct rq_flags *rf)
@@ -7136,7 +7176,8 @@ static void __sched notrace __schedule(int sched_mode)* task_is_blocked() will always be 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; }
kernel/sched/ext/ext.c +91 -16
159 lines not on this page · raw message
kernel/sched/ext/ext.h +6 -0
@@ -18,8 +18,11 @@ bool scx_can_stop_tick(struct rq *rq);void scx_rq_activate (struct rq *rq);void scx_rq_deactivate (struct rq *rq);int scx_check_setscheduler (struct task_struct *p,int policy); +void scx_prepare_setscheduler (struct task_struct *p, +const struct sched_class *next_class);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 init_sched_ext_class (void );static inline u32 scx_cpuperf_target (s32 cpu)
@@ -52,8 +55,11 @@ static inline bool scx_can_stop_tick(struct rq *rq) { return true; }static inline void scx_rq_activate (struct rq *rq) {}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 void scx_prepare_setscheduler (struct task_struct *p, +const struct sched_class *next_class) {}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 init_sched_ext_class (void ) {}#endif /* CONFIG_SCHED_CLASS_EXT */
kernel/sched/ext/internal.h +22 -1
@@ -212,6 +212,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 |
@@ -220,7 +233,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,
@@ -1339,6 +1353,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,
@@ -1624,6 +1644,7 @@ void scx_task_iter_start(struct scx_task_iter *iter, struct cgroup *cgrp);void scx_task_iter_unlock (struct scx_task_iter *iter);void scx_task_iter_stop (struct scx_task_iter *iter);struct task_struct *scx_task_iter_next_locked (struct scx_task_iter *iter); +void scx_prepare_task_sched_change (struct task_struct *p,struct scx_sched *sch);bool scx_consume_dispatch_q (struct scx_sched *sch,struct rq *rq,struct scx_dispatch_q *dsq,u64 enq_flags);bool scx_consume_global_dsq (struct scx_sched *sch,struct rq *rq);
kernel/sched/ext/sub.c +9 -3
@@ -117,7 +117,9 @@ static void scx_fail_parent(struct scx_sched *sch,if (scx_task_on_sched (parent, p))continue ; - scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { +scx_prepare_task_sched_change (p, parent); +scoped_guard (sched_change, p,DEQUEUE_SAVE |DEQUEUE_MOVE | + DEQUEUE_NOCLOCK) {scx_disable_and_exit_task (sch, p);scx_set_task_sched (p, parent); }
@@ -209,7 +211,9 @@ void scx_sub_disable(struct scx_sched *sch)continue ; } - scoped_guard (sched_change, p, DEQUEUE_SAVE | DEQUEUE_MOVE) { +scx_prepare_task_sched_change (p, parent); +scoped_guard (sched_change, p,DEQUEUE_SAVE |DEQUEUE_MOVE | + DEQUEUE_NOCLOCK) {/* * $p is initialized for $parent and still attached to * @sch. Disable and exit for @sch, switch over to
@@ -503,7 +507,9 @@ 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) { +scx_prepare_task_sched_change (p, sch); +scoped_guard (sched_change, p,DEQUEUE_SAVE |DEQUEUE_MOVE | + DEQUEUE_NOCLOCK) {/* * $p must be either READY or ENABLED. If ENABLED, * __scx_disabled_and_exit_task() first disables and
kernel/sched/sched.h +6 -0
@@ -2471,6 +2471,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;
kernel/sched/syscalls.c +3 -0
@@ -678,6 +678,9 @@ int __sched_setscheduler(struct task_struct *p,if (prev_class != next_class) queue_flags |=DEQUEUE_CLASS ; +if (!(attr->sched_flags &SCHED_FLAG_KEEP_PARAMS )) +scx_prepare_setscheduler (p, next_class); + scoped_guard (sched_change, p, queue_flags) {if (!(attr->sched_flags &SCHED_FLAG_KEEP_PARAMS )) {
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 +1 -0
@@ -55,6 +55,7 @@#define HAVE_SCX_ENQ_IMMED #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
tools/sched_ext/include/scx/enums.autogen.bpf.h +3 -0
@@ -130,6 +130,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
@@ -47,6 +47,7 @@SCX_ENUM_SET (skel, scx_enq_flags,SCX_ENQ_IMMED ); \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
Re: [PATCH 06/10] sched_ext: Delegate proxy donor admission to BPF schedulers
Quoted 13 lines · Andrea Righi
kernel/sched/ext/ext.c +0
-9@@ -2866,18 +2938,12 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,set_task_runnable (rq, p);/* -* Mutex-blocked donors stay queued on the runqueue under proxy -* execution, but the donor never runs as itself, proxy-exec -* walks the blocked_on chain on the next __schedule() and runs -* the lock owner in its place. -* -* Put the donor on the local DSQ directly so pick_next_task() -* can still see it. find_proxy_task() will either run the chain -* owner or deactivate the donor so the wakeup path can return it -* and let BPF make a new dispatch decision once it is unblocked.
kernel/sched/ext/ext.c static void put_prev_task_scx(struct rq *rq, struct task_struct *p, · line 2949
Quoted 10 lines
kernel/sched/ext/ext.c +4
-1+* Mutex-blocked donors only stay queued when their BPF scheduler +* enables %SCX_OPS_ENQ_BLOCKED, so always delegate their admission. */ if (task_is_blocked (p)) { -scx_dispatch_enqueue (sch, rq, &rq->scx.local_dsq, p,0 ); +WARN_ON_ONCE (!(sch->ops.flags &SCX_OPS_ENQ_BLOCKED )); +scx_do_enqueue_task (rq, p,0 ,-1 ); goto switch_class; }
Just FYI: So in doing some testing running my priority-inversion-demo test in a loop with this series (rebased on 7.2-rc2), goining through the example schedulers in tools/sched_ext/ I hit this WARN_ON after running awhile with the scx_pair scheduler.
Haven't done much debugging on it yet, may have to wait until monday, but the splat is below.
thanks
-john
[25733.271828] WARNING: kernel/sched/ext/ext.c:2945 at put_prev_task_scx+0x449/0x460, CPU#0: foreground/25080 [25733.277072] CPU: 0 UID: 0 PID: 25080 Comm: foreground Not tainted 7.2.0-rc2-00043-g9699f4add4b0 #39 PREEMPT(full) [25733.286458] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014 [25733.295933] Sched_ext: pair (enabled+all), task: runnable_at=-798ms [25733.295937] RIP: 0010:put_prev_task_scx+0x449/0x460 [25733.306251] Code: 48 8b 83 68 11 00 00 48 8b b5 10 04 00 00 48 39 b0 98 11 00 00 0f 85 79 ff ff ff e9 65 ff ff ff 90 0f 0b 90 e9 46 fe ff ff 90 <0f> 0b 90 e9 b5 fd ff ff b8 02 00 00 00 e9 94 fc ff ff e8 90 bb 2d [25733.322497] RSP: 0018:ffffc90011f7faa8 EFLAGS: 00010046 [25733.325750] RAX: 0000000000000001 RBX: ffff8881b8e2db80 RCX: ffff8880bd482650 [25733.333460] RDX: ffff8881b8e2e618 RSI: ffff8880bd482650 RDI: ffff8881073b2650 [25733.338640] RBP: ffff8881073b22c0 R08: 0000000000000000 R09: 0000000000000000 [25733.344710] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88800ba23000 [25733.351179] R13: ffff8880bd4822c0 R14: ffffffff82f6a518 R15: ffffffff82f6a040 [25733.356585] FS: 00007f9badd90780(0000) GS:ffff888234841000(0000) knlGS:0000000000000000 [25733.362523] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [25733.365426] CR2: 00007f91f1e48110 CR3: 000000001113c006 CR4: 0000000000370ef0 [25733.372285] Call Trace: [25733.373817] <TASK> [25733.375178] __pick_next_task+0xa8/0x180 [25733.378968] __schedule+0x268/0x2600 [25733.381092] ? mark_held_locks+0x40/0x70 [25733.383556] preempt_schedule_notrace+0x43/0xa0 [25733.386311] ? preempt_schedule_notrace_thunk+0x16/0x30 [25733.390954] preempt_schedule_notrace_thunk+0x16/0x30 [25733.395476] rcu_is_watching+0x3e/0x50 [25733.397722] lock_acquire+0x2c0/0x310 [25733.401168] ? _raw_spin_unlock_irqrestore+0x50/0x60 [25733.404224] schedule+0x9d/0x100 [25733.406428] ? schedule+0x7e/0x100 [25733.408130] schedule_preempt_disabled+0x18/0x30 [25733.411008] __mutex_lock+0x800/0x1100 [25733.415788] ? __start_renaming+0x5d/0x190 [25733.419011] ? lock_acquire+0xd9/0x310 [25733.421518] ? __start_renaming+0x5d/0x190 [25733.424188] __start_renaming+0x5d/0x190 [25733.426676] filename_renameat2+0x318/0x420 [25733.429258] ? find_held_lock+0x2b/0x80 [25733.432581] __x64_sys_rename+0x48/0x70 [25733.435240] do_syscall_64+0xf3/0x6c0 [25733.437675] entry_SYSCALL_64_after_hwframe+0x77/0x7f
Andrea Righi
reviewerRe: [PATCH 06/10] sched_ext: Delegate proxy donor admission to BPF schedulers
Hi John,
Quoted 30 lines · John Stultz
Quoted 13 lines · Andrea Righi
kernel/sched/ext/ext.c +0
-9@@ -2866,18 +2938,12 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p,set_task_runnable (rq, p);/* -* Mutex-blocked donors stay queued on the runqueue under proxy -* execution, but the donor never runs as itself, proxy-exec -* walks the blocked_on chain on the next __schedule() and runs -* the lock owner in its place. -* -* Put the donor on the local DSQ directly so pick_next_task() -* can still see it. find_proxy_task() will either run the chain -* owner or deactivate the donor so the wakeup path can return it -* and let BPF make a new dispatch decision once it is unblocked. kernel/sched/ext/ext.c static void put_prev_task_scx(struct rq *rq, struct task_struct *p, · line 2949
Quoted 10 lines
kernel/sched/ext/ext.c +4
-1+* Mutex-blocked donors only stay queued when their BPF scheduler +* enables %SCX_OPS_ENQ_BLOCKED, so always delegate their admission. */ if (task_is_blocked (p)) { -scx_dispatch_enqueue (sch, rq, &rq->scx.local_dsq, p,0 ); +WARN_ON_ONCE (!(sch->ops.flags &SCX_OPS_ENQ_BLOCKED )); +scx_do_enqueue_task (rq, p,0 ,-1 ); goto switch_class; }Just FYI: So in doing some testing running my priority-inversion-demo test in a loop with this series (rebased on 7.2-rc2), goining through the example schedulers in
tools/sched_ext/I hit thisWARN_ONafter running awhile with thescx_pairscheduler.
Quoted 2 lines
Haven't done much debugging on it yet, may have to wait until monday, but the splat is below.
Yeah, this is because put_prev_task_scx() uses task_is_blocked(p), and we should use p->is_blocked instead, since task_is_blocked() can become true before the task has actually been processed as blocked by the scheduler.
I've fixed this in my local tree, this should disappear in the next version.
Thanks!
-Andrea
Quoted 50 lines
thanks
-john[25733.271828] WARNING: kernel/sched/ext/ext.c:2945 at put_prev_task_scx+0x449/0x460, CPU#0: foreground/25080 [25733.277072] CPU: 0 UID: 0 PID: 25080 Comm: foreground Not tainted 7.2.0-rc2-00043-g9699f4add4b0 #39 PREEMPT(full) [25733.286458] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014 [25733.295933] Sched_ext: pair (enabled+all), task: runnable_at=-798ms [25733.295937] RIP: 0010:put_prev_task_scx+0x449/0x460 [25733.306251] Code: 48 8b 83 68 11 00 00 48 8b b5 10 04 00 00 48 39 b0 98 11 00 00 0f 85 79 ff ff ff e9 65 ff ff ff 90 0f 0b 90 e9 46 fe ff ff 90 <0f> 0b 90 e9 b5 fd ff ff b8 02 00 00 00 e9 94 fc ff ff e8 90 bb 2d [25733.322497] RSP: 0018:ffffc90011f7faa8 EFLAGS: 00010046 [25733.325750] RAX: 0000000000000001 RBX: ffff8881b8e2db80 RCX: ffff8880bd482650 [25733.333460] RDX: ffff8881b8e2e618 RSI: ffff8880bd482650 RDI: ffff8881073b2650 [25733.338640] RBP: ffff8881073b22c0 R08: 0000000000000000 R09: 0000000000000000 [25733.344710] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88800ba23000 [25733.351179] R13: ffff8880bd4822c0 R14: ffffffff82f6a518 R15: ffffffff82f6a040 [25733.356585] FS: 00007f9badd90780(0000) GS:ffff888234841000(0000) knlGS:0000000000000000 [25733.362523] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [25733.365426] CR2: 00007f91f1e48110 CR3: 000000001113c006 CR4: 0000000000370ef0 [25733.372285] Call Trace: [25733.373817] <TASK> [25733.375178] __pick_next_task+0xa8/0x180 [25733.378968] __schedule+0x268/0x2600 [25733.381092] ? mark_held_locks+0x40/0x70 [25733.383556] preempt_schedule_notrace+0x43/0xa0 [25733.386311] ? preempt_schedule_notrace_thunk+0x16/0x30 [25733.390954] preempt_schedule_notrace_thunk+0x16/0x30 [25733.395476] rcu_is_watching+0x3e/0x50 [25733.397722] lock_acquire+0x2c0/0x310 [25733.401168] ? _raw_spin_unlock_irqrestore+0x50/0x60 [25733.404224] schedule+0x9d/0x100 [25733.406428] ? schedule+0x7e/0x100 [25733.408130] schedule_preempt_disabled+0x18/0x30 [25733.411008] __mutex_lock+0x800/0x1100 [25733.415788] ? __start_renaming+0x5d/0x190 [25733.419011] ? lock_acquire+0xd9/0x310 [25733.421518] ? __start_renaming+0x5d/0x190 [25733.424188] __start_renaming+0x5d/0x190 [25733.426676] filename_renameat2+0x318/0x420 [25733.429258] ? find_held_lock+0x2b/0x80 [25733.432581] __x64_sys_rename+0x48/0x70 [25733.435240] do_syscall_64+0xf3/0x6c0 [25733.437675] entry_SYSCALL_64_after_hwframe+0x77/0x7f
Andrea Righi
reviewerPATCH 7/10 sched_ext: Add proxy destination query kfuncs
In reply to Andrea RighiBPF schedulers admitting blocked proxy donors may want to know the CPU or cid where the mutex owner will execute.
Introduce scx_bpf_task_proxy_cpu() and scx_bpf_task_proxy_cid() to return the CPU or cid of the next mutex owner in the proxy chain.
The owner relationship may change immediately after the query, so expose the result only as a scheduling hint. Return a negative errno when no valid proxy destination or cid mapping is available.
Provide compatibility wrappers that return -EOPNOTSUPP when the kfuncs are unavailable.
--- kernel/sched/core.c | 28 ++++++++++++++++ kernel/sched/ext/ext.c | 42 ++++++++++++++++++++++++ kernel/sched/ext/internal.h | 6 ++-- kernel/sched/sched.h | 2 ++ tools/sched_ext/include/scx/common.bpf.h | 2 ++ tools/sched_ext/include/scx/compat.bpf.h | 18 ++++++++++ 6 files changed, 96 insertions(+), 2 deletions(-)
kernel/sched/core.c +28 -0
@@ -7046,6 +7046,34 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)return NULL ; } +int task_proxy_cpu (struct task_struct *p) +{ +struct task_struct *owner; +struct mutex *mutex; + +if (!sched_proxy_exec () || !READ_ONCE (p->is_blocked)) +return -ENOENT ; + +guard (raw_spinlock_irqsave)(&p->blocked_lock); + + mutex =__get_task_blocked_on (p); +if (!mutex) +return -ENOENT ; + +/* +* @blocked_lock stabilizes @blocked_on and thus the mutex lifetime. +* The owner is an atomic snapshot used only as a scheduling hint and +* may change as soon as this function returns, so wait_lock is not +* needed here. +*/ + owner =__mutex_owner (mutex); +if (!owner) +return -ENOENT ; +if (!READ_ONCE (owner->on_rq) || owner->se.sched_delayed) +return -ENOENT ; + +return task_cpu (owner); +}#else /* SCHED_PROXY_EXEC */ static struct task_struct *find_proxy_task (struct rq *rq,struct task_struct *donor,struct rq_flags *rf)
kernel/sched/ext/ext.c +42 -0
60 lines not on this page · raw message
kernel/sched/ext/internal.h +4 -2
@@ -214,8 +214,10 @@ enum scx_ops_flags {/* * 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. +* passed to ops.enqueue() with %SCX_ENQ_BLOCKED. The BPF scheduler can +* query the next mutex owner's CPU or cid with scx_bpf_task_proxy_cpu() +* or scx_bpf_task_proxy_cid(). It controls when donors are dispatched and +* whether they should preempt work on the owner's CPU. * * If clear, mutex-blocked tasks are removed from the runqueue normally * and cannot donate their scheduling context through proxy execution.
kernel/sched/sched.h +2 -0
@@ -2472,8 +2472,10 @@ static inline bool task_is_blocked(struct task_struct *p) }#ifdef CONFIG_SCHED_PROXY_EXEC +int task_proxy_cpu (struct task_struct *p);void sched_proxy_block_task (struct rq *rq,struct task_struct *p);#else +static inline int task_proxy_cpu (struct task_struct *p) {return -EOPNOTSUPP ; }static inline void sched_proxy_block_task (struct rq *rq,struct task_struct *p) {}#endif
tools/sched_ext/include/scx/common.bpf.h +2 -0
@@ -95,6 +95,8 @@ s32 scx_bpf_pick_idle_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym; s32 scx_bpf_pick_any_cpu_node(const cpumask_t *cpus_allowed, int node, u64 flags) __ksym __weak; s32 scx_bpf_pick_any_cpu(const cpumask_t *cpus_allowed, u64 flags) __ksym; bool scx_bpf_task_running(const struct task_struct *p) __ksym; +s32 scx_bpf_task_proxy_cpu(struct task_struct *p) __ksym __weak; +s32 scx_bpf_task_proxy_cid(struct task_struct *p) __ksym __weak; s32 scx_bpf_task_cpu(const struct task_struct *p) __ksym;struct rq *scx_bpf_locked_rq (void ) __ksym;struct task_struct *scx_bpf_cpu_curr (s32 cpu) __ksym__weak ;
tools/sched_ext/include/scx/compat.bpf.h +18 -0
@@ -133,6 +133,24 @@ static inline void scx_bpf_cid_override(const s32 *cpu_to_cid, u32 cpu_to_cid__sreturn scx_bpf_cid_override___compat (cpu_to_cid, cpu_to_cid__sz); } +/* +* v7.3: scx_bpf_task_proxy_cpu() and scx_bpf_task_proxy_cid() for querying +* the next proxy execution destination. Return -EOPNOTSUPP if unavailable. +*/ +static inline s32 __COMPAT_scx_bpf_task_proxy_cpu (struct task_struct *p) +{ +if (bpf_ksym_exists (scx_bpf_task_proxy_cpu)) +return scx_bpf_task_proxy_cpu (p); +return -EOPNOTSUPP ; +} + +static inline s32 __COMPAT_scx_bpf_task_proxy_cid (struct task_struct *p) +{ +if (bpf_ksym_exists (scx_bpf_task_proxy_cid)) +return scx_bpf_task_proxy_cid (p); +return -EOPNOTSUPP ; +} +/** * __COMPAT_is_enq_cpu_selected - Test if SCX_ENQ_CPU_SELECTED is on * in a compatible way. We will preserve this __COMPAT helper until v6.16.
Signature · 1 line
2.55.0
Re: [PATCH 07/10] sched_ext: Add proxy destination query kfuncs
Quoted 4 lines · Andrea Righi
BPF schedulers admitting blocked proxy donors may want to know the CPU or cid where the mutex owner will execute.
As I mentioned before, this probably needs some more context as to why this is useful/important to the bpf scheduler.
Quoted 46 lines
Introduce
scx_bpf_task_proxy_cpu()andscx_bpf_task_proxy_cid()to return the CPU or cid of the next mutex owner in the proxy chain.The owner relationship may change immediately after the query, so expose the result only as a scheduling hint. Return a negative errno when no valid proxy destination or cid mapping is available.
Provide compatibility wrappers that return
-EOPNOTSUPPwhen the kfuncs are unavailable.Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/core.c | 28 ++++++++++++++++ kernel/sched/ext/ext.c | 42 ++++++++++++++++++++++++ kernel/sched/ext/internal.h | 6 ++-- kernel/sched/sched.h | 2 ++ tools/sched_ext/include/scx/common.bpf.h | 2 ++ tools/sched_ext/include/scx/compat.bpf.h | 18 ++++++++++ 6 files changed, 96 insertions(+), 2 deletions(-)kernel/sched/core.c +18
-0@@ -7046,6 +7046,34 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)return NULL ; } +int task_proxy_cpu (struct task_struct *p) +{ +struct task_struct *owner; +struct mutex *mutex; + +if (!sched_proxy_exec () || !READ_ONCE (p->is_blocked)) +return -ENOENT ; + +guard (raw_spinlock_irqsave)(&p->blocked_lock); + + mutex =__get_task_blocked_on (p); +if (!mutex) +return -ENOENT ; + +/* +* @blocked_lock stabilizes @blocked_on and thus the mutex lifetime. +* The owner is an atomic snapshot used only as a scheduling hint and +* may change as soon as this function returns, so wait_lock is not
kernel/sched/core.c find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf) · line 7076
Quoted 10 lines
kernel/sched/core.c +10
-0+* needed here. +*/ + owner =__mutex_owner (mutex); +if (!owner) +return -ENOENT ; +if (!READ_ONCE (owner->on_rq) || owner->se.sched_delayed) +return -ENOENT ; + +return task_cpu (owner); +}
So, I'm somewhat skeptical of this. You do disclaim in the commit log above that this is only a hint and it might change, but I fret it might be to a point it's not worth much as a hint.
First: you're only looking at the immediate mutex owner, not the actual runnable owner of the full chain. So current could be on cpu1, the next owner on cpu2, but that task also just blocked on a mutex owned on cpu3. And the task on cpu2 may be about to migrate to cpu3 (where current should ideally also proxy migrate to). So I'm not sure what such an ephemeral value might be worth. Again, maybe the optimization you have in mind is worth it, but it probably just needs some additional explanation in the commit message.
Second: The blocked_lock does stabilize the mutex lifetime, but not the owner's. Once you've read the __mutex_owner(), without holding the mutex wait_lock, the mutex owner can be releasing the lock and then immediately exit, causing the owner dereferences that follow here to cause a UAF.
thanks
-john
Andrea Righi
reviewerRe: [PATCH 07/10] sched_ext: Add proxy destination query kfuncs
Quoted 80 lines · John Stultz
Quoted 4 lines · Andrea Righi
BPF schedulers admitting blocked proxy donors may want to know the CPU or cid where the mutex owner will execute.
As I mentioned before, this probably needs some more context as to why this is useful/important to the bpf scheduler.
Quoted 46 lines
Introduce
scx_bpf_task_proxy_cpu()andscx_bpf_task_proxy_cid()to return the CPU or cid of the next mutex owner in the proxy chain.The owner relationship may change immediately after the query, so expose the result only as a scheduling hint. Return a negative errno when no valid proxy destination or cid mapping is available.
Provide compatibility wrappers that return
-EOPNOTSUPPwhen the kfuncs are unavailable.Signed-off-by Andrea Righi <[email protected]>--- kernel/sched/core.c | 28 ++++++++++++++++ kernel/sched/ext/ext.c | 42 ++++++++++++++++++++++++ kernel/sched/ext/internal.h | 6 ++-- kernel/sched/sched.h | 2 ++ tools/sched_ext/include/scx/common.bpf.h | 2 ++ tools/sched_ext/include/scx/compat.bpf.h | 18 ++++++++++ 6 files changed, 96 insertions(+), 2 deletions(-)kernel/sched/core.c +18
-0@@ -7046,6 +7046,34 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)return NULL ; } +int task_proxy_cpu (struct task_struct *p) +{ +struct task_struct *owner; +struct mutex *mutex; + +if (!sched_proxy_exec () || !READ_ONCE (p->is_blocked)) +return -ENOENT ; + +guard (raw_spinlock_irqsave)(&p->blocked_lock); + + mutex =__get_task_blocked_on (p); +if (!mutex) +return -ENOENT ; + +/* +* @blocked_lock stabilizes @blocked_on and thus the mutex lifetime. +* The owner is an atomic snapshot used only as a scheduling hint and +* may change as soon as this function returns, so wait_lock is not kernel/sched/core.c find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf) · line 7076
Quoted 10 lines
kernel/sched/core.c +10
-0+* needed here. +*/ + owner =__mutex_owner (mutex); +if (!owner) +return -ENOENT ; +if (!READ_ONCE (owner->on_rq) || owner->se.sched_delayed) +return -ENOENT ; + +return task_cpu (owner); +}So, I'm somewhat skeptical of this. You do disclaim in the commit log above that this is only a hint and it might change, but I fret it might be to a point it's not worth much as a hint.
First: you're only looking at the immediate mutex owner, not the actual runnable owner of the full chain. So current could be on cpu1, the next owner on cpu2, but that task also just blocked on a mutex owned on cpu3. And the task on cpu2 may be about to migrate to cpu3 (where current should ideally also proxy migrate to). So I'm not sure what such an ephemeral value might be worth. Again, maybe the optimization you have in mind is worth it, but it probably just needs some additional explanation in the commit message.
Quoted 5 lines
Second: The
blocked_lockdoes stabilize the mutex lifetime, but not the owner's. Once you've read the__mutex_owner(), without holding the mutexwait_lock, the mutex owner can be releasing the lock and then immediately exit, causing the owner dereferences that follow here to cause a UAF.
Yep, as mentioned in the previous email, let's get rid of this kfuncs completely for now. In this way we can get rid of unnecessary complexity, fix these issues, make the patch more self-consitent in sched_ext, it's a win-win. :)
Thanks,
-Andrea
Andrea Righi
reviewerPATCH 8/10 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 donor admission requests in enq_flags, while __COMPAT_scx_bpf_task_proxy_cpu() returns the CPU of the mutex owner or a fallback error when the kfunc is unavailable. Add test coverage for this interface and owner CPU.
Exercise a three-task priority inversion with all tasks pinned to the same CPU using a weighted vruntime BPF scheduler. A nice +19 owner holds a shared mutex, a nice -20 donor blocks on it and a nice 0 CPU contender delays the owner.
Run the workload with SCX_OPS_ENQ_BLOCKED first disabled and then enabled. In the enabled run, count blocked donor enqueues, validate that the proxy CPU matches the shared CPU and enqueue the donor on its current CPU's local DSQ to trigger proxy execution. Report average mutex hold and wait times without enforcing performance results.
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 | 109 +++ .../testing/selftests/sched_ext/enq_blocked.c | 733 ++++++++++++++++++ .../testing/selftests/sched_ext/enq_blocked.h | 27 + .../selftests/sched_ext/test_modules/Makefile | 13 + .../test_modules/scx_enq_blocked_test.c | 193 +++++ 8 files changed, 1083 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 +109 -0
109 lines not on this page · raw message
tools/testing/selftests/sched_ext/enq_blocked.c new file +733 -0
733 lines not on this page · raw message
tools/testing/selftests/sched_ext/enq_blocked.h new file +27 -0
@@ -0,0 +1,27 @@ +/* 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_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 +193 -0
193 lines not on this page · raw message
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 9/10 sched_ext: scx_qmap: Add proxy execution support
In reply to Andrea RighiAdd a -B 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 -B, set SCX_OPS_ENQ_BLOCKED and enable the corresponding BPF-side blocked-donor admission policy.
When qmap_enqueue() receives SCX_ENQ_BLOCKED for a donor with runtime budget remaining, insert it directly into the local DSQ of its current cid without refilling its slice. This lets proxy execution continue using the donor's existing budget.
Once the donor exhausts its slice, let it proceed through qmap's normal enqueue path. This applies the same placement and slice refill decisions as other enqueues instead of imposing a proxy-specific policy.
--- tools/sched_ext/scx_qmap.bpf.c | 13 +++++++++++++ tools/sched_ext/scx_qmap.c | 9 +++++++-- 2 files changed, 20 insertions(+), 2 deletions(-)
tools/sched_ext/scx_qmap.bpf.c +13 -0
@@ -49,6 +49,7 @@ const volatile u64 sub_cgroup_id;const volatile s32 disallow_tgid;const volatile bool suppress_dump;const volatile bool always_enq_immed; +const volatile bool enq_blocked;const volatile u32 immed_stress_nth;const volatile u32 max_tasks;
@@ -396,6 +397,18 @@ void BPF_STRUCT_OPS(qmap_enqueue, struct task_struct *p, u64 enq_flags)*/ taskc->core_sched_seq = qa.core_sched_tail_seqs[idx]++; +/* +* Admit a blocked donor with runtime budget left by inserting it into +* its current cid's local DSQ, reusing its remaining slice. If the +* slice is exhausted, fall through the normal enqueue path. +*/ +if (enq_blocked && + (enq_flags &SCX_ENQ_BLOCKED ) && p->scx.slice) { +scx_bpf_dsq_insert (p,SCX_DSQ_LOCAL_ON |scx_bpf_task_cid (p), +0 , enq_flags); +return ; + } +/* * IMMED stress testing: Every immed_stress_nth'th enqueue, dispatch * directly to prev_cpu's local DSQ even when busy to force dsq->nr > 1
tools/sched_ext/scx_qmap.c +7 -2
@@ -23,7 +23,7 @@ const char help_fmt[] ="See the top-level comment in .bpf.c for more details.\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] [-B]\n" " [-F COUNT] [-v]\n" "\n" " -s SLICE_US Override slice duration\n"
@@ -42,6 +42,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" +" -B 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)\n" " -v Print libbpf debug messages\n"
@@ -89,7 +90,7 @@ int main(int argc, char **argv) skel->rodata->slice_ns =__COMPAT_ENUM_OR_ZERO ("scx_public_consts" ,"SCX_SLICE_DFL" ); skel->rodata->max_tasks =16384 ; -while ((opt =getopt (argc, argv,"s:e:t:T:l:b:N:PMHc:d:D:SpIF:C:vh" )) !=-1 ) { +while ((opt =getopt (argc, argv,"s:e:t:T:l:b:N:PMHc:d:D:SpIBF:C:vh" )) !=-1 ) {switch (opt) {case 's' : skel->rodata->slice_ns =strtoull (optarg,NULL ,0 ) *1000 ;
@@ -149,6 +150,10 @@ int main(int argc, char **argv) skel->rodata->always_enq_immed = true; skel->struct_ops.qmap_ops->flags |=SCX_OPS_ALWAYS_ENQ_IMMED ;break ; +case 'B' : + skel->rodata->enq_blocked = true; + skel->struct_ops.qmap_ops->flags |=SCX_OPS_ENQ_BLOCKED ; +break ;case 'F' : skel->rodata->immed_stress_nth =strtoul (optarg,NULL ,0 );break ;
Signature · 1 line
2.55.0
Andrea Righi
reviewerPATCH 10/10 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