More lock context annotation fixes
cover More lock context annotation fixes
Hi Peter,
This patch series includes additional fixes for lock context annotations for locking primitives. Please consider this patch series for inclusion in the upstream kernel.
Thanks,
Bart.
Bart Van Assche (3):
locking: Fix rwlock support in <linux/spinlock_up.h>
locking: Add lock context support in do_raw_{read,write}_trylock()
locking: Add lock context annotations in the spinlock implementation
include/linux/rwlock.h | 14 ++++++++++++-- include/linux/spinlock_up.h | 20 ++++++++++---------- kernel/locking/spinlock.c | 12 ++++++++---- 3 files changed, 30 insertions(+), 16 deletions(-)
PATCH 1/3 locking: Fix rwlock support in <linux/spinlock_up.h>
Architecture support for rwlocks must be available whether or not CONFIG_DEBUG_SPINLOCK has been defined. Move the definitions of the arch_{read,write}_{lock,trylock,unlock}() macros such that these become visbile if CONFIG_DEBUG_SPINLOCK=n.
This patch prepares for converting do_raw_{read,write}_trylock() into inline functions. Without this patch that conversion triggers a build failure for UP architectures, e.g. arm-ep93xx. I used the following kernel configuration to build the kernel for that architecture:
CONFIG_ARCH_MULTIPLATFORM=y
CONFIG_ARCH_MULTI_V7=n
CONFIG_ATAGS=y
CONFIG_MMU=y
CONFIG_ARCH_MULTI_V4T=y
CONFIG_CPU_LITTLE_ENDIAN=y
CONFIG_ARCH_EP93XX=y
--- include/linux/spinlock_up.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
include/linux/spinlock_up.h +10 -10
@@ -48,16 +48,6 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock) lock->slock = 1; } -/* -* Read-write spinlocks. No debug version. -*/ -#define arch_read_lock (lock) do { barrier(); (void)(lock); } while (0) -#define arch_write_lock (lock) do { barrier(); (void)(lock); } while (0) -#define arch_read_trylock (lock) ({ barrier(); (void)(lock); 1; }) -#define arch_write_trylock (lock) ({ barrier(); (void)(lock); 1; }) -#define arch_read_unlock (lock) do { barrier(); (void)(lock); } while (0) -#define arch_write_unlock (lock) do { barrier(); (void)(lock); } while (0) -#else /* DEBUG_SPINLOCK */ #define arch_spin_is_locked (lock) ((void)(lock), 0)/* for sched/core.c and kernel_lock.c: */
@@ -68,4 +58,14 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock)#define arch_spin_is_contended (lock) (((void)(lock), 0)) +/* +* Read-write spinlocks. No debug version. +*/ +#define arch_read_lock (lock) do { barrier(); (void)(lock); } while (0) +#define arch_write_lock (lock) do { barrier(); (void)(lock); } while (0) +#define arch_read_trylock (lock) ({ barrier(); (void)(lock); 1; }) +#define arch_write_trylock (lock) ({ barrier(); (void)(lock); 1; }) +#define arch_read_unlock (lock) do { barrier(); (void)(lock); } while (0) +#define arch_write_unlock (lock) do { barrier(); (void)(lock); } while (0) +#endif /* __LINUX_SPINLOCK_UP_H */
[tip: locking/core] locking: Fix rwlock support in <linux/spinlock_up.h>
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: 756a0e011cfca0b45a48464aa25b05d9a9c2fb0b Gitweb: https://git.kernel.org/tip/756a0e011cfca0b45a48464aa25b05d9a9c2fb0b Author: Bart Van Assche <[email protected]> AuthorDate: Fri, 13 Mar 2026 10:15:07 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 16 Mar 2026 13:16:50 +01:00
locking: Fix rwlock support in <linux/spinlock_up.h>
Architecture support for rwlocks must be available whether or not CONFIG_DEBUG_SPINLOCK has been defined. Move the definitions of the arch_{read,write}_{lock,trylock,unlock}() macros such that these become visbile if CONFIG_DEBUG_SPINLOCK=n.
This patch prepares for converting do_raw_{read,write}_trylock() into inline functions. Without this patch that conversion triggers a build failure for UP architectures, e.g. arm-ep93xx. I used the following kernel configuration to build the kernel for that architecture:
CONFIG_ARCH_MULTIPLATFORM=y
CONFIG_ARCH_MULTI_V7=n
CONFIG_ATAGS=y
CONFIG_MMU=y
CONFIG_ARCH_MULTI_V4T=y
CONFIG_CPU_LITTLE_ENDIAN=y
CONFIG_ARCH_EP93XX=y
--- include/linux/spinlock_up.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
include/linux/spinlock_up.h +10 -10
@@ -48,16 +48,6 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock) lock->slock = 1; } -/* -* Read-write spinlocks. No debug version. -*/ -#define arch_read_lock (lock) do { barrier(); (void)(lock); } while (0) -#define arch_write_lock (lock) do { barrier(); (void)(lock); } while (0) -#define arch_read_trylock (lock) ({ barrier(); (void)(lock); 1; }) -#define arch_write_trylock (lock) ({ barrier(); (void)(lock); 1; }) -#define arch_read_unlock (lock) do { barrier(); (void)(lock); } while (0) -#define arch_write_unlock (lock) do { barrier(); (void)(lock); } while (0) -#else /* DEBUG_SPINLOCK */ #define arch_spin_is_locked (lock) ((void)(lock), 0)/* for sched/core.c and kernel_lock.c: */
@@ -68,4 +58,14 @@ static inline void arch_spin_unlock(arch_spinlock_t *lock)#define arch_spin_is_contended (lock) (((void)(lock), 0)) +/* +* Read-write spinlocks. No debug version. +*/ +#define arch_read_lock (lock) do { barrier(); (void)(lock); } while (0) +#define arch_write_lock (lock) do { barrier(); (void)(lock); } while (0) +#define arch_read_trylock (lock) ({ barrier(); (void)(lock); 1; }) +#define arch_write_trylock (lock) ({ barrier(); (void)(lock); 1; }) +#define arch_read_unlock (lock) do { barrier(); (void)(lock); } while (0) +#define arch_write_unlock (lock) do { barrier(); (void)(lock); } while (0) +#endif /* __LINUX_SPINLOCK_UP_H */
PATCH 2/3 locking: Add lock context support in do_raw_{read,write}_trylock()
In reply to Bart Van AsscheConvert 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.
--- include/linux/rwlock.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
include/linux/rwlock.h +12 -2
@@ -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()
Quoted 28 lines · Bart Van Assche
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 forCONFIG_DEBUG_SPINLOCK=y.Signed-off-by Bart Van Assche <[email protected]>--- include/linux/rwlock.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)include/linux/rwlock.h +6
-1@@ -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)
include/linux/rwlock.h extern int do_raw_write_trylock(rwlock_t *lock) __cond_acquires(true, lock); · line 55
Quoted 10 lines
include/linux/rwlock.h +6
-1# 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:
include/linux/rwlock.h +4 -4
@@ -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 /*
include/linux/rwlock_api_smp.h +2 -0
@@ -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()
In reply to Bart Van AsscheThe 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: c4d3b8c77d85082d32250c505beb1d9e46ee47ee Gitweb: https://git.kernel.org/tip/c4d3b8c77d85082d32250c505beb1d9e46ee47ee Author: Bart Van Assche <[email protected]> AuthorDate: Fri, 13 Mar 2026 10:15:08 -07:00 Committer: Peter Zijlstra <[email protected]> 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.
--- include/linux/rwlock.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
include/linux/rwlock.h +12 -2
@@ -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
PATCH 3/3 locking: Add lock context annotations in the spinlock implementation
In reply to Bart Van AsscheMake the spinlock implementation compatible with lock context analysis (CONTEXT_ANALYSIS := 1) by adding lock context annotations to the _raw_##op##_...() macros.
--- kernel/locking/spinlock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/locking/spinlock.c +8 -4
@@ -64,8 +64,9 @@ EXPORT_PER_CPU_SYMBOL(__mmiowb_state);* time (making _this_ CPU preemptible if possible), and we also signal * towards that other CPU that it should break the lock ASAP. */ -#define BUILD_LOCK_OPS (op, locktype) \ +#define BUILD_LOCK_OPS (op, locktype, lock_ctx_op) \ static void __lockfunc __raw_##op##_lock(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ for (;;) { \ preempt_disable(); \
@@ -78,6 +79,7 @@ static void __lockfunc __raw_##op##_lock(locktype##_t *lock) \ } \ \ static unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ unsigned long flags; \ \
@@ -96,11 +98,13 @@ static unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \ } \ \ static void __lockfunc __raw_##op##_lock_irq(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ _raw_##op##_lock_irqsave(lock); \ } \ \ static void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ unsigned long flags; \ \
@@ -123,11 +127,11 @@ static void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \* __[spin|read|write]_lock_irqsave() * __[spin|read|write]_lock_bh() */ -BUILD_LOCK_OPS (spin, raw_spinlock); +BUILD_LOCK_OPS (spin, raw_spinlock, __acquires);#ifndef CONFIG_PREEMPT_RT -BUILD_LOCK_OPS (read, rwlock); -BUILD_LOCK_OPS (write, rwlock); +BUILD_LOCK_OPS (read, rwlock, __acquires_shared); +BUILD_LOCK_OPS (write, rwlock, __acquires);#endif #endif
Re: [PATCH 3/3] locking: Add lock context annotations in the spinlock implementation
Quoted 3 lines · Bart Van Assche
Make the spinlock implementation compatible with lock context analysis (
CONTEXT_ANALYSIS:= 1) by adding lock context annotations to the
CONTEXT_ANALYSIS := y
Quoted 59 lines
_raw_##op##_...() macros.
Signed-off-by Bart Van Assche <[email protected]>--- kernel/locking/spinlock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)kernel/locking/spinlock.c +8
-4@@ -64,8 +64,9 @@ EXPORT_PER_CPU_SYMBOL(__mmiowb_state);* time (making _this_ CPU preemptible if possible), and we also signal * towards that other CPU that it should break the lock ASAP. */ -#define BUILD_LOCK_OPS (op, locktype) \ +#define BUILD_LOCK_OPS (op, locktype, lock_ctx_op) \ static void __lockfunc __raw_##op##_lock(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ for (;;) { \ preempt_disable(); \@@ -78,6 +79,7 @@ static void __lockfunc __raw_##op##_lock(locktype##_t *lock) \ } \ \ static unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ unsigned long flags; \ \@@ -96,11 +98,13 @@ static unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \ } \ \ static void __lockfunc __raw_##op##_lock_irq(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ _raw_##op##_lock_irqsave(lock); \ } \ \ static void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ unsigned long flags; \ \@@ -123,11 +127,11 @@ static void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \* __[spin|read|write]_lock_irqsave() * __[spin|read|write]_lock_bh() */ -BUILD_LOCK_OPS (spin, raw_spinlock); +BUILD_LOCK_OPS (spin, raw_spinlock, __acquires);#ifndef CONFIG_PREEMPT_RT -BUILD_LOCK_OPS (read, rwlock); -BUILD_LOCK_OPS (write, rwlock); +BUILD_LOCK_OPS (read, rwlock, __acquires_shared); +BUILD_LOCK_OPS (write, rwlock, __acquires);#endif #endif
[tip: locking/core] locking: Add lock context annotations in the spinlock implementation
In reply to Bart Van AsscheThe 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: b06e988c4c52ce8750616ea9b23c8bd3b611b931 Gitweb: https://git.kernel.org/tip/b06e988c4c52ce8750616ea9b23c8bd3b611b931 Author: Bart Van Assche <[email protected]> AuthorDate: Fri, 13 Mar 2026 10:15:09 -07:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 16 Mar 2026 13:16:50 +01:00
locking: Add lock context annotations in the spinlock implementation
Make the spinlock implementation compatible with lock context analysis (CONTEXT_ANALYSIS := 1) by adding lock context annotations to the _raw_##op##_...() macros.
--- kernel/locking/spinlock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/locking/spinlock.c +8 -4
@@ -64,8 +64,9 @@ EXPORT_PER_CPU_SYMBOL(__mmiowb_state);* time (making _this_ CPU preemptible if possible), and we also signal * towards that other CPU that it should break the lock ASAP. */ -#define BUILD_LOCK_OPS (op, locktype) \ +#define BUILD_LOCK_OPS (op, locktype, lock_ctx_op) \ static void __lockfunc __raw_##op##_lock(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ for (;;) { \ preempt_disable(); \
@@ -78,6 +79,7 @@ static void __lockfunc __raw_##op##_lock(locktype##_t *lock) \ } \ \ static unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ unsigned long flags; \ \
@@ -96,11 +98,13 @@ static unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \ } \ \ static void __lockfunc __raw_##op##_lock_irq(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ _raw_##op##_lock_irqsave(lock); \ } \ \ static void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \ + lock_ctx_op(lock) \ { \ unsigned long flags; \ \
@@ -123,11 +127,11 @@ static void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \* __[spin|read|write]_lock_irqsave() * __[spin|read|write]_lock_bh() */ -BUILD_LOCK_OPS (spin, raw_spinlock); +BUILD_LOCK_OPS (spin, raw_spinlock, __acquires);#ifndef CONFIG_PREEMPT_RT -BUILD_LOCK_OPS (read, rwlock); -BUILD_LOCK_OPS (write, rwlock); +BUILD_LOCK_OPS (read, rwlock, __acquires_shared); +BUILD_LOCK_OPS (write, rwlock, __acquires);#endif #endif