Root Cause Analysis
The KCSAN report is caused by an unmarked concurrent write and read to the 2-byte unsigned short field task_struct::migration_disabled (include/linux/sched.h) on the same CPU between task context and interrupt/softirq context:
-
Write side (task context):
In trace_call_bpf() (kernel/trace/bpf_trace.c:147) -> bpf_prog_run_array() (include/linux/bpf.h:2796) -> migrate_disable() -> __migrate_disable() (include/linux/sched.h:2479-2497), the task updates its own migration-disable counter:
- Line 2490:
p->migration_disabled++; (if already non-zero)
- Line 2494-2496:
guard(preempt)(); this_rq_pinned()++; p->migration_disabled = 1;
Here, guard(preempt)() only disables task preemption; it does not disable hardirqs or softirqs. Similarly, __migrate_enable() (include/linux/sched.h:2444-2477) modifies p->migration_disabled without WRITE_ONCE().
-
Read side (interrupt / softirq context on the same CPU):
While CPU 0 is inside __migrate_disable(), an APIC timer interrupt (sysvec_apic_timer_interrupt) fires and runs softirq processing (__irq_exit_rcu() -> handle_softirqs() -> rcu_core_si() -> rcu_core() -> rcu_do_batch() -> radix_tree_node_rcu_free() -> kmem_cache_free()). During softirq execution on CPU 0, current still points to the interrupted task (p).
kmem_cache_free() triggers the kmem_cache_free tracepoint (perf_trace_kmem_cache_free() -> perf_trace_run_bpf_submit() -> perf_tp_event() -> perf_trace_buf_update()), which calls tracing_gen_ctx() -> tracing_gen_ctx_flags() -> tracing_gen_ctx_irq_test() (kernel/trace/trace.c:1747) -> migration_disable_value() (kernel/trace/trace.c:1715-1722):
static unsigned short migration_disable_value(void)
{
#if defined(CONFIG_SMP)
return current->migration_disabled;
#else
return 0;
#endif
}
tracing_gen_ctx_irq_test() clamps the returned value via min_t(unsigned int, migration_disable_value(), 0xf) << 4, and tracing_generic_entry_update() (include/linux/trace_events.h:181) stores it in the upper nibble of struct trace_entry::preempt_count.
-
Missing memory access annotations:
Because __migrate_disable() (and __migrate_enable()) writes to p->migration_disabled with plain C assignments/increments and migration_disable_value() reads current->migration_disabled with a plain C load from interrupt context, KCSAN flags the unsynchronized 2-byte access to task_struct::migration_disabled. Using READ_ONCE() in migration_disable_value() and WRITE_ONCE() when updating p->migration_disabled in __migrate_disable()/__migrate_enable() (or marking the trace-context read appropriately) resolves the KCSAN data race.