From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=222.66.158.135 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796378943; b=X8fvzoMgxt8OqNcDRJu61O9o3SjdSBqgjktGhW59S/mKhu6zlMlldf7zjVmKEsPtDJuj Mehdo0VT97nvy+Cml/fetmglaR+qC/bBwQmBOXX4N5LtY3RolRmVsJoTXX4LNyWZRO0Op bMcgTEikWd2LM7r4BT0DOzw8N2ATuU7Z7+poi/5TqxfmXDHAI8eKpZ2QehvdJJSCmcRTa yid1+xH9AMeRYPYSKSV+9O7n1x4YO6Xhk+D2/MqmoWh6zNKgv4dPo7PRVm56CSOLU+APt kYJunRgyrboKI9orfANFAD0mqBhHSwpXbfkJOuGYQRBRoWLnEpfsDUfIBW9U8eF3BhQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796378943; h=DMARC-Filter:Received:Received:Received:From:To:CC:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding: Content-Type:X-Originating-IP:X-ClientProxiedBy:X-MAIL: DKIM-Signature; bh=wbnunxLmfDXulvS43pQRa9E/NKf/EQEuIE13MikOpWQ=; b=fdwTI3V3zeyav/VAaVehzPP6mq+aJWIHnbShKe3Dsx1bEuZUP17fy4SR9xcg/8DKmZcW n76TxhTQDcJ5ahWQiKsAYxM7ihF5pk87z5q6n1fx+FK6hu6b61kYWpE4we5STtdCmRS8s m2uFqmnA2rsovMCaNIkDUM7Au1jQHH3G6TbtRRjaaNDB92UexXdEgnqq8s37SXt6TYnng 81m8E/jG8ep0S+G2gVfCzgkrSVGQeaFWGKVIhyBT74ofKfuw/8igjI2lcIBn3J98amQrX mdi2GAcVc0rN1EeSsmYnssXPjPbRHuAvUKEGLNHMCsO2gtXGxOsfERv+j1Anuh7t5Mg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; dkim=pass header.d=unisoc.com header.i=@unisoc.com header.a=rsa-sha256 header.s=default header.b=OyGMEHu7; arc=none smtp.remote-ip=222.66.158.135 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9310606A322 for ; Fri, 4 Dec 2026 10:09:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E9310606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=unisoc.com header.i=@unisoc.com header.a=rsa-sha256 header.s=default header.b=OyGMEHu7 Received: from dlp.unisoc.com ([10.29.3.83]) by SHSQR01.spreadtrum.com with ESMTPS id 6B4A8elv058580 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Fri, 4 Dec 2026 18:08:40 +0800 (+08) (envelope-from Ziwei.Dai@unisoc.com) Received: from SHDLP.spreadtrum.com (BJMBX01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4jgnJC06VCz1VVX; Fri, 4 Dec 2026 18:08:26 +0800 (CST) Received: from bj04576pcu008.spreadtrum.com (10.0.77.159) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 4 Dec 2026 18:08:39 +0800 From: Ziwei Dai To: , CC: "Paul E . McKenney" , 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 , Subject: [PATCH] rcu: Stop re-initializing ->boost_mtx on every boost cycle Date: Fri, 4 Dec 2026 18:08:30 +0800 Message-ID: <20261204100830.226336-1-ziwei.dai@unisoc.com> X-Mailer: git-send-email 2.43.0 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-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SHCAS01.spreadtrum.com (10.0.1.201) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL:SHSQR01.spreadtrum.com 6B4A8elv058580 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unisoc.com; s=default; t=1796378936; bh=wbnunxLmfDXulvS43pQRa9E/NKf/EQEuIE13MikOpWQ=; h=From:To:CC:Subject:Date; b=OyGMEHu7Vpjvj7lRjz6ORWZQKOKb8ZanUlBE8M3tS26SyS1i3EadXZXDO03gxaQHC jLt5Gi3n35iYoXFqf+FMDsuG03dwFzW6y07vcw1vhtZ9ncGLQOSRnwanYbRlG3EfgO pa5FzgdMf2BJDANVPt/WOvclJQnMgr9qd0i2/gxCcu1WoAosaEK9V9M9DLSJNBE0YG 5MZ6Hi2kTK54cBuK8UHJOA5SZe7j9qKpZUV6pJpWHHwbKD9VOI6Wp38vqHkFOMUj5U Fy0ihOSTUUNzWZ2iDE7COcuKCxwK/oIkmJbTMsZw2lggIQbSLT12bKAxS4xXaBH9Hm xVngTZNFEd91Q== 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 --- 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);