* [PATCH v4 0/3] ext4: fix buffered writeback error handling
@ 2026-11-30 7:31 Julian Sun
2026-11-30 7:31 ` [PATCH v4 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Julian Sun @ 2026-11-30 7:31 UTC (permalink / raw)
To: linux-ext4
Cc: yi.zhang, jack, tytso, adilger.kernel, libaokun, ojaswin, riteshh
This series fixes error handling and races in ext4 buffer_head writeback.
When the block size is smaller than the folio size, a fatal mapping error
can discard buffers whose mapping has already succeeded, potentially
exposing stale data. On retryable errors, those buffers are submitted,
but cleanup can race with truncation after the partial folio is unlocked
and trigger a BUG_ON by accessing a replacement folio.
Submit the mapped buffers on fatal errors as well, retain the remaining
dirty buffers for later writeback, and record the partial folio's end
before unlocking it so cleanup skips its entire range.
Concurrent updates from writeback and failed-bio completion can also lose
io_end flags. Use atomic bit operations so neither update overwrites the
other.
Sashiko helped identify all three issues addressed by this series.
Changes in v4:
1. Rename release_start to partial_folio_end and clarify the cleanup
comment, following Zhang Yi's suggestion.
Changes in v3:
1. Simplify the implementation following Zhang Yi's suggestion.
2. Fix the race between partial folio submission and truncation.
Julian Sun (3):
ext4: fix a race between partial writeback and truncation
ext4: submit mapped buffers after fatal mapping errors
ext4: use atomic bitops for io_end flags
fs/ext4/inode.c | 89 +++++++++++++++++++++++++++++++++++------------------
fs/ext4/ext4.h | 16 ++++------
fs/ext4/page-io.c | 20 +++++++-----
3 files changed, 76 insertions(+), 49 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.39.5
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 1/3] ext4: fix a race between partial writeback and truncation
2026-11-30 7:31 [PATCH v4 0/3] ext4: fix buffered writeback error handling Julian Sun
@ 2026-11-30 7:31 ` Julian Sun
2026-11-30 9:11 ` Zhang Yi
2026-11-30 7:31 ` [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Julian Sun @ 2026-11-30 7:31 UTC (permalink / raw)
To: linux-ext4
Cc: yi.zhang, jack, tytso, adilger.kernel, libaokun, ojaswin, riteshh
After a retryable mapping error, mpage_submit_partial_folio() submits the
mapped buffers, advances start_pos into the folio and unlocks it.
mpage_release_unused_pages() subsequently looks up the remaining range
and skips the partial folio by comparing its position with start_pos.
Once unlocked, the folio can be truncated and replaced by smaller folios.
A replacement at or beyond start_pos passes the position check even
though writeback does not own its lock. Cleanup can then hit the locked
folio assertion or unlock a folio held by another task.
The race was reproduced reliably using local code instrumentation to
widen the race window, triggering BUG_ON(!folio_test_locked(folio)) in
mpage_release_unused_pages().
The issue could no longer be reproduced after applying this patch.
On this retryable error path, cleanup only needs to unlock unused folios.
The partial folio has already been unlocked. Its buffers still needing
mapping remain dirty, and ext4_bio_write_folio() redirties the folio for a
later writeback attempt. The existing code therefore intends to leave
it alone. Any new folios inserted into the same range were not locked by
this writeback operation and must also be excluded. The following unused
folios still need their locks released.
Record the end of the partially submitted folio before unlocking it,
and start cleanup at that offset.
Fixes: ded2d726a304 ("ext4: fix stale data if it bail out of the extents mapping loop")
Cc: stable@vger.kernel.org
Signed-off-by: Julian Sun <sunjunchao@bytedance.com>
---
fs/ext4/inode.c | 50 +++++++++++++++++++++++++++++++++++++-------------
1 file changed, 37 insertions(+), 13 deletions(-)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index e3129353d4ed..407a60ac6b85 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -1824,23 +1824,34 @@
unsigned int journalled_more_data:1;
};
static void mpage_release_unused_pages(struct mpage_da_data *mpd,
- bool invalidate)
+ bool invalidate,
+ loff_t partial_folio_end)
{
unsigned nr, i;
pgoff_t index, end;
struct folio_batch fbatch;
struct inode *inode = mpd->inode;
struct address_space *mapping = inode->i_mapping;
+ /*
+ * Cleanup can race with truncation after mpage_submit_partial_folio()
+ * unlocks the folio. Skip its entire range to avoid touching replacement
+ * folios whose locks we do not own.
+ */
+ loff_t release_start = max(mpd->start_pos, partial_folio_end);
/* This is necessary when next_pos == 0. */
if (mpd->start_pos >= mpd->next_pos)
return;
+ /* A partially submitted folio may need another writeback pass. */
mpd->scanned_until_end = 0;
+ if (release_start >= mpd->next_pos)
+ return;
+
if (invalidate) {
ext4_lblk_t start, last;
- start = EXT4_B_TO_LBLK(inode, mpd->start_pos);
+ start = EXT4_B_TO_LBLK(inode, release_start);
last = mpd->next_pos >> inode->i_blkbits;
/*
@@ -1853,7 +1864,7 @@
}
folio_batch_init(&fbatch);
- index = mpd->start_pos >> PAGE_SHIFT;
+ index = release_start >> PAGE_SHIFT;
end = mpd->next_pos >> PAGE_SHIFT;
while (index < end) {
nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
@@ -1862,7 +1873,7 @@
for (i = 0; i < nr; i++) {
struct folio *folio = fbatch.folios[i];
- if (folio_pos(folio) < mpd->start_pos)
+ if (folio_pos(folio) < release_start)
continue;
if (folio_next_index(folio) > end)
continue;
@@ -2523,7 +2534,8 @@
* This is used to submit mapped buffers in a single folio that is not fully
* mapped for various reasons, such as insufficient space or journal credits.
*/
-static int mpage_submit_partial_folio(struct mpage_da_data *mpd)
+static int mpage_submit_partial_folio(struct mpage_da_data *mpd,
+ loff_t *partial_folio_end)
{
struct inode *inode = mpd->inode;
struct folio *folio;
@@ -2544,13 +2556,17 @@
mpage_submit_folio(mpd, folio);
/*
- * Update start_pos to prevent this folio from being released in
- * mpage_release_unused_pages(), it will be reset to the aligned folio
- * pos when this folio is written again in the next round. Additionally,
+ * Update start_pos to the mapped boundary. It will be reset to the aligned
+ * folio pos when this folio is written again in the next round. Additionally,
* do not update wbc->nr_to_write here, as it will be updated once the
* entire folio has finished processing.
+ *
+ * Once unlocked, the folio can be removed from the page cache and
+ * replaced. Record partial_folio_end before unlocking it so that
+ * mpage_release_unused_pages() skips its entire range.
*/
mpd->start_pos = pos;
+ *partial_folio_end = folio_next_pos(folio);
folio_unlock(folio);
folio_put(folio);
return 0;
@@ -2565,6 +2581,9 @@
* @give_up_on_write - we set this to true iff there is a fatal error and there
* is no hope of writing the data. The caller should discard
* dirty pages to avoid infinite loops.
+ * @partial_folio_end - byte offset just past the submitted partial folio.
+ * The caller must initialize it to zero; it remains zero
+ * if no partial folio is submitted.
*
* The function maps extent starting at mpd->lblk of length mpd->len. If it is
* delayed, blocks are allocated, if it is unwritten, we may need to convert
@@ -2578,7 +2597,8 @@
*/
static int mpage_map_and_submit_extent(handle_t *handle,
struct mpage_da_data *mpd,
- bool *give_up_on_write)
+ bool *give_up_on_write,
+ loff_t *partial_folio_end)
{
struct inode *inode = mpd->inode;
struct ext4_map_blocks *map = &mpd->map;
@@ -2612,7 +2632,8 @@
* corresponding data to prevent stale data.
*/
if (progress) {
- if (mpage_submit_partial_folio(mpd))
+ if (mpage_submit_partial_folio(mpd,
+ partial_folio_end))
goto invalidate_dirty_pages;
goto update_disksize;
}
@@ -3017,7 +3038,7 @@
}
ret = mpage_prepare_extent_to_map(mpd);
/* Unlock pages we didn't use */
- mpage_release_unused_pages(mpd, false);
+ mpage_release_unused_pages(mpd, false, 0);
/* Submit prepared bio */
ext4_io_submit(&mpd->io_submit);
ext4_put_io_end_defer(mpd->io_submit.io_end);
@@ -3026,6 +3047,8 @@
goto unplug;
while (!mpd->scanned_until_end && wbc->nr_to_write > 0) {
+ loff_t partial_folio_end = 0;
+
/* For each extent of pages we use new io_end */
mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
if (!mpd->io_submit.io_end) {
@@ -3070,7 +3093,7 @@
ret = mpage_prepare_extent_to_map(mpd);
if (!ret && mpd->map.m_len)
ret = mpage_map_and_submit_extent(handle, mpd,
- &give_up_on_write);
+ &give_up_on_write, &partial_folio_end);
/*
* Caution: If the handle is synchronous,
* ext4_journal_stop() can wait for transaction commit
@@ -3087,7 +3110,8 @@
mpd->do_map = 0;
}
/* Unlock pages we didn't use */
- mpage_release_unused_pages(mpd, give_up_on_write);
+ mpage_release_unused_pages(mpd, give_up_on_write,
+ partial_folio_end);
/* Submit prepared bio */
ext4_io_submit(&mpd->io_submit);
--
2.39.5
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors
2026-11-30 7:31 [PATCH v4 0/3] ext4: fix buffered writeback error handling Julian Sun
2026-11-30 7:31 ` [PATCH v4 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
@ 2026-11-30 7:31 ` Julian Sun
2026-11-30 8:00 ` sashiko-bot
2026-11-30 9:13 ` Zhang Yi
2026-11-30 7:31 ` [PATCH v4 3/3] ext4: use atomic bitops for io_end flags Julian Sun
2026-11-30 8:00 ` [PATCH v4 0/3] ext4: fix buffered writeback error handling sashiko-bot
3 siblings, 2 replies; 8+ messages in thread
From: Julian Sun @ 2026-11-30 7:31 UTC (permalink / raw)
To: linux-ext4
Cc: yi.zhang, jack, tytso, adilger.kernel, libaokun, ojaswin, riteshh
When the block size is smaller than the folio size, mapping an earlier
extent in a folio can succeed before mapping a later extent fails.
mpage_map_and_submit_extent() submits the mapped buffers after retryable
mapping errors, but discards them along with the rest of the folio after
fatal errors. Mapped buffers are also left unsubmitted if allocating an
io_end_vec for a later extent fails.
Besides losing dirty data that could still be submitted, this can expose
stale data with dioread_nolock: the mapped ranges remain in io_end's
unwritten conversion list and can be converted without their data having
been submitted.
A failure to map a later extent does not invalidate the mappings already
established for earlier buffers. Submit those buffers after a fatal
mapping error when the filesystem is not in an emergency state. Also
submit them if allocating an io_end_vec for a later extent fails.
Buffers still needing mapping remain dirty for a later writeback attempt.
Preserve their extent status entries and delayed allocation reservations,
and invalidate only the unused folios after the partially submitted folio
on fatal errors. If a later attempt encounters a fatal mapping error
before making progress, the existing invalidation path can discard the
folio.
Suggested-by: Zhang Yi <yi.zhang@huaweicloud.com>
Fixes: c8cc88163f40 ("ext4: Add support for blocksize < pagesize in dioread_nolock")
Cc: stable@vger.kernel.org
Signed-off-by: Julian Sun <sunjunchao@bytedance.com>
---
fs/ext4/inode.c | 43 ++++++++++++++++++++++++-------------------
1 file changed, 24 insertions(+), 19 deletions(-)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 407a60ac6b85..ee51b12e31af 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2607,6 +2607,7 @@
int progress = 0;
ext4_io_end_t *io_end = mpd->io_submit.io_end;
struct ext4_io_end_vec *io_end_vec;
+ bool fatal = false;
io_end_vec = ext4_alloc_io_end_vec(io_end);
if (IS_ERR(io_end_vec))
@@ -2617,8 +2618,10 @@
if (err < 0) {
struct super_block *sb = inode->i_sb;
- if (ext4_emergency_state(sb))
- goto invalidate_dirty_pages;
+ if (ext4_emergency_state(sb)) {
+ *give_up_on_write = true;
+ return err;
+ }
/*
* Let the uper layers retry transient errors.
* In the case of ENOSPC, if ext4_count_free_blocks()
@@ -2626,18 +2629,7 @@
*/
if ((err == -ENOMEM) || (err == -EAGAIN) ||
(err == -ENOSPC && ext4_count_free_clusters(sb))) {
- /*
- * We may have already allocated extents for
- * some bhs inside the folio, issue the
- * corresponding data to prevent stale data.
- */
- if (progress) {
- if (mpage_submit_partial_folio(mpd,
- partial_folio_end))
- goto invalidate_dirty_pages;
- goto update_disksize;
- }
- return err;
+ break;
}
ext4_msg(sb, KERN_CRIT,
"Delayed block allocation failed for "
@@ -2651,9 +2643,8 @@
"be lost\n");
if (err == -ENOSPC)
ext4_print_free_blocks(inode);
- invalidate_dirty_pages:
- *give_up_on_write = true;
- return err;
+ fatal = true;
+ break;
}
progress = 1;
/*
@@ -2662,10 +2653,24 @@
*/
err = mpage_map_and_submit_buffers(mpd);
if (err < 0)
- goto update_disksize;
+ break;
} while (map->m_len);
-update_disksize:
+ if (err < 0) {
+ /*
+ * We may have already allocated extents for some bhs inside
+ * the folio, issue the corresponding data to prevent stale
+ * data.
+ */
+ if (progress && mpage_submit_partial_folio(mpd, partial_folio_end))
+ fatal = true;
+
+ if (fatal)
+ *give_up_on_write = true;
+ if (!progress)
+ return err;
+ }
+
/*
* Update on-disk size after IO is submitted. Races with
* truncate are avoided by checking i_size under i_data_sem.
--
2.39.5
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 3/3] ext4: use atomic bitops for io_end flags
2026-11-30 7:31 [PATCH v4 0/3] ext4: fix buffered writeback error handling Julian Sun
2026-11-30 7:31 ` [PATCH v4 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
2026-11-30 7:31 ` [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
@ 2026-11-30 7:31 ` Julian Sun
2026-11-30 8:00 ` [PATCH v4 0/3] ext4: fix buffered writeback error handling sashiko-bot
3 siblings, 0 replies; 8+ messages in thread
From: Julian Sun @ 2026-11-30 7:31 UTC (permalink / raw)
To: linux-ext4
Cc: yi.zhang, jack, tytso, adilger.kernel, libaokun, ojaswin, riteshh
Before EXT4_IO_END_FAILED was added, the writeback thread set
EXT4_IO_END_UNWRITTEN and final completion cleared it after the last
io_end reference was dropped. Adding EXT4_IO_END_FAILED introduced a
concurrent writer in failed-bio completion. Successful bio completions
do not update the flags.
An io_end can cover already-mapped folios submitted before a later
unwritten extent is mapped. An earlier bio can fail while writeback
first sets UNWRITTEN. The non-atomic updates can then lose a bit:
Bio completion Writeback thread
-------------- ----------------
read flag = 0
attach reserved handle to io_end
flag |= UNWRITTEN
write flag = 0 | FAILED
/* UNWRITTEN is lost */
With data_err=ignore, losing UNWRITTEN makes
ext4_io_end_defer_completion() return false. When the io_end reference
count reaches zero, ext4_end_io_end() and its reserved-handle cleanup
are therefore skipped.
ext4_release_io_end() only warns about the remaining handle before
freeing io_end. The leaked reservation keeps j_reserved_credits nonzero,
so jbd2_journal_lock_updates() can wait indefinitely.
The reverse interleaving can lose FAILED and skip a requested journal
abort.
Use atomic bit operations to fix the race.
Fixes: e856f93e0fb2 ("ext4: do not convert the unwritten extents if data writeback fails")
Cc: stable@vger.kernel.org
Reviewed-by: Zhang Yi <yi.zhang@huaweicloud.com>
Signed-off-by: Julian Sun <sunjunchao@bytedance.com>
---
fs/ext4/ext4.h | 16 ++++++----------
fs/ext4/page-io.c | 20 +++++++++++---------
2 files changed, 17 insertions(+), 19 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index bf0325ecbec7..8469c4b11648 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -272,12 +272,10 @@ struct ext4_system_blocks {
};
/*
- * Flags for ext4_io_end->flags
+ * Bit numbers for ext4_io_end->flag
*/
-#define EXT4_IO_END_UNWRITTEN 0x0001
-#define EXT4_IO_END_FAILED 0x0002
-
-#define EXT4_IO_END_DEFER_COMPLETION (EXT4_IO_END_UNWRITTEN | EXT4_IO_END_FAILED)
+#define EXT4_IO_END_UNWRITTEN 0
+#define EXT4_IO_END_FAILED 1
struct ext4_io_end_vec {
struct list_head list; /* list of io_end_vec */
@@ -296,7 +294,7 @@ typedef struct ext4_io_end {
struct inode *inode; /* file being written to */
struct bio *bio; /* Linked list of completed
* bios covering the extent */
- unsigned int flag; /* unwritten or not */
+ unsigned long flag; /* EXT4_IO_END_* bits */
refcount_t count; /* reference counter */
struct list_head list_vec; /* list of ext4_io_end_vec */
} ext4_io_end_t;
@@ -4096,14 +4094,12 @@ extern int ext4_resize_end(struct super_block *sb, bool update_backups);
static inline void ext4_set_io_unwritten_flag(struct ext4_io_end *io_end)
{
- if (!(io_end->flag & EXT4_IO_END_UNWRITTEN))
- io_end->flag |= EXT4_IO_END_UNWRITTEN;
+ set_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag);
}
static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
{
- if (io_end->flag & EXT4_IO_END_UNWRITTEN)
- io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
+ clear_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag);
}
DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next);
diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
index 734d856a6d47..437f28c56b72 100644
--- a/fs/ext4/page-io.c
+++ b/fs/ext4/page-io.c
@@ -146,7 +146,7 @@ static void ext4_release_io_end(ext4_io_end_t *io_end)
struct bio *bio, *next_bio;
BUG_ON(!list_empty(&io_end->list));
- BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
+ BUG_ON(test_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag));
WARN_ON(io_end->handle);
for (bio = io_end->bio; bio; bio = next_bio) {
@@ -183,7 +183,7 @@ static int ext4_end_io_end(ext4_io_end_t *io_end)
* or stale data may be exposed.
*/
io_end->handle = NULL; /* Following call will use up the handle */
- if (unlikely(io_end->flag & EXT4_IO_END_FAILED)) {
+ if (unlikely(test_bit(EXT4_IO_END_FAILED, &io_end->flag))) {
ret = -EIO;
if (handle)
jbd2_journal_free_reserved(handle);
@@ -194,7 +194,7 @@ static int ext4_end_io_end(ext4_io_end_t *io_end)
ret = ext4_convert_unwritten_io_end_vec(handle, io_end);
}
if (ret < 0 && !ext4_emergency_state(sb) &&
- io_end->flag & EXT4_IO_END_UNWRITTEN) {
+ test_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag)) {
ext4_msg(sb, KERN_EMERG,
"failed to convert unwritten extents to written "
"extents -- potential data loss! "
@@ -231,11 +231,11 @@ static void dump_completed_IO(struct inode *inode, struct list_head *head)
static bool ext4_io_end_defer_completion(ext4_io_end_t *io_end)
{
- if (io_end->flag & EXT4_IO_END_UNWRITTEN &&
+ if (test_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag) &&
!list_empty(&io_end->list_vec))
return true;
if (test_opt(io_end->inode->i_sb, DATA_ERR_ABORT) &&
- io_end->flag & EXT4_IO_END_FAILED &&
+ test_bit(EXT4_IO_END_FAILED, &io_end->flag) &&
!ext4_emergency_state(io_end->inode->i_sb))
return true;
return false;
@@ -250,8 +250,9 @@ static void ext4_add_complete_io(ext4_io_end_t *io_end)
unsigned long flags;
/* Only reserved conversions or pending IO errors will enter here. */
- WARN_ON(!(io_end->flag & EXT4_IO_END_DEFER_COMPLETION));
- WARN_ON(io_end->flag & EXT4_IO_END_UNWRITTEN &&
+ WARN_ON(!test_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag) &&
+ !test_bit(EXT4_IO_END_FAILED, &io_end->flag));
+ WARN_ON(test_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag) &&
!io_end->handle && sbi->s_journal);
WARN_ON(!io_end->bio);
@@ -279,7 +280,8 @@ static int ext4_do_flush_completed_IO(struct inode *inode,
while (!list_empty(&unwritten)) {
io_end = list_entry(unwritten.next, ext4_io_end_t, list);
- BUG_ON(!(io_end->flag & EXT4_IO_END_DEFER_COMPLETION));
+ BUG_ON(!test_bit(EXT4_IO_END_UNWRITTEN, &io_end->flag) &&
+ !test_bit(EXT4_IO_END_FAILED, &io_end->flag));
list_del_init(&io_end->list);
err = ext4_end_io_end(io_end);
@@ -365,7 +367,7 @@ static void ext4_end_bio(struct bio *bio)
bio->bi_status, inode->i_ino,
(unsigned long long)
bi_sector >> (inode->i_blkbits - 9));
- io_end->flag |= EXT4_IO_END_FAILED;
+ set_bit(EXT4_IO_END_FAILED, &io_end->flag);
mapping_set_error(inode->i_mapping,
blk_status_to_errno(bio->bi_status));
}
--
2.39.5
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors
2026-11-30 7:31 ` [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
@ 2026-11-30 8:00 ` sashiko-bot
2026-11-30 9:13 ` Zhang Yi
1 sibling, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-30 8:00 UTC (permalink / raw)
To: Julian Sun, linux-ext4; +Cc: tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ext4: writeback loop retries after fatal error with
give_up_on_write stuck true
--
commit a666f18d34d90ff94f75e3f4027ae28b8d8778ad
Author: Julian Sun <sunjunchao@bytedance.com>
ext4: submit mapped buffers after fatal mapping errors
This commit updates mpage_map_and_submit_extent() to submit already mapped
buffers in a folio when a subsequent extent mapping or io_end_vec
allocation fails.
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 407a60ac6b85f..ee51b12e31afe 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -2607,6 +2607,7 @@ static int mpage_map_and_submit_extent(handle_t *handle,
> int progress = 0;
> ext4_io_end_t *io_end = mpd->io_submit.io_end;
> struct ext4_io_end_vec *io_end_vec;
> + bool fatal = false;
>
> io_end_vec = ext4_alloc_io_end_vec(io_end);
> if (IS_ERR(io_end_vec))
> @@ -2617,8 +2618,10 @@ static int mpage_map_and_submit_extent(handle_t *handle,
> if (err < 0) {
> struct super_block *sb = inode->i_sb;
>
> - if (ext4_emergency_state(sb))
> - goto invalidate_dirty_pages;
> + if (ext4_emergency_state(sb)) {
> + *give_up_on_write = true;
> + return err;
> + }
> /*
> * Let the uper layers retry transient errors.
> * In the case of ENOSPC, if ext4_count_free_blocks()
> @@ -2626,18 +2629,7 @@ static int mpage_map_and_submit_extent(handle_t *handle,
> */
> if ((err == -ENOMEM) || (err == -EAGAIN) ||
> (err == -ENOSPC && ext4_count_free_clusters(sb))) {
> - /*
> - * We may have already allocated extents for
> - * some bhs inside the folio, issue the
> - * corresponding data to prevent stale data.
> - */
> - if (progress) {
> - if (mpage_submit_partial_folio(mpd,
> - partial_folio_end))
> - goto invalidate_dirty_pages;
> - goto update_disksize;
> - }
> - return err;
> + break;
> }
> ext4_msg(sb, KERN_CRIT,
> "Delayed block allocation failed for "
> @@ -2651,9 +2643,8 @@ static int mpage_map_and_submit_extent(handle_t *handle,
> "be lost\n");
> if (err == -ENOSPC)
> ext4_print_free_blocks(inode);
> - invalidate_dirty_pages:
> - *give_up_on_write = true;
> - return err;
> + fatal = true;
> + break;
> }
> progress = 1;
> /*
> @@ -2662,10 +2653,24 @@ static int mpage_map_and_submit_extent(handle_t *handle,
> */
> err = mpage_map_and_submit_buffers(mpd);
> if (err < 0)
> - goto update_disksize;
> + break;
> } while (map->m_len);
>
> -update_disksize:
> + if (err < 0) {
> + /*
> + * We may have already allocated extents for some bhs inside
> + * the folio, issue the corresponding data to prevent stale
> + * data.
> + */
> + if (progress && mpage_submit_partial_folio(mpd, partial_folio_end))
> + fatal = true;
> +
> + if (fatal)
> + *give_up_on_write = true;
> + if (!progress)
> + return err;
> + }
[Severity: Medium]
Does ext4_do_writepages() keep looping with give_up_on_write latched to
true when a fatal -ENOSPC occurs after partial progress in a folio?
When mpage_map_one_extent() fails with fatal -ENOSPC (free clusters == 0)
after progress == 1, mpage_map_and_submit_extent() submits the partial
folio, sets *give_up_on_write = true, and returns -ENOSPC. In
ext4_do_writepages(), mpage_release_unused_pages() sets
mpd->scanned_until_end = 0 and skips the partially submitted folio up to
partial_folio_end, but the loop then treats -ENOSPC as retryable without
checking give_up_on_write:
fs/ext4/inode.c:ext4_do_writepages() {
...
bool give_up_on_write = false;
...
while (!mpd->scanned_until_end && wbc->nr_to_write > 0) {
loff_t partial_folio_end = 0;
...
if (ret == -ENOSPC && sbi->s_journal) {
jbd2_journal_force_commit_nested(sbi->s_journal);
ret = 0;
continue;
}
if (ret == -EAGAIN)
ret = 0;
/* Fatal error - ENOMEM, EIO... */
if (ret)
break;
}
}
Because give_up_on_write is never reset and the loop does not break when
it is set, the next iteration re-enters with partial_folio_end == 0 and
give_up_on_write == true. In WB_SYNC_ALL mode (or once the bio finishes
during jbd2_journal_force_commit_nested()), it immediately re-processes
the partially submitted folio at mpd->start_pos, fails on the unmapped
extent with progress == 0, invalidates the folio that was meant to be
preserved, and clears ret to 0; if the loop instead advances to later
folios, the stale give_up_on_write == true causes
mpage_release_unused_pages() to invalidate valid dirty folios on any
subsequent extent boundary or transient error.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130073139.4183184-1-sunjunchao@bytedance.com?part=2
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 0/3] ext4: fix buffered writeback error handling
2026-11-30 7:31 [PATCH v4 0/3] ext4: fix buffered writeback error handling Julian Sun
` (2 preceding siblings ...)
2026-11-30 7:31 ` [PATCH v4 3/3] ext4: use atomic bitops for io_end flags Julian Sun
@ 2026-11-30 8:00 ` sashiko-bot
3 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-30 8:00 UTC (permalink / raw)
To: Julian Sun, linux-ext4; +Cc: tytso
Sashiko has finished reviewing this 3-patch series:
- Patches 1 and 3: no issues found.
- Patch 2 ("ext4: submit mapped buffers after fatal mapping errors"): 1
potential issue (1 Medium)
A detailed review has been sent as a separate reply to patch 2.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130073139.4183184-1-sunjunchao@bytedance.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 1/3] ext4: fix a race between partial writeback and truncation
2026-11-30 7:31 ` [PATCH v4 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
@ 2026-11-30 9:11 ` Zhang Yi
0 siblings, 0 replies; 8+ messages in thread
From: Zhang Yi @ 2026-11-30 9:11 UTC (permalink / raw)
To: Julian Sun, linux-ext4
Cc: jack, tytso, adilger.kernel, libaokun, ojaswin, riteshh
On 11/30/2026 3:31 PM, Julian Sun wrote:
> After a retryable mapping error, mpage_submit_partial_folio() submits the
> mapped buffers, advances start_pos into the folio and unlocks it.
> mpage_release_unused_pages() subsequently looks up the remaining range
> and skips the partial folio by comparing its position with start_pos.
>
> Once unlocked, the folio can be truncated and replaced by smaller folios.
> A replacement at or beyond start_pos passes the position check even
> though writeback does not own its lock. Cleanup can then hit the locked
> folio assertion or unlock a folio held by another task.
>
> The race was reproduced reliably using local code instrumentation to
> widen the race window, triggering BUG_ON(!folio_test_locked(folio)) in
> mpage_release_unused_pages().
> The issue could no longer be reproduced after applying this patch.
>
> On this retryable error path, cleanup only needs to unlock unused folios.
> The partial folio has already been unlocked. Its buffers still needing
> mapping remain dirty, and ext4_bio_write_folio() redirties the folio for a
> later writeback attempt. The existing code therefore intends to leave
> it alone. Any new folios inserted into the same range were not locked by
> this writeback operation and must also be excluded. The following unused
> folios still need their locks released.
>
> Record the end of the partially submitted folio before unlocking it,
> and start cleanup at that offset.
>
> Fixes: ded2d726a304 ("ext4: fix stale data if it bail out of the extents mapping loop")
> Cc: stable@vger.kernel.org
> Signed-off-by: Julian Sun <sunjunchao@bytedance.com>
Thank you for the update. This looks good to me.
Reviewed-by: Zhang Yi <yi.zhang@huawei.com>
> ---
> fs/ext4/inode.c | 50 +++++++++++++++++++++++++++++++++++++-------------
> 1 file changed, 37 insertions(+), 13 deletions(-)
>
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index e3129353d4ed..407a60ac6b85 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -1824,23 +1824,34 @@
> unsigned int journalled_more_data:1;
> };
>
> static void mpage_release_unused_pages(struct mpage_da_data *mpd,
> - bool invalidate)
> + bool invalidate,
> + loff_t partial_folio_end)
> {
> unsigned nr, i;
> pgoff_t index, end;
> struct folio_batch fbatch;
> struct inode *inode = mpd->inode;
> struct address_space *mapping = inode->i_mapping;
> + /*
> + * Cleanup can race with truncation after mpage_submit_partial_folio()
> + * unlocks the folio. Skip its entire range to avoid touching replacement
> + * folios whose locks we do not own.
> + */
> + loff_t release_start = max(mpd->start_pos, partial_folio_end);
>
> /* This is necessary when next_pos == 0. */
> if (mpd->start_pos >= mpd->next_pos)
> return;
>
> + /* A partially submitted folio may need another writeback pass. */
> mpd->scanned_until_end = 0;
> + if (release_start >= mpd->next_pos)
> + return;
> +
> if (invalidate) {
> ext4_lblk_t start, last;
> - start = EXT4_B_TO_LBLK(inode, mpd->start_pos);
> + start = EXT4_B_TO_LBLK(inode, release_start);
> last = mpd->next_pos >> inode->i_blkbits;
>
> /*
> @@ -1853,7 +1864,7 @@
> }
>
> folio_batch_init(&fbatch);
> - index = mpd->start_pos >> PAGE_SHIFT;
> + index = release_start >> PAGE_SHIFT;
> end = mpd->next_pos >> PAGE_SHIFT;
> while (index < end) {
> nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
> @@ -1862,7 +1873,7 @@
> for (i = 0; i < nr; i++) {
> struct folio *folio = fbatch.folios[i];
>
> - if (folio_pos(folio) < mpd->start_pos)
> + if (folio_pos(folio) < release_start)
> continue;
> if (folio_next_index(folio) > end)
> continue;
> @@ -2523,7 +2534,8 @@
> * This is used to submit mapped buffers in a single folio that is not fully
> * mapped for various reasons, such as insufficient space or journal credits.
> */
> -static int mpage_submit_partial_folio(struct mpage_da_data *mpd)
> +static int mpage_submit_partial_folio(struct mpage_da_data *mpd,
> + loff_t *partial_folio_end)
> {
> struct inode *inode = mpd->inode;
> struct folio *folio;
> @@ -2544,13 +2556,17 @@
>
> mpage_submit_folio(mpd, folio);
> /*
> - * Update start_pos to prevent this folio from being released in
> - * mpage_release_unused_pages(), it will be reset to the aligned folio
> - * pos when this folio is written again in the next round. Additionally,
> + * Update start_pos to the mapped boundary. It will be reset to the aligned
> + * folio pos when this folio is written again in the next round. Additionally,
> * do not update wbc->nr_to_write here, as it will be updated once the
> * entire folio has finished processing.
> + *
> + * Once unlocked, the folio can be removed from the page cache and
> + * replaced. Record partial_folio_end before unlocking it so that
> + * mpage_release_unused_pages() skips its entire range.
> */
> mpd->start_pos = pos;
> + *partial_folio_end = folio_next_pos(folio);
> folio_unlock(folio);
> folio_put(folio);
> return 0;
> @@ -2565,6 +2581,9 @@
> * @give_up_on_write - we set this to true iff there is a fatal error and there
> * is no hope of writing the data. The caller should discard
> * dirty pages to avoid infinite loops.
> + * @partial_folio_end - byte offset just past the submitted partial folio.
> + * The caller must initialize it to zero; it remains zero
> + * if no partial folio is submitted.
> *
> * The function maps extent starting at mpd->lblk of length mpd->len. If it is
> * delayed, blocks are allocated, if it is unwritten, we may need to convert
> @@ -2578,7 +2597,8 @@
> */
> static int mpage_map_and_submit_extent(handle_t *handle,
> struct mpage_da_data *mpd,
> - bool *give_up_on_write)
> + bool *give_up_on_write,
> + loff_t *partial_folio_end)
> {
> struct inode *inode = mpd->inode;
> struct ext4_map_blocks *map = &mpd->map;
> @@ -2612,7 +2632,8 @@
> * corresponding data to prevent stale data.
> */
> if (progress) {
> - if (mpage_submit_partial_folio(mpd))
> + if (mpage_submit_partial_folio(mpd,
> + partial_folio_end))
> goto invalidate_dirty_pages;
> goto update_disksize;
> }
> @@ -3017,7 +3038,7 @@
> }
> ret = mpage_prepare_extent_to_map(mpd);
> /* Unlock pages we didn't use */
> - mpage_release_unused_pages(mpd, false);
> + mpage_release_unused_pages(mpd, false, 0);
> /* Submit prepared bio */
> ext4_io_submit(&mpd->io_submit);
> ext4_put_io_end_defer(mpd->io_submit.io_end);
> @@ -3026,6 +3047,8 @@
> goto unplug;
>
> while (!mpd->scanned_until_end && wbc->nr_to_write > 0) {
> + loff_t partial_folio_end = 0;
> +
> /* For each extent of pages we use new io_end */
> mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
> if (!mpd->io_submit.io_end) {
> @@ -3070,7 +3093,7 @@
> ret = mpage_prepare_extent_to_map(mpd);
> if (!ret && mpd->map.m_len)
> ret = mpage_map_and_submit_extent(handle, mpd,
> - &give_up_on_write);
> + &give_up_on_write, &partial_folio_end);
> /*
> * Caution: If the handle is synchronous,
> * ext4_journal_stop() can wait for transaction commit
> @@ -3087,7 +3110,8 @@
> mpd->do_map = 0;
> }
> /* Unlock pages we didn't use */
> - mpage_release_unused_pages(mpd, give_up_on_write);
> + mpage_release_unused_pages(mpd, give_up_on_write,
> + partial_folio_end);
> /* Submit prepared bio */
> ext4_io_submit(&mpd->io_submit);
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors
2026-11-30 7:31 ` [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
2026-11-30 8:00 ` sashiko-bot
@ 2026-11-30 9:13 ` Zhang Yi
1 sibling, 0 replies; 8+ messages in thread
From: Zhang Yi @ 2026-11-30 9:13 UTC (permalink / raw)
To: Julian Sun, linux-ext4
Cc: jack, tytso, adilger.kernel, libaokun, ojaswin, riteshh
On 11/30/2026 3:31 PM, Julian Sun wrote:
> When the block size is smaller than the folio size, mapping an earlier
> extent in a folio can succeed before mapping a later extent fails.
> mpage_map_and_submit_extent() submits the mapped buffers after retryable
> mapping errors, but discards them along with the rest of the folio after
> fatal errors. Mapped buffers are also left unsubmitted if allocating an
> io_end_vec for a later extent fails.
>
> Besides losing dirty data that could still be submitted, this can expose
> stale data with dioread_nolock: the mapped ranges remain in io_end's
> unwritten conversion list and can be converted without their data having
> been submitted.
>
> A failure to map a later extent does not invalidate the mappings already
> established for earlier buffers. Submit those buffers after a fatal
> mapping error when the filesystem is not in an emergency state. Also
> submit them if allocating an io_end_vec for a later extent fails.
>
> Buffers still needing mapping remain dirty for a later writeback attempt.
> Preserve their extent status entries and delayed allocation reservations,
> and invalidate only the unused folios after the partially submitted folio
> on fatal errors. If a later attempt encounters a fatal mapping error
> before making progress, the existing invalidation path can discard the
> folio.
>
> Suggested-by: Zhang Yi <yi.zhang@huaweicloud.com>
> Fixes: c8cc88163f40 ("ext4: Add support for blocksize < pagesize in dioread_nolock")
> Cc: stable@vger.kernel.org
> Signed-off-by: Julian Sun <sunjunchao@bytedance.com>
Looks good to me.
Reviewed-by: Zhang Yi <yi.zhang@huawei.com>
> ---
> fs/ext4/inode.c | 43 ++++++++++++++++++++++++-------------------
> 1 file changed, 24 insertions(+), 19 deletions(-)
>
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 407a60ac6b85..ee51b12e31af 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -2607,6 +2607,7 @@
> int progress = 0;
> ext4_io_end_t *io_end = mpd->io_submit.io_end;
> struct ext4_io_end_vec *io_end_vec;
> + bool fatal = false;
>
> io_end_vec = ext4_alloc_io_end_vec(io_end);
> if (IS_ERR(io_end_vec))
> @@ -2617,8 +2618,10 @@
> if (err < 0) {
> struct super_block *sb = inode->i_sb;
>
> - if (ext4_emergency_state(sb))
> - goto invalidate_dirty_pages;
> + if (ext4_emergency_state(sb)) {
> + *give_up_on_write = true;
> + return err;
> + }
> /*
> * Let the uper layers retry transient errors.
> * In the case of ENOSPC, if ext4_count_free_blocks()
> @@ -2626,18 +2629,7 @@
> */
> if ((err == -ENOMEM) || (err == -EAGAIN) ||
> (err == -ENOSPC && ext4_count_free_clusters(sb))) {
> - /*
> - * We may have already allocated extents for
> - * some bhs inside the folio, issue the
> - * corresponding data to prevent stale data.
> - */
> - if (progress) {
> - if (mpage_submit_partial_folio(mpd,
> - partial_folio_end))
> - goto invalidate_dirty_pages;
> - goto update_disksize;
> - }
> - return err;
> + break;
> }
> ext4_msg(sb, KERN_CRIT,
> "Delayed block allocation failed for "
> @@ -2651,9 +2643,8 @@
> "be lost\n");
> if (err == -ENOSPC)
> ext4_print_free_blocks(inode);
> - invalidate_dirty_pages:
> - *give_up_on_write = true;
> - return err;
> + fatal = true;
> + break;
> }
> progress = 1;
> /*
> @@ -2662,10 +2653,24 @@
> */
> err = mpage_map_and_submit_buffers(mpd);
> if (err < 0)
> - goto update_disksize;
> + break;
> } while (map->m_len);
>
> -update_disksize:
> + if (err < 0) {
> + /*
> + * We may have already allocated extents for some bhs inside
> + * the folio, issue the corresponding data to prevent stale
> + * data.
> + */
> + if (progress && mpage_submit_partial_folio(mpd, partial_folio_end))
> + fatal = true;
> +
> + if (fatal)
> + *give_up_on_write = true;
> + if (!progress)
> + return err;
> + }
> +
> /*
> * Update on-disk size after IO is submitted. Races with
> * truncate are avoided by checking i_size under i_data_sem.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-30 9:13 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 7:31 [PATCH v4 0/3] ext4: fix buffered writeback error handling Julian Sun
2026-11-30 7:31 ` [PATCH v4 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
2026-11-30 9:11 ` Zhang Yi
2026-11-30 7:31 ` [PATCH v4 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
2026-11-30 8:00 ` sashiko-bot
2026-11-30 9:13 ` Zhang Yi
2026-11-30 7:31 ` [PATCH v4 3/3] ext4: use atomic bitops for io_end flags Julian Sun
2026-11-30 8:00 ` [PATCH v4 0/3] ext4: fix buffered writeback error handling sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox