KCSAN: data-race in memalloc_restore_scope / perf_callchain

Status: moderation: reported on 2026/11/18 00:31
Subsystems: perf
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+60f7653c8724d5142482@syzkaller.appspotmail.com
First crash: 13h37m, last: 13h37m
✨ AI-generated root cause summary:

A KCSAN data race occurs on current->flags (struct task_struct::flags) between memalloc_flags_restore() and is_user_task(). While a task updates scoped allocation flags using an unmarked read-modify-write operation (current->flags &= ~flags;) with interrupts enabled, an interrupt or scheduler event can occur on the same CPU, causing perf to invoke perf_callchain() and subsequently is_user_task(current). The latter reads current->flags without concurrency-safe markings (such as READ_ONCE() or data_race()), triggering KCSAN. In practice, the race is benign because is_user_task() inspects immutable bits (PF_KTHREAD and PF_USER_WORKER) that are completely disjoint from the memory-allocation scope bits modified by memalloc_flags_restore().

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 666539eecdf4:

Bug Analysis: Data Race on current->flags in memalloc_restore_scope / perf_callchain

1. Raced Object and Locations

The 4-byte data race reported by KCSAN at address 0xffff88810250c52c occurs on current->flags (unsigned int flags in struct task_struct, defined in include/linux/sched.h:858) between two code paths:

  1. Writer (RMW):

    • File: include/linux/sched/mm.h:341 (invoked via memalloc_restore_scope() at mm/vmalloc.c:3899).
    • Function: memalloc_flags_restore(unsigned int flags)
    • Operation: An unmarked read-modify-write operation:
      current->flags &= ~flags;
      
      This function is called with local interrupts enabled to clear scoped memory allocation flags (such as PF_MEMALLOC, PF_MEMALLOC_NOFS, or PF_MEMALLOC_NOIO).
  2. Reader:

    • File: include/linux/sched.h:1874 (inlined into perf_callchain() at kernel/events/core.c:8742).
    • Function: is_user_task(struct task_struct *task)
    • Operation: An unmarked read of task->flags:
      static __always_inline bool is_user_task(struct task_struct *task)
      {
          return task->mm && !(task->flags & (PF_KTHREAD | PF_USER_WORKER));
      }
      

2. Trigger Mechanism

The reported race occurs on the same CPU (CPU 1) and within the context of the same task (PID 4438):

  1. Task 4438 in kernel mode enters memalloc_restore_scope() -> memalloc_flags_restore() while finishing a percpu memory allocation (pcpu_alloc_noprof).
  2. When accessing current->flags, KCSAN instruments the store and sets up a watchpoint (kcsan_setup_watchpoint), stalling execution briefly in a delay loop (delay_access -> udelay).
  3. While the watchpoint on current->flags is active, a hardware timer interrupt (sysvec_apic_timer_interrupt) occurs on CPU 1, interrupting task 4438.
  4. On the interrupt exit path (irqentry_exit -> irqentry_exit_to_kernel_mode_preempt -> preempt_schedule_irq -> __schedule), hardirq context has ended, re-enabling KCSAN access tracking while still running under task 4438.
  5. In __schedule(), the sched:sched_switch tracepoint fires. A perf event attached to sched_switch samples the context switch via: perf_trace_sched_switch() $\to$ perf_tp_event() $\to$ perf_swevent_event() $\to$ perf_swevent_overflow() $\to$ __perf_event_overflow() $\to$ perf_prepare_sample() $\to$ perf_sample_save_callchain() $\to$ perf_callchain().
  6. Inside perf_callchain(), is_user_task(current) is called to determine whether user callchain sampling should be performed.
  7. is_user_task() executes an unmarked read on task->flags (current->flags). Because KCSAN's watchpoint for task 4438's store to current->flags is still active, KCSAN detects the concurrent read-write access without synchronization annotations and emits the data race splat.

This scenario is not limited to scheduler tracepoints; any interrupt, NMI, or PMU sampling event that interrupts a task while it updates current->flags and subsequently invokes perf routines calling is_user_task(current) can trigger this data race.


3. Root Cause and Concurrency Analysis

  • Missing Concurrency Annotations: Under the Linux Kernel Memory Model (LKMM), shared memory locations concurrently accessed across asynchronous or preemptive contexts must use marked access helpers (such as READ_ONCE() or data_race()). For instance, other readers of current->flags in the memory-management subsystem (such as current_gfp_context() in include/linux/sched/mm.h:250) use READ_ONCE(current->flags). However, is_user_task() reads task->flags directly without READ_ONCE() or data_race().
  • Benign Race Invariant:
    • task->flags is an aligned 32-bit unsigned int, ensuring atomic single-copy word reads and writes on supported architectures.
    • is_user_task(current) checks only PF_KTHREAD (bit 21, 0x00200000) and PF_USER_WORKER (bit 14, 0x00004000), which are immutable flags configured when a thread is created and never altered over its lifetime.
    • memalloc_flags_restore() and memalloc_restore_scope() only touch dynamic allocation flags (PF_MEMALLOC, PF_MEMALLOC_NOFS, PF_MEMALLOC_NOIO, PF_MEMALLOC_PIN), which occupy entirely disjoint bit offsets.
    • Whether is_user_task() observes current->flags before or after memalloc_flags_restore(), the expression !(task->flags & (PF_KTHREAD | PF_USER_WORKER)) yields the exact same boolean result, and the read itself cannot corrupt the interrupted task's state.
▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
2f6e4de4 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in memalloc_restore_scope / perf_callchain 2026/11/18 00:30 2026/11/18 00:30 2026/11/18 00:54 ca622473
f2b88635 bisect-cause 🏃 KCSAN: data-race in memalloc_restore_scope / perf_callchain 2026/11/18 00:30 2026/11/18 00:30 ca622473
2f112469 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in memalloc_restore_scope / perf_callchain 2026/11/18 00:21 2026/11/18 00:21 2026/11/18 00:30 ca622473

Sample crash report:
==================================================================
BUG: KCSAN: data-race in memalloc_restore_scope / perf_callchain

read to 0xffff88810250c52c of 4 bytes by task 4438 on cpu 1:
 is_user_task include/linux/sched.h:1874 [inline]
 perf_callchain+0xa2/0x270 kernel/events/core.c:8742
 perf_sample_save_callchain include/linux/perf_event.h:1385 [inline]
 perf_prepare_sample+0x1f5/0x1550 kernel/events/core.c:8829
 __perf_event_output kernel/events/core.c:9070 [inline]
 perf_event_output_forward+0x61/0x170 kernel/events/core.c:9091
 __perf_event_overflow+0x62f/0x950 kernel/events/core.c:10980
 perf_swevent_overflow kernel/events/core.c:11062 [inline]
 perf_swevent_event+0x40e/0x4f0 kernel/events/core.c:11123
 perf_tp_event+0x32f/0x1650 kernel/events/core.c:11635
 perf_trace_run_bpf_submit+0xac/0x110 kernel/events/core.c:11554
 do_perf_trace_sched_switch include/trace/events/sched.h:220 [inline]
 perf_trace_sched_switch+0x187/0x1d0 include/trace/events/sched.h:220
 __traceiter_sched_switch+0x3f/0x60 include/trace/events/sched.h:220
 __do_trace_sched_switch include/trace/events/sched.h:220 [inline]
 trace_sched_switch include/trace/events/sched.h:220 [inline]
 __schedule+0xc3d/0xd30 kernel/sched/core.c:7383
 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
 native_pause arch/x86/include/asm/vdso/processor.h:13 [inline]
 delay_tsc+0x43/0xa0 arch/x86/lib/delay.c:76
 udelay include/asm-generic/delay.h:64 [inline]
 delay_access kernel/kcsan/core.c:332 [inline]
 kcsan_setup_watchpoint+0x255/0x3f0 kernel/kcsan/core.c:622
 memalloc_flags_restore include/linux/sched/mm.h:341 [inline]
 memalloc_restore_scope+0x37/0x50 mm/vmalloc.c:3899
 __pcpu_map_pages mm/percpu-vm.c:215 [inline]
 pcpu_map_pages mm/percpu-vm.c:242 [inline]
 pcpu_populate_chunk+0x5ba/0xa30 mm/percpu-vm.c:309
 pcpu_alloc_noprof+0x81d/0xfc0 mm/percpu.c:1883
 bpf_map_alloc_percpu+0x62/0xa0 kernel/bpf/syscall.c:583
 prealloc_init+0x1a8/0x4d0 kernel/bpf/hashtab.c:434
 htab_map_alloc+0x58f/0x770 kernel/bpf/hashtab.c:757
 map_create_alloc kernel/bpf/syscall.c:1553 [inline]
 map_create+0xb14/0x1050 kernel/bpf/syscall.c:1663
 __sys_bpf+0x8e0/0xc50 kernel/bpf/syscall.c:6437
 __do_sys_bpf kernel/bpf/syscall.c:6582 [inline]
 __se_sys_bpf kernel/bpf/syscall.c:6579 [inline]
 __x64_sys_bpf+0x69/0x80 kernel/bpf/syscall.c:6579
 x64_sys_call+0xf26/0x2550 arch/x86/include/generated/asm/syscalls_64.h:322
 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 0xffff88810250c52c of 4 bytes by task 4438 on cpu 1:
 memalloc_flags_restore include/linux/sched/mm.h:341 [inline]
 memalloc_restore_scope+0x37/0x50 mm/vmalloc.c:3899
 __pcpu_map_pages mm/percpu-vm.c:215 [inline]
 pcpu_map_pages mm/percpu-vm.c:242 [inline]
 pcpu_populate_chunk+0x5ba/0xa30 mm/percpu-vm.c:309
 pcpu_alloc_noprof+0x81d/0xfc0 mm/percpu.c:1883
 bpf_map_alloc_percpu+0x62/0xa0 kernel/bpf/syscall.c:583
 prealloc_init+0x1a8/0x4d0 kernel/bpf/hashtab.c:434
 htab_map_alloc+0x58f/0x770 kernel/bpf/hashtab.c:757
 map_create_alloc kernel/bpf/syscall.c:1553 [inline]
 map_create+0xb14/0x1050 kernel/bpf/syscall.c:1663
 __sys_bpf+0x8e0/0xc50 kernel/bpf/syscall.c:6437
 __do_sys_bpf kernel/bpf/syscall.c:6582 [inline]
 __se_sys_bpf kernel/bpf/syscall.c:6579 [inline]
 __x64_sys_bpf+0x69/0x80 kernel/bpf/syscall.c:6579
 x64_sys_call+0xf26/0x2550 arch/x86/include/generated/asm/syscalls_64.h:322
 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

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 4438 Comm: syz.2.266 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 11:30 upstream 666539eecdf4 43af6cac .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in memalloc_restore_scope / perf_callchain
* Struck through repros no longer work on HEAD.