The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [syzbot] [btrfs?] kernel BUG in wait_subpage_spinlock
@ 2026-09-22 19:05 syzbot
  2026-11-30 18:26 ` [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage() Brahmajit Das
  2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
  0 siblings, 2 replies; 17+ messages in thread
From: syzbot @ 2026-09-22 19:05 UTC (permalink / raw)
  To: clm, dsterba, linux-btrfs, linux-kernel, mason, syzkaller-bugs

Hello,

syzbot found the following issue on:

HEAD commit:    5dd1818b15d9 Merge tag 'for-next-keys-v7.3-rc4' of git://g..
git tree:       upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=1694d7f9580000
kernel config:  https://syzkaller.appspot.com/x/.config?x=c3bddd52b22c2498
dashboard link: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
compiler:       Debian clang version 22.1.8 (++20260613092233+e80beda6e255-1~exp1~20260613092250.77), Debian LLD 22.1.8

Unfortunately, I don't have any reproducer for this issue yet.

Downloadable assets:
disk image (non-bootable): https://storage.googleapis.com/syzbot-assets/d900f083ada3/non_bootable_disk-5dd1818b.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/fa126b13cb66/vmlinux-5dd1818b.xz
kernel image: https://storage.googleapis.com/syzbot-assets/e10c187acaf6/bzImage-5dd1818b.xz

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com

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


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title

If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)

If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report

If you want to undo deduplication, reply with:
#syz undup

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
  2026-09-22 19:05 [syzbot] [btrfs?] kernel BUG in wait_subpage_spinlock syzbot
@ 2026-11-30 18:26 ` Brahmajit Das
  2026-11-30 20:47   ` Qu Wenruo
  2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
  1 sibling, 1 reply; 17+ messages in thread
From: Brahmajit Das @ 2026-11-30 18:26 UTC (permalink / raw)
  To: dsterba, linux-btrfs
  Cc: clm, mason, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

syzbot reported an assertion failure in wait_subpage_spinlock():

  assertion failed: folio_test_private(folio) && folio_get_private(folio),
  in fs/btrfs/inode.c:7616
  kernel BUG at fs/btrfs/inode.c:7616!
  Call Trace:
   btrfs_invalidate_folio+0x26e/0xc50
   extent_writepage fs/btrfs/extent_io.c:2058 [inline]
   extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
   btrfs_writepages+0x129f/0x28c0
   do_writepages+0x338/0x560
   filemap_fdatawrite_range+0x1ef/0x2f0
   btrfs_start_ordered_extent_nowriteback+0x305/0x6e0
   btrfs_page_mkwrite+0x856/0x1a40
   ...
   __gup_longterm_locked+0x1032/0x15c0
   ...
   blkdev_direct_IO+0x121d/0x17b0
   aio_read+0x33a/0x4d0

The program does an O_DIRECT read into a MAP_SHARED mapping of a btrfs
file, while the same file is being truncated (creat() with O_TRUNC).

extent_writepage() unconditionally calls folio_invalidate() on a dirty
folio that is fully beyond EOF.  However such a folio can be dirty
without folio::private: a pinned folio can lose its private once it is
clean, and then be re-dirtied through btrfs_data_dirty_folio(), which
intentionally does not touch any btrfs state for blocks beyond EOF.

For the bs < folio size case (which includes large folios),
btrfs_invalidate_folio() requires folio::private, both in
wait_subpage_spinlock() and in the subpage bitmap helpers, so we hit the
above ASSERT() (or a NULL pointer dereference without
CONFIG_BTRFS_ASSERT).

The MM layer never calls ->invalidate_folio() for such folios, as
truncate_cleanup_folio() only does so if folio_needs_release().  A folio
without private has no btrfs per-folio state to tear down, and since
try_release_extent_mapping() must have succeeded to detach it, there is
no delalloc range for it either.

So follow the same rule in extent_writepage(): only invalidate the
beyond-EOF folio if it has private, otherwise just clear its dirty flag
and dirty tag.

Note: syzbot has no reproducer for this issue.  The fix was only build
tested, the race has not been reproduced locally.

This patch was written with the help of an AI coding assistant (Claude),
from the syzbot report and console log.

Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
Fixes: cc38d178ff33 ("btrfs: enable large data folio support under CONFIG_BTRFS_EXPERIMENTAL")
Signed-off-by: Brahmajit Das <bdas@suse.de>
---
 fs/btrfs/extent_io.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..166034ec0c3b 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2046,7 +2046,24 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
 	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));
+		/*
+		 * A folio beyond EOF can be dirtied without folio::private,
+		 * e.g. a pinned folio that lost its private and was then
+		 * re-dirtied by btrfs_data_dirty_folio(), which leaves blocks
+		 * beyond EOF alone.
+		 *
+		 * Such a folio has no btrfs state to tear down, and
+		 * btrfs_invalidate_folio() requires folio::private for the
+		 * bs < folio size case.  So just like truncate_cleanup_folio()
+		 * only invalidate the folio if it has private, otherwise just
+		 * clear the dirty flag and tag.
+		 */
+		if (folio_test_private(folio)) {
+			folio_invalidate(folio, 0, folio_size(folio));
+		} else {
+			folio_clear_dirty_for_io(folio);
+			btrfs_clear_folio_dirty_tag(folio);
+		}
 		folio_unlock(folio);
 		return 0;
 	}
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
  2026-11-30 18:26 ` [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage() Brahmajit Das
@ 2026-11-30 20:47   ` Qu Wenruo
  2026-12-01 13:14     ` listout
  0 siblings, 1 reply; 17+ messages in thread
From: Qu Wenruo @ 2026-11-30 20:47 UTC (permalink / raw)
  To: Brahmajit Das, dsterba, linux-btrfs
  Cc: clm, mason, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das



在 2026/12/1 04:56, Brahmajit Das 写道:
> syzbot reported an assertion failure in wait_subpage_spinlock():
> 
>    assertion failed: folio_test_private(folio) && folio_get_private(folio),
>    in fs/btrfs/inode.c:7616
>    kernel BUG at fs/btrfs/inode.c:7616!
>    Call Trace:
>     btrfs_invalidate_folio+0x26e/0xc50
>     extent_writepage fs/btrfs/extent_io.c:2058 [inline]
>     extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
>     btrfs_writepages+0x129f/0x28c0
>     do_writepages+0x338/0x560
>     filemap_fdatawrite_range+0x1ef/0x2f0
>     btrfs_start_ordered_extent_nowriteback+0x305/0x6e0
>     btrfs_page_mkwrite+0x856/0x1a40
>     ...
>     __gup_longterm_locked+0x1032/0x15c0
>     ...
>     blkdev_direct_IO+0x121d/0x17b0
>     aio_read+0x33a/0x4d0
> 
> The program does an O_DIRECT read into a MAP_SHARED mapping of a btrfs
> file, while the same file is being truncated (creat() with O_TRUNC).
> 
> extent_writepage() unconditionally calls folio_invalidate() on a dirty
> folio that is fully beyond EOF.  However such a folio can be dirty
> without folio::private: a pinned folio can lose its private once it is
> clean,

Mind to explain this a little more? On exact which path the folio's 
private is cleared?

Better with a calltrace showing the situation.

> and then be re-dirtied through btrfs_data_dirty_folio(), which
> intentionally does not touch any btrfs state for blocks beyond EOF.

I'm not a huge fan of breaking the assumption that all btrfs filemap 
folios should have its private properly set.

Can we fix the bug by ensuring even filemap folios beyond EOF will have 
proper private set?

> 
> For the bs < folio size case (which includes large folios),
> btrfs_invalidate_folio() requires folio::private, both in
> wait_subpage_spinlock() and in the subpage bitmap helpers, so we hit the
> above ASSERT() (or a NULL pointer dereference without
> CONFIG_BTRFS_ASSERT).
> 
> The MM layer never calls ->invalidate_folio() for such folios, as
> truncate_cleanup_folio() only does so if folio_needs_release().  A folio
> without private has no btrfs per-folio state to tear down, and since
> try_release_extent_mapping() must have succeeded to detach it, there is
> no delalloc range for it either.
> 
> So follow the same rule in extent_writepage(): only invalidate the
> beyond-EOF folio if it has private, otherwise just clear its dirty flag
> and dirty tag.
> 
> Note: syzbot has no reproducer for this issue.  The fix was only build
> tested, the race has not been reproduced locally.
> 
> This patch was written with the help of an AI coding assistant (Claude),
> from the syzbot report and console log.
> 
> Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
> Fixes: cc38d178ff33 ("btrfs: enable large data folio support under CONFIG_BTRFS_EXPERIMENTAL")
> Signed-off-by: Brahmajit Das <bdas@suse.de>
> ---
>   fs/btrfs/extent_io.c | 19 ++++++++++++++++++-
>   1 file changed, 18 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index b2739838c646..166034ec0c3b 100644
> --- a/fs/btrfs/extent_io.c
> +++ b/fs/btrfs/extent_io.c
> @@ -2046,7 +2046,24 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
>   	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));
> +		/*
> +		 * A folio beyond EOF can be dirtied without folio::private,
> +		 * e.g. a pinned folio that lost its private and was then
> +		 * re-dirtied by btrfs_data_dirty_folio(), which leaves blocks
> +		 * beyond EOF alone.
> +		 *
> +		 * Such a folio has no btrfs state to tear down, and
> +		 * btrfs_invalidate_folio() requires folio::private for the
> +		 * bs < folio size case.  So just like truncate_cleanup_folio()
> +		 * only invalidate the folio if it has private, otherwise just
> +		 * clear the dirty flag and tag.
> +		 */
> +		if (folio_test_private(folio)) {
> +			folio_invalidate(folio, 0, folio_size(folio));
> +		} else {
> +			folio_clear_dirty_for_io(folio);
> +			btrfs_clear_folio_dirty_tag(folio);
> +		}
>   		folio_unlock(folio);
>   		return 0;
>   	}


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
  2026-11-30 20:47   ` Qu Wenruo
@ 2026-12-01 13:14     ` listout
  2026-12-01 20:27       ` Qu Wenruo
  0 siblings, 1 reply; 17+ messages in thread
From: listout @ 2026-12-01 13:14 UTC (permalink / raw)
  To: quwenruo.btrfs
  Cc: dsterba, linux-btrfs, clm, mason, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff

On 2026/12/1 Qu Wenruo wrote:
> Mind to explain this a little more? On exact which path the folio's
> private is cleared?
>
> Better with a calltrace showing the situation.

Unfortunately the console log doesn't show where private was dropped, and
syzbot has no reproducer, so I can't give you a trace from the actual
crash. From code reading, a pinned folio can lose its private but stay in
the page cache in two ways:

1) Partial invalidation in btrfs itself. For a range smaller than the
   whole folio, btrfs_invalidate_folio() calls btrfs_release_folio(),
   which only checks dirty/writeback:

   truncate_pagecache()
     truncate_inode_pages_range()
       truncate_inode_partial_folio()
         folio_invalidate(folio, offset, length)
           btrfs_invalidate_folio()
             btrfs_release_folio()
               __btrfs_release_folio()
                 clear_folio_extent_mapped()
         folio_split_or_unmap()
           folio_split()   <- fails, folio pinned by an in-flight O_DIRECT read

2) Any failed large folio split. __folio_freeze_split_file() calls
   filemap_release_folio() before folio_ref_freeze(), with no pin check,
   so a split failing on a pinned folio leaves it without private
   (reachable from truncate and from migration).

When the O_DIRECT read completes, bio_release_pages() -> folio_mark_dirty()
-> btrfs_data_dirty_folio() re-dirties the folio. If it's beyond EOF by
then, nothing is attached and we get the reported crash. If it straddles
EOF, btrfs_subpage_set_fixup_dirty() would dereference the NULL private
instead.

> Can we fix the bug by ensuring even filemap folios beyond EOF will have
> proper private set?

Agreed, that's better. For v2 I'm thinking of making btrfs_release_folio()
refuse to release while folio_maybe_dma_pinned(), so any folio that can
still be re-dirtied without btrfs knowing keeps its private. That covers
both paths above.

The remaining case is the full invalidation extent_writepage() does on
beyond-EOF folios, which clears private unconditionally while the folio
may stay in the page cache until truncate removes it. Would you prefer
to skip that invalidation for pinned folios, or to re-attach private
there before invalidating?

Thanks,
Brahmajit

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
  2026-12-01 13:14     ` listout
