KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead

Status: moderation: reported on 2026/11/19 00:02
Subsystems: fs
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+b004c255a1761c327ca8@syzkaller.appspotmail.com
First crash: 1d22h, last: 17h48m
✨ AI-generated root cause summary:

A KCSAN data race occurs between task 3865 calling lockref_get_not_dead() (via RCU path unlazying in __legitimize_path()) and task 3867 calling dget_dlock() in __d_alloc_parallel(). In lockref_get_not_dead(), CMPXCHG_LOOP performs a marked 8-byte read of lockref->lock_count (READ_ONCE) without holding d_lock, while dget_dlock() concurrently performs an unmarked 4-byte read-modify-write (dentry->d_lockref.count++) under parent->d_lock. Because struct lockref overlays lock_count and the (lock, count) pair in a union, this concurrent unmarked write constitutes an LKMM data race, though the spinlock state check and cmpxchg ensure it is benign at runtime.

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

Root Cause Analysis

1. Data Structure Layout and Invariant

In include/linux/lockref.h, struct lockref is defined as a union when USE_CMPXCHG_LOCKREF is enabled (true on 64-bit SMP systems where SPINLOCK_SIZE <= 4):

struct lockref {
	union {
#if USE_CMPXCHG_LOCKREF
		aligned_u64 lock_count;
#endif
		struct {
			spinlock_t lock;
			int count;
		};
	};
};

On little-endian 64-bit architectures (such as x86_64), the 8-byte lock_count occupies the exact same memory as the 4-byte lock (bytes 0–3) and the 4-byte count (bytes 4–7).

The design invariant of lockref allows lockless optimistic operations (via CMPXCHG_LOOP in lib/lockref.c) to read the full 64-bit lock_count using READ_ONCE, verify whether the spinlock is unlocked (arch_spin_value_unlocked()), compute updated count values, and commit them atomically with try_cmpxchg64_relaxed().

2. Conflicting Memory Accesses

The reported KCSAN bug report highlights a data race on struct lockref between two paths accessing the same dentry:

  1. Marked 8-byte read (lockref_get_not_dead in lib/lockref.c:147):

    • In fs/namei.c, during RCU path walk resolution (complete_walk -> try_to_unlazy -> legitimize_path -> __legitimize_path), task 3865 attempts to obtain a reference on the target dentry using lockref_get_not_dead(&path->dentry->d_lockref).
    • lockref_get_not_dead() executes the CMPXCHG_LOOP macro (lib/lockref.c:15):
      old.lock_count = READ_ONCE(lockref->lock_count);
      
    • This performs a marked 8-byte load from 0xffff8881054e4200 without holding the dentry's d_lock.
  2. Unmarked 4-byte read-modify-write (dget_dlock in include/linux/dcache.h:344):

    • Concurrently, task 3867 is creating a child dentry under this parent dentry via __d_alloc_parallel() (fs/dcache.c:2909–2914).
    • Task 3867 acquires spin_lock(&parent->d_lock) (which is parent->d_lockref.lock) and increments the parent's refcount via new->d_parent = dget_dlock(parent).
    • dget_dlock() performs a plain C increment:
      static inline struct dentry *dget_dlock(struct dentry *dentry)
      {
          dentry->d_lockref.count++;
          return dentry;
      }
      
    • This writes to the 4-byte count field at 0xffff8881054e4204 using an unmarked read-modify-write access.

3. Why KCSAN Flags It vs. Real-World Correctness

  • According to LKMM / KCSAN rules: Because dentry->d_lockref.count++ is an unmarked plain write to memory that overlaps with the 8-byte READ_ONCE(lockref->lock_count), it constitutes an unmarked data race under the C memory model.
  • According to the lockref algorithmic design:
    1. dget_dlock(parent) runs strictly inside a critical section bounded by spin_lock(&parent->d_lock) and spin_unlock(&parent->d_lock).
    2. If lockref_get_not_dead() reads lock_count while d_lock is held by __d_alloc_parallel(), arch_spin_value_unlocked(old.lock.rlock.raw_lock) is false. CMPXCHG_LOOP immediately terminates without using old.count and falls back to spin_lock(&lockref->lock). It then blocks until parent->d_lock is unlocked and safely performs the increment under the lock.
    3. If lockref_get_not_dead() reads lock_count right before the spinlock is locked or right as it is being unlocked, the 64-bit try_cmpxchg64_relaxed() detects the discrepancy against old.lock_count, fails, reloads the current lock_count, and either retries or falls back to spin_lock.

In practice, the race is benign algorithmically, but KCSAN flags it because the write in dget_dlock() (and similarly in lib/lockref.c lock-held fallback paths) is unmarked.

▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
cbd6a6e0 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead 2026/11/18 22:31 2026/11/18 22:31 2026/11/18 22:38 2006bdcc
2b6ddf35 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead 2026/11/18 22:25 2026/11/18 22:25 2026/11/18 23:00 2006bdcc

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

read (marked) to 0xffff8881054e4200 of 8 bytes by task 3865 on cpu 1:
 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
 complete_walk+0xc5/0x200 fs/namei.c:1070
 path_lookupat+0x373/0x4c0 fs/namei.c:2826
 filename_lookup+0x18f/0x390 fs/namei.c:2848
 vfs_statx+0x96/0x170 fs/stat.c:357
 vfs_fstatat+0x118/0x170 fs/stat.c:377
 __do_sys_newfstatat fs/stat.c:542 [inline]
 __se_sys_newfstatat+0x55/0x3e0 fs/stat.c:536
 __x64_sys_newfstatat+0x55/0x70 fs/stat.c:536
 x64_sys_call+0xee6/0x2550 arch/x86/include/generated/asm/syscalls_64.h:263
 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 0xffff8881054e4204 of 4 bytes by task 3867 on cpu 0:
 dget_dlock include/linux/dcache.h:344 [inline]
 __d_alloc_parallel+0xbb/0xdb0 fs/dcache.c:2910
 d_alloc_parallel+0x1f/0x30 fs/dcache.c:3035
 lookup_open+0x221/0x10a0 fs/namei.c:4479
 open_last_lookups fs/namei.c:4774 [inline]
 path_openat+0x492/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

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

Crashes (5):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/18 06:59 upstream 20eef29b7c84 eb4895d7 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead
2026/11/17 21:28 upstream 63fdb511c27a 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead
2026/11/17 17:09 upstream 666539eecdf4 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead
2026/11/17 10:16 upstream 666539eecdf4 43af6cac .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead
2026/11/17 02:00 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __d_alloc_parallel / lockref_get_not_dead
* Struck through repros no longer work on HEAD.