FAILED: patch "[PATCH] btrfs: subpage: keep TOWRITE tag until folio is cleaned" failed to apply to 6.16-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.16-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.16.y
git checkout FETCH_HEAD
git cherry-pick -x b1511360c8ac882b0c52caa263620538e8d73220
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2025082101-survive-mannish-1c90@gregkh' --subject-prefix 'PATCH 6.16.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From b1511360c8ac882b0c52caa263620538e8d73220 Mon Sep 17 00:00:00 2001
From: Naohiro Aota <[email protected]>
Date: Thu, 31 Jul 2025 12:46:56 +0900
Subject: [PATCH] btrfs: subpage: keep TOWRITE tag until folio is cleaned
btrfs_subpage_set_writeback() calls folio_start_writeback() the first time a folio is written back, and it also clears the PAGECACHE_TAG_TOWRITE tag even if there are still dirty blocks in the folio. This can break ordering guarantees, such as those required by btrfs_wait_ordered_extents().
That ordering breakage leads to a real failure. For example, running generic/464 on a zoned setup will hit the following ASSERT. This happens because the broken ordering fails to flush existing dirty pages before the file size is truncated.
assertion failed: !list_empty(&ordered->list) :: 0, in fs/btrfs/zoned.c:1899
------------[ cut here ]------------ kernel BUG at fs/btrfs/zoned.c:1899! Oops: invalid opcode: 0000 [#1] SMP NOPTI CPU: 2 UID: 0 PID: 1906169 Comm: kworker/u130:2 Kdump: loaded Not tainted 6.16.0-rc6-BTRFS-ZNS+ #554 PREEMPT(voluntary) Hardware name: Supermicro Super Server/H12SSL-NT, BIOS 2.0 02/22/2021 Workqueue: btrfs-endio-write btrfs_work_helper [btrfs] RIP: 0010:btrfs_finish_ordered_zoned.cold+0x50/0x52 [btrfs] RSP: 0018:ffffc9002efdbd60 EFLAGS: 00010246 RAX: 000000000000004c RBX: ffff88811923c4e0 RCX: 0000000000000000 RDX: 0000000000000000 RSI: ffffffff827e38b1 RDI: 00000000ffffffff RBP: ffff88810005d000 R08: 00000000ffffdfff R09: ffffffff831051c8 R10: ffffffff83055220 R11: 0000000000000000 R12: ffff8881c2458c00 R13: ffff88811923c540 R14: ffff88811923c5e8 R15: ffff8881c1bd9680 FS: 0000000000000000(0000) GS:ffff88a04acd0000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f907c7a918c CR3: 0000000004024000 CR4: 0000000000350ef0 Call Trace: <TASK> ? srso_return_thunk+0x5/0x5f btrfs_finish_ordered_io+0x4a/0x60 [btrfs] btrfs_work_helper+0xf9/0x490 [btrfs] process_one_work+0x204/0x590 ? srso_return_thunk+0x5/0x5f worker_thread+0x1d6/0x3d0 ? __pfx_worker_thread+0x10/0x10 kthread+0x118/0x230 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x205/0x260 ? __pfx_kthread+0x10/0x10 ret_from_fork_asm+0x1a/0x30 </TASK>
Consider process A calling writepages() with WB_SYNC_NONE. In zoned mode or for compressed writes, it locks several folios for delalloc and starts writing them out. Let's call the last locked folio folio X. Suppose the write range only partially covers folio X, leaving some pages dirty. Process A calls btrfs_subpage_set_writeback() when building a bio. This function call clears the TOWRITE tag of folio X, whose size = 8K and the block size = 4K. It is following state.
0 4K 8K |/////|/////| (flag: DIRTY, tag: DIRTY)
<-----> Process A will write this range.
Now suppose process B concurrently calls writepages() with WB_SYNC_ALL. It calls tag_pages_for_writeback() to tag dirty folios with PAGECACHE_TAG_TOWRITE. Since folio X is still dirty, it gets tagged. Then, B collects tagged folios using filemap_get_folios_tag() and must wait for folio X to be written before returning from writepages().
0 4K 8K |/////|/////| (flag: DIRTY, tag: DIRTY|TOWRITE)
However, between tagging and collecting, process A may call btrfs_subpage_set_writeback() and clear folio X's TOWRITE tag.
0 4K 8K | |/////| (flag: DIRTY|WRITEBACK, tag: DIRTY)
As a result, process B won't see folio X in its batch, and returns without waiting for it. This breaks the WB_SYNC_ALL ordering requirement.
Fix this by using btrfs_subpage_set_writeback_keepwrite(), which retains the TOWRITE tag. We now manually clear the tag only after the folio becomes clean, via the xas operation.
fs/btrfs/subpage.c +18 -1
@@ -448,8 +448,25 @@ void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,spin_lock_irqsave (&bfs->lock, flags);bitmap_set (bfs->bitmaps, start_bit, len >> fs_info->sectorsize_bits); + +/* +* Don't clear the TOWRITE tag when starting writeback on a still-dirty +* folio. Doing so can cause WB_SYNC_ALL writepages() to overlook it, +* assume writeback is complete, and exit too early — violating sync +* ordering guarantees. +*/ if (!folio_test_writeback (folio)) -folio_start_writeback (folio); +__folio_start_writeback (folio, true); +if (!folio_test_dirty (folio)) { +struct address_space *mapping =folio_mapping (folio); +XA_STATE (xas, &mapping->i_pages, folio->index); +unsigned long flags; + +xas_lock_irqsave (&xas, flags); +xas_load (&xas); +xas_clear_mark (&xas,PAGECACHE_TAG_TOWRITE ); +xas_unlock_irqrestore (&xas, flags); + }spin_unlock_irqrestore (&bfs->lock, flags); }
Sasha Levin
maintainerPATCH 1/3 btrfs: add comments on the extra btrfs specific subpage bitmaps
From: Qu Wenruo <[email protected]>
[ Upstream commit 1e17738d6b76cdc76d240d64de87fa66ba2365f7 ]
Unlike the iomap_folio_state structure, the btrfs_subpage structure has a lot of extra sub-bitmaps, namely:
- writeback sub-bitmap
- locked sub-bitmap
iomap_folio_state uses an atomic for writeback tracking, while it has no per-block locked tracking.
This is because iomap always locks a single folio, and submits dirty blocks with that folio locked.
But btrfs has async delalloc ranges (for compression), which are queued with their range locked, until the compression is done, then marks the involved range writeback and unlocked.
This means a range can be unlocked and marked writeback at seemingly random timing, thus it needs the extra tracking.
This needs a huge rework on the lifespan of async delalloc range before we can remove/simplify these two sub-bitmaps.
- ordered sub-bitmap
- checked sub-bitmap
These are for COW-fixup, but as I mentioned in the past, the COW-fixup is not really needed anymore and these two flags are already marked deprecated, and will be removed in the near future after comprehensive tests.
Add related comments to indicate we're actively trying to align the sub-bitmaps to the iomap ones.
Stable-dep-of: b1511360c8ac ("btrfs: subpage: keep TOWRITE tag until folio is cleaned")
--- fs/btrfs/subpage.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/btrfs/subpage.h +14 -0
@@ -33,8 +33,22 @@ enum { btrfs_bitmap_nr_uptodate =0 , btrfs_bitmap_nr_dirty, btrfs_bitmap_nr_writeback, +/* +* The ordered and checked flags are for COW fixup, already marked +* deprecated, and will be removed eventually. +*/ btrfs_bitmap_nr_ordered, btrfs_bitmap_nr_checked, + +/* +* The locked bit is for async delalloc range (compression), currently +* async extent is queued with the range locked, until the compression +* is done. +* So an async extent can unlock the range at any random timing. +* +* This will need a rework on the async extent lifespan (mark writeback +* and do compression) before deprecating this flag. +*/ btrfs_bitmap_nr_locked, btrfs_bitmap_nr_max };
Signature · 1 line
2.50.1
Sasha Levin
maintainerPATCH 2/3 btrfs: rename btrfs_subpage structure
From: Qu Wenruo <[email protected]>
[ Upstream commit 582cd4bad4332cca95c578e99442eb148366eb82 ]
With the incoming large data folios support, the structure name btrfs_subpage is no longer correct, as for we can have multiple blocks inside a large folio, and the block size is still page size.
So to follow the schema of iomap, rename btrfs_subpage to btrfs_folio_state, along with involved enums.
There are still exported functions with "btrfs_subpage_" prefix, and I believe for metadata the name "subpage" will stay forever as we will never allocate a folio larger than nodesize anyway.
The full cleanup of the word "subpage" will happen in much smaller steps in the future.
Stable-dep-of: b1511360c8ac ("btrfs: subpage: keep TOWRITE tag until folio is cleaned")
--- fs/btrfs/extent_io.c | 28 ++--- fs/btrfs/inode.c | 8 +- fs/btrfs/subpage.c | 239 +++++++++++++++++++++---------------------- fs/btrfs/subpage.h | 31 ++++-- 4 files changed, 156 insertions(+), 150 deletions(-)
fs/btrfs/extent_io.c +14 -14
@@ -782,7 +782,7 @@ static void submit_extent_folio(struct btrfs_bio_ctrl *bio_ctrl,static int attach_extent_buffer_folio (struct extent_buffer *eb,struct folio *folio, -struct btrfs_subpage *prealloc) +struct btrfs_folio_state *prealloc) {struct btrfs_fs_info *fs_info = eb->fs_info;int ret =0 ;
@@ -806,7 +806,7 @@ static int attach_extent_buffer_folio(struct extent_buffer *eb,/* Already mapped, just free prealloc */ if (folio_test_private (folio)) { -btrfs_free_subpage (prealloc); +btrfs_free_folio_state (prealloc);return 0 ; }
@@ -815,7 +815,7 @@ 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_subpage (fs_info, folio,BTRFS_SUBPAGE_METADATA ); + ret =btrfs_attach_folio_state (fs_info, folio,BTRFS_SUBPAGE_METADATA );return ret; }
@@ -831,7 +831,7 @@ 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_subpage (fs_info, folio,BTRFS_SUBPAGE_DATA ); +return btrfs_attach_folio_state (fs_info, folio,BTRFS_SUBPAGE_DATA );folio_attach_private (folio, (void *)EXTENT_FOLIO_PRIVATE );return 0 ;
@@ -848,7 +848,7 @@ void clear_folio_extent_mapped(struct folio *folio) fs_info =folio_to_fs_info (folio);if (btrfs_is_subpage (fs_info, folio)) -return btrfs_detach_subpage (fs_info, folio,BTRFS_SUBPAGE_DATA ); +return btrfs_detach_folio_state (fs_info, folio,BTRFS_SUBPAGE_DATA );folio_detach_private (folio); }
@@ -2731,13 +2731,13 @@ static int extent_buffer_under_io(const struct extent_buffer *eb)static bool folio_range_has_eb (struct folio *folio) { -struct btrfs_subpage *subpage; +struct btrfs_folio_state *bfs;lockdep_assert_held (&folio->mapping->i_private_lock);if (folio_test_private (folio)) { - subpage =folio_get_private (folio); -if (atomic_read (&subpage->eb_refs)) + bfs =folio_get_private (folio); +if (atomic_read (&bfs->eb_refs))return true; }return false;
@@ -2787,7 +2787,7 @@ static void detach_extent_buffer_folio(const struct extent_buffer *eb, struct fo* attached to one dummy eb, no sharing. */ if (!mapped) { -btrfs_detach_subpage (fs_info, folio,BTRFS_SUBPAGE_METADATA ); +btrfs_detach_folio_state (fs_info, folio,BTRFS_SUBPAGE_METADATA );return ; }
@@ -2798,7 +2798,7 @@ static void detach_extent_buffer_folio(const struct extent_buffer *eb, struct fo* page range and no unfinished IO. */ if (!folio_range_has_eb (folio)) -btrfs_detach_subpage (fs_info, folio,BTRFS_SUBPAGE_METADATA ); +btrfs_detach_folio_state (fs_info, folio,BTRFS_SUBPAGE_METADATA );spin_unlock (&mapping->i_private_lock); }
@@ -3141,7 +3141,7 @@ static bool check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start)* The caller needs to free the existing folios and retry using the same order. */ static int attach_eb_folio_to_filemap (struct extent_buffer *eb,int i, -struct btrfs_subpage *prealloc, +struct btrfs_folio_state *prealloc,struct extent_buffer **found_eb_ret) {
@@ -3224,7 +3224,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,int attached =0 ;struct extent_buffer *eb;struct extent_buffer *existing_eb =NULL ; -struct btrfs_subpage *prealloc =NULL ; +struct btrfs_folio_state *prealloc =NULL ;u64 lockdep_owner = owner_root;bool page_contig = true;int uptodate =1 ;
@@ -3269,7 +3269,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,* manually if we exit earlier. */ if (btrfs_meta_is_subpage (fs_info)) { - prealloc =btrfs_alloc_subpage (fs_info,PAGE_SIZE ,BTRFS_SUBPAGE_METADATA ); + prealloc =btrfs_alloc_folio_state (fs_info,PAGE_SIZE ,BTRFS_SUBPAGE_METADATA );if (IS_ERR (prealloc)) { ret =PTR_ERR (prealloc); goto out;
@@ -3280,7 +3280,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,/* Allocate all pages first. */ ret =alloc_eb_folio_array (eb, true);if (ret <0 ) { -btrfs_free_subpage (prealloc); +btrfs_free_folio_state (prealloc); goto out; }
fs/btrfs/inode.c +4 -4
@@ -7364,13 +7364,13 @@ struct extent_map *btrfs_create_io_em(struct btrfs_inode *inode, u64 start,static void wait_subpage_spinlock (struct folio *folio) {struct btrfs_fs_info *fs_info =folio_to_fs_info (folio); -struct btrfs_subpage *subpage; +struct btrfs_folio_state *bfs;if (!btrfs_is_subpage (fs_info, folio))return ;ASSERT (folio_test_private (folio) &&folio_get_private (folio)); - subpage =folio_get_private (folio); + bfs =folio_get_private (folio);/* * This may look insane as we just acquire the spinlock and release it,
@@ -7383,8 +7383,8 @@ static void wait_subpage_spinlock(struct folio *folio)* Here we just acquire the spinlock so that all existing callers * should exit and we're safe to release/invalidate the page. */ -spin_lock_irq (&subpage->lock); -spin_unlock_irq (&subpage->lock); +spin_lock_irq (&bfs->lock); +spin_unlock_irq (&bfs->lock); }static int btrfs_launder_folio (struct folio *folio)
fs/btrfs/subpage.c +117 -122
566 lines not on this page · raw message
fs/btrfs/subpage.h +21 -10
@@ -32,7 +32,15 @@ struct folio;enum { btrfs_bitmap_nr_uptodate =0 , btrfs_bitmap_nr_dirty, + +/* +* This can be changed to atomic eventually. But this change will rely +* on the async delalloc range rework for locked bitmap. As async +* delalloc can unlock its range and mark blocks writeback at random +* timing. +*/ btrfs_bitmap_nr_writeback, +/* * The ordered and checked flags are for COW fixup, already marked * deprecated, and will be removed eventually.
@@ -57,7 +65,7 @@ enum {* Structure to trace status of each sector inside a page, attached to * page::private for both data and metadata inodes. */ -struct btrfs_subpage { +struct btrfs_folio_state {/* Common members for both data and metadata pages */ spinlock_t lock;union {
@@ -65,7 +73,7 @@ struct btrfs_subpage {* Structures only used by metadata * * @eb_refs should only be operated under private_lock, as it -* manages whether the subpage can be detached. +* manages whether the btrfs_folio_state can be detached. */ atomic_t eb_refs;
@@ -79,7 +87,7 @@ struct btrfs_subpage {unsigned long bitmaps[]; }; -enum btrfs_subpage_type { +enum btrfs_folio_type {BTRFS_SUBPAGE_METADATA ,BTRFS_SUBPAGE_DATA , };
@@ -119,15 +127,18 @@ static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, }#endif -int btrfs_attach_subpage (const struct btrfs_fs_info *fs_info, -struct folio *folio,enum btrfs_subpage_type type); -void btrfs_detach_subpage (const struct btrfs_fs_info *fs_info,struct folio *folio, -enum btrfs_subpage_type type); +int btrfs_attach_folio_state (const struct btrfs_fs_info *fs_info, +struct folio *folio,enum btrfs_folio_type type); +void btrfs_detach_folio_state (const struct btrfs_fs_info *fs_info,struct folio *folio, +enum btrfs_folio_type type);/* Allocate additional data where page represents more than one sector */ -struct btrfs_subpage *btrfs_alloc_subpage (const struct btrfs_fs_info *fs_info, -size_t fsize,enum btrfs_subpage_type type); -void btrfs_free_subpage (struct btrfs_subpage *subpage); +struct btrfs_folio_state *btrfs_alloc_folio_state (const struct btrfs_fs_info *fs_info, +size_t fsize,enum btrfs_folio_type type); +static inline void btrfs_free_folio_state (struct btrfs_folio_state *bfs) +{ +kfree (bfs); +}void btrfs_folio_inc_eb_refs (const struct btrfs_fs_info *fs_info,struct folio *folio);void btrfs_folio_dec_eb_refs (const struct btrfs_fs_info *fs_info,struct folio *folio);
Signature · 1 line
2.50.1
Sasha Levin
maintainerPATCH 3/3 btrfs: subpage: keep TOWRITE tag until folio is cleaned
In reply to Sasha LevinFrom: Naohiro Aota <[email protected]>
[ Upstream commit b1511360c8ac882b0c52caa263620538e8d73220 ]
btrfs_subpage_set_writeback() calls folio_start_writeback() the first time a folio is written back, and it also clears the PAGECACHE_TAG_TOWRITE tag even if there are still dirty blocks in the folio. This can break ordering guarantees, such as those required by btrfs_wait_ordered_extents().
That ordering breakage leads to a real failure. For example, running generic/464 on a zoned setup will hit the following ASSERT. This happens because the broken ordering fails to flush existing dirty pages before the file size is truncated.
assertion failed: !list_empty(&ordered->list) :: 0, in fs/btrfs/zoned.c:1899
------------[ cut here ]------------ kernel BUG at fs/btrfs/zoned.c:1899! Oops: invalid opcode: 0000 [#1] SMP NOPTI CPU: 2 UID: 0 PID: 1906169 Comm: kworker/u130:2 Kdump: loaded Not tainted 6.16.0-rc6-BTRFS-ZNS+ #554 PREEMPT(voluntary) Hardware name: Supermicro Super Server/H12SSL-NT, BIOS 2.0 02/22/2021 Workqueue: btrfs-endio-write btrfs_work_helper [btrfs] RIP: 0010:btrfs_finish_ordered_zoned.cold+0x50/0x52 [btrfs] RSP: 0018:ffffc9002efdbd60 EFLAGS: 00010246 RAX: 000000000000004c RBX: ffff88811923c4e0 RCX: 0000000000000000 RDX: 0000000000000000 RSI: ffffffff827e38b1 RDI: 00000000ffffffff RBP: ffff88810005d000 R08: 00000000ffffdfff R09: ffffffff831051c8 R10: ffffffff83055220 R11: 0000000000000000 R12: ffff8881c2458c00 R13: ffff88811923c540 R14: ffff88811923c5e8 R15: ffff8881c1bd9680 FS: 0000000000000000(0000) GS:ffff88a04acd0000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f907c7a918c CR3: 0000000004024000 CR4: 0000000000350ef0 Call Trace: <TASK> ? srso_return_thunk+0x5/0x5f btrfs_finish_ordered_io+0x4a/0x60 [btrfs] btrfs_work_helper+0xf9/0x490 [btrfs] process_one_work+0x204/0x590 ? srso_return_thunk+0x5/0x5f worker_thread+0x1d6/0x3d0 ? __pfx_worker_thread+0x10/0x10 kthread+0x118/0x230 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x205/0x260 ? __pfx_kthread+0x10/0x10 ret_from_fork_asm+0x1a/0x30 </TASK>
Consider process A calling writepages() with WB_SYNC_NONE. In zoned mode or for compressed writes, it locks several folios for delalloc and starts writing them out. Let's call the last locked folio folio X. Suppose the write range only partially covers folio X, leaving some pages dirty. Process A calls btrfs_subpage_set_writeback() when building a bio. This function call clears the TOWRITE tag of folio X, whose size = 8K and the block size = 4K. It is following state.
0 4K 8K |/////|/////| (flag: DIRTY, tag: DIRTY)
<-----> Process A will write this range.
Now suppose process B concurrently calls writepages() with WB_SYNC_ALL. It calls tag_pages_for_writeback() to tag dirty folios with PAGECACHE_TAG_TOWRITE. Since folio X is still dirty, it gets tagged. Then, B collects tagged folios using filemap_get_folios_tag() and must wait for folio X to be written before returning from writepages().
0 4K 8K |/////|/////| (flag: DIRTY, tag: DIRTY|TOWRITE)
However, between tagging and collecting, process A may call btrfs_subpage_set_writeback() and clear folio X's TOWRITE tag.
0 4K 8K | |/////| (flag: DIRTY|WRITEBACK, tag: DIRTY)
As a result, process B won't see folio X in its batch, and returns without waiting for it. This breaks the WB_SYNC_ALL ordering requirement.
Fix this by using btrfs_subpage_set_writeback_keepwrite(), which retains the TOWRITE tag. We now manually clear the tag only after the folio becomes clean, via the xas operation.
--- fs/btrfs/subpage.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
fs/btrfs/subpage.c +18 -1
@@ -448,8 +448,25 @@ void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,spin_lock_irqsave (&bfs->lock, flags);bitmap_set (bfs->bitmaps, start_bit, len >> fs_info->sectorsize_bits); + +/* +* Don't clear the TOWRITE tag when starting writeback on a still-dirty +* folio. Doing so can cause WB_SYNC_ALL writepages() to overlook it, +* assume writeback is complete, and exit too early — violating sync +* ordering guarantees. +*/ if (!folio_test_writeback (folio)) -folio_start_writeback (folio); +__folio_start_writeback (folio, true); +if (!folio_test_dirty (folio)) { +struct address_space *mapping =folio_mapping (folio); +XA_STATE (xas, &mapping->i_pages, folio->index); +unsigned long flags; + +xas_lock_irqsave (&xas, flags); +xas_load (&xas); +xas_clear_mark (&xas,PAGECACHE_TAG_TOWRITE ); +xas_unlock_irqrestore (&xas, flags); + }spin_unlock_irqrestore (&bfs->lock, flags); }
Signature · 1 line
2.50.1