linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/23] kcsan: Support detecting a subset of missing memory barriers
@ 2021-11-18  8:10 Marco Elver
  2021-11-18  8:10 ` [PATCH v2 01/23] kcsan: Refactor reading of instrumented memory Marco Elver
                   ` (22 more replies)
  0 siblings, 23 replies; 34+ messages in thread
From: Marco Elver @ 2021-11-18  8:10 UTC (permalink / raw)
  To: elver, Paul E. McKenney
  Cc: Alexander Potapenko, Boqun Feng, Borislav Petkov, Dmitry Vyukov,
	Ingo Molnar, Josh Poimboeuf, Mark Rutland, Peter Zijlstra,
	Thomas Gleixner, Waiman Long, Will Deacon, kasan-dev, linux-arch,
	linux-doc, linux-kbuild, linux-kernel, linux-mm, x86

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/YRo58c+JGOvec7tc@elver.google.com


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. The last two patches are
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).

---

v2:
* 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/20211005105905.1994700-1-elver@google.com/

Marco Elver (23):
  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

 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/kcsan-checks.h                  |  51 ++-
 include/linux/kcsan.h                         |  11 +-
 include/linux/sched.h                         |   3 +
 include/linux/spinlock.h                      |   2 +-
 init/init_task.c                              |   9 +-
 kernel/kcsan/Makefile                         |   2 +
 kernel/kcsan/core.c                           | 326 +++++++++++---
 kernel/kcsan/kcsan_test.c                     | 416 ++++++++++++++++--
 kernel/kcsan/report.c                         |  51 ++-
 kernel/kcsan/selftest.c                       | 141 ++++++
 kernel/sched/Makefile                         |   7 +-
 lib/Kconfig.kcsan                             |  16 +
 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 +-
 25 files changed, 1248 insertions(+), 175 deletions(-)

-- 
2.34.0.rc2.393.gf8c9666880-goog


^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2021-11-29 18:11 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-18  8:10 [PATCH v2 00/23] kcsan: Support detecting a subset of missing memory barriers Marco Elver
2021-11-18  8:10 ` [PATCH v2 01/23] kcsan: Refactor reading of instrumented memory Marco Elver
2021-11-18 11:08   ` Mark Rutland
2021-11-18  8:10 ` [PATCH v2 02/23] kcsan: Remove redundant zero-initialization of globals Marco Elver
2021-11-18 11:09   ` Mark Rutland
2021-11-18  8:10 ` [PATCH v2 03/23] kcsan: Avoid checking scoped accesses from nested contexts Marco Elver
2021-11-29  8:47   ` Boqun Feng
2021-11-29 10:57     ` Marco Elver
2021-11-29 14:26       ` Boqun Feng
2021-11-29 14:42         ` Marco Elver
2021-11-18  8:10 ` [PATCH v2 04/23] kcsan: Add core support for a subset of weak memory modeling Marco Elver
2021-11-18  8:10 ` [PATCH v2 05/23] kcsan: Add core memory barrier instrumentation functions Marco Elver
2021-11-18  8:10 ` [PATCH v2 06/23] kcsan, kbuild: Add option for barrier instrumentation only Marco Elver
2021-11-18  8:10 ` [PATCH v2 07/23] kcsan: Call scoped accesses reordered in reports Marco Elver
2021-11-18  8:10 ` [PATCH v2 08/23] kcsan: Show location access was reordered to Marco Elver
2021-11-18  8:10 ` [PATCH v2 09/23] kcsan: Document modeling of weak memory Marco Elver
2021-11-18  8:10 ` [PATCH v2 10/23] kcsan: test: Match reordered or normal accesses Marco Elver
2021-11-18  8:10 ` [PATCH v2 11/23] kcsan: test: Add test cases for memory barrier instrumentation Marco Elver
2021-11-18  8:10 ` [PATCH v2 12/23] kcsan: Ignore GCC 11+ warnings about TSan runtime support Marco Elver
2021-11-18  8:10 ` [PATCH v2 13/23] kcsan: selftest: Add test case to check memory barrier instrumentation Marco Elver
2021-11-18  8:10 ` [PATCH v2 14/23] locking/barriers, kcsan: Add instrumentation for barriers Marco Elver
2021-11-18  8:10 ` [PATCH v2 15/23] locking/barriers, kcsan: Support generic instrumentation Marco Elver
2021-11-18  8:10 ` [PATCH v2 16/23] locking/atomics, kcsan: Add instrumentation for barriers Marco Elver
2021-11-18  8:10 ` [PATCH v2 17/23] asm-generic/bitops, " Marco Elver
2021-11-18  8:10 ` [PATCH v2 18/23] x86/barriers, kcsan: Use generic instrumentation for non-smp barriers Marco Elver
2021-11-18  8:10 ` [PATCH v2 19/23] x86/qspinlock, kcsan: Instrument barrier of pv_queued_spin_unlock() Marco Elver
2021-11-18  8:10 ` [PATCH v2 20/23] mm, kcsan: Enable barrier instrumentation Marco Elver
2021-11-18  8:10 ` [PATCH v2 21/23] sched, kcsan: Enable memory " Marco Elver
2021-11-18  8:10 ` [PATCH v2 22/23] objtool, kcsan: Add memory barrier instrumentation to whitelist Marco Elver
2021-11-18  8:10 ` [PATCH v2 23/23] objtool, kcsan: Remove memory barrier instrumentation from noinstr Marco Elver
2021-11-19 20:31   ` Josh Poimboeuf
2021-11-19 21:31     ` Marco Elver
2021-11-23 11:29     ` Marco Elver
2021-11-24 17:53       ` Josh Poimboeuf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).