[RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating

From: Mathieu Desnoyers

Date: Mon Nov 16 2026 - 14:27:10 EST


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@xxxxxxxxxxxx>
---
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