Skip to contentSkip to conversation

locking/rtmutex: Annotate API and implementation

PATCH Applied Bart Van Assche 5 participants 19 messages May 5 – May 8 Locking primitives

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 locking/rtmutex: Annotate API and implementation

Add lock context annotations to the rtmutex API and implementation and enable lock context analysis.

---

Changes compared to v1: fixed the CONFIG_PREEMPT_RT=y build.

 include/linux/rtmutex.h      | 22 +++++++++++++++-------
 kernel/locking/Makefile      |  1 +
 kernel/locking/rtmutex.c     |  5 +++++
 kernel/locking/rtmutex_api.c |  6 ++++++
 4 files changed, 27 insertions(+), 7 deletions(-)
include/linux/rtmutex.h +15 -7
index 78e7e588817c..9e1f012f89db 100644
@@ -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);
 
+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, struct lock_class_key *key);
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
-extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
-extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock);
+extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
+	__acquires(lock);
+extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_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_mutex *lock, struct lockdep_map *
 	} while (0)
 
 #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
 
-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);
 
-extern void rt_mutex_unlock(struct rt_mutex *lock);
+extern void rt_mutex_unlock(struct rt_mutex *lock)
+	__releases(lock);
 
 #endif
kernel/locking/Makefile +1 -0
index cee1901d4cff..24dc00e12aa6 100644
@@ -4,6 +4,7 @@
 KCOV_INSTRUMENT		:= n
 
 CONTEXT_ANALYSIS_mutex.o := y
+CONTEXT_ANALYSIS_rtmutex.o := y
 CONTEXT_ANALYSIS_rtmutex_api.o := y
 CONTEXT_ANALYSIS_ww_rt_mutex.o := y
 CONTEXT_ANALYSIS_rwsem.o := y
kernel/locking/rtmutex.c +5 -0
index 4f386ea6c792..69759fde7d10 100644
@@ -484,6 +484,7 @@ static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_nod
 
 static __always_inline void
 rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
