Technical Root Cause Analysis
1. Involved Entities and Files
- Files:
- Structs and Fields:
2. Triggering Sequence and Data Race Mechanism
The KCSAN report records a concurrent access to the parent dentry's d_lockref:
-
Reader Path (lockref_get_not_dead on CPU 0):
-
Writer Path (d_alloc on CPU 1):
-
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}.