@ 2026-12-01 20:27       ` Qu Wenruo
  2026-12-02  3:34         ` Brahmajit Das
  0 siblings, 1 reply; 17+ messages in thread
From: Qu Wenruo @ 2026-12-01 20:27 UTC (permalink / raw)
  To: listout
  Cc: dsterba, linux-btrfs, clm, mason, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff



在 2026/12/1 23:44, listout@listout.xyz 写道:
> On 2026/12/1 Qu Wenruo wrote:
>> Mind to explain this a little more? On exact which path the folio's
>> private is cleared?
>>
>> Better with a calltrace showing the situation.
> 
> Unfortunately the console log doesn't show where private was dropped, and
> syzbot has no reproducer, so I can't give you a trace from the actual
> crash. From code reading, a pinned folio can lose its private but stay in
> the page cache in two ways:
> 
> 1) Partial invalidation in btrfs itself. For a range smaller than the
>     whole folio, btrfs_invalidate_folio() calls btrfs_release_folio(),
>     which only checks dirty/writeback:
> 
>     truncate_pagecache()
>       truncate_inode_pages_range()
>         truncate_inode_partial_folio()
>           folio_invalidate(folio, offset, length)
>             btrfs_invalidate_folio()
>               btrfs_release_folio()
>                 __btrfs_release_folio()
>                   clear_folio_extent_mapped()
>           folio_split_or_unmap()
>             folio_split()   <- fails, folio pinned by an in-flight O_DIRECT read
> 
> 2) Any failed large folio split. __folio_freeze_split_file() calls
>     filemap_release_folio() before folio_ref_freeze(), with no pin check,
>     so a split failing on a pinned folio leaves it without private
>     (reachable from truncate and from migration).
> 
> When the O_DIRECT read completes, bio_release_pages() -> folio_mark_dirty()
> -> btrfs_data_dirty_folio() re-dirties the folio. If it's beyond EOF by
> then, nothing is attached and we get the reported crash. If it straddles
> EOF, btrfs_subpage_set_fixup_dirty() would dereference the NULL private
> instead.
> 
>> Can we fix the bug by ensuring even filemap folios beyond EOF will have
>> proper private set?
> 
> Agreed, that's better. For v2 I'm thinking of making btrfs_release_folio()
> refuse to release while folio_maybe_dma_pinned(),

Would that race? E.g. at the time of btrfs_release_folio(), the folio is 
not yet pinned, but after the check, GUP pinned the folio, resulting the 
same folio private cleared problem.

I'm wondering if we can re-attach the private structure during 
btrfs_data_dirty_folio() to close the loop?

Thanks,
Qu

> so any folio that can
> still be re-dirtied without btrfs knowing keeps its private. That covers
> both paths above.
> 
> The remaining case is the full invalidation extent_writepage() does on
> beyond-EOF folios, which clears private unconditionally while the folio
> may stay in the page cache until truncate removes it. Would you prefer
> to skip that invalidation for pinned folios, or to re-attach private
> there before invalidating?
> 
> Thanks,
> Brahmajit


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
  2026-12-01 20:27       ` Qu Wenruo
@ 2026-12-02  3:34         ` Brahmajit Das
  2026-12-02  3:55           ` Qu Wenruo
  0 siblings, 1 reply; 17+ messages in thread
From: Brahmajit Das @ 2026-12-02  3:34 UTC (permalink / raw)
  To: quwenruo.btrfs
  Cc: dsterba, linux-btrfs, clm, mason, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff

On 2026/12/2 Qu Wenruo wrote:
>> Agreed, that's better. For v2 I'm thinking of making btrfs_release_folio()
>> refuse to release while folio_maybe_dma_pinned(),
>
> Would that race? E.g. at the time of btrfs_release_folio(), the folio is
> not yet pinned, but after the check, GUP pinned the folio, resulting the
> same folio private cleared problem.

I don't think a new pin can cause it. btrfs_release_folio() only
releases a locked, clean folio. A clean folio of a shared mapping has
all its PTEs write-protected (folio_mkclean() at writeback), so a new
FOLL_WRITE pin, the only kind that re-dirties the folio when released,
has to take the write fault -> btrfs_page_mkwrite(), which locks the
folio and does set_folio_extent_mapped() before the pin completes.
GUP-fast doesn't bypass this either, since it requires
pte_access_permitted(pte, FOLL_WRITE).

The real gap is that folio_maybe_dma_pinned() only catches FOLL_PIN.
Legacy get_user_pages() + set_page_dirty_lock() users would still
re-dirty a folio after its private was released. So I agree the check
isn't enough on its own.

> I'm wondering if we can re-attach the private structure during
> btrfs_data_dirty_folio() to close the loop?

That looks more complete to me: it covers FOLL_GET users and the
beyond-EOF folio invalidated by extent_writepage(). Some constraints
I see:

- ->dirty_folio() can't fail and may be called in atomic context, so
  the allocation has to be GFP_NOWAIT. (For the PTE lock caller the
  folio is already dirty, so private should already be there.)

- btrfs_alloc_folio_state() returns zeroed bitmaps, so for an uptodate
  folio we'd need to fill the uptodate bitmap before marking blocks
  dirty/fixup.

- If the allocation fails we still need a fallback. Writeback can
  sleep, so I'd make extent_writepage() attach private with GFP_NOFS
  before the beyond-EOF check, so btrfs_invalidate_folio() always sees
  it there.

So for v2 I plan to:
1) attach private in btrfs_data_dirty_folio() if missing, including
   for folios beyond EOF;
2) attach private in extent_writepage() before the EOF check as the
   fallback, instead of skipping folio_invalidate() as v1 did.

Does that sound reasonable? For the in-EOF case with a failed
allocation, I'm not sure how best to record the fixup without the
bitmap. Treating the whole folio as needing fixup would be simplest,
if you're fine with that.

Thanks,
Brahmajit

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
  2026-12-02  3:34         ` Brahmajit Das
