[PATCH 03/17] locking/rwsem: assert static storage for semaphore definitions

From: Yury Norov

Date: Mon Sep 14 2026 - 23:08:33 EST


The static rwsem initializers rely on persistent lockdep class keys,
both for their own maps and for embedded locks or wait queues.

Enforce static storage in DECLARE_RWSEM(), including PREEMPT_RT, and in
__DEFINE_PERCPU_RWSEM(), covering both public per-CPU rwsem declarations.
Automatic objects must use init_rwsem() or percpu_init_rwsem().

Assisted-by: OpenAI Codex
Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
---
include/linux/percpu-rwsem.h | 4 +++-
include/linux/rwsem.h | 7 +++++--
2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/include/linux/percpu-rwsem.h b/include/linux/percpu-rwsem.h
index 39d5bf8e6562..680c3efc14c8 100644
--- a/include/linux/percpu-rwsem.h
+++ b/include/linux/percpu-rwsem.h
@@ -2,6 +2,7 @@
#ifndef _LINUX_PERCPU_RWSEM_H
#define _LINUX_PERCPU_RWSEM_H

+#include <linux/compiler.h>
#include <linux/atomic.h>
#include <linux/percpu.h>
#include <linux/rcuwait.h>
@@ -36,7 +37,8 @@ is_static struct percpu_rw_semaphore name = { \
.waiters = __WAIT_QUEUE_HEAD_INITIALIZER(name.waiters), \
.block = ATOMIC_INIT(0), \
__PERCPU_RWSEM_DEP_MAP_INIT(name) \
-}
+}; \
+ ASSERT_STATIC_STORAGE(name)

#define DEFINE_PERCPU_RWSEM(name) \
__DEFINE_PERCPU_RWSEM(name, /* not static */)
diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
index 6a1a7bae5f81..faf52c0c8dd3 100644
--- a/include/linux/rwsem.h
+++ b/include/linux/rwsem.h
@@ -8,6 +8,7 @@
#ifndef _LINUX_RWSEM_H
#define _LINUX_RWSEM_H

+#include <linux/compiler.h>
#include <linux/linkage.h>

#include <linux/types.h>
@@ -111,7 +112,8 @@ static inline void rwsem_assert_held_write_nolockdep(const struct rw_semaphore *
__RWSEM_DEP_MAP_INIT(name) }

#define DECLARE_RWSEM(name) \
- struct rw_semaphore name = __RWSEM_INITIALIZER(name)
+ struct rw_semaphore name = __RWSEM_INITIALIZER(name); \
+ ASSERT_STATIC_STORAGE(name)

extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
struct lock_class_key *key);
@@ -164,7 +166,8 @@ context_lock_struct(rw_semaphore) {
}

#define DECLARE_RWSEM(lockname) \
- struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
+ struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname); \
+ ASSERT_STATIC_STORAGE(lockname)

extern void __init_rwsem(struct rw_semaphore *rwsem, const char *name,
struct lock_class_key *key);
--
2.53.0