+	__must_hold(&lock->wait_lock)
 {
 	lockdep_assert_held(&lock->wait_lock);
 
@@ -492,6 +493,7 @@ rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
 
 static __always_inline void
 rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
+	__must_hold(&lock->wait_lock)
 {
 	lockdep_assert_held(&lock->wait_lock);
 
@@ -1092,6 +1094,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
 static int __sched
 try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
 		     struct rt_mutex_waiter *waiter)
+	__must_hold(&lock->wait_lock)
 {
 	lockdep_assert_held(&lock->wait_lock);
 
@@ -1319,6 +1322,7 @@ static int __sched task_blocks_on_rt_mutex(struct rt_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;
 
@@ -1479,6 +1483,7 @@ static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
 }
 
 static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
+	__no_context_analysis
 {
 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
 		return;
kernel/locking/rtmutex_api.c +6 -0
index 124219aea46e..23ad997ddd65 100644
@@ -41,6 +41,7 @@ static __always_inline int __rt_mutex_lock_common(struct rt_mutex *lock,
 						  unsigned int state,
 						  struct lockdep_map *nest_lock,
 						  unsigned int subclass)
+	__cond_acquires(0, lock)
 {
 	int ret;
 
@@ -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 subclass)
+	__no_context_analysis /* ignoring the return value below is fine in this case */
 {
 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
 }
 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
 
 void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
+	__no_context_analysis /* ignoring the return value below is fine in this 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);
 
@@ -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);
 
@@ -312,6 +317,7 @@ int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
 					struct rt_mutex_waiter *waiter,
 					struct task_struct *task,
 					struct wake_q_head *wake_q)
+	__must_hold(&lock->wait_lock)
 {
 	int ret;
 
#2 lore raw
Quoted 6 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Bart Van Assche

Add lock context annotations to the rtmutex API and implementation and enable lock context analysis.

Is this in addition to commit 90bb681dcdf7e ("locking/rtmutex: Add context analysis") ?

Yes. This patch add context annotations in header file <linux/rtmutex.h>. Maybe I overlooked something but I have not yet seen any other patch that adds context annotations in that header file.

Thanks,

Bart.

#3 lore raw
Quoted 11 lines · Bart Van Assche
Quoted 6 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Bart Van Assche

Add lock context annotations to the rtmutex API and implementation and enable lock context analysis.

Is this in addition to commit 90bb681dcdf7e ("locking/rtmutex: Add context analysis") ?

Yes. This patch add context annotations in header file <linux/rtmutex.h>. Maybe I overlooked something but I have not yet seen any other patch that adds context annotations in that header file.

I see. I don't know why CONTEXT_ANALYSIS_rtmutex was skipped. Was this just a regular compile with newer clang or was there more to it? I do remember clang was not complaining but then context-analysis flag was missing…

Quoted 3 lines

Thanks,

Bart.

Sebastian

#4 lore raw
Quoted 4 lines · Sebastian Andrzej Siewior

I see. I don't know why CONTEXT_ANALYSIS_rtmutex was skipped. Was this just a regular compile with newer clang or was there more to it? I do remember clang was not complaining but then context-analysis flag was missing…

Hi Sebastian,

This was noticed by enabling CONFIG_WARN_CONTEXT_ANALYSIS_ALL=n. I'm not sure why this was not noticed earlier. Maybe as the result of an oversight?

Thanks,

Bart.

#5 lore raw
Quoted 8 lines · Bart Van Assche
Quoted 4 lines · Sebastian Andrzej Siewior

I see. I don't know why CONTEXT_ANALYSIS_rtmutex was skipped. Was this just a regular compile with newer clang or was there more to it? I do remember clang was not complaining but then context-analysis flag was missing…

Hi Sebastian,

Hi Bart,

Quoted 3 lines

This was noticed by enabling CONFIG_WARN_CONTEXT_ANALYSIS_ALL=n. I'm not sure why this was not noticed earlier. Maybe as the result of an oversight?

I have here clang-22 and make with CONFIG_WARN_CONTEXT_ANALYSIS_ALL=n and CONFIG_WARN_CONTEXT_ANALYSIS=y. I don't see any warning without your patch during "make kernel/locking/".

The change
CONTEXT_ANALYSIS_rtmutex.o := y

is not needed because rtmutex.o is not compiled on its own:

| $ git grep "rtmutex\.c" kernel/locking/
| kernel/locking/rtmutex_api.c:#include "rtmutex.c"
| kernel/locking/rwsem.c:#include "rtmutex.c"
| kernel/locking/spinlock_rt.c:#include "rtmutex.c"
| kernel/locking/ww_rt_mutex.c:#include "rtmutex.c"

or do I miss something here?
I tried with PREEMPT_RT enabled and disabled. Also with and without LOCKDEP in case it makes a change.

Quoted 3 lines

Thanks,

Bart.

Sebastian

#6 lore raw
Quoted 3 lines · Sebastian Andrzej Siewior

I have here clang-22 and make with CONFIG_WARN_CONTEXT_ANALYSIS_ALL=n and CONFIG_WARN_CONTEXT_ANALYSIS=y. I don't see any warning without your patch during "make kernel/locking/".

That is expected.

Quoted 4 lines

The change
CONTEXT_ANALYSIS_rtmutex.o := y

is not needed because rtmutex.o is not compiled on its own:

Agreed. Do you perhaps want me to repost this patch with that line left out?

In case it would not be clear, the goal of this patch is to let the compiler verify whether users of the rtmutex_*() API use this API correctly.

Thanks,

Bart.

#7 lore raw
Quoted 6 lines · Bart Van Assche
Quoted 3 lines · Sebastian Andrzej Siewior

I have here clang-22 and make with CONFIG_WARN_CONTEXT_ANALYSIS_ALL=n and CONFIG_WARN_CONTEXT_ANALYSIS=y. I don't see any warning without your patch during "make kernel/locking/".

That is expected.

Ah okay.

Quoted 11 lines
Quoted 4 lines

The change
CONTEXT_ANALYSIS_rtmutex.o := y

is not needed because rtmutex.o is not compiled on its own:

Agreed. Do you perhaps want me to repost this patch with that line left out?

In case it would not be clear, the goal of this patch is to let the compiler verify whether users of the rtmutex_*() API use this API correctly.

I somehow did not connect the dots. I just did comment on the patch after reading this one. But yes, please remove CONTEXT_ANALYSIS_rtmutex.o since it is not required.

Quoted 3 lines

Thanks,

Bart.

Sebastian

#8 lore raw
In reply to Bart Van Assche
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Gitweb:        https://git.kernel.org/tip/d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Tue, 05 May 2026 04:26:44 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 05 May 2026 12:50:49 +02:00

locking/rtmutex: Annotate API and implementation

Add lock context annotations to the rtmutex API and implementation and enable lock context analysis.

---
 include/linux/rtmutex.h      | 22 +++++++++++++++-------
 kernel/locking/Makefile      |  1 +
 kernel/locking/rtmutex.c     |  5 +++++
 kernel/locking/rtmutex_api.c |  6 ++++++
 4 files changed, 27 insertions(+), 7 deletions(-)
include/linux/rtmutex.h +15 -7
index 78e7e58..9e1f012 100644
@@ -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);
 
+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, struct lock_class_key *key);
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
-extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
-extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock);
+extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
+	__acquires(lock);
+extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_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_mutex *lock, struct lockdep_map *
 	} while (0)
 
 #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
 
-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);
 
-extern void rt_mutex_unlock(struct rt_mutex *lock);
+extern void rt_mutex_unlock(struct rt_mutex *lock)
+	__releases(lock);
 
 #endif
kernel/locking/Makefile +1 -0
index cee1901..24dc00e 100644
@@ -4,6 +4,7 @@
 KCOV_INSTRUMENT		:= n
 
 CONTEXT_ANALYSIS_mutex.o := y
+CONTEXT_ANALYSIS_rtmutex.o := y
 CONTEXT_ANALYSIS_rtmutex_api.o := y
 CONTEXT_ANALYSIS_ww_rt_mutex.o := y
 CONTEXT_ANALYSIS_rwsem.o := y
kernel/locking/rtmutex.c +5 -0
index 4f386ea..69759fd 100644
@@ -484,6 +484,7 @@ static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_nod
 
 static __always_inline void
 rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
+	__must_hold(&lock->wait_lock)
 {
 	lockdep_assert_held(&lock->wait_lock);
 
@@ -492,6 +493,7 @@ rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
 
 static __always_inline void
 rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
+	__must_hold(&lock->wait_lock)
 {
 	lockdep_assert_held(&lock->wait_lock);
 
@@ -1092,6 +1094,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
 static int __sched
 try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
 		     struct rt_mutex_waiter *waiter)
+	__must_hold(&lock->wait_lock)
 {
 	lockdep_assert_held(&lock->wait_lock);
 
@@ -1319,6 +1322,7 @@ static int __sched task_blocks_on_rt_mutex(struct rt_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;
 
@@ -1479,6 +1483,7 @@ static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
 }
 
 static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
+	__no_context_analysis
 {
 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
 		return;
kernel/locking/rtmutex_api.c +6 -0
index 124219a..23ad997 100644
@@ -41,6 +41,7 @@ static __always_inline int __rt_mutex_lock_common(struct rt_mutex *lock,
 						  unsigned int state,
 						  struct lockdep_map *nest_lock,
 						  unsigned int subclass)
+	__cond_acquires(0, lock)
 {
 	int ret;
 
@@ -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 subclass)
+	__no_context_analysis /* ignoring the return value below is fine in this case */
 {
 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
 }
 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
 
 void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
+	__no_context_analysis /* ignoring the return value below is fine in this 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);
 
@@ -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);
 
@@ -312,6 +317,7 @@ int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
 					struct rt_mutex_waiter *waiter,
 					struct task_struct *task,
 					struct wake_q_head *wake_q)
+	__must_hold(&lock->wait_lock)
 {
 	int ret;
 
#9 lore raw
Quoted 9 lines · On Tue, May 05, 2026 at 10:55:34AM -0000, tip-bot2 for Bart Van Assche wrote:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Gitweb:        https://git.kernel.org/tip/d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Tue, 05 May 2026 04:26:44 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 05 May 2026 12:50:49 +02:00
Quoted 8 lines

locking/rtmutex: Annotate API and implementation

Add lock context annotations to the rtmutex API and implementation and enable lock context analysis.

This breaks the build for me.

  $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 mrproper defconfig kernel/locking/rtmutex_api.o
  kernel/locking/rtmutex_api.c:93:1: error: expecting rt_mutex 'lock' to be held at the end of function [-Werror,-Wthread-safety-analysis]
     93 | }
        | ^
  kernel/locking/rtmutex_api.c:90:14: note: rt_mutex acquired here
     90 | void __sched rt_mutex_lock(struct rt_mutex *lock)
        |              ^
  1 error generated.

This is with LLVM 22.1.5, in case it matters.

Cheers,
Nathan

#10 lore raw
Quoted 30 lines · Nathan Chancellor
Quoted 9 lines · On Tue, May 05, 2026 at 10:55:34AM -0000, tip-bot2 for Bart Van Assche wrote:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Gitweb:        https://git.kernel.org/tip/d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Tue, 05 May 2026 04:26:44 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 05 May 2026 12:50:49 +02:00
Quoted 8 lines

locking/rtmutex: Annotate API and implementation

Add lock context annotations to the rtmutex API and implementation and enable lock context analysis.

This breaks the build for me.

   $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 mrproper defconfig kernel/locking/rtmutex_api.o
   kernel/locking/rtmutex_api.c:93:1: error: expecting rt_mutex 'lock' to be held at the end of function [-Werror,-Wthread-safety-analysis]
      93 | }
         | ^
   kernel/locking/rtmutex_api.c:90:14: note: rt_mutex acquired here
      90 | void __sched rt_mutex_lock(struct rt_mutex *lock)
         |              ^
   1 error generated.
Quoted 1 line

This is with LLVM 22.1.5, in case it matters.

Hi Nathan,

I think this is the same error as what has been reported by the kernel test robot (https://lore.kernel.org/all/[email protected]/).
I plan to post a new version with a fix for CONFIG_DEBUG_LOCK_ALLOC=n soon.

Thanks,

Bart.

#11 lore raw
Quoted 33 lines · Bart Van Assche
Quoted 30 lines · Nathan Chancellor
Quoted 9 lines · On Tue, May 05, 2026 at 10:55:34AM -0000, tip-bot2 for Bart Van Assche wrote:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Gitweb:        https://git.kernel.org/tip/d30a456aa7ce1bab05d9b364eeacad0c9cb10cc2
Author:        Bart Van Assche <[email protected]>
AuthorDate:    Tue, 05 May 2026 04:26:44 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 05 May 2026 12:50:49 +02:00
Quoted 8 lines

locking/rtmutex: Annotate API and implementation

Add lock context annotations to the rtmutex API and implementation and enable lock context analysis.

This breaks the build for me.

   $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 mrproper defconfig kernel/locking/rtmutex_api.o
   kernel/locking/rtmutex_api.c:93:1: error: expecting rt_mutex 'lock' to be held at the end of function [-Werror,-Wthread-safety-analysis]
      93 | }
         | ^
   kernel/locking/rtmutex_api.c:90:14: note: rt_mutex acquired here
      90 | void __sched rt_mutex_lock(struct rt_mutex *lock)
         |              ^
   1 error generated.
Quoted 1 line

This is with LLVM 22.1.5, in case it matters.

Quoted 7 lines

Hi Nathan,

I think this is the same error as what has been reported by the kernel test robot
(https://lore.kernel.org/all/[email protected]/).
I plan to post a new version with a fix for CONFIG_DEBUG_LOCK_ALLOC=n soon.

Thanks, I missed that report in trying to keep up with my inbox on vacation :) In the meantime, perhaps this should be dropped from locking/core since it is on top? Peter?

Signature · 2 lines
Cheers,
Nathan
maintainer
#12 lore raw
Quoted 3 lines · Nathan Chancellor

Thanks, I missed that report in trying to keep up with my inbox on vacation :) In the meantime, perhaps this should be dropped from locking/core since it is on top? Peter?

Yes, I was hoping the new version would show up quickly, I'll drop the thing for now.

#13 lore raw
Quoted 2 lines · Peter Zijlstra

Yes, I was hoping the new version would show up quickly, I'll drop the thing for now.

Please keep in mind that I was attending the LFS/MM/BPF summit this week and hence that I had less time than usual to contribute to the kernel.

Bart.

#14 lore raw
In reply to Bart Van Assche

include/linux/rtmutex.h static inline struct task_struct *rt_mutex_owner(struct rt_mutex_base *lock) · line 59

Quoted 7 lines · Bart Van Assche
include/linux/rtmutex.h +1 -0
@@ -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);
 
+context_lock_struct(rt_mutex);

What does this do? Shouldn't this define the struct?

Quoted 17 lines
include/linux/rtmutex.h +4 -2
 /**
  * The rt_mutex structure
  *
@@ -108,8 +110,10 @@ do { \
 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
 
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
-extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
-extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock);
+extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
+	__acquires(lock);
+extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_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 {								\

include/linux/rtmutex.h extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map * · line 125

Quoted 5 lines
include/linux/rtmutex.h +1 -1
@@ -118,15 +122,19 @@ extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *
 	} while (0)
 
 #else
-extern void rt_mutex_lock(struct rt_mutex *lock);
+extern void rt_mutex_lock(struct rt_mutex *lock) __acquires(lock);

So this is "one" thing where you add annotation to the rt_mutex*() API for external users. Then you add the wait_lock annotation. Different scope but okay.

…

Quoted 7 lines
kernel/locking/rtmutex_api.c +1 -0
@@ -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 subclass)
+	__no_context_analysis /* ignoring the return value below is fine in this case */

kernel/locking/rtmutex_api.c EXPORT_SYMBOL(rt_mutex_base_init); · line 80

Quoted 10 lines
kernel/locking/rtmutex_api.c +1 -0
 {
 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
 }
 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
 
 void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
+	__no_context_analysis /* ignoring the return value below is fine in this case */
 {
 	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0);
 }

*Why* is it okay? Because the void always acquire the lock and only the conditional locking (which can be interrupted by signal/ timeout) return an error if they failed to acquire the lock. Something like that would be nice for the comment.

Not sure if "__no_context_analysis" is the right thing to do here.
__acquires(lock) __no_context_analysis

might be better if just __acquires leads to trouble.

Sebastian

#15 lore raw
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 1 line · Bart Van Assche

+context_lock_struct(rt_mutex);

What does this do? Shouldn't this define the struct?

This enables context locking support for struct rt_mutex. I placed context_lock_struct() on a line by itself because in my opinion that results in a header file that is easier to read compared to
context_lock_struct(name) { ... }.

Quoted 12 lines
Quoted 12 lines
kernel/locking/rtmutex_api.c +1 -0
@@ -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 subclass)
+	__no_context_analysis /* ignoring the return value below is fine in this case */
  {
  	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
  }
  EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
 
Quoted 9 lines

kernel/locking/rtmutex_api.c EXPORT_SYMBOL(rt_mutex_base_init); · line 80

Quoted 5 lines
kernel/locking/rtmutex_api.c +1 -0
  void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
+	__no_context_analysis /* ignoring the return value below is fine in this case */
  {
  	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0);
  }

