* [PATCH v3 0/3] ext4: fix buffered writeback error handling
@ 2026-11-16 6:44 Julian Sun
2026-11-16 6:44 ` [PATCH v3 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Julian Sun @ 2026-11-16 6:44 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 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 | 81 +++++++++++++++++++++++++++++++++--------------------
fs/ext4/ext4.h | 16 ++++------
fs/ext4/page-io.c | 20 +++++++------
3 files changed, 68 insertions(+), 49 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.39.5
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v3 1/3] ext4: fix a race between partial writeback and truncation
2026-11-16 6:44 [PATCH v3 0/3] ext4: fix buffered writeback error handling Julian Sun
@ 2026-11-16 6:44 ` Julian Sun
2026-11-16 7:02 ` sashiko-bot
2026-11-16 6:47 ` [PATCH v3 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
2026-11-16 6:47 ` [PATCH v3 3/3] ext4: use atomic bitops for io_end flags Julian Sun
2 siblings, 1 reply; 7+ messages in thread
From: Julian Sun @ 2026-11-16 6:44 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 | 42 +++++++++++++++++++++++++++++-------------
1 file changed, 29 insertions(+), 13 deletions(-)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index e3129353d4ed..6dad9eea7824 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -1825,22 +1825,27 @@
};
static void mpage_release_unused_pages(struct mpage_da_data *mpd,
- bool invalidate)
+ bool invalidate, loff_t release_start)
{
unsigned nr, i;
pgoff_t index, end;
struct folio_batch fbatch;
struct inode *inode = mpd->inode;
struct address_space *mapping = inode->i_mapping;
+ loff_t start_pos = max(mpd->start_pos, release_start);
/* 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 (start_pos >= 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, start_pos);
last = mpd->next_pos >> inode->i_blkbits;
/*
@@ -1853,7 +1858,7 @@
}
folio_batch_init(&fbatch);
- index = mpd->start_pos >> PAGE_SHIFT;
+ index = start_pos >> PAGE_SHIFT;
end = mpd->next_pos >> PAGE_SHIFT;
while (index < end) {
nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
@@ -1862,7 +1867,7 @@
for (i = 0; i < nr; i++) {
struct folio *folio = fbatch.folios[i];
- if (folio_pos(folio) < mpd->start_pos)
+ if (folio_pos(folio) < start_pos)
continue;
if (folio_next_index(folio) > end)
continue;
@@ -2523,7 +2528,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 *release_start)
{
struct inode *inode = mpd->inode;
struct folio *folio;
@@ -2544,13 +2550,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. Set release_start past this folio so that
+ * mpage_release_unused_pages() skips its entire range.
*/
mpd->start_pos = pos;
+ *release_start = folio_next_pos(folio);
folio_unlock(folio);
folio_put(folio);
return 0;
@@ -2565,6 +2575,8 @@
* @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.
+ * @release_start - minimum byte offset for releasing unused folios.
+ * The caller must initialize it to zero.
*
* 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 +2590,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 *release_start)
{
struct inode *inode = mpd->inode;
struct ext4_map_blocks *map = &mpd->map;
@@ -2612,7 +2625,8 @@
* corresponding data to prevent stale data.
*/
if (progress) {
- if (mpage_submit_partial_folio(mpd))
+ if (mpage_submit_partial_folio(mpd,
+ release_start))
goto invalidate_dirty_pages;
goto update_disksize;
}
@@ -3017,7 +3031,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 +3040,8 @@
goto unplug;
while (!mpd->scanned_until_end && wbc->nr_to_write > 0) {
+ loff_t release_start = 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 +3086,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, &release_start);
/*
* Caution: If the handle is synchronous,
* ext4_journal_stop() can wait for transaction commit
@@ -3087,7 +3103,7 @@
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, release_start);
/* Submit prepared bio */
ext4_io_submit(&mpd->io_submit);
--
2.39.5
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v3 2/3] ext4: submit mapped buffers after fatal mapping errors
2026-11-16 6:44 [PATCH v3 0/3] ext4: fix buffered writeback error handling Julian Sun
2026-11-16 6:44 ` [PATCH v3 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
@ 2026-11-16 6:47 ` Julian Sun
2026-11-16 7:24 ` sashiko-bot
2026-11-16 6:47 ` [PATCH v3 3/3] ext4: use atomic bitops for io_end flags Julian Sun
2 siblings, 1 reply; 7+ messages in thread
From: Julian Sun @ 2026-11-16 6:47 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 6dad9eea7824..ff33ae870b35 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2600,6 +2600,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))
@@ -2610,8 +2611,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()
@@ -2619,18 +2622,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,
- release_start))
- goto invalidate_dirty_pages;
- goto update_disksize;
- }
- return err;
+ break;
}
ext4_msg(sb, KERN_CRIT,
"Delayed block allocation failed for "
@@ -2644,9 +2636,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;
/*
@@ -2655,10 +2646,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, release_start))
+ 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] 7+ messages in thread
* [PATCH v3 3/3] ext4: use atomic bitops for io_end flags
2026-11-16 6:44 [PATCH v3 0/3] ext4: fix buffered writeback error handling Julian Sun
2026-11-16 6:44 ` [PATCH v3 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
2026-11-16 6:47 ` [PATCH v3 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
@ 2026-11-16 6:47 ` Julian Sun
2026-11-16 6:59 ` sashiko-bot
2 siblings, 1 reply; 7+ messages in thread
From: Julian Sun @ 2026-11-16 6:47 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] 7+ messages in thread
* Re: [PATCH v3 3/3] ext4: use atomic bitops for io_end flags
2026-11-16 6:47 ` [PATCH v3 3/3] ext4: use atomic bitops for io_end flags Julian Sun
@ 2026-11-16 6:59 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-16 6:59 UTC (permalink / raw)
To: Julian Sun, linux-ext4; +Cc: tytso
> 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
> [ ... ]
>
> 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>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116064404.189911-1-sunjunchao@bytedance.com?part=3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3 1/3] ext4: fix a race between partial writeback and truncation
2026-11-16 6:44 ` [PATCH v3 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
@ 2026-11-16 7:02 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-16 7:02 UTC (permalink / raw)
To: Julian Sun, linux-ext4; +Cc: tytso
> 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>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116064404.189911-1-sunjunchao@bytedance.com?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v3 2/3] ext4: submit mapped buffers after fatal mapping errors
2026-11-16 6:47 ` [PATCH v3 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
@ 2026-11-16 7:24 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-16 7:24 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 continues with give_up_on_write set
after fatal -ENOSPC
--
commit 582dfee51394f04b2556a1a60b05a5f6ce7a4668
Author: Julian Sun <sunjunchao@bytedance.com>
ext4: submit mapped buffers after fatal mapping errors
This commit submits already-mapped buffers in a folio when a subsequent
extent mapping or io_end_vec allocation fails, rather than discarding
the whole folio on fatal mapping errors.
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 6dad9eea78240..ff33ae870b359 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -2600,6 +2600,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))
> @@ -2610,8 +2611,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()
> @@ -2619,18 +2622,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,
> - release_start))
> - goto invalidate_dirty_pages;
> - goto update_disksize;
> - }
> - return err;
> + break;
> }
> ext4_msg(sb, KERN_CRIT,
> "Delayed block allocation failed for "
> @@ -2644,9 +2636,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;
> /*
> @@ -2655,10 +2646,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, release_start))
> + fatal = true;
> +
> + if (fatal)
> + *give_up_on_write = true;
> + if (!progress)
> + return err;
> + }
[Severity: Medium]
Can ext4_do_writepages() keep looping with give_up_on_write set to true
after a fatal -ENOSPC?
When mpage_map_one_extent() fails with -ENOSPC and
ext4_count_free_clusters(sb) == 0 after an earlier extent in the folio was
mapped, mpage_map_and_submit_extent() now submits the partial folio, sets
*give_up_on_write = true, and returns -ENOSPC. However,
ext4_do_writepages() initializes give_up_on_write once outside the
writeback loop and retries -ENOSPC unconditionally:
fs/ext4/inode.c:ext4_do_writepages() {
...
mpage_release_unused_pages(mpd, give_up_on_write, release_start);
/* Submit prepared bio */
ext4_io_submit(&mpd->io_submit);
...
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 mpage_release_unused_pages() clears mpd->scanned_until_end when
mpd->start_pos < mpd->next_pos, the loop continues with give_up_on_write
stuck at true and release_start reset to 0. On the next iteration, either
the same partially submitted folio is re-locked and invalidated in the same
ext4_do_writepages() call, or (in WB_SYNC_NONE while the partial folio is
still under writeback) subsequent dirty folios are processed and any unused
folios left in [mpd->start_pos, mpd->next_pos) get invalidated by the stale
give_up_on_write flag, while ext4_do_writepages() clears ret = 0 and
returns success. The same happens if mpage_submit_partial_folio() fails
after a retryable -ENOSPC or -EAGAIN.
Should ext4_do_writepages() break out of the loop when give_up_on_write is
set instead of retrying -ENOSPC or clearing -EAGAIN?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116064404.189911-1-sunjunchao@bytedance.com?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-16 7:24 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 6:44 [PATCH v3 0/3] ext4: fix buffered writeback error handling Julian Sun
2026-11-16 6:44 ` [PATCH v3 1/3] ext4: fix a race between partial writeback and truncation Julian Sun
2026-11-16 7:02 ` sashiko-bot
2026-11-16 6:47 ` [PATCH v3 2/3] ext4: submit mapped buffers after fatal mapping errors Julian Sun
2026-11-16 7:24 ` sashiko-bot
2026-11-16 6:47 ` [PATCH v3 3/3] ext4: use atomic bitops for io_end flags Julian Sun
2026-11-16 6:59 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox