Re: [RFC PATCH v1 1/8] sched/proxy: Reschedule when a blocked task is woken under a proxy

From: Ramin Moussavi

Date: Tue Nov 24 2026 - 11:13:02 EST


Hi Mathieu, John,

A field report for patch 1, from a single-CPU system running 6.18.y.
We tracked down the same bug independently on real hardware and only
then came across your series. It may help with two open points: the
shortcut in proxy_needs_return(), and 6.18.y.

System: ARM Cortex-A5 (SAMA5D2), one CPU, 6.18.64, PREEMPT, HZ=100,
SCHED_PROXY_EXEC=y. The firmware of an industrial Ethernet switch runs
about 65 SCHED_FIFO threads. A ring redundancy manager (FIFO 99, 6 ms
cycle) stalled for 25 to 60 ms about twice a day, together with every
other RT thread. A 1 ms sampling hrtimer showed a SCHED_OTHER task
running the whole time, partly in user space, with FIFO 99 threads
runnable and need_resched never set.

The mechanism is the one in your commit message. The owner's
mutex_unlock() clears the donor's blocked_on and wakes it.
ttwu_runnable() calls wakeup_preempt(), which compares the donor with
rq->donor, that is with itself, and asks for nothing. The owner keeps
the FIFO 99 context until it sleeps on its own.

1) UP and the proxy_needs_return() shortcut

On a single CPU, task_cpu(p) == p->wake_cpu always holds, so the
shortcut at the top of proxy_needs_return() is always taken there and
the donor never goes through proxy_reset_donor(). The variant discussed
in the thread puts the resched in an else branch inside
proxy_needs_return(). That only helps UP if the shortcut goes away
completely. If the shortcut stays for some classes, please keep the
resched reachable when it is taken, as your patch 1 does.

2) Two more cases on 6.18.y

On 6.18 the wakeup itself can be a no-op, because the waiter is
already TASK_RUNNING and ttwu_state_match() fails:

a) A signal to a donor in mutex_lock_interruptible() sets it
TASK_RUNNING but leaves blocked_on set. find_proxy_task() then
picks the owner again, and the donor cannot act on the signal
until the unlock. At the unlock, the wakeup of the donor does
nothing, and the owner keeps the donor's context again. This is
your signal_fifo case.
b) The first waiter was woken earlier, but the lock was stolen before
it ran, and the donor is queued behind it. The unlock wakes the
first waiter, which is already running, so nothing reschedules.

As far as I can see, mainline hands the lock to blocked_donor, so b)
should not exist there. 6.18 has no blocked_donor and picks the first
waiter.

3) Measurements on the hardware

Reproducer A: a SCHED_OTHER task O writes and reads a pipe
(pipe->mutex), then spins 50 ms in user space and sleeps 150 ms. D
(FIFO 99) does a non-blocking read() of the same pipe every 5 ms. M
(FIFO 80) runs a 1 ms clock_nanosleep() loop and records how late it
wakes up.

6.18.64, proxy exec on: M up to 51 ms late in nearly every cycle
(= O's spin), D's read() up to 50 ms
sched_proxy_exec=0: M up to 21 ms, D's read() up to 220 ms
with the patch below: M up to 5.6 ms, D up to 1.05 ms
(5 runs of 60 s)

Reproducer B: a test module with one struct mutex, similar to your
test_proxy_exec. O holds the mutex for 5 ms in a preemptible kernel
loop, then spins 50 ms in user space. D (FIFO 99) calls
mutex_lock_interruptible() every 3 ms and gets SIGUSR1 from a POSIX
timer every 7 ms or 3 ms.

resched in ttwu_runnable() only: 0 EINTR, D up to 18.7 ms,
M up to 19.1 ms
all three changes below: 127 / 425 EINTR, D up to 4.97 ms
(= hold time), M up to 5.2 ms

Both reproducers ran on a board with the same SoC. We have not yet
tested the patch on the field units that showed the stalls. Case b)
above comes from reading the code; I have no reproducer for it.

Proxy execution stays on: with sched_proxy_exec=0 the priority
inversion is worse for us than the stall.

4) 6.18.y

6.18 is an LTS kernel and has SCHED_PROXY_EXEC as an option, but none
of the mainline infrastructure (is_blocked, proxy_needs_return(),
blocked_donor), so the mainline fix will not apply there. Could the
final fix carry

Fixes: 7de9d4f94638 ("sched: Start blocked_on chain processing in find_proxy_task()")
Cc: stable@xxxxxxxxxxxxxxx

so that 6.18.y gets a backport? Below is what we run on 6.18.64, for
discussion and as a starting point, not for merging as is. It
reschedules in three places:

