Root Cause Analysis
The KCSAN report is triggered by an unmarked read/write of the per-CPU recursion-tracking bitmask cpu_buffer->current_context (unsigned int in struct ring_buffer_per_cpu) inside trace_recursive_lock() (kernel/trace/ring_buffer.c:4482-4507), which is inlined into ring_buffer_lock_reserve() (kernel/trace/ring_buffer.c:5068).
Mechanism
- Task context on CPU 0:
process_one_work() emits the workqueue_execute_end tracepoint, calling ring_buffer_lock_reserve(). It disables preemption via preempt_disable_notrace(), selects the CPU 0 struct ring_buffer_per_cpu *cpu_buffer, and enters trace_recursive_lock(cpu_buffer).
- Watchpoint setup: At
kernel/trace/ring_buffer.c:4485, trace_recursive_lock() performs a plain load:
unsigned int val = cpu_buffer->current_context;
With CONFIG_KCSAN_INTERRUPT_WATCHER=y, KCSAN sets a watchpoint on &cpu_buffer->current_context and delays execution inside kcsan_setup_watchpoint() (kernel/kcsan/core.c:622).
- Interrupt context on CPU 0: During that delay, an IPI (
sysvec_call_function_single -> __sysvec_call_function_single()) arrives on CPU 0 and emits the call_function_single_entry tracepoint (arch/x86/include/asm/trace/irq_vectors.h:98). This also calls ring_buffer_lock_reserve() -> trace_recursive_lock(cpu_buffer) on the same CPU 0 cpu_buffer.
- Conflicting write: The interrupt handler reads
cpu_buffer->current_context, sets its context bit (RB_CTX_IRQ), and performs a plain store at kernel/trace/ring_buffer.c:4504:
val |= (1 << (bit + cpu_buffer->nest));
cpu_buffer->current_context = val;
This triggers KCSAN's watchpoint, reporting a 4-byte read/write data race on cpu_buffer->current_context.
Why the Race Occurs and Invariants
kernel/trace/Makefile only disables KCSAN instrumentation (KCSAN_SANITIZE := n) when CONFIG_FUNCTION_TRACER is enabled; with function tracing disabled and CONFIG_KCSAN_INTERRUPT_WATCHER=y enabled, KCSAN instruments kernel/trace/ring_buffer.c and monitors accesses across interrupt boundaries on the same CPU.
trace_recursive_lock() (lines 4485, 4504) and trace_recursive_unlock() (lines 4512–4513) access cpu_buffer->current_context via plain C reads/writes without READ_ONCE()/WRITE_ONCE() or data_race().
- Logically, the protocol preserves
cpu_buffer->current_context across same-CPU interrupts:
ring_buffer_lock_reserve() disables preemption before calling trace_recursive_lock(), so only higher-priority interrupt contexts (softirq, hardirq, NMI) on the same CPU can preempt it.
- Whenever
trace_recursive_lock() in the interrupted task reaches line 4504, at most the task's RB_CTX_NORMAL bit (bit 4) was set and RB_CTX_TRANSITION (bit 0) was clear. An interrupting IRQ handler sets a strictly lower context bit (RB_CTX_IRQ, bit 2, or RB_CTX_NMI, bit 1) and always calls trace_recursive_unlock() (either on rb_reserve_next_event() failure at ring_buffer.c:5078 or on commit in ring_buffer_unlock_commit() at ring_buffer.c:4584 / ring_buffer_discard_commit() at ring_buffer.c:5167) before returning from the interrupt.
trace_recursive_unlock() clears the lowest set bit at or above cpu_buffer->nest (current_context &= current_context - (1 << cpu_buffer->nest)), which restores cpu_buffer->current_context to its exact pre-interrupt value before the task resumes at line 4504.
Annotating the accesses to cpu_buffer->current_context in trace_recursive_lock() and trace_recursive_unlock() with READ_ONCE() / WRITE_ONCE() (or data_race() if compiler reordering/tearing is otherwise prevented) resolves the KCSAN report.