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 race:benign
[Documentation on labels]
Reported-by: syzbot+9447d5b55e93049fc6fa@syzkaller.appspotmail.com
First crash: 6d14h, last: 3d18h
✨ 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 Benign: ✅ ❓ KCSAN: data-race in kthread_is_per_cpu / loop_process_work 2026/11/18 03:22 2026/11/18 03:22 2026/11/18 04:13 ca622473

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

read to 0xffff8881017bd66c of 4 bytes by interrupt on cpu 1:
 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_domains+0x1ef/0x2b0 kernel/sched/fair.c:14296
 handle_softirqs+0x109/0x310 kernel/softirq.c:669
 __do_softirq kernel/softirq.c:703 [inline]
 invoke_softirq kernel/softirq.c:542 [inline]
 __irq_exit_rcu+0x40/0xf0 kernel/softirq.c:786
 instr_sysvec_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1062 [inline]
 sysvec_apic_timer_interrupt+0x74/0x80 arch/x86/kernel/apic/apic.c:1062
 asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:674
 context_switch kernel/sched/core.c:5587 [inline]
 __schedule+0x796/0xd30 kernel/sched/core.c:7386
 preempt_schedule_irq+0x2c/0x50 kernel/sched/core.c:7666
 irqentry_exit_cond_resched kernel/entry/common.c:145 [inline]
 irqentry_exit_to_kernel_mode_preempt+0x63/0x80 include/linux/irq-entry-common.h:453
 irqentry_exit_to_kernel_mode include/linux/irq-entry-common.h:543 [inline]
 irqentry_exit+0x99/0x640 kernel/entry/common.c:154
 sysvec_apic_timer_interrupt+0x44/0x80 arch/x86/kernel/apic/apic.c:1062
 asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:674
 pud_none_or_clear_bad include/linux/pgtable.h:1590 [inline]
 free_pud_range mm/memory.c:231 [inline]
 free_p4d_range mm/memory.c:267 [inline]
 free_pgd_range+0x47f/0xe00 mm/memory.c:356
 free_pgtables+0x650/0x6a0 mm/memory.c:419
 unmap_region+0x179/0x1b0 mm/vma.c:515
 vms_clear_ptes mm/vma.c:1410 [inline]
 vms_complete_munmap_vmas+0x2f3/0x610 mm/vma.c:1452
 do_vmi_align_munmap+0x48d/0x4e0 mm/vma.c:1722
 do_vmi_munmap+0x1fe/0x240 mm/vma.c:1771
 __vm_munmap+0x192/0x2c0 mm/vma.c:3628
 __do_sys_munmap mm/mmap.c:1094 [inline]
 __se_sys_munmap mm/mmap.c:1091 [inline]
 __x64_sys_munmap+0x36/0x50 mm/mmap.c:1091
 x64_sys_call+0x23df/0x2550 arch/x86/include/generated/asm/syscalls_64.h:12
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

read-write to 0xffff8881017bd66c of 4 bytes by task 81 on cpu 0:
 loop_process_work+0x55/0xb00 drivers/block/loop.c:1874
 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: 0 UID: 0 PID: 81 Comm: kworker/u8:5 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Workqueue: loop7 loop_workfn
==================================================================

Crashes (3):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/19 22:41 upstream 8dac6df92a2e 229865fb .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 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.