@ 2026-12-02  3:55           ` Qu Wenruo
  0 siblings, 0 replies; 17+ messages in thread
From: Qu Wenruo @ 2026-12-02  3:55 UTC (permalink / raw)
  To: Brahmajit Das
  Cc: dsterba, linux-btrfs, clm, mason, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff



在 2026/12/2 14:04, Brahmajit Das 写道:
> On 2026/12/2 Qu Wenruo wrote:
>>> Agreed, that's better. For v2 I'm thinking of making btrfs_release_folio()
>>> refuse to release while folio_maybe_dma_pinned(),
>>
>> Would that race? E.g. at the time of btrfs_release_folio(), the folio is
>> not yet pinned, but after the check, GUP pinned the folio, resulting the
>> same folio private cleared problem.
> 
> I don't think a new pin can cause it. btrfs_release_folio() only
> releases a locked, clean folio. A clean folio of a shared mapping has
> all its PTEs write-protected (folio_mkclean() at writeback), so a new
> FOLL_WRITE pin, the only kind that re-dirties the folio when released,
> has to take the write fault -> btrfs_page_mkwrite(), which locks the
> folio and does set_folio_extent_mapped() before the pin completes.
> GUP-fast doesn't bypass this either, since it requires
> pte_access_permitted(pte, FOLL_WRITE).
> 
> The real gap is that folio_maybe_dma_pinned() only catches FOLL_PIN.
> Legacy get_user_pages() + set_page_dirty_lock() users would still
> re-dirty a folio after its private was released. So I agree the check
> isn't enough on its own.
> 
>> I'm wondering if we can re-attach the private structure during
>> btrfs_data_dirty_folio() to close the loop?
> 
> That looks more complete to me: it covers FOLL_GET users and the
> beyond-EOF folio invalidated by extent_writepage(). Some constraints
> I see:
> 
> - ->dirty_folio() can't fail and may be called in atomic context, so
>    the allocation has to be GFP_NOWAIT. (For the PTE lock caller the
>    folio is already dirty, so private should already be there.)
> 
> - btrfs_alloc_folio_state() returns zeroed bitmaps, so for an uptodate
>    folio we'd need to fill the uptodate bitmap before marking blocks
>    dirty/fixup.

If the folio has its private removed, can it still be uptodate?
That folio should be invalidated, thus I think it should not be uptodate 
either.

> 
> - If the allocation fails we still need a fallback. Writeback can
>    sleep, so I'd make extent_writepage() attach private with GFP_NOFS
>    before the beyond-EOF check, so btrfs_invalidate_folio() always sees
>    it there.
> 
> So for v2 I plan to:
> 1) attach private in btrfs_data_dirty_folio() if missing, including
>     for folios beyond EOF;
> 2) attach private in extent_writepage() before the EOF check as the
>     fallback, instead of skipping folio_invalidate() as v1 did.

If that only affects folio_invalidate() call, I think you can add do the 
attach inside that beyond EOF branch, so it won't affect the existing 
unexpected dirty folio check.

Sorry I'm not an expert in the GUP part, although it sounds fine to me, 
some review from others would definitely help.

> 
> Does that sound reasonable? For the in-EOF case with a failed
> allocation, I'm not sure how best to record the fixup without the
> bitmap. Treating the whole folio as needing fixup would be simplest,
> if you're fine with that.
> 
> Thanks,
> Brahmajit


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private
  2026-09-22 19:05 [syzbot] [btrfs?] kernel BUG in wait_subpage_spinlock syzbot
  2026-11-30 18:26 ` [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage() Brahmajit Das
@ 2026-12-03 13:30 ` Brahmajit Das
  2026-12-03 13:30   ` [PATCH v3 1/3] btrfs: don't dereference a missing folio private on writeback error Brahmajit Das
                     ` (3 more replies)
  1 sibling, 4 replies; 17+ messages in thread
From: Brahmajit Das @ 2026-12-03 13:30 UTC (permalink / raw)
  To: David Sterba, linux-btrfs
  Cc: Qu Wenruo, Chris Mason, David Hildenbrand, Jason Gunthorpe,
	John Hubbard, Peter Xu, linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

From: Brahmajit Das <bdas@suse.de>

syzbot hit an ASSERT() in wait_subpage_spinlock() when writeback
invalidates a beyond-EOF folio that has no folio::private.  The folio
lost its private while it stayed in the page cache, pinned by an
O_DIRECT read, from btrfs_release_folio() during a partial invalidation
or a failed large folio split.  When the read completed, the folio was
dirtied again through btrfs_data_dirty_folio(), still without private.

Patch 3 is the fix: it re-attaches the private in
btrfs_data_dirty_folio() with GFP_NOWAIT, and as a fallback in the
beyond-EOF branch of extent_writepage().

Patches 1 and 2 fix problems that already exist and that patch 3 relies
on.  Both were found by Sashiko AI review of v2:

- Patch 1: the -EUCLEAN path of extent_writepage() for a dirty folio
  without private dereferences the NULL private for the bs < folio
  size case.  After patch 3, this path is reached if the GFP_NOWAIT
  allocation fails for a folio inside EOF.

- Patch 2: attaching a new folio state to a released folio that is still
  uptodate leaves its uptodate bitmap empty.  This is now synced in
  btrfs_attach_folio_state() for all callers instead of only in
  btrfs_data_dirty_folio().

Open question: if the GFP_NOWAIT allocation fails for a folio inside
EOF, and some other path such as btrfs_page_mkwrite() attaches the
private before writeback, the folio passes the -EUCLEAN check but only
the caller's range is marked dirty.  The rest of the data written by
the O_DIRECT read is then silently not written back.  This was already
the case before this series for any folio dirtied without private.  I
can mark the in-EOF range for fixup in btrfs_attach_folio_state() if
the folio is already dirty, if that is wanted.

The series is build tested only.  syzbot has no reproducer.

It was written with the help of an AI coding assistant (Claude).

v3:
- Split into a series, adding patches 1 and 2 (Sashiko AI review)
- Drop the explicit uptodate bitmap sync from btrfs_data_dirty_folio(),
  now done by patch 2

v2: https://lore.kernel.org/linux-btrfs/20261202041853.4184717-1-listout@listout.xyz/
v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/

Brahmajit Das (3):
  btrfs: don't dereference a missing folio private on writeback error
  btrfs: sync the uptodate bitmap when attaching a data folio state
  btrfs: re-attach folio private when a released folio gets dirtied

 fs/btrfs/extent_io.c | 35 ++++++++++++++++++++++++++++++-----
 fs/btrfs/extent_io.h |  1 +
 fs/btrfs/inode.c     | 16 +++++++++++++++-
 fs/btrfs/subpage.c   | 18 ++++++++++++++++--
 fs/btrfs/subpage.h   |  3 ++-
 5 files changed, 64 insertions(+), 9 deletions(-)


base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106
-- 
2.56.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 1/3] btrfs: don't dereference a missing folio private on writeback error
  2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
@ 2026-12-03 13:30   ` Brahmajit Das
  2026-12-03 13:30   ` [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state Brahmajit Das
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 17+ messages in thread
From: Brahmajit Das @ 2026-12-03 13:30 UTC (permalink / raw)
  To: David Sterba, linux-btrfs
  Cc: Qu Wenruo, Chris Mason, David Hildenbrand, Jason Gunthorpe,
	John Hubbard, Peter Xu, linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

From: Brahmajit Das <bdas@suse.de>

extent_writepage() rejects a dirty folio without folio::private with
-EUCLEAN, then jumps to the common exit path, which calls
btrfs_folio_end_lock_bitmap().  For the bs < folio size case, that
function dereferences the folio private:

  btrfs_folio_end_lock_bitmap()
    bfs = folio_get_private(folio);	/* NULL */
    ...
    if (atomic_read(&bfs->nr_locked) == 0)	/* NULL dereference */

so the error path meant to catch such a folio crashes instead.

No subpage lock can be held on a folio without private, so just set the
mapping error and unlock the folio directly.

Note: this was only build tested.

This patch was written with the help of an AI coding assistant (Claude).

Fixes: 7ca3e84980ef ("btrfs: reject out-of-band dirty folios during writeback")
Signed-off-by: Brahmajit Das <bdas@suse.de>
---
 fs/btrfs/extent_io.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..9dded5c670de 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2070,8 +2070,15 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
 	"root %lld ino %llu folio %llu is marked dirty without notifying the fs",
 			     btrfs_root_id(inode->root),
 			     btrfs_ino(inode), folio_pos(folio));
-		ret = -EUCLEAN;
-		goto done;
+		/*
+		 * Don't go through btrfs_folio_end_lock_bitmap(), which
+		 * dereferences the missing folio private for the
+		 * bs < folio size case.  No subpage lock can be held on a
+		 * folio without private, so a plain unlock is enough.
+		 */
+		mapping_set_error(folio->mapping, -EUCLEAN);
+		folio_unlock(folio);
+		return -EUCLEAN;
 	}
 
 	ret = set_folio_extent_mapped(folio);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state
  2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
  2026-12-03 13:30   ` [PATCH v3 1/3] btrfs: don't dereference a missing folio private on writeback error Brahmajit Das
@ 2026-12-03 13:30   ` Brahmajit Das
  2026-12-03 20:36     ` Qu Wenruo
  2026-12-03 13:30   ` [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
  2026-12-04  6:32   ` [PATCH v4 0/2] btrfs: fix dirtying of folios that lost their private listout
  3 siblings, 1 reply; 17+ messages in thread
From: Brahmajit Das @ 2026-12-03 13:30 UTC (permalink / raw)
  To: David Sterba, linux-btrfs
  Cc: Qu Wenruo, Chris Mason, David Hildenbrand, Jason Gunthorpe,
	John Hubbard, Peter Xu, linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

From: Brahmajit Das <bdas@suse.de>

A data folio can lose its private while it stays in the page cache, e.g.
btrfs_release_folio() from a partial invalidation, or from a large folio
split that then fails.  Such a folio was only released, not invalidated,
so it can still be uptodate.

When something calls set_folio_extent_mapped() on it again, such as
prepare_one_folio(), btrfs_page_mkwrite() or btrfs_truncate_block(), a
new btrfs_folio_state is attached with empty bitmaps, while PG_uptodate
is still set on the folio.  Those callers see an uptodate folio, skip
reading it, and only update the uptodate bits of their own range.  The
other blocks are left marked not uptodate in the subpage bitmap.

That can trip the ASSERT() in can_skip_one_ordered_range(), which
expects the whole range up to the ordered extent end or folio end to be
uptodate if its first block is.

Fix it by setting the uptodate bitmap in btrfs_attach_folio_state() if
the data folio is uptodate.  On the read path, private is attached
before the folio is uptodate, so nothing changes there.

Note: this was only build tested.

This patch was written with the help of an AI coding assistant (Claude),
following a review comment from Sashiko AI review.

Signed-off-by: Brahmajit Das <bdas@suse.de>
---
 fs/btrfs/subpage.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index ebf18efe1ea3..97d4824ae623 100644
--- a/fs/btrfs/subpage.c
+++ b/fs/btrfs/subpage.c
@@ -63,6 +63,19 @@ int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
 	if (IS_ERR(bfs))
 		return PTR_ERR(bfs);
 
+	/*
+	 * A data folio can lose its private while staying uptodate in the
+	 * page cache, e.g. btrfs_release_folio() on a folio that then fails
+	 * to be removed or split.  The new state starts with empty bitmaps,
+	 * so sync the uptodate bitmap with the folio flag, or the blocks not
+	 * touched by the caller would be considered not uptodate.
+	 */
+	if (type == BTRFS_SUBPAGE_DATA && folio_test_uptodate(folio)) {
+		const unsigned int bpf = btrfs_blocks_per_folio(fs_info, folio);
+
+		bitmap_set(bfs->bitmaps, bpf * btrfs_bitmap_nr_uptodate, bpf);
+	}
+
 	folio_attach_private(folio, bfs);
 	return 0;
 }
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied
  2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
  2026-12-03 13:30   ` [PATCH v3 1/3] btrfs: don't dereference a missing folio private on writeback error Brahmajit Das
  2026-12-03 13:30   ` [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state Brahmajit Das
@ 2026-12-03 13:30   ` Brahmajit Das
  2026-12-03 20:36     ` Qu Wenruo
  2026-12-04  6:32   ` [PATCH v4 0/2] btrfs: fix dirtying of folios that lost their private listout
  3 siblings, 1 reply; 17+ messages in thread
From: Brahmajit Das @ 2026-12-03 13:30 UTC (permalink / raw)
  To: David Sterba, linux-btrfs
  Cc: Qu Wenruo, Chris Mason, David Hildenbrand, Jason Gunthorpe,
	John Hubbard, Peter Xu, linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

From: Brahmajit Das <bdas@suse.de>

syzbot reported an assertion failure in wait_subpage_spinlock():

  assertion failed: folio_test_private(folio) && folio_get_private(folio),
  in fs/btrfs/inode.c:7616
  kernel BUG at fs/btrfs/inode.c:7616!
  Call Trace:
   btrfs_invalidate_folio+0x26e/0xc50
   extent_writepage fs/btrfs/extent_io.c:2058 [inline]
   extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
   btrfs_writepages+0x129f/0x28c0
   do_writepages+0x338/0x560
   filemap_fdatawrite_range+0x1ef/0x2f0
   btrfs_start_ordered_extent_nowriteback+0x305/0x6e0
   btrfs_page_mkwrite+0x856/0x1a40
   ...
   __gup_longterm_locked+0x1032/0x15c0
   ...
   blkdev_direct_IO+0x121d/0x17b0
   aio_read+0x33a/0x4d0

The program does an O_DIRECT read into a MAP_SHARED mapping of a btrfs
file, while the same file is being truncated.

A dirty folio of a btrfs data inode is expected to have folio::private,
but a folio can lose it while it stays in the page cache and is pinned
by an in-flight O_DIRECT read, e.g.:

  truncate_inode_partial_folio()
    folio_invalidate(folio, offset, length)
      btrfs_invalidate_folio()
        btrfs_release_folio()          /* folio is clean */
          __btrfs_release_folio()
            clear_folio_extent_mapped()
    folio_split_or_unmap()
      folio_split()                    /* fails, the folio is pinned */

or from any failed large folio split, as __folio_freeze_split_file()
calls filemap_release_folio() before folio_ref_freeze().

When the read completes, the folio is re-dirtied without notifying the
fs:

  bio_release_pages()
    folio_mark_dirty()
      btrfs_data_dirty_folio()
        filemap_dirty_folio()

If the folio is beyond EOF by then, btrfs_data_dirty_folio() does not
touch any btrfs state, and the next writeback calls folio_invalidate()
on a folio without private, triggering the above ASSERT() in the
bs < folio size case.  If the folio straddles EOF,
btrfs_folio_set_fixup_dirty() dereferences the NULL folio private
instead.

Fix it by re-attaching the folio private in btrfs_data_dirty_folio()
if it is missing.  ->dirty_folio() may not block, so use GFP_NOWAIT.
The folio was only released, not invalidated, so it may still be
uptodate; btrfs_attach_folio_state() now syncs the uptodate bitmap with
the folio flag for that.

If the allocation fails, skip marking the blocks for fixup rather than
dereferencing a NULL private.  For a folio beyond EOF, attach the
private in the beyond-EOF branch of extent_writepage(), which can
sleep, before calling folio_invalidate().  A folio inside EOF is then
rejected by writeback with -EUCLEAN, which no longer crashes after the
previous patch.

Note: syzbot has no reproducer for this issue.  The fix was only build
tested, the race has not been reproduced locally.

This patch was written with the help of an AI coding assistant (Claude),
from the syzbot report, console log and review feedback.

Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
Fixes: 0680cbbf39ca ("btrfs: trigger cow fixup via dirty_folio()")
Signed-off-by: Brahmajit Das <bdas@suse.de>
---

Notes:
    v3:
    - Split into a series: fix the NULL dereference on the -EUCLEAN path
      of extent_writepage() (patch 1), sync the uptodate bitmap in
      btrfs_attach_folio_state() for all callers (patch 2) (Sashiko AI review)
    - Drop the explicit uptodate bitmap sync from btrfs_data_dirty_folio(),
      now done by patch 2
    - v2: https://lore.kernel.org/linux-btrfs/20261202041853.4184717-1-listout@listout.xyz/
    
    v2:
    - Re-attach the folio private in btrfs_data_dirty_folio() instead of
      skipping folio_invalidate() for folios without private (Qu)
    - Attach private only inside the beyond-EOF branch of extent_writepage(),
      leaving the unexpected dirty folio check alone (Qu)
    - Sync the uptodate bitmap of the new folio state with the folio flag
    - Explain where the folio private is dropped in the changelog
    - Cc GUP maintainers for the GUP part of the reasoning (Qu)
    - v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/

 fs/btrfs/extent_io.c | 24 +++++++++++++++++++++---
 fs/btrfs/extent_io.h |  1 +
 fs/btrfs/inode.c     | 16 +++++++++++++++-
 fs/btrfs/subpage.c   |  5 +++--
 fs/btrfs/subpage.h   |  3 ++-
 5 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 9dded5c670de..e2a6a4bf7f4b 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -923,11 +923,12 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,
 		folio_attach_private(folio, prealloc);
 	else
 		/* Do new allocation to attach subpage */
-		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
+		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA,
+					       GFP_NOFS);
 	return ret;
 }
 
-int set_folio_extent_mapped(struct folio *folio)
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp)
 {
 	struct btrfs_fs_info *fs_info;
 
@@ -939,12 +940,18 @@ int set_folio_extent_mapped(struct folio *folio)
 	fs_info = folio_to_fs_info(folio);
 
 	if (btrfs_is_subpage(fs_info, folio))
-		return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
+		return btrfs_attach_folio_state(fs_info, folio,
+						BTRFS_SUBPAGE_DATA, gfp);
 
 	folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
 	return 0;
 }
 
+int set_folio_extent_mapped(struct folio *folio)
+{
+	return set_folio_extent_mapped_gfp(folio, GFP_NOFS);
+}
+
 void clear_folio_extent_mapped(struct folio *folio)
 {
 	struct btrfs_fs_info *fs_info;
@@ -2046,6 +2053,17 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
 	pg_offset = offset_in_folio(folio, i_size);
 	if (folio->index > end_index ||
 	   (folio->index == end_index && !pg_offset)) {
+		/*
+		 * btrfs_invalidate_folio() requires folio::private for the
+		 * bs < folio size case.  A folio can still get here without
+		 * it, if btrfs_data_dirty_folio() failed its GFP_NOWAIT
+		 * allocation.  We can sleep here, so attach it now.
+		 */
+		ret = set_folio_extent_mapped(folio);
+		if (unlikely(ret < 0)) {
+			folio_unlock(folio);
+			return ret;
+		}
 		folio_invalidate(folio, 0, folio_size(folio));
 		folio_unlock(folio);
 		return 0;
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index c55b37cb24b7..1da7606efaad 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -285,6 +285,7 @@ int btrfs_writepages(struct address_space *mapping, struct writeback_control *wb
 int btree_writepages(struct address_space *mapping, struct writeback_control *wbc);
 void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
 void btrfs_readahead(struct readahead_control *rac);
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp);
 int set_folio_extent_mapped(struct folio *folio);
 void clear_folio_extent_mapped(struct folio *folio);
 
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 95c9530554d0..a8fdd1b834f2 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -10739,7 +10739,21 @@ static bool btrfs_data_dirty_folio(struct address_space *mapping,
 				    round_up(i_size_read(&inode->vfs_inode),
 					     fs_info->sectorsize));
 
-	if (range_end > page_start)
+	/*
+	 * The folio may have lost its private while it stayed in the page
+	 * cache, e.g. btrfs_release_folio() from a partial invalidation or
+	 * from a failed large folio split, while it was pinned by GUP.  When
+	 * the pin is released, the folio is dirtied through here, so
+	 * re-attach the private, as every dirty folio is expected to have it.
+	 *
+	 * We may not block here, thus GFP_NOWAIT.  If that fails, the blocks
+	 * can't be marked for fixup.  Writeback attaches the private later
+	 * for folios beyond EOF, and rejects folios inside EOF with -EUCLEAN.
+	 */
+	if (unlikely(!folio_test_private(folio)))
+		set_folio_extent_mapped_gfp(folio, GFP_NOWAIT | __GFP_NOWARN);
+
+	if (range_end > page_start && folio_test_private(folio))
 		btrfs_folio_set_fixup_dirty(fs_info, folio, page_start,
 					    range_end - page_start);
 	return filemap_dirty_folio(mapping, folio);
diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index 97d4824ae623..3b86c37af5aa 100644
--- a/fs/btrfs/subpage.c
+++ b/fs/btrfs/subpage.c
@@ -36,7 +36,8 @@
  */
 
 int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
-			     struct folio *folio, enum btrfs_folio_type type)
+			     struct folio *folio, enum btrfs_folio_type type,
+			     gfp_t gfp)
 {
 	struct btrfs_folio_state *bfs;
 
@@ -59,7 +60,7 @@ int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
 	if (type == BTRFS_SUBPAGE_DATA && !btrfs_is_subpage(fs_info, folio))
 		return 0;
 
-	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, GFP_NOFS);
+	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, gfp);
 	if (IS_ERR(bfs))
 		return PTR_ERR(bfs);
 
diff --git a/fs/btrfs/subpage.h b/fs/btrfs/subpage.h
index 9b106a73d682..83e758af4e4e 100644
--- a/fs/btrfs/subpage.h
+++ b/fs/btrfs/subpage.h
@@ -104,7 +104,8 @@ static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
 }
 
 int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
-			     struct folio *folio, enum btrfs_folio_type type);
+			     struct folio *folio, enum btrfs_folio_type type,
+			     gfp_t gfp);
 void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
 			      enum btrfs_folio_type type);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state
  2026-12-03 13:30   ` [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state Brahmajit Das
@ 2026-12-03 20:36     ` Qu Wenruo
  0 siblings, 0 replies; 17+ messages in thread
From: Qu Wenruo @ 2026-12-03 20:36 UTC (permalink / raw)
  To: Brahmajit Das, David Sterba, linux-btrfs
  Cc: Chris Mason, David Hildenbrand, Jason Gunthorpe, John Hubbard,
	Peter Xu, linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das



在 2026/12/4 00:00, Brahmajit Das 写道:
> From: Brahmajit Das <bdas@suse.de>
> 
> A data folio can lose its private while it stays in the page cache, e.g.
> btrfs_release_folio() from a partial invalidation, or from a large folio
> split that then fails.  Such a folio was only released, not invalidated,
> so it can still be uptodate.
> 
> When something calls set_folio_extent_mapped() on it again, such as
> prepare_one_folio(), btrfs_page_mkwrite() or btrfs_truncate_block(), a
> new btrfs_folio_state is attached with empty bitmaps, while PG_uptodate
> is still set on the folio.  Those callers see an uptodate folio, skip
> reading it, and only update the uptodate bits of their own range.  The
> other blocks are left marked not uptodate in the subpage bitmap.
> 
> That can trip the ASSERT() in can_skip_one_ordered_range(), which
> expects the whole range up to the ordered extent end or folio end to be
> uptodate if its first block is.
> 
> Fix it by setting the uptodate bitmap in btrfs_attach_folio_state() if
> the data folio is uptodate.  On the read path, private is attached
> before the folio is uptodate, so nothing changes there.
> 
> Note: this was only build tested.
> 
> This patch was written with the help of an AI coding assistant (Claude),
> following a review comment from Sashiko AI review.
> 
> Signed-off-by: Brahmajit Das <bdas@suse.de>
> ---
>   fs/btrfs/subpage.c | 13 +++++++++++++
>   1 file changed, 13 insertions(+)
> 
> diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
> index ebf18efe1ea3..97d4824ae623 100644
> --- a/fs/btrfs/subpage.c
> +++ b/fs/btrfs/subpage.c
> @@ -63,6 +63,19 @@ int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
>   	if (IS_ERR(bfs))
>   		return PTR_ERR(bfs);
>   
> +	/*
> +	 * A data folio can lose its private while staying uptodate in the
> +	 * page cache, e.g. btrfs_release_folio() on a folio that then fails
> +	 * to be removed or split.  The new state starts with empty bitmaps,
> +	 * so sync the uptodate bitmap with the folio flag, or the blocks not
> +	 * touched by the caller would be considered not uptodate.
> +	 */
> +	if (type == BTRFS_SUBPAGE_DATA && folio_test_uptodate(folio)) {
> +		const unsigned int bpf = btrfs_blocks_per_folio(fs_info, folio);
> +
> +		bitmap_set(bfs->bitmaps, bpf * btrfs_bitmap_nr_uptodate, bpf);
> +	}

There is really only one call site requiring this.

Please do it in the btrfs_data_dirty_folio() call site.

> +
>   	folio_attach_private(folio, bfs);
>   	return 0;
>   }


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied
  2026-12-03 13:30   ` [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
@ 2026-12-03 20:36     ` Qu Wenruo
  2026-12-04  6:36       ` Brahmajit Das
  0 siblings, 1 reply; 17+ messages in thread
From: Qu Wenruo @ 2026-12-03 20:36 UTC (permalink / raw)
  To: Brahmajit Das, David Sterba, linux-btrfs
  Cc: Chris Mason, David Hildenbrand, Jason Gunthorpe, John Hubbard,
	Peter Xu, linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das



在 2026/12/4 00:00, Brahmajit Das 写道:
> From: Brahmajit Das <bdas@suse.de>
> 
> syzbot reported an assertion failure in wait_subpage_spinlock():
> 
>    assertion failed: folio_test_private(folio) && folio_get_private(folio),
>    in fs/btrfs/inode.c:7616
>    kernel BUG at fs/btrfs/inode.c:7616!
>    Call Trace:
>     btrfs_invalidate_folio+0x26e/0xc50
>     extent_writepage fs/btrfs/extent_io.c:2058 [inline]
>     extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
>     btrfs_writepages+0x129f/0x28c0
>     do_writepages+0x338/0x560
>     filemap_fdatawrite_range+0x1ef/0x2f0
>     btrfs_start_ordered_extent_nowriteback+0x305/0x6e0
>     btrfs_page_mkwrite+0x856/0x1a40
>     ...
>     __gup_longterm_locked+0x1032/0x15c0
>     ...
>     blkdev_direct_IO+0x121d/0x17b0
>     aio_read+0x33a/0x4d0
> 
> The program does an O_DIRECT read into a MAP_SHARED mapping of a btrfs
> file, while the same file is being truncated.
> 
> A dirty folio of a btrfs data inode is expected to have folio::private,
> but a folio can lose it while it stays in the page cache and is pinned
> by an in-flight O_DIRECT read, e.g.:
> 
>    truncate_inode_partial_folio()
>      folio_invalidate(folio, offset, length)
>        btrfs_invalidate_folio()
>          btrfs_release_folio()          /* folio is clean */
>            __btrfs_release_folio()
>              clear_folio_extent_mapped()
>      folio_split_or_unmap()
>        folio_split()                    /* fails, the folio is pinned */
> 
> or from any failed large folio split, as __folio_freeze_split_file()
> calls filemap_release_folio() before folio_ref_freeze().
> 
> When the read completes, the folio is re-dirtied without notifying the
> fs:
> 
>    bio_release_pages()
>      folio_mark_dirty()
>        btrfs_data_dirty_folio()
>          filemap_dirty_folio()
> 
> If the folio is beyond EOF by then, btrfs_data_dirty_folio() does not
> touch any btrfs state, and the next writeback calls folio_invalidate()
> on a folio without private, triggering the above ASSERT() in the
> bs < folio size case.  If the folio straddles EOF,
> btrfs_folio_set_fixup_dirty() dereferences the NULL folio private
> instead.
> 
> Fix it by re-attaching the folio private in btrfs_data_dirty_folio()
> if it is missing.  ->dirty_folio() may not block, so use GFP_NOWAIT.
> The folio was only released, not invalidated, so it may still be
> uptodate; btrfs_attach_folio_state() now syncs the uptodate bitmap with
> the folio flag for that.
> 
> If the allocation fails, skip marking the blocks for fixup rather than
> dereferencing a NULL private.  For a folio beyond EOF, attach the
> private in the beyond-EOF branch of extent_writepage(), which can
> sleep, before calling folio_invalidate().  A folio inside EOF is then
> rejected by writeback with -EUCLEAN, which no longer crashes after the
> previous patch.
> 
> Note: syzbot has no reproducer for this issue.  The fix was only build
> tested, the race has not been reproduced locally.
> 
> This patch was written with the help of an AI coding assistant (Claude),
> from the syzbot report, console log and review feedback.
> 
> Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
> Fixes: 0680cbbf39ca ("btrfs: trigger cow fixup via dirty_folio()")
> Signed-off-by: Brahmajit Das <bdas@suse.de>
> ---
> 
> Notes:
>      v3:
>      - Split into a series: fix the NULL dereference on the -EUCLEAN path
>        of extent_writepage() (patch 1), sync the uptodate bitmap in
>        btrfs_attach_folio_state() for all callers (patch 2) (Sashiko AI review)
>      - Drop the explicit uptodate bitmap sync from btrfs_data_dirty_folio(),
>        now done by patch 2
>      - v2: https://lore.kernel.org/linux-btrfs/20261202041853.4184717-1-listout@listout.xyz/
>      
>      v2:
>      - Re-attach the folio private in btrfs_data_dirty_folio() instead of
>        skipping folio_invalidate() for folios without private (Qu)
>      - Attach private only inside the beyond-EOF branch of extent_writepage(),
>        leaving the unexpected dirty folio check alone (Qu)
>      - Sync the uptodate bitmap of the new folio state with the folio flag
>      - Explain where the folio private is dropped in the changelog
>      - Cc GUP maintainers for the GUP part of the reasoning (Qu)
>      - v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/
> 
>   fs/btrfs/extent_io.c | 24 +++++++++++++++++++++---
>   fs/btrfs/extent_io.h |  1 +
>   fs/btrfs/inode.c     | 16 +++++++++++++++-
>   fs/btrfs/subpage.c   |  5 +++--
>   fs/btrfs/subpage.h   |  3 ++-
>   5 files changed, 42 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> index 9dded5c670de..e2a6a4bf7f4b 100644
> --- a/fs/btrfs/extent_io.c
> +++ b/fs/btrfs/extent_io.c
> @@ -923,11 +923,12 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,
>   		folio_attach_private(folio, prealloc);
>   	else
>   		/* Do new allocation to attach subpage */
> -		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
> +		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA,
> +					       GFP_NOFS);
>   	return ret;
>   }
>   
> -int set_folio_extent_mapped(struct folio *folio)
> +int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp)
>   {
>   	struct btrfs_fs_info *fs_info;
>   
> @@ -939,12 +940,18 @@ int set_folio_extent_mapped(struct folio *folio)
>   	fs_info = folio_to_fs_info(folio);
>   
>   	if (btrfs_is_subpage(fs_info, folio))
> -		return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
> +		return btrfs_attach_folio_state(fs_info, folio,
> +						BTRFS_SUBPAGE_DATA, gfp);
>   
>   	folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
>   	return 0;
>   }
>   
> +int set_folio_extent_mapped(struct folio *folio)
> +{
> +	return set_folio_extent_mapped_gfp(folio, GFP_NOFS);
> +}
> +
>   void clear_folio_extent_mapped(struct folio *folio)
>   {
>   	struct btrfs_fs_info *fs_info;
> @@ -2046,6 +2053,17 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
>   	pg_offset = offset_in_folio(folio, i_size);
>   	if (folio->index > end_index ||
>   	   (folio->index == end_index && !pg_offset)) {
> +		/*
> +		 * btrfs_invalidate_folio() requires folio::private for the
> +		 * bs < folio size case.  A folio can still get here without
> +		 * it, if btrfs_data_dirty_folio() failed its GFP_NOWAIT
> +		 * allocation.  We can sleep here, so attach it now.
> +		 */
> +		ret = set_folio_extent_mapped(folio);
> +		if (unlikely(ret < 0)) {
> +			folio_unlock(folio);
> +			return ret;
> +		}
>   		folio_invalidate(folio, 0, folio_size(folio));
>   		folio_unlock(folio);
>   		return 0;
> diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
> index c55b37cb24b7..1da7606efaad 100644
> --- a/fs/btrfs/extent_io.h
> +++ b/fs/btrfs/extent_io.h
> @@ -285,6 +285,7 @@ int btrfs_writepages(struct address_space *mapping, struct writeback_control *wb
>   int btree_writepages(struct address_space *mapping, struct writeback_control *wbc);
>   void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
>   void btrfs_readahead(struct readahead_control *rac);
> +int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp);
>   int set_folio_extent_mapped(struct folio *folio);
>   void clear_folio_extent_mapped(struct folio *folio);
>   
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 95c9530554d0..a8fdd1b834f2 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -10739,7 +10739,21 @@ static bool btrfs_data_dirty_folio(struct address_space *mapping,
>   				    round_up(i_size_read(&inode->vfs_inode),
>   					     fs_info->sectorsize));
>   
> -	if (range_end > page_start)
> +	/*
> +	 * The folio may have lost its private while it stayed in the page
> +	 * cache, e.g. btrfs_release_folio() from a partial invalidation or
> +	 * from a failed large folio split, while it was pinned by GUP.  When
> +	 * the pin is released, the folio is dirtied through here, so
> +	 * re-attach the private, as every dirty folio is expected to have it.
> +	 *
> +	 * We may not block here, thus GFP_NOWAIT.  If that fails, the blocks
> +	 * can't be marked for fixup.  Writeback attaches the private later
> +	 * for folios beyond EOF, and rejects folios inside EOF with -EUCLEAN.
> +	 */
> +	if (unlikely(!folio_test_private(folio)))
> +		set_folio_extent_mapped_gfp(folio, GFP_NOWAIT | __GFP_NOWARN);

Can you explain why iomap can go NOFAIL but why we can not here?

I don't think this is any different from your previous atttempt, which 
can still fail.

And I think the iomap solution is more reliable than this one.

> +
> +	if (range_end > page_start && folio_test_private(folio))
>   		btrfs_folio_set_fixup_dirty(fs_info, folio, page_start,
>   					    range_end - page_start);
>   	return filemap_dirty_folio(mapping, folio);
> diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
> index 97d4824ae623..3b86c37af5aa 100644
> --- a/fs/btrfs/subpage.c
> +++ b/fs/btrfs/subpage.c
> @@ -36,7 +36,8 @@
>    */
>   
>   int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
> -			     struct folio *folio, enum btrfs_folio_type type)
> +			     struct folio *folio, enum btrfs_folio_type type,
> +			     gfp_t gfp)
>   {
>   	struct btrfs_folio_state *bfs;
>   
> @@ -59,7 +60,7 @@ int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
>   	if (type == BTRFS_SUBPAGE_DATA && !btrfs_is_subpage(fs_info, folio))
>   		return 0;
>   
> -	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, GFP_NOFS);
> +	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, gfp);
>   	if (IS_ERR(bfs))
>   		return PTR_ERR(bfs);
>   
> diff --git a/fs/btrfs/subpage.h b/fs/btrfs/subpage.h
> index 9b106a73d682..83e758af4e4e 100644
> --- a/fs/btrfs/subpage.h
> +++ b/fs/btrfs/subpage.h
> @@ -104,7 +104,8 @@ static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
>   }
>   
>   int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
> -			     struct folio *folio, enum btrfs_folio_type type);
> +			     struct folio *folio, enum btrfs_folio_type type,
> +			     gfp_t gfp);
>   void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
>   			      enum btrfs_folio_type type);
>   


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v4 0/2] btrfs: fix dirtying of folios that lost their private
  2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
                     ` (2 preceding siblings ...)
  2026-12-03 13:30   ` [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
@ 2026-12-04  6:32   ` listout
  2026-12-04  6:32     ` [PATCH v4 1/2] btrfs: don't dereference a missing folio private on writeback error listout
  2026-12-04  6:32     ` [PATCH v4 2/2] btrfs: re-attach folio private when a released folio gets dirtied listout
  3 siblings, 2 replies; 17+ messages in thread
From: listout @ 2026-12-04  6:32 UTC (permalink / raw)
  To: David Sterba, linux-btrfs
  Cc: Qu Wenruo, Chris Mason, clm, david, jgg, jhubbard, peterx,
	linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

From: Brahmajit Das <bdas@suse.de>

syzbot hit an ASSERT() in wait_subpage_spinlock() when writeback
invalidates a beyond-EOF folio that has no folio::private.  The folio
lost its private while it stayed in the page cache, pinned by an
O_DIRECT read, from btrfs_release_folio() during a partial invalidation
or a failed large folio split.  When the read completed, the folio was
dirtied again through btrfs_data_dirty_folio(), still without private.

Patch 2 is the fix.  btrfs_data_dirty_folio() now re-attaches the
private with GFP_NOFS | __GFP_NOFAIL, the same way iomap_dirty_folio()
allocates its per-folio state, and syncs the uptodate bitmap with the
folio flag.

Patch 1 fixes a NULL dereference in the -EUCLEAN path of
extent_writepage() for a dirty folio without private, which predates
this series.  It is unchanged from v3.

Both patches are build tested only.  syzbot has no reproducer.

The series was written with the help of an AI coding assistant (Claude).

v4:
- Use GFP_NOFS | __GFP_NOFAIL like iomap, and drop the GFP_NOWAIT
  failure handling and the extent_writepage() fallback (Qu).  This also
  resolves the v3 open question about losing fixup ranges when the
  allocation fails.
- Drop v3 patch 2 and sync the uptodate bitmap only in
  btrfs_data_dirty_folio() (Qu)

v3: https://lore.kernel.org/linux-btrfs/20261203133059.2193197-1-listout@listout.xyz/
v2: https://lore.kernel.org/linux-btrfs/20261202041853.4184717-1-listout@listout.xyz/
v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/

Brahmajit Das (2):
  btrfs: don't dereference a missing folio private on writeback error
  btrfs: re-attach folio private when a released folio gets dirtied

 fs/btrfs/extent_io.c | 24 +++++++++++++++++++-----
 fs/btrfs/extent_io.h |  1 +
 fs/btrfs/inode.c     | 23 +++++++++++++++++++++++
 fs/btrfs/subpage.c   |  5 +++--
 fs/btrfs/subpage.h   |  3 ++-
 5 files changed, 48 insertions(+), 8 deletions(-)


base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106
-- 
2.56.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v4 1/2] btrfs: don't dereference a missing folio private on writeback error
  2026-12-04  6:32   ` [PATCH v4 0/2] btrfs: fix dirtying of folios that lost their private listout
@ 2026-12-04  6:32     ` listout
  2026-12-04  6:32     ` [PATCH v4 2/2] btrfs: re-attach folio private when a released folio gets dirtied listout
  1 sibling, 0 replies; 17+ messages in thread
From: listout @ 2026-12-04  6:32 UTC (permalink / raw)
  To: David Sterba, linux-btrfs
  Cc: Qu Wenruo, Chris Mason, clm, david, jgg, jhubbard, peterx,
	linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

From: Brahmajit Das <bdas@suse.de>

extent_writepage() rejects a dirty folio without folio::private with
-EUCLEAN, then jumps to the common exit path, which calls
btrfs_folio_end_lock_bitmap().  For the bs < folio size case, that
function dereferences the folio private:

  btrfs_folio_end_lock_bitmap()
    bfs = folio_get_private(folio);	/* NULL */
    ...
    if (atomic_read(&bfs->nr_locked) == 0)	/* NULL dereference */

so the error path meant to catch such a folio crashes instead.

No subpage lock can be held on a folio without private, so just set the
mapping error and unlock the folio directly.

Note: this was only build tested.

This patch was written with the help of an AI coding assistant (Claude).

Fixes: 7ca3e84980ef ("btrfs: reject out-of-band dirty folios during writeback")
Signed-off-by: Brahmajit Das <bdas@suse.de>
---
 fs/btrfs/extent_io.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..9dded5c670de 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2070,8 +2070,15 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
 	"root %lld ino %llu folio %llu is marked dirty without notifying the fs",
 			     btrfs_root_id(inode->root),
 			     btrfs_ino(inode), folio_pos(folio));
