kcsan: Support detecting a subset of missing memory barriers
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v3 Latest25 patches 13 repliesIn review
- v223 patches 10 repliesSuperseded
- v123 patches 12 repliesSuperseded
Changes in v3 from the cover letter
* Rework to avoid kcsan_noinstr hackery, because it is unclear if this works on architectures like arm64. A better alternative exists where we can get __no_kcsan to work for barrier instrumentation, too. Clang's and GCC's __no_kcsan (== __no_sanitize_thread) behave slightly differently, which is reflected in KCSAN_WEAK_MEMORY's dependencies (either STACK_VALIDATION for older Clang, or GCC which works as-is).
* Rework to avoid inserting explicit calls for barrier instrumentation, and instead repurpose __atomic_signal_fence (see comment at __tsan_atomic_signal_fence), which is handled by fsanitize=thread instrumentation and can therefore be removed via __no_kcsan.
* objtool: s/removable_instr/profiling_func/, and more comments per Josh's suggestion.
* Minimize diff in patch removing zero-initialization of globals.
* Don't define kcsan_weak_memory bool if !KCSAN_WEAK_MEMORY.
* Apply Acks.
* 2 new patches to make it work with Clang >= 14.0 without objtool, which will be required on non-x86 architectures.
Patches in v3 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/25 | kcsan: Refactor reading of instrumented memorykernel/ | +17 | 0 replies | |
| 2/25 | kcsan: Remove redundant zero-initialization of globalsinit/ kernel/ | +0 | 0 replies | |
| 3/25 | kcsan: Avoid checking scoped accesses from nested contextsinclude/ kernel/ | +16 | 0 replies | |
| 4/25 | kcsan: Add core support for a subset of weak memory modelinginclude/ include/ include/ +3 more | +242 | 7 replies | |
| 5/25 | kcsan: Add core memory barrier instrumentation functionsinclude/ kernel/ | +136 | 0 replies | |
| 6/25 | kcsan, kbuild: Add option for barrier instrumentation onlyscripts/ | +5 | 0 replies | |
| 7/25 | kcsan: Call scoped accesses reordered in reportskernel/ kernel/ | +10 | 0 replies | |
| 8/25 | kcsan: Show location access was reordered tokernel/ | +23 | 6 replies | |
| 9/25 | kcsan: Document modeling of weak memoryDocumentation/ | +63 | 0 replies | |
| 10/25 | kcsan: test: Match reordered or normal accesseskernel/ | +63 | 0 replies | |
| 11/25 | kcsan: test: Add test cases for memory barrier instrumentationkernel/ | +319 | 0 replies | |
| 12/25 | kcsan: Ignore GCC 11+ warnings about TSan runtime supportscripts/ | +6 | 0 replies | |
| 13/25 | kcsan: selftest: Add test case to check memory barrier instrumentationkernel/ kernel/ | +143 | 0 replies | |
| 14/25 | locking/barriers, kcsan: Add instrumentation for barriersinclude/ include/ | +16 | 0 replies | |
| 15/25 | locking/barriers, kcsan: Support generic instrumentationinclude/ | +25 | 0 replies | |
| 16/25 | locking/atomics, kcsan: Add instrumentation for barriersinclude/ scripts/ | +166 | 0 replies | |
| 17/25 | asm-generic/bitops, kcsan: Add instrumentation for barriersinclude/ include/ | +6 | 0 replies | |
| 18/25 | x86/barriers, kcsan: Use generic instrumentation for non-smp barriersarch/ | +5 | 0 replies | |
| 19/25 | x86/qspinlock, kcsan: Instrument barrier of pv_queued_spin_unlock()arch/ | +1 | 0 replies | |
| 20/25 | mm, kcsan: Enable barrier instrumentationmm/ | +2 | 0 replies | |
| 21/25 | sched, kcsan: Enable memory barrier instrumentationkernel/ | +3 | 0 replies | |
| 22/25 | objtool, kcsan: Add memory barrier instrumentation to whitelisttools/ | +4 | 0 replies | |
| 23/25 | objtool, kcsan: Remove memory barrier instrumentation from noinstrtools/ tools/ | +32 | 0 replies | |
| 24/25 | compiler_attributes.h: Add __disable_sanitizer_instrumentationinclude/ | +18 | 0 replies | |
| 25/25 | kcsan: Support WEAK_MEMORY with Clang where no objtool support existsinclude/ lib/ | +13 | 0 replies |
Reviews trailers per patch
4 of 25 patches have a Reviewed-by or Acked-by.
| Patch | Mark Rutland | Josh Poimboeuf | Boqun Feng |
|---|---|---|---|
| 1/25 | A-b | ||
| 2/25 | A-b | ||
| 3/25 | |||
| 4/25 | |||
| 5/25 | |||
| 6/25 | |||
| 7/25 | |||
| 8/25 | R-b | ||
| 9/25 | |||
| 10/25 | |||
| 11/25 | |||
| 12/25 | |||
| 13/25 | |||
| 14/25 | |||
| 15/25 | |||
| 16/25 | |||
| 17/25 | |||
| 18/25 | |||
| 19/25 | |||
| 20/25 | |||
| 21/25 | |||
| 22/25 | |||
| 23/25 | A-b | ||
| 24/25 | |||
| 25/25 |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.
Cover letter [PATCH v3 00/25] kcsan: Support detecting a subset of missing memory barriers
Detection of some missing memory barriers has been on the KCSAN feature wishlist for some time: this series adds support for modeling a subset of weak memory as defined by the LKMM, which enables detection of a subset of data races due to missing memory barriers.
KCSAN's approach to detecting missing memory barriers is based on modeling access reordering. Each memory access for which a watchpoint is set up, is also selected for simulated reordering within the scope of its function (at most 1 in-flight access).
We are limited to modeling the effects of "buffering" (delaying the access), since the runtime cannot "prefetch" accesses. Once an access has been selected for reordering, it is checked along every other access until the end of the function scope. If an appropriate memory barrier is encountered, the access will no longer be considered for reordering.
When the result of a memory operation should be ordered by a barrier, KCSAN can then detect data races where the conflict only occurs as a result of a missing barrier due to reordering accesses.
Some more details and an example are captured in the updated <Documentation/dev-tools/kcsan.rst>.
Some light fuzzing with the feature also resulted in a discussion [1] around an issue which appears to be allowed, but unlikely in practice.
[1] https://lkml.kernel.org/r/[email protected]
The first half of the series are core KCSAN changes, documentation updates, and test changes. The second half adds instrumentation to barriers, atomics, bitops, along with enabling barrier instrumentation for some currently uninstrumented subsystems.
Followed by objtool changes to add the usual entries to the uaccess whitelist, but also instruct objtool to remove memory barrier instrumentation from noinstr code (on x86), given not all versions of Clang currently respect __no_kcsan (== __no_sanitize_thread) for the new instrumentation.
The last 2 patches (new in v3) fix up __no_kcsan for newer versions of Clang, so that non-x86 architectures can enable weak memory modeling with Clang 14.0 or newer.
Changelog
---------
v3:
* Rework to avoid kcsan_noinstr hackery, because it is unclear if this works on architectures like arm64. A better alternative exists where we can get __no_kcsan to work for barrier instrumentation, too. Clang's and GCC's __no_kcsan (== __no_sanitize_thread) behave slightly differently, which is reflected in KCSAN_WEAK_MEMORY's dependencies (either STACK_VALIDATION for older Clang, or GCC which works as-is).
* Rework to avoid inserting explicit calls for barrier instrumentation, and instead repurpose __atomic_signal_fence (see comment at __tsan_atomic_signal_fence), which is handled by fsanitize=thread instrumentation and can therefore be removed via __no_kcsan.
* objtool: s/removable_instr/profiling_func/, and more comments per Josh's suggestion.
* Minimize diff in patch removing zero-initialization of globals.
* Don't define kcsan_weak_memory bool if !KCSAN_WEAK_MEMORY.
* Apply Acks.
* 2 new patches to make it work with Clang >= 14.0 without objtool, which will be required on non-x86 architectures.
v2: https://lkml.kernel.org/r/[email protected]
* Rewrite objtool patch after rebase to v5.16-rc1.
* Note the reason in documentation that address or control dependencies do not require special handling.
* Rename kcsan_atomic_release() to kcsan_atomic_builtin_memorder() to avoid confusion.
* Define kcsan_noinstr as noinline if we rely on objtool nop'ing out calls, to avoid things like LTO inlining it.
v1: https://lore.kernel.org/all/[email protected]/
---
Alexander Potapenko (1):
compiler_attributes.h: Add __disable_sanitizer_instrumentation
Marco Elver (24):
kcsan: Refactor reading of instrumented memory
kcsan: Remove redundant zero-initialization of globals
kcsan: Avoid checking scoped accesses from nested contexts
kcsan: Add core support for a subset of weak memory modeling
kcsan: Add core memory barrier instrumentation functions kcsan, kbuild: Add option for barrier instrumentation only
kcsan: Call scoped accesses reordered in reports
kcsan: Show location access was reordered to
kcsan: Document modeling of weak memory
kcsan: test: Match reordered or normal accesses
kcsan: test: Add test cases for memory barrier instrumentation
kcsan: Ignore GCC 11+ warnings about TSan runtime support
kcsan: selftest: Add test case to check memory barrier instrumentation locking/barriers, kcsan: Add instrumentation for barriers locking/barriers, kcsan: Support generic instrumentation locking/atomics, kcsan: Add instrumentation for barriers asm-generic/bitops, kcsan: Add instrumentation for barriers x86/barriers, kcsan: Use generic instrumentation for non-smp barriers x86/qspinlock, kcsan: Instrument barrier of pv_queued_spin_unlock() mm, kcsan: Enable barrier instrumentation
sched, kcsan: Enable memory barrier instrumentation objtool, kcsan: Add memory barrier instrumentation to whitelist objtool, kcsan: Remove memory barrier instrumentation from noinstr
kcsan: Support WEAK_MEMORY with Clang where no objtool support exists
Documentation/dev-tools/kcsan.rst | 76 +++- arch/x86/include/asm/barrier.h | 10 +- arch/x86/include/asm/qspinlock.h | 1 + include/asm-generic/barrier.h | 54 ++- .../asm-generic/bitops/instrumented-atomic.h | 3 + .../asm-generic/bitops/instrumented-lock.h | 3 + include/linux/atomic/atomic-instrumented.h | 135 +++++- include/linux/compiler_attributes.h | 18 + include/linux/compiler_types.h | 13 +- include/linux/kcsan-checks.h | 81 +++- include/linux/kcsan.h | 11 +- include/linux/sched.h | 3 + include/linux/spinlock.h | 2 +- init/init_task.c | 5 - kernel/kcsan/Makefile | 2 + kernel/kcsan/core.c | 345 ++++++++++++--- kernel/kcsan/kcsan_test.c | 415 ++++++++++++++++-- kernel/kcsan/report.c | 51 ++- kernel/kcsan/selftest.c | 141 ++++++ kernel/sched/Makefile | 7 +- lib/Kconfig.kcsan | 20 + mm/Makefile | 2 + scripts/Makefile.kcsan | 15 +- scripts/Makefile.lib | 5 + scripts/atomic/gen-atomic-instrumented.sh | 41 +- tools/objtool/check.c | 41 +- tools/objtool/include/objtool/elf.h | 2 +- 27 files changed, 1330 insertions(+), 172 deletions(-)
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog