Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Mike Snitzer <snitzer@kernel.org>
To: Matthew Wilcox <willy@infradead.org>, Jan Kara <jack@suse.cz>,
	linux-mm@kvack.org, Christian Brauner <brauner@kernel.org>,
	linux-fsdevel@vger.kernel.org
Subject: [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it
Date: Fri,  4 Dec 2026 02:38:54 -0500	[thread overview]
Message-ID: <077b73a253ca413d615b8d53be147efee84f78c8.1796367526.git.snitzer@kernel.org> (raw)
In-Reply-To: <cover.1796367526.git.snitzer@kernel.org>

An RWF_DONTCACHE buffered write goes through the page cache like any
buffered write.  It marks every folio it creates dropbehind, so
writeback drops the folio as soon as the data of the write is on disk.
A write that starts or ends inside a folio covers that folio only
partially.  Usually other writes cover the rest of the folio.  An
example is the next record of a file written in records that are not a
multiple of the page size.  Writeback can drop the folio before the
other writes write their parts.  Then the next of them must first read
the folio back from disk: one read per misaligned write.

When a DONTCACHE write starts or ends inside the smallest folio the
mapping allows, iomap looks that folio up without FGP_DONTCACHE.  A new
folio is then not marked dropbehind.  Then iomap checks whether the
other writes have written their parts.  If so, this write completes the
folio.  iomap marks it with folio_mark_dropbehind() once the write has
copied its part into the folio, before it unlocks the folio.  The
writeback that cleans the data of all these writes drops it.  iomap can
give a folio up without writing its part: the mapping can go stale, or a
copy can come up short.  Such a folio stays unmarked.  A larger folio,
such as one from readahead, is left alone.  A found folio that is
already marked loses its mark in the lookup, and gets it back if this
write completes the folio.

The other parts are written if the folio is uptodate.  With blocks
smaller than the folio, the edge is not uptodate yet, so it is enough
that the blocks outside this write are uptodate.  But an uptodate folio
is not always a written one.  A write that extends the file fills the
part of the folio past EOF with zeroes, and the folio becomes uptodate.
The next append then finds the folio uptodate before it is complete, and
the folio is dropped.  The append after it must read the folio back.  So
the part of the folio outside this write must also lie below EOF.

The kernel cannot know if another write will come, and this commit does
not assume it.  It reads only the uptodate state of the folio and the
file size.  An edge that no other write completes, such as the last page
of a file, stays cached until reclaim.  Writeback drops all other folios
of the write, as before.

With this commit, 32 threads write interleaved 47008-byte records to
one XFS file with RWF_DONTCACHE.  Device reads per write fell from 0.97
to 0.0005, and throughput rose from 831 to 2281 MiB/s.  47008 bytes is
the transfer size of IO500's ior-hard, in which each rank writes
interleaved records to one shared file.  Any record size that is not a
multiple of the page size has the same problem.  With 6000-byte records,
device reads per write fell from 0.97 to 0.0001, and throughput rose
from 121 to 488 MiB/s.

Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
 fs/iomap/buffered-io.c | 103 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 96 insertions(+), 7 deletions(-)

diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 25915c0f5e1cc..65e48e99f234c 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -787,6 +787,63 @@ bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
 }
 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
 
+/*
+ * Have the other writes written the parts of this edge folio that this write
+ * does not cover?  The unit the writes share is the smallest folio the mapping
+ * allows.  Call before the write, while i_size does not include it yet.
+ */
+static bool iomap_dontcache_edge_written(struct folio *folio, loff_t pos,
+					 size_t len)
+{
+	struct inode *inode = folio->mapping->host;
+	size_t size = folio_size(folio);
+	loff_t fpos = folio_pos(folio);
+	loff_t isize = i_size_read(inode);
+	size_t from, to, bs;
+
+	if (folio_nr_pages(folio) != mapping_min_folio_nrpages(folio->mapping))
+		return false;
+	/*
+	 * Past EOF the folio holds zeroes, not the data of the other writes.  An
+	 * appending write makes it uptodate with those zeroes, so the next
+	 * append would find it uptodate.
+	 */
+	if ((pos > fpos && pos > isize) ||
+	    (pos + len < fpos + size && fpos + size > isize))
+		return false;
+	if (folio_test_uptodate(folio))
+		return true;
+
+	/*
+	 * With blocks smaller than the folio, the edge is not uptodate yet.
+	 * The blocks this write covers in full are [from, to).
+	 */
+	bs = i_blocksize(inode);
+	from = offset_in_folio(folio, pos);
+	to = min(from + len, size);
+	from = round_up(from, bs);
+	to = round_down(to, bs);
+	if (from && !iomap_is_partially_uptodate(folio, 0, from))
+		return false;
+	if (to < size && !iomap_is_partially_uptodate(folio, to, size - to))
+		return false;
+	return true;
+}
+
+/*
+ * For a DONTCACHE write: the bytes it covers of the unit at @pos if it
+ * covers that unit only partially, else 0.
+ */
+static size_t iomap_dontcache_edge(const struct iomap_iter *iter, loff_t pos)
+{
+	size_t unit = mapping_min_folio_nrbytes(iter->inode->i_mapping);
+	size_t offset = pos & (unit - 1);
+
+	if (!offset && iter->len >= unit)
+		return 0;
+	return min_t(u64, iter->len, unit - offset);
+}
+
 /**
  * iomap_get_folio - get a folio reference for writing
  * @iter: iteration structure
@@ -802,8 +859,20 @@ struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len)
 
 	if (iter->flags & IOMAP_NOWAIT)
 		fgp |= FGP_NOWAIT;
-	if (iter->flags & IOMAP_DONTCACHE)
-		fgp |= FGP_DONTCACHE;
+	if (iter->flags & IOMAP_DONTCACHE) {
+		size_t edge = iomap_dontcache_edge(iter, pos);
+
+		/*
+		 * Other writes may cover the rest of this unit.  Look it up
+		 * without FGP_DONTCACHE, as one unit, so that a new folio is
+		 * not marked.  If this write completes it, iomap_write_iter()
+		 * marks it once the write has written its part.
+		 */
+		if (edge)
+			len = min(len, edge);
+		else
+			fgp |= FGP_DONTCACHE;
+	}
 	fgp |= fgf_set_order(len);
 
 	return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
