| 0/0 |
2026/07/05 21:40 |
flow |
assessment-kcsan |
20m
Results: map[Benign:true Explanation:The data race occurs between `__d_rehash` (writer) and `__d_lookup` (reader) on
the `pprev` field of a dentry's hash node (`dentry->d_hash.pprev`).
### Race Summary
* **Writer (`__d_rehash`):** When adding a new dentry to the head of a hash
bucket, it updates the `pprev` pointer of the dentry that was previously at the
head. This happens in `hlist_bl_add_head_rcu` (inlined in `__d_rehash`):
`first->pprev = &n->next;`.
* **Reader (`__d_lookup`):** While iterating the hash table, it checks if a
candidate dentry is still hashed via `d_unhashed(dentry)`, which reads
`dentry->d_hash.pprev`.
### Synchronization Context
The writer holds the hash bucket's bit-lock (`hlist_bl_lock`) and the new
dentry's `d_lock`. The reader holds `rcu_read_lock()` and the candidate dentry's
`d_lock`. However, the candidate dentry's `d_lock` does not protect its `pprev`
pointer from being updated by a concurrent `__d_rehash` adding a *different*
dentry to the same bucket head.
### Final Classification: **BENIGN**
**Reasoning:**
The race is structurally tolerant to the concurrent update for several reasons:
1. **Invariant Result:** The writer updates `pprev` from one non-NULL value
(`&h->first`) to another non-NULL value (`&n->next`). The reader (`d_unhashed`)
only checks if the value is `NULL`. Therefore, the reader will correctly see the
dentry as "hashed" regardless of whether it sees the old or new pointer value.
2. **Atomicity:** On x86_64, aligned 64-bit pointer accesses are atomic. A torn
read is not expected. Even if a hypothetical torn read resulted in a `NULL`
value, `__d_lookup` would simply skip the dentry (a false negative), leading to
a slow-path lookup which is safe.
3. **RCU Guarantees:** The publication of the new head is correctly ordered via
`rcu_assign_pointer` in `hlist_bl_set_first_rcu`, ensuring that any reader
following the new head will see a consistent state.
**Recommended Annotations:**
While benign, the code should be annotated to follow the pattern used in the
non-bit-locked RCU list implementation (`hlist_add_head_rcu`):
* In `include/linux/rculist_bl.h`, use `WRITE_ONCE(first->pprev, &n->next);`
in `hlist_bl_add_head_rcu`.
* In `include/linux/list_bl.h`, use `READ_ONCE(h->pprev)` in
`hlist_bl_unhashed`.]
|
| 1/1 |
2026/07/05 21:40 |
action |
kernel-checkouter |
1m
Results: map[KernelSrc:/app/workdir/cache/src/7f96d8fab09f463021219eba2aa999d493c559e9]
|
| 2/1 |
2026/07/05 21:41 |
action |
kernel-builder |
9m
Results: map[KernelObj:/app/workdir/cache/build/0a834c591e4edfe89bcc769fda18da52d3c8ac90]
|
| 3/1 |
2026/07/05 21:50 |
action |
codesearch-prepare |
7m
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/07/05 21:58 |
agent |
expert |
2m
Model:
Results:
Instruction: # KCSAN Data Race Severity Analysis Protocol
You are an expert Linux kernel concurrency engineer analyzing a Kernel
Concurrency Sanitizer (KCSAN) data race report to determine if it is
**BENIGN** or **HARMFUL**.
## 1. CLASSIFICATIONS
### **BENIGN (Truly Benign)**
The logic is sound and structurally tolerant to compiler optimizations or
stale/torn reads.
- **Diagnostics/Stats:** Reads used only for `/proc`, `/sys`, counters, or
`pr_info`.
- **Heuristic Hints:** A "hint" flag where an old value only causes a
slightly delayed update or a sub-optimal but safe fast-path.
- **Single-Writer Flag Updates:** A single writer updating flags where the
concurrent read is a simple bitwise check (e.g., `flags & MASK`). These are
historically tolerated, assuming neither "Fused Accesses" nor "Ordering
Violations" are relevant in this context.
- **Marked Reloads:** A load feeding into a `cmpxchg()` loop or checked
against a later `READ_ONCE()` reload.
- **Safe Overwrites:** Writing the same value already present.
### **HARMFUL (Logic Bug or Marking Required)**
The race causes incorrect behavior due to a synchronization failure or
because missing annotations allow the compiler to break the algorithm.
**Marking Required for Correctness:**
The algorithm is logically sound but requires annotations (`READ_ONCE()`,
`WRITE_ONCE()`, `smp_load_acquire()`, `smp_store_release()`, etc.) to be safe.
- **Fused Accesses:** The compiler might merge accesses or hoist a load out
of a loop, breaking polling/wait loops (livelocks).
- **Torn Accesses:** A large access (e.g., 64-bit on 32-bit arch) might be
split into multiple non-atomic accesses. Note that `READ_ONCE()` does **not**
guarantee atomicity for 64-bit variables on 32-bit architectures.
- **Ordering Violations:** The race breaks a "happens-before" relationship
(requires primitives with implied or explicit memory barriers).
**Logic Bugs:**
A fundamental synchronization failure. Marking accesses will **not** fix it;
the logic itself must change.
- **Pointers/Lifecycle:** The racing variable is a pointer being dereferenced
or a refcount governing object lifecycle (Use-After-Free risk).
- **Control Flow:** The variable guards a critical section, memory allocation,
or hardware command.
- **Bitfields:** Concurrent writes to different bits in the same word.
Compilers often use non-atomic read-modify-write sequences, meaning a
write to `bit_A` can "clobber" a concurrent write to `bit_B`. However,
do not blindly assume all bitfield accesses are harmful; you must prove
that a concurrent write actually clobbers another in a way that breaks
logic.
- **Complex Structures:** Races on shared lists, trees, or hashmaps.
- **Lossy Updates:** Concurrent plain RMW operations (e.g., `var++`) on
non-diagnostic variables where every increment must be preserved.
- **State Machines:** Races allowing a state machine to bypass transitions
or enter an invalid state.
- **Adjacent Unsynchronized Operations:** Consider races happening at the
same time. For example, if both threads execute `struct->has_elements = true;
list_add(node, &struct->list);`, the race on `has_elements` implies an
adjacent race on `list_head`, which is HARMFUL.
## 2. RESEARCH & ANALYSIS WORKFLOW
1. **Locate the Race:** Find the exact variables and functions in the stack
traces. Do not speculate about hypothetical compiler
behaviors or theoretical dangers (e.g., dismissing something as
"fundamentally unsafe") without tracing the actual data flow to a crash.
2. **Contextualize:** Identify held locks, RCU sections, or interrupt
contexts. Explain why the race is possible (e.g., "Thread A holds `lock`,
but Thread B is a lockless reader").
3. **Data Flow:** Follow the racing variable into its subsequent uses. If
the reader sees a stale or torn value, what branch is taken? Does it lead
to a pointer dereference or `BUG_ON()`? **You must prove harm via this
data flow.**
4. **Failure Sequencing (if HARMFUL):** Construct a two-column execution trace
demonstrating the failure.
*Example format:*
```text
CPU0 CPU1
function_a()
// lockless read
ptr = global_ptr
<PREEMPT>
function_b()
lock(&my_lock)
global_ptr = NULL
unlock(&my_lock)
kfree(ptr)
if (ptr)
*ptr = 1; // -> Use-After-Free!
```
## 3. OUTPUT FORMAT
- **Race Summary:** `[Function A]` vs `[Function B]` on `[Variable/Field]`.
- **Synchronization context:** List held locks and explain the lack of mutual
exclusion.
- **Final Classification:** **BENIGN** or **HARMFUL**.
**If BENIGN:**
- **Reasoning:** Briefly explain why the race is structurally tolerant to stale
or torn reads.
- **Recommended Annotations:** Suggest appropriate annotations (e.g.,
`READ_ONCE()`, `data_race()`).
**If HARMFUL:**
- **Failure Reasoning:** Explain and provide a two-column interleaving showing
exactly how the race leads to a failure such as corruption or kernel crash.
- **Recommended Fix (ONLY if fix is trivial):** Suggest a structural fix (e.g.,
"Hold `mapping->i_pages` lock", "Convert to `atomic_t`") or required memory
ordering annotations (e.g., "Wrap in `READ_ONCE()`", "Use
`smp_load_acquire()`").
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
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_lookup / __d_rehash
write to 0xffff88811a845a90 of 8 bytes by task 15291 on cpu 0:
hlist_bl_add_head_rcu include/linux/rculist_bl.h:91 [inline]
__d_rehash+0xa3/0x1d0 fs/dcache.c:2709
__d_add+0x326/0x490 fs/dcache.c:2937
d_splice_alias_ops+0xe3/0x370 fs/dcache.c:3286
d_splice_alias+0x1f/0x30 fs/dcache.c:3315
simple_lookup+0xb7/0xd0 fs/libfs.c:81
lookup_one_qstr_excl+0xc7/0x240 fs/namei.c:1810
__start_dirop fs/namei.c:2920 [inline]
start_dirop+0x3e/0x70 fs/namei.c:2942
simple_start_creating+0xa2/0xd0 fs/libfs.c:2305
debugfs_start_creating+0xda/0x150 fs/debugfs/inode.c:394
debugfs_create_symlink+0x39/0x150 fs/debugfs/inode.c:672
nsim_dev_port_debugfs_init drivers/net/netdevsim/dev.c:437 [inline]
__nsim_dev_port_add+0x4fb/0x700 drivers/net/netdevsim/dev.c:1505
nsim_dev_port_add_all+0x33/0xc0 drivers/net/netdevsim/dev.c:1570
nsim_drv_probe+0x68f/0x7c0 drivers/net/netdevsim/dev.c:1731
nsim_bus_probe+0x15/0x20 drivers/net/netdevsim/bus.c:420
call_driver_probe drivers/base/dd.c:-1 [inline]
really_probe+0x1ba/0x600 drivers/base/dd.c:706
__driver_probe_device+0x14f/0x1c0 drivers/base/dd.c:868
driver_probe_device+0x37/0x2f0 drivers/base/dd.c:898
__device_attach_driver+0x227/0x330 drivers/base/dd.c:1026
bus_for_each_drv+0x1c0/0x230 drivers/base/bus.c:500
__device_attach+0x1d1/0x2a0 drivers/base/dd.c:1098
device_initial_probe+0x70/0x90 drivers/base/dd.c:1153
bus_probe_device+0x101/0x1b0 drivers/base/bus.c:620
device_add+0x5e8/0x790 drivers/base/core.c:3772
device_register+0x1d/0x30 drivers/base/core.c:3854
nsim_bus_dev_new drivers/net/netdevsim/bus.c:471 [inline]
new_device_store+0x276/0x470 drivers/net/netdevsim/bus.c:191
bus_attr_store+0x57/0x80 drivers/base/bus.c:172
sysfs_kf_write+0xfe/0x120 fs/sysfs/file.c:145
kernfs_fop_write_iter+0x1d2/0x2e0 fs/kernfs/file.c:345
new_sync_write fs/read_write.c:595 [inline]
vfs_write+0x57d/0x990 fs/read_write.c:687
ksys_write+0xdc/0x1a0 fs/read_write.c:739
__do_sys_write fs/read_write.c:750 [inline]
__se_sys_write fs/read_write.c:747 [inline]
__x64_sys_write+0x40/0x50 fs/read_write.c:747
x64_sys_call+0x27e1/0x3020 arch/x86/include/generated/asm/syscalls_64.h:2
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x136/0x3c0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff88811a845a90 of 8 bytes by task 2981 on cpu 1:
hlist_bl_unhashed include/linux/list_bl.h:54 [inline]
d_unhashed include/linux/dcache.h:396 [inline]
__d_lookup+0xf8/0x340 fs/dcache.c:2620
lookup_fast+0x5e/0x320 fs/namei.c:1878
walk_component fs/namei.c:2278 [inline]
link_path_walk+0x523/0xdd0 fs/namei.c:2656
path_lookupat+0x53/0x4c0 fs/namei.c:2812
filename_lookup+0x18f/0x390 fs/namei.c:2842
do_readlinkat+0x74/0x2f0 fs/stat.c:571
__do_sys_readlink fs/stat.c:605 [inline]
__se_sys_readlink fs/stat.c:602 [inline]
__x64_sys_readlink+0x47/0x60 fs/stat.c:602
x64_sys_call+0x2b51/0x3020 arch/x86/include/generated/asm/syscalls_64.h:90
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x136/0x3c0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0xffff88823753f650 -> 0xffff888105f1a848
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 2981 Comm: udevd Tainted: G W syzkaller #0 PREEMPT(lazy)
Tainted: [W]=WARN
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/09/2026
==================================================================
Reply: The data race occurs between `__d_rehash` (writer) and `__d_lookup` (reader) on the `pprev` field of a dentry's hash node (`dentry->d_hash.pprev`).
### Race Summary
* **Writer (`__d_rehash`):** When adding a new dentry to the head of a hash bucket, it updates the `pprev` pointer of the dentry that was previously at the head. This happens in `hlist_bl_add_head_rcu` (inlined in `__d_rehash`): `first->pprev = &n->next;`.
* **Reader (`__d_lookup`):** While iterating the hash table, it checks if a candidate dentry is still hashed via `d_unhashed(dentry)`, which reads `dentry->d_hash.pprev`.
### Synchronization Context
The writer holds the hash bucket's bit-lock (`hlist_bl_lock`) and the new dentry's `d_lock`. The reader holds `rcu_read_lock()` and the candidate dentry's `d_lock`. However, the candidate dentry's `d_lock` does not protect its `pprev` pointer from being updated by a concurrent `__d_rehash` adding a *different* dentry to the same bucket head.
### Final Classification: **BENIGN**
**Reasoning:**
The race is structurally tolerant to the concurrent update for several reasons:
1. **Invariant Result:** The writer updates `pprev` from one non-NULL value (`&h->first`) to another non-NULL value (`&n->next`). The reader (`d_unhashed`) only checks if the value is `NULL`. Therefore, the reader will correctly see the dentry as "hashed" regardless of whether it sees the old or new pointer value.
2. **Atomicity:** On x86_64, aligned 64-bit pointer accesses are atomic. A torn read is not expected. Even if a hypothetical torn read resulted in a `NULL` value, `__d_lookup` would simply skip the dentry (a false negative), leading to a slow-path lookup which is safe.
3. **RCU Guarantees:** The publication of the new head is correctly ordered via `rcu_assign_pointer` in `hlist_bl_set_first_rcu`, ensuring that any reader following the new head will see a consistent state.
**Recommended Annotations:**
While benign, the code should be annotated to follow the pattern used in the non-bit-locked RCU list implementation (`hlist_add_head_rcu`):
* In `include/linux/rculist_bl.h`, use `WRITE_ONCE(first->pprev, &n->next);` in `hlist_bl_add_head_rcu`.
* In `include/linux/list_bl.h`, use `READ_ONCE(h->pprev)` in `hlist_bl_unhashed`.
|
| 5/2 |
2026/07/05 21:58 |
llm |
expert |
0m
Model:
Tokens:
input: 7007
output: 36
thoughts: 184
Thoughts: **Data Race Analysis: `__d_rehash` vs. `__d_lookup`**
Okay, here's what I'm seeing: the report flags a data race, a classic writer-reader conflict, specifically between `__d_rehash` and `__d_lookup`. The race is happening on a field at that memory address, `0xffff88811a845a90`. The writer, `__d_rehash`, uses `hlist_bl_add_head_rcu` to modify the field, and the reader, `__d_lookup`, uses `hlist_bl_unhashed`.
So, the next logical step is to delve into the source. Let's look at the implementation details of those functions. `__d_rehash`, clearly, calls `hlist_bl_add_head_rcu`. On the reader's side, `__d_lookup` calls `d_unhashed`, which in turn, calls `hlist_bl_unhashed`. The common thread is the `hlist_bl_*` family. Given the functions involved and the structure of the code, I'm fairly certain the racing field is `dentry->d_hash`. It’s likely a hash table entry.
Time to consult `fs/dcache.c`. I need to inspect the code around the reported line numbers for the calls to `__d_rehash`, `__d_lookup`, and those crucial `hlist_bl_*` calls. This should give a clearer picture of the data access patterns and confirm the field in question.
|
| 6/2 |
2026/07/05 21:58 |
tool |
read-file |
0m
Args: map[File:fs/dcache.c FirstLine:2600 LineCount:150]
Results: map[Contents:2600: * Take d_lock when comparing a candidate dentry, to avoid races
2601: * with d_move().
2602: *
2603: * It is possible that concurrent renames can mess up our list
2604: * walk here and result in missing our dentry, resulting in the
2605: * false-negative result. d_lookup() protects against concurrent
2606: * renames using rename_lock seqlock.
2607: *
2608: * See Documentation/filesystems/path-lookup.txt for more details.
2609: */
2610: rcu_read_lock();
2611:
2612: hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2613:
2614: if (dentry->d_name.hash != hash)
2615: continue;
2616:
2617: spin_lock(&dentry->d_lock);
2618: if (dentry->d_parent != parent)
2619: goto next;
2620: if (d_unhashed(dentry))
2621: goto next;
2622:
2623: if (!d_same_name(dentry, parent, name))
2624: goto next;
2625:
2626: dentry->d_lockref.count++;
2627: found = dentry;
2628: spin_unlock(&dentry->d_lock);
2629: break;
2630: next:
2631: spin_unlock(&dentry->d_lock);
2632: }
2633: rcu_read_unlock();
2634:
2635: return found;
2636: }
2637:
2638: /**
2639: * d_hash_and_lookup - hash the qstr then search for a dentry
2640: * @dir: Directory to search in
2641: * @name: qstr of name we wish to find
2642: *
2643: * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
2644: */
2645: struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2646: {
2647: /*
2648: * Check for a fs-specific hash function. Note that we must
2649: * calculate the standard hash first, as the d_op->d_hash()
2650: * routine may choose to leave the hash value unchanged.
2651: */
2652: name->hash = full_name_hash(dir, name->name, name->len);
2653: if (dir->d_flags & DCACHE_OP_HASH) {
2654: int err = dir->d_op->d_hash(dir, name);
2655: if (unlikely(err < 0))
2656: return ERR_PTR(err);
2657: }
2658: return d_lookup(dir, name);
2659: }
2660:
2661: /*
2662: * When a file is deleted, we have two options:
2663: * - turn this dentry into a negative dentry
2664: * - unhash this dentry and free it.
2665: *
2666: * Usually, we want to just turn this into
2667: * a negative dentry, but if anybody else is
2668: * currently using the dentry or the inode
2669: * we can't do that and we fall back on removing
2670: * it from the hash queues and waiting for
2671: * it to be deleted later when it has no users
2672: */
2673:
2674: /**
2675: * d_delete - delete a dentry
2676: * @dentry: The dentry to delete
2677: *
2678: * Turn the dentry into a negative dentry if possible, otherwise
2679: * remove it from the hash queues so it can be deleted later
2680: */
2681:
2682: void d_delete(struct dentry * dentry)
2683: {
2684: struct inode *inode = dentry->d_inode;
2685:
2686: spin_lock(&inode->i_lock);
2687: spin_lock(&dentry->d_lock);
2688: /*
2689: * Are we the only user?
2690: */
2691: if (dentry->d_lockref.count == 1) {
2692: if (dentry_negative_policy)
2693: __d_drop(dentry);
2694: dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2695: dentry_unlink_inode(dentry);
2696: } else {
2697: __d_drop(dentry);
2698: spin_unlock(&dentry->d_lock);
2699: spin_unlock(&inode->i_lock);
]
|
| 7/2 |
2026/07/05 21:58 |
llm |
expert |
0m
Model:
Tokens:
input: 9047
output: 35
thoughts: 43
Thoughts: **Analyzing the Hash Table Report**
Okay, so I'm focusing on `__d_lookup`, but something about this report feels off. It flagged a comparison with `__d_rehash`, and I'm not seeing that interplay here. I need to get my bearings and look for `__d_rehash` directly to understand the context of this comparison. What is driving the divergence between these two calls? I have to go back to the source and get the full picture.
|
| 8/2 |
2026/07/05 21:58 |
tool |
read-file |
0m
Args: map[File:fs/dcache.c FirstLine:2690 LineCount:50]
Results: map[Contents:2690: */
2691: if (dentry->d_lockref.count == 1) {
2692: if (dentry_negative_policy)
2693: __d_drop(dentry);
2694: dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2695: dentry_unlink_inode(dentry);
2696: } else {
2697: __d_drop(dentry);
2698: spin_unlock(&dentry->d_lock);
2699: spin_unlock(&inode->i_lock);
2700: }
2701: }
2702: EXPORT_SYMBOL(d_delete);
2703:
2704: static void __d_rehash(struct dentry *entry)
2705: {
2706: struct hlist_bl_head *b = d_hash(entry->d_name.hash);
2707:
2708: hlist_bl_lock(b);
2709: hlist_bl_add_head_rcu(&entry->d_hash, b);
2710: hlist_bl_unlock(b);
2711: }
2712:
2713: /**
2714: * d_rehash - add an entry back to the hash
2715: * @entry: dentry to add to the hash
2716: *
2717: * Adds a dentry to the hash according to its name.
2718: */
2719:
2720: void d_rehash(struct dentry * entry)
2721: {
2722: spin_lock(&entry->d_lock);
2723: __d_rehash(entry);
2724: spin_unlock(&entry->d_lock);
2725: }
2726: EXPORT_SYMBOL(d_rehash);
2727:
2728: static inline unsigned start_dir_add(struct inode *dir)
2729: {
2730: preempt_disable_nested();
2731: for (;;) {
2732: unsigned n = READ_ONCE(dir->i_dir_seq);
2733: if (!(n & 1) && try_cmpxchg(&dir->i_dir_seq, &n, n + 1))
2734: return n;
2735: cpu_relax();
2736: }
2737: }
2738:
2739: static inline void end_dir_add(struct inode *dir, unsigned int n)
]
|
| 9/2 |
2026/07/05 21:58 |
llm |
expert |
0m
Model:
Tokens:
input: 9961
output: 38
thoughts: 621
Thoughts: **Analyzing a Potential Data Race in the Dentry Cache**
Okay, so I'm looking at this report highlighting a potential data race within the dentry cache implementation, specifically around the `d_hash` structure and its associated `hlist_bl` operations. The crash report pinpoints a write to the address `0xffff88811a845a90` by task 15291, attributed to `hlist_bl_add_head_rcu` within `__d_rehash`, and a read from the same address by task 2981, triggered by `hlist_bl_unhashed` and `__d_lookup`.
Let's break this down further. The code snippets confirm what I suspected. `__d_rehash` uses `hlist_bl_add_head_rcu` to insert a dentry into a hash bucket. `__d_lookup` iterates through these same buckets and checks if a dentry is 'unhashed' using `d_unhashed`. `d_unhashed` uses `hlist_bl_unhashed` to see if a dentry's `pprev` pointer is NULL, which indicates it's not currently in a hash list.
Now, that memory address... `0xffff88811a845a90`. Knowing the dentry structure, this screams " `dentry->d_hash.pprev`". This makes the conflict even more stark. `hlist_bl_add_head_rcu` is writing to that pointer while `hlist_bl_unhashed` is *reading* from it. This is a classic read-write data race scenario, and the report, with its simultaneous access to that address, confirms it.
The interesting part here is that RCU is involved in these operations. That means that an unhashed dentry, by the time `__d_lookup` executes, is in the process of being rehashed by another thread. It implies that we have an incorrect RCU use, or a missed RCU synchronization point. A deeper dive is definitely needed into how RCU is *supposed* to be used here. I need to refresh myself on how RCU protects against these very situations by properly defining read-side and write-side critical sections.
|
| 10/2 |
2026/07/05 21:58 |
tool |
read-file |
0m
Args: map[File:include/linux/rculist_bl.h FirstLine:80 LineCount:30]
Results: map[Contents: 80: */
81: static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n,
82: struct hlist_bl_head *h)
83: {
84: struct hlist_bl_node *first;
85:
86: /* don't need hlist_bl_first_rcu* because we're under lock */
87: first = hlist_bl_first(h);
88:
89: n->next = first;
90: if (first)
91: first->pprev = &n->next;
92: n->pprev = &h->first;
93:
94: /* need _rcu because we can have concurrent lock free readers */
95: hlist_bl_set_first_rcu(h, n);
96: }
97: /**
98: * hlist_bl_for_each_entry_rcu - iterate over rcu list of given type
99: * @tpos: the type * to use as a loop cursor.
100: * @pos: the &struct hlist_bl_node to use as a loop cursor.
101: * @head: the head for your list.
102: * @member: the name of the hlist_bl_node within the struct.
103: *
104: */
105: #define hlist_bl_for_each_entry_rcu(tpos, pos, head, member) \
106: for (pos = hlist_bl_first_rcu_dereference(head); \
107: pos && \
108: ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1; }); \
109: pos = rcu_dereference_raw(hlist_bl_next_rcu(pos)))
]
|
| 11/2 |
2026/07/05 21:58 |
llm |
expert |
0m
Model:
Tokens:
input: 11653
output: 32
thoughts: 3675
Thoughts: **Analysis of Potential Race Condition in dentry Hashing**
Okay, so I'm looking at a KCSAN report indicating a potential data race between `__d_rehash` (writer) and `__d_lookup` (reader) related to the `pprev` pointer within the `hlist_bl` data structure used for dentry hashing. The write is happening at `first->pprev = &n->next` within `hlist_bl_add_head_rcu`, which is called by `__d_rehash`. The read is performed by the `d_unhashed` macro in `__d_lookup`.
From what I can tell, `__d_rehash` adds `entry` to the head of a hash list. `first` is the previous head (possibly the `dentry` that `__d_lookup` is currently looking at) and `n` is a pointer to the `entry`. The problematic write updates `first->pprev` to point to the next entry of the new entry that is being added.
The `__d_lookup` function iterates through the hash list and checks if each `dentry` is unhashed using `d_unhashed(dentry)`. This involves reading `dentry->d_hash.pprev`. If `d_unhashed` returns true, the lookup skips this `dentry`. In the normal case, a dentry is considered "unhashed" if its `pprev` pointer is NULL, indicating it's not currently in the hash list.
Now, if `__d_rehash` modifies `first->pprev` concurrently with `__d_lookup`, we have a potential race. The key is to understand what can happen to `d_unhashed`.
I am considering the case when `dentry` that `__d_lookup` is looking at is `first`, the `dentry` that `__d_rehash` modifies. In this scenario, `first->pprev`'s value is modified from a non-NULL value (either `&h->first` or `&n->next`, or some other valid address) to another non-NULL value (`&n->next`).
Given this, the most important question to ask is can a torn read of a pointer result in a NULL pointer? On 64-bit systems, aligned reads and writes are usually atomic, so this is highly unlikely. However, if the read is torn, the value of `first->pprev` might be corrupted but it won't be NULL in that case.
If the read *is* torn and it reads a NULL, then `d_unhashed` would incorrectly return true, and we skip that dentry and potentially return NULL when a dentry really exists in the list.
However, `d_unhashed` is called after holding `dentry->d_lock`, so it is not possible that `__d_rehash` would be modifying the entry that the lookup is looking at. `__d_rehash` holds `entry->d_lock`, but not the lock for the entries already in the hash.
In this scenario, `d_unhashed` is still checking if `dentry` has been unhashed while we are waiting for the spin lock to protect against dentries being removed or moved.
Furthermore, because the writes in the rehash case is to a non-NULL value to a non-NULL value, the result of `d_unhashed` is going to be `false`.
So, the KCSAN report is likely a false positive. Because a pointer write is concurrent to pointer read, KCSAN is reporting the race condition, however, because both values of `pprev` are non-NULL, the logic in `__d_lookup` is not affected.
|
| 12/2 |
2026/07/05 21:59 |
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:d_lru OffsetBits:1088 SizeBits:128] map[Name:d_sib OffsetBits:1216 SizeBits:128] map[Name:d_children OffsetBits:1344 SizeBits:64] map[Name: OffsetBits:1408 SizeBits:128]]]
|
| 13/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 16525
output: 34
thoughts: 479
Thoughts: **Analyzing a KCSAN Report: Atomic Writes and Potential Race Conditions**
Okay, so I'm looking at this KCSAN report flagging a potential issue with a write to `pprev` within a `struct dentry`. The `d_hash` member is 64 bits (8 bytes) into the structure, and the `struct hlist_bl_node` definition shows `pprev` immediately follows `next` at an offset of 8 bytes relative to `d_hash`. This places `pprev` at 16 bytes from the beginning of `struct dentry`, and it's 8-byte aligned on x86_64. Given the alignment and size, the write to `pprev` *should* be atomic on this architecture.
So, why is KCSAN complaining? The key is that it's not marked as atomic. I need to consider all possible scenarios. Is there a case where `pprev` could be `NULL` at the time of the write? Yes, if the dentry is in the process of being unhashed. But `__d_rehash` is *adding* a dentry, not removing one. The dentry being added (`n`) has its `pprev` initialized to something non-`NULL`. The existing dentry in the list (`first`) has its `pprev` updated from a non-`NULL` value to another non-`NULL` value.
Wait... could `first` be *about to be* unhashed when `__d_rehash` is running? That's the likely scenario. If another thread is unhashing `first`, it would be holding the hash bucket's `b` lock, right? Let's check the code:
```c
2708: hlist_bl_lock(b);
2709: hlist_bl_add_head_rcu(&entry->d_hash, b);
2710: hlist_bl_unlock(b);
```
Yes, `__d_rehash` *does* take the `b` lock. So, the thread unhashing would have the hash bucket lock, preventing the race condition. `__d_rehash` *also* holds that same `b` lock. The two operations can't execute concurrently, then, because unhashing also needs that bucket lock.
To confirm this, let's verify that the `__d_drop` function, which handles unhashing, takes the bucket lock. It calls `__d_shrink` or just `hlist_bl_del_rcu`. So, I'll review the `__d_drop` code.
|
| 14/2 |
2026/07/05 21:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c Name:__d_drop]
Results: map[SourceCode: 574: void __d_drop(struct dentry *dentry)
575: {
576: if (!d_unhashed(dentry)) {
577: ___d_drop(dentry);
578: dentry->d_hash.pprev = NULL;
579: write_seqcount_invalidate(&dentry->d_seq);
580: }
581: }
SourceFile:fs/dcache.c]
|
| 15/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 17764
output: 34
thoughts: 20
Thoughts: Okay, let's see. Here's my attempt to summarize the prompt's instructions, playing the role of someone thinking about them in a way a field expert might:
**Analyzing the Prompt: A Deep Dive**
Alright, so the task is pretty clear. I'm supposed to take a chunk of text – presumably some kind of data, prompt or information – and synthesize it into a first-person summary, conveying it as if it's my own internal thought process. It's like a stream of consciousness interpretation, right? And the target is a fellow expert in this field, so I can assume a certain level of familiarity with the underlying concepts. I can get away with making more assumptions about their prior understanding than I would with a novice.
The critical piece seems to be the "___d_drop" part. That's clearly a placeholder, a signal, a directive to do *something*. It's likely related to filtering or extracting a specific kind of input. Let's see how I can use this.
|
| 16/2 |
2026/07/05 21:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c Name:___d_drop]
Results: map[SourceCode: 565: static void ___d_drop(struct dentry *dentry)
566: {
567: struct hlist_bl_head *b = d_hash(dentry->d_name.hash);
568:
569: hlist_bl_lock(b);
570: __hlist_bl_del(&dentry->d_hash);
571: hlist_bl_unlock(b);
572: }
SourceFile:fs/dcache.c]
|
| 17/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 18190
output: 37
thoughts: 1044
Thoughts: **Analyzing the dentry hash list concurrency**
Okay, let me break this down. So, `___d_drop` takes `hlist_bl_lock(b)`, which means it's exclusive with `__d_rehash` for the same bucket. That's good; mutual exclusion is key in concurrent data structures.
If `__d_rehash` is updating `first->pprev`, it means `first` is *not* being unhashed. The pointer `first->pprev` is changing from `&h->first` to `&n->next`. Both are valid, non-NULL pointers.
Now, `__d_lookup` is lockless, except for `dentry->d_lock`, which it grabs *after* finding a potential candidate. It uses `rcu_read_lock()`. If `__d_lookup` is looking at `dentry` (which is `first`), it has already dereferenced the head of the list, taken `dentry->d_lock`, and checked `d_unhashed(dentry)`.
`__d_rehash`, when running, *holds* the bucket lock but *doesn't* hold `dentry->d_lock`. This means `__d_rehash` can update `dentry->d_hash.pprev` while `__d_lookup` has its `dentry->d_lock`. This is what the report highlights.
However, since `pprev` is only switching between two valid, non-NULL values, `d_unhashed(dentry)` will always return `false`. The check in `__d_lookup` should correctly conclude that the dentry is still hashed.
Are there other consequences, though? If `pprev` is torn and looks like NULL, `__d_lookup` might skip the dentry. This would be a false negative. However, given that `pprev` is an aligned pointer and the update is atomic, tearing to NULL is highly unlikely. Even if it *did* happen, it would just cause a lookup failure, which might be retried or lead to a new dentry being created (though dcache can handle duplicates). `lookup_fast` failing just leads to `lookup_slow`.
If `__d_lookup` returns NULL, the VFS will call the filesystem's `lookup` method, which will probably call `d_splice_alias`. `d_splice_alias` will find the existing dentry if it's still present. So even a false negative in `__d_lookup` is not a disaster, just slower.
But wait, could `pprev` be used for something else? In `hlist_bl_node`, `pprev` points to the `next` pointer of the previous element (or the `first` pointer of the head). It's used during *deletion* to update the previous element. But `__d_lookup` never deletes.
Okay, what about `__d_rehash`? It's adding `n` to the head. It sets `n->next = first`, then `first->pprev = &n->next`, then `n->pprev = &h->first`, and finally, it sets `h->first = n` (using `hlist_bl_set_first_rcu`).
Now, consider a reader iterating:
1. If the reader sees the *old* `h->first` (which is `first`), it then checks `d_unhashed(first)`. `first->pprev` might be `&h->first` or `&n->next`. Both are non-NULL. Then it moves to `first->next`.
2. If it sees the *new* `h->first` (which is `n`), it then checks `d_unhashed(n)`. `n->pprev` is `&h->first` (non-NULL). Then it moves to `n->next` (which is `first`).
In both cases, it *works*.
The only "issue" is the unmarked access. In the Linux kernel, `d_unhashed` is often used locklessly. However, `pprev` isn't updated with `WRITE_ONCE` in `hlist_bl_add_head_rcu`.
Wait, `hlist_bl_add_head_rcu` is a *standard* RCU list operation. Why isn't `WRITE_ONCE` used for `pprev`? Even `hlist_add_head_rcu` (the non-bit-locked version) doesn't use `WRITE_ONCE` for `pprev`. I should check `include/linux/rculist.h` to see why
|
| 18/2 |
2026/07/05 21:59 |
tool |
read-file |
0m
Args: map[File:include/linux/rculist.h FirstLine:680 LineCount:20]
Results: map[Contents: 680: if (first)
681: WRITE_ONCE(first->pprev, &n->next);
682: }
683:
684: /**
685: * hlist_add_tail_rcu
686: * @n: the element to add to the hash list.
687: * @h: the list to add to.
688: *
689: * Description:
690: * Adds the specified element to the specified hlist,
691: * while permitting racing traversals.
692: *
693: * The caller must take whatever precautions are necessary
694: * (such as holding appropriate locks) to avoid racing
695: * with another list-mutation primitive, such as hlist_add_head_rcu()
696: * or hlist_del_rcu(), running on this same list.
697: * However, it is perfectly legal to run concurrently with
698: * the _rcu list-traversal primitives, such as
699: * hlist_for_each_entry_rcu(), used to prevent memory-consistency
]
|
| 19/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 20625
output: 38
thoughts: 115
Thoughts: **RCU List Mystery Solved!**
Okay, wait a second... It just hit me! `hlist_add_head_rcu` *does* actually use `WRITE_ONCE` for `pprev`. I was so focused on the `rculist_bl.h` header file from my previous check that I missed it. There it is, right in line 681 of the code – the `WRITE_ONCE(first->pprev, &n->next);` call.
I should probably confirm that I'm looking at the correct source file, just to be absolutely sure. My previous `read-file` command might have been pointed at the wrong header, `rculist_bl.h`, so let's re-check the actual source file this time.
|
| 20/2 |
2026/07/05 21:59 |
tool |
read-file |
0m
Args: map[File:include/linux/rculist_bl.h FirstLine:80 LineCount:20]
Results: map[Contents: 80: */
81: static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n,
82: struct hlist_bl_head *h)
83: {
84: struct hlist_bl_node *first;
85:
86: /* don't need hlist_bl_first_rcu* because we're under lock */
87: first = hlist_bl_first(h);
88:
89: n->next = first;
90: if (first)
91: first->pprev = &n->next;
92: n->pprev = &h->first;
93:
94: /* need _rcu because we can have concurrent lock free readers */
95: hlist_bl_set_first_rcu(h, n);
96: }
97: /**
98: * hlist_bl_for_each_entry_rcu - iterate over rcu list of given type
99: * @tpos: the type * to use as a loop cursor.
]
|
| 21/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 21264
output: 36
thoughts: 498
Thoughts: **RCU List Issue Analysis**
Okay, so I'm looking at `rculist_bl.h` and immediately spot something that needs attention. Line 91, specifically, the `first->pprev = &n->next;` assignment in `hlist_bl_add_head_rcu` doesn't utilize `WRITE_ONCE`. And, it seems `hlist_bl_unhashed` is missing a `READ_ONCE` as well. The latter is defined in `include/linux/list_bl.h`, but no read barriers there either.
The implication is that if `hlist_bl_add_head_rcu` is truly intended for concurrent RCU readers, as its name and the comment at line 94 indicate, then these list structure updates need to be safe for those readers. `hlist_bl_unhashed` uses the `pprev` pointer, which is a key check for RCU readers, especially in functions like `__d_lookup`.
Without these memory barriers, there's a risk of compiler optimizations leading to stale or torn reads. The compiler *could* reorder things.
The question is, how harmful is this?
Analyzing `__d_lookup`, a stale `pprev` read resulting in a false positive for `d_unhashed` (seeing NULL when it's not) would cause us to skip a valid dentry. This is definitely a performance hit, creating a false negative in the cache, but is usually not a full-blown correctness issue because the lookup will fall back to the slow path.
However, a false negative is more problematic. If `d_unhashed` returns a non-NULL value when it *is* actually NULL, that's where things could get dicey. But `__d_rehash` modifies `pprev` from non-NULL to non-NULL. The transition to NULL happens in `__d_drop`, specifically via `__hlist_bl_del`. Now, looking at `__hlist_bl_del`, it's clear: there's no `WRITE_ONCE` there either. I'll need to check `include/linux/list_bl.h` to make sure I'm not missing anything.
|
| 22/2 |
2026/07/05 21:59 |
tool |
read-file |
0m
Args: map[File:include/linux/list_bl.h FirstLine:40 LineCount:50]
Results: map[Contents: 40: };
41: #define INIT_HLIST_BL_HEAD(ptr) \
42: ((ptr)->first = NULL)
43:
44: static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
45: {
46: h->next = NULL;
47: h->pprev = NULL;
48: }
49:
50: #define hlist_bl_entry(ptr, type, member) container_of(ptr,type,member)
51:
52: static inline bool hlist_bl_unhashed(const struct hlist_bl_node *h)
53: {
54: return !h->pprev;
55: }
56:
57: static inline struct hlist_bl_node *hlist_bl_first(struct hlist_bl_head *h)
58: {
59: return (struct hlist_bl_node *)
60: ((unsigned long)h->first & ~LIST_BL_LOCKMASK);
61: }
62:
63: static inline void hlist_bl_set_first(struct hlist_bl_head *h,
64: struct hlist_bl_node *n)
65: {
66: LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
67: LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
68: LIST_BL_LOCKMASK);
69: h->first = (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK);
70: }
71:
72: static inline bool hlist_bl_empty(const struct hlist_bl_head *h)
73: {
74: return !((unsigned long)READ_ONCE(h->first) & ~LIST_BL_LOCKMASK);
75: }
76:
77: static inline void hlist_bl_add_head(struct hlist_bl_node *n,
78: struct hlist_bl_head *h)
79: {
80: struct hlist_bl_node *first = hlist_bl_first(h);
81:
82: n->next = first;
83: if (first)
84: first->pprev = &n->next;
85: n->pprev = &h->first;
86: hlist_bl_set_first(h, n);
87: }
88:
89: static inline void hlist_bl_add_before(struct hlist_bl_node *n,
]
|
| 23/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 23041
output: 37
thoughts: 10
Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:
**Delving into `__hlist_bl_del`**
Alright, let's dissect this `__hlist_bl_del` function. My initial thought is, based on the naming, this must be a low-level helper for deleting an element from a "hashed list" that's likely block-based. The double underscores suggest internal implementation details, intended for the kernel's internals. I'm immediately looking for how this differs from a more standard list deletion function like `list_del`. The "bl" likely means "block list," which reinforces the memory management implications. I'm wondering if this is related to how the kernel handles page faults or memory allocation in certain contexts, particularly those involving some sort of hash-based lookup to quickly find and free blocks. I expect that race conditions have been considered and handled here. This suggests that the list is using some form of locks or atomic operations to provide some degree of concurrency. I'm now thinking about the implications on cache coherency and memory barriers given that this is a low-level operation.
|
| 24/2 |
2026/07/05 21:59 |
tool |
read-file |
0m
Args: map[File:include/linux/list_bl.h FirstLine:100 LineCount:20]
Results: map[Contents: 100: (struct hlist_bl_node *)
101: ((uintptr_t)n | ((uintptr_t)*pprev & LIST_BL_LOCKMASK)));
102: }
103:
104: static inline void hlist_bl_add_behind(struct hlist_bl_node *n,
105: struct hlist_bl_node *prev)
106: {
107: n->next = prev->next;
108: n->pprev = &prev->next;
109: prev->next = n;
110:
111: if (n->next)
112: n->next->pprev = &n->next;
113: }
114:
115: static inline void __hlist_bl_del(struct hlist_bl_node *n)
116: {
117: struct hlist_bl_node *next = n->next;
118: struct hlist_bl_node **pprev = n->pprev;
119:
]
|
| 25/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 23654
output: 37
thoughts: 216
Thoughts: **Analyzing the `__hlist_bl_del` Function for a Bug**
Okay, so I'm looking at this `__hlist_bl_del` function, and it seems to be designed to remove a node (`n`) from a blist-style hash list. Right away, I see it's taking the node, `n`, and extracting its `next` and `pprev` pointers, which is standard for list manipulation.
The line that catches my eye is 120, `LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);`. This is crucial. It's a `BUG_ON` check, meaning if this condition is true, the kernel will panic. The code is verifying that the lower bits of the node pointer `n` are not set in the `LIST_BL_LOCKMASK`. This implies that the memory address of the node `n` is not currently representing a locked node. This suggests some kind of locking mechanism is in place, and this function should *not* be called on a locked node. If this check fails, it indicates a serious error in the concurrent access/locking strategy.
Now, let's step through the removal process:
1. `*pprev = next;` - This assigns the `next` node to the previous `pprev` pointer. So, the previous entry now points to the node *after* the target node that's being removed.
2. The `if (next)` block is important. If the node being deleted is *not* the last node in the list, then `next->pprev = pprev;` must set the previous pointer for the next node to point back to the previous node.
3. `n->pprev = NULL;` sets the previous pointer to `NULL`, to make it clear that the node, `n`, is no longer part of the list.
It is important to notice that the `pprev` pointer can be "poisoned", to indicate the node is not part of any list.
So, in essence, I understand the code. It performs a standard doubly-linked list removal with an important safety check to prevent removal of locked nodes. I will make sure the locking is carefully done.
|
| 26/2 |
2026/07/05 21:59 |
tool |
read-file |
0m
Args: map[File:include/linux/list_bl.h FirstLine:120 LineCount:10]
Results: map[Contents: 120: LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
121:
122: /* pprev may be `first`, so be careful not to lose the lock bit */
123: WRITE_ONCE(*pprev,
124: (struct hlist_bl_node *)
125: ((unsigned long)next |
126: ((unsigned long)*pprev & LIST_BL_LOCKMASK)));
127: if (next)
128: next->pprev = pprev;
129: }
]
|
| 27/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 24561
output: 37
thoughts: 75
Thoughts: Okay, let me think this through.
**Analyzing `__hlist_bl_del` in Detail**
Alright, hang on a second. I need to clarify something about `__hlist_bl_del`. I was just reviewing this code and I *thought* I had a handle on it. But, no, wait... I just realized, `__hlist_bl_del` *does* actually use `WRITE_ONCE` for `*pprev`. That's critical because `*pprev` could point to the head of the list, and that might have a lock bit. So, that part checks out. I had missed that earlier.
Now, the thing that's making me pause is... I don't *see* it explicitly setting `n->pprev = NULL` within `__hlist_bl_del`. This seems odd. Where does that happen? Or maybe it doesn't need to? I need to look further down in the function to see how the node's previous pointer is actually handled in the overall process of removing it from the list. Let me scroll down and dive deeper into the code to figure this out.
|
| 28/2 |
2026/07/05 21:59 |
tool |
read-file |
0m
Args: map[File:include/linux/list_bl.h FirstLine:130 LineCount:10]
Results: map[Contents: 130:
131: static inline void hlist_bl_del(struct hlist_bl_node *n)
132: {
133: __hlist_bl_del(n);
134: n->next = LIST_POISON1;
135: n->pprev = LIST_POISON2;
136: }
137:
138: static inline void hlist_bl_del_init(struct hlist_bl_node *n)
139: {
]
|
| 29/2 |
2026/07/05 21:59 |
llm |
expert |
0m
Model:
Tokens:
input: 25080
output: 38
thoughts: 2570
Thoughts: **Analysis of a Potential Race Condition in the Linux dcache and hlist_bl**
Okay, let's break this down. My initial thought is that `hlist_bl_del` poisons `pprev`, and `hlist_bl_del_init` sets it to `NULL`. I see that `__d_drop` is calling `__hlist_bl_del` and subsequently setting `dentry->d_hash.pprev = NULL`. This looks like a plain write to `pprev`, while `d_unhashed` performs a plain read of it. If these operations overlap, I can see a potential issue.
If `__d_drop` is running, then it will set `pprev = NULL`. Conversely, if `__d_lookup` is running, it reads `pprev`. If `__d_lookup` sees a stale non-NULL value, it might erroneously believe the entry is still hashed. However, `__d_drop` usually calls with `dentry->d_lock` held, and checking the callers of `__d_drop`, specifically `d_delete` and `dentry_kill`, shows that it indeed holds `dentry->d_lock` when `__d_drop` is called. So, in this scenario, they are synchronized. `__d_lookup` also takes `dentry->d_lock`, as confirmed at line 2617. So I see that `__d_drop` and `__d_lookup` are synchronized via `dentry->d_lock` and there is no race.
However, I realize the race isn't with `__d_drop`, but instead with `__d_rehash`. `__d_rehash` is called from `__d_add` and this is where it's happening! `__d_add` is called by `d_splice_alias`. Within `__d_add`, I see that `spin_lock(&dentry->d_lock)` is taken, the inode is set, `__d_rehash` is called, and then the lock is released. Therefore, `__d_rehash` gets called with `dentry->d_lock` held.
The key is that the `dentry` here is the *new* dentry being added. The race is actually on `first->pprev` -- `first` is an *existing* dentry in the list. `__d_rehash` holds `dentry->d_lock` and the bucket lock, but *not* `first->d_lock`.
So, the race is between `__d_lookup` (holding `first->d_lock`) and `__d_rehash` (adding the *new* dentry to the head of the list, potentially where `first` is). My analysis confirms: the race is an update of `pprev` from a non-NULL value to a different non-NULL value.
Is this benign? It appears so. `d_unhashed` only checks for NULL, and the update is atomic. Even if it weren't atomic, the "worst" that could happen is a lookup failure, which is safe.
Wait, could it cause a false negative of `d_unhashed`? If it were `NULL` and changed to non-`NULL`. But `first` was *already* in the list, so its `pprev` was *already* non-`NULL`.
What if `first` was being unhashed at the same time? As I said, `__d_rehash` and `___d_drop` are mutually exclusive because they both take the bucket lock. So `first->pprev` can't be changed to `NULL` when `__d_rehash` is changing it.
The only possible values for `first->pprev` during the race are `&h->first` and `&n->next`. Both are non-NULL. So `d_unhashed(first)` will *always* return `false`.
So, the race is harmless.
However, even though it's technically benign, to avoid compiler issues and KCSAN reports, it's good practice to mark these accesses, considering the Linux kernel's trend to mark RCU-protected accesses. `hlist_add_head_rcu` already uses `WRITE_ONCE` for `pprev`. `hlist_bl_add_head_rcu` should probably follow suit.
Actually, is this a known issue? I should just conclude based on my analysis.
Let's double check if `pprev` is used for anything else that might be broken by this race. In `hlist_bl`, `pprev` is used in: `hlist_bl
|
| 30/2 |
2026/07/05 22:00 |
tool |
read-file |
0m
Args: map[File:include/linux/rculist_bl.h FirstLine:20 LineCount:50]
Results: map[Contents: 20: LIST_BL_BUG_ON((unsigned long)n & LIST_BL_LOCKMASK);
21: LIST_BL_BUG_ON(((unsigned long)h->first & LIST_BL_LOCKMASK) !=
22: LIST_BL_LOCKMASK);
23: rcu_assign_pointer(hlist_bl_first_rcu(h),
24: (struct hlist_bl_node *)((unsigned long)n | LIST_BL_LOCKMASK));
25: }
26:
27: #define hlist_bl_first_rcu_dereference(head) \
28: ({ \
29: struct hlist_bl_head *__head = (head); \
30: \
31: (struct hlist_bl_node *) \
32: ((unsigned long)rcu_dereference_check(hlist_bl_first_rcu(__head), \
33: hlist_bl_is_locked(__head)) & \
34: ~LIST_BL_LOCKMASK); \
35: })
36:
37: /**
38: * hlist_bl_del_rcu - deletes entry from hash list without re-initialization
39: * @n: the element to delete from the hash list.
40: *
41: * Note: hlist_bl_unhashed() on entry does not return true after this,
42: * the entry is in an undefined state. It is useful for RCU based
43: * lockfree traversal.
44: *
45: * In particular, it means that we can not poison the forward
46: * pointers that may still be used for walking the hash list.
47: *
48: * The caller must take whatever precautions are necessary
49: * (such as holding appropriate locks) to avoid racing
50: * with another list-mutation primitive, such as hlist_bl_add_head_rcu()
51: * or hlist_bl_del_rcu(), running on this same list.
52: * However, it is perfectly legal to run concurrently with
53: * the _rcu list-traversal primitives, such as
54: * hlist_bl_for_each_entry().
55: */
56: static inline void hlist_bl_del_rcu(struct hlist_bl_node *n)
57: {
58: __hlist_bl_del(n);
59: n->pprev = LIST_POISON2;
60: }
61:
62: /**
63: * hlist_bl_add_head_rcu
64: * @n: the element to add to the hash list.
65: * @h: the list to add to.
66: *
67: * Description:
68: * Adds the specified element to the specified hlist_bl,
69: * while permitting racing traversals.
]
|
| 31/2 |
2026/07/05 22:00 |
llm |
expert |
0m
Model:
Tokens:
input: 29527
output: 25
thoughts: 2001
Thoughts: **Analysis of hlist_bl_add_head_rcu and Potential Data Race**
Okay, let's break this down. My initial observation is that `hlist_bl_set_first_rcu` uses `rcu_assign_pointer`, as expected. But I see `hlist_bl_first_rcu` and `rcu_assign_pointer` used near the definition in line 23. It dawns on me that line 18 must be where `hlist_bl_set_first_rcu` begins. I confirm the head update is properly marked. However, I notice that the `pprev` update in `hlist_bl_add_head_rcu` isn't using any of the standard memory barrier primitives like `WRITE_ONCE`. This is different from `hlist_add_head_rcu`, where `WRITE_ONCE` *is* used for the `pprev` update. This seems like a potential oversight.
The main concern is that, in `hlist_bl_add_head_rcu` we do:
```c
89: n->next = first;
90: if (first)
91: first->pprev = &n->next;
92: n->pprev = &h->first;
93:
94: /* need _rcu because we can have concurrent lock free readers */
95: hlist_bl_set_first_rcu(h, n);
```
So `first->pprev` is updated *before* `h->first` is published. A reader might see an inconsistent state.
I need to analyze this further. Let's look at `hlist_bl_unhashed`. It seems the concern is that if `pprev` is updated without `WRITE_ONCE`, and read without `READ_ONCE`, the compiler might reorder, split, or invent a NULL value. However, the use of `rcu_assign_pointer` for head publication includes a barrier, so the primary RCU list issue of new node init/publication race is avoided. I also see that `first->pprev` is updated before the new head is published. This means a reader will see either the old or the new head, but the `first->pprev` value should always be non-NULL.
I need to consider corner cases. What if `first` was the only element? Then `first->pprev` was `&h->first` and is updated to `&n->next`. Both are non-NULL. What if `first` wasn't the head? It won't be modified by `hlist_bl_add_head_rcu` at all. Therefore, the data race only applies to the dentry that was previously at the head.
The good news is that both values written to `pprev` are non-NULL, and the reader only checks for NULL. Because it's an aligned pointer, intermediate NULL values shouldn't be possible.
**Conclusion:** **BENIGN.** The observed race is harmless.
**Recommendation:** For consistency, I recommend using `WRITE_ONCE` for the `pprev` update in `hlist_bl_add_head_rcu` and `READ_ONCE` in `hlist_bl_unhashed` (or `data_race` if we don't care about the actual value). Since `hlist_add_head_rcu` uses `WRITE_ONCE`, `hlist_bl_add_head_rcu` should do the same.
Now, let's explore if `hlist_bl_unhashed` is used anywhere else. It is used in numerous places like `d_unhashed`, `__d_drop`, `d_add`, etc. It's only a NULL check in all these cases.
Also, I need to look at `hlist_bl_del_rcu`. After deletion, `n->pprev` is set to `LIST_POISON2`, which is non-NULL. The comment says that `hlist_bl_unhashed` should not return true after deletion, which is intentional for RCU based lockfree traversal. This means `d_unhashed` won't return true, and `__d_lookup` might not skip it! This is fine because even if `__d_lookup` finds a deleted entry, RCU guarantees it won't be freed while it's in the hash or list.
The problem could happen if `__d_lookup` reads a deleted entry. But `dentry->d_hash.pprev = NULL` happens AFTER `___d
|
| 32/2 |
2026/07/05 22:00 |
tool |
grepper |
0m
Args: map[Expression:hlist_bl_add_head_rcu]
Results: map[Output:Documentation/RCU/whatisRCU.rst=1048=RCU pointer/list update::
--
Documentation/RCU/whatisRCU.rst-1071- hlists_swap_heads_rcu
Documentation/RCU/whatisRCU.rst:1072: hlist_bl_add_head_rcu
Documentation/RCU/whatisRCU.rst-1073- hlist_bl_del_rcu
--
fs/dcache.c=2704=static void __d_rehash(struct dentry *entry)
--
fs/dcache.c-2708- hlist_bl_lock(b);
fs/dcache.c:2709: hlist_bl_add_head_rcu(&entry->d_hash, b);
fs/dcache.c-2710- hlist_bl_unlock(b);
--
fs/gfs2/quota.c=293=static int qd_get(struct gfs2_sbd *sdp, struct kqid qid,
--
fs/gfs2/quota.c-315- list_add(&new_qd->qd_list, &sdp->sd_quota_list);
fs/gfs2/quota.c:316: hlist_bl_add_head_rcu(&new_qd->qd_hlist, &qd_hash_table[hash]);
fs/gfs2/quota.c-317- atomic_inc(&sdp->sd_quota_count);
--
fs/gfs2/quota.c=1403=int gfs2_quota_init(struct gfs2_sbd *sdp)
--
fs/gfs2/quota.c-1497- atomic_inc(&sdp->sd_quota_count);
fs/gfs2/quota.c:1498: hlist_bl_add_head_rcu(&qd->qd_hlist, &qd_hash_table[hash]);
fs/gfs2/quota.c-1499- spin_unlock_bucket(hash);
--
include/linux/rculist_bl.h=17=static inline void hlist_bl_set_first_rcu(struct hlist_bl_head *h,
--
include/linux/rculist_bl.h-49- * (such as holding appropriate locks) to avoid racing
include/linux/rculist_bl.h:50: * with another list-mutation primitive, such as hlist_bl_add_head_rcu()
include/linux/rculist_bl.h-51- * or hlist_bl_del_rcu(), running on this same list.
--
include/linux/rculist_bl.h=56=static inline void hlist_bl_del_rcu(struct hlist_bl_node *n)
--
include/linux/rculist_bl.h-62-/**
include/linux/rculist_bl.h:63: * hlist_bl_add_head_rcu
include/linux/rculist_bl.h-64- * @n: the element to add to the hash list.
--
include/linux/rculist_bl.h-72- * (such as holding appropriate locks) to avoid racing
include/linux/rculist_bl.h:73: * with another list-mutation primitive, such as hlist_bl_add_head_rcu()
include/linux/rculist_bl.h-74- * or hlist_bl_del_rcu(), running on this same list.
--
include/linux/rculist_bl.h-80- */
include/linux/rculist_bl.h:81:static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n,
include/linux/rculist_bl.h-82- struct hlist_bl_head *h)
--
net/netfilter/ipvs/ip_vs_conn.c=243=static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
--
net/netfilter/ipvs/ip_vs_conn.c-277- refcount_inc(&cp->refcnt);
net/netfilter/ipvs/ip_vs_conn.c:278: hlist_bl_add_head_rcu(&cp->hn0.node, head);
net/netfilter/ipvs/ip_vs_conn.c-279- if (use2)
net/netfilter/ipvs/ip_vs_conn.c:280: hlist_bl_add_head_rcu(&cp->hn1.node, head2);
net/netfilter/ipvs/ip_vs_conn.c-281-
--
net/netfilter/ipvs/ip_vs_conn.c=620=void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport)
--
net/netfilter/ipvs/ip_vs_conn.c-738- hlist_bl_del_rcu(&hn->node);
net/netfilter/ipvs/ip_vs_conn.c:739: hlist_bl_add_head_rcu(&hn->node, head_new);
net/netfilter/ipvs/ip_vs_conn.c-740- }
--
net/netfilter/ipvs/ip_vs_conn.c=800=static void conn_resize_work_handler(struct work_struct *work)
--
net/netfilter/ipvs/ip_vs_conn.c-889- WRITE_ONCE(cp->hn1.hash_key, hash_key);
net/netfilter/ipvs/ip_vs_conn.c:890: hlist_bl_add_head_rcu(&hn->node, head2);
net/netfilter/ipvs/ip_vs_conn.c-891- hlist_bl_unlock(head2);
--
net/netfilter/ipvs/ip_vs_ctl.c=382=static int ip_vs_svc_hash(struct ip_vs_service *svc)
--
net/netfilter/ipvs/ip_vs_ctl.c-424- svc->flags |= IP_VS_SVC_F_HASHED;
net/netfilter/ipvs/ip_vs_ctl.c:425: hlist_bl_add_head_rcu(&svc->s_list, head);
net/netfilter/ipvs/ip_vs_ctl.c-426- hlist_bl_unlock(head);
--
net/netfilter/ipvs/ip_vs_ctl.c=670=static void svc_resize_work_handler(struct work_struct *work)
--
net/netfilter/ipvs/ip_vs_ctl.c-776- */
net/netfilter/ipvs/ip_vs_ctl.c:777: hlist_bl_add_head_rcu(&svc->s_list, head2);
net/netfilter/ipvs/ip_vs_ctl.c-778- hlist_bl_unlock(head2);
]
|
| 33/2 |
2026/07/05 22:00 |
llm |
expert |
0m
Model:
Tokens:
input: 34374
output: 35
thoughts: 1089
Thoughts: **Benign Race Condition in hlist_bl_add_head_rcu and hlist_bl_unhashed**
Okay, let's break this down. I've been looking at this race condition and it's present in several kernel subsystems – dcache, gfs2 quota, and ipvs. In each case, it's about hash tables with RCU readers. The potential race stems from readers checking `hlist_bl_unhashed`. If that check hinges on a simple non-NULL test, the race boils down to two non-NULL values clashing, which is generally fine.
First, I quickly confirmed that `hlist_bl_unhashed` *is* used for similar checks within IPVS. It's used in `ip_vs_conn_unhash` and related functions. Good, confirms the original understanding.
Now, the compiler reordering is the key. Specifically, the write to `first->pprev` might happen *before* the write to `h->first` in the code, like this:
```c
91: first->pprev = &n->next;
...
95: hlist_bl_set_first_rcu(h, n);
```
However, `hlist_bl_set_first_rcu` uses `rcu_assign_pointer`, which enforces a release barrier. This *guarantees* `first->pprev` is updated *before* `h->first`. Therefore, a reader seeing the new `h->first` (which is `n`) will *also* see the updated `first->pprev` if it follows `n->next`. The reader will correctly dereference `first->pprev`. If it *did* see the old one, it’s still non-NULL, and the overall effect is benign. Furthermore, the `hlist_bl_first_rcu_dereference` uses `rcu_dereference_check` which ensures the necessary ordering.
Looking at the report, the address of the relevant field is confirmed to be `0xffff88811a845a90`, corresponding to `first->d_hash.pprev`. The change in value in the report is precisely what's expected: the old `pprev` (pointing to `&h->first`, `0xffff88823753f650`) becoming the new `pprev` (pointing to `&n->next`, `0xffff888105f1a848`), and both are non-NULL.
The report also shows a write to the same address by task 15291 on cpu 0 ( `hlist_bl_add_head_rcu` at include/linux/rculist_bl.h:91) and a read by task 2981 on cpu 1 ( `hlist_bl_unhashed` at include/linux/list_bl.h:54), which is exactly the scenario I've been analyzing.
**Conclusion: The race is benign.**
However, to be absolutely safe and clear, I’d recommend adding `WRITE_ONCE` to `hlist_bl_add_head_rcu` and `READ_ONCE` to `hlist_bl_unhashed` - OR using `data_race()` to be less intrusive. Since the non-bit-locked equivalent function `hlist_add_head_rcu` *does* use `WRITE_ONCE`, `hlist_bl_add_head_rcu` should do the same.
Just checking one last thing – the non-bit-locked `hlist_unhashed` doesn't use `READ_ONCE`, but that's not the same scenario.
|
| 34/2 |
2026/07/05 22:00 |
tool |
read-file |
0m
Args: map[File:include/linux/list.h FirstLine:900 LineCount:20]
Results: map[Contents: 900: * @pos: the type * to use as a loop cursor.
901: * @n: another type * to use as temporary storage
902: * @head: the head for your list.
903: * @member: the name of the list_head within the struct.
904: */
905: #define list_for_each_entry_safe(pos, n, head, member) \
906: for (pos = list_first_entry(head, typeof(*pos), member), \
907: n = list_next_entry(pos, member); \
908: !list_entry_is_head(pos, head, member); \
909: pos = n, n = list_next_entry(n, member))
910:
911: /**
912: * list_for_each_entry_safe_continue - continue list iteration safe against removal
913: * @pos: the type * to use as a loop cursor.
914: * @n: another type * to use as temporary storage
915: * @head: the head for your list.
916: * @member: the name of the list_head within the struct.
917: *
918: * Iterate over list of given type, continuing after current point,
919: * safe against removal of list entry.
]
|
| 35/2 |
2026/07/05 22:00 |
llm |
expert |
0m
Model:
Tokens:
input: 36720
output: 22
thoughts: 9
Thoughts: Okay, here's the summary, expressed as my internal thought process, assuming I'm a seasoned professional in this area:
**Internal Reflection on Code Clarity**
Right, so I was looking at that code snippet... and immediately my mind jumped to `hlist_unhashed`. It was a slip of the tongue, a momentary mental shortcut, but it's important to be precise. When dealing with these low-level data structures, the specific terminology is crucial. There's a subtle but significant difference, and even a seasoned professional can let these things slide in thought, but it would not do for implementation.
I need to make sure I'm using the *right* term, ensuring absolute clarity, especially in a collaborative setting or for documentation. It’s about more than just correctness; it’s about maintainability and future-proofing the code. A seemingly minor imprecision now can create a cascade of confusion down the line, especially when handed off to another engineer. So, the clarification, even internally, is important.
|
| 36/2 |
2026/07/05 22:00 |
tool |
grepper |
0m
Args: map[Expression:static inline int hlist_unhashed]
Results: map[Output:include/linux/list.h=983=static inline void INIT_HLIST_NODE(struct hlist_node *h)
--
include/linux/list.h-996- */
include/linux/list.h:997:static inline int hlist_unhashed(const struct hlist_node *h)
include/linux/list.h-998-{
--
include/linux/list.h-1009- */
include/linux/list.h:1010:static inline int hlist_unhashed_lockless(const struct hlist_node *h)
include/linux/list.h-1011-{
--
scripts/include/list.h=322=static inline void INIT_HLIST_NODE(struct hlist_node *h)
--
scripts/include/list.h-335- */
scripts/include/list.h:336:static inline int hlist_unhashed(const struct hlist_node *h)
scripts/include/list.h-337-{
--
tools/include/linux/list.h=619=static inline void INIT_HLIST_NODE(struct hlist_node *h)
--
tools/include/linux/list.h-624-
tools/include/linux/list.h:625:static inline int hlist_unhashed(const struct hlist_node *h)
tools/include/linux/list.h-626-{
]
|
| 37/2 |
2026/07/05 22:00 |
llm |
expert |
0m
Model:
Tokens:
input: 37328
output: 35
thoughts: 32
Thoughts: Okay, let's dive into this. Here's my take on those `hlist` functions:
**Examining hlist_unhashed and hlist_unhashed_lockless**
Alright, so I'm focusing on `hlist_unhashed` and `hlist_unhashed_lockless` in `include/linux/list.h`. My immediate thought is, "What's the *unhashed* aspect of these lists trying to achieve?" I'm expecting this relates to hash table implementation, where these lists are likely used for collisions or some kind of probing. I anticipate the core difference between the two to be the use of locking. Let's dig deeper and see what the code reveals about the underlying structures and intended use-cases.
|
| 38/2 |
2026/07/05 22:00 |
tool |
read-file |
0m
Args: map[File:include/linux/list.h FirstLine:997 LineCount:20]
Results: map[Contents: 997: static inline int hlist_unhashed(const struct hlist_node *h)
998: {
999: return !h->pprev;
1000: }
1001:
1002: /**
1003: * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
1004: * @h: Node to be checked
1005: *
1006: * This variant of hlist_unhashed() must be used in lockless contexts
1007: * to avoid potential load-tearing. The READ_ONCE() is paired with the
1008: * various WRITE_ONCE() in hlist helpers that are defined below.
1009: */
1010: static inline int hlist_unhashed_lockless(const struct hlist_node *h)
1011: {
1012: return !READ_ONCE(h->pprev);
1013: }
1014:
1015: /**
1016: * hlist_empty - Is the specified hlist_head structure an empty hlist?
]
|
| 39/2 |
2026/07/05 22:00 |
llm |
expert |
0m
Model:
Tokens:
input: 37877
output: 1059
thoughts: 1806
Thoughts: **Analysis of `dentry` Hash List Concurrency**
Okay, here's what I've been thinking through. I noticed the use of `hlist_unhashed_lockless` and `READ_ONCE`, and then how `d_unhashed` relies on `hlist_bl_unhashed`. Since `d_unhashed` is used in lockless contexts, it seemed logical that it should employ a lockless variant of `hlist_bl_unhashed`.
However, after a closer look, I realized `d_unhashed` is used in multiple locations. In `__d_lookup`, it's called *after* acquiring `dentry->d_lock`. This means it *isn't* completely lockless concerning the `dentry` itself. But, and this is key, it *is* lockless regarding `__d_rehash`, which is responsible for adding *new* dentries to the head of the hash list.
This distinction is crucial: `dentry->d_lock` doesn't protect `dentry->d_hash.pprev` from modification by `__d_rehash`. In essence, `d_unhashed` *is* being used in a potentially racy manner.
To be perfectly correct, we should either use `READ_ONCE` in `hlist_bl_unhashed` or introduce a `hlist_bl_unhashed_lockless` version. Nevertheless, given the analysis, it is benign, and I will classify it as such.
Now, I had to double-check whether `dentry->d_lock` is intended to protect the `d_hash` structure. Usually, the bucket lock handles this, and `__d_rehash` does indeed hold that lock. However, `__d_lookup` does not, instead using RCU. Therefore, `__d_lookup` is a lockless reader of the hash list. The presence of `dentry->d_lock` in `__d_lookup` serves other purposes—stabilizing `d_parent` and `d_name`, but it *doesn't* synchronize the hash list pointers of `dentry`.
So, yes, it's a concurrent access issue.
One other point: `hlist_bl_add_head_rcu` contains this line:
```c
91: first->pprev = &n->next;
```
If `first` is *already* in the list, its `pprev` pointer is being updated. If `hlist_bl_for_each_entry_rcu` is used, the reader is traversing the list. If the reader is currently at `first`, it already holds a pointer to `first`. Then, it checks `d_unhashed(first)`.
Therefore, if `first->pprev` is updated concurrently, we have a data race.
I went and looked at other RCU list implementations. `hlist_add_head_rcu` uses `WRITE_ONCE(first->pprev, &n->next)`. This indicates that `hlist_bl_add_head_rcu` is indeed missing the necessary `WRITE_ONCE` protection.
Here's my conclusion:
**Summary:**
- A race exists between `__d_rehash` (the writer) and `__d_lookup` (the reader) on `dentry->d_hash.pprev`.
- `__d_rehash` updates `pprev` of the dentry at the head of the list.
- `__d_lookup` reads `pprev` through `d_unhashed` to see if the dentry is hashed.
- The update is from one non-NULL pointer to another non-NULL pointer.
- The reader only cares if the pointer is NULL.
- On the target architecture (x86\_64), aligned 64-bit pointer accesses are atomic.
- Even if a torn read occurred (unlikely), it would result in a false positive for `d_unhashed`, leading to a safe lookup failure.
- The race is benign in practice.
**Recommended fix:** Use `WRITE_ONCE` in `hlist_bl_add_head_rcu` and `READ_ONCE` in `hlist_bl_unhashed` (or a lockless variant).
I needed to check if `hlist_bl_unhashed` had to be exactly correct.
In `__d_drop`, the `dentry->d_lock` is held.
If `d_unhashed` returns a stale `false`, we call `___d_drop`.
`___d_drop` takes the bucket
The KCSAN data race report identifies a race between `__d_rehash` (writer) and `__d_lookup` (reader) on the `pprev` field of a `dentry->d_hash` node (part of an `hlist_bl` list).
### Analysis
**1. Context:**
- **Writer (`__d_rehash`):** This function is adding a new dentry (`n`) to the head of a hash bucket. If the bucket was not empty, it updates the `pprev` pointer of the previous head dentry (`first`) to point to the `next` field of the new dentry. This is done inside `hlist_bl_add_head_rcu` at line 91: `first->pprev = &n->next;`. The writer holds the hash bucket lock (`hlist_bl_lock`).
- **Reader (`__d_lookup`):** This function iterates through the hash bucket under `rcu_read_lock()`. When it finds a candidate dentry, it takes `dentry->d_lock` and then checks if the dentry is still hashed using `d_unhashed(dentry)`. `d_unhashed` calls `hlist_bl_unhashed`, which simply checks if `dentry->d_hash.pprev` is `NULL`.
**2. The Race:**
The race occurs because `__d_lookup` reads `dentry->d_hash.pprev` without holding the bucket lock, while `__d_rehash` might be updating it. Although `__d_lookup` holds `dentry->d_lock`, `__d_rehash` only holds the lock of the *new* dentry being added, not the lock of the existing dentry (`first`) whose `pprev` it is updating.
**3. Impact and Severity:**
- **Value Transition:** In `__d_rehash`, the `pprev` pointer of the existing dentry is being changed from `&h->first` (pointing to the bucket head) to `&n->next` (pointing to the new dentry's next pointer). Both of these are valid, non-NULL kernel addresses.
- **Logic Tolerance:** The reader (`d_unhashed`) only cares whether `pprev` is `NULL`. Since the value is changing from one non-NULL value to another non-NULL value, the result of `d_unhashed` remains `false` throughout the update.
- **Atomicity:** On the reported architecture (x86_64), aligned 64-bit pointer writes and reads are atomic. Even if the compiler were to produce a torn read (which is highly unlikely for a simple pointer access), the worst-case scenario would be `d_unhashed` returning `true` (a false positive for being unhashed). This would cause `__d_lookup` to skip the dentry and fall back to a slow-path lookup, which is a minor performance penalty but not a correctness issue.
- **Consistency:** The standard RCU hlist implementation (`hlist_add_head_rcu`) uses `WRITE_ONCE` for this exact update, and there is a `hlist_unhashed_lockless` variant that uses `READ_ONCE`. The bit-locked version (`hlist_bl_add_head_rcu`) appears to be missing these annotations, which is what KCSAN is flagging.
### Conclusion
The data race is **BENIGN**. It does not lead to incorrect logic, crashes, or data corruption. The check `d_unhashed` is structurally tolerant to seeing either the old or new non-NULL pointer value.
### Recommended Annotations
To silence the KCSAN report and ensure compiler consistency, the following changes are recommended:
1. Use `WRITE_ONCE(first->pprev, &n->next)` in `hlist_bl_add_head_rcu`.
2. Use `READ_ONCE(h->pprev)` in `hlist_bl_unhashed` or introduce a `hlist_bl_unhashed_lockless` variant.
```c
// include/linux/rculist_bl.h
static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n, struct hlist_bl_head *h) {
...
if (first)
WRITE_ONCE(first->pprev, &n->next); // Mark the concurrent write
...
}
// include/linux/list_bl.h
static inline bool hlist_bl_unhashed(const struct hlist_bl_node *h) {
return !READ_ONCE(h->pprev); // Mark the concurrent read
}
```
|
| 40/2 |
2026/07/05 22:01 |
tool |
set-results |
0m
Args:
Results:
|
| 41/2 |
2026/07/05 22:01 |
llm |
expert |
0m
Model:
Tokens:
input: 41780
output: 673
thoughts: 38
Thoughts: Okay, here's my interpretation of those thoughts, framed as a summary of my own reasoning:
**Preparing the Final Output**
Alright, I see the request. The user needs a summary of the analysis I've done, but they want it presented in a specific way. I've already meticulously crafted a detailed analysis and drawn a clear conclusion, all within the "thought block." My task now is to take that work and tailor it to meet the requirements of the final output. Essentially, I'm packaging my existing findings into the format the user needs. I understand the importance of clear communication, especially when dealing with complex information. So, I will proceed to do just that, ensuring the summary is both concise and faithful to the prior analysis and conclusion.
|
| 42/1 |
2026/07/05 22:01 |
action |
format-explanation |
0m
Results: map[Explanation:The data race occurs between `__d_rehash` (writer) and `__d_lookup` (reader) on
the `pprev` field of a dentry's hash node (`dentry->d_hash.pprev`).
### Race Summary
* **Writer (`__d_rehash`):** When adding a new dentry to the head of a hash
bucket, it updates the `pprev` pointer of the dentry that was previously at the
head. This happens in `hlist_bl_add_head_rcu` (inlined in `__d_rehash`):
`first->pprev = &n->next;`.
* **Reader (`__d_lookup`):** While iterating the hash table, it checks if a
candidate dentry is still hashed via `d_unhashed(dentry)`, which reads
`dentry->d_hash.pprev`.
### Synchronization Context
The writer holds the hash bucket's bit-lock (`hlist_bl_lock`) and the new
dentry's `d_lock`. The reader holds `rcu_read_lock()` and the candidate dentry's
`d_lock`. However, the candidate dentry's `d_lock` does not protect its `pprev`
pointer from being updated by a concurrent `__d_rehash` adding a *different*
dentry to the same bucket head.
### Final Classification: **BENIGN**
**Reasoning:**
The race is structurally tolerant to the concurrent update for several reasons:
1. **Invariant Result:** The writer updates `pprev` from one non-NULL value
(`&h->first`) to another non-NULL value (`&n->next`). The reader (`d_unhashed`)
only checks if the value is `NULL`. Therefore, the reader will correctly see the
dentry as "hashed" regardless of whether it sees the old or new pointer value.
2. **Atomicity:** On x86_64, aligned 64-bit pointer accesses are atomic. A torn
read is not expected. Even if a hypothetical torn read resulted in a `NULL`
value, `__d_lookup` would simply skip the dentry (a false negative), leading to
a slow-path lookup which is safe.
3. **RCU Guarantees:** The publication of the new head is correctly ordered via
`rcu_assign_pointer` in `hlist_bl_set_first_rcu`, ensuring that any reader
following the new head will see a consistent state.
**Recommended Annotations:**
While benign, the code should be annotated to follow the pattern used in the
non-bit-locked RCU list implementation (`hlist_add_head_rcu`):
* In `include/linux/rculist_bl.h`, use `WRITE_ONCE(first->pprev, &n->next);`
in `hlist_bl_add_head_rcu`.
* In `include/linux/list_bl.h`, use `READ_ONCE(h->pprev)` in
`hlist_bl_unhashed`.]
|