- ttwu_runnable(): after wakeup_preempt(), if rq->curr != rq->donor,
as in your patch 1.
- __mutex_unlock_slowpath(): after waking a waiter, the unlocking
task sets need_resched on itself if it runs as a proxy. This covers
a) and b), where the wakeup does nothing.
- find_proxy_task(): a task in the chain that is TASK_RUNNING while
blocked_on is still set (signal) runs itself, so it can return from
schedule() and see the signal.

I am happy to test a 6.18 backport of whatever lands upstream on this
hardware.

How this was made: the analysis, both reproducers and the changes
below were developed with an AI coding assistant (Claude Code) under my
direction. GPT and Gemini models reviewed the changes in three rounds.
I reviewed the result, and all numbers above come from runs on the
hardware.

Thanks,
Ramin

-- >8 --
From: Ramin Moussavi <ramin.moussavi@xxxxxxxxx>
Subject: [PATCH 6.18.y RFC] sched/proxy: Reschedule when the donor stops being blocked

Signed-off-by: Ramin Moussavi <ramin.moussavi@xxxxxxxxx>
---
include/linux/sched.h | 8 ++++++++
kernel/locking/mutex.c | 4 ++++
kernel/sched/core.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 58 insertions(+)

--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2228,6 +2228,14 @@ static inline void clear_task_blocked_on
}
#endif /* !CONFIG_PREEMPT_RT */

+#ifdef CONFIG_SCHED_PROXY_EXEC
+extern void sched_proxy_unlock_resched(void);
+#else
+static inline void sched_proxy_unlock_resched(void)
+{
+}
+#endif
+
static __always_inline bool need_resched(void)
{
return unlikely(tif_need_resched());
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -963,6 +963,10 @@ static noinline void __sched __mutex_unl
__mutex_handoff(lock, next);

raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
+
+ /* Current may run as proxy, see sched_proxy_unlock_resched() */
+ if (next)
+ sched_proxy_unlock_resched();
}

#ifndef CONFIG_DEBUG_LOCK_ALLOC
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3789,6 +3789,16 @@ static int ttwu_runnable(struct task_str
* it should preempt the task that is current now.
*/
wakeup_preempt(rq, p, wake_flags);
+ /*
+ * rq->curr != rq->donor: rq->curr runs as proxy for a
+ * mutex-blocked donor, and @p may be that donor or a task
+ * of its blocked_on chain. wakeup_preempt() compares @p
+ * with the donor, possibly with itself, and does not
+ * reschedule. The proxy would then keep the donor's
+ * scheduling context until it blocks. Pick again.
+ */
+ if (sched_proxy_exec() && rq->curr != rq->donor)
+ resched_curr(rq);
}
ttwu_do_wakeup(p);
ret = 1;
@@ -6589,6 +6599,31 @@ static bool try_to_block_task(struct rq
}

#ifdef CONFIG_SCHED_PROXY_EXEC
+/*
+ * Called by mutex_unlock() after it woke a waiter. If current runs as proxy
+ * for a mutex-blocked donor, the unlock may have ended the wait of the donor
+ * or of a task in its blocked_on chain. The wakeup alone does not always
+ * reschedule: try_to_wake_up() does nothing for a waiter that is already
+ * TASK_RUNNING (woken by a signal, or woken earlier and the lock was stolen
+ * before it ran).
+ */
+void sched_proxy_unlock_resched(void)
+{
+ if (!sched_proxy_exec())
+ return;
+
+ /*
+ * With preemption disabled, rq->donor of this CPU only changes in
+ * __schedule() on this CPU. Flag current directly, like resched_curr()
+ * does for the local CPU, without taking rq->lock.
+ */
+ guard(preempt)();
+ if (!task_current_donor(this_rq(), current)) {
+ set_tsk_need_resched(current);
+ set_preempt_need_resched();
+ }
+}
+
static inline struct task_struct *proxy_resched_idle(struct rq *rq)
{
put_prev_set_next_task(rq, rq->donor, rq->idle);
@@ -6676,6 +6711,17 @@ find_proxy_task(struct rq *rq, struct ta
return NULL;
}

+ /*
+ * @p was woken (signal, wake_up_process()) but is still marked
+ * as blocked. Let it run itself, so that it can return from
+ * schedule() and check for signals, instead of running the
+ * owner on its behalf until the mutex is released.
+ */
+ if (READ_ONCE(p->__state) == TASK_RUNNING) {
+ __clear_task_blocked_on(p, mutex);
+ return p;
+ }
+
owner = __mutex_owner(mutex);
if (!owner) {
__clear_task_blocked_on(p, mutex);