KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch (2)

Status: moderation: reported on 2026/11/19 00:05
Subsystems: fs
Labels: prio:high race:benign
[Documentation on labels]
Reported-by: syzbot+9822bf047405e317be17@syzkaller.appspotmail.com
First crash: 1d10h, last: 15h46m
✨ AI-generated root cause summary:

A KCSAN data race occurs between a lockless read in percpu_counter_read_positive() and an in-lock update in percpu_counter_add_batch(). In alloc_empty_file(), get_nr_files() calls percpu_counter_read_positive() to read fbc->count locklessly via READ_ONCE(fbc->count), while a concurrent alloc_empty_file() updates fbc->count via plain arithmetic under raw_spin_lock in percpu_counter_add_batch(). Because the read lacks a data_race() annotation to signal that a concurrent plain write under lock is intentional, KCSAN detects the concurrent unmarked write and flags a data race.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 20eef29b7c84:

Root Cause Analysis

1. Functions, Structs, and Fields Involved


2. Triggering Mechanism and Race Lifecycle

  1. Reader Path:

    • A task allocates a file (in the report, task 3294 inside __sys_socket() calling sock_alloc_file() $\to$ alloc_empty_file()).
    • In fs/file_table.c:252, alloc_empty_file() calls get_nr_files(), which invokes percpu_counter_read_positive(&nr_files).
    • percpu_counter_read_positive() executes:
      s64 ret = READ_ONCE(fbc->count);
      
      This read intentionally runs locklessly without acquiring fbc->lock, serving as an approximate, optimistic check against files_stat.max_files.
  2. Writer Path:

    • Concurrently, another task creates or opens a file (task 3463 inside do_sys_openat2() $\to$ alloc_empty_file()).
    • After initializing the file object, alloc_empty_file() calls percpu_counter_inc(&nr_files) at fs/file_table.c:272.
    • In percpu_counter_add_batch() (lib/percpu_counter.c), when the accumulated per-CPU count exceeds the batch threshold, the function takes raw_spin_lock_irqsave(&fbc->lock, flags) and aggregates the local delta into fbc->count:
      fbc->count += count + amount;
      
      This update is an unmarked (plain) read-modify-write performed while holding fbc->lock.
  3. Inconsistency / KCSAN Report:

    • Under the Linux Kernel Memory Model and KCSAN rules, when an unmarked write (plain access) races with a marked read (READ_ONCE), KCSAN treats it as a data race because the writer is not marked with WRITE_ONCE() or data_race().
    • In percpu_counter, lock-protected updates intentionally remain plain accesses so that inadvertent lockless writes can still be detected. Therefore, intentional lockless readers (such as in __percpu_counter_limited_add()) are expected to be annotated using data_race(READ_ONCE(fbc->count)).
    • Because percpu_counter_read_positive() in include/linux/percpu_counter.h uses READ_ONCE(fbc->count) without data_race(...), KCSAN detects the concurrent unmarked write from percpu_counter_add_batch() and flags a data race.
▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
10a0e0e9 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch (2) 2026/11/18 16:07 2026/11/18 16:07 2026/11/18 16:47 2006bdcc
61672838 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ✅ NetworkTrigger: ✅ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch (2) 2026/11/18 16:02 2026/11/18 16:02 2026/11/18 16:31 2006bdcc
▶ ▼ Similar bugs (1)
Kernel Title Rank 🛈 Repro Cause bisect Fix bisect Count Last Reported Patched Status
upstream KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch fs 6 59 2410d 2453d 0/29 auto-closed as invalid on 2020/06/18 13:11

Sample crash report:
==================================================================
BUG: KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch

read (marked) to 0xffffffff86e09788 of 8 bytes by task 3294 on cpu 0:
 percpu_counter_read_positive include/linux/percpu_counter.h:121 [inline]
 get_nr_files fs/file_table.c:113 [inline]
 alloc_empty_file+0x20/0x1e0 fs/file_table.c:252
 alloc_file fs/file_table.c:396 [inline]
 alloc_file_pseudo+0x92/0x120 fs/file_table.c:427
 sock_alloc_file+0x9c/0x1e0 net/socket.c:557
 sock_map_fd net/socket.c:587 [inline]
 __sys_socket+0x118/0x180 net/socket.c:1830
 __do_sys_socket net/socket.c:1835 [inline]
 __se_sys_socket net/socket.c:1833 [inline]
 __x64_sys_socket+0x3f/0x50 net/socket.c:1833
 x64_sys_call+0x246e/0x2550 arch/x86/include/generated/asm/syscalls_64.h:42
 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 0xffffffff86e09788 of 8 bytes by task 3463 on cpu 1:
 percpu_counter_add_batch+0x101/0x120 lib/percpu_counter.c:107
 percpu_counter_add include/linux/percpu_counter.h:71 [inline]
 percpu_counter_inc include/linux/percpu_counter.h:267 [inline]
 alloc_empty_file+0xbc/0x1e0 fs/file_table.c:272
 path_openat+0x50/0x11e0 fs/namei.c:5002
 do_file_open+0x16c/0x290 fs/namei.c:5045
 do_sys_openat2+0xa1/0x140 fs/open.c:1443
 do_sys_open fs/open.c:1449 [inline]
 __do_sys_openat fs/open.c:1465 [inline]
 __se_sys_openat fs/open.c:1460 [inline]
 __x64_sys_openat+0xf2/0x120 fs/open.c:1460
 x64_sys_call+0x1f72/0x2550 arch/x86/include/generated/asm/syscalls_64.h:258
 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: 3463 Comm: modprobe Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (3):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/18 09:05 upstream 20eef29b7c84 eb4895d7 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch
2026/11/17 17:43 upstream 666539eecdf4 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch
2026/11/17 14:22 upstream 666539eecdf4 43af6cac .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in alloc_empty_file / percpu_counter_add_batch
* Struck through repros no longer work on HEAD.