Root Cause Analysis
1. Functions, Structs, and Files Involved
- File:
include/linux/sched.h (and kernel/sched/Makefile)
- Functions:
__migrate_enable(), __migrate_disable(), migrate_enable(), migrate_disable()
- Calling Contexts:
- Structs & Fields:
struct task_struct::migration_disabled (and rq::nr_pinned via this_rq_pinned())
2. Technical Mechanism and Trigger Conditions
In commit 378b7708194f ("sched: Make migrate_{en,dis}able() inline"), the definitions of migrate_disable() and migrate_enable() (via __migrate_disable() and __migrate_enable()) were moved out of kernel/sched/core.c and into include/linux/sched.h to allow inlining at call sites:
// include/linux/sched.h:
static inline void __migrate_enable(void)
{
struct task_struct *p = current;
...
guard(preempt)();
if (unlikely(p->cpus_ptr != &p->cpus_mask))
___migrate_enable();
barrier();
p->migration_disabled = 0;
this_rq_pinned()--;
}
static inline void __migrate_disable(void)
{
struct task_struct *p = current;
if (p->migration_disabled) {
...
p->migration_disabled++;
return;
}
guard(preempt)();
this_rq_pinned()++;
p->migration_disabled = 1;
}
-
Instrumentation Exposure:
In kernel/sched/Makefile, KCSAN_SANITIZE := n explicitly disables KCSAN instrumentation on scheduler core source files to prevent instrumentation overhead and false-positive warnings on scheduler synchronization primitives. When __migrate_disable() and __migrate_enable() resided in kernel/sched/core.c, their accesses were not instrumented by KCSAN. Moving them into include/linux/sched.h exposed their unmarked memory accesses to KCSAN instrumentation inside external callers (kernel/trace/bpf_trace.c, net/core/filter.c, etc.).
-
Unmarked Plain Access with IRQs Enabled:
__migrate_enable() uses guard(preempt)() to disable preemption, but interrupts remain enabled. The write p->migration_disabled = 0; (line 2475) is a plain, unannotated C assignment.
-
Interrupt Arrival and Watchpoint Trigger:
While task 4520 is executing __migrate_enable() inside bpf_prog_run_array() after a tracepoint hook, KCSAN sets up a watchpoint on p->migration_disabled at line 2475 and delays execution (udelay()).
During this window, an APIC timer interrupt occurs on the same CPU (CPU 0). Upon exiting the interrupt, softirqs are processed (run_timer_softirq), invoking an ARP neighbour timer (neigh_timer_handler() $\to$ arp_solicit()), which transmits an ARP packet. During packet capture/receive processing (packet_rcv_spkt() $\to$ sock_queue_rcv_skb_reason() $\to$ sk_filter_trim_cap()), bpf_prog_run_save_cb() invokes migrate_disable().
Because softirqs run in the interrupted task's context, current is task 4520. When __migrate_disable() executes if (p->migration_disabled) (line 2483), it performs an unmarked read on task 4520->migration_disabled, colliding with the pending write watchpoint and causing KCSAN to report a data race.
3. Correctness in Real-World Setups
While flagged by KCSAN as an unmarked concurrent access, this pattern is benign in practice:
- Interrupt/softirq handlers running on the same CPU execute in a strictly nested LIFO manner relative to the interrupted task context.
- In softirq context,
preempt_count() is non-zero, preventing preemption, scheduling, or invocation of migrate_disable_switch().
- The softirq calls
migrate_disable() and migrate_enable() in a strictly balanced pair inside bpf_prog_run_save_cb().
- If the interrupt occurs before
p->migration_disabled = 0; (when p->migration_disabled == 1), the softirq increments it to 2 and decrements it back to 1 before returning. The interrupted task then sets it to 0.
- If the interrupt occurs after
p->migration_disabled = 0;, the softirq sets it to 1 and restores it to 0 before returning.
- Upon return from the interrupt, the memory state is restored to its exact pre-interrupt value, ensuring neither
migration_disabled nor rq->nr_pinned suffers counter corruption or leaks. Marking accesses with READ_ONCE()/WRITE_ONCE() or data_race() (or applying __no_kcsan) silences the warning.