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 <[email protected]>
---
 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


Reply via email to