Technical Analysis of Root Cause
1. Raced Object and Memory Location
The memory access reported by KCSAN is to 0xffff88810280a2ac (4 bytes), which corresponds to the flags field (unsigned int flags, at byte offset 0x2c / 352 bits) of struct task_struct for the current task (PID: 3515).
2. Call Paths and Contexts Involved
-
Task Context (read-write on CPU 1):
During a mount operation, v2_read_file_info() (fs/quota/quota_v2.c:191) restores filesystem memory allocation flags by calling memalloc_nofs_restore(memalloc):
// include/linux/sched/mm.h:401
static inline void memalloc_nofs_restore(unsigned int flags)
{
memalloc_flags_restore(flags);
}
// include/linux/sched/mm.h:339-342
static inline void memalloc_flags_restore(unsigned flags)
{
current->flags &= ~flags;
}
memalloc_flags_restore() performs an unmarked C read-modify-write on current->flags to clear dynamic allocation flags (in this case, PF_MEMALLOC_NOFS). Note that v2_read_file_info() is merely an incidental caller; any task-context caller of memalloc_flags_{save,restore}() or other plain current->flags mutators can encounter this race.
-
Interrupt Context (read on CPU 1):
With CONFIG_KCSAN_INTERRUPT_WATCHER enabled, kcsan_setup_watchpoint() leaves local IRQs enabled while inserting a delay on the watchpoint for ¤t->flags. During this window, an APIC timer interrupt occurs on the same CPU:
sysvec_apic_timer_interrupt $\to$ hrtimer_interrupt $\to$ perf_swevent_hrtimer $\to$ perf_prepare_sample $\to$ perf_callchain (kernel/events/core.c:8742).
In perf_callchain(), the helper is_user_task(current) (include/linux/sched.h:1874) is executed to determine if user space callchains should be recorded:
// include/linux/sched.h:1872-1875
static __always_inline bool is_user_task(struct task_struct *task)
{
return task->mm && !(task->flags & (PF_KTHREAD | PF_USER_WORKER));
}
This performs an unmarked plain load of task->flags (current->flags).
3. Root Cause Mechanism
The task->flags field contains per-task flags that are modified synchronously by current itself. However, interrupt and NMI handlers running on the same CPU (such as perf sampling current) can asynchronously inspect current->flags.
While some callers that inspect current->flags from asynchronous contexts use marked access (for example, current_gfp_context() in include/linux/sched/mm.h:250 explicitly uses READ_ONCE(current->flags)), is_user_task() uses a plain, unmarked load:
task->flags & (PF_KTHREAD | PF_USER_WORKER)
Simultaneously, memalloc_flags_{save,restore}() mutate current->flags via plain read-modify-write operations (current->flags |= flags; and current->flags &= ~flags;) without READ_ONCE() or WRITE_ONCE().
When an interrupt arrives while the task is modifying current->flags, KCSAN detects the concurrent plain load against the active watchpoint on current->flags.
To eliminate the KCSAN data-race report and prevent any theoretical compiler load/store tearing, is_user_task() should use READ_ONCE(task->flags) (or data_race()), and memalloc_flags_{save,restore}() should access current->flags using marked operations (READ_ONCE()/WRITE_ONCE()).