*Why* is it okay? Because the void always acquire the lock and only the conditional locking (which can be interrupted by signal/ timeout) return an error if they failed to acquire the lock.

Yes, that's correct.

Quoted 6 lines

Something like that would be nice for the comment.

Not sure if "__no_context_analysis" is the right thing to do here.
__acquires(lock) __no_context_analysis

might be better if just __acquires leads to trouble.

There is an alternative that does not require __no_context_analysis:

void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass) {
int ret = __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
NULL, subclass);

        BUG_ON(ret);
}

EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);

Please let me know which style you prefer.

Thanks,

Bart.

#16 lore raw
Quoted 10 lines · Bart Van Assche
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 1 line · Bart Van Assche

+context_lock_struct(rt_mutex);

What does this do? Shouldn't this define the struct?

This enables context locking support for struct rt_mutex. I placed context_lock_struct() on a line by itself because in my opinion that results in a header file that is easier to read compared to context_lock_struct(name) { ... }.

Hmm. This was the confusing part, because everything else such as rwlock, rw_semaphore, mutex, … use that way.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
kernel/locking/rtmutex_api.c +1 -0
@@ -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 subclass)
+	__no_context_analysis /* ignoring the return value below is fine in this case */
  {
  	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
  }
  EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
  void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
Quoted 8 lines

