[PATCH 2/3] locking: Add lock context support in do_raw_{read,write}_trylock()

Bart Van Assche posted 3 patches 3 weeks, 3 days ago
[PATCH 2/3] locking: Add lock context support in do_raw_{read,write}_trylock()
Posted by Bart Van Assche 3 weeks, 3 days ago
Convert do_raw_{read,write}_trylock() from macros into inline functions
and annotate these inline functions with __cond_acquires_shared() or
__cond_acquires() as appropriate. This change is necessary to build
kernel drivers or subsystems that use rwlock synchronization objects with
lock context analysis enabled. The return type 'int' matches the return
type for CONFIG_DEBUG_SPINLOCK=y.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 include/linux/rwlock.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/linux/rwlock.h b/include/linux/rwlock.h
index 21ceefc4a49f..4e67cd934d8f 100644
--- a/include/linux/rwlock.h
+++ b/include/linux/rwlock.h
@@ -37,10 +37,20 @@ extern int do_raw_write_trylock(rwlock_t *lock) __cond_acquires(true, lock);
  extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock);
 #else
 # define do_raw_read_lock(rwlock)	do {__acquire_shared(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
-# define do_raw_read_trylock(rwlock)	arch_read_trylock(&(rwlock)->raw_lock)
+static inline int do_raw_read_trylock(rwlock_t *rwlock)
+	__cond_acquires_shared(true, rwlock)
+	__no_context_analysis
+{
+	return arch_read_trylock(&(rwlock)->raw_lock);
+}
 # define do_raw_read_unlock(rwlock)	do {arch_read_unlock(&(rwlock)->raw_lock); __release_shared(lock); } while (0)
 # define do_raw_write_lock(rwlock)	do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
-# define do_raw_write_trylock(rwlock)	arch_write_trylock(&(rwlock)->raw_lock)
+static inline int do_raw_write_trylock(rwlock_t *rwlock)
+	__cond_acquires(true, rwlock)
+	__no_context_analysis
+{
+	return arch_write_trylock(&(rwlock)->raw_lock);
+}
 # define do_raw_write_unlock(rwlock)	do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
 #endif
 
Re: [PATCH 2/3] locking: Add lock context support in do_raw_{read,write}_trylock()
Posted by Marco Elver 2 weeks, 6 days ago
On Fri, Mar 13, 2026 at 10:15AM -0700, Bart Van Assche wrote:
> Convert do_raw_{read,write}_trylock() from macros into inline functions
> and annotate these inline functions with __cond_acquires_shared() or
> __cond_acquires() as appropriate. This change is necessary to build
> kernel drivers or subsystems that use rwlock synchronization objects with
> lock context analysis enabled. The return type 'int' matches the return
> type for CONFIG_DEBUG_SPINLOCK=y.
> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>  include/linux/rwlock.h | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/rwlock.h b/include/linux/rwlock.h
> index 21ceefc4a49f..4e67cd934d8f 100644
> --- a/include/linux/rwlock.h
> +++ b/include/linux/rwlock.h
> @@ -37,10 +37,20 @@ extern int do_raw_write_trylock(rwlock_t *lock) __cond_acquires(true, lock);
>   extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock);
>  #else
>  # define do_raw_read_lock(rwlock)	do {__acquire_shared(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
> -# define do_raw_read_trylock(rwlock)	arch_read_trylock(&(rwlock)->raw_lock)
> +static inline int do_raw_read_trylock(rwlock_t *rwlock)
> +	__cond_acquires_shared(true, rwlock)
> +	__no_context_analysis
> +{
> +	return arch_read_trylock(&(rwlock)->raw_lock);
> +}
>  # define do_raw_read_unlock(rwlock)	do {arch_read_unlock(&(rwlock)->raw_lock); __release_shared(lock); } while (0)
>  # define do_raw_write_lock(rwlock)	do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
> -# define do_raw_write_trylock(rwlock)	arch_write_trylock(&(rwlock)->raw_lock)
> +static inline int do_raw_write_trylock(rwlock_t *rwlock)
> +	__cond_acquires(true, rwlock)
> +	__no_context_analysis
> +{
> +	return arch_write_trylock(&(rwlock)->raw_lock);
> +}
>  # define do_raw_write_unlock(rwlock)	do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
>  #endif

The pre-existing annotations were wrong (from Sparse era, introduced in
b97c4bc16734a) - I think you need to include this to build with
CONFIG_DEBUG_SPINLOCK=n now:

diff --git a/include/linux/rwlock.h b/include/linux/rwlock.h
index 3d1834ef15d5..9e577e649bf9 100644
--- a/include/linux/rwlock.h
+++ b/include/linux/rwlock.h
@@ -36,22 +36,22 @@ do {								\
  extern int do_raw_write_trylock(rwlock_t *lock);
  extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock);
 #else
