Re: [PATCH v9 21/33] rtmutex: Continue the walk when the owner's top waiter changes lock

From: sashiko-bot

Date: Fri Nov 20 2026 - 09:20:58 EST


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] rtmutex: premature chain walk stop in
rt_mutex_adjust_prio_chain()
--

commit f337380aa6d8b1a73b3aab01c70615b369f151be
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>

rtmutex: Continue the walk when the owner's top waiter changes lock

This commit removes the top_waiter != task_top_pi_waiter(task) check in
rt_mutex_adjust_prio_chain() so the PI chain walk continues when the
previous step changes the owner's top PI waiter to a waiter on another
lock.

> diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
> index c78dfe9c93fc5..c06261259a563 100644
> --- a/kernel/locking/rtmutex.c
> +++ b/kernel/locking/rtmutex.c
> @@ -821,23 +821,15 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
> * Drop out, when the task has no waiters. Note,
> * top_waiter can be NULL, when we are in the deboosting
> * mode!
> + *
> + * Do not compare top_waiter with the top pi waiter of the
> + * task here. The previous step can have made a waiter on
> + * another lock the top pi waiter of the task. The priority of
> + * the task changed then, and the next owner must follow. The
> + * test below tells whether there is something to pass on.
> */
> - if (top_waiter) {
> - if (!task_has_pi_waiters(task))
> - goto out_unlock_pi;
> - /*
> - * If deadlock detection is off, we stop here if we
> - * are not the top pi waiter of the task. If deadlock
> - * detection is enabled we continue, but stop the
> - * requeueing in the chain walk.
> - */
> - if (top_waiter != task_top_pi_waiter(task)) {
> - if (!detect_deadlock)
> - goto out_unlock_pi;
> - else
> - requeue = false;
> - }
> - }
> + if (top_waiter && !task_has_pi_waiters(task))
> + goto out_unlock_pi;
>
> /*
> * If the waiter priority is the same as the task priority

[Severity: High]
Can this !task_has_pi_waiters(task) check still stop a deboost chain walk
early and leave the next owner with a stale priority or dangling
SCHED_DEADLINE donor?

The comment assumes top_waiter is NULL in deboosting mode, which is only
true on the first step: at the end of step 1, rt_mutex_adjust_prio_chain()
sets top_waiter = rt_mutex_top_waiter(lock) before dropping task->pi_lock
and lock->wait_lock.

Suppose O (SCHED_OTHER) holds L2, M (SCHED_OTHER) holds L1 and blocks on
L2, and W (SCHED_DEADLINE) and X (SCHED_OTHER) both block on L1, so M and
O inherit W's deadline donor (&W->dl):

CPU0 (W) CPU1 (X)
sched_setscheduler(SCHED_OTHER)
rt_mutex_adjust_pi(W)
rt_mutex_adjust_prio_chain()
/* step 1: task = W, lock = L1 */
/* requeues W behind X on L1 */
/* deboosts M to SCHED_OTHER */
top_waiter = X_waiter
unlock(M->pi_lock, L1->wait_lock)
/* X times out on L1 */
remove_waiter(L1, X_waiter)
/* M->pi_waiters is now empty */
unlock(M->pi_lock, L1->wait_lock)
/* step 2: task = M, lock = L2 */
lock(M->pi_lock)
if (top_waiter &&
!task_has_pi_waiters(M))
goto out_unlock_pi;
/* W times out on L1 */
remove_waiter(L1, W_waiter)
/* !is_top_waiter: returns */
/* W exits and frees task_struct */
/* O still has O->dl.pi_se == &W->dl */
/* before X's walk reaches O */
replenish_dl_entity() <-- use-after-free

Because step 1 demoted W below X on L1, W's own remove_waiter() sees
!is_top_waiter and returns without walking the chain, while O stays
boosted to W's deadline parameters (and can even preempt X's chain walk on
the same CPU) after W exits.

Removing the if (top_waiter && !task_has_pi_waiters(task)) check as well
lets rt_waiter_up_to_date(waiter, task) below stop the walk when the
waiter is already unboosted, while requeueing an out-of-date waiter when M
was just deboosted.

--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795181240.git.mathieu.desnoyers@xxxxxxxxxxxx?part=21