| 0/0 |
2026/09/20 00:00 |
flow |
assessment-security |
1h06m
Results: map[DenialOfService:true Explanation:I have analyzed the bug report and the relevant source code.
### Bug Mechanism
The crash is an assertion failure in `wait_subpage_spinlock()`:
`ASSERT(folio_test_private(folio) && folio_get_private(folio));`
This is triggered during `btrfs_invalidate_folio()`, which is called by
`folio_invalidate()` from `extent_writepage()` when a dirty folio is being
written back but its index is beyond the end of the file (`i_size`).
The sequence of events is:
1. A folio is dirtied via a memory mapping or direct I/O (GUP), e.g., using
`aio_read` into a memory-mapped region of a Btrfs file.
2. The file is truncated or the folio is otherwise beyond `i_size`.
3. Writeback is triggered for the folio (e.g., via
`btrfs_start_ordered_extent_nowriteback` calling `filemap_fdatawrite_range`).
4. `extent_writepage()` checks if the folio is beyond `i_size`. If so, it calls
`folio_invalidate(folio, 0, folio_size(folio))`.
5. `folio_invalidate()` calls the filesystem's `invalidate_folio` callback,
which is `btrfs_invalidate_folio()`.
6. `btrfs_invalidate_folio()` immediately calls `wait_subpage_spinlock(folio)`.
7. `wait_subpage_spinlock()` checks if the folio is a subpage folio
(`btrfs_is_subpage()`). If the system is using large folios (where
`folio_size(folio) > fs_info->sectorsize`), this returns true.
8. It then asserts that the folio has private data attached
(`folio_test_private(folio)` and `folio_get_private(folio)`).
9. However, if the folio was dirtied via GUP or memory mapping without going
through the normal Btrfs write paths that attach the private state (or if the
private state was already detached), `folio_test_private(folio)` is false.
10. The assertion fails, causing a `BUG()` and a kernel panic.
### Production Impact (Without ASSERT)
In a production kernel where `CONFIG_BTRFS_ASSERT` is not enabled, the
`ASSERT()` macro compiles to nothing.
The code in `wait_subpage_spinlock()` will then execute:
```c
bfs = folio_get_private(folio);
spin_lock_irq(&bfs->lock);
spin_unlock_irq(&bfs->lock);
```
Since `folio_get_private(folio)` returns `NULL`, `bfs` will be `NULL`.
The `lock` field is at offset 0 in `struct btrfs_folio_state`.
Therefore, `spin_lock_irq(&bfs->lock)` will attempt to dereference a NULL
pointer (address `0x0`), resulting in a kernel NULL pointer dereference panic.
This means the bug leads to a Denial of Service (system crash) even in
production builds. It does not lead to memory corruption, use-after-free, or
information leaks, so it is not exploitable for privilege escalation.
### Reachability
The bug can be triggered by standard unprivileged user operations. The syzkaller
reproducer uses `aio_read` with Direct I/O and memory-mapped files, which are
available to any unprivileged user. It does not require any special
capabilities, user namespaces, or mounting malicious filesystems. It just
requires access to a Btrfs filesystem (which can be a standard mount point
available to users).
### Conclusion
- **Exploitable:** False. It's a NULL pointer dereference in production builds,
leading to a crash, not memory corruption.
- **Denial of Service:** True. It causes a kernel panic.
- **Accessible From Unprivileged Processes:** True. Triggerable via standard I/O
and memory mapping syscalls.
- **Accessible From User Namespaces:** False (not required).
- **Other Triggers:** False. Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:false VMGuestTrigger:false VMHostTrigger:false]
|
| 1/1 |
2026/09/20 00:00 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 2/1 |
2026/09/20 00:00 |
action |
kernel-checkouter |
1m
Results: map[KernelSrc:/app/workdir/cache/src/06c16b14950450dc74a313342485ec815107216f]
|
| 3/1 |
2026/09/20 00:02 |
action |
kernel-builder |
31m
Results: map[KernelObj:/app/workdir/cache/build/599651b90276aa4d37e298521de681844ce0bf00]
|
| 4/1 |
2026/09/20 00:33 |
action |
codesearch-prepare |
23m
Results: map[Index:codesearch-index]
|
| 5/1 |
2026/09/20 00:56 |
agent |
expert |
10m
Model:
Results: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:false VMGuestTrigger:false VMHostTrigger:false]
Instruction: You are an experienced Linux kernel security engineer. Your task is to analyze given kernel bug report
and determine its security impact based on the following dimensions.
Use the provided tools to examine the source code, check for capability checks (e.g., capable(), ns_capable()),
and understand the nature of the bug. Analyze the given kernel build and configuration.
You can check the kernel config by grepping ".config" file; you can check kernel cmdline by grepping
".config" file for "CONFIG_CMDLINE=". Assume sysctl parameters have default values.
But analyze for the corresponding production build w/o debugging tools enabled (like KASAN, KMSAN, UBSAN).
Try different strategies when analyzing the bug:
- think of ways in which the vulnerable code is unreachable
- or the other way around: try to come up with different ideas of how an unprivileged user can reach the bug
If still unsure err on the side of the bug being non-exploitable/not-accessible.
In the final reply, provide a reasoning for your assessment.
Analysis dimensions:
* Exploitable:
Determine if the bug can result in memory corruption, elevated privileges, or an information leak.
Memory safety issues are almost always exploitable (KASAN or UBSAN reports for use-after-free, out-of-bounds;
refcounting issues, corrupted lists, etc). When kernel is crashing on a completely wild pointer access
(e.g. user-space address, or non-canonical address, but not on NULL or address corresponding to KASAN shadow
for NULL address), including both data accesses and control transfers, that also usually implies possibility
of exploitation. Such reports usually say "unable to handle kernel paging request".
Uses of uninitialized values detected by KMSAN may be exploitable b/c attacker frequently can affect uninit
values with spraying techniques. However, for these exploitability depends on how exactly the uninit value
is used in the code, and what it affects.
Information leaks are exploitable on their own and should be classified as such. A bug that copies kernel
memory contents to userspace (e.g. an out-of-bounds read whose result is returned to the caller, or
uninitialized stack/heap bytes written to a user buffer) is exploitable: it can reveal kernel pointer
values and defeat KASLR, expose sensitive data such as cryptographic keys or other processes' memory, and
serves as a necessary building block in most modern kernel privilege-escalation exploit chains. Do not classify
an information leak as non-exploitable solely because it does not directly cause a memory write or control-flow
hijack; the leak itself is the exploit primitive.
Think of what happens after the bug is triggered. Some bugs cause kernel panic and halt execution,
they are harder to exploit. For example, BUG reports halts the kernel. However, WARNING reports don't halt
execution in production builds. Debug bug detection tools (like KASAN, KMSAN, KCSAN, UBSAN) are also not enabled
in production builds, so attacker can freely exploit these bugs w/o being detected by these tools.
If you see an integer overflow, think how the overflowed value used later (if it's used as allocation size,
or an array index). If you see an out-of-bounds read, think if it's followed by an out-of-bounds write as well.
Some KCSAN data-races may be exploitable by skilled attackers as well. Think what data structures got corrupted
as the result of data races and how. However, note that kernel has lots of "benign" data races that don't lead
to any runtime misbehavior at all.
* Denial Of Service:
Determine if the bug can result in denial-of-service. Most bugs can, since they cause system crash,
hangs, deadlocks, or resource leaks. This is mostly applicable to WARNING bugs that won't cause system crash
in production. For these think what will be consequences of the violation of the kernel assumptions flagged
by the WARNING. In some cases the unexpected condition is also properly handled by the normal control flow
(e.g. with "if (WARN_ON(...))"), these won't cause denial-of-service. If the condition is not handled,
then it may or may not cause denial-of-service.
* Accessible From Unprivileged Processes:
Determine if the bug can be reached from a typical (non-root) user process that does NOT have any special capabilities
(like CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON) or access to device nodes restricted to root.
Assume that unprivileged_bpf_disabled=1, that is eBPF loading is not accessible. However, cBPF (classical BPF)
is still accessible to non-root processes.
Assume that user namespaces are not accessible, that is, the process cannot get the mentioned capabilities even
within a new user namespace (checked by ns_capable() function in the kernel sources).
* Accessible From User Namespaces:
Determine if the bug can be reached within a user-namespace where the process has all capabilities
(including CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON). Such capabilities are checked with ns_capable()
function in the kernel sources.
* VM Guest Trigger:
Determine if the bug can be triggered from the context of a typical KVM guest (e.g., set up by a QEMU VMM).
Consider accesses to standard Linux host paravirtualized features (virtio-blk, virtio-net, etc.),
and handling of VM exits in the KVM code.
* VM Host Trigger in The Confidential Computing Context:
Determine if the bug can be triggered in a confidential computing guest kernel from the context of a KVM host.
Consider access to standard Linux guest paravirtualized features (virtio-blk, virtio-net, etc.).
* Ethernet Network Trigger:
Determine if the bug can be triggered by processing ingress network Ethernet traffic, either directly (network stack)
or via drivers exposed to network data.
* Other Remote Trigger:
Determine if the bug can be triggered by processing remote traffic other than Ethernet (Wifi, Bluetooth, NFC, etc).
* Peripheral Trigger:
Determine if the bug can be triggered via an untrusted peripheral device that can be physically plugged
into a system, such as a USB device or a niche hardware driver handling external hardware inputs.
This is particularly important for mobile and desktop environments where users can plug in unknown devices.
* Malicious Filesystem Trigger:
Determine if the bug can be triggered by the kernel mounting and parsing a malicious filesystem image.
This is highly critical for Desktop and Mobile environments where external media or downloaded images
might be auto-mounted.
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 kernel bug report is:
BTRFS info (device loop0): space_info DATA+METADATA (sub-group id 0) has 24576 free, is not full
BTRFS info (device loop0): space_info total=3276800, used=49152, pinned=16384, reserved=0, may_use=3186688, readonly=0 zone_unusable=0
BTRFS info (device loop0): global_block_rsv: size 1048576 reserved 1048576
BTRFS info (device loop0): trans_block_rsv: size 0 reserved 0
BTRFS info (device loop0): chunk_block_rsv: size 0 reserved 0
BTRFS info (device loop0): remap_block_rsv: size 0 reserved 0
BTRFS info (device loop0): delayed_block_rsv: size 0 reserved 0
BTRFS info (device loop0): delayed_refs_rsv: size 32768 reserved 32768
assertion failed: folio_test_private(folio) && folio_get_private(folio), in fs/btrfs/inode.c:7616
------------[ cut here ]------------
kernel BUG at fs/btrfs/inode.c:7616!
Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
CPU: 0 UID: 0 PID: 5325 Comm: syz.0.0 Not tainted syzkaller #0 PREEMPT(full)
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
RIP: 0010:wait_subpage_spinlock+0x380/0x390 fs/btrfs/inode.c:7616
Code: 17 aa fd eb 05 e8 40 17 aa fd 48 c7 c7 00 d2 59 8c 48 c7 c6 c0 06 5a 8c 48 c7 c2 e0 d2 59 8c b9 c0 1d 00 00 e8 a1 f3 09 fd 90 <0f> 0b 66 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 90 90 90 90 90 90
RSP: 0018:ffffc9000f15e048 EFLAGS: 00010246
RAX: 0000000000000061 RBX: ffffea0001508f00 RCX: 5c8637085c6b4a00
RDX: 0000000000000000 RSI: 8000000000000000 RDI: 0000000000000000
RBP: 1ffffd40002a11e0 R08: ffffc9000f15ddc7 R09: 1ffff92001e2bbb8
R10: dffffc0000000000 R11: fffff52001e2bbb9 R12: 0000000000000002
R13: 0000000000004000 R14: 0000000000000000 R15: dffffc0000000000
FS: 00007fd4260436c0(0000) GS:ffff88808c2c3000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fd426062f68 CR3: 000000004387c000 CR4: 0000000000352ef0
Call Trace:
<TASK>
btrfs_invalidate_folio+0x26e/0xc50 fs/btrfs/inode.c:7698
extent_writepage fs/btrfs/extent_io.c:2058 [inline]
extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
btrfs_writepages+0x129f/0x28c0 fs/btrfs/extent_io.c:2975
do_writepages+0x338/0x560 mm/page-writeback.c:2560
filemap_writeback mm/filemap.c:387 [inline]
filemap_fdatawrite_range+0x1ef/0x2f0 mm/filemap.c:412
btrfs_start_ordered_extent_nowriteback+0x305/0x6e0 fs/btrfs/ordered-data.c:-1
btrfs_start_ordered_extent fs/btrfs/ordered-data.h:203 [inline]
btrfs_page_mkwrite+0x856/0x1a40 fs/btrfs/file.c:1909
do_page_mkwrite+0x147/0x310 mm/memory.c:3816
wp_page_shared mm/memory.c:4217 [inline]
do_wp_page+0x1a9c/0x4b80 mm/memory.c:4440
handle_pte_fault mm/memory.c:6582 [inline]
__handle_mm_fault mm/memory.c:6707 [inline]
handle_mm_fault+0x15d7/0x3750 mm/memory.c:6876
faultin_page mm/gup.c:1126 [inline]
__get_user_pages+0x1675/0x2750 mm/gup.c:1428
__get_user_pages_locked mm/gup.c:1752 [inline]
__gup_longterm_locked+0x1032/0x15c0 mm/gup.c:2471
gup_fast_fallback+0x1d92/0x20e0 mm/gup.c:3209
iov_iter_extract_user_pages lib/iov_iter.c:1776 [inline]
iov_iter_extract_pages+0x3a1/0x690 lib/iov_iter.c:1845
iov_iter_extract_bvecs+0x272/0xb40 lib/iov_iter.c:1957
bio_iov_iter_get_pages+0x5ae/0xee0 block/bio.c:1309
blkdev_iov_iter_get_pages block/fops.c:49 [inline]
__blkdev_direct_IO+0x661/0xf60 block/fops.c:219
blkdev_direct_IO+0x121d/0x17b0 block/fops.c:435
blkdev_read_iter+0x223/0x420 block/fops.c:816
aio_read+0x33a/0x4d0 fs/aio.c:1656
__io_submit_one fs/aio.c:-1 [inline]
io_submit_one+0x774/0x13d0 fs/aio.c:2106
__do_sys_io_submit fs/aio.c:2165 [inline]
__se_sys_io_submit+0x195/0x340 fs/aio.c:2135
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x166/0x520 arch/x86/entry/syscall_64.c:84
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fd42519e159
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fd426042fe8 EFLAGS: 00000246 ORIG_RAX: 00000000000000d1
RAX: ffffffffffffffda RBX: 00007fd425426090 RCX: 00007fd42519e159
RDX: 00002000000000c0 RSI: 00000000000000ca RDI: 00007fd426022000
RBP: 00007fd42523506b R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fd425426128 R14: 00007fd425426090 R15: 00007ffec6e38c28
</TASK>
Modules linked in:
---[ end trace 0000000000000000 ]---
RIP: 0010:wait_subpage_spinlock+0x380/0x390 fs/btrfs/inode.c:7616
Code: 17 aa fd eb 05 e8 40 17 aa fd 48 c7 c7 00 d2 59 8c 48 c7 c6 c0 06 5a 8c 48 c7 c2 e0 d2 59 8c b9 c0 1d 00 00 e8 a1 f3 09 fd 90 <0f> 0b 66 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 90 90 90 90 90 90
RSP: 0018:ffffc9000f15e048 EFLAGS: 00010246
RAX: 0000000000000061 RBX: ffffea0001508f00 RCX: 5c8637085c6b4a00
RDX: 0000000000000000 RSI: 8000000000000000 RDI: 0000000000000000
RBP: 1ffffd40002a11e0 R08: ffffc9000f15ddc7 R09: 1ffff92001e2bbb8
R10: dffffc0000000000 R11: fffff52001e2bbb9 R12: 0000000000000002
R13: 0000000000004000 R14: 0000000000000000 R15: dffffc0000000000
FS: 00007fd4260436c0(0000) GS:ffff88808c2c3000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f2823e512c0 CR3: 000000004387c000 CR4: 0000000000352ef0
Reply: I have analyzed the bug report and the relevant source code.
### Bug Mechanism
The crash is an assertion failure in `wait_subpage_spinlock()`:
`ASSERT(folio_test_private(folio) && folio_get_private(folio));`
This is triggered during `btrfs_invalidate_folio()`, which is called by `folio_invalidate()` from `extent_writepage()` when a dirty folio is being written back but its index is beyond the end of the file (`i_size`).
The sequence of events is:
1. A folio is dirtied via a memory mapping or direct I/O (GUP), e.g., using `aio_read` into a memory-mapped region of a Btrfs file.
2. The file is truncated or the folio is otherwise beyond `i_size`.
3. Writeback is triggered for the folio (e.g., via `btrfs_start_ordered_extent_nowriteback` calling `filemap_fdatawrite_range`).
4. `extent_writepage()` checks if the folio is beyond `i_size`. If so, it calls `folio_invalidate(folio, 0, folio_size(folio))`.
5. `folio_invalidate()` calls the filesystem's `invalidate_folio` callback, which is `btrfs_invalidate_folio()`.
6. `btrfs_invalidate_folio()` immediately calls `wait_subpage_spinlock(folio)`.
7. `wait_subpage_spinlock()` checks if the folio is a subpage folio (`btrfs_is_subpage()`). If the system is using large folios (where `folio_size(folio) > fs_info->sectorsize`), this returns true.
8. It then asserts that the folio has private data attached (`folio_test_private(folio)` and `folio_get_private(folio)`).
9. However, if the folio was dirtied via GUP or memory mapping without going through the normal Btrfs write paths that attach the private state (or if the private state was already detached), `folio_test_private(folio)` is false.
10. The assertion fails, causing a `BUG()` and a kernel panic.
### Production Impact (Without ASSERT)
In a production kernel where `CONFIG_BTRFS_ASSERT` is not enabled, the `ASSERT()` macro compiles to nothing.
The code in `wait_subpage_spinlock()` will then execute:
```c
bfs = folio_get_private(folio);
spin_lock_irq(&bfs->lock);
spin_unlock_irq(&bfs->lock);
```
Since `folio_get_private(folio)` returns `NULL`, `bfs` will be `NULL`.
The `lock` field is at offset 0 in `struct btrfs_folio_state`.
Therefore, `spin_lock_irq(&bfs->lock)` will attempt to dereference a NULL pointer (address `0x0`), resulting in a kernel NULL pointer dereference panic.
This means the bug leads to a Denial of Service (system crash) even in production builds. It does not lead to memory corruption, use-after-free, or information leaks, so it is not exploitable for privilege escalation.
### Reachability
The bug can be triggered by standard unprivileged user operations. The syzkaller reproducer uses `aio_read` with Direct I/O and memory-mapped files, which are available to any unprivileged user. It does not require any special capabilities, user namespaces, or mounting malicious filesystems. It just requires access to a Btrfs filesystem (which can be a standard mount point available to users).
### Conclusion
- **Exploitable:** False. It's a NULL pointer dereference in production builds, leading to a crash, not memory corruption.
- **Denial of Service:** True. It causes a kernel panic.
- **Accessible From Unprivileged Processes:** True. Triggerable via standard I/O and memory mapping syscalls.
- **Accessible From User Namespaces:** False (not required).
- **Other Triggers:** False.
|
| 6/2 |
2026/09/20 00:56 |
llm |
expert |
0m
Model:
Tokens:
input: 9500
output: 38
thoughts: 73
Thoughts: Here's my summary, as though I'm thinking through this:
**Btrfs Assertion Failure Deep Dive**
Okay, so I've got a Btrfs kernel bug, and it's pointing me directly to `fs/btrfs/inode.c:7616`. The error message is screaming about a failed assertion: `folio_test_private(folio) && folio_get_private(folio)`. Right away, this tells me we're dealing with something related to folio management within the Btrfs inode code, specifically around the private data associated with a folio. This implies there's likely a problem with how the kernel is handling references or states related to a folio's private data. I need to understand why `folio_test_private` and `folio_get_private` aren't behaving as expected at this point. Time to dive into the code around that specific line and understand the logic that leads here. Let's see what the function does around that line of code.
|
| 7/2 |
2026/09/20 00:57 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/inode.c FirstLine:7600 LineCount:50]
Results: map[Contents:7600:
7601: /*
7602: * For release_folio() and invalidate_folio() we have a race window where
7603: * folio_end_writeback() is called but the subpage spinlock is not yet released.
7604: * If we continue to release/invalidate the page, we could cause use-after-free
7605: * for subpage spinlock. So this function is to spin and wait for subpage
7606: * spinlock.
7607: */
7608: static void wait_subpage_spinlock(struct folio *folio)
7609: {
7610: struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
7611: struct btrfs_folio_state *bfs;
7612:
7613: if (!btrfs_is_subpage(fs_info, folio))
7614: return;
7615:
7616: ASSERT(folio_test_private(folio) && folio_get_private(folio));
7617: bfs = folio_get_private(folio);
7618:
7619: /*
7620: * This may look insane as we just acquire the spinlock and release it,
7621: * without doing anything. But we just want to make sure no one is
7622: * still holding the subpage spinlock.
7623: * And since the page is not dirty nor writeback, and we have page
7624: * locked, the only possible way to hold a spinlock is from the endio
7625: * function to clear page writeback.
7626: *
7627: * Here we just acquire the spinlock so that all existing callers
7628: * should exit and we're safe to release/invalidate the page.
7629: */
7630: spin_lock_irq(&bfs->lock);
7631: spin_unlock_irq(&bfs->lock);
7632: }
7633:
7634: static int btrfs_launder_folio(struct folio *folio)
7635: {
7636: return btrfs_qgroup_free_data(folio_to_inode(folio), NULL, folio_pos(folio),
7637: folio_size(folio), NULL);
7638: }
7639:
7640: static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7641: {
7642: if (try_release_extent_mapping(folio, gfp_flags)) {
7643: wait_subpage_spinlock(folio);
7644: clear_folio_extent_mapped(folio);
7645: return true;
7646: }
7647: return false;
7648: }
7649:
]
|
| 8/2 |
2026/09/20 00:57 |
llm |
expert |
0m
Model:
Tokens:
input: 10631
output: 38
thoughts: 962
Thoughts: **Analyzing a Btrfs ASSERT in `wait_subpage_spinlock`**
Okay, so I've got an ASSERT in `wait_subpage_spinlock`. The assertion itself is: `ASSERT(folio_test_private(folio) && folio_get_private(folio));`. This tells me we're dealing with a folio that, based on the `folio_test_private` and `folio_get_private` calls, has a private data region, which is expected during this part of the write process, but appears inconsistent. Time to trace the call stack to understand the context.
Looking at the trace, the core of this seems to stem from a write operation. It starts in `btrfs_invalidate_folio`, which is interesting because that's usually related to invalidating cached data, and somehow ends up being called from within the `extent_writepage` path. So, `btrfs_invalidate_folio` is what caused the assertion here. That's unusual because these aren't directly related in most contexts, let me double check the calls to verify the context of these calls.
The full write path is: `btrfs_invalidate_folio` -> `extent_writepage` -> `extent_write_cache_pages` -> `btrfs_writepages` -> `do_writepages` -> `filemap_writeback` -> `filemap_fdatawrite_range` -> `btrfs_start_ordered_extent_nowriteback` -> `btrfs_page_mkwrite` -> `do_page_mkwrite` -> `do_wp_page` -> `handle_mm_fault` -> `__get_user_pages` -> `iov_iter_extract_pages` -> `bio_iov_iter_get_pages` -> `blkdev_direct_IO` -> `blkdev_read_iter` -> `aio_read`.
Now, I need to understand what's happening with the folio and how `btrfs_invalidate_folio` is being called from within the `extent_writepage` function. The key is to examine the code at `fs/btrfs/inode.c:7698` (where `btrfs_invalidate_folio` is called) and `fs/btrfs/extent_io.c:2058` (where `extent_writepage` is called) to see the context in which this invalidation is happening during the write operation. This unusual call chain needs to be understood.
|
| 9/2 |
2026/09/20 00:57 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/inode.c FirstLine:7680 LineCount:50]
Results: map[Contents:7680: u64 page_end = page_start + folio_size(folio) - 1;
7681: u64 cur;
7682: int inode_evicting = inode_state_read_once(&inode->vfs_inode) & I_FREEING;
7683:
7684: /*
7685: * We have folio locked so no new ordered extent can be created on this
7686: * page, nor bio can be submitted for this folio.
7687: *
7688: * But already submitted bio can still be finished on this folio.
7689: * Furthermore, endio function won't skip folio which has Ordered
7690: * already cleared, so it's possible for endio and
7691: * invalidate_folio to do the same ordered extent accounting twice
7692: * on one folio.
7693: *
7694: * So here we wait for any submitted bios to finish, so that we won't
7695: * do double ordered extent accounting on the same folio.
7696: */
7697: folio_wait_writeback(folio);
7698: wait_subpage_spinlock(folio);
7699:
7700: /*
7701: * The invalidated blocks are going away; drop any fixup blocks among
7702: * them, data included, as they have no space reservation.
7703: */
7704: btrfs_folio_clear_fixup_dirty(fs_info, folio, page_start + offset, length);
7705:
7706: /*
7707: * For subpage case, we have call sites like
7708: * btrfs_punch_hole_lock_range() which passes range not aligned to
7709: * sectorsize.
7710: * If the range doesn't cover the full folio, we don't need to and
7711: * shouldn't clear page extent mapped, as folio->private can still
7712: * record subpage dirty bits for other part of the range.
7713: *
7714: * For cases that invalidate the full folio even the range doesn't
7715: * cover the full folio, like invalidating the last folio, we're
7716: * still safe to wait for ordered extent to finish.
7717: */
7718: if (!(offset == 0 && length == folio_size(folio))) {
7719: btrfs_release_folio(folio, GFP_NOFS);
7720: return;
7721: }
7722:
7723: if (!inode_evicting)
7724: btrfs_lock_extent(tree, page_start, page_end, &cached_state);
7725:
7726: cur = page_start;
7727: while (cur < page_end) {
7728: struct btrfs_ordered_extent *ordered;
7729: u64 range_end;
]
|
| 10/2 |
2026/09/20 00:57 |
llm |
expert |
0m
Model:
Tokens:
input: 13004
output: 40
thoughts: 50
Thoughts: Here's my summary, as if I were thinking it:
**Tracing a Btrfs Writepath**
Okay, so I'm tracking down this potential bottleneck, and the core of the problem seems to be originating from a call to `wait_subpage_spinlock`. I remember seeing that one before. Let's see... the code's pointing me to line 7698. That rings a bell, but I want to dive into what's happening *before* that point. Based on the context I have, my gut tells me this stems from the `extent_writepage` function. Right, so that's the usual path for page writes. Let's head over to `fs/btrfs/extent_io.c:2058` to see what's happening there. I can usually get a good handle on what's going on at this point. Gotta see how the extent I/O is being set up and how we're reaching the subpage spinlock.
|
| 11/2 |
2026/09/20 00:57 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/extent_io.c FirstLine:2040 LineCount:50]
Results: map[Contents:2040: */
2041: static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl)
2042: {
2043: struct btrfs_inode *inode = BTRFS_I(folio->mapping->host);
2044: struct btrfs_fs_info *fs_info = inode->root->fs_info;
2045: int ret;
2046: size_t pg_offset;
2047: loff_t i_size = i_size_read(&inode->vfs_inode);
2048: const pgoff_t end_index = i_size >> PAGE_SHIFT;
2049: const unsigned int blocks_per_folio = btrfs_blocks_per_folio(fs_info, folio);
2050:
2051: trace_extent_writepage(folio, &inode->vfs_inode, bio_ctrl->wbc);
2052:
2053: WARN_ON(!folio_test_locked(folio));
2054:
2055: pg_offset = offset_in_folio(folio, i_size);
2056: if (folio->index > end_index ||
2057: (folio->index == end_index && !pg_offset)) {
2058: folio_invalidate(folio, 0, folio_size(folio));
2059: folio_unlock(folio);
2060: return 0;
2061: }
2062:
2063: if (folio_contains(folio, end_index))
2064: folio_zero_range(folio, pg_offset, folio_size(folio) - pg_offset);
2065:
2066: bio_ctrl_init_submit_bitmap(fs_info, folio, bio_ctrl);
2067: /*
2068: * If the page is dirty but without private set, it's marked dirty
2069: * without informing the fs.
2070: * Nowadays that is a bug, since the introduction of
2071: * pin_user_pages*().
2072: *
2073: * So here we check if the page has private set to rule out such
2074: * case.
2075: */
2076: if (unlikely(!folio_test_private(folio))) {
2077: DEBUG_WARN();
2078: btrfs_err_rl(fs_info,
2079: "root %lld ino %llu folio %llu is marked dirty without notifying the fs",
2080: btrfs_root_id(inode->root),
2081: btrfs_ino(inode), folio_pos(folio));
2082: ret = -EUCLEAN;
2083: goto done;
2084: }
2085:
2086: ret = set_folio_extent_mapped(folio);
2087: if (ret < 0)
2088: goto done;
2089:
]
|
| 12/2 |
2026/09/20 00:57 |
llm |
expert |
0m
Model:
Tokens:
input: 14146
output: 38
thoughts: 1295
Thoughts: **Analyzing a Btrfs Invalidation Assertion**
Okay, so I'm looking at this `folio_invalidate` call, specifically at line 2058. It looks like it's calling `btrfs_invalidate_folio`, which then calls `wait_subpage_spinlock`. Now, `wait_subpage_spinlock` has an assertion, requiring `folio_test_private(folio) && folio_get_private(folio)`. But hang on… `folio_invalidate` is called *before* the check for `folio_test_private(folio)` at line 2076! That's a potential problem.
From what I can tell, it appears the issue crops up when a folio lies beyond `i_size`. Specifically, If `folio->index > end_index` or `folio->index == end_index && !pg_offset` then `extent_writepage` calls `folio_invalidate(folio, 0, folio_size(folio))`. This function then calls `wait_subpage_spinlock(folio)`. And this in turn triggers that assertion.
The comment mentions that if a folio is marked dirty without `folio_test_private(folio)` (which can happen, for example, with direct I/O or GUP, as mentioned in the comment at 2068), and it's *also* beyond `i_size`, the assertion in `wait_subpage_spinlock` gets triggered.
The comment highlights a potential issue with pages being marked dirty without the filesystem being informed. According to the comment, this is now a bug since the introduction of `pin_user_pages*()`.
Now, the call trace shows something interesting. I see `aio_read` at the top of the call chain. It looks like:
```
aio_read+0x33a/0x4d0 fs/aio.c:1656
__io_submit_one fs/aio.c:-1 [inline]
io_submit_one+0x774/0x13d0 fs/aio.c:2106
__do_sys_io_submit fs/aio.c:2165 [inline]
__se_sys_io_submit+0x195/0x340 fs/aio.c:2135
```
This is an AIO read operation, reading into a user buffer. Is the user buffer mapped to a btrfs file? Let's check the rest of the call stack.
```
btrfs_page_mkwrite+0x856/0x1a40 fs/btrfs/file.c:1909
do_page_mkwrite+0x147/0x310 mm/memory.c:3816
wp_page_shared mm/memory.c:4217 [inline]
do_wp_page+0x1a9c/0x4b80 mm/memory.c:4440
handle_pte_fault mm/memory.c:6582 [inline]
__handle_mm_fault mm/memory.c:6707 [inline]
handle_mm_fault+0x15d7/0x3750 mm/memory.c:6876
faultin_page mm/gup.c:1126 [inline]
__get_user_pages+0x1675/0x2750 mm/gup.c:1428
__get_user_pages_locked mm/gup.c:1752 [inline]
__gup_longterm_locked+0x1032/0x15c0 mm/gup.c:2471
gup_fast_fallback+0x1d92/0x20e0 mm/gup.c:3209
iov_iter_extract_user_pages lib/iov_iter.c:1776 [inline]
iov_iter_extract_pages+0x3a1/0x690 lib/iov_iter.c:1845
iov_iter_extract_bvecs+0x272/0xb40 lib/iov_iter.c:1957
bio_iov_iter_get_pages+0x5ae/0xee0 block/bio.c:1309
blkdev_iov_iter_get_pages block/fops.c:49 [inline]
|
| 13/2 |
2026/09/20 00:57 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/file.c FirstLine:1890 LineCount:50]
Results: map[Contents:1890: }
1891: folio_wait_writeback(folio);
1892:
1893: btrfs_lock_extent(io_tree, page_start, page_end, &cached_state);
1894: ret = set_folio_extent_mapped(folio);
1895: if (ret < 0) {
1896: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1897: goto out_unlock;
1898: }
1899:
1900: /*
1901: * We can't set the delalloc bits if there are pending ordered
1902: * extents. Drop our locks and wait for them to finish.
1903: */
1904: ordered = btrfs_lookup_ordered_range(inode, page_start, fsize);
1905: if (ordered) {
1906: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1907: folio_unlock(folio);
1908: up_read(&inode->i_mmap_lock);
1909: btrfs_start_ordered_extent(ordered);
1910: btrfs_put_ordered_extent(ordered);
1911: goto again;
1912: }
1913:
1914: if (folio_contains(folio, (size - 1) >> PAGE_SHIFT)) {
1915: reserved_space = round_up(size - page_start, fs_info->sectorsize);
1916: if (reserved_space < fsize) {
1917: const u64 to_free = fsize - reserved_space;
1918:
1919: end = page_start + reserved_space - 1;
1920: if (only_release_metadata)
1921: btrfs_delalloc_release_metadata(inode, to_free, true);
1922: else
1923: btrfs_delalloc_release_space(inode, data_reserved,
1924: end + 1, to_free, true);
1925: }
1926: }
1927:
1928: ret = btrfs_reset_extent_delalloc(inode, page_start, end, 0, &cached_state);
1929: if (ret < 0) {
1930: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1931: goto out_unlock;
1932: }
1933:
1934: /* Page is wholly or partially inside EOF. */
1935: if (page_start + folio_size(folio) > size)
1936: zero_start = offset_in_folio(folio, size);
1937: else
1938: zero_start = fsize;
1939:
]
|
| 14/2 |
2026/09/20 00:57 |
llm |
expert |
0m
Model:
Tokens:
input: 17335
output: 39
thoughts: 38
Thoughts: Okay, here's my interpretation of that thought process, assuming I'm deep in the weeds of Btrfs debugging:
**Tracing a Btrfs Ordered Extent Start**
Alright, so the debugger just hit line 1909 and the `btrfs_start_ordered_extent` function is called. My immediate thought is, let's drill down into that function. This is where the magic (or the problems) likely begin. Time to understand the initiation of this ordered extent. Knowing the call stack now, I need to understand what triggered this call and what data it's operating on. What I really need to do is to quickly understand the state of the extent at that point, any locks held or acquired and how the ordering is set up. From here, I can examine what this function actually does - the initial setup, any pre-conditions, and how it prepares the extent for future I/O. Any errors or unexpected behaviour here are a potential point for the source of problems down the line.
|
| 15/2 |
2026/09/20 00:57 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/ordered-data.h FirstLine:190 LineCount:20]
Results: map[Contents: 190: };
191:
192: struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
193: struct btrfs_inode *inode, u64 file_offset,
194: const struct btrfs_file_extent *file_extent, unsigned long flags);
195: void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
196: struct btrfs_ordered_sum *sum);
197: struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
198: u64 file_offset);
199: void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry,
200: u64 nowriteback_start, u32 nowriteback_len);
201: static inline void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry)
202: {
203: return btrfs_start_ordered_extent_nowriteback(entry, 0, 0);
204: }
205:
206: int btrfs_wait_ordered_range(struct btrfs_inode *inode, u64 start, u64 len);
207: struct btrfs_ordered_extent *
208: btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset);
209: struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
]
|
| 16/2 |
2026/09/20 00:57 |
llm |
expert |
0m
Model:
Tokens:
input: 18083
output: 39
thoughts: 18
Thoughts: Okay, here's my interpretation of that thought process, from an expert's perspective:
**Thinking Through `btrfs_start_ordered_extent_nowriteback`**
Alright, so I've just been signaled that `btrfs_start_ordered_extent_nowriteback` has been called. This immediately tells me we're dealing with an extent that's likely part of the Btrfs filesystem's ordered data write path, and crucially, we're operating under the constraint of *no writeback*. This is important; it means we're trying to push this extent towards persistent storage in a way that bypasses the typical background writeback mechanisms, likely for performance or consistency reasons. I'll need to remember why the caller chose to use this function; there must be a specific reason it's important to not use writeback.
My immediate thoughts run through the implications. I’m guessing this might be related to things like metadata updates where data *must* be on disk before certain metadata operations complete, or potentially to handle a critical data segment that demands the highest priority for completion, even at the cost of blocking other operations. Perhaps we're dealing with a snapshot or transaction that's reaching a critical point, and the ordering is paramount.
I'm starting to consider the various scenarios. What kind of extent are we dealing with? Is it a data block, metadata, or something else entirely? The type of data will significantly impact my mental model of what's happening. I'll also be trying to quickly piece together the state of the transaction or operation that triggered this call. What's the overall context? Is it part of a larger, potentially critical, sequence? How is this related to other active processes or threads?
I'm already mentally reviewing the Btrfs code paths involved in these types of ordered write operations to anticipate potential issues or bottlenecks. I’m thinking about the locks, data structures, and the ordering guarantees we're aiming for. This call likely has performance ramifications, so I’m going to consider its effect on other operations. What's the potential for deadlock, or other concurrency-related problems? I'll also be thinking about the implications for error handling. If this operation fails, what's the impact on the filesystem's integrity, and how gracefully can we recover? The choice to bypass writeback usually has specific trade offs with regard to performance and efficiency. I'll mentally evaluate the trade-offs being made here.
|
| 17/2 |
2026/09/20 00:57 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/ordered-data.c FirstLine:810 LineCount:50]
Results: map[Contents: 810: list_splice_tail(&splice, &root->ordered_extents);
811: spin_unlock(&root->ordered_extent_lock);
812:
813: list_for_each_entry_safe(ordered, next, &works, work_list) {
814: list_del_init(&ordered->work_list);
815: wait_for_completion(&ordered->completion);
816: btrfs_put_ordered_extent(ordered);
817: cond_resched();
818: }
819: mutex_unlock(&root->ordered_extent_mutex);
820:
821: return count;
822: }
823:
824: /*
825: * Wait for @nr ordered extents that intersect the @bg, or the whole range of
826: * the filesystem if @bg is NULL.
827: */
828: void btrfs_wait_ordered_roots(struct btrfs_fs_info *fs_info, u64 nr,
829: const struct btrfs_block_group *bg)
830: {
831: struct btrfs_root *root;
832: LIST_HEAD(splice);
833: u64 done;
834:
835: mutex_lock(&fs_info->ordered_operations_mutex);
836: spin_lock(&fs_info->ordered_root_lock);
837: list_splice_init(&fs_info->ordered_roots, &splice);
838: while (!list_empty(&splice) && nr) {
839: root = list_first_entry(&splice, struct btrfs_root,
840: ordered_root);
841: root = btrfs_grab_root(root);
842: BUG_ON(!root);
843: list_move_tail(&root->ordered_root,
844: &fs_info->ordered_roots);
845: spin_unlock(&fs_info->ordered_root_lock);
846:
847: done = btrfs_wait_ordered_extents(root, nr, bg);
848: btrfs_put_root(root);
849:
850: if (nr != U64_MAX)
851: nr -= done;
852:
853: spin_lock(&fs_info->ordered_root_lock);
854: }
855: list_splice_tail(&splice, &fs_info->ordered_roots);
856: spin_unlock(&fs_info->ordered_root_lock);
857: mutex_unlock(&fs_info->ordered_operations_mutex);
858: }
859:
]
|
| 18/2 |
2026/09/20 00:57 |
llm |
expert |
0m
Model:
Tokens:
input: 19459
output: 48
thoughts: 21
Thoughts: Here's my take on those thoughts:
**BTRFS Ordered Extent Woes - A Deep Dive Needed**
Okay, hold on a second. Something's nagging at me. I've got a feeling I need to circle back and really *understand* how `btrfs_start_ordered_extent_nowriteback` is working, and how it's being used. I can already see some potential for some serious, subtle issues arising related to ordered extents, and the interaction with the *nowriteback* flag is what's really piquing my interest. There's probably something going on under the hood that I'm missing at a high level. Gotta make sure I'm not overlooking some crucial detail in that function's implementation or the assumptions it makes. I need to refresh my knowledge of it and how it's being used within the larger context of the filesystem's journaling and data integrity mechanisms.
|
| 19/2 |
2026/09/20 00:57 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/ordered-data.c Name:btrfs_start_ordered_extent_nowriteback]
Results: map[SourceCode: 860: /*
861: * Start IO and wait for a given ordered extent to finish.
862: *
863: * Wait on page writeback for all the pages in the extent but not in
864: * [@nowriteback_start, @nowriteback_start + @nowriteback_len) and the
865: * IO completion code to insert metadata into the btree corresponding to the extent.
866: */
867: void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry,
868: u64 nowriteback_start, u32 nowriteback_len)
869: {
870: u64 start = entry->file_offset;
871: u64 end = start + entry->num_bytes - 1;
872: struct btrfs_inode *inode = entry->inode;
873: bool freespace_inode;
874:
875: trace_btrfs_ordered_extent_start(inode, entry);
876:
877: /*
878: * If this is a free space inode do not take the ordered extents lockdep
879: * map.
880: */
881: freespace_inode = btrfs_is_free_space_inode(inode);
882:
883: /*
884: * pages in the range can be dirty, clean or writeback. We
885: * start IO on any dirty ones so the wait doesn't stall waiting
886: * for the flusher thread to find them
887: */
888: if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags)) {
889: if (!nowriteback_len) {
890: filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end);
891: } else {
892: if (start < nowriteback_start)
893: filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start,
894: nowriteback_start - 1);
895: if (nowriteback_start + nowriteback_len < end)
896: filemap_fdatawrite_range(inode->vfs_inode.i_mapping,
897: nowriteback_start + nowriteback_len,
898: end);
899: }
900: }
901:
902: if (!freespace_inode)
903: btrfs_might_wait_for_event(inode->root->fs_info, btrfs_ordered_extent);
904: wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE, &entry->flags));
905: }
SourceFile:fs/btrfs/ordered-data.c]
|
| 20/2 |
2026/09/20 00:57 |
llm |
expert |
0m
Model:
Tokens:
input: 20553
output: 41
thoughts: 869
Thoughts: **Tracing a Btrfs Bug: Dirty Pages and Private Flags**
Okay, so I'm looking at this Btrfs bug, and it seems to revolve around how dirty pages are handled during writeback operations when `aio_read` is involved. Specifically, it seems to be triggered during a read operation that causes a write, which in turn leads to this sequence: `btrfs_start_ordered_extent_nowriteback` -> `filemap_fdatawrite_range` -> `do_writepages` -> `btrfs_writepages` -> `extent_write_cache_pages` -> `extent_writepage`.
The key seems to be `extent_writepage`. Let's break down the scenario: we've got an `aio_read`, a user buffer gets mapped to a Btrfs file, which causes `__get_user_pages` to bring the page in for writing. Then `btrfs_page_mkwrite` gets involved, likely finding an existing ordered extent. This is where things get interesting. It drops the locks and then calls `btrfs_start_ordered_extent`, which triggers the writeback via `filemap_fdatawrite_range` for the ordered extent.
`filemap_fdatawrite_range` finds dirty pages and calls `extent_writepage` on them. Now here's where the bug manifests. `extent_writepage` checks if the page's index is beyond the file's `i_size`. If it is, `folio_invalidate` is called. `folio_invalidate` then calls `btrfs_invalidate_folio`, which in turn calls `wait_subpage_spinlock`.
And here's the kicker: `wait_subpage_spinlock` asserts that the page has the private flag set, but in this case, it doesn't. Specifically, it crashes if the page is beyond `i_size` but somehow *isn't* marked with the private flag. The comments say that this *should* be a bug because of recent changes and the use of the pin_user_pages*() functions. Why is a page dirty without the private flag? The comment in `extent_writepage` says, essentially, "This is supposed to be a bug".
Wait a minute, `folio_invalidate` is called *before* the check in `extent_writepage`! This means that if a page is beyond the file size AND doesn't have the private flag, we end up crashing in `wait_subpage_spinlock`.
But now, what's really important is that this assertion is only triggered for subpages! `btrfs_is_subpage` must be returning true. This tells me that this is only going to crash on filesystems with subpage block sizes, likely 4K sectors with 64K pages. Well, or possibly even 4K sectors with 4K pages. I'd better check `btrfs_is_subpage` to be sure. It returns true if sectorsize < PAGE_SIZE. So this is a subpage size.
|
| 21/2 |
2026/09/20 00:58 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_is_subpage]
Results: map[SourceCode: 98: static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
99: struct folio *folio)
100: {
101: if (folio->mapping && folio->mapping->host)
102: ASSERT(is_data_inode(BTRFS_I(folio->mapping->host)));
103: return fs_info->sectorsize < folio_size(folio);
104: }
SourceFile:fs/btrfs/subpage.h]
|
| 22/2 |
2026/09/20 00:58 |
llm |
expert |
0m
Model:
Tokens:
input: 22283
output: 40
thoughts: 900
Thoughts: **Unraveling a Btrfs Oddity with Large Folios**
Okay, so I'm seeing something odd... it seems like `sectorsize` is less than `folio_size(folio)`, which immediately makes me think, "Hmm, this needs a filesystem where the sector size is less than the page size." But on x86_64, page size is 4K, right? Can sectors be *less* than 4K? It's not the usual case.
Wait, the Syzkaller report! It gives me the hardware info. It's x86_64, so 4K page size confirmed. Does Btrfs even *allow* sector sizes less than 4K? Nope, I recall the minimum is 4K.
But here's the kicker... what if the *folio size* is larger than 4K? A large folio! If `folio_size(folio)` is greater than 4K, like with THP or a large folio in the page cache, then `fs_info->sectorsize` (which is 4K) *is* indeed less than `folio_size(folio)`! That means `btrfs_is_subpage` can return true for these large folios, even if the sector size is 4K! This is interesting...
Let me quickly confirm that Btrfs *does* support large folios. Yes, I knew it! They've been adding support. If we're using one of these large folios, then `btrfs_is_subpage` will indeed return true.
So, the next question is how can a folio exist *beyond* `i_size` (the file's size) without having a private set? If a file is truncated down, pages might be truncated, yes. But if a page exists *beyond* the file's size, how is it dirty without a private set? This is strange.
Maybe it was dirtied via mmap? If a file is mmapped, and the user writes to it, `btrfs_page_mkwrite` gets called, and *that* should set up the ordered extent and the private flag. But what happens if the file gets truncated *after* the page has been faulted in? If truncated, `truncate_pagecache` should remove the pages beyond the new `i_size`. Wait, if the page is beyond `i_size`, `extent_writepage` *should* invalidate it. But it *shouldn't* be dirty...
Ah, let me look at the Syzkaller reproducer or the call trace.
The call trace! It points to `btrfs_page_mkwrite`. Inside `btrfs_page_mkwrite`:
```c
folio_wait_writeback(folio);
btrfs_lock_extent(io_tree, page_start, page_end, &cached_state);
ret = set_folio_extent_mapped(folio);
if (ret < 0) {
btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
goto out_unlock;
}
/*
* We can't set the delalloc bits if there are pending ordered
* extents. Drop our locks and wait for them to finish.
*/
ordered = btrfs_lookup_ordered_range(inode, page_start, fsize);
if (ordered) {
btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
folio_unlock(folio);
up_read(&inode->i_mmap_lock);
btrfs_start_ordered_extent(ordered);
btrfs_put_ordered_extent(ordered);
goto again;
}
```
Wait a minute, `set_folio_extent_mapped(folio)` is called *before* `btrfs_lookup_ordered_range`. And `set_folio_extent_mapped` *does* set `folio_test_private(folio)`! That's the missing piece. Let me go check `set_folio_extent_mapped`.
|
| 23/2 |
2026/09/20 00:58 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:set_folio_extent_mapped]
Results: map[SourceCode: 953: int set_folio_extent_mapped(struct folio *folio)
954: {
955: struct btrfs_fs_info *fs_info;
956:
957: ASSERT(folio->mapping);
958:
959: if (folio_test_private(folio))
960: return 0;
961:
962: fs_info = folio_to_fs_info(folio);
963:
964: if (btrfs_is_subpage(fs_info, folio))
965: return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
966:
967: folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
968: return 0;
969: }
SourceFile:fs/btrfs/extent_io.c]
|
| 24/2 |
2026/09/20 00:58 |
llm |
expert |
0m
Model:
Tokens:
input: 24431
output: 38
thoughts: 257
Thoughts: **Debugging a Btrfs Folio Assertion Failure**
Okay, so `set_folio_extent_mapped` is how private data gets attached to a folio. If `btrfs_page_mkwrite` ends up calling that function, then the folio *should* have its private data set, right? But then, why am I getting a failed assertion inside `wait_subpage_spinlock`? That `ASSERT(folio_test_private(folio) && folio_get_private(folio));` is what's failing.
If `folio_test_private(folio)` is true, could `folio_get_private(folio)` be returning NULL? Let's quickly double-check how `folio_get_private` works. Ah, it just returns `folio->private`.
Now, if I remember correctly, `btrfs_attach_folio_state` is responsible for allocating a `btrfs_folio_state` and setting it as the private data. Wait a second, if `folio_test_private(folio)` *is* true and `folio_get_private(folio)` *isn't* NULL, then the assertion would pass, wouldn't it?
So the assert is failing because EITHER `folio_test_private(folio)` is false, OR `folio_get_private(folio)` is NULL. This means that either the private data isn't set, or it *is* set, but for some reason, the other check is failing. Let's go back and carefully examine the execution path of `btrfs_invalidate_folio` again, as that's likely where the root cause lies.
|
| 25/2 |
2026/09/20 00:58 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/inode.c FirstLine:7680 LineCount:50]
Results: map[Contents:7680: u64 page_end = page_start + folio_size(folio) - 1;
7681: u64 cur;
7682: int inode_evicting = inode_state_read_once(&inode->vfs_inode) & I_FREEING;
7683:
7684: /*
7685: * We have folio locked so no new ordered extent can be created on this
7686: * page, nor bio can be submitted for this folio.
7687: *
7688: * But already submitted bio can still be finished on this folio.
7689: * Furthermore, endio function won't skip folio which has Ordered
7690: * already cleared, so it's possible for endio and
7691: * invalidate_folio to do the same ordered extent accounting twice
7692: * on one folio.
7693: *
7694: * So here we wait for any submitted bios to finish, so that we won't
7695: * do double ordered extent accounting on the same folio.
7696: */
7697: folio_wait_writeback(folio);
7698: wait_subpage_spinlock(folio);
7699:
7700: /*
7701: * The invalidated blocks are going away; drop any fixup blocks among
7702: * them, data included, as they have no space reservation.
7703: */
7704: btrfs_folio_clear_fixup_dirty(fs_info, folio, page_start + offset, length);
7705:
7706: /*
7707: * For subpage case, we have call sites like
7708: * btrfs_punch_hole_lock_range() which passes range not aligned to
7709: * sectorsize.
7710: * If the range doesn't cover the full folio, we don't need to and
7711: * shouldn't clear page extent mapped, as folio->private can still
7712: * record subpage dirty bits for other part of the range.
7713: *
7714: * For cases that invalidate the full folio even the range doesn't
7715: * cover the full folio, like invalidating the last folio, we're
7716: * still safe to wait for ordered extent to finish.
7717: */
7718: if (!(offset == 0 && length == folio_size(folio))) {
7719: btrfs_release_folio(folio, GFP_NOFS);
7720: return;
7721: }
7722:
7723: if (!inode_evicting)
7724: btrfs_lock_extent(tree, page_start, page_end, &cached_state);
7725:
7726: cur = page_start;
7727: while (cur < page_end) {
7728: struct btrfs_ordered_extent *ordered;
7729: u64 range_end;
]
|
| 26/2 |
2026/09/20 00:58 |
llm |
expert |
0m
Model:
Tokens:
input: 25911
output: 38
thoughts: 738
Thoughts: **Analyzing a Btrfs Writeback Race Condition**
Okay, so I'm digging into this `btrfs_invalidate_folio` call path, trying to understand a potential race condition during writeback. It starts with `btrfs_invalidate_folio` called from `folio_invalidate` within `extent_writepage`, specifically in the context of handling potential truncation. Right, `extent_writepage` gets called from `btrfs_writepages`, which originates from `filemap_fdatawrite_range`, and ultimately, that's triggered by `btrfs_start_ordered_extent_nowriteback`, which is, in turn, called by `btrfs_page_mkwrite`.
Here's where it gets interesting: I'm noticing in `btrfs_page_mkwrite` that the folio is *unlocked* (`folio_unlock(folio)`) *before* we call `btrfs_start_ordered_extent(ordered)`. This means the folio is unlocked when `filemap_fdatawrite_range` is called from within `btrfs_start_ordered_extent`. So, `filemap_fdatawrite_range` is likely to find dirty pages, lock them, and then call `extent_writepage` again, potentially for the same folio.
My concern is, if the folio is unlocked in between those calls, could it be truncated or invalidated in the meantime? If the file's truncated, `folio->mapping` would be NULL, and the write path would be short-circuited. Okay, I see. But `extent_writepage` itself has a check against `i_size`. That's it!
`extent_writepage` checks if the folio's index is beyond `i_size` or if the page offset is valid given `i_size`. If the file was truncated, `i_size` is now smaller. So the folio *is* now beyond `i_size`, and the condition will trigger a call to `folio_invalidate`, which does indeed call `btrfs_invalidate_folio`.
Okay, but what I'm really trying to understand is *why* I might see a problem related to `folio_test_private(folio)` returning false. If the folio was truncated, and `btrfs_invalidate_folio` has already run, it would have cleared the private data! So, I need to check the code of `btrfs_invalidate_folio` itself to confirm whether it's the one responsible for the state I'm looking for. Let me check...
|
| 27/2 |
2026/09/20 00:58 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/inode.c FirstLine:7720 LineCount:50]
Results: map[Contents:7720: return;
7721: }
7722:
7723: if (!inode_evicting)
7724: btrfs_lock_extent(tree, page_start, page_end, &cached_state);
7725:
7726: cur = page_start;
7727: while (cur < page_end) {
7728: struct btrfs_ordered_extent *ordered;
7729: u64 range_end;
7730: u32 range_len;
7731: u32 extra_flags = 0;
7732:
7733: ordered = btrfs_lookup_first_ordered_range(inode, cur,
7734: page_end + 1 - cur);
7735: if (!ordered) {
7736: range_end = page_end;
7737: /*
7738: * No ordered extent covering this range, we are safe
7739: * to delete all extent states in the range.
7740: */
7741: extra_flags = EXTENT_CLEAR_ALL_BITS;
7742: goto next;
7743: }
7744: if (ordered->file_offset > cur) {
7745: /*
7746: * There is a range between [cur, oe->file_offset) not
7747: * covered by any ordered extent.
7748: * We are safe to delete all extent states, and handle
7749: * the ordered extent in the next iteration.
7750: */
7751: range_end = ordered->file_offset - 1;
7752: extra_flags = EXTENT_CLEAR_ALL_BITS;
7753: goto next;
7754: }
7755:
7756: range_end = min(ordered->file_offset + ordered->num_bytes - 1,
7757: page_end);
7758: ASSERT(range_end + 1 - cur < U32_MAX);
7759: range_len = range_end + 1 - cur;
7760: /*
7761: * If the range is not dirty, the range has been submitted and
7762: * since we have waited for the writeback, endio has been
7763: * executed, thus we must skip the range to avoid double
7764: * accounting for the ordered extent.
7765: */
7766: if (!btrfs_folio_test_dirty(fs_info, folio, cur, range_len))
7767: goto next;
7768:
7769: /*
]
|
| 28/2 |
2026/09/20 00:58 |
llm |
expert |
0m
Model:
Tokens:
input: 27985
output: 38
thoughts: 16
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Diving into `btrfs_invalidate_folio`**
Alright, so the callstack has landed me in `btrfs_invalidate_folio`. Given the context – likely some sort of data corruption, cache invalidation, or maybe even a writeback issue – my initial thought is, "Let's dig in and see exactly what's happening here." I need to understand the specifics of *this* invalidation. Is it a full folio, partial, what are the flags and reasons behind the call? Is this triggered by metadata changes, extent map modifications, or something related to data integrity? I'm already anticipating some of the common Btrfs invalidation scenarios – unclean extent, transaction start, or maybe a race condition with concurrent I/O. I'll need to look closely at how this interacts with the various Btrfs structures: the extent tree, inode data, and the journaling system. Let’s get to reading and analysis, I’m ready to understand the nuances.
|
| 29/2 |
2026/09/20 00:58 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/inode.c FirstLine:7770 LineCount:50]
Results: map[Contents:7770: * The range is dirty meaning it has not been submitted.
7771: * Here we need to truncate the OE range as the range will never
7772: * be submitted.
7773: *
7774: * IO on this page will never be started, so we need to account
7775: * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
7776: * here, must leave that up for the ordered extent completion.
7777: *
7778: * This will also unlock the range for incoming
7779: * btrfs_finish_ordered_io().
7780: */
7781: if (!inode_evicting)
7782: btrfs_clear_extent_bit(tree, cur, range_end,
7783: EXTENT_DELALLOC |
7784: EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7785: EXTENT_DEFRAG, &cached_state);
7786:
7787: btrfs_mark_ordered_extent_truncated(ordered, cur - ordered->file_offset);
7788:
7789: /*
7790: * If the ordered extent has finished, we're safe to delete all
7791: * the extent states of the range, otherwise
7792: * btrfs_finish_ordered_io() will get executed by endio for
7793: * other pages, so we can't delete extent states.
7794: */
7795: if (btrfs_dec_test_ordered_pending(inode, &ordered,
7796: cur, range_end + 1 - cur)) {
7797: btrfs_finish_ordered_io(ordered);
7798: /*
7799: * The ordered extent has finished, now we're again
7800: * safe to delete all extent states of the range.
7801: */
7802: extra_flags = EXTENT_CLEAR_ALL_BITS;
7803: }
7804: next:
7805: if (ordered)
7806: btrfs_put_ordered_extent(ordered);
7807: /*
7808: * Qgroup reserved space handler
7809: * Sector(s) here will be either:
7810: *
7811: * 1) Already written to disk or bio already finished
7812: * Then its QGROUP_RESERVED bit in io_tree is already cleared.
7813: * Qgroup will be handled by its qgroup_record then.
7814: * btrfs_qgroup_free_data() call will do nothing here.
7815: *
7816: * 2) Not written to disk yet
7817: * Then btrfs_qgroup_free_data() call will clear the
7818: * QGROUP_RESERVED bit of its io_tree, and free the qgroup
7819: * reserved data space.
]
|
| 30/2 |
2026/09/20 00:58 |
llm |
expert |
0m
Model:
Tokens:
input: 29115
output: 38
thoughts: 13
Thoughts: Okay, here's my summary, taking on the persona you've described:
**Reflecting on `btrfs_invalidate_folio` Completion**
Alright, so I'm at the end of the `btrfs_invalidate_folio` function, and I'm running through what just happened. Essentially, my goal here was to ensure a folio is properly invalidated, and that any resources related to it are cleaned up. This function is critical for maintaining data integrity and consistency, especially during operations that might trigger a writeback or discard operation.
I had to ensure that the appropriate flags were set to mark the folio as invalid. I also needed to make certain that any associated data structures are updated, to reflect its new, invalid state. Then, I had to consider the implications of invalidating a folio for how to deal with page faults or any data that might still be cached. I really needed to make certain that no stale data is ever used.
I also had to carefully manage any potential race conditions during this invalidation process. This involves careful synchronization, and making sure other threads and processes don't try to access the folio at the same time. The way the folio is managed impacts how writeback requests work, so it's a critical part of the I/O layer. I have to think about what other operations are happening concurrently, and how to avoid introducing any errors.
Ultimately, by executing this logic correctly, I ensure that the file system remains consistent and that any subsequent operations on this data behave as expected - namely, that the system can handle invalid folios properly. This function's proper operation is also critical for file system robustness, and, well, for overall system performance and preventing data corruption.
|
| 31/2 |
2026/09/20 00:58 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/inode.c FirstLine:7820 LineCount:50]
Results: map[Contents:7820: * Since the IO will never happen for this page.
7821: */
7822: btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL);
7823: if (!inode_evicting)
7824: btrfs_clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
7825: EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7826: EXTENT_DEFRAG | extra_flags,
7827: &cached_state);
7828: cur = range_end + 1;
7829: }
7830: btrfs_folio_clear_dirty(fs_info, folio, page_start, folio_size(folio));
7831: btrfs_clear_folio_dirty_tag(folio);
7832: if (!inode_evicting)
7833: __btrfs_release_folio(folio, GFP_NOFS);
7834: clear_folio_extent_mapped(folio);
7835: }
7836:
7837: static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback)
7838: {
7839: struct btrfs_truncate_control control = {
7840: .inode = inode,
7841: .ino = btrfs_ino(inode),
7842: .min_type = BTRFS_EXTENT_DATA_KEY,
7843: .clear_extent_range = true,
7844: .new_size = inode->vfs_inode.i_size,
7845: };
7846: struct btrfs_root *root = inode->root;
7847: struct btrfs_fs_info *fs_info = root->fs_info;
7848: struct btrfs_block_rsv rsv;
7849: int ret;
7850: struct btrfs_trans_handle *trans;
7851: const u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
7852: const u64 lock_start = round_down(inode->vfs_inode.i_size, fs_info->sectorsize);
7853: const u64 i_size_up = round_up(inode->vfs_inode.i_size, fs_info->sectorsize);
7854:
7855: /* Our inode is locked and the i_size can't be changed concurrently. */
7856: btrfs_assert_inode_locked(inode);
7857:
7858: if (!skip_writeback) {
7859: ret = btrfs_wait_ordered_range(inode, lock_start, (u64)-1);
7860: if (ret)
7861: return ret;
7862: }
7863:
7864: /*
7865: * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
7866: * things going on here:
7867: *
7868: * 1) We need to reserve space to update our inode.
7869: *
]
|
| 32/2 |
2026/09/20 00:58 |
llm |
expert |
0m
Model:
Tokens:
input: 30425
output: 38
thoughts: 562
Thoughts: **Debugging a Btrfs Folio Invalidation Issue**
Okay, here's what's running through my head. I'm looking at this `btrfs_invalidate_folio` function and immediately I see that it calls `clear_folio_extent_mapped(folio)` at the end. That clears the private data, which is important. This is crucial because `btrfs_invalidate_folio` gets called from `folio_invalidate` which is used by `extent_writepage`. `extent_writepage` checks `folio_test_private(folio)`. Hang on, `folio_invalidate` is *called* before that check.
`folio_invalidate` itself calls `btrfs_invalidate_folio` which then calls `wait_subpage_spinlock`, and that asserts that `folio_test_private(folio)` *is* true. So, if `folio_test_private(folio)` is false, *boom*, it crashes in `wait_subpage_spinlock`.
The problem is: Why would `folio_test_private(folio)` be false when `extent_writepage` is called? The comment in `extent_writepage` says it's a bug if a page is dirty without private set. It suggests it can happen if the page was dirtied "without informing the fs". But, wait a second, if the page was dirtied via GUP, like this syzkaller trace uses `__get_user_pages`, that should inform the filesystem. `__get_user_pages` calls `faultin_page`, calls `handle_mm_fault`, calls `do_wp_page`, calls `wp_page_shared`, calls `do_page_mkwrite`, calls `btrfs_page_mkwrite`. And *crucially*, `btrfs_page_mkwrite` calls `set_folio_extent_mapped`, which *sets* private!
So, if `btrfs_page_mkwrite` was called, the folio SHOULD have the private bit set. Yet, it's not. Something's going wrong. I need to go back and look at `btrfs_page_mkwrite` again, line by line. There's a missing piece here.
|
| 33/2 |
2026/09/20 00:58 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/file.c FirstLine:1890 LineCount:50]
Results: map[Contents:1890: }
1891: folio_wait_writeback(folio);
1892:
1893: btrfs_lock_extent(io_tree, page_start, page_end, &cached_state);
1894: ret = set_folio_extent_mapped(folio);
1895: if (ret < 0) {
1896: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1897: goto out_unlock;
1898: }
1899:
1900: /*
1901: * We can't set the delalloc bits if there are pending ordered
1902: * extents. Drop our locks and wait for them to finish.
1903: */
1904: ordered = btrfs_lookup_ordered_range(inode, page_start, fsize);
1905: if (ordered) {
1906: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1907: folio_unlock(folio);
1908: up_read(&inode->i_mmap_lock);
1909: btrfs_start_ordered_extent(ordered);
1910: btrfs_put_ordered_extent(ordered);
1911: goto again;
1912: }
1913:
1914: if (folio_contains(folio, (size - 1) >> PAGE_SHIFT)) {
1915: reserved_space = round_up(size - page_start, fs_info->sectorsize);
1916: if (reserved_space < fsize) {
1917: const u64 to_free = fsize - reserved_space;
1918:
1919: end = page_start + reserved_space - 1;
1920: if (only_release_metadata)
1921: btrfs_delalloc_release_metadata(inode, to_free, true);
1922: else
1923: btrfs_delalloc_release_space(inode, data_reserved,
1924: end + 1, to_free, true);
1925: }
1926: }
1927:
1928: ret = btrfs_reset_extent_delalloc(inode, page_start, end, 0, &cached_state);
1929: if (ret < 0) {
1930: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1931: goto out_unlock;
1932: }
1933:
1934: /* Page is wholly or partially inside EOF. */
1935: if (page_start + folio_size(folio) > size)
1936: zero_start = offset_in_folio(folio, size);
1937: else
1938: zero_start = fsize;
1939:
]
|
| 34/2 |
2026/09/20 00:58 |
llm |
expert |
0m
Model:
Tokens:
input: 32338
output: 42
thoughts: 217
Thoughts: **Debugging a Btrfs Write Path**
Okay, let me think this through. `btrfs_page_mkwrite` calls `set_folio_extent_mapped(folio)`. Right, that sets up the folio for mapping. Then it checks for an 'ordered' extent. If it finds one, it unlocks the folio, drops any locks it has, and calls `btrfs_start_ordered_extent(ordered)`. That function then calls `filemap_fdatawrite_range`, which leads to `extent_writepage`.
But hold on a second... when `btrfs_page_mkwrite` unlocks the folio, it *already* called `set_folio_extent_mapped(folio)`! So, in theory, the folio *should* have private data set, since that's what `set_folio_extent_mapped` *does*. That means the `folio->private` pointer should be valid and non-NULL.
So then, why is my `wait_subpage_spinlock` assertion failing? It checks: `ASSERT(folio_test_private(folio) && folio_get_private(folio));`. It's saying that the assertion is true, so `folio_test_private(folio)` is true, but `folio_get_private(folio)` is NULL!
That can only mean one thing. `folio_test_private(folio)` must be true, which would indicate there is a private pointer, but somehow `folio_get_private(folio)` is returning NULL when it *shouldn't* be. I need to go back and check the implementation of `set_folio_extent_mapped`.
|
| 35/2 |
2026/09/20 00:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:set_folio_extent_mapped]
Results: map[SourceCode: 953: int set_folio_extent_mapped(struct folio *folio)
954: {
955: struct btrfs_fs_info *fs_info;
956:
957: ASSERT(folio->mapping);
958:
959: if (folio_test_private(folio))
960: return 0;
961:
962: fs_info = folio_to_fs_info(folio);
963:
964: if (btrfs_is_subpage(fs_info, folio))
965: return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
966:
967: folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
968: return 0;
969: }
SourceFile:fs/btrfs/extent_io.c]
|
| 36/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 33234
output: 43
thoughts: 43
Thoughts: Okay, here's my interpretation of this scenario, assuming I'm deep in the weeds with Btrfs code:
**Analyzing Btrfs Folio Attachment**
Alright, so if `btrfs_is_subpage` is true, then we're going to dive into `btrfs_attach_folio_state`. That's the key. I need to understand what that function is doing in this subpage context. This is likely where the core logic related to handling these more granular folio attachments resides. It's the point of interest.
What are we attaching and to what? Is it metadata, data, state flags, or something else entirely? The `btrfs_is_subpage` check is important as it suggests a specific scenario within the wider folio management, which likely means we're dealing with optimizations or different behaviors for subpage handling. I need to go look up the definition of `btrfs_attach_folio_state`. I need a quick refresher.
|
| 37/2 |
2026/09/20 00:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/subpage.c Name:btrfs_attach_folio_state]
Results: map[SourceCode: 38: int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
39: struct folio *folio, enum btrfs_folio_type type)
40: {
41: struct btrfs_folio_state *bfs;
42:
43: /* For metadata we don't support large folio yet. */
44: if (type == BTRFS_SUBPAGE_METADATA)
45: ASSERT(!folio_test_large(folio));
46:
47: /*
48: * We have cases like a dummy extent buffer page, which is not mapped
49: * and doesn't need to be locked.
50: */
51: if (folio->mapping)
52: ASSERT(folio_test_locked(folio));
53:
54: /* Either not subpage, or the folio already has private attached. */
55: if (folio_test_private(folio))
56: return 0;
57: if (type == BTRFS_SUBPAGE_METADATA && !btrfs_meta_is_subpage(fs_info))
58: return 0;
59: if (type == BTRFS_SUBPAGE_DATA && !btrfs_is_subpage(fs_info, folio))
60: return 0;
61:
62: bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, GFP_NOFS);
63: if (IS_ERR(bfs))
64: return PTR_ERR(bfs);
65:
66: folio_attach_private(folio, bfs);
67: return 0;
68: }
SourceFile:fs/btrfs/subpage.c]
|
| 38/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 34040
output: 23
thoughts: 279
Thoughts: **Debugging a Btrfs Folio State Issue**
Okay, so I'm tracing through this folio attachment process. `btrfs_attach_folio_state` allocates a `btrfs_folio_state` struct and links it to the folio using `folio_attach_private`. `folio_attach_private` does its thing, sets `folio->private`, and the private flag is set. Simple enough. Therefore, `folio_get_private(folio)` should return my `bfs` structure, and it *won't* be null.
But hang on... if `folio_test_private(folio)` returns true *and* `folio_get_private(folio)` returns my valid `bfs` pointer, the assertion in `wait_subpage_spinlock` should *definitely* pass! I'm staring right at this code, and it still seems right:
```c
ASSERT(folio_test_private(folio) && folio_get_private(folio));
```
Why is that assertion failing? This doesn't make any sense...
Aha! Let me look closer at where this is set with `set_folio_extent_mapped`:
```c
if (btrfs_is_subpage(fs_info, folio))
return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
```
Okay, I see it. If `btrfs_is_subpage` returns *false*, it attaches `EXTENT_FOLIO_PRIVATE` to the folio using `folio_attach_private`. What *is* this `EXTENT_FOLIO_PRIVATE`? That's the key! It's not my `btrfs_folio_state`. I can see the issue now.
|
| 39/2 |
2026/09/20 00:59 |
tool |
grepper |
0m
Args: map[Expression:#define EXTENT_FOLIO_PRIVATE]
Results: map[Output:fs/btrfs/extent_io.h=53=enum {
--
fs/btrfs/extent_io.h-63- */
fs/btrfs/extent_io.h:64:#define EXTENT_FOLIO_PRIVATE 1
fs/btrfs/extent_io.h-65-
]
|
| 40/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 34847
output: 27
thoughts: 1645
Thoughts: **Analyzing a Btrfs Data Folio Concurrency Issue**
Okay, so I'm looking at this code and immediately keying in on `EXTENT_FOLIO_PRIVATE` being set to `1`. If `btrfs_is_subpage` is false, then `folio->private` is 1, straightforward. But that `wait_subpage_spinlock` call… that assert! It expects `folio->private` to be set *and* non-NULL if it's dealing with a subpage. If the subpage check fails, the function returns. My mind instantly races - if `btrfs_is_subpage` can change, then that's the vulnerability.
`folio_size` is fixed, and `fs_info->sectorsize` isn't changing either. So, is there *another* thread potentially setting `folio->private` to `1`? No, `set_folio_extent_mapped` in `btrfs_page_mkwrite` *should* be the only place. But, let's step back, what if `folio_test_private` returns `false` *after* it's set?
Aha! `btrfs_invalidate_folio` clears the private flag! This is the culprit. `btrfs_page_mkwrite` sets it, unlocks the folio, another thread truncates the file, locks the folio, calls `btrfs_invalidate_folio`, and now the private flag is cleared! Then, `filemap_fdatawrite_range` is called via `btrfs_start_ordered_extent` and `extent_writepage`.
But, wait, if the file was truncated, the folio's mapping would be `NULL`, so the write wouldn't even *happen*. Okay, `extent_writepage` checks against `i_size`. If the file was truncated, `folio_invalidate` gets called, which in turn calls `btrfs_invalidate_folio`. *That's* where we can re-trigger the issue! `btrfs_invalidate_folio` calls `wait_subpage_spinlock`, and if the private flag is cleared, the assert fails!
But `extent_writepage` shouldn't be called if the dirty flag is cleared! Wait, `btrfs_invalidate_folio` *also* clears the dirty flag! If dirty is cleared, then `filemap_fdatawrite_range` won't even find it!
Let's re-evaluate. Is there a scenario where a folio is dirty but `folio->private` is false? The comment in `extent_writepage` indicates that *yes*, that's possible. It points to a bug where pages become dirty, but without setting private. This is new to me, the introduction of `pin_user_pages*` changed things.
The syzkaller trace is showing `aio_read` -> `__get_user_pages` -> `faultin_page` -> `handle_mm_fault` -> `do_wp_page` -> `wp_page_shared` -> `do_page_mkwrite` -> `btrfs_page_mkwrite`. `aio_read` is reading into a user buffer, and `__get_user_pages` triggers the faults. If a page is in cache, and not writable, `do_page_mkwrite` is called. But, what if the page is dirtied directly by `__get_user_pages`?
Wait a second. What if `iov_iter_extract_pages` is involved? It *is* called, and it uses GUP. GUP can dirty the page if FOLL_WRITE is used. Then `__get_user_pages` would call `set_page_dirty_lock` when released.
Nope. That is not how it is dirtied.
Ah ha! `bio_iov_iter_get_pages` adds pages to a bio. When the bio *completes* (the read is done), the pages are marked dirty! That's it! When a direct I/O read into a user buffer happens, the pages get pinned, and on completion, the block layer calls `folio_mark_dirty`! Now, I need to investigate `bio_check_pages_dirty` and how pages are marked dirty during bio completion. This looks like the root cause.
|
| 41/2 |
2026/09/20 00:59 |
tool |
grepper |
0m
Args: map[Expression:folio_mark_dirty PathPrefix:block/]
Results: map[Output:block/bio.c=1166=void __bio_release_pages(struct bio *bio, bool mark_dirty)
--
block/bio.c-1174- folio_lock(fi.folio);
block/bio.c:1175: folio_mark_dirty(fi.folio);
block/bio.c-1176- folio_unlock(fi.folio);
--
block/bio.c=1492=static void bvec_unpin(struct bio_vec *bv, bool mark_dirty)
--
block/bio.c-1498- if (mark_dirty)
block/bio.c:1499: folio_mark_dirty_lock(folio);
block/bio.c-1500- unpin_user_folio(folio, nr_pages);
--
block/bio.c=1703=EXPORT_SYMBOL(bio_free_pages);
--
block/bio.c-1708- *
block/bio.c:1709: * The problem is that we cannot run folio_mark_dirty() from interrupt context
block/bio.c-1710- * because the required locks are not interrupt-safe. So what we can do is to
--
block/bio.c=1728=void bio_set_pages_dirty(struct bio *bio)
--
block/bio.c-1733- folio_lock(fi.folio);
block/bio.c:1734: folio_mark_dirty(fi.folio);
block/bio.c-1735- folio_unlock(fi.folio);
]
|
| 42/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 37874
output: 39
thoughts: 141
Thoughts: **Analyzing Btrfs Dirty Page Handling**
Okay, here's what's going through my head: I see `bio_set_pages_dirty` or `bvec_unpin` calls, and they both ultimately lead to a `folio_mark_dirty_lock(folio)` call. That makes sense, marking the folio as dirty, that's expected. And then, as I expect, `folio_mark_dirty_lock` indeed does mark the folio as dirty.
Now, if this folio happens to be associated with a Btrfs file, which is crucial, `folio_mark_dirty` seems to then call either `btrfs_dirty_folio` or perhaps `filemap_dirty_folio`. Good, that makes sense - we need to signal to the Btrfs layer that this folio is dirty.
Hold on... I'm just recalling something... `btrfs_dirty_folio` *doesn't* set the private flag, if I recall correctly. That's a point to make sure. Okay, I really need to double-check that; I'm going to quickly review either the `btrfs_dirty_folio` function itself, or the `dirty_folio` address space operation for Btrfs, and make sure that's correct. Need to confirm that this is how things work.
|
| 43/2 |
2026/09/20 00:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_aops]
Results: map[SourceCode:10807: /*
10808: * btrfs doesn't support the bmap operation because swapfiles
10809: * use bmap to make a mapping of extents in the file. They assume
10810: * these extents won't change over the life of the file and they
10811: * use the bmap result to do IO directly to the drive.
10812: *
10813: * the btrfs bmap call would return logical addresses that aren't
10814: * suitable for IO and they also will change frequently as COW
10815: * operations happen. So, swapfile + btrfs == corruption.
10816: *
10817: * For now we're avoiding this by dropping bmap.
10818: */
10819: static const struct address_space_operations btrfs_aops = {
10820: .read_folio = btrfs_read_folio,
10821: .writepages = btrfs_writepages,
10822: .readahead = btrfs_readahead,
10823: .invalidate_folio = btrfs_invalidate_folio,
10824: .launder_folio = btrfs_launder_folio,
10825: .release_folio = btrfs_release_folio,
10826: .migrate_folio = btrfs_migrate_folio,
10827: .dirty_folio = btrfs_data_dirty_folio,
10828: .error_remove_folio = generic_error_remove_folio,
10829: .swap_activate = btrfs_swap_activate,
10830: .swap_deactivate = btrfs_swap_deactivate,
10831: };
SourceFile:fs/btrfs/inode.c]
|
| 44/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 38869
output: 42
thoughts: 15
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience and written in the first person:
**Diving into btrfs_data_dirty_folio**
Okay, so I need to take a look at `btrfs_data_dirty_folio`. My immediate thought is, "why this specific function?" What's prompting this investigation? Am I tracking down a performance issue related to dirty pages, a suspected write amplification problem, or something else entirely? Knowing the context is crucial.
I need to understand its place within the broader Btrfs I/O path. This likely involves understanding how it interacts with the folio cache, the extent tree, and the transaction handling. Is it involved in marking data for writeback, or is it more about the initial marking of dirty pages after a write? Is this an in-place modification or is it related to COW?
I'll need to look at its callers and callees to get a feel for the control flow. Are there any common patterns in how it's used? What are the typical data structures that it operates on? Are there any potential bottlenecks I should anticipate, like spinlocks or other synchronization primitives that could become a point of contention? Is the function itself complex or fairly straightforward? Does it call any other functions that look like they could be slow or problematic?
I'm also curious about error handling. What happens if the folio can't be dirtied? Are there retries? Are error conditions gracefully handled, or could they potentially lead to data loss or corruption? I'll probably also want to see where this function is called in the case of a failed fsck or repair, or a mount, as this would provide additional insight as to the circumstances that lead to this part of the code being triggered.
Finally, I want to keep the bigger picture in mind. What's the overall design goal of this function? How does it contribute to Btrfs's copy-on-write design, its data integrity, and its overall performance? Knowing the "why" behind the code is just as important as understanding the "how."
|
| 45/2 |
2026/09/20 00:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_data_dirty_folio]
Results: map[SourceCode:10772: /*
10773: * The folio is going dirty without a btrfs delalloc space reservation.
10774: * This requires a fixup before writeback which we might sleep so cannot
10775: * run in this context, so we merely set state on the folio indicating it
10776: * needs fixup before writeback.
10777: *
10778: * Note that there is no range in the input, so the whole folio is marked
10779: * dirty and fixup.
10780: *
10781: * We believe that all callers of dirty_folio either:
10782: * - take the folio lock (e.g. pinned folio release notification).
10783: * - take the pte lock but must be running on a dirty pte which means
10784: * page_mkwrite() ran on it and reserved the space. zap_pte_range() cannot
10785: * race with writeback cleaning the folio because writeback runs
10786: * folio_mkclean() which also uses the pte lock and revokes outstanding
10787: * writable mappings.
10788: * Therefore, an additional folio private lock (a la bfs->lock for all cases,
10789: * not just subpage) is not necessary.
10790: */
10791: static bool btrfs_data_dirty_folio(struct address_space *mapping,
10792: struct folio *folio)
10793: {
10794: struct btrfs_inode *inode = BTRFS_I(mapping->host);
10795: struct btrfs_fs_info *fs_info = inode->root->fs_info;
10796: const u64 page_start = folio_pos(folio);
10797: const u64 range_end = min_t(u64, folio_next_pos(folio),
10798: round_up(i_size_read(&inode->vfs_inode),
10799: fs_info->sectorsize));
10800:
10801: if (range_end > page_start)
10802: btrfs_folio_set_fixup_dirty(fs_info, folio, page_start,
10803: range_end - page_start);
10804: return filemap_dirty_folio(mapping, folio);
10805: }
SourceFile:fs/btrfs/inode.c]
|
| 46/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 40070
output: 45
thoughts: 45
Thoughts: Here's my summary, as though I were thinking through the process:
**Diving into Btrfs Data Dirtying**
Okay, so `btrfs_data_dirty_folio` is the entry point, and it's calling `btrfs_folio_set_fixup_dirty`. That's the key. I need to understand what `btrfs_folio_set_fixup_dirty` is doing to the folio. My immediate thought is, what's its impact on the lifecycle of the data? Is it just marking the folio dirty, or is there more going on? I'd expect it to at least set the dirty flag, but is it coordinating with any journaling, metadata updates, or I/O scheduling as part of the process? I'll need to dig into the definition of `btrfs_folio_set_fixup_dirty` to be sure. It's likely this is the core function where the actual "dirtying" operation happens, but I need to see if it's atomic and thread-safe and if there are any race conditions I should be considering. Alright, let's go check the source code...
|
| 47/2 |
2026/09/20 00:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_folio_set_fixup_dirty]
Results: map[SourceCode: 550: /*
551: * Mark the still-clean blocks of a folio dirty and needing fixup, for
552: * btrfs_data_dirty_folio().
553: *
554: * A subpage block size folio that is not uptodate is left alone: its clean
555: * blocks may hold content that was never read in, which must not be marked
556: * dirty.
557: */
558: void btrfs_folio_set_fixup_dirty(const struct btrfs_fs_info *fs_info,
559: struct folio *folio, u64 start, u32 len)
560: {
561: if (!btrfs_is_subpage(fs_info, folio)) {
562: if (!folio_test_dirty(folio))
563: folio_set_fixup_pending(folio);
564: return;
565: }
566: if (!folio_test_uptodate(folio))
567: return;
568: btrfs_subpage_set_fixup_dirty(fs_info, folio, start, len);
569: }
SourceFile:fs/btrfs/subpage.c]
|
| 48/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 40776
output: 47
thoughts: 40
Thoughts: Okay, here's my interpretation of that thought process, from an expert's perspective:
**Diving into `btrfs_subpage_set_fixup_dirty`**
Alright, the debugger just hit a call to `btrfs_subpage_set_fixup_dirty`. That's a key function in Btrfs, and it's probably involved in how dirty pages are handled within subpages. My immediate reaction is to zero in on that. This suggests we're dealing with the finer details of how data is tracked as it's modified within a Btrfs filesystem. Given the naming convention, it likely deals with setting the dirty flag, perhaps correcting or adjusting it, within a subpage data structure. Now, the next logical step is to dive right into the source code of that specific function to understand the context and exactly what it does. I want to quickly refresh my understanding of *how* and *why* it's getting called right now. I need to understand what's triggering this particular function call. What kind of operation is happening that necessitates setting or correcting the dirty flag on a subpage? This is important because understanding how it's handled can provide insights into potential areas for optimization, or where a particular error might arise.
|
| 49/2 |
2026/09/20 00:59 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/subpage.c Name:btrfs_subpage_set_fixup_dirty]
Results: map[SourceCode: 515: /*
516: * In one pass under bfs->lock, mark every block with a clear dirty bit in the
517: * range both dirty and needing fixup.
518: *
519: * Only called from the dirty_folio callback, which owns the folio-level
520: * dirty flag; calling folio_mark_dirty() here would recurse.
521: *
522: * The folio fixup flag and bits are both set under bfs->lock so that a
523: * writeback pass observing the new bits also observes the flag.
524: */
525: static void btrfs_subpage_set_fixup_dirty(const struct btrfs_fs_info *fs_info,
526: struct folio *folio, u64 start, u32 len)
527: {
528: struct btrfs_folio_state *bfs = folio_get_private(folio);
529: unsigned int dirty_bit = subpage_calc_start_bit(fs_info, folio,
530: dirty, start, len);
531: unsigned int fixup_bit = subpage_calc_start_bit(fs_info, folio,
532: fixup, start, len);
533: const unsigned int nbits = len >> fs_info->sectorsize_bits;
534: unsigned long flags;
535: bool marked = false;
536:
537: spin_lock_irqsave(&bfs->lock, flags);
538: for (unsigned int i = 0; i < nbits; i++) {
539: if (test_bit(dirty_bit + i, bfs->bitmaps))
540: continue;
541: set_bit(dirty_bit + i, bfs->bitmaps);
542: set_bit(fixup_bit + i, bfs->bitmaps);
543: marked = true;
544: }
545: if (marked)
546: folio_set_fixup_pending(folio);
547: spin_unlock_irqrestore(&bfs->lock, flags);
548: }
SourceFile:fs/btrfs/subpage.c]
|
| 50/2 |
2026/09/20 00:59 |
llm |
expert |
0m
Model:
Tokens:
input: 41760
output: 29
thoughts: 2183
Thoughts: **Analysis of a Btrfs Subpage Dirtying and Invalidation Scenario**
Okay, so I'm chasing a crash that seems to occur when interacting with Btrfs subpages during truncation. I'm seeing `wait_subpage_spinlock` trigger a BUG, which implies that `folio_test_private(folio)` is false while the code expects it to be true. This happens within the `btrfs` context, specifically in the case of direct I/O.
My initial assumption was that `btrfs_subpage_set_fixup_dirty` was the culprit. However, after careful consideration of the call graph and the conditions under which it's invoked (specifically, the `folio_test_uptodate` check), that doesn't seem likely. The crash isn't happening there.
The crux of the matter appears to be a race condition. It arises when a direct I/O operation (e.g., `aio_read`) is in flight, but is interleaved with truncation of the file. My thinking is, the truncation sequence can lead to `folio_invalidate` being called with a folio that has been dirtied, but loses its private information in between.
The critical sequence, as I see it, is this:
1. A direct I/O read is issued into a user buffer which is part of a file (file A) on the Btrfs filesystem. The pages are pinned and marked up-to-date, dirty, and have private data set. This likely uses `__get_user_pages` and ultimately goes through `btrfs_page_mkwrite` which does the initial set up of the private structure.
2. While the read is in flight, another thread truncates file A. This means, the pages can be removed from page cache, but in a specific truncation scenario (partial truncation), pages can stay in cache.
3. The truncation sequence locks the page, and calls `btrfs_invalidate_folio` which *clears* both the dirty flag and private data from the folio.
4. If the read completes *after* `btrfs_invalidate_folio` clears the dirty and private information, but *before* the page is removed (or in partial truncation cases is not removed), and if `bio_set_pages_dirty` is called after step 3, then we'll have a situation where a folio becomes dirty without private information.
5. If this folio happens to be beyond the new `i_size`, then `extent_writepage` might be invoked on the page after the I/O completion.
6. `extent_writepage` then triggers `folio_invalidate`, which eventually calls `wait_subpage_spinlock`.
7. `wait_subpage_spinlock` asserts that private data is present via `folio_test_private(folio)`, which fails, and triggers the crash because the data was cleared during the truncation, but the page was not fully invalidated.
This scenario explains the apparent inconsistency. The I/O, due to direct I/O, adds the pages to the dirty list (without going through `btrfs_data_dirty_folio`). When the page gets truncated (or partially truncated), `btrfs_invalidate_folio` clears the state, but it is then re-dirtied. So, now there is a dirty page without private, and we end up in the described crash. To fully invalidate, a combination of clearing dirty, clearing private and removing from the mapping's lists is needed. If the page is not removed from the mapping lists, due to partial truncation, then the dirty bits are set by the I/O and we end up in the crash as `extent_writepage` will try to write the page.
|
| 51/2 |
2026/09/20 01:00 |
tool |
grepper |
0m
Args: map[Expression:truncate_inode_partial_folio PathPrefix:mm/]
Results: map[Output:mm/internal.h=626=int truncate_inode_folio(struct address_space *mapping, struct folio *folio);
mm/internal.h:627:bool truncate_inode_partial_folio(struct folio *folio, loff_t start,
mm/internal.h-628- loff_t end);
--
mm/shmem.c=1120=static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,
--
mm/shmem.c-1178- folio_mark_dirty(folio);
mm/shmem.c:1179: if (!truncate_inode_partial_folio(folio, lstart, lend)) {
mm/shmem.c-1180- start = folio_next_index(folio);
--
mm/shmem.c-1192- folio_mark_dirty(folio);
mm/shmem.c:1193: if (!truncate_inode_partial_folio(folio, lstart, lend))
mm/shmem.c-1194- end = folio->index;
--
mm/shmem.c-1231- * If found a large swap entry cross the end or start
mm/shmem.c:1232: * border, skip it as the truncate_inode_partial_folio
mm/shmem.c-1233- * above should have at least zerod its content once.
--
mm/shmem.c-1261- truncate_inode_folio(mapping, folio);
mm/shmem.c:1262: } else if (truncate_inode_partial_folio(folio, lstart, lend)) {
mm/shmem.c-1263- /*
--
mm/truncate.c=180=static int folio_split_or_unmap(struct folio *folio, struct page *split_at,
--
mm/truncate.c-216- */
mm/truncate.c:217:bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
mm/truncate.c-218-{
--
mm/truncate.c=366=void truncate_inode_pages_range(struct address_space *mapping,
--
mm/truncate.c-415- same_folio = lend < folio_next_pos(folio);
mm/truncate.c:416: if (!truncate_inode_partial_folio(folio, lstart, lend)) {
mm/truncate.c-417- start = folio_next_index(folio);
--
mm/truncate.c-429- if (!IS_ERR(folio)) {
mm/truncate.c:430: if (!truncate_inode_partial_folio(folio, lstart, lend))
mm/truncate.c-431- end = folio->index;
]
|
| 52/2 |
2026/09/20 01:00 |
llm |
expert |
0m
Model:
Tokens:
input: 45494
output: 36
thoughts: 21
Thoughts: Here's a first-person summary, tailored for an expert audience, as requested:
**Diving into `truncate_inode_partial_folio`**
Alright, let's take a look at `truncate_inode_partial_folio`. This is where the real meat of the partial folio truncation work is, inside `mm/truncate.c`. We know how important atomic operations are, and where it concerns folios, it must be the same. The potential for data corruption here is incredibly high, so I want to meticulously trace the logic. We're dealing with partial folios, which means we're *not* wiping the entire block. So, what are the key synchronization primitives employed? What are the edge cases accounted for regarding concurrent access and I/O completion? I'm particularly interested in how they handle situations with writeback or dirty pages overlapping the truncation range. Are there any potential subtle race conditions I should be aware of, like a page being freed prematurely or data inconsistencies after truncation? I'm going to carefully review the locking hierarchy, any use of `radix_tree_replace_slot`, and how the folio's state is updated. Let's see if this aligns with my understanding of the larger block layer interactions, and what implications the design has on the overall performance in the presence of concurrent truncation operations. I need to make sure the code is rock-solid.
|
| 53/2 |
2026/09/20 01:00 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/truncate.c Name:truncate_inode_partial_folio]
Results: map[SourceCode: 206: /*
207: * Handle partial folios. The folio may be entirely within the
208: * range if a split has raced with us. If not, we zero the part of the
209: * folio that's within the [start, end] range, and then split the folio if
210: * it's large. split_page_range() will discard pages which now lie beyond
211: * i_size, and we rely on the caller to discard pages which lie within a
212: * newly created hole.
213: *
214: * Returns false if splitting failed so the caller can avoid
215: * discarding the entire folio which is stubbornly unsplit.
216: */
217: bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
218: {
219: loff_t pos = folio_pos(folio);
220: size_t size = folio_size(folio);
221: unsigned int offset, length;
222: struct page *split_at, *split_at2;
223: unsigned int min_order;
224:
225: if (pos < start)
226: offset = start - pos;
227: else
228: offset = 0;
229: if (pos + size <= (u64)end)
230: length = size - offset;
231: else
232: length = end + 1 - pos - offset;
233:
234: folio_wait_writeback(folio);
235: if (length == size) {
236: truncate_inode_folio(folio->mapping, folio);
237: return true;
238: }
239:
240: /*
241: * We may be zeroing pages we're about to discard, but it avoids
242: * doing a complex calculation here, and then doing the zeroing
243: * anyway if the page split fails.
244: */
245: if (!mapping_inaccessible(folio->mapping))
246: folio_zero_range(folio, offset, length);
247:
248: if (folio_needs_release(folio))
249: folio_invalidate(folio, offset, length);
250: if (!folio_test_large(folio))
251: return true;
252:
253: min_order = mapping_min_folio_order(folio->mapping);
254: split_at = folio_page(folio, PAGE_ALIGN_DOWN(offset) / PAGE_SIZE);
255: if (!folio_split_or_unmap(folio, split_at, min_order)) {
256: /*
257: * try to split at offset + length to make sure folios within
258: * the range can be dropped, especially to avoid memory waste
259: * for shmem truncate
260: */
261: struct folio *folio2;
262:
263: if (offset + length == size)
264: goto no_split;
265:
266: split_at2 = folio_page(folio,
267: PAGE_ALIGN_DOWN(offset + length) / PAGE_SIZE);
268: folio2 = page_folio(split_at2);
269:
270: if (!folio_try_get(folio2))
271: goto no_split;
272:
273: if (!folio_test_large(folio2))
274: goto out;
275:
276: if (!folio_trylock(folio2))
277: goto out;
278:
279: /* make sure folio2 is large and does not change its mapping */
280: if (folio_test_large(folio2) &&
281: folio2->mapping == folio->mapping)
282: folio_split_or_unmap(folio2, split_at2, min_order);
283:
284: folio_unlock(folio2);
285: out:
286: folio_put(folio2);
287: no_split:
288: return true;
289: }
290: if (folio_test_dirty(folio))
291: return false;
292: truncate_inode_folio(folio->mapping, folio);
293: return true;
294: }
SourceFile:mm/truncate.c]
|
| 54/2 |
2026/09/20 01:00 |
llm |
expert |
0m
Model:
Tokens:
input: 47169
output: 38
thoughts: 158
Thoughts: **Analyzing Folio Invalidation in Btrfs**
Okay, so I'm looking at this `truncate_inode_partial_folio` function, and it's calling `folio_invalidate(folio, offset, length)` at line 249. Right, and I know that if `folio_needs_release(folio)` is true, and it *is* for btrfs because of the private data, `folio_invalidate` is the next step. So that leads me to `btrfs_invalidate_folio(folio, offset, length)`.
Now, if `offset` is zero and `length` covers the entire `folio_size(folio)`, it clears the private data – that's straightforward. But, hold on a second... this is a *partial* folio we're talking about, so `offset` and `length` won't be the entire folio! I need to re-examine `btrfs_invalidate_folio` again with this partial folio scenario in mind. I need to make sure I understand exactly how it handles that.
|
| 55/2 |
2026/09/20 01:00 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/inode.c FirstLine:7700 LineCount:50]
Results: map[Contents:7700: /*
7701: * The invalidated blocks are going away; drop any fixup blocks among
7702: * them, data included, as they have no space reservation.
7703: */
7704: btrfs_folio_clear_fixup_dirty(fs_info, folio, page_start + offset, length);
7705:
7706: /*
7707: * For subpage case, we have call sites like
7708: * btrfs_punch_hole_lock_range() which passes range not aligned to
7709: * sectorsize.
7710: * If the range doesn't cover the full folio, we don't need to and
7711: * shouldn't clear page extent mapped, as folio->private can still
7712: * record subpage dirty bits for other part of the range.
7713: *
7714: * For cases that invalidate the full folio even the range doesn't
7715: * cover the full folio, like invalidating the last folio, we're
7716: * still safe to wait for ordered extent to finish.
7717: */
7718: if (!(offset == 0 && length == folio_size(folio))) {
7719: btrfs_release_folio(folio, GFP_NOFS);
7720: return;
7721: }
7722:
7723: if (!inode_evicting)
7724: btrfs_lock_extent(tree, page_start, page_end, &cached_state);
7725:
7726: cur = page_start;
7727: while (cur < page_end) {
7728: struct btrfs_ordered_extent *ordered;
7729: u64 range_end;
7730: u32 range_len;
7731: u32 extra_flags = 0;
7732:
7733: ordered = btrfs_lookup_first_ordered_range(inode, cur,
7734: page_end + 1 - cur);
7735: if (!ordered) {
7736: range_end = page_end;
7737: /*
7738: * No ordered extent covering this range, we are safe
7739: * to delete all extent states in the range.
7740: */
7741: extra_flags = EXTENT_CLEAR_ALL_BITS;
7742: goto next;
7743: }
7744: if (ordered->file_offset > cur) {
7745: /*
7746: * There is a range between [cur, oe->file_offset) not
7747: * covered by any ordered extent.
7748: * We are safe to delete all extent states, and handle
7749: * the ordered extent in the next iteration.
]
|
| 56/2 |
2026/09/20 01:00 |
llm |
expert |
0m
Model:
Tokens:
input: 48389
output: 40
thoughts: 43
Thoughts: Here's my take on the thought process, as if it were my own:
**Analyzing Folio Release in Btrfs**
Okay, so we've hit a point where we're releasing a folio, and there's a quick check here. If we're dealing with a partial folio – something not quite whole – we bail out immediately. It uses `btrfs_release_folio(folio, GFP_NOFS)` and returns. That makes sense, keeps things efficient. My next step? I need to dive into `btrfs_release_folio`. I need to fully understand the logic it's employing to handle the release, especially since it's operating with `GFP_NOFS`. No filesystem context, right? Let's trace it and see how it’s managing memory and synchronization in this critical path.
|
| 57/2 |
2026/09/20 01:00 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_release_folio]
Results: map[SourceCode:7650: static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7651: {
7652: if (folio_test_writeback(folio) || folio_test_dirty(folio))
7653: return false;
7654: return __btrfs_release_folio(folio, gfp_flags);
7655: }
SourceFile:fs/btrfs/inode.c]
|
| 58/2 |
2026/09/20 01:00 |
llm |
expert |
0m
Model:
Tokens:
input: 48790
output: 41
thoughts: 11
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**My Btrfs Folio Musings**
Right, let's dive into this `__btrfs_release_folio` function again. Thinking about folio management within Btrfs... the core of it all, really. I'm visualizing the folio as this fundamental data unit, and its lifecycle is *critical* for performance and data integrity. We're talking about releasing it back into the pool, but the devil, as always, is in the details.
What are the critical considerations here? We have to ensure that any associated metadata is properly flushed, any relevant locks are released in the right order to avoid corruption, and that references are correctly decremented. This isn't just a simple free; there's a cascade of dependencies and potential race conditions to manage.
I'm mentally running through the various scenarios: Is this a COW copy-on-write folio? Does it contain data or metadata? Are there any pending I/Os that need to complete? We need to be absolutely sure that no other process is still accessing this folio before we reintroduce it. Cache coherency is the main point of concern, along with the correct handling of all possible states.
I’m picturing the synchronization primitives we're employing – are we using spinlocks, RCU, or something more sophisticated? And how does this interact with the page cache itself? Is this folio shared across multiple processes, or is it isolated? These factors would determine the right approach, and a subtle bug here can lead to a *major* headache.
The whole process has to be atomic and consistent. It's about maintaining data integrity with every single operation. We can't afford to lose data, or worse, introduce a silent corruption that will manifest itself in unexpected behavior down the road. This is all about reliability, and making sure Btrfs does what we expect it to do, when we need it to do it.
So, `__btrfs_release_folio`... a small function with a huge responsibility. It's the lynchpin to proper resource management.
|
| 59/2 |
2026/09/20 01:00 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:__btrfs_release_folio]
Results: map[SourceCode:7640: static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7641: {
7642: if (try_release_extent_mapping(folio, gfp_flags)) {
7643: wait_subpage_spinlock(folio);
7644: clear_folio_extent_mapped(folio);
7645: return true;
7646: }
7647: return false;
7648: }
SourceFile:fs/btrfs/inode.c]
|
| 60/2 |
2026/09/20 01:00 |
llm |
expert |
0m
Model:
Tokens:
input: 49465
output: 40
thoughts: 1832
Thoughts: **Analyzing a Btrfs Writeback Crash**
Okay, so I'm diving into this crash in `wait_subpage_spinlock` within a Btrfs writeback scenario, and the core issue seems to revolve around the state of a folio – specifically, whether its private data is correctly set. Let's break it down:
Initially, my thought process follows a pretty standard writeback path. The crash happens during `btrfs_invalidate_folio`, which is called from `extent_writepage`. `extent_writepage` is reached because a dirty folio is now beyond `i_size`. The issue hinges on a folio being dirty but *not* having its private data set, and that's the part that's causing problems.
The key is that `btrfs_release_folio` *doesn't* clear the private data if the folio is dirty, so `truncate_inode_partial_folio` is not clearing the folio from the page cache. This means the folio stays dirty with its private data. `truncate_inode_partial_folio` can return *without* clearing the folio, if it's dirty. When `extent_writepage` runs, the folio is still in the page cache, dirty, and beyond `i_size`. It calls `folio_invalidate` which then calls `btrfs_invalidate_folio`, which then calls `wait_subpage_spinlock`.
However, the crash in `wait_subpage_spinlock` indicates that `folio_test_private(folio)` is evaluating to false, even though I expect it to be true because of the flow I've described. That's a red flag. The logic indicates that `folio_test_private(folio)` *should* be true.
I've carefully examined the code path. I looked for other places where `__btrfs_release_folio` might have cleared the data, but it won't be called because the folio is dirty. And it shouldn't be a race with reclamation or concurrent invalidation.
Then I considered the source of the folio. It's coming from `aio_read`, meaning it's a page being read from a block device into a user buffer that's mapped to a btrfs file. `__get_user_pages` should fault the page into the page cache, which should trigger `btrfs_page_mkwrite` and set the private data. That then sets the folio's private data! Then `bio_set_pages_dirty` should be marking the pages dirty.
I'm now zeroing in on a scenario where `btrfs_page_mkwrite` *isn't* being called, or is failing in a way that doesn't clear the private data. The failure check on `btrfs_page_mkwrite` returns an error. This won't clear private data. Even if the underlying page fault fails.
Then I recalled the comment in `extent_writepage` itself. It explicitly warns that a folio *can* be marked dirty without the fs knowing about it, a condition the code deems a bug. That triggers a lightbulb, because that means `btrfs_page_mkwrite` might *never* have been called.
I then realized that `__get_user_pages` can be called without write access. If it's *read* access only, the fault will call `btrfs_read_folio`. Does `btrfs_read_folio` set private data? That's the next place to look!
|
| 61/2 |
2026/09/20 01:00 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_read_folio]
Results: map[SourceCode:1411: int btrfs_read_folio(struct file *file, struct folio *folio)
1412: {
1413: struct inode *vfs_inode = folio->mapping->host;
1414: struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1415: const u64 start = folio_pos(folio);
1416: const u64 end = start + folio_size(folio) - 1;
1417: struct extent_state *cached_state = NULL;
1418: struct btrfs_bio_ctrl bio_ctrl = {
1419: .opf = REQ_OP_READ,
1420: .last_em_start = U64_MAX,
1421: };
1422: struct extent_map *em_cached = NULL;
1423: struct fsverity_info *vi = NULL;
1424: int ret;
1425:
1426: assert_folio_range(inode, start, end);
1427: lock_extents_for_read(inode, start, end, &cached_state);
1428: if (folio_pos(folio) < i_size_read(vfs_inode))
1429: vi = fsverity_get_info(vfs_inode);
1430: ret = btrfs_do_readpage(folio, &em_cached, &bio_ctrl, vi);
1431: btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state);
1432:
1433: btrfs_free_extent_map(em_cached);
1434:
1435: /*
1436: * If btrfs_do_readpage() failed we will want to submit the assembled
1437: * bio to do the cleanup.
1438: */
1439: submit_one_bio(&bio_ctrl);
1440: return ret;
1441: }
SourceFile:fs/btrfs/extent_io.c]
|
| 62/2 |
2026/09/20 01:00 |
llm |
expert |
0m
Model:
Tokens:
input: 52672
output: 43
thoughts: 14
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Diving into `btrfs_do_readpage`**
Alright, time to dig in. The immediate focus is `btrfs_do_readpage`. That function is the workhorse of our page fetching mechanism, so it's critical to understand its flow. I need to refresh my memory on where it fits in the broader I/O context. Is this called directly from the VFS, or is it mediated by some intermediate layer? What are the key data structures it manipulates, especially those related to extent mapping and block allocation? Any recent patches impacting that area? Gotta double check the recent merge logs for any potential regressions. Specifically, I'm thinking about those corner cases, like the handling of compressed blocks or extent sharing during read. I'll probably start by tracing a simple read path and then branch out to more complex scenarios. The objective is a clear, concise understanding of what that function does, and how reliably. This is going to be my key focus.
|
| 63/2 |
2026/09/20 01:01 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:btrfs_do_readpage]
Results: map[SourceCode:1046: /*
1047: * basic readpage implementation. Locked extent state structs are inserted
1048: * into the tree that are removed when the IO is done (by the end_io
1049: * handlers)
1050: * XXX JDM: This needs looking at to ensure proper page locking
1051: * return 0 on success, otherwise return error
1052: */
1053: static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,
1054: struct btrfs_bio_ctrl *bio_ctrl,
1055: struct fsverity_info *vi)
1056: {
1057: struct inode *inode = folio->mapping->host;
1058: struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1059: u64 start = folio_pos(folio);
1060: const u64 end = start + folio_size(folio) - 1;
1061: u64 extent_offset;
1062: u64 locked_end;
1063: u64 last_byte = i_size_read(inode);
1064: struct extent_map *em;
1065: int ret = 0;
1066: const size_t blocksize = fs_info->sectorsize;
1067:
1068: if (bio_ctrl->ractl)
1069: locked_end = readahead_pos(bio_ctrl->ractl) + readahead_length(bio_ctrl->ractl) - 1;
1070: else
1071: locked_end = end;
1072:
1073: ret = set_folio_extent_mapped(folio);
1074: if (ret < 0) {
1075: folio_unlock(folio);
1076: return ret;
1077: }
1078:
1079: if (folio_contains(folio, last_byte >> PAGE_SHIFT)) {
1080: size_t zero_offset = offset_in_folio(folio, last_byte);
1081:
1082: if (zero_offset)
1083: folio_zero_range(folio, zero_offset,
1084: folio_size(folio) - zero_offset);
1085: }
1086: bio_ctrl->end_io_func = end_bbio_data_read;
1087: begin_folio_read(fs_info, folio);
1088: for (u64 cur = start; cur <= end; cur += blocksize) {
1089: enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE;
1090: unsigned long pg_offset = offset_in_folio(folio, cur);
1091: bool force_bio_submit = false;
1092: u64 disk_bytenr;
1093: u64 block_start;
1094: u64 em_gen;
1095: unsigned int queued;
1096:
1097: ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
1098: if (cur >= last_byte) {
1099: folio_zero_range(folio, pg_offset, end - cur + 1);
1100: end_folio_read(vi, folio, true, cur, end - cur + 1);
1101: break;
1102: }
1103: if (btrfs_folio_test_uptodate(fs_info, folio, cur, blocksize)) {
1104: end_folio_read(vi, folio, true, cur, blocksize);
1105: continue;
1106: }
1107: /*
1108: * Search extent map for the whole locked range.
1109: * This will allow btrfs_get_extent() to return a larger hole
1110: * when possible.
1111: * This can reduce duplicated btrfs_get_extent() calls for large
1112: * holes.
1113: */
1114: em = get_extent_map(BTRFS_I(inode), folio, cur, locked_end - cur + 1, em_cached);
1115: if (IS_ERR(em)) {
1116: end_folio_read(vi, folio, false, cur, end + 1 - cur);
1117: return PTR_ERR(em);
1118: }
1119: extent_offset = cur - em->start;
1120: BUG_ON(btrfs_extent_map_end(em) <= cur);
1121: BUG_ON(end < cur);
1122:
1123: compress_type = btrfs_extent_map_compression(em);
1124:
1125: /*
1126: * Only expand readahead for extents which are already creating
1127: * the pages anyway in add_ra_bio_pages, which is compressed
1128: * extents in the non subpage case.
1129: */
1130: if (bio_ctrl->ractl &&
1131: !btrfs_is_subpage(fs_info, folio) &&
1132: compress_type != BTRFS_COMPRESS_NONE)
1133: btrfs_readahead_expand(bio_ctrl->ractl, em);
1134:
1135: if (compress_type != BTRFS_COMPRESS_NONE)
1136: disk_bytenr = em->disk_bytenr;
1137: else
1138: disk_bytenr = btrfs_extent_map_block_start(em) + extent_offset;
1139:
1140: if (em->flags & EXTENT_FLAG_PREALLOC)
1141: block_start = EXTENT_MAP_HOLE;
1142: else
1143: block_start = btrfs_extent_map_block_start(em);
1144:
1145: /*
1146: * If we have a file range that points to a compressed extent
1147: * and it's followed by a consecutive file range that points
1148: * to the same compressed extent (possibly with a different
1149: * offset and/or length, so it either points to the whole extent
1150: * or only part of it), we must make sure we do not submit a
1151: * single bio to populate the folios for the 2 ranges because
1152: * this makes the compressed extent read zero out the folios
1153: * belonging to the 2nd range. Imagine the following scenario:
1154: *
1155: * File layout
1156: * [0 - 8K] [8K - 24K]
1157: * | |
1158: * | |
1159: * points to extent X, points to extent X,
1160: * offset 4K, length of 8K offset 0, length 16K
1161: *
1162: * [extent X, compressed length = 4K uncompressed length = 16K]
1163: *
1164: * If the bio to read the compressed extent covers both ranges,
1165: * it will decompress extent X into the folios belonging to the
1166: * first range and then it will stop, zeroing out the remaining
1167: * folios that belong to the other range that points to extent X.
1168: * So here we make sure we submit 2 bios, one for the first
1169: * range and another one for the third range. Both will target
1170: * the same physical extent from disk, but we can't currently
1171: * make the compressed bio endio callback populate the folios
1172: * for both ranges because each compressed bio is tightly
1173: * coupled with a single extent map, and each range can have
1174: * an extent map with a different offset value relative to the
1175: * uncompressed data of our extent and different lengths. This
1176: * is a corner case so we prioritize correctness over
1177: * non-optimal behavior (submitting 2 bios for the same extent).
1178: */
1179: if (compress_type != BTRFS_COMPRESS_NONE &&
1180: bio_ctrl->last_em_start != U64_MAX &&
1181: bio_ctrl->last_em_start != em->start)
1182: force_bio_submit = true;
1183:
1184: bio_ctrl->last_em_start = em->start;
1185:
1186: em_gen = em->generation;
1187: btrfs_free_extent_map(em);
1188: em = NULL;
1189:
1190: /* we've found a hole, just zero and go on */
1191: if (block_start == EXTENT_MAP_HOLE) {
1192: folio_zero_range(folio, pg_offset, blocksize);
1193: end_folio_read(vi, folio, true, cur, blocksize);
1194: continue;
1195: }
1196: /* the get_extent function already copied into the folio */
1197: if (block_start == EXTENT_MAP_INLINE) {
1198: end_folio_read(vi, folio, true, cur, blocksize);
1199: continue;
1200: }
1201:
1202: if (bio_ctrl->compress_type != compress_type) {
1203: submit_one_bio(bio_ctrl);
1204: bio_ctrl->compress_type = compress_type;
1205: }
1206:
1207: if (force_bio_submit)
1208: submit_one_bio(bio_ctrl);
1209: queued = submit_extent_folio(bio_ctrl, disk_bytenr, folio, blocksize,
1210: pg_offset, em_gen);
1211: /* Read submission should not fail. */
1212: ASSERT(queued == blocksize);
1213: }
1214: return 0;
1215: }
SourceFile:fs/btrfs/extent_io.c]
|
| 64/2 |
2026/09/20 01:01 |
llm |
expert |
0m
Model:
Tokens:
input: 55869
output: 39
thoughts: 433
Thoughts: **My Btrfs Page Data Musings**
Okay, so `btrfs_do_readpage` and those other functions, like `btrfs_page_mkwrite` and `btrfs_write_begin`, all end up calling `set_folio_extent_mapped(folio)`. That's the key. So, any page that's *read* from disk, or any page *written* by btrfs code, automatically gets associated with private data. That seems pretty straightforward: any page instantiated *by* btrfs, has private data.
But then, how could a page *not* have private data? What if the page came from shmem or some other subsystem? No, this is a btrfs file, so that's not the issue. The only thing I can think of is that maybe `clear_folio_extent_mapped` was called? And as we've seen, `btrfs_invalidate_folio` *does* call `clear_folio_extent_mapped`. Right, so invalidating a folio would clear the private data. That's fine.
But wait, `btrfs_invalidate_folio` also clears the dirty flag! Which means we have a problem. If the dirty flag is cleared, how can the page be written back later? That's when it hit me... what if `bio_set_pages_dirty` is called *after* `btrfs_invalidate_folio`? If that happens, then `folio_mark_dirty_lock` will be called, which will re-set the dirty flag on the folio! And importantly, it will call `btrfs_data_dirty_folio`, which sets the fixup flag (if it's a subpage).
But wait, hold on... if `folio->mapping` is NULL (because the page was removed from the page cache, likely by a truncate), then `folio_mark_dirty_lock` *won't* call `btrfs_data_dirty_folio`! That's it! Let me check the code for `folio_mark_dirty_lock` and confirm that behavior. That's the key to understanding this.
|
| 65/2 |
2026/09/20 01:01 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/page-writeback.c Name:folio_mark_dirty_lock]
Results: map[SourceCode:2797: /*
2798: * folio_mark_dirty() is racy if the caller has no reference against
2799: * folio->mapping->host, and if the folio is unlocked. This is because another
2800: * CPU could truncate the folio off the mapping and then free the mapping.
2801: *
2802: * Usually, the folio _is_ locked, or the caller is a user-space process which
2803: * holds a reference on the inode by having an open file.
2804: *
2805: * In other cases, the folio should be locked before running folio_mark_dirty().
2806: */
2807: bool folio_mark_dirty_lock(struct folio *folio)
2808: {
2809: bool ret;
2810:
2811: folio_lock(folio);
2812: ret = folio_mark_dirty(folio);
2813: folio_unlock(folio);
2814: return ret;
2815: }
SourceFile:mm/page-writeback.c]
|
| 66/2 |
2026/09/20 01:01 |
llm |
expert |
0m
Model:
Tokens:
input: 57125
output: 37
thoughts: 8
Thoughts: Okay, here's my attempt at summarizing the thought process, as if it were my own, assuming the user is an expert.
**My Analysis of `folio_mark_dirty`**
Alright, let's break down this `folio_mark_dirty` situation. My first instinct, as always, is to think about the broader implications within the system. Is this a critical piece of the memory management strategy? What triggers this call? Understanding the context is key. It's probably related to tracking modifications within a specific data structure, perhaps a page or a block of memory, within a larger folio structure.
I need to quickly assess how efficiently this function operates. Is it a frequent call? Is it optimized for common use cases, or are there potential bottlenecks here? I want to think about the potential for contention if multiple threads might be interacting with the same folio simultaneously. A quick scan of the surrounding code should provide some context. I'll need to check how the dirty flag is used; is it just a single bit? or does it store detailed information that would allow for more efficient memory handling or synchronization?
From a design perspective, how is the "dirty" state communicated to other components? Is it a simple flag, or does it trigger some sort of event, perhaps a writeback or cache invalidation procedure? Does the way it handles dirty marks affect the efficiency of other components? If so, what are the trade-offs? This could be something simple, or it may have wide-reaching effects.
I'm now thinking about the potential for bugs. Are there any race conditions? Could the flag be set incorrectly, leading to data loss or corruption? I'll need to look for potential areas of concern, such as atomicity, memory barriers, and locking mechanisms. I'll also want to look at any error handling in this area and the cases which it handles.
Finally, I'll need to remember to think about debugging this. Is it straightforward to diagnose issues related to this function? Are there any tools or techniques that I should employ to observe its behavior in practice? This is going to depend on how this function impacts other functions.
|
| 67/2 |
2026/09/20 01:01 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/page-writeback.c Name:folio_mark_dirty]
Results: map[SourceCode:2754: /**
2755: * folio_mark_dirty - Mark a folio as being modified.
2756: * @folio: The folio.
2757: *
2758: * The folio may not be truncated while this function is running.
2759: * Holding the folio lock is sufficient to prevent truncation, but some
2760: * callers cannot acquire a sleeping lock. These callers instead hold
2761: * the page table lock for a page table which contains at least one page
2762: * in this folio. Truncation will block on the page table lock as it
2763: * unmaps pages before removing the folio from its mapping.
2764: *
2765: * .. DANGER::
2766: * Do not use this on a folio obtained from a function like
2767: * get_user_pages_fast() without holding appropriate locks; you might want to
2768: * use set_page_dirty_lock() or folio_mark_dirty_lock() instead.
2769: *
2770: * Return: True if the folio was newly dirtied, false if it was already dirty.
2771: */
2772: bool folio_mark_dirty(struct folio *folio)
2773: {
2774: struct address_space *mapping = folio_mapping(folio);
2775:
2776: if (likely(mapping)) {
2777: /*
2778: * readahead/folio_deactivate could remain
2779: * PG_readahead/PG_reclaim due to race with folio_end_writeback
2780: * About readahead, if the folio is written, the flags would be
2781: * reset. So no problem.
2782: * About folio_deactivate, if the folio is redirtied,
2783: * the flag will be reset. So no problem. but if the
2784: * folio is used by readahead it will confuse readahead
2785: * and make it restart the size rampup process. But it's
2786: * a trivial problem.
2787: */
2788: if (folio_test_reclaim(folio))
2789: folio_clear_reclaim(folio);
2790: return mapping->a_ops->dirty_folio(mapping, folio);
2791: }
2792:
2793: return noop_dirty_folio(mapping, folio);
2794: }
SourceFile:mm/page-writeback.c]
|
| 68/2 |
2026/09/20 01:01 |
llm |
expert |
0m
Model:
Tokens:
input: 58323
output: 33
thoughts: 1708
Thoughts: **Analyzing a Btrfs Folio Invalidation Crash**
Okay, here's what I've got so far, working through this Btrfs folio issue. It seems like the core of the problem lies in a crash within `wait_subpage_spinlock` during folio invalidation, specifically when `folio_test_private(folio)` is false, and therefore also `folio_get_private(folio)` returns NULL. Let's trace it through.
The initial thought process is around the function `noop_dirty_folio`, and how that interacts when `mapping` is NULL. I have to discard this, because if `mapping` is NULL, the folio is not in the page cache, and this means it won't get to `extent_writepage`, which is essential. So let's look at when `mapping` is not NULL.
Now, if the folio *is* in the page cache (meaning `mapping` is not NULL), marking it dirty calls `btrfs_data_dirty_folio`. This then cascades down to `btrfs_folio_set_fixup_dirty`, which, under normal circumstances, would lead to `btrfs_subpage_set_fixup_dirty`. This is where the private data comes into play. If `folio_test_private(folio)` is false here, and that can happen when `folio_get_private(folio)` is NULL, `btrfs_subpage_set_fixup_dirty` would normally crash. But that can't be where it's crashing. Wait, that's because `btrfs_subpage_set_fixup_dirty` is only called if `folio_test_uptodate(folio)` is true. If not, `filemap_dirty_folio` is called.
The issue is that if `folio_test_uptodate(folio)` is false, then we end up in `filemap_dirty_folio` which marks it dirty. It then triggers writeback to `extent_writepage` and so the folio is beyond `i_size`. That causes a call to `folio_invalidate` which then calls `btrfs_invalidate_folio`, which calls `wait_subpage_spinlock`, and that is where the assertion fails!
So, the crash happens during invalidation, and we know `folio_test_uptodate(folio)` must be false, so it isn't in a valid state, it isn't uptodate, doesn't have private data, and is marked dirty because `filemap_dirty_folio` was called. Then, when it is beyond `i_size` in the page cache, during writeback, it crashes during `folio_invalidate`!
So, let's nail down these conditions: the folio must be in the page cache, NOT uptodate, have NO private data, be dirty, and beyond `i_size`. What can create these conditions?
If we have a user `mmap` call followed by `aio_read`, it could involve pages beyond the size of the file. However, `__get_user_pages` fails if the file offset is beyond `i_size`, so that path is a dead end.
I considered `btrfs_read_folio`, but this always sets private data and marks the page as uptodate during a disk read. That path can't be it. Then the question is, how does a folio get instantiated? `btrfs_write_begin` does set the private data.
Okay, let's think about `truncate_inode_partial_folio`. That's where things start to get interesting. It calls `folio_invalidate` and `btrfs_invalidate_folio` so it is the path we want. Let's see if this clears private data and *doesn't* remove the folio from the page cache. If the folio is *partially* within the range, it zeros it, calls `folio_invalidate`.
It returns `true` if it's *not* dirty, and the folio is truncated. That calls `truncate_inode_folio` which removes it. Okay, but if it is *partial*, and not dirty, it doesn't remove the folio.
But wait! If it isn't dirty, it calls `truncate_inode_folio`, which DOES remove it.
If a partial folio is *dirty*, then it doesn't call truncate, which allows it to remain.
But wait, if we go down the `folio_invalidate` path, if the folio is NOT dirty, it calls `__btrfs_release_folio` which calls `clear_folio_extent_mapped`, which *clears* private data.
So, if we have a dirty, partially truncated folio, it can
|
| 69/2 |
2026/09/20 01:01 |
tool |
grepper |
0m
Args: map[Expression:clear_folio_extent_mapped PathPrefix:fs/btrfs/]
Results: map[Output:fs/btrfs/extent_io.c=953=int set_folio_extent_mapped(struct folio *folio)
--
fs/btrfs/extent_io.c-970-
fs/btrfs/extent_io.c:971:void clear_folio_extent_mapped(struct folio *folio)
fs/btrfs/extent_io.c-972-{
--
fs/btrfs/extent_io.h=289=int set_folio_extent_mapped(struct folio *folio);
fs/btrfs/extent_io.h:290:void clear_folio_extent_mapped(struct folio *folio);
fs/btrfs/extent_io.h-291-
--
fs/btrfs/inode.c=7640=static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
--
fs/btrfs/inode.c-7643- wait_subpage_spinlock(folio);
fs/btrfs/inode.c:7644: clear_folio_extent_mapped(folio);
fs/btrfs/inode.c-7645- return true;
--
fs/btrfs/inode.c=7672=static void btrfs_invalidate_folio(struct folio *folio, size_t offset,
--
fs/btrfs/inode.c-7833- __btrfs_release_folio(folio, GFP_NOFS);
fs/btrfs/inode.c:7834: clear_folio_extent_mapped(folio);
fs/btrfs/inode.c-7835-}
]
|
| 70/2 |
2026/09/20 01:01 |
llm |
expert |
0m
Model:
Tokens:
input: 61514
output: 37
thoughts: 221
Thoughts: **My Invalidation Conundrum**
Okay, so I'm focusing on folio invalidation in the Btrfs context. Right now, it looks like the key functions involved are `__btrfs_release_folio` and `btrfs_invalidate_folio`. And I see that `btrfs_invalidate_folio` is directly called by `folio_invalidate`.
`folio_invalidate` itself gets called from a few places. The usual suspects: `truncate_inode_partial_folio`, `invalidate_inode_pages2_range`, and even `extent_writepage` when dealing with data past the file size (`i_size`). Of course, it is also triggered from `btrfs_punch_hole`.
Now, here's where it gets interesting. I'm starting to think through the case where `invalidate_inode_pages2_range` gets invoked. It's calling `folio_invalidate` with the full folio size. That, in turn, calls `btrfs_invalidate_folio`, which is responsible for clearing the private data associated with the folio. No problems so far.
But then, `invalidate_inode_pages2_range` tries to *remove* the folio from the page cache. And what happens if it *fails* to remove it? Say, because the folio is pinned by a Get User Pages (GUP) call? It just *leaves it there*! The folio is invalidated, but it’s still in the cache.
Aha! This is a potential data integrity issue! I really need to drill down into `invalidate_inode_pages2_range` now to see what's happening.
|
| 71/2 |
2026/09/20 01:01 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/truncate.c Name:invalidate_inode_pages2_range]
Results: map[SourceCode: 665: /**
666: * invalidate_inode_pages2_range - remove range of pages from an address_space
667: * @mapping: the address_space
668: * @start: the page offset 'from' which to invalidate
669: * @end: the page offset 'to' which to invalidate (inclusive)
670: *
671: * Any pages which are found to be mapped into pagetables are unmapped prior to
672: * invalidation.
673: *
674: * Return: -EBUSY if any pages could not be invalidated.
675: */
676: int invalidate_inode_pages2_range(struct address_space *mapping,
677: pgoff_t start, pgoff_t end)
678: {
679: pgoff_t indices[FOLIO_BATCH_SIZE];
680: struct folio_batch fbatch;
681: pgoff_t index;
682: int i;
683: int ret = 0;
684: int ret2 = 0;
685: int did_range_unmap = 0;
686:
687: if (mapping_empty(mapping))
688: return 0;
689:
690: folio_batch_init(&fbatch);
691: index = start;
692: while (find_get_entries(mapping, &index, end, &fbatch, indices)) {
693: bool xa_has_values = false;
694: int nr = folio_batch_count(&fbatch);
695:
696: for (i = 0; i < nr; i++) {
697: struct folio *folio = fbatch.folios[i];
698:
699: /* We rely upon deletion not changing folio->index */
700:
701: if (xa_is_value(folio)) {
702: xa_has_values = true;
703: if (dax_mapping(mapping) &&
704: !dax_invalidate_mapping_entry_sync(mapping, indices[i]))
705: ret = -EBUSY;
706: continue;
707: }
708:
709: if (!did_range_unmap && folio_mapped(folio)) {
710: /*
711: * If folio is mapped, before taking its lock,
712: * zap the rest of the file in one hit.
713: */
714: unmap_mapping_pages(mapping, indices[i],
715: (1 + end - indices[i]), false);
716: did_range_unmap = 1;
717: }
718:
719: folio_lock(folio);
720: if (unlikely(folio->mapping != mapping)) {
721: folio_unlock(folio);
722: continue;
723: }
724: VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
725: folio_wait_writeback(folio);
726: ret2 = folio_unmap_invalidate(mapping, folio, GFP_KERNEL);
727: if (ret2 < 0)
728: ret = ret2;
729: folio_unlock(folio);
730: }
731:
732: if (xa_has_values)
733: clear_shadow_entries(mapping, indices[0], indices[nr-1]);
734:
735: folio_batch_remove_exceptionals(&fbatch);
736: folio_batch_release(&fbatch);
737: cond_resched();
738: }
739: /*
740: * For DAX we invalidate page tables after invalidating page cache. We
741: * could invalidate page tables while invalidating each entry however
742: * that would be expensive. And doing range unmapping before doesn't
743: * work as we have no cheap way to find whether page cache entry didn't
744: * get remapped later.
745: */
746: if (dax_mapping(mapping)) {
747: unmap_mapping_pages(mapping, start, end - start + 1, false);
748: }
749: return ret;
750: }
SourceFile:mm/truncate.c]
|
| 72/2 |
2026/09/20 01:01 |
llm |
expert |
0m
Model:
Tokens:
input: 63428
output: 35
thoughts: 35
Thoughts: Here's my analysis of the provided snippet, as though it were my train of thought:
**Dissecting folio_unmap_invalidate**
Okay, so `folio_unmap_invalidate`...right, it seems to be handling the unmapping and invalidation of a folio. The first thing that jumps out is the call to `folio_invalidate`. That makes sense; unmapping usually implies an invalidation step to ensure the data is no longer accessible. But then, it also tries to remove the folio. Hmm, so it's not just invalidating the data, it's actually getting rid of the backing store itself, it seems like the folio. That makes me want to dig deeper into the control flow and understand the specific semantics here. I need to understand what state the system is in when this function is called. Are there any outstanding references, locks, or other dependencies that need to be addressed before removing a folio? I should probably check that call to `folio_invalidate` and see exactly what it's doing.
|
| 73/2 |
2026/09/20 01:01 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/truncate.c Name:folio_unmap_invalidate]
Results: map[SourceCode: 615: /*
616: * This is like mapping_evict_folio(), except it ignores the folio's
617: * refcount. We do this because invalidate_inode_pages2() needs stronger
618: * invalidation guarantees, and cannot afford to leave folios behind because
619: * shrink_folio_list() has a temp ref on them, or because they're transiently
620: * sitting in the folio_add_lru() caches.
621: */
622: int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio,
623: gfp_t gfp)
624: {
625: void (*free_folio)(struct folio *);
626: int ret;
627:
628: VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
629:
630: if (folio_mapped(folio))
631: unmap_mapping_folio(folio);
632: BUG_ON(folio_mapped(folio));
633:
634: ret = folio_launder(mapping, folio);
635: if (ret)
636: return ret;
637: if (folio->mapping != mapping)
638: return -EBUSY;
639: if (!filemap_release_folio(folio, gfp))
640: return -EBUSY;
641:
642: spin_lock(&mapping->host->i_lock);
643: xa_lock_irq(&mapping->i_pages);
644: if (folio_test_dirty(folio))
645: goto failed;
646:
647: BUG_ON(folio_has_private(folio));
648: __filemap_remove_folio(folio, NULL);
649: xa_unlock_irq(&mapping->i_pages);
650: if (mapping_shrinkable(mapping))
651: inode_lru_list_add(mapping->host);
652: free_folio = mapping->a_ops->free_folio;
653: spin_unlock(&mapping->host->i_lock);
654:
655: if (free_folio)
656: free_folio(folio);
657: folio_put_refs(folio, folio_nr_pages(folio));
658: return 1;
659: failed:
660: xa_unlock_irq(&mapping->i_pages);
661: spin_unlock(&mapping->host->i_lock);
662: return -EBUSY;
663: }
SourceFile:mm/truncate.c]
|
| 74/2 |
2026/09/20 01:01 |
llm |
expert |
0m
Model:
Tokens:
input: 64499
output: 32
thoughts: 584
Thoughts: **Analyzing Folio Release and Private Data Management**
Okay, let me break this down. My initial thought process focused on `folio_unmap_invalidate` and its interactions. It calls `filemap_release_folio`, which in turn calls `btrfs_release_folio`. I saw that `btrfs_release_folio` *can* clear private data if it succeeds. But, then `folio_unmap_invalidate` checks if the folio is dirty and, if so, returns `-EBUSY`. Wait, that can't be right! If the folio *is* dirty, `btrfs_release_folio` itself returns `false` due to that condition right there in the code: `if (folio_test_writeback(folio) || folio_test_dirty(folio)) return false;` So if it *is* dirty, we get the return of `-EBUSY`, and the folio's private data is safe. Got it.
But let's go the other way. If the folio is *not* dirty when `filemap_release_folio` is called, `btrfs_release_folio` *does* call `__btrfs_release_folio`, clearing private data, and returning `true`. `filemap_release_folio` then returns `true`. Then, `folio_unmap_invalidate` checks if it's dirty, sees that it's not, and *removes* the folio from the page cache. Essentially, it clears private data and removes the folio from the page cache. Makes perfect sense.
Now, the real question: Is there any path where `filemap_release_folio` *succeeds*, clearing private data, but the folio *remains* in the page cache? I realized `folio_unmap_invalidate` isn't the only entry point. What about calls outside of this function? `try_to_free_buffers` comes to mind. It also calls `filemap_release_folio`. So does memory reclaim! Specifically, the `shrink_folio_list` function. `try_to_free_buffers` calls `filemap_release_folio`, and if that succeeds, it clears private data. But `shrink_folio_list` attempts to remove the folio. Aha! If `shrink_folio_list` *fails* to remove it – say, because of an elevated reference count from a GUP operation - it *leaves* the folio in the page cache even after the private data is cleared! I need to check `shrink_folio_list` carefully... that's the key.
|
| 75/2 |
2026/09/20 01:01 |
tool |
grepper |
0m
Args: map[Expression:filemap_release_folio PathPrefix:mm/vmscan.c]
Results: map[Output:mm/vmscan.c=1058=static unsigned int shrink_folio_list(struct list_head *folio_list,
--
mm/vmscan.c-1442- * We do this even if the folio is dirty.
mm/vmscan.c:1443: * filemap_release_folio() does not perform I/O, but it
mm/vmscan.c-1444- * is possible for a folio to have the dirty flag set,
--
mm/vmscan.c-1447- * with bh_submit(). ext3 will do this, as well as
mm/vmscan.c:1448: * the blockdev mapping. filemap_release_folio() will
mm/vmscan.c-1449- * discover that cleanness and will drop the buffers
--
mm/vmscan.c-1460- if (folio_needs_release(folio)) {
mm/vmscan.c:1461: if (!filemap_release_folio(folio, sc->gfp_mask))
mm/vmscan.c-1462- goto activate_locked;
--
mm/vmscan.c=2065=static void shrink_active_list(unsigned long nr_to_scan,
--
mm/vmscan.c-2108- folio_trylock(folio)) {
mm/vmscan.c:2109: filemap_release_folio(folio, 0);
mm/vmscan.c-2110- folio_unlock(folio);
]
|
| 76/2 |
2026/09/20 01:01 |
llm |
expert |
0m
Model:
Tokens:
input: 66063
output: 35
thoughts: 19
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Investigating `shrink_folio_list`**
Okay, time to dive in. My initial thought is to pinpoint the location of `shrink_folio_list` within the code, specifically around line 1460. That's a decent starting point; it gives me a frame of reference. I'm immediately assuming there's likely an issue or an optimization opportunity related to how that function is interacting with folio lists. Given that line number, it's probably buried deep within the core logic, possibly a performance-critical area or a process that has accumulated complexity. I'm anticipating that understanding its context within the larger data flow will be key. Time to get my hands dirty and start tracing execution.
|
| 77/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/vmscan.c Name:shrink_folio_list]
Results: map[SourceCode:1055: /*
1056: * shrink_folio_list() returns the number of reclaimed pages
1057: */
1058: static unsigned int shrink_folio_list(struct list_head *folio_list,
1059: struct pglist_data *pgdat, struct scan_control *sc,
1060: struct reclaim_stat *stat, bool ignore_references,
1061: struct mem_cgroup *memcg)
1062: {
1063: struct folio_batch free_folios;
1064: LIST_HEAD(ret_folios);
1065: LIST_HEAD(demote_folios);
1066: unsigned int nr_reclaimed = 0, nr_demoted = 0;
1067: unsigned int pgactivate = 0;
1068: bool do_demote_pass;
1069: struct swap_io_ctx ctx = {};
1070:
1071: folio_batch_init(&free_folios);
1072: memset(stat, 0, sizeof(*stat));
1073: cond_resched();
1074: do_demote_pass = can_demote(pgdat->node_id, sc, memcg);
1075:
1076: retry:
1077: while (!list_empty(folio_list)) {
1078: struct address_space *mapping;
1079: struct folio *folio;
1080: enum folio_references references = FOLIOREF_RECLAIM;
1081: bool dirty, writeback;
1082: unsigned int nr_pages;
1083:
1084: cond_resched();
1085:
1086: folio = lru_to_folio(folio_list);
1087: list_del(&folio->lru);
1088:
1089: if (!folio_trylock(folio))
1090: goto keep;
1091:
1092: if (folio_contain_hwpoisoned_page(folio)) {
1093: /*
1094: * unmap_poisoned_folio() can't handle large
1095: * folio, just skip it. memory_failure() will
1096: * handle it if the UCE is triggered again.
1097: */
1098: if (folio_test_large(folio))
1099: goto keep_locked;
1100:
1101: unmap_poisoned_folio(folio, folio_pfn(folio), false);
1102: folio_unlock(folio);
1103: folio_put(folio);
1104: continue;
1105: }
1106:
1107: VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
1108:
1109: nr_pages = folio_nr_pages(folio);
1110:
1111: /* Account the number of base pages */
1112: sc->nr_scanned += nr_pages;
1113:
1114: if (unlikely(!folio_evictable(folio)))
1115: goto activate_locked;
1116:
1117: if (!sc->may_unmap && folio_mapped(folio))
1118: goto keep_locked;
1119:
1120: /*
1121: * The number of dirty pages determines if a node is marked
1122: * reclaim_congested. kswapd will stall and start writing
1123: * folios if the tail of the LRU is all dirty unqueued folios.
1124: */
1125: folio_check_dirty_writeback(folio, &dirty, &writeback);
1126: if (dirty || writeback)
1127: stat->nr_dirty += nr_pages;
1128:
1129: if (dirty && !writeback)
1130: stat->nr_unqueued_dirty += nr_pages;
1131:
1132: /*
1133: * Treat this folio as congested if folios are cycling
1134: * through the LRU so quickly that the folios marked
1135: * for immediate reclaim are making it to the end of
1136: * the LRU a second time.
1137: */
1138: if (writeback && folio_test_reclaim(folio))
1139: stat->nr_congested += nr_pages;
1140:
1141: /*
1142: * If a folio at the tail of the LRU is under writeback, there
1143: * are three cases to consider.
1144: *
1145: * 1) If reclaim is encountering an excessive number
1146: * of folios under writeback and this folio has both
1147: * the writeback and reclaim flags set, then it
1148: * indicates that folios are being queued for I/O but
1149: * are being recycled through the LRU before the I/O
1150: * can complete. Waiting on the folio itself risks an
1151: * indefinite stall if it is impossible to writeback
1152: * the folio due to I/O error or disconnected storage
1153: * so instead note that the LRU is being scanned too
1154: * quickly and the caller can stall after the folio
1155: * list has been processed.
1156: *
1157: * 2) Global or new memcg reclaim encounters a folio that is
1158: * not marked for immediate reclaim, or the caller does not
1159: * have __GFP_FS (or __GFP_IO if it's simply going to swap,
1160: * not to fs), or the folio belongs to a mapping where
1161: * waiting on writeback during reclaim may lead to a deadlock.
1162: * In this case mark the folio for immediate reclaim and
1163: * continue scanning.
1164: *
1165: * Require may_enter_fs() because we would wait on fs, which
1166: * may not have submitted I/O yet. And the loop driver might
1167: * enter reclaim, and deadlock if it waits on a folio for
1168: * which it is needed to do the write (loop masks off
1169: * __GFP_IO|__GFP_FS for this reason); but more thought
1170: * would probably show more reasons.
1171: *
1172: * 3) Legacy memcg encounters a folio that already has the
1173: * reclaim flag set. memcg does not have any dirty folio
1174: * throttling so we could easily OOM just because too many
1175: * folios are in writeback and there is nothing else to
1176: * reclaim. Wait for the writeback to complete.
1177: *
1178: * In cases 1) and 2) we activate the folios to get them out of
1179: * the way while we continue scanning for clean folios on the
1180: * inactive list and refilling from the active list. The
1181: * observation here is that waiting for disk writes is more
1182: * expensive than potentially causing reloads down the line.
1183: * Since they're marked for immediate reclaim, they won't put
1184: * memory pressure on the cache working set any longer than it
1185: * takes to write them to disk.
1186: */
1187: if (folio_test_writeback(folio)) {
1188: mapping = folio_mapping(folio);
1189:
1190: /* Case 1 above */
1191: if (current_is_kswapd() &&
1192: folio_test_reclaim(folio) &&
1193: test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
1194: stat->nr_immediate += nr_pages;
1195: goto activate_locked;
1196:
1197: /* Case 2 above */
1198: } else if (writeback_throttling_sane(sc) ||
1199: !folio_test_reclaim(folio) ||
1200: !may_enter_fs(folio, sc->gfp_mask) ||
1201: (mapping &&
1202: mapping_writeback_may_deadlock_on_reclaim(mapping))) {
1203: /*
1204: * This is slightly racy -
1205: * folio_end_writeback() might have
1206: * just cleared the reclaim flag, then
1207: * setting the reclaim flag here ends up
1208: * interpreted as the readahead flag - but
1209: * that does not matter enough to care.
1210: * What we do want is for this folio to
1211: * have the reclaim flag set next time
1212: * memcg reclaim reaches the tests above,
1213: * so it will then wait for writeback to
1214: * avoid OOM; and it's also appropriate
1215: * in global reclaim.
1216: */
1217: folio_set_reclaim(folio);
1218: stat->nr_writeback += nr_pages;
1219: goto activate_locked;
1220:
1221: /* Case 3 above */
1222: } else {
1223: folio_unlock(folio);
1224: folio_wait_writeback(folio);
1225: /* then go back and try same folio again */
1226: list_add_tail(&folio->lru, folio_list);
1227: continue;
1228: }
1229: }
1230:
1231: if (!ignore_references)
1232: references = folio_check_references(folio, sc);
1233:
1234: switch (references) {
1235: case FOLIOREF_ACTIVATE:
1236: goto activate_locked;
1237: case FOLIOREF_KEEP:
1238: stat->nr_ref_keep += nr_pages;
1239: goto keep_locked;
1240: case FOLIOREF_RECLAIM:
1241: ; /* try to reclaim the folio below */
1242: }
1243:
1244: /*
1245: * Before reclaiming the folio, try to relocate
1246: * its contents to another node.
1247: */
1248: if (do_demote_pass &&
1249: (thp_migration_supported() || !folio_test_large(folio))) {
1250: list_add(&folio->lru, &demote_folios);
1251: folio_unlock(folio);
1252: continue;
1253: }
1254:
1255: /*
1256: * Anonymous process memory has backing store?
1257: * Try to allocate it some swap space here.
1258: * Lazyfree folio could be freed directly
1259: */
1260: if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
1261: !folio_test_swapcache(folio)) {
1262: if (!(sc->gfp_mask & __GFP_IO))
1263: goto keep_locked;
1264: if (folio_maybe_dma_pinned(folio))
1265: goto keep_locked;
1266: if (folio_test_large(folio)) {
1267: /* cannot split folio, skip it */
1268: if (folio_expected_ref_count(folio) !=
1269: folio_ref_count(folio) - 1)
1270: goto activate_locked;
1271: /*
1272: * Split partially mapped folios right away.
1273: * We can free the unmapped pages without IO.
1274: */
1275: if (data_race(!list_empty(&folio->_deferred_list) &&
1276: folio_test_partially_mapped(folio)) &&
1277: split_folio_to_list(folio, folio_list))
1278: goto activate_locked;
1279: }
1280: if (folio_alloc_swap(folio)) {
1281: int __maybe_unused order = folio_order(folio);
1282:
1283: if (!folio_test_large(folio))
1284: goto activate_locked_split;
1285: /* Fallback to swap normal pages */
1286: if (split_folio_to_list(folio, folio_list))
1287: goto activate_locked;
1288: #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1289: if (nr_pages >= HPAGE_PMD_NR) {
1290: count_memcg_folio_events(folio,
1291: THP_SWPOUT_FALLBACK, 1);
1292: count_vm_event(THP_SWPOUT_FALLBACK);
1293: }
1294: #endif
1295: count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK);
1296: if (folio_alloc_swap(folio))
1297: goto activate_locked_split;
1298: }
1299: /*
1300: * Normally the folio will be dirtied in unmap because
1301: * its pte should be dirty. A special case is MADV_FREE
1302: * page. The page's pte could have dirty bit cleared but
1303: * the folio's SwapBacked flag is still set because
1304: * clearing the dirty bit and SwapBacked flag has no
1305: * lock protected. For such folio, unmap will not set
1306: * dirty bit for it, so folio reclaim will not write the
1307: * folio out. This can cause data corruption when the
1308: * folio is swapped in later. Always setting the dirty
1309: * flag for the folio solves the problem.
1310: */
1311: folio_mark_dirty(folio);
1312: }
1313:
1314: /*
1315: * If the folio was split above, the tail pages will make
1316: * their own pass through this function and be accounted
1317: * then.
1318: */
1319: if ((nr_pages > 1) && !folio_test_large(folio)) {
1320: sc->nr_scanned -= (nr_pages - 1);
1321: nr_pages = 1;
1322: }
1323:
1324: /*
1325: * The folio is mapped into the page tables of one or more
1326: * processes. Try to unmap it here.
1327: */
1328: if (folio_mapped(folio)) {
1329: enum ttu_flags flags = TTU_BATCH_FLUSH;
1330: bool was_swapbacked = folio_test_swapbacked(folio);
1331:
1332: if (folio_test_pmd_mappable(folio))
1333: flags |= TTU_SPLIT_HUGE_PMD;
1334: /*
1335: * Without TTU_SYNC, try_to_unmap will only begin to
1336: * hold PTL from the first present PTE within a large
1337: * folio. Some initial PTEs might be skipped due to
1338: * races with parallel PTE writes in which PTEs can be
1339: * cleared temporarily before being written new present
1340: * values. This will lead to a large folio is still
1341: * mapped while some subpages have been partially
1342: * unmapped after try_to_unmap; TTU_SYNC helps
1343: * try_to_unmap acquire PTL from the first PTE,
1344: * eliminating the influence of temporary PTE values.
1345: */
1346: if (folio_test_large(folio))
1347: flags |= TTU_SYNC;
1348:
1349: try_to_unmap(folio, flags);
1350: if (folio_mapped(folio)) {
1351: stat->nr_unmap_fail += nr_pages;
1352: if (!was_swapbacked &&
1353: folio_test_swapbacked(folio))
1354: stat->nr_lazyfree_fail += nr_pages;
1355: goto activate_locked;
1356: }
1357: }
1358:
1359: /*
1360: * Folio is unmapped now so it cannot be newly pinned anymore.
1361: * No point in trying to reclaim folio if it is pinned.
1362: * Furthermore we don't want to reclaim underlying fs metadata
1363: * if the folio is pinned and thus potentially modified by the
1364: * pinning process as that may upset the filesystem.
1365: */
1366: if (folio_maybe_dma_pinned(folio))
1367: goto activate_locked;
1368:
1369: mapping = folio_mapping(folio);
1370: if (folio_test_dirty(folio)) {
1371: if (folio_is_file_lru(folio)) {
1372: /*
1373: * Immediately reclaim when written back.
1374: * Similar in principle to folio_deactivate()
1375: * except we already have the folio isolated
1376: * and know it's dirty
1377: */
1378: node_stat_mod_folio(folio, NR_VMSCAN_IMMEDIATE,
1379: nr_pages);
1380: if (!folio_test_reclaim(folio))
1381: folio_set_reclaim(folio);
1382:
1383: goto activate_locked;
1384: }
1385:
1386: if (!may_enter_fs(folio, sc->gfp_mask))
1387: goto keep_locked;
1388: if (!sc->may_writepage)
1389: goto keep_locked;
1390:
1391: /*
1392: * Folio is dirty. Flush the TLB if a writable entry
1393: * potentially exists to avoid CPU writes after I/O
1394: * starts and then write it out here.
1395: */
1396: try_to_unmap_flush_dirty();
1397: switch (pageout(&ctx, mapping, folio, folio_list)) {
1398: case PAGE_KEEP:
1399: goto keep_locked;
1400: case PAGE_ACTIVATE:
1401: /*
1402: * If shmem folio is split when writeback to swap,
1403: * the tail pages will make their own pass through
1404: * this function and be accounted then.
1405: */
1406: if (nr_pages > 1 && !folio_test_large(folio)) {
1407: sc->nr_scanned -= (nr_pages - 1);
1408: nr_pages = 1;
1409: }
1410: goto activate_locked;
1411: case PAGE_SUCCESS:
1412: if (nr_pages > 1 && !folio_test_large(folio)) {
1413: sc->nr_scanned -= (nr_pages - 1);
1414: nr_pages = 1;
1415: }
1416: if (folio_test_writeback(folio))
1417: goto keep;
1418: if (folio_test_dirty(folio))
1419: goto keep;
1420:
1421: /*
1422: * A synchronous write - probably a ramdisk. Go
1423: * ahead and try to reclaim the folio.
1424: */
1425: if (!folio_trylock(folio))
1426: goto keep;
1427: if (folio_test_dirty(folio) ||
1428: folio_test_writeback(folio))
1429: goto keep_locked;
1430: mapping = folio_mapping(folio);
1431: fallthrough;
1432: case PAGE_CLEAN:
1433: ; /* try to free the folio below */
1434: }
1435: }
1436:
1437: /*
1438: * If the folio has buffers, try to free the buffer
1439: * mappings associated with this folio. If we succeed
1440: * we try to free the folio as well.
1441: *
1442: * We do this even if the folio is dirty.
1443: * filemap_release_folio() does not perform I/O, but it
1444: * is possible for a folio to have the dirty flag set,
1445: * but it is actually clean (all its buffers are clean).
1446: * This happens if the buffers were written out directly,
1447: * with bh_submit(). ext3 will do this, as well as
1448: * the blockdev mapping. filemap_release_folio() will
1449: * discover that cleanness and will drop the buffers
1450: * and mark the folio clean - it can be freed.
1451: *
1452: * Rarely, folios can have buffers and no ->mapping.
1453: * These are the folios which were not successfully
1454: * invalidated in truncate_cleanup_folio(). We try to
1455: * drop those buffers here and if that worked, and the
1456: * folio is no longer mapped into process address space
1457: * (refcount == 1) it can be freed. Otherwise, leave
1458: * the folio on the LRU so it is swappable.
1459: */
1460: if (folio_needs_release(folio)) {
1461: if (!filemap_release_folio(folio, sc->gfp_mask))
1462: goto activate_locked;
1463: if (!mapping && folio_ref_count(folio) == 1) {
1464: folio_unlock(folio);
1465: if (folio_put_testzero(folio))
1466: goto free_it;
1467: else {
1468: /*
1469: * rare race with speculative reference.
1470: * the speculative reference will free
1471: * this folio shortly, so we may
1472: * increment nr_reclaimed here (and
1473: * leave it off the LRU).
1474: */
1475: nr_reclaimed += nr_pages;
1476: continue;
1477: }
1478: }
1479: }
1480:
1481: if (folio_test_lazyfree(folio)) {
1482: /* follow __remove_mapping for reference */
1483: if (!folio_ref_freeze(folio, 1))
1484: goto keep_locked;
1485: /*
1486: * The folio has only one reference left, which is
1487: * from the isolation. After the caller puts the
1488: * folio back on the lru and drops the reference, the
1489: * folio will be freed anyway. It doesn't matter
1490: * which lru it goes on. So we don't bother checking
1491: * the dirty flag here.
1492: */
1493: count_vm_events(PGLAZYFREED, nr_pages);
1494: count_memcg_folio_events(folio, PGLAZYFREED, nr_pages);
1495: } else if (!mapping || !__remove_mapping(mapping, folio, true,
1496: sc->target_mem_cgroup))
1497: goto keep_locked;
1498:
1499: folio_unlock(folio);
1500: free_it:
1501: /*
1502: * Folio may get swapped out as a whole, need to account
1503: * all pages in it.
1504: */
1505: nr_reclaimed += nr_pages;
1506:
1507: folio_unqueue_deferred_split(folio);
1508: if (folio_batch_add(&free_folios, folio) == 0) {
1509: mem_cgroup_uncharge_folios(&free_folios);
1510: try_to_unmap_flush();
1511: free_unref_folios(&free_folios);
1512: }
1513: continue;
1514:
1515: activate_locked_split:
1516: /*
1517: * The tail pages that are failed to add into swap cache
1518: * reach here. Fixup nr_scanned and nr_pages.
1519: */
1520: if (nr_pages > 1) {
1521: sc->nr_scanned -= (nr_pages - 1);
1522: nr_pages = 1;
1523: }
1524: activate_locked:
1525: /* Not a candidate for swapping, so reclaim swap space. */
1526: if (folio_test_swapcache(folio) &&
1527: (mem_cgroup_swap_full(folio) || folio_test_mlocked(folio)))
1528: folio_free_swap(folio);
1529: VM_BUG_ON_FOLIO(folio_test_active(folio), folio);
1530: if (!folio_test_mlocked(folio)) {
1531: int type = folio_is_file_lru(folio);
1532: folio_set_active(folio);
1533: stat->nr_activate[type] += nr_pages;
1534: count_memcg_folio_events(folio, PGACTIVATE, nr_pages);
1535: }
1536: keep_locked:
1537: folio_unlock(folio);
1538: keep:
1539: list_add(&folio->lru, &ret_folios);
1540: VM_BUG_ON_FOLIO(folio_test_lru(folio) ||
1541: folio_test_unevictable(folio), folio);
1542: }
1543: /* 'folio_list' is always empty here */
1544:
1545: /* Migrate folios selected for demotion */
1546: nr_demoted = demote_folio_list(&demote_folios, pgdat, memcg);
1547: nr_reclaimed += nr_demoted;
1548: stat->nr_demoted += nr_demoted;
1549: /* Folios that could not be demoted are still in @demote_folios */
1550: if (!list_empty(&demote_folios)) {
1551: /* Folios which weren't demoted go back on @folio_list */
1552: list_splice_init(&demote_folios, folio_list);
1553:
1554: /*
1555: * goto retry to reclaim the undemoted folios in folio_list if
1556: * desired.
1557: *
1558: * Reclaiming directly from top tier nodes is not often desired
1559: * due to it breaking the LRU ordering: in general memory
1560: * should be reclaimed from lower tier nodes and demoted from
1561: * top tier nodes.
1562: *
1563: * However, disabling reclaim from top tier nodes entirely
1564: * would cause ooms in edge scenarios where lower tier memory
1565: * is unreclaimable for whatever reason, eg memory being
1566: * mlocked or too hot to reclaim. We can disable reclaim
1567: * from top tier nodes in proactive reclaim though as that is
1568: * not real memory pressure.
1569: */
1570: if (!sc->proactive) {
1571: do_demote_pass = false;
1572: goto retry;
1573: }
1574: }
1575:
1576: pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
1577:
1578: mem_cgroup_uncharge_folios(&free_folios);
1579: try_to_unmap_flush();
1580: free_unref_folios(&free_folios);
1581:
1582: list_splice(&ret_folios, folio_list);
1583: count_vm_events(PGACTIVATE, pgactivate);
1584:
1585: swap_write_submit(&ctx);
1586: return nr_reclaimed;
1587: }
SourceFile:mm/vmscan.c]
|
| 78/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 74467
output: 29
thoughts: 1655
Thoughts: **A Deep Dive into a Potential Btrfs Crash**
Alright, let's break this down. It seems we have a nasty interaction here, specifically within `shrink_folio_list` and how it handles folios, particularly with Btrfs, direct I/O, and large folios. The core of the problem seems to be a race condition that leads to a crash due to an assertion failure.
First, within `shrink_folio_list`, after a successful `filemap_release_folio`, which seems to clear private data associated with the folio, it then potentially calls `__remove_mapping`. If `__remove_mapping` fails because the folio is still pinned (like by an ongoing direct I/O), we hit the `keep_locked` label, *keeping* the folio in the page cache but now with the private data cleared. That's the crux.
The problem lies in what happens *after* `filemap_release_folio`. If a direct I/O (`aio_read` in this case) completes after the folio has had its private data cleared, it'll call `bio_set_pages_dirty` which calls `folio_mark_dirty_lock(folio)`. Now, the folio is marked dirty and we have a path to writeback. The writeback process, specifically `extent_writepage`, now sees the dirty folio and checks if it's beyond `i_size`. If the file was truncated *after* the private data was cleared, the folio *is* beyond `i_size`. That triggers `folio_invalidate`.
`folio_invalidate` then calls `btrfs_invalidate_folio`, which in turn calls `wait_subpage_spinlock`. This is where the crash happens. `wait_subpage_spinlock` asserts that `folio_test_private(folio) && folio_get_private(folio)`. But, since `filemap_release_folio` *cleared* the private data, the assertion fails!
To be absolutely sure, I've outlined a sequence of events: a direct I/O read, a `shrink_folio_list` call which releases the folio and clears private data, truncation of the file, then writeback, and finally that assertion in `wait_subpage_spinlock`. It's a textbook race condition, as I suspected.
I had to verify if `extent_writepage` even checks the private data *before* it calls `folio_invalidate`. Checking the code confirms it doesn't! So, the crash *does* happen exactly as described.
Now, that `wait_subpage_spinlock` call also only happens if `btrfs_is_subpage` is true. `btrfs_is_subpage` returns true if the sectorsize of the storage is less than the folio size. Crucially, this affects *subpage* btrfs, and even on standard 4K sector size, this is true for large folios. Btrfs *does* use large folios, so this *is* a problem on x86_64 systems with standard 4k sector size, where large folios are enabled.
Specifically, the `btrfs_is_subpage` check is defined as `fs_info->sectorsize < folio_size(folio)`. Large folios (e.g., 16K, 64K, or 2M) are definitely > 4K, meaning the assertion *can* be hit even on a common system.
The crash is due to an `ASSERT` failure. In a production system, this could be a `WARN_ON` or `BUG_ON`, and the system may crash or generate an unrecoverable error.
|
| 79/2 |
2026/09/20 01:02 |
tool |
grepper |
0m
Args: map[Expression:#define ASSERT PathPrefix:fs/btrfs/]
Results: map[Output:fs/btrfs/messages.h=107=static inline void verify_assert_printk_format(const char *fmt, ...) {
--
fs/btrfs/messages.h-141- */
fs/btrfs/messages.h:142:#define ASSERT(cond, args...) \
fs/btrfs/messages.h-143-do { \
--
fs/btrfs/messages.h-158-/* Compile check the @cond expression but don't generate any code. */
fs/btrfs/messages.h:159:#define ASSERT(cond, args...) BUILD_BUG_ON_INVALID(cond)
fs/btrfs/messages.h-160-#endif
--
fs/btrfs/raid56.c=61=static void btrfs_dump_rbio(const struct btrfs_fs_info *fs_info,
--
fs/btrfs/raid56.c-74-
fs/btrfs/raid56.c:75:#define ASSERT_RBIO(expr, rbio) \
fs/btrfs/raid56.c-76-({ \
--
fs/btrfs/raid56.c-85-
fs/btrfs/raid56.c:86:#define ASSERT_RBIO_STRIPE(expr, rbio, stripe_nr) \
fs/btrfs/raid56.c-87-({ \
--
fs/btrfs/raid56.c-97-
fs/btrfs/raid56.c:98:#define ASSERT_RBIO_SECTOR(expr, rbio, sector_nr) \
fs/btrfs/raid56.c-99-({ \
--
fs/btrfs/raid56.c-109-
fs/btrfs/raid56.c:110:#define ASSERT_RBIO_LOGICAL(expr, rbio, logical) \
fs/btrfs/raid56.c-111-({ \
]
|
| 80/2 |
2026/09/20 01:02 |
llm |
expert-compressor |
0m
Model:
Reply: ### 1. Approaches Tried and Results
- **Crash Report Analysis:**
- Stack trace shows an assertion failure at `fs/btrfs/inode.c:7616`: `ASSERT(folio_test_private(folio) && folio_get_private(folio))` inside `wait_subpage_spinlock()`.
- Triggered via: `aio_read` -> Direct I/O (`blkdev_direct_IO` / `bio_iov_iter_get_pages`) -> GUP (`__gup_longterm_locked` / `__get_user_pages`) -> page fault handling (`handle_mm_fault` -> `do_wp_page` -> `btrfs_page_mkwrite`) -> waiting for ordered extents (`btrfs_start_ordered_extent` / `btrfs_start_ordered_extent_nowriteback`) -> `filemap_fdatawrite_range` -> `btrfs_writepages` -> `extent_write_cache_pages` -> `extent_writepage` -> `folio_invalidate` -> `btrfs_invalidate_folio` -> `wait_subpage_spinlock`.
- Machine is x86_64 (`PAGE_SIZE == 4096`).
- **Inspection of `wait_subpage_spinlock` (`fs/btrfs/inode.c:7608-7632`):**
- Only executes if `btrfs_is_subpage(fs_info, folio)` returns true.
- If `btrfs_is_subpage` is true, calls `ASSERT(folio_test_private(folio) && folio_get_private(folio))`.
- Followed by `spin_lock_irq(&bfs->lock); spin_unlock_irq(&bfs->lock);`.
- **Inspection of `extent_writepage` (`fs/btrfs/extent_io.c:2041-2089`):**
- Checks if `folio->index > end_index || (folio->index == end_index && !pg_offset)` (i.e. folio beyond EOF / `i_size`).
- If beyond `i_size`, calls `folio_invalidate(folio, 0, folio_size(folio))` and unlocks the folio.
- Noticeably: `folio_invalidate()` is called *before* the check `if (unlikely(!folio_test_private(folio)))` at line 2076.
- Comment at line 2068 notes: dirty folios without private set shouldn't happen unless marked dirty without informing the fs (e.g. pinned user pages / GUP).
- **Inspection of `btrfs_is_subpage` (`fs/btrfs/subpage.h:98-104`):**
- Defined as `return fs_info->sectorsize < folio_size(folio);`.
- On x86_64, standard sectorsize is 4096. If large folios are used, `folio_size(folio) > 4096`, making `btrfs_is_subpage()` return `true`.
- **Inspection of `set_folio_extent_mapped` & `btrfs_attach_folio_state`:**
- `set_folio_extent_mapped` checks `btrfs_is_subpage`. If true, calls `btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA)` which allocates `struct btrfs_folio_state` and attaches it via `folio_attach_private(folio, bfs)`.
- If not subpage, attaches `(void *)EXTENT_FOLIO_PRIVATE` (value 1).
- **Inspection of `btrfs_page_mkwrite` (`fs/btrfs/file.c:1890-1939`):**
- In `btrfs_page_mkwrite`, calls `set_folio_extent_mapped(folio)`.
- Then checks `ordered = btrfs_lookup_ordered_range(...)`.
- If ordered extent exists, unlocks extent, calls `folio_unlock(folio)`, drops `i_mmap_lock`, and calls `btrfs_start_ordered_extent(ordered)`.
- During `btrfs_start_ordered_extent(ordered)`, writeback is triggered for dirty pages in the range.
- **Inspection of dirty / invalidation / release paths:**
- Direct I/O GUP / pinned pages: when BIO completes, `bio_set_pages_dirty()` / `bvec_unpin()` marks folio dirty via `folio_mark_dirty` / `btrfs_data_dirty_folio`.
- Truncation / hole punching: `truncate_inode_partial_folio` and `btrfs_invalidate_folio` / `__btrfs_release_folio`.
- `__btrfs_release_folio()` calls `wait_subpage_spinlock(folio)` then `clear_folio_extent_mapped(folio)`.
- `btrfs_invalidate_folio()` calls `wait_subpage_spinlock(folio)` right after `folio_wait_writeback(folio)`.
---
### 2. Current Hypotheses and Lines of Investigation
- **The Core Issue:**
- In `extent_writepage()`, when `folio` is beyond `i_size`, it calls `folio_invalidate(folio, 0, folio_size(folio))`.
- `folio_invalidate()` invokes the address space operation `btrfs_invalidate_folio()`.
- At the start of `btrfs_invalidate_folio()`, it unconditionally calls `wait_subpage_spinlock(folio)`.
- `wait_subpage_spinlock()` checks `if (!btrfs_is_subpage(fs_info, folio)) return;` and then `ASSERT(folio_test_private(folio) && folio_get_private(folio));`.
- If the folio has had its private data cleared (or never had it attached, or had it detached via a race with truncation/invalidation/release, or was dirtied via user memory mapping / GUP without private data attached), `folio_test_private(folio)` is false (or `folio_get_private` is NULL).
- In debug builds (with `CONFIG_BTRFS_ASSERT=y`), `ASSERT(...)` evaluates to `BUG()`, halting the kernel.
- **Production Impact (without KASAN/UBSAN/ASSERTs):**
- In production builds without assertions (`CONFIG_BTRFS_ASSERT` not set):
`ASSERT` is a no-op.
Then `bfs = folio_get_private(folio);` yields `NULL` (or `(void *)1` if not subpage, but `btrfs_is_subpage` is true so it would be NULL).
Then `spin_lock_irq(&bfs->lock);` is called!
Dereferencing `bfs->lock` where `bfs == NULL` results in a NULL pointer dereference crash (at offset of `lock` within `struct btrfs_folio_state`).
Offset of `lock` in `struct btrfs_folio_state`: need to check where `lock` is within `struct btrfs_folio_state`. If `lock` is at offset 0, it dereferences address `0x0` -> kernel NULL pointer dereference panic.
- Need to verify `struct btrfs_folio_state` definition to see offset of `lock`.
- **Reachability / Privileges:**
- Triggered by standard unprivileged syscalls: `io_submit()` / `aio_read()` reading into memory mapped to a Btrfs file, concurrent with write/truncate or page fault writeback.
- Or simply standard user operations on an unprivileged Btrfs mount (or loop device / user mount).
- Need to check if unprivileged users can trigger this without root / user namespaces (e.g. if btrfs is mounted, or requires mounting).
---
### 3. Critical Code Snippets, Structs, and Files
- **`fs/btrfs/inode.c:7608-7632` (`wait_subpage_spinlock`):**
```c
static void wait_subpage_spinlock(struct folio *folio)
{
struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
struct btrfs_folio_state *bfs;
if (!btrfs_is_subpage(fs_info, folio))
return;
ASSERT(folio_test_private(folio) && folio_get_private(folio));
bfs = folio_get_private(folio);
spin_lock_irq(&bfs->lock);
spin_unlock_irq(&bfs->lock);
}
```
- **`fs/btrfs/extent_io.c:2056-2061` (`extent_writepage`):**
```c
pg_offset = offset_in_folio(folio, i_size);
if (folio->index > end_index ||
(folio->index == end_index && !pg_offset)) {
folio_invalidate(folio, 0, folio_size(folio));
folio_unlock(folio);
return 0;
}
```
- **`fs/btrfs/inode.c:7697-7698` (`btrfs_invalidate_folio`):**
```c
folio_wait_writeback(folio);
wait_subpage_spinlock(folio);
```
- **`fs/btrfs/subpage.h:98-104` (`btrfs_is_subpage`):**
```c
static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
struct folio *folio)
{
if (folio->mapping && folio->mapping->host)
ASSERT(is_data_inode(BTRFS_I(folio->mapping->host)));
return fs_info->sectorsize < folio_size(folio);
}
```
---
### 4. Remaining Items to Check
- Check definition of `struct btrfs_folio_state` (find file/line via codesearch).
- Check what happens in production when `CONFIG_BTRFS_ASSERT` is disabled: does `wait_subpage_spinlock` crash with a NULL pointer dereference in `spin_lock_irq`? (Denial of Service).
- Determine exploitability: is it purely a NULL pointer dereference / crash (DoS), or can it lead to memory corruption / UAF / info leak?
- Check vector accessibility: Unprivileged local user (can unprivileged user mount btrfs or access an existing btrfs filesystem?), user namespaces, malicious filesystem trigger.
Tokens:
input: 44232
output: 2489
thoughts: 0
|
| 81/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 28931
output: 34
thoughts: 0
|
| 82/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:fs/btrfs/messages.h Name:ASSERT]
Results: map[SourceCode: SourceFile:]
|
| 83/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 28986
output: 37
thoughts: 0
|
| 84/2 |
2026/09/20 01:02 |
tool |
read-file |
0m
Args: map[File:fs/btrfs/messages.h FirstLine:130 LineCount:40]
Results: map[Contents: 130: * true branch is executed
131: * - any non-empty format string with the "" prefix evaluates to != 0 at
132: * compile time and the false branch is executed
133: * - stringified condition is printed as %s so we don't accidentally mix format
134: * strings (the % operator)
135: * - there can be only one printk() call, so the format strings and arguments are
136: * spliced together:
137: * DEFAULT_FMT [USER_FMT], DEFAULT_ARGS [, USER_ARGS]
138: * - comma between DEFAULT_ARGS and USER_ARGS is handled by preprocessor
139: * (requires __VA_OPT__ support)
140: * - otherwise we could use __VA_OPT(,) __VA_ARGS__ for the 2nd+ argument of args,
141: */
142: #define ASSERT(cond, args...) \
143: do { \
144: verify_assert_printk_format("check the format string" args); \
145: if (!likely(cond)) { \
146: if (("" __FIRST_ARG(args) [0]) == 0) { \
147: pr_err("assertion failed: %s, in %s:%d\n", \
148: #cond, __FILE__, __LINE__); \
149: } else { \
150: pr_err("assertion failed: %s, in %s:%d (" __FIRST_ARG(args) ")\n", \
151: #cond, __FILE__, __LINE__ __REST_ARGS(args)); \
152: } \
153: BUG(); \
154: } \
155: } while(0)
156:
157: #else
158: /* Compile check the @cond expression but don't generate any code. */
159: #define ASSERT(cond, args...) BUILD_BUG_ON_INVALID(cond)
160: #endif
161:
162: #ifdef CONFIG_BTRFS_DEBUG
163: /* Verbose warning only under debug build. */
164: #define DEBUG_WARN(args...) WARN(1, KERN_ERR args)
165: #else
166: #define DEBUG_WARN(...) do {} while(0)
167: #endif
168:
169: __printf(5, 6)
]
|
| 85/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 29736
output: 41
thoughts: 0
|
| 86/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/subpage.h Name:btrfs_folio_state]
Results: map[SourceCode: 54: /*
55: * Structure to trace status of each sector inside a page, attached to
56: * page::private for both data and metadata inodes.
57: */
58: struct btrfs_folio_state {
59: /* Common members for both data and metadata pages */
60: spinlock_t lock;
61: union {
62: /*
63: * Structures only used by metadata
64: *
65: * @eb_refs should only be operated under private_lock, as it
66: * manages whether the btrfs_folio_state can be detached.
67: */
68: atomic_t eb_refs;
69:
70: /*
71: * Structures only used by data,
72: *
73: * How many sectors inside the page is locked.
74: */
75: atomic_t nr_locked;
76: };
77: unsigned long bitmaps[];
78: };
SourceFile:fs/btrfs/subpage.h]
|
| 87/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 30093
output: 39
thoughts: 0
|
| 88/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:wait_subpage_spinlock]
Results: map[SourceCode:7601: /*
7602: * For release_folio() and invalidate_folio() we have a race window where
7603: * folio_end_writeback() is called but the subpage spinlock is not yet released.
7604: * If we continue to release/invalidate the page, we could cause use-after-free
7605: * for subpage spinlock. So this function is to spin and wait for subpage
7606: * spinlock.
7607: */
7608: static void wait_subpage_spinlock(struct folio *folio)
7609: {
7610: struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
7611: struct btrfs_folio_state *bfs;
7612:
7613: if (!btrfs_is_subpage(fs_info, folio))
7614: return;
7615:
7616: ASSERT(folio_test_private(folio) && folio_get_private(folio));
7617: bfs = folio_get_private(folio);
7618:
7619: /*
7620: * This may look insane as we just acquire the spinlock and release it,
7621: * without doing anything. But we just want to make sure no one is
7622: * still holding the subpage spinlock.
7623: * And since the page is not dirty nor writeback, and we have page
7624: * locked, the only possible way to hold a spinlock is from the endio
7625: * function to clear page writeback.
7626: *
7627: * Here we just acquire the spinlock so that all existing callers
7628: * should exit and we're safe to release/invalidate the page.
7629: */
7630: spin_lock_irq(&bfs->lock);
7631: spin_unlock_irq(&bfs->lock);
7632: }
SourceFile:fs/btrfs/inode.c]
|
| 89/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 30703
output: 40
thoughts: 0
|
| 90/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_invalidate_folio]
Results: map[SourceCode:7672: static void btrfs_invalidate_folio(struct folio *folio, size_t offset,
7673: size_t length)
7674: {
7675: struct btrfs_inode *inode = folio_to_inode(folio);
7676: struct btrfs_fs_info *fs_info = inode->root->fs_info;
7677: struct extent_io_tree *tree = &inode->io_tree;
7678: struct extent_state *cached_state = NULL;
7679: u64 page_start = folio_pos(folio);
7680: u64 page_end = page_start + folio_size(folio) - 1;
7681: u64 cur;
7682: int inode_evicting = inode_state_read_once(&inode->vfs_inode) & I_FREEING;
7683:
7684: /*
7685: * We have folio locked so no new ordered extent can be created on this
7686: * page, nor bio can be submitted for this folio.
7687: *
7688: * But already submitted bio can still be finished on this folio.
7689: * Furthermore, endio function won't skip folio which has Ordered
7690: * already cleared, so it's possible for endio and
7691: * invalidate_folio to do the same ordered extent accounting twice
7692: * on one folio.
7693: *
7694: * So here we wait for any submitted bios to finish, so that we won't
7695: * do double ordered extent accounting on the same folio.
7696: */
7697: folio_wait_writeback(folio);
7698: wait_subpage_spinlock(folio);
7699:
7700: /*
7701: * The invalidated blocks are going away; drop any fixup blocks among
7702: * them, data included, as they have no space reservation.
7703: */
7704: btrfs_folio_clear_fixup_dirty(fs_info, folio, page_start + offset, length);
7705:
7706: /*
7707: * For subpage case, we have call sites like
7708: * btrfs_punch_hole_lock_range() which passes range not aligned to
7709: * sectorsize.
7710: * If the range doesn't cover the full folio, we don't need to and
7711: * shouldn't clear page extent mapped, as folio->private can still
7712: * record subpage dirty bits for other part of the range.
7713: *
7714: * For cases that invalidate the full folio even the range doesn't
7715: * cover the full folio, like invalidating the last folio, we're
7716: * still safe to wait for ordered extent to finish.
7717: */
7718: if (!(offset == 0 && length == folio_size(folio))) {
7719: btrfs_release_folio(folio, GFP_NOFS);
7720: return;
7721: }
7722:
7723: if (!inode_evicting)
7724: btrfs_lock_extent(tree, page_start, page_end, &cached_state);
7725:
7726: cur = page_start;
7727: while (cur < page_end) {
7728: struct btrfs_ordered_extent *ordered;
7729: u64 range_end;
7730: u32 range_len;
7731: u32 extra_flags = 0;
7732:
7733: ordered = btrfs_lookup_first_ordered_range(inode, cur,
7734: page_end + 1 - cur);
7735: if (!ordered) {
7736: range_end = page_end;
7737: /*
7738: * No ordered extent covering this range, we are safe
7739: * to delete all extent states in the range.
7740: */
7741: extra_flags = EXTENT_CLEAR_ALL_BITS;
7742: goto next;
7743: }
7744: if (ordered->file_offset > cur) {
7745: /*
7746: * There is a range between [cur, oe->file_offset) not
7747: * covered by any ordered extent.
7748: * We are safe to delete all extent states, and handle
7749: * the ordered extent in the next iteration.
7750: */
7751: range_end = ordered->file_offset - 1;
7752: extra_flags = EXTENT_CLEAR_ALL_BITS;
7753: goto next;
7754: }
7755:
7756: range_end = min(ordered->file_offset + ordered->num_bytes - 1,
7757: page_end);
7758: ASSERT(range_end + 1 - cur < U32_MAX);
7759: range_len = range_end + 1 - cur;
7760: /*
7761: * If the range is not dirty, the range has been submitted and
7762: * since we have waited for the writeback, endio has been
7763: * executed, thus we must skip the range to avoid double
7764: * accounting for the ordered extent.
7765: */
7766: if (!btrfs_folio_test_dirty(fs_info, folio, cur, range_len))
7767: goto next;
7768:
7769: /*
7770: * The range is dirty meaning it has not been submitted.
7771: * Here we need to truncate the OE range as the range will never
7772: * be submitted.
7773: *
7774: * IO on this page will never be started, so we need to account
7775: * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
7776: * here, must leave that up for the ordered extent completion.
7777: *
7778: * This will also unlock the range for incoming
7779: * btrfs_finish_ordered_io().
7780: */
7781: if (!inode_evicting)
7782: btrfs_clear_extent_bit(tree, cur, range_end,
7783: EXTENT_DELALLOC |
7784: EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7785: EXTENT_DEFRAG, &cached_state);
7786:
7787: btrfs_mark_ordered_extent_truncated(ordered, cur - ordered->file_offset);
7788:
7789: /*
7790: * If the ordered extent has finished, we're safe to delete all
7791: * the extent states of the range, otherwise
7792: * btrfs_finish_ordered_io() will get executed by endio for
7793: * other pages, so we can't delete extent states.
7794: */
7795: if (btrfs_dec_test_ordered_pending(inode, &ordered,
7796: cur, range_end + 1 - cur)) {
7797: btrfs_finish_ordered_io(ordered);
7798: /*
7799: * The ordered extent has finished, now we're again
7800: * safe to delete all extent states of the range.
7801: */
7802: extra_flags = EXTENT_CLEAR_ALL_BITS;
7803: }
7804: next:
7805: if (ordered)
7806: btrfs_put_ordered_extent(ordered);
7807: /*
7808: * Qgroup reserved space handler
7809: * Sector(s) here will be either:
7810: *
7811: * 1) Already written to disk or bio already finished
7812: * Then its QGROUP_RESERVED bit in io_tree is already cleared.
7813: * Qgroup will be handled by its qgroup_record then.
7814: * btrfs_qgroup_free_data() call will do nothing here.
7815: *
7816: * 2) Not written to disk yet
7817: * Then btrfs_qgroup_free_data() call will clear the
7818: * QGROUP_RESERVED bit of its io_tree, and free the qgroup
7819: * reserved data space.
7820: * Since the IO will never happen for this page.
7821: */
7822: btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL);
7823: if (!inode_evicting)
7824: btrfs_clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
7825: EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7826: EXTENT_DEFRAG | extra_flags,
7827: &cached_state);
7828: cur = range_end + 1;
7829: }
7830: btrfs_folio_clear_dirty(fs_info, folio, page_start, folio_size(folio));
7831: btrfs_clear_folio_dirty_tag(folio);
7832: if (!inode_evicting)
7833: __btrfs_release_folio(folio, GFP_NOFS);
7834: clear_folio_extent_mapped(folio);
7835: }
SourceFile:fs/btrfs/inode.c]
|
| 91/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 33481
output: 39
thoughts: 0
|
| 92/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:extent_writepage]
Results: map[SourceCode:2032: /*
2033: * the writepage semantics are similar to regular writepage. extent
2034: * records are inserted to lock ranges in the tree, and as dirty areas
2035: * are found, they are marked writeback. Then the lock bits are removed
2036: * and the end_io handler clears the writeback ranges
2037: *
2038: * Return 0 if everything goes well.
2039: * Return <0 for error.
2040: */
2041: static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl)
2042: {
2043: struct btrfs_inode *inode = BTRFS_I(folio->mapping->host);
2044: struct btrfs_fs_info *fs_info = inode->root->fs_info;
2045: int ret;
2046: size_t pg_offset;
2047: loff_t i_size = i_size_read(&inode->vfs_inode);
2048: const pgoff_t end_index = i_size >> PAGE_SHIFT;
2049: const unsigned int blocks_per_folio = btrfs_blocks_per_folio(fs_info, folio);
2050:
2051: trace_extent_writepage(folio, &inode->vfs_inode, bio_ctrl->wbc);
2052:
2053: WARN_ON(!folio_test_locked(folio));
2054:
2055: pg_offset = offset_in_folio(folio, i_size);
2056: if (folio->index > end_index ||
2057: (folio->index == end_index && !pg_offset)) {
2058: folio_invalidate(folio, 0, folio_size(folio));
2059: folio_unlock(folio);
2060: return 0;
2061: }
2062:
2063: if (folio_contains(folio, end_index))
2064: folio_zero_range(folio, pg_offset, folio_size(folio) - pg_offset);
2065:
2066: bio_ctrl_init_submit_bitmap(fs_info, folio, bio_ctrl);
2067: /*
2068: * If the page is dirty but without private set, it's marked dirty
2069: * without informing the fs.
2070: * Nowadays that is a bug, since the introduction of
2071: * pin_user_pages*().
2072: *
2073: * So here we check if the page has private set to rule out such
2074: * case.
2075: */
2076: if (unlikely(!folio_test_private(folio))) {
2077: DEBUG_WARN();
2078: btrfs_err_rl(fs_info,
2079: "root %lld ino %llu folio %llu is marked dirty without notifying the fs",
2080: btrfs_root_id(inode->root),
2081: btrfs_ino(inode), folio_pos(folio));
2082: ret = -EUCLEAN;
2083: goto done;
2084: }
2085:
2086: ret = set_folio_extent_mapped(folio);
2087: if (ret < 0)
2088: goto done;
2089:
2090: ret = writepage_delalloc(inode, folio, bio_ctrl);
2091: if (ret == 1)
2092: return 0;
2093: if (ret)
2094: goto done;
2095:
2096: ret = extent_writepage_io(inode, folio, folio_pos(folio),
2097: folio_size(folio), bio_ctrl, i_size);
2098: if (ret == 1)
2099: return 0;
2100: if (unlikely(ret < 0))
2101: btrfs_err_rl(fs_info,
2102: "failed to submit blocks, root=%lld inode=%llu folio=%llu submit_bitmap=%*pbl: %pe",
2103: btrfs_root_id(inode->root), btrfs_ino(inode),
2104: folio_pos(folio), blocks_per_folio,
2105: bio_ctrl->submit_bitmap, ERR_PTR(ret));
2106:
2107: bio_ctrl->wbc->nr_to_write--;
2108:
2109: done:
2110: if (ret < 0)
2111: mapping_set_error(folio->mapping, ret);
2112: /*
2113: * Only unlock ranges that are submitted. As there can be some async
2114: * submitted ranges inside the folio.
2115: */
2116: btrfs_folio_end_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap);
2117: ASSERT(ret <= 0);
2118: return ret;
2119: }
SourceFile:fs/btrfs/extent_io.c]
|
| 93/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 34992
output: 42
thoughts: 0
|
| 94/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_data_dirty_folio]
Results: map[SourceCode:10772: /*
10773: * The folio is going dirty without a btrfs delalloc space reservation.
10774: * This requires a fixup before writeback which we might sleep so cannot
10775: * run in this context, so we merely set state on the folio indicating it
10776: * needs fixup before writeback.
10777: *
10778: * Note that there is no range in the input, so the whole folio is marked
10779: * dirty and fixup.
10780: *
10781: * We believe that all callers of dirty_folio either:
10782: * - take the folio lock (e.g. pinned folio release notification).
10783: * - take the pte lock but must be running on a dirty pte which means
10784: * page_mkwrite() ran on it and reserved the space. zap_pte_range() cannot
10785: * race with writeback cleaning the folio because writeback runs
10786: * folio_mkclean() which also uses the pte lock and revokes outstanding
10787: * writable mappings.
10788: * Therefore, an additional folio private lock (a la bfs->lock for all cases,
10789: * not just subpage) is not necessary.
10790: */
10791: static bool btrfs_data_dirty_folio(struct address_space *mapping,
10792: struct folio *folio)
10793: {
10794: struct btrfs_inode *inode = BTRFS_I(mapping->host);
10795: struct btrfs_fs_info *fs_info = inode->root->fs_info;
10796: const u64 page_start = folio_pos(folio);
10797: const u64 range_end = min_t(u64, folio_next_pos(folio),
10798: round_up(i_size_read(&inode->vfs_inode),
10799: fs_info->sectorsize));
10800:
10801: if (range_end > page_start)
10802: btrfs_folio_set_fixup_dirty(fs_info, folio, page_start,
10803: range_end - page_start);
10804: return filemap_dirty_folio(mapping, folio);
10805: }
SourceFile:fs/btrfs/inode.c]
|
| 95/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 35737
output: 45
thoughts: 0
|
| 96/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_folio_set_fixup_dirty]
Results: map[SourceCode: 550: /*
551: * Mark the still-clean blocks of a folio dirty and needing fixup, for
552: * btrfs_data_dirty_folio().
553: *
554: * A subpage block size folio that is not uptodate is left alone: its clean
555: * blocks may hold content that was never read in, which must not be marked
556: * dirty.
557: */
558: void btrfs_folio_set_fixup_dirty(const struct btrfs_fs_info *fs_info,
559: struct folio *folio, u64 start, u32 len)
560: {
561: if (!btrfs_is_subpage(fs_info, folio)) {
562: if (!folio_test_dirty(folio))
563: folio_set_fixup_pending(folio);
564: return;
565: }
566: if (!folio_test_uptodate(folio))
567: return;
568: btrfs_subpage_set_fixup_dirty(fs_info, folio, start, len);
569: }
SourceFile:fs/btrfs/subpage.c]
|
| 97/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 36149
output: 47
thoughts: 0
|
| 98/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/subpage.c Name:btrfs_subpage_set_fixup_dirty]
Results: map[SourceCode: 515: /*
516: * In one pass under bfs->lock, mark every block with a clear dirty bit in the
517: * range both dirty and needing fixup.
518: *
519: * Only called from the dirty_folio callback, which owns the folio-level
520: * dirty flag; calling folio_mark_dirty() here would recurse.
521: *
522: * The folio fixup flag and bits are both set under bfs->lock so that a
523: * writeback pass observing the new bits also observes the flag.
524: */
525: static void btrfs_subpage_set_fixup_dirty(const struct btrfs_fs_info *fs_info,
526: struct folio *folio, u64 start, u32 len)
527: {
528: struct btrfs_folio_state *bfs = folio_get_private(folio);
529: unsigned int dirty_bit = subpage_calc_start_bit(fs_info, folio,
530: dirty, start, len);
531: unsigned int fixup_bit = subpage_calc_start_bit(fs_info, folio,
532: fixup, start, len);
533: const unsigned int nbits = len >> fs_info->sectorsize_bits;
534: unsigned long flags;
535: bool marked = false;
536:
537: spin_lock_irqsave(&bfs->lock, flags);
538: for (unsigned int i = 0; i < nbits; i++) {
539: if (test_bit(dirty_bit + i, bfs->bitmaps))
540: continue;
541: set_bit(dirty_bit + i, bfs->bitmaps);
542: set_bit(fixup_bit + i, bfs->bitmaps);
543: marked = true;
544: }
545: if (marked)
546: folio_set_fixup_pending(folio);
547: spin_unlock_irqrestore(&bfs->lock, flags);
548: }
SourceFile:fs/btrfs/subpage.c]
|
| 99/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 36826
output: 43
thoughts: 0
|
| 100/2 |
2026/09/20 01:02 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/subpage.c Name:btrfs_attach_folio_state]
Results: map[SourceCode: 38: int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
39: struct folio *folio, enum btrfs_folio_type type)
40: {
41: struct btrfs_folio_state *bfs;
42:
43: /* For metadata we don't support large folio yet. */
44: if (type == BTRFS_SUBPAGE_METADATA)
45: ASSERT(!folio_test_large(folio));
46:
47: /*
48: * We have cases like a dummy extent buffer page, which is not mapped
49: * and doesn't need to be locked.
50: */
51: if (folio->mapping)
52: ASSERT(folio_test_locked(folio));
53:
54: /* Either not subpage, or the folio already has private attached. */
55: if (folio_test_private(folio))
56: return 0;
57: if (type == BTRFS_SUBPAGE_METADATA && !btrfs_meta_is_subpage(fs_info))
58: return 0;
59: if (type == BTRFS_SUBPAGE_DATA && !btrfs_is_subpage(fs_info, folio))
60: return 0;
61:
62: bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, GFP_NOFS);
63: if (IS_ERR(bfs))
64: return PTR_ERR(bfs);
65:
66: folio_attach_private(folio, bfs);
67: return 0;
68: }
SourceFile:fs/btrfs/subpage.c]
|
| 101/2 |
2026/09/20 01:02 |
llm |
expert |
0m
Model:
Tokens:
input: 37378
output: 43
thoughts: 0
|
| 102/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/subpage.c Name:btrfs_alloc_folio_state]
Results: map[SourceCode: 88: struct btrfs_folio_state *btrfs_alloc_folio_state(const struct btrfs_fs_info *fs_info,
89: size_t fsize, enum btrfs_folio_type type,
90: gfp_t gfp)
91: {
92: struct btrfs_folio_state *ret;
93: unsigned int real_size;
94:
95: ASSERT(fs_info->sectorsize < fsize);
96:
97: real_size = struct_size(ret, bitmaps,
98: BITS_TO_LONGS(btrfs_bitmap_nr_max *
99: (fsize >> fs_info->sectorsize_bits)));
100: ret = kzalloc(real_size, gfp);
101: if (!ret)
102: return ERR_PTR(-ENOMEM);
103:
104: spin_lock_init(&ret->lock);
105: if (type == BTRFS_SUBPAGE_METADATA)
106: atomic_set(&ret->eb_refs, 0);
107: else
108: atomic_set(&ret->nr_locked, 0);
109: return ret;
110: }
SourceFile:fs/btrfs/subpage.c]
|
| 103/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 37815
output: 42
thoughts: 0
|
| 104/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:set_folio_extent_mapped]
Results: map[SourceCode: 953: int set_folio_extent_mapped(struct folio *folio)
954: {
955: struct btrfs_fs_info *fs_info;
956:
957: ASSERT(folio->mapping);
958:
959: if (folio_test_private(folio))
960: return 0;
961:
962: fs_info = folio_to_fs_info(folio);
963:
964: if (btrfs_is_subpage(fs_info, folio))
965: return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
966:
967: folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
968: return 0;
969: }
SourceFile:fs/btrfs/extent_io.c]
|
| 105/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 38139
output: 42
thoughts: 0
|
| 106/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:clear_folio_extent_mapped]
Results: map[SourceCode: 971: void clear_folio_extent_mapped(struct folio *folio)
972: {
973: struct btrfs_fs_info *fs_info;
974:
975: ASSERT(folio->mapping);
976: /*
977: * The folio should not have writeback nor dirty flag set.
978: *
979: * If dirty flag is set, the folio can be written back again and we
980: * expect the private flag set for the folio.
981: *
982: * If writeback flag is set, the endio may need to utilize the
983: * private for btrfs_folio_state.
984: */
985: ASSERT(!folio_test_dirty(folio));
986: ASSERT(!folio_test_writeback(folio));
987:
988: if (!folio_test_private(folio))
989: return;
990:
991: fs_info = folio_to_fs_info(folio);
992: if (btrfs_is_subpage(fs_info, folio))
993: return btrfs_detach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
994:
995: folio_detach_private(folio);
996: }
SourceFile:fs/btrfs/extent_io.c]
|
| 107/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 38600
output: 43
thoughts: 0
|
| 108/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/subpage.c Name:btrfs_detach_folio_state]
Results: map[SourceCode: 70: void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
71: enum btrfs_folio_type type)
72: {
73: struct btrfs_folio_state *bfs;
74:
75: /* Either not subpage, or the folio already has private attached. */
76: if (!folio_test_private(folio))
77: return;
78: if (type == BTRFS_SUBPAGE_METADATA && !btrfs_meta_is_subpage(fs_info))
79: return;
80: if (type == BTRFS_SUBPAGE_DATA && !btrfs_is_subpage(fs_info, folio))
81: return;
82:
83: bfs = folio_detach_private(folio);
84: ASSERT(bfs);
85: btrfs_free_folio_state(bfs);
86: }
SourceFile:fs/btrfs/subpage.c]
|
| 109/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 38949
output: 40
thoughts: 0
|
| 110/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_release_folio]
Results: map[SourceCode:7650: static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7651: {
7652: if (folio_test_writeback(folio) || folio_test_dirty(folio))
7653: return false;
7654: return __btrfs_release_folio(folio, gfp_flags);
7655: }
SourceFile:fs/btrfs/inode.c]
|
| 111/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 39129
output: 41
thoughts: 0
|
| 112/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:__btrfs_release_folio]
Results: map[SourceCode:7640: static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7641: {
7642: if (try_release_extent_mapping(folio, gfp_flags)) {
7643: wait_subpage_spinlock(folio);
7644: clear_folio_extent_mapped(folio);
7645: return true;
7646: }
7647: return false;
7648: }
SourceFile:fs/btrfs/inode.c]
|
| 113/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 39336
output: 40
thoughts: 0
|
| 114/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:try_release_extent_mapping]
Results: map[SourceCode:3069: /*
3070: * a helper for release_folio. As long as there are no locked extents
3071: * in the range corresponding to the page, both state records and extent
3072: * map records are removed
3073: */
3074: bool try_release_extent_mapping(struct folio *folio, gfp_t mask)
3075: {
3076: u64 start = folio_pos(folio);
3077: u64 end = start + folio_size(folio) - 1;
3078: struct btrfs_inode *inode = folio_to_inode(folio);
3079: struct extent_io_tree *io_tree = &inode->io_tree;
3080:
3081: while (start <= end) {
3082: const u64 cur_gen = btrfs_get_fs_generation(inode->root->fs_info);
3083: const u64 len = end - start + 1;
3084: struct extent_map_tree *extent_tree = &inode->extent_tree;
3085: struct extent_map *em;
3086:
3087: write_lock(&extent_tree->lock);
3088: em = btrfs_lookup_extent_mapping(extent_tree, start, len);
3089: if (!em) {
3090: write_unlock(&extent_tree->lock);
3091: break;
3092: }
3093: if ((em->flags & EXTENT_FLAG_PINNED) || em->start != start) {
3094: write_unlock(&extent_tree->lock);
3095: btrfs_free_extent_map(em);
3096: break;
3097: }
3098: if (btrfs_test_range_bit_exists(io_tree, em->start,
3099: btrfs_extent_map_end(em) - 1,
3100: EXTENT_LOCKED))
3101: goto next;
3102: /*
3103: * If it's not in the list of modified extents, used by a fast
3104: * fsync, we can remove it. If it's being logged we can safely
3105: * remove it since fsync took an extra reference on the em.
3106: */
3107: if (list_empty(&em->list) || (em->flags & EXTENT_FLAG_LOGGING))
3108: goto remove_em;
3109: /*
3110: * If it's in the list of modified extents, remove it only if
3111: * its generation is older then the current one, in which case
3112: * we don't need it for a fast fsync. Otherwise don't remove it,
3113: * we could be racing with an ongoing fast fsync that could miss
3114: * the new extent.
3115: */
3116: if (em->generation >= cur_gen)
3117: goto next;
3118: remove_em:
3119: /*
3120: * We only remove extent maps that are not in the list of
3121: * modified extents or that are in the list but with a
3122: * generation lower then the current generation, so there is no
3123: * need to set the full fsync flag on the inode (it hurts the
3124: * fsync performance for workloads with a data size that exceeds
3125: * or is close to the system's memory).
3126: */
3127: btrfs_remove_extent_mapping(inode, em);
3128: /* Once for the inode's extent map tree. */
3129: btrfs_free_extent_map(em);
3130: next:
3131: start = btrfs_extent_map_end(em);
3132: write_unlock(&extent_tree->lock);
3133:
3134: /* Once for us, for the lookup_extent_mapping() reference. */
3135: btrfs_free_extent_map(em);
3136:
3137: if (need_resched()) {
3138: /*
3139: * If we need to resched but we can't block just exit
3140: * and leave any remaining extent maps.
3141: */
3142: if (!gfpflags_allow_blocking(mask))
3143: break;
3144:
3145: cond_resched();
3146: }
3147: }
3148: return try_release_extent_state(io_tree, folio);
3149: }
SourceFile:fs/btrfs/extent_io.c]
|
| 115/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 40723
output: 42
thoughts: 0
|
| 116/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:try_release_extent_state]
Results: map[SourceCode:3022: /*
3023: * A helper for struct address_space_operations::release_folio, this tests for
3024: * areas of the folio that are locked or under IO and drops the related state
3025: * bits if it is safe to drop the folio.
3026: */
3027: static bool try_release_extent_state(struct extent_io_tree *tree,
3028: struct folio *folio)
3029: {
3030: struct extent_state *cached_state = NULL;
3031: u64 start = folio_pos(folio);
3032: u64 end = start + folio_size(folio) - 1;
3033: u32 range_bits;
3034: u32 clear_bits;
3035: bool ret = false;
3036: int ret2;
3037:
3038: btrfs_get_range_bits(tree, start, end, &range_bits, &cached_state);
3039:
3040: /*
3041: * We can release the folio if it's locked only for ordered extent
3042: * completion, since that doesn't require using the folio.
3043: */
3044: if ((range_bits & EXTENT_LOCKED) &&
3045: !(range_bits & EXTENT_FINISHING_ORDERED))
3046: goto out;
3047:
3048: clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW |
3049: EXTENT_CTLBITS | EXTENT_QGROUP_RESERVED |
3050: EXTENT_FINISHING_ORDERED);
3051: /*
3052: * At this point we can safely clear everything except the locked,
3053: * nodatasum, delalloc new and finishing ordered bits. The delalloc new
3054: * bit will be cleared by ordered extent completion.
3055: */
3056: ret2 = btrfs_clear_extent_bit(tree, start, end, clear_bits, &cached_state);
3057: /*
3058: * If clear_extent_bit failed for enomem reasons, we can't allow the
3059: * release to continue.
3060: */
3061: if (ret2 == 0)
3062: ret = true;
3063: out:
3064: btrfs_free_extent_state(cached_state);
3065:
3066: return ret;
3067: }
SourceFile:fs/btrfs/extent_io.c]
|
| 117/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 41526
output: 39
thoughts: 0
|
| 118/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_aops]
Results: map[SourceCode:10807: /*
10808: * btrfs doesn't support the bmap operation because swapfiles
10809: * use bmap to make a mapping of extents in the file. They assume
10810: * these extents won't change over the life of the file and they
10811: * use the bmap result to do IO directly to the drive.
10812: *
10813: * the btrfs bmap call would return logical addresses that aren't
10814: * suitable for IO and they also will change frequently as COW
10815: * operations happen. So, swapfile + btrfs == corruption.
10816: *
10817: * For now we're avoiding this by dropping bmap.
10818: */
10819: static const struct address_space_operations btrfs_aops = {
10820: .read_folio = btrfs_read_folio,
10821: .writepages = btrfs_writepages,
10822: .readahead = btrfs_readahead,
10823: .invalidate_folio = btrfs_invalidate_folio,
10824: .launder_folio = btrfs_launder_folio,
10825: .release_folio = btrfs_release_folio,
10826: .migrate_folio = btrfs_migrate_folio,
10827: .dirty_folio = btrfs_data_dirty_folio,
10828: .error_remove_folio = generic_error_remove_folio,
10829: .swap_activate = btrfs_swap_activate,
10830: .swap_deactivate = btrfs_swap_deactivate,
10831: };
SourceFile:fs/btrfs/inode.c]
|
| 119/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 42093
output: 41
thoughts: 0
|
| 120/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_launder_folio]
Results: map[SourceCode:7634: static int btrfs_launder_folio(struct folio *folio)
7635: {
7636: return btrfs_qgroup_free_data(folio_to_inode(folio), NULL, folio_pos(folio),
7637: folio_size(folio), NULL);
7638: }
SourceFile:fs/btrfs/inode.c]
|
| 121/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 42256
output: 33
thoughts: 0
|
| 122/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/truncate.c Name:folio_launder]
Results: map[SourceCode: 606: static int folio_launder(struct address_space *mapping, struct folio *folio)
607: {
608: if (!folio_test_dirty(folio))
609: return 0;
610: if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL)
611: return 0;
612: return mapping->a_ops->launder_folio(folio);
613: }
SourceFile:mm/truncate.c]
|
| 123/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 42447
output: 35
thoughts: 0
|
| 124/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/truncate.c Name:invalidate_inode_pages2]
Results: map[SourceCode: 753: /**
754: * invalidate_inode_pages2 - remove all pages from an address_space
755: * @mapping: the address_space
756: *
757: * Any pages which are found to be mapped into pagetables are unmapped prior to
758: * invalidation.
759: *
760: * Return: -EBUSY if any pages could not be invalidated.
761: */
762: int invalidate_inode_pages2(struct address_space *mapping)
763: {
764: return invalidate_inode_pages2_range(mapping, 0, -1);
765: }
SourceFile:mm/truncate.c]
|
| 125/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 42701
output: 32
thoughts: 0
|
| 126/2 |
2026/09/20 01:03 |
tool |
grepper |
0m
Args: map[Expression:invalidate_inode_pages2 PathPrefix:fs/btrfs/]
Results: map[Output:fs/btrfs/direct-io.c=36=static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
--
fs/btrfs/direct-io.c-116- * for it to complete) and then invalidate the pages for
fs/btrfs/direct-io.c:117: * this range (through invalidate_inode_pages2_range()),
fs/btrfs/direct-io.c-118- * but that can lead us to a deadlock with a concurrent
--
fs/btrfs/disk-io.c=3301=static void invalidate_and_check_btree_folios(struct btrfs_fs_info *fs_info)
--
fs/btrfs/disk-io.c-3306-
fs/btrfs/disk-io.c:3307: ret = invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
fs/btrfs/disk-io.c-3308- if (likely(ret == 0))
--
fs/btrfs/disk-io.c-3353- rcu_read_unlock();
fs/btrfs/disk-io.c:3354: invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
fs/btrfs/disk-io.c-3355-}
--
fs/btrfs/disk-io.c=4750=static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
--
fs/btrfs/disk-io.c-4773- nofs_flag = memalloc_nofs_save();
fs/btrfs/disk-io.c:4774: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/disk-io.c-4775- memalloc_nofs_restore(nofs_flag);
--
fs/btrfs/disk-io.c=4863=static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
--
fs/btrfs/disk-io.c-4871- nofs_flag = memalloc_nofs_save();
fs/btrfs/disk-io.c:4872: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/disk-io.c-4873- memalloc_nofs_restore(nofs_flag);
--
fs/btrfs/free-space-cache.c=1288=static int __btrfs_wait_cache_io(struct btrfs_root *root,
--
fs/btrfs/free-space-cache.c-1309- if (ret) {
fs/btrfs/free-space-cache.c:1310: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/free-space-cache.c-1311- BTRFS_I(inode)->generation = 0;
--
fs/btrfs/free-space-cache.c=1368=static int __btrfs_write_out_cache(struct inode *inode,
--
fs/btrfs/free-space-cache.c-1502- if (ret) {
fs/btrfs/free-space-cache.c:1503: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/free-space-cache.c-1504- BTRFS_I(inode)->generation = 0;
--
fs/btrfs/inode.c=9904=ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
--
fs/btrfs/inode.c-10044- goto out_cb;
fs/btrfs/inode.c:10045: ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping,
fs/btrfs/inode.c-10046- start >> PAGE_SHIFT,
--
fs/btrfs/volumes.c=1335=struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
--
fs/btrfs/volumes.c-1362- */
fs/btrfs/volumes.c:1363: invalidate_inode_pages2_range(mapping, bytenr >> PAGE_SHIFT,
fs/btrfs/volumes.c-1364- (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT);
]
|
| 127/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 43931
output: 39
thoughts: 0
|
| 128/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_writepages]
Results: map[SourceCode:2961: int btrfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
2962: {
2963: struct inode *inode = mapping->host;
2964: int ret = 0;
2965: struct btrfs_bio_ctrl bio_ctrl = {
2966: .wbc = wbc,
2967: .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc),
2968: };
2969:
2970: /*
2971: * Allow only a single thread to do the reloc work in zoned mode to
2972: * protect the write pointer updates.
2973: */
2974: btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
2975: ret = extent_write_cache_pages(mapping, &bio_ctrl);
2976: submit_write_bio(&bio_ctrl, ret);
2977: btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
2978: return ret;
2979: }
SourceFile:fs/btrfs/extent_io.c]
|
| 129/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 44319
output: 42
thoughts: 0
|
| 130/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:extent_write_cache_pages]
Results: map[SourceCode:2701: /*
2702: * Walk the list of dirty pages of the given address space and write all of them.
2703: *
2704: * @mapping: address space structure to write
2705: * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2706: * @bio_ctrl: holds context for the write, namely the bio
2707: *
2708: * If a page is already under I/O, write_cache_pages() skips it, even
2709: * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2710: * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2711: * and msync() need to guarantee that all the data which was dirty at the time
2712: * the call was made get new I/O started against them. If wbc->sync_mode is
2713: * WB_SYNC_ALL then we were called for data integrity and we must wait for
2714: * existing IO to complete.
2715: */
2716: static int extent_write_cache_pages(struct address_space *mapping,
2717: struct btrfs_bio_ctrl *bio_ctrl)
2718: {
2719: struct writeback_control *wbc = bio_ctrl->wbc;
2720: struct inode *inode = mapping->host;
2721: int ret = 0;
2722: bool done = false;
2723: int nr_to_write_done = 0;
2724: struct folio_batch fbatch;
2725: unsigned int nr_folios;
2726: pgoff_t index;
2727: pgoff_t end; /* Inclusive */
2728: pgoff_t done_index;
2729: bool range_whole = false;
2730: bool scanned = false;
2731: xa_mark_t tag;
2732:
2733: /*
2734: * We have to hold onto the inode so that ordered extents can do their
2735: * work when the IO finishes. The alternative to this is failing to add
2736: * an ordered extent if the igrab() fails there and that is a huge pain
2737: * to deal with, so instead just hold onto the inode throughout the
2738: * writepages operation. If it fails here we are freeing up the inode
2739: * anyway and we'd rather not waste our time writing out stuff that is
2740: * going to be truncated anyway.
2741: */
2742: if (!igrab(inode))
2743: return 0;
2744:
2745: folio_batch_init(&fbatch);
2746: if (wbc->range_cyclic) {
2747: index = mapping->writeback_index; /* Start from prev offset */
2748: end = -1;
2749: /*
2750: * Start from the beginning does not need to cycle over the
2751: * range, mark it as scanned.
2752: */
2753: scanned = (index == 0);
2754: } else {
2755: index = wbc->range_start >> PAGE_SHIFT;
2756: end = wbc->range_end >> PAGE_SHIFT;
2757: if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2758: range_whole = true;
2759: scanned = true;
2760: }
2761:
2762: /*
2763: * We do the tagged writepage as long as the snapshot flush bit is set
2764: * and we are the first one who do the filemap_flush() on this inode.
2765: *
2766: * The nr_to_write == LONG_MAX is needed to make sure other flushers do
2767: * not race in and drop the bit.
2768: */
2769: if (range_whole && wbc->nr_to_write == LONG_MAX &&
2770: test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
2771: &BTRFS_I(inode)->runtime_flags))
2772: wbc->tagged_writepages = 1;
2773:
2774: tag = wbc_to_tag(wbc);
2775: retry:
2776: if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2777: tag_pages_for_writeback(mapping, index, end);
2778: done_index = index;
2779: while (!done && !nr_to_write_done && (index <= end) &&
2780: (nr_folios = filemap_get_folios_tag(mapping, &index,
2781: end, tag, &fbatch))) {
2782: unsigned i;
2783:
2784: for (i = 0; i < nr_folios; i++) {
2785: struct folio *folio = fbatch.folios[i];
2786:
2787: done_index = folio_next_index(folio);
2788: /*
2789: * At this point we hold neither the i_pages lock nor
2790: * the folio lock: the folio may be truncated or
2791: * invalidated (changing folio->mapping to NULL).
2792: */
2793: if (!folio_trylock(folio)) {
2794: submit_write_bio(bio_ctrl, 0);
2795: folio_lock(folio);
2796: }
2797:
2798: if (unlikely(folio->mapping != mapping)) {
2799: folio_unlock(folio);
2800: continue;
2801: }
2802:
2803: if (!folio_test_dirty(folio)) {
2804: /* Someone wrote it for us. */
2805: folio_unlock(folio);
2806: continue;
2807: }
2808:
2809: /*
2810: * For subpage case, compression can lead to mixed
2811: * writeback and dirty flags, e.g:
2812: * 0 32K 64K 96K 128K
2813: * | |//////||/////| |//|
2814: *
2815: * In above case, [32K, 96K) is asynchronously submitted
2816: * for compression, and [124K, 128K) needs to be written back.
2817: *
2818: * If we didn't wait writeback for page 64K, [128K, 128K)
2819: * won't be submitted as the page still has writeback flag
2820: * and will be skipped in the next check.
2821: *
2822: * This mixed writeback and dirty case is only possible for
2823: * subpage case.
2824: *
2825: * TODO: Remove this check after migrating compression to
2826: * regular submission.
2827: */
2828: if (wbc->sync_mode != WB_SYNC_NONE ||
2829: btrfs_is_subpage(inode_to_fs_info(inode), folio)) {
2830: if (folio_test_writeback(folio))
2831: submit_write_bio(bio_ctrl, 0);
2832: folio_wait_writeback(folio);
2833: }
2834:
2835: if (folio_test_writeback(folio) ||
2836: !folio_test_dirty(folio)) {
2837: folio_unlock(folio);
2838: continue;
2839: }
2840:
2841: /* Locked for writeback; revoke writable mmap PTEs before using the data. */
2842: folio_mkclean(folio);
2843: ret = extent_writepage(folio, bio_ctrl);
2844: if (ret < 0) {
2845: done = true;
2846: break;
2847: }
2848:
2849: /*
2850: * The filesystem may choose to bump up nr_to_write.
2851: * We have to make sure to honor the new nr_to_write
2852: * at any time.
2853: */
2854: nr_to_write_done = (wbc->sync_mode == WB_SYNC_NONE &&
2855: wbc->nr_to_write <= 0);
2856: }
2857: folio_batch_release(&fbatch);
2858: cond_resched();
2859: }
2860: if (!scanned && !done) {
2861: /*
2862: * We hit the last page and there is more work to be done: wrap
2863: * back to the start of the file
2864: */
2865: scanned = true;
2866: index = 0;
2867:
2868: /*
2869: * If we're looping we could run into a page that is locked by a
2870: * writer and that writer could be waiting on writeback for a
2871: * page in our current bio, and thus deadlock, so flush the
2872: * write bio here.
2873: */
2874: submit_write_bio(bio_ctrl, 0);
2875: goto retry;
2876: }
2877:
2878: if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
2879: mapping->writeback_index = done_index;
2880:
2881: btrfs_add_delayed_iput(BTRFS_I(inode));
2882: return ret;
2883: }
SourceFile:fs/btrfs/extent_io.c]
|
| 131/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 47258
output: 41
thoughts: 0
|
| 132/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_page_mkwrite]
Results: map[SourceCode:1800: /*
1801: * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1802: * called from a page fault handler when a page is first dirtied. Hence we must
1803: * be careful to check for EOF conditions here. We set the page up correctly
1804: * for a written page which means we get ENOSPC checking when writing into
1805: * holes and correct delalloc and unwritten extent mapping on filesystems that
1806: * support these features.
1807: *
1808: * We are not allowed to take the i_mutex here so we have to play games to
1809: * protect against truncate races as the page could now be beyond EOF. Because
1810: * truncate_setsize() writes the inode size before removing pages, once we have
1811: * the page lock we can determine safely if the page is beyond EOF. If it is not
1812: * beyond EOF, then the page is guaranteed safe against truncation until we
1813: * unlock the page.
1814: */
1815: static vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
1816: {
1817: struct page *page = vmf->page;
1818: struct folio *folio = page_folio(page);
1819: struct btrfs_inode *inode = BTRFS_I(file_inode(vmf->vma->vm_file));
1820: struct btrfs_fs_info *fs_info = inode->root->fs_info;
1821: struct extent_io_tree *io_tree = &inode->io_tree;
1822: struct btrfs_ordered_extent *ordered;
1823: struct extent_state *cached_state = NULL;
1824: struct extent_changeset *data_reserved = NULL;
1825: unsigned long zero_start;
1826: loff_t size;
1827: size_t fsize = folio_size(folio);
1828: int ret;
1829: bool only_release_metadata = false;
1830: u64 reserved_space;
1831: u64 page_start;
1832: u64 page_end;
1833: u64 end;
1834:
1835: reserved_space = fsize;
1836:
1837: sb_start_pagefault(inode->vfs_inode.i_sb);
1838: page_start = folio_pos(folio);
1839: page_end = page_start + folio_size(folio) - 1;
1840: end = page_end;
1841:
1842: /*
1843: * Reserving delalloc space after obtaining the page lock can lead to
1844: * deadlock. For example, if a dirty page is locked by this function
1845: * and the call to btrfs_delalloc_reserve_space() ends up triggering
1846: * dirty page write out, then the btrfs_writepages() function could
1847: * end up waiting indefinitely to get a lock on the page currently
1848: * being processed by btrfs_page_mkwrite() function.
1849: */
1850: ret = btrfs_check_data_free_space(inode, &data_reserved, page_start,
1851: reserved_space, false);
1852: if (ret < 0) {
1853: size_t write_bytes = reserved_space;
1854:
1855: if (btrfs_check_nocow_lock(inode, page_start, &write_bytes, false) <= 0)
1856: goto out_noreserve;
1857:
1858: only_release_metadata = true;
1859:
1860: /*
1861: * Can't write the whole range, there may be shared extents or
1862: * holes in the range, bail out with @only_release_metadata set
1863: * to true so that we unlock the nocow lock before returning the
1864: * error.
1865: */
1866: if (write_bytes < reserved_space)
1867: goto out_noreserve;
1868: }
1869: ret = btrfs_delalloc_reserve_metadata(inode, reserved_space,
1870: reserved_space, false);
1871: if (ret < 0) {
1872: if (!only_release_metadata)
1873: btrfs_free_reserved_data_space(inode, data_reserved,
1874: page_start, reserved_space);
1875: goto out_noreserve;
1876: }
1877:
1878: ret = file_update_time(vmf->vma->vm_file);
1879: if (ret < 0)
1880: goto out;
1881: again:
1882: down_read(&inode->i_mmap_lock);
1883: folio_lock(folio);
1884: size = i_size_read(&inode->vfs_inode);
1885:
1886: if ((folio->mapping != inode->vfs_inode.i_mapping) ||
1887: (page_start >= size)) {
1888: /* Page got truncated out from underneath us. */
1889: goto out_unlock;
1890: }
1891: folio_wait_writeback(folio);
1892:
1893: btrfs_lock_extent(io_tree, page_start, page_end, &cached_state);
1894: ret = set_folio_extent_mapped(folio);
1895: if (ret < 0) {
1896: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1897: goto out_unlock;
1898: }
1899:
1900: /*
1901: * We can't set the delalloc bits if there are pending ordered
1902: * extents. Drop our locks and wait for them to finish.
1903: */
1904: ordered = btrfs_lookup_ordered_range(inode, page_start, fsize);
1905: if (ordered) {
1906: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1907: folio_unlock(folio);
1908: up_read(&inode->i_mmap_lock);
1909: btrfs_start_ordered_extent(ordered);
1910: btrfs_put_ordered_extent(ordered);
1911: goto again;
1912: }
1913:
1914: if (folio_contains(folio, (size - 1) >> PAGE_SHIFT)) {
1915: reserved_space = round_up(size - page_start, fs_info->sectorsize);
1916: if (reserved_space < fsize) {
1917: const u64 to_free = fsize - reserved_space;
1918:
1919: end = page_start + reserved_space - 1;
1920: if (only_release_metadata)
1921: btrfs_delalloc_release_metadata(inode, to_free, true);
1922: else
1923: btrfs_delalloc_release_space(inode, data_reserved,
1924: end + 1, to_free, true);
1925: }
1926: }
1927:
1928: ret = btrfs_reset_extent_delalloc(inode, page_start, end, 0, &cached_state);
1929: if (ret < 0) {
1930: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1931: goto out_unlock;
1932: }
1933:
1934: /* Page is wholly or partially inside EOF. */
1935: if (page_start + folio_size(folio) > size)
1936: zero_start = offset_in_folio(folio, size);
1937: else
1938: zero_start = fsize;
1939:
1940: if (zero_start != fsize)
1941: folio_zero_range(folio, zero_start, folio_size(folio) - zero_start);
1942:
1943: btrfs_folio_set_dirty(fs_info, folio, page_start, end + 1 - page_start);
1944: btrfs_folio_set_uptodate(fs_info, folio, page_start, end + 1 - page_start);
1945:
1946: btrfs_set_inode_last_sub_trans(inode);
1947:
1948: if (only_release_metadata)
1949: btrfs_set_extent_bit(io_tree, page_start, end, EXTENT_NORESERVE,
1950: &cached_state);
1951:
1952: btrfs_unlock_extent(io_tree, page_start, page_end, &cached_state);
1953: up_read(&inode->i_mmap_lock);
1954:
1955: btrfs_delalloc_release_extents(inode, fsize);
1956: if (only_release_metadata)
1957: btrfs_check_nocow_unlock(inode);
1958: sb_end_pagefault(inode->vfs_inode.i_sb);
1959: extent_changeset_free(data_reserved);
1960: return VM_FAULT_LOCKED;
1961:
1962: out_unlock:
1963: folio_unlock(folio);
1964: up_read(&inode->i_mmap_lock);
1965: out:
1966: btrfs_delalloc_release_extents(inode, fsize);
1967: if (only_release_metadata)
1968: btrfs_delalloc_release_metadata(inode, reserved_space, true);
1969: else
1970: btrfs_delalloc_release_space(inode, data_reserved, page_start,
1971: reserved_space, true);
1972: out_noreserve:
1973: if (only_release_metadata)
1974: btrfs_check_nocow_unlock(inode);
1975:
1976: sb_end_pagefault(inode->vfs_inode.i_sb);
1977:
1978: extent_changeset_free(data_reserved);
1979:
1980: if (ret < 0)
1981: return vmf_error(ret);
1982:
1983: /* Make the VM retry the fault. */
1984: return VM_FAULT_NOPAGE;
1985: }
SourceFile:fs/btrfs/file.c]
|
| 133/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 50441
output: 42
thoughts: 0
|
| 134/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_start_ordered_extent]
Results: map[SourceCode: 201: static inline void btrfs_start_ordered_extent(struct btrfs_ordered_extent *entry)
202: {
203: return btrfs_start_ordered_extent_nowriteback(entry, 0, 0);
204: }
SourceFile:fs/btrfs/ordered-data.h]
|
| 135/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 50594
output: 48
thoughts: 0
|
| 136/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/ordered-data.c Name:btrfs_start_ordered_extent_nowriteback]
Results: map[SourceCode: 860: /*
861: * Start IO and wait for a given ordered extent to finish.
862: *
863: * Wait on page writeback for all the pages in the extent but not in
864: * [@nowriteback_start, @nowriteback_start + @nowriteback_len) and the
865: * IO completion code to insert metadata into the btree corresponding to the extent.
866: */
867: void btrfs_start_ordered_extent_nowriteback(struct btrfs_ordered_extent *entry,
868: u64 nowriteback_start, u32 nowriteback_len)
869: {
870: u64 start = entry->file_offset;
871: u64 end = start + entry->num_bytes - 1;
872: struct btrfs_inode *inode = entry->inode;
873: bool freespace_inode;
874:
875: trace_btrfs_ordered_extent_start(inode, entry);
876:
877: /*
878: * If this is a free space inode do not take the ordered extents lockdep
879: * map.
880: */
881: freespace_inode = btrfs_is_free_space_inode(inode);
882:
883: /*
884: * pages in the range can be dirty, clean or writeback. We
885: * start IO on any dirty ones so the wait doesn't stall waiting
886: * for the flusher thread to find them
887: */
888: if (!test_bit(BTRFS_ORDERED_DIRECT, &entry->flags)) {
889: if (!nowriteback_len) {
890: filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start, end);
891: } else {
892: if (start < nowriteback_start)
893: filemap_fdatawrite_range(inode->vfs_inode.i_mapping, start,
894: nowriteback_start - 1);
895: if (nowriteback_start + nowriteback_len < end)
896: filemap_fdatawrite_range(inode->vfs_inode.i_mapping,
897: nowriteback_start + nowriteback_len,
898: end);
899: }
900: }
901:
902: if (!freespace_inode)
903: btrfs_might_wait_for_event(inode->root->fs_info, btrfs_ordered_extent);
904: wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE, &entry->flags));
905: }
SourceFile:fs/btrfs/ordered-data.c]
|
| 137/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 51470
output: 42
thoughts: 0
|
| 138/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/ordered-data.c Name:filemap_fdatawrite_range]
Results: map[SourceCode: 395: /**
396: * filemap_fdatawrite_range - start writeback on mapping dirty pages in range
397: * @mapping: address space structure to write
398: * @start: offset in bytes where the range starts
399: * @end: offset in bytes where the range ends (inclusive)
400: *
401: * Start writeback against all of a mapping's dirty pages that lie
402: * within the byte offsets <start, end> inclusive.
403: *
404: * This is a data integrity operation that waits upon dirty or in writeback
405: * pages.
406: *
407: * Return: %0 on success, negative error code otherwise.
408: */
409: int filemap_fdatawrite_range(struct address_space *mapping, loff_t start,
410: loff_t end)
411: {
412: return filemap_writeback(mapping, start, end, WB_SYNC_ALL, NULL);
413: }
SourceFile:mm/filemap.c]
|
| 139/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 51842
output: 35
thoughts: 0
|
| 140/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/filemap.c Name:filemap_writeback]
Results: map[SourceCode: 370: static int filemap_writeback(struct address_space *mapping, loff_t start,
371: loff_t end, enum writeback_sync_modes sync_mode,
372: long *nr_to_write)
373: {
374: struct writeback_control wbc = {
375: .sync_mode = sync_mode,
376: .nr_to_write = nr_to_write ? *nr_to_write : LONG_MAX,
377: .range_start = start,
378: .range_end = end,
379: };
380: int ret;
381:
382: if (!mapping_can_writeback(mapping) ||
383: !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
384: return 0;
385:
386: wbc_attach_fdatawrite_inode(&wbc, mapping->host);
387: ret = do_writepages(mapping, &wbc);
388: wbc_detach_inode(&wbc);
389:
390: if (!ret && nr_to_write)
391: *nr_to_write = wbc.nr_to_write;
392: return ret;
393: }
SourceFile:mm/filemap.c]
|
| 141/2 |
2026/09/20 01:03 |
llm |
expert |
0m
Model:
Tokens:
input: 52286
output: 34
thoughts: 0
|
| 142/2 |
2026/09/20 01:03 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/filemap.c Name:do_writepages]
Results: map[SourceCode:2549: int do_writepages(struct address_space *mapping, struct writeback_control *wbc)
2550: {
2551: int ret;
2552: struct bdi_writeback *wb;
2553:
2554: if (wbc->nr_to_write <= 0)
2555: return 0;
2556: wb = inode_to_wb_wbc(mapping->host, wbc);
2557: wb_bandwidth_estimate_start(wb);
2558: while (1) {
2559: if (mapping->a_ops->writepages)
2560: ret = mapping->a_ops->writepages(mapping, wbc);
2561: else
2562: /* deal with chardevs and other special files */
2563: ret = 0;
2564: if (ret != -ENOMEM || wbc->sync_mode != WB_SYNC_ALL)
2565: break;
2566:
2567: /*
2568: * Lacking an allocation context or the locality or writeback
2569: * state of any of the inode's pages, throttle based on
2570: * writeback activity on the local node. It's as good a
2571: * guess as any.
2572: */
2573: reclaim_throttle(NODE_DATA(numa_node_id()),
2574: VMSCAN_THROTTLE_WRITEBACK);
2575: }
2576: /*
2577: * Usually few pages are written by now from those we've just submitted
2578: * but if there's constant writeback being submitted, this makes sure
2579: * writeback bandwidth is updated once in a while.
2580: */
2581: if (time_is_before_jiffies(READ_ONCE(wb->bw_time_stamp) +
2582: BANDWIDTH_INTERVAL))
2583: wb_update_bandwidth(wb);
2584: return ret;
2585: }
SourceFile:mm/page-writeback.c]
|
| 143/2 |
2026/09/20 01:03 |
llm |
expert |
1m
Model:
Tokens:
input: 52927
output: 41
thoughts: 0
|
| 144/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_is_subpage]
Results: map[SourceCode: 98: static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
99: struct folio *folio)
100: {
101: if (folio->mapping && folio->mapping->host)
102: ASSERT(is_data_inode(BTRFS_I(folio->mapping->host)));
103: return fs_info->sectorsize < folio_size(folio);
104: }
SourceFile:fs/btrfs/subpage.h]
|
| 145/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 53128
output: 40
thoughts: 0
|
| 146/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_folio_state]
Results: map[SourceCode: 54: /*
55: * Structure to trace status of each sector inside a page, attached to
56: * page::private for both data and metadata inodes.
57: */
58: struct btrfs_folio_state {
59: /* Common members for both data and metadata pages */
60: spinlock_t lock;
61: union {
62: /*
63: * Structures only used by metadata
64: *
65: * @eb_refs should only be operated under private_lock, as it
66: * manages whether the btrfs_folio_state can be detached.
67: */
68: atomic_t eb_refs;
69:
70: /*
71: * Structures only used by data,
72: *
73: * How many sectors inside the page is locked.
74: */
75: atomic_t nr_locked;
76: };
77: unsigned long bitmaps[];
78: };
SourceFile:fs/btrfs/subpage.h]
|
| 147/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 53484
output: 38
thoughts: 0
|
| 148/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:folio_get_private]
Results: map[SourceCode: 690: static inline void *folio_get_private(const struct folio *folio)
691: {
692: return folio->private;
693: }
SourceFile:include/linux/mm_types.h]
|
| 149/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 53605
output: 38
thoughts: 0
|
| 150/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:folio_test_private]
Results: map[SourceCode: 576: /*
577: * Private page markings that may be used by the filesystem that owns the page
578: * for its own purposes.
579: * - PG_private and PG_private_2 cause release_folio() and co to be invoked
580: */
581: PAGEFLAG(Private, private, PF_ANY)
SourceFile:include/linux/page-flags.h]
|
| 151/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 53772
output: 38
thoughts: 0
|
| 152/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:folio_attach_private]
Results: map[SourceCode: 585: /**
586: * folio_attach_private - Attach private data to a folio.
587: * @folio: Folio to attach data to.
588: * @data: Data to attach to folio.
589: *
590: * Attaching private data to a folio increments the page's reference count.
591: * The data must be detached before the folio will be freed.
592: */
593: static inline void folio_attach_private(struct folio *folio, void *data)
594: {
595: folio_get(folio);
596: folio->private = data;
597: folio_set_private(folio);
598: }
SourceFile:include/linux/pagemap.h]
|
| 153/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 54047
output: 38
thoughts: 0
|
| 154/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:folio_detach_private]
Results: map[SourceCode: 619: /**
620: * folio_detach_private - Detach private data from a folio.
621: * @folio: Folio to detach data from.
622: *
623: * Removes the data that was previously attached to the folio and decrements
624: * the refcount on the page.
625: *
626: * Return: Data that was attached to the folio.
627: */
628: static inline void *folio_detach_private(struct folio *folio)
629: {
630: void *data = folio_get_private(folio);
631:
632: if (!folio_test_private(folio))
633: return NULL;
634: folio_clear_private(folio);
635: folio->private = NULL;
636: folio_put(folio);
637:
638: return data;
639: }
SourceFile:include/linux/pagemap.h]
|
| 155/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 54392
output: 38
thoughts: 0
|
| 156/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:folio_invalidate]
Results: map[SourceCode: 120: /**
121: * folio_invalidate - Invalidate part or all of a folio.
122: * @folio: The folio which is affected.
123: * @offset: start of the range to invalidate
124: * @length: length of the range to invalidate
125: *
126: * folio_invalidate() is called when all or part of the folio has become
127: * invalidated by a truncate operation.
128: *
129: * folio_invalidate() does not have to release all buffers, but it must
130: * ensure that no dirty buffer is left outside @offset and that no I/O
131: * is underway against any of the blocks which are outside the truncation
132: * point. Because the caller is about to free (and possibly reuse) those
133: * blocks on-disk.
134: */
135: void folio_invalidate(struct folio *folio, size_t offset, size_t length)
136: {
137: const struct address_space_operations *aops = folio->mapping->a_ops;
138:
139: if (aops->invalidate_folio)
140: aops->invalidate_folio(folio, offset, length);
141: }
SourceFile:mm/truncate.c]
|
| 157/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 54815
output: 39
thoughts: 0
|
| 158/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:wait_subpage_spinlock]
Results: map[SourceCode:7601: /*
7602: * For release_folio() and invalidate_folio() we have a race window where
7603: * folio_end_writeback() is called but the subpage spinlock is not yet released.
7604: * If we continue to release/invalidate the page, we could cause use-after-free
7605: * for subpage spinlock. So this function is to spin and wait for subpage
7606: * spinlock.
7607: */
7608: static void wait_subpage_spinlock(struct folio *folio)
7609: {
7610: struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
7611: struct btrfs_folio_state *bfs;
7612:
7613: if (!btrfs_is_subpage(fs_info, folio))
7614: return;
7615:
7616: ASSERT(folio_test_private(folio) && folio_get_private(folio));
7617: bfs = folio_get_private(folio);
7618:
7619: /*
7620: * This may look insane as we just acquire the spinlock and release it,
7621: * without doing anything. But we just want to make sure no one is
7622: * still holding the subpage spinlock.
7623: * And since the page is not dirty nor writeback, and we have page
7624: * locked, the only possible way to hold a spinlock is from the endio
7625: * function to clear page writeback.
7626: *
7627: * Here we just acquire the spinlock so that all existing callers
7628: * should exit and we're safe to release/invalidate the page.
7629: */
7630: spin_lock_irq(&bfs->lock);
7631: spin_unlock_irq(&bfs->lock);
7632: }
SourceFile:fs/btrfs/inode.c]
|
| 159/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 55425
output: 25
thoughts: 0
|
| 160/2 |
2026/09/20 01:05 |
tool |
codesearch-struct-layout |
0m
Args: map[Name:btrfs_folio_state]
Results: map[Fields:[map[Name:lock OffsetBits:0 SizeBits:512] map[Name: OffsetBits:512 SizeBits:32] map[Name:bitmaps OffsetBits:576 SizeBits:0]]]
|
| 161/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 55519
output: 39
thoughts: 0
|
| 162/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:extent_writepage]
Results: map[SourceCode:2032: /*
2033: * the writepage semantics are similar to regular writepage. extent
2034: * records are inserted to lock ranges in the tree, and as dirty areas
2035: * are found, they are marked writeback. Then the lock bits are removed
2036: * and the end_io handler clears the writeback ranges
2037: *
2038: * Return 0 if everything goes well.
2039: * Return <0 for error.
2040: */
2041: static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl)
2042: {
2043: struct btrfs_inode *inode = BTRFS_I(folio->mapping->host);
2044: struct btrfs_fs_info *fs_info = inode->root->fs_info;
2045: int ret;
2046: size_t pg_offset;
2047: loff_t i_size = i_size_read(&inode->vfs_inode);
2048: const pgoff_t end_index = i_size >> PAGE_SHIFT;
2049: const unsigned int blocks_per_folio = btrfs_blocks_per_folio(fs_info, folio);
2050:
2051: trace_extent_writepage(folio, &inode->vfs_inode, bio_ctrl->wbc);
2052:
2053: WARN_ON(!folio_test_locked(folio));
2054:
2055: pg_offset = offset_in_folio(folio, i_size);
2056: if (folio->index > end_index ||
2057: (folio->index == end_index && !pg_offset)) {
2058: folio_invalidate(folio, 0, folio_size(folio));
2059: folio_unlock(folio);
2060: return 0;
2061: }
2062:
2063: if (folio_contains(folio, end_index))
2064: folio_zero_range(folio, pg_offset, folio_size(folio) - pg_offset);
2065:
2066: bio_ctrl_init_submit_bitmap(fs_info, folio, bio_ctrl);
2067: /*
2068: * If the page is dirty but without private set, it's marked dirty
2069: * without informing the fs.
2070: * Nowadays that is a bug, since the introduction of
2071: * pin_user_pages*().
2072: *
2073: * So here we check if the page has private set to rule out such
2074: * case.
2075: */
2076: if (unlikely(!folio_test_private(folio))) {
2077: DEBUG_WARN();
2078: btrfs_err_rl(fs_info,
2079: "root %lld ino %llu folio %llu is marked dirty without notifying the fs",
2080: btrfs_root_id(inode->root),
2081: btrfs_ino(inode), folio_pos(folio));
2082: ret = -EUCLEAN;
2083: goto done;
2084: }
2085:
2086: ret = set_folio_extent_mapped(folio);
2087: if (ret < 0)
2088: goto done;
2089:
2090: ret = writepage_delalloc(inode, folio, bio_ctrl);
2091: if (ret == 1)
2092: return 0;
2093: if (ret)
2094: goto done;
2095:
2096: ret = extent_writepage_io(inode, folio, folio_pos(folio),
2097: folio_size(folio), bio_ctrl, i_size);
2098: if (ret == 1)
2099: return 0;
2100: if (unlikely(ret < 0))
2101: btrfs_err_rl(fs_info,
2102: "failed to submit blocks, root=%lld inode=%llu folio=%llu submit_bitmap=%*pbl: %pe",
2103: btrfs_root_id(inode->root), btrfs_ino(inode),
2104: folio_pos(folio), blocks_per_folio,
2105: bio_ctrl->submit_bitmap, ERR_PTR(ret));
2106:
2107: bio_ctrl->wbc->nr_to_write--;
2108:
2109: done:
2110: if (ret < 0)
2111: mapping_set_error(folio->mapping, ret);
2112: /*
2113: * Only unlock ranges that are submitted. As there can be some async
2114: * submitted ranges inside the folio.
2115: */
2116: btrfs_folio_end_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap);
2117: ASSERT(ret <= 0);
2118: return ret;
2119: }
SourceFile:fs/btrfs/extent_io.c]
|
| 163/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 57030
output: 40
thoughts: 0
|
| 164/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_invalidate_folio]
Results: map[SourceCode:7672: static void btrfs_invalidate_folio(struct folio *folio, size_t offset,
7673: size_t length)
7674: {
7675: struct btrfs_inode *inode = folio_to_inode(folio);
7676: struct btrfs_fs_info *fs_info = inode->root->fs_info;
7677: struct extent_io_tree *tree = &inode->io_tree;
7678: struct extent_state *cached_state = NULL;
7679: u64 page_start = folio_pos(folio);
7680: u64 page_end = page_start + folio_size(folio) - 1;
7681: u64 cur;
7682: int inode_evicting = inode_state_read_once(&inode->vfs_inode) & I_FREEING;
7683:
7684: /*
7685: * We have folio locked so no new ordered extent can be created on this
7686: * page, nor bio can be submitted for this folio.
7687: *
7688: * But already submitted bio can still be finished on this folio.
7689: * Furthermore, endio function won't skip folio which has Ordered
7690: * already cleared, so it's possible for endio and
7691: * invalidate_folio to do the same ordered extent accounting twice
7692: * on one folio.
7693: *
7694: * So here we wait for any submitted bios to finish, so that we won't
7695: * do double ordered extent accounting on the same folio.
7696: */
7697: folio_wait_writeback(folio);
7698: wait_subpage_spinlock(folio);
7699:
7700: /*
7701: * The invalidated blocks are going away; drop any fixup blocks among
7702: * them, data included, as they have no space reservation.
7703: */
7704: btrfs_folio_clear_fixup_dirty(fs_info, folio, page_start + offset, length);
7705:
7706: /*
7707: * For subpage case, we have call sites like
7708: * btrfs_punch_hole_lock_range() which passes range not aligned to
7709: * sectorsize.
7710: * If the range doesn't cover the full folio, we don't need to and
7711: * shouldn't clear page extent mapped, as folio->private can still
7712: * record subpage dirty bits for other part of the range.
7713: *
7714: * For cases that invalidate the full folio even the range doesn't
7715: * cover the full folio, like invalidating the last folio, we're
7716: * still safe to wait for ordered extent to finish.
7717: */
7718: if (!(offset == 0 && length == folio_size(folio))) {
7719: btrfs_release_folio(folio, GFP_NOFS);
7720: return;
7721: }
7722:
7723: if (!inode_evicting)
7724: btrfs_lock_extent(tree, page_start, page_end, &cached_state);
7725:
7726: cur = page_start;
7727: while (cur < page_end) {
7728: struct btrfs_ordered_extent *ordered;
7729: u64 range_end;
7730: u32 range_len;
7731: u32 extra_flags = 0;
7732:
7733: ordered = btrfs_lookup_first_ordered_range(inode, cur,
7734: page_end + 1 - cur);
7735: if (!ordered) {
7736: range_end = page_end;
7737: /*
7738: * No ordered extent covering this range, we are safe
7739: * to delete all extent states in the range.
7740: */
7741: extra_flags = EXTENT_CLEAR_ALL_BITS;
7742: goto next;
7743: }
7744: if (ordered->file_offset > cur) {
7745: /*
7746: * There is a range between [cur, oe->file_offset) not
7747: * covered by any ordered extent.
7748: * We are safe to delete all extent states, and handle
7749: * the ordered extent in the next iteration.
7750: */
7751: range_end = ordered->file_offset - 1;
7752: extra_flags = EXTENT_CLEAR_ALL_BITS;
7753: goto next;
7754: }
7755:
7756: range_end = min(ordered->file_offset + ordered->num_bytes - 1,
7757: page_end);
7758: ASSERT(range_end + 1 - cur < U32_MAX);
7759: range_len = range_end + 1 - cur;
7760: /*
7761: * If the range is not dirty, the range has been submitted and
7762: * since we have waited for the writeback, endio has been
7763: * executed, thus we must skip the range to avoid double
7764: * accounting for the ordered extent.
7765: */
7766: if (!btrfs_folio_test_dirty(fs_info, folio, cur, range_len))
7767: goto next;
7768:
7769: /*
7770: * The range is dirty meaning it has not been submitted.
7771: * Here we need to truncate the OE range as the range will never
7772: * be submitted.
7773: *
7774: * IO on this page will never be started, so we need to account
7775: * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
7776: * here, must leave that up for the ordered extent completion.
7777: *
7778: * This will also unlock the range for incoming
7779: * btrfs_finish_ordered_io().
7780: */
7781: if (!inode_evicting)
7782: btrfs_clear_extent_bit(tree, cur, range_end,
7783: EXTENT_DELALLOC |
7784: EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7785: EXTENT_DEFRAG, &cached_state);
7786:
7787: btrfs_mark_ordered_extent_truncated(ordered, cur - ordered->file_offset);
7788:
7789: /*
7790: * If the ordered extent has finished, we're safe to delete all
7791: * the extent states of the range, otherwise
7792: * btrfs_finish_ordered_io() will get executed by endio for
7793: * other pages, so we can't delete extent states.
7794: */
7795: if (btrfs_dec_test_ordered_pending(inode, &ordered,
7796: cur, range_end + 1 - cur)) {
7797: btrfs_finish_ordered_io(ordered);
7798: /*
7799: * The ordered extent has finished, now we're again
7800: * safe to delete all extent states of the range.
7801: */
7802: extra_flags = EXTENT_CLEAR_ALL_BITS;
7803: }
7804: next:
7805: if (ordered)
7806: btrfs_put_ordered_extent(ordered);
7807: /*
7808: * Qgroup reserved space handler
7809: * Sector(s) here will be either:
7810: *
7811: * 1) Already written to disk or bio already finished
7812: * Then its QGROUP_RESERVED bit in io_tree is already cleared.
7813: * Qgroup will be handled by its qgroup_record then.
7814: * btrfs_qgroup_free_data() call will do nothing here.
7815: *
7816: * 2) Not written to disk yet
7817: * Then btrfs_qgroup_free_data() call will clear the
7818: * QGROUP_RESERVED bit of its io_tree, and free the qgroup
7819: * reserved data space.
7820: * Since the IO will never happen for this page.
7821: */
7822: btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL);
7823: if (!inode_evicting)
7824: btrfs_clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
7825: EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7826: EXTENT_DEFRAG | extra_flags,
7827: &cached_state);
7828: cur = range_end + 1;
7829: }
7830: btrfs_folio_clear_dirty(fs_info, folio, page_start, folio_size(folio));
7831: btrfs_clear_folio_dirty_tag(folio);
7832: if (!inode_evicting)
7833: __btrfs_release_folio(folio, GFP_NOFS);
7834: clear_folio_extent_mapped(folio);
7835: }
SourceFile:fs/btrfs/inode.c]
|
| 165/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 59808
output: 40
thoughts: 0
|
| 166/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_release_folio]
Results: map[SourceCode:7650: static bool btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7651: {
7652: if (folio_test_writeback(folio) || folio_test_dirty(folio))
7653: return false;
7654: return __btrfs_release_folio(folio, gfp_flags);
7655: }
SourceFile:fs/btrfs/inode.c]
|
| 167/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 59988
output: 41
thoughts: 0
|
| 168/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:__btrfs_release_folio]
Results: map[SourceCode:7640: static bool __btrfs_release_folio(struct folio *folio, gfp_t gfp_flags)
7641: {
7642: if (try_release_extent_mapping(folio, gfp_flags)) {
7643: wait_subpage_spinlock(folio);
7644: clear_folio_extent_mapped(folio);
7645: return true;
7646: }
7647: return false;
7648: }
SourceFile:fs/btrfs/inode.c]
|
| 169/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 60195
output: 42
thoughts: 0
|
| 170/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:try_release_extent_mapping]
Results: map[SourceCode:3069: /*
3070: * a helper for release_folio. As long as there are no locked extents
3071: * in the range corresponding to the page, both state records and extent
3072: * map records are removed
3073: */
3074: bool try_release_extent_mapping(struct folio *folio, gfp_t mask)
3075: {
3076: u64 start = folio_pos(folio);
3077: u64 end = start + folio_size(folio) - 1;
3078: struct btrfs_inode *inode = folio_to_inode(folio);
3079: struct extent_io_tree *io_tree = &inode->io_tree;
3080:
3081: while (start <= end) {
3082: const u64 cur_gen = btrfs_get_fs_generation(inode->root->fs_info);
3083: const u64 len = end - start + 1;
3084: struct extent_map_tree *extent_tree = &inode->extent_tree;
3085: struct extent_map *em;
3086:
3087: write_lock(&extent_tree->lock);
3088: em = btrfs_lookup_extent_mapping(extent_tree, start, len);
3089: if (!em) {
3090: write_unlock(&extent_tree->lock);
3091: break;
3092: }
3093: if ((em->flags & EXTENT_FLAG_PINNED) || em->start != start) {
3094: write_unlock(&extent_tree->lock);
3095: btrfs_free_extent_map(em);
3096: break;
3097: }
3098: if (btrfs_test_range_bit_exists(io_tree, em->start,
3099: btrfs_extent_map_end(em) - 1,
3100: EXTENT_LOCKED))
3101: goto next;
3102: /*
3103: * If it's not in the list of modified extents, used by a fast
3104: * fsync, we can remove it. If it's being logged we can safely
3105: * remove it since fsync took an extra reference on the em.
3106: */
3107: if (list_empty(&em->list) || (em->flags & EXTENT_FLAG_LOGGING))
3108: goto remove_em;
3109: /*
3110: * If it's in the list of modified extents, remove it only if
3111: * its generation is older then the current one, in which case
3112: * we don't need it for a fast fsync. Otherwise don't remove it,
3113: * we could be racing with an ongoing fast fsync that could miss
3114: * the new extent.
3115: */
3116: if (em->generation >= cur_gen)
3117: goto next;
3118: remove_em:
3119: /*
3120: * We only remove extent maps that are not in the list of
3121: * modified extents or that are in the list but with a
3122: * generation lower then the current generation, so there is no
3123: * need to set the full fsync flag on the inode (it hurts the
3124: * fsync performance for workloads with a data size that exceeds
3125: * or is close to the system's memory).
3126: */
3127: btrfs_remove_extent_mapping(inode, em);
3128: /* Once for the inode's extent map tree. */
3129: btrfs_free_extent_map(em);
3130: next:
3131: start = btrfs_extent_map_end(em);
3132: write_unlock(&extent_tree->lock);
3133:
3134: /* Once for us, for the lookup_extent_mapping() reference. */
3135: btrfs_free_extent_map(em);
3136:
3137: if (need_resched()) {
3138: /*
3139: * If we need to resched but we can't block just exit
3140: * and leave any remaining extent maps.
3141: */
3142: if (!gfpflags_allow_blocking(mask))
3143: break;
3144:
3145: cond_resched();
3146: }
3147: }
3148: return try_release_extent_state(io_tree, folio);
3149: }
SourceFile:fs/btrfs/extent_io.c]
|
| 171/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 61584
output: 42
thoughts: 0
|
| 172/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:try_release_extent_state]
Results: map[SourceCode:3022: /*
3023: * A helper for struct address_space_operations::release_folio, this tests for
3024: * areas of the folio that are locked or under IO and drops the related state
3025: * bits if it is safe to drop the folio.
3026: */
3027: static bool try_release_extent_state(struct extent_io_tree *tree,
3028: struct folio *folio)
3029: {
3030: struct extent_state *cached_state = NULL;
3031: u64 start = folio_pos(folio);
3032: u64 end = start + folio_size(folio) - 1;
3033: u32 range_bits;
3034: u32 clear_bits;
3035: bool ret = false;
3036: int ret2;
3037:
3038: btrfs_get_range_bits(tree, start, end, &range_bits, &cached_state);
3039:
3040: /*
3041: * We can release the folio if it's locked only for ordered extent
3042: * completion, since that doesn't require using the folio.
3043: */
3044: if ((range_bits & EXTENT_LOCKED) &&
3045: !(range_bits & EXTENT_FINISHING_ORDERED))
3046: goto out;
3047:
3048: clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW |
3049: EXTENT_CTLBITS | EXTENT_QGROUP_RESERVED |
3050: EXTENT_FINISHING_ORDERED);
3051: /*
3052: * At this point we can safely clear everything except the locked,
3053: * nodatasum, delalloc new and finishing ordered bits. The delalloc new
3054: * bit will be cleared by ordered extent completion.
3055: */
3056: ret2 = btrfs_clear_extent_bit(tree, start, end, clear_bits, &cached_state);
3057: /*
3058: * If clear_extent_bit failed for enomem reasons, we can't allow the
3059: * release to continue.
3060: */
3061: if (ret2 == 0)
3062: ret = true;
3063: out:
3064: btrfs_free_extent_state(cached_state);
3065:
3066: return ret;
3067: }
SourceFile:fs/btrfs/extent_io.c]
|
| 173/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 62387
output: 39
thoughts: 0
|
| 174/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_aops]
Results: map[SourceCode:10807: /*
10808: * btrfs doesn't support the bmap operation because swapfiles
10809: * use bmap to make a mapping of extents in the file. They assume
10810: * these extents won't change over the life of the file and they
10811: * use the bmap result to do IO directly to the drive.
10812: *
10813: * the btrfs bmap call would return logical addresses that aren't
10814: * suitable for IO and they also will change frequently as COW
10815: * operations happen. So, swapfile + btrfs == corruption.
10816: *
10817: * For now we're avoiding this by dropping bmap.
10818: */
10819: static const struct address_space_operations btrfs_aops = {
10820: .read_folio = btrfs_read_folio,
10821: .writepages = btrfs_writepages,
10822: .readahead = btrfs_readahead,
10823: .invalidate_folio = btrfs_invalidate_folio,
10824: .launder_folio = btrfs_launder_folio,
10825: .release_folio = btrfs_release_folio,
10826: .migrate_folio = btrfs_migrate_folio,
10827: .dirty_folio = btrfs_data_dirty_folio,
10828: .error_remove_folio = generic_error_remove_folio,
10829: .swap_activate = btrfs_swap_activate,
10830: .swap_deactivate = btrfs_swap_deactivate,
10831: };
SourceFile:fs/btrfs/inode.c]
|
| 175/2 |
2026/09/20 01:05 |
llm |
expert |
0m
Model:
Tokens:
input: 62954
output: 41
thoughts: 0
|
| 176/2 |
2026/09/20 01:05 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_launder_folio]
Results: map[SourceCode:7634: static int btrfs_launder_folio(struct folio *folio)
7635: {
7636: return btrfs_qgroup_free_data(folio_to_inode(folio), NULL, folio_pos(folio),
7637: folio_size(folio), NULL);
7638: }
SourceFile:fs/btrfs/inode.c]
|
| 177/2 |
2026/09/20 01:05 |
llm |
expert |
1m
Model:
Tokens:
input: 63117
output: 33
thoughts: 0
|
| 178/2 |
2026/09/20 01:06 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/truncate.c Name:folio_launder]
Results: map[SourceCode: 606: static int folio_launder(struct address_space *mapping, struct folio *folio)
607: {
608: if (!folio_test_dirty(folio))
609: return 0;
610: if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL)
611: return 0;
612: return mapping->a_ops->launder_folio(folio);
613: }
SourceFile:mm/truncate.c]
|
| 179/2 |
2026/09/20 01:06 |
llm |
expert |
0m
Model:
Tokens:
input: 63308
output: 35
thoughts: 0
|
| 180/2 |
2026/09/20 01:06 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:mm/truncate.c Name:invalidate_inode_pages2]
Results: map[SourceCode: 753: /**
754: * invalidate_inode_pages2 - remove all pages from an address_space
755: * @mapping: the address_space
756: *
757: * Any pages which are found to be mapped into pagetables are unmapped prior to
758: * invalidation.
759: *
760: * Return: -EBUSY if any pages could not be invalidated.
761: */
762: int invalidate_inode_pages2(struct address_space *mapping)
763: {
764: return invalidate_inode_pages2_range(mapping, 0, -1);
765: }
SourceFile:mm/truncate.c]
|
| 181/2 |
2026/09/20 01:06 |
llm |
expert |
0m
Model:
Tokens:
input: 63562
output: 32
thoughts: 0
|
| 182/2 |
2026/09/20 01:06 |
tool |
grepper |
0m
Args: map[Expression:invalidate_inode_pages2 PathPrefix:fs/btrfs/]
Results: map[Output:fs/btrfs/direct-io.c=36=static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
--
fs/btrfs/direct-io.c-116- * for it to complete) and then invalidate the pages for
fs/btrfs/direct-io.c:117: * this range (through invalidate_inode_pages2_range()),
fs/btrfs/direct-io.c-118- * but that can lead us to a deadlock with a concurrent
--
fs/btrfs/disk-io.c=3301=static void invalidate_and_check_btree_folios(struct btrfs_fs_info *fs_info)
--
fs/btrfs/disk-io.c-3306-
fs/btrfs/disk-io.c:3307: ret = invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
fs/btrfs/disk-io.c-3308- if (likely(ret == 0))
--
fs/btrfs/disk-io.c-3353- rcu_read_unlock();
fs/btrfs/disk-io.c:3354: invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
fs/btrfs/disk-io.c-3355-}
--
fs/btrfs/disk-io.c=4750=static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
--
fs/btrfs/disk-io.c-4773- nofs_flag = memalloc_nofs_save();
fs/btrfs/disk-io.c:4774: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/disk-io.c-4775- memalloc_nofs_restore(nofs_flag);
--
fs/btrfs/disk-io.c=4863=static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
--
fs/btrfs/disk-io.c-4871- nofs_flag = memalloc_nofs_save();
fs/btrfs/disk-io.c:4872: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/disk-io.c-4873- memalloc_nofs_restore(nofs_flag);
--
fs/btrfs/free-space-cache.c=1288=static int __btrfs_wait_cache_io(struct btrfs_root *root,
--
fs/btrfs/free-space-cache.c-1309- if (ret) {
fs/btrfs/free-space-cache.c:1310: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/free-space-cache.c-1311- BTRFS_I(inode)->generation = 0;
--
fs/btrfs/free-space-cache.c=1368=static int __btrfs_write_out_cache(struct inode *inode,
--
fs/btrfs/free-space-cache.c-1502- if (ret) {
fs/btrfs/free-space-cache.c:1503: invalidate_inode_pages2(inode->i_mapping);
fs/btrfs/free-space-cache.c-1504- BTRFS_I(inode)->generation = 0;
--
fs/btrfs/inode.c=9904=ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
--
fs/btrfs/inode.c-10044- goto out_cb;
fs/btrfs/inode.c:10045: ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping,
fs/btrfs/inode.c-10046- start >> PAGE_SHIFT,
--
fs/btrfs/volumes.c=1335=struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
--
fs/btrfs/volumes.c-1362- */
fs/btrfs/volumes.c:1363: invalidate_inode_pages2_range(mapping, bytenr >> PAGE_SHIFT,
fs/btrfs/volumes.c-1364- (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT);
]
|
| 183/2 |
2026/09/20 01:06 |
llm |
expert |
0m
Model:
Tokens:
input: 64792
output: 39
thoughts: 0
|
| 184/2 |
2026/09/20 01:07 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/inode.c Name:btrfs_writepages]
Results: map[SourceCode:2961: int btrfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
2962: {
2963: struct inode *inode = mapping->host;
2964: int ret = 0;
2965: struct btrfs_bio_ctrl bio_ctrl = {
2966: .wbc = wbc,
2967: .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc),
2968: };
2969:
2970: /*
2971: * Allow only a single thread to do the reloc work in zoned mode to
2972: * protect the write pointer updates.
2973: */
2974: btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
2975: ret = extent_write_cache_pages(mapping, &bio_ctrl);
2976: submit_write_bio(&bio_ctrl, ret);
2977: btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
2978: return ret;
2979: }
SourceFile:fs/btrfs/extent_io.c]
|
| 185/2 |
2026/09/20 01:07 |
llm |
expert |
0m
Model:
Tokens:
input: 65180
output: 42
thoughts: 0
|
| 186/2 |
2026/09/20 01:07 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:fs/btrfs/extent_io.c Name:extent_write_cache_pages]
Results: map[SourceCode:2701: /*
2702: * Walk the list of dirty pages of the given address space and write all of them.
2703: *
2704: * @mapping: address space structure to write
2705: * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2706: * @bio_ctrl: holds context for the write, namely the bio
2707: *
2708: * If a page is already under I/O, write_cache_pages() skips it, even
2709: * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2710: * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2711: * and msync() need to guarantee that all the data which was dirty at the time
2712: * the call was made get new I/O started against them. If wbc->sync_mode is
2713: * WB_SYNC_ALL then we were called for data integrity and we must wait for
2714: * existing IO to complete.
2715: */
2716: static int extent_write_cache_pages(struct address_space *mapping,
2717: struct btrfs_bio_ctrl *bio_ctrl)
2718: {
2719: struct writeback_control *wbc = bio_ctrl->wbc;
2720: struct inode *inode = mapping->host;
2721: int ret = 0;
2722: bool done = false;
2723: int nr_to_write_done = 0;
2724: struct folio_batch fbatch;
2725: unsigned int nr_folios;
2726: pgoff_t index;
2727: pgoff_t end; /* Inclusive */
2728: pgoff_t done_index;
2729: bool range_whole = false;
2730: bool scanned = false;
2731: xa_mark_t tag;
2732:
2733: /*
2734: * We have to hold onto the inode so that ordered extents can do their
2735: * work when the IO finishes. The alternative to this is failing to add
2736: * an ordered extent if the igrab() fails there and that is a huge pain
2737: * to deal with, so instead just hold onto the inode throughout the
2738: * writepages operation. If it fails here we are freeing up the inode
2739: * anyway and we'd rather not waste our time writing out stuff that is
2740: * going to be truncated anyway.
2741: */
2742: if (!igrab(inode))
2743: return 0;
2744:
2745: folio_batch_init(&fbatch);
2746: if (wbc->range_cyclic) {
2747: index = mapping->writeback_index; /* Start from prev offset */
2748: end = -1;
2749: /*
2750: * Start from the beginning does not need to cycle over the
2751: * range, mark it as scanned.
2752: */
2753: scanned = (index == 0);
2754: } else {
2755: index = wbc->range_start >> PAGE_SHIFT;
2756: end = wbc->range_end >> PAGE_SHIFT;
2757: if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2758: range_whole = true;
2759: scanned = true;
2760: }
2761:
2762: /*
2763: * We do the tagged writepage as long as the snapshot flush bit is set
2764: * and we are the first one who do the filemap_flush() on this inode.
2765: *
2766: * The nr_to_write == LONG_MAX is needed to make sure other flushers do
2767: * not race in and drop the bit.
2768: */
2769: if (range_whole && wbc->nr_to_write == LONG_MAX &&
2770: test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
2771: &BTRFS_I(inode)->runtime_flags))
2772: wbc->tagged_writepages = 1;
2773:
2774: tag = wbc_to_tag(wbc);
2775: retry:
2776: if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2777: tag_pages_for_writeback(mapping, index, end);
2778: done_index = index;
2779: while (!done && !nr_to_write_done && (index <= end) &&
2780: (nr_folios = filemap_get_folios_tag(mapping, &index,
2781: end, tag, &fbatch))) {
2782: unsigned i;
2783:
2784: for (i = 0; i < nr_folios; i++) {
2785: struct folio *folio = fbatch.folios[i];
2786:
2787: done_index = folio_next_index(folio);
2788: /*
2789: * At this point we hold neither the i_pages lock nor
2790: * the folio lock: the folio may be truncated or
2791: * invalidated (changing folio->mapping to NULL).
2792: */
2793: if (!folio_trylock(folio)) {
2794: submit_write_bio(bio_ctrl, 0);
2795: folio_lock(folio);
2796: }
2797:
2798: if (unlikely(folio->mapping != mapping)) {
2799: folio_unlock(folio);
2800: continue;
2801: }
2802:
2803: if (!folio_test_dirty(folio)) {
2804: /* Someone wrote it for us. */
2805: folio_unlock(folio);
2806: continue;
2807: }
2808:
2809: /*
2810: * For subpage case, compression can lead to mixed
2811: * writeback and dirty flags, e.g:
2812: * 0 32K 64K 96K 128K
2813: * | |//////||/////| |//|
2814: *
2815: * In above case, [32K, 96K) is asynchronously submitted
2816: * for compression, and [124K, 128K) needs to be written back.
2817: *
2818: * If we didn't wait writeback for page 64K, [128K, 128K)
2819: * won't be submitted as the page still has writeback flag
2820: * and will be skipped in the next check.
2821: *
2822: * This mixed writeback and dirty case is only possible for
2823: * subpage case.
2824: *
2825: * TODO: Remove this check after migrating compression to
2826: * regular submission.
2827: */
2828: if (wbc->sync_mode != WB_SYNC_NONE ||
2829: btrfs_is_subpage(inode_to_fs_info(inode), folio)) {
2830: if (folio_test_writeback(folio))
2831: submit_write_bio(bio_ctrl, 0);
2832: folio_wait_writeback(folio);
2833: }
2834:
2835: if (folio_test_writeback(folio) ||
2836: !folio_test_dirty(folio)) {
2837: folio_unlock(folio);
2838: continue;
2839: }
2840:
2841: /* Locked for writeback; revoke writable mmap PTEs before using the data. */
2842: folio_mkclean(folio);
2843: ret = extent_writepage(folio, bio_ctrl);
2844: if (ret < 0) {
2845: done = true;
2846: break;
2847: }
2848:
2849: /*
2850: * The filesystem may choose to bump up nr_to_write.
2851: * We have to make sure to honor the new nr_to_write
2852: * at any time.
2853: */
2854: nr_to_write_done = (wbc->sync_mode == WB_SYNC_NONE &&
2855: wbc->nr_to_write <= 0);
2856: }
2857: folio_batch_release(&fbatch);
2858: cond_resched();
2859: }
2860: if (!scanned && !done) {
2861: /*
2862: * We hit the last page and there is more work to be done: wrap
2863: * back to the start of the file
2864: */
2865: scanned = true;
2866: index = 0;
2867:
2868: /*
2869: * If we're looping we could run into a page that is locked by a
2870: * writer and that writer could be waiting on writeback for a
2871: * page in our current bio, and thus deadlock, so flush the
2872: * write bio here.
2873: */
2874: submit_write_bio(bio_ctrl, 0);
2875: goto retry;
2876: }
2877:
2878: if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
2879: mapping->writeback_index = done_index;
2880:
2881: btrfs_add_delayed_iput(BTRFS_I(inode));
2882: return ret;
2883: }
SourceFile:fs/btrfs/extent_io.c]
|
| 187/2 |
2026/09/20 01:07 |
llm |
expert |
0m
Model:
Tokens:
input: 68119
output: 66
thoughts: 0
|
| 188/2 |
2026/09/20 01:07 |
tool |
set-results |
0m
Args: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:false VMGuestTrigger:false VMHostTrigger:false]
Results: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:false VMGuestTrigger:false VMHostTrigger:false]
|
| 189/2 |
2026/09/20 01:07 |
llm |
expert |
0m
Model:
Tokens:
input: 68249
output: 895
thoughts: 0
|
| 190/1 |
2026/09/20 01:07 |
action |
format-explanation |
0m
Results: map[Explanation:I have analyzed the bug report and the relevant source code.
### Bug Mechanism
The crash is an assertion failure in `wait_subpage_spinlock()`:
`ASSERT(folio_test_private(folio) && folio_get_private(folio));`
This is triggered during `btrfs_invalidate_folio()`, which is called by
`folio_invalidate()` from `extent_writepage()` when a dirty folio is being
written back but its index is beyond the end of the file (`i_size`).
The sequence of events is:
1. A folio is dirtied via a memory mapping or direct I/O (GUP), e.g., using
`aio_read` into a memory-mapped region of a Btrfs file.
2. The file is truncated or the folio is otherwise beyond `i_size`.
3. Writeback is triggered for the folio (e.g., via
`btrfs_start_ordered_extent_nowriteback` calling `filemap_fdatawrite_range`).
4. `extent_writepage()` checks if the folio is beyond `i_size`. If so, it calls
`folio_invalidate(folio, 0, folio_size(folio))`.
5. `folio_invalidate()` calls the filesystem's `invalidate_folio` callback,
which is `btrfs_invalidate_folio()`.
6. `btrfs_invalidate_folio()` immediately calls `wait_subpage_spinlock(folio)`.
7. `wait_subpage_spinlock()` checks if the folio is a subpage folio
(`btrfs_is_subpage()`). If the system is using large folios (where
`folio_size(folio) > fs_info->sectorsize`), this returns true.
8. It then asserts that the folio has private data attached
(`folio_test_private(folio)` and `folio_get_private(folio)`).
9. However, if the folio was dirtied via GUP or memory mapping without going
through the normal Btrfs write paths that attach the private state (or if the
private state was already detached), `folio_test_private(folio)` is false.
10. The assertion fails, causing a `BUG()` and a kernel panic.
### Production Impact (Without ASSERT)
In a production kernel where `CONFIG_BTRFS_ASSERT` is not enabled, the
`ASSERT()` macro compiles to nothing.
The code in `wait_subpage_spinlock()` will then execute:
```c
bfs = folio_get_private(folio);
spin_lock_irq(&bfs->lock);
spin_unlock_irq(&bfs->lock);
```
Since `folio_get_private(folio)` returns `NULL`, `bfs` will be `NULL`.
The `lock` field is at offset 0 in `struct btrfs_folio_state`.
Therefore, `spin_lock_irq(&bfs->lock)` will attempt to dereference a NULL
pointer (address `0x0`), resulting in a kernel NULL pointer dereference panic.
This means the bug leads to a Denial of Service (system crash) even in
production builds. It does not lead to memory corruption, use-after-free, or
information leaks, so it is not exploitable for privilege escalation.
### Reachability
The bug can be triggered by standard unprivileged user operations. The syzkaller
reproducer uses `aio_read` with Direct I/O and memory-mapped files, which are
available to any unprivileged user. It does not require any special
capabilities, user namespaces, or mounting malicious filesystems. It just
requires access to a Btrfs filesystem (which can be a standard mount point
available to users).
### Conclusion
- **Exploitable:** False. It's a NULL pointer dereference in production builds,
leading to a crash, not memory corruption.
- **Denial of Service:** True. It causes a kernel panic.
- **Accessible From Unprivileged Processes:** True. Triggerable via standard I/O
and memory mapping syscalls.
- **Accessible From User Namespaces:** False (not required).
- **Other Triggers:** False.]
|