* [PATCH v3 0/3] btrfs: fix dirtying of folios that lost their private [not found] <6ab2d179.e8212013.2d060.0001.GAE@google.com> @ 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) 0 siblings, 4 replies; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ 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; 10+ 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] 10+ messages in thread
end of thread, other threads:[~2026-12-04 6:36 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <6ab2d179.e8212013.2d060.0001.GAE@google.com>
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