[PATCH 04/17] locking/semaphore: assert static storage for DEFINE_SEMAPHORE()

From
Yury Norov <hidden>
Date
2026-09-15 03:04:14
Also in
linux-fsdevel, linux-mm, linux-rt-devel, lkml, llvm, netdev, rcu, virtualization
Reply to
Yury Norov, 2026-09-15
DEFINE_SEMAPHORE() statically initializes its embedded raw spinlock.
Using it for an automatic local semaphore leaves lockdep without a
persistent class key.

Reject this use with ASSERT_STATIC_STORAGE(). Automatic semaphores must
use sema_init(), which supplies a static class key. Preserve the underlying
initializer used by sema_init() and by embedded objects.

Assisted-by: OpenAI Codex
Signed-off-by: Yury Norov <redacted>
---
 include/linux/semaphore.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/include/linux/semaphore.h b/include/linux/semaphore.h
index a4c8651ef021..c0657a6aeb12 100644
--- a/include/linux/semaphore.h
+++ b/include/linux/semaphore.h
@@ -8,6 +8,7 @@
 #ifndef __LINUX_SEMAPHORE_H
 #define __LINUX_SEMAPHORE_H
 
+#include <linux/compiler.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
 
@@ -44,7 +45,8 @@ struct semaphore {
  * context.
  */
 #define DEFINE_SEMAPHORE(_name, _n)	\
-	struct semaphore _name = __SEMAPHORE_INITIALIZER(_name, _n)
+	struct semaphore _name = __SEMAPHORE_INITIALIZER(_name, _n);	\
+	ASSERT_STATIC_STORAGE(_name)
 
 static inline void sema_init(struct semaphore *sem, int val)
 {
-- 
2.53.0

Thread (29 messages) 29 messages, 7 authors, last reply 3d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help