| 0/0 |
2026/11/18 07:20 |
flow |
bisect-cause |
1h54m
Results: map[BisectReasoning:### Bisection Analysis & Root Cause
#### 1. Root Cause
KCSAN reports a 2-byte data race on `struct inode::i_mode` (`umode_t`) between `dnotify_flush()` and `kernfs_iop_getattr()`:
- **Read side (`fs/notify/dnotify/dnotify.c:161`)**:
During `close()` (`__x64_sys_close` -> `filp_close_sync` -> `filp_flush` -> `dnotify_flush`), `dnotify_flush()` reads `inode->i_mode` without holding any VFS, inode, or filesystem lock:
```c
inode = file_inode(filp);
if (!S_ISDIR(inode->i_mode))
return;
```
In the VFS, the file-type bits (`S_IFMT`) of `inode->i_mode` are invariant once an inode is initialized (`I_NEW` cleared), and VFS paths such as `dnotify_flush()` expect `inode->i_mode` not to be gratuitously overwritten during read-only operations.
- **Write side (`fs/kernfs/inode.c:169`, inlined into `kernfs_iop_getattr` at line 191)**:
During `newfstat()` (`__x64_sys_newfstat` -> `vfs_fstat` -> `vfs_getattr` -> `vfs_getattr_nosec` -> `kernfs_iop_getattr`), `kernfs_iop_getattr()` acquires `down_read(&root->kernfs_iattr_rwsem)` and calls `kernfs_refresh_inode(kn, inode)`:
```c
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs;
inode->i_mode = kn->mode;
...
```
`kernfs_refresh_inode()` performs an unconditional, unmarked plain 2-byte store to `inode->i_mode` on every `.getattr` (and `.permission`) call on a live VFS inode—even when `inode->i_mode` already equals `kn->mode`—while holding only a kernfs-internal lock that VFS callers like `dnotify_flush()` never acquire. Under KCSAN (`CONFIG_KCSAN_STRICT=y`), this concurrent unmarked store and load on `inode->i_mode` is detected and reported as a data race.
---
#### 2. Git History Traced & Candidate Commits Examined
1. **Read path (`fs/notify/dnotify/dnotify.c` and `fs/open.c`)**:
- `1da177e4c3f4` ("Linux-2.6.12-rc2"): `if (!S_ISDIR(inode->i_mode))` in `dnotify_flush()` and the `dnotify_flush(filp, id)` call in `filp_flush()` both predate git history.
- `1abf0c718f15` ("fs: make O_PATH file descriptors usable for 'fstat()'") and `2b256ebe8287` ("fs: add filp_close_sync()"): Only gated `dnotify_flush()` on `!(filp->f_mode & FMODE_PATH)` and refactored `filp_close` into a synchronous helper; neither altered locking around `dnotify_flush()` or `inode->i_mode`.
2. **Write path (`fs/kernfs/inode.c` <- `fs/sysfs/inode.c`)**:
Tracing `kernfs_refresh_inode()` line 169 (`inode->i_mode = kn->mode;`) and `kernfs_iop_getattr()` backwards through git history:
- `9caf69614225` ("kernfs: Introduce separate rwsem to protect inode attributes.", 2023-03-09): Switched `kernfs_iop_getattr()` from `down_read(&root->kernfs_rwsem)` to `down_read(&root->kernfs_iattr_rwsem)`. Ruled out: `dnotify_flush()` acquires neither rwsem.
- `aa1d058d48f2` ("kernfs: dont take i_lock on inode attr read", 2022-10-18) & `47b5c64d0ab5` ("kernfs: use i_lock to protect concurrent inode updates", 2021-07-16) & `7ba0273b2f34` ("kernfs: switch kernfs to use an rwsem", 2021-07-16):
`47b5c64d0ab5` converted `kernfs_iop_getattr()` from `down_write(&kernfs_rwsem)` to `down_read(&kernfs_rwsem)` plus `spin_lock(&inode->i_lock)`, and `aa1d058d48f2` subsequently dropped `spin_lock(&inode->i_lock)`. Ruled out as the introducing commit for this bug because `dnotify_flush()` does **not** hold `inode->i_lock`, `kernfs_rwsem`, or `kernfs_mutex`; thus, the exact data race between `dnotify_flush()` and `kernfs_iop_getattr()` already existed prior to `aa1d058d48f2` and `47b5c64d0ab5`.
- `c637b8acbe07` ("kernfs: s/sysfs/kernfs/ in internal functions and whatever is left", 2013-12-11): Pure identifier rename (`sysfs_refresh_inode` -> `kernfs_refresh_inode`, `sysfs_getattr` -> `kernfs_iop_getattr`). Ruled out (pure rename).
- `adc5e8b58f48` ("kernfs: drop s_ prefix from kernfs_node members", 2013-12-11): Pure member rename (`kn->s_mode` -> `kn->mode`). Ruled out (pure rename).
- `ffed24e22845` ("sysfs, kernfs: move inode code to fs/kernfs/inode.c", 2013-11-28): Pure file move of `fs/sysfs/inode.c` to `fs/kernfs/inode.c`. Ruled out (pure code movement).
- **`e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` ("sysfs: Implement sysfs_getattr & sysfs_permission", 2009-11-20)**:
Before this commit, `sysfs` did not define `.getattr` or `.permission` inode operations; `fstat()` / `vfs_getattr()` simply called `generic_fillattr()`, which only read `inode->i_mode`, and `inode->i_mode` was only written during new-inode initialization (`I_NEW`) or explicit `setattr` (`chmod`) under `inode->i_mutex`.
Commit `e61ab4ae48fb` introduced lazy attribute propagation via `sysfs_refresh_inode()`:
```c
static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
{
struct sysfs_inode_attrs *iattrs = sd->s_iattr;
inode->i_mode = sd->s_mode;
...
```
and wired up `sysfs_getattr()` (`.getattr`) and `sysfs_permission()` (`.permission`) to call `sysfs_refresh_inode()` on live VFS inodes under `sysfs_mutex`. Because VFS paths such as `dnotify_flush()` read `inode->i_mode` without acquiring `sysfs_mutex` (or its successors in `kernfs`), this commit directly introduced the unmarked concurrent write to `inode->i_mode` during `getattr`.
---
#### 3. Conclusion
The bug was introduced by commit **`e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`** (`sysfs: Implement sysfs_getattr & sysfs_permission`). Commits:[map[AuthorEmail:ebiederm@xmission.com AuthorName:Eric W. Biederman Date:2009-11-20T16:08:53-08:00 Hash:e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0 Recipients:[map[Email:ebiederm@aristanetworks.com Name: To:true] map[Email:ebiederm@xmission.com Name: To:true] map[Email:gregkh@suse.de Name: To:true] map[Email:serue@us.ibm.com Name: To:true] map[Email:tj@kernel.org Name: To:true]] Title:sysfs: Implement sysfs_getattr & sysfs_permission]] KernelCommit:63fdb511c27afcebce797e3db580b3534ddfa639]
|
| 1/1 |
2026/11/18 07:20 |
action |
use-crash-commit |
0m
Results: map[KernelCommit:63fdb511c27afcebce797e3db580b3534ddfa639]
|
| 2/1 |
2026/11/18 07:20 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 3/1 |
2026/11/18 07:20 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/85c4c4f3b730b6e1f3d3b07fab639eb9e0c33b71/data]
|
| 4/1 |
2026/11/18 07:20 |
action |
kernel-builder |
0m
Results: map[KernelObj:/app/workdir/cache/build/c779c0ec9b156e2661f717e7eac528a6edb3233e/data]
|
| 5/1 |
2026/11/18 07:20 |
action |
codesearch-prepare |
0m
Results: map[Index:codesearch-index]
|
| 6/1 |
2026/11/18 07:21 |
agent |
cause-finder |
1h54m
Model:
Results: map[CandidateCommits:[e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0]]
Instruction:
You are an experienced Linux kernel developer tasked with identifying the commit
that introduced a kernel bug (cause bisection).
Your investigation strategy:
1. Establish the root cause first.
Analyze the crash report and the code it points to (stack traces, allocation/free stacks,
the involved functions and structs) until you understand the exact invariant that is
violated and the exact execution sequence that leads to the crash.
Identify the specific functions, structs, invariants, or lines of code where the buggy
logic resides.
2. Use git tools (git-blame, git-log, git-show) to trace the history
of the affected functions and lines.
3. Trace past pure refactorings, code movements, formatting changes, or variable renames
to find the commit that actually introduced the buggy logic or broke an existing invariant.
4. Do NOT blame commits that merely added debugging or sanitizer checks (WARN_ON, BUG_ON,
KASAN/KMSAN/LOCKDEP annotations) that exposed an older bug, unless the check itself is
incorrect and is the bug.
5. Do NOT return a merge commit unless the bug was introduced by the merge conflict
resolution itself; otherwise identify the underlying non-merge commit(s).
6. Verify your hypothesis by inspecting the full commit diff with git-show and
confirming how the change introduced the exact condition described in the root cause.
Output rules:
- If you can confidently determine the single commit, return its hash in CandidateCommits
(as a single-element list).
- If multiple commits contributed or you can narrow it down to a small set of candidate
commits (at most 3), return their hashes in CandidateCommits ordered by relevance: the
FIRST commit must be the primary commit, followed by the remaining commits in descending
order of importance/likelihood.
- If you cannot determine the commit with confidence, return an empty CandidateCommits list
rather than guessing.
- Your text reply must provide a clear bisection log explaining the root cause of the bug,
the git history you traced, the candidate commits you examined (with their hashes and
titles), why you ruled out other commits, and how you reached your conclusion.
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The crash is:
==================================================================
BUG: KCSAN: data-race in dnotify_flush / kernfs_iop_getattr
read to 0xffff88810632faa0 of 2 bytes by task 3286 on cpu 1:
dnotify_flush+0x32/0x380 fs/notify/dnotify/dnotify.c:161
filp_flush+0xcd/0x130 fs/open.c:1549
filp_close_sync fs/open.c:1573 [inline]
__do_sys_close fs/open.c:1597 [inline]
__se_sys_close fs/open.c:1584 [inline]
__x64_sys_close+0x4b/0xf0 fs/open.c:1584
x64_sys_call+0x812/0x2550 arch/x86/include/generated/asm/syscalls_64.h:4
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
write to 0xffff88810632faa0 of 2 bytes by task 3283 on cpu 0:
kernfs_refresh_inode fs/kernfs/inode.c:169 [inline]
kernfs_iop_getattr+0xa1/0x220 fs/kernfs/inode.c:191
vfs_getattr_nosec+0x194/0x230 fs/stat.c:217
vfs_getattr fs/stat.c:266 [inline]
vfs_fstat fs/stat.c:285 [inline]
__do_sys_newfstat fs/stat.c:555 [inline]
__se_sys_newfstat+0x96/0x460 fs/stat.c:550
__x64_sys_newfstat+0x31/0x40 fs/stat.c:550
x64_sys_call+0x1b5f/0x2550 arch/x86/include/generated/asm/syscalls_64.h:6
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 3283 Comm: udevd Not tainted syzkaller #0 PREEMPT(lazy)
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================
Search for the commit(s) that introduced this bug.
Reply: ### Bisection Analysis & Root Cause
#### 1. Root Cause
KCSAN reports a 2-byte data race on `struct inode::i_mode` (`umode_t`) between `dnotify_flush()` and `kernfs_iop_getattr()`:
- **Read side (`fs/notify/dnotify/dnotify.c:161`)**:
During `close()` (`__x64_sys_close` -> `filp_close_sync` -> `filp_flush` -> `dnotify_flush`), `dnotify_flush()` reads `inode->i_mode` without holding any VFS, inode, or filesystem lock:
```c
inode = file_inode(filp);
if (!S_ISDIR(inode->i_mode))
return;
```
In the VFS, the file-type bits (`S_IFMT`) of `inode->i_mode` are invariant once an inode is initialized (`I_NEW` cleared), and VFS paths such as `dnotify_flush()` expect `inode->i_mode` not to be gratuitously overwritten during read-only operations.
- **Write side (`fs/kernfs/inode.c:169`, inlined into `kernfs_iop_getattr` at line 191)**:
During `newfstat()` (`__x64_sys_newfstat` -> `vfs_fstat` -> `vfs_getattr` -> `vfs_getattr_nosec` -> `kernfs_iop_getattr`), `kernfs_iop_getattr()` acquires `down_read(&root->kernfs_iattr_rwsem)` and calls `kernfs_refresh_inode(kn, inode)`:
```c
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs;
inode->i_mode = kn->mode;
...
```
`kernfs_refresh_inode()` performs an unconditional, unmarked plain 2-byte store to `inode->i_mode` on every `.getattr` (and `.permission`) call on a live VFS inode—even when `inode->i_mode` already equals `kn->mode`—while holding only a kernfs-internal lock that VFS callers like `dnotify_flush()` never acquire. Under KCSAN (`CONFIG_KCSAN_STRICT=y`), this concurrent unmarked store and load on `inode->i_mode` is detected and reported as a data race.
---
#### 2. Git History Traced & Candidate Commits Examined
1. **Read path (`fs/notify/dnotify/dnotify.c` and `fs/open.c`)**:
- `1da177e4c3f4` ("Linux-2.6.12-rc2"): `if (!S_ISDIR(inode->i_mode))` in `dnotify_flush()` and the `dnotify_flush(filp, id)` call in `filp_flush()` both predate git history.
- `1abf0c718f15` ("fs: make O_PATH file descriptors usable for 'fstat()'") and `2b256ebe8287` ("fs: add filp_close_sync()"): Only gated `dnotify_flush()` on `!(filp->f_mode & FMODE_PATH)` and refactored `filp_close` into a synchronous helper; neither altered locking around `dnotify_flush()` or `inode->i_mode`.
2. **Write path (`fs/kernfs/inode.c` <- `fs/sysfs/inode.c`)**:
Tracing `kernfs_refresh_inode()` line 169 (`inode->i_mode = kn->mode;`) and `kernfs_iop_getattr()` backwards through git history:
- `9caf69614225` ("kernfs: Introduce separate rwsem to protect inode attributes.", 2023-03-09): Switched `kernfs_iop_getattr()` from `down_read(&root->kernfs_rwsem)` to `down_read(&root->kernfs_iattr_rwsem)`. Ruled out: `dnotify_flush()` acquires neither rwsem.
- `aa1d058d48f2` ("kernfs: dont take i_lock on inode attr read", 2022-10-18) & `47b5c64d0ab5` ("kernfs: use i_lock to protect concurrent inode updates", 2021-07-16) & `7ba0273b2f34` ("kernfs: switch kernfs to use an rwsem", 2021-07-16):
`47b5c64d0ab5` converted `kernfs_iop_getattr()` from `down_write(&kernfs_rwsem)` to `down_read(&kernfs_rwsem)` plus `spin_lock(&inode->i_lock)`, and `aa1d058d48f2` subsequently dropped `spin_lock(&inode->i_lock)`. Ruled out as the introducing commit for this bug because `dnotify_flush()` does **not** hold `inode->i_lock`, `kernfs_rwsem`, or `kernfs_mutex`; thus, the exact data race between `dnotify_flush()` and `kernfs_iop_getattr()` already existed prior to `aa1d058d48f2` and `47b5c64d0ab5`.
- `c637b8acbe07` ("kernfs: s/sysfs/kernfs/ in internal functions and whatever is left", 2013-12-11): Pure identifier rename (`sysfs_refresh_inode` -> `kernfs_refresh_inode`, `sysfs_getattr` -> `kernfs_iop_getattr`). Ruled out (pure rename).
- `adc5e8b58f48` ("kernfs: drop s_ prefix from kernfs_node members", 2013-12-11): Pure member rename (`kn->s_mode` -> `kn->mode`). Ruled out (pure rename).
- `ffed24e22845` ("sysfs, kernfs: move inode code to fs/kernfs/inode.c", 2013-11-28): Pure file move of `fs/sysfs/inode.c` to `fs/kernfs/inode.c`. Ruled out (pure code movement).
- **`e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` ("sysfs: Implement sysfs_getattr & sysfs_permission", 2009-11-20)**:
Before this commit, `sysfs` did not define `.getattr` or `.permission` inode operations; `fstat()` / `vfs_getattr()` simply called `generic_fillattr()`, which only read `inode->i_mode`, and `inode->i_mode` was only written during new-inode initialization (`I_NEW`) or explicit `setattr` (`chmod`) under `inode->i_mutex`.
Commit `e61ab4ae48fb` introduced lazy attribute propagation via `sysfs_refresh_inode()`:
```c
static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
{
struct sysfs_inode_attrs *iattrs = sd->s_iattr;
inode->i_mode = sd->s_mode;
...
```
and wired up `sysfs_getattr()` (`.getattr`) and `sysfs_permission()` (`.permission`) to call `sysfs_refresh_inode()` on live VFS inodes under `sysfs_mutex`. Because VFS paths such as `dnotify_flush()` read `inode->i_mode` without acquiring `sysfs_mutex` (or its successors in `kernfs`), this commit directly introduced the unmarked concurrent write to `inode->i_mode` during `getattr`.
---
#### 3. Conclusion
The bug was introduced by commit **`e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`** (`sysfs: Implement sysfs_getattr & sysfs_permission`).
|
| 7/2 |
2026/11/18 07:21 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 5172
output: 202
thoughts: 484
Thoughts: **KCSAN Data-Race Analysis: Examining `dnotify_flush` and `kernfs_iop_getattr`**
My immediate priority is to meticulously scrutinize this KCSAN data-race report. It's flagging a race condition between `dnotify_flush` and `kernfs_iop_getattr`, specifically concerning a two-byte write to the memory address `0xffff88810632faa0`. This immediately makes me think of `umode_t`, which is precisely the size of an inode's `i_mode`.
The report details a read operation within `dnotify_flush` (task 3286 on CPU 1) and a write operation within `kernfs_iop_getattr` (task 3283 on CPU 0) to the same memory location. This confirms my suspicion about a shared resource being accessed concurrently without proper synchronization.
My next step is to dive directly into the source code. I need to examine `fs/notify/dnotify/dnotify.c` around line 161, where the `dnotify_flush` function is called, and `fs/kernfs/inode.c` around lines 169 and 191, where `kernfs_iop_getattr` is implemented. To gain deeper context, I'll be utilizing `codesearch-definition-source` to understand the declarations and definitions related to this memory region and the involved functions. Additionally, `git-blame` will be crucial for tracing the history of these code sections, potentially revealing when and why this race condition might have been introduced or become exposed. This focused analysis of the relevant code paths and their historical context is paramount to understanding the root cause of this data-race.
|
| 8/2 |
2026/11/18 07:21 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/notify/dnotify/dnotify.c Name:dnotify_flush]
Results: map[SourceCode: 144: /*
145: * Called every time a file is closed. Looks first for a dnotify mark on the
146: * inode. If one is found run all of the ->dn structures attached to that
147: * mark for one relevant to this process closing the file and remove that
148: * dnotify_struct. If that was the last dnotify_struct also remove the
149: * fsnotify_mark.
150: */
151: void dnotify_flush(struct file *filp, fl_owner_t id)
152: {
153: struct fsnotify_mark *fsn_mark;
154: struct dnotify_mark *dn_mark;
155: struct dnotify_struct *dn;
156: struct dnotify_struct **prev;
157: struct inode *inode;
158: bool free = false;
159:
160: inode = file_inode(filp);
161: if (!S_ISDIR(inode->i_mode))
162: return;
163:
164: fsn_mark = fsnotify_find_inode_mark(inode, dnotify_group);
165: if (!fsn_mark)
166: return;
167: dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
168:
169: fsnotify_group_lock(dnotify_group);
170:
171: spin_lock(&fsn_mark->lock);
172: prev = &dn_mark->dn;
173: while ((dn = *prev) != NULL) {
174: if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
175: *prev = dn->dn_next;
176: kmem_cache_free(dnotify_struct_cache, dn);
177: dnotify_recalc_inode_mask(fsn_mark);
178: break;
179: }
180: prev = &dn->dn_next;
181: }
182:
183: spin_unlock(&fsn_mark->lock);
184:
185: /* nothing else could have found us thanks to the dnotify_groups
186: mark_mutex */
187: if (dn_mark->dn == NULL) {
188: fsnotify_detach_mark(fsn_mark);
189: free = true;
190: }
191:
192: fsnotify_group_unlock(dnotify_group);
193:
194: if (free)
195: fsnotify_free_mark(fsn_mark);
196: fsnotify_put_mark(fsn_mark);
197: }
SourceFile:fs/notify/dnotify/dnotify.c]
|
| 9/2 |
2026/11/18 07:21 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/kernfs/inode.c Name:kernfs_refresh_inode]
Results: map[SourceCode: 165: static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
166: {
167: struct kernfs_iattrs *attrs;
168:
169: inode->i_mode = kn->mode;
170: attrs = kernfs_iattrs_noalloc(kn);
171: if (attrs)
172: /*
173: * kernfs_node has non-default attributes get them from
174: * persistent copy in kernfs_node.
175: */
176: set_inode_attr(inode, attrs);
177:
178: if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING))
179: set_nlink(inode, kn->dir.subdirs + 2);
180: }
SourceFile:fs/kernfs/inode.c]
|
| 10/2 |
2026/11/18 07:21 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/kernfs/inode.c Name:kernfs_iop_getattr]
Results: map[SourceCode: 182: int kernfs_iop_getattr(const struct mnt_idmap *idmap,
183: const struct path *path, struct kstat *stat,
184: u32 request_mask, unsigned int query_flags)
185: {
186: struct inode *inode = d_inode(path->dentry);
187: struct kernfs_node *kn = inode->i_private;
188: struct kernfs_root *root = kernfs_root(kn);
189:
190: down_read(&root->kernfs_iattr_rwsem);
191: kernfs_refresh_inode(kn, inode);
192: generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
193: up_read(&root->kernfs_iattr_rwsem);
194:
195: return 0;
196: }
SourceFile:fs/kernfs/inode.c]
|
| 11/2 |
2026/11/18 07:21 |
tool |
git-blame |
0m
Args: map[End:205 File:fs/kernfs/inode.c Start:150]
Results: map[Output:ffed24e22845a 150) {
ffed24e22845a 151) inode->i_mode = mode;
2a45ac155937a 152) simple_inode_init_ts(inode);
ffed24e22845a 153) }
ffed24e22845a 154)
05895219627c4 155) static inline void set_inode_attr(struct inode *inode,
05895219627c4 156) struct kernfs_iattrs *attrs)
ffed24e22845a 157) {
05895219627c4 158) inode->i_uid = attrs->ia_uid;
05895219627c4 159) inode->i_gid = attrs->ia_gid;
2a45ac155937a 160) inode_set_atime_to_ts(inode, attrs->ia_atime);
2a45ac155937a 161) inode_set_mtime_to_ts(inode, attrs->ia_mtime);
f7f438589f468 162) inode_set_ctime_to_ts(inode, attrs->ia_ctime);
ffed24e22845a 163) }
ffed24e22845a 164)
c637b8acbe079 165) static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
ffed24e22845a 166) {
d1f4e9026007d 167) struct kernfs_iattrs *attrs;
ffed24e22845a 168)
adc5e8b58f488 169) inode->i_mode = kn->mode;
d1f4e9026007d 170) attrs = kernfs_iattrs_noalloc(kn);
0ac6075a32fc0 171) if (attrs)
324a56e16e44b 172) /*
324a56e16e44b 173) * kernfs_node has non-default attributes get them from
324a56e16e44b 174) * persistent copy in kernfs_node.
ffed24e22845a 175) */
05895219627c4 176) set_inode_attr(inode, attrs);
ffed24e22845a 177)
507d8ce13f5b9 178) if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING))
adc5e8b58f488 179) set_nlink(inode, kn->dir.subdirs + 2);
ffed24e22845a 180) }
ffed24e22845a 181)
c3150ded7bc0e 182) int kernfs_iop_getattr(const struct mnt_idmap *idmap,
549c7297717c3 183) const struct path *path, struct kstat *stat,
a528d35e8bfcc 184) u32 request_mask, unsigned int query_flags)
ffed24e22845a 185) {
a528d35e8bfcc 186) struct inode *inode = d_inode(path->dentry);
319ba91d352a7 187) struct kernfs_node *kn = inode->i_private;
393c3714081a5 188) struct kernfs_root *root = kernfs_root(kn);
ffed24e22845a 189)
9caf696142252 190) down_read(&root->kernfs_iattr_rwsem);
c637b8acbe079 191) kernfs_refresh_inode(kn, inode);
0d72b92883c65 192) generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
9caf696142252 193) up_read(&root->kernfs_iattr_rwsem);
47b5c64d0ab5e 194)
ffed24e22845a 195) return 0;
ffed24e22845a 196) }
ffed24e22845a 197)
c637b8acbe079 198) static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
ffed24e22845a 199) {
324a56e16e44b 200) kernfs_get(kn);
324a56e16e44b 201) inode->i_private = kn;
c1e3dbe9818e3 202) inode->i_mapping->a_ops = &ram_aops;
a797bfc305323 203) inode->i_op = &kernfs_iops;
67c0496e87d19 204) inode->i_generation = kernfs_gen(kn);
ffed24e22845a 205)
]
|
| 12/2 |
2026/11/18 07:21 |
tool |
git-blame |
0m
Args: map[End:180 File:fs/notify/dnotify/dnotify.c Start:150]
Results: map[Output:3c5119c05d624 fs/notify/dnotify/dnotify.c 150) */
^1da177e4c3f4 fs/dnotify.c 151) void dnotify_flush(struct file *filp, fl_owner_t id)
^1da177e4c3f4 fs/dnotify.c 152) {
ef5e2b785fb32 fs/notify/dnotify/dnotify.c 153) struct fsnotify_mark *fsn_mark;
ef5e2b785fb32 fs/notify/dnotify/dnotify.c 154) struct dnotify_mark *dn_mark;
^1da177e4c3f4 fs/dnotify.c 155) struct dnotify_struct *dn;
^1da177e4c3f4 fs/dnotify.c 156) struct dnotify_struct **prev;
^1da177e4c3f4 fs/dnotify.c 157) struct inode *inode;
4712e722f9145 fs/notify/dnotify/dnotify.c 158) bool free = false;
^1da177e4c3f4 fs/dnotify.c 159)
496ad9aa8ef44 fs/notify/dnotify/dnotify.c 160) inode = file_inode(filp);
^1da177e4c3f4 fs/dnotify.c 161) if (!S_ISDIR(inode->i_mode))
^1da177e4c3f4 fs/dnotify.c 162) return;
3c5119c05d624 fs/notify/dnotify/dnotify.c 163)
230d97d39ee2e fs/notify/dnotify/dnotify.c 164) fsn_mark = fsnotify_find_inode_mark(inode, dnotify_group);
ef5e2b785fb32 fs/notify/dnotify/dnotify.c 165) if (!fsn_mark)
3c5119c05d624 fs/notify/dnotify/dnotify.c 166) return;
ef5e2b785fb32 fs/notify/dnotify/dnotify.c 167) dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
3c5119c05d624 fs/notify/dnotify/dnotify.c 168)
aabb45fdcb31f fs/notify/dnotify/dnotify.c 169) fsnotify_group_lock(dnotify_group);
3c5119c05d624 fs/notify/dnotify/dnotify.c 170)
ef5e2b785fb32 fs/notify/dnotify/dnotify.c 171) spin_lock(&fsn_mark->lock);
ef5e2b785fb32 fs/notify/dnotify/dnotify.c 172) prev = &dn_mark->dn;
^1da177e4c3f4 fs/dnotify.c 173) while ((dn = *prev) != NULL) {
^1da177e4c3f4 fs/dnotify.c 174) if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
^1da177e4c3f4 fs/dnotify.c 175) *prev = dn->dn_next;
3c5119c05d624 fs/notify/dnotify/dnotify.c 176) kmem_cache_free(dnotify_struct_cache, dn);
ef5e2b785fb32 fs/notify/dnotify/dnotify.c 177) dnotify_recalc_inode_mask(fsn_mark);
^1da177e4c3f4 fs/dnotify.c 178) break;
^1da177e4c3f4 fs/dnotify.c 179) }
^1da177e4c3f4 fs/dnotify.c 180) prev = &dn->dn_next;
]
|
| 13/2 |
2026/11/18 07:21 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 10033
output: 63
thoughts: 518
Thoughts: **Observations on `kernfs_iop_getattr` and Potential Concurrency Issues**
My attention was immediately drawn to the `kernfs_iop_getattr` function. As I reviewed its implementation, I noticed a sequence of operations that gave me pause. Specifically, in lines 190 through 193, I see:
down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
up_read(&root->kernfs_iattr_rwsem);
This is where my internal alarm bells started ringing. The critical part for me is `down_read(&root->kernfs_iattr_rwsem);`. While holding a *read* lock, the function proceeds to call `kernfs_refresh_inode(kn, inode)`. My understanding is that `kernfs_refresh_inode` is responsible for updating several inode attributes, including `inode->i_mode`, `inode->i_uid`, `inode->i_gid`, `inode->i_atime`, `inode->i_mtime`, `inode->i_ctime`, and `inode->i_nlink`.
This immediately presents a concern: I'm writing to inode metadata fields while holding what appears to be a read lock on the `kernfs_iattr_rwsem`. This seems counterintuitive, as one would typically expect write operations to require a write lock.
Furthermore, this situation raises questions about concurrency. If multiple `kernfs_iop_getattr` calls were to execute concurrently, they could each acquire the `down_read` lock. This means that *two* or more such calls could simultaneously enter the critical section and *write* to `inode->i_mode` (and other fields) without exclusive access. This is a potential race condition.
Adding to this suspicion, I also recall that `dnotify_flush` can read `inode->i_mode` without holding any lock related to `kernfs_iattr_rwsem`. This exacerbates the concurrency issue, as it might read stale or inconsistent data if `kernfs_refresh_inode` is modifying it concurrently under a read lock.
The fact that commit `9caf696142252` specifically touched lines 190 and 193 makes me particularly curious. I need to delve into the details of that commit and the surrounding Git history of `fs/kernfs/inode.c` to understand the motivation behind those changes and whether they were intended to address, or perhaps inadvertently introduced, this apparent concurrency concern. My immediate thought is that there might be a misunderstanding of the lock's purpose or a subtle aspect of `kernfs_refresh_inode` that I'm not fully appreciating at this moment.
|
| 14/2 |
2026/11/18 07:23 |
tool |
git-show |
0m
Args: map[Commit:9caf696142252]
Results: map[Output:commit 9caf696142252a466fb89e629d0eddcdced027b0
Author: Imran Khan <imran.f.khan@oracle.com>
Date: Thu Mar 9 22:09:30 2023 +1100
kernfs: Introduce separate rwsem to protect inode attributes.
Right now a global per-fs rwsem (kernfs_rwsem) synchronizes multiple
kernfs operations. On a large system with few hundred CPUs and few
hundred applications simultaneoulsy trying to access sysfs, this
results in multiple sys_open(s) contending on kernfs_rwsem via
kernfs_iop_permission and kernfs_dop_revalidate.
For example on a system with 384 cores, if I run 200 instances of an
application which is mostly executing the following loop:
for (int loop = 0; loop <100 ; loop++)
{
for (int port_num = 1; port_num < 2; port_num++)
{
for (int gid_index = 0; gid_index < 254; gid_index++ )
{
char ret_buf[64], ret_buf_lo[64];
char gid_file_path[1024];
int ret_len;
int ret_fd;
ssize_t ret_rd;
ub4 i, saved_errno;
memset(ret_buf, 0, sizeof(ret_buf));
memset(gid_file_path, 0, sizeof(gid_file_path));
ret_len = snprintf(gid_file_path, sizeof(gid_file_path),
"/sys/class/infiniband/%s/ports/%d/gids/%d",
dev_name,
port_num,
gid_index);
ret_fd = open(gid_file_path, O_RDONLY | O_CLOEXEC);
if (ret_fd < 0)
{
printf("Failed to open %s\n", gid_file_path);
continue;
}
/* Read the GID */
ret_rd = read(ret_fd, ret_buf, 40);
if (ret_rd == -1)
{
printf("Failed to read from file %s, errno: %u\n",
gid_file_path, saved_errno);
continue;
}
close(ret_fd);
}
}
I see contention around kernfs_rwsem as follows:
path_openat
|
|----link_path_walk.part.0.constprop.0
| |
| |--49.92%--inode_permission
| | |
| | --48.69%--kernfs_iop_permission
| | |
| | |--18.16%--down_read
| | |
| | |--15.38%--up_read
| | |
| | --14.58%--_raw_spin_lock
| | |
| | -----
| |
| |--29.08%--walk_component
| | |
| | --29.02%--lookup_fast
| | |
| | |--24.26%--kernfs_dop_revalidate
| | | |
| | | |--14.97%--down_read
| | | |
| | | --9.01%--up_read
| | |
| | --4.74%--__d_lookup
| | |
| | --4.64%--_raw_spin_lock
| | |
| | ----
Having a separate per-fs rwsem to protect kernfs inode attributes,
will avoid the above mentioned contention and result in better
performance as can bee seen below:
path_openat
|
|----link_path_walk.part.0.constprop.0
| |
| |
| |--27.06%--inode_permission
| | |
| | --25.84%--kernfs_iop_permission
| | |
| | |--9.29%--up_read
| | |
| | |--8.19%--down_read
| | |
| | --7.89%--_raw_spin_lock
| | |
| | ----
| |
| |--22.42%--walk_component
| | |
| | --22.36%--lookup_fast
| | |
| | |--16.07%--__d_lookup
| | | |
| | | --16.01%--_raw_spin_lock
| | | |
| | | ----
| | |
| | --6.28%--kernfs_dop_revalidate
| | |
| | |--3.76%--down_read
| | |
| | --2.26%--up_read
As can be seen from the above data the overhead due to both
kerfs_iop_permission and kernfs_dop_revalidate have gone down and
this also reduces overall run time of the earlier mentioned loop.
Signed-off-by: Imran Khan <imran.f.khan@oracle.com>
Link: https://lore.kernel.org/r/20230309110932.2889010-2-imran.f.khan@oracle.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index ef00b5fe8cee..953b2717c60e 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -770,12 +770,15 @@ int kernfs_add_one(struct kernfs_node *kn)
goto out_unlock;
/* Update timestamps on the parent */
+ down_write(&root->kernfs_iattr_rwsem);
+
ps_iattr = parent->iattr;
if (ps_iattr) {
ktime_get_real_ts64(&ps_iattr->ia_ctime);
ps_iattr->ia_mtime = ps_iattr->ia_ctime;
}
+ up_write(&root->kernfs_iattr_rwsem);
up_write(&root->kernfs_rwsem);
/*
@@ -940,6 +943,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
idr_init(&root->ino_idr);
init_rwsem(&root->kernfs_rwsem);
+ init_rwsem(&root->kernfs_iattr_rwsem);
INIT_LIST_HEAD(&root->supers);
/*
@@ -1462,11 +1466,14 @@ static void __kernfs_remove(struct kernfs_node *kn)
pos->parent ? pos->parent->iattr : NULL;
/* update timestamps on the parent */
+ down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
+
if (ps_iattr) {
ktime_get_real_ts64(&ps_iattr->ia_ctime);
ps_iattr->ia_mtime = ps_iattr->ia_ctime;
}
+ up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
kernfs_put(pos);
}
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 30494dcb0df3..b22b74d1a115 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -101,9 +101,9 @@ int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
int ret;
struct kernfs_root *root = kernfs_root(kn);
- down_write(&root->kernfs_rwsem);
+ down_write(&root->kernfs_iattr_rwsem);
ret = __kernfs_setattr(kn, iattr);
- up_write(&root->kernfs_rwsem);
+ up_write(&root->kernfs_iattr_rwsem);
return ret;
}
@@ -119,7 +119,7 @@ int kernfs_iop_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
return -EINVAL;
root = kernfs_root(kn);
- down_write(&root->kernfs_rwsem);
+ down_write(&root->kernfs_iattr_rwsem);
error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
if (error)
goto out;
@@ -132,7 +132,7 @@ int kernfs_iop_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
setattr_copy(&nop_mnt_idmap, inode, iattr);
out:
- up_write(&root->kernfs_rwsem);
+ up_write(&root->kernfs_iattr_rwsem);
return error;
}
@@ -189,10 +189,10 @@ int kernfs_iop_getattr(struct mnt_idmap *idmap,
struct kernfs_node *kn = inode->i_private;
struct kernfs_root *root = kernfs_root(kn);
- down_read(&root->kernfs_rwsem);
+ down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&nop_mnt_idmap, inode, stat);
- up_read(&root->kernfs_rwsem);
+ up_read(&root->kernfs_iattr_rwsem);
return 0;
}
@@ -285,10 +285,10 @@ int kernfs_iop_permission(struct mnt_idmap *idmap,
kn = inode->i_private;
root = kernfs_root(kn);
- down_read(&root->kernfs_rwsem);
+ down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
ret = generic_permission(&nop_mnt_idmap, inode, mask);
- up_read(&root->kernfs_rwsem);
+ up_read(&root->kernfs_iattr_rwsem);
return ret;
}
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index 236c3a6113f1..3297093c920d 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -47,6 +47,7 @@ struct kernfs_root {
wait_queue_head_t deactivate_waitq;
struct rw_semaphore kernfs_rwsem;
+ struct rw_semaphore kernfs_iattr_rwsem;
};
/* +1 to avoid triggering overflow warning when negating it */
]
|
| 15/2 |
2026/11/18 07:23 |
tool |
git-log |
0m
Args: map[Count:30 PathPrefix:fs/kernfs/inode.c]
Results: map[Output:e2f0d6d4569d fs: port ->setattr() to pass const mnt_idmap
c3150ded7bc0 fs: port ->getattr() to pass const mnt_idmap
16d90b57d843 fs: port xattr to const mnt_idmap
41839e7bd92a fs: port ->permission() to pass const mnt_idmap
7b15d6cf25e6 kernfs: preserve security xattrs without allocating iattrs
d5e81a5650b5 kernfs: avoid iattr allocation in listxattr
1e7cd8a53b72 simpe_xattr: use per-sb cache
076e5cef28e2 simple_xattr: change interface to pass struct simple_xattrs **
6a07814ff643 kernfs: fix xattr race condition with multiple superblocks
507d8ce13f5b kernfs: Don't set_nlink for directories being removed
4fbe9e78bb41 xattr: move user limits for xattrs to generic infra
5bd97f5c5f24 kernfs: adapt to rhashtable-based simple_xattrs with lazy allocation
b4dbfd8653b3 Coccinelle-based conversion to use ->i_state accessors
c237aa9884f2 kernfs: don't fail listing extended attributes
d1f4e9026007 kernfs: remove iattr_mutex
2a45ac155937 kernfs: convert to new timestamp accessors
ffb2e0650827 kernfs: move kernfs_xattr_handlers to .rodata
5de75970c9fd xattr: simple_xattr_set() return old_xattr to be freed
0d72b92883c6 fs: pass the request_mask to generic_fillattr
f7f438589f46 kernfs: convert to ctime accessor functions
9caf69614225 kernfs: Introduce separate rwsem to protect inode attributes.
39f60c1ccee7 fs: port xattr to mnt_idmap
4609e1f18e19 fs: port ->permission() to pass mnt_idmap
b74d24f7a74f fs: port ->getattr() to pass mnt_idmap
c1632a0f1120 fs: port ->setattr() to pass mnt_idmap
24b3e3dd9c9c kernfs: fix all kernel-doc warnings and multiple typos
aa1d058d48f2 kernfs: dont take i_lock on inode attr read
393c3714081a kernfs: switch global kernfs_rwsem lock to per-fs lock
47b5c64d0ab5 kernfs: use i_lock to protect concurrent inode updates
7ba0273b2f34 kernfs: switch kernfs to use an rwsem
]
|
| 16/2 |
2026/11/18 07:23 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 14511
output: 160
thoughts: 246
Thoughts: **Observing Kernfs Inode Synchronization Evolution**
My attention was immediately captured by the commit history of `fs/kernfs/inode.c`. It's clear there's been a significant evolution in how inodes within kernfs are synchronized, and I felt compelled to dive deep into the underlying changes. My initial reaction was a strong "WHOA!!" because the sheer number of related commits and the nature of the changes suggest a foundational shift.
I decided to meticulously examine several key commits that stood out:
* `7ba0273b2f34 kernfs: switch kernfs to use an rwsem`: This commit marks a fundamental change in the locking mechanism, moving from whatever was previously used to a read-write semaphore (`rwsem`). This immediately tells me the goal is to improve concurrency by allowing multiple readers while ensuring exclusive access for writers.
* `47b5c64d0ab5 kernfs: use i_lock to protect concurrent inode updates`: This is a critical piece of the puzzle. The introduction of `i_lock` specifically for inode updates indicates a move towards finer-grained locking. Instead of a global lock, inode-level locking is being implemented, which is a standard approach for enhancing concurrency in filesystem-like structures.
* `393c3714081a kernfs: switch global kernfs_rwsem lock to per-fs lock`: This commit further refines the locking strategy. Moving from a global `kernfs_rwsem` to a per-filesystem lock is a significant optimization. It implies that operations on different kernfs instances will no longer block each other, greatly improving scalability.
* `aa1d058d48f2 kernfs: dont take i_lock on inode attr read`: This is a performance optimization. By recognizing that attribute reads can often proceed without holding the inode's update lock, the developers are reducing contention and improving read performance. This suggests a thoughtful analysis of read vs. write patterns.
* `d1f4e9026007 kernfs: remove iattr_mutex`: The removal of `iattr_mutex` is a direct consequence of the other locking changes. It signifies that the functionality previously managed by this mutex is now handled by the `rwsem` and `i_lock`, indicating a consolidation and simplification of the locking infrastructure.
* `adc5e8b58f48`, `ffed24e22845a`, and `c637b8acbe079`: These are also noted as important commits. While their specific descriptions aren't provided here, their inclusion in my thought process means I'm assuming they are integral to the overall transition, likely containing granular bug fixes, performance tweaks, or further refinements of the locking mechanisms.
To gain a comprehensive understanding, I also made a point to investigate the older history of functions like `kernfs_refresh_inode` and `sysfs_refresh_inode`. This is because the changes to inode synchronization in `inode.c` are almost certainly tied to how these refresh operations manage inode state. Understanding the pre-existing patterns and potential issues in these functions would shed light on *why* these locking changes were necessary and what problems they were intended to solve. My goal is to see the complete picture of how kernfs evolved its inode management to be more robust and performant.
|
| 17/2 |
2026/11/18 07:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 7ba0273b2f34a55efe967d3c7381fb1da2ca195f
Author: Ian Kent <raven@themaw.net>
Date: Fri Jul 16 17:28:29 2021 +0800
kernfs: switch kernfs to use an rwsem
The kernfs global lock restricts the ability to perform kernfs node
lookup operations in parallel during path walks.
Change the kernfs mutex to an rwsem so that, when opportunity arises,
node searches can be done in parallel with path walk lookups.
Reviewed-by: Miklos Szeredi <mszeredi@redhat.com>
Signed-off-by: Ian Kent <raven@themaw.net>
Link: https://lore.kernel.org/r/162642770946.63632.2218304587223241374.stgit@web.messagingengine.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 0b21a8f961ac..4994723d6cf7 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -17,7 +17,7 @@
#include "kernfs-internal.h"
-DEFINE_MUTEX(kernfs_mutex);
+DECLARE_RWSEM(kernfs_rwsem);
static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */
static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by rename_lock */
static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */
@@ -26,7 +26,7 @@ static DEFINE_SPINLOCK(kernfs_idr_lock); /* root->ino_idr */
static bool kernfs_active(struct kernfs_node *kn)
{
- lockdep_assert_held(&kernfs_mutex);
+ lockdep_assert_held(&kernfs_rwsem);
return atomic_read(&kn->active) >= 0;
}
@@ -340,7 +340,7 @@ static int kernfs_sd_compare(const struct kernfs_node *left,
* @kn->parent->dir.children.
*
* Locking:
- * mutex_lock(kernfs_mutex)
+ * kernfs_rwsem held exclusive
*
* RETURNS:
* 0 on susccess -EEXIST on failure.
@@ -386,7 +386,7 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
* removed, %false if @kn wasn't on the rbtree.
*
* Locking:
- * mutex_lock(kernfs_mutex)
+ * kernfs_rwsem held exclusive
*/
static bool kernfs_unlink_sibling(struct kernfs_node *kn)
{
@@ -457,14 +457,14 @@ void kernfs_put_active(struct kernfs_node *kn)
* return after draining is complete.
*/
static void kernfs_drain(struct kernfs_node *kn)
- __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
+ __releases(&kernfs_rwsem) __acquires(&kernfs_rwsem)
{
struct kernfs_root *root = kernfs_root(kn);
- lockdep_assert_held(&kernfs_mutex);
+ lockdep_assert_held_write(&kernfs_rwsem);
WARN_ON_ONCE(kernfs_active(kn));
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
if (kernfs_lockdep(kn)) {
rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
@@ -483,7 +483,7 @@ static void kernfs_drain(struct kernfs_node *kn)
kernfs_drain_open_files(kn);
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
}
/**
@@ -722,7 +722,7 @@ int kernfs_add_one(struct kernfs_node *kn)
bool has_ns;
int ret;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
ret = -EINVAL;
has_ns = kernfs_ns_enabled(parent);
@@ -753,7 +753,7 @@ int kernfs_add_one(struct kernfs_node *kn)
ps_iattr->ia_mtime = ps_iattr->ia_ctime;
}
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
/*
* Activate the new node unless CREATE_DEACTIVATED is requested.
@@ -767,7 +767,7 @@ int kernfs_add_one(struct kernfs_node *kn)
return 0;
out_unlock:
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
return ret;
}
@@ -788,7 +788,7 @@ static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
bool has_ns = kernfs_ns_enabled(parent);
unsigned int hash;
- lockdep_assert_held(&kernfs_mutex);
+ lockdep_assert_held(&kernfs_rwsem);
if (has_ns != (bool)ns) {
WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
@@ -820,7 +820,7 @@ static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
size_t len;
char *p, *name;
- lockdep_assert_held(&kernfs_mutex);
+ lockdep_assert_held_read(&kernfs_rwsem);
/* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */
spin_lock_irq(&kernfs_rename_lock);
@@ -860,10 +860,10 @@ struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
{
struct kernfs_node *kn;
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
kn = kernfs_find_ns(parent, name, ns);
kernfs_get(kn);
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
return kn;
}
@@ -884,10 +884,10 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
{
struct kernfs_node *kn;
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
kn = kernfs_walk_ns(parent, path, ns);
kernfs_get(kn);
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
return kn;
}
@@ -1046,18 +1046,18 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
/* If the kernfs parent node has changed discard and
* proceed to ->lookup.
*/
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
spin_lock(&dentry->d_lock);
parent = kernfs_dentry_node(dentry->d_parent);
if (parent) {
if (kernfs_dir_changed(parent, dentry)) {
spin_unlock(&dentry->d_lock);
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
return 0;
}
}
spin_unlock(&dentry->d_lock);
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
/* The kernfs parent node hasn't changed, leave the
* dentry negative and return success.
@@ -1066,7 +1066,7 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
}
kn = kernfs_dentry_node(dentry);
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
/* The kernfs node has been deactivated */
if (!kernfs_active(kn))
@@ -1085,10 +1085,10 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
kernfs_info(dentry->d_sb)->ns != kn->ns)
goto out_bad;
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
return 1;
out_bad:
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
return 0;
}
@@ -1106,7 +1106,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir,
struct inode *inode = NULL;
const void *ns = NULL;
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
if (kernfs_ns_enabled(parent))
ns = kernfs_info(dir->i_sb)->ns;
@@ -1122,7 +1122,7 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir,
kernfs_set_rev(parent, dentry);
/* instantiate and hash (possibly negative) dentry */
ret = d_splice_alias(inode, dentry);
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
return ret;
}
@@ -1244,7 +1244,7 @@ static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
{
struct rb_node *rbn;
- lockdep_assert_held(&kernfs_mutex);
+ lockdep_assert_held_write(&kernfs_rwsem);
/* if first iteration, visit leftmost descendant which may be root */
if (!pos)
@@ -1280,7 +1280,7 @@ void kernfs_activate(struct kernfs_node *kn)
{
struct kernfs_node *pos;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
pos = NULL;
while ((pos = kernfs_next_descendant_post(pos, kn))) {
@@ -1294,14 +1294,14 @@ void kernfs_activate(struct kernfs_node *kn)
pos->flags |= KERNFS_ACTIVATED;
}
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
}
static void __kernfs_remove(struct kernfs_node *kn)
{
struct kernfs_node *pos;
- lockdep_assert_held(&kernfs_mutex);
+ lockdep_assert_held_write(&kernfs_rwsem);
/*
* Short-circuit if non-root @kn has already finished removal.
@@ -1324,7 +1324,7 @@ static void __kernfs_remove(struct kernfs_node *kn)
pos = kernfs_leftmost_descendant(kn);
/*
- * kernfs_drain() drops kernfs_mutex temporarily and @pos's
+ * kernfs_drain() drops kernfs_rwsem temporarily and @pos's
* base ref could have been put by someone else by the time
* the function returns. Make sure it doesn't go away
* underneath us.
@@ -1371,9 +1371,9 @@ static void __kernfs_remove(struct kernfs_node *kn)
*/
void kernfs_remove(struct kernfs_node *kn)
{
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
__kernfs_remove(kn);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
}
/**
@@ -1460,17 +1460,17 @@ bool kernfs_remove_self(struct kernfs_node *kn)
{
bool ret;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
kernfs_break_active_protection(kn);
/*
* SUICIDAL is used to arbitrate among competing invocations. Only
* the first one will actually perform removal. When the removal
* is complete, SUICIDED is set and the active ref is restored
- * while holding kernfs_mutex. The ones which lost arbitration
- * waits for SUICDED && drained which can happen only after the
- * enclosing kernfs operation which executed the winning instance
- * of kernfs_remove_self() finished.
+ * while kernfs_rwsem for held exclusive. The ones which lost
+ * arbitration waits for SUICIDED && drained which can happen only
+ * after the enclosing kernfs operation which executed the winning
+ * instance of kernfs_remove_self() finished.
*/
if (!(kn->flags & KERNFS_SUICIDAL)) {
kn->flags |= KERNFS_SUICIDAL;
@@ -1488,9 +1488,9 @@ bool kernfs_remove_self(struct kernfs_node *kn)
atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
break;
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
schedule();
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
}
finish_wait(waitq, &wait);
WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
@@ -1498,12 +1498,12 @@ bool kernfs_remove_self(struct kernfs_node *kn)
}
/*
- * This must be done while holding kernfs_mutex; otherwise, waiting
- * for SUICIDED && deactivated could finish prematurely.
+ * This must be done while kernfs_rwsem held exclusive; otherwise,
+ * waiting for SUICIDED && deactivated could finish prematurely.
*/
kernfs_unbreak_active_protection(kn);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
return ret;
}
@@ -1527,13 +1527,13 @@ int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
return -ENOENT;
}
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
kn = kernfs_find_ns(parent, name, ns);
if (kn)
__kernfs_remove(kn);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
if (kn)
return 0;
@@ -1559,7 +1559,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
if (!kn->parent)
return -EINVAL;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
error = -ENOENT;
if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
@@ -1613,7 +1613,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
error = 0;
out:
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
return error;
}
@@ -1688,7 +1688,7 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
if (!dir_emit_dots(file, ctx))
return 0;
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
if (kernfs_ns_enabled(parent))
ns = kernfs_info(dentry->d_sb)->ns;
@@ -1705,12 +1705,12 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
file->private_data = pos;
kernfs_get(pos);
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
if (!dir_emit(ctx, name, len, ino, type))
return 0;
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
}
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
file->private_data = NULL;
ctx->pos = INT_MAX;
return 0;
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index c75719312147..60e2a86c535e 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -860,7 +860,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
spin_unlock_irq(&kernfs_notify_lock);
/* kick fsnotify */
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
list_for_each_entry(info, &kernfs_root(kn)->supers, node) {
struct kernfs_node *parent;
@@ -898,7 +898,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
iput(inode);
}
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
kernfs_put(kn);
goto repeat;
}
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 26f2aa3586f9..dad749f39518 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -100,9 +100,9 @@ int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
{
int ret;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
ret = __kernfs_setattr(kn, iattr);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
return ret;
}
@@ -116,7 +116,7 @@ int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
if (!kn)
return -EINVAL;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
error = setattr_prepare(&init_user_ns, dentry, iattr);
if (error)
goto out;
@@ -129,7 +129,7 @@ int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
setattr_copy(&init_user_ns, inode, iattr);
out:
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
return error;
}
@@ -185,9 +185,9 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns,
struct inode *inode = d_inode(path->dentry);
struct kernfs_node *kn = inode->i_private;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
kernfs_refresh_inode(kn, inode);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
generic_fillattr(&init_user_ns, inode, stat);
return 0;
@@ -278,9 +278,9 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns,
kn = inode->i_private;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
kernfs_refresh_inode(kn, inode);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
return generic_permission(&init_user_ns, inode, mask);
}
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index c2ae58f3b202..f9cc912c31e1 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -13,6 +13,7 @@
#include <linux/lockdep.h>
#include <linux/fs.h>
#include <linux/mutex.h>
+#include <linux/rwsem.h>
#include <linux/xattr.h>
#include <linux/kernfs.h>
@@ -69,7 +70,7 @@ struct kernfs_super_info {
*/
const void *ns;
- /* anchored at kernfs_root->supers, protected by kernfs_mutex */
+ /* anchored at kernfs_root->supers, protected by kernfs_rwsem */
struct list_head node;
};
#define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info))
@@ -121,7 +122,7 @@ int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
/*
* dir.c
*/
-extern struct mutex kernfs_mutex;
+extern struct rw_semaphore kernfs_rwsem;
extern const struct dentry_operations kernfs_dops;
extern const struct file_operations kernfs_dir_fops;
extern const struct inode_operations kernfs_dir_iops;
diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c
index 9dc7e7a64e10..baa4155ba2ed 100644
--- a/fs/kernfs/mount.c
+++ b/fs/kernfs/mount.c
@@ -255,9 +255,9 @@ static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *k
sb->s_shrink.seeks = 0;
/* get root inode, initialize and unlock it */
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
inode = kernfs_get_inode(sb, info->root->kn);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
if (!inode) {
pr_debug("kernfs: could not get root inode\n");
return -ENOMEM;
@@ -344,9 +344,9 @@ int kernfs_get_tree(struct fs_context *fc)
}
sb->s_flags |= SB_ACTIVE;
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
list_add(&info->node, &info->root->supers);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
}
fc->root = dget(sb->s_root);
@@ -372,9 +372,9 @@ void kernfs_kill_sb(struct super_block *sb)
{
struct kernfs_super_info *info = kernfs_info(sb);
- mutex_lock(&kernfs_mutex);
+ down_write(&kernfs_rwsem);
list_del(&info->node);
- mutex_unlock(&kernfs_mutex);
+ up_write(&kernfs_rwsem);
/*
* Remove the superblock from fs_supers/s_instances
diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c
index 5432883d819f..c8f8e41b8411 100644
--- a/fs/kernfs/symlink.c
+++ b/fs/kernfs/symlink.c
@@ -116,9 +116,9 @@ static int kernfs_getlink(struct inode *inode, char *path)
struct kernfs_node *target = kn->symlink.target_kn;
int error;
- mutex_lock(&kernfs_mutex);
+ down_read(&kernfs_rwsem);
error = kernfs_get_target_path(parent, target, path);
- mutex_unlock(&kernfs_mutex);
+ up_read(&kernfs_rwsem);
return error;
}
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index d68b4ad09573..1093abf7c28c 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -193,7 +193,7 @@ struct kernfs_root {
u32 id_highbits;
struct kernfs_syscall_ops *syscall_ops;
- /* list of kernfs_super_info of this root, protected by kernfs_mutex */
+ /* list of kernfs_super_info of this root, protected by kernfs_rwsem */
struct list_head supers;
wait_queue_head_t deactivate_waitq;
]
|
| 18/2 |
2026/11/18 07:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 47b5c64d0ab5e7136db2b78c6ec710e0d8a5a36b
Author: Ian Kent <raven@themaw.net>
Date: Fri Jul 16 17:28:34 2021 +0800
kernfs: use i_lock to protect concurrent inode updates
The inode operations .permission() and .getattr() use the kernfs node
write lock but all that's needed is the read lock to protect against
partial updates of these kernfs node fields which are all done under
the write lock.
And .permission() is called frequently during path walks and can cause
quite a bit of contention between kernfs node operations and path
walks when the number of concurrent walks is high.
To change kernfs_iop_getattr() and kernfs_iop_permission() to take
the rw sem read lock instead of the write lock an additional lock is
needed to protect against multiple processes concurrently updating
the inode attributes and link count in kernfs_refresh_inode().
The inode i_lock seems like the sensible thing to use to protect these
inode attribute updates so use it in kernfs_refresh_inode().
The last hunk in the patch, applied to kernfs_fill_super(), is possibly
not needed but taking the lock was present originally. I prefer to
continue to take it to protect against a partial update of the source
kernfs fields during the call to kernfs_refresh_inode() made by
kernfs_get_inode().
Reviewed-by: Miklos Szeredi <mszeredi@redhat.com>
Signed-off-by: Ian Kent <raven@themaw.net>
Link: https://lore.kernel.org/r/162642771474.63632.16295959115893904470.stgit@web.messagingengine.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index dad749f39518..c0eae1725435 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -185,11 +185,13 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns,
struct inode *inode = d_inode(path->dentry);
struct kernfs_node *kn = inode->i_private;
- down_write(&kernfs_rwsem);
+ down_read(&kernfs_rwsem);
+ spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
- up_write(&kernfs_rwsem);
-
generic_fillattr(&init_user_ns, inode, stat);
+ spin_unlock(&inode->i_lock);
+ up_read(&kernfs_rwsem);
+
return 0;
}
@@ -272,17 +274,21 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns,
struct inode *inode, int mask)
{
struct kernfs_node *kn;
+ int ret;
if (mask & MAY_NOT_BLOCK)
return -ECHILD;
kn = inode->i_private;
- down_write(&kernfs_rwsem);
+ down_read(&kernfs_rwsem);
+ spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
- up_write(&kernfs_rwsem);
+ ret = generic_permission(&init_user_ns, inode, mask);
+ spin_unlock(&inode->i_lock);
+ up_read(&kernfs_rwsem);
- return generic_permission(&init_user_ns, inode, mask);
+ return ret;
}
int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c
index baa4155ba2ed..f2f909d09f52 100644
--- a/fs/kernfs/mount.c
+++ b/fs/kernfs/mount.c
@@ -255,9 +255,9 @@ static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *k
sb->s_shrink.seeks = 0;
/* get root inode, initialize and unlock it */
- down_write(&kernfs_rwsem);
+ down_read(&kernfs_rwsem);
inode = kernfs_get_inode(sb, info->root->kn);
- up_write(&kernfs_rwsem);
+ up_read(&kernfs_rwsem);
if (!inode) {
pr_debug("kernfs: could not get root inode\n");
return -ENOMEM;
]
|
| 19/2 |
2026/11/18 07:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit aa1d058d48f292aa138e33ad12b7b4d18b5407cd
Author: Ian Kent <raven@themaw.net>
Date: Tue Oct 18 10:32:42 2022 +0800
kernfs: dont take i_lock on inode attr read
The kernfs write lock is held when the kernfs node inode attributes
are updated. Therefore, when either kernfs_iop_getattr() or
kernfs_iop_permission() are called the kernfs node inode attributes
won't change.
Consequently concurrent kernfs_refresh_inode() calls always copy the
same values from the kernfs node.
So there's no need to take the inode i_lock to get consistent values
for generic_fillattr() and generic_permission(), the kernfs read lock
is sufficient.
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Ian Kent <raven@themaw.net>
Link: https://lore.kernel.org/r/166606036215.13363.1288735296954908554.stgit@donald.themaw.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 3d783d80f5da..74f3453f4639 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -190,10 +190,8 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns,
struct kernfs_root *root = kernfs_root(kn);
down_read(&root->kernfs_rwsem);
- spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&init_user_ns, inode, stat);
- spin_unlock(&inode->i_lock);
up_read(&root->kernfs_rwsem);
return 0;
@@ -288,10 +286,8 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns,
root = kernfs_root(kn);
down_read(&root->kernfs_rwsem);
- spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
ret = generic_permission(&init_user_ns, inode, mask);
- spin_unlock(&inode->i_lock);
up_read(&root->kernfs_rwsem);
return ret;
]
|
| 20/2 |
2026/11/18 07:23 |
tool |
git-show |
0m
Args:
Results: map[Output:commit d1f4e9026007d50e6b28cf8f57a38f03fc3b0ce6
Author: Christian Brauner <brauner@kernel.org>
Date: Sun Jun 22 23:38:51 2025 -0700
kernfs: remove iattr_mutex
All allocations of struct kernfs_iattrs are serialized through a global
mutex. Simply do a racy allocation and let the first one win. I bet most
callers are under inode->i_rwsem anyway and it wouldn't be needed but
let's not require that.
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/20250623063854.1896364-2-song@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index b83054da68b3..3c293a5a21b1 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -24,45 +24,46 @@ static const struct inode_operations kernfs_iops = {
.listxattr = kernfs_iop_listxattr,
};
-static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
+static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, bool alloc)
{
- static DEFINE_MUTEX(iattr_mutex);
- struct kernfs_iattrs *ret;
+ struct kernfs_iattrs *ret __free(kfree) = NULL;
+ struct kernfs_iattrs *attr;
- mutex_lock(&iattr_mutex);
+ attr = READ_ONCE(kn->iattr);
+ if (attr || !alloc)
+ return attr;
- if (kn->iattr || !alloc)
- goto out_unlock;
-
- kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
- if (!kn->iattr)
- goto out_unlock;
+ ret = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
+ if (!ret)
+ return NULL;
/* assign default attributes */
- kn->iattr->ia_uid = GLOBAL_ROOT_UID;
- kn->iattr->ia_gid = GLOBAL_ROOT_GID;
-
- ktime_get_real_ts64(&kn->iattr->ia_atime);
- kn->iattr->ia_mtime = kn->iattr->ia_atime;
- kn->iattr->ia_ctime = kn->iattr->ia_atime;
-
- simple_xattrs_init(&kn->iattr->xattrs);
- atomic_set(&kn->iattr->nr_user_xattrs, 0);
- atomic_set(&kn->iattr->user_xattr_size, 0);
-out_unlock:
- ret = kn->iattr;
- mutex_unlock(&iattr_mutex);
- return ret;
+ ret->ia_uid = GLOBAL_ROOT_UID;
+ ret->ia_gid = GLOBAL_ROOT_GID;
+
+ ktime_get_real_ts64(&ret->ia_atime);
+ ret->ia_mtime = ret->ia_atime;
+ ret->ia_ctime = ret->ia_atime;
+
+ simple_xattrs_init(&ret->xattrs);
+ atomic_set(&ret->nr_user_xattrs, 0);
+ atomic_set(&ret->user_xattr_size, 0);
+
+ /* If someone raced us, recognize it. */
+ if (!try_cmpxchg(&kn->iattr, &attr, ret))
+ return READ_ONCE(kn->iattr);
+
+ return no_free_ptr(ret);
}
static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
{
- return __kernfs_iattrs(kn, 1);
+ return __kernfs_iattrs(kn, true);
}
static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
{
- return __kernfs_iattrs(kn, 0);
+ return __kernfs_iattrs(kn, false);
}
int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
@@ -141,9 +142,9 @@ ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
struct kernfs_node *kn = kernfs_dentry_node(dentry);
struct kernfs_iattrs *attrs;
- attrs = kernfs_iattrs(kn);
+ attrs = kernfs_iattrs_noalloc(kn);
if (!attrs)
- return -ENOMEM;
+ return -ENODATA;
return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
}
@@ -166,9 +167,10 @@ static inline void set_inode_attr(struct inode *inode,
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
- struct kernfs_iattrs *attrs = kn->iattr;
+ struct kernfs_iattrs *attrs;
inode->i_mode = kn->mode;
+ attrs = kernfs_iattrs_noalloc(kn);
if (attrs)
/*
* kernfs_node has non-default attributes get them from
@@ -306,7 +308,9 @@ int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
const void *value, size_t size, int flags)
{
struct simple_xattr *old_xattr;
- struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
+ struct kernfs_iattrs *attrs;
+
+ attrs = kernfs_iattrs(kn);
if (!attrs)
return -ENOMEM;
@@ -345,8 +349,9 @@ static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
struct simple_xattrs *xattrs,
const void *value, size_t size, int flags)
{
- atomic_t *sz = &kn->iattr->user_xattr_size;
- atomic_t *nr = &kn->iattr->nr_user_xattrs;
+ struct kernfs_iattrs *attr = kernfs_iattrs_noalloc(kn);
+ atomic_t *sz = &attr->user_xattr_size;
+ atomic_t *nr = &attr->nr_user_xattrs;
struct simple_xattr *old_xattr;
int ret;
@@ -384,8 +389,9 @@ static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
struct simple_xattrs *xattrs,
const void *value, size_t size, int flags)
{
- atomic_t *sz = &kn->iattr->user_xattr_size;
- atomic_t *nr = &kn->iattr->nr_user_xattrs;
+ struct kernfs_iattrs *attr = kernfs_iattrs_noalloc(kn);
+ atomic_t *sz = &attr->user_xattr_size;
+ atomic_t *nr = &attr->nr_user_xattrs;
struct simple_xattr *old_xattr;
old_xattr = simple_xattr_set(xattrs, full_name, value, size, flags);
]
|
| 21/2 |
2026/11/18 07:23 |
tool |
git-log |
0m
Args: map[Count:40 SourcePath:fs/kernfs/inode.c SymbolName:kernfs_refresh_inode]
Results: map[Output:507d8ce13f5b kernfs: Don't set_nlink for directories being removed
fb7b30cb0e31 kernfs: remove iattr_mutex
0ac6075a32fc kernfs: use simple_xattrs for security attributes
05895219627c kernfs: clean up struct kernfs_iattrs
c637b8acbe07 kernfs: s/sysfs/kernfs/ in internal functions and whatever is left
df23fc39bce0 kernfs: s/sysfs/kernfs/ in constants
c525aaddc366 kernfs: s/sysfs/kernfs/ in various data structures
adc5e8b58f48 kernfs: drop s_ prefix from kernfs_node members
324a56e16e44 kernfs: s/sysfs_dirent/kernfs_node/ and rename its friends accordingly
9a8049affd55 kernfs: update sysfs_init_inode_attrs()
ffed24e22845 sysfs, kernfs: move inode code to fs/kernfs/inode.c
]
|
| 22/2 |
2026/11/18 07:23 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 26440
output: 140
thoughts: 1049
Thoughts: **Analyzing a Kernel Data Race: A Deep Dive into `dnotify_flush` and `kernfs_iop_getattr`**
Hold everything! I need to stop and think very carefully about this KCSAN report that's just popped up. It's indicating a data race, and I need to dissect it meticulously. Let's re-read this KCSAN report *very carefully*.
The report highlights a data race involving two distinct operations: `dnotify_flush` and `kernfs_iop_getattr`.
Specifically, I'm seeing a *read* to memory address `0xffff88810632faa0` of 2 bytes by task 3286 on CPU 1. This read is occurring within the call stack originating from `dnotify_flush`, which is called via `filp_flush` and ultimately `__do_sys_close`.
Simultaneously, there's a *write* to the *exact same memory address* (`0xffff88810632faa0` of 2 bytes) by task 3283 on CPU 0. This write is happening within `kernfs_iop_getattr`, which is called from `kernfs_refresh_inode` and ultimately `__do_sys_newfstat`.
Now, let's pause. Something significant jumps out immediately. Look at the context of these two tasks.
Task 3286, the one performing the read during `dnotify_flush`, is part of a `close()` operation. Following the stack trace, we see it goes through `filp_close_sync`, `filp_flush`, and then `dnotify_flush`. The critical point in `dnotify_flush` is this check: `if (!S_ISDIR(inode->i_mode))`. This means it's only interested in directories.
The crucial question arises: **Does `dnotify_flush` hold *any* locks when it performs this check and potentially accesses `inode->i_mode`?** My immediate assessment is **NO**. It appears to access `inode` directly via `file_inode(filp)` without acquiring `inode->i_lock`, `inode->i_rwsem`, any of the `kernfs_mutex`, `kernfs_rwsem`, or `kernfs_iattr_rwsem`.
This is a major red flag. Why would `dnotify_flush` be racing with `kernfs_iop_getattr`? This race condition might be a new phenomenon or it might have been lurking for a while. To understand this, I need to perform a thorough investigation. I will check *all* recent commits affecting `fs/kernfs/`, `fs/notify/`, `fs/open.c`, `fs/stat.c`, `include/linux/fs.h`, and any other related files. I also need to ascertain the current state of the repository by checking the `HEAD` commit and examining the `git log` for changes in these critical files. This will help me pinpoint any recent modifications that might have introduced or exposed this specific data race.
|
| 23/2 |
2026/11/18 07:24 |
tool |
git-show |
0m
Args: map[Commit:HEAD Stat:true]
Results: map[Output:commit 63fdb511c27afcebce797e3db580b3534ddfa639
Merge: 666539eecdf4 25f5c92e1ac0
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date: Tue Nov 17 11:51:40 2026 -0800
Merge tag 'mm-core-7.4-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/mm/core
Pull core memory management fixes from David Hildenbrand:
"The main changes are:
- Prevent folio refcount overflows with pagecache folios, mostly
relevant for large folios today. The networking changes for this
will go through the net tree.
- Fix out-of-bounds page table accesses with folded PMDs in 3
scenarios.
- Fix write-sealing of memfd to detect GUP pins with concurrent
(un)mapping more reliably, preventing writes through GUP pins after
write-sealing.
- Fix lost wakeups when splitting large folios.
- Fix exposing uninitialized data through MADV_COLLAPSE to user
space.
The refcount changes also include a related iommufd fix for properly
cleaning up when pinning memfd folios fails, which is now easier to
trigger"
* tag 'mm-core-7.4-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/mm/core:
mm/gup: limit pagecache folio refcount when grabbing references
iommufd: fix cleanup when adding memfd folio pins fails
mm/mmu_gather: flush pending references early before the folio limit
mm: limit pagecache folio refcount when splicing into pipes
mm: limit pagecache folio refcount when mapping pages
mm/huge_memory: prevent losing PG_waiters in __split_folio_to_order()
mm/rmap: fix PTE table overruns during batched reference checks
mm/rmap: fix PTE table overruns during batched unmap
mm/userfaultfd: fix PTE table overruns in UFFDIO_MOVE
mm/memfd: handle GUP pins more reliably when write-sealing
mm/khugepaged: don't map folios that are not uptodate in MADV_COLLAPSE
drivers/iommu/iommufd/pages.c | 15 ++++++--
fs/fuse/dev.c | 4 +-
include/asm-generic/tlb.h | 4 +-
include/linux/mm.h | 89 ++++++++++++++++++++++++++++++++++++-------
include/linux/pgtable.h | 24 ++++++++++++
mm/filemap.c | 28 +++++++++-----
mm/folio.c | 26 +++++++++++++
mm/gup.c | 32 ++++++++++------
mm/huge_memory.c | 64 ++++++++++++++++++++++++++++---
mm/internal.h | 6 ++-
mm/khugepaged.c | 6 +++
mm/memfd.c | 7 ++++
mm/memory.c | 48 +++++++++++++++++------
mm/migrate.c | 11 ++++++
mm/mmu_gather.c | 10 +++--
mm/page_alloc.c | 17 ++++++++-
mm/rmap.c | 4 +-
mm/shmem.c | 7 +++-
mm/userfaultfd.c | 8 +++-
19 files changed, 336 insertions(+), 74 deletions(-)
]
|
| 24/2 |
2026/11/18 07:24 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:fs/open.c]
Results: map[Output:323899b4ed23 ovl: inherit FMODE_NONOTIFY when opening directory realfiles
2b256ebe8287 fs: add filp_close_sync()
025756ee7b47 fs: port mnt_idmap() and file_mnt_idmap() to const mnt_idmap
e2f7f94b5a25 fs: port vfs_*() helpers to const mnt_idmap
161ce1e692d0 fs: avoid spurious dentry ref/unref cycle on open
b1221afa31cc fs: support FD_FAILFS_ROOT in fchroot()
20370a5f5d9b fs: add fchroot()
cdf930a00949 fs: support FD_FAILFS_ROOT in fchdir()
044925f9b565 mm: fs: remove filemap_nr_thps*() functions and their users
8b82cacad92e openat2: new OPENAT2_REGULAR flag support
31cf44efa6df vfs: add O_EMPTYPATH to openat(2)/openat2(2)
e8767a3134ca fs: remove do_sys_truncate
0924f6b80d4a fs: pass on FTRUNCATE_* flags to do_truncate
e43dce8a0bc0 fs: fix archiecture-specific compat_ftruncate64
57483461e19c chroot(2): switch to CLASS(filename)
33b54bc79989 do_f{chmod,chown,access}at(): use CLASS(filename_uflags)
97ed55d2f503 do_sys_truncate(): switch to CLASS(filename)
4e9654c2bb46 chdir(2): unspaghettify a bit...
f770e4c1a488 do_fchownat(): unspaghettify a bit...
47b3b9bf93ec simplify the callers of file_open_name()
]
|
| 25/2 |
2026/11/18 07:24 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:fs/stat.c]
Results: map[Output:925d238324c7 statx: don't leak STATX_ATTR_CHANGE_MONOTONIC in stx_attributes_mask
f9e450420da2 fsverity: hoist statx reporting of fs-verity flag
025756ee7b47 fs: port mnt_idmap() and file_mnt_idmap() to const mnt_idmap
c3150ded7bc0 fs: port ->getattr() to pass const mnt_idmap
c610d2d07879 fs: annotate inode timestamp accessors
a4503461fb6b statx: switch to CLASS(filename_maybe_null)
d4ffeabea744 do_readlinkat(): switch to CLASS(filename_flags)
1ee5220eb364 do_readlinkat(): import pathname only once
dd22857510e1 constify path argument of vfs_statx_path()
5d894321c49e fs: add atomic write unit max opt to statx
777d0961ff95 fs: move the bdex_statx call to vfs_getattr_nosec
eaec2cd1670d fs: sort out cosmetic differences between stat funcs and add predicts
0fac3ed473dd fs/stat.c: avoid harmless garbage value problem in vfs_statx_path()
7ed6cbe0f8ca fs: add STATX_DIO_READ_ALIGN
95f567f81e43 fs: Simplify getattr interface function checking AT_GETATTR_NOSEC flag
0dd4fb733132 fs/stat.c: switch to CLASS(fd_raw)
88a20626d8ed kill getname_statx_lookup_flags()
e896474fe485 getname_maybe_null() - the third variant of pathname copy-in
c86e3c47187a fs: tracepoints around multigrain timestamp events
4e40eff0b573 fs: add infrastructure for multigrain timestamps
]
|
| 26/2 |
2026/11/18 07:24 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:fs/notify/dnotify/]
Results: map[Output:fdfe0133473a fix a leak in fcntl_dirnotify()
1751f872cc97 treewide: const qualify ctl_tables where applicable
8fd3395ec905 get rid of ...lookup...fdget_rcu() family
cad3f4a22cfa inotify: Fix possible deadlock in fsnotify_destroy_mark
1934b212615d file: reclaim 24 bytes from f_owner
230d97d39ee2 fsnotify: create a wrapper fsnotify_find_inode_mark()
9d5b94753566 fs: Remove the now superfluous sentinel elements from ctl_table array
0ede61d8589c file: convert to SLAB_TYPESAFE_BY_RCU
68279f9c9f59 treewide: mark stuff as __ro_after_init
f4ae4081e5a8 dnotify: Pass argument of fcntl_dirnotify as int
aabb45fdcb31 dnotify: use fsnotify group lock helpers
867a448d587e fsnotify: pass flags argument to fsnotify_alloc_group()
49a4de75719b dnotify: move dnotify sysctl to dnotify.c
e54183fa7047 fsnotify: generate FS_RENAME event with rich information
460b4f812a9d file: Rename fcheck lookup_fd_rcu
950cc0d2bef0 fsnotify: generalize handle_inode_event()
b9a1b9772509 fsnotify: create method handle_inode_event() in fsnotify_operations
62cb0af4cea8 dnotify: report both events on parent and child with single callback
b54cecf5e229 fsnotify: pass dir argument to handle_event() callback
ac5656d8a4cd fanotify, inotify, dnotify, security: add security hook for fs notifications
]
|
| 27/2 |
2026/11/18 07:24 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:fs/kernfs/]
Results: map[Output:ef179363b043 kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()
09452e5114bd kernfs: free the old name outside kernfs_rwsem
8b64f32fee95 kernfs: allocate the new name outside kernfs_rwsem
4424f06120fd kernfs: activate a new node without dropping kernfs_rwsem
2d2a2d7aa987 super: make iterate_supers_type() deletion-safe
e2f0d6d4569d fs: port ->setattr() to pass const mnt_idmap
c3150ded7bc0 fs: port ->getattr() to pass const mnt_idmap
566707035a6e fs: port ->mkdir() to pass const mnt_idmap
8ee61322bb53 fs: port ->rename() to pass const mnt_idmap
16d90b57d843 fs: port xattr to const mnt_idmap
41839e7bd92a fs: port ->permission() to pass const mnt_idmap
341c65579de5 kernfs: don't hold kernfs_rwsem across dir_emit()
45126ed80bb1 kernfs: resume readdir on the full entry key
5ebcb578adee kernfs: don't repeat or skip an entry when readdir resumes
23985e2b1f31 kernfs: don't hold kernfs_rwsem across notification delivery
5ad03af18434 kernfs: allocate the open node outside the open file mutex
81bdc85b4c08 kernfs: fix up the unlocked attribute reads on the creation paths
dc1a0cc95737 kernfs: don't lose IN_DELETE_SELF when decoding a file handle
6034974e3aec kernfs: take kernfs_rename_lock for same-parent renames too
7b15d6cf25e6 kernfs: preserve security xattrs without allocating iattrs
]
|
| 28/2 |
2026/11/18 07:24 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 30609
output: 145
thoughts: 129
Thoughts: **Analyzing Recent Kernel Commits for Potential Race Conditions**
My immediate reaction upon seeing recent commits in `fs/kernfs/` and `fs/` was one of caution. My expertise immediately flagged a potential for race conditions, especially when dealing with filesystem operations. I decided to prioritize investigating specific commits that caught my eye: `81bdc85b4c08`, `ef179363b043`, and particularly `c610d2d07879`, which is explicitly titled "fs: annotate inode timestamp accessors." The very nature of inode timestamp accessors suggests a high likelihood of concurrent modification and therefore potential data races.
To further refine my investigation, I'm employing targeted `git log` queries. I'm specifically looking for commits that mention "KCSAN" (Kernel Concurrency Sanitizer) or "data-race" as these are direct indicators of issues related to concurrency bugs. Additionally, given the involvement of inode accessors, I'm also filtering for messages containing "i_mode," as modifications to inode metadata like `i_mode` are common sources of contention. I plan to execute these queries immediately to get a clearer picture of the changes and to identify any areas that might require deeper scrutiny for race conditions.
|
| 29/2 |
2026/11/18 07:25 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 81bdc85b4c086502f4446a58eb5d25f29bb5e9f7
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 5 12:16:13 2026 -0700
kernfs: fix up the unlocked attribute reads on the creation paths
Two creation paths read a live node's attributes without holding
kernfs_iattr_rwsem, which kernfs_iop_setattr() takes for writing. They
need different fixes.
kernfs_create_link() copies the target's ia_uid and then its ia_gid into
the new link. A chown of the target between the two reads leaves the
link with the old uid and the new gid, an owner the target never had.
Read both under the rwsem.
kernfs_new_node() reads the parent's mode and ia_gid for S_ISGID
inheritance. Either value is fine there: the node does not exist yet,
so nothing orders a racing chmod or chown against the creation. Taking
the rwsem would only pick between two answers that are both right. Mark
the reads with READ_ONCE() instead.
The pointer that leads to them is already fine: __kernfs_iattrs()
publishes kernfs_node::iattr with try_cmpxchg(), and both sides read it
with READ_ONCE(), like the rest of fs/kernfs.
The Fixes tag is for the symlink half. kernfs_create_link() has read
the pair unlocked since it started copying the target's owner; only the
name of the lock its writer takes has changed. The READ_ONCE() markings
are not a fix.
Fixes: 488dee96bb62 ("kernfs: allow creating kernfs objects with arbitrary uid/gid")
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260905191613.3143937-5-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 214c97130a8a..07abf59f0264 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -736,13 +736,19 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
{
struct kernfs_node *kn;
- if (parent->mode & S_ISGID) {
+ /*
+ * The mode and the gid below are read unlocked on purpose: they feed
+ * a node that does not exist yet, so nothing orders a racing chmod or
+ * chown against this creation.
+ */
+ if (READ_ONCE(parent->mode) & S_ISGID) {
/* this code block imitates inode_init_owner() for
* kernfs
*/
+ struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
- if (parent->iattr)
- gid = parent->iattr->ia_gid;
+ if (attrs)
+ gid = READ_ONCE(attrs->ia_gid);
if (flags & KERNFS_DIR)
mode |= S_ISGID;
diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c
index 90e2b3221b83..3e53105d3abf 100644
--- a/fs/kernfs/symlink.c
+++ b/fs/kernfs/symlink.c
@@ -31,9 +31,20 @@ struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
kuid_t uid = GLOBAL_ROOT_UID;
kgid_t gid = GLOBAL_ROOT_GID;
- if (target->iattr) {
- uid = target->iattr->ia_uid;
- gid = target->iattr->ia_gid;
+ /*
+ * A symlink takes its owner from its target, so both fields have to
+ * come from the same moment: read them under kernfs_iattr_rwsem, or
+ * a chown of the target racing this could leave the link with the
+ * old uid and the new gid. The section ends before kernfs_add_one()
+ * takes kernfs_rwsem.
+ */
+ scoped_guard(rwsem_read, &kernfs_root(target)->kernfs_iattr_rwsem) {
+ struct kernfs_iattrs *attrs = READ_ONCE(target->iattr);
+
+ if (attrs) {
+ uid = attrs->ia_uid;
+ gid = attrs->ia_gid;
+ }
}
kn = kernfs_new_node(parent, name, S_IFLNK|0777, uid, gid, KERNFS_LINK);
]
|
| 30/2 |
2026/11/18 07:25 |
tool |
git-show |
0m
Args:
Results: map[Output:commit ef179363b04306dbc645c53207b9ad3516ebc75f
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Tue Oct 27 07:32:47 2026 -0700
kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()
kernfs_new_node() reads the parent's gid with READ_ONCE(attrs->ia_gid).
That gid is a kgid_t, which is a struct.
On SMP alpha, READ_ONCE() ends by casting the value it loaded back to
the type of its argument. Here that is a cast from a struct to the same
struct. GCC accepts that as an extension, but ISO C does not allow a
cast to or from a struct type, so sparse warns:
fs/kernfs/dir.c:751:31: sparse: sparse: cast to non-scalar
fs/kernfs/dir.c:751:31: sparse: sparse: cast from non-scalar
Read the gid_t inside the kgid_t instead, and wrap it back up with
KGIDT_INIT(). This is still a single marked load of the same four bytes.
There is no functional change. With GCC 12 for alpha (the config from
the report) and GCC 14 for x86-64 (with and without KCSAN),
fs/kernfs/dir.o has the same object code before and after this change.
Fixes: 81bdc85b4c08 ("kernfs: fix up the unlocked attribute reads on the creation paths")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202610261608.vItiXCGW-lkp@intel.com/
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://patch.msgid.link/20261027143247.3537239-1-shakeel.butt@linux.dev
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 324d61a00545..800af10d9511 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -750,7 +750,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
if (attrs)
- gid = READ_ONCE(attrs->ia_gid);
+ gid = KGIDT_INIT(READ_ONCE(attrs->ia_gid.val));
if (flags & KERNFS_DIR)
mode |= S_ISGID;
]
|
| 31/2 |
2026/11/18 07:25 |
tool |
git-show |
0m
Args:
Results: map[Output:commit c610d2d0787961cdd6fc1de69d9be1ff3687e1a6
Author: Yu Peng <pengyu@kylinos.cn>
Date: Wed Jul 8 16:02:32 2026 +0800
fs: annotate inode timestamp accessors
syzbot reported a KCSAN race between fill_mg_cmtime() and
inode_set_ctime_to_ts() on inode->i_ctime_{sec,nsec}.
stat/getattr can sample inode timestamps while update paths store new
values concurrently, so KCSAN can report benign races on these fields.
Annotate the timestamp accessors with READ_ONCE()/WRITE_ONCE(), and use
the ctime accessor for the remaining ctime loads. This avoids the KCSAN
reports without changing timestamp semantics.
Fixes: 4e40eff0b573 ("fs: add infrastructure for multigrain timestamps")
Reported-by: syzbot+8b3bd9f8a06658479d4a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8b3bd9f8a06658479d4a
Signed-off-by: Yu Peng <pengyu@kylinos.cn>
Link: https://patch.msgid.link/20260708080232.2564807-1-pengyu@kylinos.cn
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/inode.c b/fs/inode.c
index a31aa7cb47f6..238fcd1cad6e 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -2830,8 +2830,8 @@ struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 t
{
trace_inode_set_ctime_to_ts(inode, &ts);
set_normalized_timespec64(&ts, ts.tv_sec, ts.tv_nsec);
- inode->i_ctime_sec = ts.tv_sec;
- inode->i_ctime_nsec = ts.tv_nsec;
+ WRITE_ONCE(inode->i_ctime_sec, ts.tv_sec);
+ WRITE_ONCE(inode->i_ctime_nsec, ts.tv_nsec);
return ts;
}
EXPORT_SYMBOL(inode_set_ctime_to_ts);
@@ -2905,7 +2905,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
*/
cns = smp_load_acquire(&inode->i_ctime_nsec);
if (cns & I_CTIME_QUERIED) {
- struct timespec64 ctime = { .tv_sec = inode->i_ctime_sec,
+ struct timespec64 ctime = { .tv_sec = inode_get_ctime_sec(inode),
.tv_nsec = cns & ~I_CTIME_QUERIED };
if (timespec64_compare(&now, &ctime) <= 0) {
@@ -2917,7 +2917,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
mgtime_counter_inc(mg_ctime_updates);
/* No need to cmpxchg if it's exactly the same */
- if (cns == now.tv_nsec && inode->i_ctime_sec == now.tv_sec) {
+ if (cns == now.tv_nsec && inode_get_ctime_sec(inode) == now.tv_sec) {
trace_ctime_xchg_skip(inode, &now);
goto out;
}
@@ -2926,7 +2926,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
/* Try to swap the nsec value into place. */
if (try_cmpxchg(&inode->i_ctime_nsec, &cur, now.tv_nsec)) {
/* If swap occurred, then we're (mostly) done */
- inode->i_ctime_sec = now.tv_sec;
+ WRITE_ONCE(inode->i_ctime_sec, now.tv_sec);
trace_ctime_ns_xchg(inode, cns, now.tv_nsec, cur);
mgtime_counter_inc(mg_ctime_swaps);
} else {
@@ -2941,7 +2941,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
goto retry;
}
/* Otherwise, keep the existing ctime */
- now.tv_sec = inode->i_ctime_sec;
+ now.tv_sec = inode_get_ctime_sec(inode);
now.tv_nsec = cur & ~I_CTIME_QUERIED;
}
out:
@@ -2974,7 +2974,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
/* pairs with try_cmpxchg below */
cur = smp_load_acquire(&inode->i_ctime_nsec);
cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED;
- cur_ts.tv_sec = inode->i_ctime_sec;
+ cur_ts.tv_sec = inode_get_ctime_sec(inode);
/* If the update is older than the existing value, skip it. */
if (timespec64_compare(&update, &cur_ts) <= 0)
@@ -3000,7 +3000,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
retry:
old = cur;
if (try_cmpxchg(&inode->i_ctime_nsec, &cur, update.tv_nsec)) {
- inode->i_ctime_sec = update.tv_sec;
+ WRITE_ONCE(inode->i_ctime_sec, update.tv_sec);
mgtime_counter_inc(mg_ctime_swaps);
return update;
}
@@ -3016,7 +3016,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
goto retry;
/* Otherwise, it was a new timestamp. */
- cur_ts.tv_sec = inode->i_ctime_sec;
+ cur_ts.tv_sec = inode_get_ctime_sec(inode);
cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED;
return cur_ts;
}
diff --git a/fs/stat.c b/fs/stat.c
index 89909746bed1..c461c3054234 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -53,7 +53,7 @@ void fill_mg_cmtime(struct kstat *stat, u32 request_mask, struct inode *inode)
}
stat->mtime = inode_get_mtime(inode);
- stat->ctime.tv_sec = inode->i_ctime_sec;
+ stat->ctime.tv_sec = inode_get_ctime_sec(inode);
stat->ctime.tv_nsec = (u32)atomic_read(pcn);
if (!(stat->ctime.tv_nsec & I_CTIME_QUERIED))
stat->ctime.tv_nsec = ((u32)atomic_fetch_or(I_CTIME_QUERIED, pcn));
diff --git a/include/linux/fs.h b/include/linux/fs.h
index d10897b3a1e3..e5b97e324db1 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1598,12 +1598,12 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode,
static inline time64_t inode_get_atime_sec(const struct inode *inode)
{
- return inode->i_atime_sec;
+ return READ_ONCE(inode->i_atime_sec);
}
static inline long inode_get_atime_nsec(const struct inode *inode)
{
- return inode->i_atime_nsec;
+ return READ_ONCE(inode->i_atime_nsec);
}
static inline struct timespec64 inode_get_atime(const struct inode *inode)
@@ -1617,8 +1617,8 @@ static inline struct timespec64 inode_get_atime(const struct inode *inode)
static inline struct timespec64 inode_set_atime_to_ts(struct inode *inode,
struct timespec64 ts)
{
- inode->i_atime_sec = ts.tv_sec;
- inode->i_atime_nsec = ts.tv_nsec;
+ WRITE_ONCE(inode->i_atime_sec, ts.tv_sec);
+ WRITE_ONCE(inode->i_atime_nsec, ts.tv_nsec);
return ts;
}
@@ -1633,12 +1633,12 @@ static inline struct timespec64 inode_set_atime(struct inode *inode,
static inline time64_t inode_get_mtime_sec(const struct inode *inode)
{
- return inode->i_mtime_sec;
+ return READ_ONCE(inode->i_mtime_sec);
}
static inline long inode_get_mtime_nsec(const struct inode *inode)
{
- return inode->i_mtime_nsec;
+ return READ_ONCE(inode->i_mtime_nsec);
}
static inline struct timespec64 inode_get_mtime(const struct inode *inode)
@@ -1651,8 +1651,8 @@ static inline struct timespec64 inode_get_mtime(const struct inode *inode)
static inline struct timespec64 inode_set_mtime_to_ts(struct inode *inode,
struct timespec64 ts)
{
- inode->i_mtime_sec = ts.tv_sec;
- inode->i_mtime_nsec = ts.tv_nsec;
+ WRITE_ONCE(inode->i_mtime_sec, ts.tv_sec);
+ WRITE_ONCE(inode->i_mtime_nsec, ts.tv_nsec);
return ts;
}
@@ -1677,12 +1677,12 @@ static inline struct timespec64 inode_set_mtime(struct inode *inode,
static inline time64_t inode_get_ctime_sec(const struct inode *inode)
{
- return inode->i_ctime_sec;
+ return READ_ONCE(inode->i_ctime_sec);
}
static inline long inode_get_ctime_nsec(const struct inode *inode)
{
- return inode->i_ctime_nsec & ~I_CTIME_QUERIED;
+ return READ_ONCE(inode->i_ctime_nsec) & ~I_CTIME_QUERIED;
}
static inline struct timespec64 inode_get_ctime(const struct inode *inode)
]
|
| 32/2 |
2026/11/18 07:25 |
tool |
git-log |
0m
Args: map[Count:30 MessageRegexps:[i_mode]]
Results: map[Output:0cb7f6d9e4b1 drm/meson: encoder_dsi: load ENCL gamma table while ENCL is disabled
7c972e73641c ata: libata-scsi: set the field pointer when rejecting CDL
0fdb8eef88a8 ata: libata-scsi: pass the whole mode page to the MODE SELECT handlers
a3e52dcf0fba ata: libata-scsi: fix MODE SELECT field pointer for block descriptor length
2a4c61cf94d4 ata: libata-scsi: fix MODE SELECT field pointer for unsupported subpages
a1eb72c477ec ata: libata-scsi: reject MODE SELECT mode pages with a wrong SPF bit
b29c40dc150b ata: libata-scsi: do not accept subpage code FFh in MODE SELECT
6fde0444d07e ata: libata-scsi: fix MODE SELECT handling of data after the mode page
bde0010631a2 xfs: mark i_mode=0 and shut down on attr fork inactivation failure
0237c47ca170 mm/shmem: honour F_SEAL_EXEC when an ACL is set
e227cdac03df namei: don't assert S_ISDIR on a stale rcu-walk inode
89467388e8af ntfs: roll back the ACL xattr commit when the WSL mode update fails
4334dc4092c2 iio: pressure: dps310: fix CFG_REG bit definitions
c8c1795aa810 net: stmmac: selftests: Validate EEE based on the actual LPI timer value
d653818324ed drm/display: hdmi: Add HDMI 2.0 scrambling management helpers
2a12d96dd863 virtio_pci_modern: move avq cleanup from reset to del_vqs
5a5d26f2cfe1 x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash()
3d676e458fe0 scsi: core: Validate MODE SENSE lengths in scsi_cdl_enable()
86f901803080 ksmbd: don't check directory emptiness when deleting a stream
4af021a97773 drm: bridge: ti-sn65dsi83: Remove NO_HFP and NO_HBP mode flags for LVDS stability
5b08dccecf82 ntfs3: reject direct userspace writes to reserved $LX* xattrs
ce73a5db44e3 drm/msm/dsi: support DSC configurations with slice_per_pkt > 1
ffa88de8ddb6 drm/mipi-dsi: add flag for sending all DSC slices in one packet
80f43c521771 ASoC: rsnd: ssiu: Add RZ/G3E BUSIF support
948b075ebc95 ASoC: rsnd: Add RZ/G3E SoC probing and register map
5366a017099c ocfs2: reject dinodes with non-canonical i_mode type
2c7d48c75c90 drm/bridge: tc358768: Add LP mode command support
47d758a58494 drm/bridge: tc358768: Fix typo in TC358768_DSI_CONTROL_DIS_MODE
2d4e80271f78 drm/panel: himax-hx83102: restore MODE_LPM after sending disable cmds
570cf799e87a drm/panel: boe-tv101wum-nl6: restore MODE_LPM after sending disable cmds
]
|
| 33/2 |
2026/11/18 07:25 |
tool |
git-log |
0m
Args: map[Count:30 MessageRegexps:[kernfs_refresh_inode]]
Results: map[Output:dc1a0cc95737 kernfs: don't lose IN_DELETE_SELF when decoding a file handle
507d8ce13f5b kernfs: Don't set_nlink for directories being removed
aa1d058d48f2 kernfs: dont take i_lock on inode attr read
47b5c64d0ab5 kernfs: use i_lock to protect concurrent inode updates
0db64dd060f7 perf report: Show inline stack for stdio mode
]
|
| 34/2 |
2026/11/18 07:26 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 37034
output: 133
thoughts: 116
Thoughts: **Delving into the Depths of VFS, Kernfs, and Data Races**
Hold on a minute! My immediate priority is to gain a comprehensive understanding of how data races manifest within the VFS, specifically concerning `kernfs` and inodes. I need to trace the evolution of `kernfs_refresh_inode` and its role in mitigating these races. To achieve this, I plan to meticulously examine all commits within the `fs/` directory that reference "KCSAN" or "data-race." I'll be leveraging `git log` with the `MessageRegexps` filter set to "KCSAN" and applying it to the `fs/` path. Additionally, I need to pay special attention to the commits identified by `dc1a0cc95737` and `ffed24e22845`, as these likely represent pivotal moments in this exploration. My goal is to build a robust mental model of the underlying mechanisms and the historical context that led to the current implementation.
|
| 35/2 |
2026/11/18 07:27 |
tool |
git-log |
0m
Args: map[Count:40 MessageRegexps:[KCSAN] PathPrefix:fs/]
Results: map[Output:ef179363b043 kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()
bf4ccbaad8bd fuse: fix copy_file_range reply handling race
e01f7ddcb18a mm/page-flags: check page/folio->private instead of PG_private
cefc1a24ace5 aio: Use accessor for hrtimer_sleeper ->task field
8d01d6b4c17c fs_pin: publish pins with RCU list helpers
8c2a5e65a977 kernfs: Remove kernfs_rwsem from dentry revalidation
d9e00c457d4a ntfs: serialize resident iomap reads with mrec_lock
1dc88208cfdc binfmt_misc: annotate racy accesses to ->enabled
c610d2d07879 fs: annotate inode timestamp accessors
b952837f734c f2fs: annotate lockless last_time[] accesses
521fed4886cc fuse: use READ_ONCE in fuse_chan_num_background()
46725a0056c8 fuse-uring: fix data races on ring->ready
cdf8a566eeef btrfs: add data_race() in btrfs_account_ro_block_groups_free_space()
80eb65ccf6f7 btrfs: annotate block group access with data_race() when sorting for reclaim
986bf6ed44df btrfs: avoid load/store tearing races when checking if an inode was logged
2b7c9664c3ce fs: annotate suspected data race between poll_schedule_timeout() and pollwake()
af98b0157adf jbd2: fix data-race and null-ptr-deref in jbd2_journal_dirty_metadata()
7dbfa4266c5e btrfs: fix harmless race getting delayed ref head count when running delayed refs
ba5d06440cae btrfs: get used bytes while holding lock at btrfs_reclaim_bgs_work()
1283b8c125a8 btrfs: get zone unusable bytes while holding lock at btrfs_reclaim_bgs_work()
902cc179c931 ext4: supress data-race warnings in ext4_free_inodes_{count,set}()
448494051429 btrfs: avoid races when tracking progress for extent map shrinking
ca84529a842f btrfs: fix data race when accessing the last_trans field of a root
c7bb26b847e5 btrfs: fix data race at btrfs_use_block_rsv() when accessing block reserve
e06cc89475ed btrfs: fix data races when accessing the reserved amount of block reserves
0124855ff18b btrfs: add and use helpers for reading and writing last_trans_committed
4a4f8fe2b023 btrfs: add and use helpers for reading and writing fs_info->generation
6008859b6c6e btrfs: add and use helpers for reading and writing log_transid
f98507879699 btrfs: add and use helpers for reading and writing last_log_commit
7827c81f0248 Revert "SUNRPC: Use RMW bitops in single-threaded hot paths"
748f553c3c4c btrfs: add KCSAN annotations for unlocked access to block_rsv->full
97e86631bccd btrfs: don't set lock_owner when locking extent buffer for reading
f485922d8fe4 pipe: make poll_usage boolean and annotate its access
b16ef427adf3 io_uring: fix data race to avoid potential NULL-deref
e19eb11f4f3d btrfs: only let one thread pre-flush delayed refs in commit
dce8e237100f ext4: fix a data race at inode->i_disksize
28936b62e71e ext4: fix a data race at inode->i_blocks
6c5d91124929 jbd2: fix data races at struct journal_head
35df4299a648 ext4: fix a data race in EXT4_I(inode)->i_disksize
d8e464ecc17b vfs: mark pipes and sockets as stream-like file descriptors
]
|
| 36/2 |
2026/11/18 07:27 |
tool |
git-show |
0m
Args:
Results: map[Output:commit dc1a0cc95737344b1a6aa42075e573e9625c6b9a
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 5 12:16:12 2026 -0700
kernfs: don't lose IN_DELETE_SELF when decoding a file handle
__kernfs_remove() clears i_nlink on a node's inodes and finds them with
ilookup(). A decode that has pinned the node but not yet hashed its
inode is invisible to that pass:
CPU0 CPU1
open_by_handle_at()
kernfs_find_and_get_node_by_id()
pins the node, still active
rmdir()
marks the subtree removing
ilookup() finds no inode
kernfs_get_inode()
hashes an inode with i_nlink 1
Nothing fixes it later: kernfs_refresh_inode() never touches i_nlink for
a file and skips it for a directory being removed. The inode keeps the
1 it was born with, and dentry_unlink_inode() sends IN_DELETE_SELF only
at 0, so a watcher never learns the node went away.
The other callers of kernfs_get_inode() are safe: those in fs/kernfs
hold kernfs_rwsem, and cgroup_may_write() is covered by cgroup_mutex,
which cgroup_destroy_locked() holds across kernfs_remove().
__kernfs_fh_to_dentry() has held nothing since exportfs support was
added.
Take kernfs_rwsem for reading, as ->get_parent already does, and cover
the lookup as well as kernfs_get_inode(). __kernfs_remove() deactivates
the whole subtree under the write lock, so under the read lock either
the lookup refuses the node, or the inode is hashed before the ilookup()
pass runs. The same holds for ->fh_to_parent, since a node cannot be
active while an ancestor is being removed.
Reproduced with a 300ms delay between the lookup and kernfs_get_inode(),
decoding a handle for a file in a cgroup directory while another task
rmdir()s it: st_nlink is 1 without this patch and 0 with it.
->get_parent still has a window of its own. It takes the same lock but
has no active check, so reconnect_path() can build an inode for an
ancestor that is already gone. That needs the active test rather than a
lock, and changes what ->get_parent returns, so it is left to the series
that reworks these paths.
Fixes: eea5d2bb34ba ("kernfs: Send IN_DELETE_SELF and IN_IGNORED")
Cc: stable@vger.kernel.org
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260905191613.3143937-4-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c
index f183a96778b9..c15ba6357162 100644
--- a/fs/kernfs/mount.c
+++ b/fs/kernfs/mount.c
@@ -124,22 +124,32 @@ static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb,
return NULL;
}
- kn = kernfs_find_and_get_node_by_id(info->root, id);
- if (!kn)
- return ERR_PTR(-ESTALE);
+ /*
+ * Hold kernfs_rwsem across the lookup as well as kernfs_get_inode().
+ * __kernfs_remove() deactivates the subtree and clears i_nlink on its
+ * inodes under the write lock, so under the read lock either
+ * kernfs_find_and_get_node_by_id() refuses the node, or the inode is
+ * in the inode hash before the ilookup() pass goes looking for it.
+ */
+ scoped_guard(rwsem_read, &info->root->kernfs_rwsem) {
+ kn = kernfs_find_and_get_node_by_id(info->root, id);
+ if (!kn)
+ return ERR_PTR(-ESTALE);
- if (get_parent) {
- struct kernfs_node *parent;
+ if (get_parent) {
+ struct kernfs_node *parent;
- parent = kernfs_get_parent(kn);
+ parent = kernfs_get_parent(kn);
+ kernfs_put(kn);
+ kn = parent;
+ if (!kn)
+ return ERR_PTR(-ESTALE);
+ }
+
+ inode = kernfs_get_inode(sb, kn);
kernfs_put(kn);
- kn = parent;
- if (!kn)
- return ERR_PTR(-ESTALE);
}
- inode = kernfs_get_inode(sb, kn);
- kernfs_put(kn);
return d_obtain_alias(inode);
}
]
|
| 37/2 |
2026/11/18 07:27 |
tool |
git-show |
0m
Args: map[Commit:ffed24e22845 File:fs/kernfs/inode.c]
Results: map[Output:commit ffed24e22845a3da0ae01095ae3f11c8d16e889d
Author: Tejun Heo <tj@kernel.org>
Date: Thu Nov 28 14:54:32 2013 -0500
sysfs, kernfs: move inode code to fs/kernfs/inode.c
There's nothing sysfs-specific in fs/sysfs/inode.c. Move everything
in it to fs/kernfs/inode.c. The respective declarations in
fs/sysfs/sysfs.h are moved to fs/kernfs/kernfs-internal.h.
This is pure relocation.
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 86bfeea07de2..9d4fab49ea20 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -7,3 +7,330 @@
*
* This file is released under the GPLv2.
*/
+
+#include <linux/pagemap.h>
+#include <linux/backing-dev.h>
+#include <linux/capability.h>
+#include <linux/errno.h>
+#include <linux/slab.h>
+#include <linux/xattr.h>
+#include <linux/security.h>
+
+#include "kernfs-internal.h"
+
+static const struct address_space_operations sysfs_aops = {
+ .readpage = simple_readpage,
+ .write_begin = simple_write_begin,
+ .write_end = simple_write_end,
+};
+
+static struct backing_dev_info sysfs_backing_dev_info = {
+ .name = "sysfs",
+ .ra_pages = 0, /* No readahead */
+ .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
+};
+
+static const struct inode_operations sysfs_inode_operations = {
+ .permission = sysfs_permission,
+ .setattr = sysfs_setattr,
+ .getattr = sysfs_getattr,
+ .setxattr = sysfs_setxattr,
+};
+
+int __init sysfs_inode_init(void)
+{
+ return bdi_init(&sysfs_backing_dev_info);
+}
+
+static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
+{
+ struct sysfs_inode_attrs *attrs;
+ struct iattr *iattrs;
+
+ attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
+ if (!attrs)
+ return NULL;
+ iattrs = &attrs->ia_iattr;
+
+ /* assign default attributes */
+ iattrs->ia_mode = sd->s_mode;
+ iattrs->ia_uid = GLOBAL_ROOT_UID;
+ iattrs->ia_gid = GLOBAL_ROOT_GID;
+ iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
+
+ return attrs;
+}
+
+static int __kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr)
+{
+ struct sysfs_inode_attrs *sd_attrs;
+ struct iattr *iattrs;
+ unsigned int ia_valid = iattr->ia_valid;
+
+ sd_attrs = sd->s_iattr;
+
+ if (!sd_attrs) {
+ /* setting attributes for the first time, allocate now */
+ sd_attrs = sysfs_init_inode_attrs(sd);
+ if (!sd_attrs)
+ return -ENOMEM;
+ sd->s_iattr = sd_attrs;
+ }
+ /* attributes were changed at least once in past */
+ iattrs = &sd_attrs->ia_iattr;
+
+ if (ia_valid & ATTR_UID)
+ iattrs->ia_uid = iattr->ia_uid;
+ if (ia_valid & ATTR_GID)
+ iattrs->ia_gid = iattr->ia_gid;
+ if (ia_valid & ATTR_ATIME)
+ iattrs->ia_atime = iattr->ia_atime;
+ if (ia_valid & ATTR_MTIME)
+ iattrs->ia_mtime = iattr->ia_mtime;
+ if (ia_valid & ATTR_CTIME)
+ iattrs->ia_ctime = iattr->ia_ctime;
+ if (ia_valid & ATTR_MODE) {
+ umode_t mode = iattr->ia_mode;
+ iattrs->ia_mode = sd->s_mode = mode;
+ }
+ return 0;
+}
+
+/**
+ * kernfs_setattr - set iattr on a node
+ * @sd: target node
+ * @iattr: iattr to set
+ *
+ * Returns 0 on success, -errno on failure.
+ */
+int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr)
+{
+ int ret;
+
+ mutex_lock(&sysfs_mutex);
+ ret = __kernfs_setattr(sd, iattr);
+ mutex_unlock(&sysfs_mutex);
+ return ret;
+}
+
+int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
+{
+ struct inode *inode = dentry->d_inode;
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ int error;
+
+ if (!sd)
+ return -EINVAL;
+
+ mutex_lock(&sysfs_mutex);
+ error = inode_change_ok(inode, iattr);
+ if (error)
+ goto out;
+
+ error = __kernfs_setattr(sd, iattr);
+ if (error)
+ goto out;
+
+ /* this ignores size changes */
+ setattr_copy(inode, iattr);
+
+out:
+ mutex_unlock(&sysfs_mutex);
+ return error;
+}
+
+static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata,
+ u32 *secdata_len)
+{
+ struct sysfs_inode_attrs *iattrs;
+ void *old_secdata;
+ size_t old_secdata_len;
+
+ if (!sd->s_iattr) {
+ sd->s_iattr = sysfs_init_inode_attrs(sd);
+ if (!sd->s_iattr)
+ return -ENOMEM;
+ }
+
+ iattrs = sd->s_iattr;
+ old_secdata = iattrs->ia_secdata;
+ old_secdata_len = iattrs->ia_secdata_len;
+
+ iattrs->ia_secdata = *secdata;
+ iattrs->ia_secdata_len = *secdata_len;
+
+ *secdata = old_secdata;
+ *secdata_len = old_secdata_len;
+ return 0;
+}
+
+int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
+ size_t size, int flags)
+{
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ void *secdata;
+ int error;
+ u32 secdata_len = 0;
+
+ if (!sd)
+ return -EINVAL;
+
+ if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
+ const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
+ error = security_inode_setsecurity(dentry->d_inode, suffix,
+ value, size, flags);
+ if (error)
+ goto out;
+ error = security_inode_getsecctx(dentry->d_inode,
+ &secdata, &secdata_len);
+ if (error)
+ goto out;
+
+ mutex_lock(&sysfs_mutex);
+ error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len);
+ mutex_unlock(&sysfs_mutex);
+
+ if (secdata)
+ security_release_secctx(secdata, secdata_len);
+ } else
+ return -EINVAL;
+out:
+ return error;
+}
+
+static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
+{
+ inode->i_mode = mode;
+ inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
+}
+
+static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
+{
+ inode->i_uid = iattr->ia_uid;
+ inode->i_gid = iattr->ia_gid;
+ inode->i_atime = iattr->ia_atime;
+ inode->i_mtime = iattr->ia_mtime;
+ inode->i_ctime = iattr->ia_ctime;
+}
+
+static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
+{
+ struct sysfs_inode_attrs *iattrs = sd->s_iattr;
+
+ inode->i_mode = sd->s_mode;
+ if (iattrs) {
+ /* sysfs_dirent has non-default attributes
+ * get them from persistent copy in sysfs_dirent
+ */
+ set_inode_attr(inode, &iattrs->ia_iattr);
+ security_inode_notifysecctx(inode,
+ iattrs->ia_secdata,
+ iattrs->ia_secdata_len);
+ }
+
+ if (sysfs_type(sd) == SYSFS_DIR)
+ set_nlink(inode, sd->s_dir.subdirs + 2);
+}
+
+int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
+ struct kstat *stat)
+{
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ struct inode *inode = dentry->d_inode;
+
+ mutex_lock(&sysfs_mutex);
+ sysfs_refresh_inode(sd, inode);
+ mutex_unlock(&sysfs_mutex);
+
+ generic_fillattr(inode, stat);
+ return 0;
+}
+
+static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
+{
+ kernfs_get(sd);
+ inode->i_private = sd;
+ inode->i_mapping->a_ops = &sysfs_aops;
+ inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
+ inode->i_op = &sysfs_inode_operations;
+
+ set_default_inode_attr(inode, sd->s_mode);
+ sysfs_refresh_inode(sd, inode);
+
+ /* initialize inode according to type */
+ switch (sysfs_type(sd)) {
+ case SYSFS_DIR:
+ inode->i_op = &sysfs_dir_inode_operations;
+ inode->i_fop = &sysfs_dir_operations;
+ break;
+ case SYSFS_KOBJ_ATTR:
+ inode->i_size = sd->s_attr.size;
+ inode->i_fop = &kernfs_file_operations;
+ break;
+ case SYSFS_KOBJ_LINK:
+ inode->i_op = &sysfs_symlink_inode_operations;
+ break;
+ default:
+ BUG();
+ }
+
+ unlock_new_inode(inode);
+}
+
+/**
+ * sysfs_get_inode - get inode for sysfs_dirent
+ * @sb: super block
+ * @sd: sysfs_dirent to allocate inode for
+ *
+ * Get inode for @sd. If such inode doesn't exist, a new inode
+ * is allocated and basics are initialized. New inode is
+ * returned locked.
+ *
+ * LOCKING:
+ * Kernel thread context (may sleep).
+ *
+ * RETURNS:
+ * Pointer to allocated inode on success, NULL on failure.
+ */
+struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd)
+{
+ struct inode *inode;
+
+ inode = iget_locked(sb, sd->s_ino);
+ if (inode && (inode->i_state & I_NEW))
+ sysfs_init_inode(sd, inode);
+
+ return inode;
+}
+
+/*
+ * The sysfs_dirent serves as both an inode and a directory entry for sysfs.
+ * To prevent the sysfs inode numbers from being freed prematurely we take a
+ * reference to sysfs_dirent from the sysfs inode. A
+ * super_operations.evict_inode() implementation is needed to drop that
+ * reference upon inode destruction.
+ */
+void sysfs_evict_inode(struct inode *inode)
+{
+ struct sysfs_dirent *sd = inode->i_private;
+
+ truncate_inode_pages(&inode->i_data, 0);
+ clear_inode(inode);
+ kernfs_put(sd);
+}
+
+int sysfs_permission(struct inode *inode, int mask)
+{
+ struct sysfs_dirent *sd;
+
+ if (mask & MAY_NOT_BLOCK)
+ return -ECHILD;
+
+ sd = inode->i_private;
+
+ mutex_lock(&sysfs_mutex);
+ sysfs_refresh_inode(sd, inode);
+ mutex_unlock(&sysfs_mutex);
+
+ return generic_permission(inode, mask);
+}
]
|
| 38/2 |
2026/11/18 07:27 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 2b256ebe82872a85c91cbb26e9edd314923e0fbc
Author: Christian Brauner <brauner@kernel.org>
Date: Thu Sep 10 17:48:03 2026 +0200
fs: add filp_close_sync()
Currently close() already does a synchronous release of the last
reference since the task is about to return to userspace and the
deferral through task work buys nothing. Add a filp_close_sync() helper.
We'll use that in the next patches.
No functional changes.
Link: https://patch.msgid.link/20260910-work-coredump-unlock-self-v4-5-a5c1800dc930@kernel.org
Reviewed-by: NeilBrown <neil@brown.name>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/internal.h b/fs/internal.h
index c658c8a5ebd5..8812de210d3f 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -198,6 +198,7 @@ extern struct file *do_file_open_root(const struct path *,
extern struct open_how build_open_how(int flags, umode_t mode);
extern int build_open_flags(const struct open_how *how, struct open_flags *op);
struct file *file_close_fd_locked(struct files_struct *files, unsigned fd);
+int filp_close_sync(struct file *filp, fl_owner_t id);
int do_ftruncate(struct file *file, loff_t length, unsigned int flags);
int chmod_common(const struct path *path, umode_t mode);
diff --git a/fs/open.c b/fs/open.c
index 6b1c14e684a9..998e42ac319a 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1537,6 +1537,19 @@ int filp_close(struct file *filp, fl_owner_t id)
}
EXPORT_SYMBOL(filp_close);
+/* Like filp_close() but the last reference is put right here. */
+int filp_close_sync(struct file *filp, fl_owner_t id)
+{
+ int retval;
+
+ /* Kernel threads must never put their final reference here. */
+ VFS_WARN_ON_ONCE(current->flags & PF_KTHREAD);
+ retval = filp_flush(filp, id);
+ fput_close_sync(filp);
+
+ return retval;
+}
+
/*
* Careful here! We test whether the file pointer is NULL before
* releasing the fd. This ensures that one clone task can't release
@@ -1551,13 +1564,11 @@ SYSCALL_DEFINE1(close, unsigned int, fd)
if (!file)
return -EBADF;
- retval = filp_flush(file, current->files);
-
/*
* We're returning to user space. Don't bother
* with any delayed fput() cases.
*/
- fput_close_sync(file);
+ retval = filp_close_sync(file, current->files);
if (likely(retval == 0))
return 0;
]
|
| 39/2 |
2026/11/18 07:27 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 44419
output: 77
thoughts: 405
Thoughts: **Diving into a KCSAN Data-Race:**
My immediate reaction to this KCSAN report is to pause and investigate the configuration and rules underlying its operation. I'm particularly intrigued by the specific data race it's flagging: `BUG: KCSAN: data-race in dnotify_flush / kernfs_iop_getattr`.
I need to understand the mechanics of this race. The report shows a read operation in `dnotify_flush` at `fs/notify/dnotify/dnotify.c:161` accessing memory at `0xffff88810632faa0`, and a write operation in `kernfs_iop_getattr` at `fs/kernfs/inode.c:191` also targeting the same address `0xffff88810632faa0`. This is the core of the issue – concurrent access to the same memory location.
However, a crucial question arises: how does KCSAN differentiate between different types of writes, specifically when the written value is the same as the existing value versus when it's different? I need to delve into the `kernel/kcsan/` directory to understand its internal logic. Specifically, I want to confirm if KCSAN has a mode like `KCSAN_REPORT_VALUE_CHANGE_ONLY` that would ignore writes not altering the value, or if it reports all writes, regardless of value modification. My next step is to meticulously examine the KCSAN implementation within `kernel/kcsan/` and its configuration options in `lib/Kconfig.kcsan` to clarify this behavior.
|
| 40/2 |
2026/11/18 07:27 |
tool |
grepper |
0m
Args: map[Expression:VALUE_CHANGE_ONLY PathPrefixes:[kernel/kcsan/ lib/Kconfig.kcsan]]
Results: map[Output:kernel/kcsan/core.c=803=void __init kcsan_init(void)
--
kernel/kcsan/core.c-820-
kernel/kcsan/core.c:821: if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) ||
kernel/kcsan/core.c-822- IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) ||
--
kernel/kcsan/kcsan_test.c=776=static void test_concurrent_races(struct kunit *test)
--
kernel/kcsan/kcsan_test.c-802-
kernel/kcsan/kcsan_test.c:803:/* Test the KCSAN_REPORT_VALUE_CHANGE_ONLY option. */
kernel/kcsan/kcsan_test.c-804-__no_kcsan
kernel/kcsan/kcsan_test.c=805=static void test_novalue_change(struct kunit *test)
--
kernel/kcsan/kcsan_test.c-825- } while (!end_test_checks(match_expect));
kernel/kcsan/kcsan_test.c:826: if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY))
kernel/kcsan/kcsan_test.c-827- KUNIT_EXPECT_FALSE(test, match_expect);
--
kernel/kcsan/kcsan_test.c-832-/*
kernel/kcsan/kcsan_test.c:833: * Test that the rules where the KCSAN_REPORT_VALUE_CHANGE_ONLY option should
kernel/kcsan/kcsan_test.c-834- * never apply work.
--
kernel/kcsan/report.c=172=skip_report(enum kcsan_value_change value_change, unsigned long top_frame)
--
kernel/kcsan/report.c-179- * cannot know the value written of an instrumented access. For the 2nd
kernel/kcsan/report.c:180: * call there are 6 cases with CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY:
kernel/kcsan/report.c-181- *
--
kernel/kcsan/report.c-193- */
kernel/kcsan/report.c:194: if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) &&
kernel/kcsan/report.c-195- value_change == KCSAN_VALUE_CHANGE_MAYBE) {
--
lib/Kconfig.kcsan=184=config KCSAN_WEAK_MEMORY
--
lib/Kconfig.kcsan-199-
lib/Kconfig.kcsan:200:config KCSAN_REPORT_VALUE_CHANGE_ONLY
lib/Kconfig.kcsan-201- bool "Only report races where watcher observed a data value change"
--
lib/Kconfig.kcsan=241=config KCSAN_PERMISSIVE
lib/Kconfig.kcsan-242- bool "Enable all additional permissive rules"
lib/Kconfig.kcsan:243: depends on KCSAN_REPORT_VALUE_CHANGE_ONLY
lib/Kconfig.kcsan-244- help
]
|
| 41/2 |
2026/11/18 07:27 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:kernel/kcsan/]
Results: map[Output:0af298e62e5c treewide: refresh kmalloc_obj() conversions
bd3cc16d1d7d kcsan: test: Do not count report printing time against test duration
99b6fcce8703 kcsan: Avoid division by zero in kcsan_prandom_u32_max()
a8488ecbd7ba kcsan: avoid unintended access checking in NMIs
795469820c63 kcsan: test: Adjust "expect" allocation type for kmalloc_obj
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
a9e5620c9a9e kernel: fix off-by-one benign bugs
0eaa911f8908 kcsan: Enable context analysis
b9a214b5f6aa kunit: Pass parameterized test context to generate_params()
800348aa34b2 kcsan: test: Replace deprecated strcpy() with strscpy()
9872916ad1a1 kcsan: test: Initialize dummy variable
aad823aa3a7d treewide, timers: Rename destroy_timer_on_stack() as timer_destroy_on_stack()
8fa7292fee5c treewide: Switch/rename to timer_delete[_sync]()
b86f7c9fad06 kcsan: Remove redundant call of kallsyms_lookup_name()
59458fa4ddb4 kcsan: Turn report_filterlist_lock into a raw_spinlock
43d631bf06ec kcsan: Use min() to fix Coccinelle warning
ddd7432d621d kcsan: Add missing MODULE_DESCRIPTION() macro
31f605a308e6 kcsan, compiler_types: Introduce __data_racy type qualifier
f12fb73b74fd mm: delete checks for xor_unlock_is_negative_byte()
]
|
| 42/2 |
2026/11/18 07:27 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 46308
output: 81
thoughts: 75
Thoughts: **Hold On, I Think I've Found Something Important**
Okay, wait a second! I just had a thought – a real "aha!" moment. I need to examine `kernel/kcsan/report.c` specifically around line 172, and then immediately cross-reference that with `lib/Kconfig.kcsan`, focusing on approximately line 200. My immediate next step needs to be a `read-file` operation for both of those files. This feels like a crucial juncture.
|
| 43/2 |
2026/11/18 07:27 |
tool |
read-file |
0m
Args: map[File:kernel/kcsan/report.c FirstLine:165 LineCount:70]
Results: map[Contents: 165: return false;
166: }
167:
168: /*
169: * Special rules to skip reporting.
170: */
171: static bool
172: skip_report(enum kcsan_value_change value_change, unsigned long top_frame)
173: {
174: /* Should never get here if value_change==FALSE. */
175: WARN_ON_ONCE(value_change == KCSAN_VALUE_CHANGE_FALSE);
176:
177: /*
178: * The first call to skip_report always has value_change==TRUE, since we
179: * cannot know the value written of an instrumented access. For the 2nd
180: * call there are 6 cases with CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY:
181: *
182: * 1. read watchpoint, conflicting write (value_change==TRUE): report;
183: * 2. read watchpoint, conflicting write (value_change==MAYBE): skip;
184: * 3. write watchpoint, conflicting write (value_change==TRUE): report;
185: * 4. write watchpoint, conflicting write (value_change==MAYBE): skip;
186: * 5. write watchpoint, conflicting read (value_change==MAYBE): skip;
187: * 6. write watchpoint, conflicting read (value_change==TRUE): report;
188: *
189: * Cases 1-4 are intuitive and expected; case 5 ensures we do not report
190: * data races where the write may have rewritten the same value; case 6
191: * is possible either if the size is larger than what we check value
192: * changes for or the access type is KCSAN_ACCESS_ASSERT.
193: */
194: if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) &&
195: value_change == KCSAN_VALUE_CHANGE_MAYBE) {
196: /*
197: * The access is a write, but the data value did not change.
198: *
199: * We opt-out of this filter for certain functions at request of
200: * maintainers.
201: */
202: char buf[64];
203: int len = scnprintf(buf, sizeof(buf), "%ps", (void *)top_frame);
204:
205: if (!strnstr(buf, "rcu_", len) &&
206: !strnstr(buf, "_rcu", len) &&
207: !strnstr(buf, "_srcu", len))
208: return true;
209: }
210:
211: return kcsan_skip_report_debugfs(top_frame);
212: }
213:
214: static const char *get_access_type(int type)
215: {
216: if (type & KCSAN_ACCESS_ASSERT) {
217: if (type & KCSAN_ACCESS_SCOPED) {
218: if (type & KCSAN_ACCESS_WRITE)
219: return "assert no accesses (reordered)";
220: else
221: return "assert no writes (reordered)";
222: } else {
223: if (type & KCSAN_ACCESS_WRITE)
224: return "assert no accesses";
225: else
226: return "assert no writes";
227: }
228: }
229:
230: switch (type) {
231: case 0:
232: return "read";
233: case KCSAN_ACCESS_ATOMIC:
234: return "read (marked)";
]
|
| 44/2 |
2026/11/18 07:27 |
tool |
read-file |
0m
Args: map[File:lib/Kconfig.kcsan FirstLine:195 LineCount:65]
Results: map[Contents: 195: particular reordered writes.
196:
197: Weak memory modeling relies on additional instrumentation and may
198: affect performance.
199:
200: config KCSAN_REPORT_VALUE_CHANGE_ONLY
201: bool "Only report races where watcher observed a data value change"
202: default y
203: depends on !KCSAN_STRICT
204: help
205: If enabled and a conflicting write is observed via a watchpoint, but
206: the data value of the memory location was observed to remain
207: unchanged, do not report the data race.
208:
209: config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
210: bool "Assume that plain aligned writes up to word size are atomic"
211: default y
212: depends on !KCSAN_STRICT
213: help
214: Assume that plain aligned writes up to word size are atomic by
215: default, and also not subject to other unsafe compiler optimizations
216: resulting in data races. This will cause KCSAN to not report data
217: races due to conflicts where the only plain accesses are aligned
218: writes up to word size: conflicts between marked reads and plain
219: aligned writes up to word size will not be reported as data races;
220: notice that data races between two conflicting plain aligned writes
221: will also not be reported.
222:
223: config KCSAN_IGNORE_ATOMICS
224: bool "Do not instrument marked atomic accesses"
225: depends on !KCSAN_STRICT
226: help
227: Never instrument marked atomic accesses. This option can be used for
228: additional filtering. Conflicting marked atomic reads and plain
229: writes will never be reported as a data race, however, will cause
230: plain reads and marked writes to result in "unknown origin" reports.
231: If combined with CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n, data
232: races where at least one access is marked atomic will never be
233: reported.
234:
235: Similar to KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, but including unaligned
236: accesses, conflicting marked atomic reads and plain writes will not
237: be reported as data races; however, unlike that option, data races
238: due to two conflicting plain writes will be reported (aligned and
239: unaligned, if CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n).
240:
241: config KCSAN_PERMISSIVE
242: bool "Enable all additional permissive rules"
243: depends on KCSAN_REPORT_VALUE_CHANGE_ONLY
244: help
245: Enable additional permissive rules to ignore certain classes of data
246: races (also see kernel/kcsan/permissive.h). None of the permissive
247: rules imply that such data races are generally safe, but can be used
248: to further reduce reported data races due to data-racy patterns
249: common across the kernel.
250:
251: endif # KCSAN
]
|
| 45/2 |
2026/11/18 07:27 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 48533
output: 145
thoughts: 360
Thoughts: **Ah, the realization dawns! I need to revisit this thoroughly.**
I've just spotted something crucial within the KCSAN configuration: `KCSAN_REPORT_VALUE_CHANGE_ONLY`. The default setting is 'y', and it's enabled unless `KCSAN_STRICT` is active. The description here is key: "Only report races where watcher observed a data value change." It explicitly states that if a conflicting write occurs, but the memory location's value *doesn't* change, KCSAN won't report it. This is a significant detail.
Now, this immediately brings me to a question about a specific operation I observed: `inode->i_mode = kn->mode` within the `kernfs_iop_getattr` function. My immediate thought is: why did this assignment *actually change* the value of `inode->i_mode` at that particular moment?
Furthermore, I've noted something else striking in the crash report itself: the `Comm` field reads `udevd`. This is not a kernel thread; it's a userspace process. The report states "Task 3283 is `udevd`!!". This is a peculiar observation.
Putting these together, I'm now asking myself: why would `kn->mode` *differ* from `inode->i_mode` specifically in the context of a sysfs/kernfs file? This discrepancy, coupled with the `udevd` involvement and the KCSAN configuration, suggests a potential race condition that might have been masked if the value hadn't changed.
My next logical step, therefore, is to conduct a comprehensive search across the entire kernel. I need to identify every single location where `kn->mode` is modified. Additionally, I must investigate all calls to `kernfs_setattr`, `sysfs_chmod_file`, and `chmod`. My search will be focused on writes to `.mode` fields and these specific functions, particularly within the `fs/kernfs/` and `fs/sysfs/` directories, but also extending to any other relevant areas of the kernel code. I'm looking for any and all instances that could lead to this value mismatch or interact with these attributes.
|
| 46/2 |
2026/11/18 07:28 |
tool |
codesearch-find-references |
0m
Args: map[IncludeSnippetLines:5 Name:kernfs_node::mode]
Results: map[References:[map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:__kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:690 SourceSnippet: 685: atomic_set(&kn->count, 1);
686: atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
687: RB_CLEAR_NODE(&kn->rb);
688:
689: rcu_assign_pointer(kn->name, name);
690: kn->mode = mode;
691: kn->flags = flags;
692:
693: if (!uid_eq(uid, GLOBAL_ROOT_UID) || !gid_eq(gid, GLOBAL_ROOT_GID)) {
694: struct iattr iattr = {
695: .ia_valid = ATTR_UID | ATTR_GID,
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:__kernfs_setattr SourceFile:fs/kernfs/inode.c SourceLine:88 SourceSnippet: 83: if (ia_valid & ATTR_MTIME)
84: attrs->ia_mtime = iattr->ia_mtime;
85: if (ia_valid & ATTR_CTIME)
86: attrs->ia_ctime = iattr->ia_ctime;
87: if (ia_valid & ATTR_MODE)
88: kn->mode = iattr->ia_mode;
89: return 0;
90: }
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_fop_readdir SourceFile:fs/kernfs/dir.c SourceLine:2029 SourceSnippet:2024: ns = kernfs_info(dentry->d_sb)->ns;
2025:
2026: for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos, NULL, NULL);
2027: pos;
2028: pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos, name)) {
2029: unsigned int type = fs_umode_to_dtype(pos->mode);
2030: ino_t ino = kernfs_ino(pos);
2031: int len;
2032:
2033: /*
2034: * The copy is also the resume key, so a truncated name would
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_init_inode SourceFile:fs/kernfs/inode.c SourceLine:206 SourceSnippet: 201: inode->i_private = kn;
202: inode->i_mapping->a_ops = &ram_aops;
203: inode->i_op = &kernfs_iops;
204: inode->i_generation = kernfs_gen(kn);
205:
206: set_default_inode_attr(inode, kn->mode);
207: kernfs_refresh_inode(kn, inode);
208:
209: /* initialize inode according to type */
210: switch (kernfs_type(kn)) {
211: case KERNFS_DIR:
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:746 SourceSnippet: 741: /*
742: * The mode and the gid below are read unlocked on purpose: they feed
743: * a node that does not exist yet, so nothing orders a racing chmod or
744: * chown against this creation.
745: */
746: if (READ_ONCE(parent->mode) & S_ISGID) {
747: /* this code block imitates inode_init_owner() for
748: * kernfs
749: */
750: struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
751:
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:746 SourceSnippet: 741: /*
742: * The mode and the gid below are read unlocked on purpose: they feed
743: * a node that does not exist yet, so nothing orders a racing chmod or
744: * chown against this creation.
745: */
746: if (READ_ONCE(parent->mode) & S_ISGID) {
747: /* this code block imitates inode_init_owner() for
748: * kernfs
749: */
750: struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
751:
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:746 SourceSnippet: 741: /*
742: * The mode and the gid below are read unlocked on purpose: they feed
743: * a node that does not exist yet, so nothing orders a racing chmod or
744: * chown against this creation.
745: */
746: if (READ_ONCE(parent->mode) & S_ISGID) {
747: /* this code block imitates inode_init_owner() for
748: * kernfs
749: */
750: struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
751:
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:746 SourceSnippet: 741: /*
742: * The mode and the gid below are read unlocked on purpose: they feed
743: * a node that does not exist yet, so nothing orders a racing chmod or
744: * chown against this creation.
745: */
746: if (READ_ONCE(parent->mode) & S_ISGID) {
747: /* this code block imitates inode_init_owner() for
748: * kernfs
749: */
750: struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
751:
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:746 SourceSnippet: 741: /*
742: * The mode and the gid below are read unlocked on purpose: they feed
743: * a node that does not exist yet, so nothing orders a racing chmod or
744: * chown against this creation.
745: */
746: if (READ_ONCE(parent->mode) & S_ISGID) {
747: /* this code block imitates inode_init_owner() for
748: * kernfs
749: */
750: struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
751:
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:746 SourceSnippet: 741: /*
742: * The mode and the gid below are read unlocked on purpose: they feed
743: * a node that does not exist yet, so nothing orders a racing chmod or
744: * chown against this creation.
745: */
746: if (READ_ONCE(parent->mode) & S_ISGID) {
747: /* this code block imitates inode_init_owner() for
748: * kernfs
749: */
750: struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
751:
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:kernfs_new_node SourceFile:fs/kernfs/dir.c SourceLine:746 SourceSnippet: 741: /*
742: * The mode and the gid below are read unlocked on purpose: they feed
743: * a node that does not exist yet, so nothing orders a racing chmod or
744: * chown against this creation.
745: */
746: if (READ_ONCE(parent->mode) & S_ISGID) {
747: /* this code block imitates inode_init_owner() for
748: * kernfs
749: */
750: struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
751:
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:kernfs_refresh_inode SourceFile:fs/kernfs/inode.c SourceLine:169 SourceSnippet: 165: static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
166: {
167: struct kernfs_iattrs *attrs;
168:
169: inode->i_mode = kn->mode;
170: attrs = kernfs_iattrs_noalloc(kn);
171: if (attrs)
172: /*
173: * kernfs_node has non-default attributes get them from
174: * persistent copy in kernfs_node.
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:selinux_kernfs_init_security SourceFile:security/selinux/hooks.c SourceLine:3792 SourceSnippet:3787: return rc;
3788:
3789: if (crsec->create_sid) {
3790: newsid = crsec->create_sid;
3791: } else {
3792: u16 secclass = inode_mode_to_security_class(kn->mode);
3793: const char *kn_name;
3794: struct qstr q;
3795:
3796: /* kn is fresh, can't be renamed, name goes not away */
3797: kn_name = rcu_dereference_check(kn->name, true);
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:sysfs_chmod_file SourceFile:fs/sysfs/file.c SourceLine:446 SourceSnippet: 441:
442: kn = kernfs_find_and_get(kobj->sd, attr->name);
443: if (!kn)
444: return -ENOENT;
445:
446: newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
447: newattrs.ia_valid = ATTR_MODE;
448:
449: rc = kernfs_setattr(kn, &newattrs);
450:
451: kernfs_put(kn);
]] TruncatedOutput:false]
|
| 47/2 |
2026/11/18 07:28 |
tool |
codesearch-find-references |
0m
Args: map[IncludeSnippetLines:5 Name:kernfs_setattr]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:internal_change_owner SourceFile:fs/sysfs/file.c SourceLine:617 SourceSnippet: 612: struct iattr newattrs = {
613: .ia_valid = ATTR_UID | ATTR_GID,
614: .ia_uid = kuid,
615: .ia_gid = kgid,
616: };
617: return kernfs_setattr(kn, &newattrs);
618: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:sysfs_chmod_file SourceFile:fs/sysfs/file.c SourceLine:449 SourceSnippet: 444: return -ENOENT;
445:
446: newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
447: newattrs.ia_valid = ATTR_MODE;
448:
449: rc = kernfs_setattr(kn, &newattrs);
450:
451: kernfs_put(kn);
452: return rc;
453: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:sysfs_group_attrs_change_owner SourceFile:fs/sysfs/group.c SourceLine:534 SourceSnippet: 529: }
530: kn = kernfs_find_and_get(grp_kn, (*attr)->name);
531: if (!kn)
532: return -ENOENT;
533:
534: error = kernfs_setattr(kn, newattrs);
535: kernfs_put(kn);
536: if (error)
537: return error;
538: }
539: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:sysfs_group_attrs_change_owner SourceFile:fs/sysfs/group.c SourceLine:556 SourceSnippet: 551: }
552: kn = kernfs_find_and_get(grp_kn, (*bin_attr)->attr.name);
553: if (!kn)
554: return -ENOENT;
555:
556: error = kernfs_setattr(kn, newattrs);
557: kernfs_put(kn);
558: if (error)
559: return error;
560: }
561: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:sysfs_group_change_owner SourceFile:fs/sysfs/group.c SourceLine:599 SourceSnippet: 594: grp_kn = kobj->sd;
595: }
596: if (!grp_kn)
597: return -ENOENT;
598:
599: error = kernfs_setattr(grp_kn, &newattrs);
600: if (!error)
601: error = sysfs_group_attrs_change_owner(kobj, grp_kn, grp, &newattrs);
602:
603: kernfs_put(grp_kn);
604:
]] TruncatedOutput:false]
|
| 48/2 |
2026/11/18 07:28 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/kernfs/inode.c Name:__kernfs_setattr]
Results: map[SourceCode: 68: int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
69: {
70: struct kernfs_iattrs *attrs;
71: unsigned int ia_valid = iattr->ia_valid;
72:
73: attrs = kernfs_iattrs(kn);
74: if (!attrs)
75: return -ENOMEM;
76:
77: if (ia_valid & ATTR_UID)
78: attrs->ia_uid = iattr->ia_uid;
79: if (ia_valid & ATTR_GID)
80: attrs->ia_gid = iattr->ia_gid;
81: if (ia_valid & ATTR_ATIME)
82: attrs->ia_atime = iattr->ia_atime;
83: if (ia_valid & ATTR_MTIME)
84: attrs->ia_mtime = iattr->ia_mtime;
85: if (ia_valid & ATTR_CTIME)
86: attrs->ia_ctime = iattr->ia_ctime;
87: if (ia_valid & ATTR_MODE)
88: kn->mode = iattr->ia_mode;
89: return 0;
90: }
SourceFile:fs/kernfs/inode.c]
|
| 49/2 |
2026/11/18 07:28 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/kernfs/inode.c Name:kernfs_iop_setattr]
Results: map[SourceCode: 110: int kernfs_iop_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
111: struct iattr *iattr)
112: {
113: struct inode *inode = d_inode(dentry);
114: struct kernfs_node *kn = inode->i_private;
115: struct kernfs_root *root;
116: int error;
117:
118: if (!kn)
119: return -EINVAL;
120:
121: root = kernfs_root(kn);
122: down_write(&root->kernfs_iattr_rwsem);
123: error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
124: if (error)
125: goto out;
126:
127: error = __kernfs_setattr(kn, iattr);
128: if (error)
129: goto out;
130:
131: /* this ignores size changes */
132: setattr_copy(&nop_mnt_idmap, inode, iattr);
133:
134: out:
135: up_write(&root->kernfs_iattr_rwsem);
136: return error;
137: }
SourceFile:fs/kernfs/inode.c]
|
| 50/2 |
2026/11/18 07:28 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 53998
output: 84
thoughts: 291
Thoughts: **An Unexpected Realization Regarding KCSAN Value Change Reporting**
Wait a second! I need to revisit `kernel/kcsan/report.c`, specifically lines 180-195. This section is crucial for understanding how KCSAN handles `skip_report` when `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY` is enabled. I'm re-reading it very carefully to grasp the nuances of the six different cases presented. It seems there's a distinction being made based on whether the `value_change` flag is `TRUE` or `MAYBE`, and whether the conflict involves a read watchpoint or a write watchpoint, and whether the conflicting access is a write or a read.
Specifically, I'm focusing on the logic that determines whether a report is generated or skipped. The initial call to `skip_report` always has `value_change == TRUE` because we lack prior knowledge of the instrumented access's value. However, for the *second* call, the decision branches significantly. I need to trace each of the six listed scenarios:
1. Read watchpoint encountering a conflicting write where `value_change` is `TRUE` – this should trigger a report.
2. Read watchpoint encountering a conflicting write where `value_change` is `MAYBE` – this should be skipped.
3. Write watchpoint encountering a conflicting write where `value_change` is `TRUE` – this should trigger a report.
4. Write watchpoint encountering a conflicting write where `value_change` is `MAYBE` – this should be skipped.
5. Write watchpoint encountering a conflicting read where `value_change` is `MAYBE` – this should be skipped.
6. Write watchpoint encountering a conflicting read where `value_change` is `TRUE` – this should trigger a report.
Simultaneously, I'm also examining how KCSAN actually formats these reports, by reading lines 350 to 550 of the same `kernel/kcsan/report.c` file. This is important because the *presentation* of the report, especially any value-related information, is directly tied to the logic I'm scrutinizing in the `skip_report` section. Understanding the reporting format will help confirm my interpretation of when and why certain conflicts are deemed reportable under the `REPORT_VALUE_CHANGE_ONLY` configuration. This feels like a critical intersection where the decision to report and the content of that report are intricately linked.
|
| 51/2 |
2026/11/18 07:29 |
tool |
read-file |
0m
Args: map[File:kernel/kcsan/report.c FirstLine:350 LineCount:100]
Results: map[Contents: 350: unsigned long *replaced)
351: {
352: return ip ? replace_stack_entry(stack_entries, num_entries, ip, replaced) :
353: get_stack_skipnr(stack_entries, num_entries);
354: }
355:
356: /* Compares symbolized strings of addr1 and addr2. */
357: static int sym_strcmp(void *addr1, void *addr2)
358: {
359: char buf1[64];
360: char buf2[64];
361:
362: snprintf(buf1, sizeof(buf1), "%pS", addr1);
363: snprintf(buf2, sizeof(buf2), "%pS", addr2);
364:
365: return strncmp(buf1, buf2, sizeof(buf1));
366: }
367:
368: static void
369: print_stack_trace(unsigned long stack_entries[], int num_entries, unsigned long reordered_to)
370: __must_hold(&report_lock)
371: {
372: stack_trace_print(stack_entries, num_entries, 0);
373: if (reordered_to)
374: pr_err(" |\n +-> reordered to: %pS\n", (void *)reordered_to);
375: }
376:
377: static void print_verbose_info(struct task_struct *task)
378: __must_hold(&report_lock)
379: {
380: if (!task)
381: return;
382:
383: /* Restore IRQ state trace for printing. */
384: kcsan_restore_irqtrace(task);
385:
386: pr_err("\n");
387: debug_show_held_locks(task);
388: print_irqtrace_events(task);
389: }
390:
391: static void print_report(enum kcsan_value_change value_change,
392: const struct access_info *ai,
393: struct other_info *other_info,
394: u64 old, u64 new, u64 mask)
395: __must_hold(&report_lock)
396: {
397: unsigned long reordered_to = 0;
398: unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 };
399: int num_stack_entries = stack_trace_save(stack_entries, NUM_STACK_ENTRIES, 1);
400: int skipnr = sanitize_stack_entries(stack_entries, num_stack_entries, ai->ip, &reordered_to);
401: unsigned long this_frame = stack_entries[skipnr];
402: unsigned long other_reordered_to = 0;
403: unsigned long other_frame = 0;
404: int other_skipnr = 0; /* silence uninit warnings */
405:
406: /*
407: * Must check report filter rules before starting to print.
408: */
409: if (skip_report(KCSAN_VALUE_CHANGE_TRUE, stack_entries[skipnr]))
410: return;
411:
412: if (other_info) {
413: other_skipnr = sanitize_stack_entries(other_info->stack_entries,
414: other_info->num_stack_entries,
415: other_info->ai.ip, &other_reordered_to);
416: other_frame = other_info->stack_entries[other_skipnr];
417:
418: /* @value_change is only known for the other thread */
419: if (skip_report(value_change, other_frame))
420: return;
421: }
422:
423: if (rate_limit_report(this_frame, other_frame))
424: return;
425:
426: /* Print report header. */
427: pr_err("==================================================================\n");
428: if (other_info) {
429: int cmp;
430:
431: /*
432: * Order functions lexographically for consistent bug titles.
433: * Do not print offset of functions to keep title short.
434: */
435: cmp = sym_strcmp((void *)other_frame, (void *)this_frame);
436: pr_err("BUG: KCSAN: %s in %ps / %ps\n",
437: get_bug_type(ai->access_type | other_info->ai.access_type),
438: (void *)(cmp < 0 ? other_frame : this_frame),
439: (void *)(cmp < 0 ? this_frame : other_frame));
440: } else {
441: pr_err("BUG: KCSAN: %s in %pS\n", get_bug_type(ai->access_type),
442: (void *)this_frame);
443: }
444:
445: pr_err("\n");
446:
447: /* Print information about the racing accesses. */
448: if (other_info) {
449: pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
]
|
| 52/2 |
2026/11/18 07:29 |
tool |
read-file |
0m
Args: map[File:kernel/kcsan/report.c FirstLine:450 LineCount:100]
Results: map[Contents: 450: get_access_type(other_info->ai.access_type), other_info->ai.ptr,
451: other_info->ai.size, get_thread_desc(other_info->ai.task_pid),
452: other_info->ai.cpu_id);
453:
454: /* Print the other thread's stack trace. */
455: print_stack_trace(other_info->stack_entries + other_skipnr,
456: other_info->num_stack_entries - other_skipnr,
457: other_reordered_to);
458: if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
459: print_verbose_info(other_info->task);
460:
461: pr_err("\n");
462: pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
463: get_access_type(ai->access_type), ai->ptr, ai->size,
464: get_thread_desc(ai->task_pid), ai->cpu_id);
465: } else {
466: pr_err("race at unknown origin, with %s to 0x%px of %zu bytes by %s on cpu %i:\n",
467: get_access_type(ai->access_type), ai->ptr, ai->size,
468: get_thread_desc(ai->task_pid), ai->cpu_id);
469: }
470: /* Print stack trace of this thread. */
471: print_stack_trace(stack_entries + skipnr, num_stack_entries - skipnr, reordered_to);
472: if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
473: print_verbose_info(current);
474:
475: /* Print observed value change. */
476: if (ai->size <= 8) {
477: int hex_len = ai->size * 2;
478: u64 diff = old ^ new;
479:
480: if (mask)
481: diff &= mask;
482: if (diff) {
483: pr_err("\n");
484: pr_err("value changed: 0x%0*llx -> 0x%0*llx\n",
485: hex_len, old, hex_len, new);
486: if (mask) {
487: pr_err(" bits changed: 0x%0*llx with mask 0x%0*llx\n",
488: hex_len, diff, hex_len, mask);
489: }
490: }
491: }
492:
493: /* Print report footer. */
494: pr_err("\n");
495: pr_err("Reported by Kernel Concurrency Sanitizer on:\n");
496: dump_stack_print_info(KERN_DEFAULT);
497: pr_err("==================================================================\n");
498:
499: check_panic_on_warn("KCSAN");
500: }
501:
502: static void release_report(unsigned long *flags, struct other_info *other_info)
503: __releases(&report_lock)
504: {
505: /*
506: * Use size to denote valid/invalid, since KCSAN entirely ignores
507: * 0-sized accesses.
508: */
509: other_info->ai.size = 0;
510: raw_spin_unlock_irqrestore(&report_lock, *flags);
511: }
512:
513: /*
514: * Sets @other_info->task and awaits consumption of @other_info.
515: */
516: static void set_other_info_task_blocking(unsigned long *flags,
517: const struct access_info *ai,
518: struct other_info *other_info)
519: __must_hold(&report_lock)
520: {
521: /*
522: * We may be instrumenting a code-path where current->state is already
523: * something other than TASK_RUNNING.
524: */
525: const bool is_running = task_is_running(current);
526: /*
527: * To avoid deadlock in case we are in an interrupt here and this is a
528: * race with a task on the same CPU (KCSAN_INTERRUPT_WATCHER), provide a
529: * timeout to ensure this works in all contexts.
530: *
531: * Await approximately the worst case delay of the reporting thread (if
532: * we are not interrupted).
533: */
534: int timeout = max(kcsan_udelay_task, kcsan_udelay_interrupt);
535:
536: other_info->task = current;
537: do {
538: if (is_running) {
539: /*
540: * Let lockdep know the real task is sleeping, to print
541: * the held locks (recall we turned lockdep off, so
542: * locking/unlocking @report_lock won't be recorded).
543: */
544: set_current_state(TASK_UNINTERRUPTIBLE);
545: }
546: raw_spin_unlock_irqrestore(&report_lock, *flags);
547: /*
548: * We cannot call schedule() since we also cannot reliably
549: * determine if sleeping here is permitted -- see in_atomic().
]
|
| 53/2 |
2026/11/18 07:29 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 57645
output: 42
thoughts: 323
Thoughts: **Eureka! I've Found the Missing Piece!**
I've just had a significant breakthrough. I was poring over the KCSAN reporting mechanism, specifically looking at the output in our reports, and something felt off. I noticed the absence of a "value changed:" line, which is crucial for understanding certain race conditions. My suspicion led me to `kernel/kcsan/report.c`, lines 475-491. This code block clearly shows the logic for printing observed value changes, including the size, old value, new value, and even a mask for specific bits if they've changed.
The immediate question that sprang to mind was: why isn't this information appearing in our reports? My investigative path then led me to the KCSAN configuration options. I recalled that `CONFIG_KCSAN_STRICT=y` is currently enabled. This configuration choice effectively disables `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY`. To confirm this, I immediately planned to cross-reference this with the relevant Kconfig files – specifically `kernel/configs/kcsan.config`, syzkaller configurations, and, most importantly, `lib/Kconfig.kcsan` between lines 150 and 200. This is where the conditional logic for value change reporting is defined, and I'm confident this is the root cause of the missing output. This is the key to understanding why those specific details are being suppressed.
|
| 54/2 |
2026/11/18 07:30 |
tool |
read-file |
0m
Args: map[File:lib/Kconfig.kcsan FirstLine:150 LineCount:50]
Results: map[Contents: 150: Currently disabled by default, because not all safe per-CPU access
151: primitives and patterns may be accounted for, and therefore could
152: result in false positives.
153:
154: config KCSAN_REPORT_ONCE_IN_MS
155: int "Duration in milliseconds, in which any given race is only reported once"
156: default 3000
157: help
158: Any given race is only reported once in the defined time window.
159: Different races may still generate reports within a duration that is
160: smaller than the duration defined here. This allows rate limiting
161: reporting to avoid flooding the console with reports. Setting this
162: to 0 disables rate limiting.
163:
164: # The main purpose of the below options is to control reported data races, and
165: # are not expected to be switched frequently by non-testers or at runtime.
166: # The defaults are chosen to be conservative, and can miss certain bugs.
167:
168: config KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
169: bool "Report races of unknown origin"
170: default y
171: help
172: If KCSAN should report races where only one access is known, and the
173: conflicting access is of unknown origin. This type of race is
174: reported if it was only possible to infer a race due to a data value
175: change while an access is being delayed on a watchpoint.
176:
177: config KCSAN_STRICT
178: bool "Strict data-race checking"
179: help
180: KCSAN will report data races with the strictest possible rules, which
181: closely aligns with the rules defined by the Linux-kernel memory
182: consistency model (LKMM).
183:
184: config KCSAN_WEAK_MEMORY
185: bool "Enable weak memory modeling to detect missing memory barriers"
186: default y
187: depends on KCSAN_STRICT
188: help
189: Enable support for modeling a subset of weak memory, which allows
190: detecting a subset of data races due to missing memory barriers.
191:
192: Depends on KCSAN_STRICT, because the options strengthening certain
193: plain accesses by default (depending on !KCSAN_STRICT) reduce the
194: ability to detect any data races invoving reordered accesses, in
195: particular reordered writes.
196:
197: Weak memory modeling relies on additional instrumentation and may
198: affect performance.
199:
]
|
| 55/2 |
2026/11/18 07:30 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 58808
output: 152
thoughts: 782
Thoughts: **KCSAN Strict Mode and a Potentially Old `kernfs_refresh_inode` Race Condition**
Whoa, wait a minute! KCSAN is running with `CONFIG_KCSAN_STRICT=y`, which is a huge detail. And I just noticed that `value changed:` wasn't printed because the `diff` was 0. This means the value written to `inode->i_mode` was the *exact same value* that was already there – `inode->i_mode = kn->mode`, where they were already equal. This is really throwing me for a loop.
Hold everything! I need to stop and think this through. Why is `kernfs_refresh_inode` even writing to `inode->i_mode` on *every* `getattr()` and `permission()` call? And has it *always* done this, or did something change recently? Or has it been like this for years?
Before I jump to any conclusions, I absolutely need to investigate the history. I have to check *all* commits related to `kernfs_refresh_inode`, `sysfs_refresh_inode`, `kernfs_iop_getattr`, `dnotify_flush`, `filp_flush`, and even `inode->i_mode` itself, across the entire history of the kernel.
My primary question is: could this potential race condition have existed for a long time, or was it introduced recently, or even a few years ago? I need to explore *all* possibilities.
I'm also trying to understand *why* `dnotify_flush` and `kernfs_iop_getattr` are happening in this specific context. I need to trace *both* stack traces line by line, carefully examining the code.
For Stack Trace 1 (Read), involving `dnotify_flush`:
I'm reading `fs/open.c` around line 1549 to understand how `filp_flush` and `filp_close_sync` interact with `dnotify_flush`.
For Stack Trace 2 (Write), involving `kernfs_refresh_inode` and `kernfs_iop_getattr`:
I'm focusing on `fs/stat.c` around line 217 (for `vfs_getattr_nosec`) and then I'll dive completely into `fs/kernfs/inode.c` to understand the behavior of `kernfs_refresh_inode` and `kernfs_iop_getattr` in detail.
|
| 56/2 |
2026/11/18 07:30 |
tool |
read-file |
0m
Args: map[File:fs/open.c FirstLine:1525 LineCount:80]
Results: map[Contents:1525: if (force_o_largefile())
1526: flags |= O_LARGEFILE;
1527: return do_sys_open(AT_FDCWD, pathname, flags, mode);
1528: }
1529: #endif
1530:
1531: /*
1532: * "id" is the POSIX thread ID. We use the
1533: * files pointer for this..
1534: */
1535: static int filp_flush(struct file *filp, fl_owner_t id)
1536: {
1537: int retval = 0;
1538:
1539: if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,
1540: "VFS: Close: file count is 0 (f_op=%ps)",
1541: filp->f_op)) {
1542: return 0;
1543: }
1544:
1545: if (filp->f_op->flush)
1546: retval = filp->f_op->flush(filp, id);
1547:
1548: if (likely(!(filp->f_mode & FMODE_PATH))) {
1549: dnotify_flush(filp, id);
1550: locks_remove_posix(filp, id);
1551: }
1552: return retval;
1553: }
1554:
1555: int filp_close(struct file *filp, fl_owner_t id)
1556: {
1557: int retval;
1558:
1559: retval = filp_flush(filp, id);
1560: fput_close(filp);
1561:
1562: return retval;
1563: }
1564: EXPORT_SYMBOL(filp_close);
1565:
1566: /* Like filp_close() but the last reference is put right here. */
1567: int filp_close_sync(struct file *filp, fl_owner_t id)
1568: {
1569: int retval;
1570:
1571: /* Kernel threads must never put their final reference here. */
1572: VFS_WARN_ON_ONCE(current->flags & PF_KTHREAD);
1573: retval = filp_flush(filp, id);
1574: fput_close_sync(filp);
1575:
1576: return retval;
1577: }
1578:
1579: /*
1580: * Careful here! We test whether the file pointer is NULL before
1581: * releasing the fd. This ensures that one clone task can't release
1582: * an fd while another clone is opening it.
1583: */
1584: SYSCALL_DEFINE1(close, unsigned int, fd)
1585: {
1586: int retval;
1587: struct file *file;
1588:
1589: file = file_close_fd(fd);
1590: if (!file)
1591: return -EBADF;
1592:
1593: /*
1594: * We're returning to user space. Don't bother
1595: * with any delayed fput() cases.
1596: */
1597: retval = filp_close_sync(file, current->files);
1598:
1599: if (likely(retval == 0))
1600: return 0;
1601:
1602: /* can't restart close syscall because file table entry was cleared */
1603: if (retval == -ERESTARTSYS ||
1604: retval == -ERESTARTNOINTR ||
]
|
| 57/2 |
2026/11/18 07:30 |
tool |
read-file |
0m
Args: map[File:fs/stat.c FirstLine:190 LineCount:100]
Results: map[Contents: 190:
191: /* allow the fs to override these if it really wants to */
192: /* SB_NOATIME means filesystem supplies dummy atime value */
193: if (inode->i_sb->s_flags & SB_NOATIME)
194: stat->result_mask &= ~STATX_ATIME;
195:
196: /*
197: * Note: If you add another clause to set an attribute flag, please
198: * update attributes_mask below.
199: */
200: if (IS_AUTOMOUNT(inode))
201: stat->attributes |= STATX_ATTR_AUTOMOUNT;
202:
203: if (IS_DAX(inode))
204: stat->attributes |= STATX_ATTR_DAX;
205:
206: if (IS_VERITY(inode))
207: stat->attributes |= STATX_ATTR_VERITY;
208:
209: stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
210: STATX_ATTR_DAX |
211: STATX_ATTR_VERITY);
212:
213: idmap = mnt_idmap(path->mnt);
214: if (inode->i_op->getattr) {
215: int ret;
216:
217: ret = inode->i_op->getattr(idmap, path, stat, request_mask,
218: query_flags);
219: if (ret)
220: return ret;
221: } else {
222: generic_fillattr(idmap, request_mask, inode, stat);
223: }
224:
225: /*
226: * If this is a block device inode, override the filesystem attributes
227: * with the block device specific parameters that need to be obtained
228: * from the bdev backing inode.
229: */
230: if (S_ISBLK(stat->mode))
231: bdev_statx(path, stat, request_mask);
232:
233: return 0;
234: }
235: EXPORT_SYMBOL(vfs_getattr_nosec);
236:
237: /*
238: * vfs_getattr - Get the enhanced basic attributes of a file
239: * @path: The file of interest
240: * @stat: Where to return the statistics
241: * @request_mask: STATX_xxx flags indicating what the caller wants
242: * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
243: *
244: * Ask the filesystem for a file's attributes. The caller must indicate in
245: * request_mask and query_flags to indicate what they want.
246: *
247: * If the file is remote, the filesystem can be forced to update the attributes
248: * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
249: * suppress the update by passing AT_STATX_DONT_SYNC.
250: *
251: * Bits must have been set in request_mask to indicate which attributes the
252: * caller wants retrieving. Any such attribute not requested may be returned
253: * anyway, but the value may be approximate, and, if remote, may not have been
254: * synchronised with the server.
255: *
256: * 0 will be returned on success, and a -ve error code if unsuccessful.
257: */
258: int vfs_getattr(const struct path *path, struct kstat *stat,
259: u32 request_mask, unsigned int query_flags)
260: {
261: int retval;
262:
263: retval = security_inode_getattr(path);
264: if (unlikely(retval))
265: return retval;
266: return vfs_getattr_nosec(path, stat, request_mask, query_flags);
267: }
268: EXPORT_SYMBOL(vfs_getattr);
269:
270: /**
271: * vfs_fstat - Get the basic attributes by file descriptor
272: * @fd: The file descriptor referring to the file of interest
273: * @stat: The result structure to fill in.
274: *
275: * This function is a wrapper around vfs_getattr(). The main difference is
276: * that it uses a file descriptor to determine the file location.
277: *
278: * 0 will be returned on success, and a -ve error code if unsuccessful.
279: */
280: int vfs_fstat(int fd, struct kstat *stat)
281: {
282: CLASS(fd_raw, f)(fd);
283: if (fd_empty(f))
284: return -EBADF;
285: return vfs_getattr(&fd_file(f)->f_path, stat, STATX_BASIC_STATS, 0);
286: }
287:
288: static int statx_lookup_flags(int flags)
289: {
]
|
| 58/2 |
2026/11/18 07:30 |
tool |
read-file |
0m
Args: map[File:fs/kernfs/inode.c FirstLine:1 LineCount:100]
Results: map[Contents: 1: // SPDX-License-Identifier: GPL-2.0-only
2: /*
3: * fs/kernfs/inode.c - kernfs inode implementation
4: *
5: * Copyright (c) 2001-3 Patrick Mochel
6: * Copyright (c) 2007 SUSE Linux Products GmbH
7: * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
8: */
9:
10: #include <linux/pagemap.h>
11: #include <linux/backing-dev.h>
12: #include <linux/capability.h>
13: #include <linux/errno.h>
14: #include <linux/slab.h>
15: #include <linux/xattr.h>
16: #include <linux/security.h>
17:
18: #include "kernfs-internal.h"
19:
20: static const struct inode_operations kernfs_iops = {
21: .permission = kernfs_iop_permission,
22: .setattr = kernfs_iop_setattr,
23: .getattr = kernfs_iop_getattr,
24: .listxattr = kernfs_iop_listxattr,
25: };
26:
27: static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, bool alloc)
28: {
29: struct kernfs_iattrs *ret __free(kfree) = NULL;
30: struct kernfs_iattrs *attr;
31:
32: attr = READ_ONCE(kn->iattr);
33: if (attr || !alloc)
34: return attr;
35:
36: ret = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
37: if (!ret)
38: return NULL;
39:
40: INIT_LIST_HEAD_RCU(&ret->xattrs);
41: /* assign default attributes */
42: ret->ia_uid = GLOBAL_ROOT_UID;
43: ret->ia_gid = GLOBAL_ROOT_GID;
44:
45: ktime_get_real_ts64(&ret->ia_atime);
46: ret->ia_mtime = ret->ia_atime;
47: ret->ia_ctime = ret->ia_atime;
48:
49: simple_xattr_limits_init(&ret->xattr_limits);
50:
51: /* If someone raced us, recognize it. */
52: if (!try_cmpxchg(&kn->iattr, &attr, ret))
53: return READ_ONCE(kn->iattr);
54:
55: return no_free_ptr(ret);
56: }
57:
58: static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
59: {
60: return __kernfs_iattrs(kn, true);
61: }
62:
63: static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
64: {
65: return __kernfs_iattrs(kn, false);
66: }
67:
68: int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
69: {
70: struct kernfs_iattrs *attrs;
71: unsigned int ia_valid = iattr->ia_valid;
72:
73: attrs = kernfs_iattrs(kn);
74: if (!attrs)
75: return -ENOMEM;
76:
77: if (ia_valid & ATTR_UID)
78: attrs->ia_uid = iattr->ia_uid;
79: if (ia_valid & ATTR_GID)
80: attrs->ia_gid = iattr->ia_gid;
81: if (ia_valid & ATTR_ATIME)
82: attrs->ia_atime = iattr->ia_atime;
83: if (ia_valid & ATTR_MTIME)
84: attrs->ia_mtime = iattr->ia_mtime;
85: if (ia_valid & ATTR_CTIME)
86: attrs->ia_ctime = iattr->ia_ctime;
87: if (ia_valid & ATTR_MODE)
88: kn->mode = iattr->ia_mode;
89: return 0;
90: }
91:
92: /**
93: * kernfs_setattr - set iattr on a node
94: * @kn: target node
95: * @iattr: iattr to set
96: *
97: * Return: %0 on success, -errno on failure.
98: */
99: int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
100: {
]
|
| 59/2 |
2026/11/18 07:30 |
tool |
read-file |
0m
Args: map[File:fs/kernfs/inode.c FirstLine:101 LineCount:100]
Results: map[Contents: 101: int ret;
102: struct kernfs_root *root = kernfs_root(kn);
103:
104: down_write(&root->kernfs_iattr_rwsem);
105: ret = __kernfs_setattr(kn, iattr);
106: up_write(&root->kernfs_iattr_rwsem);
107: return ret;
108: }
109:
110: int kernfs_iop_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
111: struct iattr *iattr)
112: {
113: struct inode *inode = d_inode(dentry);
114: struct kernfs_node *kn = inode->i_private;
115: struct kernfs_root *root;
116: int error;
117:
118: if (!kn)
119: return -EINVAL;
120:
121: root = kernfs_root(kn);
122: down_write(&root->kernfs_iattr_rwsem);
123: error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
124: if (error)
125: goto out;
126:
127: error = __kernfs_setattr(kn, iattr);
128: if (error)
129: goto out;
130:
131: /* this ignores size changes */
132: setattr_copy(&nop_mnt_idmap, inode, iattr);
133:
134: out:
135: up_write(&root->kernfs_iattr_rwsem);
136: return error;
137: }
138:
139: ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
140: {
141: struct kernfs_node *kn = kernfs_dentry_node(dentry);
142: struct kernfs_iattrs *attrs;
143:
144: attrs = kernfs_iattrs_noalloc(kn);
145:
146: return simple_xattr_list(d_inode(dentry), attrs ? &attrs->xattrs : NULL, buf, size);
147: }
148:
149: static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
150: {
151: inode->i_mode = mode;
152: simple_inode_init_ts(inode);
153: }
154:
155: static inline void set_inode_attr(struct inode *inode,
156: struct kernfs_iattrs *attrs)
157: {
158: inode->i_uid = attrs->ia_uid;
159: inode->i_gid = attrs->ia_gid;
160: inode_set_atime_to_ts(inode, attrs->ia_atime);
161: inode_set_mtime_to_ts(inode, attrs->ia_mtime);
162: inode_set_ctime_to_ts(inode, attrs->ia_ctime);
163: }
164:
165: static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
166: {
167: struct kernfs_iattrs *attrs;
168:
169: inode->i_mode = kn->mode;
170: attrs = kernfs_iattrs_noalloc(kn);
171: if (attrs)
172: /*
173: * kernfs_node has non-default attributes get them from
174: * persistent copy in kernfs_node.
175: */
176: set_inode_attr(inode, attrs);
177:
178: if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING))
179: set_nlink(inode, kn->dir.subdirs + 2);
180: }
181:
182: int kernfs_iop_getattr(const struct mnt_idmap *idmap,
183: const struct path *path, struct kstat *stat,
184: u32 request_mask, unsigned int query_flags)
185: {
186: struct inode *inode = d_inode(path->dentry);
187: struct kernfs_node *kn = inode->i_private;
188: struct kernfs_root *root = kernfs_root(kn);
189:
190: down_read(&root->kernfs_iattr_rwsem);
191: kernfs_refresh_inode(kn, inode);
192: generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
193: up_read(&root->kernfs_iattr_rwsem);
194:
195: return 0;
196: }
197:
198: static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
199: {
200: kernfs_get(kn);
]
|
| 60/2 |
2026/11/18 07:30 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 65367
output: 84
thoughts: 123
Thoughts: **My Intense Realization and Debugging Dive**
Okay, hold on a minute. Something's not right here. I need to re-examine that KCSAN report. I'm seeing a data race, but the crucial `value changed:` information isn't showing up. This is a critical piece of context for understanding the race condition, so its absence is a red flag.
My immediate thought is to dive straight into the KCSAN source code, specifically `kernel/kcsan/core.c`. I need to understand the exact conditions under which `print_report` is invoked. More importantly, I need to get a solid grasp on how the `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY` configuration option influences this behavior. Does it *filter out* reports that don't involve a value change, or does it affect how the value change itself is logged?
To clarify this, I'll be scrutinizing the code flow around key functions like `kcsan_report_set_info`, `kcsan_found_watchpoint`, and `setup_watchpoint`. Understanding the logic within these functions, particularly how they interact with the reporting mechanism and how watchpoints are established and potentially updated, will be crucial for pinpointing why the `value changed:` output is missing in my current scenario. This is where I expect to find the answer.
|
| 61/2 |
2026/11/18 07:30 |
tool |
read-file |
0m
Args: map[File:kernel/kcsan/core.c FirstLine:370 LineCount:100]
Results: map[Contents: 370: #endif
371: }
372:
373: static __always_inline int get_kcsan_stack_depth(void)
374: {
375: #ifdef CONFIG_KCSAN_WEAK_MEMORY
376: return current->kcsan_stack_depth;
377: #else
378: BUILD_BUG();
379: return 0;
380: #endif
381: }
382:
383: static __always_inline void add_kcsan_stack_depth(int val)
384: {
385: #ifdef CONFIG_KCSAN_WEAK_MEMORY
386: current->kcsan_stack_depth += val;
387: #else
388: BUILD_BUG();
389: #endif
390: }
391:
392: static __always_inline struct kcsan_scoped_access *get_reorder_access(struct kcsan_ctx *ctx)
393: {
394: #ifdef CONFIG_KCSAN_WEAK_MEMORY
395: return ctx->disable_scoped ? NULL : &ctx->reorder_access;
396: #else
397: return NULL;
398: #endif
399: }
400:
401: static __always_inline bool
402: find_reorder_access(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size,
403: int type, unsigned long ip)
404: {
405: struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
406:
407: if (!reorder_access)
408: return false;
409:
410: /*
411: * Note: If accesses are repeated while reorder_access is identical,
412: * never matches the new access, because !(type & KCSAN_ACCESS_SCOPED).
413: */
414: return reorder_access->ptr == ptr && reorder_access->size == size &&
415: reorder_access->type == type && reorder_access->ip == ip;
416: }
417:
418: static inline void
419: set_reorder_access(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size,
420: int type, unsigned long ip)
421: {
422: struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
423:
424: if (!reorder_access || !kcsan_weak_memory)
425: return;
426:
427: /*
428: * To avoid nested interrupts or scheduler (which share kcsan_ctx)
429: * reading an inconsistent reorder_access, ensure that the below has
430: * exclusive access to reorder_access by disallowing concurrent use.
431: */
432: ctx->disable_scoped++;
433: barrier();
434: reorder_access->ptr = ptr;
435: reorder_access->size = size;
436: reorder_access->type = type | KCSAN_ACCESS_SCOPED;
437: reorder_access->ip = ip;
438: reorder_access->stack_depth = get_kcsan_stack_depth();
439: barrier();
440: ctx->disable_scoped--;
441: }
442:
443: /*
444: * Pull everything together: check_access() below contains the performance
445: * critical operations; the fast-path (including check_access) functions should
446: * all be inlinable by the instrumentation functions.
447: *
448: * The slow-path (kcsan_found_watchpoint, kcsan_setup_watchpoint) are
449: * non-inlinable -- note that, we prefix these with "kcsan_" to ensure they can
450: * be filtered from the stacktrace, as well as give them unique names for the
451: * UACCESS whitelist of objtool. Each function uses user_access_save/restore(),
452: * since they do not access any user memory, but instrumentation is still
453: * emitted in UACCESS regions.
454: */
455:
456: static noinline void kcsan_found_watchpoint(const volatile void *ptr,
457: size_t size,
458: int type,
459: unsigned long ip,
460: atomic_long_t *watchpoint,
461: long encoded_watchpoint)
462: {
463: const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
464: struct kcsan_ctx *ctx = get_ctx();
465: unsigned long flags;
466: bool consumed;
467:
468: /*
469: * We know a watchpoint exists. Let's try to keep the race-window
]
|
| 62/2 |
2026/11/18 07:30 |
tool |
read-file |
0m
Args: map[File:kernel/kcsan/core.c FirstLine:470 LineCount:100]
Results: map[Contents: 470: * between here and finally consuming the watchpoint below as small as
471: * possible -- avoid unneccessarily complex code until consumed.
472: */
473:
474: if (!kcsan_is_enabled(ctx))
475: return;
476:
477: /*
478: * The access_mask check relies on value-change comparison. To avoid
479: * reporting a race where e.g. the writer set up the watchpoint, but the
480: * reader has access_mask!=0, we have to ignore the found watchpoint.
481: *
482: * reorder_access is never created from an access with access_mask set.
483: */
484: if (ctx->access_mask && !find_reorder_access(ctx, ptr, size, type, ip))
485: return;
486:
487: /*
488: * If the other thread does not want to ignore the access, and there was
489: * a value change as a result of this thread's operation, we will still
490: * generate a report of unknown origin.
491: *
492: * Use CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n to filter.
493: */
494: if (!is_assert && kcsan_ignore_address(ptr))
495: return;
496:
497: /*
498: * Consuming the watchpoint must be guarded by kcsan_is_enabled() to
499: * avoid erroneously triggering reports if the context is disabled.
500: */
501: consumed = try_consume_watchpoint(watchpoint, encoded_watchpoint);
502:
503: /* keep this after try_consume_watchpoint */
504: flags = user_access_save();
505:
506: if (consumed) {
507: kcsan_save_irqtrace(current);
508: kcsan_report_set_info(ptr, size, type, ip, watchpoint - watchpoints);
509: kcsan_restore_irqtrace(current);
510: } else {
511: /*
512: * The other thread may not print any diagnostics, as it has
513: * already removed the watchpoint, or another thread consumed
514: * the watchpoint before this thread.
515: */
516: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_REPORT_RACES]);
517: }
518:
519: if (is_assert)
520: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
521: else
522: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_DATA_RACES]);
523:
524: user_access_restore(flags);
525: }
526:
527: static noinline void
528: kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned long ip)
529: {
530: const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0;
531: const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
532: atomic_long_t *watchpoint;
533: u64 old, new, diff;
534: enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE;
535: bool interrupt_watcher = kcsan_interrupt_watcher;
536: unsigned long ua_flags = user_access_save();
537: struct kcsan_ctx *ctx = get_ctx();
538: unsigned long access_mask = ctx->access_mask;
539: unsigned long irq_flags = 0;
540: bool is_reorder_access;
541:
542: /*
543: * Always reset kcsan_skip counter in slow-path to avoid underflow; see
544: * should_watch().
545: */
546: reset_kcsan_skip();
547:
548: if (!kcsan_is_enabled(ctx))
549: goto out;
550:
551: /*
552: * Check to-ignore addresses after kcsan_is_enabled(), as we may access
553: * memory that is not yet initialized during early boot.
554: */
555: if (!is_assert && kcsan_ignore_address(ptr))
556: goto out;
557:
558: if (!check_encodable((unsigned long)ptr, size)) {
559: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_UNENCODABLE_ACCESSES]);
560: goto out;
561: }
562:
563: /*
564: * The local CPU cannot observe reordering of its own accesses, and
565: * therefore we need to take care of 2 cases to avoid false positives:
566: *
567: * 1. Races of the reordered access with interrupts. To avoid, if
568: * the current access is reorder_access, disable interrupts.
569: * 2. Avoid races of scoped accesses from nested interrupts (below).
]
|
| 63/2 |
2026/11/18 07:30 |
llm |
cause-finder |
2m
Model:
Tokens:
input: 68655
output: 105
thoughts: 1051
Thoughts: **The Case of the Elusive `inode->i_mode` Race: A Deep Dive into KCSAN's Revelation**
Alright, let's break down this KCSAN report. It's flagging a race condition, and my initial reaction is a mix of "aha!" at the detection and a immediate need to dissect the *exact* mechanics. The report points to a read at `0xffff88810632faa0` by task 3286 and a write to the *exact same address* by task 3283. KCSAN caught this on CPU 1 and CPU 0 respectively.
The key players, according to KCSAN, are task 3283 (`udevd`, performing the write within `kernfs_refresh_inode`) and task 3286 (performing the read within `dnotify_flush`). This is where my thinking really kicks in. KCSAN identified task 3283 as setting up a WRITE watchpoint (`this_frame`), and task 3286 as consuming it (`other_info`).
Now, this is crucial: the watchpoint was a WRITE watchpoint, established *before* task 3283 executed its write to `inode->i_mode`. While task 3283 was effectively "sleeping" on that watchpoint, task 3286 performed a READ. The report notes that `old == new` during this watchpoint delay, leading to `value_change == KCSAN_VALUE_CHANGE_MAYBE`. Coupled with `CONFIG_KCSAN_STRICT=y` (and `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY=n`), this explains why KCSAN reported a "plain write + plain read" race, even though the value might not have *visibly* changed from the perspective of the watchpoint itself.
But wait. This is where I need to pause and *really* think. Why did this race occur? Is this a longstanding issue, or is it something newer? The critical question is: what is the precise concurrency bug here?
Let's examine the two sides of this reported race:
**Side A (The Writer):** The write to `inode->i_mode = kn->mode` happens within `kernfs_refresh_inode`. This function is called by `kernfs_iop_getattr` (and potentially `kernfs_iop_permission`). It's important to note the context: `kernfs_iop_getattr` acquires a read lock on `root->kernfs_iattr_rwsem` before calling `kernfs_refresh_inode`.
**Side B (The Reader):** The read in question is within `dnotify_flush`, where it accesses `inode->i_mode`.
Now, my mind immediately goes to the history. HOLD EVERYTHING. Why did this race manifest? Could it have been lurking since Linux 2.6.x? Or did a specific change introduce it? This requires a deep dive. I need to check the introduction of:
1. `sysfs_refresh_inode` (and its evolution to `kernfs_refresh_inode`).
2. `dnotify_flush`.
3. `kernfs_iop_getattr` (and its predecessor, `sysfs_getattr`).
More importantly, did `vfs_getattr`, `sysfs`, `kernfs`, `dnotify`, or operations on `inode->i_mode` *ever* share a protective lock that would prevent such a race? Or have they consistently operated without such shared synchronization?
This isn't a time for assumptions. I need to meticulously check the git history, dating back to at least `1da177e4c3f4` (around 2.6.12-rc2), and trace the evolution of `fs/sysfs/inode.c` and related files, specifically looking for any changes introduced by commit `ffed24e22845a` and before. Only by understanding the historical context and lock usage can I definitively pinpoint the introduction and nature of this race.
|
| 64/2 |
2026/11/18 07:33 |
tool |
git-log |
0m
Args: map[Count:40 PathPrefix:fs/sysfs/inode.c]
Results: map[Output:ffed24e22845 sysfs, kernfs: move inode code to fs/kernfs/inode.c
ccf73cf336dc sysfs, kernfs: introduce kernfs[_find_and]_get() and kernfs_put()
a7dc66dfb4c6 sysfs, kernfs: remove SYSFS_KOBJ_BIN_ATTR
471bd7b78bd5 sysfs, kernfs: add sysfs_dirent->s_attr.size
c6fb449515f2 sysfs, kernfs: prepare open, release, poll paths for kernfs
7c6e2d362c19 sysfs, kernfs: replace sysfs_dirent->s_dir.kobj and ->s_attr.[bin_]attr with ->priv
5d60418e5475 sysfs, kernfs: introduce kernfs_setattr()
7eed6ecb0785 sysfs: move sysfs_hash_and_remove() to fs/sysfs/dir.c
56b3f3b88465 sysfs: merge sysfs_elem_bin_attr into sysfs_elem_attr
3124eb1679b2 sysfs: merge regular and bin file handling
250f7c3fee52 sysfs: introduce [__]sysfs_remove()
d69ac5a0bbcf sysfs: remove sysfs_addrm_cxt->parent_sd
cfec0bc835c8 sysfs: @name comes before @ns
ddfd6d074e0f sysfs: fix up 80 column coding style issues
1b18dc2beb31 sysfs: fix up space coding style issues
ab9bf4be4dd5 sysfs: remove trailing whitespace
5240d58c0449 sysfs: kill sysfs_sb declaration in fs/sysfs/inode.c.
ab27b91b9f19 userns: Convert sysfs to use kgid/kuid where appropriate
dbd5768f87ff vfs: Rename end_writeback() to clear_inode()
54d20f006cef Revert "sysfs: Kill nlink counting."
93518dd2ebaf sysfs: Fix memory leak in sysfs_sd_setsecdata().
524b6c5b39b9 sysfs: Kill nlink counting.
ce597919361d sysfs: Complain bitterly about attempts to remove files from nonexistent directories.
faef2b6c9960 sysfs: propagate umode_t
bfe868486960 filesystems: add set_nlink()
23396180a977 sysfs: Remove support for tagged directories with untagged members.
7f9838fd0183 sysfs: count subdirectories
10556cb21a0d ->permission() sanitizing: don't pass flags to ->permission()
2830ba7f34eb ->permission() sanitizing: don't pass flags to generic_permission()
178ea73521d6 kill check_acl callback of generic_permission()
57cc7215b708 headers: kobject.h redux
b74c79e99389 fs: provide rcu-walk aware permission i_ops
01cd9fef6eb3 switch sysfs to ->evict_inode()
6a1a90ad1b0e rename generic_setattr
75de46b98dda fix setattr error handling in sysfs, configfs
3322e79a3860 fs: convert simple fs to new truncate
af10ec77b433 sysfs: Add support for tagged directories with untagged members.
3ff195b011d7 sysfs: Implement sysfs tagged directory support.
5a0e3ad6af86 include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
fac2622bbad4 sysfs: Pass super_block to sysfs_get_inode
]
|
| 65/2 |
2026/11/18 07:33 |
tool |
git-log |
10m
Error:
Args: map[CodeRegexp:refresh_inode Count:30]
Results:
|
| 66/2 |
2026/11/18 07:43 |
tool |
git-log |
0m
Args: map[Count:20 SourcePath:fs/notify/dnotify/dnotify.c SymbolName:dnotify_flush]
Results: map[Output:230d97d39ee2 fsnotify: create a wrapper fsnotify_find_inode_mark()
aabb45fdcb31 dnotify: use fsnotify group lock helpers
b1362edfe15b fsnotify: Remove fsnotify_find_{inode|vfsmount}_mark()
4712e722f914 fsnotify: get rid of fsnotify_destroy_mark_locked()
52f85729805b dnotify: replace dnotify_mark_mutex with mark mutex of dnotify_group
496ad9aa8ef4 new helper: file_inode(file)
e2a29943e9a2 fsnotify: pass group to fsnotify_destroy_mark()
43709a288ed0 fsnotify: remove group->mask
5444e2981c31 fsnotify: split generic and inode specific mark code
35566087099c fsnotify: take inode->i_lock inside fsnotify_find_mark_entry()
ef5e2b785fb3 dnotify: rename mark_entry to mark
d07754412f9c fsnotify: rename fsnotify_find_mark_entry to fsnotify_find_mark
e61ce86737b4 fsnotify: rename fsnotify_mark_entry to just fsnotify_mark
3c5119c05d62 dnotify: reimplement dnotify using fsnotify
0f7fc9e4d039 [PATCH] VFS: change struct file to use struct path
1da177e4c3f4 Linux-2.6.12-rc2
]
|
| 67/2 |
2026/11/18 07:43 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 71270
output: 47
thoughts: 55
Thoughts: **Following the Threads of `sysfs_refresh_inode`**
My current focus is to achieve a deeper understanding of the kernel's `sysfs` implementation, specifically how it handles inode refreshes. To that end, I'm diving into the `fs/sysfs/inode.c` file. My immediate goal is to meticulously trace the execution path and dependencies related to the `sysfs_refresh_inode` function. I'm employing `git log` with a specific regular expression, `CodeRegexp: "sysfs_refresh_inode"`, to pinpoint all commits that have touched or are directly related to this function. Furthermore, by specifying `PathPrefix: "fs/sysfs/inode.c"`, I'm ensuring that my search is narrowly scoped to this particular file, preventing noise from other parts of the `sysfs` subsystem or the kernel as a whole. This rigorous approach will allow me to reconstruct the evolution of `sysfs_refresh_inode`, understand the rationale behind its design choices, and identify any historical context that might be crucial for my current analysis.
|
| 68/2 |
2026/11/18 07:43 |
tool |
git-log |
0m
Args: map[CodeRegexp:sysfs_refresh_inode Count:20 PathPrefix:fs/sysfs/inode.c]
Results: map[Output:ffed24e22845 sysfs, kernfs: move inode code to fs/kernfs/inode.c
e61ab4ae48fb sysfs: Implement sysfs_getattr & sysfs_permission
]
|
| 69/2 |
2026/11/18 07:43 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 71441
output: 69
thoughts: 85
Thoughts: **Examining Recent Sysfs Enhancements**
Wow, this commit, `e61ab4ae48fb`, which introduces `sysfs_getattr` and `sysfs_permission`, immediately catches my eye. My immediate thought is to dive deep into this specific change. However, as an expert, I know that isolated examination can be misleading. Therefore, I need to contextualize this commit. This means not only meticulously reviewing `e61ab4ae48fb` itself, but also carefully analyzing *all* the commits that directly surround it. By doing so, I can understand the incremental nature of the changes, identify any dependencies or regressions introduced in nearby commits, and grasp the overall evolution of this sysfs functionality. Furthermore, I must cast a wider net to ensure there aren't any *other* seemingly unrelated commits, perhaps in different parts of the kernel tree, that might have a significant impact on or be impacted by these sysfs modifications. This comprehensive approach is crucial for a complete and accurate understanding of the system's current state and the implications of this particular development.
|
| 70/2 |
2026/11/18 07:44 |
tool |
git-show |
0m
Args:
Results: map[Output:commit e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Nov 20 16:08:53 2009 -0800
sysfs: Implement sysfs_getattr & sysfs_permission
With the implementation of sysfs_getattr and sysfs_permission
sysfs becomes able to lazily propogate inode attribute changes
from the sysfs_dirents to the vfs inodes. This paves the way
for deleting significant chunks of now unnecessary code.
While doing this we did not reference sysfs_setattr from
sysfs_symlink_inode_operations so I added along with
sysfs_getattr and sysfs_permission.
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 36ee6d817956..e319379d36db 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -800,7 +800,9 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
const struct inode_operations sysfs_dir_inode_operations = {
.lookup = sysfs_lookup,
+ .permission = sysfs_permission,
.setattr = sysfs_setattr,
+ .getattr = sysfs_getattr,
.setxattr = sysfs_setxattr,
};
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c
index 76e977c300f7..1ffd5559926f 100644
--- a/fs/sysfs/inode.c
+++ b/fs/sysfs/inode.c
@@ -37,7 +37,9 @@ static struct backing_dev_info sysfs_backing_dev_info = {
};
static const struct inode_operations sysfs_inode_operations ={
+ .permission = sysfs_permission,
.setattr = sysfs_setattr,
+ .getattr = sysfs_getattr,
.setxattr = sysfs_setxattr,
};
@@ -196,7 +198,6 @@ static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
{
- inode->i_mode = iattr->ia_mode;
inode->i_uid = iattr->ia_uid;
inode->i_gid = iattr->ia_gid;
inode->i_atime = iattr->ia_atime;
@@ -227,38 +228,56 @@ static int sysfs_count_nlink(struct sysfs_dirent *sd)
return nr + 2;
}
+static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
+{
+ struct sysfs_inode_attrs *iattrs = sd->s_iattr;
+
+ inode->i_mode = sd->s_mode;
+ if (iattrs) {
+ /* sysfs_dirent has non-default attributes
+ * get them from persistent copy in sysfs_dirent
+ */
+ set_inode_attr(inode, &iattrs->ia_iattr);
+ security_inode_notifysecctx(inode,
+ iattrs->ia_secdata,
+ iattrs->ia_secdata_len);
+ }
+
+ if (sysfs_type(sd) == SYSFS_DIR)
+ inode->i_nlink = sysfs_count_nlink(sd);
+}
+
+int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
+{
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ struct inode *inode = dentry->d_inode;
+
+ mutex_lock(&sysfs_mutex);
+ sysfs_refresh_inode(sd, inode);
+ mutex_unlock(&sysfs_mutex);
+
+ generic_fillattr(inode, stat);
+ return 0;
+}
+
static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
{
struct bin_attribute *bin_attr;
- struct sysfs_inode_attrs *iattrs;
inode->i_private = sysfs_get(sd);
inode->i_mapping->a_ops = &sysfs_aops;
inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
inode->i_op = &sysfs_inode_operations;
- inode->i_ino = sd->s_ino;
lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
- iattrs = sd->s_iattr;
- if (iattrs) {
- /* sysfs_dirent has non-default attributes
- * get them for the new inode from persistent copy
- * in sysfs_dirent
- */
- set_inode_attr(inode, &iattrs->ia_iattr);
- if (iattrs->ia_secdata)
- security_inode_notifysecctx(inode,
- iattrs->ia_secdata,
- iattrs->ia_secdata_len);
- } else
- set_default_inode_attr(inode, sd->s_mode);
+ set_default_inode_attr(inode, sd->s_mode);
+ sysfs_refresh_inode(sd, inode);
/* initialize inode according to type */
switch (sysfs_type(sd)) {
case SYSFS_DIR:
inode->i_op = &sysfs_dir_inode_operations;
inode->i_fop = &sysfs_dir_operations;
- inode->i_nlink = sysfs_count_nlink(sd);
break;
case SYSFS_KOBJ_ATTR:
inode->i_size = PAGE_SIZE;
@@ -341,3 +360,14 @@ int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
else
return -ENOENT;
}
+
+int sysfs_permission(struct inode *inode, int mask)
+{
+ struct sysfs_dirent *sd = inode->i_private;
+
+ mutex_lock(&sysfs_mutex);
+ sysfs_refresh_inode(sd, inode);
+ mutex_unlock(&sysfs_mutex);
+
+ return generic_permission(inode, mask, NULL);
+}
diff --git a/fs/sysfs/symlink.c b/fs/sysfs/symlink.c
index 11374182ca67..c5eff49fa41b 100644
--- a/fs/sysfs/symlink.c
+++ b/fs/sysfs/symlink.c
@@ -214,6 +214,9 @@ const struct inode_operations sysfs_symlink_inode_operations = {
.readlink = generic_readlink,
.follow_link = sysfs_follow_link,
.put_link = sysfs_put_link,
+ .setattr = sysfs_setattr,
+ .getattr = sysfs_getattr,
+ .permission = sysfs_permission,
};
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index a96d9678ae73..12ccc07459d8 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -156,7 +156,9 @@ static inline void __sysfs_put(struct sysfs_dirent *sd)
struct inode *sysfs_get_inode(struct sysfs_dirent *sd);
void sysfs_delete_inode(struct inode *inode);
int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr);
+int sysfs_permission(struct inode *inode, int mask);
int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
+int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
size_t size, int flags);
int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name);
]
|
| 71/2 |
2026/11/18 07:44 |
tool |
git-log |
0m
Args: map[Count:40 PathPrefix:fs/sysfs/inode.c Since:20 years]
Results: map[Output:ffed24e22845 sysfs, kernfs: move inode code to fs/kernfs/inode.c
ccf73cf336dc sysfs, kernfs: introduce kernfs[_find_and]_get() and kernfs_put()
a7dc66dfb4c6 sysfs, kernfs: remove SYSFS_KOBJ_BIN_ATTR
471bd7b78bd5 sysfs, kernfs: add sysfs_dirent->s_attr.size
c6fb449515f2 sysfs, kernfs: prepare open, release, poll paths for kernfs
7c6e2d362c19 sysfs, kernfs: replace sysfs_dirent->s_dir.kobj and ->s_attr.[bin_]attr with ->priv
5d60418e5475 sysfs, kernfs: introduce kernfs_setattr()
7eed6ecb0785 sysfs: move sysfs_hash_and_remove() to fs/sysfs/dir.c
56b3f3b88465 sysfs: merge sysfs_elem_bin_attr into sysfs_elem_attr
3124eb1679b2 sysfs: merge regular and bin file handling
250f7c3fee52 sysfs: introduce [__]sysfs_remove()
d69ac5a0bbcf sysfs: remove sysfs_addrm_cxt->parent_sd
cfec0bc835c8 sysfs: @name comes before @ns
ddfd6d074e0f sysfs: fix up 80 column coding style issues
1b18dc2beb31 sysfs: fix up space coding style issues
ab9bf4be4dd5 sysfs: remove trailing whitespace
5240d58c0449 sysfs: kill sysfs_sb declaration in fs/sysfs/inode.c.
ab27b91b9f19 userns: Convert sysfs to use kgid/kuid where appropriate
dbd5768f87ff vfs: Rename end_writeback() to clear_inode()
54d20f006cef Revert "sysfs: Kill nlink counting."
93518dd2ebaf sysfs: Fix memory leak in sysfs_sd_setsecdata().
524b6c5b39b9 sysfs: Kill nlink counting.
ce597919361d sysfs: Complain bitterly about attempts to remove files from nonexistent directories.
faef2b6c9960 sysfs: propagate umode_t
bfe868486960 filesystems: add set_nlink()
23396180a977 sysfs: Remove support for tagged directories with untagged members.
7f9838fd0183 sysfs: count subdirectories
10556cb21a0d ->permission() sanitizing: don't pass flags to ->permission()
2830ba7f34eb ->permission() sanitizing: don't pass flags to generic_permission()
178ea73521d6 kill check_acl callback of generic_permission()
57cc7215b708 headers: kobject.h redux
b74c79e99389 fs: provide rcu-walk aware permission i_ops
01cd9fef6eb3 switch sysfs to ->evict_inode()
6a1a90ad1b0e rename generic_setattr
75de46b98dda fix setattr error handling in sysfs, configfs
3322e79a3860 fs: convert simple fs to new truncate
af10ec77b433 sysfs: Add support for tagged directories with untagged members.
3ff195b011d7 sysfs: Implement sysfs tagged directory support.
5a0e3ad6af86 include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
fac2622bbad4 sysfs: Pass super_block to sysfs_get_inode
]
|
| 72/2 |
2026/11/18 07:44 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 74953
output: 83
thoughts: 556
Thoughts: **My Deep Dive into a KCSAN Bug: From a Single Commit to Systemic Behavior**
Initially, my attention was immediately drawn to commit `e61ab4ae48fb` (sysfs: Implement sysfs_getattr & sysfs_permission). It's a natural instinct to suspect a foundational commit when a bug surfaces. However, a healthy dose of skepticism kicked in. I couldn't just stop there; I needed to broaden my investigation.
My thought process then became a cascade of questions, each building upon the last, all aimed at a comprehensive understanding of the situation.
First, I questioned the timeline. Could a bug reported in 2026 genuinely stem from a 2009 commit? Or, more plausibly, was there a *more recent* commit that either introduced the condition, enabled it, or simply triggered it? I needed to evaluate both possibilities – that `e61ab4ae48fb` was the sole culprit, and the alternative that something else, perhaps more recent, was the true instigator.
This led me to the specific context of the crash: `udevd` triggering a KCSAN bug in syzkaller on November 2026, running on Linux 7.4-rc4. This was puzzling. Why `udevd`? Why then? My immediate reaction was a sharp intake of breath: "Wait a second!"
Upon re-examining the crash report, a crucial detail emerged: the bug was reported by the Kernel Concurrency Sanitizer (KCSAN) on CPU 0, UID 0, PID 3283, with `udevd` as the offending process. The hardware was a Google Compute Engine.
Then, a realization hit me, hard: "`udevd` runs on EVERY boot of EVERY syzkaller VM!!" This was a game-changer. If `udevd` is actively interacting with sysfs files (opening, `fstat`-ing, and closing them) on every boot when KCSAN is enabled, then why hadn't this KCSAN issue with `dnotify_flush / kernfs_iop_getattr` surfaced *years* ago? This question demanded an immediate halt and a thorough investigation. Had it *already* happened and been missed, or had something fundamentally changed recently to expose it now? This was the pivotal question I needed to answer.
My investigation then focused on two specific areas:
1. **The KCSAN Race Condition (`value_change == MAYBE`):** I needed to understand why KCSAN was reporting a race condition where the value change was uncertain. This led me to a critical self-correction: I had initially *assumed* `CONFIG_KCSAN_STRICT=y` was enabled on syzkaller. I had to explicitly verify this. This prompted me to dive back into `kernel/kcsan/report.c`, scrutinizing lines 180-195 and 475-491 for any clues related to strictness. Following that, I immediately moved to read `kernel/kcsan/core.c` lines 570-720, searching for the exact logic that would lead to a `MAYBE` state and how it interacted with the `udevd` scenario.
|
| 73/2 |
2026/11/18 07:45 |
tool |
read-file |
0m
Args: map[File:kernel/kcsan/core.c FirstLine:570 LineCount:100]
Results: map[Contents: 570: */
571: is_reorder_access = find_reorder_access(ctx, ptr, size, type, ip);
572: if (is_reorder_access)
573: interrupt_watcher = false;
574: /*
575: * Avoid races of scoped accesses from nested interrupts (or scheduler).
576: * Assume setting up a watchpoint for a non-scoped (normal) access that
577: * also conflicts with a current scoped access. In a nested interrupt,
578: * which shares the context, it would check a conflicting scoped access.
579: * To avoid, disable scoped access checking.
580: */
581: ctx->disable_scoped++;
582:
583: /*
584: * Save and restore the IRQ state trace touched by KCSAN, since KCSAN's
585: * runtime is entered for every memory access, and potentially useful
586: * information is lost if dirtied by KCSAN.
587: */
588: kcsan_save_irqtrace(current);
589: if (!interrupt_watcher) {
590: local_irq_save(irq_flags);
591: /*
592: * NMIs can still fire, disable checking for all interrupt
593: * contexts.
594: */
595: raw_cpu_ptr(&kcsan_cpu_ctx)->disable_count++;
596: }
597:
598: watchpoint = insert_watchpoint((unsigned long)ptr, size, is_write);
599: if (watchpoint == NULL) {
600: /*
601: * Out of capacity: the size of 'watchpoints', and the frequency
602: * with which should_watch() returns true should be tweaked so
603: * that this case happens very rarely.
604: */
605: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_NO_CAPACITY]);
606: goto out_unlock;
607: }
608:
609: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_SETUP_WATCHPOINTS]);
610: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]);
611:
612: /*
613: * Read the current value, to later check and infer a race if the data
614: * was modified via a non-instrumented access, e.g. from a device.
615: */
616: old = is_reorder_access ? 0 : read_instrumented_memory(ptr, size);
617:
618: /*
619: * Delay this thread, to increase probability of observing a racy
620: * conflicting access.
621: */
622: delay_access(type);
623:
624: /*
625: * Re-read value, and check if it is as expected; if not, we infer a
626: * racy access.
627: */
628: if (!is_reorder_access) {
629: new = read_instrumented_memory(ptr, size);
630: } else {
631: /*
632: * Reordered accesses cannot be used for value change detection,
633: * because the memory location may no longer be accessible and
634: * could result in a fault.
635: */
636: new = 0;
637: access_mask = 0;
638: }
639:
640: diff = old ^ new;
641: if (access_mask)
642: diff &= access_mask;
643:
644: /*
645: * Check if we observed a value change.
646: *
647: * Also check if the data race should be ignored (the rules depend on
648: * non-zero diff); if it is to be ignored, the below rules for
649: * KCSAN_VALUE_CHANGE_MAYBE apply.
650: */
651: if (diff && !kcsan_ignore_data_race(size, type, old, new, diff))
652: value_change = KCSAN_VALUE_CHANGE_TRUE;
653:
654: /* Check if this access raced with another. */
655: if (!consume_watchpoint(watchpoint)) {
656: /*
657: * Depending on the access type, map a value_change of MAYBE to
658: * TRUE (always report) or FALSE (never report).
659: */
660: if (value_change == KCSAN_VALUE_CHANGE_MAYBE) {
661: if (access_mask != 0) {
662: /*
663: * For access with access_mask, we require a
664: * value-change, as it is likely that races on
665: * ~access_mask bits are expected.
666: */
667: value_change = KCSAN_VALUE_CHANGE_FALSE;
668: } else if (size > 8 || is_assert) {
669: /* Always assume a value-change. */
]
|
| 74/2 |
2026/11/18 07:45 |
tool |
read-file |
0m
Args: map[File:kernel/kcsan/core.c FirstLine:670 LineCount:90]
Results: map[Contents: 670: value_change = KCSAN_VALUE_CHANGE_TRUE;
671: }
672: }
673:
674: /*
675: * No need to increment 'data_races' counter, as the racing
676: * thread already did.
677: *
678: * Count 'assert_failures' for each failed ASSERT access,
679: * therefore both this thread and the racing thread may
680: * increment this counter.
681: */
682: if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
683: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
684:
685: kcsan_report_known_origin(ptr, size, type, ip,
686: value_change, watchpoint - watchpoints,
687: old, new, access_mask);
688: } else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
689: /* Inferring a race, since the value should not have changed. */
690:
691: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN]);
692: if (is_assert)
693: atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
694:
695: if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert) {
696: kcsan_report_unknown_origin(ptr, size, type, ip,
697: old, new, access_mask);
698: }
699: }
700:
701: /*
702: * Remove watchpoint; must be after reporting, since the slot may be
703: * reused after this point.
704: */
705: remove_watchpoint(watchpoint);
706: atomic_long_dec(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]);
707:
708: out_unlock:
709: if (!interrupt_watcher) {
710: raw_cpu_ptr(&kcsan_cpu_ctx)->disable_count--;
711: local_irq_restore(irq_flags);
712: }
713: kcsan_restore_irqtrace(current);
714: ctx->disable_scoped--;
715:
716: /*
717: * Reordered accesses cannot be used for value change detection,
718: * therefore never consider for reordering if access_mask is set.
719: * ASSERT_EXCLUSIVE are not real accesses, ignore them as well.
720: */
721: if (!access_mask && !is_assert)
722: set_reorder_access(ctx, ptr, size, type, ip);
723: out:
724: user_access_restore(ua_flags);
725: }
726:
727: static __always_inline void
728: check_access(const volatile void *ptr, size_t size, int type, unsigned long ip)
729: {
730: atomic_long_t *watchpoint;
731: long encoded_watchpoint;
732:
733: /*
734: * Do nothing for 0 sized check; this comparison will be optimized out
735: * for constant sized instrumentation (__tsan_{read,write}N).
736: */
737: if (unlikely(size == 0))
738: return;
739:
740: again:
741: /*
742: * Avoid user_access_save in fast-path: find_watchpoint is safe without
743: * user_access_save, as the address that ptr points to is only used to
744: * check if a watchpoint exists; ptr is never dereferenced.
745: */
746: watchpoint = find_watchpoint((unsigned long)ptr, size,
747: !(type & KCSAN_ACCESS_WRITE),
748: &encoded_watchpoint);
749: /*
750: * It is safe to check kcsan_is_enabled() after find_watchpoint in the
751: * slow-path, as long as no state changes that cause a race to be
752: * detected and reported have occurred until kcsan_is_enabled() is
753: * checked.
754: */
755:
756: if (unlikely(watchpoint != NULL))
757: kcsan_found_watchpoint(ptr, size, type, ip, watchpoint, encoded_watchpoint);
758: else {
759: struct kcsan_ctx *ctx = get_ctx(); /* Call only once in fast-path. */
]
|
| 75/2 |
2026/11/18 07:45 |
llm |
cause-finder |
8m
Model:
Tokens:
input: 78540
output: 25
thoughts: 53
Thoughts: **My Thought Process Regarding KCSAN Configuration**
Okay, I've just had a moment of realization – or rather, an excited confirmation of a hunch. My immediate thought was, "Yes!" This is because I'm looking for the KCSAN configuration files, specifically within the `syzkaller` project. My internal checklist immediately directed me to investigate the `tools/testing/syzkaller` directory. My rationale here is that `syzkaller` is a prominent fuzzer, and if KCSAN (Kernel Concurrency Sanity Checker) is being used for testing or is relevant to its setup, its configuration would very likely reside within its own testing infrastructure.
However, to ensure absolute certainty, my next step was to broaden the search. I thought, "Let's check if there are any config files in the tree just to be 100% certain!" This is a standard procedure for me when I need definitive proof. The "tree" in this context refers to the broader kernel source tree. Therefore, I'm also mentally scanning or would be programmatically searching within `kernel/configs/`. This directory is the conventional location for general kernel configuration files, including those related to specific subsystems or testing tools like KCSAN. By checking both `syzkaller`'s specific directory and the general kernel configuration directory, I can be entirely confident that I'm not missing any relevant KCSAN configuration details.
|
| 76/2 |
2026/11/18 07:54 |
tool |
grepper |
0m
Args: map[Expression:KCSAN_STRICT]
Results: map[Output:Documentation/dev-tools/kcsan.rst=128=Kconfig options:
--
Documentation/dev-tools/kcsan.rst-147-
Documentation/dev-tools/kcsan.rst:148:To use the strictest possible rules, select ``CONFIG_KCSAN_STRICT=y``, which
Documentation/dev-tools/kcsan.rst-149-configures KCSAN to follow the Linux-kernel memory consistency model (LKMM) as
--
Documentation/translations/zh_CN/dev-tools/kcsan.rst=121=Kconfig 参数进行更改:
--
Documentation/translations/zh_CN/dev-tools/kcsan.rst-137-
Documentation/translations/zh_CN/dev-tools/kcsan.rst:138:要使用尽可能严格的规则,选择 ``CONFIG_KCSAN_STRICT=y``,这将配置 KCSAN 尽可
Documentation/translations/zh_CN/dev-tools/kcsan.rst-139-能紧密地遵循 Linux 内核内存一致性模型(LKMM)。
--
kernel/kcsan/.kunitconfig=15=CONFIG_KCSAN_KUNIT_TEST=y
--
kernel/kcsan/.kunitconfig-17-# Set these if you want to run test_barrier_nothreads
kernel/kcsan/.kunitconfig:18:#CONFIG_KCSAN_STRICT=y
kernel/kcsan/.kunitconfig-19-#CONFIG_KCSAN_WEAK_MEMORY=y
--
kernel/kcsan/core.c=803=void __init kcsan_init(void)
--
kernel/kcsan/core.c-824- IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {
kernel/kcsan/core.c:825: pr_warn("non-strict mode configured - use CONFIG_KCSAN_STRICT=y to see all data races\n");
kernel/kcsan/core.c-826- } else {
--
lib/Kconfig.kcsan=142=config KCSAN_INTERRUPT_WATCHER
lib/Kconfig.kcsan:143: bool "Interruptible watchers" if !KCSAN_STRICT
lib/Kconfig.kcsan:144: default KCSAN_STRICT
lib/Kconfig.kcsan-145- help
--
lib/Kconfig.kcsan=168=config KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
--
lib/Kconfig.kcsan-176-
lib/Kconfig.kcsan:177:config KCSAN_STRICT
lib/Kconfig.kcsan-178- bool "Strict data-race checking"
--
lib/Kconfig.kcsan=184=config KCSAN_WEAK_MEMORY
--
lib/Kconfig.kcsan-186- default y
lib/Kconfig.kcsan:187: depends on KCSAN_STRICT
lib/Kconfig.kcsan-188- help
--
lib/Kconfig.kcsan-191-
lib/Kconfig.kcsan:192: Depends on KCSAN_STRICT, because the options strengthening certain
lib/Kconfig.kcsan:193: plain accesses by default (depending on !KCSAN_STRICT) reduce the
lib/Kconfig.kcsan-194- ability to detect any data races invoving reordered accesses, in
--
lib/Kconfig.kcsan=200=config KCSAN_REPORT_VALUE_CHANGE_ONLY
--
lib/Kconfig.kcsan-202- default y
lib/Kconfig.kcsan:203: depends on !KCSAN_STRICT
lib/Kconfig.kcsan-204- help
--
lib/Kconfig.kcsan=209=config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
--
lib/Kconfig.kcsan-211- default y
lib/Kconfig.kcsan:212: depends on !KCSAN_STRICT
lib/Kconfig.kcsan-213- help
--
lib/Kconfig.kcsan=223=config KCSAN_IGNORE_ATOMICS
lib/Kconfig.kcsan-224- bool "Do not instrument marked atomic accesses"
lib/Kconfig.kcsan:225: depends on !KCSAN_STRICT
lib/Kconfig.kcsan-226- help
--
tools/testing/selftests/rcutorture/bin/kvm.sh=100=do
--
tools/testing/selftests/rcutorture/bin/kvm.sh-207- --kcsan)
tools/testing/selftests/rcutorture/bin/kvm.sh:208: TORTURE_KCONFIG_KCSAN_ARG="$debuginfo CONFIG_KCSAN=y CONFIG_KCSAN_STRICT=y CONFIG_KCSAN_REPORT_ONCE_IN_MS=100000 CONFIG_KCSAN_VERBOSE=y CONFIG_DEBUG...
tools/testing/selftests/rcutorture/bin/kvm.sh-209- ;;
]
|
| 77/2 |
2026/11/18 07:54 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 79923
output: 108
thoughts: 433
Thoughts: **My Journey Through the Data Race Investigation**
Here I am, staring at a syzkaller report. The first thing that catches my eye is `CONFIG_KCSAN_STRICT=y`. Okay, so KCSAN is in strict mode, which is good for catching these things, and `CONFIG_KCSAN_VERBOSE=n` tells me there's no `debug_show_held_locks` output, which means I'll have to do a bit more digging to understand the lock contention.
Now, before I even *think* about that commit `e61ab4ae48fb`, which seems to be flagged, I'm going to hit the brakes. This is critical: I *must* meticulously examine *every single commit* that has touched *any* part of the following code paths:
1. The `kernfs_refresh_inode`, `kernfs_iop_getattr`, `kernfs_iop_permission`, and `kernfs_iop_setattr` functions. These are clearly central to the issue.
2. The `dnotify_flush` function. Notifications often interact with inode state, so this is a potential source of race conditions.
3. Accesses to `inode->i_mode` across the VFS and specifically within kernfs. Mode changes are fundamental and could easily lead to races if not handled carefully.
4. The evolution of lock protection for inode attributes. I need to trace the history involving commits like `aa1d058d48f2` ("kernfs: dont take i_lock on inode attr read"), `47b5c64d0ab5` ("kernfs: use i_lock to protect concurrent inode updates"), `7ba0273b2f34` ("kernfs: switch kernfs to use an rwsem"), `9caf69614225` ("kernfs: Introduce separate rwsem to protect inode attributes"), and that potentially problematic `e61ab4ae48fb` ("sysfs: Implement sysfs_getattr & sysfs_permission").
But I'm not stopping there. I'm also going to broaden my search horizon. I need to check if *any* recent commits in the *near future* (the report mentions 2025/2026, which is a bit unusual for a current bug, but I'll assume it's a hypothetical future context or a typo and look at recent history) have altered *anything* related to this path.
To be absolutely thorough, I will be scrutinizing all recent commits affecting:
* `fs/kernfs/`
* `fs/sysfs/`
* `fs/stat.c`
* `fs/open.c`
* `fs/notify/`
* `fs/inode.c`
* `include/linux/fs.h`
* `fs/namei.c`
Essentially, I'm planning to run `git-log` on the entire `fs/` directory covering the last six months to ensure no stone is left unturned. This is the only way I'll be able to confidently understand the root cause of this data race and pinpoint the exact commit or sequence of commits responsible.
|
| 78/2 |
2026/11/18 07:54 |
tool |
git-log |
0m
Args: map[Count:60 PathPrefix:fs/kernfs/ Since:2 years]
Results: map[Output:ef179363b043 kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()
09452e5114bd kernfs: free the old name outside kernfs_rwsem
8b64f32fee95 kernfs: allocate the new name outside kernfs_rwsem
4424f06120fd kernfs: activate a new node without dropping kernfs_rwsem
2d2a2d7aa987 super: make iterate_supers_type() deletion-safe
e2f0d6d4569d fs: port ->setattr() to pass const mnt_idmap
c3150ded7bc0 fs: port ->getattr() to pass const mnt_idmap
566707035a6e fs: port ->mkdir() to pass const mnt_idmap
8ee61322bb53 fs: port ->rename() to pass const mnt_idmap
16d90b57d843 fs: port xattr to const mnt_idmap
41839e7bd92a fs: port ->permission() to pass const mnt_idmap
341c65579de5 kernfs: don't hold kernfs_rwsem across dir_emit()
45126ed80bb1 kernfs: resume readdir on the full entry key
5ebcb578adee kernfs: don't repeat or skip an entry when readdir resumes
23985e2b1f31 kernfs: don't hold kernfs_rwsem across notification delivery
5ad03af18434 kernfs: allocate the open node outside the open file mutex
81bdc85b4c08 kernfs: fix up the unlocked attribute reads on the creation paths
dc1a0cc95737 kernfs: don't lose IN_DELETE_SELF when decoding a file handle
6034974e3aec kernfs: take kernfs_rename_lock for same-parent renames too
7b15d6cf25e6 kernfs: preserve security xattrs without allocating iattrs
8c2a5e65a977 kernfs: Remove kernfs_rwsem from dentry revalidation
4c291d7d3dbd kernfs: Avoid namespace dereference in d_revalidate()
ea1c84cca6ab kernfs: Prepare directory revisions for lockless reads
0ef1e65c9f63 kernfs: Use VFS lookup context in d_revalidate()
d5e81a5650b5 kernfs: avoid iattr allocation in listxattr
06553566a841 kernfs: Replace strcpy(s, "../") with memcpy(s, "../", 4)
d996995a0ffb kernfs: simplify kernfs_name_hash()
6cccc49b027c kernfs: link kn to its parent before the LSM init hook
1e7cd8a53b72 simpe_xattr: use per-sb cache
076e5cef28e2 simple_xattr: change interface to pass struct simple_xattrs **
6a07814ff643 kernfs: fix xattr race condition with multiple superblocks
0fdde3f2aead kernfs: fix suspicious RCU usage in kernfs_put()
cb76a81c7cec kernfs: make directory seek namespace-aware
1fe989e1c42a kernfs: use namespace id instead of pointer for hashing and comparison
e3b2cf6e5dba kernfs: pass struct ns_common instead of const void * for namespace tags
16de94a1b090 kernfs: Add missing documentation for kernfs_put_active's drop_supers argument
eea5d2bb34ba kernfs: Send IN_DELETE_SELF and IN_IGNORED
507d8ce13f5b kernfs: Don't set_nlink for directories being removed
4fbe9e78bb41 xattr: move user limits for xattrs to generic infra
5bd97f5c5f24 kernfs: adapt to rhashtable-based simple_xattrs with lazy allocation
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
2b742094582d fs/kernfs: null-ptr deref in simple_xattrs_free()
382b1e8f30f7 kernfs: fix memory leak of kernfs_iattrs in __kernfs_new_node
2467f9928c98 fs/kernfs: raise sb->maxbytes to MAX_LFS_FILESIZE
b4dbfd8653b3 Coccinelle-based conversion to use ->i_state accessors
f99b3917789d fs: rename generic_delete_inode() and generic_drop_inode()
3c9ba2777d6c kernfs: Fix UAF in polling when open file is released
c237aa9884f2 kernfs: don't fail listing extended attributes
fb7b30cb0e31 kernfs: remove iattr_mutex
d1f4e9026007 kernfs: remove iattr_mutex
05fb0e666495 new helper: set_default_d_op()
071d8e4c2a3b kernfs: Relax constraint in draining guard
ef2ed04eba99 kernfs: add warning about implementing freeze/thaw
93b27a845ec1 kernfs: switch global kernfs_rename_lock to per-fs lock
cec59c440a05 kernfs: switch global kernfs_idr_lock to per-fs lock
fa6fe07d1536 VFS: rename lookup_one_len family to lookup_noperm and remove permission check
88d5baf69082 Change inode_operations.mkdir to return struct dentry *
c5020c5be9d2 kernfs: Move dput() outside of the RCU section.
6ef5b6fae304 kernfs: Drop kernfs_rwsem while invoking lookup_positive_unlocked().
]
|
| 79/2 |
2026/11/18 07:54 |
tool |
git-log |
0m
Args: map[Count:60 PathPrefix:fs/sysfs/ Since:2 years]
Results: map[Output:454257f6d124 sysfs: clamp show() return value in sysfs_kf_read()
237557b8a81a sysfs: don't remove existing directory on update failure
78d797520f6a sysfs: remove trivial sysfs_get_tree() wrapper
dc4edae7f41d fs: move SB_I_USERNS_VISIBLE to FS_USERNS_MOUNT_RESTRICTED
f71cc65c01bd sysfs: upgrade OOB write by buggy .show hook into WARNing
9ce4a8c07b28 sysfs: attribute_group: Respect is_visible_const() when changing owner
e3b2cf6e5dba kernfs: pass struct ns_common instead of const void * for namespace tags
fe2511adb1fc sysfs: constify group arrays in function arguments
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
d79ff2d61202 sysfs: remove exports of sysfs_*change_owner()
7dd9fdb4939b sysfs: attribute_group: enable const variants of is_visible()
c7fbb8218b4a sysfs: check visibility before changing group attribute ownership
1d6249c1ce82 sysfs: remove bin_attribute::read_new/write_new()
340d8e66c43f sysfs: constify internal references to 'struct bin_attribute'
9bec944506fa sysfs: constify attribute_group::bin_attrs
741c10b096bc kernfs: Use RCU to access kernfs_node::name.
633488947ef6 kernfs: Use RCU to access kernfs_node::parent.
3675a926feef sysfs: constify bin_attribute argument of sysfs_bin_attr_simple_read()
]
|
| 80/2 |
2026/11/18 07:54 |
tool |
git-log |
0m
Args: map[Count:40 PathPrefix:fs/notify/ Since:2 years]
Results: map[Output:581230d2ec3c inotify: do not coalesce rename events
1dcf2be94b9b fsnotify: preserve 64-bit pre-content ranges
36ebddca56b9 fsnotify: prevent mark additions during connector teardown
ff05910cb1b7 fanotify: report names longer than NAME_MAX
c9ec8607b4d1 fsnotify: let a filesystem refuse marks on its objects
3b79adcf0716 fsnotify: fix typo "anonynous" in comment
e422777fdd47 fsnotify: Fix stale object mask after concurrent mark updates
68615158c12d fanotify: report full event length for FIONREAD
d7f1cf5be33e fanotify: fix use-after-free of file range info
17463fe75130 fanotify: stop permission watchdog when timeout is zero
a3aa899823dd fanotify: initialize permission event watchdog state
82c6dd20479b fanotify: allow reporting pidfds for reaped tasks
17171128513b fanotify: report thread pidfds for FAN_REPORT_TID
f58617ff687b fanotify: simplify fanotify_error_event_equal
12ffbb117b64 fsnotify: add fsnotify_modify_mark_mask()
ad4489dcd08d fsnotify: new tracepoint in fsnotify()
4aca914ac152 fsnotify: fix inode reference leak in fsnotify_recalc_mask()
ae974ca6f0f3 fanotify: Fix spelling mistake "enforecement" -> "enforcement"
7746e3bd4cc1 fanotify: fix false positive on permission events
408d8af01f3a for_each_alias(): helper macro for iterating through dentries of given inode
0fdbe845534f fanotify: replace deprecated strcpy in fanotify_info_copy_{name,name2}
0b2600f81cef treewide: change inode->i_ino from unsigned long to u64
4520b96b8136 fsnotify: inotify: pass mark connector to fsnotify_recalc_mask()
66052a768d47 fanotify: call fanotify_events_supported() before path_permission() and security_path_notify()
0d5ee3373426 fanotify: avoid/silence premature LSM capability checks
6a320935fa42 inotify: fix watch count leak when fsnotify_add_inode_mark_locked() fails
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
a05fc7edd988 fsnotify: Use connector list for destroying inode marks
94bd01253c3d fsnotify: Track inode connectors for a superblock
635bc4def026 fsnotify: do not generate ACCESS/MODIFY events on child for special files
7129098f4f7b fanotify: convert fanotify_init() to FD_PREPARE()
b4dbfd8653b3 Coccinelle-based conversion to use ->i_state accessors
a7c4bb43bfdc fs/notify: call exportfs_encode_fid with s_umount
7a4f92d39f66 fs: replace use of system_unbound_wq with system_dfl_wq
b8cf8fda522d fanotify: add watchdog for permission events
62e59ffe8787 fanotify: Validate the return value of mnt_ns_from_dentry() before dereferencing
6746c36c94dd fsnotify: fix "rewriten"->"rewritten"
37b27bd5d621 fs: add an icount_read helper
0d4c4d4ea443 fsnotify: optimize FMODE_NONOTIFY_PERM for the common cases
]
|
| 81/2 |
2026/11/18 07:54 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 83551
output: 142
thoughts: 506
Thoughts: **Navigating a Conundrum in KCSAN Reporting Logic**
Alright, hold on a second. I need to stop and think this through very carefully. I'm feeling a distinct sense of confusion, and I suspect I might have gotten tangled up earlier regarding the interplay between `CONFIG_KCSAN_STRICT` and `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY`. This is a critical distinction, and I need to get it right.
So, the immediate priority is to *re-check* the source code, specifically `kernel/kcsan/report.c` and `kernel/kcsan/core.c`. I recall reading something like this earlier:
> The first call to `skip_report` always has `value_change==TRUE`, since we cannot know the value written of an instrumented access. For the 2nd call there are 6 cases with `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY`:
>
> 1. read watchpoint, conflicting write (`value_change==TRUE`): report;
> 2. read watchpoint, conflicting write (`value_change==MAYBE`): skip;
> 3. write watchpoint, conflicting write (`value_change==TRUE`): report;
> 4. write watchpoint, conflicting write (`value_change==MAYBE`): skip;
> 5. write watchpoint, conflicting read (`value_change==MAYBE`): skip;
> 6. write watchpoint, conflicting read (`value_change==TRUE`): report;
This is where the knot in my thinking tightens. Now, in the context of syzkaller, the crucial question is: Is `CONFIG_KCSAN_STRICT` set to `y` or `n`? This setting will dramatically alter how these reports are interpreted and what actions are taken.
To resolve this, I need to look at specific commits. Commit `c610d2d07879` ("fs: annotate inode timestamp accessors") is the immediate target. But beyond that, I need to broaden my investigation. I'll be checking the syzbot link associated with that commit, and importantly, I'll also examine other syzbot KCSAN commits like `bf4ccbaad8bd`, `8c2a5e65a977`, `1dc88208cfdc`, `b952837f734c`, and `521fed4886cc`. The goal here is to understand the *actual KCSAN reports* that syzbot is generating, and crucially, to see which commits these reports are explicitly marked as `Fixes:`. By examining the `git-show` output of several of these commits, I should be able to piece together the real-world behavior and clarify the impact of `CONFIG_KCSAN_STRICT` on the reporting logic I've been contemplating.
|
| 82/2 |
2026/11/18 07:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 8c2a5e65a977685056513677c45d4f08d87ef081
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Thu Aug 20 22:05:07 2026 -0700
kernfs: Remove kernfs_rwsem from dentry revalidation
kernfs_dop_revalidate() takes kernfs_rwsem for read once per path component
of every walk into a kernfs mount. Linux rwsems do not permit reader lock
stealing once a writer is queued, so a single writer parks the whole
incoming reader stream in uninterruptible sleep, stalling cgroup-polling
daemons for minutes.
Nothing the callback reads requires the semaphore. kn->active is an
atomic_t that kernfs_find_and_get_node_by_id() already tests through
__kernfs_active(); kn->__parent and kn->name are RCU pointers whose old
values are freed only after a grace period; kn->ns is now compared rather
than dereferenced; parent->dir.rev was annotated earlier in this series.
What the semaphore does provide is a coherent snapshot, and that is not
needed. ->d_revalidate() answers a question about a single instant, and the
answer is already stale when it returns: a rename landing just after
up_read() gives the same outcome as one observed mid-read. A lockless
reader can only return "valid" for the (parent, name, namespace) triple
identifying the dentry it was handed, and that triple was true when the
dentry was instantiated, so it reports a genuine past state exactly as the
locked version did. Removal is backstopped by kernfs_get_active() failing
in the subsequent open().
Take an RCU read lock instead. kernfs_parent() and kernfs_rcu_name() work
unchanged: the condition in their rcu_dereference_check() is an alternative
to holding the RCU read lock, not an extra requirement. The negative dentry
path needs nothing, as @dir pins the parent. The namespace check can use
@parent directly once the preceding check establishes it equals
kernfs_parent(kn), so the kn_parent local and its NULL test go away.
kernfs_ns_enabled() reads @parent->flags, which KERNFS_ACTIVATED and
KERNFS_REMOVING update as a plain read-modify-write under kernfs_rwsem.
Those bits are not read here and KERNFS_NS cannot change once the directory
has children, so mark the read data_race() rather than READ_ONCE(), which
would not silence KCSAN against the unmarked writers anyway.
kernfs_iop_permission() still forces every walk out of RCU-walk before
children are revalidated, so lifting the LOOKUP_RCU bail here would have no
observable effect; it is left to the series fixing that path.
Readers walking cgroupfs and sysfs against concurrent cgroup and netdev
churn: kernfs_rwsem read acquisitions drop from 48,593,360 to 1,280,280,
and kernfs_dop_revalidate() no longer appears among its contention sites.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260821050507.2161607-5-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 27949b0e027c..cd7a8ff8b6b2 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -1171,9 +1171,8 @@ struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
static int kernfs_dop_revalidate(struct inode *dir, const struct qstr *name,
struct dentry *dentry, unsigned int flags)
{
- struct kernfs_node *kn, *kn_parent;
struct kernfs_node *parent = dir->i_private;
- struct kernfs_root *root;
+ struct kernfs_node *kn;
const char *kn_name;
if (flags & LOOKUP_RCU)
@@ -1191,49 +1190,41 @@ static int kernfs_dop_revalidate(struct inode *dir, const struct qstr *name,
* changes and the lookup re-done so that a new positive
* dentry can be properly created.
*/
- root = kernfs_root(parent);
- down_read(&root->kernfs_rwsem);
- if (kernfs_dir_changed(parent, dentry)) {
- up_read(&root->kernfs_rwsem);
- return 0;
- }
- up_read(&root->kernfs_rwsem);
-
- /* The kernfs parent node hasn't changed, leave the
- * dentry negative and return success.
- */
- return 1;
+ return !kernfs_dir_changed(parent, dentry);
}
kn = kernfs_dentry_node(dentry);
- root = kernfs_root(kn);
- down_read(&root->kernfs_rwsem);
+
+ guard(rcu)();
/* The kernfs node has been deactivated */
- if (!kernfs_active(kn))
- goto out_bad;
+ if (!__kernfs_active(kn))
+ return 0;
- kn_parent = kernfs_parent(kn);
/* The kernfs node has been moved? */
- if (parent != kn_parent)
- goto out_bad;
+ if (kernfs_parent(kn) != parent)
+ return 0;
/* The kernfs node has been renamed */
kn_name = kernfs_rcu_name(kn);
if (name->len != strlen(kn_name) ||
memcmp(name->name, kn_name, name->len))
- goto out_bad;
+ return 0;
- /* The kernfs node has been moved to a different namespace */
- if (kn_parent && kernfs_ns_enabled(kn_parent) &&
+ /*
+ * The kernfs node has been moved to a different namespace.
+ *
+ * KERNFS_NS is set by kernfs_enable_ns() while @parent still has no
+ * children, so it cannot change while a child of @parent is being
+ * revalidated. The other bits in that word, KERNFS_ACTIVATED and
+ * KERNFS_REMOVING, are updated under kernfs_rwsem and are not read
+ * here, so racing with them is intentional and harmless.
+ */
+ if (data_race(kernfs_ns_enabled(parent)) &&
kernfs_info(dir->i_sb)->ns != READ_ONCE(kn->ns))
- goto out_bad;
+ return 0;
- up_read(&root->kernfs_rwsem);
return 1;
-out_bad:
- up_read(&root->kernfs_rwsem);
- return 0;
}
const struct dentry_operations kernfs_dops = {
]
|
| 83/2 |
2026/11/18 07:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 1dc88208cfdce26858c59609242f2bb0e2b5c031
Author: Christian Brauner <brauner@kernel.org>
Date: Fri Jul 10 11:33:07 2026 +0200
binfmt_misc: annotate racy accesses to ->enabled
->enabled has always been read and written locklessly: every exec
reads it in load_misc_binary() while bm_status_write() or a concurrent
remount via bm_fill_super() may flip it. That is fine as it is an
independent boolean toggle but the accesses should be marked
accordingly for KCSAN. Annotate them with READ_ONCE()/WRITE_ONCE().
Link: https://patch.msgid.link/20260710-work-binfmt_misc-locking-v3-6-a162f7cb58d6@kernel.org
Reviewed-by: Jori Koolstra <jkoolstra@xs4all.nl>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index 236ebaf3be5c..0e56eb225862 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -217,7 +217,7 @@ static int load_misc_binary(struct linux_binprm *bprm)
struct binfmt_misc *misc;
misc = load_binfmt_misc();
- if (!misc->enabled)
+ if (!READ_ONCE(misc->enabled))
return retval;
fmt = get_binfmt_handler(misc, bprm);
@@ -874,7 +874,7 @@ bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
char *s;
misc = i_binfmt_misc(file_inode(file));
- s = misc->enabled ? "enabled\n" : "disabled\n";
+ s = READ_ONCE(misc->enabled) ? "enabled\n" : "disabled\n";
return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
}
@@ -891,11 +891,11 @@ static ssize_t bm_status_write(struct file *file, const char __user *buffer,
switch (res) {
case 1:
/* Disable all handlers. */
- misc->enabled = false;
+ WRITE_ONCE(misc->enabled, false);
break;
case 2:
/* Enable all handlers. */
- misc->enabled = true;
+ WRITE_ONCE(misc->enabled, true);
break;
case 3:
/* Delete all handlers. */
@@ -1000,7 +1000,7 @@ static int bm_fill_super(struct super_block *sb, struct fs_context *fc)
* is true. Instead, if someone mounts binfmt_misc for the first time or
* again we simply reset ->enabled to true.
*/
- misc->enabled = true;
+ WRITE_ONCE(misc->enabled, true);
err = simple_fill_super(sb, BINFMTFS_MAGIC, bm_files);
if (!err)
]
|
| 84/2 |
2026/11/18 07:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit b952837f734c3a627877bf922408dac04588a643
Author: Cen Zhang <zzzccc427@gmail.com>
Date: Wed May 6 09:07:09 2026 +0800
f2fs: annotate lockless last_time[] accesses
f2fs stores mount-wide activity timestamps in sbi->last_time[] and
samples them from background discard, GC, and balance paths without a
dedicated lock. The timestamps are used as best-effort heuristics to
decide whether background work should run now or sleep a bit longer.
The current helpers use plain loads and stores, so KCSAN can report races
between frequent foreground updates and background readers. Exact
freshness is not required here, but the intentional lockless accesses
should be marked explicitly.
Use WRITE_ONCE() in f2fs_update_time() and READ_ONCE() in
f2fs_time_over() and f2fs_time_to_wait(). This preserves the existing
heuristic behavior and avoids adding locking to hot paths.
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index e555c1d794df..6d2048cffa66 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -2125,12 +2125,12 @@ static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
{
unsigned long now = jiffies;
- sbi->last_time[type] = now;
+ WRITE_ONCE(sbi->last_time[type], now);
/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
if (type == REQ_TIME) {
- sbi->last_time[DISCARD_TIME] = now;
- sbi->last_time[GC_TIME] = now;
+ WRITE_ONCE(sbi->last_time[DISCARD_TIME], now);
+ WRITE_ONCE(sbi->last_time[GC_TIME], now);
}
}
@@ -2138,7 +2138,7 @@ static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
{
unsigned long interval = sbi->interval_time[type] * HZ;
- return time_after(jiffies, sbi->last_time[type] + interval);
+ return time_after(jiffies, READ_ONCE(sbi->last_time[type]) + interval);
}
static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
@@ -2148,7 +2148,7 @@ static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
unsigned int wait_ms = 0;
long delta;
- delta = (sbi->last_time[type] + interval) - jiffies;
+ delta = (READ_ONCE(sbi->last_time[type]) + interval) - jiffies;
if (delta > 0)
wait_ms = jiffies_to_msecs(delta);
]
|
| 85/2 |
2026/11/18 07:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 521fed4886ccb3b78846595e843ec9419d343623
Author: Li Wang <liwang@kylinos.cn>
Date: Fri May 22 16:38:05 2026 +0800
fuse: use READ_ONCE in fuse_chan_num_background()
fuse_chan_num_background() is called without holding fch->bg_lock (for
example from fuse_writepages() to compare against fc->congestion_threshold),
while fch->num_background is updated under bg_lock in dev.c and dev_uring.c.
This is the same locked-write/lockless-read pattern already used for
max_background in fuse_chan_max_background().
Use READ_ONCE() on the read side so that:
- The compiler does not cache or coalesce loads of a value that may change
concurrently on another CPU.
- Prevent KCSAN from reporting an unexpected race.
Signed-off-by: Li Wang <liwang@kylinos.cn>
Fixes: 670d21c6e17f ("fuse: remove reliance on bdi congestion")
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c
index 63a289853023..d09cddd68e03 100644
--- a/fs/fuse/dev.c
+++ b/fs/fuse/dev.c
@@ -383,7 +383,7 @@ EXPORT_SYMBOL_GPL(fuse_dev_chan_new);
unsigned int fuse_chan_num_background(struct fuse_chan *fch)
{
- return fch->num_background;
+ return READ_ONCE(fch->num_background);
}
unsigned int fuse_chan_max_background(struct fuse_chan *fch)
]
|
| 86/2 |
2026/11/18 07:55 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 2b7c9664c3ce9d8da1113a1ca2546046de8eb93e
Author: Dmitry Antipov <dmantipov@yandex.ru>
Date: Fri Jun 20 09:30:59 2025 +0300
fs: annotate suspected data race between poll_schedule_timeout() and pollwake()
When running almost any select()/poll() workload intense enough,
KCSAN is likely to report data races around using 'triggered' flag
of 'struct poll_wqueues'. For example, running 'find /' on a tty
console may trigger the following:
BUG: KCSAN: data-race in poll_schedule_timeout / pollwake
write to 0xffffc900030cfb90 of 4 bytes by task 97 on cpu 5:
pollwake+0xd1/0x130
__wake_up_common_lock+0x7f/0xd0
n_tty_receive_buf_common+0x776/0xc30
n_tty_receive_buf2+0x3d/0x60
tty_ldisc_receive_buf+0x6b/0x100
tty_port_default_receive_buf+0x63/0xa0
flush_to_ldisc+0x169/0x3c0
process_scheduled_works+0x6fe/0xf40
worker_thread+0x53b/0x7b0
kthread+0x4f8/0x590
ret_from_fork+0x28c/0x450
ret_from_fork_asm+0x1a/0x30
read to 0xffffc900030cfb90 of 4 bytes by task 5802 on cpu 4:
poll_schedule_timeout+0x96/0x160
do_sys_poll+0x966/0xb30
__se_sys_ppoll+0x1c3/0x210
__x64_sys_ppoll+0x71/0x90
x64_sys_call+0x3079/0x32b0
do_syscall_64+0xfa/0x3b0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
According to Jan, "there's no practical issue here because it is hard
to imagine how the compiler could compile the above code using some
intermediate values stored into 'triggered' or multiple fetches from
'triggered'". Nevertheless, silence KCSAN by using WRITE_ONCE() in
__pollwake() and READ_ONCE() in poll_schedule_timeout(), respectively.
Link: https://lore.kernel.org/linux-fsdevel/bwx72orsztfjx6aoftzzkl7wle3hi4syvusuwc7x36nw6t235e@bjwrosehblty
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Link: https://lore.kernel.org/20250620063059.1800689-1-dmantipov@yandex.ru
Acked-by: Marco Elver <elver@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
diff --git a/fs/select.c b/fs/select.c
index 9fb650d03d52..082cf60c7e23 100644
--- a/fs/select.c
+++ b/fs/select.c
@@ -192,7 +192,7 @@ static int __pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *k
* and is paired with smp_store_mb() in poll_schedule_timeout.
*/
smp_wmb();
- pwq->triggered = 1;
+ WRITE_ONCE(pwq->triggered, 1);
/*
* Perform the default wake up operation using a dummy
@@ -237,7 +237,7 @@ static int poll_schedule_timeout(struct poll_wqueues *pwq, int state,
int rc = -EINTR;
set_current_state(state);
- if (!pwq->triggered)
+ if (!READ_ONCE(pwq->triggered))
rc = schedule_hrtimeout_range(expires, slack, HRTIMER_MODE_ABS);
__set_current_state(TASK_RUNNING);
]
|
| 87/2 |
2026/11/18 07:55 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 89674
output: 64
thoughts: 152
Thoughts: **Diving into KCSAN's Context Analysis Activation**
Okay, I need to circle back and examine commit `0eaa911f8908` in `kernel/kcsan/`. The title, "kcsan: Enable context analysis," immediately flags it for a deeper look. My immediate thought is to verify the specifics of that commit, but it's not enough. I need to cast a wider net.
My next step is to query the entire kernel history using `git-log` with a specific filter: `MessageRegexps: ["syzbot", "KCSAN"]`. This is crucial because I need to understand the broader context of how syzbot has been interacting with KCSAN. Specifically, I'm looking for any changes or patterns in syzbot's KCSAN testing that might indicate a recent enablement of `CONFIG_KCSAN_STRICT`, or conversely, whether it has consistently been running with that strict mode enabled all along. This comparison will be key to understanding the implications of commit `0eaa911f8908` and its potential impact on recent findings.
|
| 88/2 |
2026/11/18 07:56 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 0eaa911f890812a7868a44bbfd656636b2c7caf8
Author: Marco Elver <elver@google.com>
Date: Fri Dec 19 16:40:19 2025 +0100
kcsan: Enable context analysis
Enable context analysis for the KCSAN subsystem.
Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20251219154418.3592607-31-elver@google.com
diff --git a/kernel/kcsan/Makefile b/kernel/kcsan/Makefile
index a45f3dfc8d14..824f30c93252 100644
--- a/kernel/kcsan/Makefile
+++ b/kernel/kcsan/Makefile
@@ -1,4 +1,6 @@
# SPDX-License-Identifier: GPL-2.0
+CONTEXT_ANALYSIS := y
+
KCSAN_SANITIZE := n
KCOV_INSTRUMENT := n
UBSAN_SANITIZE := n
diff --git a/kernel/kcsan/report.c b/kernel/kcsan/report.c
index e95ce7d7a76e..11a48b78f8d1 100644
--- a/kernel/kcsan/report.c
+++ b/kernel/kcsan/report.c
@@ -116,6 +116,7 @@ static DEFINE_RAW_SPINLOCK(report_lock);
* been reported since (now - KCSAN_REPORT_ONCE_IN_MS).
*/
static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
+ __must_hold(&report_lock)
{
struct report_time *use_entry = &report_times[0];
unsigned long invalid_before;
@@ -366,6 +367,7 @@ static int sym_strcmp(void *addr1, void *addr2)
static void
print_stack_trace(unsigned long stack_entries[], int num_entries, unsigned long reordered_to)
+ __must_hold(&report_lock)
{
stack_trace_print(stack_entries, num_entries, 0);
if (reordered_to)
@@ -373,6 +375,7 @@ print_stack_trace(unsigned long stack_entries[], int num_entries, unsigned long
}
static void print_verbose_info(struct task_struct *task)
+ __must_hold(&report_lock)
{
if (!task)
return;
@@ -389,6 +392,7 @@ static void print_report(enum kcsan_value_change value_change,
const struct access_info *ai,
struct other_info *other_info,
u64 old, u64 new, u64 mask)
+ __must_hold(&report_lock)
{
unsigned long reordered_to = 0;
unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 };
@@ -496,6 +500,7 @@ static void print_report(enum kcsan_value_change value_change,
}
static void release_report(unsigned long *flags, struct other_info *other_info)
+ __releases(&report_lock)
{
/*
* Use size to denote valid/invalid, since KCSAN entirely ignores
@@ -507,13 +512,11 @@ static void release_report(unsigned long *flags, struct other_info *other_info)
/*
* Sets @other_info->task and awaits consumption of @other_info.
- *
- * Precondition: report_lock is held.
- * Postcondition: report_lock is held.
*/
static void set_other_info_task_blocking(unsigned long *flags,
const struct access_info *ai,
struct other_info *other_info)
+ __must_hold(&report_lock)
{
/*
* We may be instrumenting a code-path where current->state is already
@@ -572,6 +575,7 @@ static void set_other_info_task_blocking(unsigned long *flags,
static void prepare_report_producer(unsigned long *flags,
const struct access_info *ai,
struct other_info *other_info)
+ __must_not_hold(&report_lock)
{
raw_spin_lock_irqsave(&report_lock, *flags);
@@ -603,6 +607,7 @@ static void prepare_report_producer(unsigned long *flags,
static bool prepare_report_consumer(unsigned long *flags,
const struct access_info *ai,
struct other_info *other_info)
+ __cond_acquires(true, &report_lock)
{
raw_spin_lock_irqsave(&report_lock, *flags);
]
|
| 89/2 |
2026/11/18 07:56 |
tool |
git-log |
0m
Args: map[Count:25 MessageRegexps:[syzbot KCSAN]]
Results: map[Output:f25f6319e248 bpf: Annotate data races on bpf_lru_node type
bda345b3fc59 workqueue: mark pool->nr_running as __data_racy
8535c259c8fb percpu_counter: annotate lockless read in _limited_add()
12e19bba68ba pid: use READ_ONCE() in pid_alive()
c20cf1c3c8cd mm/memcontrol: fix data-race on reading jiffies_64
1ca924044bf0 selinux: fix data race on AVC latest_notif
46bc52d13594 ipv4: fib: fix data-race and stale genid check around nh->nh_saddr
5e289c5a4a52 bpf: Annotate bpf_obj_memcpy with data_race
889d3783ae3f mm/shmem: annotate benign data-race in shmem_getattr()
c610d2d07879 fs: annotate inode timestamp accessors
749179c2e25b can: bcm: add locking when updating filter and timer values
1d28a67d496f timer_list: Annotate print_cpu() diagnostic reads
68de007d5ac9 xfrm: annotate data-races around xfrm_policy_count[] and xfrm_policy_default[]
67636cab273e rds: annotate data-race around rs_seen_congestion
c4f050ce06c5 bonding: 3ad: implement proper RCU rules for port->aggregator
b47bcab6ee92 rtc: add data_race() in rtc_dev_poll()
8dc51459ef70 selinux: annotate intentional data race in inode_doinit_with_dentry()
6e84fc395e90 ipv4: fib: Annotate access to struct fib_alias.fa_state.
f6c3665b6dc5 bonding: annotate data-races around slave->last_rx
5d5fe8bcd331 rxrpc: Fix data-race warning and potential load/store tearing
9a063f96d87e ipv6: annotate data-race in ndisc_router_discovery()
8175dbf174d4 mISDN: annotate data-race around dev->work
42b12cb5fd45 io_uring/timeout: annotate data race in io_flush_timeouts()
b994ace83a2b io_uring/waitid: fix KCSAN warning on io_waitid->head
7a29f6bf60f2 l2tp: avoid one data-race in l2tp_tunnel_del_work()
]
|
| 90/2 |
2026/11/18 07:56 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 91892
output: 85
thoughts: 97
Thoughts: **My Dive into the Kernel's Memory Sharing Commit Log**
Wow, these commit hashes caught my eye: `889d3783ae3f` (specifically, "mm/shmem: annotate benign data-race in shmem_getattr()"), `bda345b3fc59`, `bda345b3fc59`, and `8dc51459ef70`. The one about annotating a data race in `shmem_getattr` immediately sparked my curiosity. I just had to pull up the details for `889d3783ae3f` using `git show` to understand what's going on there.
|
| 91/2 |
2026/11/18 07:56 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 889d3783ae3f78c8593bc7837a2a68b49db4166d
Author: Jaeyeon Lee <jaeyeon.lee.dev@gmail.com>
Date: Wed Jul 8 13:19:41 2026 +0200
mm/shmem: annotate benign data-race in shmem_getattr()
shmem_getattr() reads info->alloced, info->swapped and i_mapping->nrpages
without info->lock to decide whether shmem_recalc_inode() should be
called. shmem_recalc_inode() recomputes these under info->lock, so a
stale read here only affects whether shmem_recalc_inode() runs.
Annotate data_race() to silence KCSAN.
Link: https://lore.kernel.org/20260708111941.35460-1-jaeyeon.lee.dev@gmail.com
Signed-off-by: Jaeyeon Lee <jaeyeon.lee.dev@gmail.com>
Reported-by: syzbot+dfb578404df369f6599b@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=dfb578404df369f6599b
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Marco Elver <elver@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/mm/shmem.c b/mm/shmem.c
index fa8bb59f87b4..0a36b48f667e 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1297,7 +1297,8 @@ static int shmem_getattr(struct mnt_idmap *idmap,
struct inode *inode = path->dentry->d_inode;
struct shmem_inode_info *info = SHMEM_I(inode);
- if (info->alloced - info->swapped != inode->i_mapping->nrpages)
+ /* Fast-path hint; recalc under info->lock corrects any stale read. */
+ if (data_race(info->alloced - info->swapped != inode->i_mapping->nrpages))
shmem_recalc_inode(inode, 0, 0);
if (info->fsflags & FS_APPEND_FL)
]
|
| 92/2 |
2026/11/18 07:56 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 12e19bba68ba4ef222033ff9a3b63df07b7fad38
Author: Babanpreet Singh <bbnpreetsingh@gmail.com>
Date: Fri Oct 2 01:21:41 2026 +0000
pid: use READ_ONCE() in pid_alive()
KCSAN reports pid_alive() reading task->thread_pid while it gets cleared
under tasklist_lock. The check only cares about NULL, so READ_ONCE() and
WRITE_ONCE() are enough.
Reported-by: syzbot+c382ee653fd70f5cf1bb@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c382ee653fd70f5cf1bb
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
Link: https://patch.msgid.link/20261002012141.7-1-bbnpreetsingh@gmail.com
Acked-by: Oleg Nesterov <oleg@redhat.com>
Reviewed-by: Bradley Morgan <brads@mainlining.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/include/linux/pid.h b/include/linux/pid.h
index f5476d7140a3..a0458c713e4e 100644
--- a/include/linux/pid.h
+++ b/include/linux/pid.h
@@ -273,7 +273,7 @@ static inline pid_t task_tgid_nr(struct task_struct *tsk)
*/
static inline int pid_alive(const struct task_struct *p)
{
- return p->thread_pid != NULL;
+ return READ_ONCE(p->thread_pid) != NULL;
}
static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
diff --git a/kernel/pid.c b/kernel/pid.c
index 95b8ccfa8269..adf721606768 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -411,7 +411,7 @@ static void __change_pid(struct pid **pids, struct task_struct *task,
pid = *pid_ptr;
hlist_del_rcu(&task->pid_links[type]);
- *pid_ptr = new;
+ WRITE_ONCE(*pid_ptr, new);
for (tmp = PIDTYPE_MAX; --tmp >= 0; )
if (pid_has_task(pid, tmp))
]
|
| 93/2 |
2026/11/18 07:56 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 8dc51459ef702bcc0ef5fb26bb4d362b38aa56c2
Author: Christian Göttsche <cgzones@googlemail.com>
Date: Mon Feb 2 14:48:03 2026 +0100
selinux: annotate intentional data race in inode_doinit_with_dentry()
Like other `isec->initialized == LABEL_INITIALIZED` checks annotate the
check in inode_doinit_with_dentry() with data_race to please KCSAN.
The value is rechecked next with lock held.
Reported-by: syzbot+9ab96b38b76bec93939a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=9ab96b38b76bec93939a
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index d8224ea113d1..6ef9c1aade65 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -1436,7 +1436,8 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent
struct dentry *dentry;
int rc = 0;
- if (isec->initialized == LABEL_INITIALIZED)
+ /* check below is racy, but we will recheck with lock held */
+ if (data_race(isec->initialized == LABEL_INITIALIZED))
return 0;
spin_lock(&isec->lock);
]
|
| 94/2 |
2026/11/18 07:56 |
llm |
cause-finder |
2m
Model:
Tokens:
input: 94002
output: 78
thoughts: 272
Thoughts: **My Eureka Moment: Unraveling a Persistent Data Race**
Wow! I just stumbled upon a veritable treasure trove of syzbot KCSAN reports from 2026 – `c610d2d07879`, `889d3783ae3f`, `12e19bba68ba`, and `8dc51459ef70` all point to the same issue. This is fascinating because it confirms syzbot is indeed exercising KCSAN with `CONFIG_KCSAN_STRICT=y`. What's even more intriguing is that many of the functions involved, like `pid_alive()`, `shmem_getattr()`, and `inode_doinit_with_dentry()`, have been around for ages. This suggests the data race isn't necessarily due to a brand-new function introduction, but rather a subtler interaction.
Now, I need to pause and take a deep breath. It's crucial that I don't jump to conclusions or make assumptions. Instead, my next step is a meticulous, in-depth analysis of the actual root cause and the history of *our* specific data race. I'm going to dive deep into the code, meticulously trace the locking invariants, and scrutinize *every single commit* that touches `kernfs_refresh_inode`, `kernfs_iop_getattr`, and `dnotify_flush`.
To start this rigorous examination, I need to get a solid understanding of the fundamentals. First, I'll be investigating exactly what `inode->i_mode` represents. My immediate action is to look it up in `include/linux/fs.h`, specifically around the `struct inode` definition and the declaration of `i_mode`. This foundational knowledge is key to understanding how file modes are managed and could potentially be a contributing factor to the race condition.
|
| 95/2 |
2026/11/18 07:59 |
tool |
grepper |
0m
Args: map[Expression:umode_t.*i_mode PathPrefixes:[include/linux/fs.h]]
Results: map[Output:include/linux/fs.h=763=struct inode {
include/linux/fs.h:764: umode_t i_mode;
include/linux/fs.h-765- unsigned short i_opflags;
]
|
| 96/2 |
2026/11/18 07:59 |
tool |
read-file |
0m
Args: map[File:include/linux/fs.h FirstLine:730 LineCount:70]
Results: map[Contents: 730: I_DIRTY_DATASYNC = (1U << 5),
731: I_DIRTY_PAGES = (1U << 6),
732: I_WILL_FREE = (1U << 7),
733: I_FREEING = (1U << 8),
734: I_CLEAR = (1U << 9),
735: I_REFERENCED = (1U << 10),
736: I_LINKABLE = (1U << 11),
737: I_DIRTY_TIME = (1U << 12),
738: I_WB_SWITCH = (1U << 13),
739: I_OVL_INUSE = (1U << 14),
740: I_CREATING = (1U << 15),
741: I_DONTCACHE = (1U << 16),
742: I_SYNC_QUEUED = (1U << 17),
743: I_PINNING_NETFS_WB = (1U << 18),
744: I_METADATA_WRITEBACK = (1U << 19),
745: };
746:
747: #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
748: #define I_DIRTY (I_DIRTY_INODE | I_DIRTY_PAGES)
749: #define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
750:
751: /*
752: * Use inode_state_read() & friends to access.
753: */
754: struct inode_state_flags {
755: enum inode_state_flags_enum __state;
756: };
757:
758: /*
759: * Keep mostly read-only and often accessed (especially for
760: * the RCU path lookup and 'stat' data) fields at the beginning
761: * of the 'struct inode'
762: */
763: struct inode {
764: umode_t i_mode;
765: unsigned short i_opflags;
766: unsigned int i_flags;
767: #ifdef CONFIG_FS_POSIX_ACL
768: struct posix_acl *i_acl;
769: struct posix_acl *i_default_acl;
770: #endif
771: kuid_t i_uid;
772: kgid_t i_gid;
773:
774: const struct inode_operations *i_op;
775: struct super_block *i_sb;
776: struct address_space *i_mapping;
777:
778: #ifdef CONFIG_SECURITY
779: void *i_security;
780: #endif
781:
782: /* Stat data, not accessed from path walking */
783: u64 i_ino;
784: /*
785: * Filesystems may only read i_nlink directly. They shall use the
786: * following functions for modification:
787: *
788: * (set|clear|inc|drop)_nlink
789: * inode_(inc|dec)_link_count
790: */
791: union {
792: const unsigned int i_nlink;
793: unsigned int __i_nlink;
794: };
795: dev_t i_rdev;
796: loff_t i_size;
797: time64_t i_atime_sec;
798: time64_t i_mtime_sec;
799: time64_t i_ctime_sec;
]
|
| 97/2 |
2026/11/18 07:59 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 95565
output: 108
thoughts: 101
Thoughts: **Deep Dive into VFS Attribute and Permission Handling**
Okay, I just had a moment of realization – I need to really dig into how the VFS is managing file attributes and permissions. Specifically, I'm seeing a lot of interconnectedness between `inode->i_mode`, `setattr_copy()`, `generic_fillattr()`, `generic_permission()`, `d_is_dir()`, and `S_ISDIR(inode->i_mode)`. This seems like a critical area for understanding file system behavior. My next step is to meticulously examine the implementations of `setattr_copy`, `generic_fillattr`, and `generic_permission`. I'll be focusing my investigation on the relevant files: `fs/attr.c`, `fs/stat.c`, and `fs/namei.c`. I expect to gain significant clarity on the flow of attribute setting and permission checking within the VFS by dissecting these functions and their interactions.
|
| 98/2 |
2026/11/18 07:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/attr.c Name:setattr_copy]
Results: map[SourceCode: 317: /**
318: * setattr_copy - copy simple metadata updates into the generic inode
319: * @idmap: idmap of the mount the inode was found from
320: * @inode: the inode to be updated
321: * @attr: the new attributes
322: *
323: * setattr_copy must be called with i_rwsem held exclusively.
324: *
325: * setattr_copy updates the inode's metadata with that specified
326: * in attr on idmapped mounts. Necessary permission checks to determine
327: * whether or not the S_ISGID property needs to be removed are performed with
328: * the correct idmapped mount permission helpers.
329: * Noticeably missing is inode size update, which is more complex
330: * as it requires pagecache updates.
331: *
332: * If the inode has been found through an idmapped mount the idmap of
333: * the vfsmount must be passed through @idmap. This function will then
334: * take care to map the inode according to @idmap before checking
335: * permissions. On non-idmapped mounts or if permission checking is to be
336: * performed on the raw inode simply pass @nop_mnt_idmap.
337: *
338: * The inode is not marked as dirty after this operation. The rationale is
339: * that for "simple" filesystems, the struct inode is the inode storage.
340: * The caller is free to mark the inode dirty afterwards if needed.
341: */
342: void setattr_copy(const struct mnt_idmap *idmap, struct inode *inode,
343: const struct iattr *attr)
344: {
345: unsigned int ia_valid = attr->ia_valid;
346:
347: i_uid_update(idmap, attr, inode);
348: i_gid_update(idmap, attr, inode);
349: if (ia_valid & ATTR_MODE) {
350: umode_t mode = attr->ia_mode;
351: if (!in_group_or_capable(idmap, inode,
352: i_gid_into_vfsgid(idmap, inode)))
353: mode &= ~S_ISGID;
354: inode->i_mode = mode;
355: }
356:
357: if (is_mgtime(inode))
358: return setattr_copy_mgtime(inode, attr);
359:
360: if (ia_valid & ATTR_ATIME)
361: inode_set_atime_to_ts(inode, attr->ia_atime);
362: if (ia_valid & ATTR_MTIME)
363: inode_set_mtime_to_ts(inode, attr->ia_mtime);
364:
365: if (ia_valid & ATTR_CTIME_SET)
366: inode_set_ctime_deleg(inode, attr->ia_ctime);
367: else if (ia_valid & ATTR_CTIME)
368: inode_set_ctime_to_ts(inode, attr->ia_ctime);
369: }
SourceFile:fs/attr.c]
|
| 99/2 |
2026/11/18 07:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/stat.c Name:generic_fillattr]
Results: map[SourceCode: 65: /**
66: * generic_fillattr - Fill in the basic attributes from the inode struct
67: * @idmap: idmap of the mount the inode was found from
68: * @request_mask: statx request_mask
69: * @inode: Inode to use as the source
70: * @stat: Where to fill in the attributes
71: *
72: * Fill in the basic attributes in the kstat structure from data that's to be
73: * found on the VFS inode structure. This is the default if no getattr inode
74: * operation is supplied.
75: *
76: * If the inode has been found through an idmapped mount the idmap of
77: * the vfsmount must be passed through @idmap. This function will then
78: * take care to map the inode according to @idmap before filling in the
79: * uid and gid filds. On non-idmapped mounts or if permission checking is to be
80: * performed on the raw inode simply pass @nop_mnt_idmap.
81: */
82: void generic_fillattr(const struct mnt_idmap *idmap, u32 request_mask,
83: struct inode *inode, struct kstat *stat)
84: {
85: vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
86: vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
87:
88: stat->dev = inode->i_sb->s_dev;
89: stat->ino = inode->i_ino;
90: stat->mode = inode->i_mode;
91: stat->nlink = inode->i_nlink;
92: stat->uid = vfsuid_into_kuid(vfsuid);
93: stat->gid = vfsgid_into_kgid(vfsgid);
94: stat->rdev = inode->i_rdev;
95: stat->size = i_size_read(inode);
96: stat->atime = inode_get_atime(inode);
97:
98: if (is_mgtime(inode)) {
99: fill_mg_cmtime(stat, request_mask, inode);
100: } else {
101: stat->ctime = inode_get_ctime(inode);
102: stat->mtime = inode_get_mtime(inode);
103: }
104:
105: stat->blksize = i_blocksize(inode);
106: stat->blocks = inode->i_blocks;
107:
108: if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
109: stat->result_mask |= STATX_CHANGE_COOKIE;
110: stat->change_cookie = inode_query_iversion(inode);
111: }
112:
113: }
SourceFile:fs/stat.c]
|
| 100/2 |
2026/11/18 07:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/namei.c Name:generic_permission]
Results: map[SourceCode: 499: /**
500: * generic_permission - check for access rights on a Posix-like filesystem
501: * @idmap: idmap of the mount the inode was found from
502: * @inode: inode to check access rights for
503: * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
504: * %MAY_NOT_BLOCK ...)
505: *
506: * Used to check for read/write/execute permissions on a file.
507: * We use "fsuid" for this, letting us set arbitrary permissions
508: * for filesystem access without changing the "normal" uids which
509: * are used for other things.
510: *
511: * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
512: * request cannot be satisfied (eg. requires blocking or too much complexity).
513: * It would then be called again in ref-walk mode.
514: *
515: * If the inode has been found through an idmapped mount the idmap of
516: * the vfsmount must be passed through @idmap. This function will then take
517: * care to map the inode according to @idmap before checking permissions.
518: * On non-idmapped mounts or if permission checking is to be performed on the
519: * raw inode simply pass @nop_mnt_idmap.
520: */
521: int generic_permission(const struct mnt_idmap *idmap, struct inode *inode,
522: int mask)
523: {
524: int ret;
525:
526: /*
527: * Do the basic permission checks.
528: */
529: ret = acl_permission_check(idmap, inode, mask);
530: if (ret != -EACCES)
531: return ret;
532:
533: if (S_ISDIR(inode->i_mode)) {
534: /* DACs are overridable for directories */
535: if (!(mask & MAY_WRITE))
536: if (capable_wrt_inode_uidgid(idmap, inode,
537: CAP_DAC_READ_SEARCH))
538: return 0;
539: if (capable_wrt_inode_uidgid(idmap, inode,
540: CAP_DAC_OVERRIDE))
541: return 0;
542: return -EACCES;
543: }
544:
545: /*
546: * Searching includes executable on directories, else just read.
547: */
548: mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
549: if (mask == MAY_READ)
550: if (capable_wrt_inode_uidgid(idmap, inode,
551: CAP_DAC_READ_SEARCH))
552: return 0;
553: /*
554: * Read/write DACs are always overridable.
555: * Executable DACs are overridable when there is
556: * at least one exec bit set.
557: */
558: if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
559: if (capable_wrt_inode_uidgid(idmap, inode,
560: CAP_DAC_OVERRIDE))
561: return 0;
562:
563: return -EACCES;
564: }
SourceFile:fs/namei.c]
|
| 101/2 |
2026/11/18 07:59 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 98656
output: 37
thoughts: 78
Thoughts: **My Current Train of Thought**
Hold on a minute. I need to pause and consider how other filesystems in the kernel are handling `getattr` and `inode` refreshes. My immediate instinct is to broaden my perspective beyond just this specific implementation. I need to understand the existing patterns and established practices.
To do this effectively, I'm going to perform a targeted search across the entire `fs/` directory. Specifically, I want to find every instance where `->i_mode =` is being assigned. This will give me a comprehensive view of how inode modes are being manipulated.
More importantly, I need to focus on the critical subset of those assignments: I want to identify **every place in `fs/` that writes to `inode->i_mode` on a LIVE, meaning an already initialized and unlocked, inode**. This is where the potential for race conditions or inconsistencies is highest, and understanding how other, presumably stable, filesystems handle this scenario will be invaluable. It will help me identify potential pitfalls or, conversely, confirm the validity of my current approach by seeing if similar patterns are used elsewhere.
|
| 102/2 |
2026/11/18 08:01 |
tool |
grepper |
0m
Args: map[Expression:->i_mode[[:space:]]*= PathPrefixes:[fs/]]
Results: map[Output:
Full output is too long, showing 200 out of 1228 lines.
Use more precise expression or PathPrefixes if possible.
Number of matching lines per file (120 files in total):
fs/9p/vfs_inode.c:1
fs/9p/vfs_inode_dotl.c:2
fs/adfs/inode.c:2
fs/affs/inode.c:4
fs/affs/namei.c:3
fs/afs/dynroot.c:3
fs/afs/inode.c:4
fs/attr.c:1
fs/autofs/inode.c:1
fs/bad_inode.c:1
fs/befs/linuxvfs.c:1
fs/binfmt_misc.c:1
fs/btrfs/acl.c:1
fs/btrfs/delayed-inode.c:1
fs/btrfs/inode.c:2
fs/btrfs/tests/btrfs-tests.c:1
fs/ceph/caps.c:1
fs/ceph/inode.c:6
fs/coda/cnode.c:1
fs/coda/coda_linux.c:1
fs/configfs/inode.c:2
fs/cramfs/inode.c:1
fs/crypto/keysetup.c:1
fs/debugfs/inode.c:3
fs/devpts/inode.c:2
fs/efivarfs/inode.c:1
fs/erofs/inode.c:2
fs/erofs/ishare.c:1
fs/exfat/inode.c:2
fs/exfat/super.c:1
fs/ext2/acl.c:1
fs/ext2/ialloc.c:1
fs/ext2/inode.c:3
fs/ext4/acl.c:1
fs/ext4/hash-test.c:1
fs/ext4/ialloc.c:2
fs/ext4/inode.c:4
fs/ext4/ioctl.c:1
fs/f2fs/file.c:1
fs/f2fs/inode.c:2
fs/f2fs/recovery.c:1
fs/f2fs/xattr.c:1
fs/failfs.c:1
fs/fat/inode.c:3
fs/fuse/control.c:1
fs/fuse/inode.c:2
fs/gfs2/acl.c:1
fs/gfs2/glops.c:1
fs/gfs2/inode.c:2
fs/hfs/inode.c:3
... and 70 more files
fs/9p/vfs_inode.c=1133=v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
--
fs/9p/vfs_inode.c-1168- mode |= inode->i_mode & ~S_IALLUGO;
fs/9p/vfs_inode.c:1169: inode->i_mode = mode;
fs/9p/vfs_inode.c-1170-
--
fs/9p/vfs_inode_dotl.c=637=v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode,
--
fs/9p/vfs_inode_dotl.c-655- mode |= inode->i_mode & ~S_IALLUGO;
fs/9p/vfs_inode_dotl.c:656: inode->i_mode = mode;
fs/9p/vfs_inode_dotl.c-657-
--
fs/9p/vfs_inode_dotl.c-685- mode |= inode->i_mode & ~S_IALLUGO;
fs/9p/vfs_inode_dotl.c:686: inode->i_mode = mode;
fs/9p/vfs_inode_dotl.c-687- }
--
fs/adfs/inode.c=245=adfs_iget(struct super_block *sb, struct object_info *obj)
--
fs/adfs/inode.c-273-
fs/adfs/inode.c:274: inode->i_mode = adfs_atts2mode(sb, inode);
fs/adfs/inode.c-275- adfs_adfs2unix_time(&ts, inode);
--
fs/adfs/inode.c=302=adfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *attr)
--
fs/adfs/inode.c-341- ADFS_I(inode)->attr = adfs_mode2atts(sb, inode, attr->ia_mode);
fs/adfs/inode.c:342: inode->i_mode = adfs_atts2mode(sb, inode);
fs/adfs/inode.c-343- }
--
fs/affs/inode.c=18=struct inode *affs_iget(struct super_block *sb, unsigned long ino)
--
fs/affs/inode.c-54- set_nlink(inode, 1);
fs/affs/inode.c:55: inode->i_mode = 0;
fs/affs/inode.c-56- AFFS_I(inode)->i_extcnt = 1;
--
fs/affs/inode.c-71- if (affs_test_opt(sbi->s_flags, SF_SETMODE))
fs/affs/inode.c:72: inode->i_mode = sbi->s_mode;
fs/affs/inode.c-73- else
fs/affs/inode.c:74: inode->i_mode = affs_prot_to_mode(prot);
fs/affs/inode.c-75-
--
fs/affs/inode.c-107- } else
fs/affs/inode.c:108: inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
fs/affs/inode.c-109- /* Maybe it should be controlled by mount parameter? */
--
fs/affs/namei.c=245=affs_create(const struct mnt_idmap *idmap, struct inode *dir,
--
fs/affs/namei.c-258-
fs/affs/namei.c:259: inode->i_mode = mode;
fs/affs/namei.c-260- affs_mode_to_prot(inode);
--
fs/affs/namei.c=277=affs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
--
fs/affs/namei.c-289-
fs/affs/namei.c:290: inode->i_mode = mode;
fs/affs/namei.c-291- affs_mode_to_prot(inode);
--
fs/affs/namei.c=316=affs_symlink(const struct mnt_idmap *idmap, struct inode *dir,
--
fs/affs/namei.c-336- inode->i_data.a_ops = &affs_symlink_aops;
fs/affs/namei.c:337: inode->i_mode = S_IFLNK | 0777;
fs/affs/namei.c-338- affs_mode_to_prot(inode);
--
fs/afs/dynroot.c=47=static struct inode *afs_iget_pseudo_dir(struct super_block *sb, ino_t ino)
--
fs/afs/dynroot.c-71- inode->i_size = 0;
fs/afs/dynroot.c:72: inode->i_mode = S_IFDIR | 0555;
fs/afs/dynroot.c-73- inode->i_op = &afs_autocell_inode_operations;
--
fs/afs/dynroot.c=249=static struct dentry *afs_lookup_atcell(struct inode *dir, struct dentry *dentry, ino_t ino)
--
fs/afs/dynroot.c-266- inode->i_size = 0;
fs/afs/dynroot.c:267: inode->i_mode = S_IFLNK | 0555;
fs/afs/dynroot.c-268- inode->i_op = &afs_atcell_inode_operations;
--
fs/afs/dynroot.c=369=struct inode *afs_dynroot_iget_root(struct super_block *sb)
--
fs/afs/dynroot.c-391- inode->i_size = 0;
fs/afs/dynroot.c:392: inode->i_mode = S_IFDIR | 0555;
fs/afs/dynroot.c-393- inode->i_op = &afs_dynroot_inode_operations;
--
fs/afs/inode.c=63=static int afs_inode_init_from_status(struct afs_operation *op,
--
fs/afs/inode.c-101- case AFS_FTYPE_FILE:
fs/afs/inode.c:102: inode->i_mode = S_IFREG | (status->mode & S_IALLUGO);
fs/afs/inode.c-103- inode->i_op = &afs_file_inode_operations;
--
fs/afs/inode.c-108- case AFS_FTYPE_DIR:
fs/afs/inode.c:109: inode->i_mode = S_IFDIR | (status->mode & S_IALLUGO);
fs/afs/inode.c-110- inode->i_op = &afs_dir_inode_operations;
--
fs/afs/inode.c-123-
fs/afs/inode.c:124: inode->i_mode = S_IFDIR | 0555;
fs/afs/inode.c-125- inode->i_op = &afs_mntpt_inode_operations;
--
fs/afs/inode.c-127- } else {
fs/afs/inode.c:128: inode->i_mode = S_IFLNK | status->mode;
fs/afs/inode.c-129- inode->i_op = &afs_symlink_inode_operations;
--
fs/attr.c=342=void setattr_copy(const struct mnt_idmap *idmap, struct inode *inode,
--
fs/attr.c-353- mode &= ~S_ISGID;
fs/attr.c:354: inode->i_mode = mode;
fs/attr.c-355- }
--
fs/autofs/inode.c=436=struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
--
fs/autofs/inode.c-442-
fs/autofs/inode.c:443: inode->i_mode = mode;
fs/autofs/inode.c-444- if (sb->s_root) {
--
fs/bad_inode.c=207=void make_bad_inode(struct inode *inode)
--
fs/bad_inode.c-210-
fs/bad_inode.c:211: inode->i_mode = S_IFREG;
fs/bad_inode.c-212- simple_inode_init_ts(inode);
--
fs/befs/linuxvfs.c=299=static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
--
fs/befs/linuxvfs.c-339-
fs/befs/linuxvfs.c:340: inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
fs/befs/linuxvfs.c-341-
--
fs/binfmt_misc.c=1113=static struct inode *bm_get_inode(struct super_block *sb, umode_t mode)
--
fs/binfmt_misc.c-1118- inode->i_ino = get_next_ino();
fs/binfmt_misc.c:1119: inode->i_mode = mode;
fs/binfmt_misc.c-1120- simple_inode_init_ts(inode);
--
fs/btrfs/acl.c=104=int btrfs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
--
fs/btrfs/acl.c-121- if (ret)
fs/btrfs/acl.c:122: inode->i_mode = old_mode;
fs/btrfs/acl.c-123- return ret;
--
fs/btrfs/delayed-inode.c=1937=int btrfs_fill_inode(struct btrfs_inode *inode, u32 *rdev)
--
fs/btrfs/delayed-inode.c-1959- btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
fs/btrfs/delayed-inode.c:1960: vfs_inode->i_mode = btrfs_stack_inode_mode(inode_item);
fs/btrfs/delayed-inode.c-1961- set_nlink(vfs_inode, btrfs_stack_inode_nlink(inode_item));
--
fs/btrfs/inode.c=4001=static int btrfs_read_locked_inode(struct btrfs_inode *inode, struct btrfs_path *path)
--
fs/btrfs/inode.c-4041- struct btrfs_inode_item);
fs/btrfs/inode.c:4042: vfs_inode->i_mode = btrfs_inode_mode(leaf, inode_item);
fs/btrfs/inode.c-4043- set_nlink(vfs_inode, btrfs_inode_nlink(leaf, inode_item));
--
fs/btrfs/inode.c=5958=static struct btrfs_inode *new_simple_dir(struct inode *dir,
--
fs/btrfs/inode.c-5983- vfs_inode->i_fop = &simple_dir_operations;
fs/btrfs/inode.c:5984: vfs_inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
fs/btrfs/inode.c-5985-
--
fs/btrfs/tests/btrfs-tests.c=57=struct inode *btrfs_new_test_inode(void)
--
fs/btrfs/tests/btrfs-tests.c-64-
fs/btrfs/tests/btrfs-tests.c:65: inode->i_mode = S_IFREG;
fs/btrfs/tests/btrfs-tests.c-66- btrfs_set_inode_number(BTRFS_I(inode), BTRFS_FIRST_FREE_OBJECTID);
--
fs/ceph/caps.c=3742=static void handle_cap_grant(struct inode *inode,
--
fs/ceph/caps.c-3893- else
fs/ceph/caps.c:3894: inode->i_mode = mode;
fs/ceph/caps.c-3895- inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
--
fs/ceph/inode.c=126=struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry,
--
fs/ceph/inode.c-144- inode_state_assign_raw(inode, 0);
fs/ceph/inode.c:145: inode->i_mode = *mode;
fs/ceph/inode.c-146-
--
fs/ceph/inode.c=220=struct inode *ceph_get_snapdir(struct inode *parent)
--
fs/ceph/inode.c-245-
fs/ceph/inode.c:246: inode->i_mode = parent->i_mode;
fs/ceph/inode.c-247- inode->i_uid = parent->i_uid;
--
fs/ceph/inode.c=1084=int ceph_fill_inode(struct inode *inode, struct folio *locked_folio,
--
fs/ceph/inode.c-1114- if (inode_state_read_once(inode) & I_NEW) {
fs/ceph/inode.c:1115: inode->i_mode = mode;
fs/ceph/inode.c-1116- } else {
--
fs/ceph/inode.c-1203- (issued & CEPH_CAP_AUTH_EXCL) == 0) {
fs/ceph/inode.c:1204: inode->i_mode = mode;
fs/ceph/inode.c-1205- inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
--
fs/ceph/inode.c=2645=int __ceph_setattr(const struct mnt_idmap *idmap, struct inode *inode,
--
fs/ceph/inode.c-2797- if (!do_sync && (issued & CEPH_CAP_AUTH_EXCL)) {
fs/ceph/inode.c:2798: inode->i_mode = attr->ia_mode;
fs/ceph/inode.c-2799- dirtied |= CEPH_CAP_AUTH_EXCL;
--
fs/ceph/inode.c-2801- attr->ia_mode != inode->i_mode) {
fs/ceph/inode.c:2802: inode->i_mode = attr->ia_mode;
fs/ceph/inode.c-2803- req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
--
fs/coda/cnode.c=167=struct inode *coda_cnode_makectl(struct super_block *sb)
--
fs/coda/cnode.c-173- inode->i_fop = &coda_ioctl_operations;
fs/coda/cnode.c:174: inode->i_mode = 0444;
]
|
| 103/2 |
2026/11/18 08:01 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 103484
output: 61
thoughts: 205
Thoughts: **Kernel Inode Mode Discovery**
Wow, I've just stumbled upon something absolutely fascinating concerning how various filesystems handle inode modes! I need to pause everything and really absorb this. When I started comparing the standard filesystems to `fs/kernfs/inode.c`, a pattern emerged that I hadn't fully appreciated before.
My initial observation is that in most "normal" filesystems, the `inode->i_mode` field is only ever written to in two specific scenarios:
1. **Inode Initialization**: This happens right at the very beginning, during the `I_NEW` phase. Crucially, this is *before* the inode is made visible to any file operations (`filp`) or path lookups. So, at this point, it's essentially internal setup.
2. **`setattr` (specifically `chmod`)**: This is when the inode mode is *actually changed*. The write occurs only when there's a legitimate modification to the file's permissions. I even double-checked `ceph`'s `__ceph_setattr` function, and sure enough, it explicitly checks `attr->ia_mode != inode->i_mode` before proceeding, reinforcing this point.
This got me thinking about other special-purpose filesystems. What about `proc`, `fuse`, `nfs`, and `configfs`? How do they manage their `getattr` or refresh operations? I'm particularly keen to dig into `fs/proc/`, `fs/fuse/`, and `fs/nfs/` to see their implementation details for refreshing or retrieving attributes, as I suspect they might have a different approach given their unique characteristics. This is a significant insight into inode management across the kernel!
|
| 104/2 |
2026/11/18 08:01 |
tool |
grepper |
0m
Args: map[Expression:->i_mode[[:space:]]*= PathPrefixes:[fs/proc/ fs/fuse/ fs/nfs/ fs/configfs/]]
Results: map[Output:fs/configfs/inode.c=88=static inline void set_default_inode_attr(struct inode * inode, umode_t mode)
fs/configfs/inode.c-89-{
fs/configfs/inode.c:90: inode->i_mode = mode;
fs/configfs/inode.c-91- simple_inode_init_ts(inode);
--
fs/configfs/inode.c=94=static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
fs/configfs/inode.c-95-{
fs/configfs/inode.c:96: inode->i_mode = iattr->ia_mode;
fs/configfs/inode.c-97- inode->i_uid = iattr->ia_uid;
--
fs/fuse/control.c=198=static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
--
fs/fuse/control.c-217- inode->i_ino = get_next_ino();
fs/fuse/control.c:218: inode->i_mode = mode;
fs/fuse/control.c-219- inode->i_uid = fc->user_id;
--
fs/fuse/inode.c=210=void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
--
fs/fuse/inode.c-237- inode->i_ino = fuse_squash_ino(attr->ino);
fs/fuse/inode.c:238: inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
fs/fuse/inode.c-239- set_nlink(inode, attr->nlink);
--
fs/fuse/inode.c=410=static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
--
fs/fuse/inode.c-412-{
fs/fuse/inode.c:413: inode->i_mode = attr->mode & S_IFMT;
fs/fuse/inode.c-414- inode->i_size = attr->size;
--
fs/nfs/inode.c=325=nfs_init_locked(struct inode *inode, void *opaque)
--
fs/nfs/inode.c-330- inode->i_ino = fattr->fileid;
fs/nfs/inode.c:331: inode->i_mode = fattr->mode;
fs/nfs/inode.c-332- nfs_copy_fh(NFS_FH(inode), desc->fh);
--
fs/nfs/inode.c=436=nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
--
fs/nfs/inode.c-468- inode->i_flags |= S_NOATIME|S_NOCMTIME;
fs/nfs/inode.c:469: inode->i_mode = fattr->mode;
fs/nfs/inode.c-470- nfsi->cache_validity = 0;
--
fs/nfs/inode.c=862=void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
--
fs/nfs/inode.c-888- mode |= inode->i_mode & ~S_IALLUGO;
fs/nfs/inode.c:889: inode->i_mode = mode;
fs/nfs/inode.c-890- }
--
fs/nfs/inode.c=2277=static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
--
fs/nfs/inode.c-2462- newmode |= fattr->mode & S_IALLUGO;
fs/nfs/inode.c:2463: inode->i_mode = newmode;
fs/nfs/inode.c-2464- invalid |= NFS_INO_INVALID_ACCESS
--
fs/proc/base.c=1923=struct inode *proc_pid_make_inode(struct super_block *sb,
--
fs/proc/base.c-1937- ei = PROC_I(inode);
fs/proc/base.c:1938: inode->i_mode = mode;
fs/proc/base.c-1939- inode->i_ino = get_next_ino();
--
fs/proc/fd.c=127=static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
--
fs/proc/fd.c-137- i_mode |= S_IWUSR | S_IXUSR;
fs/proc/fd.c:138: inode->i_mode = i_mode;
fs/proc/fd.c-139- }
--
fs/proc/inode.c=628=struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
--
fs/proc/inode.c-646- if (de->mode) {
fs/proc/inode.c:647: inode->i_mode = de->mode;
fs/proc/inode.c-648- inode->i_uid = de->uid;
--
fs/proc/proc_sysctl.c=449=static struct inode *proc_sys_make_inode(struct super_block *sb,
--
fs/proc/proc_sysctl.c-476- simple_inode_init_ts(inode);
fs/proc/proc_sysctl.c:477: inode->i_mode = table->mode;
fs/proc/proc_sysctl.c-478- if (!S_ISDIR(table->mode)) {
--
fs/proc/self.c=36=int proc_setup_self(struct super_block *s)
--
fs/proc/self.c-46- simple_inode_init_ts(inode);
fs/proc/self.c:47: inode->i_mode = S_IFLNK | S_IRWXUGO;
fs/proc/self.c-48- inode->i_uid = GLOBAL_ROOT_UID;
--
fs/proc/thread_self.c=36=int proc_setup_thread_self(struct super_block *s)
--
fs/proc/thread_self.c-46- simple_inode_init_ts(inode);
fs/proc/thread_self.c:47: inode->i_mode = S_IFLNK | S_IRWXUGO;
fs/proc/thread_self.c-48- inode->i_uid = GLOBAL_ROOT_UID;
]
|
| 105/2 |
2026/11/18 08:01 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 105449
output: 116
thoughts: 102
Thoughts: **Eureka! A Pattern Emerges in Kernel Inode Handling**
I was just scrutinizing the inode update logic across different filesystems, and it struck me as incredibly significant. My attention was drawn to `fs/nfs/inode.c` specifically around line 2463, where `nfs_update_inode` resides, and then I simultaneously considered `fs/fuse/inode.c` near line 238, the domain of `fuse_change_attributes_common`. Immediately after that, my focus broadened to include `fs/kernfs/inode.c` as well. The sudden realization, the "WAIT A SECOND!" moment, came when I started diving into the details of these functions. There's a clear thematic overlap in how these distinct filesystem implementations are managing inode attribute updates. It's not just a superficial similarity; I suspect there's a deeper architectural principle or a shared defensive coding strategy at play here. My immediate inclination is to thoroughly understand *why* these specific lines and functions are so critical and what commonalities in their operation are suggesting a broader pattern.
|
| 106/2 |
2026/11/18 08:02 |
tool |
read-file |
0m
Args: map[File:fs/nfs/inode.c FirstLine:2450 LineCount:25]
Results: map[Contents:2450: nfsi->cache_validity |=
2451: save_cache_validity & NFS_INO_INVALID_SIZE;
2452:
2453: if (fattr->valid & NFS_ATTR_FATTR_ATIME)
2454: inode_set_atime_to_ts(inode, fattr->atime);
2455: else if (fattr_supported & NFS_ATTR_FATTR_ATIME)
2456: nfsi->cache_validity |=
2457: save_cache_validity & NFS_INO_INVALID_ATIME;
2458:
2459: if (fattr->valid & NFS_ATTR_FATTR_MODE) {
2460: if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
2461: umode_t newmode = inode->i_mode & S_IFMT;
2462: newmode |= fattr->mode & S_IALLUGO;
2463: inode->i_mode = newmode;
2464: invalid |= NFS_INO_INVALID_ACCESS
2465: | NFS_INO_INVALID_ACL;
2466: }
2467: } else if (fattr_supported & NFS_ATTR_FATTR_MODE)
2468: nfsi->cache_validity |=
2469: save_cache_validity & NFS_INO_INVALID_MODE;
2470:
2471: if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
2472: if (!uid_eq(inode->i_uid, fattr->uid)) {
2473: invalid |= NFS_INO_INVALID_ACCESS
2474: | NFS_INO_INVALID_ACL;
]
|
| 107/2 |
2026/11/18 08:02 |
tool |
read-file |
0m
Args: map[File:fs/fuse/inode.c FirstLine:225 LineCount:30]
Results: map[Contents: 225: * if the attr_version would've been preserved.
226: *
227: * !evict_ctr -> this is create
228: * fi->attr_version != 0 -> this is not a new inode
229: * evict_ctr == fuse_get_evict_ctr() -> no evicts while during request
230: */
231: if (!evict_ctr || fi->attr_version || evict_ctr == fuse_get_evict_ctr(fc))
232: set_mask_bits(&fi->inval_mask, STATX_BASIC_STATS, 0);
233:
234: fi->attr_version = atomic64_inc_return(&fc->attr_version);
235: fi->i_time = attr_valid;
236:
237: inode->i_ino = fuse_squash_ino(attr->ino);
238: inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
239: set_nlink(inode, attr->nlink);
240: inode->i_uid = make_kuid(fc->user_ns, attr->uid);
241: inode->i_gid = make_kgid(fc->user_ns, attr->gid);
242: inode->i_blocks = attr->blocks;
243:
244: /* Sanitize nsecs */
245: attr->atimensec = min_t(u32, attr->atimensec, NSEC_PER_SEC - 1);
246: attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
247: attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
248:
249: inode_set_atime(inode, attr->atime, attr->atimensec);
250: /* mtime from server may be stale due to local buffered write */
251: if (!(cache_mask & STATX_MTIME)) {
252: inode_set_mtime(inode, attr->mtime, attr->mtimensec);
253: }
254: if (!(cache_mask & STATX_CTIME)) {
]
|
| 108/2 |
2026/11/18 08:02 |
tool |
read-file |
0m
Args: map[File:fs/fuse/inode.c FirstLine:280 LineCount:45]
Results: map[Contents: 280: fi->cached_i_blkbits = inode->i_sb->s_blocksize_bits;
281:
282: /*
283: * Don't set the sticky bit in i_mode, unless we want the VFS
284: * to check permissions. This prevents failures due to the
285: * check in may_delete().
286: */
287: fi->orig_i_mode = inode->i_mode;
288: if (!fc->default_permissions)
289: inode->i_mode &= ~S_ISVTX;
290:
291: fi->orig_ino = attr->ino;
292:
293: /*
294: * We are refreshing inode data and it is possible that another
295: * client set suid/sgid or security.capability xattr. So clear
296: * S_NOSEC. Ideally, we could have cleared it only if suid/sgid
297: * was set or if security.capability xattr was set. But we don't
298: * know if security.capability has been set or not. So clear it
299: * anyway. Its less efficient but should be safe.
300: */
301: inode->i_flags &= ~S_NOSEC;
302: }
303:
304: u32 fuse_get_cache_mask(struct inode *inode)
305: {
306: struct fuse_conn *fc = get_fuse_conn(inode);
307:
308: if (!fc->writeback_cache || !S_ISREG(inode->i_mode))
309: return 0;
310:
311: return STATX_MTIME | STATX_CTIME | STATX_SIZE;
312: }
313:
314: static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr,
315: struct fuse_statx *sx, u64 attr_valid,
316: u64 attr_version, u64 evict_ctr)
317: {
318: struct fuse_conn *fc = get_fuse_conn(inode);
319: struct fuse_inode *fi = get_fuse_inode(inode);
320: u32 cache_mask;
321: loff_t oldsize;
322: struct timespec64 old_mtime;
323:
324: spin_lock(&fi->lock);
]
|
| 109/2 |
2026/11/18 08:02 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 107554
output: 115
thoughts: 1290
Thoughts: **Hold on! My understanding of the KCSAN configuration used by syzbot requires a significant re-evaluation.**
I've been operating under the assumption that syzbot was running KCSAN with `CONFIG_KCSAN_STRICT=y`. However, a closer look at the KCSAN source code, specifically `kernel/kcsan/core.c` lines 821-826, has made me question this. This section prints a warning if KCSAN is *not* in strict mode, indicating that not all data races will be reported.
My suspicion was further amplified when I meticulously re-examined `kernel/kcsan/report.c`, particularly the `skip_report` and `print_report` functions, including lines 180-195 and 406-425. The logic within `skip_report` is crucial here. It defines conditions under which a potential data race report might be skipped.
Specifically, the comment within `skip_report` outlines several scenarios. With `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY=y`, scenario 5 – "write watchpoint, conflicting read (value_change==MAYBE)" – is explicitly **SKIPPED**.
Now, let's apply this to the crash report I'm examining:
* The "watchpoint thread" (`kernfs_iop_getattr`, PID 3283 on CPU 0) is the **WRITE**.
* The "other thread" (`dnotify_flush`, PID 3286 on CPU 1) is the **READ**.
* The report indicates that `value changed:` was *not* printed, which implies `diff == 0`, meaning `value_change` was `KCSAN_VALUE_CHANGE_MAYBE`.
Given these facts, if `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY=y` were set, the `skip_report(value_change, other_frame)` call for `dnotify_flush` (which does not contain "rcu_", "_rcu", or "_srcu" in its name) *should* have returned `true`, thus skipping the report.
**However, the report *was* generated.** This leads me to a definitive conclusion: `IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY)` *must be false* in the kernel that produced this crash. Therefore, `CONFIG_KCSAN_STRICT=y` is unequivocally enabled. This is mathematically proven by the code's behavior in `kernel/kcsan/report.c`.
**With this crucial clarification about the KCSAN configuration, I can now proceed with identifying *why* this is a bug and precisely *which commit* introduced it.**
My next step is to meticulously examine ALL candidate commits in the history of `fs/kernfs/inode.c`. I will specifically focus on any commits that touched locking mechanisms, `kernfs_refresh_inode`, or `kernfs_iop_getattr`. I'll trace the timeline chronologically from 2009 to the present, looking at commits before `e61ab4ae48fb` (November 2009) to understand the behavior of `fs/sysfs/inode.c` prior to that change. I'll be looking at `e61ab4ae48fb~1:fs/sysfs/inode.c` or the state immediately after `e61ab4ae48fb` to understand what it replaced.
|
| 110/2 |
2026/11/18 08:03 |
tool |
git-log |
0m
Args: map[Count:15 PathPrefix:fs/sysfs/]
Results: map[Output:454257f6d124 sysfs: clamp show() return value in sysfs_kf_read()
237557b8a81a sysfs: don't remove existing directory on update failure
78d797520f6a sysfs: remove trivial sysfs_get_tree() wrapper
dc4edae7f41d fs: move SB_I_USERNS_VISIBLE to FS_USERNS_MOUNT_RESTRICTED
f71cc65c01bd sysfs: upgrade OOB write by buggy .show hook into WARNing
9ce4a8c07b28 sysfs: attribute_group: Respect is_visible_const() when changing owner
e3b2cf6e5dba kernfs: pass struct ns_common instead of const void * for namespace tags
fe2511adb1fc sysfs: constify group arrays in function arguments
bf4afc53b77a Convert 'alloc_obj' family to use the new default GFP_KERNEL argument
69050f8d6d07 treewide: Replace kmalloc with kmalloc_obj for non-scalar types
d79ff2d61202 sysfs: remove exports of sysfs_*change_owner()
7dd9fdb4939b sysfs: attribute_group: enable const variants of is_visible()
c7fbb8218b4a sysfs: check visibility before changing group attribute ownership
1d6249c1ce82 sysfs: remove bin_attribute::read_new/write_new()
340d8e66c43f sysfs: constify internal references to 'struct bin_attribute'
]
|
| 111/2 |
2026/11/18 08:03 |
tool |
git-show |
0m
Args: map[Commit:e61ab4ae48fb~1 Stat:true]
Results: map[Output:commit c099aacd48ee73bd2de7da029e536ed005d72a43
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Nov 20 16:08:52 2009 -0800
sysfs: Nicely indent sysfs_symlink_inode_operations
Lining up the functions in sysfs_symlink_inode_operations
follows the pattern in the rest of sysfs and makes things
slightly more readable.
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
fs/sysfs/symlink.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
]
|
| 112/2 |
2026/11/18 08:03 |
tool |
git-log |
0m
Args: map[CodeRegexp:sysfs_refresh_inode|sysfs_setattr_inode|refresh_inode Count:20 PathPrefix:fs/sysfs/]
Results: map[Output:ffed24e22845 sysfs, kernfs: move inode code to fs/kernfs/inode.c
e61ab4ae48fb sysfs: Implement sysfs_getattr & sysfs_permission
]
|
| 113/2 |
2026/11/18 08:03 |
llm |
cause-finder |
13m
Model:
Tokens:
input: 109708
output: 157
thoughts: 61
Thoughts: **Focusing on the sysfs-related changes within a specific commit series.**
My immediate thought is to dive into the commit history, specifically looking for any modifications that occurred *after* a particular commit, `e61ab4ae48fb`, which I know is part of a series by Eric Biederman. I'm trying to pinpoint changes related to `sysfs`. To do this efficiently, I'll leverage `git-log`. I can either filter the log output directly using a message regular expression like `"sysfs:"`, which will show me commits with "sysfs" in their messages, or I can examine a specific commit hash, `fac2622bbad4`, if I suspect it's a good entry point or marker for this particular `sysfs`-related work. The goal is to isolate and understand the impact of these `sysfs` changes in that particular temporal slice of the commit history.
|
| 114/2 |
2026/11/18 08:17 |
tool |
git-show |
0m
Args: map[Commit:fac2622bbad4 Stat:true]
Results: map[Output:commit fac2622bbad4d7e3a32c53e631e018b80ec631dc
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Feb 12 19:22:27 2010 -0800
sysfs: Pass super_block to sysfs_get_inode
Currently sysfs_get_inode magically returns an inode on
sysfs_sb. Make the super_block parameter explicit and
the code becomes clearer.
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
fs/sysfs/dir.c | 2 +-
fs/sysfs/inode.c | 5 +++--
fs/sysfs/mount.c | 2 +-
fs/sysfs/sysfs.h | 2 +-
4 files changed, 6 insertions(+), 5 deletions(-)
]
|
| 115/2 |
2026/11/18 08:17 |
tool |
git-show |
0m
Args: map[Commit:fac2622bbad4~1 Stat:true]
Results: map[Output:commit 2354dcc7218853a6537ec722be40fde9a11c413b
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Feb 12 19:22:26 2010 -0800
driver core: Use sysfs_rename_link in device_rename
Don't open code the renaming of symlinks in sysfs
instead use the new helper function sysfs_rename_link
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
drivers/base/core.c | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)
]
|
| 116/2 |
2026/11/18 08:17 |
tool |
git-show |
0m
Args: map[Commit:fac2622bbad4~2 Stat:true]
Results: map[Output:commit 7cb32942d91a501b2df944928ccc9e6590ab237b
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Feb 12 19:22:25 2010 -0800
sysfs: Implement sysfs_rename_link
Because of rename ordering problems we occassionally give false
warnings about invalid sysfs operations. So using sysfs_rename
create a sysfs_rename_link function that doesn't need strange
workarounds.
Cc: Benjamin Thery <benjamin.thery@bull.net>
Cc: Daniel Lezcano <dlezcano@fr.ibm.com>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
fs/sysfs/symlink.c | 38 ++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 9 +++++++++
2 files changed, 47 insertions(+)
]
|
| 117/2 |
2026/11/18 08:17 |
tool |
git-show |
0m
Args: map[Commit:fac2622bbad4~3 Stat:true]
Results: map[Output:commit 19c38b632d4554329986021fd66bdea468cf452c
Author: Eric W. Biederman <ebiederm@aristanetworks.com>
Date: Fri Feb 12 19:22:24 2010 -0800
sysfs: Pack sysfs_dirent more tightly.
Placing the 16bit s_mode between a pointer and a long doesn't pack well
especailly on 64bit where we wast 48 bits. So move s_mode and
declare it as a unsigned short. This is the sysfs backing store
after all we don't need fields extra large just in case someday
we want userspace to be able to use a larger value.
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
fs/sysfs/sysfs.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
]
|
| 118/2 |
2026/11/18 08:17 |
tool |
git-show |
0m
Args: map[Commit:fac2622bbad4~4 Stat:true]
Results: map[Output:commit f8d4f618fed5a4978afada52166bc2efaf7656d1
Author: Eric W. Biederman <ebiederm@aristanetworks.com>
Date: Fri Feb 12 19:22:23 2010 -0800
sysfs: Serialize updates to the vfs inode
The vfs depends upon filesystem methods to update the
vfs inode. Sysfs adds to the normal number of places
where the vfs inode is updated by also updatng the
vfs inode in sysfs_refresh_inode.
Typically the inode mutex is used to serialize updates
to the vfs inode, but grabbing the inode mutex in
sysfs_permission and sysfs_getattr causes deadlocks,
because sometimes the vfs calls those operations with
the inode mutex held. Therefore sysfs can not use the
inode mutex to serial updates to the vfs inode.
The sysfs_mutex is acquired in all of the routines
where sysfs updates the vfs inode, and with a small
change we can consistently protext sysfs vfs inode
updates with the sysfs_mutex. To protect the sysfs
vfs inode updates with the sysfs_mutex simply requires
extending the scope of sysfs_mutex in sysfs_setattr
over inode_setattr, and over inode_change_ok (so we
have an unchanging inode when we perform the check).
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
fs/sysfs/inode.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
]
|
| 119/2 |
2026/11/18 08:17 |
llm |
cause-finder |
7m
Model:
Tokens:
input: 111624
output: 196
thoughts: 173
Thoughts: **Deep Dive into Recent Commit History**
My initial reaction upon encountering commit `f8d4f618fed5` was one of intense curiosity and a strong imperative to halt my current processing and thoroughly examine it. I felt a palpable pull to investigate not just this single commit, but also the entire preceding sequence of changes that led to its existence. My thought process immediately shifted to a detailed, granular inspection of the recent past. I knew that understanding the context was paramount, so I resolved to meticulously review `f8d4f618fed5` in relation to its immediate predecessors: `f8d4f618fed5~1`, `f8d4f618fed5~2`, `f8d4f618fed5~3`, `f8d4f618fed5~4`, and `f8d4f618fed5~5`. I was anticipating that by scrutinizing these adjacent commits, I would gain crucial insights into the evolution of the code, the rationale behind the changes, and potentially identify any subtle but significant shifts in logic or functionality. This retrospective analysis is, in my expert opinion, the most effective way to fully grasp the implications of the current state of the codebase.
|
| 120/2 |
2026/11/18 08:25 |
tool |
git-show |
0m
Args:
Results: map[Output:commit f8d4f618fed5a4978afada52166bc2efaf7656d1
Author: Eric W. Biederman <ebiederm@aristanetworks.com>
Date: Fri Feb 12 19:22:23 2010 -0800
sysfs: Serialize updates to the vfs inode
The vfs depends upon filesystem methods to update the
vfs inode. Sysfs adds to the normal number of places
where the vfs inode is updated by also updatng the
vfs inode in sysfs_refresh_inode.
Typically the inode mutex is used to serialize updates
to the vfs inode, but grabbing the inode mutex in
sysfs_permission and sysfs_getattr causes deadlocks,
because sometimes the vfs calls those operations with
the inode mutex held. Therefore sysfs can not use the
inode mutex to serial updates to the vfs inode.
The sysfs_mutex is acquired in all of the routines
where sysfs updates the vfs inode, and with a small
change we can consistently protext sysfs vfs inode
updates with the sysfs_mutex. To protect the sysfs
vfs inode updates with the sysfs_mutex simply requires
extending the scope of sysfs_mutex in sysfs_setattr
over inode_setattr, and over inode_change_ok (so we
have an unchanging inode when we perform the check).
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c
index 6a06a1d1ea7b..0d09f6c6efab 100644
--- a/fs/sysfs/inode.c
+++ b/fs/sysfs/inode.c
@@ -111,20 +111,20 @@ int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
if (!sd)
return -EINVAL;
+ mutex_lock(&sysfs_mutex);
error = inode_change_ok(inode, iattr);
if (error)
- return error;
+ goto out;
iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */
error = inode_setattr(inode, iattr);
if (error)
- return error;
+ goto out;
- mutex_lock(&sysfs_mutex);
error = sysfs_sd_setattr(sd, iattr);
+out:
mutex_unlock(&sysfs_mutex);
-
return error;
}
]
|
| 121/2 |
2026/11/18 08:25 |
tool |
git-show |
0m
Args: map[Commit:f8d4f618fed5~1 Stat:true]
Results: map[Output:commit b35c74dab64f4c212f36d868419108556c9cf3e2
Author: Johannes Berg <johannes@sipsolutions.net>
Date: Sat Feb 20 16:43:02 2010 +0100
sysfs: windfarm: init sysfs attributes
This is required for lockdep.
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
drivers/macintosh/windfarm_core.c | 1 +
drivers/macintosh/windfarm_smu_controls.c | 1 +
2 files changed, 2 insertions(+)
]
|
| 122/2 |
2026/11/18 08:25 |
tool |
git-show |
0m
Args: map[Commit:f8d4f618fed5~2 Stat:true]
Results: map[Output:commit 361795b1eb7c08e9e65a2ebb4a4e536294d378a2
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Feb 12 13:41:56 2010 -0800
sysfs: Use sysfs_attr_init and sysfs_bin_attr_init on module dynamic attributes
A little more whack-a-mole annotating the dynamic sysfs attributes. I
had everything built into my earlier test kernel, and so I missed
these.
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
kernel/module.c | 3 +++
1 file changed, 3 insertions(+)
]
|
| 123/2 |
2026/11/18 08:25 |
tool |
git-show |
0m
Args: map[Commit:f8d4f618fed5~3 Stat:true]
Results: map[Output:commit 35960258ed388cdcebdb71df35fd5126978ca325
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Feb 12 04:35:32 2010 -0800
sysfs: Document sysfs_attr_init and sysfs_bin_attr_init
I have added a new requirement to the external sysfs interface
that dynamically allocated sysfs attributes must call sysfs_attr_init
if lockdep is enabled. For the time being callying sysfs_attr_init
is only mandatory if lockdep is enabled, so we can live with a few
unconverted instances until we find them all. As this is part of
the public interface of sysfs it is a good idea to document these
pseudo functions so someone inspeciting the code can find out
what has happened.
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
include/linux/sysfs.h | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
]
|
| 124/2 |
2026/11/18 08:25 |
tool |
git-show |
0m
Args: map[Commit:f8d4f618fed5~4 Stat:true]
Results: map[Output:commit a07e4156a2ee6359d31a44946d7ee7f85dbf6bca
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Thu Feb 11 15:23:05 2010 -0800
sysfs: Use sysfs_attr_init and sysfs_bin_attr_init on dynamic attributes
These are the non-static sysfs attributes that exist on
my test machine. Fix them to use sysfs_attr_init or
sysfs_bin_attr_init as appropriate. It simply requires
making a sysfs attribute present to see this. So this
is a little bit tedious but otherwise not too bad.
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Acked-by: WANG Cong <xiyou.wangcong@gmail.com>
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
arch/x86/kernel/cpu/mcheck/mce.c | 1 +
drivers/acpi/system.c | 2 ++
drivers/pci/pci-sysfs.c | 5 +++++
kernel/params.c | 1 +
4 files changed, 9 insertions(+)
]
|
| 125/2 |
2026/11/18 08:25 |
tool |
git-show |
0m
Args: map[Commit:f8d4f618fed5~5 Stat:true]
Results: map[Output:commit 6992f5334995af474c2b58d010d08bc597f0f2fe
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Thu Feb 11 15:21:53 2010 -0800
sysfs: Use one lockdep class per sysfs attribute.
Acknowledge that the logical sysfs rwsem has one instance per
sysfs attribute with different locking depencencies for different
attributes.
There is a sysfs idiom where writing to one sysfs file causes the
addition or removal of other sysfs files. Lumping all of the
sysfs attributes together in one lock class causes lockdep to
generate lots of false positives.
This introduces the requirement that non-static sysfs attributes
need to be initialized with sysfs_attr_init or sysfs_bin_attr_init.
Strictly speaking this requirement only exists when lockdep is
enabled, and when lockdep is enabled we get a bit fat warning
if this requirement is not met.
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Acked-by: WANG Cong <xiyou.wangcong@gmail.com>
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
fs/sysfs/sysfs.h | 7 +++++--
include/linux/sysfs.h | 18 ++++++++++++++++++
2 files changed, 23 insertions(+), 2 deletions(-)
]
|
| 126/2 |
2026/11/18 08:25 |
llm |
cause-finder |
10m
Model:
Tokens:
input: 114274
output: 43
thoughts: 408
Thoughts: **A Realization About Sysfs Inode Attribute Handling**
Wait a second! I need to stop and examine four specific commits side-by-side: `f8d4f618fed5`, `47b5c64d0ab5`, `aa1d058d48f2`, and `e61ab4ae48fb`. I need to read all four of these commits *very carefully*.
Let's start with commit `e61ab4ae48fb`, dated November 2009. This commit introduced `sysfs_getattr` and `sysfs_permission`. I see the `sysfs_refresh_inode` function here, which takes a `struct sysfs_dirent` and a `struct inode`, and importantly, it sets `inode->i_mode = sd->s_mode`. Then, within `sysfs_getattr`, there's a call to `sysfs_refresh_inode` after acquiring the `sysfs_mutex`. This seems to be the mechanism for updating the inode's mode based on the `sysfs_dirent`'s stored mode.
Now, this makes me wonder: what was the behavior *before* this change, specifically when `sysfs_chmod_file` or `sysfs_setattr` were called? I need to dig into the `git log` for `fs/sysfs/inode.c` right around the time of `e61ab4ae48fb`. I'll specifically look at the commits from `f8d4f618fed5~6` up to `e61ab4ae48fb`. This should reveal the state of affairs concerning how sysfs handled inode attributes and permissions before this particular set of functions was implemented.
|
| 127/2 |
2026/11/18 08:36 |
tool |
git-log |
0m
Args: map[Count:25 PathPrefix:fs/sysfs/inode.c Since:18 years]
Results: map[Output:ffed24e22845 sysfs, kernfs: move inode code to fs/kernfs/inode.c
ccf73cf336dc sysfs, kernfs: introduce kernfs[_find_and]_get() and kernfs_put()
a7dc66dfb4c6 sysfs, kernfs: remove SYSFS_KOBJ_BIN_ATTR
471bd7b78bd5 sysfs, kernfs: add sysfs_dirent->s_attr.size
c6fb449515f2 sysfs, kernfs: prepare open, release, poll paths for kernfs
7c6e2d362c19 sysfs, kernfs: replace sysfs_dirent->s_dir.kobj and ->s_attr.[bin_]attr with ->priv
5d60418e5475 sysfs, kernfs: introduce kernfs_setattr()
7eed6ecb0785 sysfs: move sysfs_hash_and_remove() to fs/sysfs/dir.c
56b3f3b88465 sysfs: merge sysfs_elem_bin_attr into sysfs_elem_attr
3124eb1679b2 sysfs: merge regular and bin file handling
250f7c3fee52 sysfs: introduce [__]sysfs_remove()
d69ac5a0bbcf sysfs: remove sysfs_addrm_cxt->parent_sd
cfec0bc835c8 sysfs: @name comes before @ns
ddfd6d074e0f sysfs: fix up 80 column coding style issues
1b18dc2beb31 sysfs: fix up space coding style issues
ab9bf4be4dd5 sysfs: remove trailing whitespace
5240d58c0449 sysfs: kill sysfs_sb declaration in fs/sysfs/inode.c.
ab27b91b9f19 userns: Convert sysfs to use kgid/kuid where appropriate
dbd5768f87ff vfs: Rename end_writeback() to clear_inode()
54d20f006cef Revert "sysfs: Kill nlink counting."
93518dd2ebaf sysfs: Fix memory leak in sysfs_sd_setsecdata().
524b6c5b39b9 sysfs: Kill nlink counting.
ce597919361d sysfs: Complain bitterly about attempts to remove files from nonexistent directories.
faef2b6c9960 sysfs: propagate umode_t
bfe868486960 filesystems: add set_nlink()
]
|
| 128/2 |
2026/11/18 08:36 |
llm |
cause-finder |
7m
Model:
Tokens:
input: 115380
output: 35
thoughts: 45
Thoughts: **My Thought Process for Analyzing `fs/sysfs/inode.c` Commits**
Right, so my first instinct here is to get a focused view on recent changes within a specific file that I'm particularly interested in. The file is `fs/sysfs/inode.c`. I know this file is crucial for how the sysfs filesystem interacts with inodes, so any recent development there is likely to be significant.
To get the most relevant recent history, I'm going to use `git log`. I don't need to specify a starting point with `--since` because I'm assuming "now" – HEAD – is the most up-to-date point I care about. The date of HEAD being 2026 is a bit of a curveball, but it doesn't fundamentally change my approach; I'm still looking for recent history relative to the current tip of the repository.
Instead, I want to limit the *number* of commits I see to keep the output manageable and focused. I don't need a sprawling history; I'm looking for the most impactful or recent batch of changes. Therefore, I'm going to use the `--count` (or `-n`) option, setting it to `60`. This gives me the last 60 commits that have touched this specific file. This number feels like a good balance – enough to capture a meaningful trend or set of recent fixes/features, but not so many that it becomes overwhelming to review.
So, the command I'm mentally constructing is `git log --count 60 fs/sysfs/inode.c`. This will fetch the most recent 60 commits that have modified `fs/sysfs/inode.c`, presented in chronological order (newest first by default with `git log`), which is exactly what I need to begin my analysis.
|
| 129/2 |
2026/11/18 08:43 |
tool |
git-log |
0m
Args: map[Count:60 PathPrefix:fs/sysfs/inode.c]
Results: map[Output:ffed24e22845 sysfs, kernfs: move inode code to fs/kernfs/inode.c
ccf73cf336dc sysfs, kernfs: introduce kernfs[_find_and]_get() and kernfs_put()
a7dc66dfb4c6 sysfs, kernfs: remove SYSFS_KOBJ_BIN_ATTR
471bd7b78bd5 sysfs, kernfs: add sysfs_dirent->s_attr.size
c6fb449515f2 sysfs, kernfs: prepare open, release, poll paths for kernfs
7c6e2d362c19 sysfs, kernfs: replace sysfs_dirent->s_dir.kobj and ->s_attr.[bin_]attr with ->priv
5d60418e5475 sysfs, kernfs: introduce kernfs_setattr()
7eed6ecb0785 sysfs: move sysfs_hash_and_remove() to fs/sysfs/dir.c
56b3f3b88465 sysfs: merge sysfs_elem_bin_attr into sysfs_elem_attr
3124eb1679b2 sysfs: merge regular and bin file handling
250f7c3fee52 sysfs: introduce [__]sysfs_remove()
d69ac5a0bbcf sysfs: remove sysfs_addrm_cxt->parent_sd
cfec0bc835c8 sysfs: @name comes before @ns
ddfd6d074e0f sysfs: fix up 80 column coding style issues
1b18dc2beb31 sysfs: fix up space coding style issues
ab9bf4be4dd5 sysfs: remove trailing whitespace
5240d58c0449 sysfs: kill sysfs_sb declaration in fs/sysfs/inode.c.
ab27b91b9f19 userns: Convert sysfs to use kgid/kuid where appropriate
dbd5768f87ff vfs: Rename end_writeback() to clear_inode()
54d20f006cef Revert "sysfs: Kill nlink counting."
93518dd2ebaf sysfs: Fix memory leak in sysfs_sd_setsecdata().
524b6c5b39b9 sysfs: Kill nlink counting.
ce597919361d sysfs: Complain bitterly about attempts to remove files from nonexistent directories.
faef2b6c9960 sysfs: propagate umode_t
bfe868486960 filesystems: add set_nlink()
23396180a977 sysfs: Remove support for tagged directories with untagged members.
7f9838fd0183 sysfs: count subdirectories
10556cb21a0d ->permission() sanitizing: don't pass flags to ->permission()
2830ba7f34eb ->permission() sanitizing: don't pass flags to generic_permission()
178ea73521d6 kill check_acl callback of generic_permission()
57cc7215b708 headers: kobject.h redux
b74c79e99389 fs: provide rcu-walk aware permission i_ops
01cd9fef6eb3 switch sysfs to ->evict_inode()
6a1a90ad1b0e rename generic_setattr
75de46b98dda fix setattr error handling in sysfs, configfs
3322e79a3860 fs: convert simple fs to new truncate
af10ec77b433 sysfs: Add support for tagged directories with untagged members.
3ff195b011d7 sysfs: Implement sysfs tagged directory support.
5a0e3ad6af86 include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
fac2622bbad4 sysfs: Pass super_block to sysfs_get_inode
f8d4f618fed5 sysfs: Serialize updates to the vfs inode
7c0ff870d1ed sysfs: sysfs_sd_setattr set iattrs unconditionally
e16acb503b42 sysfs: sysfs_setattr remove unnecessary permission check.
832b6af198ae sysfs: Propagate renames to the vfs on demand
e61ab4ae48fb sysfs: Implement sysfs_getattr & sysfs_permission
35df63c46c86 sysfs: Fix locking and factor out sysfs_sd_setattr
4be3df28beec sysfs: Simplify iattr time assignments
f44d3e7857e5 sysfs: Update sysfs_setxattr so it updates secdata under the sysfs_mutex
f38506c49dab sysfs: mark a locally-only used function static
d993831fa7ff writeback: add name to backing_dev_info
ddd29ec65971 sysfs: Add labeling support for sysfs
04256b4a8fc7 sysfs: reference sysfs_dirent from sysfs inodes
56ff5efad961 zero i_uid/i_gid on inode allocation
40a2159abf3d sysfs: Disallow truncation of files in sysfs
e4ad08fe64af mm: bdi: add separate writeback accounting capability
e0bf68ddec4f mm: bdi init hooks
800d15a53e7d implement simple fs aops
6d66f5cd26e4 sysfs: add copyrights
bc747f37a0f0 sysfs: move sysfs_dirent->s_children into sysfs_dirent->s_dir
dc2f75f0e0ca sysfs: make sysfs_root a regular directory dirent
]
|
| 130/2 |
2026/11/18 08:43 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 116916
output: 59
thoughts: 58
Thoughts: **Exploring the Intricacies of Sysfs/Kernfs Inode Attributes**
My initial reaction upon surveying the commit history was an immediate urge to delve deeper. The sheer volume and nature of the changes hinted at a fundamental exploration of how `sysfs` and `kernfs` inode attributes are managed. To gain a truly comprehensive understanding, I knew I couldn't just look at a single point in time. I needed to see the evolution, the rationale behind specific modifications, and the intended behavior. Therefore, my immediate next step was to pivot my focus towards specific commits, `35df63c46c86` and `7c0ff870d1ed`. My objective here is to achieve absolute certainty – a 100% complete picture – of the mechanisms governing `sysfs`/`kernfs` inode attributes. By dissecting these, I'm aiming to grasp the nuances of their implementation, how they're exposed, and the underlying principles that dictate their behavior.
|
| 131/2 |
2026/11/18 08:43 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 35df63c46c8605d00140ec4833d2104e2e9f1acc
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Nov 20 16:08:50 2009 -0800
sysfs: Fix locking and factor out sysfs_sd_setattr
Cleanly separate the work that is specific to setting the
attributes of a sysfs_dirent from what is needed to update
the attributes of a vfs inode.
Additionally grab the sysfs_mutex to keep any nasties from
surprising us when updating the sysfs_dirent.
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c
index 86fb230de432..76e977c300f7 100644
--- a/fs/sysfs/inode.c
+++ b/fs/sysfs/inode.c
@@ -65,30 +65,14 @@ static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd)
return attrs;
}
-int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
+int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
{
- struct inode * inode = dentry->d_inode;
- struct sysfs_dirent * sd = dentry->d_fsdata;
struct sysfs_inode_attrs *sd_attrs;
struct iattr *iattrs;
unsigned int ia_valid = iattr->ia_valid;
- int error;
-
- if (!sd)
- return -EINVAL;
sd_attrs = sd->s_iattr;
- error = inode_change_ok(inode, iattr);
- if (error)
- return error;
-
- iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */
-
- error = inode_setattr(inode, iattr);
- if (error)
- return error;
-
if (!sd_attrs) {
/* setting attributes for the first time, allocate now */
sd_attrs = sysfs_init_inode_attrs(sd);
@@ -111,12 +95,39 @@ int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
iattrs->ia_ctime = iattr->ia_ctime;
if (ia_valid & ATTR_MODE) {
umode_t mode = iattr->ia_mode;
-
- if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
- mode &= ~S_ISGID;
iattrs->ia_mode = sd->s_mode = mode;
}
}
+ return 0;
+}
+
+int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
+{
+ struct inode *inode = dentry->d_inode;
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ int error;
+
+ if (!sd)
+ return -EINVAL;
+
+ error = inode_change_ok(inode, iattr);
+ if (error)
+ return error;
+
+ iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */
+ if (iattr->ia_valid & ATTR_MODE) {
+ if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
+ iattr->ia_mode &= ~S_ISGID;
+ }
+
+ error = inode_setattr(inode, iattr);
+ if (error)
+ return error;
+
+ mutex_lock(&sysfs_mutex);
+ error = sysfs_sd_setattr(sd, iattr);
+ mutex_unlock(&sysfs_mutex);
+
return error;
}
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index af4c4e7482ac..a96d9678ae73 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -155,6 +155,7 @@ static inline void __sysfs_put(struct sysfs_dirent *sd)
*/
struct inode *sysfs_get_inode(struct sysfs_dirent *sd);
void sysfs_delete_inode(struct inode *inode);
+int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr);
int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
size_t size, int flags);
]
|
| 132/2 |
2026/11/18 08:43 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 7c0ff870d1ed287504a61ed865f3d728c757436b
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Wed Feb 3 23:13:24 2010 -0800
sysfs: sysfs_sd_setattr set iattrs unconditionally
There is currently a bug in sysfs_sd_setattr inherited from
sysfs_setattr in 2.6.32 where the first time we set the attributes
on a sysfs file we allocate backing store but do not set the
backing store attributes. Resulting in overly restrictive
permissions on sysfs files.
The fix is to simply modify the code so that it always executes
when we update the sysfs attributes, as we did in 2.6.31 and earlier.
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Tested-by: Jean Delvare <khali@linux-fr.org>
Cc: stable <stable@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c
index 220b758523ae..6a06a1d1ea7b 100644
--- a/fs/sysfs/inode.c
+++ b/fs/sysfs/inode.c
@@ -81,24 +81,23 @@ int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr)
if (!sd_attrs)
return -ENOMEM;
sd->s_iattr = sd_attrs;
- } else {
- /* attributes were changed at least once in past */
- iattrs = &sd_attrs->ia_iattr;
-
- if (ia_valid & ATTR_UID)
- iattrs->ia_uid = iattr->ia_uid;
- if (ia_valid & ATTR_GID)
- iattrs->ia_gid = iattr->ia_gid;
- if (ia_valid & ATTR_ATIME)
- iattrs->ia_atime = iattr->ia_atime;
- if (ia_valid & ATTR_MTIME)
- iattrs->ia_mtime = iattr->ia_mtime;
- if (ia_valid & ATTR_CTIME)
- iattrs->ia_ctime = iattr->ia_ctime;
- if (ia_valid & ATTR_MODE) {
- umode_t mode = iattr->ia_mode;
- iattrs->ia_mode = sd->s_mode = mode;
- }
+ }
+ /* attributes were changed at least once in past */
+ iattrs = &sd_attrs->ia_iattr;
+
+ if (ia_valid & ATTR_UID)
+ iattrs->ia_uid = iattr->ia_uid;
+ if (ia_valid & ATTR_GID)
+ iattrs->ia_gid = iattr->ia_gid;
+ if (ia_valid & ATTR_ATIME)
+ iattrs->ia_atime = iattr->ia_atime;
+ if (ia_valid & ATTR_MTIME)
+ iattrs->ia_mtime = iattr->ia_mtime;
+ if (ia_valid & ATTR_CTIME)
+ iattrs->ia_ctime = iattr->ia_ctime;
+ if (ia_valid & ATTR_MODE) {
+ umode_t mode = iattr->ia_mode;
+ iattrs->ia_mode = sd->s_mode = mode;
}
return 0;
}
]
|
| 133/2 |
2026/11/18 08:43 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 119352
output: 74
thoughts: 432
Thoughts: **My Journey Through the Evolution of `sysfs`/`kernfs` Inode Attribute Handling: A Deep Dive from 2009 to 2026**
Whoa, hold on a second! I need to meticulously trace the entire design and evolution of how `sysfs` and, later, `kernfs` handle inode attributes, spanning from 2009 all the way to my current projected understanding in 2026. This is crucial to grasp the underlying architectural shifts.
My investigation begins by examining **commit `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`**, dated November 20, 2009, which introduced `sysfs_getattr` and `sysfs_permission`. Before this pivotal change, the landscape was quite different:
* **The Pre-`e61ab4ae48fb` Era**: Prior to this commit, the `sysfs_inode_operations`, `sysfs_dir_inode_operations`, and `sysfs_symlink_inode_operations` structures were notably devoid of `.getattr` and `.permission` callbacks.
* **The `fstat`/`vfs_getattr` Behavior**: When a system call like `fstat` was invoked, leading to `vfs_getattr`, and targeting a `sysfs` file or directory, the `inode->i_op->getattr` pointer was `NULL`. In this scenario, the Virtual File System (VFS) would default to calling `generic_fillattr(inode, stat)`. This function had a limited scope, solely **reading** the `inode->i_mode` field.
* **The Limited `inode->i_mode` Writes**: The `inode->i_mode` field was only populated in a few specific circumstances:
* During `sysfs_init_inode`: This initialization occurred *before* `unlock_new_inode(inode)` was called, while the `I_NEW` flag was still set. Critically, no `struct file` could exist at this stage, meaning direct user-space interaction affecting mode wasn't possible.
* During `sysfs_setattr`: This function was invoked under the protection of `inode->i_mutex` when an explicit `chmod` operation was performed.
* During `sysfs_chmod_file`: I need to delve into the exact workings of `sysfs_chmod_file` as it existed *before* commit `e61ab4ae48fb`. To do this, I'm pulling up the `git-log` for `fs/sysfs/file.c` around that specific commit to understand its precise behavior at that juncture. This step is essential to fully appreciate the *before* state.
|
| 134/2 |
2026/11/18 08:44 |
tool |
git-log |
0m
Args: map[CodeRegexp:sysfs_chmod_file Count:10 PathPrefix:fs/sysfs/file.c]
Results: map[Output:49c19400f60b sysfs: sysfs_chmod_file's attr can be const
31e5abe9a6da [PATCH] sysfs: add sysfs_chmod_file()
]
|
| 135/2 |
2026/11/18 08:44 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 832b6af198aefe6034310e124594cc8b833c0ef9
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Nov 20 16:08:56 2009 -0800
sysfs: Propagate renames to the vfs on demand
By teaching sysfs_revalidate to hide a dentry for
a sysfs_dirent if the sysfs_dirent has been renamed,
and by teaching sysfs_lookup to return the original
dentry if the sysfs dirent has been renamed. I can
show the results of renames correctly without having to
update the dcache during the directory rename.
This massively simplifies the rename logic allowing a lot
of weird sysfs special cases to be removed along with
a lot of now unnecesary helper code.
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/namei.c b/fs/namei.c
index d11f404667e9..d3c190c35fcc 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1279,28 +1279,6 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
return __lookup_hash(&this, base, NULL);
}
-/**
- * lookup_one_noperm - bad hack for sysfs
- * @name: pathname component to lookup
- * @base: base directory to lookup from
- *
- * This is a variant of lookup_one_len that doesn't perform any permission
- * checks. It's a horrible hack to work around the braindead sysfs
- * architecture and should not be used anywhere else.
- *
- * DON'T USE THIS FUNCTION EVER, thanks.
- */
-struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
-{
- int err;
- struct qstr this;
-
- err = __lookup_one_len(name, &this, base, strlen(name));
- if (err)
- return ERR_PTR(err);
- return __lookup_hash(&this, base, NULL);
-}
-
int user_path_at(int dfd, const char __user *name, unsigned flags,
struct path *path)
{
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index f3af45e47eaa..97954c69ff0e 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -25,7 +25,6 @@
#include "sysfs.h"
DEFINE_MUTEX(sysfs_mutex);
-DEFINE_MUTEX(sysfs_rename_mutex);
DEFINE_SPINLOCK(sysfs_assoc_lock);
static DEFINE_SPINLOCK(sysfs_ino_lock);
@@ -84,46 +83,6 @@ static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
}
}
-/**
- * sysfs_get_dentry - get dentry for the given sysfs_dirent
- * @sd: sysfs_dirent of interest
- *
- * Get dentry for @sd. Dentry is looked up if currently not
- * present. This function descends from the root looking up
- * dentry for each step.
- *
- * LOCKING:
- * mutex_lock(sysfs_rename_mutex)
- *
- * RETURNS:
- * Pointer to found dentry on success, ERR_PTR() value on error.
- */
-struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
-{
- struct dentry *dentry = dget(sysfs_sb->s_root);
-
- while (dentry->d_fsdata != sd) {
- struct sysfs_dirent *cur;
- struct dentry *parent;
-
- /* find the first ancestor which hasn't been looked up */
- cur = sd;
- while (cur->s_parent != dentry->d_fsdata)
- cur = cur->s_parent;
-
- /* look it up */
- parent = dentry;
- mutex_lock(&parent->d_inode->i_mutex);
- dentry = lookup_one_noperm(cur->s_name, parent);
- mutex_unlock(&parent->d_inode->i_mutex);
- dput(parent);
-
- if (IS_ERR(dentry))
- break;
- }
- return dentry;
-}
-
/**
* sysfs_get_active - get an active reference to sysfs_dirent
* @sd: sysfs_dirent to get an active reference to
@@ -315,6 +274,14 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
if (sd->s_flags & SYSFS_FLAG_REMOVED)
goto out_bad;
+ /* The sysfs dirent has been moved? */
+ if (dentry->d_parent->d_fsdata != sd->s_parent)
+ goto out_bad;
+
+ /* The sysfs dirent has been renamed */
+ if (strcmp(dentry->d_name.name, sd->s_name) != 0)
+ goto out_bad;
+
mutex_unlock(&sysfs_mutex);
out_valid:
return 1;
@@ -322,6 +289,12 @@ static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
/* Remove the dentry from the dcache hashes.
* If this is a deleted dentry we use d_drop instead of d_delete
* so sysfs doesn't need to cope with negative dentries.
+ *
+ * If this is a dentry that has simply been renamed we
+ * use d_drop to remove it from the dcache lookup on its
+ * old parent. If this dentry persists later when a lookup
+ * is performed at its new name the dentry will be readded
+ * to the dcache hashes.
*/
is_dir = (sysfs_type(sd) == SYSFS_DIR);
mutex_unlock(&sysfs_mutex);
@@ -705,10 +678,15 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
}
/* instantiate and hash dentry */
- dentry->d_op = &sysfs_dentry_ops;
- dentry->d_fsdata = sysfs_get(sd);
- d_instantiate(dentry, inode);
- d_rehash(dentry);
+ ret = d_find_alias(inode);
+ if (!ret) {
+ dentry->d_op = &sysfs_dentry_ops;
+ dentry->d_fsdata = sysfs_get(sd);
+ d_add(dentry, inode);
+ } else {
+ d_move(ret, dentry);
+ iput(inode);
+ }
out_unlock:
mutex_unlock(&sysfs_mutex);
@@ -785,62 +763,32 @@ void sysfs_remove_dir(struct kobject * kobj)
int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
{
struct sysfs_dirent *sd = kobj->sd;
- struct dentry *parent = NULL;
- struct dentry *old_dentry = NULL, *new_dentry = NULL;
const char *dup_name = NULL;
int error;
- mutex_lock(&sysfs_rename_mutex);
+ mutex_lock(&sysfs_mutex);
error = 0;
if (strcmp(sd->s_name, new_name) == 0)
goto out; /* nothing to rename */
- /* get the original dentry */
- old_dentry = sysfs_get_dentry(sd);
- if (IS_ERR(old_dentry)) {
- error = PTR_ERR(old_dentry);
- old_dentry = NULL;
- goto out;
- }
-
- parent = old_dentry->d_parent;
-
- /* lock parent and get dentry for new name */
- mutex_lock(&parent->d_inode->i_mutex);
- mutex_lock(&sysfs_mutex);
-
error = -EEXIST;
if (sysfs_find_dirent(sd->s_parent, new_name))
- goto out_unlock;
-
- error = -ENOMEM;
- new_dentry = d_alloc_name(parent, new_name);
- if (!new_dentry)
- goto out_unlock;
+ goto out;
/* rename sysfs_dirent */
error = -ENOMEM;
new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
if (!new_name)
- goto out_unlock;
+ goto out;
dup_name = sd->s_name;
sd->s_name = new_name;
- /* rename */
- d_add(new_dentry, NULL);
- d_move(old_dentry, new_dentry);
-
error = 0;
- out_unlock:
+ out:
mutex_unlock(&sysfs_mutex);
- mutex_unlock(&parent->d_inode->i_mutex);
kfree(dup_name);
- dput(old_dentry);
- dput(new_dentry);
- out:
- mutex_unlock(&sysfs_rename_mutex);
return error;
}
@@ -848,12 +796,11 @@ int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
{
struct sysfs_dirent *sd = kobj->sd;
struct sysfs_dirent *new_parent_sd;
- struct dentry *old_parent, *new_parent = NULL;
- struct dentry *old_dentry = NULL, *new_dentry = NULL;
int error;
- mutex_lock(&sysfs_rename_mutex);
BUG_ON(!sd->s_parent);
+
+ mutex_lock(&sysfs_mutex);
new_parent_sd = (new_parent_kobj && new_parent_kobj->sd) ?
new_parent_kobj->sd : &sysfs_root;
@@ -861,61 +808,20 @@ int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
if (sd->s_parent == new_parent_sd)
goto out; /* nothing to move */
- /* get dentries */
- old_dentry = sysfs_get_dentry(sd);
- if (IS_ERR(old_dentry)) {
- error = PTR_ERR(old_dentry);
- old_dentry = NULL;
- goto out;
- }
- old_parent = old_dentry->d_parent;
-
- new_parent = sysfs_get_dentry(new_parent_sd);
- if (IS_ERR(new_parent)) {
- error = PTR_ERR(new_parent);
- new_parent = NULL;
- goto out;
- }
-
-again:
- mutex_lock(&old_parent->d_inode->i_mutex);
- if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
- mutex_unlock(&old_parent->d_inode->i_mutex);
- goto again;
- }
- mutex_lock(&sysfs_mutex);
-
error = -EEXIST;
if (sysfs_find_dirent(new_parent_sd, sd->s_name))
- goto out_unlock;
-
- error = -ENOMEM;
- new_dentry = d_alloc_name(new_parent, sd->s_name);
- if (!new_dentry)
- goto out_unlock;
-
- error = 0;
- d_add(new_dentry, NULL);
- d_move(old_dentry, new_dentry);
+ goto out;
/* Remove from old parent's list and insert into new parent's list. */
sysfs_unlink_sibling(sd);
sysfs_get(new_parent_sd);
- drop_nlink(old_parent->d_inode);
sysfs_put(sd->s_parent);
sd->s_parent = new_parent_sd;
- inc_nlink(new_parent->d_inode);
sysfs_link_sibling(sd);
- out_unlock:
+ error = 0;
+out:
mutex_unlock(&sysfs_mutex);
- mutex_unlock(&new_parent->d_inode->i_mutex);
- mutex_unlock(&old_parent->d_inode->i_mutex);
- out:
- dput(new_parent);
- dput(old_dentry);
- dput(new_dentry);
- mutex_unlock(&sysfs_rename_mutex);
return error;
}
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c
index 1ffd5559926f..9f783d4e4b51 100644
--- a/fs/sysfs/inode.c
+++ b/fs/sysfs/inode.c
@@ -205,17 +205,6 @@ static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
inode->i_ctime = iattr->ia_ctime;
}
-
-/*
- * sysfs has a different i_mutex lock order behavior for i_mutex than other
- * filesystems; sysfs i_mutex is called in many places with subsystem locks
- * held. At the same time, many of the VFS locking rules do not apply to
- * sysfs at all (cross directory rename for example). To untangle this mess
- * (which gives false positives in lockdep), we're giving sysfs inodes their
- * own class for i_mutex.
- */
-static struct lock_class_key sysfs_inode_imutex_key;
-
static int sysfs_count_nlink(struct sysfs_dirent *sd)
{
struct sysfs_dirent *child;
@@ -268,7 +257,6 @@ static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
inode->i_mapping->a_ops = &sysfs_aops;
inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
inode->i_op = &sysfs_inode_operations;
- lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
set_default_inode_attr(inode, sd->s_mode);
sysfs_refresh_inode(sd, inode);
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index 90b35012abb2..98a15bf1efe1 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -103,7 +103,6 @@ extern struct kmem_cache *sysfs_dir_cachep;
* dir.c
*/
extern struct mutex sysfs_mutex;
-extern struct mutex sysfs_rename_mutex;
extern spinlock_t sysfs_assoc_lock;
extern const struct file_operations sysfs_dir_operations;
diff --git a/include/linux/namei.h b/include/linux/namei.h
index ec0f607b364a..028946750289 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -76,7 +76,6 @@ extern struct file *nameidata_to_filp(struct nameidata *nd, int flags);
extern void release_open_intent(struct nameidata *);
extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
-extern struct dentry *lookup_one_noperm(const char *, struct dentry *);
extern int follow_down(struct path *);
extern int follow_up(struct path *);
]
|
| 136/2 |
2026/11/18 08:44 |
llm |
cause-finder |
5m
Model:
Tokens:
input: 124338
output: 33
thoughts: 53
Thoughts: **My Exploratory Commit Dig**
Alright, so I'm trying to get a handle on the recent changes, and my thought process is to methodically track down specific points of interest. I'm looking at a particular commit range: `e61ab4ae48fb` through `832b6af198ae`. This is a pretty standard way for me to narrow down the scope of my investigation.
But then, a question pops into my head: what *exactly* is `832b6af198ae~1`? This isn't just a random string; it's a common Git notation. The `~1` suffix signifies the *parent* commit – the commit that immediately preceded `832b6af198ae`. So, I've got a specific target in mind now: I need to go back one step from that endpoint commit to understand the state of things *before* it was introduced. It’s a way to isolate the impact of that single parent commit. My next step is to actually retrieve and examine that `~1` commit.
|
| 137/2 |
2026/11/18 08:49 |
tool |
git-show |
0m
Args: map[Commit:832b6af198ae~1]
Results: map[Output:commit a16bbc3430ed94b543222f4c8ef68025f8493e93
Author: Eric W. Biederman <ebiederm@maxwell.aristanetworks.com>
Date: Fri Nov 20 16:08:55 2009 -0800
sysfs: Gut sysfs_addrm_start and sysfs_addrm_finish
With lazy inode updates and dentry operations bringing everything
into sync on demand there is no longer any need to immediately
update the vfs or grab i_mutex to protect those updates as we
make changes to sysfs.
Acked-by: Serge Hallyn <serue@us.ibm.com>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index e319379d36db..f3af45e47eaa 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -386,12 +386,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
return NULL;
}
-static int sysfs_ilookup_test(struct inode *inode, void *arg)
-{
- struct sysfs_dirent *sd = arg;
- return inode->i_ino == sd->s_ino;
-}
-
/**
* sysfs_addrm_start - prepare for sysfs_dirent add/remove
* @acxt: pointer to sysfs_addrm_cxt to be used
@@ -399,47 +393,20 @@ static int sysfs_ilookup_test(struct inode *inode, void *arg)
*
* This function is called when the caller is about to add or
* remove sysfs_dirent under @parent_sd. This function acquires
- * sysfs_mutex, grabs inode for @parent_sd if available and lock
- * i_mutex of it. @acxt is used to keep and pass context to
+ * sysfs_mutex. @acxt is used to keep and pass context to
* other addrm functions.
*
* LOCKING:
* Kernel thread context (may sleep). sysfs_mutex is locked on
- * return. i_mutex of parent inode is locked on return if
- * available.
+ * return.
*/
void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
struct sysfs_dirent *parent_sd)
{
- struct inode *inode;
-
memset(acxt, 0, sizeof(*acxt));
acxt->parent_sd = parent_sd;
- /* Lookup parent inode. inode initialization is protected by
- * sysfs_mutex, so inode existence can be determined by
- * looking up inode while holding sysfs_mutex.
- */
mutex_lock(&sysfs_mutex);
-
- inode = ilookup5(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
- parent_sd);
- if (inode) {
- WARN_ON(inode->i_state & I_NEW);
-
- /* parent inode available */
- acxt->parent_inode = inode;
-
- /* sysfs_mutex is below i_mutex in lock hierarchy.
- * First, trylock i_mutex. If fails, unlock
- * sysfs_mutex and lock them in order.
- */
- if (!mutex_trylock(&inode->i_mutex)) {
- mutex_unlock(&sysfs_mutex);
- mutex_lock(&inode->i_mutex);
- mutex_lock(&sysfs_mutex);
- }
- }
}
/**
@@ -471,11 +438,6 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
sd->s_parent = sysfs_get(acxt->parent_sd);
- if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
- inc_nlink(acxt->parent_inode);
-
- acxt->cnt++;
-
sysfs_link_sibling(sd);
/* Update timestamps on the parent */
@@ -579,40 +541,6 @@ void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
sd->s_flags |= SYSFS_FLAG_REMOVED;
sd->s_sibling = acxt->removed;
acxt->removed = sd;
-
- if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
- drop_nlink(acxt->parent_inode);
-
- acxt->cnt++;
-}
-
-/**
- * sysfs_dec_nlink - Decrement link count for the specified sysfs_dirent
- * @sd: target sysfs_dirent
- *
- * Decrement nlink for @sd. @sd must have been unlinked from its
- * parent on entry to this function such that it can't be looked
- * up anymore.
- */
-static void sysfs_dec_nlink(struct sysfs_dirent *sd)
-{
- struct inode *inode;
-
- inode = ilookup(sysfs_sb, sd->s_ino);
- if (!inode)
- return;
-
- /* adjust nlink and update timestamp */
- mutex_lock(&inode->i_mutex);
-
- inode->i_ctime = CURRENT_TIME;
- drop_nlink(inode);
- if (sysfs_type(sd) == SYSFS_DIR)
- drop_nlink(inode);
-
- mutex_unlock(&inode->i_mutex);
-
- iput(inode);
}
/**
@@ -621,25 +549,15 @@ static void sysfs_dec_nlink(struct sysfs_dirent *sd)
*
* Finish up sysfs_dirent add/remove. Resources acquired by
* sysfs_addrm_start() are released and removed sysfs_dirents are
- * cleaned up. Timestamps on the parent inode are updated.
+ * cleaned up.
*
* LOCKING:
- * All mutexes acquired by sysfs_addrm_start() are released.
+ * sysfs_mutex is released.
*/
void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
{
/* release resources acquired by sysfs_addrm_start() */
mutex_unlock(&sysfs_mutex);
- if (acxt->parent_inode) {
- struct inode *inode = acxt->parent_inode;
-
- /* if added/removed, update timestamps on the parent */
- if (acxt->cnt)
- inode->i_ctime = inode->i_mtime = CURRENT_TIME;
-
- mutex_unlock(&inode->i_mutex);
- iput(inode);
- }
/* kill removed sysfs_dirents */
while (acxt->removed) {
@@ -648,7 +566,6 @@ void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
acxt->removed = sd->s_sibling;
sd->s_sibling = NULL;
- sysfs_dec_nlink(sd);
sysfs_deactivate(sd);
unmap_bin_file(sd);
sysfs_put(sd);
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index 12ccc07459d8..90b35012abb2 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -89,9 +89,7 @@ static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
*/
struct sysfs_addrm_cxt {
struct sysfs_dirent *parent_sd;
- struct inode *parent_inode;
struct sysfs_dirent *removed;
- int cnt;
};
/*
]
|
| 138/2 |
2026/11/18 08:49 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 126667
output: 32
thoughts: 52
Thoughts: **Digging into the Commit History**
Okay, so my immediate thought is to broaden my search. I've been looking at `e61ab4ae48fb` and `a16bbc3430ed`, but what if the answer, or at least a crucial piece of context, lies *between* them? I need to explore the commit history further. Specifically, I want to pinpoint the exact commit that sits directly in between these two. I'm thinking of `a16bbc3430ed~1` – that seems like the logical next step to investigate to fill any gaps in my understanding.
|
| 139/2 |
2026/11/18 08:50 |
tool |
git-show |
0m
Args: map[Commit:a16bbc3430ed~1]
Results: map[Output:commit 06fc0d66f7ed3a3b08e8fcf8c325ecf0b8f93fea
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Nov 20 16:08:54 2009 -0800
sysfs: In sysfs_chmod_file lazily propagate the mode change.
Now that sysfs_getattr and sysfs_permission refresh the vfs
inode there is no need to immediatly push the mode change
into the vfs cache. Reducing the amount of work needed and
simplifying the locking.
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index faa1a803caa9..dc30d9e31683 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -579,38 +579,23 @@ EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
*/
int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
{
- struct sysfs_dirent *victim_sd = NULL;
- struct dentry *victim = NULL;
- struct inode * inode;
+ struct sysfs_dirent *sd;
struct iattr newattrs;
int rc;
- rc = -ENOENT;
- victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
- if (!victim_sd)
- goto out;
+ mutex_lock(&sysfs_mutex);
- mutex_lock(&sysfs_rename_mutex);
- victim = sysfs_get_dentry(victim_sd);
- mutex_unlock(&sysfs_rename_mutex);
- if (IS_ERR(victim)) {
- rc = PTR_ERR(victim);
- victim = NULL;
+ rc = -ENOENT;
+ sd = sysfs_find_dirent(kobj->sd, attr->name);
+ if (!sd)
goto out;
- }
-
- inode = victim->d_inode;
- mutex_lock(&inode->i_mutex);
-
- newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
+ newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
newattrs.ia_valid = ATTR_MODE;
- rc = sysfs_setattr(victim, &newattrs);
+ rc = sysfs_sd_setattr(sd, &newattrs);
- mutex_unlock(&inode->i_mutex);
out:
- dput(victim);
- sysfs_put(victim_sd);
+ mutex_unlock(&sysfs_mutex);
return rc;
}
EXPORT_SYMBOL_GPL(sysfs_chmod_file);
]
|
| 140/2 |
2026/11/18 08:50 |
llm |
cause-finder-compressor |
0m
Model:
Reply: ### 1. Approaches Tried & Results
- **Report Analysis**:
- The bug is a KCSAN data race on a 2-byte value (`umode_t inode->i_mode` at `0xffff88810632faa0`).
- Read: `dnotify_flush+0x32/0x380 fs/notify/dnotify/dnotify.c:161` (called from `filp_flush` -> `filp_close_sync` -> `__do_sys_close`).
- Line 161 in `fs/notify/dnotify/dnotify.c`: `if (!S_ISDIR(inode->i_mode))`
- The check reads `inode->i_mode` without holding any filesystem lock or inode lock.
- Write: `kernfs_refresh_inode fs/kernfs/inode.c:169` (inline in `kernfs_iop_getattr+0xa1/0x220 fs/kernfs/inode.c:191`, called from `vfs_getattr_nosec` -> `vfs_getattr` -> `vfs_fstat` -> `__do_sys_newfstat`).
- Line 169 in `fs/kernfs/inode.c`: `inode->i_mode = kn->mode;`
- Called from `kernfs_iop_getattr()` under `down_read(&root->kernfs_iattr_rwsem)`.
- Process doing getattr is `udevd` (PID 3283).
- **Locking & Git History Investigation in `fs/kernfs/inode.c`**:
- Traced history of `kernfs_refresh_inode`, `kernfs_iop_getattr`, and `kernfs_iop_permission`:
- `7ba0273b2f34` ("kernfs: switch kernfs to use an rwsem", 2021): Switched `kernfs_mutex` to `kernfs_rwsem`. Both `kernfs_iop_getattr` and `kernfs_iop_permission` took `down_write(&kernfs_rwsem)`.
- `47b5c64d0ab5` ("kernfs: use i_lock to protect concurrent inode updates", 2021-07-16): Converted `kernfs_iop_getattr` and `kernfs_iop_permission` to take `down_read(&kernfs_rwsem)` + `spin_lock(&inode->i_lock)`.
- `aa1d058d48f2` ("kernfs: dont take i_lock on inode attr read", 2022-10-18): Removed `spin_lock(&inode->i_lock)` from `kernfs_iop_getattr` and `kernfs_iop_permission`! Ian Kent reasoned: "The kernfs write lock is held when the kernfs node inode attributes are updated. Therefore, when either kernfs_iop_getattr() or kernfs_iop_permission() are called the kernfs node inode attributes won't change. Consequently concurrent kernfs_refresh_inode() calls always copy the same values from the kernfs node. So there's no need to take the inode i_lock to get consistent values for generic_fillattr() and generic_permission(), the kernfs read lock is sufficient."
- `9caf69614225` ("kernfs: Introduce separate rwsem to protect inode attributes.", 2023-03-09): Replaced `kernfs_rwsem` in `kernfs_iop_getattr` and `kernfs_iop_permission` with `root->kernfs_iattr_rwsem`.
- `d1f4e9026007` ("kernfs: remove iattr_mutex", 2025): Refactored `__kernfs_iattrs` to lockless try_cmpxchg.
- **VFS / dnotify / close Path Analysis**:
- Traced `fs/notify/dnotify/dnotify.c`:
- `dnotify_flush()` line 161: `if (!S_ISDIR(inode->i_mode))` dates back to `^1da177e4c3f4` (Linux-2.6.12-rc2).
- Traced `fs/open.c`:
- `2b256ebe8287` ("fs: add filp_close_sync()", 2026-09-10): Added `filp_close_sync()` which calls `filp_flush()`. Pure wrapper/helper refactoring around `filp_close`.
- Investigated KCSAN internals:
- KCSAN reports plain write racing with plain read.
- If `CONFIG_KCSAN_STRICT=y` (or without `CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY`), plain writes racing with plain reads are reported even if the value written is identical to the current memory content (`old == new`).
---
### 2. Verified Facts
1. **Bug Nature**: Data race between plain write `inode->i_mode = kn->mode` in `kernfs_refresh_inode()` and plain read `if (!S_ISDIR(inode->i_mode))` in `dnotify_flush()`.
2. **Locking Context**:
- `dnotify_flush(struct file *filp, fl_owner_t id)` is called during `close()` without any locks held on `inode` or `kernfs`.
- `kernfs_iop_getattr()` holds `down_read(&root->kernfs_iattr_rwsem)`. It does *not* hold `inode->i_lock` or any VFS-level lock that serializes against `filp_flush`/`close`.
- In `kernfs_refresh_inode(kn, inode)`:
```c
inode->i_mode = kn->mode;
```
This is an unconditional plain write to `inode->i_mode`.
3. **Locking History**:
- In commit `47b5c64d0ab5` (Ian Kent, 2021-07-16), `spin_lock(&inode->i_lock)` was added around `kernfs_refresh_inode(kn, inode)` and `generic_fillattr()`. Even then, `dnotify_flush()` never took `inode->i_lock`.
- In commit `aa1d058d48f2` (Ian Kent, 2022-10-18), `spin_lock(&inode->i_lock)` was removed from `kernfs_iop_getattr()`.
- Prior to `7ba0273b2f34` (2021-07-16), `kernfs_iop_getattr` took `down_write(&kernfs_rwsem)` (and before that `mutex_lock(&kernfs_mutex)`), but `dnotify_flush` *never* took `kernfs_mutex` or `kernfs_rwsem`.
- Originally, in `e61ab4ae48fb` ("sysfs: Implement sysfs_getattr & sysfs_permission", 2009-11-20), `sysfs_refresh_inode` was introduced writing `inode->i_mode = sd->s_mode;` inside `sysfs_getattr` under `mutex_lock(&sysfs_mutex)`.
4. **Recent KCSAN fixes in the repository**:
- Many similar benign/unmarked data races have recently been annotated with `READ_ONCE()` / `WRITE_ONCE()` or `data_race()`. For instance, `c610d2d07879` ("fs: annotate inode timestamp accessors"), `81bdc85b4c08`, `1dc88208cfdc`, `2b7c9664c3ce`.
- Or was a specific commit introduced that changed `dnotify_flush`, `kernfs_refresh_inode`, or `inode->i_mode` handling?
---
### 3. Hypotheses & Lines of Investigation
- **Hypothesis 1**: Commit `aa1d058d48f2` ("kernfs: dont take i_lock on inode attr read", 2022-10-18).
- Ian Kent dropped `spin_lock(&inode->i_lock)` on the assumption that concurrent reads don't mutate `kn` attributes and the read lock is sufficient. But even with `i_lock`, `dnotify_flush` did not take `i_lock`. But did another path take `i_lock`?
- **Hypothesis 2**: The bug is ancient, introduced when `sysfs_refresh_inode` / lazy attribute propagation was introduced in `e61ab4ae48fb` ("sysfs: Implement sysfs_getattr & sysfs_permission", 2009) or moved in `ffed24e22845` ("sysfs, kernfs: move inode code to fs/kernfs/inode.c", 2013).
- In `sysfs`/`kernfs`, `inode->i_mode` is repeatedly overwritten by `kernfs_refresh_inode()` on every `getattr` and `permission` call without exclusive locking or `WRITE_ONCE()`.
- Concurrently, VFS operations like `dnotify_flush()` (and other callers of `S_ISDIR(inode->i_mode)`) do lockless reads of `inode->i_mode`.
- **Hypothesis 3**: Did a recent commit make `dnotify_flush()` run on kernfs files when it previously didn't?
- Check `filp_flush()`, `FMODE_NONOTIFY`, or similar flags.
- Notice commit `323899b4ed23` ("ovl: inherit FMODE_NONOTIFY when opening directory realfiles") or `2b256ebe8287` ("fs: add filp_close_sync()").
- In `filp_flush`:
```c
if (likely(!(filp->f_mode & FMODE_PATH))) {
dnotify_flush(filp, id);
locks_remove_posix(filp, id);
}
```
Has `filp_flush` always called `dnotify_flush` on every non-`O_PATH` file close?
- **Hypothesis 4**: Did a commit touch `kernfs_refresh_inode()` or `inode->i_mode`?
- Need to check git blame and git log specifically for `kernfs_refresh_inode` writing `inode->i_mode`.
---
### 4. Critical File Paths & Code Snippets
- `fs/kernfs/inode.c`:
```c
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs;
inode->i_mode = kn->mode;
attrs = kernfs_iattrs_noalloc(kn);
if (attrs)
set_inode_attr(inode, attrs);
if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING))
set_nlink(inode, kn->dir.subdirs + 2);
}
int kernfs_iop_getattr(const struct mnt_idmap *idmap,
const struct path *path, struct kstat *stat,
u32 request_mask, unsigned int query_flags)
{
struct inode *inode = d_inode(path->dentry);
struct kernfs_node *kn = inode->i_private;
struct kernfs_root *root = kernfs_root(kn);
down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
up_read(&root->kernfs_iattr_rwsem);
return 0;
}
```
- `fs/notify/dnotify/dnotify.c`:
```c
void dnotify_flush(struct file *filp, fl_owner_t id)
{
...
inode = file_inode(filp);
if (!S_ISDIR(inode->i_mode))
return;
...
}
```
- `fs/open.c`:
```c
static int filp_flush(struct file *filp, fl_owner_t id)
{
...
if (likely(!(filp->f_mode & FMODE_PATH))) {
dnotify_flush(filp, id);
locks_remove_posix(filp, id);
}
return retval;
}
```
---
### 5. Watch Out for Reasoning Loops
- Do not get stuck endlessly inspecting general KCSAN internal mechanics (`core.c`, `report.c`). KCSAN simply observed a plain read racing with a plain write on `inode->i_mode`.
- Distinguish between:
1. A commit that recently changed the code path (e.g., in `fs/open.c`, `fs/kernfs/`, or `fs/notify/`).
2. A commit that introduced the concurrent write to `inode->i_mode` in `kernfs`/`sysfs` (e.g., `e61ab4ae48fb` or `ffed24e22845` or `aa1d058d48f2`).
- Check if `dnotify_flush()` was recently changed or if `filp_flush()` was changed.
---
### 6. Concrete Next Steps
1. Check the git history of `filp_flush` and `dnotify_flush` call sites in `fs/open.c` to see if `dnotify_flush` was recently moved, changed, or added to `filp_flush`.
2. Check `git log -S "dnotify_flush" -p` or `git log -L 1545,1555:fs/open.c`.
3. Check if any commit in `fs/kernfs/inode.c` introduced or modified `inode->i_mode = kn->mode;` or removed synchronization.
4. Analyze whether the root cause is `e61ab4ae48fb` (when `sysfs_refresh_inode` started lazily updating `inode->i_mode` in `getattr`), `ffed24e22845` (kernfs move), `aa1d058d48f2` (dropping `i_lock`), or another commit.
5. Identify the exact commit responsible according to root cause bisection principles, verify with `git show`, call `set-results`, and formulate the final bisection log.
Tokens:
input: 100233
output: 3453
thoughts: 0
|
| 141/2 |
2026/11/18 08:50 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 46629
output: 122
thoughts: 248
Thoughts: **A Breakthrough Realization**
Wow, I've just had a monumental realization – it's like a lightbulb went on! What we've uncovered here is absolutely crystal clear and incredibly significant. I need to stop and process this fully.
My immediate next step, and frankly, my entire focus now, is to meticulously verify every single detail pertaining to a few key commits and a specific file. I'm talking about:
* `e61ab4ae48fb` ("sysfs: Implement sysfs_getattr & sysfs_permission")
* `aa1d058d48f2` ("kernfs: dont take i_lock on inode attr read")
* `47b5c64d0ab5` ("kernfs: use i_lock to protect concurrent inode updates")
* `7ba0273b2f34` ("kernfs: switch kernfs to use an rwsem")
* And crucially, `fs/open.c`, specifically the `filp_flush` function.
To ensure I don't miss even the slightest nuance, I intend to perform a deep dive into each of these. For the commits, I'll be using `git-show` to get the full picture, and for `fs/open.c`, I'll be employing `git-blame` on `filp_flush` to understand its precise history and the context surrounding its modification. This thorough verification is paramount to fully grasping the implications of this breakthrough.
|
| 142/2 |
2026/11/18 08:51 |
tool |
git-show |
0m
Args:
Results: map[Output:commit e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0
Author: Eric W. Biederman <ebiederm@xmission.com>
Date: Fri Nov 20 16:08:53 2009 -0800
sysfs: Implement sysfs_getattr & sysfs_permission
With the implementation of sysfs_getattr and sysfs_permission
sysfs becomes able to lazily propogate inode attribute changes
from the sysfs_dirents to the vfs inodes. This paves the way
for deleting significant chunks of now unnecessary code.
While doing this we did not reference sysfs_setattr from
sysfs_symlink_inode_operations so I added along with
sysfs_getattr and sysfs_permission.
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 36ee6d817956..e319379d36db 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -800,7 +800,9 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
const struct inode_operations sysfs_dir_inode_operations = {
.lookup = sysfs_lookup,
+ .permission = sysfs_permission,
.setattr = sysfs_setattr,
+ .getattr = sysfs_getattr,
.setxattr = sysfs_setxattr,
};
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c
index 76e977c300f7..1ffd5559926f 100644
--- a/fs/sysfs/inode.c
+++ b/fs/sysfs/inode.c
@@ -37,7 +37,9 @@ static struct backing_dev_info sysfs_backing_dev_info = {
};
static const struct inode_operations sysfs_inode_operations ={
+ .permission = sysfs_permission,
.setattr = sysfs_setattr,
+ .getattr = sysfs_getattr,
.setxattr = sysfs_setxattr,
};
@@ -196,7 +198,6 @@ static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
{
- inode->i_mode = iattr->ia_mode;
inode->i_uid = iattr->ia_uid;
inode->i_gid = iattr->ia_gid;
inode->i_atime = iattr->ia_atime;
@@ -227,38 +228,56 @@ static int sysfs_count_nlink(struct sysfs_dirent *sd)
return nr + 2;
}
+static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode)
+{
+ struct sysfs_inode_attrs *iattrs = sd->s_iattr;
+
+ inode->i_mode = sd->s_mode;
+ if (iattrs) {
+ /* sysfs_dirent has non-default attributes
+ * get them from persistent copy in sysfs_dirent
+ */
+ set_inode_attr(inode, &iattrs->ia_iattr);
+ security_inode_notifysecctx(inode,
+ iattrs->ia_secdata,
+ iattrs->ia_secdata_len);
+ }
+
+ if (sysfs_type(sd) == SYSFS_DIR)
+ inode->i_nlink = sysfs_count_nlink(sd);
+}
+
+int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
+{
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ struct inode *inode = dentry->d_inode;
+
+ mutex_lock(&sysfs_mutex);
+ sysfs_refresh_inode(sd, inode);
+ mutex_unlock(&sysfs_mutex);
+
+ generic_fillattr(inode, stat);
+ return 0;
+}
+
static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
{
struct bin_attribute *bin_attr;
- struct sysfs_inode_attrs *iattrs;
inode->i_private = sysfs_get(sd);
inode->i_mapping->a_ops = &sysfs_aops;
inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
inode->i_op = &sysfs_inode_operations;
- inode->i_ino = sd->s_ino;
lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
- iattrs = sd->s_iattr;
- if (iattrs) {
- /* sysfs_dirent has non-default attributes
- * get them for the new inode from persistent copy
- * in sysfs_dirent
- */
- set_inode_attr(inode, &iattrs->ia_iattr);
- if (iattrs->ia_secdata)
- security_inode_notifysecctx(inode,
- iattrs->ia_secdata,
- iattrs->ia_secdata_len);
- } else
- set_default_inode_attr(inode, sd->s_mode);
+ set_default_inode_attr(inode, sd->s_mode);
+ sysfs_refresh_inode(sd, inode);
/* initialize inode according to type */
switch (sysfs_type(sd)) {
case SYSFS_DIR:
inode->i_op = &sysfs_dir_inode_operations;
inode->i_fop = &sysfs_dir_operations;
- inode->i_nlink = sysfs_count_nlink(sd);
break;
case SYSFS_KOBJ_ATTR:
inode->i_size = PAGE_SIZE;
@@ -341,3 +360,14 @@ int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
else
return -ENOENT;
}
+
+int sysfs_permission(struct inode *inode, int mask)
+{
+ struct sysfs_dirent *sd = inode->i_private;
+
+ mutex_lock(&sysfs_mutex);
+ sysfs_refresh_inode(sd, inode);
+ mutex_unlock(&sysfs_mutex);
+
+ return generic_permission(inode, mask, NULL);
+}
diff --git a/fs/sysfs/symlink.c b/fs/sysfs/symlink.c
index 11374182ca67..c5eff49fa41b 100644
--- a/fs/sysfs/symlink.c
+++ b/fs/sysfs/symlink.c
@@ -214,6 +214,9 @@ const struct inode_operations sysfs_symlink_inode_operations = {
.readlink = generic_readlink,
.follow_link = sysfs_follow_link,
.put_link = sysfs_put_link,
+ .setattr = sysfs_setattr,
+ .getattr = sysfs_getattr,
+ .permission = sysfs_permission,
};
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index a96d9678ae73..12ccc07459d8 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -156,7 +156,9 @@ static inline void __sysfs_put(struct sysfs_dirent *sd)
struct inode *sysfs_get_inode(struct sysfs_dirent *sd);
void sysfs_delete_inode(struct inode *inode);
int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr);
+int sysfs_permission(struct inode *inode, int mask);
int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
+int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
size_t size, int flags);
int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name);
]
|
| 143/2 |
2026/11/18 08:51 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 47b5c64d0ab5e7136db2b78c6ec710e0d8a5a36b
Author: Ian Kent <raven@themaw.net>
Date: Fri Jul 16 17:28:34 2021 +0800
kernfs: use i_lock to protect concurrent inode updates
The inode operations .permission() and .getattr() use the kernfs node
write lock but all that's needed is the read lock to protect against
partial updates of these kernfs node fields which are all done under
the write lock.
And .permission() is called frequently during path walks and can cause
quite a bit of contention between kernfs node operations and path
walks when the number of concurrent walks is high.
To change kernfs_iop_getattr() and kernfs_iop_permission() to take
the rw sem read lock instead of the write lock an additional lock is
needed to protect against multiple processes concurrently updating
the inode attributes and link count in kernfs_refresh_inode().
The inode i_lock seems like the sensible thing to use to protect these
inode attribute updates so use it in kernfs_refresh_inode().
The last hunk in the patch, applied to kernfs_fill_super(), is possibly
not needed but taking the lock was present originally. I prefer to
continue to take it to protect against a partial update of the source
kernfs fields during the call to kernfs_refresh_inode() made by
kernfs_get_inode().
Reviewed-by: Miklos Szeredi <mszeredi@redhat.com>
Signed-off-by: Ian Kent <raven@themaw.net>
Link: https://lore.kernel.org/r/162642771474.63632.16295959115893904470.stgit@web.messagingengine.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index dad749f39518..c0eae1725435 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -185,11 +185,13 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns,
struct inode *inode = d_inode(path->dentry);
struct kernfs_node *kn = inode->i_private;
- down_write(&kernfs_rwsem);
+ down_read(&kernfs_rwsem);
+ spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
- up_write(&kernfs_rwsem);
-
generic_fillattr(&init_user_ns, inode, stat);
+ spin_unlock(&inode->i_lock);
+ up_read(&kernfs_rwsem);
+
return 0;
}
@@ -272,17 +274,21 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns,
struct inode *inode, int mask)
{
struct kernfs_node *kn;
+ int ret;
if (mask & MAY_NOT_BLOCK)
return -ECHILD;
kn = inode->i_private;
- down_write(&kernfs_rwsem);
+ down_read(&kernfs_rwsem);
+ spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
- up_write(&kernfs_rwsem);
+ ret = generic_permission(&init_user_ns, inode, mask);
+ spin_unlock(&inode->i_lock);
+ up_read(&kernfs_rwsem);
- return generic_permission(&init_user_ns, inode, mask);
+ return ret;
}
int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c
index baa4155ba2ed..f2f909d09f52 100644
--- a/fs/kernfs/mount.c
+++ b/fs/kernfs/mount.c
@@ -255,9 +255,9 @@ static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *k
sb->s_shrink.seeks = 0;
/* get root inode, initialize and unlock it */
- down_write(&kernfs_rwsem);
+ down_read(&kernfs_rwsem);
inode = kernfs_get_inode(sb, info->root->kn);
- up_write(&kernfs_rwsem);
+ up_read(&kernfs_rwsem);
if (!inode) {
pr_debug("kernfs: could not get root inode\n");
return -ENOMEM;
]
|
| 144/2 |
2026/11/18 08:51 |
tool |
git-show |
0m
Args:
Results: map[Output:commit aa1d058d48f292aa138e33ad12b7b4d18b5407cd
Author: Ian Kent <raven@themaw.net>
Date: Tue Oct 18 10:32:42 2022 +0800
kernfs: dont take i_lock on inode attr read
The kernfs write lock is held when the kernfs node inode attributes
are updated. Therefore, when either kernfs_iop_getattr() or
kernfs_iop_permission() are called the kernfs node inode attributes
won't change.
Consequently concurrent kernfs_refresh_inode() calls always copy the
same values from the kernfs node.
So there's no need to take the inode i_lock to get consistent values
for generic_fillattr() and generic_permission(), the kernfs read lock
is sufficient.
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Ian Kent <raven@themaw.net>
Link: https://lore.kernel.org/r/166606036215.13363.1288735296954908554.stgit@donald.themaw.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 3d783d80f5da..74f3453f4639 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -190,10 +190,8 @@ int kernfs_iop_getattr(struct user_namespace *mnt_userns,
struct kernfs_root *root = kernfs_root(kn);
down_read(&root->kernfs_rwsem);
- spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&init_user_ns, inode, stat);
- spin_unlock(&inode->i_lock);
up_read(&root->kernfs_rwsem);
return 0;
@@ -288,10 +286,8 @@ int kernfs_iop_permission(struct user_namespace *mnt_userns,
root = kernfs_root(kn);
down_read(&root->kernfs_rwsem);
- spin_lock(&inode->i_lock);
kernfs_refresh_inode(kn, inode);
ret = generic_permission(&init_user_ns, inode, mask);
- spin_unlock(&inode->i_lock);
up_read(&root->kernfs_rwsem);
return ret;
]
|
| 145/2 |
2026/11/18 08:51 |
tool |
git-blame |
0m
Args: map[End:1560 File:fs/open.c Start:1530]
Results: map[Output:^1da177e4c3f4 1530)
^1da177e4c3f4 1531) /*
^1da177e4c3f4 1532) * "id" is the POSIX thread ID. We use the
^1da177e4c3f4 1533) * files pointer for this..
^1da177e4c3f4 1534) */
021a160abf62c 1535) static int filp_flush(struct file *filp, fl_owner_t id)
^1da177e4c3f4 1536) {
45778ca819acc 1537) int retval = 0;
^1da177e4c3f4 1538)
30cee1e4861b5 1539) if (CHECK_DATA_CORRUPTION(file_count(filp) == 0, filp,
47d586913f2ab 1540) "VFS: Close: file count is 0 (f_op=%ps)",
47d586913f2ab 1541) filp->f_op)) {
45778ca819acc 1542) return 0;
^1da177e4c3f4 1543) }
^1da177e4c3f4 1544)
72c2d53192004 1545) if (filp->f_op->flush)
75e1fcc0b18df 1546) retval = filp->f_op->flush(filp, id);
^1da177e4c3f4 1547)
1abf0c718f15a 1548) if (likely(!(filp->f_mode & FMODE_PATH))) {
1abf0c718f15a 1549) dnotify_flush(filp, id);
1abf0c718f15a 1550) locks_remove_posix(filp, id);
1abf0c718f15a 1551) }
^1da177e4c3f4 1552) return retval;
^1da177e4c3f4 1553) }
^1da177e4c3f4 1554)
021a160abf62c 1555) int filp_close(struct file *filp, fl_owner_t id)
021a160abf62c 1556) {
021a160abf62c 1557) int retval;
021a160abf62c 1558)
021a160abf62c 1559) retval = filp_flush(filp, id);
a914bd93f3edf 1560) fput_close(filp);
]
|
| 146/2 |
2026/11/18 08:51 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 52252
output: 64
thoughts: 210
Thoughts: **Reflexive Deep Dive into Inode Timestamp Management**
Hold on a moment! I need to stop and very, very carefully consider this. My immediate impulse is to scrutinize two specific commits: `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` and `aa1d058d48f292aa138e33ad12b7b4d18b5407cd`. However, to be absolutely certain of the context and implications, I'm going to broaden my investigation. I need to also examine the changes introduced by `ffed24e22845` (specifically, the commit titled "sysfs, kernfs: move inode code to fs/kernfs/inode.c") and `c610d2d07879` (titled "fs: annotate inode timestamp accessors"). My goal here is to gain a complete understanding of precisely what these two latter commits did, and then to cross-reference that understanding with the first two commits I identified. I'll be running `git-show` on both `ffed24e22845` and `c610d2d07879` to achieve this thorough due diligence.
|
| 147/2 |
2026/11/18 08:51 |
tool |
git-show |
0m
Args: map[Commit:ffed24e22845 Stat:true]
Results: map[Output:commit ffed24e22845a3da0ae01095ae3f11c8d16e889d
Author: Tejun Heo <tj@kernel.org>
Date: Thu Nov 28 14:54:32 2013 -0500
sysfs, kernfs: move inode code to fs/kernfs/inode.c
There's nothing sysfs-specific in fs/sysfs/inode.c. Move everything
in it to fs/kernfs/inode.c. The respective declarations in
fs/sysfs/sysfs.h are moved to fs/kernfs/kernfs-internal.h.
This is pure relocation.
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/kernfs/inode.c | 327 ++++++++++++++++++++++++++++++++++++++++++
fs/kernfs/kernfs-internal.h | 13 ++
fs/sysfs/Makefile | 2 +-
fs/sysfs/inode.c | 342 --------------------------------------------
fs/sysfs/sysfs.h | 13 --
5 files changed, 341 insertions(+), 356 deletions(-)
]
|
| 148/2 |
2026/11/18 08:51 |
tool |
git-show |
0m
Args:
Results: map[Output:commit c610d2d0787961cdd6fc1de69d9be1ff3687e1a6
Author: Yu Peng <pengyu@kylinos.cn>
Date: Wed Jul 8 16:02:32 2026 +0800
fs: annotate inode timestamp accessors
syzbot reported a KCSAN race between fill_mg_cmtime() and
inode_set_ctime_to_ts() on inode->i_ctime_{sec,nsec}.
stat/getattr can sample inode timestamps while update paths store new
values concurrently, so KCSAN can report benign races on these fields.
Annotate the timestamp accessors with READ_ONCE()/WRITE_ONCE(), and use
the ctime accessor for the remaining ctime loads. This avoids the KCSAN
reports without changing timestamp semantics.
Fixes: 4e40eff0b573 ("fs: add infrastructure for multigrain timestamps")
Reported-by: syzbot+8b3bd9f8a06658479d4a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8b3bd9f8a06658479d4a
Signed-off-by: Yu Peng <pengyu@kylinos.cn>
Link: https://patch.msgid.link/20260708080232.2564807-1-pengyu@kylinos.cn
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/inode.c b/fs/inode.c
index a31aa7cb47f6..238fcd1cad6e 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -2830,8 +2830,8 @@ struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 t
{
trace_inode_set_ctime_to_ts(inode, &ts);
set_normalized_timespec64(&ts, ts.tv_sec, ts.tv_nsec);
- inode->i_ctime_sec = ts.tv_sec;
- inode->i_ctime_nsec = ts.tv_nsec;
+ WRITE_ONCE(inode->i_ctime_sec, ts.tv_sec);
+ WRITE_ONCE(inode->i_ctime_nsec, ts.tv_nsec);
return ts;
}
EXPORT_SYMBOL(inode_set_ctime_to_ts);
@@ -2905,7 +2905,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
*/
cns = smp_load_acquire(&inode->i_ctime_nsec);
if (cns & I_CTIME_QUERIED) {
- struct timespec64 ctime = { .tv_sec = inode->i_ctime_sec,
+ struct timespec64 ctime = { .tv_sec = inode_get_ctime_sec(inode),
.tv_nsec = cns & ~I_CTIME_QUERIED };
if (timespec64_compare(&now, &ctime) <= 0) {
@@ -2917,7 +2917,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
mgtime_counter_inc(mg_ctime_updates);
/* No need to cmpxchg if it's exactly the same */
- if (cns == now.tv_nsec && inode->i_ctime_sec == now.tv_sec) {
+ if (cns == now.tv_nsec && inode_get_ctime_sec(inode) == now.tv_sec) {
trace_ctime_xchg_skip(inode, &now);
goto out;
}
@@ -2926,7 +2926,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
/* Try to swap the nsec value into place. */
if (try_cmpxchg(&inode->i_ctime_nsec, &cur, now.tv_nsec)) {
/* If swap occurred, then we're (mostly) done */
- inode->i_ctime_sec = now.tv_sec;
+ WRITE_ONCE(inode->i_ctime_sec, now.tv_sec);
trace_ctime_ns_xchg(inode, cns, now.tv_nsec, cur);
mgtime_counter_inc(mg_ctime_swaps);
} else {
@@ -2941,7 +2941,7 @@ struct timespec64 inode_set_ctime_current(struct inode *inode)
goto retry;
}
/* Otherwise, keep the existing ctime */
- now.tv_sec = inode->i_ctime_sec;
+ now.tv_sec = inode_get_ctime_sec(inode);
now.tv_nsec = cur & ~I_CTIME_QUERIED;
}
out:
@@ -2974,7 +2974,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
/* pairs with try_cmpxchg below */
cur = smp_load_acquire(&inode->i_ctime_nsec);
cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED;
- cur_ts.tv_sec = inode->i_ctime_sec;
+ cur_ts.tv_sec = inode_get_ctime_sec(inode);
/* If the update is older than the existing value, skip it. */
if (timespec64_compare(&update, &cur_ts) <= 0)
@@ -3000,7 +3000,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
retry:
old = cur;
if (try_cmpxchg(&inode->i_ctime_nsec, &cur, update.tv_nsec)) {
- inode->i_ctime_sec = update.tv_sec;
+ WRITE_ONCE(inode->i_ctime_sec, update.tv_sec);
mgtime_counter_inc(mg_ctime_swaps);
return update;
}
@@ -3016,7 +3016,7 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 u
goto retry;
/* Otherwise, it was a new timestamp. */
- cur_ts.tv_sec = inode->i_ctime_sec;
+ cur_ts.tv_sec = inode_get_ctime_sec(inode);
cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED;
return cur_ts;
}
diff --git a/fs/stat.c b/fs/stat.c
index 89909746bed1..c461c3054234 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -53,7 +53,7 @@ void fill_mg_cmtime(struct kstat *stat, u32 request_mask, struct inode *inode)
}
stat->mtime = inode_get_mtime(inode);
- stat->ctime.tv_sec = inode->i_ctime_sec;
+ stat->ctime.tv_sec = inode_get_ctime_sec(inode);
stat->ctime.tv_nsec = (u32)atomic_read(pcn);
if (!(stat->ctime.tv_nsec & I_CTIME_QUERIED))
stat->ctime.tv_nsec = ((u32)atomic_fetch_or(I_CTIME_QUERIED, pcn));
diff --git a/include/linux/fs.h b/include/linux/fs.h
index d10897b3a1e3..e5b97e324db1 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1598,12 +1598,12 @@ struct timespec64 inode_set_ctime_deleg(struct inode *inode,
static inline time64_t inode_get_atime_sec(const struct inode *inode)
{
- return inode->i_atime_sec;
+ return READ_ONCE(inode->i_atime_sec);
}
static inline long inode_get_atime_nsec(const struct inode *inode)
{
- return inode->i_atime_nsec;
+ return READ_ONCE(inode->i_atime_nsec);
}
static inline struct timespec64 inode_get_atime(const struct inode *inode)
@@ -1617,8 +1617,8 @@ static inline struct timespec64 inode_get_atime(const struct inode *inode)
static inline struct timespec64 inode_set_atime_to_ts(struct inode *inode,
struct timespec64 ts)
{
- inode->i_atime_sec = ts.tv_sec;
- inode->i_atime_nsec = ts.tv_nsec;
+ WRITE_ONCE(inode->i_atime_sec, ts.tv_sec);
+ WRITE_ONCE(inode->i_atime_nsec, ts.tv_nsec);
return ts;
}
@@ -1633,12 +1633,12 @@ static inline struct timespec64 inode_set_atime(struct inode *inode,
static inline time64_t inode_get_mtime_sec(const struct inode *inode)
{
- return inode->i_mtime_sec;
+ return READ_ONCE(inode->i_mtime_sec);
}
static inline long inode_get_mtime_nsec(const struct inode *inode)
{
- return inode->i_mtime_nsec;
+ return READ_ONCE(inode->i_mtime_nsec);
}
static inline struct timespec64 inode_get_mtime(const struct inode *inode)
@@ -1651,8 +1651,8 @@ static inline struct timespec64 inode_get_mtime(const struct inode *inode)
static inline struct timespec64 inode_set_mtime_to_ts(struct inode *inode,
struct timespec64 ts)
{
- inode->i_mtime_sec = ts.tv_sec;
- inode->i_mtime_nsec = ts.tv_nsec;
+ WRITE_ONCE(inode->i_mtime_sec, ts.tv_sec);
+ WRITE_ONCE(inode->i_mtime_nsec, ts.tv_nsec);
return ts;
}
@@ -1677,12 +1677,12 @@ static inline struct timespec64 inode_set_mtime(struct inode *inode,
static inline time64_t inode_get_ctime_sec(const struct inode *inode)
{
- return inode->i_ctime_sec;
+ return READ_ONCE(inode->i_ctime_sec);
}
static inline long inode_get_ctime_nsec(const struct inode *inode)
{
- return inode->i_ctime_nsec & ~I_CTIME_QUERIED;
+ return READ_ONCE(inode->i_ctime_nsec) & ~I_CTIME_QUERIED;
}
static inline struct timespec64 inode_get_ctime(const struct inode *inode)
]
|
| 149/2 |
2026/11/18 08:51 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 55805
output: 35
thoughts: 123
Thoughts: **Hold On, Let's Re-Examine Those Commits!**
I've just had a significant realization, a moment of pause that demands careful attention. I need to stop and meticulously compare the commits identified as `aa1d058d48f2` and `e61ab4ae48fb`. This isn't a casual glance; I intend to scrutinize every detail to ensure I haven't overlooked anything crucial.
To achieve this, my immediate next step is to conduct a comprehensive review of *all* commits specifically within the `fs/kernfs/inode.c` file. My temporal scope for this investigation will span from the commit at `ffed24e22845` in 2013 all the way to the current HEAD. The objective is to leave no stone unturned, to definitively confirm that no commit affecting `fs/kernfs/inode.c` has slipped through my previous analysis. To facilitate this thoroughness, I'll be leveraging `git-log` directly on `fs/kernfs/inode.c` to generate a complete history, which I will then meticulously examine.
|
| 150/2 |
2026/11/18 08:51 |
tool |
git-log |
0m
Args: map[Count:60 PathPrefix:fs/kernfs/inode.c]
Results: map[Output:e2f0d6d4569d fs: port ->setattr() to pass const mnt_idmap
c3150ded7bc0 fs: port ->getattr() to pass const mnt_idmap
16d90b57d843 fs: port xattr to const mnt_idmap
41839e7bd92a fs: port ->permission() to pass const mnt_idmap
7b15d6cf25e6 kernfs: preserve security xattrs without allocating iattrs
d5e81a5650b5 kernfs: avoid iattr allocation in listxattr
1e7cd8a53b72 simpe_xattr: use per-sb cache
076e5cef28e2 simple_xattr: change interface to pass struct simple_xattrs **
6a07814ff643 kernfs: fix xattr race condition with multiple superblocks
507d8ce13f5b kernfs: Don't set_nlink for directories being removed
4fbe9e78bb41 xattr: move user limits for xattrs to generic infra
5bd97f5c5f24 kernfs: adapt to rhashtable-based simple_xattrs with lazy allocation
b4dbfd8653b3 Coccinelle-based conversion to use ->i_state accessors
c237aa9884f2 kernfs: don't fail listing extended attributes
d1f4e9026007 kernfs: remove iattr_mutex
2a45ac155937 kernfs: convert to new timestamp accessors
ffb2e0650827 kernfs: move kernfs_xattr_handlers to .rodata
5de75970c9fd xattr: simple_xattr_set() return old_xattr to be freed
0d72b92883c6 fs: pass the request_mask to generic_fillattr
f7f438589f46 kernfs: convert to ctime accessor functions
9caf69614225 kernfs: Introduce separate rwsem to protect inode attributes.
39f60c1ccee7 fs: port xattr to mnt_idmap
4609e1f18e19 fs: port ->permission() to pass mnt_idmap
b74d24f7a74f fs: port ->getattr() to pass mnt_idmap
c1632a0f1120 fs: port ->setattr() to pass mnt_idmap
24b3e3dd9c9c kernfs: fix all kernel-doc warnings and multiple typos
aa1d058d48f2 kernfs: dont take i_lock on inode attr read
393c3714081a kernfs: switch global kernfs_rwsem lock to per-fs lock
47b5c64d0ab5 kernfs: use i_lock to protect concurrent inode updates
7ba0273b2f34 kernfs: switch kernfs to use an rwsem
c1e3dbe9818e fs: move ramfs_aops to libfs
549c7297717c fs: make helpers idmap mount aware
0d56a4518d5e stat: handle idmapped mounts
e65ce2a50cf6 acl: handle idmapped mounts
2f221d6f7b88 attr: handle idmapped mounts
47291baa8ddf namei: make permission helpers idmapped mount aware
0c47383ba3bd kernfs: Add option to enable user xattrs
a46a22955bae kernfs: Add removed_size out param for simple_xattr_set
f0f3588f7a95 kernfs: don't bother with timestamp truncation
67c0496e87d1 kernfs: convert kernfs_node->id from union kernfs_node_id to u64
3818c1907a5e timestamp_truncate: Replace users of timespec64_trunc
55716d26439f treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 428
1537ad15c9c5 kernfs: fix xattr name handling in LSM helpers
b230d5aba2d1 LSM: add new hook for kernfs node initialization
0ac6075a32fc kernfs: use simple_xattrs for security attributes
d0c9c153b4bd kernfs: do not alloc iattrs in kernfs_xattr_get
05895219627c kernfs: clean up struct kernfs_iattrs
26e28d68b146 kernfs: Allocating memory for kernfs_iattrs with kmem_cache.
488dee96bb62 kernfs: allow creating kernfs objects with arbitrary uid/gid
95582b008388 vfs: change inode times to use struct timespec64
c53cd490b1a4 kernfs: introduce kernfs_node_id
319ba91d352a kernfs: don't set dentry->d_fsdata
4a3ef68acacf kernfs: implement i_generation
a528d35e8bfc statx: Add a system call to make enhanced file info available
b5a062344419 kernfs: Declare two local data structures static
fd50ecaddf83 vfs: Remove {get,set,remove}xattr inode operations
e72a1a8b3a5a kernfs: Switch to generic xattr handlers
c2050a454c7f fs: Replace current_fs_time() with current_time()
31051c85b5e2 fs: Give dentry to inode_change_ok() instead of inode
3767e255b390 switch ->setxattr() to passing dentry and inode separately
]
|
| 151/2 |
2026/11/18 08:51 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 57431
output: 31
thoughts: 40
Thoughts: **My Internal Monologue on `kernfs: Introduce separate rwsem to protect inode attributes.`**
Alright, time to dive into this `kernfs` change, commit `9caf69614225`. I need to understand precisely *why* they're introducing a new `rwsem` just for inode attributes. My initial thought is about potential contention. Are they seeing performance bottlenecks because the existing locks, likely the main `kernfs` dentry/inode lock, are too broad and are blocking unrelated inode attribute operations?
I'll pull up the commit message first to get their explicit reasoning. I'm expecting to see something along the lines of: "We're observing contention on the primary `kernfs` mutex/rwsem when multiple threads attempt to modify or read inode attributes concurrently with other filesystem operations. This new `rwsem` will allow attribute operations to proceed more independently, reducing lock hold times and improving scalability, especially in highly concurrent environments."
Then, I'll meticulously examine the diff. I'll be looking for:
* **Where the new `rwsem` is declared and initialized.** Is it directly associated with the `kernfs_node` structure?
* **How and where it's being acquired and released.** I need to trace every `down_read`/`up_read`, `down_write`/`up_write` (or their `rcu` equivalents if applicable, though `rwsem` is more typical here) call. Are they correctly paired? Are they in the right places to protect the specific inode attribute fields they intend to cover?
* **What specific inode attribute fields are now protected by this new `rwsem`.** This is crucial. Are they targeting `i_mode`, `i_size`, timestamps, ownership, etc.? Or are they being more granular?
* **How the existing locks are being *modified*.** Are they *removing* some protections from the old lock and *adding* them to the new one? Or is this purely additive, intended to *supplement* existing protections for better concurrency? The latter is more likely for a performance optimization.
* **Any new helper functions or changes to existing ones.** They might have refactored attribute read/write operations to incorporate the new locking mechanism.
* **Impact on related subsystems.** While the diff might not show everything, I'll consider if this change has any downstream implications for other parts of the kernel that interact with `kernfs` or inode attributes.
My hypothesis is that this is a targeted optimization. `kernfs` has grown significantly, and its locking strategy is critical for performance. By segmenting the locking for inode attributes, they're aiming to reduce the scope of locks held, allowing more operations to happen in parallel without interfering with each other. This is especially relevant for workloads that involve frequent metadata updates or heavy inode attribute inspection. I'm looking forward to seeing the concrete code that supports this reasoning.
|
| 152/2 |
2026/11/18 08:51 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 9caf696142252a466fb89e629d0eddcdced027b0
Author: Imran Khan <imran.f.khan@oracle.com>
Date: Thu Mar 9 22:09:30 2023 +1100
kernfs: Introduce separate rwsem to protect inode attributes.
Right now a global per-fs rwsem (kernfs_rwsem) synchronizes multiple
kernfs operations. On a large system with few hundred CPUs and few
hundred applications simultaneoulsy trying to access sysfs, this
results in multiple sys_open(s) contending on kernfs_rwsem via
kernfs_iop_permission and kernfs_dop_revalidate.
For example on a system with 384 cores, if I run 200 instances of an
application which is mostly executing the following loop:
for (int loop = 0; loop <100 ; loop++)
{
for (int port_num = 1; port_num < 2; port_num++)
{
for (int gid_index = 0; gid_index < 254; gid_index++ )
{
char ret_buf[64], ret_buf_lo[64];
char gid_file_path[1024];
int ret_len;
int ret_fd;
ssize_t ret_rd;
ub4 i, saved_errno;
memset(ret_buf, 0, sizeof(ret_buf));
memset(gid_file_path, 0, sizeof(gid_file_path));
ret_len = snprintf(gid_file_path, sizeof(gid_file_path),
"/sys/class/infiniband/%s/ports/%d/gids/%d",
dev_name,
port_num,
gid_index);
ret_fd = open(gid_file_path, O_RDONLY | O_CLOEXEC);
if (ret_fd < 0)
{
printf("Failed to open %s\n", gid_file_path);
continue;
}
/* Read the GID */
ret_rd = read(ret_fd, ret_buf, 40);
if (ret_rd == -1)
{
printf("Failed to read from file %s, errno: %u\n",
gid_file_path, saved_errno);
continue;
}
close(ret_fd);
}
}
I see contention around kernfs_rwsem as follows:
path_openat
|
|----link_path_walk.part.0.constprop.0
| |
| |--49.92%--inode_permission
| | |
| | --48.69%--kernfs_iop_permission
| | |
| | |--18.16%--down_read
| | |
| | |--15.38%--up_read
| | |
| | --14.58%--_raw_spin_lock
| | |
| | -----
| |
| |--29.08%--walk_component
| | |
| | --29.02%--lookup_fast
| | |
| | |--24.26%--kernfs_dop_revalidate
| | | |
| | | |--14.97%--down_read
| | | |
| | | --9.01%--up_read
| | |
| | --4.74%--__d_lookup
| | |
| | --4.64%--_raw_spin_lock
| | |
| | ----
Having a separate per-fs rwsem to protect kernfs inode attributes,
will avoid the above mentioned contention and result in better
performance as can bee seen below:
path_openat
|
|----link_path_walk.part.0.constprop.0
| |
| |
| |--27.06%--inode_permission
| | |
| | --25.84%--kernfs_iop_permission
| | |
| | |--9.29%--up_read
| | |
| | |--8.19%--down_read
| | |
| | --7.89%--_raw_spin_lock
| | |
| | ----
| |
| |--22.42%--walk_component
| | |
| | --22.36%--lookup_fast
| | |
| | |--16.07%--__d_lookup
| | | |
| | | --16.01%--_raw_spin_lock
| | | |
| | | ----
| | |
| | --6.28%--kernfs_dop_revalidate
| | |
| | |--3.76%--down_read
| | |
| | --2.26%--up_read
As can be seen from the above data the overhead due to both
kerfs_iop_permission and kernfs_dop_revalidate have gone down and
this also reduces overall run time of the earlier mentioned loop.
Signed-off-by: Imran Khan <imran.f.khan@oracle.com>
Link: https://lore.kernel.org/r/20230309110932.2889010-2-imran.f.khan@oracle.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index ef00b5fe8cee..953b2717c60e 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -770,12 +770,15 @@ int kernfs_add_one(struct kernfs_node *kn)
goto out_unlock;
/* Update timestamps on the parent */
+ down_write(&root->kernfs_iattr_rwsem);
+
ps_iattr = parent->iattr;
if (ps_iattr) {
ktime_get_real_ts64(&ps_iattr->ia_ctime);
ps_iattr->ia_mtime = ps_iattr->ia_ctime;
}
+ up_write(&root->kernfs_iattr_rwsem);
up_write(&root->kernfs_rwsem);
/*
@@ -940,6 +943,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
idr_init(&root->ino_idr);
init_rwsem(&root->kernfs_rwsem);
+ init_rwsem(&root->kernfs_iattr_rwsem);
INIT_LIST_HEAD(&root->supers);
/*
@@ -1462,11 +1466,14 @@ static void __kernfs_remove(struct kernfs_node *kn)
pos->parent ? pos->parent->iattr : NULL;
/* update timestamps on the parent */
+ down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
+
if (ps_iattr) {
ktime_get_real_ts64(&ps_iattr->ia_ctime);
ps_iattr->ia_mtime = ps_iattr->ia_ctime;
}
+ up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
kernfs_put(pos);
}
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 30494dcb0df3..b22b74d1a115 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -101,9 +101,9 @@ int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
int ret;
struct kernfs_root *root = kernfs_root(kn);
- down_write(&root->kernfs_rwsem);
+ down_write(&root->kernfs_iattr_rwsem);
ret = __kernfs_setattr(kn, iattr);
- up_write(&root->kernfs_rwsem);
+ up_write(&root->kernfs_iattr_rwsem);
return ret;
}
@@ -119,7 +119,7 @@ int kernfs_iop_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
return -EINVAL;
root = kernfs_root(kn);
- down_write(&root->kernfs_rwsem);
+ down_write(&root->kernfs_iattr_rwsem);
error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
if (error)
goto out;
@@ -132,7 +132,7 @@ int kernfs_iop_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
setattr_copy(&nop_mnt_idmap, inode, iattr);
out:
- up_write(&root->kernfs_rwsem);
+ up_write(&root->kernfs_iattr_rwsem);
return error;
}
@@ -189,10 +189,10 @@ int kernfs_iop_getattr(struct mnt_idmap *idmap,
struct kernfs_node *kn = inode->i_private;
struct kernfs_root *root = kernfs_root(kn);
- down_read(&root->kernfs_rwsem);
+ down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&nop_mnt_idmap, inode, stat);
- up_read(&root->kernfs_rwsem);
+ up_read(&root->kernfs_iattr_rwsem);
return 0;
}
@@ -285,10 +285,10 @@ int kernfs_iop_permission(struct mnt_idmap *idmap,
kn = inode->i_private;
root = kernfs_root(kn);
- down_read(&root->kernfs_rwsem);
+ down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
ret = generic_permission(&nop_mnt_idmap, inode, mask);
- up_read(&root->kernfs_rwsem);
+ up_read(&root->kernfs_iattr_rwsem);
return ret;
}
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index 236c3a6113f1..3297093c920d 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -47,6 +47,7 @@ struct kernfs_root {
wait_queue_head_t deactivate_waitq;
struct rw_semaphore kernfs_rwsem;
+ struct rw_semaphore kernfs_iattr_rwsem;
};
/* +1 to avoid triggering overflow warning when negating it */
]
|
| 153/2 |
2026/11/18 08:51 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 60646
output: 43
thoughts: 3921
Thoughts: **Unpacking the Data Race: A Deep Dive into `dnotify_flush` and `kernfs_iop_getattr`**
My thought process has been entirely consumed by meticulously dissecting this KCSAN report: `BUG: KCSAN: data-race in dnotify_flush / kernfs_iop_getattr`. I'm not just looking for a quick fix; I'm aiming for a profound understanding of *exactly* what happened, when it happened, and why every single candidate commit matters.
The core of the problem lies in the interaction between `dnotify_flush`, called during `close()`, and `kernfs_iop_getattr`, invoked by `stat()` or `fstat()`. The specific memory location at `0xffff88810632faa0` is being accessed by both, with one thread (task 3286 on CPU 1) attempting a read within `dnotify_flush`, and another (task 3283 on CPU 0) performing a write in `kernfs_iop_getattr`.
Let's break down the execution sequences and invariants.
On one hand, when a file descriptor is closed, `close()` eventually leads to `filp_flush()`, then `dnotify_flush()`. Inside `dnotify_flush`, the code reads `inode->i_mode`. My immediate question was: why is this read happening locklessly? The VFS usually treats file type bits in `inode->i_mode` as immutable once an inode is initialized. Permission bits are typically modified under specific locks like `inode_lock(inode)` or `inode->i_rwsem` during `setattr`. Crucially, even `chmod` operations, which modify `inode->i_mode`, don't block `dnotify_flush` during `close()`.
On the other hand, `kernfs` (and its predecessor `sysfs`) introduces a significant deviation. On *every single* `getattr` (`stat`/`fstat`) and `permission` (path lookup) call, `kernfs_refresh_inode()` is executed. This function unconditionally writes to `inode->i_mode` with the value from `kn->mode`. This happens regardless of whether the `inode->i_mode` has actually changed, and it does so without holding the necessary locks to synchronize with potential readers like `dnotify_flush`. This is the direct cause of the race condition – a read of `inode->i_mode` in `dnotify_flush` can occur simultaneously with a write to `inode->i_mode` in `kernfs_refresh_inode`.
Now, to truly understand the fix, I must examine *every* candidate commit.
**Candidate A: `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` ("sysfs: Implement sysfs_getattr & sysfs_permission", Nov 20, 2009)**
Before this commit, `sysfs` lacked `.getattr` and `.permission` operations. `fstat()` would simply call `generic_fillattr()`, which only read the existing `inode` data without modifying `inode->i_mode`. `inode->i_mode` was set once during initialization and only changed during explicit `chmod` operations under `inode->i_mutex`.
Commit `e61ab4ae48fb` introduced `sysfs_refresh_inode()` containing the problematic `inode->i_mode = sd->s_mode;` line. It then wired this into `sysfs_getattr` and `sysfs_permission`, ensuring that this unconditional write happened on *every* attribute read. This commit, I realized with certainty, is the origin of the data race. It created the possibility for `dnotify_flush` to race with `sysfs_getattr` (which later evolved into `kernfs_iop_getattr` with minimal changes).
**Candidate B: `aa1d058d48f292aa138e33ad12b7b4d18b5407cd` ("kernfs: dont take i_lock on inode attr read", Oct 18, 2022)**
This commit and its predecessor (`47b5c64d0ab5`) sparked a crucial line of inquiry: did holding `inode->i_lock` in `kernfs_iop_getattr` prevent the race? My analysis showed a resounding *no*. `dnotify_flush` does *not* hold `inode->i_lock`, nor do its callers (`filp_flush`, `filp_close_sync`). Therefore, even when `kernfs_iop_getattr` acquired `inode->i_lock`, it provided no synchronization against the lockless read in `dnotify_flush`. The data race persisted.
I then delved into what `aa1d058d48f2` *actually* did. It removed the `spin_lock(&inode->i_lock)` from `kernfs_iop_getattr`. This, while seemingly a reduction in locking, *didn't* fix the fundamental race with `dnotify_flush`. Instead, it opened up potential races between *concurrent* `kernfs_iop_getattr` calls themselves (write-write races, or read-write races with other VFS operations).
My bisecting thought experiment solidified this:
* **HEAD:** `kernfs_iop_getattr` writes `inode->i_mode` under `down_read(&root->kernfs_iattr_rwsem)`; `dnotify_flush` reads `inode->i_mode` locklessly. **Crash.**
* **Before `aa1d058d48f2`:** `kernfs_iop_getattr` writes `inode->i_mode` under `down_read(&root->kernfs_rwsem)` + `spin_lock(&inode->i_lock)`; `dnotify_flush` reads `inode->i_mode` locklessly. **Still crashes.**
* **Before `47b5c64d0ab5`:** `kernfs_iop_getattr` writes `inode->i_mode` under `down_write(&kernfs_rwsem)`. **Still crashes.**
* **Before `7ba0273b2f34`:** `kernfs_iop_getattr` writes `inode->i_mode` under `mutex_lock(&kernfs_mutex)`. **Still crashes.**
* **When moved to `fs/kernfs/inode.c` (`ffed24e22845`):** `sysfs_getattr` writes `inode->i_mode` under `mutex_lock(&sysfs_mutex)`. **Still crashes.**
* **Candidate A (`e61ab4ae48fb`):** `sysfs_getattr` writes `inode->i_mode` under `mutex_lock(&sysfs_mutex)`. **This is where the crash started.**
* **Before Candidate A:** `sysfs` had no `.getattr`, so `fstat` didn't write `inode->i_mode`. **No crash.**
The crucial realization here is that the `kernfs_iop_getattr` (or `sysfs_getattr`) operation, in its design, *always* writes `inode->i_mode` without properly synchronizing with the lockless read in `dnotify_flush` that occurs during `close()`. The specific locking mechanisms in `kernfs_iop_getattr` (whether `i_lock`, `kernfs_rwsem`, `sysfs_mutex`, etc.) only ever addressed races *between* attribute reads/writes on the same inode or within the `kernfs` filesystem itself, never the race with the VFS's `dnotify_flush` during `close()`.
To address this specific KCSAN report, I need to find a commit that correctly synchronizes the read in `dnotify_flush` with the write in `kernfs_iop_getattr`, or conversely, prevents the write in `kernfs_iop_getattr` from occurring during a `close` operation's `dnotify_flush`. I need to re-examine the file operations for kernfs to see if a `.flush` handler exists and if it acquires any locks that would prevent the race.
|
| 154/2 |
2026/11/18 08:52 |
tool |
grepper |
0m
Args: map[Expression:\.flush\b PathPrefixes:[fs/kernfs/ fs/sysfs/]]
Results:
|
| 155/2 |
2026/11/18 08:52 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 64628
output: 191
thoughts: 452
Thoughts: **My Debugging Journey: Unraveling a Potential Race Condition in File Closing**
I've been digging into the kernel's file closing mechanism, specifically around `__x64_sys_close(fd)`, and I've hit a significant point of confusion regarding file system operations. My initial exploration revealed that both `kernfs` and `sysfs` *lack* a `.flush` operation altogether. This struck me as odd, so I wanted to trace how this plays out during a file close.
When `__x64_sys_close(fd)` is invoked, the first step, `file_close_fd(fd)`, correctly removes the `struct file *` from the current process's file descriptor table. This involves acquiring `files->file_lock` as needed. The subsequent call, `filp_close_sync(file, files)`, then proceeds to `filp_flush(file, files)`.
It's within `filp_flush` that things get interesting. It first checks if the file count (`file_count(filp)`) has reached zero. Assuming it has, it then looks for the `filp->f_op->flush` operation. As I noted, this is `NULL` for `kernfs` and `sysfs`. The code then proceeds based on the `filp->f_mode` flag, and if it's not `FMODE_PATH`, it calls `dnotify_flush(filp, id)`.
Now, `dnotify_flush` retrieves the inode from the file pointer (`inode = file_inode(filp)`). Crucially, it then checks `if (!S_ISDIR(inode->i_mode))`. Here's where the alarm bells truly started ringing: this read of `inode->i_mode` is performed with *zero locks held*! This is a plain 2-byte read, and given the asynchronous nature of kernel operations and potential concurrent access, this looks like a textbook data race waiting to happen, especially considering that `kernfs` and `sysfs` don't have a `.flush` handler to potentially synchronize this access.
This realization immediately prompted a deeper investigation into how the kernel community handles these types of KCSAN (Kernel Concurrency Sanitizer) reported data-race bugs. I decided to examine how kernel developers and maintainers assign `Fixes:` tags to these issues. To do this, I performed a `git log` to find relevant KCSAN fix commits and then used `git show` on about 8-10 of them (specifically `f25f6319e248`, `bda345b3fc59`, `8535c259c8fb`, `c20cf1c3c8cd`, `1ca924044bf0`, `46bc52d13594`, `5e289c5a4a52`, `67636cab273e`, `b47bcab6ee92`, `f6c3665b6dc5`). My goal was to understand the established patterns and conventions for attributing fixes to specific upstream issues, which is vital for maintaining code integrity and understanding the history of bug resolution.
|
| 156/2 |
2026/11/18 08:53 |
tool |
git-show |
0m
Args: map[Commit:f25f6319e248 Stat:true]
Results: map[Output:commit f25f6319e248034f3df83352e450ab3172eece43
Author: Nicholas Velten <nicholasfvelten@gmail.com>
Date: Mon Oct 19 14:38:20 2026 -0300
bpf: Annotate data races on bpf_lru_node type
KCSAN reports a data race on bpf_lru_node::type between the LRU list
rotation and the free path of the common LRU:
BUG: KCSAN: data-race in __bpf_lru_list_rotate / bpf_lru_push_free
write to 0xffff88811b758022 of 1 bytes by task 4756 on cpu 1:
__bpf_lru_node_move
__bpf_lru_list_rotate_active
__bpf_lru_list_rotate
bpf_lru_list_pop_free_to_local
bpf_common_lru_pop_free
bpf_lru_pop_free
htab_lru_map_update_elem
read to 0xffff88811b758022 of 1 bytes by task 4752 on cpu 0:
bpf_lru_list_push_free
bpf_common_lru_push_free
bpf_lru_push_free
htab_lru_push_free
htab_lru_map_update_elem
value changed: 0x00 -> 0x01
bpf_lru_list_push_free() reads node->type for a WARN_ON_ONCE() sanity
check before taking l->lock. __bpf_lru_node_move(), called right after
under l->lock, does the same check, so drop the unlocked one.
bpf_common_lru_push_free() re-checks node->type under loc_l->lock after
the lockless READ_ONCE() at function entry. By then the node has been
removed from the htab, so only flush and rotate can change its type,
through __bpf_lru_node_move_in() and __bpf_lru_node_move() under
l->lock. Mark that re-check with READ_ONCE() and those two writers with
WRITE_ONCE(), following commit 6df8fb83301d ("bpf_lru_list: Read
double-checked variable once without lock"). The remaining writers
operate on a node with a single owner that nobody else reads and are
left alone.
Tested on bpf-next with CONFIG_KCSAN=y in a 4-vCPU virtme-ng guest,
using a reproducer that runs 8 threads issuing BPF_MAP_UPDATE_BATCH
on a 64-entry BPF_MAP_TYPE_LRU_HASH with overlapping keys.
Reported-by: syzbot+1ea4f564bf6f1f3d1dc4@syzkaller.appspotmail.com
Signed-off-by: Nicholas Velten <nicholasfvelten@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://patch.msgid.link/20261019-fix-lru-node-type-kcsan-v2-1-c10a410b3565@gmail.com
Closes: https://syzkaller.appspot.com/bug?extid=1ea4f564bf6f1f3d1dc4
kernel/bpf/bpf_lru_list.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
]
|
| 157/2 |
2026/11/18 08:53 |
tool |
git-show |
0m
Args: map[Commit:bda345b3fc59 Stat:true]
Results: map[Output:commit bda345b3fc59f2899c50379c64a0734a3e720683
Author: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
Date: Wed Oct 14 22:43:41 2026 +0700
workqueue: mark pool->nr_running as __data_racy
wq_worker_running() increments pool->nr_running with only preemption
disabled, while kick_pool_pick() reads it via need_more_worker() under
pool->lock, so the two can run concurrently on different CPUs. KCSAN
reports:
BUG: KCSAN: data-race in kick_pool_pick / wq_worker_running
write to 0xffff888059c2af24 of 4 bytes by task 3007 on cpu 0:
wq_worker_running+0x114/0x130
schedule_timeout+0x16e/0x180
io_schedule_timeout+0x4c/0x70
submit_bio_wait+0xd7/0x110
blkdev_issue_flush+0x4c/0x70
ext4_sync_file+0x60d/0x6c0
aio_fsync_work+0x7f/0x270
process_one_work+0x4ae/0xa00
read to 0xffff888059c2af24 of 4 bytes by task 3001 on cpu 1:
kick_pool_pick+0x7a/0x3a0
__queue_work+0x72d/0xcf0
queue_work_on+0x9d/0xb0
schedule_on_each_cpu+0x11a/0x2d0
vmstat_refresh+0x23/0x140
proc_sys_call_handler+0x2b9/0x4f0
The race is benign. The increment in wq_worker_running() is the only
lockless write to nr_running; decrements and the reset are done under
pool->lock, so a reader holding the lock sees either the old or the new
count. A stale 0 costs at most a spurious wakeup of an idle worker. A
nonzero count is correct because the incrementing worker is running and
re-checks the worklist under pool->lock before it stops counting. The
increment can also go 1 -> 2, e.g. when a worker sleeping in a work item
wakes up after kick_pool() woke another worker.
The lockless increment is part of how nr_running is designed to work,
as the comment above the field documents. Mark the field __data_racy
so that every reader is covered, not just need_more_worker().
Reported-by: syzbot+8922e6b8343f70be92e8@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8922e6b8343f70be92e8
Signed-off-by: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
kernel/workqueue.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
]
|
| 158/2 |
2026/11/18 08:53 |
tool |
git-show |
0m
Args: map[Commit:8535c259c8fb Stat:true]
Results: map[Output:commit 8535c259c8fb9768cbba38d50273080bf071744b
Author: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Date: Mon Sep 21 23:55:04 2026 +0200
percpu_counter: annotate lockless read in _limited_add()
syzbot reports a data race on fbc->count between the lockless read in the
fast path of __percpu_counter_limited_add() and the locked update in
percpu_counter_add_batch(), reached via shmem's used_blocks counter: the
alloc path reads it through percpu_counter_limited_add() while the free
path updates it through percpu_counter_sub().
Annotate the read rather than change the logic: the lockless read is
deliberate, only the annotation is missing. It is an approximation, not a
conservative bound. A concurrent flush moves value between fbc->count and
a per-cpu counter, and other CPUs' locked slow paths add to fbc->count
too, so a stale low value can let the fast path proceed where the slow
path would refuse. The error is bounded by the per-cpu slack (unknown =
batch * num_online_cpus()). This is existing behavior, no functional
change intended.
Read it with data_race(READ_ONCE(fbc->count)): data_race() tells KCSAN the
race is intended (READ_ONCE() alone still triggers the report, because
KCSAN reports against watchpoints set up by plain accesses), while
READ_ONCE() stops the compiler from refetching the value, as in
percpu_counter_read_positive(). The lock-protected accesses stay plain,
so future buggy lockless writes are still caught.
Link: https://lore.kernel.org/20260921215508.141641-1-azpijr@gmail.com
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: syzbot+a3c71b9db9c11c270f59@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a3c71b9db9c11c270f59
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Christoph Lameter <cl@gentwo.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
lib/percpu_counter.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
]
|
| 159/2 |
2026/11/18 08:53 |
tool |
git-show |
0m
Args: map[Commit:c20cf1c3c8cd Stat:true]
Results: map[Output:commit c20cf1c3c8cdd98618d6fcc794003a0e003ae858
Author: Jiayuan Chen <jiayuan.chen@linux.dev>
Date: Thu Aug 27 10:54:56 2026 +0800
mm/memcontrol: fix data-race on reading jiffies_64
KCSAN reported a data-race between tick_do_update_jiffies64() updating
jiffies_64 and mem_cgroup_flush_stats_ratelimited() reading it directly.
Unlike jiffies, jiffies_64 is not volatile, so raw reads are plain
accesses and can even be torn on 32-bit. Use get_jiffies_64() instead,
and fix the same pattern in mem_cgroup_flush_foreign().
Link: https://lore.kernel.org/20260827025457.116191-1-jiayuan.chen@linux.dev
Fixes: 508bed884767 ("mm: memcg: change flush_next_time to flush_last_time")
Fixes: 97b27821b485 ("writeback, memcg: Implement foreign dirty flushing")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: syzbot+ced4d9a8cadb5ef3adae@syzkaller.appspotmail.com
Acked-by: Muchun Song <muchun.song@linux.dev>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Chris Li <chrisl@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Tejun Heo <tj@kernel.org>
Cc: <stable@vger.kernel.org>
mm/memcontrol.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
]
|
| 160/2 |
2026/11/18 08:53 |
tool |
git-show |
0m
Args: map[Commit:1ca924044bf0 Stat:true]
Results: map[Output:commit 1ca924044bf0de879a1648be93a56939652267d3
Author: Christian Göttsche <cgzones@googlemail.com>
Date: Wed Sep 23 12:13:23 2026 +0200
selinux: fix data race on AVC latest_notif
avc_policy_seqno() reads latest_notif without holding notif_lock, while
avc_latest_notif_update() can update it under that lock. KCSAN reports
these concurrent accesses during policy boolean updates and permission
checks.
Use READ_ONCE() for the lockless read and WRITE_ONCE() for the update.
The spinlock continues to serialize the comparison and update against
other writers. Reads made while holding notif_lock cannot race with a
writer and can remain plain accesses.
This marks both sides of the intentional concurrent access and constrains
compiler transformations, without suppressing checking with data_race().
Cc: stable@vger.kernel.org
Fixes: 788e7dd4c22e ("SELinux: Improve read/write performance")
Reported-by: syzbot+a7f7cb0737bd91aa698c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a7f7cb0737bd91aa698c
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: Stephen Smalley <stephen.smalley.work@gmail.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
security/selinux/avc.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
]
|
| 161/2 |
2026/11/18 08:53 |
tool |
git-show |
0m
Args: map[Commit:b47bcab6ee92 Stat:true]
Results: map[Output:commit b47bcab6ee92d5ca6ba55c06b9a503e2663e942f
Author: Mauricio Faria de Oliveira <mfo@igalia.com>
Date: Tue Mar 17 20:22:16 2026 -0300
rtc: add data_race() in rtc_dev_poll()
The unlocked read of rtc->irq_data in rtc_dev_poll() can race with
the write in rtc_handle_legacy_irq() and also, theoretically, with
the write in rtc_dev_read().
These races should be safe (see inline comment), thus annotate the
read with data_race() for KCSAN.
Reported-by: syzbot+2d4127acca35ed7b31ad@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=2d4127acca35ed7b31ad
Signed-off-by: Mauricio Faria de Oliveira <mfo@igalia.com>
Link: https://patch.msgid.link/20260317-irq_data-v1-1-a2741002be60@igalia.com
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
drivers/rtc/dev.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
]
|
| 162/2 |
2026/11/18 08:53 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 69314
output: 79
thoughts: 148
Thoughts: **Observing a Potential Performance Bottleneck and Tracing its Origins**
My attention has been immediately drawn to the commit `1ca924044bf0`. The presence of "Fixes: 788e7dd4c22e ("SELinux: Improve read/write performance")" signals a likely performance enhancement, and my instinct is to delve deeper into its context. To fully understand the impact and the evolution of this fix, I need to pinpoint the exact timing of `788e7dd4c22e`. Running `git-show` on that specific commit is the next logical step to retrieve its metadata and understand its place in the history.
Simultaneously, I'm curious about a related refactoring effort. I need to identify the commit that performed the renaming of `sysfs_refresh_inode` to `kernfs_refresh_inode` and `sysfs_getattr` to `kernfs_iop_getattr`. Critically, I want to know when this renaming occurred in relation to commit `ffed24e22845`. This temporal relationship is crucial for understanding how the SELinux performance improvements might have interacted with or been affected by these broader kernel changes. My goal is to trace the lineage of these performance-related changes and ensure that the fix I'm looking at is properly contextualized within the kernel's development timeline.
|
| 163/2 |
2026/11/18 08:54 |
tool |
git-show |
0m
Args: map[Commit:788e7dd4c22e Stat:true]
Results: map[Output:commit 788e7dd4c22e6f41b3a118fd8c291f831f6fddbb
Author: Yuichi Nakamura <ynakam@hitachisoft.jp>
Date: Fri Sep 14 09:27:07 2007 +0900
SELinux: Improve read/write performance
It reduces the selinux overhead on read/write by only revalidating
permissions in selinux_file_permission if the task or inode labels have
changed or the policy has changed since the open-time check. A new LSM
hook, security_dentry_open, is added to capture the necessary state at open
time to allow this optimization.
(see http://marc.info/?l=selinux&m=118972995207740&w=2)
Signed-off-by: Yuichi Nakamura<ynakam@hitachisoft.jp>
Acked-by: Stephen Smalley <sds@tycho.nsa.gov>
Signed-off-by: James Morris <jmorris@namei.org>
fs/open.c | 4 +++
include/linux/security.h | 18 +++++++++++++
security/dummy.c | 6 +++++
security/selinux/avc.c | 5 ++++
security/selinux/hooks.c | 53 ++++++++++++++++++++++++++++++++++++++-
security/selinux/include/avc.h | 2 ++
security/selinux/include/objsec.h | 2 ++
7 files changed, 89 insertions(+), 1 deletion(-)
]
|
| 164/2 |
2026/11/18 08:54 |
tool |
git-log |
0m
Args: map[CodeRegexp:kernfs_refresh_inode Count:10 PathPrefix:fs/kernfs/inode.c]
Results: map[Output:c637b8acbe07 kernfs: s/sysfs/kernfs/ in internal functions and whatever is left
]
|
| 165/2 |
2026/11/18 08:54 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 69999
output: 81
thoughts: 687
Thoughts: **My Investigative Journey into a Kernel Bug**
WHOA! I've just stumbled upon a fascinating situation while investigating a kernel bug. The commit that supposedly *fixes* the issue, `788e7dd4c22e`, is from way back on **September 14, 2007**! This immediately caught my attention because our instructions for bug hunting are quite rigorous, and this older fix seemed out of place for a recent problem.
My primary directive is to **establish the root cause first**. This involves dissecting the crash report, analyzing the code pointed to by stack traces, and understanding the exact invariant that's being violated, along with the specific execution path leading to the crash. I need to pinpoint the precise functions, structs, invariants, or lines of code that harbor the faulty logic.
Following that, the instructions emphasize using `git` tools like `git-blame`, `git-log`, and `git-show` to meticulously trace the history of the affected code. A crucial part of this is to **trace past pure refactorings, code movements, formatting changes, or variable renames** to unearth the *actual* commit that introduced the buggy logic or broke an existing invariant.
Furthermore, I'm explicitly told **NOT to blame commits that merely added debugging or sanitizer checks** (like `WARN_ON`, `BUG_ON`, or KASAN/KMSAN/LOCKDEP annotations) unless the check *itself* is incorrect and is the source of the bug. Similarly, I'm instructed **NOT to return a merge commit** unless the bug was introduced by the merge conflict resolution itself; the goal is to identify the underlying, non-merge commit(s).
This is where my current thought process took a sharp turn. While investigating, I found myself questioning the initial assumption about the commit that introduced the buggy logic. The instruction to "Establish the root cause first" and the subsequent steps are guiding me to be exceptionally thorough.
So, I've paused to critically examine everything. My current focus is on a specific commit: `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`, titled "sysfs: Implement sysfs_getattr & sysfs_permission". I'm asking myself, "Is this *absolutely* the commit that introduced the buggy logic?"
To answer this, I'm digging into the details. Specifically, I'm looking at line 169 in `fs/kernfs/inode.c`, which contains the `kernfs_refresh_inode` function. This line, `inode->i_mode = kn->mode;`, is under scrutiny. My next step is to **trace `git blame` on this line and all subsequent lines up to 195**. I want to meticulously inspect the commit identified by this blame, `c637b8acbe07` ("kernfs: s/sysfs/kernfs/ in internal functions and whatever is left"), to ensure my bisection log accurately reflects the *exact* commit chain from the current `HEAD` back to the problematic `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`. This ensures I don't miss any subtle introductions of faulty logic as I peel back the layers of history.
|
| 166/2 |
2026/11/18 08:55 |
tool |
git-blame |
0m
Args: map[End:197 File:fs/kernfs/inode.c Start:165]
Results: map[Output:c637b8acbe079 165) static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
ffed24e22845a 166) {
d1f4e9026007d 167) struct kernfs_iattrs *attrs;
ffed24e22845a 168)
adc5e8b58f488 169) inode->i_mode = kn->mode;
d1f4e9026007d 170) attrs = kernfs_iattrs_noalloc(kn);
0ac6075a32fc0 171) if (attrs)
324a56e16e44b 172) /*
324a56e16e44b 173) * kernfs_node has non-default attributes get them from
324a56e16e44b 174) * persistent copy in kernfs_node.
ffed24e22845a 175) */
05895219627c4 176) set_inode_attr(inode, attrs);
ffed24e22845a 177)
507d8ce13f5b9 178) if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING))
adc5e8b58f488 179) set_nlink(inode, kn->dir.subdirs + 2);
ffed24e22845a 180) }
ffed24e22845a 181)
c3150ded7bc0e 182) int kernfs_iop_getattr(const struct mnt_idmap *idmap,
549c7297717c3 183) const struct path *path, struct kstat *stat,
a528d35e8bfcc 184) u32 request_mask, unsigned int query_flags)
ffed24e22845a 185) {
a528d35e8bfcc 186) struct inode *inode = d_inode(path->dentry);
319ba91d352a7 187) struct kernfs_node *kn = inode->i_private;
393c3714081a5 188) struct kernfs_root *root = kernfs_root(kn);
ffed24e22845a 189)
9caf696142252 190) down_read(&root->kernfs_iattr_rwsem);
c637b8acbe079 191) kernfs_refresh_inode(kn, inode);
0d72b92883c65 192) generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
9caf696142252 193) up_read(&root->kernfs_iattr_rwsem);
47b5c64d0ab5e 194)
ffed24e22845a 195) return 0;
ffed24e22845a 196) }
ffed24e22845a 197)
]
|
| 167/2 |
2026/11/18 08:55 |
tool |
git-show |
0m
Args: map[Commit:c637b8acbe07 File:fs/kernfs/inode.c]
Results: map[Output:commit c637b8acbe079edb477d887041755b489036f146
Author: Tejun Heo <tj@kernel.org>
Date: Wed Dec 11 14:11:58 2013 -0500
kernfs: s/sysfs/kernfs/ in internal functions and whatever is left
kernfs has just been separated out from sysfs and we're already in
full conflict mode. Nothing can make the situation any worse. Let's
take the chance to name things properly.
This patch performs the following renames.
* s/sysfs_*()/kernfs_*()/ in all internal functions
* s/sysfs/kernfs/ in internal strings, comments and whatever is remaining
* Uniformly rename various vfs operations so that they're consistently
named and distinguishable.
This patch is strictly rename only and doesn't introduce any
functional difference.
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index c5f231e8d36d..e55126f85bd2 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -31,16 +31,16 @@ static struct backing_dev_info kernfs_bdi = {
};
static const struct inode_operations kernfs_iops = {
- .permission = sysfs_permission,
- .setattr = sysfs_setattr,
- .getattr = sysfs_getattr,
- .setxattr = sysfs_setxattr,
- .removexattr = sysfs_removexattr,
- .getxattr = sysfs_getxattr,
- .listxattr = sysfs_listxattr,
+ .permission = kernfs_iop_permission,
+ .setattr = kernfs_iop_setattr,
+ .getattr = kernfs_iop_getattr,
+ .setxattr = kernfs_iop_setxattr,
+ .removexattr = kernfs_iop_removexattr,
+ .getxattr = kernfs_iop_getxattr,
+ .listxattr = kernfs_iop_listxattr,
};
-void __init sysfs_inode_init(void)
+void __init kernfs_inode_init(void)
{
if (bdi_init(&kernfs_bdi))
panic("failed to init kernfs_bdi");
@@ -115,7 +115,7 @@ int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
return ret;
}
-int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
+int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
{
struct inode *inode = dentry->d_inode;
struct kernfs_node *kn = dentry->d_fsdata;
@@ -141,8 +141,8 @@ int sysfs_setattr(struct dentry *dentry, struct iattr *iattr)
return error;
}
-static int sysfs_sd_setsecdata(struct kernfs_node *kn, void **secdata,
- u32 *secdata_len)
+static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
+ u32 *secdata_len)
{
struct kernfs_iattrs *attrs;
void *old_secdata;
@@ -163,8 +163,8 @@ static int sysfs_sd_setsecdata(struct kernfs_node *kn, void **secdata,
return 0;
}
-int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
- size_t size, int flags)
+int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
+ const void *value, size_t size, int flags)
{
struct kernfs_node *kn = dentry->d_fsdata;
struct kernfs_iattrs *attrs;
@@ -188,7 +188,7 @@ int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
return error;
mutex_lock(&kernfs_mutex);
- error = sysfs_sd_setsecdata(kn, &secdata, &secdata_len);
+ error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
mutex_unlock(&kernfs_mutex);
if (secdata)
@@ -202,7 +202,7 @@ int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
return -EINVAL;
}
-int sysfs_removexattr(struct dentry *dentry, const char *name)
+int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
{
struct kernfs_node *kn = dentry->d_fsdata;
struct kernfs_iattrs *attrs;
@@ -214,8 +214,8 @@ int sysfs_removexattr(struct dentry *dentry, const char *name)
return simple_xattr_remove(&attrs->xattrs, name);
}
-ssize_t sysfs_getxattr(struct dentry *dentry, const char *name, void *buf,
- size_t size)
+ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
+ size_t size)
{
struct kernfs_node *kn = dentry->d_fsdata;
struct kernfs_iattrs *attrs;
@@ -227,7 +227,7 @@ ssize_t sysfs_getxattr(struct dentry *dentry, const char *name, void *buf,
return simple_xattr_get(&attrs->xattrs, name, buf, size);
}
-ssize_t sysfs_listxattr(struct dentry *dentry, char *buf, size_t size)
+ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
{
struct kernfs_node *kn = dentry->d_fsdata;
struct kernfs_iattrs *attrs;
@@ -254,7 +254,7 @@ static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
inode->i_ctime = iattr->ia_ctime;
}
-static void sysfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
+static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs = kn->iattr;
@@ -273,21 +273,21 @@ static void sysfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
set_nlink(inode, kn->dir.subdirs + 2);
}
-int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
- struct kstat *stat)
+int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
+ struct kstat *stat)
{
struct kernfs_node *kn = dentry->d_fsdata;
struct inode *inode = dentry->d_inode;
mutex_lock(&kernfs_mutex);
- sysfs_refresh_inode(kn, inode);
+ kernfs_refresh_inode(kn, inode);
mutex_unlock(&kernfs_mutex);
generic_fillattr(inode, stat);
return 0;
}
-static void sysfs_init_inode(struct kernfs_node *kn, struct inode *inode)
+static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
{
kernfs_get(kn);
inode->i_private = kn;
@@ -296,7 +296,7 @@ static void sysfs_init_inode(struct kernfs_node *kn, struct inode *inode)
inode->i_op = &kernfs_iops;
set_default_inode_attr(inode, kn->mode);
- sysfs_refresh_inode(kn, inode);
+ kernfs_refresh_inode(kn, inode);
/* initialize inode according to type */
switch (kernfs_type(kn)) {
@@ -319,7 +319,7 @@ static void sysfs_init_inode(struct kernfs_node *kn, struct inode *inode)
}
/**
- * sysfs_get_inode - get inode for kernfs_node
+ * kernfs_get_inode - get inode for kernfs_node
* @sb: super block
* @kn: kernfs_node to allocate inode for
*
@@ -333,25 +333,25 @@ static void sysfs_init_inode(struct kernfs_node *kn, struct inode *inode)
* RETURNS:
* Pointer to allocated inode on success, NULL on failure.
*/
-struct inode *sysfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
+struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
{
struct inode *inode;
inode = iget_locked(sb, kn->ino);
if (inode && (inode->i_state & I_NEW))
- sysfs_init_inode(kn, inode);
+ kernfs_init_inode(kn, inode);
return inode;
}
/*
- * The kernfs_node serves as both an inode and a directory entry for sysfs.
- * To prevent the sysfs inode numbers from being freed prematurely we take
- * a reference to kernfs_node from the sysfs inode. A
+ * The kernfs_node serves as both an inode and a directory entry for
+ * kernfs. To prevent the kernfs inode numbers from being freed
+ * prematurely we take a reference to kernfs_node from the kernfs inode. A
* super_operations.evict_inode() implementation is needed to drop that
* reference upon inode destruction.
*/
-void sysfs_evict_inode(struct inode *inode)
+void kernfs_evict_inode(struct inode *inode)
{
struct kernfs_node *kn = inode->i_private;
@@ -360,7 +360,7 @@ void sysfs_evict_inode(struct inode *inode)
kernfs_put(kn);
}
-int sysfs_permission(struct inode *inode, int mask)
+int kernfs_iop_permission(struct inode *inode, int mask)
{
struct kernfs_node *kn;
@@ -370,7 +370,7 @@ int sysfs_permission(struct inode *inode, int mask)
kn = inode->i_private;
mutex_lock(&kernfs_mutex);
- sysfs_refresh_inode(kn, inode);
+ kernfs_refresh_inode(kn, inode);
mutex_unlock(&kernfs_mutex);
return generic_permission(inode, mask);
]
|
| 168/2 |
2026/11/18 08:55 |
llm |
cause-finder |
9m
Model:
Tokens:
input: 74707
output: 45
thoughts: 54
Thoughts: **Exploring the `git blame` Chain for `inode->i_mode = kn->mode;`**
Right, so my next step in this debugging or code exploration is to dig into the `git blame` output, specifically for the line `inode->i_mode = kn->mode;`. The commit hash that's surfaced is `adc5e8b58f488`. My rationale here is straightforward: I want to ensure I'm not missing any crucial context. By tracing back this particular commit, I'm aiming to reconstruct the full "blame chain" associated with this line. This means understanding not just who introduced this assignment, but also the history of changes leading up to it, and any subsequent modifications or related commits. It's about getting a complete picture, ensuring no stone is left unturned as I trace the evolution of this specific piece of code.
|
| 169/2 |
2026/11/18 09:04 |
tool |
git-show |
0m
Args: map[Commit:adc5e8b58f488 File:fs/kernfs/inode.c]
Results: map[Output:commit adc5e8b58f4886d45f79f4ff41a09001a76a6b12
Author: Tejun Heo <tj@kernel.org>
Date: Wed Dec 11 14:11:54 2013 -0500
kernfs: drop s_ prefix from kernfs_node members
kernfs has just been separated out from sysfs and we're already in
full conflict mode. Nothing can make the situation any worse. Let's
take the chance to name things properly.
s_ prefix for kernfs members is used inconsistently and a misnomer
now. It's not like kernfs_node is used widely across the kernel
making the ability to grep for the members particularly useful. Let's
just drop the prefix.
This patch is strictly rename only and doesn't introduce any
functional difference.
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 9e74eed63539..f6c0aae3dd5c 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -50,23 +50,23 @@ static struct sysfs_inode_attrs *sysfs_inode_attrs(struct kernfs_node *kn)
{
struct iattr *iattrs;
- if (kn->s_iattr)
- return kn->s_iattr;
+ if (kn->iattr)
+ return kn->iattr;
- kn->s_iattr = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
- if (!kn->s_iattr)
+ kn->iattr = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL);
+ if (!kn->iattr)
return NULL;
- iattrs = &kn->s_iattr->ia_iattr;
+ iattrs = &kn->iattr->ia_iattr;
/* assign default attributes */
- iattrs->ia_mode = kn->s_mode;
+ iattrs->ia_mode = kn->mode;
iattrs->ia_uid = GLOBAL_ROOT_UID;
iattrs->ia_gid = GLOBAL_ROOT_GID;
iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
- simple_xattrs_init(&kn->s_iattr->xattrs);
+ simple_xattrs_init(&kn->iattr->xattrs);
- return kn->s_iattr;
+ return kn->iattr;
}
static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
@@ -93,7 +93,7 @@ static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
iattrs->ia_ctime = iattr->ia_ctime;
if (ia_valid & ATTR_MODE) {
umode_t mode = iattr->ia_mode;
- iattrs->ia_mode = kn->s_mode = mode;
+ iattrs->ia_mode = kn->mode = mode;
}
return 0;
}
@@ -256,9 +256,9 @@ static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
static void sysfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
- struct sysfs_inode_attrs *attrs = kn->s_iattr;
+ struct sysfs_inode_attrs *attrs = kn->iattr;
- inode->i_mode = kn->s_mode;
+ inode->i_mode = kn->mode;
if (attrs) {
/*
* kernfs_node has non-default attributes get them from
@@ -270,7 +270,7 @@ static void sysfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
}
if (sysfs_type(kn) == SYSFS_DIR)
- set_nlink(inode, kn->s_dir.subdirs + 2);
+ set_nlink(inode, kn->dir.subdirs + 2);
}
int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
@@ -295,7 +295,7 @@ static void sysfs_init_inode(struct kernfs_node *kn, struct inode *inode)
inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
inode->i_op = &sysfs_inode_operations;
- set_default_inode_attr(inode, kn->s_mode);
+ set_default_inode_attr(inode, kn->mode);
sysfs_refresh_inode(kn, inode);
/* initialize inode according to type */
@@ -305,7 +305,7 @@ static void sysfs_init_inode(struct kernfs_node *kn, struct inode *inode)
inode->i_fop = &sysfs_dir_operations;
break;
case SYSFS_KOBJ_ATTR:
- inode->i_size = kn->s_attr.size;
+ inode->i_size = kn->attr.size;
inode->i_fop = &kernfs_file_operations;
break;
case SYSFS_KOBJ_LINK:
@@ -337,7 +337,7 @@ struct inode *sysfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
{
struct inode *inode;
- inode = iget_locked(sb, kn->s_ino);
+ inode = iget_locked(sb, kn->ino);
if (inode && (inode->i_state & I_NEW))
sysfs_init_inode(kn, inode);
]
|
| 170/2 |
2026/11/18 09:04 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 76395
output: 119
thoughts: 814
Thoughts: **Reflecting on the `kernfs` Data Race: Was `aa1d058d48f292aa138e33ad12b7b4d18b5407cd` a Secondary Culprit?**
I've just had a significant realization that needs careful consideration. I'm questioning whether commit `aa1d058d48f292aa138e33ad12b7b4d18b5407cd` ("kernfs: dont take i_lock on inode attr read") should be treated as a secondary introducing commit for the data race I'm investigating, or if commit `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` is *solely* responsible. I need to approach this with absolute rigor.
The core question I'm posing to myself is: Did `aa1d058d48f292aa138e33ad12b7b4d18b5407cd` introduce or contribute to the data race observed in `dnotify_flush` versus `kernfs_iop_getattr`? Let's break down the evidence meticulously.
Looking at the crash report, I see the following interactions:
* On CPU 1 (task 3286), `dnotify_flush` is executing `if (!S_ISDIR(inode->i_mode))`. Crucially, this is a plain, two-byte read of `inode->i_mode` and *no locks are held* at this point.
* On CPU 0 (task 3283, running `udevd`), `kernfs_iop_getattr` is calling into `kernfs_refresh_inode`, which then performs `inode->i_mode = kn->mode;`. This is a plain, two-byte write to `inode->i_mode`, but it *is* protected by `down_read(&root->kernfs_iattr_rwsem)`.
Now, I need to specifically ask: Does `dnotify_flush` acquire `inode->i_lock`? The answer is a definitive **no**. Historically, `dnotify_flush` has *never* acquired `inode->i_lock`, nor has it ever held `kernfs_rwsem`, `sysfs_mutex`, or `inode->i_rwsem`.
This is where my critical thought emerges: Even *before* commit `aa1d058d48f2` was introduced – when `kernfs_iop_getattr` *did* acquire `spin_lock(&inode->i_lock)` – the write to `inode->i_mode` by `kernfs_iop_getattr` was still racing with the read of `inode->i_mode` by `dnotify_flush` in the *exact same manner*. The lack of locking in `dnotify_flush` is the persistent vulnerability.
However, I'm still pondering: Why, then, would commit `aa1d058d48f2` still be relevant for a developer trying to fix this `kernfs_refresh_inode` issue? I need to really think this through.
Consider this scenario: If a kernel developer were to fix this syzbot KCSAN bug within `fs/kernfs/inode.c` – perhaps by modifying the line `inode->i_mode = kn->mode;` in `kernfs_refresh_inode()` to something like `if (READ_ONCE(inode->i_mode) != kn->mode) WRITE_ONCE(inode->i_mode, kn->mode);` or simply `WRITE_ONCE(inode->i_mode, kn->mode);` and ensuring `READ_ONCE(inode->i_mode)` is used in `dnotify_flush()`, or by only updating `inode->i_mode` when it differs from `kn->mode` – what `Fixes:` tag would such a patch carry?
I must pause and seriously consider that. What would be the correct attribution? To answer this, I feel compelled to examine all recent KCSAN, VFS, and kernfs fixes in the git log, specifically looking at the `Fixes:` tags applied by maintainers like Greg KH, Tejun Heo, Christian Brauner, and Jan Kara to changes in `fs/kernfs/`, `fs/notify/`, and the broader `fs/` directory. I'm going to run `git-log` on `fs/kernfs/` and `fs/notify/` filtering for keywords like `Fixes:`, `data-race`, `KCSAN`, or `syzbot` to gain definitive clarity on how these issues are being historically and currently addressed and attributed.
|
| 171/2 |
2026/11/18 09:05 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[Fixes:] PathPrefix:fs/kernfs/]
Results: map[Output:ef179363b043 kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()
2d2a2d7aa987 super: make iterate_supers_type() deletion-safe
341c65579de5 kernfs: don't hold kernfs_rwsem across dir_emit()
5ebcb578adee kernfs: don't repeat or skip an entry when readdir resumes
23985e2b1f31 kernfs: don't hold kernfs_rwsem across notification delivery
81bdc85b4c08 kernfs: fix up the unlocked attribute reads on the creation paths
dc1a0cc95737 kernfs: don't lose IN_DELETE_SELF when decoding a file handle
6034974e3aec kernfs: take kernfs_rename_lock for same-parent renames too
7b15d6cf25e6 kernfs: preserve security xattrs without allocating iattrs
6cccc49b027c kernfs: link kn to its parent before the LSM init hook
1e7cd8a53b72 simpe_xattr: use per-sb cache
6a07814ff643 kernfs: fix xattr race condition with multiple superblocks
0fdde3f2aead kernfs: fix suspicious RCU usage in kernfs_put()
16de94a1b090 kernfs: Add missing documentation for kernfs_put_active's drop_supers argument
2b742094582d fs/kernfs: null-ptr deref in simple_xattrs_free()
382b1e8f30f7 kernfs: fix memory leak of kernfs_iattrs in __kernfs_new_node
3c9ba2777d6c kernfs: Fix UAF in polling when open file is released
c237aa9884f2 kernfs: don't fail listing extended attributes
071d8e4c2a3b kernfs: Relax constraint in draining guard
c5020c5be9d2 kernfs: Move dput() outside of the RCU section.
]
|
| 172/2 |
2026/11/18 09:05 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[Fixes:] PathPrefix:fs/notify/]
Results: map[Output:581230d2ec3c inotify: do not coalesce rename events
1dcf2be94b9b fsnotify: preserve 64-bit pre-content ranges
36ebddca56b9 fsnotify: prevent mark additions during connector teardown
ff05910cb1b7 fanotify: report names longer than NAME_MAX
e422777fdd47 fsnotify: Fix stale object mask after concurrent mark updates
68615158c12d fanotify: report full event length for FIONREAD
d7f1cf5be33e fanotify: fix use-after-free of file range info
17463fe75130 fanotify: stop permission watchdog when timeout is zero
a3aa899823dd fanotify: initialize permission event watchdog state
4aca914ac152 fsnotify: fix inode reference leak in fsnotify_recalc_mask()
7746e3bd4cc1 fanotify: fix false positive on permission events
66052a768d47 fanotify: call fanotify_events_supported() before path_permission() and security_path_notify()
0d5ee3373426 fanotify: avoid/silence premature LSM capability checks
6a320935fa42 inotify: fix watch count leak when fsnotify_add_inode_mark_locked() fails
a7c4bb43bfdc fs/notify: call exportfs_encode_fid with s_umount
62e59ffe8787 fanotify: Validate the return value of mnt_ns_from_dentry() before dereferencing
fdfe0133473a fix a leak in fcntl_dirnotify()
8631e01c2c5d fanotify: sanitize handle_type values when reporting fid
c73c67026fe6 fanotify: fix flush of mntns marks
0c0214df28f0 fanotify: Fix crash in fanotify_init(2)
]
|
| 173/2 |
2026/11/18 09:05 |
tool |
git-log |
0m
Args: map[Count:20 MessageRegexps:[data-race|data race|KCSAN] PathPrefix:fs/]
Results:
|
| 174/2 |
2026/11/18 09:05 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 78398
output: 59
thoughts: 131
Thoughts: **My Reaction to the `fs/kernfs/` Commits**
Wait a second! Hold everything. My immediate reaction, upon seeing those two commits at the very top of the `fs/kernfs/` directory, is one of intense curiosity and a strong urge to investigate.
1. The first commit, `ef179363b043`, titled "kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()", immediately flags a potential issue. Why would `READ_ONCE()` be problematic on a `kgid_t` within `kernfs_new_node()`? This implies a subtle concurrency or memory ordering concern that I need to understand. The use of `READ_ONCE()` suggests an attempt to deal with potential race conditions, but its inapplicability here indicates a deeper problem in how `kgid_t` is being handled, or perhaps a misunderstanding of its underlying representation in a concurrent context.
2. The second commit, `81bdc85b4c08`, with the title "kernfs: fix up the unlocked attribute reads on the creation paths", is equally compelling. The phrase "unlocked attribute reads" is a major red flag for any kernel developer. It strongly suggests that some attributes were being accessed without proper locking mechanisms during the node creation process, leading to potential data corruption or unexpected behavior. The fact that this is a "fix" implies a pre-existing bug that could have significant implications.
Given the critical nature of these findings – potential concurrency issues and unlocked access to attributes in a core filesystem component – my immediate next step is to dive deep into the details. I need to see the exact code changes. Therefore, I am compelled to `git-show` both `81bdc85b4c08` and `ef179363b043` right now to fully grasp the problem and the solution implemented. My thought process is entirely focused on understanding the "why" and "how" of these changes and their potential impact.
|
| 175/2 |
2026/11/18 09:06 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 81bdc85b4c086502f4446a58eb5d25f29bb5e9f7
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 5 12:16:13 2026 -0700
kernfs: fix up the unlocked attribute reads on the creation paths
Two creation paths read a live node's attributes without holding
kernfs_iattr_rwsem, which kernfs_iop_setattr() takes for writing. They
need different fixes.
kernfs_create_link() copies the target's ia_uid and then its ia_gid into
the new link. A chown of the target between the two reads leaves the
link with the old uid and the new gid, an owner the target never had.
Read both under the rwsem.
kernfs_new_node() reads the parent's mode and ia_gid for S_ISGID
inheritance. Either value is fine there: the node does not exist yet,
so nothing orders a racing chmod or chown against the creation. Taking
the rwsem would only pick between two answers that are both right. Mark
the reads with READ_ONCE() instead.
The pointer that leads to them is already fine: __kernfs_iattrs()
publishes kernfs_node::iattr with try_cmpxchg(), and both sides read it
with READ_ONCE(), like the rest of fs/kernfs.
The Fixes tag is for the symlink half. kernfs_create_link() has read
the pair unlocked since it started copying the target's owner; only the
name of the lock its writer takes has changed. The READ_ONCE() markings
are not a fix.
Fixes: 488dee96bb62 ("kernfs: allow creating kernfs objects with arbitrary uid/gid")
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260905191613.3143937-5-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 214c97130a8a..07abf59f0264 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -736,13 +736,19 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
{
struct kernfs_node *kn;
- if (parent->mode & S_ISGID) {
+ /*
+ * The mode and the gid below are read unlocked on purpose: they feed
+ * a node that does not exist yet, so nothing orders a racing chmod or
+ * chown against this creation.
+ */
+ if (READ_ONCE(parent->mode) & S_ISGID) {
/* this code block imitates inode_init_owner() for
* kernfs
*/
+ struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
- if (parent->iattr)
- gid = parent->iattr->ia_gid;
+ if (attrs)
+ gid = READ_ONCE(attrs->ia_gid);
if (flags & KERNFS_DIR)
mode |= S_ISGID;
diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c
index 90e2b3221b83..3e53105d3abf 100644
--- a/fs/kernfs/symlink.c
+++ b/fs/kernfs/symlink.c
@@ -31,9 +31,20 @@ struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
kuid_t uid = GLOBAL_ROOT_UID;
kgid_t gid = GLOBAL_ROOT_GID;
- if (target->iattr) {
- uid = target->iattr->ia_uid;
- gid = target->iattr->ia_gid;
+ /*
+ * A symlink takes its owner from its target, so both fields have to
+ * come from the same moment: read them under kernfs_iattr_rwsem, or
+ * a chown of the target racing this could leave the link with the
+ * old uid and the new gid. The section ends before kernfs_add_one()
+ * takes kernfs_rwsem.
+ */
+ scoped_guard(rwsem_read, &kernfs_root(target)->kernfs_iattr_rwsem) {
+ struct kernfs_iattrs *attrs = READ_ONCE(target->iattr);
+
+ if (attrs) {
+ uid = attrs->ia_uid;
+ gid = attrs->ia_gid;
+ }
}
kn = kernfs_new_node(parent, name, S_IFLNK|0777, uid, gid, KERNFS_LINK);
]
|
| 176/2 |
2026/11/18 09:06 |
tool |
git-show |
0m
Args:
Results: map[Output:commit ef179363b04306dbc645c53207b9ad3516ebc75f
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Tue Oct 27 07:32:47 2026 -0700
kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()
kernfs_new_node() reads the parent's gid with READ_ONCE(attrs->ia_gid).
That gid is a kgid_t, which is a struct.
On SMP alpha, READ_ONCE() ends by casting the value it loaded back to
the type of its argument. Here that is a cast from a struct to the same
struct. GCC accepts that as an extension, but ISO C does not allow a
cast to or from a struct type, so sparse warns:
fs/kernfs/dir.c:751:31: sparse: sparse: cast to non-scalar
fs/kernfs/dir.c:751:31: sparse: sparse: cast from non-scalar
Read the gid_t inside the kgid_t instead, and wrap it back up with
KGIDT_INIT(). This is still a single marked load of the same four bytes.
There is no functional change. With GCC 12 for alpha (the config from
the report) and GCC 14 for x86-64 (with and without KCSAN),
fs/kernfs/dir.o has the same object code before and after this change.
Fixes: 81bdc85b4c08 ("kernfs: fix up the unlocked attribute reads on the creation paths")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202610261608.vItiXCGW-lkp@intel.com/
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://patch.msgid.link/20261027143247.3537239-1-shakeel.butt@linux.dev
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 324d61a00545..800af10d9511 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -750,7 +750,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
if (attrs)
- gid = READ_ONCE(attrs->ia_gid);
+ gid = KGIDT_INIT(READ_ONCE(attrs->ia_gid.val));
if (flags & KERNFS_DIR)
mode |= S_ISGID;
]
|
| 177/2 |
2026/11/18 09:06 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 80743
output: 164
thoughts: 195
Thoughts: **Initial Alert & Deep Dive into `kernfs` Patch Series**
My attention has been forcefully arrested by a realization: the patch I'm currently scrutinizing, `81bdc85b4c08` ("kernfs: fix up the unlocked attribute reads on the creation paths"), is not an isolated change, but rather the *fifth and final* patch in a series submitted by shakeel.butt@linux.dev on September 5, 2026. This immediately triggers a cascade of questions and a demand for historical context. I *need* to know what preceded this fix. My immediate next step is to meticulously examine the four preceding patches in this series. I'll be using `git log` to traverse backwards from the current commit, specifically targeting `81bdc85b4c08~1`, `81bdc85b4c08~2`, `81bdc85b4c08~3`, and `81bdc85b4c08~4`. Furthermore, I'll broaden my scope to review the `git-log` history specifically within the `fs/kernfs/` directory around that same date to ensure I haven't missed any related discussions or earlier, perhaps unnumbered, changes that might shed light on the evolution of this specific fix. The primary goal here is to understand the problem statement and the progression of solutions that led to this final patch.
|
| 178/2 |
2026/11/18 09:07 |
tool |
git-log |
0m
Args: map[Count:20 PathPrefix:fs/kernfs/]
Results: map[Output:ef179363b043 kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()
09452e5114bd kernfs: free the old name outside kernfs_rwsem
8b64f32fee95 kernfs: allocate the new name outside kernfs_rwsem
4424f06120fd kernfs: activate a new node without dropping kernfs_rwsem
2d2a2d7aa987 super: make iterate_supers_type() deletion-safe
e2f0d6d4569d fs: port ->setattr() to pass const mnt_idmap
c3150ded7bc0 fs: port ->getattr() to pass const mnt_idmap
566707035a6e fs: port ->mkdir() to pass const mnt_idmap
8ee61322bb53 fs: port ->rename() to pass const mnt_idmap
16d90b57d843 fs: port xattr to const mnt_idmap
41839e7bd92a fs: port ->permission() to pass const mnt_idmap
341c65579de5 kernfs: don't hold kernfs_rwsem across dir_emit()
45126ed80bb1 kernfs: resume readdir on the full entry key
5ebcb578adee kernfs: don't repeat or skip an entry when readdir resumes
23985e2b1f31 kernfs: don't hold kernfs_rwsem across notification delivery
5ad03af18434 kernfs: allocate the open node outside the open file mutex
81bdc85b4c08 kernfs: fix up the unlocked attribute reads on the creation paths
dc1a0cc95737 kernfs: don't lose IN_DELETE_SELF when decoding a file handle
6034974e3aec kernfs: take kernfs_rename_lock for same-parent renames too
7b15d6cf25e6 kernfs: preserve security xattrs without allocating iattrs
]
|
| 179/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:81bdc85b4c08~1 Stat:true]
Results: map[Output:commit dc1a0cc95737344b1a6aa42075e573e9625c6b9a
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 5 12:16:12 2026 -0700
kernfs: don't lose IN_DELETE_SELF when decoding a file handle
__kernfs_remove() clears i_nlink on a node's inodes and finds them with
ilookup(). A decode that has pinned the node but not yet hashed its
inode is invisible to that pass:
CPU0 CPU1
open_by_handle_at()
kernfs_find_and_get_node_by_id()
pins the node, still active
rmdir()
marks the subtree removing
ilookup() finds no inode
kernfs_get_inode()
hashes an inode with i_nlink 1
Nothing fixes it later: kernfs_refresh_inode() never touches i_nlink for
a file and skips it for a directory being removed. The inode keeps the
1 it was born with, and dentry_unlink_inode() sends IN_DELETE_SELF only
at 0, so a watcher never learns the node went away.
The other callers of kernfs_get_inode() are safe: those in fs/kernfs
hold kernfs_rwsem, and cgroup_may_write() is covered by cgroup_mutex,
which cgroup_destroy_locked() holds across kernfs_remove().
__kernfs_fh_to_dentry() has held nothing since exportfs support was
added.
Take kernfs_rwsem for reading, as ->get_parent already does, and cover
the lookup as well as kernfs_get_inode(). __kernfs_remove() deactivates
the whole subtree under the write lock, so under the read lock either
the lookup refuses the node, or the inode is hashed before the ilookup()
pass runs. The same holds for ->fh_to_parent, since a node cannot be
active while an ancestor is being removed.
Reproduced with a 300ms delay between the lookup and kernfs_get_inode(),
decoding a handle for a file in a cgroup directory while another task
rmdir()s it: st_nlink is 1 without this patch and 0 with it.
->get_parent still has a window of its own. It takes the same lock but
has no active check, so reconnect_path() can build an inode for an
ancestor that is already gone. That needs the active test rather than a
lock, and changes what ->get_parent returns, so it is left to the series
that reworks these paths.
Fixes: eea5d2bb34ba ("kernfs: Send IN_DELETE_SELF and IN_IGNORED")
Cc: stable@vger.kernel.org
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260905191613.3143937-4-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/kernfs/mount.c | 32 +++++++++++++++++++++-----------
1 file changed, 21 insertions(+), 11 deletions(-)
]
|
| 180/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:81bdc85b4c08~2 Stat:true]
Results: map[Output:commit 6034974e3aec7bd47e4ad4f5a01e7a289e8ec9cf
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 5 12:16:11 2026 -0700
kernfs: take kernfs_rename_lock for same-parent renames too
kernfs_rename_ns() takes kernfs_rename_lock only when the rename moves
the node to a new parent. A rename that keeps the parent, like renaming
a network interface, changes kernfs_node::name under kernfs_rwsem alone.
So the lock covers ->__parent but not ->name, and a reader that wants a
stable name has to take kernfs_rwsem, the lock every lookup needs.
It is also a real bug. kernfs_path_from_node() holds the lock for
reading and reads each ancestor's name once. One rename only moves the
answer from the old path to the new one, but two renames inside one walk
build a path that never existed:
CPU0 CPU1
kernfs_path_from_node() on /a/b/c
reads the name of a, gets "a"
renames a to a2
renames b to b2
reads the name of b, gets "b2"
returns "/a/b2/c"
Only sysfs can hit this: sysfs_warn_dup() is the one caller on a root
without KERNFS_ROOT_INVARIANT_PARENT. The rest are cgroup, which sets
the flag, so it skips the lock and reads names under RCU alone. That
case needs something else and is left alone here.
So take the lock for both kinds of rename, and let kernfs_rcu_name()
accept it, like kernfs_parent() already does for ->__parent. Renames
are rare, the lock is per filesystem, and the locked section is at most
three stores. It also gives a future rename counter one place to sit.
Fixes: 741c10b096bc ("kernfs: Use RCU to access kernfs_node::name.")
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260905191613.3143937-3-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/kernfs/dir.c | 28 +++++++++++++++-------------
fs/kernfs/kernfs-internal.h | 9 ++++++++-
2 files changed, 23 insertions(+), 14 deletions(-)
]
|
| 181/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:81bdc85b4c08~3 Stat:true]
Results: map[Output:commit 60da22a38b01eae6f343f3aad8e78924a4b97aeb
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 5 12:16:10 2026 -0700
selftests: cover kernfs file handles and same-parent rename
The kernfs tests only reach kernfs through lookup and readdir. Two
paths are not covered: file handles, which find a node without a lookup
through its parent, and rename of a node that keeps its parent.
Add three tests:
- decode a file handle, live and after the node is gone;
- decode while the node is being removed;
- look up a name while an interface is renamed, which renames its
/sys/class/net entry with the parent unchanged.
Two small fixes while here. ns_tag_isolates_class_net now counts only
symlinks, because bonding adds a bonding_masters attribute that is not a
device. A failing mkdtemp() now skips instead of aborting.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260905191613.3143937-2-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
tools/testing/selftests/filesystems/config | 1 +
tools/testing/selftests/filesystems/kernfs_test.c | 296 +++++++++++++++++++++-
2 files changed, 295 insertions(+), 2 deletions(-)
]
|
| 182/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:81bdc85b4c08~4 Stat:true]
Results: map[Output:commit c38b381d20273f1f3d0f30c019a6020c34815f83
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Tue Sep 1 18:40:50 2026 -0700
selftests: cover kernfs dentry revalidation
kernfs_test only checked two xattr calls. It did not touch the part of
kernfs that userspace hits most: the dentry cache.
The kernel adds and removes kernfs nodes on its own, with no VFS call
behind it, and removal cannot always unhash the dentries that are
already cached. So ->d_revalidate() is what keeps the cache honest,
let's add tests for it.
Add tests that make the kernel change kernfs directly, instead of going
through create and unlink:
- writing cgroup.subtree_control makes the kernel add and remove files
in every child cgroup. That checks two things: a cached negative
dentry is dropped once the file appears, and a cached positive one
is dropped once the file goes away.
- renaming a network interface renames the sysfs node under a dentry
that is already cached.
- sysfs mounted in a new network namespace must list that namespace's
interfaces and not the parent's, which covers the KERNFS_NS tagging
code.
- many lookups running against concurrent mkdir and rmdir. A lookup
may only succeed or fail with an errno that means "it is gone".
- open across removal, readdir returning each entry once, and a
telldir()/seekdir() round trip, which uses the name hash cookie in
kernfs_dir_pos().
Test the other direction too: walking dentries that are already cached
must not drop them. That is not just a slowdown. d_invalidate() calls
detach_mounts(), so a dentry dropped by mistake also unmounts whatever
is mounted under that directory.
Both filesystems are needed. cgroup2 has no ->rename and is not
namespace tagged. sysfs cannot be mounted with a set of children we
control. The config fragment stops a kernel built with kselftest-merge
from passing while it runs almost nothing.
To confirm the tests really catch a broken backstop, kernfs_dir_changed()
was stubbed to return false. 8 of the 11 tests then failed.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260902014050.499002-1-shakeel.butt@linux.dev
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
MAINTAINERS | 1 +
tools/testing/selftests/filesystems/config | 7 +
tools/testing/selftests/filesystems/kernfs_test.c | 695 +++++++++++++++++++++-
3 files changed, 702 insertions(+), 1 deletion(-)
]
|
| 183/2 |
2026/11/18 09:07 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 84445
output: 132
thoughts: 192
Thoughts: **Rigorous Verification of Kernfs Commits**
Before I proceed, a critical self-check is in order. I need to ensure absolute certainty that Shakeel Butt's recent October 2026 kernfs commits haven't inadvertently impacted key areas related to inode management. Specifically, I'm targeting `inode.c`, the `i_mode` field, and the `kernfs_iattr_rwsem`. While I've already established through `git-log` on `fs/kernfs/inode.c` that this specific file was unaffected, I want to go a step further. To achieve 100% confidence, I'm going to examine the `git-show --stat` output for a comprehensive list of these commits: `09452e5114bd`, `8b64f32fee95`, `4424f06120fd`, `341c65579de5`, `23985e2b1f31`, and `5ad03af18434`. This will provide a detailed view of any file modifications, even if `inode.c` itself wasn't directly listed in the commit logs. This meticulous approach is crucial to prevent any subtle regressions or unintended side effects in how inodes are handled.
|
| 184/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:09452e5114bd Stat:true]
Results: map[Output:commit 09452e5114bd59ea79e05b5ca4579df898178bfd
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 12 19:14:53 2026 -0700
kernfs: free the old name outside kernfs_rwsem
kernfs_rename_ns() frees the replaced name with kfree_rcu_mightsleep()
while still holding the kernfs_rwsem write lock. If the batching
allocation fails, which is what happens under memory pressure,
kvfree_call_rcu() falls back to a full synchronize_rcu() before freeing.
A rename can then wait out a grace period with the write lock held, and
every create, remove and rename in the hierarchy waits with it.
The name is already unpublished by then, so nothing needs the free to
happen under the lock. Move it past the unlock.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260913021453.21507-4-shakeel.butt@linux.dev
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/kernfs/dir.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
]
|
| 185/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:4424f06120fd Stat:true]
Results: map[Output:commit 4424f06120fda0f63f3d9e9992990d7790d902a4
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Sat Sep 12 19:14:51 2026 -0700
kernfs: activate a new node without dropping kernfs_rwsem
kernfs_add_one() links the node in, drops the kernfs_rwsem write lock,
then calls kernfs_activate(), which takes it again. Roots that do not
set KERNFS_ROOT_CREATE_DEACTIVATED, such as sysfs, therefore pay two
write locks for every file, directory and symlink created.
A new node has no children, so kernfs_activate() would walk only that
node. Call kernfs_activate_one() before dropping the lock instead. Its
two WARN_ON_ONCE()s still hold: the node was just linked, and nothing
can have changed its active count yet. This also closes the window
where a node is linked but not yet activated.
lock_stat, creating and destroying five dummy netdevs:
before after
kernfs_rwsem write acquires 1200 830
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260913021453.21507-2-shakeel.butt@linux.dev
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/kernfs/dir.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
]
|
| 186/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:23985e2b1f31 Stat:true]
Results: map[Output:commit 23985e2b1f31623468244a2149b2388cda4925eb
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Wed Sep 9 21:54:06 2026 -0700
kernfs: don't hold kernfs_rwsem across notification delivery
Recently at Meta we noticed that on multi-tenant systems, a workload
under extreme memory pressure negatively impacts unrelated workloads and
system daemons like below [1] and fb-oomd [2]. Mainly we observed that
system daemons were stuck on kernfs_rwsem in cgroup-related interfaces
for long periods of time.
On further inspection, we found that the workload under pressure had
registered inotify watches on its memory.events and was continuously
receiving limit-hitting notifications. The cgroup notification is done in
kernfs_notify_workfn(), which holds kernfs_supers_rwsem and kernfs_rwsem
and calls fsnotify(), which allocates the event with
kmalloc(GFP_KERNEL_ACCOUNT|__GFP_RETRY_MAYFAIL) charged to the watching
cgroup, which is already under memory pressure. So the notify worker
ended up in memory reclaim of a cgroup already under pressure, and itself
kept triggering the limit notifications.
This notification path takes the locks in read mode, but a waiting writer
blocks all future readers. That is exactly what is happening in the Meta
fleet, and it leaves daemons that are critical to the reliability of the
system stuck for long periods of time.
Commit 400188ae361a ("kernfs: Acquire kernfs_rwsem in
kernfs_notify_workfn().") added kernfs_rwsem just to safely read
kernfs_node::name. Let's sample the name once before the loop and drop
the lock. kernfs_supers_rwsem still covers the list, and the removal
paths take that one for reading too, so a stalled worker can still hold
a removal up once a mount or unmount is queued behind it.
Two things change for a watcher. A name longer than NAME_MAX now gives
an event with no name, where before it gave one with the full name;
fsnotify() takes the name as optional, so the event still arrives. And
the name and the parent are no longer sampled under one lock, so a rename
between the two would name a file against the directory it moved to.
That cannot happen today: kernfs_notify() rejects anything that is not a
file, and no caller of kernfs_rename_ns() renames one.
[1] https://github.com/facebookincubator/below
[2] https://github.com/facebookincubator/oomd
Fixes: 400188ae361a ("kernfs: Acquire kernfs_rwsem in kernfs_notify_workfn().")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260910045406.485295-1-shakeel.butt@linux.dev
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/kernfs/file.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
]
|
| 187/2 |
2026/11/18 09:07 |
tool |
git-show |
0m
Args: map[Commit:5ad03af18434 Stat:true]
Results: map[Output:commit 5ad03af184342c760a83a18dba32043f7cec24c6
Author: Shakeel Butt <shakeel.butt@linux.dev>
Date: Wed Sep 9 21:54:33 2026 -0700
kernfs: allocate the open node outside the open file mutex
kernfs_get_open_node() allocates the kernfs_open_node while holding one
of the hashed kernfs_open_file mutexes, so opening a file nobody has open
yet can enter reclaim with that mutex held. The mutex is shared by every
node that hashes to it, so unrelated opens, closes and xattr updates in
the same bucket wait.
It is the most contended lock in kernfs on our fleet: 2.19M waiters over
29 days, more than two hundred times the waiters on kernfs_rwsem, and the
highest median hold of any kernfs lock at 77ms. Most of it is monitoring
daemons opening cgroup control files, and the open node is freed once the
last descriptor closes, so an open-read-close loop allocates every time.
Look at kn->attr.open before taking the mutex, and allocate then if
nothing has the file open. Should the peek be wrong, which needs the
last descriptor to close inside the window, fall back to allocating under
the mutex as before.
Eight tasks opening and closing four cgroup files 20000 times each, with
lock_stat on the hashed mutex:
contentions hold total hold avg
before 198 4.19 s 8.67 us
after 119 2.31 s 4.78 us
and the same number of acquisitions either way.
This does not empty the bucket. kernfs_fop_release() and
kernfs_xattr_set() still sleep under the same mutex. It stops opens
doing it.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20260910045433.489413-1-shakeel.butt@linux.dev
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/kernfs/file.c | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
]
|
| 188/2 |
2026/11/18 09:07 |
llm |
cause-finder |
4m
Model:
Tokens:
input: 87144
output: 38
thoughts: 8908
Thoughts: **Root Cause Analysis: Data-Race in `dnotify_flush` and `kernfs_iop_getattr`**
My investigation into this data-race report (`BUG: KCSAN: data-race in dnotify_flush / kernfs_iop_getattr`) leads me to believe that the primary culprit lies in the historical evolution of how `kernfs` (and its predecessor, `sysfs`) handles inode attribute updates during `getattr` operations, specifically concerning the `inode->i_mode` field.
Here's my thought process:
The core of the problem, as indicated by the KCSAN report, is a race condition between `dnotify_flush` and `kernfs_iop_getattr`. Specifically, `dnotify_flush` is reading `inode->i_mode` without holding any locks, while `kernfs_iop_getattr` (which calls `kernfs_refresh_inode`) is writing to `inode->i_mode`. The critical invariant being violated is that `inode->i_mode` is being mutated concurrently by different code paths without adequate synchronization to protect these read/write operations.
My tracing efforts focused on identifying *when* this specific write pattern into `inode->i_mode` by `kernfs_iop_getattr` (or its predecessor) was introduced and *when* the concurrent read path (`dnotify_flush`) began to interact with it in an unsafe manner.
1. **Analyzing the write path (`kernfs_iop_getattr` / `kernfs_refresh_inode`)**:
* I examined the history of `kernfs_refresh_inode` and its prior existence as `sysfs_refresh_inode`.
* My analysis revealed that commit `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` titled "sysfs: Implement sysfs_getattr & sysfs_permission" was the key commit that introduced `sysfs_getattr` and, crucially, its call to `sysfs_refresh_inode`. This commit established the pattern of `sysfs_refresh_inode` unconditionally writing `inode->i_mode = sd->s_mode;` on every `getattr` (and `permission`) call.
* I noted that commits like `ffed24e22845` ("sysfs, kernfs: move inode code to fs/kernfs/inode.c"), `adc5e8b58f48`, and `c637b8acbe07` were pure refactorings (code movement and renaming). Following Instruction #3, I traced past these to the original introduction of the buggy logic in `sysfs`.
2. **Analyzing the read path (`dnotify_flush`)**:
* I investigated `dnotify_flush` and its usage of `inode->i_mode`. The specific check `if (!S_ISDIR(inode->i_mode))` within `dnotify_flush` has been present since commit `1da177e4c3f4` (Linux 2.6.12-rc2) in 2005. This means that the lockless read of `inode->i_mode` by `dnotify_flush` has existed for a very long time, predating the introduction of the unsafe write by `sysfs_refresh_inode`.
3. **Identifying the introduction of the race**:
* The crucial realization is that the race condition, as reported, only *began* to exist *after* `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` was merged. Before this commit, `sysfs` did not implement `.getattr`, so `vfs_getattr` would call `generic_fillattr` without ever mutating `inode->i_mode` on a live inode. After this commit, `sysfs_getattr` and `sysfs_refresh_inode` *did* start writing to `inode->i_mode` unconditionally.
* The fact that `dnotify_flush` does not acquire any of the locks held by `kernfs_iop_getattr` (such as `kernfs_iattr_rwsem` or, historically, `inode->i_lock`) means that the lock introduced in `47b5c64d0ab5` ("sysfs: Use inode_lock for attribute updates") and later modified/removed in commits like `aa1d058d48f2` ("kernfs: dont take i_lock on inode attr read") did *not* prevent this specific race. The read side (`dnotify_flush`) never participated in those locking schemes.
4. **Evaluating commit `aa1d058d48f292aa138e33ad12b7b4d18b5407cd`**:
* This commit removed `spin_lock(&inode->i_lock)` from `kernfs_iop_getattr`. My analysis shows that `dnotify_flush` never held `inode->i_lock`. Therefore, this commit did not *introduce* the race between `dnotify_flush` and `kernfs_iop_getattr`. While it may have reduced serialization for concurrent `kernfs_iop_getattr` calls amongst themselves, it did not create or exacerbate the specific race reported here. The core issue of writing `inode->i_mode` without synchronizing against `dnotify_flush`'s read was already present since `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`.
* I asked myself what an upstream kernel developer would cite in a `Fixes:` tag. Given that `dnotify_flush` never acquired `inode->i_lock`, citing `aa1d058d48f2` for this specific race would be incorrect. The commit that established the fundamental unsafe write pattern that `dnotify_flush` races with is `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`.
Based on this rigorous tracing and consideration of the output rules, I am confident that commit `e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0` is the single commit that introduced the buggy logic leading to this specific data-race report.
**CandidateCommits**:
`["e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0"]`
|
| 189/2 |
2026/11/18 09:12 |
tool |
git-log |
0m
Args: map[Count:25 PathPrefix:fs/notify/dnotify/dnotify.c]
Results: map[Output:fdfe0133473a fix a leak in fcntl_dirnotify()
1751f872cc97 treewide: const qualify ctl_tables where applicable
8fd3395ec905 get rid of ...lookup...fdget_rcu() family
cad3f4a22cfa inotify: Fix possible deadlock in fsnotify_destroy_mark
1934b212615d file: reclaim 24 bytes from f_owner
230d97d39ee2 fsnotify: create a wrapper fsnotify_find_inode_mark()
9d5b94753566 fs: Remove the now superfluous sentinel elements from ctl_table array
0ede61d8589c file: convert to SLAB_TYPESAFE_BY_RCU
68279f9c9f59 treewide: mark stuff as __ro_after_init
f4ae4081e5a8 dnotify: Pass argument of fcntl_dirnotify as int
aabb45fdcb31 dnotify: use fsnotify group lock helpers
867a448d587e fsnotify: pass flags argument to fsnotify_alloc_group()
49a4de75719b dnotify: move dnotify sysctl to dnotify.c
e54183fa7047 fsnotify: generate FS_RENAME event with rich information
460b4f812a9d file: Rename fcheck lookup_fd_rcu
950cc0d2bef0 fsnotify: generalize handle_inode_event()
b9a1b9772509 fsnotify: create method handle_inode_event() in fsnotify_operations
62cb0af4cea8 dnotify: report both events on parent and child with single callback
b54cecf5e229 fsnotify: pass dir argument to handle_event() callback
ac5656d8a4cd fanotify, inotify, dnotify, security: add security hook for fs notifications
3e0a4e858033 treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 118
e43e9c339a78 fsnotify: switch send_to_group() and ->handle_event to const struct qstr *
d46eb14b735b fs: fsnotify: account fsnotify metadata to kmemcg
019191342fec signal: Use PIDTYPE_TGID to clearly store where file signals will be sent
b249f5be6165 fsnotify: add fsnotify_add_inode_mark() wrappers
]
|
| 190/2 |
2026/11/18 09:12 |
llm |
cause-finder |
0m
Model:
Tokens:
input: 96735
output: 54
thoughts: 1603
Thoughts: **My Kernel's Race to Understand: Unraveling the `inode->i_mode` Conundrum**
I've been digging into a perplexing KCSAN report about a 2-byte data race on `inode->i_mode`. My goal is to meticulously trace this issue back to its origin, ensuring I haven't missed any crucial details in the commit history.
At the heart of the problem, I see a race between two distinct operations. On the read side, during a `close` operation, specifically within `dnotify_flush()`, I'm performing a lockless read of `inode->i_mode` using `S_ISDIR(inode->i_mode)`. This code, I've confirmed, has been sitting there, untouched since a very old revision (`1da177e4c3f4`, pre-git Linux-2.6.12-rc2). It's a straightforward 2-byte load with no VFS, inode, or filesystem locks held at that point.
The write side is where things get more interesting. I've identified that `udevd`, specifically during a `stat`/`fstat` call (`__x64_sys_newfstat` leading to `vfs_getattr` and ultimately `kernfs_iop_getattr`), is unconditionally writing to `inode->i_mode`. This happens within `kernfs_refresh_inode()`, where `inode->i_mode` is set to `kn->mode`. Critically, this write operation occurs even when `inode->i_mode` already matches `kn->mode`, and it's done without any `WRITE_ONCE()` or `READ_ONCE()` annotations. The only lock held during this write is `down_read(&root->kernfs_iattr_rwsem)`. This pattern of unconditional plain C stores, concurrent with lockless reads, is the textbook definition of a C11/LKMM data race.
So, the VFS expects the file type bits (`S_IFMT`) of an active inode to be invariant after initialization, and functions like `dnotify_flush()` rely on this by reading `S_ISDIR(inode->i_mode)` without locks during `close()`. Meanwhile, the `kernfs` subsystem, through `kernfs_refresh_inode()`, is actively overwriting `inode->i_mode` on every `stat`/`fstat` and path lookup (`kernfs_iop_permission`), creating the race condition.
Now, for the history hunt. I've painstakingly gone through the relevant commit logs:
* The `dnotify_flush()` read path (`fs/notify/dnotify/dnotify.c:161`) is ancient history, dating back to `1da177e4c3f4`.
* The call site within `filp_flush()` (`fs/open.c:1549`) is similarly old, with some refinements over time but the core logic remaining.
* The `kernfs_refresh_inode()` write path is more recent and offers clues.
* `9caf69614225` switched the locking for `kernfs_iop_getattr()` to `root->kernfs_iattr_rwsem`.
* `aa1d058d48f2` removed the `spin_lock(&inode->i_lock)` from `kernfs_iop_getattr()` and `kernfs_iop_permission()`. I've ruled this out as the *introduction* of the race because `dnotify_flush()` never held `inode->i_lock` anyway, meaning the race between `dnotify_flush()` and `kernfs_iop_getattr()` likely predates this. Also, prior to `47b5c64d0ab5` and `7ba0273b2f34`, `kernfs_rwsem`/`kernfs_mutex`/`sysfs_mutex` were held, but `dnotify_flush()` never acquired those either.
* `c637b8acbe07`, `adc5e8b58f48`, and `ffed24e22845` were pure renames or code relocations, not introducing the race.
This leaves me with one critical commit: **`e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`** titled "sysfs: Implement sysfs_getattr & sysfs_permission". This commit introduced `sysfs_refresh_inode()` which contained the exact unconditional plain store `inode->i_mode = sd->s_mode;`. Prior to this, `sysfs` didn't implement `.getattr` or `.permission`, meaning `fstat()` would have just read the VFS inode via `generic_fillattr()` without ever touching `inode->i_mode`. This commit, by implementing these operations and wiring them up to refresh live VFS inodes under `sysfs_mutex`, directly created the scenario where the lockless `dnotify_flush()` read could be raced by the unconditional write from `sysfs_getattr()` (and `sysfs_permission()`).
Therefore, I'm confident that the root cause of this data race lies with **`e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0`**. I'm ready to flag this commit.
|
| 191/2 |
2026/11/18 09:13 |
tool |
set-results |
0m
Args: map[CandidateCommits:[e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0]]
Results: map[CandidateCommits:[e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0]]
|
| 192/2 |
2026/11/18 09:13 |
llm |
cause-finder |
1m
Model:
Tokens:
input: 98442
output: 1927
thoughts: 47
Thoughts: **My Thought Process for Documenting the Bisection**
Alright, I need to craft a really comprehensive and clear bisection log here. My goal is to thoroughly explain the root cause of the issue we found, detailing every step of our investigation. This means I need to lay out the complete git history we traversed, meticulously document each commit that was a candidate for the culprit, and critically, explain why we ruled out the ones that *weren't* the cause. Ultimately, this log needs to lead the reader, who is an expert in their own right, directly to our final conclusion with no ambiguity.
|
| 193/1 |
2026/11/18 09:15 |
action |
format-bisect |
0m
Results: map[Commits:[map[AuthorEmail:ebiederm@xmission.com AuthorName:Eric W. Biederman Date:2009-11-20T16:08:53-08:00 Hash:e61ab4ae48fbf477f5b9fcbec9e1b8dc789920d0 Recipients:[map[Email:ebiederm@aristanetworks.com Name: To:true] map[Email:ebiederm@xmission.com Name: To:true] map[Email:gregkh@suse.de Name: To:true] map[Email:serue@us.ibm.com Name: To:true] map[Email:tj@kernel.org Name: To:true]] Title:sysfs: Implement sysfs_getattr & sysfs_permission]]]
|