[PATCH] btrfs: don't invalidate beyond-EOF folios without private in extent_writepage()
From: Brahmajit Das
Date: Mon Nov 30 2026 - 13:27:25 EST
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@xxxxxxxxxxxxxxxxxxxxxxxxx
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@xxxxxxx>
---
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