Root Cause Analysis: KCSAN Data Race on task_struct::migration_disabled
1. Functions, Structs, and Files Involved
include/linux/sched.h:
__migrate_enable() (lines 2457β2460, 2475) and __migrate_disable() (lines 2483β2491, 2496).
- Field:
struct task_struct::migration_disabled (short / u16, 2 bytes at offset 1144 bytes / bit offset 9152).
kernel/trace/trace.c:
migration_disable_value() (lines 1715β1722).
tracing_gen_ctx_irq_test() (lines 1724β1748).
kernel/trace/trace_event_perf.c / kernel/sched/core.c:
perf_trace_buf_update(), perf_tp_event(), trace_sched_switch(), and __schedule().
2. Triggering Mechanism and Race Condition
-
Unmarked Write in Task Context (__migrate_enable):
-
Interruption and Unmarked Read in Tracing / Interrupt Preemption Context (migration_disable_value):
-
Violation of the Linux Kernel Memory Model (LKMM):
- Under LKMM, concurrent unmarked accesses where at least one access is a write constitute a data race. With
CONFIG_KCSAN_INTERRUPT_WATCHER=y, KCSAN detects races between a task context access and an interrupting same-CPU context (such as hardirq/softirq or IRQ-exit preemption).
- Because neither the read in
migration_disable_value() nor the updates in __migrate_enable() / __migrate_disable() use READ_ONCE() or WRITE_ONCE(), KCSAN flags this as an unannotated data race.
3. Suggested Remediation
Annotate concurrent accesses to task_struct::migration_disabled with READ_ONCE() and WRITE_ONCE():
- Use
READ_ONCE(current->migration_disabled) in migration_disable_value() (as well as scheduler inspection helpers like is_migration_disabled()).
- Use
WRITE_ONCE(p->migration_disabled, ...) in __migrate_enable() and __migrate_disable().