[PATCH v34 05/14] sched/proxy: Clear blocked_donor when the donor stops donating

From: John Stultz

Date: Tue Dec 01 2026 - 22:53:17 EST


From: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>

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
via deactivate_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>
[jstultz: Minor tweaks. Moved call to deactivate_task() to catch
the case more generally then calling from numerous other sites]
Signed-off-by: John Stultz <jstultz@xxxxxxxxxx>
---
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: K Prateek Nayak <kprateek.nayak@xxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Juri Lelli <juri.lelli@xxxxxxxxxx>
Cc: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
Cc: Valentin Schneider <vschneid@xxxxxxxxxx>
Cc: Suleiman Souhlal <suleiman@xxxxxxxxxx>
Cc: Andrea Righi <arighi@xxxxxxxxxx>
Cc: Johannes Weiner <hannes@xxxxxxxxxxx>
Cc: Metin Kaya <Metin.Kaya@xxxxxxx>
Cc: Dietmar Eggemann <dietmar.eggemann@xxxxxxx>
Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
Cc: Zimuzo Ezeozue <zezeozue@xxxxxxxxxx>
Cc: Waiman Long <longman@xxxxxxxxxx>
Cc: Boqun Feng <boqun.feng@xxxxxxxxx>
Cc: "Paul E. McKenney" <paulmck@xxxxxxxxxx>
Cc: Joel Fernandes <joelagnelf@xxxxxxxxxx>
Cc: Qais Yousef <qyousef@xxxxxxxxxxx>
Cc: Xuewen Yan <xuewen.yan94@xxxxxxxxx>
Cc: kuyo chang <kuyo.chang@xxxxxxxxxxxx>
Cc: hupu <hupu.gm@xxxxxxxxx>
Cc: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
Cc: zhidao su (Xiaomi) <soolaugust@xxxxxxxxx>
Cc: Hui Su <sh_def@xxxxxxx>
Cc: Shubhang Kaushik (Ampere) <sh@xxxxxxxxxx>
Cc: kernel-team@xxxxxxxxxxx
---
kernel/sched/core.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index eb2ab495f1df0..ff85346a9c49d 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2186,6 +2186,8 @@ static inline void uclamp_post_fork(struct task_struct *p) { }
static inline void init_uclamp(void) { }
#endif /* !CONFIG_UCLAMP_TASK */

+static inline void proxy_clear_curr_donor(struct rq *rq, struct task_struct *p);
+
bool sched_task_on_rq(struct task_struct *p)
{
return task_on_rq_queued(p);
@@ -2275,6 +2277,9 @@ void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
ASSERT_EXCLUSIVE_WRITER(p->on_rq);

+ /* check curr's blocked_donor back pointer and clear it if it is p */
+ proxy_clear_curr_donor(rq, p);
+
/*
* Code explicitly relies on TASK_ON_RQ_MIGRATING begin set *before*
* dequeue_task() and cleared *after* enqueue_task().
@@ -2612,6 +2617,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)
+ WRITE_ONCE(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.
*
@@ -3864,6 +3896,7 @@ static inline bool proxy_needs_return(struct rq *rq, struct task_struct *p)
{
bool dequeued;

+ proxy_clear_curr_donor(rq, p);
/*
* Typically per __set_task_cpu(), task_cpu(p) == p->wake_cpu.
*
@@ -6947,6 +6980,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.56.1.911.g883d8bcb20-goog