-		ret = -EUCLEAN;
-		goto done;
+		/*
+		 * Don't go through btrfs_folio_end_lock_bitmap(), which
+		 * dereferences the missing folio private for the
+		 * bs < folio size case.  No subpage lock can be held on a
+		 * folio without private, so a plain unlock is enough.
+		 */
+		mapping_set_error(folio->mapping, -EUCLEAN);
+		folio_unlock(folio);
+		return -EUCLEAN;
 	}
 
 	ret = set_folio_extent_mapped(folio);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v4 2/2] btrfs: re-attach folio private when a released folio gets dirtied
  2026-12-04  6:32   ` [PATCH v4 0/2] btrfs: fix dirtying of folios that lost their private listout
  2026-12-04  6:32     ` [PATCH v4 1/2] btrfs: don't dereference a missing folio private on writeback error listout
@ 2026-12-04  6:32     ` listout
  1 sibling, 0 replies; 17+ messages in thread
From: listout @ 2026-12-04  6:32 UTC (permalink / raw)
  To: David Sterba, linux-btrfs
  Cc: Qu Wenruo, Chris Mason, clm, david, jgg, jhubbard, peterx,
	linux-mm, linux-kernel, syzkaller-bugs,
	syzbot+d6f123d4060990f0f3ff, Brahmajit Das

From: Brahmajit Das <bdas@suse.de>

syzbot reported an assertion failure in wait_subpage_spinlock():

  assertion failed: folio_test_private(folio) && folio_get_private(folio),
  in fs/btrfs/inode.c:7616
  kernel BUG at fs/btrfs/inode.c:7616!
  Call Trace:
   btrfs_invalidate_folio+0x26e/0xc50
   extent_writepage fs/btrfs/extent_io.c:2058 [inline]
   extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
   btrfs_writepages+0x129f/0x28c0
   do_writepages+0x338/0x560
   filemap_fdatawrite_range+0x1ef/0x2f0
   btrfs_start_ordered_extent_nowriteback+0x305/0x6e0
   btrfs_page_mkwrite+0x856/0x1a40
   ...
   __gup_longterm_locked+0x1032/0x15c0
   ...
   blkdev_direct_IO+0x121d/0x17b0
   aio_read+0x33a/0x4d0

The program does an O_DIRECT read into a MAP_SHARED mapping of a btrfs
file, while the same file is being truncated.

A dirty folio of a btrfs data inode is expected to have folio::private,
but a folio can lose it while it stays in the page cache and is pinned
by an in-flight O_DIRECT read, e.g.:

  truncate_inode_partial_folio()
    folio_invalidate(folio, offset, length)
      btrfs_invalidate_folio()
        btrfs_release_folio()          /* folio is clean */
          __btrfs_release_folio()
            clear_folio_extent_mapped()
    folio_split_or_unmap()
      folio_split()                    /* fails, the folio is pinned */

or from any failed large folio split, as __folio_freeze_split_file()
calls filemap_release_folio() before folio_ref_freeze().

When the read completes, the folio is re-dirtied without notifying the
fs:

  bio_release_pages()
    folio_mark_dirty_lock()
      btrfs_data_dirty_folio()
        filemap_dirty_folio()

If the folio is beyond EOF by then, btrfs_data_dirty_folio() does not
touch any btrfs state, and the next writeback calls folio_invalidate()
on a folio without private, triggering the above ASSERT() in the
bs < folio size case.  If the folio straddles EOF,
btrfs_folio_set_fixup_dirty() dereferences the NULL folio private
instead.

Fix it by re-attaching the folio private in btrfs_data_dirty_folio()
if it is missing, the same way iomap_dirty_folio() allocates its
per-folio state, with GFP_NOFS | __GFP_NOFAIL.  This is safe because a
folio only loses its private while it is clean, and a clean folio has
no writable PTEs, so the atomic callers of ->dirty_folio() that hold
the PTE lock never see such a folio.  The bio path above dirties folios
from process context, as bio_check_pages_dirty() defers to a workqueue.

The folio was only released, not invalidated, so it can still be
uptodate.  The new folio state starts with an empty uptodate bitmap, so
sync it with the folio flag there.

Note: syzbot has no reproducer for this issue.  The fix was only build
tested, the race has not been reproduced locally.

This patch was written with the help of an AI coding assistant (Claude),
from the syzbot report, console log and review feedback.

Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
Fixes: 0680cbbf39ca ("btrfs: trigger cow fixup via dirty_folio()")
Signed-off-by: Brahmajit Das <bdas@suse.de>
---

Notes:
    v4:
    - Allocate with GFP_NOFS | __GFP_NOFAIL like iomap_dirty_folio(), and
      drop the GFP_NOWAIT failure handling and the extent_writepage()
      fallback (Qu)
    - Sync the uptodate bitmap in btrfs_data_dirty_folio() instead of in
      btrfs_attach_folio_state(), dropping v3 patch 2 (Qu)
    - v3: https://lore.kernel.org/linux-btrfs/20261203133059.2193197-1-listout@listout.xyz/
    
    v3:
    - Split into a series (Sashiko AI review)
    - v2: https://lore.kernel.org/linux-btrfs/20261202041853.4184717-1-listout@listout.xyz/
    
    v2:
    - Re-attach the folio private in btrfs_data_dirty_folio() (Qu)
    - v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/

 fs/btrfs/extent_io.c | 13 ++++++++++---
 fs/btrfs/extent_io.h |  1 +
 fs/btrfs/inode.c     | 23 +++++++++++++++++++++++
 fs/btrfs/subpage.c   |  5 +++--
 fs/btrfs/subpage.h   |  3 ++-
 5 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 9dded5c670de..9fc36215febf 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -923,11 +923,12 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,
 		folio_attach_private(folio, prealloc);
 	else
 		/* Do new allocation to attach subpage */
-		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
+		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA,
+					       GFP_NOFS);
 	return ret;
 }
 
-int set_folio_extent_mapped(struct folio *folio)
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp)
 {
 	struct btrfs_fs_info *fs_info;
 
@@ -939,12 +940,18 @@ int set_folio_extent_mapped(struct folio *folio)
 	fs_info = folio_to_fs_info(folio);
 
 	if (btrfs_is_subpage(fs_info, folio))
-		return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
+		return btrfs_attach_folio_state(fs_info, folio,
+						BTRFS_SUBPAGE_DATA, gfp);
 
 	folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
 	return 0;
 }
 
+int set_folio_extent_mapped(struct folio *folio)
+{
+	return set_folio_extent_mapped_gfp(folio, GFP_NOFS);
+}
+
 void clear_folio_extent_mapped(struct folio *folio)
 {
 	struct btrfs_fs_info *fs_info;
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index c55b37cb24b7..1da7606efaad 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -285,6 +285,7 @@ int btrfs_writepages(struct address_space *mapping, struct writeback_control *wb
 int btree_writepages(struct address_space *mapping, struct writeback_control *wbc);
 void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
 void btrfs_readahead(struct readahead_control *rac);
+int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp);
 int set_folio_extent_mapped(struct folio *folio);
 void clear_folio_extent_mapped(struct folio *folio);
 
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 95c9530554d0..5d598d143880 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -10739,6 +10739,29 @@ static bool btrfs_data_dirty_folio(struct address_space *mapping,
 				    round_up(i_size_read(&inode->vfs_inode),
 					     fs_info->sectorsize));
 
+	/*
+	 * The folio may have lost its private while it stayed in the page
+	 * cache, e.g. btrfs_release_folio() from a partial invalidation or
+	 * from a failed large folio split, while it was pinned by GUP.  When
+	 * the pin is released, the folio is dirtied through here, so
+	 * re-attach the private, as every dirty folio is expected to have it.
+	 *
+	 * Like iomap_dirty_folio(), use __GFP_NOFAIL.  A folio can only lose
+	 * its private while it is clean, and a clean folio has no writable
+	 * PTEs, so we are never called here for such a folio from the atomic
+	 * callers that hold the PTE lock.
+	 *
+	 * The folio was only released, not invalidated, so it can still be
+	 * uptodate.  The new folio state starts with an empty uptodate
+	 * bitmap, so sync it with the folio flag.
+	 */
+	if (unlikely(!folio_test_private(folio))) {
+		set_folio_extent_mapped_gfp(folio, GFP_NOFS | __GFP_NOFAIL);
+		if (folio_test_uptodate(folio))
+			btrfs_folio_set_uptodate(fs_info, folio, page_start,
+						 folio_size(folio));
+	}
+
 	if (range_end > page_start)
 		btrfs_folio_set_fixup_dirty(fs_info, folio, page_start,
 					    range_end - page_start);
diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index ebf18efe1ea3..c779bf4771b9 100644
--- a/fs/btrfs/subpage.c
+++ b/fs/btrfs/subpage.c
@@ -36,7 +36,8 @@
  */
 
 int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
-			     struct folio *folio, enum btrfs_folio_type type)
+			     struct folio *folio, enum btrfs_folio_type type,
+			     gfp_t gfp)
 {
 	struct btrfs_folio_state *bfs;
 
@@ -59,7 +60,7 @@ int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
 	if (type == BTRFS_SUBPAGE_DATA && !btrfs_is_subpage(fs_info, folio))
 		return 0;
 
-	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, GFP_NOFS);
+	bfs = btrfs_alloc_folio_state(fs_info, folio_size(folio), type, gfp);
 	if (IS_ERR(bfs))
 		return PTR_ERR(bfs);
 
diff --git a/fs/btrfs/subpage.h b/fs/btrfs/subpage.h
index 9b106a73d682..83e758af4e4e 100644
--- a/fs/btrfs/subpage.h
+++ b/fs/btrfs/subpage.h
@@ -104,7 +104,8 @@ static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
 }
 
 int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
-			     struct folio *folio, enum btrfs_folio_type type);
+			     struct folio *folio, enum btrfs_folio_type type,
+			     gfp_t gfp);
 void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
 			      enum btrfs_folio_type type);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied
  2026-12-03 20:36     ` Qu Wenruo
