KCSAN: data-race in dentry_kill / lockref_put_return

Status: moderation: reported on 2026/11/18 02:44
Subsystems: fs
Labels: race:benign prio:low
[Documentation on labels]
Reported-by: syzbot+ef5941ce3fbbd4b5f7d8@syzkaller.appspotmail.com
First crash: 1d02h, last: 10h53m
✨ AI-generated root cause summary:

KCSAN reported a data race between an unmarked 4-byte decrement of a parent dentry's reference count (--parent->d_lockref.count) in dentry_kill() under parent->d_lock and an optimistic, lockless 8-byte READ_ONCE(lockref->lock_count) in CMPXCHG_LOOP called via fast_dput() -> lockref_put_return(). On 64-bit SMP systems with USE_CMPXCHG_LOCKREF, struct lockref unions the 64-bit lock_count with the 32-bit spinlock lock and 32-bit count. Because the write to count in dentry_kill() is a plain (unmarked) C read-modify-write without WRITE_ONCE(), KCSAN flags concurrent overlapping access with the lockless 8-byte marked read as a data race under C memory model rules. However, because parent->d_lock is held throughout the decrement and released with release semantics, the optimistic cmpxchg loop either observes the spinlock as locked (immediately failing and falling back to the locked path) or fails the 64-bit cmpxchg comparison, making this race functionally benign.

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

Technical Analysis of KCSAN Bug Report: Data Race in dentry_kill / lockref_put_return

1. Functions, Structs, and Files Involved

  • struct lockref (include/linux/lockref.h:25-35):
    struct lockref {
        union {
    #if USE_CMPXCHG_LOCKREF
            aligned_u64 lock_count;
    #endif
            struct {
                spinlock_t lock;
                int count;
            };
        };
    };
    
    On 64-bit SMP configurations where CONFIG_ARCH_USE_CMPXCHG_LOCKREF is enabled and SPINLOCK_SIZE <= 4, struct lockref is an 8-byte union combining the 64-bit aligned_u64 lock_count with two 32-bit fields: spinlock_t lock at offset +0 (bytes 0–3) and int count at offset +4 (bytes 4–7).
  • dentry_kill() (fs/dcache.c:830-882): Evicts a dead child dentry and drops the reference held by the child on its parent dentry (parent = dentry->d_parent).
  • lockref_put_return() (lib/lockref.c:90-101) & CMPXCHG_LOOP (lib/lockref.c:11-27): Performs an optimistic, lockless decrement on dentry->d_lockref using 64-bit cmpxchg.
  • fast_dput() (fs/dcache.c:960-1027) / dput() (fs/dcache.c:1066-1075): Top-level reference decrement routine for dentries.

2. Race Condition Mechanism and Triggering Sequence

The race is triggered when one task drops a reference to a dentry locklessly while another task kills one of that dentry's children and updates the parent's refcount under parent->d_lock:

  1. Task 3695 (CPU 0) – dentry_kill slow path (fs/dcache.c:868-878):

    • In the process of destroying a child dentry, Task 3695 locks the parent dentry:
      parent = dentry->d_parent;
      spin_lock(&parent->d_lock);
      
    • After detaching and freeing the child dentry, Task 3695 decrements parent's refcount at line 877:
      if (parent && --parent->d_lockref.count) {
          spin_unlock(&parent->d_lock);
          return NULL;
      }
      
    • This decrement (--parent->d_lockref.count) compiles into a normal 4-byte read-modify-write (decl / subl) at offset +4 (0xffff8881058a8744). This is a plain, unmarked access without WRITE_ONCE() or atomic intrinsics.
  2. Task 3692 (CPU 1) – fast_dput -> lockref_put_return (fs/dcache.c:970, lib/lockref.c:92):

    • Concurrently, Task 3692 drops its reference to the same parent dentry (e.g. from vfs_statx -> path_put -> dput).
    • fast_dput() invokes lockref_put_return(&dentry->d_lockref), which executes CMPXCHG_LOOP:
      old.lock_count = READ_ONCE(lockref->lock_count);
      while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) {
          struct lockref new = old;
          new.count--;
          if (old.count <= 0)
              return -1;
          if (likely(try_cmpxchg64_relaxed(&lockref->lock_count,
                                           &old.lock_count,
                                           new.lock_count))) {
              return new.count;
          }
          if (!--retry)
              break;
      }
      
    • At line 15 of lib/lockref.c, Task 3692 issues an 8-byte marked read READ_ONCE(lockref->lock_count) starting at offset +0 (0xffff8881058a8740).
  3. KCSAN Flagging:

    • KCSAN sets a watchpoint covering the 8-byte range 0xffff8881058a8740–0xffff8881058a8748 during Task 3692's READ_ONCE.
    • Task 3695 executes the unmarked write to parent->d_lockref.count at 0xffff8881058a8744 (within the watched range).
    • Because Task 3692 does not hold parent->d_lock, the two tasks do not share a common synchronization lock from the perspective of KCSAN's lockset/happens-before tracker. Under the formal C/LKMM model, an unmarked write racing concurrently with a read to overlapping bytes is classified as a data race.

