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: 9h43m
✨ 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
Based on kernel commit 666539eecdf4:

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

  1. Loop worker execution on CPU 1:

    • In drivers/block/loop.c:loop_process_work(), loop request processing runs on shared workqueue threads (WQ_UNBOUND).
    • At entry, it saves int orig_flags = current->flags; and sets current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;.
    • After processing requests, at line 1899, it restores the flags using a plain, non-atomic full-word store:
      current->flags = orig_flags;
      
    • These accesses are plain, unsynchronized writes to current->flags without any scheduler locks held (following the kernel rule that tsk->flags is typically modified by current).
  2. 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

  1. 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).

  2. 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)).

▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
fbb5cc60 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in kthread_is_per_cpu / loop_process_work 2026/11/18 03:22 2026/11/18 03:22 2026/11/18 03:34 ca622473
addf8f20 assessment-kcsan 🏃 KCSAN: data-race in kthread_is_per_cpu / loop_process_work 2026/11/18 03:22 2026/11/18 03:22 ca622473

Sample crash report:
==================================================================
BUG: KCSAN: data-race in kthread_is_per_cpu / loop_process_work

read to 0xffff8881003c452c of 4 bytes by task 33 on cpu 0:
 tsk_is_kthread include/linux/kthread.h:23 [inline]
 kthread_is_per_cpu+0x17/0xa0 kernel/kthread.c:655
 can_migrate_task+0x45/0x240 kernel/sched/fair.c:11172
 detach_tasks kernel/sched/fair.c:11364 [inline]
 sched_balance_rq+0x18d1/0x2470 kernel/sched/fair.c:13832
 sched_balance_newidle kernel/sched/fair.c:14960 [inline]
 pick_task_fair+0x2d7/0x4e0 kernel/sched/fair.c:10308
 __pick_next_task kernel/sched/core.c:6224 [inline]
 pick_next_task kernel/sched/core.c:6773 [inline]
 __schedule+0x36e/0xd30 kernel/sched/core.c:7298
 __schedule_loop kernel/sched/core.c:7463 [inline]
 schedule+0x5b/0xa0 kernel/sched/core.c:7478
 worker_thread+0x660/0x770 kernel/workqueue.c:3674
 kthread+0x248/0x2a0 kernel/kthread.c:436
 ret_from_fork+0x146/0x330 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245

write to 0xffff8881003c452c of 4 bytes by task 53 on cpu 1:
 loop_process_work+0xac8/0xb00 drivers/block/loop.c:1899
 loop_workfn+0x31/0x40 drivers/block/loop.c:1906
 process_one_work kernel/workqueue.c:3495 [inline]
 process_scheduled_works+0x582/0xab0 kernel/workqueue.c:3578
 worker_thread+0x56c/0x770 kernel/workqueue.c:3659
 kthread+0x248/0x2a0 kernel/kthread.c:436
 ret_from_fork+0x146/0x330 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 53 Comm: kworker/u8:3 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Workqueue: loop2 loop_workfn
==================================================================
EXT4-fs error (device loop2): ext4_map_blocks:862: inode #3: block 2: comm kworker/u8:3: lblock 2 mapped to illegal pblock 2 (length 1)
EXT4-fs error (device loop2): ext4_write_dquot:7100: comm kworker/u8:3: Failed to commit dquot type 0
EXT4-fs error (device loop2): ext4_map_blocks:862: inode #3: block 1: comm kworker/u8:3: lblock 1 mapped to illegal pblock 1 (length 1)
EXT4-fs error (device loop2): ext4_release_dquot:7156: comm kworker/u8:3: Failed to release dquot type 0

Crashes (2):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 18:13 upstream 666539eecdf4 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in kthread_is_per_cpu / loop_process_work
2026/11/17 02:53 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in kthread_is_per_cpu / loop_process_work
* Struck through repros no longer work on HEAD.