Technical Root Cause Analysis
1. Functions, Structs, and Fields Involved
- Source Files:
fs/dcache.c, include/linux/list_bl.h, include/linux/dcache.h
- Functions:
__d_drop(), dentry_kill(), fast_dput(), retain_dentry(), d_unhashed(), hlist_bl_unhashed()
- Structs & Fields:
struct dentry: field dentry->d_hash.pprev (member of struct hlist_bl_node), dentry->d_lockref, dentry->d_flags
struct hlist_bl_node: h->pprev
2. Detailed Interleaving and Mechanism
The reported KCSAN data race occurs between:
- An unmarked store to
dentry->d_hash.pprev in __d_drop():
void __d_drop(struct dentry *dentry)
{
if (!d_unhashed(dentry)) {
___d_drop(dentry);
dentry->d_hash.pprev = NULL; /* line 609 */
write_seqcount_invalidate(&dentry->d_seq);
}
}
- An unmarked load from
dentry->d_hash.pprev in hlist_bl_unhashed() called via d_unhashed() in retain_dentry():
static inline bool hlist_bl_unhashed(const struct hlist_bl_node *h)
{
return !h->pprev; /* include/linux/list_bl.h:54 */
}
This race is triggered during concurrent directory operations (such as concurrent unlinkat() calls operating on the same path):
-
Task 5148 (filename_unlinkat -> end_dirop -> dput -> fast_dput):
-
Task 2977 (filename_unlinkat -> start_dirop -> lookup_one_qstr_excl -> dput):
-
The Race Condition:
- While Task 2977 executes
dentry->d_hash.pprev = NULL under dentry->d_lock, Task 5148 reads dentry->d_hash.pprev in hlist_bl_unhashed() without holding dentry->d_lock.
- Because neither the load nor the store is marked with
READ_ONCE() / WRITE_ONCE(), KCSAN detects a data race on dentry->d_hash.pprev.
3. Invariant Safety and Lifecycle Handling
retain_dentry(dentry, false) is explicitly designed to tolerate stale or changing predicates when lockless:
rcu_read_lock() is held throughout fast_dput(), ensuring that RCU-delayed freeing (call_rcu(&dentry->d_rcu, __d_free)) prevents memory reclamation while the lockless check executes.
- If Task 5148 reads the old non-NULL
pprev, d_unhashed() returns false, and retain_dentry(dentry, false) may return true. Task 5148 drops its RCU read lock and exits dput(), its reference decrement (1 -> 0) having already been committed prior to Task 2977's acquisition and eviction of the dentry.
- If Task 5148 reads NULL,
retain_dentry(dentry, false) returns false. Task 5148 falls back to the locked slow path at locked: (fs/dcache.c:1021), taking dentry->d_lock. At this point, Task 2977 has already marked the dentry dead via lockref_mark_dead(), setting dentry->d_lockref.count = __LOCKREF_DEAD_VAL (-128). Because dentry->d_lockref.count evaluates to non-zero, fast_dput() unlocks dentry->d_lock and returns true, correctly avoiding any double-free or redundant eviction.
To eliminate the KCSAN warning, access to h->pprev in hlist_bl_unhashed() and the unhashing write in __d_drop() should be annotated with READ_ONCE() / WRITE_ONCE() (or data_race()).