3. Correctness & Invariant Verification

Despite KCSAN's report, this race is functionally safe on hardware and represents the intended design of optimistic lockref concurrency:

  • Lock Protection During Decrement: Task 3695 holds parent->d_lock (with acquire ordering upon lock entry) while executing --parent->d_lockref.count, and releases it via spin_unlock(&parent->d_lock) with release ordering.
  • Cmpxchg Safeguards:
    • If Task 3692 executes READ_ONCE(lockref->lock_count) while Task 3695 holds parent->d_lock, the lower 32-bit field (old.lock) contains a non-zero (locked) value. The condition arch_spin_value_unlocked(old.lock.rlock.raw_lock) evaluates to false. The cmpxchg loop terminates without executing, returning -1. In fast_dput(), this causes Task 3692 to fall back to the slow path (spin_lock(&dentry->d_lock)), correctly synchronizing behind Task 3695.
    • If Task 3692 reads lock_count before Task 3695 acquires the spinlock, Task 3695 then acquires parent->d_lock and updates count. When Task 3692 reaches try_cmpxchg64_relaxed(), the 64-bit atomic comparison compares lockref->lock_count in memory against old.lock_count. Because the lock state (and count) has changed, the cmpxchg fails, reloading old.lock_count with the updated, locked state.
  • Therefore, no reference count corruption, tear, or lost decrement can occur.
▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
2a16aa69 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in dentry_kill / lockref_put_return 2026/11/18 02:15 2026/11/18 02:15 2026/11/18 02:35 ca622473
bb57d222 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in dentry_kill / lockref_put_return 2026/11/18 02:05 2026/11/18 02:05 2026/11/18 02:26 ca622473

Sample crash report:
==================================================================
BUG: KCSAN: data-race in dentry_kill / lockref_put_return

read (marked) to 0xffff8881058a8740 of 8 bytes by task 3692 on cpu 1:
 lockref_put_return+0x2f/0x140 lib/lockref.c:92
 fast_dput+0x24/0x300 fs/dcache.c:970
 dput+0x1a/0x50 fs/dcache.c:1071
 path_put+0x1f/0x40 fs/namei.c:728
 vfs_statx+0xd1/0x170 fs/stat.c:361
 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 0xffff8881058a8744 of 4 bytes by task 3695 on cpu 0:
 dentry_kill+0x377/0x4f0 fs/dcache.c:877
 finish_dput+0x16/0x1d0 fs/dcache.c:1032
 dput+0x43/0x50 fs/dcache.c:1073
 end_renaming fs/namei.c:4128 [inline]
 filename_renameat2+0x486/0x6c0 fs/namei.c:6346
 __do_sys_rename fs/namei.c:6388 [inline]
 __se_sys_rename+0x4a/0x1c0 fs/namei.c:6384
 __x64_sys_rename+0x31/0x40 fs/namei.c:6384
 x64_sys_call+0x20fe/0x2550 arch/x86/include/generated/asm/syscalls_64.h:83
 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: 3695 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 17:09 upstream 666539eecdf4 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in dentry_kill / lockref_put_return
2026/11/17 01:58 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in dentry_kill / lockref_put_return
* Struck through repros no longer work on HEAD.