compiler-context-analysis: Scoped init guards
Status from this message: The following commit has been merged into the locking/core branch of tip:
Get series with b4
b4 am [email protected]Versions newest first
Patches in v1 Applied
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/6 | cleanup: Make __DEFINE_LOCK_GUARD handle commas in initializersinclude/ | +4 | 0 replies | |
| 2/6 | compiler-context-analysis: Introduce scoped init guardsDocumentation/ include/ include/ +6 more | +70 | 0 replies | |
| 3/6 | kcov: Use scoped init guardkernel/ | +1 | 0 replies | |
| 4/6 | crypto: Use scoped init guardcrypto/ crypto/ | +2 | 0 replies | |
| 5/6 | tomoyo: Use scoped init guardsecurity/ | +1 | 0 replies | |
| 6/6 | compiler-context-analysis: Remove __assume_ctx_lock from initializersinclude/ include/ include/ +7 more | +2 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH tip/locking/core 0/6] compiler-context-analysis: Scoped init guards
Current context analysis treats lock_init() as implicitly "holding" the lock to allow initializing guarded members. This causes false-positive "double lock" reports if the lock is acquired immediately after initialization in the same scope; for example:
mutex_init(&d->mtx); /* ... counter is guarded by mtx ... */ d->counter = 0; /* ok, but mtx is now "held" */ ... mutex_lock(&d->mtx); /* warning: acquiring mutex already held */
This series proposes a solution to this by introducing scoped init guards which Peter suggested, using the guard(type_init)(&lock) or scoped_guard(type_init, ..) interface. This explicitly marks init scope where we can initialize guarded members. With that we can revert the "implicitly hold" after init annotations, which allows use after initialization scope as follows:
scoped_guard(mutex_init, &d->mtx) {
d->counter = 0;
}
...
mutex_lock(&d->mtx); /* ok */Note: Scoped guarded initialization remains optional, and normal initialization can still be used if no guarded members are being initialized. Another alternative is to just disable context analysis to initialize guarded members with context_unsafe(var = init) or adding the __context_unsafe(init) function attribute (the latter not being recommended for non-trivial functions due to lack of any checking):
mutex_init(&d->mtx); context_unsafe(d->counter = 0); /* ok */ ... mutex_lock(&d->mtx);
This series is an alternative to the approach in [1]:
* Scoped init guards (this series): Sound interface, requires use of guard(type_init)(&lock) or scoped_guard(type_init, ..) for guarded member initialization.
* Reentrant init [1]: Less intrusive, type_init() just works, and also allows guarded member initialization with later lock use in the same function. But unsound, and e.g. misses double-lock bugs immediately after init, trading false positives for false negatives.
[1] https://lore.kernel.org/all/[email protected]/
Marco Elver (6):
cleanup: Make __DEFINE_LOCK_GUARD handle commas in initializers
compiler-context-analysis: Introduce scoped init guards
kcov: Use scoped init guard
crypto: Use scoped init guard
tomoyo: Use scoped init guard
compiler-context-analysis: Remove __assume_ctx_lock from initializers
Documentation/dev-tools/context-analysis.rst | 30 ++++++++++++++++++-- crypto/crypto_engine.c | 2 +- crypto/drbg.c | 2 +- include/linux/cleanup.h | 8 +++--- include/linux/compiler-context-analysis.h | 9 ++---- include/linux/local_lock.h | 8 ++++++ include/linux/local_lock_internal.h | 4 +-- include/linux/mutex.h | 4 ++- include/linux/rwlock.h | 3 +- include/linux/rwlock_rt.h | 1 - include/linux/rwsem.h | 6 ++-- include/linux/seqlock.h | 6 +++- include/linux/spinlock.h | 17 ++++++++--- include/linux/spinlock_rt.h | 1 - include/linux/ww_mutex.h | 1 - kernel/kcov.c | 2 +- lib/test_context-analysis.c | 22 ++++++-------- security/tomoyo/common.c | 2 +- 18 files changed, 80 insertions(+), 48 deletions(-)
Signature · 1 line
2.52.0.457.g6b5491de43-goog