Skip to content

Commit ad66912

Browse files
committed
Merge remote-tracking branch 'tip/locking/context' into for-7.2
2 parents f80cea8 + f45c5c4 commit ad66912

4 files changed

Lines changed: 49 additions & 11 deletions

File tree

Documentation/dev-tools/context-analysis.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ features. To enable for Clang, configure the kernel with::
1717

1818
CONFIG_WARN_CONTEXT_ANALYSIS=y
1919

20-
The feature requires Clang 22 or later.
20+
The feature requires Clang 23 or later.
2121

2222
The analysis is *opt-in by default*, and requires declaring which modules and
2323
subsystems should be analyzed in the respective `Makefile`::

include/linux/compiler-context-analysis.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,51 @@
3939
# define __assumes_shared_ctx_lock(...) __attribute__((assert_shared_capability(__VA_ARGS__)))
4040

4141
/**
42-
* __guarded_by - struct member and globals attribute, declares variable
43-
* only accessible within active context
42+
* __guarded_by() - struct member and globals attribute, declares variable
43+
* only accessible within active context
44+
* @...: context lock instance pointer(s)
4445
*
4546
* Declares that the struct member or global variable is only accessible within
46-
* the context entered by the given context lock. Read operations on the data
47-
* require shared access, while write operations require exclusive access.
47+
* the context entered by the given context lock(s). Read operations on the data
48+
* require shared access to at least one of the context locks, while write
49+
* operations require exclusive access to all listed context locks.
4850
*
4951
* .. code-block:: c
5052
*
5153
* struct some_state {
5254
* spinlock_t lock;
5355
* long counter __guarded_by(&lock);
5456
* };
57+
*
58+
* struct some_state {
59+
* spinlock_t lock1, lock2;
60+
* long counter __guarded_by(&lock1, &lock2);
61+
* };
5562
*/
5663
# define __guarded_by(...) __attribute__((guarded_by(__VA_ARGS__)))
5764

5865
/**
59-
* __pt_guarded_by - struct member and globals attribute, declares pointed-to
60-
* data only accessible within active context
66+
* __pt_guarded_by() - struct member and globals attribute, declares pointed-to
67+
* data only accessible within active context
68+
* @...: context lock instance pointer(s)
6169
*
6270
* Declares that the data pointed to by the struct member pointer or global
6371
* pointer is only accessible within the context entered by the given context
64-
* lock. Read operations on the data require shared access, while write
65-
* operations require exclusive access.
72+
* lock(s). Read operations on the data require shared access to at least one
73+
* of the context locks, while write operations require exclusive access to all
74+
* listed context locks.
6675
*
6776
* .. code-block:: c
6877
*
6978
* struct some_state {
7079
* spinlock_t lock;
7180
* long *counter __pt_guarded_by(&lock);
7281
* };
82+
*
83+
* struct some_state {
84+
* spinlock_t lock1, lock2;
85+
* long *counter __pt_guarded_by(&lock1, &lock2);
86+
* };
7387
*/
7488
# define __pt_guarded_by(...) __attribute__((pt_guarded_by(__VA_ARGS__)))
7589

lib/Kconfig.debug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ config DEBUG_FORCE_WEAK_PER_CPU
630630

631631
config WARN_CONTEXT_ANALYSIS
632632
bool "Compiler context-analysis warnings"
633-
depends on CC_IS_CLANG && CLANG_VERSION >= 220100
633+
depends on CC_IS_CLANG && CLANG_VERSION >= 230000
634634
# Branch profiling re-defines "if", which messes with the compiler's
635635
# ability to analyze __cond_acquires(..), resulting in false positives.
636636
depends on !TRACE_BRANCH_PROFILING
@@ -641,7 +641,7 @@ config WARN_CONTEXT_ANALYSIS
641641
and releasing user-definable "context locks".
642642

643643
Clang's name of the feature is "Thread Safety Analysis". Requires
644-
Clang 22.1.0 or later.
644+
Clang 23 or later.
645645

646646
Produces warnings by default. Select CONFIG_WERROR if you wish to
647647
turn these warnings into errors.

lib/test_context-analysis.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ TEST_SPINLOCK_COMMON(read_lock,
159159
struct test_mutex_data {
160160
struct mutex mtx;
161161
int counter __guarded_by(&mtx);
162+
163+
struct mutex mtx2;
164+
int anyread __guarded_by(&mtx, &mtx2);
165+
int *anyptr __pt_guarded_by(&mtx, &mtx2);
162166
};
163167

164168
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)
219223
}
220224
}
221225

226+
static void __used test_mutex_multiguard(struct test_mutex_data *d)
227+
{
228+
mutex_lock(&d->mtx);
229+
(void)d->anyread;
230+
(void)*d->anyptr;
231+
mutex_unlock(&d->mtx);
232+
233+
mutex_lock(&d->mtx2);
234+
(void)d->anyread;
235+
(void)*d->anyptr;
236+
mutex_unlock(&d->mtx2);
237+
238+
mutex_lock(&d->mtx);
239+
mutex_lock(&d->mtx2);
240+
d->anyread++;
241+
(*d->anyptr)++;
242+
mutex_unlock(&d->mtx2);
243+
mutex_unlock(&d->mtx);
244+
}
245+
222246
struct test_seqlock_data {
223247
seqlock_t sl;
224248
int counter __guarded_by(&sl);

0 commit comments

Comments
 (0)