]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
compiler_types: Move lock checking attributes to compiler-context-analysis.h
authorMarco Elver <elver@google.com>
Fri, 19 Dec 2025 15:39:50 +0000 (16:39 +0100)
committerPeter Zijlstra <peterz@infradead.org>
Mon, 5 Jan 2026 15:43:26 +0000 (16:43 +0100)
The conditional definition of lock checking macros and attributes is
about to become more complex. Factor them out into their own header for
better readability, and to make it obvious which features are supported
by which mode (currently only Sparse). This is the first step towards
generalizing towards "context analysis".

No functional change intended.

Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Link: https://patch.msgid.link/20251219154418.3592607-2-elver@google.com
include/linux/compiler-context-analysis.h [new file with mode: 0644]
include/linux/compiler_types.h

diff --git a/include/linux/compiler-context-analysis.h b/include/linux/compiler-context-analysis.h
new file mode 100644 (file)
index 0000000..f8af630
--- /dev/null
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Macros and attributes for compiler-based static context analysis.
+ */
+
+#ifndef _LINUX_COMPILER_CONTEXT_ANALYSIS_H
+#define _LINUX_COMPILER_CONTEXT_ANALYSIS_H
+
+#ifdef __CHECKER__
+
+/* Sparse context/lock checking support. */
+# define __must_hold(x)                __attribute__((context(x,1,1)))
+# define __acquires(x)         __attribute__((context(x,0,1)))
+# define __cond_acquires(x)    __attribute__((context(x,0,-1)))
+# define __releases(x)         __attribute__((context(x,1,0)))
+# define __acquire(x)          __context__(x,1)
+# define __release(x)          __context__(x,-1)
+# define __cond_lock(x, c)     ((c) ? ({ __acquire(x); 1; }) : 0)
+
+#else /* !__CHECKER__ */
+
+# define __must_hold(x)
+# define __acquires(x)
+# define __cond_acquires(x)
+# define __releases(x)
+# define __acquire(x)          (void)0
+# define __release(x)          (void)0
+# define __cond_lock(x, c)     (c)
+
+#endif /* __CHECKER__ */
+
+#endif /* _LINUX_COMPILER_CONTEXT_ANALYSIS_H */
index 1280693766b9dd844ec7ca83053381799be12456..ddada1ed88ea67ec7d9bb3dcdbf346f0acaa968f 100644 (file)
@@ -41,6 +41,8 @@
 # define BTF_TYPE_TAG(value) /* nothing */
 #endif
 
+#include <linux/compiler-context-analysis.h>
+
 /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
 #ifdef __CHECKER__
 /* address spaces */
 # define __rcu         __attribute__((noderef, address_space(__rcu)))
 static inline void __chk_user_ptr(const volatile void __user *ptr) { }
 static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
-/* context/locking */
-# define __must_hold(x)        __attribute__((context(x,1,1)))
-# define __acquires(x) __attribute__((context(x,0,1)))
-# define __cond_acquires(x) __attribute__((context(x,0,-1)))
-# define __releases(x) __attribute__((context(x,1,0)))
-# define __acquire(x)  __context__(x,1)
-# define __release(x)  __context__(x,-1)
-# define __cond_lock(x,c)      ((c) ? ({ __acquire(x); 1; }) : 0)
 /* other */
 # define __force       __attribute__((force))
 # define __nocast      __attribute__((nocast))
@@ -79,14 +73,6 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
 
 # define __chk_user_ptr(x)     (void)0
 # define __chk_io_ptr(x)       (void)0
-/* context/locking */
-# define __must_hold(x)
-# define __acquires(x)
-# define __cond_acquires(x)
-# define __releases(x)
-# define __acquire(x)  (void)0
-# define __release(x)  (void)0
-# define __cond_lock(x,c) (c)
 /* other */
 # define __force
 # define __nocast