mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ziwei Dai <ziwei.dai@unisoc.com>
To: <rcu@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: "Paul E . McKenney" <paulmck@kernel.org>,
	Frederic Weisbecker <frederic@kernel.org>,
	Neeraj Upadhyay <neeraj.upadhyay@kernel.org>,
	Joel Fernandes <joelagnelf@nvidia.com>,
	Josh Triplett <josh@joshtriplett.org>,
	Boqun Feng <boqun.feng@kernel.org>,
	Uladzislau Rezki <urezki@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Waiman Long <longman@redhat.com>,
	Lai Jiangshan <jiangshanlai@gmail.com>,
	Zqiang <qiang.zhang@linux.dev>, <lina.yang@unisoc.com>
Subject: [PATCH] rcu: Stop re-initializing ->boost_mtx on every boost cycle
Date: Fri, 4 Dec 2026 18:08:30 +0800	[thread overview]
Message-ID: <20261204100830.226336-1-ziwei.dai@unisoc.com> (raw)

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@unisoc.com>
Signed-off-by: Ziwei Dai <ziwei.dai@unisoc.com>
---
 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);

             reply	other threads:[~2026-12-04 10:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 10:08 Ziwei Dai [this message]
2026-12-04 18:46 ` Paul E. McKenney
2026-12-05  9:55 ` kernel test robot
2026-12-05  9:55 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204100830.226336-1-ziwei.dai@unisoc.com \
    --to=ziwei.dai@unisoc.com \
    --cc=boqun.feng@kernel.org \
    --cc=frederic@kernel.org \
    --cc=jiangshanlai@gmail.com \
    --cc=joelagnelf@nvidia.com \
    --cc=josh@joshtriplett.org \
    --cc=lina.yang@unisoc.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@redhat.com \
    --cc=neeraj.upadhyay@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=qiang.zhang@linux.dev \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=urezki@gmail.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®