| 0/0 |
2026/02/23 15:19 |
flow |
assessment-kcsan |
10m
Results: map[Benign:true Confident:true Explanation:The data race occurs between `dentry_unlink_inode` (which clears `dentry->d_inode` to `NULL`) and `step_into` (which reads `dentry->d_inode` during an RCU-walk path lookup).
### Analysis
1. **Synchronization Mechanism**: The `dentry->d_inode` field is protected by the `dentry->d_seq` sequence counter (a seqlock).
* **Writer**: In `dentry_unlink_inode`, the write to `d_inode` is wrapped in `raw_write_seqcount_begin(&dentry->d_seq)` and `raw_write_seqcount_end(&dentry->d_seq)`. These primitives include the necessary memory barriers (`smp_wmb()`) to ensure that the sequence counter is updated before and after the pointer is cleared.
* **Reader**: In `step_into`, the read of `dentry->d_inode` is followed by `read_seqcount_retry(&dentry->d_seq, nd->next_seq)`. This check includes an `smp_rmb()` to ensure the pointer is read before the sequence counter is checked.
2. **Race Handling**: If the reader accesses `d_inode` while the writer is clearing it, the sequence counter will either be odd (indicating a write in progress) or will have changed from the value sampled at the start of the walk (`nd->next_seq`). In either case, `read_seqcount_retry` will return `true`, causing the reader to abort the RCU-walk and retry in the slow path (ref-walk).
3. **Atomicity and Validity**:
* On the reported 64-bit architecture (x86_64), 8-byte aligned pointer accesses are atomic. This prevents "word tearing" where a reader might see a partially updated pointer.
* Inodes in the Linux kernel are typically RCU-protected (using `call_rcu` in `destroy_inode`). This ensures that even if a reader obtains a pointer to an inode that is being unlinked/freed, the memory remains valid for the duration of the RCU read-side critical section.
4. **VFS Design**: This pattern is a core part of the VFS "rcu-walk" design, which prioritizes performance by avoiding heavy locking or atomic operations (like `READ_ONCE`/`WRITE_ONCE`) where the sequence counter already provides sufficient consistency guarantees.
### Conclusion
The data race is **benign**. While KCSAN correctly identifies the concurrent plain accesses, the VFS logic explicitly handles this concurrency using the dentry sequence counter to ensure that any inconsistent state read during the race is detected and discarded.]
|
| 1/1 |
2026/02/23 15:19 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/src/22b53f5d3c32fa7512dcfc9c7f3ae37c008c3955]
|
| 2/1 |
2026/02/23 15:20 |
action |
kernel-builder |
3m
Results: map[KernelObj:/usr/local/google/home/dvyukov/syzkaller/agent/workdir/cache/build/f088e7c04026f6805eaa66f1d1ae7669f331d79f]
|
| 3/1 |
2026/02/23 15:24 |
action |
codesearch-prepare |
2m
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/02/23 15:27 |
agent |
expert |
3m
Model:
Results: map[Benign:true Confident:true]
Instruction:
You are an experienced Linux kernel developer tasked with determining if the given kernel
data race is benign or not. The data race report is from KCSAN tool.
It contains 2 stack traces of the memory accesses that constitute a data race.
A "benign" data races are on a simple int/bool variable or similar field,
and the accesses are not supposed to be protected by any mutual exclusion primitives.
Common examples of such "benign" data races are accesses to various flags fields,
statistics counters, and similar. A "benign" data race does not lead to memory corruption/crash
with a conservative compiler that compiles memory accesses to primitive types
effectively as atomic.
A non-benign (or "harmful" data race) can lead to corruption/crash even with
a conservative compiler that compiles memory accesses to primitive types
effectively as atomic. A common example of a "harmful" data races is race on
a complex container (list/hashmap/etc), where accesses are supposed to be protected
by a mutual exclusion primitive.
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 dentry_unlink_inode / path_openat
write to 0xffff888107d55270 of 8 bytes by task 3307 on cpu 0:
__d_clear_type_and_inode fs/dcache.c:420 [inline]
dentry_unlink_inode+0x65/0x260 fs/dcache.c:457
d_delete+0xc8/0xe0 fs/dcache.c:2530
d_delete_notify include/linux/fsnotify.h:375 [inline]
vfs_rmdir+0x1ae/0x440 fs/namei.c:5351
filename_rmdir+0x1a6/0x360 fs/namei.c:5393
__do_sys_rmdir fs/namei.c:5416 [inline]
__se_sys_rmdir+0x2b/0xe0 fs/namei.c:5413
__x64_sys_rmdir+0x1f/0x30 fs/namei.c:5413
x64_sys_call+0x2448/0x3020 arch/x86/include/generated/asm/syscalls_64.h:85
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x12c/0x370 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888107d55270 of 8 bytes by task 3305 on cpu 1:
step_into fs/namei.c:2138 [inline]
open_last_lookups fs/namei.c:4618 [inline]
path_openat+0x65f/0x2050 fs/namei.c:4827
do_file_open+0x16c/0x290 fs/namei.c:4859
do_sys_openat2+0x94/0x130 fs/open.c:1366
do_sys_open fs/open.c:1372 [inline]
__do_sys_openat fs/open.c:1388 [inline]
__se_sys_openat fs/open.c:1383 [inline]
__x64_sys_openat+0xf2/0x120 fs/open.c:1383
x64_sys_call+0x1e39/0x3020 arch/x86/include/generated/asm/syscalls_64.h:258
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x12c/0x370 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0xffff888105696648 -> 0x0000000000000000
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 3305 Comm: udevd Tainted: G W syzkaller #0 PREEMPT(full)
Tainted: [W]=WARN
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 02/12/2026
==================================================================
Reply: The data race occurs between `dentry_unlink_inode` (which clears `dentry->d_inode` to `NULL`) and `step_into` (which reads `dentry->d_inode` during an RCU-walk path lookup).
### Analysis
1. **Synchronization Mechanism**: The `dentry->d_inode` field is protected by the `dentry->d_seq` sequence counter (a seqlock).
* **Writer**: In `dentry_unlink_inode`, the write to `d_inode` is wrapped in `raw_write_seqcount_begin(&dentry->d_seq)` and `raw_write_seqcount_end(&dentry->d_seq)`. These primitives include the necessary memory barriers (`smp_wmb()`) to ensure that the sequence counter is updated before and after the pointer is cleared.
* **Reader**: In `step_into`, the read of `dentry->d_inode` is followed by `read_seqcount_retry(&dentry->d_seq, nd->next_seq)`. This check includes an `smp_rmb()` to ensure the pointer is read before the sequence counter is checked.
2. **Race Handling**: If the reader accesses `d_inode` while the writer is clearing it, the sequence counter will either be odd (indicating a write in progress) or will have changed from the value sampled at the start of the walk (`nd->next_seq`). In either case, `read_seqcount_retry` will return `true`, causing the reader to abort the RCU-walk and retry in the slow path (ref-walk).
3. **Atomicity and Validity**:
* On the reported 64-bit architecture (x86_64), 8-byte aligned pointer accesses are atomic. This prevents "word tearing" where a reader might see a partially updated pointer.
* Inodes in the Linux kernel are typically RCU-protected (using `call_rcu` in `destroy_inode`). This ensures that even if a reader obtains a pointer to an inode that is being unlinked/freed, the memory remains valid for the duration of the RCU read-side critical section.
4. **VFS Design**: This pattern is a core part of the VFS "rcu-walk" design, which prioritizes performance by avoiding heavy locking or atomic operations (like `READ_ONCE`/`WRITE_ONCE`) where the sequence counter already provides sufficient consistency guarantees.
### Conclusion
The data race is **benign**. While KCSAN correctly identifies the concurrent plain accesses, the VFS logic explicitly handles this concurrency using the dentry sequence counter to ensure that any inconsistent state read during the race is detected and discarded.
|
| 5/2 |
2026/02/23 15:27 |
llm |
expert |
0m
Model:
Tokens:
input: 4004
output: 83
thoughts: 157
Thoughts: **Analyzing a Data Race Vulnerability**
Okay, so the report's screaming about a nasty data race. Specifically, it's highlighting a conflict between `dentry_unlink_inode` (which is writing) and `path_openat` (which is reading) on an 8-byte value at memory address `0xffff888107d55270`. The interesting bit is the value's transformation: it's going from `0xffff888105696648` to `0x0000000000000000`. That smells like a pointer being nullified, which is precisely the kind of operation that often leads to race conditions.
The write is happening inside `__d_clear_type_and_inode`, which is called as part of `dentry_unlink_inode`. That makes sense; unlinking a dentry often involves clearing its associated inode pointer. The read, on the other hand, is deep within `path_openat` – in `step_into`, which suggests it's likely occurring during the path resolution process. So, `path_openat` is presumably trying to access something that `dentry_unlink_inode` is in the process of deleting.
This is the core of the problem: a pointer is being invalidated *while* another process attempts to use it. This merits a deep dive into the code. Time to dig in and see the exact memory access and what the code's doing around it.
|
| 6/2 |
2026/02/23 15:27 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:__d_clear_type_and_inode]
Results: map[SourceCode: 414: static inline void __d_clear_type_and_inode(struct dentry *dentry)
415: {
416: unsigned flags = READ_ONCE(dentry->d_flags);
417:
418: flags &= ~DCACHE_ENTRY_TYPE;
419: WRITE_ONCE(dentry->d_flags, flags);
420: dentry->d_inode = NULL;
421: /*
422: * The negative counter only tracks dentries on the LRU. Don't inc if
423: * d_lru is on another list.
424: */
425: if ((flags & (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST)
426: this_cpu_inc(nr_dentry_negative);
427: }
SourceFile:fs/dcache.c]
|
| 7/2 |
2026/02/23 15:27 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/namei.c IncludeLines:true Name:step_into]
Results: map[SourceCode:2127: static __always_inline const char *step_into(struct nameidata *nd, int flags,
2128: struct dentry *dentry)
2129: {
2130: /*
2131: * In the common case we are in rcu-walk and traversing over a non-mounted on
2132: * directory (as opposed to e.g., a symlink).
2133: *
2134: * We can handle that and negative entries with the checks below.
2135: */
2136: if (likely((nd->flags & LOOKUP_RCU) &&
2137: !d_managed(dentry) && !d_is_symlink(dentry))) {
2138: struct inode *inode = dentry->d_inode;
2139: if (read_seqcount_retry(&dentry->d_seq, nd->next_seq))
2140: return ERR_PTR(-ECHILD);
2141: if (unlikely(!inode))
2142: return ERR_PTR(-ENOENT);
2143: nd->path.dentry = dentry;
2144: /* nd->path.mnt is retained on purpose */
2145: nd->inode = inode;
2146: nd->seq = nd->next_seq;
2147: return NULL;
2148: }
2149: return step_into_slowpath(nd, flags, dentry);
2150: }
SourceFile:fs/namei.c]
|
| 8/2 |
2026/02/23 15:27 |
llm |
expert |
0m
Model:
Tokens:
input: 5270
output: 41
thoughts: 184
Thoughts: **Analyzing a Potential Race Condition**
Okay, so I'm looking at this code and something's pinging my radar. Line 420, within `__d_clear_type_and_inode`, we have a write operation: `dentry->d_inode = NULL;`. That's setting the inode pointer to null, makes sense, typical unlinking operation. Now, looking at `step_into`, specifically line 2138, there's a read: `struct inode *inode = dentry->d_inode;`.
Immediately following that read, we call `read_seqcount_retry(&dentry->d_seq, nd->next_seq)`. That function is crucial. If it returns `true`, then the `dentry` *has* changed since `nd->next_seq` was sampled, and the read value from the previous line is potentially stale and discarded. If it's `false`, then the read value from `dentry->d_inode` is considered valid because the `dentry` has not changed between the initial read and now.
Hmm, wait a minute, `__d_clear_type_and_inode` is called from `dentry_unlink_inode`. That's important. I need to understand the full context. Let's dig into `dentry_unlink_inode` and see how it interacts with the read operation in `step_into`. I suspect there might be a race here, depending on exactly *how* these two functions are called and scheduled. This needs further investigation.
|
| 9/2 |
2026/02/23 15:27 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:dentry_unlink_inode]
Results: map[SourceCode: 446: /*
447: * Release the dentry's inode, using the filesystem
448: * d_iput() operation if defined.
449: */
450: static void dentry_unlink_inode(struct dentry * dentry)
451: __releases(dentry->d_lock)
452: __releases(dentry->d_inode->i_lock)
453: {
454: struct inode *inode = dentry->d_inode;
455:
456: raw_write_seqcount_begin(&dentry->d_seq);
457: __d_clear_type_and_inode(dentry);
458: hlist_del_init(&dentry->d_u.d_alias);
459: raw_write_seqcount_end(&dentry->d_seq);
460: spin_unlock(&dentry->d_lock);
461: spin_unlock(&inode->i_lock);
462: if (!inode->i_nlink)
463: fsnotify_inoderemove(inode);
464: if (dentry->d_op && dentry->d_op->d_iput)
465: dentry->d_op->d_iput(dentry, inode);
466: else
467: iput(inode);
468: }
SourceFile:fs/dcache.c]
|
| 10/2 |
2026/02/23 15:27 |
llm |
expert |
0m
Model:
Tokens:
input: 6246
output: 38
thoughts: 554
Thoughts: **Analyzing a Potential Data Race in dentry Unlinking**
Okay, so I'm looking at this section of the VFS code, specifically around dentry unlinking and traversal. My initial thought is that the use of `dentry->d_seq` seems like a textbook case of sequence lock protection, but there's a potential wrinkle that's making me raise an eyebrow.
I see that `dentry_unlink_inode` is clearing the inode pointer of a dentry:
```c
__d_clear_type_and_inode(dentry);
```
This write, along with other related operations, is protected by a sequence lock (`dentry->d_seq`), with calls to `raw_write_seqcount_begin` and `raw_write_seqcount_end`. Makes sense so far, standard sequence lock usage to protect the update.
Then, I'm tracing where that dentry is used. Specifically `step_into`, where there is a read of `dentry->d_inode`:
```c
struct inode *inode = dentry->d_inode;
```
This read is followed by `read_seqcount_retry(&dentry->d_seq, nd->next_seq)`. This is the typical read-side validation, checking if the sequence number has changed since the beginning of the read-side critical section.
Here's where it gets interesting. I'm wondering if this is completely safe, as KCSAN is flagging this as a data race because the `dentry->d_inode` isn't using `WRITE_ONCE` or `READ_ONCE`. On the surface, it seems like a race because the write and read are both just normal memory accesses. But the sequence lock is there, right?
The crucial bit, and what I need to carefully consider, is the sequence number validation. If `read_seqcount_retry` returns false, it means the read *either* happened before the write sequence started *or* after it completed. Given that `nd->next_seq` is usually sampled before `step_into`, this seems correct, the retry means the read succeeded. So, in theory, it should be fine, and I can ignore the KCSAN warning. I can't be sure, however, without examining where `nd->next_seq` comes from.
`nd->next_seq` is sampled *before* we get into `step_into`. I need to figure out how far before, in order to validate my thinking.
Okay, let's see how `nd->next_seq` is set. It looks like it's coming from `path_openat` -> `open_last_lookups` -> `step_into`. Also, `step_into` is called from `walk_component`. Let's pause and investigate `walk_component` to make sure I am accounting for everything.
|
| 11/2 |
2026/02/23 15:27 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/namei.c IncludeLines:true Name:walk_component]
Results: map[SourceCode:2262: static __always_inline const char *walk_component(struct nameidata *nd, int flags)
2263: {
2264: struct dentry *dentry;
2265: /*
2266: * "." and ".." are special - ".." especially so because it has
2267: * to be able to know about the current root directory and
2268: * parent relationships.
2269: */
2270: if (unlikely(nd->last_type != LAST_NORM)) {
2271: if (unlikely(nd->depth) && !(flags & WALK_MORE))
2272: put_link(nd);
2273: return handle_dots(nd, nd->last_type);
2274: }
2275: dentry = lookup_fast(nd);
2276: if (IS_ERR(dentry))
2277: return ERR_CAST(dentry);
2278: if (unlikely(!dentry)) {
2279: dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
2280: if (IS_ERR(dentry))
2281: return ERR_CAST(dentry);
2282: }
2283: if (unlikely(nd->depth) && !(flags & WALK_MORE))
2284: put_link(nd);
2285: return step_into(nd, flags, dentry);
2286: }
SourceFile:fs/namei.c]
|
| 12/2 |
2026/02/23 15:27 |
llm |
expert |
0m
Model:
Tokens:
input: 7888
output: 32
thoughts: 660
Thoughts: **Analyzing Sequence Count Validation in the Dentry Lookup Path**
Okay, so I'm digging into how the kernel handles concurrent access to dentry structures, specifically focusing on the sequence count mechanism. I see in `lookup_fast` that `nd->next_seq` gets its value from a `__d_lookup_rcu` call, which I'm assuming is the initial read of the sequence count. Then, later, in `step_into`, there's a critical check using `read_seqcount_retry` against that `nd->next_seq`. The intent here is clear: to ensure that the `dentry` hasn't changed *between* the initial lookup and the time we try to use it. That's the core of the RCU protection.
My main concern is whether this is robust enough. The scenario I'm most interested in is a race between a lookup and a dentry modification, specifically the unlinking of a dentry via `dentry_unlink_inode`. When `dentry_unlink_inode` happens, `raw_write_seqcount_begin` should increment the sequence number which would invalidate the initial sequence number, and so invalidate any read of the `dentry->d_inode`.
Let me think this through. `raw_write_seqcount_begin` makes the sequence number odd. And `read_seqcount_retry` will flag either an odd number or a change in the sequence number. So any read after an unlink should be caught, right? The sequence number will be odd, so we will know. If there is a write going on it would also be caught.
However, I'm now worrying about the compiler. Could the compiler mess with this somehow, possibly splitting the read or write of `dentry->d_inode`? That's what I'm concerned about. We are talking about a pointer here (8 bytes on a 64-bit machine). Accessing a pointer should be atomic, because it's an 8-byte aligned access, right? That should be fine. Is the `dentry->d_inode` pointer properly aligned in the `dentry` structure? I better check the `struct dentry` definition to be sure.
|
| 13/2 |
2026/02/23 15:27 |
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]]]
|
| 14/2 |
2026/02/23 15:27 |
llm |
expert |
0m
Model:
Tokens:
input: 9406
output: 44
thoughts: 614
Thoughts: **Analyzing a Data Race Report**
Okay, so KCSAN flagged a potential data race, and I need to figure out if it's a false positive or a genuine issue. The race is between writing `dentry->d_inode = NULL` in `__d_clear_type_and_inode` and reading `struct inode *inode = dentry->d_inode` in `step_into`. Both are plain accesses, which is what triggered the report.
My initial thought is, "Wait, this should be protected!" `dentry->d_seq` is a seqlock, and the reader uses `read_seqcount_retry(&dentry->d_seq, nd->next_seq)` to validate the read. If a write is in progress, the sequence number should be odd, and the reader should retry.
Now I'm diving into the `read_seqcount_retry` implementation. It seems straightforward: it checks if the current sequence number (`s->sequence`) is different from the start sequence (`start`). A write starts with `raw_write_seqcount_begin`, which increments the sequence. If the `start` was even, and `s->sequence` is now odd (write in progress) or another even (new write finished), it'll return true.
This means the read value of `dentry->d_inode` is only *used* if `read_seqcount_retry` returns *false*. That is, the read is outside of the `d_inode` write operation. This seems like it should be correct behavior.
I'm verifying that the write is properly wrapped. In `dentry_unlink_inode`, I see that the write, `__d_clear_type_and_inode(dentry)`, is indeed enclosed within `raw_write_seqcount_begin` and `raw_write_seqcount_end`. Looks good.
But wait a minute... could the compiler reorder the reads? Could it read `dentry->d_inode` *before* `dentry->d_seq`? I need to check if `read_seqcount_retry` includes a memory barrier. Time to review that implementation carefully.
|
| 15/2 |
2026/02/23 15:27 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:include/linux/seqlock.h IncludeLines:true Name:read_seqcount_retry]
Results: map[SourceCode: SourceFile:]
|
| 16/2 |
2026/02/23 15:27 |
llm |
expert |
0m
Model:
Tokens:
input: 10552
output: 45
thoughts: 10
Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:
**Examining the Sequencing Count Retry Function**
Alright, I'm thinking about this `__read_seqcount_retry` function. The name itself suggests it's involved in handling some kind of sequencing or count-related operation that might fail or need a retry. My first instinct is to consider the common failure modes it might be designed to address. Is it network-related? Perhaps issues with accessing a shared resource, or temporary inconsistencies in the data itself? Or maybe it's related to some external service with rate limits.
I need to understand what constitutes a "retry." What are the conditions that trigger it? Is it based on an error code, a timeout, or something else entirely? What's the retry logic? Are we talking about a simple exponential backoff, or something more sophisticated like circuit breaking? And how many attempts are we making? We don’t want to be hammering away endlessly at a failed endpoint, that’s just a recipe for making things worse.
I'll also want to know the scope of the function. Is it a generic retry mechanism applicable to many different operations, or is it highly specific to a particular sequence counting procedure? If it's generic, what are the parameters it takes to customize its behavior for different scenarios? If it's specific, understanding the "why" behind this dedicated retry is key.
My focus now will be on identifying the potential vulnerabilities that this retry function is designed to mitigate. I'll be looking for clues in the surrounding code regarding the nature of the data it's handling, and what could go wrong. I’ll need to understand the implications of a failed read operation, and the effect this may have on other parts of the system. I also need to think about how this affects concurrency and data consistency.
I'm essentially trying to trace the potential error paths in the execution. And I will also think about, and try to find answers to:
- what kind of data is being read?
- how is it used downstream?
- what failure modes is it trying to address?
- what are the costs of retrying?
- and what are the effects if a retry fails, what do we do then?
|
| 17/2 |
2026/02/23 15:27 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:include/linux/seqlock.h IncludeLines:true Name:__read_seqcount_retry]
Results: map[SourceCode: SourceFile:]
|
| 18/2 |
2026/02/23 15:27 |
llm |
expert |
0m
Model:
Tokens:
input: 11111
output: 22
thoughts: 7
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Initial Assessment: Leveraging the Power of `grep`**
Alright, the initial prompt or problem I'm facing suggests a need for rapid text searching and pattern matching within a dataset, likely some kind of log file, configuration file, source code, or unstructured text. My immediate instinct is to think, "Let's grep for it." The implied "it" is the specific piece of information or the recurring pattern that I'm trying to identify, isolate, or extract. I'm essentially leveraging the power and efficiency of the `grep` utility, which is a fundamental tool in my arsenal for these kinds of tasks. It's a pragmatic, often first-pass solution, and probably the most expeditious way to start dissecting the problem. Using this tool allows me to quickly assess the presence or absence of a string. I can further refine my understanding of the data by searching with more complex regular expressions, and further filter the output to find what I'm looking for. It's the go-to, rapid-prototyping step.
|
| 19/2 |
2026/02/23 15:28 |
tool |
grepper |
0m
Args: map[Expression:#define read_seqcount_retry]
Results: map[Output:include/linux/seqlock.h=389=static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
--
include/linux/seqlock.h-405- */
include/linux/seqlock.h:406:#define read_seqcount_retry(s, start) \
include/linux/seqlock.h-407- do_read_seqcount_retry(seqprop_const_ptr(s), start)
]
|
| 20/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 11515
output: 46
thoughts: 14
Thoughts: Okay, let's dive into this `do_read_seqcount_retry` function.
**Analyzing `do_read_seqcount_retry`**
Right, so I'm looking at this `do_read_seqcount_retry` function... let's see what's going on here. The name itself suggests a mechanism to handle retries when reading something that's likely related to sequence counters, which is common in concurrent systems to provide some guarantees of order or consistency, possibly in shared resources. I immediately think about the potential concurrency issues we're likely trying to address here, read vs write contention, maybe even with some form of optimistic locking or versioning under the hood. The retry aspect tells me that these read operations are likely *not* atomic, otherwise a simple read wouldn't need a retry function. This also makes me consider potential race conditions and the strategies to mitigate them. I'll need to figure out the context of what's being read, and what are likely failure modes that trigger the retry logic. Is it a counter? A flag? A pointer? Is it a sequence number used to determine the valid data within a data structure? Is the read operation part of some larger critical section? Right, I'll need to look at the surrounding code to determine how this function is used and what is being protected or accessed.
|
| 21/2 |
2026/02/23 15:28 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/seqlock.h IncludeLines:true Name:do_read_seqcount_retry]
Results: map[SourceCode: 409: static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
410: {
411: smp_rmb();
412: return do___read_seqcount_retry(s, start);
413: }
SourceFile:include/linux/seqlock.h]
|
| 22/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 11970
output: 46
thoughts: 305
Thoughts: **Understanding the Memory Barrier in `read_seqcount_retry`**
Okay, so I'm diving into how this memory barrier works, specifically within the context of `read_seqcount_retry` and its interaction with `dentry` updates. I'm focusing on the sequence dependencies.
My initial thought is that `smp_rmb()` in `read_seqcount_retry` ensures the read of `dentry->d_inode` completes *before* the subsequent read of `dentry->d_seq`. I need to ensure the `d_inode` has been fully read before checking the sequence number. I need to make sure the reads are ordered as expected.
Looking at `step_into`, this is precisely what's happening. We have a read of `dentry->d_inode` at line 2138, followed by `read_seqcount_retry` at 2139. The barrier within `read_seqcount_retry` is guaranteeing that the `d_inode` read at 2138 happens before the `d_seq` check. The `smp_rmb()` is the magic ingredient here.
Now, let's switch gears and consider the write side. We've got `raw_write_seqcount_begin` being called before `__d_clear_type_and_inode(dentry)`. Intuitively, `raw_write_seqcount_begin` *should* have a memory barrier, right? I need to verify that this barrier ensures that `d_seq` is updated *before* any changes to `d_inode` are made. Let me check the definition of `raw_write_seqcount_begin`. The write-side barrier is what matters for maintaining consistency. I need to ensure the sequence number is updated first to give readers a chance to notice.
|
| 23/2 |
2026/02/23 15:28 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:include/linux/seqlock.h IncludeLines:true Name:raw_write_seqcount_begin]
Results: map[SourceCode: SourceFile:]
|
| 24/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 12738
output: 24
thoughts: 5
Thoughts: Okay, here's my summary, presented as a stream of thought:
**Finding the Needle in the Haystack**
Right, let's get down to brass tacks. I've got a problem, a data problem. I need to pinpoint something specific within a larger, likely messy, dataset. My immediate instinct is to reach for a tool, something efficient and targeted. And, honestly, "grep" pops right into my head. It's the go-to, the workhorse, the first line of defense in these situations. Think about it: text-based searching, patterns, quickly sifting through a file or set of files... it's the natural choice when you need to quickly locate something, or even just some part of the whole. This feels like the right tool for the job. No complex queries, no unnecessary overhead – just a straightforward way to search the data.
|
| 25/2 |
2026/02/23 15:28 |
tool |
grepper |
0m
Args: map[Expression:#define raw_write_seqcount_begin]
Results: map[Output:include/linux/seqlock.h=409=static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
--
include/linux/seqlock.h-420- */
include/linux/seqlock.h:421:#define raw_write_seqcount_begin(s) \
include/linux/seqlock.h-422-do { \
]
|
| 26/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 13069
output: 24
thoughts: 7
Thoughts: Okay, here's my attempt at summarizing the thought process, tailored for an expert audience, and written in the first person:
**Initial Assessment and Information Retrieval**
Alright, so the task at hand seems to require a formal definition as the starting point. My first instinct, as always, is to establish a solid foundation of understanding. That means immediately seeking out and examining the core definition of the relevant concept. This isn't just about parroting a definition; it's about anchoring myself in the fundamental principles before diving into any potentially nuanced or complex aspects. The definition will be my baseline. I need to be sure I'm working from the same fundamental understanding as the source material.
|
| 27/2 |
2026/02/23 15:28 |
tool |
read-file |
0m
Args: map[File:include/linux/seqlock.h]
Results: map[Contents:/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LINUX_SEQLOCK_H
#define __LINUX_SEQLOCK_H
/*
* seqcount_t / seqlock_t - a reader-writer consistency mechanism with
* lockless readers (read-only retry loops), and no writer starvation.
*
* See Documentation/locking/seqlock.rst
*
* Copyrights:
* - Based on x86_64 vsyscall gettimeofday: Keith Owens, Andrea Arcangeli
* - Sequence counters with associated locks, (C) 2020 Linutronix GmbH
*/
#include <linux/compiler.h>
#include <linux/cleanup.h>
#include <linux/kcsan-checks.h>
#include <linux/lockdep.h>
#include <linux/mutex.h>
#include <linux/preempt.h>
#include <linux/seqlock_types.h>
#include <linux/spinlock.h>
#include <asm/processor.h>
/*
* The seqlock seqcount_t interface does not prescribe a precise sequence of
* read begin/retry/end. For readers, typically there is a call to
* read_seqcount_begin() and read_seqcount_retry(), however, there are more
* esoteric cases which do not follow this pattern.
*
* As a consequence, we take the following best-effort approach for raw usage
* via seqcount_t under KCSAN: upon beginning a seq-reader critical section,
* pessimistically mark the next KCSAN_SEQLOCK_REGION_MAX memory accesses as
* atomics; if there is a matching read_seqcount_retry() call, no following
* memory operations are considered atomic. Usage of the seqlock_t interface
* is not affected.
*/
#define KCSAN_SEQLOCK_REGION_MAX 1000
static inline void __seqcount_init(seqcount_t *s, const char *name,
struct lock_class_key *key)
{
/*
* Make sure we are not reinitializing a held lock:
*/
lockdep_init_map(&s->dep_map, name, key, 0);
s->sequence = 0;
}
#ifdef CONFIG_DEBUG_LOCK_ALLOC
# define SEQCOUNT_DEP_MAP_INIT(lockname) \
.dep_map = { .name = #lockname }
/**
* seqcount_init() - runtime initializer for seqcount_t
* @s: Pointer to the seqcount_t instance
*/
# define seqcount_init(s) \
do { \
static struct lock_class_key __key; \
__seqcount_init((s), #s, &__key); \
} while (0)
static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
{
seqcount_t *l = (seqcount_t *)s;
unsigned long flags;
local_irq_save(flags);
seqcount_acquire_read(&l->dep_map, 0, 0, _RET_IP_);
seqcount_release(&l->dep_map, _RET_IP_);
local_irq_restore(flags);
}
#else
# define SEQCOUNT_DEP_MAP_INIT(lockname)
# define seqcount_init(s) __seqcount_init(s, NULL, NULL)
# define seqcount_lockdep_reader_access(x)
#endif
/**
* SEQCNT_ZERO() - static initializer for seqcount_t
* @name: Name of the seqcount_t instance
*/
#define SEQCNT_ZERO(name) { .sequence = 0, SEQCOUNT_DEP_MAP_INIT(name) }
/*
* Sequence counters with associated locks (seqcount_LOCKNAME_t)
*
* A sequence counter which associates the lock used for writer
* serialization at initialization time. This enables lockdep to validate
* that the write side critical section is properly serialized.
*
* For associated locks which do not implicitly disable preemption,
* preemption protection is enforced in the write side function.
*
* Lockdep is never used in any for the raw write variants.
*
* See Documentation/locking/seqlock.rst
*/
/*
* typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
* @seqcount: The real sequence counter
* @lock: Pointer to the associated lock
*
* A plain sequence counter with external writer synchronization by
* LOCKNAME @lock. The lock is associated to the sequence counter in the
* static initializer or init function. This enables lockdep to validate
* that the write side critical section is properly serialized.
*
* LOCKNAME: raw_spinlock, spinlock, rwlock or mutex
*/
/*
* seqcount_LOCKNAME_init() - runtime initializer for seqcount_LOCKNAME_t
* @s: Pointer to the seqcount_LOCKNAME_t instance
* @lock: Pointer to the associated lock
*/
#define seqcount_LOCKNAME_init(s, _lock, lockname) \
do { \
seqcount_##lockname##_t *____s = (s); \
seqcount_init(&____s->seqcount); \
__SEQ_LOCK(____s->lock = (_lock)); \
} while (0)
#define seqcount_raw_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, raw_spinlock)
#define seqcount_spinlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, spinlock)
#define seqcount_rwlock_init(s, lock) seqcount_LOCKNAME_init(s, lock, rwlock)
#define seqcount_mutex_init(s, lock) seqcount_LOCKNAME_init(s, lock, mutex)
/*
* SEQCOUNT_LOCKNAME() - Instantiate seqcount_LOCKNAME_t and helpers
* seqprop_LOCKNAME_*() - Property accessors for seqcount_LOCKNAME_t
*
* @lockname: "LOCKNAME" part of seqcount_LOCKNAME_t
* @locktype: LOCKNAME canonical C data type
* @preemptible: preemptibility of above locktype
* @lockbase: prefix for associated lock/unlock
*/
#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase) \
static __always_inline seqcount_t * \
__seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
{ \
return &s->seqcount; \
} \
\
static __always_inline const seqcount_t * \
__seqprop_##lockname##_const_ptr(const seqcount_##lockname##_t *s) \
{ \
return &s->seqcount; \
} \
\
static __always_inline unsigned \
__seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \
{ \
unsigned seq = smp_load_acquire(&s->seqcount.sequence); \
\
if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
return seq; \
\
if (preemptible && unlikely(seq & 1)) { \
__SEQ_LOCK(lockbase##_lock(s->lock)); \
__SEQ_LOCK(lockbase##_unlock(s->lock)); \
\
/* \
* Re-read the sequence counter since the (possibly \
* preempted) writer made progress. \
*/ \
seq = smp_load_acquire(&s->seqcount.sequence); \
} \
\
return seq; \
} \
\
static __always_inline bool \
__seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \
{ \
if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
return preemptible; \
\
/* PREEMPT_RT relies on the above LOCK+UNLOCK */ \
return false; \
} \
\
static __always_inline void \
__seqprop_##lockname##_assert(const seqcount_##lockname##_t *s) \
{ \
__SEQ_LOCK(lockdep_assert_held(s->lock)); \
}
/*
* __seqprop() for seqcount_t
*/
static inline seqcount_t *__seqprop_ptr(seqcount_t *s)
{
return s;
}
static inline const seqcount_t *__seqprop_const_ptr(const seqcount_t *s)
{
return s;
}
static inline unsigned __seqprop_sequence(const seqcount_t *s)
{
return smp_load_acquire(&s->sequence);
}
static inline bool __seqprop_preemptible(const seqcount_t *s)
{
return false;
}
static inline void __seqprop_assert(const seqcount_t *s)
{
lockdep_assert_preemption_disabled();
}
#define __SEQ_RT IS_ENABLED(CONFIG_PREEMPT_RT)
SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, raw_spin)
SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, spin)
SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, read)
SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
#undef SEQCOUNT_LOCKNAME
/*
* SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
* @name: Name of the seqcount_LOCKNAME_t instance
* @lock: Pointer to the associated LOCKNAME
*/
#define SEQCOUNT_LOCKNAME_ZERO(seq_name, assoc_lock) { \
.seqcount = SEQCNT_ZERO(seq_name.seqcount), \
__SEQ_LOCK(.lock = (assoc_lock)) \
}
#define SEQCNT_RAW_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
#define SEQCNT_SPINLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
#define SEQCNT_RWLOCK_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
#define SEQCNT_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
#define SEQCNT_WW_MUTEX_ZERO(name, lock) SEQCOUNT_LOCKNAME_ZERO(name, lock)
#define __seqprop_case(s, lockname, prop) \
seqcount_##lockname##_t: __seqprop_##lockname##_##prop
#define __seqprop(s, prop) _Generic(*(s), \
seqcount_t: __seqprop_##prop, \
__seqprop_case((s), raw_spinlock, prop), \
__seqprop_case((s), spinlock, prop), \
__seqprop_case((s), rwlock, prop), \
__seqprop_case((s), mutex, prop))
#define seqprop_ptr(s) __seqprop(s, ptr)(s)
#define seqprop_const_ptr(s) __seqprop(s, const_ptr)(s)
#define seqprop_sequence(s) __seqprop(s, sequence)(s)
#define seqprop_preemptible(s) __seqprop(s, preemptible)(s)
#define seqprop_assert(s) __seqprop(s, assert)(s)
/**
* __read_seqcount_begin() - begin a seqcount_t read section
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* Return: count to be passed to read_seqcount_retry()
*/
#define __read_seqcount_begin(s) \
({ \
unsigned __seq; \
\
while (unlikely((__seq = seqprop_sequence(s)) & 1)) \
cpu_relax(); \
\
kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
__seq; \
})
/**
* raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* Return: count to be passed to read_seqcount_retry()
*/
#define raw_read_seqcount_begin(s) __read_seqcount_begin(s)
/**
* read_seqcount_begin() - begin a seqcount_t read critical section
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* Return: count to be passed to read_seqcount_retry()
*/
#define read_seqcount_begin(s) \
({ \
seqcount_lockdep_reader_access(seqprop_const_ptr(s)); \
raw_read_seqcount_begin(s); \
})
/**
* raw_read_seqcount() - read the raw seqcount_t counter value
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* raw_read_seqcount opens a read critical section of the given
* seqcount_t, without any lockdep checking, and without checking or
* masking the sequence counter LSB. Calling code is responsible for
* handling that.
*
* Return: count to be passed to read_seqcount_retry()
*/
#define raw_read_seqcount(s) \
({ \
unsigned __seq = seqprop_sequence(s); \
\
kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
__seq; \
})
/**
* raw_seqcount_try_begin() - begin a seqcount_t read critical section
* w/o lockdep and w/o counter stabilization
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
* @start: count to be passed to read_seqcount_retry()
*
* Similar to raw_seqcount_begin(), except it enables eliding the critical
* section entirely if odd, instead of doing the speculation knowing it will
* fail.
*
* Useful when counter stabilization is more or less equivalent to taking
* the lock and there is a slowpath that does that.
*
* If true, start will be set to the (even) sequence count read.
*
* Return: true when a read critical section is started.
*/
#define raw_seqcount_try_begin(s, start) \
({ \
start = raw_read_seqcount(s); \
!(start & 1); \
})
/**
* raw_seqcount_begin() - begin a seqcount_t read critical section w/o
* lockdep and w/o counter stabilization
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* raw_seqcount_begin opens a read critical section of the given
* seqcount_t. Unlike read_seqcount_begin(), this function will not wait
* for the count to stabilize. If a writer is active when it begins, it
* will fail the read_seqcount_retry() at the end of the read critical
* section instead of stabilizing at the beginning of it.
*
* Use this only in special kernel hot paths where the read section is
* small and has a high probability of success through other external
* means. It will save a single branching instruction.
*
* Return: count to be passed to read_seqcount_retry()
*/
#define raw_seqcount_begin(s) \
({ \
/* \
* If the counter is odd, let read_seqcount_retry() fail \
* by decrementing the counter. \
*/ \
raw_read_seqcount(s) & ~1; \
})
/**
* __read_seqcount_retry() - end a seqcount_t read section w/o barrier
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
* @start: count, from read_seqcount_begin()
*
* __read_seqcount_retry is like read_seqcount_retry, but has no smp_rmb()
* barrier. Callers should ensure that smp_rmb() or equivalent ordering is
* provided before actually loading any of the variables that are to be
* protected in this critical section.
*
* Use carefully, only in critical code, and comment how the barrier is
* provided.
*
* Return: true if a read section retry is required, else false
*/
#define __read_seqcount_retry(s, start) \
do___read_seqcount_retry(seqprop_const_ptr(s), start)
static inline int do___read_seqcount_retry(const seqcount_t *s, unsigned start)
{
kcsan_atomic_next(0);
return unlikely(READ_ONCE(s->sequence) != start);
}
/**
* read_seqcount_retry() - end a seqcount_t read critical section
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
* @start: count, from read_seqcount_begin()
*
* read_seqcount_retry closes the read critical section of given
* seqcount_t. If the critical section was invalid, it must be ignored
* (and typically retried).
*
* Return: true if a read section retry is required, else false
*/
#define read_seqcount_retry(s, start) \
do_read_seqcount_retry(seqprop_const_ptr(s), start)
static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
{
smp_rmb();
return do___read_seqcount_retry(s, start);
}
/**
* raw_write_seqcount_begin() - start a seqcount_t write section w/o lockdep
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* Context: check write_seqcount_begin()
*/
#define raw_write_seqcount_begin(s) \
do { \
if (seqprop_preemptible(s)) \
preempt_disable(); \
\
do_raw_write_seqcount_begin(seqprop_ptr(s)); \
} while (0)
static inline void do_raw_write_seqcount_begin(seqcount_t *s)
{
kcsan_nestable_atomic_begin();
s->sequence++;
smp_wmb();
}
/**
* raw_write_seqcount_end() - end a seqcount_t write section w/o lockdep
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* Context: check write_seqcount_end()
*/
#define raw_write_seqcount_end(s) \
do { \
do_raw_write_seqcount_end(seqprop_ptr(s)); \
\
if (seqprop_preemptible(s)) \
preempt_enable(); \
} while (0)
static inline void do_raw_write_seqcount_end(seqcount_t *s)
{
smp_wmb();
s->sequence++;
kcsan_nestable_atomic_end();
}
/**
* write_seqcount_begin_nested() - start a seqcount_t write section with
* custom lockdep nesting level
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
* @subclass: lockdep nesting level
*
* See Documentation/locking/lockdep-design.rst
* Context: check write_seqcount_begin()
*/
#define write_seqcount_begin_nested(s, subclass) \
do { \
seqprop_assert(s); \
\
if (seqprop_preemptible(s)) \
preempt_disable(); \
\
do_write_seqcount_begin_nested(seqprop_ptr(s), subclass); \
} while (0)
static inline void do_write_seqcount_begin_nested(seqcount_t *s, int subclass)
{
seqcount_acquire(&s->dep_map, subclass, 0, _RET_IP_);
do_raw_write_seqcount_begin(s);
}
/**
* write_seqcount_begin() - start a seqcount_t write side critical section
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* Context: sequence counter write side sections must be serialized and
* non-preemptible. Preemption will be automatically disabled if and
* only if the seqcount write serialization lock is associated, and
* preemptible. If readers can be invoked from hardirq or softirq
* context, interrupts or bottom halves must be respectively disabled.
*/
#define write_seqcount_begin(s) \
do { \
seqprop_assert(s); \
\
if (seqprop_preemptible(s)) \
preempt_disable(); \
\
do_write_seqcount_begin(seqprop_ptr(s)); \
} while (0)
static inline void do_write_seqcount_begin(seqcount_t *s)
{
do_write_seqcount_begin_nested(s, 0);
}
/**
* write_seqcount_end() - end a seqcount_t write side critical section
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* Context: Preemption will be automatically re-enabled if and only if
* the seqcount write serialization lock is associated, and preemptible.
*/
#define write_seqcount_end(s) \
do { \
do_write_seqcount_end(seqprop_ptr(s)); \
\
if (seqprop_preemptible(s)) \
preempt_enable(); \
} while (0)
static inline void do_write_seqcount_end(seqcount_t *s)
{
seqcount_release(&s->dep_map, _RET_IP_);
do_raw_write_seqcount_end(s);
}
/**
* raw_write_seqcount_barrier() - do a seqcount_t write barrier
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* This can be used to provide an ordering guarantee instead of the usual
* consistency guarantee. It is one wmb cheaper, because it can collapse
* the two back-to-back wmb()s.
*
* Note that writes surrounding the barrier should be declared atomic (e.g.
* via WRITE_ONCE): a) to ensure the writes become visible to other threads
* atomically, avoiding compiler optimizations; b) to document which writes are
* meant to propagate to the reader critical section. This is necessary because
* neither writes before nor after the barrier are enclosed in a seq-writer
* critical section that would ensure readers are aware of ongoing writes::
*
* seqcount_t seq;
* bool X = true, Y = false;
*
* void read(void)
* {
* bool x, y;
*
* do {
* int s = read_seqcount_begin(&seq);
*
* x = X; y = Y;
*
* } while (read_seqcount_retry(&seq, s));
*
* BUG_ON(!x && !y);
* }
*
* void write(void)
* {
* WRITE_ONCE(Y, true);
*
* raw_write_seqcount_barrier(seq);
*
* WRITE_ONCE(X, false);
* }
*/
#define raw_write_seqcount_barrier(s) \
do_raw_write_seqcount_barrier(seqprop_ptr(s))
static inline void do_raw_write_seqcount_barrier(seqcount_t *s)
{
kcsan_nestable_atomic_begin();
s->sequence++;
smp_wmb();
s->sequence++;
kcsan_nestable_atomic_end();
}
/**
* write_seqcount_invalidate() - invalidate in-progress seqcount_t read
* side operations
* @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants
*
* After write_seqcount_invalidate, no seqcount_t read side operations
* will complete successfully and see data older than this.
*/
#define write_seqcount_invalidate(s) \
do_write_seqcount_invalidate(seqprop_ptr(s))
static inline void do_write_seqcount_invalidate(seqcount_t *s)
{
smp_wmb();
kcsan_nestable_atomic_begin();
s->sequence+=2;
kcsan_nestable_atomic_end();
}
/*
* Latch sequence counters (seqcount_latch_t)
*
* A sequence counter variant where the counter even/odd value is used to
* switch between two copies of protected data. This allows the read path,
* typically NMIs, to safely interrupt the write side critical section.
*
* As the write sections are fully preemptible, no special handling for
* PREEMPT_RT is needed.
*/
typedef struct {
seqcount_t seqcount;
} seqcount_latch_t;
/**
* SEQCNT_LATCH_ZERO() - static initializer for seqcount_latch_t
* @seq_name: Name of the seqcount_latch_t instance
*/
#define SEQCNT_LATCH_ZERO(seq_name) { \
.seqcount = SEQCNT_ZERO(seq_name.seqcount), \
}
/**
* seqcount_latch_init() - runtime initializer for seqcount_latch_t
* @s: Pointer to the seqcount_latch_t instance
*/
#define seqcount_latch_init(s) seqcount_init(&(s)->seqcount)
/**
* raw_read_seqcount_latch() - pick even/odd latch data copy
* @s: Pointer to seqcount_latch_t
*
* See raw_write_seqcount_latch() for details and a full reader/writer
* usage example.
*
* Return: sequence counter raw value. Use the lowest bit as an index for
* picking which data copy to read. The full counter must then be checked
* with raw_read_seqcount_latch_retry().
*/
static __always_inline unsigned raw_read_seqcount_latch(const seqcount_latch_t *s)
{
/*
* Pairs with the first smp_wmb() in raw_write_seqcount_latch().
* Due to the dependent load, a full smp_rmb() is not needed.
*/
return READ_ONCE(s->seqcount.sequence);
}
/**
* read_seqcount_latch() - pick even/odd latch data copy
* @s: Pointer to seqcount_latch_t
*
* See write_seqcount_latch() for details and a full reader/writer usage
* example.
*
* Return: sequence counter raw value. Use the lowest bit as an index for
* picking which data copy to read. The full counter must then be checked
* with read_seqcount_latch_retry().
*/
static __always_inline unsigned read_seqcount_latch(const seqcount_latch_t *s)
{
kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
return raw_read_seqcount_latch(s);
}
/**
* raw_read_seqcount_latch_retry() - end a seqcount_latch_t read section
* @s: Pointer to seqcount_latch_t
* @start: count, from raw_read_seqcount_latch()
*
* Return: true if a read section retry is required, else false
*/
static __always_inline int
raw_read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
{
smp_rmb();
return unlikely(READ_ONCE(s->seqcount.sequence) != start);
}
/**
* read_seqcount_latch_retry() - end a seqcount_latch_t read section
* @s: Pointer to seqcount_latch_t
* @start: count, from read_seqcount_latch()
*
* Return: true if a read section retry is required, else false
*/
static __always_inline int
read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
{
kcsan_atomic_next(0);
return raw_read_seqcount_latch_retry(s, start);
}
/**
* raw_write_seqcount_latch() - redirect latch readers to even/odd copy
* @s: Pointer to seqcount_latch_t
*/
static __always_inline void raw_write_seqcount_latch(seqcount_latch_t *s)
{
smp_wmb(); /* prior stores before incrementing "sequence" */
s->seqcount.sequence++;
smp_wmb(); /* increment "sequence" before following stores */
}
/**
* write_seqcount_latch_begin() - redirect latch readers to odd copy
* @s: Pointer to seqcount_latch_t
*
* The latch technique is a multiversion concurrency control method that allows
* queries during non-atomic modifications. If you can guarantee queries never
* interrupt the modification -- e.g. the concurrency is strictly between CPUs
* -- you most likely do not need this.
*
* Where the traditional RCU/lockless data structures rely on atomic
* modifications to ensure queries observe either the old or the new state the
* latch allows the same for non-atomic updates. The trade-off is doubling the
* cost of storage; we have to maintain two copies of the entire data
* structure.
*
* Very simply put: we first modify one copy and then the other. This ensures
* there is always one copy in a stable state, ready to give us an answer.
*
* The basic form is a data structure like::
*
* struct latch_struct {
* seqcount_latch_t seq;
* struct data_struct data[2];
* };
*
* Where a modification, which is assumed to be externally serialized, does the
* following::
*
* void latch_modify(struct latch_struct *latch, ...)
* {
* write_seqcount_latch_begin(&latch->seq);
* modify(latch->data[0], ...);
* write_seqcount_latch(&latch->seq);
* modify(latch->data[1], ...);
* write_seqcount_latch_end(&latch->seq);
* }
*
* The query will have a form like::
*
* struct entry *latch_query(struct latch_struct *latch, ...)
* {
* struct entry *entry;
* unsigned seq, idx;
*
* do {
* seq = read_seqcount_latch(&latch->seq);
*
* idx = seq & 0x01;
* entry = data_query(latch->data[idx], ...);
*
* // This includes needed smp_rmb()
* } while (read_seqcount_latch_retry(&latch->seq, seq));
*
* return entry;
* }
*
* So during the modification, queries are first redirected to data[1]. Then we
* modify data[0]. When that is complete, we redirect queries back to data[0]
* and we can modify data[1].
*
* NOTE:
*
* The non-requirement for atomic modifications does _NOT_ include
* the publishing of new entries in the case where data is a dynamic
* data structure.
*
* An iteration might start in data[0] and get suspended long enough
* to miss an entire modification sequence, once it resumes it might
* observe the new entry.
*
* NOTE2:
*
* When data is a dynamic data structure; one should use regular RCU
* patterns to manage the lifetimes of the objects within.
*/
static __always_inline void write_seqcount_latch_begin(seqcount_latch_t *s)
{
kcsan_nestable_atomic_begin();
raw_write_seqcount_latch(s);
}
/**
* write_seqcount_latch() - redirect latch readers to even copy
* @s: Pointer to seqcount_latch_t
*/
static __always_inline void write_seqcount_latch(seqcount_latch_t *s)
{
raw_write_seqcount_latch(s);
}
/**
* write_seqcount_latch_end() - end a seqcount_latch_t write section
* @s: Pointer to seqcount_latch_t
*
* Marks the end of a seqcount_latch_t writer section, after all copies of the
* latch-protected data have been updated.
*/
static __always_inline void write_seqcount_latch_end(seqcount_latch_t *s)
{
kcsan_nestable_atomic_end();
}
#define __SEQLOCK_UNLOCKED(lockname) \
{ \
.seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \
.lock = __SPIN_LOCK_UNLOCKED(lockname) \
}
/**
* seqlock_init() - dynamic initializer for seqlock_t
* @sl: Pointer to the seqlock_t instance
*/
#define seqlock_init(sl) \
do { \
spin_lock_init(&(sl)->lock); \
seqcount_spinlock_init(&(sl)->seqcount, &(sl)->lock); \
} while (0)
/**
* DEFINE_SEQLOCK(sl) - Define a statically allocated seqlock_t
* @sl: Name of the seqlock_t instance
*/
#define DEFINE_SEQLOCK(sl) \
seqlock_t sl = __SEQLOCK_UNLOCKED(sl)
/**
* read_seqbegin() - start a seqlock_t read side critical section
* @sl: Pointer to seqlock_t
*
* Return: count, to be passed to read_seqretry()
*/
static inline unsigned read_seqbegin(const seqlock_t *sl)
__acquires_shared(sl) __no_context_analysis
{
return read_seqcount_begin(&sl->seqcount);
}
/**
* read_seqretry() - end a seqlock_t read side section
* @sl: Pointer to seqlock_t
* @start: count, from read_seqbegin()
*
* read_seqretry closes the read side critical section of given seqlock_t.
* If the critical section was invalid, it must be ignored (and typically
* retried).
*
* Return: true if a read section retry is required, else false
*/
static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
__releases_shared(sl) __no_context_analysis
{
return read_seqcount_retry(&sl->seqcount, start);
}
/*
* For all seqlock_t write side functions, use the internal
* do_write_seqcount_begin() instead of generic write_seqcount_begin().
* This way, no redundant lockdep_assert_held() checks are added.
*/
/**
* write_seqlock() - start a seqlock_t write side critical section
* @sl: Pointer to seqlock_t
*
* write_seqlock opens a write side critical section for the given
* seqlock_t. It also implicitly acquires the spinlock_t embedded inside
* that sequential lock. All seqlock_t write side sections are thus
* automatically serialized and non-preemptible.
*
* Context: if the seqlock_t read section, or other write side critical
* sections, can be invoked from hardirq or softirq contexts, use the
* _irqsave or _bh variants of this function instead.
*/
static inline void write_seqlock(seqlock_t *sl)
__acquires(sl) __no_context_analysis
{
spin_lock(&sl->lock);
do_write_seqcount_begin(&sl->seqcount.seqcount);
}
/**
* write_sequnlock() - end a seqlock_t write side critical section
* @sl: Pointer to seqlock_t
*
* write_sequnlock closes the (serialized and non-preemptible) write side
* critical section of given seqlock_t.
*/
static inline void write_sequnlock(seqlock_t *sl)
__releases(sl) __no_context_analysis
{
do_write_seqcount_end(&sl->seqcount.seqcount);
spin_unlock(&sl->lock);
}
/**
* write_seqlock_bh() - start a softirqs-disabled seqlock_t write section
* @sl: Pointer to seqlock_t
*
* _bh variant of write_seqlock(). Use only if the read side section, or
* other write side sections, can be invoked from softirq contexts.
*/
static inline void write_seqlock_bh(seqlock_t *sl)
__acquires(sl) __no_context_analysis
{
spin_lock_bh(&sl->lock);
do_write_seqcount_begin(&sl->seqcount.seqcount);
}
/**
* write_sequnlock_bh() - end a softirqs-disabled seqlock_t write section
* @sl: Pointer to seqlock_t
*
* write_sequnlock_bh closes the serialized, non-preemptible, and
* softirqs-disabled, seqlock_t write side critical section opened with
* write_seqlock_bh().
*/
static inline void write_sequnlock_bh(seqlock_t *sl)
__releases(sl) __no_context_analysis
{
do_write_seqcount_end(&sl->seqcount.seqcount);
spin_unlock_bh(&sl->lock);
}
/**
* write_seqlock_irq() - start a non-interruptible seqlock_t write section
* @sl: Pointer to seqlock_t
*
* _irq variant of write_seqlock(). Use only if the read side section, or
* other write sections, can be invoked from hardirq contexts.
*/
static inline void write_seqlock_irq(seqlock_t *sl)
__acquires(sl) __no_context_analysis
{
spin_lock_irq(&sl->lock);
do_write_seqcount_begin(&sl->seqcount.seqcount);
}
/**
* write_sequnlock_irq() - end a non-interruptible seqlock_t write section
* @sl: Pointer to seqlock_t
*
* write_sequnlock_irq closes the serialized and non-interruptible
* seqlock_t write side section opened with write_seqlock_irq().
*/
static inline void write_sequnlock_irq(seqlock_t *sl)
__releases(sl) __no_context_analysis
{
do_write_seqcount_end(&sl->seqcount.seqcount);
spin_unlock_irq(&sl->lock);
}
static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
__acquires(sl) __no_context_analysis
{
unsigned long flags;
spin_lock_irqsave(&sl->lock, flags);
do_write_seqcount_begin(&sl->seqcount.seqcount);
return flags;
}
/**
* write_seqlock_irqsave() - start a non-interruptible seqlock_t write
* section
* @lock: Pointer to seqlock_t
* @flags: Stack-allocated storage for saving caller's local interrupt
* state, to be passed to write_sequnlock_irqrestore().
*
* _irqsave variant of write_seqlock(). Use it only if the read side
* section, or other write sections, can be invoked from hardirq context.
*/
#define write_seqlock_irqsave(lock, flags) \
do { flags = __write_seqlock_irqsave(lock); } while (0)
/**
* write_sequnlock_irqrestore() - end non-interruptible seqlock_t write
* section
* @sl: Pointer to seqlock_t
* @flags: Caller's saved interrupt state, from write_seqlock_irqsave()
*
* write_sequnlock_irqrestore closes the serialized and non-interruptible
* seqlock_t write section previously opened with write_seqlock_irqsave().
*/
static inline void
write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
__releases(sl) __no_context_analysis
{
do_write_seqcount_end(&sl->seqcount.seqcount);
spin_unlock_irqrestore(&sl->lock, flags);
}
/**
* read_seqlock_excl() - begin a seqlock_t locking reader section
* @sl: Pointer to seqlock_t
*
* read_seqlock_excl opens a seqlock_t locking reader critical section. A
* locking reader exclusively locks out *both* other writers *and* other
* locking readers, but it does not update the embedded sequence number.
*
* Locking readers act like a normal spin_lock()/spin_unlock().
*
* Context: if the seqlock_t write section, *or other read sections*, can
* be invoked from hardirq or softirq contexts, use the _irqsave or _bh
* variant of this function instead.
*
* The opened read section must be closed with read_sequnlock_excl().
*/
static inline void read_seqlock_excl(seqlock_t *sl)
__acquires_shared(sl) __no_context_analysis
{
spin_lock(&sl->lock);
}
/**
* read_sequnlock_excl() - end a seqlock_t locking reader critical section
* @sl: Pointer to seqlock_t
*/
static inline void read_sequnlock_excl(seqlock_t *sl)
__releases_shared(sl) __no_context_analysis
{
spin_unlock(&sl->lock);
}
/**
* read_seqlock_excl_bh() - start a seqlock_t locking reader section with
* softirqs disabled
* @sl: Pointer to seqlock_t
*
* _bh variant of read_seqlock_excl(). Use this variant only if the
* seqlock_t write side section, *or other read sections*, can be invoked
* from softirq contexts.
*/
static inline void read_seqlock_excl_bh(seqlock_t *sl)
__acquires_shared(sl) __no_context_analysis
{
spin_lock_bh(&sl->lock);
}
/**
* read_sequnlock_excl_bh() - stop a seqlock_t softirq-disabled locking
* reader section
* @sl: Pointer to seqlock_t
*/
static inline void read_sequnlock_excl_bh(seqlock_t *sl)
__releases_shared(sl) __no_context_analysis
{
spin_unlock_bh(&sl->lock);
}
/**
* read_seqlock_excl_irq() - start a non-interruptible seqlock_t locking
* reader section
* @sl: Pointer to seqlock_t
*
* _irq variant of read_seqlock_excl(). Use this only if the seqlock_t
* write side section, *or other read sections*, can be invoked from a
* hardirq context.
*/
static inline void read_seqlock_excl_irq(seqlock_t *sl)
__acquires_shared(sl) __no_context_analysis
{
spin_lock_irq(&sl->lock);
}
/**
* read_sequnlock_excl_irq() - end an interrupts-disabled seqlock_t
* locking reader section
* @sl: Pointer to seqlock_t
*/
static inline void read_sequnlock_excl_irq(seqlock_t *sl)
__releases_shared(sl) __no_context_analysis
{
spin_unlock_irq(&sl->lock);
}
static inline unsigned long __read_seqlock_excl_irqsave(seqlock_t *sl)
__acquires_shared(sl) __no_context_analysis
{
unsigned long flags;
spin_lock_irqsave(&sl->lock, flags);
return flags;
}
/**
* read_seqlock_excl_irqsave() - start a non-interruptible seqlock_t
* locking reader section
* @lock: Pointer to seqlock_t
* @flags: Stack-allocated storage for saving caller's local interrupt
* state, to be passed to read_sequnlock_excl_irqrestore().
*
* _irqsave variant of read_seqlock_excl(). Use this only if the seqlock_t
* write side section, *or other read sections*, can be invoked from a
* hardirq context.
*/
#define read_seqlock_excl_irqsave(lock, flags) \
do { flags = __read_seqlock_excl_irqsave(lock); } while (0)
/**
* read_sequnlock_excl_irqrestore() - end non-interruptible seqlock_t
* locking reader section
* @sl: Pointer to seqlock_t
* @flags: Caller saved interrupt state, from read_seqlock_excl_irqsave()
*/
static inline void
read_sequnlock_excl_irqrestore(seqlock_t *sl, unsigned long flags)
__releases_shared(sl) __no_context_analysis
{
spin_unlock_irqrestore(&sl->lock, flags);
}
/**
* read_seqbegin_or_lock() - begin a seqlock_t lockless or locking reader
* @lock: Pointer to seqlock_t
* @seq : Marker and return parameter. If the passed value is even, the
* reader will become a *lockless* seqlock_t reader as in read_seqbegin().
* If the passed value is odd, the reader will become a *locking* reader
* as in read_seqlock_excl(). In the first call to this function, the
* caller *must* initialize and pass an even value to @seq; this way, a
* lockless read can be optimistically tried first.
*
* read_seqbegin_or_lock is an API designed to optimistically try a normal
* lockless seqlock_t read section first. If an odd counter is found, the
* lockless read trial has failed, and the next read iteration transforms
* itself into a full seqlock_t locking reader.
*
* This is typically used to avoid seqlock_t lockless readers starvation
* (too much retry loops) in the case of a sharp spike in write side
* activity.
*
* Context: if the seqlock_t write section, *or other read sections*, can
* be invoked from hardirq or softirq contexts, use the _irqsave or _bh
* variant of this function instead.
*
* Check Documentation/locking/seqlock.rst for template example code.
*
* Return: the encountered sequence counter value, through the @seq
* parameter, which is overloaded as a return parameter. This returned
* value must be checked with need_seqretry(). If the read section need to
* be retried, this returned value must also be passed as the @seq
* parameter of the next read_seqbegin_or_lock() iteration.
*/
static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
__acquires_shared(lock) __no_context_analysis
{
if (!(*seq & 1)) /* Even */
*seq = read_seqbegin(lock);
else /* Odd */
read_seqlock_excl(lock);
}
/**
* need_seqretry() - validate seqlock_t "locking or lockless" read section
* @lock: Pointer to seqlock_t
* @seq: sequence count, from read_seqbegin_or_lock()
*
* Return: true if a read section retry is required, false otherwise
*/
static inline int need_seqretry(seqlock_t *lock, int seq)
__releases_shared(lock) __no_context_analysis
{
return !(seq & 1) && read_seqretry(lock, seq);
}
/**
* done_seqretry() - end seqlock_t "locking or lockless" reader section
* @lock: Pointer to seqlock_t
* @seq: count, from read_seqbegin_or_lock()
*
* done_seqretry finishes the seqlock_t read side critical section started
* with read_seqbegin_or_lock() and validated by need_seqretry().
*/
static inline void done_seqretry(seqlock_t *lock, int seq)
__no_context_analysis
{
if (seq & 1)
read_sequnlock_excl(lock);
}
/**
* read_seqbegin_or_lock_irqsave() - begin a seqlock_t lockless reader, or
* a non-interruptible locking reader
* @lock: Pointer to seqlock_t
* @seq: Marker and return parameter. Check read_seqbegin_or_lock().
*
* This is the _irqsave variant of read_seqbegin_or_lock(). Use it only if
* the seqlock_t write section, *or other read sections*, can be invoked
* from hardirq context.
*
* Note: Interrupts will be disabled only for "locking reader" mode.
*
* Return:
*
* 1. The saved local interrupts state in case of a locking reader, to
* be passed to done_seqretry_irqrestore().
*
* 2. The encountered sequence counter value, returned through @seq
* overloaded as a return parameter. Check read_seqbegin_or_lock().
*/
static inline unsigned long
read_seqbegin_or_lock_irqsave(seqlock_t *lock, int *seq)
__acquires_shared(lock) __no_context_analysis
{
unsigned long flags = 0;
if (!(*seq & 1)) /* Even */
*seq = read_seqbegin(lock);
else /* Odd */
read_seqlock_excl_irqsave(lock, flags);
return flags;
}
/**
* done_seqretry_irqrestore() - end a seqlock_t lockless reader, or a
* non-interruptible locking reader section
* @lock: Pointer to seqlock_t
* @seq: Count, from read_seqbegin_or_lock_irqsave()
* @flags: Caller's saved local interrupt state in case of a locking
* reader, also from read_seqbegin_or_lock_irqsave()
*
* This is the _irqrestore variant of done_seqretry(). The read section
* must've been opened with read_seqbegin_or_lock_irqsave(), and validated
* by need_seqretry().
*/
static inline void
done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
__no_context_analysis
{
if (seq & 1)
read_sequnlock_excl_irqrestore(lock, flags);
}
enum ss_state {
ss_done = 0,
ss_lock,
ss_lock_irqsave,
ss_lockless,
};
struct ss_tmp {
enum ss_state state;
unsigned long data;
spinlock_t *lock;
spinlock_t *lock_irqsave;
};
static __always_inline void __scoped_seqlock_cleanup(struct ss_tmp *sst)
__no_context_analysis
{
if (sst->lock)
spin_unlock(sst->lock);
if (sst->lock_irqsave)
spin_unlock_irqrestore(sst->lock_irqsave, sst->data);
}
extern void __scoped_seqlock_invalid_target(void);
#if (defined(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION < 90000) || defined(CONFIG_KASAN)
/*
* For some reason some GCC-8 architectures (nios2, alpha) have trouble
* determining that the ss_done state is impossible in __scoped_seqlock_next()
* below.
*
* Similarly KASAN is known to confuse compilers enough to break this. But we
* don't care about code quality for KASAN builds anyway.
*/
static inline void __scoped_seqlock_bug(void) { }
#else
/*
* Canary for compiler optimization -- if the compiler doesn't realize this is
* an impossible state, it very likely generates sub-optimal code here.
*/
extern void __scoped_seqlock_bug(void);
#endif
static __always_inline void
__scoped_seqlock_next(struct ss_tmp *sst, seqlock_t *lock, enum ss_state target)
__no_context_analysis
{
switch (sst->state) {
case ss_done:
__scoped_seqlock_bug();
return;
case ss_lock:
case ss_lock_irqsave:
sst->state = ss_done;
return;
case ss_lockless:
if (!read_seqretry(lock, sst->data)) {
sst->state = ss_done;
return;
}
break;
}
switch (target) {
case ss_done:
__scoped_seqlock_invalid_target();
return;
case ss_lock:
sst->lock = &lock->lock;
spin_lock(sst->lock);
sst->state = ss_lock;
return;
case ss_lock_irqsave:
sst->lock_irqsave = &lock->lock;
spin_lock_irqsave(sst->lock_irqsave, sst->data);
sst->state = ss_lock_irqsave;
return;
case ss_lockless:
sst->data = read_seqbegin(lock);
return;
}
}
/*
* Context analysis no-op helper to release seqlock at the end of the for-scope;
* the alias analysis of the compiler will recognize that the pointer @s is an
* alias to @_seqlock passed to read_seqbegin(_seqlock) below.
*/
static __always_inline void __scoped_seqlock_cleanup_ctx(struct ss_tmp **s)
__releases_shared(*((seqlock_t **)s)) __no_context_analysis {}
#define __scoped_seqlock_read(_seqlock, _target, _s) \
for (struct ss_tmp _s __cleanup(__scoped_seqlock_cleanup) = \
{ .state = ss_lockless, .data = read_seqbegin(_seqlock) }, \
*__UNIQUE_ID(ctx) __cleanup(__scoped_seqlock_cleanup_ctx) =\
(struct ss_tmp *)_seqlock; \
_s.state != ss_done; \
__scoped_seqlock_next(&_s, _seqlock, _target))
/**
* scoped_seqlock_read() - execute the read-side critical section
* without manual sequence counter handling
* or calls to other helpers
* @_seqlock: pointer to seqlock_t protecting the data
* @_target: an enum ss_state: one of {ss_lock, ss_lock_irqsave, ss_lockless}
* indicating the type of critical read section
*
* Example::
*
* scoped_seqlock_read (&lock, ss_lock) {
* // read-side critical section
* }
*
* Starts with a lockess pass first. If it fails, restarts the critical
* section with the lock held.
*/
#define scoped_seqlock_read(_seqlock, _target) \
__scoped_seqlock_read(_seqlock, _target, __UNIQUE_ID(seqlock))
DEFINE_LOCK_GUARD_1(seqlock_init, seqlock_t, seqlock_init(_T->lock), /* */)
DECLARE_LOCK_GUARD_1_ATTRS(seqlock_init, __acquires(_T), __releases(*(seqlock_t **)_T))
#define class_seqlock_init_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(seqlock_init, _T)
#endif /* __LINUX_SEQLOCK_H */
]
|
| 28/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 27396
output: 42
thoughts: 839
Thoughts: **Analyzing Sequence Counter Protection in Kernel Code**
Okay, so I'm looking at this `do_raw_write_seqcount_begin` function and immediately my brain goes to the sequence counter logic. We've got `s->sequence++` and a `smp_wmb()`. The purpose here is clear: ensure the increment is visible *before* any other writes within the critical section. But then I stop and think... `s->sequence++` is a read-modify-write! If the sequence was even, now it's odd.
Now, let's step into this `step_into` function and the call to `read_seqcount_retry`. The reader side has a `smp_rmb()` before checking the sequence. This is key! This `smp_rmb()` is there to guarantee that, if the reader sees a change to `d_inode`, it also sees the *corresponding* sequence number.
If `dentry_unlink_inode` is executing, it increments the sequence (making it odd), then does a write barrier, *then* nulls out `dentry->d_inode`, then does *another* write barrier, and finally increments the sequence again (making it even). So here's the race: a reader might read `d_inode` (which might be null), and then read the sequence. The `smp_rmb()` in the reader and the `smp_wmb()` in the writer should ensure that if the reader sees `d_inode` changed, the sequence counter must be odd! That's the crux of the protection.
And that's why the `read_seqcount_retry` will function correctly, the reader seeing an odd sequence number will know a writer had just done its thing. It's essentially the classic "check-and-act" problem but using the sequence counter for this protection.
So, KCSAN is reporting this as a data race because it sees the plain accesses. I need to remember how the sequence counter with KCSAN works here though. The seqlock documentation...right. It basically marks memory accesses as atomic for the next `KCSAN_SEQLOCK_REGION_MAX` accesses in a best-effort approach. If there's a matching `read_seqcount_retry` call, following operations are *not* considered atomic.
However, the `step_into` is using `nd->next_seq`, which was sampled earlier. So it isn't using `read_seqcount_begin()` at this point. In fact, `__d_lookup_rcu` calls `__read_seqcount_begin` or something similar. I need to check that out by diving into `__d_lookup_rcu`.
|
| 29/2 |
2026/02/23 15:28 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:__d_lookup_rcu]
Results: map[SourceCode:2273: /**
2274: * __d_lookup_rcu - search for a dentry (racy, store-free)
2275: * @parent: parent dentry
2276: * @name: qstr of name we wish to find
2277: * @seqp: returns d_seq value at the point where the dentry was found
2278: * Returns: dentry, or NULL
2279: *
2280: * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2281: * resolution (store-free path walking) design described in
2282: * Documentation/filesystems/path-lookup.txt.
2283: *
2284: * This is not to be used outside core vfs.
2285: *
2286: * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2287: * held, and rcu_read_lock held. The returned dentry must not be stored into
2288: * without taking d_lock and checking d_seq sequence count against @seq
2289: * returned here.
2290: *
2291: * Alternatively, __d_lookup_rcu may be called again to look up the child of
2292: * the returned dentry, so long as its parent's seqlock is checked after the
2293: * child is looked up. Thus, an interlocking stepping of sequence lock checks
2294: * is formed, giving integrity down the path walk.
2295: *
2296: * NOTE! The caller *has* to check the resulting dentry against the sequence
2297: * number we've returned before using any of the resulting dentry state!
2298: */
2299: struct dentry *__d_lookup_rcu(const struct dentry *parent,
2300: const struct qstr *name,
2301: unsigned *seqp)
2302: {
2303: u64 hashlen = name->hash_len;
2304: const unsigned char *str = name->name;
2305: struct hlist_bl_head *b = d_hash(hashlen);
2306: struct hlist_bl_node *node;
2307: struct dentry *dentry;
2308:
2309: /*
2310: * Note: There is significant duplication with __d_lookup_rcu which is
2311: * required to prevent single threaded performance regressions
2312: * especially on architectures where smp_rmb (in seqcounts) are costly.
2313: * Keep the two functions in sync.
2314: */
2315:
2316: if (unlikely(parent->d_flags & DCACHE_OP_COMPARE))
2317: return __d_lookup_rcu_op_compare(parent, name, seqp);
2318:
2319: /*
2320: * The hash list is protected using RCU.
2321: *
2322: * Carefully use d_seq when comparing a candidate dentry, to avoid
2323: * races with d_move().
2324: *
2325: * It is possible that concurrent renames can mess up our list
2326: * walk here and result in missing our dentry, resulting in the
2327: * false-negative result. d_lookup() protects against concurrent
2328: * renames using rename_lock seqlock.
2329: *
2330: * See Documentation/filesystems/path-lookup.txt for more details.
2331: */
2332: hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2333: unsigned seq;
2334:
2335: /*
2336: * The dentry sequence count protects us from concurrent
2337: * renames, and thus protects parent and name fields.
2338: *
2339: * The caller must perform a seqcount check in order
2340: * to do anything useful with the returned dentry.
2341: *
2342: * NOTE! We do a "raw" seqcount_begin here. That means that
2343: * we don't wait for the sequence count to stabilize if it
2344: * is in the middle of a sequence change. If we do the slow
2345: * dentry compare, we will do seqretries until it is stable,
2346: * and if we end up with a successful lookup, we actually
2347: * want to exit RCU lookup anyway.
2348: *
2349: * Note that raw_seqcount_begin still *does* smp_rmb(), so
2350: * we are still guaranteed NUL-termination of ->d_name.name.
2351: */
2352: seq = raw_seqcount_begin(&dentry->d_seq);
2353: if (dentry->d_parent != parent)
2354: continue;
2355: if (dentry->d_name.hash_len != hashlen)
2356: continue;
2357: if (unlikely(dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0))
2358: continue;
2359: /*
2360: * Check for the dentry being unhashed.
2361: *
2362: * As tempting as it is, we *can't* skip it because of a race window
2363: * between us finding the dentry before it gets unhashed and loading
2364: * the sequence counter after unhashing is finished.
2365: *
2366: * We can at least predict on it.
2367: */
2368: if (unlikely(d_unhashed(dentry)))
2369: continue;
2370: *seqp = seq;
2371: return dentry;
2372: }
2373: return NULL;
2374: }
SourceFile:fs/dcache.c]
|
| 30/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 30570
output: 40
thoughts: 1460
Thoughts: **Analyzing the Sequence Count and Data Races in `__d_lookup_rcu`**
Okay, so I'm digging into `__d_lookup_rcu` and the way it handles concurrency, specifically focusing on the `dentry->d_inode` access and the sequence counter (`dentry->d_seq`). The initial read of the sequence counter using `raw_seqcount_begin` utilizes `raw_read_seqcount`, which, in turn, uses `smp_load_acquire` and also calls `kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX)`. This part is clear: KCSAN is being told that the subsequent 1000 memory accesses should be considered atomic because they are part of a seqlock critical section.
The concern comes in with `step_into`. There, we have a potentially problematic scenario. We see that `lookup_fast` has a `read_seqcount_retry` call, which calls `kcsan_atomic_next(0)` to cancel the atomic region. Then, in `step_into`, we access `dentry->d_inode` directly and then call `read_seqcount_retry`. Now, `read_seqcount_retry` *does* mark the subsequent read as atomic, but only if it's called within a `read_seqcount_begin/retry` block. However, in `step_into`, it's a standalone check. It also calls `kcsan_atomic_next(0)` at the end. That is the thing, it *doesn't* call `kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX)` before.
The VFS approach here is to sample the sequence number once and then repeatedly check the data, retrying the read if there's been a sequence number change (as indicated by the `read_seqcount_retry`). This pattern helps in RCU-walk, but it implies a data race on `d_inode`. Although the data race is real on the `d_inode` access, the seqlock is designed to handle it.
My primary concern now is whether the compiler can introduce problems. Specifically, if it does something bad to the 64-bit `d_inode` pointer load. A partial pointer read would be catastrophic, but a 64-bit read on an 8-byte aligned address is atomic on 64-bit systems. Is it possible for the compiler to reuse the value of `d_inode`? Yes, at lines 2138, 2141 and 2145 in `step_into`. If `read_seqcount_retry` returns false, it means `d_inode` hasn't changed.
If the value was changed by another CPU, the sequence counter MUST have changed. This suggests that the code is *probably* correct, even though it relies on the atomicity of the 64-bit read.
The main issue I'm seeing is that the access to `d_inode` isn't `READ_ONCE`. Now, could the compiler split the load into two operations? It's unlikely. On x86_64, a 64-bit load is a single instruction, unless explicitly forced. Still, the Linux kernel memory model (LKMM) considers such unsynchronized races undefined behavior. While, in practice, it's often considered benign for primitive types if validated by another construct, such as this sequence counter.
Also, it is notable that if `dentry->d_inode` is NULL, it means the dentry is negative. `dentry_unlink_inode` can set a dentry negative:
```c
420: dentry->d_inode = NULL;
```
If this occurs, the code will check for it in `step_into`:
```c
2141: if (unlikely(!inode))
2142: return ERR_PTR(-ENOENT);
```
So, if `dentry->d_inode` reads NULL, the return code is `-ENOENT`. If the value reads non-NULL, then things continue as normal. What I am worried about is that there might be a "torn" pointer - half old and half new - which would be catastrophic. But, the alignment is 8-byte, so this is not really possible.
I'll check `d_instantiate`. Where else could `d_inode` be written to? `d_instantiate` sets `d_inode`, and `dentry_unlink_inode` clears it. Let me just check `d_instantiate` and confirm.
|
| 31/2 |
2026/02/23 15:28 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:__d_instantiate]
Results: map[SourceCode:1971: static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1972: {
1973: unsigned add_flags = d_flags_for_inode(inode);
1974: WARN_ON(d_in_lookup(dentry));
1975:
1976: /*
1977: * The negative counter only tracks dentries on the LRU. Don't dec if
1978: * d_lru is on another list.
1979: */
1980: if ((dentry->d_flags &
1981: (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST)
1982: this_cpu_dec(nr_dentry_negative);
1983: hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
1984: raw_write_seqcount_begin(&dentry->d_seq);
1985: __d_set_inode_and_type(dentry, inode, add_flags);
1986: raw_write_seqcount_end(&dentry->d_seq);
1987: fsnotify_update_flags(dentry);
1988: }
SourceFile:fs/dcache.c]
|
| 32/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 33455
output: 37
thoughts: 542
Thoughts: **Analyzing the dentry and inode interaction**
Okay, so I'm looking at this `__d_set_inode_and_type` function, and how it interacts with the `d_inode` field of a `dentry`. My initial thought is to trace the flow of setting the inode within the dentry. I see the core operation: `dentry->d_inode = inode;`. I note that this is wrapped in `raw_write_seqcount_begin/end`, meaning `d_seq` is protecting modifications to `d_inode`.
The key potential race is between a write (setting `d_inode` to NULL, presumably during unlinking) and a read. The reader can observe either the valid pointer or NULL. If NULL, it returns `-ENOENT`. But the more interesting case is when the reader sees a non-NULL pointer, and *then* we have to see if we hit `read_seqcount_retry`.
Thinking this through, if the write finishes *before* the reader even begins to read `d_inode`, then the reader sees NULL and `read_seqcount_retry` returns false. However, if the write completes *after* the sampling of `nd->next_seq` (but before the reader reads `d_inode`), `read_seqcount_retry` will succeed. If the write completes *before* the sampling of `nd->next_seq` then we will see `-ENOENT` and a `read_seqcount_retry` will return false.
The tricky case would be if the reader sees a non-NULL pointer that's about to be freed. But wait, `dentry_unlink_inode` calls `iput(inode)` *after* `raw_write_seqcount_end`. Furthermore, the reader is in an RCU walk, meaning the inode is protected by RCU, assuming it's being accessed. But is the inode even valid throughout the RCU walk? I see that `nd->inode = inode` at line 2145. And then the reader continues to use `nd->inode`.
So the critical question then becomes: does the VFS *guarantee* that the inode remains valid during an RCU walk? I think it should, given that inodes are typically RCU-freed. Ah, looking at `struct inode`, I see `i_rcu`, which suggests they are. Now, I need to confirm this with a check on `destroy_inode` to verify this. This all seems consistent with the idea of RCU protection.
|
| 33/2 |
2026/02/23 15:28 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/inode.c IncludeLines:true Name:destroy_inode]
Results: map[SourceCode: 390: static void destroy_inode(struct inode *inode)
391: {
392: const struct super_operations *ops = inode->i_sb->s_op;
393:
394: BUG_ON(!list_empty(&inode->i_lru));
395: __destroy_inode(inode);
396: if (ops->destroy_inode) {
397: ops->destroy_inode(inode);
398: if (!ops->free_inode)
399: return;
400: }
401: inode->free_inode = ops->free_inode;
402: call_rcu(&inode->i_rcu, i_callback);
403: }
SourceFile:fs/inode.c]
|
| 34/2 |
2026/02/23 15:28 |
llm |
expert |
0m
Model:
Tokens:
input: 34815
output: 24
thoughts: 912
Thoughts: **Analyzing the VFS RCU-Walk and Data Races**
Okay, so I'm diving into this `call_rcu` business with the inode. The key thing is that the inode memory remains valid throughout the RCU grace period, which is essential. The reader in `step_into` is protected by `rcu_read_lock()`. Therefore, even if a reader gets a pointer to an inode being unlinked, it's safe to access it, as long as it avoids operations depending on reference counts or checks the sequence counter. The VFS rcu-walk seems designed to handle this type of scenario.
The use of direct access to `d_inode` is a pattern in VFS to avoid overhead of READ_ONCE/WRITE_ONCE. The seqlock offers the needed guarantees, although it seems kernel standards prefer READ_ONCE/WRITE_ONCE nowadays to keep the compiler from doing something unexpected and to satisfy KCSAN.
Now, looking at the data race: it's between `dentry->d_inode = NULL` and `struct inode *inode = dentry->d_inode`. Since it's 64-bit and aligned, it's an atomic operation, right? And the seqlock ensures that if a value is being changed when it's read, we retry.
But wait a minute, what about compiler reordering? Could the compiler move the read of `d_inode` *after* `read_seqcount_retry`? No, `smp_rmb()` in `read_seqcount_retry` should prevent that. But I'm quick to check: `smp_rmb()` only prevents reordering of *loads* before it with *loads* after it. The `inode = dentry->d_inode` is a load, and the subsequent `read_seqcount_retry` has a call to `do_read_seqcount_retry` which calls `smp_rmb()` and reads `d_seq->sequence`. So the `d_inode` read happens before the `d_seq->sequence` read. Phew.
Okay, let's consider the writer. The `raw_write_seqcount_begin` has `smp_wmb()`. `smp_wmb()` prevents reordering of *stores* before and after. `s->sequence++` and `dentry->d_inode = NULL` are both stores. So `s->sequence` gets updated before `d_inode`.
This leads to the following ordering:
**Writer:**
* W1: `d_seq->sequence++` (odd)
* W\_barrier: `smp_wmb()`
* W2: `dentry->d_inode = NULL`
* W\_barrier2: `smp_wmb()`
* W3: `d_seq->sequence++` (even)
**Reader:**
* R1: `inode = dentry->d_inode`
* R\_barrier: `smp_rmb()`
* R2: `seq = d_seq->sequence`
If R1 sees the result of W2 (`NULL`), R2 must see at least the result of W1 (odd sequence). If R2 sees an odd sequence, `read_seqcount_retry` returns true. If R2 sees the result of W3 (even, but different from `nd->next_seq`), `read_seqcount_retry` also returns true.
The only scenario where `read_seqcount_retry` returns false is if R2 sees the same sequence as `nd->next_seq`. This can only happen if R1 and R2 both happened before W1, or both happened after W3 (and `nd->next_seq` was sampled after W3).
So, the logic appears sound, and this kind of data race is "benign," meaning it doesn't lead to incorrect behavior. It's still a technical data race because of the lack of `READ_ONCE`/`WRITE_ONCE`.
However, before I sign off, I should check whether there are similar reports or if this is a known "benign" race in the kernel. In fact, many VFS races get flagged by KCSAN and are then marked as benign or fixed by adding `READ_ONCE`/`WRITE_ONCE`.
I should check if `d_inode` is ever accessed using `READ_ONCE`.
|
| 35/2 |
2026/02/23 15:29 |
tool |
grepper |
0m
Args: map[Expression:READ_ONCE\(.*d_inode\)]
Results: map[Output:fs/ecryptfs/inode.c=332=static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
--
fs/ecryptfs/inode.c-350- */
fs/ecryptfs/inode.c:351: lower_inode = READ_ONCE(lower_dentry->d_inode);
fs/ecryptfs/inode.c-352-
--
fs/libfs.c=1805=int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
--
fs/libfs.c-1825- parent = READ_ONCE(dentry->d_parent);
fs/libfs.c:1826: dir = READ_ONCE(parent->d_inode);
fs/libfs.c-1827- if (!dir || !IS_CASEFOLDED(dir))
--
fs/libfs.c=1859=int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
fs/libfs.c-1860-{
fs/libfs.c:1861: const struct inode *dir = READ_ONCE(dentry->d_inode);
fs/libfs.c-1862- struct super_block *sb = dentry->d_sb;
--
include/linux/dcache.h=544=static inline struct inode *d_inode_rcu(const struct dentry *dentry)
include/linux/dcache.h-545-{
include/linux/dcache.h:546: return READ_ONCE(dentry->d_inode);
include/linux/dcache.h-547-}
]
|
| 36/2 |
2026/02/23 15:29 |
llm |
expert |
0m
Model:
Tokens:
input: 37157
output: 43
thoughts: 164
Thoughts: **Examining the Inconsistency in Dentry Handling**
Okay, so I'm looking at this `d_inode_rcu` function. It's using `READ_ONCE` to access `dentry->d_inode`. Seems like standard practice for ensuring proper memory ordering when dealing with potentially concurrent access, right? That makes sense. But then I see this `step_into` function and it's accessing `dentry->d_inode` *directly*. No `d_inode_rcu`. Hmm, that's a bit odd.
Now hold on... I see that `step_into` also calls `d_is_symlink` and `d_managed`! Let me just quickly refresh my memory on `d_is_symlink`. I'm starting to suspect there's a reason for this discrepancy. Perhaps those functions handle the concurrency concerns in a different manner, or the access pattern within `step_into` inherently avoids the need for the RCU protection. This warrants further investigation to understand why these seemingly inconsistent accesses are happening. Is there a subtle semantic difference in how `d_inode` is used in `step_into` compared to other places? Or is there a lurking concurrency bug here that I need to flush out?
|
| 37/2 |
2026/02/23 15:29 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:include/linux/dcache.h IncludeLines:true Name:d_is_symlink]
Results: map[SourceCode: 444: static inline bool d_is_symlink(const struct dentry *dentry)
445: {
446: return __d_entry_type(dentry) == DCACHE_SYMLINK_TYPE;
447: }
SourceFile:include/linux/dcache.h]
|
| 38/2 |
2026/02/23 15:29 |
llm |
expert |
0m
Model:
Tokens:
input: 37731
output: 17
thoughts: 821
Thoughts: **Analyzing a Potential Race Condition in the VFS Code**
Okay, so I'm looking at this code and immediately see a potential KCSAN issue. Specifically, the code uses `READ_ONCE` for the `d_flags` field of a `dentry` structure, but it *doesn't* use `READ_ONCE`/`WRITE_ONCE` for the `d_inode` field in the `__d_clear_type_and_inode` function. This raised a red flag. KCSAN is going to complain about the lack of atomic operations here, which is understandable given that `dentry->d_inode` is being read without proper synchronization.
My initial thought was maybe this was just historical baggage or a performance optimization, although the overhead of `READ_ONCE` on a pointer is usually negligible, essentially just a volatile cast on a 64 bit architecture. However, the VFS is often super performance-sensitive, so I have to consider that.
Then, I started to think about the seqlock check, which *should* prevent issues since the read is followed by a `read_seqcount_retry` call. So, if the sequence counter has changed, it re-reads, which should make the access harmless, or at least that's my initial intuition. But I have to remember KCSAN's stance – any racing access *should* be marked, to avoid undefined behavior. So, even if it's "benign" because of the seqlock, it still violates the memory model principles that KCSAN enforces.
Then I look at the provided report. Yep, the report shows exactly what I suspected: a write to the `d_inode` pointer in `__d_clear_type_and_inode`, followed by a read in `step_into`, with a change in the value observed by KCSAN. That's confirmation of the race.
But then, wait! I see that `d_inode_rcu` *exists*! Why isn't it being used here? If we changed the code to `struct inode *inode = d_inode_rcu(dentry);`, KCSAN would probably be happy. That seems like a much cleaner, more correct solution. Let me look if there are other accesses to `d_inode` in `fs/namei.c` during RCU walks. I'm going to track down every place we are directly accessing `d_inode` without the proper memory barrier.
|
| 39/2 |
2026/02/23 15:29 |
tool |
grepper |
0m
Args:
Results: map[Output:
Full output is too long, showing 500 out of 15288 lines.
Use more precise expression if possible.
[Documentation/filesystems/api-summary.rst=26=Inode Handling
--
Documentation/filesystems/api-summary.rst-31-
Documentation/filesystems/api-summary.rst:32:.. kernel-doc:: fs/bad_inode.c
Documentation/filesystems/api-summary.rst-33- :export:
--
Documentation/filesystems/path-lookup.rst=173=be freed and used for something else, so the values in various fields
Documentation/filesystems/path-lookup.rst:174:will behave as expected. It also protects the ``->d_inode`` reference
Documentation/filesystems/path-lookup.rst-175-to the inode to some extent.
--
Documentation/filesystems/path-lookup.rst=179=together to the new location. When a file is created the dentry will
Documentation/filesystems/path-lookup.rst:180:initially be negative (i.e. ``d_inode`` is ``NULL``), and will be assigned
Documentation/filesystems/path-lookup.rst-181-to the new inode as part of the act of creation.
--
Documentation/filesystems/path-lookup.rst=183=When a file is deleted, this can be reflected in the cache either by
Documentation/filesystems/path-lookup.rst:184:setting ``d_inode`` to ``NULL``, or by removing it from the hash table
Documentation/filesystems/path-lookup.rst-185-(described shortly) used to look up the name in the parent directory.
--
Documentation/filesystems/path-lookup.rst=189=use (i.e. if the refcount in ``d_lockref`` is one), only then will
Documentation/filesystems/path-lookup.rst:190:``d_inode`` be set to ``NULL``. Doing it this way is more efficient for a
Documentation/filesystems/path-lookup.rst-191-very common case.
Documentation/filesystems/path-lookup.rst-192-
Documentation/filesystems/path-lookup.rst:193:So as long as a counted reference is held to a dentry, a non-``NULL`` ``->d_inode``
Documentation/filesystems/path-lookup.rst-194-value will never be changed.
--
Documentation/filesystems/path-lookup.rst=781=was mounted on, and so not visible from the root.
Documentation/filesystems/path-lookup.rst-782-
Documentation/filesystems/path-lookup.rst:783:The inode pointer, stored in ``->d_inode``, is a little more
Documentation/filesystems/path-lookup.rst-784-interesting. The inode will always need to be accessed at least
--
Documentation/filesystems/path-lookup.txt=90=reference is taken on the dentry (while the fields are verified under d_lock),
Documentation/filesystems/path-lookup.txt:91:and this stabilises its d_inode pointer and actual inode. This gives a stable
Documentation/filesystems/path-lookup.txt-92-point to perform the next step of our path walk against.
--
Documentation/filesystems/path-lookup.txt=188=than reloading from the dentry later on (otherwise we'd have interesting things
Documentation/filesystems/path-lookup.txt:189:like d_inode going NULL underneath us, if the name was unlinked).
Documentation/filesystems/path-lookup.txt-190-
--
Documentation/filesystems/path-lookup.txt=290=The detailed design for rcu-walk is like this:
--
Documentation/filesystems/path-lookup.txt-306- during the path walk.
Documentation/filesystems/path-lookup.txt:307:* inode is also RCU protected so we can load d_inode and use the inode for
Documentation/filesystems/path-lookup.txt-308- limited things.
--
Documentation/filesystems/porting.rst=181=can be used as examples of very different filesystems.
--
Documentation/filesystems/porting.rst-186-
Documentation/filesystems/porting.rst:187:iget4() and the read_inode2 callback have been superseded by iget5_locked()
Documentation/filesystems/porting.rst-188-which has the following prototype::
--
Documentation/filesystems/porting.rst=211=e.g.::
--
Documentation/filesystems/porting.rst-214- if (inode_state_read_once(inode) & I_NEW) {
Documentation/filesystems/porting.rst:215: err = read_inode_from_disk(inode);
Documentation/filesystems/porting.rst-216- if (err < 0) {
--
Documentation/filesystems/porting.rst=646=in ->get_link().
--
Documentation/filesystems/porting.rst-652-->getxattr() and xattr_handler.get() get dentry and inode passed separately.
Documentation/filesystems/porting.rst:653:dentry might be yet to be attached to inode, so do _not_ use its ->d_inode
Documentation/filesystems/porting.rst-654-in the instances. Rationale: !@#!@# security_d_instantiate() needs to be
--
Documentation/filesystems/porting.rst=719=is seen from so filesystems can idmap the i_uid and i_gid accordingly.
Documentation/filesystems/porting.rst:720:dentry might be yet to be attached to inode, so do _not_ use its ->d_inode
Documentation/filesystems/porting.rst-721-in the instances. Rationale: !@#!@# security_d_instantiate() needs to be
--
Documentation/filesystems/vfs.rst=1201=defined:
--
Documentation/filesystems/vfs.rst-1237- revalidate the dentry without blocking or storing to the dentry,
Documentation/filesystems/vfs.rst:1238: d_parent and d_inode should not be used without care (because
Documentation/filesystems/vfs.rst:1239: they can change and, in d_inode case, even become NULL under
Documentation/filesystems/vfs.rst-1240- us).
--
Documentation/filesystems/vfs.rst-1279- dereference pointers outside the dentry without lots of care
Documentation/filesystems/vfs.rst:1280: (eg. d_parent, d_inode, d_name should not be used).
Documentation/filesystems/vfs.rst-1281-
--
Documentation/filesystems/vfs.rst-1329- return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
Documentation/filesystems/vfs.rst:1330: dentry->d_inode->i_ino);
Documentation/filesystems/vfs.rst-1331- }
--
Documentation/filesystems/vfs.rst=1411=manipulate dentries:
--
Documentation/filesystems/vfs.rst-1441- add a dentry to the alias hash list for the inode and updates
Documentation/filesystems/vfs.rst:1442: the "d_inode" member. The "i_count" member in the inode
Documentation/filesystems/vfs.rst-1443- structure should be set/incremented. If the inode pointer is
--
arch/powerpc/platforms/cell/spufs/inode.c=95=spufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
--
arch/powerpc/platforms/cell/spufs/inode.c-97-{
arch/powerpc/platforms/cell/spufs/inode.c:98: struct inode *inode = d_inode(dentry);
arch/powerpc/platforms/cell/spufs/inode.c-99-
--
arch/powerpc/platforms/cell/spufs/inode.c=147=static void spufs_rmdir(struct inode *parent, struct dentry *dir)
arch/powerpc/platforms/cell/spufs/inode.c-148-{
arch/powerpc/platforms/cell/spufs/inode.c:149: struct spu_context *ctx = SPUFS_I(d_inode(dir))->i_ctx;
arch/powerpc/platforms/cell/spufs/inode.c-150-
--
arch/powerpc/platforms/cell/spufs/inode.c=174=static void unuse_gang(struct dentry *dir)
arch/powerpc/platforms/cell/spufs/inode.c-175-{
arch/powerpc/platforms/cell/spufs/inode.c:176: struct inode *inode = dir->d_inode;
arch/powerpc/platforms/cell/spufs/inode.c-177- struct spu_gang *gang = SPUFS_I(inode)->i_gang;
--
arch/powerpc/platforms/cell/spufs/inode.c=191=static int spufs_dir_close(struct inode *inode, struct file *file)
--
arch/powerpc/platforms/cell/spufs/inode.c-196- dir = file->f_path.dentry;
arch/powerpc/platforms/cell/spufs/inode.c:197: parent = d_inode(dir->d_parent);
arch/powerpc/platforms/cell/spufs/inode.c-198-
--
arch/powerpc/platforms/cell/spufs/inode.c=378=spufs_create_context(struct inode *inode, struct dentry *dentry,
--
arch/powerpc/platforms/cell/spufs/inode.c-425- if (affinity) {
arch/powerpc/platforms/cell/spufs/inode.c:426: spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,
arch/powerpc/platforms/cell/spufs/inode.c-427- neighbor);
--
arch/powerpc/platforms/cell/spufs/inode.c=527=long spufs_create(const struct path *path, struct dentry *dentry,
--
arch/powerpc/platforms/cell/spufs/inode.c-529-{
arch/powerpc/platforms/cell/spufs/inode.c:530: struct inode *dir = d_inode(path->dentry);
arch/powerpc/platforms/cell/spufs/inode.c-531- int ret;
--
arch/powerpc/platforms/cell/spufs/inode.c=578=static int spufs_show_options(struct seq_file *m, struct dentry *root)
--
arch/powerpc/platforms/cell/spufs/inode.c-580- struct spufs_sb_info *sbi = spufs_get_sb_info(root->d_sb);
arch/powerpc/platforms/cell/spufs/inode.c:581: struct inode *inode = root->d_inode;
arch/powerpc/platforms/cell/spufs/inode.c-582-
--
arch/s390/hypfs/inode.c=50=static void hypfs_update_update(struct super_block *sb)
--
arch/s390/hypfs/inode.c-52- struct hypfs_sb_info *sb_info = sb->s_fs_info;
arch/s390/hypfs/inode.c:53: struct inode *inode = d_inode(sb_info->update_file);
arch/s390/hypfs/inode.c-54-
--
arch/s390/hypfs/inode.c=317=static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
--
arch/s390/hypfs/inode.c-339- inode->i_fop = &simple_dir_operations;
arch/s390/hypfs/inode.c:340: inc_nlink(d_inode(parent));
arch/s390/hypfs/inode.c-341- } else
--
block/bdev.c=1274=void sync_bdevs(bool wait)
block/bdev.c-1275-{
block/bdev.c:1276: struct inode *inode, *old_inode = NULL;
block/bdev.c-1277-
--
block/bdev.c-1299- */
block/bdev.c:1300: iput(old_inode);
block/bdev.c:1301: old_inode = inode;
block/bdev.c-1302- bdev = I_BDEV(inode);
--
block/bdev.c-1322- spin_unlock(&blockdev_superblock->s_inode_list_lock);
block/bdev.c:1323: iput(old_inode);
block/bdev.c-1324-}
--
block/fops.c=602=static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
block/fops.c-603-{
block/fops.c:604: struct inode *bd_inode = bdev_file_inode(file);
block/fops.c-605- loff_t retval;
block/fops.c-606-
block/fops.c:607: inode_lock(bd_inode);
block/fops.c:608: retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
block/fops.c:609: inode_unlock(bd_inode);
block/fops.c-610- return retval;
--
block/fops.c=746=static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
--
block/fops.c-748- struct file *file = iocb->ki_filp;
block/fops.c:749: struct inode *bd_inode = bdev_file_inode(file);
block/fops.c:750: struct block_device *bdev = I_BDEV(bd_inode);
block/fops.c-751- bool atomic = iocb->ki_flags & IOCB_ATOMIC;
--
block/fops.c-758-
block/fops.c:759: if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
block/fops.c-760- return -ETXTBSY;
--
block/fops.c-799- */
block/fops.c:800: inode_lock_shared(bd_inode);
block/fops.c-801- ret = blkdev_buffered_write(iocb, from);
block/fops.c:802: inode_unlock_shared(bd_inode);
block/fops.c-803- }
--
block/fops.c=811=static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
block/fops.c-812-{
block/fops.c:813: struct inode *bd_inode = bdev_file_inode(iocb->ki_filp);
block/fops.c-814- struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host);
--
block/fops.c-853- */
block/fops.c:854: inode_lock_shared(bd_inode);
block/fops.c-855- ret = filemap_read(iocb, to, ret);
block/fops.c:856: inode_unlock_shared(bd_inode);
block/fops.c-857-
--
drivers/android/binder/rust_binderfs.c=114=static int binderfs_binder_device_create(struct inode *ref_inode,
--
drivers/android/binder/rust_binderfs.c-189-
drivers/android/binder/rust_binderfs.c:190: fsnotify_create(root->d_inode, dentry);
drivers/android/binder/rust_binderfs.c-191- simple_done_creating(dentry);
--
drivers/android/binder/rust_binderfs.c=464=static struct dentry *rust_binderfs_create_file(struct dentry *parent, const char *name,
--
drivers/android/binder/rust_binderfs.c-483- d_make_persistent(dentry, new_inode);
drivers/android/binder/rust_binderfs.c:484: fsnotify_create(parent->d_inode, dentry);
drivers/android/binder/rust_binderfs.c-485- simple_done_creating(dentry);
--
drivers/android/binder/rust_binderfs.c=503=static struct dentry *binderfs_create_dir(struct dentry *parent,
--
drivers/android/binder/rust_binderfs.c-521-
drivers/android/binder/rust_binderfs.c:522: inc_nlink(parent->d_inode);
drivers/android/binder/rust_binderfs.c-523- set_nlink(new_inode, 2);
drivers/android/binder/rust_binderfs.c-524- d_make_persistent(dentry, new_inode);
drivers/android/binder/rust_binderfs.c:525: fsnotify_mkdir(parent->d_inode, dentry);
drivers/android/binder/rust_binderfs.c-526- simple_done_creating(dentry);
--
drivers/android/binderfs.c=114=static int binderfs_binder_device_create(struct inode *ref_inode,
--
drivers/android/binderfs.c-192- d_make_persistent(dentry, inode);
drivers/android/binderfs.c:193: fsnotify_create(root->d_inode, dentry);
drivers/android/binderfs.c-194- simple_done_creating(dentry);
--
drivers/android/binderfs.c=469=struct dentry *binderfs_create_file(struct dentry *parent, const char *name,
--
drivers/android/binderfs.c-476-
drivers/android/binderfs.c:477: parent_inode = d_inode(parent);
drivers/android/binderfs.c-478-
--
drivers/android/binderfs.c=498=static struct dentry *binderfs_create_dir(struct dentry *parent,
--
drivers/android/binderfs.c-504-
drivers/android/binderfs.c:505: parent_inode = d_inode(parent);
drivers/android/binderfs.c-506-
--
drivers/base/devtmpfs.c=173=static int dev_mkdir(const char *name, umode_t mode)
--
drivers/base/devtmpfs.c-181-
drivers/base/devtmpfs.c:182: dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode, NULL);
drivers/base/devtmpfs.c-183- if (!IS_ERR(dentry))
drivers/base/devtmpfs.c-184- /* mark as kernel-created inode */
drivers/base/devtmpfs.c:185: d_inode(dentry)->i_private = &thread;
drivers/base/devtmpfs.c-186- end_creating_path(&path, dentry);
--
drivers/base/devtmpfs.c=217=static int handle_create(const char *nodename, umode_t mode, kuid_t uid,
--
drivers/base/devtmpfs.c-231-
drivers/base/devtmpfs.c:232: err = vfs_mknod(&nop_mnt_idmap, d_inode(path.dentry), dentry, mode,
drivers/base/devtmpfs.c-233- dev->devt, NULL);
--
drivers/base/devtmpfs.c-240- newattrs.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID;
drivers/base/devtmpfs.c:241: inode_lock(d_inode(dentry));
drivers/base/devtmpfs.c-242- notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
drivers/base/devtmpfs.c:243: inode_unlock(d_inode(dentry));
drivers/base/devtmpfs.c-244-
drivers/base/devtmpfs.c-245- /* mark as kernel-created inode */
drivers/base/devtmpfs.c:246: d_inode(dentry)->i_private = &thread;
drivers/base/devtmpfs.c-247- }
--
drivers/base/devtmpfs.c=252=static int dev_rmdir(const char *name)
--
drivers/base/devtmpfs.c-260- return PTR_ERR(dentry);
drivers/base/devtmpfs.c:261: if (d_inode(dentry)->i_private == &thread)
drivers/base/devtmpfs.c:262: err = vfs_rmdir(&nop_mnt_idmap, d_inode(parent.dentry),
drivers/base/devtmpfs.c-263- dentry, NULL);
--
drivers/base/devtmpfs.c=317=static int handle_remove(const char *nodename, struct device *dev)
--
drivers/base/devtmpfs.c-328-
drivers/base/devtmpfs.c:329: inode = d_inode(dentry);
drivers/base/devtmpfs.c-330- if (dev_mynode(dev, inode)) {
--
drivers/base/devtmpfs.c-340- ATTR_UID|ATTR_GID|ATTR_MODE;
drivers/base/devtmpfs.c:341: inode_lock(d_inode(dentry));
drivers/base/devtmpfs.c-342- notify_change(&nop_mnt_idmap, dentry, &newattrs, NULL);
drivers/base/devtmpfs.c:343: inode_unlock(d_inode(dentry));
drivers/base/devtmpfs.c:344: err = vfs_unlink(&nop_mnt_idmap, d_inode(parent.dentry),
drivers/base/devtmpfs.c-345- dentry, NULL);
--
drivers/block/drbd/drbd_debugfs.c=418=static int drbd_single_open(struct file *file, int (*show)(struct seq_file *, void *),
--
drivers/block/drbd/drbd_debugfs.c-428- /* serialize with d_delete() */
drivers/block/drbd/drbd_debugfs.c:429: inode_lock(d_inode(parent));
drivers/block/drbd/drbd_debugfs.c-430- /* Make sure the object is still alive */
--
drivers/block/drbd/drbd_debugfs.c-433- ret = 0;
drivers/block/drbd/drbd_debugfs.c:434: inode_unlock(d_inode(parent));
drivers/block/drbd/drbd_debugfs.c-435- if (!ret) {
--
drivers/char/xillybus/xillybus_class.c=213=EXPORT_SYMBOL(xillybus_cleanup_chrdev);
drivers/char/xillybus/xillybus_class.c-214-
drivers/char/xillybus/xillybus_class.c:215:int xillybus_find_inode(struct inode *inode,
drivers/char/xillybus/xillybus_class.c-216- void **private_data, int *index)
--
drivers/char/xillybus/xillybus_class.c-242-}
drivers/char/xillybus/xillybus_class.c:243:EXPORT_SYMBOL(xillybus_find_inode);
drivers/char/xillybus/xillybus_class.c-244-
--
drivers/char/xillybus/xillybus_class.h=24=void xillybus_cleanup_chrdev(void *private_data,
--
drivers/char/xillybus/xillybus_class.h-26-
drivers/char/xillybus/xillybus_class.h:27:int xillybus_find_inode(struct inode *inode,
drivers/char/xillybus/xillybus_class.h-28- void **private_data, int *index);
--
drivers/char/xillybus/xillybus_core.c=1426=static int xillybus_open(struct inode *inode, struct file *filp)
--
drivers/char/xillybus/xillybus_core.c-1433-
drivers/char/xillybus/xillybus_core.c:1434: rc = xillybus_find_inode(inode, (void **)&endpoint, &index);
drivers/char/xillybus/xillybus_core.c-1435- if (rc)
--
drivers/char/xillybus/xillyusb.c=164=struct xillyusb_dev {
--
drivers/char/xillybus/xillyusb.c-190- * struct from being freed during the gap between being found by
drivers/char/xillybus/xillyusb.c:191: * xillybus_find_inode() and having its reference count incremented.
drivers/char/xillybus/xillyusb.c-192- */
--
drivers/char/xillybus/xillyusb.c=1236=static int xillyusb_open(struct inode *inode, struct file *filp)
--
drivers/char/xillybus/xillyusb.c-1246-
drivers/char/xillybus/xillyusb.c:1247: rc = xillybus_find_inode(inode, (void **)&xdev, &index);
drivers/char/xillybus/xillyusb.c-1248- if (rc) {
--
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c=1623=int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
--
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c-1633- if (!i && !IS_ERR_OR_NULL(ent))
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c:1634: i_size_write(ent->d_inode, adev->rmmio_size);
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c-1635- }
--
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c=1262=void amdgpu_ras_debugfs_set_ret_size(struct amdgpu_ras_eeprom_control *control)
--
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c-1268- if (de)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c:1269: d_inode(de)->i_size = amdgpu_ras_debugfs_table_size(control);
drivers/gpu/drm/amd/amdgpu/amdgpu_ras_eeprom.c-1270-}
--
drivers/gpu/drm/drm_debugfs.c=275=int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
--
drivers/gpu/drm/drm_debugfs.c-285-
drivers/gpu/drm/drm_debugfs.c:286: drmm_kfree(minor->dev, d_inode(dent)->i_private);
drivers/gpu/drm/drm_debugfs.c-287- debugfs_remove(dent);
--
drivers/gpu/drm/nouveau/nouveau_debugfs.c=267=nouveau_drm_debugfs_init(struct drm_minor *minor)
--
drivers/gpu/drm/nouveau/nouveau_debugfs.c-290-
drivers/gpu/drm/nouveau/nouveau_debugfs.c:291: d_inode(dentry)->i_size = drm->vbios.length;
drivers/gpu/drm/nouveau/nouveau_debugfs.c-292- dput(dentry);
--
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c=1316=static struct dentry *create_debugfs(struct nvkm_gsp *gsp, const char *name,
--
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c-1333- */
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c:1334: i_size_write(d_inode(dent), blob->size);
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c-1335-
--
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c=1365=r535_gsp_libos_debugfs_init(struct nvkm_gsp *gsp)
--
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c-1410-
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c:1411: i_size_write(d_inode(gsp->debugfs.init), gsp->blob_init.size);
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c:1412: i_size_write(d_inode(gsp->debugfs.intr), gsp->blob_intr.size);
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c:1413: i_size_write(d_inode(gsp->debugfs.rm), gsp->blob_rm.size);
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c:1414: i_size_write(d_inode(gsp->debugfs.pmu), gsp->blob_pmu.size);
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c-1415-
--
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c=1937=static int r535_gsp_copy_log(struct dentry *parent,
--
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c-1962-
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c:1963: i_size_write(d_inode(dent), t->size);
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c-1964-
--
drivers/gpu/drm/xe/xe_gt_debugfs.c=37=static struct xe_gt *node_to_gt(struct drm_info_node *node)
drivers/gpu/drm/xe/xe_gt_debugfs.c-38-{
drivers/gpu/drm/xe/xe_gt_debugfs.c:39: return node->dent->d_parent->d_inode->i_private;
drivers/gpu/drm/xe/xe_gt_debugfs.c-40-}
--
drivers/gpu/drm/xe/xe_gt_debugfs.c-50- * It is assumed that those debugfs files will be created on directory entry
drivers/gpu/drm/xe/xe_gt_debugfs.c:51: * which struct dentry d_inode->i_private points to &xe_gt.
drivers/gpu/drm/xe/xe_gt_debugfs.c-52- *
--
drivers/gpu/drm/xe/xe_gt_debugfs.c-74- * dir = debugfs_create_dir("gt", parent);
drivers/gpu/drm/xe/xe_gt_debugfs.c:75: * dir->d_inode->i_private = gt;
drivers/gpu/drm/xe/xe_gt_debugfs.c-76- * drm_debugfs_create_files(bar, ARRAY_SIZE(bar), dir, minor);
--
drivers/gpu/drm/xe/xe_gt_debugfs.c=311=void xe_gt_debugfs_register(struct xe_gt *gt)
--
drivers/gpu/drm/xe/xe_gt_debugfs.c-336- */
drivers/gpu/drm/xe/xe_gt_debugfs.c:337: root->d_inode->i_private = gt;
drivers/gpu/drm/xe/xe_gt_debugfs.c-338-
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-29- * /sys/kernel/debug/dri/BDF/
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:30: * ├── sriov # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:31: * │ ├── pf # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:32: * │ │ ├── tile0 # d_inode->i_private = (xe_tile*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:33: * │ │ │ ├── gt0 # d_inode->i_private = (xe_gt*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:34: * │ │ │ ├── gt1 # d_inode->i_private = (xe_gt*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-35- * │ │ ├── tile1
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-36- * │ │ │ :
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:37: * │ ├── vf1 # d_inode->i_private = VFID(1)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:38: * │ │ ├── tile0 # d_inode->i_private = (xe_tile*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:39: * │ │ │ ├── gt0 # d_inode->i_private = (xe_gt*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:40: * │ │ │ ├── gt1 # d_inode->i_private = (xe_gt*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-41- * │ │ ├── tile1
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-43- * : :
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:44: * │ ├── vfN # d_inode->i_private = VFID(N)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-45- */
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c=47=static void *extract_priv(struct dentry *d)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-48-{
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:49: return d->d_inode->i_private;
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-50-}
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c=847=void xe_gt_sriov_pf_debugfs_populate(struct xe_gt *gt, struct dentry *parent, unsigned int vfid)
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-861- * │ │ ├── tile0 # parent
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:862: * │ │ │ ├── gt0 # d_inode->i_private = (xe_gt*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-863- * │ │ │ ├── gt1
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-866- * │ │ ├── tile0 # parent
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:867: * │ │ │ ├── gt0 # d_inode->i_private = (xe_gt*)
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-868- * │ │ │ ├── gt1
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-874- return;
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:875: dent->d_inode->i_private = gt;
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-876-
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c=909=void xe_gt_sriov_pf_debugfs_register(struct xe_gt *gt, struct dentry *dent)
--
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-911- xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c:912: xe_gt_assert(gt, dent->d_inode->i_private == gt);
drivers/gpu/drm/xe/xe_gt_sriov_pf_debugfs.c-913-
--
drivers/gpu/drm/xe/xe_gt_sriov_vf_debugfs.c=52=void xe_gt_sriov_vf_debugfs_register(struct xe_gt *gt, struct dentry *root)
--
drivers/gpu/drm/xe/xe_gt_sriov_vf_debugfs.c-58- xe_assert(xe, IS_SRIOV_VF(xe));
drivers/gpu/drm/xe/xe_gt_sriov_vf_debugfs.c:59: xe_assert(xe, root->d_inode->i_private == gt);
drivers/gpu/drm/xe/xe_gt_sriov_vf_debugfs.c-60-
--
drivers/gpu/drm/xe/xe_gt_sriov_vf_debugfs.c-68- return;
drivers/gpu/drm/xe/xe_gt_sriov_vf_debugfs.c:69: vfdentry->d_inode->i_private = gt;
drivers/gpu/drm/xe/xe_gt_sriov_vf_debugfs.c-70-
--
drivers/gpu/drm/xe/xe_guc_debugfs.c-28- * It is assumed that those debugfs files will be created on directory entry
drivers/gpu/drm/xe/xe_guc_debugfs.c:29: * which grandparent struct dentry d_inode->i_private points to &xe_gt.
drivers/gpu/drm/xe/xe_guc_debugfs.c-30- *
drivers/gpu/drm/xe/xe_guc_debugfs.c-31- * /sys/kernel/debug/dri/0/
drivers/gpu/drm/xe/xe_guc_debugfs.c:32: * ├── gt0 # dent->d_parent->d_parent (d_inode->i_private == gt)
drivers/gpu/drm/xe/xe_guc_debugfs.c-33- * │ ├── uc # dent->d_parent
--
drivers/gpu/drm/xe/xe_guc_debugfs.c=63=static int guc_debugfs_show(struct seq_file *m, void *data)
--
drivers/gpu/drm/xe/xe_guc_debugfs.c-68- struct dentry *grandparent = parent->d_parent;
drivers/gpu/drm/xe/xe_guc_debugfs.c:69: struct xe_gt *gt = grandparent->d_inode->i_private;
drivers/gpu/drm/xe/xe_guc_debugfs.c-70- struct xe_device *xe = gt_to_xe(gt);
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-22- * /sys/kernel/debug/dri/BDF/
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:23: * ├── sriov # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:24: * │ ├── pf # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:25: * │ ├── vf1 # d_inode->i_private = VFID(1)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-26- * : :
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:27: * │ ├── vfN # d_inode->i_private = VFID(N)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-28- */
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c=30=static void *extract_priv(struct dentry *d)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-31-{
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:32: return d->d_inode->i_private;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-33-}
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c=136=static int simple_show(struct seq_file *m, void *data)
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-140- struct dentry *parent = node->dent->d_parent;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:141: struct xe_device *xe = parent->d_inode->i_private;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-142- void (*print)(struct xe_device *, struct drm_printer *) = node->info_ent->data;
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c=340=void xe_sriov_pf_debugfs_register(struct xe_device *xe, struct dentry *root)
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-350- * /sys/kernel/debug/dri/BDF/
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:351: * ├── sriov # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-352- * │ ├── ...
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-356- return;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:357: dent->d_inode->i_private = xe;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-358-
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-362- * /sys/kernel/debug/dri/BDF/
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:363: * ├── sriov # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:364: * │ ├── pf # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-365- * │ │ ├── ...
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-369- return;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:370: pfdent->d_inode->i_private = xe;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-371-
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-376- * /sys/kernel/debug/dri/BDF/
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:377: * ├── sriov # d_inode->i_private = (xe_device*)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:378: * │ ├── vf1 # d_inode->i_private = VFID(1)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:379: * │ ├── vf2 # d_inode->i_private = VFID(2)
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-380- * │ ├── ...
--
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-386- return;
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c:387: vfdent->d_inode->i_private = (void *)(uintptr_t)VFID(n);
drivers/gpu/drm/xe/xe_sriov_pf_debugfs.c-388-
--
drivers/gpu/drm/xe/xe_tile_debugfs.c=14=static struct xe_tile *node_to_tile(struct drm_info_node *node)
drivers/gpu/drm/xe/xe_tile_debugfs.c-15-{
drivers/gpu/drm/xe/xe_tile_debugfs.c:16: return node->dent->d_parent->d_inode->i_private;
drivers/gpu/drm/xe/xe_tile_debugfs.c-17-}
--
]
]
|
| 40/2 |
2026/02/23 15:29 |
llm |
expert |
0m
Model:
Tokens:
input: 51447
output: 39
thoughts: 2655
Thoughts: **Benign Data Race in VFS RCU Walk**
Okay, so I'm looking at this KCSAN report about a potential data race with `d_inode` in the VFS rcu-walk code. I see that there are many direct accesses to `d_inode`, but most are protected by some kind of lock, like `inode_lock`. The rcu-walk, however, is lockless, which raises this concern.
The `step_into` function in `fs/namei.c` is what's flagged. Specifically, the line `struct inode *inode = dentry->d_inode;` doesn't use `READ_ONCE`. While this might be for performance reasons or just historical, KCSAN is correct in identifying it as a data race. My initial instinct is to categorize this as "benign" in kernel terms, because it's handled by the seqlock.
Let me look closely at `step_into`:
```c
2138: struct inode *inode = dentry->d_inode;
2139: if (read_seqcount_retry(&dentry->d_seq, nd->next_seq))
2140: return ERR_PTR(-ECHILD);
```
If I change it to `struct inode *inode = d_inode_rcu(dentry);`, it might quiet KCSAN, but I want to be certain this is the right thing to do.
Okay, I see there's been discussion of this already, and VFS maintainers are aware, so that gives me some confidence.
I need to make sure I understand the write side correctly. Specifically, in `dcache.c`. I see `__d_clear_type_and_inode` setting `dentry->d_inode = NULL`. It's a plain store, so no inherent memory barriers there.
If I look at `dentry_unlink_inode`, I see:
```c
456: raw_write_seqcount_begin(&dentry->d_seq);
457: __d_clear_type_and_inode(dentry);
458: hlist_del_init(&dentry->d_u.d_alias);
459: raw_write_seqcount_end(&dentry->d_seq);
```
The sequence begin and end provide the necessary barriers, preventing reordering.
The only real "danger" is compiler reordering or tearing. On x86_64, pointer tearing is not an issue, and reordering is prevented by `smp_wmb()` in `raw_write_seqcount_begin` and `smp_rmb()` in `read_seqcount_retry`.
So it seems the race *is* benign.
Let's double-check the `smp_rmb()` in `read_seqcount_retry`:
```c
static inline int do_read_seqcount_retry(const seqcount_t *s, unsigned start)
{
smp_rmb();
return do___read_seqcount_retry(s, start);
}
```
Yes, it's there. And the writer is doing the right things, too.
The sequence: Writer first increments the sequence, then writes the `d_inode = NULL` after `smp_wmb()`. The reader reads `d_inode`, then checks the seqlock after `smp_rmb()`.
If the reader sees `NULL`, they *must* also see the odd sequence number because the `smp_wmb()` acts as a barrier, and the reader will then notice that the seqlock has changed and retry.
What about the case where `nd->next_seq` is already odd? The `__read_seqcount_begin` ensures that `nd->next_seq` is always even.
```c
#define __read_seqcount_begin(s) \
({ \
unsigned __seq; \
\
while (unlikely((__seq = seqprop_sequence(s)) & 1)) \
cpu_relax(); \
\
kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \
__seq; \
})
```
So the logic holds. This race *is* benign.
Now I have to look at `d_managed` and `d_is_symlink` which are called in `step_into`. They use `d_flags`. In `__d_clear_type_and_inode`, I
|
| 41/2 |
2026/02/23 15:29 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/dcache.c IncludeLines:true Name:__d_move]
Results: map[SourceCode:2869: /*
2870: * __d_move - move a dentry
2871: * @dentry: entry to move
2872: * @target: new dentry
2873: * @exchange: exchange the two dentries
2874: *
2875: * Update the dcache to reflect the move of a file name. Negative dcache
2876: * entries should not be moved in this way. Caller must hold rename_lock, the
2877: * i_rwsem of the source and target directories (exclusively), and the sb->
2878: * s_vfs_rename_mutex if they differ. See lock_rename().
2879: */
2880: static void __d_move(struct dentry *dentry, struct dentry *target,
2881: bool exchange)
2882: {
2883: struct dentry *old_parent, *p;
2884: wait_queue_head_t *d_wait;
2885: struct inode *dir = NULL;
2886: unsigned n;
2887:
2888: WARN_ON(!dentry->d_inode);
2889: if (WARN_ON(dentry == target))
2890: return;
2891:
2892: BUG_ON(d_ancestor(target, dentry));
2893: old_parent = dentry->d_parent;
2894: p = d_ancestor(old_parent, target);
2895: if (IS_ROOT(dentry)) {
2896: BUG_ON(p);
2897: spin_lock(&target->d_parent->d_lock);
2898: } else if (!p) {
2899: /* target is not a descendent of dentry->d_parent */
2900: spin_lock(&target->d_parent->d_lock);
2901: spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED);
2902: } else {
2903: BUG_ON(p == dentry);
2904: spin_lock(&old_parent->d_lock);
2905: if (p != target)
2906: spin_lock_nested(&target->d_parent->d_lock,
2907: DENTRY_D_LOCK_NESTED);
2908: }
2909: spin_lock_nested(&dentry->d_lock, 2);
2910: spin_lock_nested(&target->d_lock, 3);
2911:
2912: if (unlikely(d_in_lookup(target))) {
2913: dir = target->d_parent->d_inode;
2914: n = start_dir_add(dir);
2915: d_wait = __d_lookup_unhash(target);
2916: }
2917:
2918: write_seqcount_begin(&dentry->d_seq);
2919: write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
2920:
2921: /* unhash both */
2922: if (!d_unhashed(dentry))
2923: ___d_drop(dentry);
2924: if (!d_unhashed(target))
2925: ___d_drop(target);
2926:
2927: /* ... and switch them in the tree */
2928: dentry->d_parent = target->d_parent;
2929: if (!exchange) {
2930: copy_name(dentry, target);
2931: target->d_hash.pprev = NULL;
2932: dentry->d_parent->d_lockref.count++;
2933: if (dentry != old_parent) /* wasn't IS_ROOT */
2934: WARN_ON(!--old_parent->d_lockref.count);
2935: } else {
2936: target->d_parent = old_parent;
2937: swap_names(dentry, target);
2938: if (!hlist_unhashed(&target->d_sib))
2939: __hlist_del(&target->d_sib);
2940: hlist_add_head(&target->d_sib, &target->d_parent->d_children);
2941: __d_rehash(target);
2942: fsnotify_update_flags(target);
2943: }
2944: if (!hlist_unhashed(&dentry->d_sib))
2945: __hlist_del(&dentry->d_sib);
2946: hlist_add_head(&dentry->d_sib, &dentry->d_parent->d_children);
2947: __d_rehash(dentry);
2948: fsnotify_update_flags(dentry);
2949: fscrypt_handle_d_move(dentry);
2950:
2951: write_seqcount_end(&target->d_seq);
2952: write_seqcount_end(&dentry->d_seq);
2953:
2954: if (dir)
2955: end_dir_add(dir, n, d_wait);
2956:
2957: if (dentry->d_parent != old_parent)
2958: spin_unlock(&dentry->d_parent->d_lock);
2959: if (dentry != old_parent)
2960: spin_unlock(&old_parent->d_lock);
2961: spin_unlock(&target->d_lock);
2962: spin_unlock(&dentry->d_lock);
2963: }
SourceFile:fs/dcache.c]
|
| 42/2 |
2026/02/23 15:29 |
llm |
expert |
0m
Model:
Tokens:
input: 56782
output: 19
thoughts: 274
Thoughts: **My Analysis of the Data Race in Dentry Operations**
Okay, let me break this down. I'm looking at how `__d_move` interacts with the dentry structure and I'm seeing it leverages `write_seqcount_begin/end`. Interesting. It doesn't appear to directly modify the `d_inode` field, unless, and let me double check, unless that `exchange` flag is set. No, that swaps names and parents, not the inode itself. Aha, wait... what about `d_splice_alias`? Right. Actually, `d_inode` is only ever modified during dentry instantiation or when it gets unlinked.
So, here's the core of it: the data race is happening between the point where `dentry->d_inode` is set to NULL, and where `inode = dentry->d_inode` is read. Simple reads and writes, but they're being coordinated using that `d_seq` seqlock.
Specifically:
1. The potential race condition exists because the reader of the `d_inode` pointer could see an outdated value.
2. Both reads and writes are straightforward memory accesses.
3. Synchronization is handled by the seqlock (`d_seq`) mechanism. The reader checks the seqlock after reading `d_inode` to ensure that the read was consistent.
4. The writer updates the seqlock before and after updating `d_inode`.
5. Memory barriers within the seqlock primitives are crucial here, they're ensuring the correct ordering of memory operations.
6. On 64-bit architectures, aligned pointer accesses are atomic. We don't have to worry about the pointer "tearing" during reads and writes.
7. The inodes themselves are RCU-protected, so even if the inode is undergoing freeing operations the pointer itself is valid.
8. The RCU-walk design within the VFS specifically considers and handles this sort of race condition explicitly.
All things considered, this data race is completely benign. The mechanisms are in place to prevent any corruption or incorrect behavior. I can confidently conclude that.
|
| 43/2 |
2026/02/23 15:30 |
tool |
set-results |
0m
Args: map[Benign:true Confident:true]
Results: map[Benign:true Confident:true]
|
| 44/2 |
2026/02/23 15:30 |
llm |
expert |
0m
Model:
Tokens:
input: 57556
output: 599
thoughts: 286
Thoughts: **My Assessment of the Data Race**
Okay, so the user wants my take on this potential data race. After careful consideration, I'm confident that this specific race condition is benign. Here's why I've come to that conclusion:
The core of the issue revolves around `dentry->d_inode`. We have a writer, `dentry_unlink_inode`, that's setting this pointer to `NULL`. The crucial point is that this write happens under the protection of `dentry->d_lock` *and* within a `dentry->d_seq` seqlock write-side critical section. Meanwhile, the reader, most likely `step_into` during an RCU walk, is accessing this same pointer without any explicit locking.
Now, this *could* be a recipe for disaster, but the kernel's design cleverly mitigates the risk. The reader doesn't just blindly trust what it reads. Immediately after reading `dentry->d_inode`, it validates the read against `dentry->d_seq` using `read_seqcount_retry`. This is key.
If the write to `dentry->d_inode` was in progress or completed *after* the reader sampled the sequence number from `dentry->d_seq`, the `read_seqcount_retry` will fail. The reader then gracefully handles this by retrying the operation, effectively dropping out of the RCU walk and starting over.
Crucially, if `read_seqcount_retry` *succeeds*, the reader is guaranteed that the value of `dentry->d_inode` it sees is consistent with the state of the sequence number. That's the real power of the seqlock.
Furthermore, on the x86_64 architecture, which I'm assuming is the platform in question, aligned 64-bit pointer accesses (like reading a `struct inode*`) are atomic. This ensures that we don't have to worry about word tearing, a common concern in concurrent memory access.
Finally, we also need to consider the RCU context. Because inodes are RCU-protected, this helps guarantee that the `dentry->d_inode` pointer remains valid during the RCU walk. Even if the underlying inode is being unlinked, it's safe within the RCU domain.
This entire pattern, with seqlocks protecting readers from writers and validated reads, is a pretty standard practice within the VFS rcu-walk implementation. So, while a data race is technically present, the kernel's design ensures it's functionally benign. It's a well-thought-out approach to concurrent access.
|