Bug Analysis: Data Race on current->flags in memalloc_restore_scope / perf_callchain
1. Raced Object and Locations
The 4-byte data race reported by KCSAN at address 0xffff88810250c52c occurs on current->flags (unsigned int flags in struct task_struct, defined in include/linux/sched.h:858) between two code paths:
-
Writer (RMW):
-
Reader:
2. Trigger Mechanism
The reported race occurs on the same CPU (CPU 1) and within the context of the same task (PID 4438):
- Task 4438 in kernel mode enters
memalloc_restore_scope() -> memalloc_flags_restore() while finishing a percpu memory allocation (pcpu_alloc_noprof).
- When accessing
current->flags, KCSAN instruments the store and sets up a watchpoint (kcsan_setup_watchpoint), stalling execution briefly in a delay loop (delay_access -> udelay).
- While the watchpoint on
current->flags is active, a hardware timer interrupt (sysvec_apic_timer_interrupt) occurs on CPU 1, interrupting task 4438.
- On the interrupt exit path (
irqentry_exit -> irqentry_exit_to_kernel_mode_preempt -> preempt_schedule_irq -> __schedule), hardirq context has ended, re-enabling KCSAN access tracking while still running under task 4438.
- In
__schedule(), the sched:sched_switch tracepoint fires. A perf event attached to sched_switch samples the context switch via:
perf_trace_sched_switch() $\to$ perf_tp_event() $\to$ perf_swevent_event() $\to$ perf_swevent_overflow() $\to$ __perf_event_overflow() $\to$ perf_prepare_sample() $\to$ perf_sample_save_callchain() $\to$ perf_callchain().
- Inside
perf_callchain(), is_user_task(current) is called to determine whether user callchain sampling should be performed.
is_user_task() executes an unmarked read on task->flags (current->flags). Because KCSAN's watchpoint for task 4438's store to current->flags is still active, KCSAN detects the concurrent read-write access without synchronization annotations and emits the data race splat.
This scenario is not limited to scheduler tracepoints; any interrupt, NMI, or PMU sampling event that interrupts a task while it updates current->flags and subsequently invokes perf routines calling is_user_task(current) can trigger this data race.
3. Root Cause and Concurrency Analysis
- Missing Concurrency Annotations: Under the Linux Kernel Memory Model (LKMM), shared memory locations concurrently accessed across asynchronous or preemptive contexts must use marked access helpers (such as
READ_ONCE() or data_race()). For instance, other readers of current->flags in the memory-management subsystem (such as current_gfp_context() in include/linux/sched/mm.h:250) use READ_ONCE(current->flags). However, is_user_task() reads task->flags directly without READ_ONCE() or data_race().
- Benign Race Invariant:
task->flags is an aligned 32-bit unsigned int, ensuring atomic single-copy word reads and writes on supported architectures.
is_user_task(current) checks only PF_KTHREAD (bit 21, 0x00200000) and PF_USER_WORKER (bit 14, 0x00004000), which are immutable flags configured when a thread is created and never altered over its lifetime.
memalloc_flags_restore() and memalloc_restore_scope() only touch dynamic allocation flags (PF_MEMALLOC, PF_MEMALLOC_NOFS, PF_MEMALLOC_NOIO, PF_MEMALLOC_PIN), which occupy entirely disjoint bit offsets.
- Whether
is_user_task() observes current->flags before or after memalloc_flags_restore(), the expression !(task->flags & (PF_KTHREAD | PF_USER_WORKER)) yields the exact same boolean result, and the read itself cannot corrupt the interrupted task's state.