| 0/0 |
2026/01/15 01:01 |
flow |
assessment-kcsan |
23m
Model:
Results: map[Benign:true Confident:true Explanation:The data race reported by KCSAN occurs between `__d_lookup_rcu` (the reader) and `__d_rehash` (the writer) on the `dentry->d_hash.pprev` field.
### Analysis of the Race
1. **Reader Side (`__d_lookup_rcu`)**:
* The reader is iterating through a dentry hash bucket using `hlist_bl_for_each_entry_rcu`.
* For each dentry found in the bucket, it performs a series of checks. One of these checks is `d_unhashed(dentry)`, which calls `hlist_bl_unhashed`.
* `hlist_bl_unhashed` simply checks if `dentry->d_hash.pprev` is NULL.
* If `d_unhashed` returns true, the reader skips this dentry (`continue`) and proceeds to the next one in the bucket.
2. **Writer Side (`__d_rehash` / `hlist_bl_add_head_rcu`)**:
* The writer is adding a dentry to a hash bucket.
* In `hlist_bl_add_head_rcu`, it first sets `n->pprev = &h->first;` (a plain store).
* Then it calls `hlist_bl_set_first_rcu(h, n)`, which uses `rcu_assign_pointer` (a store-release) to make the new dentry visible at the head of the bucket.
### Why the Race is Benign
* **Logic and Synchronization**: The reader only finds the dentry in the bucket after the writer has updated the bucket's head. Since the writer uses `rcu_assign_pointer` to update the head and the reader uses `hlist_bl_for_each_entry_rcu` (which uses `rcu_dereference` or equivalent) to traverse the list, there is a formal RCU ordering. This ordering ensures that the write to `n->pprev` should be visible to any reader that finds `n` in the list.
* **Consequences of the Race**: Even if a reader sees a stale NULL value for `pprev` (due to the lack of `READ_ONCE`/`WRITE_ONCE` on the specific field or compiler reordering), the only consequence is that `d_unhashed(dentry)` returns true. This causes `__d_lookup_rcu` to skip the dentry and potentially return `NULL` (a false negative).
* **Error Handling**: In the VFS, `__d_lookup_rcu` is a fast-path optimization. If it fails to find a dentry, the caller (typically `lookup_fast` or `walk_component`) handles the failure by falling back to a "slow path" (non-RCU walk), which involves taking the proper locks and retrying the lookup. This ensures that the overall result is correct.
* **Safety**: The access is a simple pointer-sized load/comparison. There is no risk of memory corruption, crashes, or undefined behavior that could compromise the kernel's integrity.
This type of race is common in highly optimized, RCU-protected structures like the dcache and is generally treated as benign because the algorithm is designed to be resilient to such transient inconsistencies through sequence counters and fallback paths.
### Conclusion
The data race is **benign**. It may cause a transient false negative in a fast-path lookup, which is correctly handled by existing retry mechanisms.]
|
| 1/1 |
2026/01/15 01:01 |
action |
kernel-checkouter |
0m
Model:
Results: map[KernelSrc:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/src/a8cdb5a178c0afc02aca9f7a83d7f6bfe478ea9e]
|
| 2/1 |
2026/01/15 01:01 |
action |
kernel-builder |
4m
Model:
Results: map[KernelObj:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/build/480c7270dfdb6b3b80f5245d0988f700574901ff]
|
| 3/1 |
2026/01/15 01:05 |
action |
codesearch-prepare |
14m
Model:
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/01/15 01:20 |
agent |
expert |
3m
Model:
Results: map[Benign:true Confident:true]
Instruction:
You are an experienced Linux kernel developer tasked with determining if the given kernel
data race is benign or not. The data race report is from KCSAN tool.
It contains 2 stack traces of the memory accesses that constitute a data race.
A "benign" data races are on a simple int/bool variable or similar field,
and the accesses are not supposed to be protected by any mutual exclusion primitives.
Common examples of such "benign" data races are accesses to various flags fields,
statistics counters, and similar. A "benign" data race does not lead to memory corruption/crash
with a conservative compiler that compiles memory accesses to primitive types
effectively as atomic.
A non-benign (or "harmful" data race) can lead to corruption/crash even with
a conservative compiler that compiles memory accesses to primitive types
effectively as atomic. A common example of a "harmful" data races is race on
a complex container (list/hashmap/etc), where accesses are supposed to be protected
by a mutual exclusion primitive.
In the final reply explain why you think the given data race is benign or is harmful.
Use the provided tools to confirm any assumptions, variables/fields being accessed, etc.
In particular, don't make assumptions about the kernel source code,
use codesearch tools to read the actual source code.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The data race report is:
==================================================================
BUG: KCSAN: data-race in __d_lookup_rcu / __d_rehash
read to 0xffff88810051b550 of 8 bytes by task 3006 on cpu 1:
hlist_bl_unhashed include/linux/list_bl.h:54 [inline]
d_unhashed include/linux/dcache.h:366 [inline]
__d_lookup_rcu+0x21c/0x2d0 fs/dcache.c:2358
lookup_fast+0xa3/0x320 fs/namei.c:1792
walk_component fs/namei.c:2217 [inline]
link_path_walk+0x51c/0xe30 fs/namei.c:2589
path_lookupat+0x8c/0x500 fs/namei.c:2745
filename_lookup+0x147/0x340 fs/namei.c:2775
vfs_statx+0x9d/0x390 fs/stat.c:353
vfs_fstatat+0x115/0x170 fs/stat.c:375
__do_sys_newfstatat fs/stat.c:542 [inline]
__se_sys_newfstatat+0x55/0x260 fs/stat.c:536
__x64_sys_newfstatat+0x55/0x70 fs/stat.c:536
x64_sys_call+0x111f/0x3000 arch/x86/include/generated/asm/syscalls_64.h:263
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xd8/0x2a0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
write to 0xffff88810051b550 of 8 bytes by task 10110 on cpu 0:
hlist_bl_add_head_rcu include/linux/rculist_bl.h:81 [inline]
__d_rehash+0x9e/0x1f0 fs/dcache.c:2534
__d_add+0x344/0x4a0 fs/dcache.c:2759
d_add+0x47/0x50 fs/dcache.c:2782
simple_lookup+0xba/0xd0 fs/libfs.c:81
lookup_one_qstr_excl+0xcb/0x250 fs/namei.c:1747
__start_dirop fs/namei.c:2866 [inline]
start_dirop fs/namei.c:2875 [inline]
filename_create+0x141/0x210 fs/namei.c:4879
do_symlinkat+0x94/0x470 fs/namei.c:5534
__do_sys_symlinkat fs/namei.c:5562 [inline]
__se_sys_symlinkat fs/namei.c:5559 [inline]
__x64_sys_symlinkat+0x5a/0x70 fs/namei.c:5559
x64_sys_call+0x2a36/0x3000 arch/x86/include/generated/asm/syscalls_64.h:267
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xd8/0x2a0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 10110 Comm: syz-executor Tainted: G W syzkaller #0 PREEMPT(voluntary)
Tainted: [W]=WARN
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/25/2025
==================================================================
Reply: The data race reported by KCSAN occurs between `__d_lookup_rcu` (the reader) and `__d_rehash` (the writer) on the `dentry->d_hash.pprev` field.
### Analysis of the Race
1. **Reader Side (`__d_lookup_rcu`)**:
* The reader is iterating through a dentry hash bucket using `hlist_bl_for_each_entry_rcu`.
* For each dentry found in the bucket, it performs a series of checks. One of these checks is `d_unhashed(dentry)`, which calls `hlist_bl_unhashed`.
* `hlist_bl_unhashed` simply checks if `dentry->d_hash.pprev` is NULL.
* If `d_unhashed` returns true, the reader skips this dentry (`continue`) and proceeds to the next one in the bucket.
2. **Writer Side (`__d_rehash` / `hlist_bl_add_head_rcu`)**:
* The writer is adding a dentry to a hash bucket.
* In `hlist_bl_add_head_rcu`, it first sets `n->pprev = &h->first;` (a plain store).
* Then it calls `hlist_bl_set_first_rcu(h, n)`, which uses `rcu_assign_pointer` (a store-release) to make the new dentry visible at the head of the bucket.
### Why the Race is Benign
* **Logic and Synchronization**: The reader only finds the dentry in the bucket after the writer has updated the bucket's head. Since the writer uses `rcu_assign_pointer` to update the head and the reader uses `hlist_bl_for_each_entry_rcu` (which uses `rcu_dereference` or equivalent) to traverse the list, there is a formal RCU ordering. This ordering ensures that the write to `n->pprev` should be visible to any reader that finds `n` in the list.
* **Consequences of the Race**: Even if a reader sees a stale NULL value for `pprev` (due to the lack of `READ_ONCE`/`WRITE_ONCE` on the specific field or compiler reordering), the only consequence is that `d_unhashed(dentry)` returns true. This causes `__d_lookup_rcu` to skip the dentry and potentially return `NULL` (a false negative).
* **Error Handling**: In the VFS, `__d_lookup_rcu` is a fast-path optimization. If it fails to find a dentry, the caller (typically `lookup_fast` or `walk_component`) handles the failure by falling back to a "slow path" (non-RCU walk), which involves taking the proper locks and retrying the lookup. This ensures that the overall result is correct.
* **Safety**: The access is a simple pointer-sized load/comparison. There is no risk of memory corruption, crashes, or undefined behavior that could compromise the kernel's integrity.
This type of race is common in highly optimized, RCU-protected structures like the dcache and is generally treated as benign because the algorithm is designed to be resilient to such transient inconsistencies through sequence counters and fallback paths.
### Conclusion
The data race is **benign**. It may cause a transient false negative in a fast-path lookup, which is correctly handled by existing retry mechanisms.
|
| 5/2 |
2026/01/15 01:20 |
llm |
expert |
0m
Model:
Thoughts: **Decoding this Data Race**
Okay, so the report flags a data race, specifically between `__d_lookup_rcu` acting as a reader and `__d_rehash` acting as the writer. The conflict centers around the memory location `0xffff88810051b550`. It appears this address is accessed via `hlist_bl_unhashed` and `hlist_bl_add_head_rcu`. Time to dig into the code.
Let's see, `__d_lookup_rcu` internally calls `d_unhashed`, which, in turn, calls `hlist_bl_unhashed`. Meanwhile, `__d_rehash` calls `hlist_bl_add_head_rcu`. Right. So, that address, `0xffff88810051b550`, where the race occurs... I'm betting that's the `pprev` pointer of an `hlist_bl_node`. That makes sense given the way `hlist_bl_unhashed` works – it's going to check that `pprev` pointer.
Just to be certain, let me quickly double-check the definition of `hlist_bl_unhashed` to confirm my suspicion.
|
| 6/2 |
2026/01/15 01:20 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_unhashed SourceFile:include/linux/list_bl.h]
Results: map[Missing:false SourceCode: 51: static inline bool hlist_bl_unhashed(const struct hlist_bl_node *h)
52: {
53: return !h->pprev;
54: }
SourceFile:include/linux/list_bl.h]
|
| 7/2 |
2026/01/15 01:20 |
llm |
expert |
0m
Model:
Thoughts: Okay, here's my take on those thoughts, as if they were my own:
**Verifying the Unhashed Data**
Alright, time to shift gears and get into the nitty-gritty. We've been working with this data, and now it's time to put it through its paces. Specifically, I need to make absolutely sure `d_unhashed` is behaving as expected. There's so much that relies on its integrity. A misstep here could cascade into a real mess, so thoroughness is key. Let's make sure it's doing what we need it to do. Let's verify it.
|
| 8/2 |
2026/01/15 01:20 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:d_unhashed SourceFile:include/linux/dcache.h]
Results: map[Missing:false SourceCode: 357: /**
358: * d_unhashed - is dentry hashed
359: * @dentry: entry to check
360: *
361: * Returns true if the dentry passed is not currently hashed.
362: */
363: static inline int d_unhashed(const struct dentry *dentry)
364: {
365: return hlist_bl_unhashed(&dentry->d_hash);
366: }
SourceFile:include/linux/dcache.h]
|
| 9/2 |
2026/01/15 01:20 |
llm |
expert |
0m
Model:
Thoughts: Okay, let's dive into this. Here's my thought process on verifying `hlist_bl_add_head_rcu`, as if these are my own internal workings:
**Verifying `hlist_bl_add_head_rcu` - Deep Dive**
Alright, time to critically examine this function. I'm already visualizing the underlying data structures – specifically the `hlist_bl_head` and the `hlist_bl_node`. My primary focus is on how it integrates with RCU (Read-Copy-Update). I need to ensure the RCU semantics are being respected completely, so that readers can correctly access the list concurrently with modifications.
My initial questions are:
* How does the `rcu_read_lock()`/`rcu_read_unlock()` interaction play out around any modification? Is it clear that the insertion is protected by RCU? Is the existing list consistent before and after the modification?
* What about memory barriers? `hlist_bl_add_head_rcu` is a write-side operation. I need to see how memory ordering is enforced to ensure that the newly inserted node's data is visible to readers under RCU protection.
* Is it vulnerable to any race conditions, even with RCU? Even with RCU there are still possibilities for error if not carefully handled.
* Are there any assumptions about the data structure that might limit its general applicability, especially in complex, concurrent scenarios?
I'm thinking about the potential for ABA problems in the context of `hlist_bl_add_head_rcu` and its interactions with the RCU. Are there any steps taken to mitigate this or would the general properties of the list itself prevent it?
I need to confirm if it's correctly handling the pointer updates, and if any prefetching is appropriately performed. Any subtle bugs will become obvious in performance, or even worse cause data loss. I'm going to carefully step through the logic, mentally tracing its execution to ensure there are no surprises in the code. I am particularly curious to see how the code handles `hlist_bl_head` initialization, and the role of the `hlist_bl_node`'s `next` and `pprev` pointers during the add operation. I am paying very close attention to any uses of `rcu_assign_pointer` or similar atomic operations, which are the bread and butter of correct RCU usage.
Okay, let's get into the details...
|
| 10/2 |
2026/01/15 01:20 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_add_head_rcu SourceFile:include/linux/rculist_bl.h]
Results: map[Missing:false SourceCode: 51: /**
52: * hlist_bl_add_head_rcu
53: * @n: the element to add to the hash list.
54: * @h: the list to add to.
55: *
56: * Description:
57: * Adds the specified element to the specified hlist_bl,
58: * while permitting racing traversals.
59: *
60: * The caller must take whatever precautions are necessary
61: * (such as holding appropriate locks) to avoid racing
62: * with another list-mutation primitive, such as hlist_bl_add_head_rcu()
63: * or hlist_bl_del_rcu(), running on this same list.
64: * However, it is perfectly legal to run concurrently with
65: * the _rcu list-traversal primitives, such as
66: * hlist_bl_for_each_entry_rcu(), used to prevent memory-consistency
67: * problems on Alpha CPUs. Regardless of the type of CPU, the
68: * list-traversal primitive must be guarded by rcu_read_lock().
69: */
70: static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n,
71: struct hlist_bl_head *h)
72: {
73: struct hlist_bl_node *first;
74:
75: /* don't need hlist_bl_first_rcu because we're under lock */
76: first = hlist_bl_first(h);
77:
78: n->next = first;
79: if (first)
80: first->pprev = &n->next;
81: n->pprev = &h->first;
82:
83: /* need _rcu because we can have concurrent lock free readers */
84: hlist_bl_set_first_rcu(h, n);
85: }
SourceFile:include/linux/rculist_bl.h]
|
| 11/2 |
2026/01/15 01:20 |
llm |
expert |
0m
Model:
Thoughts: **Dentry Hashing Conundrum**
Okay, so I'm looking at this report, and it seems like we have a read-write race condition here. The report points to a specific memory location, `0xffff88810051b550`, where we see a conflict. Task 3006 on CPU 1 is reading from that location, specifically using `hlist_bl_unhashed`. This function, at line 53, is simply returning the negation of `h->pprev`. Effectively, it is checking if the list is "unhashed", where the list being "unhashed" means the previous pointer is `NULL`.
Then, on CPU 0, task 10110 is writing to the *same* location. The function being called is `hlist_bl_add_head_rcu`, at line 81, and it's setting `n->pprev` to a non-null value – assigning the address of `h->first`. Now, the crucial piece is this: `n` represents `&dentry->d_hash`. Therefore, `n->pprev` is actually `dentry->d_hash.pprev`.
So, we're seeing `dentry->d_hash.pprev` being set by one task and read by another. If the value of `dentry->d_hash.pprev` is transitioning from `NULL` to something *other* than `NULL`, the function `d_unhashed` will return false. I think this points to a potential issue with how we're handling the dentry hash list. We need to check the exact conditions. Let's dig into `__d_lookup_rcu` in `fs/dcache.c` and see how this value is used in the context of the dcache.
|
| 12/2 |
2026/01/15 01:20 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:__d_lookup_rcu SourceFile:fs/dcache.c]
Results: map[Missing:false SourceCode:2262: /**
2263: * __d_lookup_rcu - search for a dentry (racy, store-free)
2264: * @parent: parent dentry
2265: * @name: qstr of name we wish to find
2266: * @seqp: returns d_seq value at the point where the dentry was found
2267: * Returns: dentry, or NULL
2268: *
2269: * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2270: * resolution (store-free path walking) design described in
2271: * Documentation/filesystems/path-lookup.txt.
2272: *
2273: * This is not to be used outside core vfs.
2274: *
2275: * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2276: * held, and rcu_read_lock held. The returned dentry must not be stored into
2277: * without taking d_lock and checking d_seq sequence count against @seq
2278: * returned here.
2279: *
2280: * Alternatively, __d_lookup_rcu may be called again to look up the child of
2281: * the returned dentry, so long as its parent's seqlock is checked after the
2282: * child is looked up. Thus, an interlocking stepping of sequence lock checks
2283: * is formed, giving integrity down the path walk.
2284: *
2285: * NOTE! The caller *has* to check the resulting dentry against the sequence
2286: * number we've returned before using any of the resulting dentry state!
2287: */
2288: struct dentry *__d_lookup_rcu(const struct dentry *parent,
2289: const struct qstr *name,
2290: unsigned *seqp)
2291: {
2292: u64 hashlen = name->hash_len;
2293: const unsigned char *str = name->name;
2294: struct hlist_bl_head *b = d_hash(hashlen);
2295: struct hlist_bl_node *node;
2296: struct dentry *dentry;
2297:
2298: /*
2299: * Note: There is significant duplication with __d_lookup_rcu which is
2300: * required to prevent single threaded performance regressions
2301: * especially on architectures where smp_rmb (in seqcounts) are costly.
2302: * Keep the two functions in sync.
2303: */
2304:
2305: if (unlikely(parent->d_flags & DCACHE_OP_COMPARE))
2306: return __d_lookup_rcu_op_compare(parent, name, seqp);
2307:
2308: /*
2309: * The hash list is protected using RCU.
2310: *
2311: * Carefully use d_seq when comparing a candidate dentry, to avoid
2312: * races with d_move().
2313: *
2314: * It is possible that concurrent renames can mess up our list
2315: * walk here and result in missing our dentry, resulting in the
2316: * false-negative result. d_lookup() protects against concurrent
2317: * renames using rename_lock seqlock.
2318: *
2319: * See Documentation/filesystems/path-lookup.txt for more details.
2320: */
2321: hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2322: unsigned seq;
2323:
2324: /*
2325: * The dentry sequence count protects us from concurrent
2326: * renames, and thus protects parent and name fields.
2327: *
2328: * The caller must perform a seqcount check in order
2329: * to do anything useful with the returned dentry.
2330: *
2331: * NOTE! We do a "raw" seqcount_begin here. That means that
2332: * we don't wait for the sequence count to stabilize if it
2333: * is in the middle of a sequence change. If we do the slow
2334: * dentry compare, we will do seqretries until it is stable,
2335: * and if we end up with a successful lookup, we actually
2336: * want to exit RCU lookup anyway.
2337: *
2338: * Note that raw_seqcount_begin still *does* smp_rmb(), so
2339: * we are still guaranteed NUL-termination of ->d_name.name.
2340: */
2341: seq = raw_seqcount_begin(&dentry->d_seq);
2342: if (dentry->d_parent != parent)
2343: continue;
2344: if (dentry->d_name.hash_len != hashlen)
2345: continue;
2346: if (unlikely(dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0))
2347: continue;
2348: /*
2349: * Check for the dentry being unhashed.
2350: *
2351: * As tempting as it is, we *can't* skip it because of a race window
2352: * between us finding the dentry before it gets unhashed and loading
2353: * the sequence counter after unhashing is finished.
2354: *
2355: * We can at least predict on it.
2356: */
2357: if (unlikely(d_unhashed(dentry)))
2358: continue;
2359: *seqp = seq;
2360: return dentry;
2361: }
2362: return NULL;
2363: }
SourceFile:fs/dcache.c]
|
| 13/2 |
2026/01/15 01:20 |
llm |
expert |
0m
Model:
Thoughts: **Analyzing a Discrepancy in `__d_lookup_rcu`**
Okay, let's break this down. Line 2357-2358 in `__d_lookup_rcu` – specifically, that `if (unlikely(d_unhashed(dentry))) continue;` check – that's the key. The report says it correlates to `__d_lookup_rcu+0x21c/0x2d0 fs/dcache.c:2358`. Makes sense.
But wait. If we're inside the `hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash)` loop, that means we *are* iterating through a hash bucket, `b`. And the `dentry` we're examining *should* be in that bucket, shouldn't it? So, logically, `d_unhashed(dentry)` should evaluate to `false`. That's the expected state.
However... I see the potential concurrency issue. A `dentry` could be concurrently removed from the hash list. But we're specifically looking at `__d_rehash`, which implies that the dentry is *being added* to a hash list. That's the crucial point. Why are we seeing `d_unhashed` as true when we expect it to be false in the context of `__d_rehash`?
I need to dig deeper into the code. Let's examine how `__d_rehash` functions. What are the potential race conditions that could lead to this?
|
| 14/2 |
2026/01/15 01:21 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:__d_rehash SourceFile:fs/dcache.c]
Results: map[Missing:false SourceCode:2528: static void __d_rehash(struct dentry *entry)
2529: {
2530: struct hlist_bl_head *b = d_hash(entry->d_name.hash);
2531:
2532: hlist_bl_lock(b);
2533: hlist_bl_add_head_rcu(&entry->d_hash, b);
2534: hlist_bl_unlock(b);
2535: }
SourceFile:fs/dcache.c]
|
| 15/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: **Analyzing Potential Race Conditions in `hlist_bl_add_head_rcu` and `__d_lookup_rcu`**
Okay, so I'm looking at this code, specifically the interaction between `hlist_bl_add_head_rcu` and `__d_lookup_rcu`. The core of the issue seems to stem from how we're manipulating the `pprev` pointer within the RCU-protected `hlist_bl` data structure.
First, I see `hlist_bl_add_head_rcu`. The critical line is `n->pprev = &h->first;` (line 81). This sets the `pprev` of the *new* node `n` to point to the `first` pointer within the head `h`. Then, line 84 updates the head using `hlist_bl_set_first_rcu(h, n)`. This means we're dealing with a head insertion.
Now, my concern is with `__d_lookup_rcu`. I'm seeing this loop: `hlist_bl_for_each_entry_rcu`. And within it, the code checks `d_unhashed(dentry)`, which translates to looking at `dentry->d_hash.pprev`. This is where it gets interesting, or potentially problematic.
If `__d_lookup_rcu` is in the middle of iterating through a list, how can it see an entry that is simultaneously being added to the *beginning* of that same list by `hlist_bl_add_head_rcu`? It looks like there's a window of vulnerability here.
`hlist_bl_for_each_entry_rcu` iterates from the *head* of the list. The issue becomes clear when I think about how `hlist_bl_set_first_rcu` interacts with the reader and writer. It looks like it's a window of vulnerability during the update.
I'm thinking, if `__d_lookup_rcu` is already looking at a `dentry`, it must have reached it via either a `next` pointer in the list or from the `head` pointer.
So, the crucial question is: Can `__d_lookup_rcu` observe an uninitialized or partially initialized `dentry`? Because if `__d_lookup_rcu` is examining a dentry, how can that dentry be in the list, unless it was removed and then rehashed?
Let me think… `__d_rehash` is called from `__d_add`.
This smells like a potential race condition when adding a dentry. I need to dig deeper into `__d_add` to understand how `__d_rehash` is called and if there is another race condition there. There is likely an ordering constraint that I need to confirm.
|
| 16/2 |
2026/01/15 01:21 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:__d_add SourceFile:fs/dcache.c]
Results: map[Missing:false SourceCode:2736: static inline void __d_add(struct dentry *dentry, struct inode *inode,
2737: const struct dentry_operations *ops)
2738: {
2739: wait_queue_head_t *d_wait;
2740: struct inode *dir = NULL;
2741: unsigned n;
2742: spin_lock(&dentry->d_lock);
2743: if (unlikely(d_in_lookup(dentry))) {
2744: dir = dentry->d_parent->d_inode;
2745: n = start_dir_add(dir);
2746: d_wait = __d_lookup_unhash(dentry);
2747: }
2748: if (unlikely(ops))
2749: d_set_d_op(dentry, ops);
2750: if (inode) {
2751: unsigned add_flags = d_flags_for_inode(inode);
2752: hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2753: raw_write_seqcount_begin(&dentry->d_seq);
2754: __d_set_inode_and_type(dentry, inode, add_flags);
2755: raw_write_seqcount_end(&dentry->d_seq);
2756: fsnotify_update_flags(dentry);
2757: }
2758: __d_rehash(dentry);
2759: if (dir)
2760: end_dir_add(dir, n, d_wait);
2761: spin_unlock(&dentry->d_lock);
2762: if (inode)
2763: spin_unlock(&inode->i_lock);
2764: }
SourceFile:fs/dcache.c]
|
| 17/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: **RCU and Dentry Hashing: A Race Condition Investigation**
Okay, let's break this down. My initial thought is that if `__d_add` is calling `__d_rehash`, we're dealing with a dentry being hashed for the first time or after being unhashed. That's our starting point. Now, I'm focusing on the interaction with `__d_lookup_rcu`.
`__d_lookup_rcu` iterates through a bucket `b`. The key observation here is this: if `__d_lookup_rcu` *finds* the dentry in bucket `b`, it *must* already be present in that bucket. And, if `__d_rehash` is *adding* the dentry to that same bucket, it logically wasn't there *before* `hlist_bl_set_first_rcu`. So far, so good.
Hold on, though. The question is: are these processes accessing *different* dentries, or the *same* dentry, but `__d_lookup_rcu` got there by some other route? The latter is impossible since it iterates over the list. Okay, so if `__d_lookup_rcu` finds the dentry, it means *another* thread has already finished the `hlist_bl_set_first_rcu` operation if this dentry is the first element, or some other element's `next` pointer references it.
But the race condition, I'm realizing, is centered around `dentry->d_hash.pprev`.
If `__d_rehash` is executing, let's trace this line by line:
At line 81: `n->pprev = &h->first;` is setting `n->pprev`. At line 84: `hlist_bl_set_first_rcu(h, n)` makes it visible to readers of the list `h`.
If `__d_lookup_rcu` is reading `dentry->d_hash.pprev` (which is `n->pprev`), it has already found the dentry in the list. This leads me to believe `hlist_bl_set_first_rcu(h, n)` must have *already* happened, from the reader's perspective. But wait, RCU doesn't guarantee that the store to `n->pprev` is seen before `h->first = n` *unless* there's a memory barrier. The key is in how `hlist_bl_set_first_rcu` is implemented, using `rcu_assign_pointer` (or something equivalent). Let's check that function to verify my memory of this!
|
| 18/2 |
2026/01/15 01:21 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_set_first_rcu SourceFile:include/linux/rculist_bl.h]
Results: map[Missing:false SourceCode: 10: static inline void hlist_bl_set_first_rcu(struct hlist_bl_head *h,
11: struct hlist_bl_node *n)
12: {
13: LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
14: LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
15: LIST_BL_LOCKMASK);
16: rcu_assign_pointer(h->first,
17: (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK));
18: }
SourceFile:include/linux/rculist_bl.h]
|
| 19/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: **Analyzing a Potential Data Race in Hlist Operations**
Okay, so I'm looking at this reported data race involving `hlist_bl_unhashed` and trying to understand the problem. The core issue seems to be a potential visibility issue with the `pprev` pointer in `hlist_bl` data structures.
Initially, I reasoned that `rcu_assign_pointer` used when setting `n->pprev` to `&h->first` should provide a release barrier. This implies that any subsequent reader, seeing `n` through `h->first`, should also see the updated `n->pprev` value. But the report claims there's a data race, which is perplexing.
The potential race stems from `hlist_bl_unhashed` using a plain load: `return !h->pprev;`. KCSAN will flag a data race here if it detects a concurrent plain store. In this case, `n->pprev = &h->first;` is a plain store, but the barrier should mean this should not be a problem. My initial thought was that if `hlist_bl_unhashed` is used in an RCU-safe manner, it *should* be using `READ_ONCE` to ensure proper ordering.
I see that `hlist_bl_unhashed` is called in `__d_lookup_rcu` via `d_unhashed(dentry)`. If `d_unhashed` returns true, the dentry is skipped, and this is where the race might manifest. A dentry's `pprev` would be NULL before being added to a list, and `__d_rehash` subsequently sets it to non-NULL. The question is if this race is actually harmful. If `d_unhashed(dentry)` returns true (i.e., `pprev` is NULL) during a lookup, we skip a valid dentry, which is bad, but how can we find `dentry` in the list if it's "unhashed"?
Now, let's step back and consider how dentries are added and looked up. If we find `dentry`, it means it *is* hashed. So `d_unhashed(dentry)` should be false. If it's returning true due to a race, we might skip a valid entry. If we are iterating and find `dentry` this means `hlist_bl_set_first_rcu` had to have already happened or it was already in the list.
Now, looking at `__d_lookup_rcu` closely, it uses `hlist_bl_for_each_entry_rcu`. This implies that `hlist_bl_first_rcu` and `next` pointers are used.
If `__d_rehash` is adding `dentry`:
1. `n->next = first;`
2. `n->pprev = &h->first;`
3. `hlist_bl_set_first_rcu(h, n);`
A reader starting to iterate after step 3 *should* see `dentry` because of the release barrier. The reader should be able to see the effects of steps 1 and 2. The problem again is `hlist_bl_unhashed` not using `READ_ONCE`. So, theoretically, the compiler could reorder the read of `pprev`? However, `hlist_bl_for_each_entry_rcu` should provide some ordering, since it uses RCU primitives.
Here's the sticking point: If the reader sees `dentry`, then `n->pprev` SHOULD be `&h->first`. If the reader sees a NULL `pprev`, it's seeing the state *before* step 2. But if the reader sees `dentry` at all, it's because it read `h->first` and got `n`. The release-acquire relationship in `hlist_bl_for_each_entry_rcu` and `hlist_bl_set_first_rcu` should mean that `n->pprev` is visible. So the race is likely to manifest as skipping an entry in `__d_lookup_rcu`.
So, it seems that there is a missing `READ_ONCE` in `hlist_bl_unhashed`. This is likely the root cause of the race.
|
| 20/2 |
2026/01/15 01:21 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_for_each_entry_rcu SourceFile:include/linux/rculist_bl.h]
Results: map[Missing:true SourceCode: SourceFile:]
|
| 21/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience:
**Is That a Macro? Time to Dig In.**
Right, a potential issue has surfaced, and my gut feeling is leaning towards a macro being the culprit. Let me quickly rule that in or out. Where would such a beast lurk? My first instinct is to check the likely suspects, the core kernel data structures. My fingers instinctively go to `include/linux/rculist_bl.h`. That header file is a prime location for those sorts of implementation details, particularly given the mention of `rcu`. Hopefully, I'll find my answer there quickly. I'll need to confirm if it's there and then assess how it's being used. Let's see...
|
| 22/2 |
2026/01/15 01:21 |
tool |
codesearch-file-index |
0m
Model:
Args: map[SourceFile:include/linux/rculist_bl.h]
Results: map[Entities:[map[Kind:function Name:hlist_bl_add_head_rcu] map[Kind:function Name:hlist_bl_del_rcu] map[Kind:function Name:hlist_bl_first_rcu] map[Kind:function Name:hlist_bl_set_first_rcu]] Missing:false]
|
| 23/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: Okay, here's my take on those thoughts, as if I were the one having them:
**Navigating the Kernel's RCU List Implementation**
Alright, I need to find the definition, or at least the implementation, of that RCU list manipulation function. Given the naming convention and my experience, it's a pretty safe bet it's buried somewhere in the core RCU code. Specifically, the "bl" suffix suggests we're dealing with something related to "bl" or "batch-list" RCU variants, so `include/linux/rculist_bl.h` is the prime suspect. Now, the kernel's coding style leans heavily on macros for performance-critical or frequently-used operations, so I'm not expecting a clean function definition. More likely, it'll be a macro that expands into the necessary assembly or inline code to interact with the underlying RCU data structures. I'll need to remember to look for any preprocessor tricks or potential arguments being passed. Let's head there and see what we can find.
|
| 24/2 |
2026/01/15 01:21 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_first_rcu SourceFile:include/linux/rculist_bl.h]
Results: map[Missing:false SourceCode: 20: static inline struct hlist_bl_node *hlist_bl_first_rcu(struct hlist_bl_head *h)
21: {
22: return (struct hlist_bl_node *)
23: ((unsigned long)rcu_dereference_check(h->first, hlist_bl_is_locked(h)) & ~LIST_BL_LOCKMASK);
24: }
SourceFile:include/linux/rculist_bl.h]
|
| 25/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: **Delving into a KCSAN Data Race Report**
Okay, so `hlist_bl_first_rcu` uses `rcu_dereference_check`, which implies a store-release from `hlist_bl_add_head_rcu` with `rcu_assign_pointer`. Therefore, if `hlist_bl_first_rcu` returns a node `n`, then `n->pprev` *should* be valid and point to `&h->first`. But KCSAN is screaming about a data race, which means a concurrent read and write to the same memory location, at least one of which is non-atomic. Both accesses here are plain, non-atomic loads and stores, even if RCU is theoretically ordering them.
The question is, is this harmless? If `d_unhashed` returns true incorrectly, `__d_lookup_rcu` skips the dentry. If it's the dentry we're looking for, we return NULL, leading to a potential fallback to a non-RCU path or a failed lookup. But why the `d_unhashed` check *at all* in `__d_lookup_rcu`? The comment mentions a race window between finding the dentry and the sequence counter.
Wait a second. If the dentry is *being added* (the opposite of unhashing), then `pprev` goes from NULL to non-NULL. If a reader sees it in the list (so it's being added or already added), but reads `pprev` as NULL, it thinks it's unhashed. The crucial question is: can a reader *actually* see the dentry in the list with `pprev` still NULL? RCU should prevent that if `hlist_bl_first_rcu` returns it.
Could the compiler reorder the load of `pprev`? No, it needs `dentry` first. KCSAN is detecting races even if they're ordered if they are plain memory accesses. RCU might be ordering this, and it might be benign, but KCSAN isn't smart enough to know that, apparently.
Let's look closer at `d_unhashed` and `__d_rehash`. Is it being re-hashed from *another* list? No, `__d_rehash` is called from `__d_add`, usually for new dentries. Wait, look at `__d_add`! If `d_in_lookup` is true, the dentry is actually unhashed first! The check in `__d_lookup_rcu` is to catch dentries that were just deleted, i.e. when `hlist_bl_del_init_rcu` is called from `__d_drop`. Okay, that's important; let me review `hlist_bl_del_init_rcu`.
|
| 26/2 |
2026/01/15 01:21 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_del_init_rcu SourceFile:include/linux/rculist_bl.h]
Results: map[Missing:true SourceCode: SourceFile:]
|
| 27/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: Okay, here's my take on those thoughts, as if I were the one having them:
**Navigating the Kernel's RCU List Head**
Alright, I need to dig into the implementation of that RCU list head. Given my experience with the kernel, my initial instinct is to check the core header files. Considering the topic, it's highly likely that the definition and related macros for `rculist_bl` are tucked away in `include/linux`. Specifically, I'd bet a decent sum that I can find what I'm looking for in `include/linux/rculist_bl.h`. That's the most probable location. Let's start there and see what we can find.
|
| 28/2 |
2026/01/15 01:21 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_del_rcu SourceFile:include/linux/rculist_bl.h]
Results: map[Missing:false SourceCode: 26: /**
27: * hlist_bl_del_rcu - deletes entry from hash list without re-initialization
28: * @n: the element to delete from the hash list.
29: *
30: * Note: hlist_bl_unhashed() on entry does not return true after this,
31: * the entry is in an undefined state. It is useful for RCU based
32: * lockfree traversal.
33: *
34: * In particular, it means that we can not poison the forward
35: * pointers that may still be used for walking the hash list.
36: *
37: * The caller must take whatever precautions are necessary
38: * (such as holding appropriate locks) to avoid racing
39: * with another list-mutation primitive, such as hlist_bl_add_head_rcu()
40: * or hlist_bl_del_rcu(), running on this same list.
41: * However, it is perfectly legal to run concurrently with
42: * the _rcu list-traversal primitives, such as
43: * hlist_bl_for_each_entry().
44: */
45: static inline void hlist_bl_del_rcu(struct hlist_bl_node *n)
46: {
47: __hlist_bl_del(n);
48: n->pprev = LIST_POISON2;
49: }
SourceFile:include/linux/rculist_bl.h]
|
| 29/2 |
2026/01/15 01:21 |
llm |
expert |
0m
Model:
Thoughts: Here's the summary, tailored to an expert like you:
**Kernel Data Structure Conundrum**
Okay, hold on a second. I'm tracing this `hlist_bl_del_rcu` function, and it's setting `n->pprev = LIST_POISON2`. That's unusual, but the critical part here is that `d_unhashed` is checking for a NULL value. This feels like an indirect data dependency that I need to resolve.
I need to see where `__d_drop` is being called now. That has the potential to clarify this data flow and how this interaction is designed to function.
|
| 30/2 |
2026/01/15 01:22 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:__d_drop SourceFile:fs/dcache.c]
Results: map[Missing:false SourceCode: 571: void __d_drop(struct dentry *dentry)
572: {
573: if (!d_unhashed(dentry)) {
574: ___d_drop(dentry);
575: dentry->d_hash.pprev = NULL;
576: write_seqcount_invalidate(&dentry->d_seq);
577: }
578: }
SourceFile:fs/dcache.c]
|
| 31/2 |
2026/01/15 01:22 |
llm |
expert |
0m
Model:
Thoughts: Here's my thought process, as if I were the one analyzing this code:
**Analyzing the dentry Dropping Mechanism**
Okay, so I see `___d_drop` is the function responsible for, well, dropping a dentry. My initial thought is that it's doing something fundamental with the dentry's hash table entry. I'm noting that the function first locks a `hlist_bl_head` presumably protecting the hash bucket. That's good practice. I can see the call to `__hlist_bl_del` in the code, which looks like the core removal operation. But before it gets to that line, it sets `pprev` to NULL, and the code looks like the `hlist_bl_del_rcu` function, so I'm guessing this function likely deals with RCU-related synchronization for the hash list. Seems like an rcu-safe deletion function. So, I see that the function starts by getting the hash bucket, locks it, removes the dentry from the hash list, and then unlocks the bucket. A standard, thread-safe approach.
|
| 32/2 |
2026/01/15 01:22 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:___d_drop SourceFile:fs/dcache.c]
Results: map[Missing:false SourceCode: 553: static void ___d_drop(struct dentry *dentry)
554: {
555: struct hlist_bl_head *b;
556: /*
557: * Hashed dentries are normally on the dentry hashtable,
558: * with the exception of those newly allocated by
559: * d_obtain_root, which are always IS_ROOT:
560: */
561: if (unlikely(IS_ROOT(dentry)))
562: b = &dentry->d_sb->s_roots;
563: else
564: b = d_hash(dentry->d_name.hash);
565:
566: hlist_bl_lock(b);
567: __hlist_bl_del(&dentry->d_hash);
568: hlist_bl_unlock(b);
569: }
SourceFile:fs/dcache.c]
|
| 33/2 |
2026/01/15 01:22 |
llm |
expert |
0m
Model:
Thoughts: **Analyzing a Potential Data Race in dcache Operations**
Okay, so I'm looking at this KCSAN report, and it seems like there's a potential data race involving `__d_lookup_rcu` and `__d_rehash`, specifically concerning the `pprev` pointer within the `dentry`'s hash list. My initial thought was that perhaps `__hlist_bl_del` was missing a `pprev = NULL` assignment, but that's not the case. `__d_drop` correctly handles setting `dentry->d_hash.pprev = NULL`.
The reported race involves a reader (Task 3006 in `__d_lookup_rcu` calling `d_unhashed`) and a writer (Task 10110 in `__d_rehash` calling `hlist_bl_add_head_rcu`). `hlist_bl_add_head_rcu` sets `n->pprev = &h->first;`. Now, if the `dentry` was unhashed before, `pprev` would have been NULL.
The core question is, how can a reader be looking at this entry within the bucket *b* if it's in the process of being added? The key here lies in `hlist_bl_set_first_rcu(h, n)`, which happens *after* the `n->pprev` assignment. So, if the reader encounters this `dentry`, its `n->pprev` *should* already be set.
Since KCSAN flagged a race, these operations must be happening concurrently. `hlist_bl_add_head_rcu` does:
```c
81: n->pprev = &h->first;
82:
83: /* need _rcu because we can have concurrent lock free readers */
84: hlist_bl_set_first_rcu(h, n);
```
If Task A is rehashing and Task B is looking up, if B sees the `dentry`, A must have already executed line 84. Line 84 *must* be executed after line 81. With an `rcu_assign_pointer` (store-release) and implicit `rcu_dereference` (load-acquire) on most architectures, the store to `n->pprev` *should* be visible.
However, `hlist_bl_unhashed` does a plain load: `return !h->pprev;`. And `n->pprev = &h->first;` is a plain store. KCSAN flags it as a race because they are plain accesses.
The critical path is that `d_unhashed` can only return true (skipping the dentry) if it reads NULL. But a dentry must be in the list, if it followed a pointer to it. If the reader sees the *dentry* after the writer modified its `pprev`, then the updated value *should* be visible.
Could it be the reader is looking at a *different* dentry? The report specifies the *exact* address `0xffff88810051b550`, which is `dentry->d_hash.pprev`. `__d_lookup_rcu` accesses this *specific* address, so it's checking `d_unhashed` on the *same* `dentry` it found. So the reader is checking if the dentry it found is unhashed, that's what the code is doing.
If `__d_lookup_rcu` found the `dentry`, then the `dentry` is in the list, and `d_unhashed` *should* return false. If it's true, we skip it. This would be a false negative for the lookup. The caller would then handle the NULL return from `__d_lookup_rcu`. The report points to the lookup failing with `lookup_fast` calling `__d_lookup_rcu`. I'm starting to think this is a false negative.
|
| 34/2 |
2026/01/15 01:22 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:lookup_fast SourceFile:fs/namei.c]
Results: map[Missing:false SourceCode:1766: /**
1767: * lookup_fast - do fast lockless (but racy) lookup of a dentry
1768: * @nd: current nameidata
1769: *
1770: * Do a fast, but racy lookup in the dcache for the given dentry, and
1771: * revalidate it. Returns a valid dentry pointer or NULL if one wasn't
1772: * found. On error, an ERR_PTR will be returned.
1773: *
1774: * If this function returns a valid dentry and the walk is no longer
1775: * lazy, the dentry will carry a reference that must later be put. If
1776: * RCU mode is still in force, then this is not the case and the dentry
1777: * must be legitimized before use. If this returns NULL, then the walk
1778: * will no longer be in RCU mode.
1779: */
1780: static struct dentry *lookup_fast(struct nameidata *nd)
1781: {
1782: struct dentry *dentry, *parent = nd->path.dentry;
1783: int status = 1;
1784:
1785: /*
1786: * Rename seqlock is not required here because in the off chance
1787: * of a false negative due to a concurrent rename, the caller is
1788: * going to fall back to non-racy lookup.
1789: */
1790: if (nd->flags & LOOKUP_RCU) {
1791: dentry = __d_lookup_rcu(parent, &nd->last, &nd->next_seq);
1792: if (unlikely(!dentry)) {
1793: if (!try_to_unlazy(nd))
1794: return ERR_PTR(-ECHILD);
1795: return NULL;
1796: }
1797:
1798: /*
1799: * This sequence count validates that the parent had no
1800: * changes while we did the lookup of the dentry above.
1801: */
1802: if (read_seqcount_retry(&parent->d_seq, nd->seq))
1803: return ERR_PTR(-ECHILD);
1804:
1805: status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags);
1806: if (likely(status > 0))
1807: return dentry;
1808: if (!try_to_unlazy_next(nd, dentry))
1809: return ERR_PTR(-ECHILD);
1810: if (status == -ECHILD)
1811: /* we'd been told to redo it in non-rcu mode */
1812: status = d_revalidate(nd->inode, &nd->last,
1813: dentry, nd->flags);
1814: } else {
1815: dentry = __d_lookup(parent, &nd->last);
1816: if (unlikely(!dentry))
1817: return NULL;
1818: status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags);
1819: }
1820: if (unlikely(status <= 0)) {
1821: if (!status)
1822: d_invalidate(dentry);
1823: dput(dentry);
1824: return ERR_PTR(status);
1825: }
1826: return dentry;
1827: }
SourceFile:fs/namei.c]
|
| 35/2 |
2026/01/15 01:22 |
llm |
expert |
0m
Model:
Thoughts: **Analyzing a Potential Race Condition in the Directory Entry Lookup**
Okay, so I'm looking at this `__d_lookup_rcu` function, and it seems like if it returns NULL, the code calls `try_to_unlazy(nd)`. That makes sense; a missed lookup in RCU mode triggers a retry in non-RCU mode, so it's a performance optimization, a false negative, basically a slower lookup. No big deal, right?
But then I started thinking, why the heck are we even racing in the first place? If we're adding something, it should only become visible after `n->pprev` is set. And we're using `rcu_assign_pointer`, so that sounds safe on the surface.
Then it hits me: this race is actually between the reader and the writer. The reader is checking `hlist_bl_unhashed(dentry)`, looking for `!dentry->d_hash.pprev`. Meanwhile, the writer is setting `n->pprev = &h->first` in `hlist_bl_add_head_rcu`.
The crux is this: if the writer *hasn't* yet called `hlist_bl_set_first_rcu(h, n)`, how can the reader possibly see `dentry`? It's gotta be there, or at least have been there before, and now it's not. Could the `dentry` have been *already* in the list, then vanished? The code calls `__d_rehash` after all. But, `__d_rehash` is called from `__d_add`.
Hang on a second… could it be in a *different* list? No, the writer is adding it to `b = d_hash(entry->d_name.hash)`. That's that hash bucket.
Aha! Maybe `dentry` is being *moved* from one hash bucket to another. We've got `d_move` for that. And `d_move` calls `__d_move`, and `__d_move` calls `d_rehash` or something similar, likely.
Right, I need to check `__d_move`. Let's dig into that and see what's really happening.
|
| 36/2 |
2026/01/15 01:22 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:__d_move SourceFile:fs/dcache.c]
Results: map[Missing:false SourceCode:2858: /*
2859: * __d_move - move a dentry
2860: * @dentry: entry to move
2861: * @target: new dentry
2862: * @exchange: exchange the two dentries
2863: *
2864: * Update the dcache to reflect the move of a file name. Negative dcache
2865: * entries should not be moved in this way. Caller must hold rename_lock, the
2866: * i_rwsem of the source and target directories (exclusively), and the sb->
2867: * s_vfs_rename_mutex if they differ. See lock_rename().
2868: */
2869: static void __d_move(struct dentry *dentry, struct dentry *target,
2870: bool exchange)
2871: {
2872: struct dentry *old_parent, *p;
2873: wait_queue_head_t *d_wait;
2874: struct inode *dir = NULL;
2875: unsigned n;
2876:
2877: WARN_ON(!dentry->d_inode);
2878: if (WARN_ON(dentry == target))
2879: return;
2880:
2881: BUG_ON(d_ancestor(target, dentry));
2882: old_parent = dentry->d_parent;
2883: p = d_ancestor(old_parent, target);
2884: if (IS_ROOT(dentry)) {
2885: BUG_ON(p);
2886: spin_lock(&target->d_parent->d_lock);
2887: } else if (!p) {
2888: /* target is not a descendent of dentry->d_parent */
2889: spin_lock(&target->d_parent->d_lock);
2890: spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED);
2891: } else {
2892: BUG_ON(p == dentry);
2893: spin_lock(&old_parent->d_lock);
2894: if (p != target)
2895: spin_lock_nested(&target->d_parent->d_lock,
2896: DENTRY_D_LOCK_NESTED);
2897: }
2898: spin_lock_nested(&dentry->d_lock, 2);
2899: spin_lock_nested(&target->d_lock, 3);
2900:
2901: if (unlikely(d_in_lookup(target))) {
2902: dir = target->d_parent->d_inode;
2903: n = start_dir_add(dir);
2904: d_wait = __d_lookup_unhash(target);
2905: }
2906:
2907: write_seqcount_begin(&dentry->d_seq);
2908: write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
2909:
2910: /* unhash both */
2911: if (!d_unhashed(dentry))
2912: ___d_drop(dentry);
2913: if (!d_unhashed(target))
2914: ___d_drop(target);
2915:
2916: /* ... and switch them in the tree */
2917: dentry->d_parent = target->d_parent;
2918: if (!exchange) {
2919: copy_name(dentry, target);
2920: target->d_hash.pprev = NULL;
2921: dentry->d_parent->d_lockref.count++;
2922: if (dentry != old_parent) /* wasn't IS_ROOT */
2923: WARN_ON(!--old_parent->d_lockref.count);
2924: } else {
2925: target->d_parent = old_parent;
2926: swap_names(dentry, target);
2927: if (!hlist_unhashed(&target->d_sib))
2928: __hlist_del(&target->d_sib);
2929: hlist_add_head(&target->d_sib, &target->d_parent->d_children);
2930: __d_rehash(target);
2931: fsnotify_update_flags(target);
2932: }
2933: if (!hlist_unhashed(&dentry->d_sib))
2934: __hlist_del(&dentry->d_sib);
2935: hlist_add_head(&dentry->d_sib, &dentry->d_parent->d_children);
2936: __d_rehash(dentry);
2937: fsnotify_update_flags(dentry);
2938: fscrypt_handle_d_move(dentry);
2939:
2940: write_seqcount_end(&target->d_seq);
2941: write_seqcount_end(&dentry->d_seq);
2942:
2943: if (dir)
2944: end_dir_add(dir, n, d_wait);
2945:
2946: if (dentry->d_parent != old_parent)
2947: spin_unlock(&dentry->d_parent->d_lock);
2948: if (dentry != old_parent)
2949: spin_unlock(&old_parent->d_lock);
2950: spin_unlock(&target->d_lock);
2951: spin_unlock(&dentry->d_lock);
2952: }
SourceFile:fs/dcache.c]
|
| 37/2 |
2026/01/15 01:22 |
llm |
expert |
0m
Model:
Thoughts: **Analyzing Dentry Unhashing and Rehashing in `__d_move`**
Okay, so I'm digging into this `__d_move` function and trying to understand the dentry manipulations. It looks like the `dentry` gets unhashed with `___d_drop(dentry)` and then rehashed. My initial thought was that during the unhashing, the `pprev` pointer would get set to NULL, which is crucial for determining if the dentry is actually unhashed.
I went back to my notes and my previous `codesearch-definition-source` search for `___d_drop`, but I couldn't find that nullification of the `pprev` pointer there. Hold on, I'm misremembering. It's actually in `__d_drop` that `pprev` gets explicitly nulled out. I found the code:
```c
571: void __d_drop(struct dentry *dentry)
572: {
573: if (!d_unhashed(dentry)) {
574: ___d_drop(dentry);
575: dentry->d_hash.pprev = NULL;
576: write_seqcount_invalidate(&dentry->d_seq);
577: }
578: }
```
The problem is that `__d_move` calls `___d_drop(dentry)` directly, at line 2912, *not* `__d_drop`. And, as I look closer, `___d_drop` doesn't seem to set `pprev` to NULL. That's a potential issue. If `pprev` *isn't* set to NULL, then `d_unhashed` might return `FALSE` even when the dentry is effectively out of the hash list. That's not what we want.
Okay, I need to check something now. Let me see... perhaps `__hlist_bl_del`, which is called within `___d_drop`, *itself* doesn't NULL out `pprev` directly. I need to verify how that works. Let's look at the implementation of `__hlist_bl_del`.
|
| 38/2 |
2026/01/15 01:22 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:__hlist_bl_del SourceFile:include/linux/list_bl.h]
Results: map[Missing:false SourceCode: 114: static inline void __hlist_bl_del(struct hlist_bl_node *n)
115: {
116: struct hlist_bl_node *next = n->next;
117: struct hlist_bl_node **pprev = n->pprev;
118:
119: LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
120:
121: /* pprev may be `first`, so be careful not to lose the lock bit */
122: WRITE_ONCE(*pprev,
123: (struct hlist_bl_node *)
124: ((unsigned long)next |
125: ((unsigned long)*pprev & LIST_BL_LOCKMASK)));
126: if (next)
127: next->pprev = pprev;
128: }
SourceFile:include/linux/list_bl.h]
|
| 39/2 |
2026/01/15 01:22 |
llm |
expert |
0m
Model:
Thoughts: **Analyzing Potential Data Races in the Dentry Hashing Mechanism**
Okay, so I'm looking into this dentry hashing issue, and it seems like the key lies in how dentries are moved and rehashed. My initial thought was that `__hlist_bl_del` doesn't nullify `n->pprev`, potentially leaving a stale pointer. This, in turn, would cause `___d_drop` to leave `n->pprev` pointing to the old list. However, if `n->pprev` isn't NULL, then `d_unhashed` will return FALSE, which explains why `__d_drop` explicitly nullifies it at line 575.
Now, things get more interesting with `__d_move`. It calls `___d_drop` and then proceeds with additional operations. I see it explicitly sets `target->d_hash.pprev = NULL` within `__d_move` (line 2920) but there are other calls to d_rehash. Specifically, for `dentry` there's a call to `__d_rehash(dentry)`. So it seems a dentry is first unhashed via `___d_drop(dentry)` and then rehashed via `__d_rehash(dentry)`.
The concern I have here is whether `__d_lookup_rcu` could potentially see a dentry in this "intermediate" state during the move. I realize `__d_move` is protected by `rename_lock`, a seqlock, but `__d_lookup_rcu` doesn't check this lock. The comments explicitly mention this at line 2316. Instead, it uses `d_seq` for each dentry. This seems to be the critical area of concern.
`__d_lookup_rcu` iterates through a hash bucket. This presents a potential race condition if a dentry is moved from bucket A to bucket B, as the reader might be in either bucket during the move.
If `__d_rehash` is called, it is adding an entry to a bucket. If it's the same bucket it was in before, then it's effectively just moving it to the head of that bucket.
The data race, as I now understand it, is centered on `dentry->d_hash.pprev`. `__d_lookup_rcu` is reading this field, while `__d_rehash` is writing it.
Specifically, in `__d_rehash` within the `hlist_bl_add_head_rcu` function: `n->pprev = &h->first;`. This line is where the write occurs. If `n->pprev` was NULL before, it now becomes non-NULL. But if `n->pprev` wasn't NULL before, it's now updated.
Thinking about it more, if it wasn't NULL beforehand, `d_unhashed` would have returned false anyway.
Now, the report mentions a write by task 10110 in `__d_rehash` via `d_add` via `simple_lookup`. I need to look closer at `simple_lookup` to understand how this is being triggered.
|
| 40/2 |
2026/01/15 01:22 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:simple_lookup SourceFile:fs/libfs.c]
Results: map[Missing:false SourceCode: 64: /*
65: * Lookup the data. This is trivial - if the dentry didn't already
66: * exist, we know it is negative. Set d_op to delete negative dentries.
67: */
68: struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
69: {
70: if (dentry->d_name.len > NAME_MAX)
71: return ERR_PTR(-ENAMETOOLONG);
72: if (!dentry->d_op && !(dentry->d_flags & DCACHE_DONTCACHE)) {
73: spin_lock(&dentry->d_lock);
74: dentry->d_flags |= DCACHE_DONTCACHE;
75: spin_unlock(&dentry->d_lock);
76: }
77: if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))
78: return NULL;
79:
80: d_add(dentry, NULL);
81: return NULL;
82: }
SourceFile:fs/libfs.c]
|
| 41/2 |
2026/01/15 01:22 |
llm |
expert |
0m
Model:
Thoughts: **Analysis of a Potential Data Race in the Dentry Cache**
Okay, so I'm diving into this KCSAN report about a possible data race within the dentry cache, specifically around `d_unhashed` and `__d_lookup_rcu`. The initial clue is that `d_add` calls `__d_add`, which in turn calls `__d_rehash`, modifying the dentry's hash list entry (`pprev`). Meanwhile, `__d_lookup_rcu` is concurrently iterating through the hash table. The heart of the issue seems to be whether the reader in `__d_lookup_rcu` might see a partially updated `pprev` during the add operation.
My initial thought is that the race, if it exists, is likely benign. A new dentry's `pprev` is initially `NULL`, and `__d_rehash` sets it when adding it to the hash list. The reader, using RCU, should be shielded from seeing an incomplete write thanks to `rcu_assign_pointer` and `rcu_dereference`. However, KCSAN is flagging this because it sees a store/load pair that it doesn't recognize as explicitly synchronized. It's essentially a *false positive* stemming from KCSAN being overly strict, and lacking understanding of RCU ordering when there are no explicit `READ_ONCE/WRITE_ONCE` barriers.
The main concern is, what if the reader sees an inconsistent value, or *garbage*? Well, `d_unhashed` simply checks if `pprev` is `NULL`. If it sees `NULL` when it shouldn't, it just skips that dentry; a *false negative*. That means it will simply miss a dentry that has just been added. But the RCU-walk can retry. This is a common pattern in the kernel's RCU code—missing an entry is recoverable, so skipping it leads to the same outcome as if it wasn't present at all, requiring a subsequent retry.
But what if the reader gets *garbage* from that read? Because it's a pointer-sized write, it's atomic on all supported architectures. So it should see either `NULL` or a valid pointer, but not something in between.
The kernel *can* skip the check due to a possible race window between finding the dentry before it is unhashed and loading the sequence counter after unhashing is complete. The dentry, however, must not be used due to a race between the unhash and the sequence counter invalidation.
However, the comment states, "As tempting as it is, we *can't* skip it" because it is a race window between finding the dentry before it is unhashed and loading the sequence counter after unhashing is complete. In other words, if it is in the process of being removed or added, we *MUST* do the check to ensure we are using a consistent dentry.
Then, there is the potential *crash* risk. The reader could read an inconsistent value from `pprev`. However, in the case of `d_unhashed`, all it's doing is checking if a pointer is `NULL` or not, so there is no potential for a crash.
My reasoning is the RCU mechanism has a design that mitigates the race. The reader, if it picks up a "wrong" value (e.g., dentry is "unhashed" when being added, or vice versa), the caller will catch it via a sequence count check. This is an artifact of a dentry that has changed since the scan. However, a missed dentry is no problem since the RCU can retry to see the *correct* result or, if a race occurred, fall back to the non-RCU-walk. The race is *benign*.
I need to confirm this, though. Most of these data races in the dcache are known and typically are benign because they involve statistical counters, flags that are checked/verified, or RCU-protected structures, where a slightly outdated value is acceptable.
After reviewing the code, I see that `d_unhashed` is used as an *optimization* during the RCU-walk. If it returns a "wrong" value, it simply means we miss a dentry that was just added. Given that the dentry was *just* added, the effect is almost the same as if we had searched a few nanoseconds earlier and didn't find it yet. The caller will retry anyway if they need to find it urgently.
Now, let's look for evidence. I've found that the kernel is *already* adding `READ_ONCE`/`WRITE_ONCE` for synchronization purposes. The presence of `READ_ONCE`/`WRITE_ONCE` is the best practice and
|
| 42/2 |
2026/01/15 01:23 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:__d_lookup SourceFile:fs/dcache.c]
Results: map[Missing:false SourceCode:2391: /**
2392: * __d_lookup - search for a dentry (racy)
2393: * @parent: parent dentry
2394: * @name: qstr of name we wish to find
2395: * Returns: dentry, or NULL
2396: *
2397: * __d_lookup is like d_lookup, however it may (rarely) return a
2398: * false-negative result due to unrelated rename activity.
2399: *
2400: * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2401: * however it must be used carefully, eg. with a following d_lookup in
2402: * the case of failure.
2403: *
2404: * __d_lookup callers must be commented.
2405: */
2406: struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2407: {
2408: unsigned int hash = name->hash;
2409: struct hlist_bl_head *b = d_hash(hash);
2410: struct hlist_bl_node *node;
2411: struct dentry *found = NULL;
2412: struct dentry *dentry;
2413:
2414: /*
2415: * Note: There is significant duplication with __d_lookup_rcu which is
2416: * required to prevent single threaded performance regressions
2417: * especially on architectures where smp_rmb (in seqcounts) are costly.
2418: * Keep the two functions in sync.
2419: */
2420:
2421: /*
2422: * The hash list is protected using RCU.
2423: *
2424: * Take d_lock when comparing a candidate dentry, to avoid races
2425: * with d_move().
2426: *
2427: * It is possible that concurrent renames can mess up our list
2428: * walk here and result in missing our dentry, resulting in the
2429: * false-negative result. d_lookup() protects against concurrent
2430: * renames using rename_lock seqlock.
2431: *
2432: * See Documentation/filesystems/path-lookup.txt for more details.
2433: */
2434: rcu_read_lock();
2435:
2436: hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2437:
2438: if (dentry->d_name.hash != hash)
2439: continue;
2440:
2441: spin_lock(&dentry->d_lock);
2442: if (dentry->d_parent != parent)
2443: goto next;
2444: if (d_unhashed(dentry))
2445: goto next;
2446:
2447: if (!d_same_name(dentry, parent, name))
2448: goto next;
2449:
2450: dentry->d_lockref.count++;
2451: found = dentry;
2452: spin_unlock(&dentry->d_lock);
2453: break;
2454: next:
2455: spin_unlock(&dentry->d_lock);
2456: }
2457: rcu_read_unlock();
2458:
2459: return found;
2460: }
SourceFile:fs/dcache.c]
|
| 43/2 |
2026/01/15 01:23 |
llm |
expert |
0m
Model:
Thoughts: **Deconstructing the Dentry Lookup Conundrum**
Alright, let's break this down. I'm seeing that `__d_lookup` includes the `d_unhashed` check, and it's also nested within `hlist_bl_for_each_entry_rcu`. The interesting bit is that `__d_lookup` grabs `dentry->d_lock` *before* calling `d_unhashed`. `__d_add`, the writer in this scenario, also uses this same lock. So, effectively, `__d_lookup` and `__d_add` are synchronized by `d_lock` to ensure that the `d_unhashed` state is checked.
Now, `__d_lookup_rcu` – the one that's the subject of this observation – *doesn't* take `d_lock`. That's expected, given its lock-free nature. This tells me that the `d_unhashed` status must be pretty critical, otherwise, why even bother with a lock in the regular `__d_lookup`? Yet, `__d_lookup_rcu` is allowed to miss some of them, in a controlled manner.
The crucial clue is in the comment within `__d_lookup_rcu`: "...we *can't* skip it because of a race window..." It means we *must* perform the `d_unhashed` check. If we stumble upon a dentry that's in the process of being unhashed, we need to detect that. Otherwise, we might return a dentry that's invalid. But we still have the sequence check as a final safeguard.
So, the check itself *might* be racy. However, as long as it doesn't cause a crash and the sequence count check is the ultimate arbiter of correctness, this is fine.
Now, let's drill down on `d_unhashed`. It examines `pprev`. When rehashing, `__d_rehash` does the following: `n->pprev = &h->first;`. What if there's a writer interruption between setting `n->next` and `n->pprev`? The crucial point is that the reader doesn't even "see" `n` until `h->first` has been updated. The race is between the writer setting `n->pprev` and the reader reading it after already "seeing" `n` from `h->first`. This implies either store-release/load-acquire semantics aren't quite strong enough, or this is simply KCSAN flagging the absence of `READ_ONCE`/`WRITE_ONCE`.
Given it's in the dcache and is a known racy path, that relies on seqlocks/RCU, this is almost certainly benign.
Actually, one last thought. `__d_lookup_rcu` is called in RCU-walk mode. If it fails, it retries in non-RCU-walk mode, which calls `d_lookup`, which then invokes `__d_lookup`, which *does* take `d_lock`. This means that any races encountered during the RCU walk are resolved on retry.
One final check, the `hlist_bl_node` structure. Gotta cover all the bases, right?
|
| 44/2 |
2026/01/15 01:23 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_node SourceFile:include/linux/list_bl.h]
Results: map[Missing:true SourceCode: SourceFile:]
|
| 45/2 |
2026/01/15 01:23 |
llm |
expert |
0m
Model:
Thoughts: Okay, here's the summary, assuming I'm an expert in the field and encountering the word "struct" in a relevant context:
**Initial Assessment: It's a Struct**
Right, so we're looking at something that's being described as a "struct." The immediate implication is that we're dealing with a data structure. Depending on the language or domain, this could mean a *lot* of things, but fundamentally, it means an aggregate data type. My mind immediately starts filtering through possible implementations: C-style structs? Maybe a Python namedtuple analog? Is it something more specialized, like a database schema definition, or a configuration object? It's a broad term that needs immediate clarification, but the core characteristic is clear: it's a grouped collection of data elements. I need more context to understand how the struct is *used* to understand it fully.
|
| 46/2 |
2026/01/15 01:23 |
tool |
codesearch-file-index |
0m
Model:
Args: map[SourceFile:include/linux/list_bl.h]
Results: map[Entities:[map[Kind:function Name:INIT_HLIST_BL_NODE] map[Kind:function Name:__hlist_bl_del] map[Kind:function Name:hlist_bl_add_before] map[Kind:function Name:hlist_bl_add_behind] map[Kind:function Name:hlist_bl_add_head] map[Kind:function Name:hlist_bl_del] map[Kind:function Name:hlist_bl_del_init] map[Kind:function Name:hlist_bl_empty] map[Kind:function Name:hlist_bl_first] map[Kind:function Name:hlist_bl_is_locked] map[Kind:function Name:hlist_bl_lock] map[Kind:function Name:hlist_bl_set_first] map[Kind:function Name:hlist_bl_unhashed] map[Kind:function Name:hlist_bl_unlock]] Missing:false]
|
| 47/2 |
2026/01/15 01:23 |
llm |
expert |
0m
Model:
Thoughts: Okay, let's see...
**Investigating a Missing Definition**
Hmm, I can't seem to find this thing in my standard entities list. Did I overlook it, or is it defined elsewhere? My first instinct is to check another file, maybe something was missed.
Ah, right, often times these kinds of things are declared in `include/linux/list_bl.h`. I'll probably find it there.
Hold on, let me just quickly double-check using `codesearch-definition-source` on `hlist_bl_node`. That should nail down its definition quickly. I am sure I will find it now.
|
| 48/2 |
2026/01/15 01:23 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_node SourceFile:include/linux/list_bl.h]
Results: map[Missing:true SourceCode: SourceFile:]
|
| 49/2 |
2026/01/15 01:23 |
llm |
expert |
0m
Model:
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Considering the Header Files**
Alright, let's start with the basics. If I'm trying to understand the fundamental data types and definitions within the Linux kernel, the first place my mind jumps to is undoubtedly `include/linux/types.h`. That's the likely location for the core definitions, structures, and possibly typedefs that form the bedrock for much of the kernel's code. It's the logical starting point for understanding how the kernel thinks about the most elementary data structures, particularly if I'm looking for architecture-specific integer sizes, pointer definitions, or common bitfield structures. Time to dig in there.
|
| 50/2 |
2026/01/15 01:23 |
tool |
codesearch-definition-source |
0m
Model:
Args: map[IncludeLines:true Name:hlist_bl_node SourceFile:include/linux/types.h]
Results: map[Missing:true SourceCode: SourceFile:]
|
| 51/2 |
2026/01/15 01:23 |
llm |
expert |
0m
Model:
Thoughts: **Benign Race in Kernel Dentry Lookup**
Okay, I've got this. That `hlist_bl_node` struct… it's just a standard doubly-linked list node where `pprev` points to the `next` pointer of the previous element. Simple enough.
The core of the issue is a race condition. It's between a reader checking if a dentry is unhashed (by examining `pprev` for NULL) and a writer adding the dentry to a hash list (by assigning `pprev = &something`).
The conclusion is that, because the reader *should* see a non-NULL `pprev` if it has already found the dentry, if it encounters NULL due to a race, the reader will simply skip the dentry. This results in a false negative in the RCU-walk. The important thing is that the system correctly falls back to a non-RCU-walk, so there is no memory corruption or crash, and the race condition centers on a single pointer field.
Given that this is a classic example of a benign data race in the kernel's fast lookup path, I'll mark it as benign with confidence.
Now, let me just double-check… is there any architecture where reading `pprev` could be dangerous? Nope, it's just a pointer. Could the compiler be doing anything sneaky with the `!h->pprev` check? Unlikely.
The use of `hlist_bl_unhashed` without `READ_ONCE` is common in the kernel. I've seen similar reports before and they were usually dismissed as benign or fixed with `READ_ONCE` just to satisfy KCSAN. Since my task is to determine if this is benign, I'll say it is.
Wait, let's look at `hlist_bl_add_head_rcu` again.
Line 84 is where it becomes visible.
If KCSAN says they are concurrent, it means the reader IS at line 53 (of `list_bl.h`) while the writer IS at line 81 (of `rculist_bl.h`).
But how can the reader be at the dentry `n` if it's still at line 81?
Ah! Maybe it's a DIFFERENT bucket or the dentry was already in the list but being rehashed to a DIFFERENT list?
No, `simple_lookup` creates a NEW dentry.
Wait, `dentry` passed to `simple_lookup` is new.
Wait, `lookup_one_qstr_excl` calls `d_alloc_parallel` or `d_alloc`.
If it's a new dentry, it's not in any list.
Is it possible that `dentry` was ALREADY hashed?
In `simple_lookup`, `dentry` is passed as an argument.
`lookup_one_qstr_excl` calls `lookup_slow` which calls `dir->i_op->lookup`.
`lookup` (e.g. `simple_lookup`) gets a newly created dentry that is not yet hashed.
Wait, I might have found a subtle point.
Is it possible that KCSAN is reporting a race on a DIFFERENT dentry that happens to be at the same address?
No, `0xffff88810051b550` is a specific address.
Wait! What if the reader is iterating through bucket B, and the writer is adding a dentry to bucket B?
The reader reads the head of bucket B, sees some dentry `D1`, follows its `next` to `D2`, etc.
The writer adds `D_new` to the head of bucket B.
Now bucket B's head points to `D_new`, and `D_new->next` points to `D1`.
If the reader starts its iteration NOW, it sees `D_new`.
It reads `D_new->pprev`. This is where the race happens.
Yes, this makes perfect sense. The reader sees the dentry because it just became the head of the list.
And because the store-release in `hlist_bl_set_first_rcu` and the load-acquire in `hlist_bl_first_rcu` are supposed to order the `pprev` store, the reader should see the updated `pprev`.
KCSAN flags it because the `pprev` store and load themselves are not marked atomic.
This confirms it's a benign race (ordered by RCU, but using plain accesses).
Actually, I should check if `hlist_bl_unhashed` is used anywhere else where it might be more critical. In `__
|
| 52/2 |
2026/01/15 01:24 |
tool |
set-results |
0m
Model:
Args: map[Benign:true Confident:true]
Results: map[Benign:true Confident:true]
|
| 53/2 |
2026/01/15 01:24 |
llm |
expert |
0m
Model:
Thoughts: Here's a breakdown of my thought process regarding this data race, specifically through the lens of my expertise:
**Benign Data Race in the Dentry Cache**
Alright, after reviewing the code and the KCSAN report, I'm comfortable concluding that this data race is, in fact, benign. My analysis focuses on the interaction between `__d_lookup_rcu` and `__d_rehash`, specifically concerning the `dentry->d_hash.pprev` field.
The key here is understanding the RCU synchronization. `__d_lookup_rcu` is the reader; it uses `d_unhashed()` which checks if `pprev` is NULL. Meanwhile, `__d_rehash` is the writer, responsible for adding the dentry to a hash list and, importantly, *then* setting `pprev` to a non-NULL value. The race exists because `__d_lookup_rcu` loads the field directly, while `__d_rehash` does a store.
The potential issue is that the reader might observe a stale value (NULL) before the write completes. However, this is where RCU comes in. The code ensures that the dentry is only visible to the RCU-based list iterator *after* `pprev` is set. This is achieved via a store-release in `hlist_bl_set_first_rcu`. So, if the reader sees NULL, it's a false negative. The code is structured to handle this scenario – the reader will simply skip that dentry and fall back to a non-RCU walk. This is a deliberate design choice for performance. It's not ideal, and technically, KCSAN is correct to report it as a race condition. But because the system is designed to handle this possibility, it's considered safe.
Given the existing RCU synchronization and the graceful fallback mechanism, and with my experience with the dcache, I would consider this a common and acceptable pattern in this part of the code. We're trading a possible false negative for increased lookup performance under typical workloads, and this trade-off is often considered beneficial to the overall performance of the filesystem.
|