compiler-context-analysis: Add support for multi-argument guarded_by
Subsystems (4)
Marco Elver
maintainerPATCH 1/1 compiler-context-analysis: Add support for multi-argument guarded_by
Clang 23 introduces support for multiple arguments in the guarded_by and pt_guarded_by attributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.
To use the feature while maintaining compatibility with Clang 22, add the __guarded_by_any() and __pt_guarded_by_any() macros. On Clang 23 and newer, these expand to the underlying attributes; with older Clang versions, they fall back to a no-op (false negatives possible).
--- include/linux/compiler-context-analysis.h | 66 ++++++++++++++++++++--- init/Kconfig | 5 ++ lib/test_context-analysis.c | 24 +++++++++ 3 files changed, 87 insertions(+), 8 deletions(-)
include/linux/compiler-context-analysis.h +58 -8
@@ -39,8 +39,9 @@# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__)))/** -* __guarded_by - struct member and globals attribute, declares variable -* only accessible within active context +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @x: context lock instance pointer * * Declares that the struct member or global variable is only accessible within * the context entered by the given context lock. Read operations on the data
@@ -53,11 +54,12 @@* long counter __guarded_by(&lock); * }; */ -# define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__))) +# define __guarded_by (x) __attribute__((guarded_by(x)))/** -* __pt_guarded_by - struct member and globals attribute, declares pointed-to -* data only accessible within active context +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @x: context lock instance pointer * * Declares that the data pointed to by the struct member pointer or global * pointer is only accessible within the context entered by the given context
@@ -71,7 +73,53 @@* long *counter __pt_guarded_by(&lock); * }; */ -# define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__))) +# define __pt_guarded_by (x) __attribute__((pt_guarded_by(x))) + +/** +* __guarded_by_any() - struct member and globals attribute, declares variable +* only accessible within active contexts +* @...: context lock instance pointers +* +* Declares that the struct member or global variable is protected by multiple +* context locks. Write access requires all listed context locks to be held +* exclusively; read access requires at least one of them to be held (shared or +* exclusive). +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by_any(&lock1, &lock2); +* }; +*/ +# ifdef CONFIG_CC_HAS_MULTI_ARG_GUARDED_BY_ATTR +# define __guarded_by_any (...) __attribute__((guarded_by(__VA_ARGS__))) +# else +# define __guarded_by_any (...) +# endif + +/** +* __pt_guarded_by_any() - struct member and globals attribute, declares pointed-to +* data only accessible within active contexts +* @...: context lock instance pointers +* +* Declares that the data pointed to by the struct member pointer or global +* pointer is protected by multiple context locks. Write access requires all +* listed context locks to be held exclusively; read access requires at least +* one of them to be held (shared or exclusive). +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by_any(&lock1, &lock2); +* }; +*/ +# ifdef CONFIG_CC_HAS_MULTI_ARG_GUARDED_BY_ATTR +# define __pt_guarded_by_any (...) __attribute__((pt_guarded_by(__VA_ARGS__))) +# else +# define __pt_guarded_by_any (...) +# endif /** * context_lock_struct() - declare or define a context lock struct
@@ -158,8 +206,10 @@# define __assumes_ctx_lock (...)# define __assumes_shared_ctx_lock (...)# define __returns_ctx_lock (var) -# define __guarded_by (...) -# define __pt_guarded_by (...) +# define __guarded_by (x) +# define __pt_guarded_by (x) +# define __guarded_by_any (...) +# define __pt_guarded_by_any (...)# define __excludes_ctx_lock (...)# define __requires_ctx_lock (...)# define __requires_shared_ctx_lock (...)
init/Kconfig +5 -0
@@ -158,6 +158,11 @@ config CC_HAS_BROKEN_COUNTED_BY_REF config CC_HAS_MULTIDIMENSIONAL_NONSTRING def_bool $(success,echo 'char tag[][4] __attribute__((__nonstring__)) = { };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror) +config CC_HAS_MULTI_ARG_GUARDED_BY_ATTR + # supported since clang 23 + depends on CC_IS_CLANG + def_bool $(success,echo 'typedef int __attribute__((capability("l"))) L; L l1; L l2; int __attribute__((guarded_by(&l1, &l2))) x;' | $(CC) -x c - -c -o /dev/null -Werror) + config LD_CAN_USE_KEEP_IN_OVERLAY # ld.lld prior to 21.0.0 did not support KEEP within an overlay description # https://github.com/llvm/llvm-project/pull/130661
lib/test_context-analysis.c +24 -0
@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,struct test_mutex_data {struct mutex mtx; int counter __guarded_by(&mtx); + +struct mutex mtx2; + int anyread __guarded_by_any(&mtx, &mtx2); + int *anyptr __pt_guarded_by_any(&mtx, &mtx2); };static void __used test_mutex_init (struct test_mutex_data *d)
@@ -219,6 +223,26 @@ static void __used test_mutex_cond_guard(struct test_mutex_data *d) } } +static void __used test_mutex_multiguard (struct test_mutex_data *d) +{ +mutex_lock (&d->mtx); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx); + +mutex_lock (&d->mtx2); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx2); + +mutex_lock (&d->mtx); +mutex_lock (&d->mtx2); + d->anyread++; + (*d->anyptr)++; +mutex_unlock (&d->mtx2); +mutex_unlock (&d->mtx); +} +struct test_seqlock_data {seqlock_t sl; int counter __guarded_by(&sl);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Marco Elver
maintainerQuoted 7 lines · Marco Elver
Clang 23 introduces support for multiple arguments in the
guarded_byandpt_guarded_byattributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.
Quoted 8 lines
To use the feature while maintaining compatibility with Clang 22, add the
__guarded_by_any()and__pt_guarded_by_any()macros. On Clang 23 and newer, these expand to the underlying attributes; with older Clang versions, they fall back to a no-op (false negatives possible).Requested-by Peter Zijlstra <[email protected]>Signed-off-by Marco Elver <[email protected]>
If we want to retain compatibility with Clang 22, we need __guarded_by_any, which ultimately maps to 'guarded_by' as well if we have Clang 23+.
The alternative would be to wait a few weeks and then require Context Analysis to have a Clang 23 compiler. That'd avoid adding the new __guarded_by_any variant and we can just use __guarded_by as-is.
We likely want to do that when Clang 23.1 is released anyway, because there were some other fixes (arrays of lock fix:
https://github.com/llvm/llvm-project/pull/148551).
Preferences?
Quoted 167 lines
--- include/linux/compiler-context-analysis.h | 66 ++++++++++++++++++++--- init/Kconfig | 5 ++ lib/test_context-analysis.c | 24 +++++++++ 3 files changed, 87 insertions(+), 8 deletions(-)include/linux/compiler-context-analysis.h +58
-8@@ -39,8 +39,9 @@# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__)))/** -* __guarded_by - struct member and globals attribute, declares variable -* only accessible within active context +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @x: context lock instance pointer * * Declares that the struct member or global variable is only accessible within * the context entered by the given context lock. Read operations on the data @@ -53,11 +54,12 @@* long counter __guarded_by(&lock); * }; */ -# define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__))) +# define __guarded_by (x) __attribute__((guarded_by(x)))/** -* __pt_guarded_by - struct member and globals attribute, declares pointed-to -* data only accessible within active context +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @x: context lock instance pointer * * Declares that the data pointed to by the struct member pointer or global * pointer is only accessible within the context entered by the given context @@ -71,7 +73,53 @@* long *counter __pt_guarded_by(&lock); * }; */ -# define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__))) +# define __pt_guarded_by (x) __attribute__((pt_guarded_by(x))) + +/** +* __guarded_by_any() - struct member and globals attribute, declares variable +* only accessible within active contexts +* @...: context lock instance pointers +* +* Declares that the struct member or global variable is protected by multiple +* context locks. Write access requires all listed context locks to be held +* exclusively; read access requires at least one of them to be held (shared or +* exclusive). +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by_any(&lock1, &lock2); +* }; +*/ +# ifdef CONFIG_CC_HAS_MULTI_ARG_GUARDED_BY_ATTR +# define __guarded_by_any (...) __attribute__((guarded_by(__VA_ARGS__))) +# else +# define __guarded_by_any (...) +# endif + +/** +* __pt_guarded_by_any() - struct member and globals attribute, declares pointed-to +* data only accessible within active contexts +* @...: context lock instance pointers +* +* Declares that the data pointed to by the struct member pointer or global +* pointer is protected by multiple context locks. Write access requires all +* listed context locks to be held exclusively; read access requires at least +* one of them to be held (shared or exclusive). +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by_any(&lock1, &lock2); +* }; +*/ +# ifdef CONFIG_CC_HAS_MULTI_ARG_GUARDED_BY_ATTR +# define __pt_guarded_by_any (...) __attribute__((pt_guarded_by(__VA_ARGS__))) +# else +# define __pt_guarded_by_any (...) +# endif /** * context_lock_struct() - declare or define a context lock struct @@ -158,8 +206,10 @@# define __assumes_ctx_lock (...)# define __assumes_shared_ctx_lock (...)# define __returns_ctx_lock (var) -# define __guarded_by (...) -# define __pt_guarded_by (...) +# define __guarded_by (x) +# define __pt_guarded_by (x) +# define __guarded_by_any (...) +# define __pt_guarded_by_any (...)# define __excludes_ctx_lock (...)# define __requires_ctx_lock (...)# define __requires_shared_ctx_lock (...)init/Kconfig +5
-0@@ -158,6 +158,11 @@ config CC_HAS_BROKEN_COUNTED_BY_REF config CC_HAS_MULTIDIMENSIONAL_NONSTRING def_bool $(success,echo 'char tag[][4] __attribute__((__nonstring__)) = { };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror) +config CC_HAS_MULTI_ARG_GUARDED_BY_ATTR + # supported since clang 23 + depends on CC_IS_CLANG + def_bool $(success,echo 'typedef int __attribute__((capability("l"))) L; L l1; L l2; int __attribute__((guarded_by(&l1, &l2))) x;' | $(CC) -x c - -c -o /dev/null -Werror) + config LD_CAN_USE_KEEP_IN_OVERLAY # ld.lld prior to 21.0.0 did not support KEEP within an overlay description # https://github.com/llvm/llvm-project/pull/130661lib/test_context-analysis.c +24
-1@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,struct test_mutex_data {struct mutex mtx; int counter __guarded_by(&mtx); + +struct mutex mtx2; + int anyread __guarded_by_any(&mtx, &mtx2); + int *anyptr __pt_guarded_by_any(&mtx, &mtx2); };static void __used test_mutex_init (struct test_mutex_data *d)@@ -219,6 +223,26 @@ static void __used test_mutex_cond_guard(struct test_mutex_data *d) } } +static void __used test_mutex_multiguard (struct test_mutex_data *d) +{ +mutex_lock (&d->mtx); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx); + +mutex_lock (&d->mtx2); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx2); + +mutex_lock (&d->mtx); +mutex_lock (&d->mtx2); + d->anyread++; + (*d->anyptr)++; +mutex_unlock (&d->mtx2); +mutex_unlock (&d->mtx); +} +struct test_seqlock_data {seqlock_t sl; int counter __guarded_by(&sl); --2.53.0.1018.g2bb0e51243-goog
Quoted 25 lines · Marco Elver
Quoted 7 lines · Marco Elver
Clang 23 introduces support for multiple arguments in the
guarded_byandpt_guarded_byattributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.Quoted 8 lines
To use the feature while maintaining compatibility with Clang 22, add the
__guarded_by_any()and__pt_guarded_by_any()macros. On Clang 23 and newer, these expand to the underlying attributes; with older Clang versions, they fall back to a no-op (false negatives possible).Requested-by Peter Zijlstra <[email protected]>Signed-off-by Marco Elver <[email protected]>If we want to retain compatibility with Clang 22, we need
__guarded_by_any, which ultimately maps to 'guarded_by' as well if we have Clang 23+.The alternative would be to wait a few weeks and then require Context Analysis to have a Clang 23 compiler. That'd avoid adding the new
__guarded_by_anyvariant and we can just use__guarded_byas-is.
Quoted 5 lines
We likely want to do that when Clang 23.1 is released anyway, because there were some other fixes (arrays of lock fix:
https://github.com/llvm/llvm-project/pull/148551).Preferences?
I think we'll be okay just upping the requirement now. But I'll sit on this a little until Debian's clang-23 build is fresh enough to includes the awesome :-)
Marco Elver
maintainerPATCH 1/1 compiler-context-analysis: Bump required Clang version to 23
Clang 23 introduces support for multiple arguments in the guarded_by and pt_guarded_by attributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.
Clang 23 also provides a few other improvements (such as being able to deal with arrays of locks [2]) that make it worthwhile bumping the compiler version instead of trying to make both Clang 22 and later work while supporting these new features.
---
v2:
* Bump version instead of __guarded_by_any workaround.
--- Documentation/dev-tools/context-analysis.rst | 2 +- include/linux/compiler-context-analysis.h | 30 ++++++++++++++------ lib/Kconfig.debug | 4 +-- lib/test_context-analysis.c | 24 ++++++++++++++++ 4 files changed, 49 insertions(+), 11 deletions(-)
Documentation/dev-tools/context-analysis.rst +1 -1
@@ -17,7 +17,7 @@ features. To enable for Clang, configure the kernel with:: CONFIG_WARN_CONTEXT_ANALYSIS=y -The feature requires Clang 22 or later. +The feature requires Clang 23 or later. The analysis is *opt-in by default*, and requires declaring which modules and subsystems should be analyzed in the respective `Makefile`::
include/linux/compiler-context-analysis.h +22 -8
@@ -39,12 +39,14 @@# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__)))/** -* __guarded_by - struct member and globals attribute, declares variable -* only accessible within active context +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the struct member or global variable is only accessible within -* the context entered by the given context lock. Read operations on the data -* require shared access, while write operations require exclusive access. +* the context entered by the given context lock(s). Read operations on the data +* require shared access to at least one of the context locks, while write +* operations require exclusive access to all listed context locks. * * .. code-block:: c *
@@ -52,17 +54,24 @@* spinlock_t lock; * long counter __guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by(&lock1, &lock2); +* }; */ # define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__)))/** -* __pt_guarded_by - struct member and globals attribute, declares pointed-to -* data only accessible within active context +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the data pointed to by the struct member pointer or global * pointer is only accessible within the context entered by the given context -* lock. Read operations on the data require shared access, while write -* operations require exclusive access. +* lock(s). Read operations on the data require shared access to at least one +* of the context locks, while write operations require exclusive access to all +* listed context locks. * * .. code-block:: c *
@@ -70,6 +79,11 @@* spinlock_t lock; * long *counter __pt_guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by(&lock1, &lock2); +* }; */ # define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__)))
lib/Kconfig.debug +2 -2
@@ -630,7 +630,7 @@ config DEBUG_FORCE_WEAK_PER_CPU config WARN_CONTEXT_ANALYSIS bool "Compiler context-analysis warnings" - depends on CC_IS_CLANG && CLANG_VERSION >= 220100 + depends on CC_IS_CLANG && CLANG_VERSION >= 230000 # Branch profiling re-defines "if", which messes with the compiler's # ability to analyze __cond_acquires(..), resulting in false positives. depends on !TRACE_BRANCH_PROFILING
@@ -641,7 +641,7 @@ config WARN_CONTEXT_ANALYSIS and releasing user-definable "context locks". Clang's name of the feature is "Thread Safety Analysis". Requires - Clang 22.1.0 or later. + Clang 23 or later. Produces warnings by default. Select CONFIG_WERROR if you wish to turn these warnings into errors.
lib/test_context-analysis.c +24 -0
@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,struct test_mutex_data {struct mutex mtx; int counter __guarded_by(&mtx); + +struct mutex mtx2; + int anyread __guarded_by(&mtx, &mtx2); + int *anyptr __pt_guarded_by(&mtx, &mtx2); };static void __used test_mutex_init (struct test_mutex_data *d)
@@ -219,6 +223,26 @@ static void __used test_mutex_cond_guard(struct test_mutex_data *d) } } +static void __used test_mutex_multiguard (struct test_mutex_data *d) +{ +mutex_lock (&d->mtx); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx); + +mutex_lock (&d->mtx2); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx2); + +mutex_lock (&d->mtx); +mutex_lock (&d->mtx2); + d->anyread++; + (*d->anyptr)++; +mutex_unlock (&d->mtx2); +mutex_unlock (&d->mtx); +} +struct test_seqlock_data {seqlock_t sl; int counter __guarded_by(&sl);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH tip/locking/core] compiler-context-analysis: Bump required Clang version to 23
Quoted 11 lines · Marco Elver
Clang 23 introduces support for multiple arguments in the
guarded_byandpt_guarded_byattributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.Clang 23 also provides a few other improvements (such as being able to deal with arrays of locks [2]) that make it worthwhile bumping the compiler version instead of trying to make both Clang 22 and later work while supporting these new features.
Quoted 4 lines
Requested-by Peter Zijlstra <[email protected]>Signed-off-by Marco Elver <[email protected]>
Fair enough. I will upload a new main snapshot to kernel.org to make it more accessible to folks for testing.
Quoted 153 lines
---
v2:
* Bump version instead of__guarded_by_anyworkaround.--- Documentation/dev-tools/context-analysis.rst | 2 +- include/linux/compiler-context-analysis.h | 30 ++++++++++++++------ lib/Kconfig.debug | 4 +-- lib/test_context-analysis.c | 24 ++++++++++++++++ 4 files changed, 49 insertions(+), 11 deletions(-)Documentation/dev-tools/context-analysis.rst +1
-1@@ -17,7 +17,7 @@ features. To enable for Clang, configure the kernel with:: CONFIG_WARN_CONTEXT_ANALYSIS=y -The feature requires Clang 22 or later. +The feature requires Clang 23 or later. The analysis is *opt-in by default*, and requires declaring which modules and subsystems should be analyzed in the respective `Makefile`::include/linux/compiler-context-analysis.h +22
-8@@ -39,12 +39,14 @@# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__)))/** -* __guarded_by - struct member and globals attribute, declares variable -* only accessible within active context +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the struct member or global variable is only accessible within -* the context entered by the given context lock. Read operations on the data -* require shared access, while write operations require exclusive access. +* the context entered by the given context lock(s). Read operations on the data +* require shared access to at least one of the context locks, while write +* operations require exclusive access to all listed context locks. * * .. code-block:: c * @@ -52,17 +54,24 @@* spinlock_t lock; * long counter __guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by(&lock1, &lock2); +* }; */ # define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__)))/** -* __pt_guarded_by - struct member and globals attribute, declares pointed-to -* data only accessible within active context +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the data pointed to by the struct member pointer or global * pointer is only accessible within the context entered by the given context -* lock. Read operations on the data require shared access, while write -* operations require exclusive access. +* lock(s). Read operations on the data require shared access to at least one +* of the context locks, while write operations require exclusive access to all +* listed context locks. * * .. code-block:: c * @@ -70,6 +79,11 @@* spinlock_t lock; * long *counter __pt_guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by(&lock1, &lock2); +* }; */ # define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__)))lib/Kconfig.debug +2
-2@@ -630,7 +630,7 @@ config DEBUG_FORCE_WEAK_PER_CPU config WARN_CONTEXT_ANALYSIS bool "Compiler context-analysis warnings" - depends on CC_IS_CLANG && CLANG_VERSION >= 220100 + depends on CC_IS_CLANG && CLANG_VERSION >= 230000 # Branch profiling re-defines "if", which messes with the compiler's # ability to analyze __cond_acquires(..), resulting in false positives. depends on !TRACE_BRANCH_PROFILING@@ -641,7 +641,7 @@ config WARN_CONTEXT_ANALYSIS and releasing user-definable "context locks". Clang's name of the feature is "Thread Safety Analysis". Requires - Clang 22.1.0 or later. + Clang 23 or later. Produces warnings by default. Select CONFIG_WERROR if you wish to turn these warnings into errors.lib/test_context-analysis.c +24
-0@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,struct test_mutex_data {struct mutex mtx; int counter __guarded_by(&mtx); + +struct mutex mtx2; + int anyread __guarded_by(&mtx, &mtx2); + int *anyptr __pt_guarded_by(&mtx, &mtx2); };static void __used test_mutex_init (struct test_mutex_data *d)@@ -219,6 +223,26 @@ static void __used test_mutex_cond_guard(struct test_mutex_data *d) } } +static void __used test_mutex_multiguard (struct test_mutex_data *d) +{ +mutex_lock (&d->mtx); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx); + +mutex_lock (&d->mtx2); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx2); + +mutex_lock (&d->mtx); +mutex_lock (&d->mtx2); + d->anyread++; + (*d->anyptr)++; +mutex_unlock (&d->mtx2); +mutex_unlock (&d->mtx); +} +struct test_seqlock_data {seqlock_t sl; int counter __guarded_by(&sl);Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Marco Elver
maintainerRe: [PATCH tip/locking/core] compiler-context-analysis: Bump required Clang version to 23
Quoted 18 lines · Nathan Chancellor
Quoted 11 lines · Marco Elver
Clang 23 introduces support for multiple arguments in the
guarded_byandpt_guarded_byattributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.Clang 23 also provides a few other improvements (such as being able to deal with arrays of locks [2]) that make it worthwhile bumping the compiler version instead of trying to make both Clang 22 and later work while supporting these new features.
Quoted 4 lines
Requested-by Peter Zijlstra <[email protected]>Signed-off-by Marco Elver <[email protected]>
Quoted 4 lines
Fair enough. I will upload a new main snapshot to kernel.org to make it more accessible to folks for testing.
Reviewed-by Nathan Chancellor <[email protected]>
Thanks. I always suspected we'll need to bump the Clang version here, which is a bit inconvenient for now. But I think it's better to do now than find odd workarounds in the kernel tree later that we need to revert.
Quoted 153 lines
Quoted 153 lines
---
v2:
* Bump version instead of__guarded_by_anyworkaround.--- Documentation/dev-tools/context-analysis.rst | 2 +- include/linux/compiler-context-analysis.h | 30 ++++++++++++++------ lib/Kconfig.debug | 4 +-- lib/test_context-analysis.c | 24 ++++++++++++++++ 4 files changed, 49 insertions(+), 11 deletions(-)Documentation/dev-tools/context-analysis.rst +1
-1@@ -17,7 +17,7 @@ features. To enable for Clang, configure the kernel with:: CONFIG_WARN_CONTEXT_ANALYSIS=y -The feature requires Clang 22 or later. +The feature requires Clang 23 or later. The analysis is *opt-in by default*, and requires declaring which modules and subsystems should be analyzed in the respective `Makefile`::include/linux/compiler-context-analysis.h +22
-8@@ -39,12 +39,14 @@# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__)))/** -* __guarded_by - struct member and globals attribute, declares variable -* only accessible within active context +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the struct member or global variable is only accessible within -* the context entered by the given context lock. Read operations on the data -* require shared access, while write operations require exclusive access. +* the context entered by the given context lock(s). Read operations on the data +* require shared access to at least one of the context locks, while write +* operations require exclusive access to all listed context locks. * * .. code-block:: c * @@ -52,17 +54,24 @@* spinlock_t lock; * long counter __guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by(&lock1, &lock2); +* }; */ # define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__)))/** -* __pt_guarded_by - struct member and globals attribute, declares pointed-to -* data only accessible within active context +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the data pointed to by the struct member pointer or global * pointer is only accessible within the context entered by the given context -* lock. Read operations on the data require shared access, while write -* operations require exclusive access. +* lock(s). Read operations on the data require shared access to at least one +* of the context locks, while write operations require exclusive access to all +* listed context locks. * * .. code-block:: c * @@ -70,6 +79,11 @@* spinlock_t lock; * long *counter __pt_guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by(&lock1, &lock2); +* }; */ # define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__)))lib/Kconfig.debug +2
-2@@ -630,7 +630,7 @@ config DEBUG_FORCE_WEAK_PER_CPU config WARN_CONTEXT_ANALYSIS bool "Compiler context-analysis warnings" - depends on CC_IS_CLANG && CLANG_VERSION >= 220100 + depends on CC_IS_CLANG && CLANG_VERSION >= 230000 # Branch profiling re-defines "if", which messes with the compiler's # ability to analyze __cond_acquires(..), resulting in false positives. depends on !TRACE_BRANCH_PROFILING@@ -641,7 +641,7 @@ config WARN_CONTEXT_ANALYSIS and releasing user-definable "context locks". Clang's name of the feature is "Thread Safety Analysis". Requires - Clang 22.1.0 or later. + Clang 23 or later. Produces warnings by default. Select CONFIG_WERROR if you wish to turn these warnings into errors.lib/test_context-analysis.c +24
-1@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,struct test_mutex_data {struct mutex mtx; int counter __guarded_by(&mtx); + +struct mutex mtx2; + int anyread __guarded_by(&mtx, &mtx2); + int *anyptr __pt_guarded_by(&mtx, &mtx2); };static void __used test_mutex_init (struct test_mutex_data *d)@@ -219,6 +223,26 @@ static void __used test_mutex_cond_guard(struct test_mutex_data *d) } } +static void __used test_mutex_multiguard (struct test_mutex_data *d) +{ +mutex_lock (&d->mtx); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx); + +mutex_lock (&d->mtx2); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx2); + +mutex_lock (&d->mtx); +mutex_lock (&d->mtx2); + d->anyread++; + (*d->anyptr)++; +mutex_unlock (&d->mtx2); +mutex_unlock (&d->mtx); +} +struct test_seqlock_data {seqlock_t sl; int counter __guarded_by(&sl); --2.53.0.1018.g2bb0e51243-goog
Marco Elver
maintainerRe: [PATCH tip/locking/core] compiler-context-analysis: Bump required Clang version to 23
In reply to Marco ElverQuoted 12 lines · Marco Elver
Clang 23 introduces support for multiple arguments in the
guarded_byandpt_guarded_byattributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.Clang 23 also provides a few other improvements (such as being able to deal with arrays of locks [2]) that make it worthwhile bumping the compiler version instead of trying to make both Clang 22 and later work while supporting these new features.
Quoted 4 lines
Requested-by Peter Zijlstra <[email protected]>Signed-off-by Marco Elver <[email protected]>
Maybe it's time - function pointer support, multi-arg guarded_by, and some more bug fixes are already in unreleased clang 23.
FWIW - Bart just sent:
https://lore.kernel.org/all/[email protected]/
... using attributes on function pointers, so we'll need to bump if that gets merged soon.
I expect if we target Linux 7.2, its release may coincide with clang 23's release in August (per llvm.org schedule).
Preferences?
Thanks,
-- Marco
Quoted 153 lines
---
v2:
* Bump version instead of__guarded_by_anyworkaround.--- Documentation/dev-tools/context-analysis.rst | 2 +- include/linux/compiler-context-analysis.h | 30 ++++++++++++++------ lib/Kconfig.debug | 4 +-- lib/test_context-analysis.c | 24 ++++++++++++++++ 4 files changed, 49 insertions(+), 11 deletions(-)Documentation/dev-tools/context-analysis.rst +1
-1@@ -17,7 +17,7 @@ features. To enable for Clang, configure the kernel with:: CONFIG_WARN_CONTEXT_ANALYSIS=y -The feature requires Clang 22 or later. +The feature requires Clang 23 or later. The analysis is *opt-in by default*, and requires declaring which modules and subsystems should be analyzed in the respective `Makefile`::include/linux/compiler-context-analysis.h +22
-8@@ -39,12 +39,14 @@# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__)))/** -* __guarded_by - struct member and globals attribute, declares variable -* only accessible within active context +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the struct member or global variable is only accessible within -* the context entered by the given context lock. Read operations on the data -* require shared access, while write operations require exclusive access. +* the context entered by the given context lock(s). Read operations on the data +* require shared access to at least one of the context locks, while write +* operations require exclusive access to all listed context locks. * * .. code-block:: c * @@ -52,17 +54,24 @@* spinlock_t lock; * long counter __guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by(&lock1, &lock2); +* }; */ # define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__)))/** -* __pt_guarded_by - struct member and globals attribute, declares pointed-to -* data only accessible within active context +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @...: context lock instance pointer(s) * * Declares that the data pointed to by the struct member pointer or global * pointer is only accessible within the context entered by the given context -* lock. Read operations on the data require shared access, while write -* operations require exclusive access. +* lock(s). Read operations on the data require shared access to at least one +* of the context locks, while write operations require exclusive access to all +* listed context locks. * * .. code-block:: c * @@ -70,6 +79,11 @@* spinlock_t lock; * long *counter __pt_guarded_by(&lock); * }; +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by(&lock1, &lock2); +* }; */ # define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__)))lib/Kconfig.debug +2
-2@@ -630,7 +630,7 @@ config DEBUG_FORCE_WEAK_PER_CPU config WARN_CONTEXT_ANALYSIS bool "Compiler context-analysis warnings" - depends on CC_IS_CLANG && CLANG_VERSION >= 220100 + depends on CC_IS_CLANG && CLANG_VERSION >= 230000 # Branch profiling re-defines "if", which messes with the compiler's # ability to analyze __cond_acquires(..), resulting in false positives. depends on !TRACE_BRANCH_PROFILING@@ -641,7 +641,7 @@ config WARN_CONTEXT_ANALYSIS and releasing user-definable "context locks". Clang's name of the feature is "Thread Safety Analysis". Requires - Clang 22.1.0 or later. + Clang 23 or later. Produces warnings by default. Select CONFIG_WERROR if you wish to turn these warnings into errors.lib/test_context-analysis.c +24
-1@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,struct test_mutex_data {struct mutex mtx; int counter __guarded_by(&mtx); + +struct mutex mtx2; + int anyread __guarded_by(&mtx, &mtx2); + int *anyptr __pt_guarded_by(&mtx, &mtx2); };static void __used test_mutex_init (struct test_mutex_data *d)@@ -219,6 +223,26 @@ static void __used test_mutex_cond_guard(struct test_mutex_data *d) } } +static void __used test_mutex_multiguard (struct test_mutex_data *d) +{ +mutex_lock (&d->mtx); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx); + +mutex_lock (&d->mtx2); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx2); + +mutex_lock (&d->mtx); +mutex_lock (&d->mtx2); + d->anyread++; + (*d->anyptr)++; +mutex_unlock (&d->mtx2); +mutex_unlock (&d->mtx); +} +struct test_seqlock_data {seqlock_t sl; int counter __guarded_by(&sl); --2.53.0.1018.g2bb0e51243-goog
Quoted 6 lines · Marco Elver
Clang 23 introduces support for multiple arguments in the
guarded_byandpt_guarded_byattributes [1]. This allows defining variables protected by multiple context locks, where read access requires holding at least one lock (shared or exclusive), and write access requires holding all of them exclusively.
Quoted 8 lines
To use the feature while maintaining compatibility with Clang 22, add the
__guarded_by_any()and__pt_guarded_by_any()macros. On Clang 23 and newer, these expand to the underlying attributes; with older Clang versions, they fall back to a no-op (false negatives possible).Requested-by Peter Zijlstra <[email protected]>Signed-off-by Marco Elver <[email protected]>
One small comment below.
Quoted 111 lines
--- include/linux/compiler-context-analysis.h | 66 ++++++++++++++++++++--- init/Kconfig | 5 ++ lib/test_context-analysis.c | 24 +++++++++ 3 files changed, 87 insertions(+), 8 deletions(-)include/linux/compiler-context-analysis.h +58
-8@@ -39,8 +39,9 @@# define __assumes_shared_ctx_lock (...) __attribute__((assert_shared_capability(__VA_ARGS__)))/** -* __guarded_by - struct member and globals attribute, declares variable -* only accessible within active context +* __guarded_by() - struct member and globals attribute, declares variable +* only accessible within active context +* @x: context lock instance pointer * * Declares that the struct member or global variable is only accessible within * the context entered by the given context lock. Read operations on the data @@ -53,11 +54,12 @@* long counter __guarded_by(&lock); * }; */ -# define __guarded_by (...) __attribute__((guarded_by(__VA_ARGS__))) +# define __guarded_by (x) __attribute__((guarded_by(x)))/** -* __pt_guarded_by - struct member and globals attribute, declares pointed-to -* data only accessible within active context +* __pt_guarded_by() - struct member and globals attribute, declares pointed-to +* data only accessible within active context +* @x: context lock instance pointer * * Declares that the data pointed to by the struct member pointer or global * pointer is only accessible within the context entered by the given context @@ -71,7 +73,53 @@* long *counter __pt_guarded_by(&lock); * }; */ -# define __pt_guarded_by (...) __attribute__((pt_guarded_by(__VA_ARGS__))) +# define __pt_guarded_by (x) __attribute__((pt_guarded_by(x))) + +/** +* __guarded_by_any() - struct member and globals attribute, declares variable +* only accessible within active contexts +* @...: context lock instance pointers +* +* Declares that the struct member or global variable is protected by multiple +* context locks. Write access requires all listed context locks to be held +* exclusively; read access requires at least one of them to be held (shared or +* exclusive). +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock1, lock2; +* long counter __guarded_by_any(&lock1, &lock2); +* }; +*/ +# ifdef CONFIG_CC_HAS_MULTI_ARG_GUARDED_BY_ATTR +# define __guarded_by_any (...) __attribute__((guarded_by(__VA_ARGS__))) +# else +# define __guarded_by_any (...) +# endif + +/** +* __pt_guarded_by_any() - struct member and globals attribute, declares pointed-to +* data only accessible within active contexts +* @...: context lock instance pointers +* +* Declares that the data pointed to by the struct member pointer or global +* pointer is protected by multiple context locks. Write access requires all +* listed context locks to be held exclusively; read access requires at least +* one of them to be held (shared or exclusive). +* +* .. code-block:: c +* +* struct some_state { +* spinlock_t lock1, lock2; +* long *counter __pt_guarded_by_any(&lock1, &lock2); +* }; +*/ +# ifdef CONFIG_CC_HAS_MULTI_ARG_GUARDED_BY_ATTR +# define __pt_guarded_by_any (...) __attribute__((pt_guarded_by(__VA_ARGS__))) +# else +# define __pt_guarded_by_any (...) +# endif /** * context_lock_struct() - declare or define a context lock struct @@ -158,8 +206,10 @@# define __assumes_ctx_lock (...)# define __assumes_shared_ctx_lock (...)# define __returns_ctx_lock (var) -# define __guarded_by (...) -# define __pt_guarded_by (...) +# define __guarded_by (x) +# define __pt_guarded_by (x) +# define __guarded_by_any (...) +# define __pt_guarded_by_any (...)# define __excludes_ctx_lock (...)# define __requires_ctx_lock (...)# define __requires_shared_ctx_lock (...)init/Kconfig +0
-0
init/Kconfig config CC_HAS_BROKEN_COUNTED_BY_REF · line 164
Quoted 7 lines
init/Kconfig +4
-0@@ -158,6 +158,11 @@ config CC_HAS_BROKEN_COUNTED_BY_REF config CC_HAS_MULTIDIMENSIONAL_NONSTRING def_bool $(success,echo 'char tag[][4] __attribute__((__nonstring__)) = { };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror) +config CC_HAS_MULTI_ARG_GUARDED_BY_ATTR + # supported since clang 23 + depends on CC_IS_CLANG + def_bool $(success,echo 'typedef int __attribute__((capability("l"))) L; L l1; L l2; int __attribute__((guarded_by(&l1, &l2))) x;' | $(CC) -x c - -c -o /dev/null -Werror)
Include $(CLANG_FLAGS) after $(CC) like the test above to harden against the weird case when $(CC) does not have the host backend enabled (dumb, I know, but I have seen it).
Quoted 49 lines
init/Kconfig +1
-0+ config LD_CAN_USE_KEEP_IN_OVERLAY # ld.lld prior to 21.0.0 did not support KEEP within an overlay description # https://github.com/llvm/llvm-project/pull/130661lib/test_context-analysis.c +24
-0@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,struct test_mutex_data {struct mutex mtx; int counter __guarded_by(&mtx); + +struct mutex mtx2; + int anyread __guarded_by_any(&mtx, &mtx2); + int *anyptr __pt_guarded_by_any(&mtx, &mtx2); };static void __used test_mutex_init (struct test_mutex_data *d)@@ -219,6 +223,26 @@ static void __used test_mutex_cond_guard(struct test_mutex_data *d) } } +static void __used test_mutex_multiguard (struct test_mutex_data *d) +{ +mutex_lock (&d->mtx); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx); + +mutex_lock (&d->mtx2); + (void )d->anyread; + (void )*d->anyptr; +mutex_unlock (&d->mtx2); + +mutex_lock (&d->mtx); +mutex_lock (&d->mtx2); + d->anyread++; + (*d->anyptr)++; +mutex_unlock (&d->mtx2); +mutex_unlock (&d->mtx); +} +struct test_seqlock_data {seqlock_t sl; int counter __guarded_by(&sl);Signature · 1 line
2.53.0.1018.g2bb0e51243-goog