compiler-context-analysis: Add infrastructure for Context Analysis with Clang

Context Analysis is a language extension, which enables statically
checking that required contexts are active (or inactive), by acquiring
and releasing user-definable "context locks". An obvious application is
lock-safety checking for the kernel's various synchronization primitives
(each of which represents a "context lock"), and checking that locking
rules are not violated.

Clang originally called the feature "Thread Safety Analysis" [1]. This
was later changed and the feature became more flexible, gaining the
ability to define custom "capabilities". Its foundations can be found in
"Capability Systems" [2], used to specify the permissibility of
operations to depend on some "capability" being held (or not held).

Because the feature is not just able to express "capabilities" related
to synchronization primitives, and "capability" is already overloaded in
the kernel, the naming chosen for the kernel departs from Clang's
"Thread Safety" and "capability" nomenclature; we refer to the feature
as "Context Analysis" to avoid confusion. The internal implementation
still makes references to Clang's terminology in a few places, such as
`-Wthread-safety` being the warning option that also still appears in
diagnostic messages.

 [1] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
 [2] https://www.cs.cornell.edu/talc/papers/capabilities.pdf

See more details in the kernel-doc documentation added in this and
subsequent changes.

Clang version 22+ is required.

[peterz: disable the thing for __CHECKER__ builds]
Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20251219154418.3592607-3-elver@google.com
This commit is contained in:
Marco Elver
2025-12-19 16:39:51 +01:00
committed by Peter Zijlstra
parent de15fecae4
commit 3269701cb2
5 changed files with 505 additions and 7 deletions

View File

@@ -621,6 +621,36 @@ config DEBUG_FORCE_WEAK_PER_CPU
To ensure that generic code follows the above rules, this
option forces all percpu variables to be defined as weak.
config WARN_CONTEXT_ANALYSIS
bool "Compiler context-analysis warnings"
depends on CC_IS_CLANG && CLANG_VERSION >= 220000
# 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
default y
help
Context Analysis is a language extension, which enables statically
checking that required contexts are active (or inactive) by acquiring
and releasing user-definable "context locks".
Clang's name of the feature is "Thread Safety Analysis". Requires
Clang 22 or later.
Produces warnings by default. Select CONFIG_WERROR if you wish to
turn these warnings into errors.
For more details, see Documentation/dev-tools/context-analysis.rst.
config WARN_CONTEXT_ANALYSIS_ALL
bool "Enable context analysis for all source files"
depends on WARN_CONTEXT_ANALYSIS
depends on EXPERT && !COMPILE_TEST
help
Enable tree-wide context analysis. This is likely to produce a
large number of false positives - enable at your own risk.
If unsure, say N.
endmenu # "Compiler options"
menu "Generic Kernel Debugging Instruments"