@ 2026-12-04  6:36       ` Brahmajit Das
  0 siblings, 0 replies; 17+ messages in thread
From: Brahmajit Das @ 2026-12-04  6:36 UTC (permalink / raw)
  To: Qu Wenruo
  Cc: David Sterba, linux-btrfs, Chris Mason, David Hildenbrand,
	Jason Gunthorpe, John Hubbard, Peter Xu, linux-mm, linux-kernel,
	syzkaller-bugs, syzbot+d6f123d4060990f0f3ff

On Fri, Dec 04, 2026 at 07:06:55 +1030, Qu Wenruo wrote:
> 
> 
> 在 2026/12/4 00:00, Brahmajit Das 写道:
> > From: Brahmajit Das <bdas@suse.de>
> > 
> > syzbot reported an assertion failure in wait_subpage_spinlock():
> > 
> >    assertion failed: folio_test_private(folio) && folio_get_private(folio),
> >    in fs/btrfs/inode.c:7616
> >    kernel BUG at fs/btrfs/inode.c:7616!
> >    Call Trace:
> >     btrfs_invalidate_folio+0x26e/0xc50
> >     extent_writepage fs/btrfs/extent_io.c:2058 [inline]
> >     extent_write_cache_pages fs/btrfs/extent_io.c:2843 [inline]
> >     btrfs_writepages+0x129f/0x28c0
> >     do_writepages+0x338/0x560
> >     filemap_fdatawrite_range+0x1ef/0x2f0
> >     btrfs_start_ordered_extent_nowriteback+0x305/0x6e0
> >     btrfs_page_mkwrite+0x856/0x1a40
> >     ...
> >     __gup_longterm_locked+0x1032/0x15c0
> >     ...
> >     blkdev_direct_IO+0x121d/0x17b0
> >     aio_read+0x33a/0x4d0
> > 
> > The program does an O_DIRECT read into a MAP_SHARED mapping of a btrfs
> > file, while the same file is being truncated.
> > 
> > A dirty folio of a btrfs data inode is expected to have folio::private,
> > but a folio can lose it while it stays in the page cache and is pinned
> > by an in-flight O_DIRECT read, e.g.:
> > 
> >    truncate_inode_partial_folio()
> >      folio_invalidate(folio, offset, length)
> >        btrfs_invalidate_folio()
> >          btrfs_release_folio()          /* folio is clean */
> >            __btrfs_release_folio()
> >              clear_folio_extent_mapped()
> >      folio_split_or_unmap()
> >        folio_split()                    /* fails, the folio is pinned */
> > 
> > or from any failed large folio split, as __folio_freeze_split_file()
> > calls filemap_release_folio() before folio_ref_freeze().
> > 
> > When the read completes, the folio is re-dirtied without notifying the
> > fs:
> > 
> >    bio_release_pages()
> >      folio_mark_dirty()
> >        btrfs_data_dirty_folio()
> >          filemap_dirty_folio()
> > 
> > If the folio is beyond EOF by then, btrfs_data_dirty_folio() does not
> > touch any btrfs state, and the next writeback calls folio_invalidate()
> > on a folio without private, triggering the above ASSERT() in the
> > bs < folio size case.  If the folio straddles EOF,
> > btrfs_folio_set_fixup_dirty() dereferences the NULL folio private
> > instead.
> > 
> > Fix it by re-attaching the folio private in btrfs_data_dirty_folio()
> > if it is missing.  ->dirty_folio() may not block, so use GFP_NOWAIT.
> > The folio was only released, not invalidated, so it may still be
> > uptodate; btrfs_attach_folio_state() now syncs the uptodate bitmap with
> > the folio flag for that.
> > 
> > If the allocation fails, skip marking the blocks for fixup rather than
> > dereferencing a NULL private.  For a folio beyond EOF, attach the
> > private in the beyond-EOF branch of extent_writepage(), which can
> > sleep, before calling folio_invalidate().  A folio inside EOF is then
> > rejected by writeback with -EUCLEAN, which no longer crashes after the
> > previous patch.
> > 
> > Note: syzbot has no reproducer for this issue.  The fix was only build
> > tested, the race has not been reproduced locally.
> > 
> > This patch was written with the help of an AI coding assistant (Claude),
> > from the syzbot report, console log and review feedback.
> > 
> > Reported-by: syzbot+d6f123d4060990f0f3ff@syzkaller.appspotmail.com
> > Closes: https://syzkaller.appspot.com/bug?extid=d6f123d4060990f0f3ff
> > Fixes: 0680cbbf39ca ("btrfs: trigger cow fixup via dirty_folio()")
> > Signed-off-by: Brahmajit Das <bdas@suse.de>
> > ---
> > 
> > Notes:
> >      v3:
> >      - Split into a series: fix the NULL dereference on the -EUCLEAN path
> >        of extent_writepage() (patch 1), sync the uptodate bitmap in
> >        btrfs_attach_folio_state() for all callers (patch 2) (Sashiko AI review)
> >      - Drop the explicit uptodate bitmap sync from btrfs_data_dirty_folio(),
> >        now done by patch 2
> >      - v2: https://lore.kernel.org/linux-btrfs/20261202041853.4184717-1-listout@listout.xyz/
> >      v2:
> >      - Re-attach the folio private in btrfs_data_dirty_folio() instead of
> >        skipping folio_invalidate() for folios without private (Qu)
> >      - Attach private only inside the beyond-EOF branch of extent_writepage(),
> >        leaving the unexpected dirty folio check alone (Qu)
> >      - Sync the uptodate bitmap of the new folio state with the folio flag
> >      - Explain where the folio private is dropped in the changelog
> >      - Cc GUP maintainers for the GUP part of the reasoning (Qu)
> >      - v1: https://lore.kernel.org/linux-btrfs/20261130182637.468143-1-listout@listout.xyz/
> > 
> >   fs/btrfs/extent_io.c | 24 +++++++++++++++++++++---
> >   fs/btrfs/extent_io.h |  1 +
> >   fs/btrfs/inode.c     | 16 +++++++++++++++-
> >   fs/btrfs/subpage.c   |  5 +++--
> >   fs/btrfs/subpage.h   |  3 ++-
> >   5 files changed, 42 insertions(+), 7 deletions(-)
> > 
> > diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
> > index 9dded5c670de..e2a6a4bf7f4b 100644
> > --- a/fs/btrfs/extent_io.c
> > +++ b/fs/btrfs/extent_io.c
> > @@ -923,11 +923,12 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,
> >   		folio_attach_private(folio, prealloc);
> >   	else
> >   		/* Do new allocation to attach subpage */
> > -		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
> > +		ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA,
> > +					       GFP_NOFS);
> >   	return ret;
> >   }
> > -int set_folio_extent_mapped(struct folio *folio)
> > +int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp)
> >   {
> >   	struct btrfs_fs_info *fs_info;
> > @@ -939,12 +940,18 @@ int set_folio_extent_mapped(struct folio *folio)
> >   	fs_info = folio_to_fs_info(folio);
> >   	if (btrfs_is_subpage(fs_info, folio))
> > -		return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
> > +		return btrfs_attach_folio_state(fs_info, folio,
> > +						BTRFS_SUBPAGE_DATA, gfp);
> >   	folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
> >   	return 0;
> >   }
> > +int set_folio_extent_mapped(struct folio *folio)
> > +{
> > +	return set_folio_extent_mapped_gfp(folio, GFP_NOFS);
> > +}
> > +
> >   void clear_folio_extent_mapped(struct folio *folio)
> >   {
> >   	struct btrfs_fs_info *fs_info;
> > @@ -2046,6 +2053,17 @@ static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl
> >   	pg_offset = offset_in_folio(folio, i_size);
> >   	if (folio->index > end_index ||
> >   	   (folio->index == end_index && !pg_offset)) {
> > +		/*
> > +		 * btrfs_invalidate_folio() requires folio::private for the
> > +		 * bs < folio size case.  A folio can still get here without
> > +		 * it, if btrfs_data_dirty_folio() failed its GFP_NOWAIT
> > +		 * allocation.  We can sleep here, so attach it now.
> > +		 */
> > +		ret = set_folio_extent_mapped(folio);
> > +		if (unlikely(ret < 0)) {
> > +			folio_unlock(folio);
> > +			return ret;
> > +		}
> >   		folio_invalidate(folio, 0, folio_size(folio));
> >   		folio_unlock(folio);
> >   		return 0;
> > diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
> > index c55b37cb24b7..1da7606efaad 100644
> > --- a/fs/btrfs/extent_io.h
> > +++ b/fs/btrfs/extent_io.h
> > @@ -285,6 +285,7 @@ int btrfs_writepages(struct address_space *mapping, struct writeback_control *wb
> >   int btree_writepages(struct address_space *mapping, struct writeback_control *wbc);
> >   void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
> >   void btrfs_readahead(struct readahead_control *rac);
> > +int set_folio_extent_mapped_gfp(struct folio *folio, gfp_t gfp);
> >   int set_folio_extent_mapped(struct folio *folio);
> >   void clear_folio_extent_mapped(struct folio *folio);
> > diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> > index 95c9530554d0..a8fdd1b834f2 100644
> > --- a/fs/btrfs/inode.c
> > +++ b/fs/btrfs/inode.c
> > @@ -10739,7 +10739,21 @@ static bool btrfs_data_dirty_folio(struct address_space *mapping,
> >   				    round_up(i_size_read(&inode->vfs_inode),
> >   					     fs_info->sectorsize));
> > -	if (range_end > page_start)
> > +	/*
> > +	 * The folio may have lost its private while it stayed in the page
> > +	 * cache, e.g. btrfs_release_folio() from a partial invalidation or
> > +	 * from a failed large folio split, while it was pinned by GUP.  When
> > +	 * the pin is released, the folio is dirtied through here, so
> > +	 * re-attach the private, as every dirty folio is expected to have it.
> > +	 *
> > +	 * We may not block here, thus GFP_NOWAIT.  If that fails, the blocks
> > +	 * can't be marked for fixup.  Writeback attaches the private later
> > +	 * for folios beyond EOF, and rejects folios inside EOF with -EUCLEAN.
> > +	 */
> > +	if (unlikely(!folio_test_private(folio)))
> > +		set_folio_extent_mapped_gfp(folio, GFP_NOWAIT | __GFP_NOWARN);
> 
> Can you explain why iomap can go NOFAIL but why we can not here?
> 
> I don't think this is any different from your previous atttempt, which can
> still fail.
> 
> And I think the iomap solution is more reliable than this one.
> 

I used GFP_NOWAIT because I assumed ->dirty_folio() can run in atomic
context. The atomic callers do exist (folio_mark_dirty() under the PTE
lock in zap/unmap), but they only get there for a folio with a dirty
PTE. A folio can only lose its private while it's clean, and a clean
folio has no writable PTEs, so those callers always see a folio that
already has private. The bio path dirties pages from process context,
since bio_check_pages_dirty() defers to a workqueue.

v4 uses GFP_NOFS | __GFP_NOFAIL like iomap_dirty_folio(), and the
NOWAIT failure handling and the extent_writepage() fallback are gone.
The uptodate sync now lives only in btrfs_data_dirty_folio(), as you
asked on patch 2.

-- 
Brahmajit Das
SUSE L3

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2026-12-04  6:36 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-22 19:05 [syzbot] [btrfs?] kernel BUG in wait_subpage_spinlock syzbot
2026-11-30 18:26 ` [PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage() Brahmajit Das
2026-11-30 20:47   ` Qu Wenruo
2026-12-01 13:14     ` listout
2026-12-01 20:27       ` Qu Wenruo
2026-12-02  3:34         ` Brahmajit Das
2026-12-02  3:55           ` Qu Wenruo
2026-12-03 13:30 ` [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private Brahmajit Das
2026-12-03 13:30   ` [PATCH v3 1/3] btrfs: don't dereference a missing folio private on writeback error Brahmajit Das
2026-12-03 13:30   ` [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state Brahmajit Das
2026-12-03 20:36     ` Qu Wenruo
2026-12-03 13:30   ` [PATCH v3 3/3] btrfs: re-attach folio private when a released folio gets dirtied Brahmajit Das
2026-12-03 20:36     ` Qu Wenruo
2026-12-04  6:36       ` Brahmajit Das
2026-12-04  6:32   ` [PATCH v4 0/2] btrfs: fix dirtying of folios that lost their private listout
2026-12-04  6:32     ` [PATCH v4 1/2] btrfs: don't dereference a missing folio private on writeback error listout
2026-12-04  6:32     ` [PATCH v4 2/2] btrfs: re-attach folio private when a released folio gets dirtied listout

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox