Root Cause Analysis
KCSAN reported a 4-byte read/write data race on task_struct::flags (an unsigned int at offset 44 bytes / 352 bits in struct task_struct) occurring on the same CPU (cpu 0) between task context and hardirq context:
-
Task-context write (fs/exec.c:1914):
In do_execveat_common(), the executing task (current, PID 4267) clears PF_NPROC_EXCEEDED (0x00001000) with interrupts enabled using an unmarked read-modify-write:
current->flags &= ~PF_NPROC_EXCEEDED;
-
Interrupt-context read (kernel/signal.c:2027):
While do_execveat_common() is executing (and delayed by KCSAN's watchpoint in kcsan_setup_watchpoint()), an APIC timer interrupt fires on the same CPU and invokes posix_timer_fn() -> posix_timer_queue_signal() -> posixtimer_send_sigqueue() (kernel/signal.c:2056).
Before calling lock_task_sighand(), posixtimer_send_sigqueue() calls posixtimer_get_target() (kernel/signal.c:2022-2030), which reads current->flags to check PF_EXITING (0x00000004):
static inline struct task_struct *posixtimer_get_target(struct k_itimer *tmr)
{
struct task_struct *t = pid_task(tmr->it_pid, tmr->it_pid_type);
if (t && tmr->it_pid_type != PIDTYPE_PID &&
same_thread_group(t, current) && !(current->flags & PF_EXITING))
t = current;
return t;
}
Because current in the interrupt handler refers to the interrupted task (task 4267) on CPU 0, posixtimer_get_target() reads the exact same current->flags field concurrently with the unmarked modification in do_execveat_common().
Synchronisation / Invariant Violated
As noted in include/linux/sched.h:1838-1848, task_struct::flags is generally updated by current itself without a lock, while exit_signals() (kernel/signal.c:3210-3211) sets PF_EXITING under spinlock_irq(&tsk->sighand->siglock).
However, posixtimer_get_target() runs in hardirq context without holding sighand->siglock and reads current->flags via an unmarked access (!(current->flags & PF_EXITING)), while do_execveat_common() (and other task-context flag updates that do not disable interrupts) modifies current->flags via an unmarked read-modify-write (&= ~PF_NPROC_EXCEEDED). Under KCSAN's memory model, this constitutes an unmarked data race on current->flags. Using READ_ONCE(current->flags) in posixtimer_get_target() and WRITE_ONCE(current->flags, ...) (or appropriate atomic/accessor helpers) for concurrent task-context flag updates resolves the unmarked race.