@@ -1029,11 +1098,13 @@ static int iomap_write_begin_inline(const struct iomap_iter *iter,
 /*
  * Grab and prepare a folio for write based on iter state. Returns the folio,
  * offset, and length. Callers can optionally pass a max length *plen,
- * otherwise init to zero.
+ * otherwise init to zero.  If @pdontcache_edge is not NULL, it is set to
+ * whether this DONTCACHE write completes a folio it covers only partially,
+ * which the caller then marks dropbehind once it has written its part.
  */
 static int iomap_write_begin(struct iomap_iter *iter,
 		const struct iomap_write_ops *write_ops, struct folio **foliop,
-		size_t *poffset, u64 *plen)
+		size_t *poffset, u64 *plen, bool *pdontcache_edge)
 {
 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
 	loff_t pos;
@@ -1099,6 +1170,15 @@ static int iomap_write_begin(struct iomap_iter *iter,
 
 	pos = iomap_trim_folio_range(iter, folio, poffset, &len);
 
+	/*
+	 * Decide now, before __iomap_write_begin() reads or zeroes the rest of
+	 * the folio, whether this write completes a folio it shares with other
+	 * writes.
+	 */
+	if (pdontcache_edge)
+		*pdontcache_edge = iomap_dontcache_edge(iter, pos) &&
+				   iomap_dontcache_edge_written(folio, pos, len);
+
 	if (srcmap->type == IOMAP_INLINE)
 		status = iomap_write_begin_inline(iter, folio);
 	else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
@@ -1202,6 +1282,7 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i,
 		size_t copied;		/* Bytes copied from user */
 		u64 written;		/* Bytes have been written */
 		loff_t pos;
+		bool dontcache_edge = false; /* completes a shared folio? */
 
 		bytes = iov_iter_count(i);
 retry:
@@ -1231,7 +1312,8 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i,
 		}
 
 		status = iomap_write_begin(iter, write_ops, &folio, &offset,
-				&bytes);
+				&bytes, (iter->flags & IOMAP_DONTCACHE) ?
+				&dontcache_edge : NULL);
 		if (unlikely(status)) {
 			iomap_write_failed(iter->inode, iter->pos, bytes);
 			break;
@@ -1247,6 +1329,13 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i,
 		copied = copy_folio_from_iter_atomic(folio, offset, bytes, i);
 		written = iomap_write_end(iter, bytes, copied, folio) ?
 			  copied : 0;
+		/*
+		 * Mark a folio this write completes only now, with the folio
+		 * still locked: a stale mapping or a short copy can make the
+		 * write give the folio up without writing its part.
+		 */
+		if (dontcache_edge && written == bytes)
+			folio_mark_dropbehind(folio);
 
 		/*
 		 * Update the in-memory inode size after copying the data into
@@ -1616,7 +1705,7 @@ static int iomap_unshare_iter(struct iomap_iter *iter,
 
 		bytes = min_t(u64, SIZE_MAX, bytes);
 		status = iomap_write_begin(iter, write_ops, &folio, &offset,
-				&bytes);
+				&bytes, NULL);
 		if (unlikely(status))
 			return status;
 		if (iomap->flags & IOMAP_F_STALE)
@@ -1692,7 +1781,7 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero,
 
 		bytes = min_t(u64, SIZE_MAX, bytes);
 		status = iomap_write_begin(iter, write_ops, &folio, &offset,
-				&bytes);
+				&bytes, NULL);
 		if (status)
 			return status;
 		if (iter->iomap.flags & IOMAP_F_STALE)
-- 
2.52.0



  parent reply	other threads:[~2026-12-04  7:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  7:38 [PATCH v13 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 1/7] mm: move clearing the dropbehind flag of a found folio into a helper Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 2/7] mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 3/7] mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 4/7] mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it Mike Snitzer
2026-12-04  7:38 ` Mike Snitzer [this message]
2026-12-04  7:38 ` [PATCH v13 6/7] fs: let a DONTCACHE writer that syncs the range itself skip the kick Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 7/7] NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs Mike Snitzer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=077b73a253ca413d615b8d53be147efee84f78c8.1796367526.git.snitzer@kernel.org \
    --to=snitzer@kernel.org \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox