Root Cause Analysis: KCSAN Data Race in kthread_is_per_cpu / ns_capable
1. Raced Memory Location and Struct Fields
The data race reported by KCSAN occurs at memory address 0xffff8881045f802c, which corresponds to struct task_struct::flags (offset 0x2c, a 4-byte / 32-bit unsigned int field defined in include/linux/sched.h) of task 3297 (syz-executor), running on CPU 0.
2. Call Paths and Concurrency Breakdown
Write Side (CPU 0 / Task 3297):
Task 3297 is actively running on CPU 0 executing a system call (getsockopt -> do_ipt_get_ctl -> ns_capable -> ns_capable_common()):
/* kernel/capability.c:341-346 */
capable = security_capable(current_cred(), ns, cap, opts);
if (capable == 0) {
current->flags |= PF_SUPERPRIV;
return true;
}
At line 343, ns_capable_common() performs a plain read-modify-write operation setting PF_SUPERPRIV (0x00000100, bit 8) on current->flags. As a task modifying its own process flags, it does not acquire the runqueue lock (rq->lock).
Read Side (CPU 1 / Task 4076):
Concurrently, task 4076 is on CPU 1 entering the scheduler idle/balance path via sched_balance_newidle() -> sched_balance_rq().
- CPU 1 locks CPU 0's runqueue (
rq_lock_irqsave(busiest, &rf)).
- It invokes
detach_tasks(&env), which walks CPU 0's cfs_tasks list (struct list_head *tasks = &env->src_rq->cfs_tasks;). The list still includes task 3297 (CPU 0's currently running CFS task).
detach_tasks() calls can_migrate_task(p, env) (kernel/sched/fair.c:11172).
- At line 11172,
can_migrate_task() evaluates:
/* Disregard percpu kthreads; they are where they need to be. */
if (kthread_is_per_cpu(p))
return 0;
Note: This check runs before line 11213 where task_on_cpu(env->src_rq, p) is tested to skip actively executing tasks.
kthread_is_per_cpu(p) (kernel/kthread.c:655) invokes tsk_is_kthread(p) (include/linux/kthread.h:21-26):
static inline struct kthread *tsk_is_kthread(struct task_struct *p)
{
if (p->flags & PF_KTHREAD)
return p->worker_private;
return NULL;
}
At line 23, tsk_is_kthread() performs a plain, unmarked read of p->flags to test PF_KTHREAD (0x00200000, bit 21).
3. Root Cause Mechanism
Because can_migrate_task() checks kthread_is_per_cpu(p) before testing whether p is currently running on the source CPU (task_on_cpu(env->src_rq, p)), and because current->flags updates do not acquire rq->lock, CPU 1 performs an unsynchronized plain read of p->flags while CPU 0 performs an unsynchronized plain write (|= PF_SUPERPRIV) to the same 32-bit word. Neither side uses concurrency-marking primitives (such as READ_ONCE() / WRITE_ONCE() or data_race()), triggering KCSAN.