-# define do_raw_read_lock(rwlock)	do {__acquire_shared(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
+# define do_raw_read_lock(rwlock)	do {__acquire_shared(rwlock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
 static inline int do_raw_read_trylock(rwlock_t *rwlock)
 	__cond_acquires_shared(true, rwlock)
 	__no_context_analysis
 {
 	return arch_read_trylock(&(rwlock)->raw_lock);
 }
-# define do_raw_read_unlock(rwlock)	do {arch_read_unlock(&(rwlock)->raw_lock); __release_shared(lock); } while (0)
-# define do_raw_write_lock(rwlock)	do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
+# define do_raw_read_unlock(rwlock)	do {arch_read_unlock(&(rwlock)->raw_lock); __release_shared(rwlock); } while (0)
+# define do_raw_write_lock(rwlock)	do {__acquire(rwlock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
 static inline int do_raw_write_trylock(rwlock_t *rwlock)
 	__cond_acquires(true, rwlock)
 	__no_context_analysis
 {
 	return arch_write_trylock(&(rwlock)->raw_lock);
 }
-# define do_raw_write_unlock(rwlock)	do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
+# define do_raw_write_unlock(rwlock)	do {arch_write_unlock(&(rwlock)->raw_lock); __release(rwlock); } while (0)
 #endif
 
 /*
diff --git a/include/linux/rwlock_api_smp.h b/include/linux/rwlock_api_smp.h
index 61a852609eab..25dfee624cf1 100644
--- a/include/linux/rwlock_api_smp.h
+++ b/include/linux/rwlock_api_smp.h
@@ -116,6 +116,7 @@ _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
 #endif
 
 static inline int __raw_read_trylock(rwlock_t *lock)
+	__cond_acquires_shared(true, lock)
 {
 	preempt_disable();
 	if (do_raw_read_trylock(lock)) {
@@ -127,6 +128,7 @@ static inline int __raw_read_trylock(rwlock_t *lock)
 }
 
 static inline int __raw_write_trylock(rwlock_t *lock)
+	__cond_acquires(true, lock)
 {
 	preempt_disable();
 	if (do_raw_write_trylock(lock)) {
[tip: locking/core] locking: Add lock context support in do_raw_{read,write}_trylock()
Posted by tip-bot2 for Bart Van Assche 2 weeks, 6 days ago
The following commit has been merged into the locking/core branch of tip:

Commit-ID:     c4d3b8c77d85082d32250c505beb1d9e46ee47ee
Gitweb:        https://git.kernel.org/tip/c4d3b8c77d85082d32250c505beb1d9e46ee47ee
Author:        Bart Van Assche <bvanassche@acm.org>
AuthorDate:    Fri, 13 Mar 2026 10:15:08 -07:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Mon, 16 Mar 2026 13:16:50 +01:00

locking: Add lock context support in do_raw_{read,write}_trylock()

Convert do_raw_{read,write}_trylock() from macros into inline functions
and annotate these inline functions with __cond_acquires_shared() or
__cond_acquires() as appropriate. This change is necessary to build
kernel drivers or subsystems that use rwlock synchronization objects with
lock context analysis enabled. The return type 'int' matches the return
type for CONFIG_DEBUG_SPINLOCK=y.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260313171510.230998-3-bvanassche@acm.org
---
 include/linux/rwlock.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/linux/rwlock.h b/include/linux/rwlock.h
index 21ceefc..4e67cd9 100644
--- a/include/linux/rwlock.h
+++ b/include/linux/rwlock.h
@@ -37,10 +37,20 @@ extern int do_raw_write_trylock(rwlock_t *lock) __cond_acquires(true, lock);
  extern void do_raw_write_unlock(rwlock_t *lock) __releases(lock);
 #else
 # define do_raw_read_lock(rwlock)	do {__acquire_shared(lock); arch_read_lock(&(rwlock)->raw_lock); } while (0)
-# define do_raw_read_trylock(rwlock)	arch_read_trylock(&(rwlock)->raw_lock)
+static inline int do_raw_read_trylock(rwlock_t *rwlock)
+	__cond_acquires_shared(true, rwlock)
+	__no_context_analysis
+{
+	return arch_read_trylock(&(rwlock)->raw_lock);
+}
 # define do_raw_read_unlock(rwlock)	do {arch_read_unlock(&(rwlock)->raw_lock); __release_shared(lock); } while (0)
 # define do_raw_write_lock(rwlock)	do {__acquire(lock); arch_write_lock(&(rwlock)->raw_lock); } while (0)
-# define do_raw_write_trylock(rwlock)	arch_write_trylock(&(rwlock)->raw_lock)
+static inline int do_raw_write_trylock(rwlock_t *rwlock)
+	__cond_acquires(true, rwlock)
+	__no_context_analysis
+{
+	return arch_write_trylock(&(rwlock)->raw_lock);
+}
 # define do_raw_write_unlock(rwlock)	do {arch_write_unlock(&(rwlock)->raw_lock); __release(lock); } while (0)
 #endif