From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from 011.lax.mailroute.net (011.lax.mailroute.net [199.89.1.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 534731DFF0 for ; Sat, 2 May 2026 01:54:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=199.89.1.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777686865; cv=none; b=qMQv1fEAov9qI7y7BRZluDeSkneDyPp7rD+xNqZrOu920GdjiWQN0QewuV9fkH7Z3FUswSMpE4I/6V++hn3VPhDNoHCbMNI5flNrzAgAMngxXaDx/bGhJU8+WMSyU9Sft0Wf0nkzPaHolOvTtNImDBV8hy46JKSm/pyT7F/fAhE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777686865; c=relaxed/simple; bh=56GNIWBqVfN9GqnA9Sy1OSaHKUdPCfKXNdw2PNhSHKA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=oAAoavfjJhQOlr6gGaQPp1kgsBGbFep9iYC7PGHiT7Q2Ou4rPhV+Zh169IKs/wg92LBHJDG7HgY7OKzA3fvyTZc2f9eBD4dmicgZivDjmrJnk68NaddWe+9eFdCrHSvZYVthcxh/uyssjq6zUl4lJpPWcf3x+yXUPkez6nzZL9Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b=MECY59b+; arc=none smtp.client-ip=199.89.1.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=acm.org header.i=@acm.org header.b="MECY59b+" Received: from localhost (localhost [127.0.0.1]) by 011.lax.mailroute.net (Postfix) with ESMTP id 4g6rYq5kZ9z1XM6Jb; Sat, 2 May 2026 01:54:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:mime-version:x-mailer:message-id:date :date:subject:subject:from:from:received:received; s=mr01; t= 1777686852; x=1780278853; bh=iIqJw7GVWEBWWDM+mbLA5+F/ZqxNyznu7yT OI5w5ZTQ=; b=MECY59b+a+KrGBe+jBe0LEjTuR7agVUsR0ujphK0J9fCfo1zn4M yGSrHbnzrVULT5ZJBD01rrxyQjbpnAStGTdD9qvBBxHaWGTFU6a/GxR319id/TiG fSYV/YE7uDhRiaHydGXSQujiLb83cwRfOkudL4r64nbcZhV0YEv7oBy1BYb6WaEM F9slpmpxeiIrjcxihvvV9LKC4P97eaQ40i/9Oy+SWxZcBMxAyP6fFWwNW6VMEwzN 6B+1E7eCIItFkxtOFc9CXSeeV/EKgX2uBqgVoTEUqVdFuEINf3sIEF6JUPYQywiO fV/xyCznxi3OsXl08aQ44XcYGImadMCfROw== X-Virus-Scanned: by MailRoute Received: from 011.lax.mailroute.net ([127.0.0.1]) by localhost (011.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id lq2djgbicrSN; Sat, 2 May 2026 01:54:12 +0000 (UTC) Received: from bvanassche-glaptop2.roam.corp.google.com (unknown [216.9.110.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 011.lax.mailroute.net (Postfix) with ESMTPSA id 4g6rYX0vlHz1XM6J4; Sat, 2 May 2026 01:54:07 +0000 (UTC) From: Bart Van Assche To: Peter Zijlstra Cc: Marco Elver , linux-kernel@vger.kernel.org, Bart Van Assche , Ingo Molnar , Will Deacon , Boqun Feng , Joel Granados , Alexei Starovoitov , Sebastian Andrzej Siewior , Vlastimil Babka Subject: [PATCH] locking/rtmutex: Annotate API and implementation Date: Fri, 1 May 2026 18:53:49 -0700 Message-ID: <20260502015354.430273-1-bvanassche@acm.org> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Add lock context annotations to the rtmutex API and implementation and enable lock context analysis. Signed-off-by: Bart Van Assche --- include/linux/rtmutex.h | 22 +++++++++++++++------- kernel/locking/Makefile | 1 + kernel/locking/rtmutex.c | 4 ++++ kernel/locking/rtmutex_api.c | 10 ++++++++-- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/linux/rtmutex.h b/include/linux/rtmutex.h index 78e7e588817c..9e1f012f89db 100644 --- a/include/linux/rtmutex.h +++ b/include/linux/rtmutex.h @@ -56,6 +56,8 @@ static inline struct task_struct *rt_mutex_owner(struct= rt_mutex_base *lock) #endif extern void rt_mutex_base_init(struct rt_mutex_base *rtb); =20 +context_lock_struct(rt_mutex); + /** * The rt_mutex structure * @@ -108,8 +110,10 @@ do { \ extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, str= uct lock_class_key *key); =20 #ifdef CONFIG_DEBUG_LOCK_ALLOC -extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int sub= class); -extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockd= ep_map *nest_lock); +extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int sub= class) + __acquires(lock); +extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockd= ep_map *nest_lock) + __acquires(lock); #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0) #define rt_mutex_lock_nest_lock(lock, nest_lock) \ do { \ @@ -118,15 +122,19 @@ extern void _rt_mutex_lock_nest_lock(struct rt_mute= x *lock, struct lockdep_map * } while (0) =20 #else -extern void rt_mutex_lock(struct rt_mutex *lock); +extern void rt_mutex_lock(struct rt_mutex *lock) __acquires(lock); #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock) #define rt_mutex_lock_nest_lock(lock, nest_lock) rt_mutex_lock(lock) #endif =20 -extern int rt_mutex_lock_interruptible(struct rt_mutex *lock); -extern int rt_mutex_lock_killable(struct rt_mutex *lock); -extern int rt_mutex_trylock(struct rt_mutex *lock); +extern int rt_mutex_lock_interruptible(struct rt_mutex *lock) + __cond_acquires(0, lock); +extern int rt_mutex_lock_killable(struct rt_mutex *lock) + __cond_acquires(0, lock); +extern int rt_mutex_trylock(struct rt_mutex *lock) + __cond_acquires(true, lock); =20 -extern void rt_mutex_unlock(struct rt_mutex *lock); +extern void rt_mutex_unlock(struct rt_mutex *lock) + __releases(lock); =20 #endif diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile index cee1901d4cff..24dc00e12aa6 100644 --- a/kernel/locking/Makefile +++ b/kernel/locking/Makefile @@ -4,6 +4,7 @@ KCOV_INSTRUMENT :=3D n =20 CONTEXT_ANALYSIS_mutex.o :=3D y +CONTEXT_ANALYSIS_rtmutex.o :=3D y CONTEXT_ANALYSIS_rtmutex_api.o :=3D y CONTEXT_ANALYSIS_ww_rt_mutex.o :=3D y CONTEXT_ANALYSIS_rwsem.o :=3D y diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c index 4f386ea6c792..9147d6a31b78 100644 --- a/kernel/locking/rtmutex.c +++ b/kernel/locking/rtmutex.c @@ -484,6 +484,7 @@ static __always_inline bool __waiter_less(struct rb_n= ode *a, const struct rb_nod =20 static __always_inline void rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *wai= ter) + __must_hold(&lock->wait_lock) { lockdep_assert_held(&lock->wait_lock); =20 @@ -492,6 +493,7 @@ rt_mutex_enqueue(struct rt_mutex_base *lock, struct r= t_mutex_waiter *waiter) =20 static __always_inline void rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *wai= ter) + __must_hold(&lock->wait_lock) { lockdep_assert_held(&lock->wait_lock); =20 @@ -1092,6 +1094,7 @@ static int __sched rt_mutex_adjust_prio_chain(struc= t task_struct *task, static int __sched try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *tas= k, struct rt_mutex_waiter *waiter) + __must_hold(&lock->wait_lock) { lockdep_assert_held(&lock->wait_lock); =20 @@ -1319,6 +1322,7 @@ static int __sched task_blocks_on_rt_mutex(struct r= t_mutex_base *lock, */ static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh, struct rt_mutex_base *lock) + __must_hold(&lock->wait_lock) { struct rt_mutex_waiter *waiter; =20 diff --git a/kernel/locking/rtmutex_api.c b/kernel/locking/rtmutex_api.c index 124219aea46e..09675b9cb9bd 100644 --- a/kernel/locking/rtmutex_api.c +++ b/kernel/locking/rtmutex_api.c @@ -41,6 +41,7 @@ static __always_inline int __rt_mutex_lock_common(struc= t rt_mutex *lock, unsigned int state, struct lockdep_map *nest_lock, unsigned int subclass) + __cond_acquires(0, lock) { int ret; =20 @@ -66,12 +67,14 @@ EXPORT_SYMBOL(rt_mutex_base_init); * @subclass: the lockdep subclass */ void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int su= bclass) + __no_context_analysis /* ignoring the return value below is fine in thi= s case */ { __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass); } EXPORT_SYMBOL_GPL(rt_mutex_lock_nested); =20 void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lock= dep_map *nest_lock) + __no_context_analysis /* ignoring the return value below is fine in thi= s case */ { __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0); } @@ -157,6 +160,7 @@ void __sched rt_mutex_unlock(struct rt_mutex *lock) { mutex_release(&lock->dep_map, _RET_IP_); __rt_mutex_unlock(&lock->rtmutex); + __release(lock); } EXPORT_SYMBOL_GPL(rt_mutex_unlock); =20 @@ -182,6 +186,7 @@ int __sched __rt_mutex_futex_trylock(struct rt_mutex_= base *lock) */ bool __sched __rt_mutex_futex_unlock(struct rt_mutex_base *lock, struct rt_wake_q_head *wqh) + __must_hold(&lock->wait_lock) { lockdep_assert_held(&lock->wait_lock); =20 @@ -312,6 +317,7 @@ int __sched __rt_mutex_start_proxy_lock(struct rt_mut= ex_base *lock, struct rt_mutex_waiter *waiter, struct task_struct *task, struct wake_q_head *wake_q) + __must_hold(&lock->wait_lock) { int ret; =20 @@ -526,7 +532,7 @@ static __always_inline int __mutex_lock_common(struct= mutex *lock, unsigned int subclass, struct lockdep_map *nest_lock, unsigned long ip) - __acquires(lock) __no_context_analysis + __acquires(lock) { int ret; =20 @@ -648,7 +654,7 @@ EXPORT_SYMBOL(mutex_trylock); #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ =20 void __sched mutex_unlock(struct mutex *lock) - __releases(lock) __no_context_analysis + __releases(lock) { mutex_release(&lock->dep_map, _RET_IP_); __rt_mutex_unlock(&lock->rtmutex);