* [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 21:12 ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 2/8] sched/proxy: Wake blocked RT and deadline tasks through the full path Mathieu Desnoyers
` (8 subsequent siblings)
9 siblings, 1 reply; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
With proxy execution, a task blocked on a mutex stays on its runqueue.
The scheduler can pick it as the donor and run the mutex owner in its
place. When the donor is woken, the CPU must stop running the owner on
the donor's context.
Commit 7918cf369361 ("sched/proxy: Only return migrate when needed")
wakes a blocked task in place when it is still on its original CPU.
ttwu_runnable() then relies on wakeup_preempt() to preempt the running
task. But every class compares the woken task with rq->donor. When the
woken task is the donor, it compares the task with itself, and no
reschedule is requested. Only sched_ext handles this, with WF_TTWU_RQ.
So the old owner keeps the donor's context after the mutex hand-off or
after a signal. With a SCHED_FIFO donor nothing bounds this: the owner
runs at the donor's priority until it blocks. With a SCHED_DEADLINE
donor the owner consumes the donor's runtime.
In a test on one CPU, a SCHED_OTHER owner kept spinning in user space
after it released the mutex. A SCHED_FIFO waiter then ran 800ms late. A
SCHED_DEADLINE waiter ran one period late.
The same would happen when a task in the middle of a chain is woken.
proxy_needs_return() resets the donor only when the woken task is the
donor.
Fix this in ttwu_runnable(). When a blocked task is woken on a runqueue
that runs a proxy, reschedule. The next pick walks the chain again. A
blocked task outside the donor's chain causes a harmless reschedule
too.
Fixes: 7918cf369361 ("sched/proxy: Only return migrate when needed")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
kernel/sched/core.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index dc270a23179d..326026ba0b2b 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4057,9 +4057,20 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags)
update_rq_clock(rq);
if (p->is_blocked) {
+ bool returned;
+
if (p->se.sched_delayed)
enqueue_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_DELAYED);
- if (proxy_needs_return(rq, p))
+ returned = proxy_needs_return(rq, p);
+ /*
+ * @p was blocked and this CPU runs a proxy. If @p is the donor
+ * or a task of its chain, the donor no longer waits for the
+ * task that runs. wakeup_preempt() compares @p with the donor,
+ * so it does not see this. Pick again.
+ */
+ if (rq->donor != rq->curr)
+ resched_curr(rq);
+ if (returned)
return 0;
}
if (!task_on_cpu(rq, p)) {
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy
2026-11-16 19:26 ` [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy Mathieu Desnoyers
@ 2026-11-16 21:12 ` John Stultz
2026-11-17 13:18 ` Mathieu Desnoyers
0 siblings, 1 reply; 21+ messages in thread
From: John Stultz @ 2026-11-16 21:12 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> With proxy execution, a task blocked on a mutex stays on its runqueue.
> The scheduler can pick it as the donor and run the mutex owner in its
> place. When the donor is woken, the CPU must stop running the owner on
> the donor's context.
>
> Commit 7918cf369361 ("sched/proxy: Only return migrate when needed")
> wakes a blocked task in place when it is still on its original CPU.
> ttwu_runnable() then relies on wakeup_preempt() to preempt the running
> task. But every class compares the woken task with rq->donor. When the
> woken task is the donor, it compares the task with itself, and no
> reschedule is requested. Only sched_ext handles this, with WF_TTWU_RQ.
>
> So the old owner keeps the donor's context after the mutex hand-off or
> after a signal. With a SCHED_FIFO donor nothing bounds this: the owner
> runs at the donor's priority until it blocks. With a SCHED_DEADLINE
> donor the owner consumes the donor's runtime.
>
> In a test on one CPU, a SCHED_OTHER owner kept spinning in user space
> after it released the mutex. A SCHED_FIFO waiter then ran 800ms late. A
> SCHED_DEADLINE waiter ran one period late.
>
> The same would happen when a task in the middle of a chain is woken.
> proxy_needs_return() resets the donor only when the woken task is the
> donor.
>
> Fix this in ttwu_runnable(). When a blocked task is woken on a runqueue
> that runs a proxy, reschedule. The next pick walks the chain again. A
> blocked task outside the donor's chain causes a harmless reschedule
> too.
>
> Fixes: 7918cf369361 ("sched/proxy: Only return migrate when needed")
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> ---
> kernel/sched/core.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index dc270a23179d..326026ba0b2b 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -4057,9 +4057,20 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags)
>
> update_rq_clock(rq);
> if (p->is_blocked) {
> + bool returned;
> +
> if (p->se.sched_delayed)
> enqueue_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_DELAYED);
> - if (proxy_needs_return(rq, p))
> + returned = proxy_needs_return(rq, p);
> + /*
> + * @p was blocked and this CPU runs a proxy. If @p is the donor
> + * or a task of its chain, the donor no longer waits for the
> + * task that runs. wakeup_preempt() compares @p with the donor,
> + * so it does not see this. Pick again.
> + */
> + if (rq->donor != rq->curr)
> + resched_curr(rq);
> + if (returned)
> return 0;
I need to think on this some more, but my gut reaction is this feels a
bit too subtle or possibly too broad. Particularly the check being
done after proxy_needs_return, but before the exit, it feels like it's
leaning too much on a non-obvious side-effect of proxy_needs_return().
Both the comment and the commit message are also confusing, as if at
the start of ttwu_runnable() if p is the donor, proxy_needs_return()
will have called proxy_reset_donor(), setting rq->donor = rq->curr
(and already calling resched_curr()).
So it seems this patch is mainly trying to handle the case of a
mid-chain wakeup, where p isn't the current donor, but is in the
current chain? I agree that looks to be a problem, but the donor !=
curr check just is testing that we are proxying, not that p is in the
active chain. It could be previously blocked on a different unselected
chain. That said, this may be a good enough approximation (especially
as doing the donor->curr chain walk feels expensive).
Though it feels like maybe the check should be pushed into
proxy_needs_return() instead so it can be more clearly aligned with
the similar proxy_reset_donor() call?
Or am I missing something more?
thanks
-john
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy
2026-11-16 21:12 ` John Stultz
@ 2026-11-17 13:18 ` Mathieu Desnoyers
0 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 13:18 UTC (permalink / raw)
To: John Stultz
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, arighi, akpm, shuah, linux-kselftest
On 2026-11-16 16:12, John Stultz wrote:
> Both the comment and the commit message are also confusing, as if at
> the start of ttwu_runnable() if p is the donor, proxy_needs_return()
> will have called proxy_reset_donor(), setting rq->donor = rq->curr
> (and already calling resched_curr()).
Only when the donor was proxy-migrated. When the donor is still on its
own CPU, task_cpu(p) == p->wake_cpu. proxy_needs_return() then returns
false at its first check, before proxy_reset_donor(). That is the case
the commit message describes: the owner was on the donor's CPU, so no
proxy migration happened. handoff_fifo hits it on one CPU.
Once the shortcut is gone, as you propose for patch 2, the donor goes
through proxy_reset_donor(). Then this patch only matters for a task
in the middle of the chain.
> So it seems this patch is mainly trying to handle the case of a
> mid-chain wakeup, where p isn't the current donor, but is in the
> current chain? I agree that looks to be a problem, but the donor !=
> curr check just is testing that we are proxying, not that p is in the
> active chain.
Yes. All the tasks of the running chain are on this runqueue:
find_proxy_task() migrates the donor when an owner is on another CPU.
So a woken chain member is always seen here. A blocked task outside the
chain also triggers the reschedule. I found no cheap test for chain
membership.
> Though it feels like maybe the check should be pushed into
> proxy_needs_return() instead so it can be more clearly aligned with
> the similar proxy_reset_donor() call?
Agreed. Without the shortcut, this would be an else of the
task_current_donor() check:
if (task_current_donor(rq, p))
proxy_reset_donor(rq);
else if (rq->donor != rq->curr)
resched_curr(rq);
I tried this on your proxy-exec-v33-7.4-pre-rc1 branch: no shortcut,
and this else branch. Cases 1 to 4 then pass, in 3 runs of 3. No case
wakes a task in the middle of the chain, so the else branch itself is
not tested.
Your suggestion on the cover letter also fits. On tip, a task woken
from a delayed dequeue is never in the running chain: find_proxy_task()
stops at a delayed owner. So the reschedule can be skipped for it.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
^ permalink raw reply [flat|nested] 21+ messages in thread
* [RFC PATCH v1 2/8] sched/proxy: Wake blocked RT and deadline tasks through the full path
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
2026-11-16 19:26 ` [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 21:35 ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 3/8] sched/proxy: Stop the chain walk when the blocked_on chain is a cycle Mathieu Desnoyers
` (7 subsequent siblings)
9 siblings, 1 reply; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
With proxy execution, a task blocked on a mutex stays on its runqueue.
The RT and deadline classes do not put such a task on their pushable
lists. enqueue_task_rt() and put_prev_task_rt() skip a blocked task.
enqueue_task_dl() and put_prev_task_dl() do the same. find_proxy_task()
migrates it instead.
Commit 7918cf369361 ("sched/proxy: Only return migrate when needed")
wakes a blocked task in place when it is still on its original CPU.
That wake-up does no enqueue. It also skips select_task_rq() and the
task_woken() method of the class. So the woken task is runnable, but it
is not pushable, and no other CPU can pull it.
A SCHED_FIFO task woken this way waits behind a higher priority task
while another CPU is idle. In a test on two CPUs, it ran 500ms after
the mutex was released, when the higher priority task stopped. Without
proxy execution it ran at once on the idle CPU. The test uses
SCHED_FIFO. The deadline class has the same code path.
Fix this in proxy_needs_return(). An RT or deadline task takes the full
wake-up path, as a task that was proxy-migrated does. The other classes
keep the in-place wake-up.
Fixes: 7918cf369361 ("sched/proxy: Only return migrate when needed")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
kernel/sched/core.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 326026ba0b2b..cededdcfd5a3 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3953,8 +3953,15 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)
* only set p->wake_cpu when !p->on_rq, and since here p->on_rq, this
* will not apply. But if it did, this check is the safe way around
* and would migrate.
- */
- if (task_cpu(p) == p->wake_cpu)
+ *
+ * The RT and deadline classes place a task at wake-up:
+ * select_task_rq(), the pushable list, a push to another CPU. An
+ * in-place wake-up skips all of that. So their tasks take the full
+ * wake-up path too.
+ */
+ if (task_cpu(p) == p->wake_cpu &&
+ p->sched_class != &rt_sched_class &&
+ p->sched_class != &dl_sched_class)
return false;
scoped_guard(raw_spinlock, &p->blocked_lock) {
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 2/8] sched/proxy: Wake blocked RT and deadline tasks through the full path
2026-11-16 19:26 ` [RFC PATCH v1 2/8] sched/proxy: Wake blocked RT and deadline tasks through the full path Mathieu Desnoyers
@ 2026-11-16 21:35 ` John Stultz
2026-11-17 13:19 ` Mathieu Desnoyers
0 siblings, 1 reply; 21+ messages in thread
From: John Stultz @ 2026-11-16 21:35 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> With proxy execution, a task blocked on a mutex stays on its runqueue.
> The RT and deadline classes do not put such a task on their pushable
> lists. enqueue_task_rt() and put_prev_task_rt() skip a blocked task.
> enqueue_task_dl() and put_prev_task_dl() do the same. find_proxy_task()
> migrates it instead.
>
> Commit 7918cf369361 ("sched/proxy: Only return migrate when needed")
> wakes a blocked task in place when it is still on its original CPU.
> That wake-up does no enqueue. It also skips select_task_rq() and the
> task_woken() method of the class. So the woken task is runnable, but it
> is not pushable, and no other CPU can pull it.
>
> A SCHED_FIFO task woken this way waits behind a higher priority task
> while another CPU is idle. In a test on two CPUs, it ran 500ms after
> the mutex was released, when the higher priority task stopped. Without
> proxy execution it ran at once on the idle CPU. The test uses
> SCHED_FIFO. The deadline class has the same code path.
>
> Fix this in proxy_needs_return(). An RT or deadline task takes the full
> wake-up path, as a task that was proxy-migrated does. The other classes
> keep the in-place wake-up.
>
> Fixes: 7918cf369361 ("sched/proxy: Only return migrate when needed")
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
I need to look more closely here but this seems likely to overlap with
currently out-of-tree chain-migration patches that Conor came up with:
https://github.com/johnstultz-work/linux-dev/commit/80b820a921bf5058196314ab72f8446f5ca3bad6
https://github.com/johnstultz-work/linux-dev/commit/27f701c997a152cc831262b55a226d513fa6abab
https://github.com/johnstultz-work/linux-dev/commit/fd1c4d9c6df6183e5ad934851dbbaa31846619e1
https://github.com/johnstultz-work/linux-dev/commit/d0d409fad42196f4f41267593566c0e55f856d6d
(from my https://github.com/johnstultz-work/linux-dev/commits/proxy-exec-v33-7.4-pre-rc1/
branch)
So I'm not totally surprised to hear there's DL/RT balancing issues currently.
Also in another thread (which I can't find right now), a good point
was raised that some of the early return "shortcuts" that I've used
elsewhere aren't good for core-scheduling either since they can break
the cookie checks, so I wonder if a better fix here would be just
removing that (task_cpu(p) == p->wake_cpu) conditional all together?
thanks
-john
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 2/8] sched/proxy: Wake blocked RT and deadline tasks through the full path
2026-11-16 21:35 ` John Stultz
@ 2026-11-17 13:19 ` Mathieu Desnoyers
0 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 13:19 UTC (permalink / raw)
To: John Stultz
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On 2026-11-16 16:35, John Stultz wrote:
> Also in another thread (which I can't find right now), a good point
> was raised that some of the early return "shortcuts" that I've used
> elsewhere aren't good for core-scheduling either since they can break
> the cookie checks, so I wonder if a better fix here would be just
> removing that (task_cpu(p) == p->wake_cpu) conditional all together?
Yes, that is simpler. I kept the shortcut for the other classes only to
keep the optimization of commit 7918cf369361. If the cookie checks need
the full path too, I see no reason to keep it.
The reply to patch 1 has what this changes there.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
^ permalink raw reply [flat|nested] 21+ messages in thread
* [RFC PATCH v1 3/8] sched/proxy: Stop the chain walk when the blocked_on chain is a cycle
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
2026-11-16 19:26 ` [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy Mathieu Desnoyers
2026-11-16 19:26 ` [RFC PATCH v1 2/8] sched/proxy: Wake blocked RT and deadline tasks through the full path Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 20:36 ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 4/8] sched/proxy: Tell the class about a proxy migration Mathieu Desnoyers
` (6 subsequent siblings)
9 siblings, 1 reply; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
find_proxy_task() follows the blocked_on chain from the donor to a task
that can run. It holds the rq lock, with interrupts off. It has no bound
on the number of steps.
The walk relies on the chain having an end. The ww_mutex die and wound
paths clear blocked_on for that reason. Their comments say that circular
blocked_on relationships would not resolve otherwise. But two tasks that
take two plain mutexes in opposite orders deadlock for good. Once both
are on one runqueue, the walk goes around the cycle forever.
Without proxy execution, such a deadlock leaves two tasks asleep, and
the hung task detector can report them. With it, the CPU locks up with
its rq lock held. Other CPUs would then block on that lock. In a test
with two CPUs, the guest stopped without a message.
Fix this by detecting the cycle. The walk remembers the task it met at
its last step that is a power of two. When it meets that task again, it
stops the donation and deactivates the donor. The donor then sleeps on
its mutex, as without proxy execution.
Fixes: 7de9d4f94638 ("sched: Start blocked_on chain processing in find_proxy_task()")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
kernel/sched/core.c | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index cededdcfd5a3..24105eeeb3c7 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -7135,7 +7135,8 @@ static struct task_struct *
find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
__must_hold(__rq_lockp(rq))
{
- struct task_struct *owner = NULL;
+ struct task_struct *owner = NULL, *mark = NULL;
+ unsigned int steps = 0, mark_at = 1;
bool curr_in_chain = false;
int this_cpu = cpu_of(rq);
struct task_struct *p;
@@ -7145,6 +7146,29 @@ find_proxy_task(struct rq *rq, struct task_struct *donor, struct rq_flags *rf)
for (p = donor; p->is_blocked; p = owner) {
/* if its PROXY_WAKING, do return migration or run if current */
struct mutex *mutex = p->blocked_on;
+
+ /*
+ * The walk came back to a task it already met: the chain is
+ * a cycle. Either the tasks deadlock, or a ww_mutex cycle is
+ * about to be broken. Do not walk it forever with the rq lock
+ * held. Stop the donation: the donor then sleeps on its
+ * mutex, as it does without proxy execution.
+ *
+ * @mark is the task met at the last step that is a power of
+ * two. Once the walk is in a cycle, @mark is soon a task of
+ * that cycle, and the walk meets it again.
+ */
+ if (p == mark) {
+ if (curr_in_chain)
+ goto resched_idle;
+ p = donor;
+ clear_task_blocked_on(p, NULL);
+ goto deactivate;
+ }
+ if (++steps == mark_at) {
+ mark = p;
+ mark_at <<= 1;
+ }
if (!mutex) {
clear_task_blocked_on(p, mutex);
if (task_current(rq, p)) {
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 3/8] sched/proxy: Stop the chain walk when the blocked_on chain is a cycle
2026-11-16 19:26 ` [RFC PATCH v1 3/8] sched/proxy: Stop the chain walk when the blocked_on chain is a cycle Mathieu Desnoyers
@ 2026-11-16 20:36 ` John Stultz
0 siblings, 0 replies; 21+ messages in thread
From: John Stultz @ 2026-11-16 20:36 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> find_proxy_task() follows the blocked_on chain from the donor to a task
> that can run. It holds the rq lock, with interrupts off. It has no bound
> on the number of steps.
>
> The walk relies on the chain having an end. The ww_mutex die and wound
> paths clear blocked_on for that reason. Their comments say that circular
> blocked_on relationships would not resolve otherwise. But two tasks that
> take two plain mutexes in opposite orders deadlock for good. Once both
> are on one runqueue, the walk goes around the cycle forever.
>
> Without proxy execution, such a deadlock leaves two tasks asleep, and
> the hung task detector can report them. With it, the CPU locks up with
> its rq lock held. Other CPUs would then block on that lock. In a test
> with two CPUs, the guest stopped without a message.
>
> Fix this by detecting the cycle. The walk remembers the task it met at
> its last step that is a power of two. When it meets that task again, it
> stops the donation and deactivates the donor. The donor then sleeps on
> its mutex, as without proxy execution.
>
> Fixes: 7de9d4f94638 ("sched: Start blocked_on chain processing in find_proxy_task()")
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> ---
> kernel/sched/core.c | 26 +++++++++++++++++++++++++-
> 1 file changed, 25 insertions(+), 1 deletion(-)
So this looks like it duplicates the following two changes in my series:
https://lore.kernel.org/lkml/20261023220431.1106611-6-jstultz@google.com/
https://lore.kernel.org/lkml/20261023220431.1106611-7-jstultz@google.com/
And I've recently found there's a few other spots that we need to
bound chain walking (though I believe those additional cases are still
out of tree in the full proxy-exec series).
thanks
-john
^ permalink raw reply [flat|nested] 21+ messages in thread
* [RFC PATCH v1 4/8] sched/proxy: Tell the class about a proxy migration
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
` (2 preceding siblings ...)
2026-11-16 19:26 ` [RFC PATCH v1 3/8] sched/proxy: Stop the chain walk when the blocked_on chain is a cycle Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 20:42 ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating Mathieu Desnoyers
` (5 subsequent siblings)
9 siblings, 1 reply; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
find_proxy_task() moves a blocked donor to the runqueue of the mutex
owner. proxy_set_task_cpu() does this with __set_task_cpu(), so that it
can keep p->wake_cpu, the CPU the donor returns to.
But __set_task_cpu() skips what set_task_cpu() does around it:
- the migrate_task_rq() method of the class;
- the migration count and the perf event;
- the sched_migrate_task tracepoint.
For a fair task, the dequeue on the old runqueue detaches its load.
migrate_task_rq_fair() would then reset last_update_time, which makes
the enqueue on the new runqueue attach the load. Without that call, the
load is attached nowhere. When the donor is woken, the return migration
goes through set_task_cpu(). It removes that load from the owner's
runqueue, which never got it.
So each round trip of a donor lowers the load and the utilization of
the owner's runqueue. In a test, a CPU with two tasks that never sleep
showed a mean util_avg of 195, where 1024 is expected. schedutil would
then pick a low frequency for a CPU that is fully busy, and the load
balancer would see that CPU as nearly idle.
Fix this by doing in proxy_set_task_cpu() what set_task_cpu() does
around __set_task_cpu(). Tracers and perf now see a proxy migration as
a migration, and nr_migrations counts it.
migrate_task_rq_dl() moves the bandwidth of a task that migrates to
another root domain. "sched/deadline: Move a task's bandwidth to the
root domain it runs in" added that. A proxy migration must not do it:
the task does not run where it is queued. So make migrate_task_rq_dl()
leave the bandwidth of such a task in its root domain. If its wake-up
then picks a CPU of another root domain, the bandwidth moves at that
time.
Fixes: b049b81bdff6 ("sched: Handle blocked-waiter migration (and return migration)")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
kernel/sched/core.c | 13 +++++++++++++
kernel/sched/deadline.c | 8 +++++++-
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 24105eeeb3c7..5d82c343662f 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -6967,6 +6967,19 @@ static inline void proxy_set_task_cpu(struct task_struct *p, int cpu)
* was previously runnable.
*/
wake_cpu = p->wake_cpu;
+ /*
+ * Tell the class and the tracers about the migration, as
+ * set_task_cpu() does. The fair class then attaches the load of @p
+ * to the new runqueue. Without that, the return migration removes
+ * load that the runqueue never got.
+ */
+ trace_sched_migrate_task(p, cpu);
+ if (task_cpu(p) != cpu) {
+ if (p->sched_class->migrate_task_rq)
+ p->sched_class->migrate_task_rq(p, cpu);
+ p->se.nr_migrations++;
+ perf_event_task_migrate(p);
+ }
__set_task_cpu(p, cpu);
p->wake_cpu = wake_cpu;
}
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 3b62ee43c209..d14ce3615ba6 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -3030,8 +3030,14 @@ static void migrate_task_rq_dl(struct task_struct *p, int new_cpu)
* the bandwidth. A mask can hold CPUs of two root domains, and
* select_fallback_rq() can widen it. Move the bandwidth there, as
* dl_task_offline_migration() does for a throttled task.
+ *
+ * Not for a task that is blocked on a mutex and only queued for
+ * proxy execution. It does not run on the new CPU, so its bandwidth
+ * stays in its root domain. If its wake-up then picks a CPU of another
+ * root domain, the bandwidth moves at that time.
*/
- if (unlikely(dl_task_bw_elsewhere(p, new_cpu)))
+ if (unlikely(dl_task_bw_elsewhere(p, new_cpu)) &&
+ !(p->is_blocked && task_on_rq_migrating(p)))
dl_task_move_bw(p, new_cpu);
if (READ_ONCE(p->__state) != TASK_WAKING)
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 4/8] sched/proxy: Tell the class about a proxy migration
2026-11-16 19:26 ` [RFC PATCH v1 4/8] sched/proxy: Tell the class about a proxy migration Mathieu Desnoyers
@ 2026-11-16 20:42 ` John Stultz
2026-11-17 13:20 ` Mathieu Desnoyers
0 siblings, 1 reply; 21+ messages in thread
From: John Stultz @ 2026-11-16 20:42 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> find_proxy_task() moves a blocked donor to the runqueue of the mutex
> owner. proxy_set_task_cpu() does this with __set_task_cpu(), so that it
> can keep p->wake_cpu, the CPU the donor returns to.
>
> But __set_task_cpu() skips what set_task_cpu() does around it:
> - the migrate_task_rq() method of the class;
> - the migration count and the perf event;
> - the sched_migrate_task tracepoint.
>
> For a fair task, the dequeue on the old runqueue detaches its load.
> migrate_task_rq_fair() would then reset last_update_time, which makes
> the enqueue on the new runqueue attach the load. Without that call, the
> load is attached nowhere. When the donor is woken, the return migration
> goes through set_task_cpu(). It removes that load from the owner's
> runqueue, which never got it.
>
> So each round trip of a donor lowers the load and the utilization of
> the owner's runqueue. In a test, a CPU with two tasks that never sleep
> showed a mean util_avg of 195, where 1024 is expected. schedutil would
> then pick a low frequency for a CPU that is fully busy, and the load
> balancer would see that CPU as nearly idle.
>
> Fix this by doing in proxy_set_task_cpu() what set_task_cpu() does
> around __set_task_cpu(). Tracers and perf now see a proxy migration as
> a migration, and nr_migrations counts it.
>
> migrate_task_rq_dl() moves the bandwidth of a task that migrates to
> another root domain. "sched/deadline: Move a task's bandwidth to the
> root domain it runs in" added that. A proxy migration must not do it:
> the task does not run where it is queued. So make migrate_task_rq_dl()
> leave the bandwidth of such a task in its root domain. If its wake-up
> then picks a CPU of another root domain, the bandwidth moves at that
> time.
>
> Fixes: b049b81bdff6 ("sched: Handle blocked-waiter migration (and return migration)")
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> ---
> kernel/sched/core.c | 13 +++++++++++++
> kernel/sched/deadline.c | 8 +++++++-
> 2 files changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 24105eeeb3c7..5d82c343662f 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -6967,6 +6967,19 @@ static inline void proxy_set_task_cpu(struct task_struct *p, int cpu)
> * was previously runnable.
> */
> wake_cpu = p->wake_cpu;
> + /*
> + * Tell the class and the tracers about the migration, as
> + * set_task_cpu() does. The fair class then attaches the load of @p
> + * to the new runqueue. Without that, the return migration removes
> + * load that the runqueue never got.
> + */
> + trace_sched_migrate_task(p, cpu);
> + if (task_cpu(p) != cpu) {
> + if (p->sched_class->migrate_task_rq)
> + p->sched_class->migrate_task_rq(p, cpu);
> + p->se.nr_migrations++;
> + perf_event_task_migrate(p);
> + }
> __set_task_cpu(p, cpu);
> p->wake_cpu = wake_cpu;
> }
So I've got a pending fix from Yu-Ting in my tree (apologies for the
gerrit link, I've not cleaned up and pushed my tree out yet) that I
believe duplicates the bit above:
https://r.android.com/4410518
> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 3b62ee43c209..d14ce3615ba6 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -3030,8 +3030,14 @@ static void migrate_task_rq_dl(struct task_struct *p, int new_cpu)
> * the bandwidth. A mask can hold CPUs of two root domains, and
> * select_fallback_rq() can widen it. Move the bandwidth there, as
> * dl_task_offline_migration() does for a throttled task.
> + *
> + * Not for a task that is blocked on a mutex and only queued for
> + * proxy execution. It does not run on the new CPU, so its bandwidth
> + * stays in its root domain. If its wake-up then picks a CPU of another
> + * root domain, the bandwidth moves at that time.
> */
> - if (unlikely(dl_task_bw_elsewhere(p, new_cpu)))
> + if (unlikely(dl_task_bw_elsewhere(p, new_cpu)) &&
> + !(p->is_blocked && task_on_rq_migrating(p)))
> dl_task_move_bw(p, new_cpu);
>
> if (READ_ONCE(p->__state) != TASK_WAKING)
I may need to pull this bit in separately.
thanks!
-john
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 4/8] sched/proxy: Tell the class about a proxy migration
2026-11-16 20:42 ` John Stultz
@ 2026-11-17 13:20 ` Mathieu Desnoyers
0 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 13:20 UTC (permalink / raw)
To: John Stultz
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, arighi, akpm, shuah, linux-kselftest
On 2026-11-16 15:42, John Stultz wrote:
> So I've got a pending fix from Yu-Ting in my tree (apologies for the
> gerrit link, I've not cleaned up and pushed my tree out yet) that I
> believe duplicates the bit above:
> https://r.android.com/4410518
Fine with me. pelt_migration (case 5) passes on your tree, so please
drop the core.c part of this patch.
>> - if (unlikely(dl_task_bw_elsewhere(p, new_cpu)))
>> + if (unlikely(dl_task_bw_elsewhere(p, new_cpu)) &&
>> + !(p->is_blocked && task_on_rq_migrating(p)))
>> dl_task_move_bw(p, new_cpu);
>
> I may need to pull this bit in separately.
You should not need it. On tip, migrate_task_rq_dl() returns at once
for a task that is not TASK_WAKING. So a proxy migration that calls it
does nothing there. Only my deadline series makes it move bandwidth, so
I will carry this check in that series.
The check leaves the bandwidth where it is. task_woken_dl() moves it
when the task wakes in another root domain. That needs the full wake-up
path for deadline tasks, which dropping the shortcut of patch 2 keeps.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
^ permalink raw reply [flat|nested] 21+ messages in thread
* [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
` (3 preceding siblings ...)
2026-11-16 19:26 ` [RFC PATCH v1 4/8] sched/proxy: Tell the class about a proxy migration Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 23:16 ` John Stultz
2026-11-16 19:26 ` [RFC PATCH v1 6/8] sched/deadline: Use the root domain a proxy-migrated task runs in Mathieu Desnoyers
` (4 subsequent siblings)
9 siblings, 1 reply; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
find_proxy_task() sets owner->blocked_donor to the task that donates to
the owner. __mutex_unlock_slowpath() reads current->blocked_donor to
hand the mutex to that task. The comment of the field says that it is
set in schedule() and only stable under preempt_disable(). So the
pointer is valid until the owner schedules again.
But the donor can stop donating while the owner keeps running. It is
woken, or a change of its scheduling class or of its affinity takes it
off the runqueue. The owner keeps its blocked_donor until it schedules.
Meanwhile the donor can run on another CPU and exit.
This would be a use after free: __mutex_unlock_slowpath() would take
the blocked_lock of a freed task. It was found by reading the code,
and has not been reproduced. The owner must stay in the kernel without
scheduling for as long as the donor needs to exit and be freed.
Fix this by clearing rq->curr->blocked_donor when the task it points
at is woken, or leaves the runqueue in sched_proxy_block_task() or
move_queued_task(). The clear takes the blocked_lock of the running
task, which the unlock path holds while it uses the pointer.
Fixes: 1628b25248d0 ("sched: Add blocked_donor link to task for smarter mutex handoffs")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
kernel/sched/core.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5d82c343662f..de357acfec56 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2584,6 +2584,33 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
* is done.
*/
+#ifdef CONFIG_SCHED_PROXY_EXEC
+/*
+ * @p stops donating on @rq: it is woken, or it leaves the runqueue. The
+ * task that runs can still point at @p with blocked_donor until it
+ * schedules, and __mutex_unlock_slowpath() dereferences that pointer.
+ * Meanwhile @p can run on another CPU and exit. So clear the pointer.
+ * The unlock path reads it under the blocked_lock of the running task.
+ */
+static inline void proxy_clear_curr_donor(struct rq *rq, struct task_struct *p)
+{
+ struct task_struct *curr = rq->curr;
+
+ lockdep_assert_rq_held(rq);
+
+ if (READ_ONCE(curr->blocked_donor) != p)
+ return;
+
+ guard(raw_spinlock)(&curr->blocked_lock);
+ if (curr->blocked_donor == p)
+ curr->blocked_donor = NULL;
+}
+#else /* !CONFIG_SCHED_PROXY_EXEC */
+static inline void proxy_clear_curr_donor(struct rq *rq, struct task_struct *p)
+{
+}
+#endif /* !CONFIG_SCHED_PROXY_EXEC */
+
/*
* move_queued_task - move a queued task to new rq.
*
@@ -2595,6 +2622,7 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
{
lockdep_assert_rq_held(rq);
+ proxy_clear_curr_donor(rq, p);
deactivate_task(rq, p, DEQUEUE_NOCLOCK);
set_task_cpu(p, new_cpu);
rq_unlock(rq, rf);
@@ -3995,6 +4023,7 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)
{
return false;
}
+
#endif /* CONFIG_SCHED_PROXY_EXEC */
static void
@@ -4068,6 +4097,7 @@ static int ttwu_runnable(struct task_struct *p, int wake_flags)
if (p->se.sched_delayed)
enqueue_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_DELAYED);
+ proxy_clear_curr_donor(rq, p);
returned = proxy_needs_return(rq, p);
/*
* @p was blocked and this CPU runs a proxy. If @p is the donor
@@ -7031,6 +7061,7 @@ void sched_proxy_block_task(struct rq *rq, struct task_struct *p)
if (WARN_ON_ONCE(state == TASK_RUNNING))
return;
+ proxy_clear_curr_donor(rq, p);
if (task_current_donor(rq, p))
proxy_reset_donor(rq);
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating
2026-11-16 19:26 ` [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating Mathieu Desnoyers
@ 2026-11-16 23:16 ` John Stultz
2026-11-17 13:22 ` Mathieu Desnoyers
0 siblings, 1 reply; 21+ messages in thread
From: John Stultz @ 2026-11-16 23:16 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> find_proxy_task() sets owner->blocked_donor to the task that donates to
> the owner. __mutex_unlock_slowpath() reads current->blocked_donor to
> hand the mutex to that task. The comment of the field says that it is
> set in schedule() and only stable under preempt_disable(). So the
> pointer is valid until the owner schedules again.
>
> But the donor can stop donating while the owner keeps running. It is
> woken, or a change of its scheduling class or of its affinity takes it
> off the runqueue. The owner keeps its blocked_donor until it schedules.
> Meanwhile the donor can run on another CPU and exit.
>
> This would be a use after free: __mutex_unlock_slowpath() would take
> the blocked_lock of a freed task. It was found by reading the code,
> and has not been reproduced. The owner must stay in the kernel without
> scheduling for as long as the donor needs to exit and be freed.
>
> Fix this by clearing rq->curr->blocked_donor when the task it points
> at is woken, or leaves the runqueue in sched_proxy_block_task() or
> move_queued_task(). The clear takes the blocked_lock of the running
> task, which the unlock path holds while it uses the pointer.
>
> Fixes: 1628b25248d0 ("sched: Add blocked_donor link to task for smarter mutex handoffs")
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Again, I'll have to spend a bit more time looking at this one. I've
been looking at some task->blocked_donor lifetime concerns recently,
so I agree its definitely an issue.
The blocked_donor is used for mutex handoff, and whole-chain proxy
migration. The first only needs the curr->blocked_donor to be correct
and never traverses further back down the chain, where as the second
is used to migrate the entire current chain. Since the while-chain
proxy migration happens in __schedule()->find_proxy_task() where the
blocked_donor is set, it seems like clearing the blocked_donors as we
enqueue them for migration is a good idea (and cheap since we're
already iterating down the chain), and I've got a patch I'm working on
for this already.
This patch's approach of of checking and clearing curr->blocked_donor,
a task wakeup sounds reasonable, since it would prevent the mutex
handoff from potentially reaching for a task that's exited. Using
proxy_needs_return() should ensure we catch tasks in the donor chain
on wakeup. My only worry is that the wakeup and clearing might trip
while a proxy-chain is being migrated (and we don't want unexpected
writes breaking the chain), but I think we're ok because we hold the
rq lock as we walk blocked_donor and create the whole chain migration
list, and we shouldn't need blocked_donor after that. So, I think it
seems ok.
That said, Its not immediately obvious to me why clearing on
move_queued_task() is needed? Mostly because: why would a proxy
blocked task be migrated before wakeup? If a proxy-blocked task were
migrated away, __schedule() would just proxy migrate it back to the
current lock owner's rq.
thanks
-john
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating
2026-11-16 23:16 ` John Stultz
@ 2026-11-17 13:22 ` Mathieu Desnoyers
0 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 13:22 UTC (permalink / raw)
To: John Stultz
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On 2026-11-16 18:16, John Stultz wrote:
> That said, Its not immediately obvious to me why clearing on
> move_queued_task() is needed? Mostly because: why would a proxy
> blocked task be migrated before wakeup? If a proxy-blocked task were
> migrated away, __schedule() would just proxy migrate it back to the
> current lock owner's rq.
Another task can move it. For example, sched_setaffinity() on the
donor: affine_move_task() calls move_queued_task() for a task that is
queued and not running. The donor is then queued on another runqueue.
If it is woken there, the clear at wake-up runs on that runqueue. It
does not see the owner. The owner still runs on the old CPU, with
blocked_donor pointing at the donor. Your proxy_needs_return()
clear has the same limit. The proxy migration back that you describe
only happens if the new CPU picks the donor before it is woken.
> as you mentioned there's no test case in your test for this logic.
Right. It was found by reading, and I did not try to reproduce it. The
owner would have to stay in the kernel without scheduling while the
donor exits and is freed.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
^ permalink raw reply [flat|nested] 21+ messages in thread
* [RFC PATCH v1 6/8] sched/deadline: Use the root domain a proxy-migrated task runs in
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
` (4 preceding siblings ...)
2026-11-16 19:26 ` [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 19:26 ` [RFC PATCH v1 7/8] lib: Add test_proxy_exec, kernel mutexes that user space can hold Mathieu Desnoyers
` (3 subsequent siblings)
9 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
With proxy execution, find_proxy_task() can queue a task blocked on a
mutex on the runqueue of the mutex owner. It ignores the affinity of
the task, which does not run there. The task returns to p->wake_cpu when
it is woken.
__sched_setscheduler() checks that the affinity of a task covers its
root domain before it lets the task use SCHED_DEADLINE. It takes the
root domain from the runqueue the task is on. sched_dl_overflow() admits
the bandwidth of a task that enters the class in the root domain of
task_cpu(). Both predate proxy execution, when a task was always queued
on a CPU it can run on.
When the mutex owner is in another root domain, both use the wrong one.
sched_setattr() fails with EPERM for a deadline task that waits for the
mutex, and for a task that asks for SCHED_DEADLINE while it waits. In a
test with two cpuset partitions, both calls failed. Without proxy
execution they work.
Fix this by adding task_home_cpu(), which returns p->wake_cpu for such a
task, and by using its root domain in both places. An affinity change
or a CPU hotplug can leave p->wake_cpu stale while the task is queued
elsewhere. task_home_cpu() then returns an active CPU of the task's
mask.
Fixes: b049b81bdff6 ("sched: Handle blocked-waiter migration (and return migration)")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
kernel/sched/deadline.c | 2 +-
kernel/sched/sched.h | 23 +++++++++++++++++++++++
kernel/sched/syscalls.c | 9 +++++++--
3 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index d14ce3615ba6..c55967135df0 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -4625,7 +4625,7 @@ int sched_dl_overflow(struct task_struct *p, int policy,
if (new_bw == p->dl.dl_bw && counted)
return 0;
- cpu = counted && p->dl.dl_bw ? p->dl.dl_bw_cpu : task_cpu(p);
+ cpu = counted && p->dl.dl_bw ? p->dl.dl_bw_cpu : task_home_cpu(p);
dl_b = dl_bw_of(cpu);
/*
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index f80ea5541b3f..7f5e45a86285 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2585,6 +2585,29 @@ static inline int task_on_rq_queued(struct task_struct *p)
return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
}
+/*
+ * Return the CPU where @p runs, or will run. Proxy execution can queue a
+ * task blocked on a mutex on the CPU of the mutex owner. The task does
+ * not run there. It returns to p->wake_cpu when it is woken. The caller
+ * holds the rq lock of @p.
+ */
+static inline int task_home_cpu(struct task_struct *p)
+{
+ int cpu = p->wake_cpu;
+
+ if (!sched_proxy_exec() || !p->is_blocked || !task_on_rq_queued(p))
+ return task_cpu(p);
+
+ /*
+ * An affinity change or a CPU hotplug can leave wake_cpu stale
+ * while the task is queued elsewhere.
+ */
+ if (cpumask_test_cpu(cpu, p->cpus_ptr) && cpu_active(cpu))
+ return cpu;
+ cpu = cpumask_any_and(p->cpus_ptr, cpu_active_mask);
+ return cpu < nr_cpu_ids ? cpu : task_cpu(p);
+}
+
static inline int task_on_rq_migrating(struct task_struct *p)
{
return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index 59d78ae6b9fd..a88a6ddc500e 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -647,7 +647,12 @@ int __sched_setscheduler(struct task_struct *p,
#endif /* CONFIG_RT_GROUP_SCHED */
if (dl_bandwidth_enabled() && dl_policy(policy) &&
!(attr->sched_flags & SCHED_FLAG_SUGOV)) {
- cpumask_t *span = rq->rd->span;
+ /*
+ * Use the root domain @p runs in. Proxy execution can
+ * have queued it in the root domain of a mutex owner.
+ */
+ struct root_domain *rd = cpu_rq(task_home_cpu(p))->rd;
+ cpumask_t *span = rd->span;
/*
* Don't allow tasks with an affinity mask smaller than
@@ -655,7 +660,7 @@ int __sched_setscheduler(struct task_struct *p,
* will also fail if there's no bandwidth available.
*/
if (!cpumask_subset(span, &p->cpus_mask) ||
- rq->rd->dl_bw.bw == 0) {
+ rd->dl_bw.bw == 0) {
retval = -EPERM;
goto unlock;
}
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* [RFC PATCH v1 7/8] lib: Add test_proxy_exec, kernel mutexes that user space can hold
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
` (5 preceding siblings ...)
2026-11-16 19:26 ` [RFC PATCH v1 6/8] sched/deadline: Use the root domain a proxy-migrated task runs in Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 19:26 ` [RFC PATCH v1 8/8] selftests/sched: Add tests of proxy execution Mathieu Desnoyers
` (2 subsequent siblings)
9 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
Proxy execution acts on tasks blocked on a struct mutex. To test it,
user space must block tasks on a kernel mutex in a chosen way:
- hold the mutex for a chosen time;
- block on it from a chosen task and CPU;
- take two mutexes in opposite orders.
No system call gives that control. The mutexes that system calls take
are held for a short time that user space does not choose. Their
locking can change with the subsystem. And user space cannot make two
tasks deadlock on two kernel mutexes.
Add a test module with eight mutexes and a debugfs file. A write to the
file runs one command:
- hold a mutex while busy or asleep;
- take a mutex, interruptibly or not;
- take two mutexes, one inside the other. The inner lock is killable,
so that a test can end a deadlock it made.
The write returns when the command is done.
The selftest added by the next patch uses it.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
lib/Kconfig.debug | 11 ++++
lib/Makefile | 1 +
lib/test_proxy_exec.c | 132 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 144 insertions(+)
create mode 100644 lib/test_proxy_exec.c
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2d22638ec13a..8d032d7fa387 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1361,6 +1361,17 @@ config TEST_CSD_LOCK
If unsure, say N.
+config TEST_PROXY_EXEC
+ tristate "Test module for proxy execution"
+ depends on DEBUG_FS
+ help
+ This builds the "test_proxy_exec" module. It creates kernel
+ mutexes that user space can hold, take and chain through
+ debugfs. The selftests in tools/testing/selftests/sched/proxy_exec
+ use it to block tasks on a mutex in a chosen way.
+
+ If unsure, say N.
+
config TEST_LOCKUP
tristate "Test module to generate lockups"
depends on m
diff --git a/lib/Makefile b/lib/Makefile
index a2afe954805c..8add2a1473df 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -101,6 +101,7 @@ obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o
obj-$(CONFIG_TEST_CSD_LOCK) += test_csd_lock.o
obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o
+obj-$(CONFIG_TEST_PROXY_EXEC) += test_proxy_exec.o
obj-$(CONFIG_TEST_HMM) += test_hmm.o
obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o
obj-$(CONFIG_TEST_REF_TRACKER) += test_ref_tracker.o
diff --git a/lib/test_proxy_exec.c b/lib/test_proxy_exec.c
new file mode 100644
index 000000000000..d3f7af3222e8
--- /dev/null
+++ b/lib/test_proxy_exec.c
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test module for proxy execution: kernel mutexes that user space can
+ * hold, take and chain.
+ *
+ * Proxy execution acts on tasks blocked on a struct mutex. User space
+ * cannot hold a kernel mutex for a chosen time, nor make two tasks take
+ * two of them in a chosen order. This module lets it do both. See
+ * tools/testing/selftests/sched/proxy_exec.
+ *
+ * Write one command to <debugfs>/test_proxy_exec/op:
+ * hold I MS lock mutex I, busy-wait MS ms, unlock
+ * hold_sleep I MS lock mutex I, sleep MS ms, unlock
+ * take I lock mutex I, unlock
+ * take_intr I mutex_lock_interruptible(I), unlock if acquired
+ * pair A B MS lock A, busy-wait MS ms, lock B killable, unlock both
+ * The write returns when the command is done. It fails with EINTR if a
+ * signal interrupted take_intr or the inner lock of pair.
+ *
+ * Two tasks that run pair with the mutexes in opposite orders deadlock.
+ * A fatal signal ends the deadlock.
+ */
+#include <linux/debugfs.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/ktime.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/uaccess.h>
+
+#define TPE_NR_MUTEX 8
+#define TPE_MAX_MS 10000
+
+static struct mutex tpe_mutex[TPE_NR_MUTEX];
+/* One lockdep class per mutex: "pair" nests them. */
+static struct lock_class_key tpe_key[TPE_NR_MUTEX];
+static struct dentry *tpe_dir;
+
+/*
+ * Busy-wait @ms milliseconds. The caller can be preempted. cond_resched()
+ * gives a kernel without preemption a place to do it.
+ */
+static void tpe_busy_wait(unsigned int ms)
+{
+ ktime_t end = ktime_add_ms(ktime_get(), ms);
+
+ while (ktime_before(ktime_get(), end)) {
+ cpu_relax();
+ cond_resched();
+ }
+}
+
+static ssize_t tpe_op_write(struct file *file, const char __user *ubuf,
+ size_t len, loff_t *ppos)
+{
+ unsigned int a, b, ms = 0;
+ char buf[64];
+ int ret = 0;
+
+ if (len >= sizeof(buf))
+ return -EINVAL;
+ if (copy_from_user(buf, ubuf, len))
+ return -EFAULT;
+ buf[len] = '\0';
+
+ if (sscanf(buf, "hold_sleep %u %u", &a, &ms) == 2) {
+ if (a >= TPE_NR_MUTEX || ms > TPE_MAX_MS)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ msleep(ms);
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "hold %u %u", &a, &ms) == 2) {
+ if (a >= TPE_NR_MUTEX || ms > TPE_MAX_MS)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ tpe_busy_wait(ms);
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "take_intr %u", &a) == 1) {
+ if (a >= TPE_NR_MUTEX)
+ return -EINVAL;
+ ret = mutex_lock_interruptible(&tpe_mutex[a]);
+ if (!ret)
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "take %u", &a) == 1) {
+ if (a >= TPE_NR_MUTEX)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ mutex_unlock(&tpe_mutex[a]);
+ } else if (sscanf(buf, "pair %u %u %u", &a, &b, &ms) == 3) {
+ if (a >= TPE_NR_MUTEX || b >= TPE_NR_MUTEX || a == b ||
+ ms > TPE_MAX_MS)
+ return -EINVAL;
+ mutex_lock(&tpe_mutex[a]);
+ tpe_busy_wait(ms);
+ ret = mutex_lock_killable(&tpe_mutex[b]);
+ if (!ret)
+ mutex_unlock(&tpe_mutex[b]);
+ mutex_unlock(&tpe_mutex[a]);
+ } else {
+ return -EINVAL;
+ }
+ return ret ? ret : len;
+}
+
+static const struct file_operations tpe_op_fops = {
+ .owner = THIS_MODULE,
+ .write = tpe_op_write,
+ .llseek = noop_llseek,
+};
+
+static int __init tpe_init(void)
+{
+ int i;
+
+ for (i = 0; i < TPE_NR_MUTEX; i++) {
+ mutex_init(&tpe_mutex[i]);
+ lockdep_set_class(&tpe_mutex[i], &tpe_key[i]);
+ }
+ tpe_dir = debugfs_create_dir("test_proxy_exec", NULL);
+ debugfs_create_file("op", 0200, tpe_dir, NULL, &tpe_op_fops);
+ return 0;
+}
+module_init(tpe_init);
+
+static void __exit tpe_exit(void)
+{
+ debugfs_remove_recursive(tpe_dir);
+}
+module_exit(tpe_exit);
+
+MODULE_DESCRIPTION("Kernel mutexes for the proxy execution selftests");
+MODULE_LICENSE("GPL");
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* [RFC PATCH v1 8/8] selftests/sched: Add tests of proxy execution
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
` (6 preceding siblings ...)
2026-11-16 19:26 ` [RFC PATCH v1 7/8] lib: Add test_proxy_exec, kernel mutexes that user space can hold Mathieu Desnoyers
@ 2026-11-16 19:26 ` Mathieu Desnoyers
2026-11-16 20:27 ` [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests John Stultz
2026-11-17 6:28 ` John Stultz
9 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-16 19:26 UTC (permalink / raw)
To: jstultz, peterz, mingo, juri.lelli, vincent.guittot
Cc: linux-kernel, Mathieu Desnoyers, dietmar.eggemann, rostedt,
bsegall, mgorman, vschneid, kprateek.nayak, connoro, arighi,
akpm, shuah, linux-kselftest
Add proxy_exec_test. It blocks tasks on the mutexes of the
test_proxy_exec module. It checks when the waiter runs, and what the
runqueue and the root domain of the owner count.
It skips without the module.
These cases fail with proxy execution, and pass with the fix named for
each:
- handoff_fifo, handoff_deadline and signal_fifo: "sched/proxy:
Reschedule when a blocked task is woken under a proxy"
- pushable_fifo: "sched/proxy: Wake blocked RT and deadline tasks
through the full path"
- pelt_migration: "sched/proxy: Tell the class about a proxy migration"
- deadlock_cycle: "sched/proxy: Stop the chain walk when the blocked_on
chain is a cycle"
- dl_setattr_moved and dl_admit_moved: "sched/deadline: Use the root
domain a proxy-migrated task runs in"
All of them pass with sched_proxy_exec=0.
deadlock_cycle makes two tasks deadlock on two kernel mutexes, then
kills them. A kernel without the fix locks up. So it runs only with
PROXY_EXEC_TEST_DEADLOCK=1.
The last two cases block a task of one cpuset partition on a mutex held
in another one. Proxy execution then queues it on a CPU of the other
root domain. sched_setattr() must work on it there, and its bandwidth
must be counted in the root domain of its own cpuset.
A waiter spins while the mutex owner runs on another CPU. It blocks
only when the owner is preempted. The cases with several CPUs arrange
that.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
tools/testing/selftests/Makefile | 1 +
.../selftests/sched/proxy_exec/.gitignore | 1 +
.../selftests/sched/proxy_exec/Makefile | 7 +
.../testing/selftests/sched/proxy_exec/config | 5 +
.../sched/proxy_exec/proxy_exec_test.c | 913 ++++++++++++++++++
.../selftests/sched/proxy_exec/settings | 1 +
6 files changed, 928 insertions(+)
create mode 100644 tools/testing/selftests/sched/proxy_exec/.gitignore
create mode 100644 tools/testing/selftests/sched/proxy_exec/Makefile
create mode 100644 tools/testing/selftests/sched/proxy_exec/config
create mode 100644 tools/testing/selftests/sched/proxy_exec/proxy_exec_test.c
create mode 100644 tools/testing/selftests/sched/proxy_exec/settings
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 35b0c5e86ba7..73c75d289090 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -121,6 +121,7 @@ TARGETS += rseq
TARGETS += rtc
TARGETS += rust
TARGETS += sched/deadline
+TARGETS += sched/proxy_exec
TARGETS += sched_ext
TARGETS += seccomp
TARGETS += sgx
diff --git a/tools/testing/selftests/sched/proxy_exec/.gitignore b/tools/testing/selftests/sched/proxy_exec/.gitignore
new file mode 100644
index 000000000000..335ddcf0127b
--- /dev/null
+++ b/tools/testing/selftests/sched/proxy_exec/.gitignore
@@ -0,0 +1 @@
+proxy_exec_test
diff --git a/tools/testing/selftests/sched/proxy_exec/Makefile b/tools/testing/selftests/sched/proxy_exec/Makefile
new file mode 100644
index 000000000000..bbc04a47ede2
--- /dev/null
+++ b/tools/testing/selftests/sched/proxy_exec/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+CFLAGS += -O2 -Wall -g -I./ $(KHDR_INCLUDES)
+
+TEST_GEN_PROGS := proxy_exec_test
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/sched/proxy_exec/config b/tools/testing/selftests/sched/proxy_exec/config
new file mode 100644
index 000000000000..9a50fb341a74
--- /dev/null
+++ b/tools/testing/selftests/sched/proxy_exec/config
@@ -0,0 +1,5 @@
+CONFIG_CGROUPS=y
+CONFIG_CPUSETS=y
+CONFIG_DEBUG_FS=y
+CONFIG_SCHED_PROXY_EXEC=y
+CONFIG_TEST_PROXY_EXEC=m
diff --git a/tools/testing/selftests/sched/proxy_exec/proxy_exec_test.c b/tools/testing/selftests/sched/proxy_exec/proxy_exec_test.c
new file mode 100644
index 000000000000..d2a6e329c685
--- /dev/null
+++ b/tools/testing/selftests/sched/proxy_exec/proxy_exec_test.c
@@ -0,0 +1,913 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Tests of proxy execution (CONFIG_SCHED_PROXY_EXEC).
+ *
+ * With proxy execution, a task blocked on a mutex stays on its runqueue
+ * and lends its scheduling context to the mutex owner. These cases block
+ * tasks on the mutexes of the test_proxy_exec module, and check what the
+ * waiter and the owner get.
+ *
+ * Every case must also pass without proxy execution (sched_proxy_exec=0).
+ *
+ * The deadlock_cycle case makes two tasks deadlock on two kernel mutexes,
+ * then kills them. A kernel without the cycle check locks up. So the case
+ * runs only with PROXY_EXEC_TEST_DEADLOCK=1.
+ *
+ * Needs root, CONFIG_TEST_PROXY_EXEC and debugfs. The cases with a
+ * deadline task also need cgroup v2 with the cpuset controller.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "../../kselftest.h"
+
+#define OP_FILE "/sys/kernel/debug/test_proxy_exec/op"
+#define SCHED_DEBUG "/sys/kernel/debug/sched/debug"
+#define CGROUP "/sys/fs/cgroup"
+#define CG_A CGROUP "/proxy_exec_test_a"
+#define CG_B CGROUP "/proxy_exec_test_b"
+#define AUTOGROUP "/proc/sys/kernel/sched_autogroup_enabled"
+#define MS 1000000ULL
+/*
+ * A waiter that runs this long after it could have is late. In testing,
+ * a waiter ran less than 1ms late with the fixes, and 28ms or more
+ * without them.
+ */
+#define LATE_US 10000
+
+#ifndef SCHED_DEADLINE
+#define SCHED_DEADLINE 6
+#endif
+
+/* Exit codes of a case, and of its children. */
+#define CASE_PASS 0
+#define CASE_FAIL 1
+#define CASE_SKIP KSFT_SKIP
+
+struct dl_attr {
+ uint32_t size;
+ uint32_t sched_policy;
+ uint64_t sched_flags;
+ int32_t sched_nice;
+ uint32_t sched_priority;
+ uint64_t sched_runtime;
+ uint64_t sched_deadline;
+ uint64_t sched_period;
+};
+
+/* Shared between the processes of a case. */
+struct shared {
+ uint64_t t_unlock; /* owner: after its hold returned */
+ uint64_t t_got; /* waiter: after its take returned */
+ uint64_t beat; /* heartbeat counter */
+ int ret;
+};
+
+static struct shared *sh;
+
+/* The children of the running case, and what must be undone after it. */
+static pid_t kids[8];
+static int nr_kids;
+static int in_child;
+static int child_failed;
+static int autogroup_old = -1;
+
+static int write_file(const char *path, const char *val);
+
+/* Kill and reap every child of the case that is still there. */
+static void stop_children(void)
+{
+ int i;
+
+ for (i = 0; i < nr_kids; i++) {
+ if (!kids[i])
+ continue;
+ kill(kids[i], SIGKILL);
+ waitpid(kids[i], NULL, 0);
+ kids[i] = 0;
+ }
+}
+
+/* Undo what the case changed in the system. Run by the case process. */
+static void cleanup(void)
+{
+ stop_children();
+ if (autogroup_old >= 0)
+ write_file(AUTOGROUP, autogroup_old ? "1" : "0");
+ autogroup_old = -1;
+}
+
+static uint64_t now(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+}
+
+/* Skip the case: it cannot do the setup step @what on this system. */
+static void skip(const char *what)
+{
+ ksft_print_msg("cannot %s: %s\n", what, strerror(errno));
+ if (!in_child)
+ cleanup();
+ exit(CASE_SKIP);
+}
+
+/* Sleep until @ms milliseconds after @t0. */
+static void sleep_until(uint64_t t0, unsigned int ms)
+{
+ uint64_t t = t0 + ms * MS;
+ struct timespec ts = { t / 1000000000ULL, t % 1000000000ULL };
+
+ while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR)
+ ;
+}
+
+static void spin_until(uint64_t t)
+{
+ while (now() < t)
+ ;
+}
+
+/* Pin @pid on @cpu0, and on @cpu1 too unless it is negative. */
+static void pin(pid_t pid, int cpu0, int cpu1)
+{
+ cpu_set_t set;
+
+ CPU_ZERO(&set);
+ CPU_SET(cpu0, &set);
+ if (cpu1 >= 0)
+ CPU_SET(cpu1, &set);
+ if (sched_setaffinity(pid, sizeof(set), &set))
+ skip("sched_setaffinity");
+}
+
+static void set_fifo(int prio)
+{
+ struct sched_param sp = { .sched_priority = prio };
+
+ if (sched_setscheduler(0, SCHED_FIFO, &sp))
+ skip("SCHED_FIFO");
+}
+
+static int set_deadline(pid_t pid, unsigned int runtime_ms, unsigned int period_ms)
+{
+ struct dl_attr attr = {
+ .size = sizeof(attr),
+ .sched_policy = SCHED_DEADLINE,
+ .sched_runtime = runtime_ms * MS,
+ .sched_deadline = period_ms * MS,
+ .sched_period = period_ms * MS,
+ };
+
+ return syscall(SYS_sched_setattr, pid, &attr, 0);
+}
+
+/* Send @cmd to the test module. Return 0, or a negative errno. */
+static int op(const char *cmd)
+{
+ int fd = open(OP_FILE, O_WRONLY), ret;
+
+ if (fd < 0)
+ skip("open " OP_FILE);
+ ret = write(fd, cmd, strlen(cmd));
+ ret = ret < 0 ? -errno : 0;
+ close(fd);
+ return ret;
+}
+
+/* Send @cmd to the test module. A child fails if the command does. */
+static void op_ok(const char *cmd)
+{
+ int ret = op(cmd);
+
+ if (ret) {
+ ksft_print_msg("%s: %s\n", cmd, strerror(-ret));
+ exit(CASE_FAIL);
+ }
+}
+
+static int write_file(const char *path, const char *val)
+{
+ int fd = open(path, O_WRONLY), ret;
+
+ if (fd < 0)
+ return -1;
+ ret = write(fd, val, strlen(val));
+ close(fd);
+ return ret < 0 ? -1 : 0;
+}
+
+/* Move the calling process into cgroup @cg. */
+static void join(const char *cg)
+{
+ char path[128], pid[32];
+
+ snprintf(path, sizeof(path), "%s/cgroup.procs", cg);
+ snprintf(pid, sizeof(pid), "%d", getpid());
+ if (write_file(path, pid))
+ skip("move into the cgroup");
+}
+
+/* Make cgroup @cg a cpuset partition root on @cpu. Skip if it cannot be. */
+static void partition(const char *cg, const char *cpu)
+{
+ char path[128];
+
+ write_file(CGROUP "/cgroup.subtree_control", "+cpuset");
+ if (mkdir(cg, 0755) && errno != EEXIST)
+ skip("create the cgroup");
+ snprintf(path, sizeof(path), "%s/cpuset.cpus", cg);
+ if (write_file(path, cpu))
+ skip("set the CPUs of the cgroup");
+ snprintf(path, sizeof(path), "%s/cpuset.cpus.partition", cg);
+ if (write_file(path, "root"))
+ skip("make the cgroup a partition root");
+ /* Let the root domains be rebuilt. */
+ usleep(200000);
+}
+
+/*
+ * Fork a child that dies with the case. Return its pid to the parent and
+ * 0 to the child. The case fails if it cannot fork.
+ */
+static pid_t child(void)
+{
+ pid_t pid;
+
+ fflush(stdout);
+ pid = fork();
+ if (pid < 0 || nr_kids == ARRAY_SIZE(kids)) {
+ ksft_print_msg("fork: %s\n", strerror(errno));
+ cleanup();
+ exit(CASE_FAIL);
+ }
+ if (!pid) {
+ in_child = 1;
+ prctl(PR_SET_PDEATHSIG, SIGKILL);
+ } else {
+ kids[nr_kids++] = pid;
+ }
+ return pid;
+}
+
+/*
+ * Wait for @pid to exit. Skip the case if the child could not do its
+ * setup. Note a child that failed or was killed: the case then fails.
+ */
+static void reap(pid_t pid)
+{
+ int status, i;
+
+ if (waitpid(pid, &status, 0) != pid)
+ return;
+ for (i = 0; i < nr_kids; i++)
+ if (kids[i] == pid)
+ kids[i] = 0;
+ if (WIFEXITED(status) && WEXITSTATUS(status) == CASE_SKIP) {
+ cleanup();
+ exit(CASE_SKIP);
+ }
+ if (!WIFEXITED(status) || WEXITSTATUS(status)) {
+ ksft_print_msg("a child of the case failed (status 0x%x)\n", status);
+ child_failed = 1;
+ }
+}
+
+/*
+ * Return CASE_PASS if no child failed and the waiter ran less than
+ * LATE_US after @t_ref. The waiter can record its time a little before
+ * the other task records @t_ref.
+ */
+static int waiter_on_time(uint64_t t_ref, const char *what)
+{
+ long long late = (long long)(sh->t_got - t_ref) / 1000;
+
+ if (child_failed || !sh->t_got || !t_ref) {
+ ksft_print_msg("the case did not complete\n");
+ return CASE_FAIL;
+ }
+ ksft_print_msg("the waiter ran %lld us after %s (limit %d)\n",
+ late, what, LATE_US);
+ return late > -LATE_US && late < LATE_US ? CASE_PASS : CASE_FAIL;
+}
+
+/*
+ * One CPU. The owner (SCHED_OTHER) holds the mutex 300ms, then spins 1s
+ * in user space. The waiter (FIFO 50, or deadline 400ms/1s) blocks on
+ * the mutex at 100ms. It must run when the owner unlocks.
+ *
+ * The owner runs on the waiter's reservation while it holds the mutex.
+ * So the deadline waiter has a runtime above the 200ms it waits: it must
+ * not be throttled when the owner unlocks.
+ *
+ * Without "sched/proxy: Reschedule when a blocked task is woken under a
+ * proxy", the owner keeps the waiter's scheduling context after the
+ * unlock. A FIFO waiter then waits for the owner to block. A deadline
+ * waiter waits for its next period.
+ */
+static int handoff(int deadline)
+{
+ uint64_t t0;
+ pid_t o, w;
+
+ /* A deadline task cannot be pinned: use a one-CPU partition. */
+ if (deadline)
+ partition(CG_A, "0");
+ t0 = now();
+ o = child();
+ if (!o) {
+ if (deadline)
+ join(CG_A);
+ else
+ pin(0, 0, -1);
+ op_ok("hold 0 300");
+ sh->t_unlock = now();
+ spin_until(sh->t_unlock + 1000 * MS);
+ exit(0);
+ }
+ w = child();
+ if (!w) {
+ if (deadline) {
+ join(CG_A);
+ if (set_deadline(0, 400, 1000))
+ skip("SCHED_DEADLINE");
+ } else {
+ pin(0, 0, -1);
+ set_fifo(50);
+ }
+ sleep_until(t0, 100);
+ op_ok("take 0");
+ sh->t_got = now();
+ exit(0);
+ }
+ reap(w);
+ reap(o);
+ return waiter_on_time(sh->t_unlock, "the unlock");
+}
+
+static int handoff_fifo(void)
+{
+ return handoff(0);
+}
+
+static int handoff_deadline(void)
+{
+ return handoff(1);
+}
+
+static void sig_handler(int sig)
+{
+}
+
+/*
+ * Two CPUs. The owner is SCHED_OTHER, the waiter is FIFO 50. Both are on
+ * CPU 0.
+ *
+ * start the owner takes the mutex, and holds it 1500ms
+ * 100ms the waiter blocks on it, interruptibly
+ * 300ms the parent, on CPU 1, signals the waiter
+ *
+ * The signal must make the waiter return.
+ *
+ * Same fix as the hand-off cases. The busy-wait of the owner has
+ * scheduling points, so without the fix the waiter runs at the next one.
+ * In testing that was 28ms to 85ms after the signal.
+ */
+static int signal_fifo(void)
+{
+ uint64_t t0 = now(), t_signal;
+ pid_t o, w;
+
+ o = child();
+ if (!o) {
+ pin(0, 0, -1);
+ op_ok("hold 0 1500");
+ exit(0);
+ }
+ w = child();
+ if (!w) {
+ struct sigaction sa = { .sa_handler = sig_handler };
+
+ sigaction(SIGUSR1, &sa, NULL);
+ pin(0, 0, -1);
+ set_fifo(50);
+ sleep_until(t0, 100);
+ sh->ret = op("take_intr 0");
+ sh->t_got = now();
+ exit(0);
+ }
+ pin(0, 1, -1);
+ set_fifo(99);
+ sleep_until(t0, 300);
+ t_signal = now();
+ kill(w, SIGUSR1);
+ reap(w);
+ reap(o);
+ ksft_print_msg("the waiter returned %d (expect %d)\n", sh->ret, -EINTR);
+ if (sh->ret != -EINTR)
+ return CASE_FAIL;
+ return waiter_on_time(t_signal, "the signal");
+}
+
+/*
+ * Two CPUs. The owner is SCHED_OTHER. The waiter is FIFO 50, allowed on
+ * CPUs 0-1.
+ *
+ * start the owner takes the mutex on CPU 0
+ * 100ms the waiter blocks on it on CPU 0
+ * 200ms a FIFO 90 task starts to spin on CPU 0, until 1000ms
+ * 250ms the parent moves the owner to CPU 1, so that it unlocks there
+ * 500ms the owner unlocks and exits: CPU 1 is idle
+ *
+ * The waiter must run on CPU 1 then.
+ *
+ * Without "sched/proxy: Wake blocked RT and deadline tasks through the
+ * full path", the waiter is woken in place on CPU 0. It is not pushable,
+ * and waits for the FIFO 90 task.
+ */
+static int pushable_fifo(void)
+{
+ uint64_t t0 = now();
+ pid_t o, w, h;
+
+ o = child();
+ if (!o) {
+ pin(0, 0, -1);
+ op_ok("hold 0 500");
+ sh->t_unlock = now();
+ exit(0);
+ }
+ w = child();
+ if (!w) {
+ pin(0, 0, -1);
+ set_fifo(50);
+ sleep_until(t0, 100);
+ pin(0, 0, 1);
+ op_ok("take 0");
+ sh->t_got = now();
+ exit(0);
+ }
+ h = child();
+ if (!h) {
+ pin(0, 0, -1);
+ set_fifo(90);
+ sleep_until(t0, 200);
+ spin_until(t0 + 1000 * MS);
+ exit(0);
+ }
+ pin(0, 1, -1);
+ set_fifo(99);
+ sleep_until(t0, 250);
+ pin(o, 1, -1);
+ reap(w);
+ reap(o);
+ reap(h);
+ return waiter_on_time(sh->t_unlock, "the unlock");
+}
+
+/*
+ * Read @field of the root cfs_rq of @cpu from the sched debug file. Its
+ * header is "cfs_rq[N]:/" with CONFIG_FAIR_GROUP_SCHED, "cfs_rq[N]:"
+ * without. Return -1 if the field is not there.
+ */
+static long long cfs_rq_field(int cpu, const char *field)
+{
+ char line[256], hdr[32], hdr_grp[32];
+ long long v = -1;
+ int in = 0;
+ FILE *f = fopen(SCHED_DEBUG, "r");
+
+ if (!f)
+ skip("open " SCHED_DEBUG);
+ snprintf(hdr, sizeof(hdr), "cfs_rq[%d]:\n", cpu);
+ snprintf(hdr_grp, sizeof(hdr_grp), "cfs_rq[%d]:/\n", cpu);
+ while (fgets(line, sizeof(line), f)) {
+ if (!strncmp(line, "cfs_rq[", 7) || !strncmp(line, "rt_rq[", 6)) {
+ in = !strcmp(line, hdr) || !strcmp(line, hdr_grp);
+ } else if (in) {
+ char *s = strstr(line, field);
+
+ /* ".util_avg" must not match ".util_est". */
+ if (s && s[-1] == '.' && s[strlen(field)] == ' ') {
+ v = atoll(strchr(line, ':') + 1);
+ break;
+ }
+ }
+ }
+ fclose(f);
+ return v;
+}
+
+/*
+ * Three CPUs, and three SCHED_OTHER tasks that each run a loop:
+ * - The owner runs on CPU 1. It holds the mutex 15ms, then spins 5ms in
+ * user space. It never sleeps.
+ * - A second task spins on CPU 1. So the owner is preempted about half
+ * the time. A waiter only blocks while the owner is preempted:
+ * otherwise it spins.
+ * - The waiter runs at nice -10 on CPU 0. It spins 5ms, then takes the
+ * mutex. Proxy execution moves it to CPU 1 and back each time it
+ * blocks.
+ *
+ * With two tasks that never sleep, util_avg of the root cfs_rq of CPU 1
+ * stays near 1024.
+ *
+ * Without "sched/proxy: Tell the class about a proxy migration", each
+ * return of the waiter removes load that CPU 1 never got.
+ */
+static int pelt_migration(void)
+{
+ long long u, umin = 1 << 30, usum = 0;
+ uint64_t t0 = now();
+ int i, n = 40;
+ pid_t o, w, x;
+ FILE *f;
+
+ /* The tasks must be in the root cfs_rq: no autogroup. */
+ f = fopen(AUTOGROUP, "r");
+ if (f) {
+ if (fscanf(f, "%d", &autogroup_old) != 1)
+ autogroup_old = -1;
+ fclose(f);
+ write_file(AUTOGROUP, "0");
+ }
+ if (cfs_rq_field(1, "util_avg") < 0) {
+ errno = ENOENT;
+ skip("read util_avg of CPU 1 in " SCHED_DEBUG);
+ }
+ o = child();
+ if (!o) {
+ pin(0, 1, -1);
+ for (;;) {
+ op_ok("hold 0 15");
+ spin_until(now() + 5 * MS);
+ }
+ }
+ x = child();
+ if (!x) {
+ pin(0, 1, -1);
+ for (;;)
+ ;
+ }
+ w = child();
+ if (!w) {
+ pin(0, 0, -1);
+ errno = 0;
+ if (nice(-10) == -1 && errno)
+ skip("nice");
+ for (;;) {
+ spin_until(now() + 5 * MS);
+ op_ok("take 0");
+ }
+ }
+ pin(0, 2, -1);
+ set_fifo(50);
+ sleep_until(t0, 1000);
+ for (i = 0; i < n; i++) {
+ u = cfs_rq_field(1, "util_avg");
+ umin = u < umin ? u : umin;
+ usum += u;
+ sleep_until(t0, 1000 + (i + 1) * 25);
+ }
+ cleanup();
+ ksft_print_msg("util_avg of CPU 1 over %d samples: mean %lld, min %lld (mean limit 850)\n",
+ n, usum / n, umin);
+ return usum / n >= 850 ? CASE_PASS : CASE_FAIL;
+}
+
+/* Read .dl_bw->total_bw of @cpu's root domain from the sched debug file. */
+static long long total_bw(int cpu)
+{
+ char line[256], hdr[32];
+ long long v = -1;
+ int in = 0;
+ FILE *f = fopen(SCHED_DEBUG, "r");
+
+ if (!f)
+ skip("open " SCHED_DEBUG);
+ snprintf(hdr, sizeof(hdr), "dl_rq[%d]:", cpu);
+ while (fgets(line, sizeof(line), f)) {
+ if (!strncmp(line, "dl_rq[", 6)) {
+ in = !strncmp(line, hdr, strlen(hdr));
+ } else if (in && strstr(line, "dl_bw->total_bw")) {
+ v = atoll(strchr(line, ':') + 1);
+ break;
+ }
+ }
+ fclose(f);
+ return v;
+}
+
+/* Return the CPU that /proc/<pid>/stat reports for @pid. */
+static int task_cpu_of(pid_t pid)
+{
+ char path[64], buf[1024], *s;
+ int cpu = -1, i;
+ FILE *f;
+
+ snprintf(path, sizeof(path), "/proc/%d/stat", pid);
+ f = fopen(path, "r");
+ if (!f)
+ return -1;
+ if (fgets(buf, sizeof(buf), f)) {
+ /* Field 39, counted after the command name. */
+ s = strrchr(buf, ')');
+ for (i = 2; s && i < 39; i++)
+ s = strchr(s + 1, ' ');
+ if (s)
+ cpu = atoi(s + 1);
+ }
+ fclose(f);
+ return cpu;
+}
+
+/*
+ * Three CPUs. Cpusets A (CPU 0) and B (CPU 1) are partition roots. The
+ * owner is SCHED_OTHER, in B. The waiter is in A.
+ *
+ * start the owner takes the mutex, and holds it 600ms
+ * 50ms a FIFO 90 task of B starts to spin, until 450ms. The owner is
+ * then preempted when the waiter comes.
+ * 100ms the waiter blocks on the mutex. Proxy execution queues it on
+ * CPU 1.
+ * 300ms the parent calls sched_setattr() on the waiter
+ *
+ * With @admit, the waiter is SCHED_OTHER and becomes a deadline task of
+ * 10ms/100ms. Root domain A must admit it. Without @admit, the waiter is
+ * that deadline task already. Its runtime changes to 20ms, then it leaves
+ * the class. In both cases the total_bw of B must not move, and both
+ * root domains must end where they started.
+ *
+ * Without "sched/deadline: Use the root domain a proxy-migrated task runs
+ * in", sched_setattr() fails with EPERM.
+ */
+static int dl_moved(int admit)
+{
+ long long a0, b0, a1, b1, a2, b2, bw = (10LL << 20) / 100;
+ struct sched_param sp = { 0 };
+ int r, e, cpu, ok;
+ pid_t o, w, h;
+ uint64_t t0;
+
+ partition(CG_A, "0");
+ partition(CG_B, "1");
+ pin(0, 2, -1);
+ a0 = total_bw(0);
+ b0 = total_bw(1);
+ t0 = now();
+ o = child();
+ if (!o) {
+ join(CG_B);
+ op_ok("hold 0 600");
+ exit(0);
+ }
+ h = child();
+ if (!h) {
+ join(CG_B);
+ set_fifo(90);
+ sleep_until(t0, 50);
+ spin_until(t0 + 450 * MS);
+ exit(0);
+ }
+ w = child();
+ if (!w) {
+ join(CG_A);
+ if (!admit && set_deadline(0, 10, 100))
+ skip("SCHED_DEADLINE");
+ sleep_until(t0, 100);
+ op_ok("take 0");
+ exit(0);
+ }
+ sleep_until(t0, 300);
+ cpu = task_cpu_of(w);
+ r = set_deadline(w, admit ? 10 : 20, 100);
+ e = errno;
+ a1 = total_bw(0);
+ b1 = total_bw(1);
+ if (!admit && sched_setscheduler(w, SCHED_OTHER, &sp))
+ ksft_print_msg("sched_setscheduler(SCHED_OTHER): %s\n", strerror(errno));
+ reap(w);
+ reap(o);
+ reap(h);
+ sleep(1);
+ a2 = total_bw(0);
+ b2 = total_bw(1);
+ ksft_print_msg("the waiter of cpuset A (CPU 0) was queued on CPU %d\n", cpu);
+ ksft_print_msg("sched_setattr(): %s\n", r ? strerror(e) : "ok");
+ ksft_print_msg("total_bw A: before %lld, at 300ms %lld, after %lld\n", a0, a1, a2);
+ ksft_print_msg("total_bw B: before %lld, at 300ms %lld, after %lld\n", b0, b1, b2);
+ ok = !child_failed && !r && b1 == b0 && a2 == a0 && b2 == b0;
+ if (admit)
+ ok = ok && a1 - a0 >= bw - 2 && a1 - a0 <= bw + 2;
+ return ok ? CASE_PASS : CASE_FAIL;
+}
+
+static int dl_setattr_moved(void)
+{
+ return dl_moved(0);
+}
+
+static int dl_admit_moved(void)
+{
+ return dl_moved(1);
+}
+
+/*
+ * Two CPUs. Two tasks (SCHED_OTHER, CPU 0) take two mutexes in opposite
+ * orders and deadlock. A FIFO 98 heartbeat on CPU 0 ticks every 10ms. It
+ * must still tick 3s later. The inner lock is killable, so the case kills
+ * the two tasks at the end.
+ *
+ * Without "sched/proxy: Stop the chain walk when the blocked_on chain is
+ * a cycle", CPU 0 walks the cycle forever with its rq lock held.
+ */
+static int deadlock_cycle(void)
+{
+ uint64_t t0 = now(), b0, b1;
+ pid_t t1, t2, hb;
+
+ t1 = child();
+ if (!t1) {
+ pin(0, 0, -1);
+ op("pair 0 1 200");
+ exit(0);
+ }
+ t2 = child();
+ if (!t2) {
+ pin(0, 0, -1);
+ sleep_until(t0, 50);
+ op("pair 1 0 200");
+ exit(0);
+ }
+ hb = child();
+ if (!hb) {
+ pin(0, 0, -1);
+ set_fifo(98);
+ for (;;) {
+ sh->beat++;
+ usleep(10000);
+ }
+ }
+ pin(0, 1, -1);
+ set_fifo(99);
+ sleep_until(t0, 1000);
+ b0 = sh->beat;
+ sleep_until(t0, 4000);
+ b1 = sh->beat;
+ ksft_print_msg("the heartbeat of CPU 0 ticked %lld times in 3s (expect about 300)\n",
+ (long long)(b1 - b0));
+ cleanup();
+ return b1 - b0 > 100 ? CASE_PASS : CASE_FAIL;
+}
+
+static const struct {
+ const char *name;
+ int (*fn)(void);
+ int cpus; /* online CPUs 0..cpus-1 needed */
+ int cpuset; /* needs a cpuset partition */
+ int deadlock; /* needs PROXY_EXEC_TEST_DEADLOCK=1 */
+} cases[] = {
+ { "handoff_fifo", handoff_fifo, 1, 0, 0 },
+ { "handoff_deadline", handoff_deadline, 2, 1, 0 },
+ { "signal_fifo", signal_fifo, 2, 0, 0 },
+ { "pushable_fifo", pushable_fifo, 2, 0, 0 },
+ { "pelt_migration", pelt_migration, 3, 0, 0 },
+ { "dl_setattr_moved", dl_setattr_moved, 3, 1, 0 },
+ { "dl_admit_moved", dl_admit_moved, 3, 1, 0 },
+ { "deadlock_cycle", deadlock_cycle, 2, 0, 1 },
+};
+
+/* Return the number of CPUs 0..n-1 that the test may run on. */
+static int usable_cpus(void)
+{
+ cpu_set_t set;
+ int n = 0;
+
+ if (sched_getaffinity(0, sizeof(set), &set))
+ return 0;
+ while (n < CPU_SETSIZE && CPU_ISSET(n, &set))
+ n++;
+ return n;
+}
+
+/*
+ * Remove cgroup @cg. The children of a case that was killed can still be
+ * in it for a moment, so retry for a second.
+ */
+static void remove_cgroup(const char *cg)
+{
+ int i;
+
+ for (i = 0; i < 20; i++) {
+ if (!rmdir(cg) || errno == ENOENT)
+ return;
+ usleep(50000);
+ }
+ ksft_print_msg("cannot remove %s: %s\n", cg, strerror(errno));
+}
+
+/* Run case @i in its own process. Return its exit code. */
+static int run_case(unsigned int i)
+{
+ int status;
+ pid_t pid;
+
+ memset(sh, 0, sizeof(*sh));
+ fflush(stdout);
+ pid = fork();
+ if (pid < 0)
+ return CASE_SKIP;
+ if (!pid) {
+ alarm(60);
+ exit(cases[i].fn());
+ }
+ if (waitpid(pid, &status, 0) != pid)
+ status = -1;
+ if (cases[i].cpuset) {
+ remove_cgroup(CG_A);
+ remove_cgroup(CG_B);
+ }
+ if (status == -1)
+ return CASE_FAIL;
+ if (!WIFEXITED(status)) {
+ ksft_print_msg("the case was killed by signal %d\n", WTERMSIG(status));
+ return CASE_FAIL;
+ }
+ return WEXITSTATUS(status);
+}
+
+int main(void)
+{
+ int cpus = usable_cpus(), deadlock, cpuset, had_cpuset = 1, ret;
+ const char *env = getenv("PROXY_EXEC_TEST_DEADLOCK");
+ char buf[256] = "";
+ unsigned int i;
+ FILE *f;
+
+ ksft_print_header();
+ if (geteuid())
+ ksft_exit_skip("needs root\n");
+ if (access(OP_FILE, W_OK)) {
+ if (system("modprobe test_proxy_exec 2>/dev/null") || access(OP_FILE, W_OK))
+ ksft_exit_skip("needs CONFIG_TEST_PROXY_EXEC and debugfs (%s)\n", OP_FILE);
+ }
+ sh = mmap(NULL, sizeof(*sh), PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+ if (sh == MAP_FAILED)
+ ksft_exit_skip("mmap: %s\n", strerror(errno));
+ deadlock = env && !strcmp(env, "1");
+ cpuset = !access(CGROUP "/cgroup.subtree_control", W_OK);
+ /* partition() enables the cpuset controller: note if it was off. */
+ f = fopen(CGROUP "/cgroup.subtree_control", "r");
+ if (f) {
+ if (fgets(buf, sizeof(buf), f))
+ had_cpuset = !!strstr(buf, "cpuset");
+ else
+ had_cpuset = 0;
+ fclose(f);
+ }
+
+ ksft_set_plan(ARRAY_SIZE(cases));
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ if (cases[i].cpus > cpus) {
+ ksft_test_result_skip("%s: needs CPUs 0-%d\n", cases[i].name,
+ cases[i].cpus - 1);
+ continue;
+ }
+ if (cases[i].cpuset && !cpuset) {
+ ksft_test_result_skip("%s: needs cgroup v2 on %s\n", cases[i].name,
+ CGROUP);
+ continue;
+ }
+ if (cases[i].deadlock && !deadlock) {
+ ksft_test_result_skip("%s: needs PROXY_EXEC_TEST_DEADLOCK=1\n",
+ cases[i].name);
+ continue;
+ }
+ ret = run_case(i);
+ if (ret == CASE_SKIP)
+ ksft_test_result_skip("%s: see the message above\n", cases[i].name);
+ else
+ ksft_test_result(ret == CASE_PASS, "%s\n", cases[i].name);
+ }
+ if (cpuset && !had_cpuset)
+ write_file(CGROUP "/cgroup.subtree_control", "-cpuset");
+ ksft_finished();
+}
diff --git a/tools/testing/selftests/sched/proxy_exec/settings b/tools/testing/selftests/sched/proxy_exec/settings
new file mode 100644
index 000000000000..6091b45d226b
--- /dev/null
+++ b/tools/testing/selftests/sched/proxy_exec/settings
@@ -0,0 +1 @@
+timeout=120
--
2.47.3
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
` (7 preceding siblings ...)
2026-11-16 19:26 ` [RFC PATCH v1 8/8] selftests/sched: Add tests of proxy execution Mathieu Desnoyers
@ 2026-11-16 20:27 ` John Stultz
2026-11-17 6:28 ` John Stultz
9 siblings, 0 replies; 21+ messages in thread
From: John Stultz @ 2026-11-16 20:27 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> Hi John, Peter,
>
> This series fixes six problems of proxy execution, and adds a test
> module and a selftest for them. I found them while I looked at how
> SCHED_DEADLINE priority inheritance could move to proxy execution.
Hey Mathieu!
Thanks so much for sending these out! I've unfortunately been
completely underwater since LPC, so I've unfortunately not been able
to be very responsive on the list (and apologies also to everyone
who's sent patches or feedback to my last revision of the patch set).
But I have been collecting some fixes that have come up during this
time to include in my next submission. I think there is some overlap
in a few cases where we've both tried to fix the same problem, I'll
try to get some quick feedback on your patches and see what I can pull
into my pending set as well here.
I'm really excited for the additional test cases!
Thanks so much again!
-john
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests
2026-11-16 19:26 [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests Mathieu Desnoyers
` (8 preceding siblings ...)
2026-11-16 20:27 ` [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests John Stultz
@ 2026-11-17 6:28 ` John Stultz
2026-11-17 13:16 ` Mathieu Desnoyers
9 siblings, 1 reply; 21+ messages in thread
From: John Stultz @ 2026-11-17 6:28 UTC (permalink / raw)
To: Mathieu Desnoyers
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> Hi John, Peter,
>
> This series fixes six problems of proxy execution, and adds a test
> module and a selftest for them. I found them while I looked at how
> SCHED_DEADLINE priority inheritance could move to proxy execution.
>
> It applies on top of v6 of my deadline series (link below). Patches 4
> and 6 need it: they change code that v6 adds to the deadline bandwidth
> accounting. Patches 1, 2, 3 and 5 do not.
>
Just following up again after playing with your tests and patches:
So I picked up your test-driver and kselftest patches first. Initially
with my tree, only tests 5 & 8 passed (with sched_proxy_exec enabled,
with it disabled all but 6 pass).
Now, I know this series is dependent on your deadline series, but I
figured I'd try to pull the independent changes first.
> The fixes:
>
> 1 Reschedule when a blocked task is woken under a proxy, so that the
> old owner stops running on the donor's context.
With a variation of this one, tests 1, 2, 3, 5 & 8 passed.
> 2 Wake blocked RT and deadline tasks through the full wake-up path, so
> that they are pushable and another CPU can take them.
With a variation of this one (basically dropping the shortcut rather
then special casing dl/rt tasks), tests 1-5 & 8 pass.
> 3 Stop the chain walk when the blocked_on chain is a cycle, so that a
> mutex deadlock no longer locks up the CPU.
Already have a fix for this.
> 4 Tell the class about a proxy migration, so that the load of a fair
> donor is attached to the runqueue it moves to. The bandwidth of a
> deadline donor stays in its root domain.
Have a partial fix for this already, but the deadline donor bit is
dependent on your other series.
> 5 Clear blocked_donor when the donor stops donating, so that the
> unlock path does not use a task that can be freed.
I've taken a varient of this (only clearing on proxy_needs_return() -
happy to expand that but I want to understand your reasons more) and
have some related changes to clearing blockd_donor in other cases, but
as you mentioned there's no test case in your test for this logic.
> 6 Check and admit a proxy-migrated deadline task in its own root
> domain, so that sched_setattr() no longer fails with EPERM.
This one I couldn't take as it was dependent on your other series.
Basically after the first two patches tests 6, and 7 continue to fail.
Interestingly, 7 does pass with proxy-exec off, so I need to look
closer to understand what is happening in that case that isn't
dependent on your other DL patch series.
> Points I would like your opinion on:
> - Patch 2 sends RT and deadline tasks through the full wake-up again. That
> undoes part of commit 7918cf369361 ("sched/proxy: Only return migrate
> when needed") for these classes. Putting the woken task on the pushable
> list in place would keep the optimization, but it needs a hook into
> the classes.
I'm not opposed to dropping the optimization for now, but Peter added
it so he may feel more strongly and I'd defer to him :)
> - Patch 1 reschedules for any blocked task woken on a runqueue that runs
> a proxy. A fair task woken from a delayed dequeue there also triggers
> it. That is harmless, but not free.
Agreed. Would adding the resched_curr() logic to an else statement
for the if (p->se.sched_delayed) check avoid some of this? As I
replied to the patch it also catches cases where the we're proxying
but the woken task is not part of the current chain.
> - Patch 3 also stops the donation on a ww_mutex cycle that the wound or
> die path is about to break. The donor then sleeps on its mutex.
Is this a concern or did you verify this occurs? I thought we
wound/kill tasks before the cycle gets created (see
__mutex_lock_common(), before we call set_task_blocked_on()). Maybe
I'm forgetting something?
> - Patch 4 repeats in proxy_set_task_cpu() what set_task_cpu() does
> around __set_task_cpu(). A helper shared by both would keep them from
> drifting apart. I left it out to keep the fix small.
I've already done this in my version.
Thanks again for the extra testing and sending these patches out. I'll
include the one's I've picked up in my next patch series iteration on
the list, and hopefully we can sort out the deadline bits once your DL
series has landed.
thanks
-john
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [RFC PATCH v1 0/8] sched/proxy: fixes for proxy execution, with selftests
2026-11-17 6:28 ` John Stultz
@ 2026-11-17 13:16 ` Mathieu Desnoyers
0 siblings, 0 replies; 21+ messages in thread
From: Mathieu Desnoyers @ 2026-11-17 13:16 UTC (permalink / raw)
To: John Stultz
Cc: peterz, mingo, juri.lelli, vincent.guittot, linux-kernel,
dietmar.eggemann, rostedt, bsegall, mgorman, vschneid,
kprateek.nayak, connoro, arighi, akpm, shuah, linux-kselftest
Hi John,
Thanks for running the tests on your tree. I answered on patches 1, 2,
4 and 5. The rest is below.
On 2026-11-17 01:28, John Stultz wrote:
>> 3 Stop the chain walk when the blocked_on chain is a cycle, so that a
>> mutex deadlock no longer locks up the CPU.
>
> Already have a fix for this.
deadlock_cycle (case 8) passes on your tree, so please drop mine.
>> 6 Check and admit a proxy-migrated deadline task in its own root
>> domain, so that sched_setattr() no longer fails with EPERM.
>
> This one I couldn't take as it was dependent on your other series.
Only one line of it depends on that series. On tip, that line becomes
task_home_cpu(p) in place of task_cpu(p) in sched_dl_overflow(). I can
post patch 6 on tip if you want to take it.
> Basically after the first two patches tests 6, and 7 continue to fail.
> Interestingly, 7 does pass with proxy-exec off, so I need to look
> closer to understand what is happening in that case that isn't
> dependent on your other DL patch series.
Case 7 is what patch 6 fixes. Its sched_setattr() checks the root
domain of the CPU the waiter was proxy-migrated to. That is the root
domain of the mutex owner, not the waiter's own.
You also see case 6 fail with proxy execution off. It fails the same
way on tip, so it is not a proxy execution bug. The waiter changes its
parameters and then leaves SCHED_DEADLINE while it is blocked. Its
bandwidth stays counted in its root domain:
# total_bw A: before 52428, at 300ms 262143, after 262143
My deadline series fixes this. With it, the case passes with proxy
execution off.
I also ran the selftest on your public proxy-exec-v33-7.4-pre-rc1
branch, with the test module added. The results match yours, except
for case 5: Yu-Ting's fix is not on that branch.
>> - Patch 3 also stops the donation on a ww_mutex cycle that the wound or
>> die path is about to break. The donor then sleeps on its mutex.
>
> Is this a concern or did you verify this occurs? I thought we
> wound/kill tasks before the cycle gets created (see
> __mutex_lock_common(), before we call set_task_blocked_on()). Maybe
> I'm forgetting something?
You are right, and it was not verified. I wrote it from reading, and
my was wrong. A task that wounds or kills another first clears
the blocked_on of that task. The victim sets its blocked_on again under
the wait_lock of its mutex. It then sees that it must back off, and
clears blocked_on before it drops that lock. The chain walk takes the
same lock before it follows the victim. So the walk never sees the
cycle. Please ignore that point.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
^ permalink raw reply [flat|nested] 21+ messages in thread