kernel/locking/rtmutex_api.c EXPORT_SYMBOL(rt_mutex_base_init); · line 80

Quoted 4 lines
kernel/locking/rtmutex_api.c +1 -0
+	__no_context_analysis /* ignoring the return value below is fine in this case */
  {
  	__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0);
  }

*Why* is it okay? Because the void always acquire the lock and only the conditional locking (which can be interrupted by signal/ timeout) return an error if they failed to acquire the lock.

Quoted 1 line

Yes, that's correct.

I meant this as an improved comment ;)

Quoted 15 lines
Quoted 6 lines

Something like that would be nice for the comment.

Not sure if "__no_context_analysis" is the right thing to do here.
__acquires(lock) __no_context_analysis

might be better if just __acquires leads to trouble.

There is an alternative that does not require __no_context_analysis:

void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
{
int ret = __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL,
subclass);

Quoted 5 lines
        BUG_ON(ret);
}

EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);

Please let me know which style you prefer.

Hmm. This mostly reassembles __mutex_lock() from mutex.c which does the same thing. Couldn't we get away doing the same thing meaning a __cond_acquires() on those with a return value and a __acquire() in the void case? I think it would make sense to keep those two close in terms of annotations.

Quoted 3 lines

Thanks,

Bart.

Sebastian

