KCSAN: data-race in d_alloc / lockref_get_not_dead

Status: moderation: reported on 2026/11/17 19:16
Subsystems: fs
Labels: prio:low
[Documentation on labels]
Reported-by: syzbot+42724889ed6165a3d9bd@syzkaller.appspotmail.com
First crash: 14h14m, last: 1h23m
✨ AI cause bisection: candidate commits (most relevant first) (AI job):
  bc08b449ee14 lockref: implement lockless reference count updates using cmpxchg()
  e7d33bb5ea82 lockref: add ability to mark lockrefs "dead"
  
✨ AI-generated root cause summary:

When USE_CMPXCHG_LOCKREF is enabled, struct lockref overlays an 8-byte aligned_u64 lock_count with a 4-byte spinlock_t lock and a 4-byte int count. In lockref_get_not_dead() (called during unlazy path walk by __legitimize_path()), CMPXCHG_LOOP performs a marked 8-byte READ_ONCE(lockref->lock_count) without holding the lock. Concurrently, d_alloc() increments parent->d_lockref.count via dget_dlock(), which performs an unmarked plain 4-byte read-modify-write (dentry->d_lockref.count++) under parent->d_lock. Because the 4-byte write is unmarked, KCSAN detects a data race against the concurrent 8-byte READ_ONCE(). This is an annotation-only race that is benign at runtime because the 64-bit cmpxchg loop checks lock state and atomically detects any concurrently acquired lock or modified count.

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

Technical Root Cause Analysis

1. Involved Entities and Files

  • Files:
  • Structs and Fields:
    • struct lockref:
      struct lockref {
          union {
      #if USE_CMPXCHG_LOCKREF
              aligned_u64 lock_count;
      #endif
              struct {
                  spinlock_t lock;
                  int count;
              };
          };
      };
      
      On 64-bit architectures (such as x86_64) where USE_CMPXCHG_LOCKREF is enabled, lock_count is an 8-byte field aliased to spinlock_t lock (bytes 0..3) and int count (bytes 4..7).
    • struct dentry::d_lockref: The embedded struct lockref whose reference count and spinlock control dentry lifetime and serialization.

2. Triggering Sequence and Data Race Mechanism

The KCSAN report records a concurrent access to the parent dentry's d_lockref:

  1. Reader Path (lockref_get_not_dead on CPU 0):

    • Task 3285 performs an unlazy transition during RCU path walk (lookup_fast $\to$ try_to_unlazy $\to$ legitimize_path $\to$ __legitimize_path at fs/namei.c:880).
    • __legitimize_path() calls lockref_get_not_dead(&path->dentry->d_lockref) (lib/lockref.c:147).
    • Inside CMPXCHG_LOOP (lib/lockref.c:15):
      old.lock_count = READ_ONCE(lockref->lock_count);
      
      CPU 0 executes a marked 8-byte read from parent->d_lockref (offset +0, covering both lock and count) without holding parent->d_lock.
  2. Writer Path (d_alloc on CPU 1):

    • Task 3283 is in filename_unlinkat $\to$ lookup_one_qstr_excl $\to$ d_alloc(base, name) (fs/dcache.c:2056–2063).
    • d_alloc() acquires spin_lock(&parent->d_lock) (which locks parent->d_lockref.lock at offset +0).
    • It then invokes dget_dlock(parent) (include/linux/dcache.h:344), which executes:
      dentry->d_lockref.count++;
      
      This is an unmarked plain C 4-byte read-modify-write at offset +4 of d_lockref.
  3. KCSAN Detection:

    • Under the Linux Kernel Memory Model (LKMM), any plain C read or write racing concurrently with another access (even a marked READ_ONCE() or atomic access) constitutes a data race. Because count++ in dget_dlock() is not annotated (e.g., via data_race() or atomic access macros), KCSAN flags the concurrent unmarked 4-byte write against the 8-byte READ_ONCE().

3. Correctness in Real-World Execution

This data race is benign and does not lead to reference count corruption, lost updates, or memory safety bugs:

  • Writer mutual exclusion: CPU 1 holds parent->d_lock across dget_dlock(). No other writer can modify count concurrently.
  • Lock-state validation in CMPXCHG_LOOP: CMPXCHG_LOOP checks arch_spin_value_unlocked(old.lock.rlock.raw_lock) prior to inspecting old.count:
    • If CPU 0's 64-bit READ_ONCE samples lock_count while CPU 1 holds d_lock, old.lock is observed as locked. The while-loop immediately terminates without attempting cmpxchg, and CPU 0 falls back to taking the lock (spin_lock(&lockref->lock) at lib/lockref.c:155).
    • If CPU 0's READ_ONCE occurs before CPU 1 takes the lock, old contains {unlocked, old_count}. If CPU 1 then takes the lock or finishes the increment to old_count + 1, CPU 0's subsequent 64-bit try_cmpxchg64_relaxed() will compare the entire 8-byte word against old.lock_count and fail (because either lock is now locked or count has changed).
    • If CPU 0's READ_ONCE occurs after CPU 1 unlocks parent->d_lock, acquire/release semantics guarantee that CPU 0 observes the updated count {unlocked, old_count + 1}.
▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
b8d7105c assessment-kcsan 🏃 KCSAN: data-race in d_alloc / lockref_get_not_dead 2026/11/17 19:06 2026/11/17 19:06 ca622473
344332d2 bisect-cause ❓ KCSAN: data-race in d_alloc / lockref_get_not_dead 2026/11/17 19:01 2026/11/17 19:01 2026/11/17 19:28 ca622473
2f241fe4 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in d_alloc / lockref_get_not_dead 2026/11/17 19:01 2026/11/17 19:01 2026/11/17 19:16 ca622473

Sample crash report:
==================================================================
BUG: KCSAN: data-race in d_alloc / lockref_get_not_dead

read (marked) to 0xffff888105c328c0 of 8 bytes by task 3285 on cpu 0:
 lockref_get_not_dead+0x2f/0x1d0 lib/lockref.c:147
 __legitimize_path fs/namei.c:880 [inline]
 legitimize_path fs/namei.c:890 [inline]
 try_to_unlazy+0x1f1/0x580 fs/namei.c:954
 lookup_fast+0x148/0x320 fs/namei.c:1862
 lookup_fast_for_open fs/namei.c:4727 [inline]
 open_last_lookups fs/namei.c:4757 [inline]
 path_openat+0x363/0x11e0 fs/namei.c:5013
 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

read-write to 0xffff888105c328c4 of 4 bytes by task 3283 on cpu 1:
 dget_dlock include/linux/dcache.h:344 [inline]
 d_alloc+0x5a/0x100 fs/dcache.c:2061
 lookup_one_qstr_excl+0x95/0x240 fs/namei.c:1812
 __start_dirop fs/namei.c:2926 [inline]
 start_dirop fs/namei.c:2948 [inline]
 filename_unlinkat+0x146/0x400 fs/namei.c:5718
 __do_sys_unlink fs/namei.c:5772 [inline]
 __se_sys_unlink+0x2b/0xe0 fs/namei.c:5769
 __x64_sys_unlink+0x1f/0x30 fs/namei.c:5769
 x64_sys_call+0x18ee/0x2550 arch/x86/include/generated/asm/syscalls_64.h:88
 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: 3283 Comm: udevd Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

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