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 --- 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