[RFC PATCH v4 11/12] iomap: Avoid folio dirtying in case of RWF_WRITETHROUGH
From: Ojaswin Mujoo
Date: Mon Sep 28 2026 - 08:14:10 EST
RWF_WRITETHROUGH dirties the folio only to send it for IO immediately,
in the same context. Due to this, we can optimize away the folio
dirtying and clearing step usually seen in buffered IO. Althrough we can
do away with most of the accounting there are a couple of counters we
need to take care of which we do during IO submission/completion.
Further, now folio_start/end_writeback() can find an inode with no wb
attached and hence ensure its not null before using it.
Co-developed-by: Ritesh Harjani (IBM) <ritesh.list@xxxxxxxxx>
Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@xxxxxxxxx>
Signed-off-by: Ojaswin Mujoo <ojaswin@xxxxxxxxxxxxx>
---
fs/iomap/buffered-io.c | 75 ++++++++++++++++++++++++++++++++++++------
mm/page-writeback.c | 24 +++++++++++---
2 files changed, 84 insertions(+), 15 deletions(-)
diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 91dad3550398..84310c786abf 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -11,6 +11,7 @@
#include <linux/fserror.h>
#include <linux/fsverity.h>
#include <linux/rmap.h>
+#include <linux/task_io_accounting_ops.h>
#include "internal.h"
#include "trace.h"
@@ -1162,6 +1163,34 @@ static bool iomap_write_end_inline(const struct iomap_iter *iter,
return true;
}
+/*
+ * __iomap_writethrough_end() is almost same as __iomap_write_end() but with the difference
+ * that we don't mark folio dirty since we are about to issue it for IO anyways.
+ * Consequently, most of the accounting is skipped.
+ */
+static bool __iomap_writethrough_end(struct inode *inode, loff_t pos, size_t len,
+ size_t copied, struct folio *folio)
+{
+ flush_dcache_folio(folio);
+
+ /*
+ * The blocks that were entirely written will now be up-to-date, so we
+ * don't have to worry about a read_folio reading them and overwriting a
+ * partial write. However, if we've encountered a short write and only
+ * partially written into a block, it will not be marked up-to-date, so a
+ * read_folio might come in and destroy our partial write.
+ *
+ * Do the simplest thing and just treat any short write to a
+ * non-uptodate page as a zero-length write, and force the caller to
+ * redo the whole thing.
+ */
+ if (unlikely(copied < len && !folio_test_uptodate(folio)))
+ return false;
+ iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
+ return true;
+}
+
+
/*
* Returns true if all copied bytes have been written to the pagecache,
* otherwise return false.
@@ -1183,7 +1212,10 @@ static bool iomap_write_end(struct iomap_iter *iter, size_t len, size_t copied,
return bh_written == copied;
}
- return __iomap_write_end(iter->inode, pos, len, copied, folio);
+ if (iter->flags & IOMAP_WRITETHROUGH)
+ return __iomap_writethrough_end(iter->inode, pos, len, copied, folio);
+ else
+ return __iomap_write_end(iter->inode, pos, len, copied, folio);
}
static ssize_t iomap_writethrough_complete(struct iomap_writethrough_ctx *wt_ctx)
@@ -1302,9 +1334,11 @@ iomap_writethrough_submit_bio(struct iomap_writethrough_ctx *wt_ctx,
/*
* In case of error we still need the I/O completion to run so we can
- * release references and end writeback on the folios.
+ * release references, handle accounting and end writeback on the
+ * folios.
*/
if (error) {
+ task_io_account_cancelled_write(len);
bio->bi_status = errno_to_blk_status(error);
bio_endio(bio);
return error;
@@ -1352,8 +1386,10 @@ iomap_writethrough_try_submit(struct iomap_writethrough_ctx *wt_ctx,
static void iomap_folio_prepare_writethrough(struct folio *folio, size_t off,
size_t len)
{
- bool fully_written;
+ bool needs_cleardirty = false, fully_written = false;
u64 zero = 0;
+ u64 tmp_off = off;
+ struct iomap_folio_state *ifs = folio->private;
if (folio_test_writeback(folio))
folio_wait_writeback(folio);
@@ -1362,16 +1398,35 @@ static void iomap_folio_prepare_writethrough(struct folio *folio, size_t off,
folio_mark_dirty(folio);
/*
- * We might either write through the complete folio or a partial folio
- * writethrough might result in all blocks becoming non-dirty, so we need to
- * check and mark the folio clean if that is the case.
+ * For writethrough, we don't mark the write range dirty but we still
+ * need clear the dirty range if someone else has dirtied it before.
+ * Further, if the clearing results in folio becoming completely clean,
+ * then we need to take care of accounting. If we have ifs, we can
+ * simply use that to ensure this. If we don't have ifs, that implies we
+ * did a complete overwrite of the folio, in which case we can just
+ * check if the folio was dirty earlier
*/
fully_written = (off == 0 && len == folio_size(folio));
- iomap_clear_range_dirty(folio, off, len);
- if (fully_written ||
- !iomap_find_dirty_range(folio, &zero, folio_size(folio)))
- folio_clear_dirty_for_writethrough(folio);
+ if (ifs) {
+ if (iomap_find_dirty_range(folio, &tmp_off, tmp_off + len)) {
+ iomap_clear_range_dirty(folio, off, len);
+
+ /*
+ * iomap_find_dirty_range() only works for folios with ifs
+ */
+ if (!iomap_find_dirty_range(folio, &zero,
+ folio_size(folio)))
+ needs_cleardirty = true;
+ }
+ } else {
+ WARN_ON(!fully_written);
+ if (folio_test_dirty(folio))
+ needs_cleardirty = true;
+ }
+ if (needs_cleardirty)
+ folio_clear_dirty_for_writethrough(folio);
+ task_io_account_write(len);
folio_start_writeback(folio);
}
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 098aa472d368..3ca54716d08e 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -2986,11 +2986,18 @@ bool __folio_end_writeback(struct folio *folio)
__xa_clear_mark(&mapping->i_pages, folio->index,
PAGECACHE_TAG_WRITEBACK);
+ /*
+ * With RWF_WRITETHROUGH, we might not have a writeback
+ * associated with the inode
+ */
wb = inode_to_wb(inode);
- wb_stat_mod(wb, WB_WRITEBACK, -nr);
- __wb_writeout_add(wb, nr);
+ if (wb) {
+ wb_stat_mod(wb, WB_WRITEBACK, -nr);
+ __wb_writeout_add(wb, nr);
+ }
if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) {
- wb_inode_writeback_end(wb);
+ if (wb)
+ wb_inode_writeback_end(wb);
if (mapping->host)
sb_clear_inode_writeback(mapping->host);
}
@@ -3030,10 +3037,17 @@ void __folio_start_writeback(struct folio *folio, bool keep_write)
on_wblist = mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
xas_set_mark(&xas, PAGECACHE_TAG_WRITEBACK);
+
+ /*
+ * With RWF_WRITETHROUGH, we might not have a writeback
+ * associated with the inode
+ */
wb = inode_to_wb(inode);
- wb_stat_mod(wb, WB_WRITEBACK, nr);
+ if (wb)
+ wb_stat_mod(wb, WB_WRITEBACK, nr);
if (!on_wblist) {
- wb_inode_writeback_start(wb);
+ if (wb)
+ wb_inode_writeback_start(wb);
/*
* We can come through here when swapping anonymous
* folios, so we don't necessarily have an inode to
--
2.55.0