Skip to content

kcsan, seqlock: Support seqcount_latch_t

Marco Elver lkml 2 versions – Applied

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 · linked by author and title

  1. v2 Latest5 patches 7 repliesApplied
  2. v11 patch 8 repliesSuperseded

Changes in v2 from the cover letter

* New interface, courtesy of Peter Zijlstra. This simplifies things and we avoid instrumenting the raw interface which is now reserved for noinstr functions.
* Fix for read_seqbegin/retry() found during testing of new changes.

Patches in v2 Applied

#Subject and filesSizeRepliesApplied
1/5time/sched_clock: Swap update_clock_read_data() latch writeskernel/time/sched_clock.c+3 -30 replies
2/5time/sched_clock: Broaden sched_clock()'s instrumentation coveragekernel/time/sched_clock.c+14 -22 replies
3/5kcsan, seqlock: Support seqcount_latch_tDocumentation/locking/seqlock.rst include/linux/seqlock.h+72 -160 replies
4/5seqlock, treewide: Switch to non-raw seqcount_latch interfacearch/x86/kernel/tsc.c include/linux/rbtree_latch.h kernel/printk/printk.c +2 more+33 -250 replies
5/5kcsan, seqlock: Fix incorrect assumption in read_seqbegin()include/linux/seqlock.h+1 -115 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/5] kcsan, seqlock: Support seqcount_latch_t

While fuzzing an arm64 kernel, Alexander Potapenko reported:

| BUG: KCSAN: data-race in ktime_get_mono_fast_ns / timekeeping_update
|
| write to 0xffffffc082e74248 of 56 bytes by interrupt on cpu 0:
|  update_fast_timekeeper kernel/time/timekeeping.c:430 [inline]
|  timekeeping_update+0x1d8/0x2d8 kernel/time/timekeeping.c:768
|  timekeeping_advance+0x9e8/0xb78 kernel/time/timekeeping.c:2344
|  update_wall_time+0x18/0x38 kernel/time/timekeeping.c:2360
|  [...]
|
| read to 0xffffffc082e74258 of 8 bytes by task 5260 on cpu 1:
|  __ktime_get_fast_ns kernel/time/timekeeping.c:372 [inline]
|  ktime_get_mono_fast_ns+0x88/0x174 kernel/time/timekeeping.c:489
|  init_srcu_struct_fields+0x40c/0x530 kernel/rcu/srcutree.c:263
|  init_srcu_struct+0x14/0x20 kernel/rcu/srcutree.c:311
|  [...]
|
| value changed: 0x000002f875d33266 -> 0x000002f877416866
|
| Reported by Kernel Concurrency Sanitizer on:
| CPU: 1 UID: 0 PID: 5260 Comm: syz.2.7483 Not tainted 6.12.0-rc3-dirty #78

This is a false positive data race between a seqcount latch writer and a reader accessing stale data. Since its introduction, KCSAN has never understood the seqcount_latch interface (due to being unannotated).

Unlike the regular seqlock interface, the seqcount_latch interface for latch writers never has had a well-defined critical section, making it difficult to teach tooling where the critical section starts and ends.

This series introduces an instrumentable (non-raw) seqcount_latch interface, with which we can clearly denote writer critical sections. This both helps readability and tooling like KCSAN to understand when the writer is done updating all latch copies.

Changelog
=========

v2:
* New interface, courtesy of Peter Zijlstra. This simplifies things and we avoid instrumenting the raw interface which is now reserved for noinstr functions.
* Fix for read_seqbegin/retry() found during testing of new changes.

v1: https://lkml.kernel.org/r/[email protected]

Marco Elver (5):
time/sched_clock: Swap update_clock_read_data() latch writes time/sched_clock: Broaden sched_clock()'s instrumentation coverage kcsan, seqlock: Support seqcount_latch_t
seqlock, treewide: Switch to non-raw seqcount_latch interface kcsan, seqlock: Fix incorrect assumption in read_seqbegin()

 Documentation/locking/seqlock.rst |  2 +-
 arch/x86/kernel/tsc.c             |  5 +-
 include/linux/rbtree_latch.h      | 20 ++++---
 include/linux/seqlock.h           | 98 +++++++++++++++++++++++--------
 kernel/printk/printk.c            |  9 +--
 kernel/time/sched_clock.c         | 34 +++++++----
 kernel/time/timekeeping.c         | 12 ++--
 7 files changed, 123 insertions(+), 57 deletions(-)
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog