From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410007; b=mr1mbOSSVrcQIXDtMYemJUzo36t9vizsifh/CDXzvmmIfOXG7aRDx5PUCf6+AJkXi411 p5E9Xpw/6k4gmxG17bhPdGq4bnlKMf9IyuqUniZzbAu840tCAH5BZTuca6pG2BS+3EwHQ CesKh0IJYHsSk8kec1NAYMl1CNCFPTfrOH0RzzuZbOEwBp0V0WK/02Dlv7Xn3+1OQqVFv Xm2LxmvxZumGf1/2H8rAcxTSWW8rK21o2KDHdvX7k8j18wYCyu3arCeOeCwCi9fMzB04+ paXiEFtoa0DpdM0AVNB/NFd4jcVPD1CafhRS8UCxR868LHDi0bFQDmZx9H+Fv5av7Qw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796410007; h=Received:DKIM-Signature:Received:Date:From:To:Cc:Subject:Message-ID: Reply-To:References:MIME-Version:Content-Type:Content-Disposition: In-Reply-To; bh=r1x0C870V+Tgn2fqoccwwEIi5GNq8QJCFZ+0+8aAB2I=; b=sQf1oQkvAOvucaXinDGFuT9qgHyfnxKaW9W6OTSPvooj53iaBkpGsREnA4hPKt981n1D b2nuYzj4KAxSwtHyff47ovjdDL2PpWQ7M1UrthxcOYl6RNgXPcJNCed4KpDH7/iV3ffuV pmsGXJyvXyeV37nXeoWX/353foUFVHwe0/XMtC2mOaeLiIekijKnA2f6qolsr430YaCbs M0D4HU3ZiFDD7OCItRFseHpNPKH6dhGFMKG0xqWVViZByrlipLNjuIJYJlwKKhgS33Avv gXQ4DckkTmWVaaDxYwe6s1CGy8NJzh7ooOHqf6zMbf1hOe/go1GozteS6TAVUq7Ke3w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TEqMc7+f; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77F51606A322; Fri, 4 Dec 2026 18:46:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TEqMc7+f Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00FE11F00936; Fri, 4 Dec 2026 18:46:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796410007; bh=r1x0C870V+Tgn2fqoccwwEIi5GNq8QJCFZ+0+8aAB2I=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To; b=TEqMc7+fj++8GUxBrwWwaigjxAV0VwWsNVGxLCIzTzszIGwjifKEY/Q1fWnqcy2F3 DZT8wFUwngP14ho/CYA0wfC9vitVxH/PpT3sdqtXl5bLPQzL3oJ9JRDlqi5BgItLmz 774UFlNf4ScMlSYD0QXk6NHAn1G74BuDzRudCi+3y9NmxMmu4Wir9AzJXeFaiRyIr/ VQL7mUi5oUV7kTgOXS4xt1rYr9hgkiwRXP78aAtIrTIvCkNPhKRqwt3ADtEovh2fY0 88XwNESaC0Mt0QG+t24lGWuB3YQ+eFWqKglDEfmobCe9QS54yzBqGDVethOumHGtUK oMFkCrGMhUmxw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id B3EE6CE0865; Fri, 4 Dec 2026 10:46:46 -0800 (PST) Date: Fri, 4 Dec 2026 10:46:46 -0800 From: "Paul E. McKenney" To: Ziwei Dai Cc: rcu@vger.kernel.org, linux-kernel@vger.kernel.org, Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Lai Jiangshan , Zqiang , lina.yang@unisoc.com Subject: Re: [PATCH] rcu: Stop re-initializing ->boost_mtx on every boost cycle Message-ID: <54f4e89b-d235-4cc6-b381-6012e5b8abfc@paulmck-laptop> Reply-To: paulmck@kernel.org References: <20261204100830.226336-1-ziwei.dai@unisoc.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261204100830.226336-1-ziwei.dai@unisoc.com> 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 > Signed-off-by: Ziwei Dai Good catch, and nice fix, at least from an RCU viewpoint. ;-) Reviewed-by: Paul E. McKenney > --- > 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);