Root Cause Analysis
The KCSAN report is triggered by an unannotated same-CPU concurrent access to current->bpf_ctx (struct task_struct::bpf_ctx, defined in include/linux/sched.h) between task context and softirq/interrupt context:
-
Task-context plain read (include/linux/bpf.h:3422):
During a getsockopt() syscall (do_ip_getsockopt() in net/ipv4/ip_sockglue.c), sockopt_lock_sock() (net/core/sock.c:1152) calls has_current_bpf_ctx() to determine whether it is running inside a BPF program:
static inline bool has_current_bpf_ctx(void)
{
return !!current->bpf_ctx;
}
This performs a plain (non-READ_ONCE) load of current->bpf_ctx.
-
Interrupt/softirq-context plain read-write (include/linux/bpf.h:2760):
With CONFIG_KCSAN_INTERRUPT_WATCHER=y, kcsan_setup_watchpoint() sets a watchpoint on ¤t->bpf_ctx and stalls inside delay_access() (udelay()) with interrupts enabled. During this delay on CPU 1, an APIC timer interrupt fires and runs softirqs (handle_softirqs() -> rcu_core() -> __d_free() -> kmem_cache_free()). The trace_kmem_cache_free tracepoint invokes perf_trace_kmem_cache_free() -> perf_trace_run_bpf_submit() -> trace_call_bpf() (kernel/trace/bpf_trace.c:147) -> bpf_prog_run_array() (include/linux/bpf.h:2797), which calls bpf_set_run_ctx(&run_ctx.run_ctx):
static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
{
struct bpf_run_ctx *old_ctx = NULL;
#ifdef CONFIG_BPF_SYSCALL
old_ctx = current->bpf_ctx;
current->bpf_ctx = new_ctx;
#endif
return old_ctx;
}
Because current in interrupt/softirq context on CPU 1 still refers to the interrupted task (PID 3302), bpf_set_run_ctx() performs a plain read and plain write to the same ¤t->bpf_ctx address watched by KCSAN, causing KCSAN to report a data race.
Invariant & Lifecycle Details
current->bpf_ctx is only ever accessed by the task currently running on that CPU (or by interrupt/softirq/NMI handlers preempting it on the same CPU).
bpf_prog_run_array() (and all other bpf_set_run_ctx() callers) follows a strict stack-based save/restore discipline: it saves old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx) before running BPF programs and restores bpf_reset_run_ctx(old_run_ctx) (current->bpf_ctx = old_ctx) before returning from the interrupt/softirq.
- Consequently, the interrupted task cannot observe the temporary
new_ctx pointer set during interrupt execution. The defect is limited to missing READ_ONCE() / WRITE_ONCE() (or data_race()) annotations on current->bpf_ctx in has_current_bpf_ctx(), bpf_set_run_ctx(), and bpf_reset_run_ctx() in include/linux/bpf.h to prevent compiler load/store tearing and satisfy KCSAN under CONFIG_KCSAN_INTERRUPT_WATCHER=y.