syzbot
KCSAN: data-race in kthread_is_per_cpu / loop_process_work
Status:
moderation: reported on 2026/11/18 03:35
Subsystems:
kernel
Labels:
prio:low
[Documentation on labels]
Reported-by: syzbot+9447d5b55e93049fc6fa@syzkaller.appspotmail.com
First crash: 1d01h, last: 9h48m
✨ AI-generated root cause summary:
A KCSAN data race occurs on task_struct::flags between an unbound workqueue worker running loop_process_work() on one CPU and another CPU performing newidle load balancing in sched_balance_rq(). In drivers/block/loop.c, loop_process_work() performs plain unmarked writes to current->flags (setting PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO and later restoring current->flags = orig_flags). Concurrently, the balancing CPU inspects the busy CPU's task list in detach_tasks() and calls can_migrate_task(p), which invokes kthread_is_per_cpu(p) -> tsk_is_kthread(p) (include/linux/kthread.h) to perform an unmarked plain read of p->flags & PF_KTHREAD on the actively running remote task before task_on_cpu() is checked.
▶
▼
✨ Full AI root cause analysis
Root Cause Analysis
1. Functions, Structs, and Fields Involved
struct task_struct::flags (include/linux/sched.h:1800): 32-bit unsigned int representing per-task execution flags (at offset 0x2c / bits 352-383), containing flags such as PF_KTHREAD (0x00200000), PF_LOCAL_THROTTLE (0x00100000), and PF_MEMALLOC_NOIO (0x00080000).
drivers/block/loop.c:loop_process_work(): Work item handler running on the worker thread (kworker/u8:3, task 53 on CPU 1).
include/linux/kthread.h:tsk_is_kthread(): Inline function checking if a task is a kernel thread by testing p->flags & PF_KTHREAD.
kernel/kthread.c:kthread_is_per_cpu(): Checks whether a task is a per-CPU kernel thread via tsk_is_kthread(p).
kernel/sched/fair.c:can_migrate_task() / detach_tasks(): Part of CFS load balancing (sched_balance_rq() / sched_balance_newidle()) executed on an idle CPU (task 33 on CPU 0).
2. Triggering Mechanism and Race Lifecycle
-
Loop worker execution on CPU 1:
-
Concurrent load balancing on CPU 0:
- Meanwhile, CPU 0 enters idle scheduling and triggers newidle load balancing:
sched_balance_newidle() $\to$ sched_balance_rq() $\to$ detach_tasks().
- CPU 0 acquires CPU 1's runqueue lock (
rq_lock(busiest)) and iterates over tasks in busiest->cfs_tasks via detach_tasks().
- Because task 53 is on CPU 1's CFS task list, CPU 0 evaluates it by calling
can_migrate_task(p, env).
- At line 11172 of
kernel/sched/fair.c, before checking whether the task is actively executing (task_on_cpu(env->src_rq, p) is evaluated later at line 11213), can_migrate_task() tests whether p is a pinned per-CPU kthread:
/* Disregard percpu kthreads; they are where they need to be. */
if (kthread_is_per_cpu(p))
return 0;
kthread_is_per_cpu(p) calls tsk_is_kthread(p) in include/linux/kthread.h:23:
static inline struct kthread *tsk_is_kthread(struct task_struct *p)
{
if (p->flags & PF_KTHREAD)
return p->worker_private;
return NULL;
}
- This performs an unmarked, plain read of
p->flags of a remote, running task while task 53 is simultaneously writing to current->flags in loop_process_work().
3. Concurrency Invariants and Flaws
-
Unmarked cross-CPU access in tsk_is_kthread():
While tsk->flags is defined as per-task flags primarily updated by current, kernel scheduler routines inspect tsk->flags of other tasks on remote runqueues. Because tsk_is_kthread() does not use READ_ONCE(p->flags) (contrast with to_kthread() in kernel/kthread.c:84, which explicitly uses READ_ONCE(k->flags)), KCSAN flags this unsynchronized concurrent read and write as a data race under the Linux Kernel Memory Model (LKMM).
-
Full-word overwrite in loop_process_work():
loop_process_work() directly overwrites the entire current->flags word (current->flags = orig_flags;) rather than using scoped flag-saving helpers (e.g., memalloc_noio_save() / memalloc_noio_restore()) or targeted flag restoration (current_restore_flags(orig_flags, PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO)).
Sample crash report:
* Struck through repros no longer work on HEAD.