#17 lore raw
Quoted 5 lines · Sebastian Andrzej Siewior

Hmm. This mostly reassembles __mutex_lock() from mutex.c which does the same thing. Couldn't we get away doing the same thing meaning a __cond_acquires() on those with a return value and a __acquire() in the void case? I think it would make sense to keep those two close in terms of annotations.

Please take a look at the rt_mutex_lock() changes in the diff below.

Thanks,

Bart.

include/linux/rtmutex.h +2 -0
index 78e7e588817c..9e1f012f89db 100644
@@ -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);
 
+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, 

struct lock_class_key *key);

#ifdef CONFIG_DEBUG_LOCK_ALLOC
-extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass); -extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock); +extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
+ __acquires(lock);
+extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_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_mutex *lock, struct lockdep_map *
} while (0)

#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

-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);

-extern void rt_mutex_unlock(struct rt_mutex *lock); +extern void rt_mutex_unlock(struct rt_mutex *lock)
+ __releases(lock);

#endif

kernel/locking/rtmutex.c +0 -0
index 4f386ea6c792..69759fde7d10 100644
@@ -484,6 +484,7 @@ static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_nod
 
 static __always_inline void
 rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter 

*waiter)
+ __must_hold(&lock->wait_lock)
{
lockdep_assert_held(&lock->wait_lock);

@@ -492,6 +493,7 @@ rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)

