In iomap_writethrough_iter() we might encounter repeating folios across multiple iterations. Repeating folios can occur if, example, copy_folio_from_iter_atomic() does a short copy due to userspace pages not faulted in. This is an issue because a previous loop might have started writeback on them but not yet issued the IO. In the next iteration trying to get the same folio with FGP_STABLE will result in a deadlock. Since repeating folios will always be encountered back to back, we can just use a simple cur != prev check to detect them. Use this to avoid waiting for writeback or starting writeback on folios we have already processed. Note that in ->endio() we might end up calling folio_end_writethrough() twice on the same folio which can cause issues with folio_xor_flags_has_waiters(). For simplicity, just change the folio_xor_flags_has_waiters() call to an idempotent variant. Reported-by: Pankaj Raghav Co-developed-by: Ritesh Harjani (IBM) Signed-off-by: Ritesh Harjani (IBM) Signed-off-by: Ojaswin Mujoo --- fs/iomap/buffered-io.c | 37 ++++++++++++++++++++++++++++++------- mm/filemap.c | 3 ++- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 0844361fe0f1..70c1565e5416 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -804,6 +804,13 @@ struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len) { fgf_t fgp = FGP_WRITEBEGIN; + /* + * For writethrough, we open code the FGP_STABLE logic directly in + * iomap_writhrethrough_iter() so disable it here.. See + * iomap_writethrough_iter() for details. + */ + if (iter->flags & IOMAP_WRITETHROUGH) + fgp &= ~FGP_STABLE; if (iter->flags & IOMAP_NOWAIT) fgp |= FGP_NOWAIT; if (iter->flags & IOMAP_DONTCACHE) @@ -1383,15 +1390,12 @@ iomap_writethrough_try_submit(struct iomap_writethrough_ctx *wt_ctx, * need to clear the master dirty bit. */ static void iomap_folio_prepare_writethrough(struct folio *folio, size_t off, - size_t len) + size_t len, bool already_prepared) { bool fully_written; u64 zero = 0; u64 tmp_off = off; - if (folio_test_writeback(folio)) - folio_wait_writeback(folio); - if (folio_mkclean(folio)) folio_mark_dirty(folio); @@ -1409,7 +1413,8 @@ static void iomap_folio_prepare_writethrough(struct folio *folio, size_t off, } task_io_account_write(folio_nr_pages(folio) * PAGE_SIZE); - folio_test_set_writeback(folio); + if (!already_prepared) + folio_test_set_writeback(folio); } /** @@ -1426,6 +1431,17 @@ static void iomap_folio_prepare_writethrough(struct folio *folio, size_t off, * Folio handling note: We might be writing through a partial folio so we need * to be careful to not clear the folio dirty bit unless there are no dirty blocks * in the folio after the writethrough. + * + * **A corner case to be careful about** + * + * For writethrough, we open code the stable write behavior to handle the case + * where we encounter a folio that we already started writeback on but have not + * yet submitted. In that case we must not wait for writeback again to avoid + * deadlocking. Repeating folios can occur if, example, + * copy_folio_from_iter_atomic() does a short copy due to userspace pages not + * faulted in. Also, repeating folios will always be encountered back to back so + * we can just use a simple cur != prev check to detect them. + */ static int iomap_writethrough_iter(struct iomap_writethrough_ctx *wt_ctx, struct iomap_iter *iter, struct iov_iter *i, @@ -1439,6 +1455,7 @@ static int iomap_writethrough_iter(struct iomap_writethrough_ctx *wt_ctx, size_t chunk = mapping_max_folio_size(mapping); unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0; unsigned int bs = i_blocksize(iter->inode); + struct folio *prev_folio = NULL; /* copied over based on how DIO handles these flags */ if (iter->iomap.type == IOMAP_UNWRITTEN) @@ -1530,6 +1547,10 @@ static int iomap_writethrough_iter(struct iomap_writethrough_ctx *wt_ctx, if (mapping_writably_mapped(mapping)) flush_dcache_folio(folio); + /* Open coding stable write behavior, see comment on top. */ + if (prev_folio != folio) + folio_wait_writeback(folio); + copied = copy_folio_from_iter_atomic(folio, offset, bytes, i); written = iomap_write_end(iter, bytes, copied, folio) ? copied : 0; @@ -1544,8 +1565,10 @@ static int iomap_writethrough_iter(struct iomap_writethrough_ctx *wt_ctx, off_aligned = round_down(offset, bs); len_aligned = round_up(offset + written, bs) - off_aligned; - iomap_folio_prepare_writethrough(folio, off_aligned, - len_aligned); + iomap_folio_prepare_writethrough( + folio, off_aligned, len_aligned, prev_folio == folio); + + prev_folio = folio; if (!wt_ctx->nr_bvecs) { wt_ctx->bio_pos = round_down(pos, bs); diff --git a/mm/filemap.c b/mm/filemap.c index a1a5f8837e03..fc3ed0619838 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1711,7 +1711,8 @@ void folio_end_writethrough(struct folio *folio, bool error) if (!error) node_stat_mod_folio(folio, NR_WRITTEN, nr); - if (folio_xor_flags_has_waiters(folio, 1 << PG_writeback)) + folio_test_clear_writeback(folio); + if (folio_test_waiters(folio)) folio_wake_bit(folio, PG_writeback); } EXPORT_SYMBOL(folio_end_writethrough); -- 2.55.0