Technical Root Cause Analysis
1. Mechanism and Call Flow
The reported KCSAN bug is a data race on current->migration_disabled (a 16-bit integer located in struct task_struct) between a task-context write and an interrupt/softirq-context read on the same CPU:
-
Task Context (Writer):
A task executing the bpf() syscall (bpf_map_update_value) calls bpf_disable_instrumentation() (include/linux/bpf.h:2875), which invokes migrate_disable() -> __migrate_disable() (include/linux/sched.h:2479).
In __migrate_disable(), because p->migration_disabled is initially 0:
- It acquires
guard(preempt)(), which disables preemption (preempt_disable()).
- It increments
this_rq_pinned()++.
- It writes
p->migration_disabled = 1; (include/linux/sched.h:2496).
-
Interrupt and Softirq Interruption (Reader):
guard(preempt)() only disables preemption; it does not mask hardware interrupts (local_irq_save) or bottom halves.
While the write to p->migration_disabled = 1 is being executed (where KCSAN inserts a watchpoint delay), a hardware timer interrupt (sysvec_apic_timer_interrupt) triggers on CPU 1.
On IRQ exit, __irq_exit_rcu() (kernel/softirq.c:780) determines that irq_count() == HARDIRQ_OFFSET && local_softirq_pending() and invokes pending softirqs via invoke_softirq() -> __do_softirq().
Within RCU_SOFTIRQ (rcu_core_si -> rcu_core -> rcu_do_batch), deferred socket destruction is invoked (deferred_put_nlk_sk -> sk_free -> __sk_free -> sk_destruct -> __sk_destruct -> bpf_sk_storage_free).
-
Re-entrancy into __migrate_disable():
In net/core/bpf_sk_storage.c:52, bpf_sk_storage_free() calls rcu_read_lock_dont_migrate(). When CONFIG_PREEMPT_RCU is enabled, rcu_read_lock_dont_migrate() (include/linux/rcupdate.h:986) calls migrate_disable().
Because the softirq runs in the context of the interrupted task, current is still the same task_struct.
__migrate_disable() executes:
if (p->migration_disabled) {
...
}
This performs an un-annotated, non-atomic 2-byte read on p->migration_disabled (include/linux/sched.h:2483), directly racing with the interrupted thread's store to p->migration_disabled = 1 (include/linux/sched.h:2496).
2. Violated Invariants & Underlying Defect
- Unnecessary
migrate_disable() in bpf_sk_storage_free():
Socket destruction (__sk_destruct) frequently occurs in softirq or RCU callback contexts where migration is inherently impossible (interrupt/softirq execution cannot be migrated to another CPU). Furthermore, bpf_local_storage no longer relies on per-CPU busy counters. Similar local storage destructors (bpf_task_storage_free and bpf_cgrp_storage_free) use standard rcu_read_lock() / rcu_read_unlock(). bpf_sk_storage_free() unnecessarily invoked rcu_read_lock_dont_migrate().
- Lack of IRQ / Softirq Re-entrancy Protection in
__migrate_disable():
__migrate_disable() only disables preemption, leaving current->migration_disabled susceptible to concurrent reads and writes if any code executed in hardirq/softirq context on the same CPU also calls migrate_disable().