Re: [PATCH] rcu: Stop re-initializing ->boost_mtx on every boost cycle
From: Paul E. McKenney
Date: Fri Dec 04 2026 - 13:46:53 EST
On Fri, Dec 04, 2026 at 06:08:30PM +0800, Ziwei Dai wrote:
> rcu_boost() calls rt_mutex_init_proxy_locked() on the permanently
> embedded rcu_node::boost_mtx at the beginning of every boost cycle.
> That helper runs __rt_mutex_base_init(), which rewrites lock->wait_lock
> via raw_spin_lock_init().
>
> The rewritten spinlock may still be in use by the task that was
> boosted in the previous cycle. That task releases ->boost_mtx with
> rt_mutex_futex_unlock() from rcu_preempt_deferred_qs_irqrestore(),
> and it does so *after* raw_spin_unlock_irqrestore_rcu_node().
> rnp->lock therefore does not serialize that release against the next
> round's re-initialization, contrary to what the comment in rcu_boost()
> claims.
>
> Nothing else serializes the two, either. rt_mutex_futex_unlock()
> clears ->owner under ->wait_lock, and only then releases ->wait_lock.
> As soon as ->owner is NULL, the rt_mutex_lock()/rt_mutex_unlock() pair
> of the boost kthread takes the fast path, which does not touch
> ->wait_lock at all. The kthread can thus complete that pair, return
> from rcu_boost(), enter rcu_boost() again and re-initialize
> ->boost_mtx while the previous owner is still inside the wait_lock
> critical section of its rt_mutex_futex_unlock().
>
> When this happens, the non-atomic rewrite lands on a spinlock that
> another CPU currently holds. On ARM32, which uses ticket spinlocks,
> this was observed on a 32-bit ARM system with CONFIG_DEBUG_SPINLOCK=y
> during a reboot stress test (the RCU boost code there is identical to
> mainline) as a lock word going from 0x00010000 (next=1, owner=0:
> held) to 0x00010001 (next=1, owner=1: free) while the real holder was
> still inside the critical section: the re-initialization clears the
> lock word, the unlock of the previous holder increments the owner
> ticket, and the next acquisition of the boost kthread then "succeeds"
> on a lock word with owner == next. The unlock of the boost kthread
> fails
>
> SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
>
> in debug_spin_unlock() and panics:
>
> BUG: spinlock already unlocked on CPU#1, rcub/0/22
> lock: rcu_state+0x6c/0x880, .magic: dead4ead, .owner: rcub/0/22, .owner_cpu: 1
>
> On architectures using qspinlocks the tail/pending encoding of a
> queued waiter is lost instead, so the failure is expected to be less
> obvious there (a hang or silent corruption rather than a clean BUG()).
>
> Fix it by initializing ->boost_mtx exactly once, in
> rcu_spawn_one_boost_kthread(), before the boost kthread is published
> via ->boost_kthread_task and hence before any rcu_boost() can run.
> Each boost cycle now only transfers ownership through the new
> rt_mutex_boost_set_owner(), which is rt_mutex_init_proxy_locked() minus
> the destructive re-initialization.
>
> This is safe because the waiters tree is always empty at the start of a
> boost cycle: rcu_boost() is only called by the single per-rcu_node
> boost kthread, which completes its rt_mutex_lock()/rt_mutex_unlock()
> pair before returning. A WARN_ON_ONCE() documents and checks that
> invariant.
>
> Fixes: 02a7c234e540 ("rcu: Suppress lockdep false-positive ->boost_mtx complaints")
> Reported-by: Houtang Zhang <houtang.zhang@xxxxxxxxxx>
> Signed-off-by: Ziwei Dai <ziwei.dai@xxxxxxxxxx>
Good catch, and nice fix, at least from an RCU viewpoint. ;-)
Reviewed-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
> ---
> kernel/locking/rtmutex_api.c | 35 +++++++++++++++++++++++++++++++++
> kernel/locking/rtmutex_common.h | 2 ++
> kernel/rcu/tree_plugin.h | 29 ++++++++++++++++++++++++++-
> 3 files changed, 65 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/locking/rtmutex_api.c b/kernel/locking/rtmutex_api.c
> index eb18b09..19422b7 100644
> --- a/kernel/locking/rtmutex_api.c
> +++ b/kernel/locking/rtmutex_api.c
> @@ -294,6 +294,41 @@ void __sched rt_mutex_init_proxy_locked(struct rt_mutex_base *lock,
> rt_mutex_set_owner(lock, proxy_owner);
> }
>
> +/**
> + * rt_mutex_boost_set_owner - make an initialized rt_mutex appear to be held
> + by @proxy_owner
> + * @lock: the rt_mutex, which must have been initialized already
> + * @proxy_owner: the task to set as owner
> + *
> + * No locking. Caller has to do serializing itself.
> + *
> + * Counterpart of rt_mutex_init_proxy_locked() for RCU priority boosting,
> + * which reuses a single, permanently embedded rt_mutex
> + * (rcu_node::boost_mtx) for every boost cycle. This performs exactly the
> + * same owner assignment, but deliberately does *not* re-initialize @lock
> + * and therefore does not rewrite lock->wait_lock.
> + *
> + * Re-initializing ->wait_lock is not possible once the mutex is in use.
> + * The task which was boosted in the previous cycle releases ->boost_mtx
> + * via rt_mutex_futex_unlock(), and it does so without holding the rcu_node
> + * lock. The ->owner = NULL store in that path happens while ->wait_lock
> + * is held, so the fast path of rt_mutex_lock() can already succeed at
> + * that point. Rewriting ->wait_lock from the boost kthread in this
> + * window lands on a spinlock which the other CPU still holds, which
> + * corrupts the lock word (on ARM32 ticket spinlocks the previous holder
> + * then trips "BUG: spinlock already unlocked") or, worse, lets two CPUs
> + * run inside the wait_lock protected section at the same time.
> + *
> + * Callers must initialize @lock once, up front, and use this function for
> + * every subsequent ownership transfer. @lock must be free and have no
> + * waiters queued when this is called.
> + */
> +void __sched rt_mutex_boost_set_owner(struct rt_mutex_base *lock,
> + struct task_struct *proxy_owner)
> +{
> + rt_mutex_set_owner(lock, proxy_owner);
> +}
> +
> /**
> * rt_mutex_proxy_unlock - release a lock on behalf of owner
> *
> diff --git a/kernel/locking/rtmutex_common.h b/kernel/locking/rtmutex_common.h
> index c38b7bd..6167c87 100644
> --- a/kernel/locking/rtmutex_common.h
> +++ b/kernel/locking/rtmutex_common.h
> @@ -81,6 +81,8 @@ struct rt_wake_q_head {
> extern void rt_mutex_init_proxy_locked(struct rt_mutex_base *lock,
> struct task_struct *proxy_owner)
> __must_hold(&lock->wait_lock);
> +extern void rt_mutex_boost_set_owner(struct rt_mutex_base *lock,
> + struct task_struct *proxy_owner);
>
> extern void rt_mutex_proxy_unlock(struct rt_mutex_base *lock)
> __must_hold(&lock->wait_lock);
> diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h
> index ae5d21b..11f5acf 100644
> --- a/kernel/rcu/tree_plugin.h
> +++ b/kernel/rcu/tree_plugin.h
> @@ -1222,9 +1222,22 @@ static int rcu_boost(struct rcu_node *rnp)
> * rnp->lock also resolves races between our priority boosting
> * and task t's exiting its outermost RCU read-side critical
> * section.
> + *
> + * But rnp->lock does not cover the release of ->boost_mtx itself:
> + * the task boosted in the previous cycle drops it with
> + * rt_mutex_futex_unlock() *after* it has dropped rnp->lock, so that
> + * release can still be in flight here. Never re-initialize
> + * ->boost_mtx, as that would rewrite ->wait_lock while the other CPU
> + * might still hold it. ->boost_mtx was initialized once by
> + * rcu_spawn_one_boost_kthread(), so only transfer the ownership.
> + *
> + * The waiters tree must be empty here: rcu_boost() is only ever
> + * called by this rcu_node's single boost kthread, which completes
> + * its rt_mutex_lock()/rt_mutex_unlock() pair before returning.
> */
> t = container_of(tb, struct task_struct, rcu_node_entry);
> - rt_mutex_init_proxy_locked(&rnp->boost_mtx.rtmutex, t);
> + WARN_ON_ONCE(!RB_EMPTY_ROOT(&rnp->boost_mtx.rtmutex.waiters.rb_root));
> + rt_mutex_boost_set_owner(&rnp->boost_mtx.rtmutex, t);
> raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
> /* Lock only for side effect: boosts task t's priority. */
> rt_mutex_lock(&rnp->boost_mtx);
> @@ -1334,6 +1347,20 @@ static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
> if (WARN_ON_ONCE(IS_ERR(t)))
> return;
>
> + /*
> + * Initialize ->boost_mtx exactly once, here, before it is made
> + * reachable through ->boost_kthread_task and therefore before the
> + * first rcu_boost() can run. Passing a NULL proxy owner leaves the
> + * mutex unlocked and unowned, and applies the same lockdep class to
> + * ->wait_lock that rt_mutex_init_proxy_locked() used to re-apply on
> + * every boost cycle.
> + *
> + * rcu_boost() only transfers the ownership of ->boost_mtx from now
> + * on, so that ->wait_lock is never rewritten while the task boosted
> + * in the previous cycle may still be inside rt_mutex_futex_unlock().
> + */
> + rt_mutex_init_proxy_locked(&rnp->boost_mtx.rtmutex, NULL);
> +
> raw_spin_lock_irqsave_rcu_node(rnp, flags);
> rnp->boost_kthread_task = t;
> raw_spin_unlock_irqrestore_rcu_node(rnp, flags);