| 0/0 |
2026/02/24 18:32 |
flow |
assessment-kcsan |
10m
Results: map[Benign:true Confident:true Explanation:The data race occurs between `__d_drop()` and `fast_dput()`. Specifically, it is a race on the `dentry->d_hash.pprev` pointer, which is used to determine if a dentry is "hashed" (i.e., reachable via name lookups).
### Analysis
1. **The Write:** In `__d_drop()`, the dentry is removed from the hash table. After the removal, `dentry->d_hash.pprev` is set to `NULL` to mark it as unhashed. This is done while holding the dentry's `d_lock` and the hash bucket's lock.
```c
// fs/dcache.c:576
dentry->d_hash.pprev = NULL;
```
2. **The Read:** In `fast_dput()`, which is an optimistic lockless path for `dput()`, the kernel calls `retain_dentry(dentry, false)` without holding the `d_lock`. This function checks if the dentry should be kept in the LRU cache or killed. One of the checks is `d_unhashed(dentry)`, which reads `dentry->d_hash.pprev`.
```c
// include/linux/list_bl.h:54 (via d_unhashed)
return !h->pprev;
```
3. **The Race:** If `fast_dput()` reads `pprev` while `__d_drop()` is executing, it might see a stale non-NULL value. If it does, it might decide to retain the dentry (leaving it on the LRU list with a zero reference count) instead of proceeding to the locked path where the dentry would be killed (`dentry_kill()`).
### Why it is Benign
* **Intentional Design:** The comment in `retain_dentry()` explicitly acknowledges that these lockless checks are not "stable":
> "In case we aren't locked, these predicates are not 'stable'. However, it is sufficient that at some point after we dropped the reference the dentry was hashed and the flags had the proper value."
* **Safety:** The optimistic path in `fast_dput()` is conservative. If it makes a "mistake" due to a stale read, the worst consequence is that an unhashed dentry (which is no longer reachable via lookups) stays on the LRU list for a while longer. It will eventually be reclaimed by the dcache shrinker when there is memory pressure.
* **Correctness:** If `retain_dentry()` returns `false` (either because it correctly saw the dentry was unhashed or because of other flags), `fast_dput()` falls back to a locked path. In the locked path, it re-checks the state while holding `d_lock`, ensuring a consistent view.
* **Atomicity:** On the reported architecture (x86_64), the 8-byte pointer access is atomic, so there is no risk of reading a torn or garbage pointer value.
In summary, this is a classic optimistic concurrency control pattern used in the VFS for performance. The race is harmless as it only affects the timing of dentry reclamation, not the functional correctness of the filesystem.
Final verdict: **Benign**]
|
| 1/1 |
2026/02/24 18:32 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/src/2642fa7629c5424fce8f203f307b063c2f008d9f]
|
| 2/1 |
2026/02/24 18:32 |
action |
kernel-builder |
3m
Results: map[KernelObj:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/build/8a8d223043183454fcb320f80eb129c08706d7cc]
|
| 3/1 |
2026/02/24 18:36 |
action |
codesearch-prepare |
3m
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/02/24 18:40 |
agent |
expert |
2m
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.
Also consider races that happen at the same time with the given one.
If there is no synchronization in between, other memory accesses in the involved threads
race with each other if they access the same memory. For example, if both threads execute:
some_struct->have_elements = true;
list_add(new_node, &some_struct->list_head);
the race on some_struct->have_elements may appear benign, however it also implies there
is a race on some_struct->list_head which is not benign, since the list is not thread-safe.
Take into account that on 32-bit systems 64-bit memory accesses may be split into two accesses,
and thus even with a conservative compiler may not be fully atomic. However, such races may
still be benign depending on how writes are done, and how read data is used.
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.
Prefer calling several tools at the same time to save round-trips.
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_invalidate / fast_dput
write to 0xffff888110ac1cd0 of 8 bytes by task 10725 on cpu 1:
__d_drop fs/dcache.c:576 [inline]
d_invalidate+0x5b/0x200 fs/dcache.c:1691
lookup_open fs/namei.c:4378 [inline]
open_last_lookups fs/namei.c:4549 [inline]
path_openat+0x1670/0x23c0 fs/namei.c:4793
do_filp_open+0x15d/0x280 fs/namei.c:4823
do_sys_openat2+0xa6/0x150 fs/open.c:1430
do_sys_open fs/open.c:1436 [inline]
__do_sys_openat fs/open.c:1452 [inline]
__se_sys_openat fs/open.c:1447 [inline]
__x64_sys_openat+0xf2/0x120 fs/open.c:1447
x64_sys_call+0x2b07/0x3000 arch/x86/include/generated/asm/syscalls_64.h:258
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xc0/0x2a0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888110ac1cd0 of 8 bytes by task 10721 on cpu 0:
hlist_bl_unhashed include/linux/list_bl.h:54 [inline]
d_unhashed include/linux/dcache.h:366 [inline]
retain_dentry fs/dcache.c:753 [inline]
fast_dput+0x5f/0x2c0 fs/dcache.c:849
dput+0x24/0x60 fs/dcache.c:924
end_dirop fs/namei.c:2898 [inline]
end_creating include/linux/namei.h:126 [inline]
end_creating_path fs/namei.c:4931 [inline]
do_mknodat+0x2c2/0x4f0 fs/namei.c:5062
__do_sys_mknodat fs/namei.c:5080 [inline]
__se_sys_mknodat fs/namei.c:5077 [inline]
__x64_sys_mknodat+0x5e/0x70 fs/namei.c:5077
x64_sys_call+0x2b33/0x3000 arch/x86/include/generated/asm/syscalls_64.h:260
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0xc0/0x2a0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0xffff88823741a658 -> 0x0000000000000000
Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 10721 Comm: syz.8.2163 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 occurs between `__d_drop()` and `fast_dput()`. Specifically, it is a race on the `dentry->d_hash.pprev` pointer, which is used to determine if a dentry is "hashed" (i.e., reachable via name lookups).
### Analysis
1. **The Write:** In `__d_drop()`, the dentry is removed from the hash table. After the removal, `dentry->d_hash.pprev` is set to `NULL` to mark it as unhashed. This is done while holding the dentry's `d_lock` and the hash bucket's lock.
```c
// fs/dcache.c:576
dentry->d_hash.pprev = NULL;
```
2. **The Read:** In `fast_dput()`, which is an optimistic lockless path for `dput()`, the kernel calls `retain_dentry(dentry, false)` without holding the `d_lock`. This function checks if the dentry should be kept in the LRU cache or killed. One of the checks is `d_unhashed(dentry)`, which reads `dentry->d_hash.pprev`.
```c
// include/linux/list_bl.h:54 (via d_unhashed)
return !h->pprev;
```
3. **The Race:** If `fast_dput()` reads `pprev` while `__d_drop()` is executing, it might see a stale non-NULL value. If it does, it might decide to retain the dentry (leaving it on the LRU list with a zero reference count) instead of proceeding to the locked path where the dentry would be killed (`dentry_kill()`).
### Why it is Benign
* **Intentional Design:** The comment in `retain_dentry()` explicitly acknowledges that these lockless checks are not "stable":
> "In case we aren't locked, these predicates are not 'stable'. However, it is sufficient that at some point after we dropped the reference the dentry was hashed and the flags had the proper value."
* **Safety:** The optimistic path in `fast_dput()` is conservative. If it makes a "mistake" due to a stale read, the worst consequence is that an unhashed dentry (which is no longer reachable via lookups) stays on the LRU list for a while longer. It will eventually be reclaimed by the dcache shrinker when there is memory pressure.
* **Correctness:** If `retain_dentry()` returns `false` (either because it correctly saw the dentry was unhashed or because of other flags), `fast_dput()` falls back to a locked path. In the locked path, it re-checks the state while holding `d_lock`, ensuring a consistent view.
* **Atomicity:** On the reported architecture (x86_64), the 8-byte pointer access is atomic, so there is no risk of reading a torn or garbage pointer value.
In summary, this is a classic optimistic concurrency control pattern used in the VFS for performance. The race is harmless as it only affects the timing of dentry reclamation, not the functional correctness of the filesystem.
Final verdict: **Benign**
|
| 5/2 |
2026/02/24 18:40 |
llm |
expert |
0m
Model:
Tokens:
input: 4090
output: 119
thoughts: 301
Thoughts: **Analyzing a Data Race in the Dentry Cache**
Okay, so the report indicates a classic data race, and it looks like it's happening between `__d_drop` (triggered by `d_invalidate`) and `fast_dput`. The race is centered around memory address `0xffff888110ac1cd0`, which, given my knowledge of the kernel, I immediately recognize as likely residing within a `dentry` structure. Specifically, it seems to be related to the `hlist_bl_unhashed` or `d_unhashed` fields. These are key for managing dentry hash lists and unhashed entries within the dcache.
Let's dive into the stack traces to get the context. `__d_drop` is being called from `d_invalidate` in `fs/dcache.c`, and that's the write side of the race. On the read side, the trace shows `hlist_bl_unhashed` and `d_unhashed` from the include files, then `retain_dentry` in `fs/dcache.c`, ultimately leading to `fast_dput` also in `fs/dcache.c`.
The key clue is the change in value: it went from `0xffff88823741a658` to `0x0000000000000000`. That suggests a pointer is being nulled out. This is classic dentry lifecycle manipulation. Something is unlinking the dentry from a hash list or marking it as unhashed, which is a key part of the garbage collection process, while another part of the system is still trying to access it through the same pointer. Time to get the source code up at those locations and see if the locking is where it should be. The devil is in the details, as always.
|
| 6/2 |
2026/02/24 18:40 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:__d_drop]
Results: map[SourceCode: 572: void __d_drop(struct dentry *dentry)
573: {
574: if (!d_unhashed(dentry)) {
575: ___d_drop(dentry);
576: dentry->d_hash.pprev = NULL;
577: write_seqcount_invalidate(&dentry->d_seq);
578: }
579: }
SourceFile:fs/dcache.c]
|
| 7/2 |
2026/02/24 18:40 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:fast_dput]
Results: map[SourceCode: 803: /*
804: * Try to do a lockless dput(), and return whether that was successful.
805: *
806: * If unsuccessful, we return false, having already taken the dentry lock.
807: * In that case refcount is guaranteed to be zero and we have already
808: * decided that it's not worth keeping around.
809: *
810: * The caller needs to hold the RCU read lock, so that the dentry is
811: * guaranteed to stay around even if the refcount goes down to zero!
812: */
813: static inline bool fast_dput(struct dentry *dentry)
814: {
815: int ret;
816:
817: /*
818: * try to decrement the lockref optimistically.
819: */
820: ret = lockref_put_return(&dentry->d_lockref);
821:
822: /*
823: * If the lockref_put_return() failed due to the lock being held
824: * by somebody else, the fast path has failed. We will need to
825: * get the lock, and then check the count again.
826: */
827: if (unlikely(ret < 0)) {
828: spin_lock(&dentry->d_lock);
829: if (WARN_ON_ONCE(dentry->d_lockref.count <= 0)) {
830: spin_unlock(&dentry->d_lock);
831: return true;
832: }
833: dentry->d_lockref.count--;
834: goto locked;
835: }
836:
837: /*
838: * If we weren't the last ref, we're done.
839: */
840: if (ret)
841: return true;
842:
843: /*
844: * Can we decide that decrement of refcount is all we needed without
845: * taking the lock? There's a very common case when it's all we need -
846: * dentry looks like it ought to be retained and there's nothing else
847: * to do.
848: */
849: if (retain_dentry(dentry, false))
850: return true;
851:
852: /*
853: * Either not worth retaining or we can't tell without the lock.
854: * Get the lock, then. We've already decremented the refcount to 0,
855: * but we'll need to re-check the situation after getting the lock.
856: */
857: spin_lock(&dentry->d_lock);
858:
859: /*
860: * Did somebody else grab a reference to it in the meantime, and
861: * we're no longer the last user after all? Alternatively, somebody
862: * else could have killed it and marked it dead. Either way, we
863: * don't need to do anything else.
864: */
865: locked:
866: if (dentry->d_lockref.count || retain_dentry(dentry, true)) {
867: spin_unlock(&dentry->d_lock);
868: return true;
869: }
870: return false;
871: }
SourceFile:fs/dcache.c]
|
| 8/2 |
2026/02/24 18:40 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/dcache.h IncludeLines:true Name:d_unhashed]
Results: map[SourceCode: 358: /**
359: * d_unhashed - is dentry hashed
360: * @dentry: entry to check
361: *
362: * Returns true if the dentry passed is not currently hashed.
363: */
364: static inline int d_unhashed(const struct dentry *dentry)
365: {
366: return hlist_bl_unhashed(&dentry->d_hash);
367: }
SourceFile:include/linux/dcache.h]
|
| 9/2 |
2026/02/24 18:40 |
llm |
expert |
0m
Model:
Tokens:
input: 6260
output: 84
thoughts: 152
Thoughts: **Analyzing a File System Dentry Race Condition**
Okay, so I'm diving into a potential race condition within the dentry cache, specifically looking at how dentries are handled in the file system. My initial point of interest is the `__d_drop` function, specifically at line 576 in `fs/dcache.c`, where `dentry->d_hash.pprev = NULL;` is being set. That's a crucial point, marking the dentry as unhashed.
Now, where's the potential conflict? The read is coming from `hlist_bl_unhashed` at `include/linux/list_bl.h:54`. `hlist_bl_unhashed` is being called via the `d_unhashed` macro (located at `include/linux/dcache.h:366`), which, in turn, is called by `retain_dentry` (at `fs/dcache.c:753`) and then finally by `fast_dput` at `fs/dcache.c:849`. This chain indicates a common read path that might be interacting with the write operation in `__d_drop`. I've got to analyze `hlist_bl_unhashed` and `retain_dentry` very closely to see how the unhashed state is used and whether timing could create a problem. I need to understand if the read could occur *before* the write is complete, creating a period of vulnerability. This looks like a cache coherency problem, likely related to refcounting or dentry lifecycle management. I'll focus on these two functions and their interactions in the context of the dentry lifecycle. I suspect there is a race there somewhere.
|
| 10/2 |
2026/02/24 18:40 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/list_bl.h IncludeLines:true Name:hlist_bl_unhashed]
Results: map[SourceCode: 52: static inline bool hlist_bl_unhashed(const struct hlist_bl_node *h)
53: {
54: return !h->pprev;
55: }
SourceFile:include/linux/list_bl.h]
|
| 11/2 |
2026/02/24 18:40 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:retain_dentry]
Results: map[SourceCode: 734: /*
735: * Decide if dentry is worth retaining. Usually this is called with dentry
736: * locked; if not locked, we are more limited and might not be able to tell
737: * without a lock. False in this case means "punt to locked path and recheck".
738: *
739: * In case we aren't locked, these predicates are not "stable". However, it is
740: * sufficient that at some point after we dropped the reference the dentry was
741: * hashed and the flags had the proper value. Other dentry users may have
742: * re-gotten a reference to the dentry and change that, but our work is done -
743: * we can leave the dentry around with a zero refcount.
744: */
745: static inline bool retain_dentry(struct dentry *dentry, bool locked)
746: {
747: unsigned int d_flags;
748:
749: smp_rmb();
750: d_flags = READ_ONCE(dentry->d_flags);
751:
752: // Unreachable? Nobody would be able to look it up, no point retaining
753: if (unlikely(d_unhashed(dentry)))
754: return false;
755:
756: // Same if it's disconnected
757: if (unlikely(d_flags & DCACHE_DISCONNECTED))
758: return false;
759:
760: // ->d_delete() might tell us not to bother, but that requires
761: // ->d_lock; can't decide without it
762: if (unlikely(d_flags & DCACHE_OP_DELETE)) {
763: if (!locked || dentry->d_op->d_delete(dentry))
764: return false;
765: }
766:
767: // Explicitly told not to bother
768: if (unlikely(d_flags & DCACHE_DONTCACHE))
769: return false;
770:
771: // At this point it looks like we ought to keep it. We also might
772: // need to do something - put it on LRU if it wasn't there already
773: // and mark it referenced if it was on LRU, but not marked yet.
774: // Unfortunately, both actions require ->d_lock, so in lockless
775: // case we'd have to punt rather than doing those.
776: if (unlikely(!(d_flags & DCACHE_LRU_LIST))) {
777: if (!locked)
778: return false;
779: d_lru_add(dentry);
780: } else if (unlikely(!(d_flags & DCACHE_REFERENCED))) {
781: if (!locked)
782: return false;
783: dentry->d_flags |= DCACHE_REFERENCED;
784: }
785: return true;
786: }
SourceFile:fs/dcache.c]
|
| 12/2 |
2026/02/24 18:40 |
llm |
expert |
0m
Model:
Tokens:
input: 7852
output: 39
thoughts: 823
Thoughts: **Analyzing the Dentry Hashing Logic**
Okay, so I'm looking at this dentry code and immediately my attention is drawn to the `dentry->d_hash.pprev` pointer. That's the key to tracking whether a dentry is hashed or not. In `__d_drop`, we're explicitly setting `pprev` to NULL if the dentry *was* hashed, effectively unhashing it. And right below that, the `write_seqcount_invalidate` call is crucial for ensuring the stability of the `d_seq` counter after unhashing.
Now, `retain_dentry` is where things get interesting. It's using `d_unhashed` (which boils down to checking if `pprev` is NULL) to determine if a dentry should be retained. The comment is highlighting a potential race condition here, especially in the lockless path used by `fast_dput`. The comment points out that these predicates aren't "stable" if we are not locked. This is true; the state could change in the meantime. The comment goes on to explain that this is ok. We need to be aware of the race condition, but it's okay because the worst that could happen is that the check fails and the lock is then acquired.
I'm seeing `fast_dput` calling `retain_dentry` without the dentry lock. If `retain_dentry` says the dentry *isn't* hashed (returns `false`), `fast_dput` will take the lock and call `retain_dentry` again, locking it down and checking again. It seems like the lockless path is an optimization - a quick check to see if we can avoid locking. That makes sense. The code's taking a "optimistic locking" approach.
So, if `retain_dentry` reads a stale non-NULL `pprev`, it might proceed to check other flags, which could be problematic. If it reads NULL, it safely returns `false`, which is fine - it will recheck or return false to `dput`. I need to remember that `fast_dput` is returning `true` only if `retain_dentry` says we should keep the dentry (returns `true`), meaning it's hashed. That's the fast path, avoiding the lock.
Here's the critical bit: If `d_unhashed` was false (hashed), but then `__d_drop` *unhashes* it, and *then* `retain_dentry` sees this, `retain_dentry` might have *already* returned `true` based on the old value! We might decide to keep the dentry, but if `__d_drop` is running in parallel and unhashing it, there could be a problem. This is where I start to wonder about the locks protecting `__d_drop`. `d_invalidate` calls `__d_drop`. What does `___d_drop` do? I'll need to check that out, but first, I need to make sure I have the context here correct. The logic is making use of a sequence number to provide an indication to ensure stability.
|
| 13/2 |
2026/02/24 18:40 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:___d_drop]
Results: map[SourceCode: 554: static void ___d_drop(struct dentry *dentry)
555: {
556: struct hlist_bl_head *b;
557: /*
558: * Hashed dentries are normally on the dentry hashtable,
559: * with the exception of those newly allocated by
560: * d_obtain_root, which are always IS_ROOT:
561: */
562: if (unlikely(IS_ROOT(dentry)))
563: b = &dentry->d_sb->s_roots;
564: else
565: b = d_hash(dentry->d_name.hash);
566:
567: hlist_bl_lock(b);
568: __hlist_bl_del(&dentry->d_hash);
569: hlist_bl_unlock(b);
570: }
SourceFile:fs/dcache.c]
|
| 14/2 |
2026/02/24 18:40 |
llm |
expert |
0m
Model:
Tokens:
input: 9684
output: 38
thoughts: 886
Thoughts: **Analyzing a Potential Race Condition in Dentry Management**
Okay, so I'm looking at this code and I've got a potential race condition buzzing in the back of my head. I see that `___d_drop` calls `__hlist_bl_del`, which is responsible for removing a dentry from its hash list. I notice that `__hlist_bl_del` itself doesn't nullify the `pprev` pointer. Instead, `___d_drop` takes on that responsibility by explicitly setting `dentry->d_hash.pprev = NULL`. That's an important detail.
Now, I remember we have this `fast_dput` function, which performs a lockless check to see if a dentry is "unhashed" using the `hlist_bl_unhashed` macro. This macro simply checks if `h->pprev` is NULL. Here's where it gets interesting: if a `dput` happens *just* after `___d_drop` has called `__hlist_bl_del` but *before* `dentry->d_hash.pprev` is set to NULL, `fast_dput` might incorrectly believe the dentry is still hashed because `pprev` is still pointing to something.
Let's think through the implications. If `fast_dput` returns `true` (meaning it decides to retain the dentry), but the dentry was *actually* unhashed, we have a problem. The dentry, with a refcount of 0, would effectively become orphaned. It wouldn't be reachable through normal lookup operations because it's no longer in the hash table, and if it's not on the LRU list, it might linger indefinitely.
However, I recall that `retain_dentry` itself performs a check using `DCACHE_LRU_LIST`. If the dentry is *not* on the LRU, then `retain_dentry` returns `false` (under the condition `locked` is false). This will force the locked path. The locked path adds the dentry to LRU. If the dentry *is* on LRU, it's safe since it will be eventually cleaned up by the shrinker.
My intuition says that if the `retain_dentry(dentry, false)` call returns `true`, then `fast_dput` will also return `true`, even if a race condition happened and caused it to *think* the dentry is hashed. So, this might result in an unhashed dentry, with refcount 0, being left in memory for some time. But is that really a problem? It won't be in the hash table, and it'll eventually get swept up by the LRU cleaner.
But then, I consider `d_invalidate`. It's designed to make absolutely sure a dentry is gone, and it does so by calling `__d_drop(dentry)`. If `dput` gets called at the same time or right before `d_invalidate` that could cause some problems. Okay, I need to look closer at the `d_invalidate` code to see how it handles this...
|
| 15/2 |
2026/02/24 18:40 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:d_invalidate]
Results: map[SourceCode:1679: /**
1680: * d_invalidate - detach submounts, prune dcache, and drop
1681: * @dentry: dentry to invalidate (aka detach, prune and drop)
1682: */
1683: void d_invalidate(struct dentry *dentry)
1684: {
1685: bool had_submounts = false;
1686: spin_lock(&dentry->d_lock);
1687: if (d_unhashed(dentry)) {
1688: spin_unlock(&dentry->d_lock);
1689: return;
1690: }
1691: __d_drop(dentry);
1692: spin_unlock(&dentry->d_lock);
1693:
1694: /* Negative dentries can be dropped without further checks */
1695: if (!dentry->d_inode)
1696: return;
1697:
1698: shrink_dcache_parent(dentry);
1699: for (;;) {
1700: struct dentry *victim = NULL;
1701: d_walk(dentry, &victim, find_submount);
1702: if (!victim) {
1703: if (had_submounts)
1704: shrink_dcache_parent(dentry);
1705: return;
1706: }
1707: had_submounts = true;
1708: detach_mounts(victim);
1709: dput(victim);
1710: }
1711: }
SourceFile:fs/dcache.c]
|
| 16/2 |
2026/02/24 18:40 |
llm |
expert |
0m
Model:
Tokens:
input: 11767
output: 37
thoughts: 3545
Thoughts: **Analyzing the Race Condition in `fast_dput` and `d_invalidate`**
Okay, so I've been digging into this race condition, and here's what I've got: `d_invalidate` calls `__d_drop` which unhashes a dentry, and this all happens with `dentry->d_lock` held. The potential race is in `fast_dput`, where we *don't* hold the `dentry->d_lock` when we call `retain_dentry`. The key thing here is the `d_unhashed` flag, and specifically that `retain_dentry` does an optimistic check of that when not locked.
If `retain_dentry` sees the dentry as "hashed" (which might be a stale view), it can return `true`. If `__d_drop` then *just* finished and unhashed the dentry, but we missed it in our check, we incorrectly end up with refcount 0 but the dentry is on the LRU *and* unhashed. That's the core of the race. The check at line 776: if `DCACHE_LRU_LIST` isn't set, we bail out in the lockless path.
If `retain_dentry` returns `true`, `fast_dput` returns `true`, and we don't call `dentry_kill`. The dentry with zero refcount is *left on the LRU, unhashed*. But an unhashed dentry, by definition, is doomed! The shrinkers will eventually come along and reap it.
Now, if it's *not* on LRU, it's not a problem. `retain_dentry` returns `false` (either immediately or after re-checking under lock, if necessary) so the slow path in `fast_dput` kicks in, and we'll take the lock, see the dentry as unhashed, and call `dentry_kill`. So the race only bites us when the dentry is already on LRU, or *could* be on LRU.
The race specifically revolves around the `dentry->d_hash.pprev` pointer. `___d_drop` removes the dentry from the hash list, but doesn't immediately set `pprev` to NULL. `__d_drop` sets `dentry->d_hash.pprev = NULL;`. If `fast_dput` reads `pprev` between these two events, it might *think* the dentry is still hashed.
The critical "error" is that `fast_dput` can *incorrectly* retain the dentry on the LRU when it should be killed. The dentry, however, is unreachable and refcount 0, so it will be reclaimed eventually. `d_invalidate` calls are specifically used to *invalidate* entries, and by leaving an invalidated dentry on the LRU, we are in effect prolonging that invalidity, however, if it's on LRU, the shrinker will eventually clean it up anyway, so the race seems benign.
Now I'm diving deeper into the surrounding code comments. The comment says the optimistic check is *okay* if it was hashed *at some point* after we dropped the reference. The logic in `fast_dput` is basically: decrement the refcount, then *optimistically* check if we should keep this dentry around with zero refcount. The optimistic check is to avoid taking the lock.
It is possible to miss the unhashed state and return `true` with the optimistic check, and then the dentry stays on the LRU, even though it's effectively dead. Is this a leak? No, because the LRU will eventually reclaim it, though it delays its immediate death. Is it a bug? Maybe, but not a functional one, just a minor performance thing. There's not any use-after-free risk because this all happens under RCU protection.
|
| 17/2 |
2026/02/24 18:41 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:dput]
Results: map[SourceCode: 909: /*
910: * dput - release a dentry
911: * @dentry: dentry to release
912: *
913: * Release a dentry. This will drop the usage count and if appropriate
914: * call the dentry unlink method as well as removing it from the queues and
915: * releasing its resources. If the parent dentries were scheduled for release
916: * they too may now get deleted.
917: */
918: void dput(struct dentry *dentry)
919: {
920: if (!dentry)
921: return;
922: might_sleep();
923: rcu_read_lock();
924: if (likely(fast_dput(dentry))) {
925: rcu_read_unlock();
926: return;
927: }
928: finish_dput(dentry);
929: }
SourceFile:fs/dcache.c]
|
| 18/2 |
2026/02/24 18:41 |
llm |
expert |
0m
Model:
Tokens:
input: 16521
output: 39
thoughts: 312
Thoughts: **Debugging the dput/fast_dput Interaction**
Okay, the stack trace... let's see. It points to `fast_dput` and `dput` functions in `fs/dcache.c`. Specifically, `fast_dput+0x5f` and `dput+0x24`. I'm going to quickly cross-reference that with my understanding of these functions. Right, my `codesearch` confirms that `dput` is indeed at line 918, and `fast_dput` is called from within it around line 924. Yep, the stack trace lines *do* correspond to those locations.
Okay, let's break this down. Line 849 of `fast_dput`, which is `if (retain_dentry(dentry, false))`, seems to be where the branching happens. If `retain_dentry` returns true, `fast_dput` returns true. Otherwise, it calls `finish_dput(dentry)`. Now, I recall that `codesearch` shows the call to `finish_dput(dentry)` around line 928 in `dput`. This discrepancy is what I want to understand. The stack trace lines indicate it should be on line 924.
Let's dig into `finish_dput` to see what is going on there.
|
| 19/2 |
2026/02/24 18:41 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:finish_dput]
Results: map[SourceCode: 873: static void finish_dput(struct dentry *dentry)
874: __releases(dentry->d_lock)
875: __releases(RCU)
876: {
877: while (lock_for_kill(dentry)) {
878: rcu_read_unlock();
879: dentry = __dentry_kill(dentry);
880: if (!dentry)
881: return;
882: if (retain_dentry(dentry, true)) {
883: spin_unlock(&dentry->d_lock);
884: return;
885: }
886: rcu_read_lock();
887: }
888: rcu_read_unlock();
889: spin_unlock(&dentry->d_lock);
890: }
SourceFile:fs/dcache.c]
|
| 20/2 |
2026/02/24 18:41 |
llm |
expert |
0m
Model:
Tokens:
input: 17450
output: 40
thoughts: 273
Thoughts: **Analyzing the `dput` and `fast_dput` Flow**
Okay, so I'm looking at this `fast_dput` function, and something's bugging me. I see that if it returns `false`, it holds `dentry->d_lock`, which is interesting. Lines 857 and 870 confirm that. That means when `finish_dput` gets called, itβs going to be with that lock held. Makes sense. I know `dput` calls `fast_dput` so that's where this comes from.
Right, `fast_dput` itself. Let me just double check my understanding... Okay, so the code in `fast_dput` (lines 866-870) makes it clear. If it returns `false`, it's *because* the lock was taken, but it decided against retaining the dentry. If `count` is non-zero OR `retain_dentry` returns true, it unlocks and returns true. If not, it returns false with `d_lock` held. Got it.
So then, `finish_dput` does something with that lock... Ah, and it calls `lock_for_kill(dentry)`. Now, what does `lock_for_kill` do? Time to dig into *that* function.
|
| 21/2 |
2026/02/24 18:41 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:lock_for_kill]
Results: map[SourceCode: 708: static bool lock_for_kill(struct dentry *dentry)
709: {
710: struct inode *inode = dentry->d_inode;
711:
712: if (unlikely(dentry->d_lockref.count))
713: return false;
714:
715: if (!inode || likely(spin_trylock(&inode->i_lock)))
716: return true;
717:
718: do {
719: spin_unlock(&dentry->d_lock);
720: spin_lock(&inode->i_lock);
721: spin_lock(&dentry->d_lock);
722: if (likely(inode == dentry->d_inode))
723: break;
724: spin_unlock(&inode->i_lock);
725: inode = dentry->d_inode;
726: } while (inode);
727: if (likely(!dentry->d_lockref.count))
728: return true;
729: if (inode)
730: spin_unlock(&inode->i_lock);
731: return false;
732: }
SourceFile:fs/dcache.c]
|
| 22/2 |
2026/02/24 18:41 |
llm |
expert |
0m
Model:
Tokens:
input: 18435
output: 32
thoughts: 1853
Thoughts: **Analyzing a Potential Dentry Race Condition**
Okay, let me lay out my thinking on this dentry race. It boils down to a potential issue between `dput` and `d_invalidate`. The heart of it is this: `dput` might, under specific circumstances, return early, leaving a dentry with a zero refcount and unhashed, but still on the LRU. This early return happens because of `retain_dentry`.
The core of the problem is the timing window between when a dentry is unhashed (often by `d_invalidate`) and when `dput`'s `retain_dentry` checks if it's still hashed. If the unhashing happens *after* `retain_dentry`'s optimistic check (which might falsely report it as still hashed), the dentry is left in this in-between state β effectively "dead" but still lingering in memory.
Now, `d_invalidate`'s role is to ensure the dentry is no longer reachable by name, so it can be evicted from the cache. But this function does not always free the dentry immediately, it leaves it on the LRU. This is consistent with what I've seen in other parts of the kernel. `dput`, if it sees the dentry is unhashed, would normally call `__dentry_kill` to remove it from the LRU and free it. However, if the race happens, this freeing is delayed.
The comment in `retain_dentry` even hints at this kind of race: it says the checks aren't stable if not locked, but that's fine if the dentry was hashed at some point. In our case, it *was* hashed when we dropped the reference, and the comment says that is good. So, the race is *acceptable* and can lead to a slight delay in freeing the dentry. If `dput` sees the dentry as unhashed, it should call `__dentry_kill` to remove it from LRU, and if it does not, it should stay on LRU. Either way, it ends up unhashed and with a zero refcount. This race is therefore more or less benign, since the LRU shrinker will eventually clean it up.
However, KCSAN is flagging a warning because `d_unhashed`, which uses `!h->pprev`, isn't using any `READ_ONCE` or `WRITE_ONCE` primitives. The compiler could, theoretically, reorder memory accesses. Although on x86-64, 8-byte aligned reads are atomic, this does concern me. I should be using `READ_ONCE` in `hlist_bl_unhashed` and `WRITE_ONCE` in `__d_drop` to be completely safe, and eliminate the KCSAN warning. However, given the nature of the race and the existing locking strategy, the practical impact is small. This looks like a classic optimistic lockless check.
I did verify that `fast_dput` is called under `rcu_read_lock`, so even if there's a problem, it won't involve a dangling pointer, so it's only a *potential* read of uninitialized data (but it's unlikely).
Therefore, while a `READ_ONCE` and `WRITE_ONCE` solution is cleaner and recommended, the race's potential impact seems limited to a minor delay in dentry freeing, which the LRU shrinker eventually resolves.
|
| 23/2 |
2026/02/24 18:41 |
tool |
codesearch-struct-layout |
0m
Args: map[ContextFile:fs/dcache.c Name:dentry]
Results: map[Fields:[map[Name:d_flags OffsetBits:0 SizeBits:32] map[Name:d_seq OffsetBits:32 SizeBits:32] map[Name:d_hash OffsetBits:64 SizeBits:128] map[Name:d_parent OffsetBits:192 SizeBits:64] map[Name: OffsetBits:256 SizeBits:128] map[Name:d_inode OffsetBits:384 SizeBits:64] map[Name:d_shortname OffsetBits:448 SizeBits:320] map[Name:d_op OffsetBits:768 SizeBits:64] map[Name:d_sb OffsetBits:832 SizeBits:64] map[Name:d_time OffsetBits:896 SizeBits:64] map[Name:d_fsdata OffsetBits:960 SizeBits:64] map[Name:d_lockref OffsetBits:1024 SizeBits:64] map[Name: OffsetBits:1088 SizeBits:128] map[Name:d_sib OffsetBits:1216 SizeBits:128] map[Name:d_children OffsetBits:1344 SizeBits:64] map[Name:d_u OffsetBits:1408 SizeBits:128]]]
|
| 24/2 |
2026/02/24 18:41 |
llm |
expert |
0m
Model:
Tokens:
input: 21420
output: 24
thoughts: 1244
Thoughts: **Analyzing the Race Condition in `fast_dput` and `d_invalidate`**
Okay, so I'm digging into this potential race condition involving `fast_dput` and `d_invalidate` within the kernel's dentry handling. My initial thought is that `d_hash` is located at offset 64 bits (8 bytes), making it a `struct hlist_bl_node`. The `pprev` field is the second element within this structure, so it resides at offset 16 bytes from the beginning of the `dentry` structure. Because of the 8-byte alignment, the access to `pprev` should be atomic on x86_64.
However, I need to verify that this optimistic check is benign. It seems this race is not a huge issue because:
1. This check is optimistic and happens within a lockless path.
2. If the dentry appears hashed when it's not, the code simply keeps it on the LRU.
3. The dentry, in reality, is still unhashed, so no one can reach it by traversing the dentry tree.
4. The LRU shrinker is going to reclaim it anyway.
5. If it *wasn't* on the LRU, the code would've noticed that `DCACHE_LRU_LIST` isn't set, triggering the locked path, which would then see the correct state.
Let me double-check that last point. Looking at the code: if `DCACHE_LRU_LIST` isn't set, and `locked` is false, it returns `false`, *after* the `d_unhashed` check. If that check incorrectly claims it's hashed, it then moves on to look at `d_flags`. If `DCACHE_LRU_LIST` *isn't* set, it'll still return false and go to the locked path. This locked path calls `retain_dentry`, which *rechecks* `d_unhashed` *under a lock*. Because `d_invalidate` also uses a lock, the later `d_unhashed` check will see the correct updated value of `pprev`.
So the optimistic path only succeeds if the dentry *is* on the LRU. Returning `true` from `fast_dput` means "we're done, the dentry is on LRU and has a zero refcount", which is a valid state for an unhashed dentry (just waiting for the shrinker).
Actually, there's another case where the optimistic path is skipped: if `DCACHE_OP_DELETE` is set, it also punts to the locked path.
Thus, the optimistic path is very conservative. It only succeeds when the dentry is already on LRU and doesn't have delete operations, isn't disconnected, etc. The race on `d_unhashed` just might cause it to stay optimistic when it shouldn't have, but the consequence is just that it stays on the LRU.
Now, what about `d_invalidate`? Does it expect the dentry to be immediately freed? The documentation states it detaches submounts, prunes the dcache, and drops the dentry, calling `__d_drop`. It doesn't say that the dentry is instantly freed from memory. In fact, if someone *else* has a reference to the dentry, `d_invalidate` *can't* free it. It just unhashes it, so nobody else can find it via lookup.
If `dput` is the last reference, and it races with `d_invalidate`, it's perfectly fine that the dentry ends up on the LRU instead of being freed immediately.
One last thing: `dentry->d_hash.pprev` is used by `hlist_bl` macros. `__d_drop` uses `__hlist_bl_del`, which uses `pprev`. However, `__d_drop` does this under `hlist_bl_lock(b)`. The race is with `hlist_bl_unhashed`, which is just a read of `pprev`.
Wait, the code mentions that `hlist_bl` uses the LSB of `pprev` as a lock bit! If `pprev` has its LSB set, might `hlist_bl_unhashed` think it's non-NULL even if it's becoming NULL? No, `pprev` is a pointer-to-a-pointer. The LSB is set on the *pointer itself*, not on the value pointed to. Right, I need to double-check that `pprev` is never written to a value that *isn't* aligned.
|
| 25/2 |
2026/02/24 18:41 |
tool |
read-file |
0m
Args: map[File:include/linux/list_bl.h]
Results: map[Contents:/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_LIST_BL_H
#define _LINUX_LIST_BL_H
#include <linux/list.h>
#include <linux/bit_spinlock.h>
/*
* Special version of lists, where head of the list has a lock in the lowest
* bit. This is useful for scalable hash tables without increasing memory
* footprint overhead.
*
* For modification operations, the 0 bit of hlist_bl_head->first
* pointer must be set.
*
* With some small modifications, this can easily be adapted to store several
* arbitrary bits (not just a single lock bit), if the need arises to store
* some fast and compact auxiliary data.
*/
#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
#define LIST_BL_LOCKMASK 1UL
#else
#define LIST_BL_LOCKMASK 0UL
#endif
#ifdef CONFIG_DEBUG_LIST
#define LIST_BL_BUG_ON(x) BUG_ON(x)
#else
#define LIST_BL_BUG_ON(x)
#endif
struct hlist_bl_head {
struct hlist_bl_node *first;
};
struct hlist_bl_node {
struct hlist_bl_node *next, **pprev;
};
#define INIT_HLIST_BL_HEAD(ptr) \
((ptr)->first = NULL)
static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
{
h->next = NULL;
h->pprev = NULL;
}
#define hlist_bl_entry(ptr, type, member) container_of(ptr,type,member)
static inline bool hlist_bl_unhashed(const struct hlist_bl_node *h)
{
return !h->pprev;
}
static inline struct hlist_bl_node *hlist_bl_first(struct hlist_bl_head *h)
{
return (struct hlist_bl_node *)
((unsigned long)h->first & ~LIST_BL_LOCKMASK);
}
static inline void hlist_bl_set_first(struct hlist_bl_head *h,
struct hlist_bl_node *n)
{
LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
LIST_BL_LOCKMASK);
h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK);
}
static inline bool hlist_bl_empty(const struct hlist_bl_head *h)
{
return !((unsigned long)READ_ONCE(h->first) & ~LIST_BL_LOCKMASK);
}
static inline void hlist_bl_add_head(struct hlist_bl_node *n,
struct hlist_bl_head *h)
{
struct hlist_bl_node *first = hlist_bl_first(h);
n->next = first;
if (first)
first->pprev = &n->next;
n->pprev = &h->first;
hlist_bl_set_first(h, n);
}
static inline void hlist_bl_add_before(struct hlist_bl_node *n,
struct hlist_bl_node *next)
{
struct hlist_bl_node **pprev = next->pprev;
n->pprev = pprev;
n->next = next;
next->pprev = &n->next;
/* pprev may be `first`, so be careful not to lose the lock bit */
WRITE_ONCE(*pprev,
(struct hlist_bl_node *)
((uintptr_t)n | ((uintptr_t)*pprev & LIST_BL_LOCKMASK)));
}
static inline void hlist_bl_add_behind(struct hlist_bl_node *n,
struct hlist_bl_node *prev)
{
n->next = prev->next;
n->pprev = &prev->next;
prev->next = n;
if (n->next)
n->next->pprev = &n->next;
}
static inline void __hlist_bl_del(struct hlist_bl_node *n)
{
struct hlist_bl_node *next = n->next;
struct hlist_bl_node **pprev = n->pprev;
LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
/* pprev may be `first`, so be careful not to lose the lock bit */
WRITE_ONCE(*pprev,
(struct hlist_bl_node *)
((unsigned long)next |
((unsigned long)*pprev & LIST_BL_LOCKMASK)));
if (next)
next->pprev = pprev;
}
static inline void hlist_bl_del(struct hlist_bl_node *n)
{
__hlist_bl_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}
static inline void hlist_bl_del_init(struct hlist_bl_node *n)
{
if (!hlist_bl_unhashed(n)) {
__hlist_bl_del(n);
INIT_HLIST_BL_NODE(n);
}
}
static inline void hlist_bl_lock(struct hlist_bl_head *b)
{
bit_spin_lock(0, (unsigned long *)b);
}
static inline void hlist_bl_unlock(struct hlist_bl_head *b)
{
__bit_spin_unlock(0, (unsigned long *)b);
}
static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
{
return bit_spin_is_locked(0, (unsigned long *)b);
}
/**
* hlist_bl_for_each_entry - iterate over list of given type
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*
*/
#define hlist_bl_for_each_entry(tpos, pos, head, member) \
for (pos = hlist_bl_first(head); \
pos && \
({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
/**
* hlist_bl_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @n: another &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_bl_for_each_entry_safe(tpos, pos, n, head, member) \
for (pos = hlist_bl_first(head); \
pos && ({ n = pos->next; 1; }) && \
({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1;}); \
pos = n)
#endif
]
|
| 26/2 |
2026/02/24 18:41 |
llm |
expert |
0m
Model:
Tokens:
input: 25595
output: 22
thoughts: 1189
Thoughts: **Analyzing a Benign Race Condition in Dentry Handling**
Okay, let's break this down. My initial thought process is centered around understanding a KCSAN report related to dentry handling, specifically the `pprev` pointer within the `hlist_bl_node` structure. The report flags a potential race condition involving a write of NULL to `dentry->d_hash.pprev` and a read of the same pointer.
My first observation is that the lock bit isn't directly on `pprev` itself. `pprev` is a pointer to a pointer. The lock bit, if it exists, is actually within the value pointed to by `*pprev`, when `pprev` is `&head->first`. `hlist_bl_unhashed` just checks the *value* of the `pprev` pointer, not what it points to, to see if a node is linked into a list. So, it simply checks if `pprev` is NULL or not.
I then follow the flow from `___d_drop` which eventually sets `dentry->d_hash.pprev = NULL`. Critically, `___d_drop` calls `__hlist_bl_del`, not `hlist_bl_del`. `__hlist_bl_del` doesn't touch `n->pprev`, so the assignment to NULL only happens in `___d_drop` at the line cited in the report. This is important because `fast_dput` might read the `pprev` field before it's set to NULL.
Now, my mind shifts to a race condition. It seems the reported race is the read of `pprev` in `fast_dput` before it is set to NULL in `__d_drop`. `__hlist_bl_del` doesn't modify `n->pprev`, which is crucial. But then `hlist_bl_del` does, however, `___d_drop` calls `__hlist_bl_del`. So `pprev` is ONLY modified at the end of the `___d_drop` path, not within the `__hlist_bl_del` call. This makes this scenario very specific.
I consider the opposite scenario β dentries being *added* to lists. In all `hlist_bl_*_add` functions, `pprev` is always set to non-NULL values. That reinforces the conclusion that the race is a write of NULL and a read of whatever value it has.
Given this, I conclude that the race is benign. It's an intentional (or at least tolerated) optimistic check. The worst-case scenario is a slightly delayed dentry destruction. It's a potentially stale value, but it doesn't cause a corruption.
I then start thinking about improving the code, and if this could be an actual problem. I wonder if I should search for similar KCSAN reports. I decide to check if `READ_ONCE` should be used for `d_unhashed` and its implications.
I revisit the definitions of `d_unhashed` and `hlist_bl_unhashed`. Technically, using `READ_ONCE(h->pprev)` would be the purest way to handle this, but the current code is probably correct given that pointer-sized reads on most architectures are atomic.
Next, I try to explore *potential* harm from the race. What if we read a NULL where it's non-NULL, or the other way around?
If `pprev` is NULL but we read it as non-NULL, then we think it's hashed when it's not. This only leads to a delayed cleanup - a slightly "stickier" dentry on the LRU, but no corruption.
If we read `pprev` as NULL when it is actually non-NULL, then `retain_dentry` thinks it is unhashed and returns `false`. `fast_dput` will take the lock and re-check, and the locked path will see the correct value, which will be non-NULL. This is also safe, and does not seem to introduce any actual problem.
Therefore, this race condition is benign in both directions.
As a final check, I wonder if the callers of `__d_drop` are protected by a lock, so I review the lock used. They are: `d_invalidate`, `__d_move` and `d_delete`.
To double check, I search for the callers of `__d_drop`.
|
| 27/2 |
2026/02/24 18:42 |
tool |
codesearch-find-references |
0m
Args:
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__dentry_kill SourceFile:fs/dcache.c SourceLine:668] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:configfs_drop_dentry SourceFile:fs/configfs/inode.c SourceLine:212] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:d_delete SourceFile:fs/dcache.c SourceLine:2518] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:d_delete SourceFile:fs/dcache.c SourceLine:2522] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:d_drop SourceFile:fs/dcache.c SourceLine:603] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:d_invalidate SourceFile:fs/dcache.c SourceLine:1691]] TruncatedOutput:false]
|
| 28/2 |
2026/02/24 18:42 |
llm |
expert |
0m
Model:
Tokens:
input: 28091
output: 38
thoughts: 959
Thoughts: **Analyzing a Potential Race Condition in the Dentry Code**
Okay, let's break this down. The kernel's `d_drop` function, as expected, acquires the `d_lock` before calling `__d_drop`. This confirms the expected locking discipline around `__d_drop`. We're dealing with a potential race condition involving a lock-protected write and a lockless read. The focus seems to be on `dentry->d_hash.pprev`, which is a pointer-to-a-pointer. If the read wasn't atomic, we could see a partial pointer, but it *is* atomic, which is good.
KCSAN flagged this race, but I'm trying to see if there's anything else involved. I checked `d_invalidate`, which does use `write_seqcount_invalidate` on `dentry->d_seq`. However, both `fast_dput` and `retain_dentry` appear not to check `d_seq`. Next, I looked at `d_flags`; `retain_dentry` does read `d_flags` locklessly using `READ_ONCE`. But critically, it doesn't use `READ_ONCE` for `d_unhashed`.
Aha! `d_unhashed` is defined as a simple check on `hlist_bl_unhashed`, which in turn just checks if `h->pprev` is null. It *should* be using `READ_ONCE` here. Looking at other uses of `d_unhashed`, like in `d_find_alias`, the access is also lockless. This missing `READ_ONCE` in `d_unhashed` while `d_flags` *does* use it is likely the reason for the KCSAN warning. But is it *harmful*?
Let's trace `dentry_kill` to see if there's any other involvement. If `fast_dput` fails, `dput` calls `finish_dput`, which then calls `__dentry_kill`. Crucially, `__dentry_kill` calls `__d_drop`. The important check is inside `__d_drop` itself.
If `d_invalidate` has already unhashed it, `__dentry_kill` will not do the unhashing again because of the condition `!d_unhashed(dentry)`.
The main purpose of the `d_unhashed` check in `retain_dentry` is to prevent keeping dentries that are no longer in the hash table. If we miss it, we just hold onto it a little longer. That said, as an expert, I'm confident that this missing `READ_ONCE` is benign.
Let's double-check one more thing: `d_invalidate` also calls `shrink_dcache_parent`. If the dentry has children, they will be shrunk. But the race is still centered on the dentry itself. Another important detail: `d_invalidate` is called from `lookup_open`. In `lookup_open` if `d_unhashed(dentry)` is true, then `d_invalidate` is called.
Given the analysis, and the safety measures in place, I'm fairly confident that this is a benign race condition. I will keep in mind to bring this up to the developers responsible for the dentry code.
|
| 29/2 |
2026/02/24 18:42 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/namei.c IncludeLines:true Name:lookup_open]
Results: map[SourceCode:4331: /*
4332: * Look up and maybe create and open the last component.
4333: *
4334: * Must be called with parent locked (exclusive in O_CREAT case).
4335: *
4336: * Returns 0 on success, that is, if
4337: * the file was successfully atomically created (if necessary) and opened, or
4338: * the file was not completely opened at this time, though lookups and
4339: * creations were performed.
4340: * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
4341: * In the latter case dentry returned in @path might be negative if O_CREAT
4342: * hadn't been specified.
4343: *
4344: * An error code is returned on failure.
4345: */
4346: static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
4347: const struct open_flags *op,
4348: bool got_write, struct delegated_inode *delegated_inode)
4349: {
4350: struct mnt_idmap *idmap;
4351: struct dentry *dir = nd->path.dentry;
4352: struct inode *dir_inode = dir->d_inode;
4353: int open_flag = op->open_flag;
4354: struct dentry *dentry;
4355: int error, create_error = 0;
4356: umode_t mode = op->mode;
4357: DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4358:
4359: if (unlikely(IS_DEADDIR(dir_inode)))
4360: return ERR_PTR(-ENOENT);
4361:
4362: file->f_mode &= ~FMODE_CREATED;
4363: dentry = d_lookup(dir, &nd->last);
4364: for (;;) {
4365: if (!dentry) {
4366: dentry = d_alloc_parallel(dir, &nd->last, &wq);
4367: if (IS_ERR(dentry))
4368: return dentry;
4369: }
4370: if (d_in_lookup(dentry))
4371: break;
4372:
4373: error = d_revalidate(dir_inode, &nd->last, dentry, nd->flags);
4374: if (likely(error > 0))
4375: break;
4376: if (error)
4377: goto out_dput;
4378: d_invalidate(dentry);
4379: dput(dentry);
4380: dentry = NULL;
4381: }
4382: if (dentry->d_inode) {
4383: /* Cached positive dentry: will open in f_op->open */
4384: return dentry;
4385: }
4386:
4387: if (open_flag & O_CREAT)
4388: audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
4389:
4390: /*
4391: * Checking write permission is tricky, bacuse we don't know if we are
4392: * going to actually need it: O_CREAT opens should work as long as the
4393: * file exists. But checking existence breaks atomicity. The trick is
4394: * to check access and if not granted clear O_CREAT from the flags.
4395: *
4396: * Another problem is returing the "right" error value (e.g. for an
4397: * O_EXCL open we want to return EEXIST not EROFS).
4398: */
4399: if (unlikely(!got_write))
4400: open_flag &= ~O_TRUNC;
4401: idmap = mnt_idmap(nd->path.mnt);
4402: if (open_flag & O_CREAT) {
4403: if (open_flag & O_EXCL)
4404: open_flag &= ~O_TRUNC;
4405: mode = vfs_prepare_mode(idmap, dir->d_inode, mode, mode, mode);
4406: if (likely(got_write))
4407: create_error = may_o_create(idmap, &nd->path,
4408: dentry, mode);
4409: else
4410: create_error = -EROFS;
4411: }
4412: if (create_error)
4413: open_flag &= ~O_CREAT;
4414: if (dir_inode->i_op->atomic_open) {
4415: dentry = atomic_open(nd, dentry, file, open_flag, mode);
4416: if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
4417: dentry = ERR_PTR(create_error);
4418: return dentry;
4419: }
4420:
4421: if (d_in_lookup(dentry)) {
4422: struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
4423: nd->flags);
4424: d_lookup_done(dentry);
4425: if (unlikely(res)) {
4426: if (IS_ERR(res)) {
4427: error = PTR_ERR(res);
4428: goto out_dput;
4429: }
4430: dput(dentry);
4431: dentry = res;
4432: }
4433: }
4434:
4435: /* Negative dentry, just create the file */
4436: if (!dentry->d_inode && (open_flag & O_CREAT)) {
4437: /* but break the directory lease first! */
4438: error = try_break_deleg(dir_inode, delegated_inode);
4439: if (error)
4440: goto out_dput;
4441:
4442: file->f_mode |= FMODE_CREATED;
4443: audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
4444: if (!dir_inode->i_op->create) {
4445: error = -EACCES;
4446: goto out_dput;
4447: }
4448:
4449: error = dir_inode->i_op->create(idmap, dir_inode, dentry,
4450: mode, open_flag & O_EXCL);
4451: if (error)
4452: goto out_dput;
4453: }
4454: if (unlikely(create_error) && !dentry->d_inode) {
4455: error = create_error;
4456: goto out_dput;
4457: }
4458: return dentry;
4459:
4460: out_dput:
4461: dput(dentry);
4462: return ERR_PTR(error);
4463: }
SourceFile:fs/namei.c]
|
| 30/2 |
2026/02/24 18:42 |
llm |
expert |
0m
Model:
Tokens:
input: 31920
output: 19
thoughts: 1939
Thoughts: **Benign Data Race in dentry Unhashing**
Okay, let's break this down. We're looking at a potential race condition within the dentry cache, specifically around the `lookup_open` function and the interaction with `d_invalidate` and `dput`. The core of the issue seems to be the sequence: `d_invalidate(dentry)` followed by `dput(dentry)`. In the scenario where `d_revalidate` fails, `d_invalidate` unhashes the dentry, and `dput` releases the reference.
Initially, I thought there might be a race within the same thread, but that's not possible because these calls are sequential. The key is that the `dput` call might happen in a *different* thread, like the one in `do_mknodat`. The stack trace confirms this. So, we're talking about Thread 1 in `lookup_open` calling `d_invalidate` to unhash and Thread 2 in `do_mknodat` calling `dput`.
Now, the potential for a race is evident. If Thread 2's `dput` reads the dentry *before* Thread 1's `d_invalidate` has fully completed setting `pprev` to NULL, it might incorrectly assume the dentry is still hashed and attempt to retain it on the LRU list, when in fact it is being invalidated. If Thread 2 reads *after* itβs unhashed, then the dentry is killed as expected.
I checked `d_invalidate` and noticed that negative dentries are handled differently. However, in this case, we're likely dealing with a positive dentry that failed revalidation, making the initial logic relevant. The KCSAN report confirms the race involves the `pprev` field being set to NULL.
Now, my initial conclusion is that this is a benign data race. It's an optimistic lockless read of a state (`pprev`) that is being updated under a lock in a different thread. The potential for a stale read exists, but the consequence is a slightly delayed dentry destruction. This doesn't seem to cause any correctness problems in the dcache since this is a racy, optimistic approach to optimize dentry destruction.
To be absolutely sure, I have to examine the usage of `hlist_bl_unhashed`, particularly within the `retain_dentry` and `fast_dput` functions. My main concern is that if `hlist_bl_unhashed` returns a "wrong" value (because of the race), could it lead to a crash? In `retain_dentry`, it's just used for a boolean check.
After carefully analyzing, it is correct that `hlist_bl_unhashed` just checks if `pprev` is NULL. `fast_dput` only uses the result of `d_unhashed` (and consequently, the value of `pprev` in `hlist_bl_unhashed`) to determine if it should take the lock and use the slower path. It doesn't dereference `pprev` directly based on its result.
The `smp_rmb()` before reading `d_flags` in `retain_dentry` is another important point. Is there any barrier in `__d_drop` that has the corresponding `smp_wmb()`? In fact `write_seqcount_invalidate` usually has a barrier, though `fast_dput` doesn't check the dentry sequence number. The key is still that the race is on `pprev`.
A final check to ensure there's no NULL dereference if `pprev` is NULL but read as non-NULL is required. I need to review `retain_dentry` again. If `d_unhashed` is false, it proceeds by checking `d_flags` and doesn't dereference `pprev`. If `d_unhashed` is true, the function returns false. So there is no potential for a NULL dereference.
The comments in `retain_dentry` confirm this is an intentional design choice, explicitly acknowledging that predicates are not stable when not locked, and the function is just an optimization.
With that, I am confident in concluding that this is a benign data race. The KCSAN report about the change from 0xffff88823741a658 to 0x0000000000000000 in `pprev` confirms the specific field involved in the race.
|
| 31/2 |
2026/02/24 18:42 |
tool |
set-results |
0m
Args: map[Benign:true Confident:true]
Results: map[Benign:true Confident:true]
|
| 32/2 |
2026/02/24 18:42 |
llm |
expert |
0m
Model:
Tokens:
input: 34869
output: 728
thoughts: 225
Thoughts: **Benign Data Race Analysis: My Conclusion**
Okay, the analysis is done, and I've come to a pretty clear conclusion: this data race is completely benign. It's happening between the `__d_drop` function, specifically when it's setting `dentry->d_hash.pprev` to NULL, and `fast_dput`, which is reading `dentry->d_hash.pprev` through `d_unhashed`. This is all part of an optimistic, lockless path within `dput`, which is often used in the VFS layer for performance reasons.
What's happening, in essence, is that due to this race, a dentry might linger in the LRU cache longer than it should. It's essentially "stuck" there instead of being immediately removed if it's being unhashed concurrently. Now, this isn't a *good* thing in principle, but the implications are minimal. The dentry, after all, *is* being unhashed, so it's no longer accessible via regular name lookups, it's effectively "dead". If it hangs around in the LRU a bit longer, it's still eventually going to be reaped by the dentry shrinker process. This sort of behavior is explicitly acknowledged in the relevant code; the `retain_dentry` function, I believe, makes it abundantly clear that these lockless checks are inherently unstable, and that the team has determined this is an acceptable tradeoff for performance.
The good news is that on x86_64, the accesses are atomic, so we don't have to worry about the possibility of reading partially written, torn values. Thatβs another factor that I took into account.
Ultimately, there's no way this race can lead to memory corruption, crashes, or any other severe consequences. It's just a little bit of transient "laziness" in dentry cleanup. It's a trade-off that was made in the name of performance, and the code reflects that this tradeoff is acceptable.
|