static __always_inline void
rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter
*waiter)
+ __must_hold(&lock->wait_lock)
{
lockdep_assert_held(&lock->wait_lock);

@@ -1092,6 +1094,7 @@ static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
static int __sched
try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
struct rt_mutex_waiter *waiter)
+ __must_hold(&lock->wait_lock)
{
lockdep_assert_held(&lock->wait_lock);

@@ -1319,6 +1322,7 @@ static int __sched task_blocks_on_rt_mutex(struct rt_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;

@@ -1479,6 +1483,7 @@ static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
}

static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
+ __no_context_analysis
{
if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
return;

kernel/locking/rtmutex_api.c +1 -0
index 124219aea46e..f9345fa22286 100644
@@ -41,6 +41,7 @@ static __always_inline int __rt_mutex_lock_common(struct rt_mutex *lock,
 						  unsigned int state,
 						  struct lockdep_map *nest_lock,
 						  unsigned int subclass)
+	__cond_acquires(0, lock)
 {
 	int ret;
 
@@ -67,13 +68,19 @@ EXPORT_SYMBOL(rt_mutex_base_init);
  */
 void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int 

subclass)
{
- __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
+ if (__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass) == 0)
+ return;
+ WARN_ON_ONCE(true);
+ __acquire(lock); /* to keep the compiler happy */
}
EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);

void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct
lockdep_map *nest_lock)
{
- __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0);
+ if (__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0) == 0)
+ return;
+ WARN_ON_ONCE(true);
+ __acquire(lock); /* to keep the compiler happy */
}
EXPORT_SYMBOL_GPL(_rt_mutex_lock_nest_lock);

@@ -86,7 +93,10 @@ EXPORT_SYMBOL_GPL(_rt_mutex_lock_nest_lock);
*/
void __sched rt_mutex_lock(struct rt_mutex *lock)
{
- __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, 0);
+ if (__rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, 0) == 0)
+ return;
+ WARN_ON_ONCE(true);
+ __acquire(lock); /* to keep the compiler happy */
}
EXPORT_SYMBOL_GPL(rt_mutex_lock);
#endif
@@ -157,6 +167,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);

@@ -182,6 +193,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);

@@ -312,6 +324,7 @@ int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
struct rt_mutex_waiter *waiter, struct task_struct *task,
struct wake_q_head *wake_q)
+ __must_hold(&lock->wait_lock)
{
int ret;

#18 lore raw
Quoted 8 lines · Bart Van Assche
Quoted 5 lines · Sebastian Andrzej Siewior

Hmm. This mostly reassembles __mutex_lock() from mutex.c which does the same thing. Couldn't we get away doing the same thing meaning a __cond_acquires() on those with a return value and a __acquire() in the void case? I think it would make sense to keep those two close in terms of annotations.

Please take a look at the rt_mutex_lock() changes in the diff below.

So that warning keeps all trouble away and shouldn't trigger anyway. Okay.

Quoted 3 lines

Thanks,

Bart.

Sebastian