Re: [RFC PATCH v1 5/8] sched/proxy: Clear blocked_donor when the donor stops donating
From: John Stultz
Date: Mon Nov 16 2026 - 18:16:41 EST
On Mon, Nov 16, 2026 at 11:26 AM Mathieu Desnoyers
<mathieu.desnoyers@xxxxxxxxxxxx> 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@xxxxxxxxxxxx>
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