Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v13 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages
@ 2026-12-04  7:38 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
                   ` (6 more replies)
  0 siblings, 7 replies; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

Hi,

An RWF_DONTCACHE write is buffered IO: its data goes through the page
cache, and writeback drops each folio once the folio is written back.  A
write that starts or ends inside a folio covers that folio only
partially.  Usually other writes cover the rest of the folio.  If
writeback drops the folio before the other writes write their parts, the
next of them must first read the folio back from disk.  32 threads
writing interleaved records to one XFS file, 4 KiB pages, before and
after 5/7:

  record  pwritev2() flags          4 KiB device       MiB/s
  bytes                             reads per write
                                    before   after     before  after
  47008   RWF_DONTCACHE             0.97     0.0007    831     2152
  47008   RWF_DONTCACHE|RWF_DSYNC   0.99     0.0007    601     1210
  6000    RWF_DONTCACHE             0.97     0.0001    121      502
  6000    RWF_DONTCACHE|RWF_DSYNC   0.99     0.0001     90      209

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.  6000
bytes is a second example.

The test program and script: https://lore.kernel.org/linux-nfs/atqJBS8MlYfeUU-t@kernel.org/

- 1/7 moves code into a helper, with no functional change.
- 2/7 fixes 88d6f128d06d ("mm: track DONTCACHE dirty pages per
  bdi_writeback"), in v7.3: WB_DONTCACHE_DIRTY could drift.
- 3/7 reports WB_DONTCACHE_DIRTY in the bdi debugfs stats.
- 4/7 adds folio_mark_dropbehind() for a folio that is already dirty.
- 5/7 keeps a partially covered folio until the write that completes
  it.  The change is in iomap only.
- 6/7 adds IOCB_DONTCACHE_NOKICK: a writer that syncs the range itself
  does not kick DONTCACHE writeback.
- 7/7 makes NFSD set it for a split FILE_SYNC or DATA_SYNC WRITE.

Merging: Chuck suggested taking the series through the mm tree.  1/7 to
6/7 apply cleanly to each of these mm branches:

  mm-unstable  e685fdf67323 ("zram: publish streams under lock in zcomp_cpu_up_prepare()")
  mm-new       1a7610248a38 ("mm/zsmalloc: remove shrinker")
  mm-stable    6cce17ffe54a ("Merge tag 'staging-7.4-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging")

7/7 needs the NFSD direct-mode changes that are in cel/nfsd-testing.
The whole series applies to cel/nfsd-testing at eb76c146d6fa ("NFSD:
parse fsloc and secinfo export options without CONFIG_NFSD_V4"), where
each patch builds with W=1 with no warnings.  So 7/7 could follow in
the nfsd tree once the mm part is merged.

Changes since v12:
- Added Jan's Reviewed-by to 1/7, 2/7 and 3/7.
- 2/7: the count to subtract is -(long)folio_nr_pages().  v12 negated
  the unsigned value, which added about 2^32 to WB_DONTCACHE_DIRTY on
  32-bit.  sashiko found this in its review of v12 2/7
  (https://lore.kernel.org/r/sashiko-outbox-213508@kernel.org).
- 5/7 to 7/7 are much simpler, after Jan's review of v12
  (https://lore.kernel.org/linux-nfs/iushs6l3mjqkq65srej4gevaf2oeazy5j5miaipy6yxscetzjd@d4ti4qgbhman/).
  5/7 to 7/7 now add 122 lines and remove 10.  In v12 they added 287
  lines and removed 18.  The whole series now adds 198 lines and removes
  23, against 370 and 31 in v12.
- 5/7 is now in iomap only.  iomap looks up a partially covered folio
  without FGP_DONTCACHE, as Jan suggested, so a new folio is not marked
  dropbehind.  If this write completes the folio, iomap marks it after
  the write copies its part into it.  iomap can give a folio up without
  writing it: the mapping can go stale, or a copy can come up short.
  Such a folio stays unmarked.  FGP_DONTCACHE_EDGE and
  folio_dontcache_edge_written() are gone, and 5/7 does not change
  mm/filemap.c.  iomap_write_begin() makes the decision before it reads
  or zeroes the rest of the folio.  It returns the decision to
  iomap_write_iter(), which marks the folio after the copy.
- 5/7 also checks the file size.  An appending write makes its folio
  uptodate, because iomap fills the part past EOF with zeroes.  v12 took
  that for the data of the other writes, so the next append marked the
  folio before it was complete.  Benjamin Coddington found this.  One
  writer appended 4000-byte records with RWF_DONTCACHE|RWF_DSYNC to XFS
  with 16 KiB blocks.  It read back about half of its writes and left
  91% of the file cached.  Now the part of the folio outside the write
  must also lie below EOF.
- 6/7 replaces the interface of v12 6/7 (IOCB_DONTCACHE_DEFER,
  IOMAP_DONTCACHE_DEFER, FGP_DONTCACHE_DEFER,
  filemap_dontcache_edge_completes() and
  filemap_dontcache_edge_complete()) with one flag,
  IOCB_DONTCACHE_NOKICK.  v12 6/7 existed only to stop the DONTCACHE
  writeback kick for a write whose caller syncs it.  This flag does
  that and nothing else.
- 7/7 only sets IOCB_DONTCACHE_NOKICK, and describes the shared pages in
  nfsd-io-modes.rst.  NFSD keeps no state across a WRITE.  The race of
  v12 is gone: two WRITEs that checked a shared folio at the same time,
  before either wrote it, both left it cached.  Now the check runs under
  the folio lock, inside the write.
- Dropped Jeff's Reviewed-by from 7/7, which is a new patch.
- Rebased on the current cel/nfsd-testing.

Testing, 4 KiB pages:
- Through NFSD (io_cache_write=2), 32 ranks write interleaved records to
  one file, and the client sends each WRITE as FILE_SYNC.  With
  47008-byte records: 0.0003 reads per WRITE, no DONTCACHE writeback
  kick, and 1 of the file's 524066 pages cached at the end.  With
  6000-byte records: 0.0000, no kick, 11 cached.  With v12's 6/7 and 7/7
  in place of these, the same test cached 1294 and 1400 pages, with the
  same reads and no kick.
- Benjamin's case, on XFS with 16 KiB blocks: one writer appends
  4000-byte records with RWF_DONTCACHE|RWF_DSYNC.  With v12, it read
  back 0.49 folios per write and left 91% of the file cached.  With v13,
  it reads no data back and leaves only the last folio cached.  With
  10240-byte records, v12 read back 0.25 folios per write and left 40%
  cached.  v13 again reads nothing back.
- The KUnit test of v12 (not in this series) gives the same result.
- The tests of v12 with mmap stores into a file while writes run on it,
  through NFSD and locally, give the same results with v13.  So does a
  stress run of 20 iterations through NFSD and LOCALIO.  Every record
  verifies, and WB_DONTCACHE_DIRTY is 0 at the end.

Limits: 5/7 is exact for two writes that share a folio, and for any
number of writes that append to the file.  It decides whether the other
writes wrote the rest of a folio from the uptodate state of the folio
and from the file size.  A write fills the part of a block that it does
not cover with zeroes.  Below EOF, a later write cannot tell these
zeroes from the data of another write.  So when three or more writes
share a folio below EOF, the folio can be dropped too early, and a later
write reads it back.  With 16 KiB blocks, 32 threads write interleaved
4000-byte records to one file.  They read 1.95 4 KiB blocks per write,
and 91% of the file stays cached.  With 10240-byte records, they read
0.97 blocks per write, and 39% stays cached.

v12: https://lore.kernel.org/linux-nfs/20261112190911.84702-1-snitzer@kernel.org/
v11: https://lore.kernel.org/linux-nfs/20261111015607.46338-1-snitzer@kernel.org/
v10: https://lore.kernel.org/linux-nfs/20261103211754.35352-1-snitzer@kernel.org/
v9: https://lore.kernel.org/linux-nfs/20261024221152.6965-1-snitzer@kernel.org/
v8: https://lore.kernel.org/linux-nfs/20261023024548.71123-1-snitzer@kernel.org/
v7: https://lore.kernel.org/linux-nfs/20261020220346.28020-1-snitzer@kernel.org/
v6: https://lore.kernel.org/linux-nfs/atbfEbgMU6UGkn2X@kernel.org/
v5: https://lore.kernel.org/linux-nfs/20261019150725.1507-1-snitzer@kernel.org/
v4: https://lore.kernel.org/linux-nfs/20261015233309.42999-1-snitzer@kernel.org/
v3: https://lore.kernel.org/linux-nfs/20261001045502.48381-1-snitzer@kernel.org/
v2: https://lore.kernel.org/linux-nfs/20260929231329.22018-1-snitzer@kernel.org/
v1: https://lore.kernel.org/linux-nfs/20260929173423.16149-1-snitzer@kernel.org/

Thanks,
Mike

Mike Snitzer (7):
  mm: move clearing the dropbehind flag of a found folio into a helper
  mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there
  mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats
  mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it
  iomap: keep a DONTCACHE edge folio until a write completes it
  fs: let a DONTCACHE writer that syncs the range itself skip the kick
  NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs

 .../filesystems/nfs/nfsd-io-modes.rst         |  16 +++
 fs/iomap/buffered-io.c                        | 103 ++++++++++++++++--
 fs/nfsd/vfs.c                                 |   5 +-
 include/linux/fs.h                            |   8 +-
 include/linux/pagemap.h                       |   1 +
 mm/backing-dev.c                              |   6 +
 mm/filemap.c                                  |  82 +++++++++++---
 7 files changed, 198 insertions(+), 23 deletions(-)


base-commit: eb76c146d6facf9588b6c1da360e2db41cdda335
-- 
2.52.0



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v13 1/7] mm: move clearing the dropbehind flag of a found folio into a helper
  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 ` 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
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

Move the code in __filemap_get_folio_mpol() that clears PG_dropbehind
on a folio found by a cached lookup, and takes the folio off
WB_DONTCACHE_DIRTY, into filemap_clear_dropbehind().  No functional
change.  The next patch changes the helper.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
 mm/filemap.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 3bf31cc9d2709..0e7b02d5abb60 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1956,6 +1956,24 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
 	return folio;
 }
 
+/* Clear the dropbehind flag of a folio that a cached lookup found. */
+static void filemap_clear_dropbehind(struct address_space *mapping,
+				     struct folio *folio)
+{
+	if (!folio_test_clear_dropbehind(folio))
+		return;
+	if (folio_test_dirty(folio) && mapping_can_writeback(mapping)) {
+		struct inode *inode = mapping->host;
+		struct bdi_writeback *wb;
+		struct wb_lock_cookie cookie = {};
+		long nr = folio_nr_pages(folio);
+
+		wb = unlocked_inode_to_wb_begin(inode, &cookie);
+		wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -nr);
+		unlocked_inode_to_wb_end(inode, &cookie);
+	}
+}
+
 /**
  * __filemap_get_folio_mpol - Find and get a reference to a folio.
  * @mapping: The address_space to search.
@@ -2087,19 +2105,8 @@ struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
 	if (!folio)
 		return ERR_PTR(-ENOENT);
 	/* not an uncached lookup, clear uncached if set */
-	if (!(fgp_flags & FGP_DONTCACHE) && folio_test_clear_dropbehind(folio)) {
-		if (folio_test_dirty(folio) &&
-		    mapping_can_writeback(mapping)) {
-			struct inode *inode = mapping->host;
-			struct bdi_writeback *wb;
-			struct wb_lock_cookie cookie = {};
-			long nr = folio_nr_pages(folio);
-
-			wb = unlocked_inode_to_wb_begin(inode, &cookie);
-			wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -nr);
-			unlocked_inode_to_wb_end(inode, &cookie);
-		}
-	}
+	if (!(fgp_flags & FGP_DONTCACHE))
+		filemap_clear_dropbehind(mapping, folio);
 	return folio;
 }
 EXPORT_SYMBOL(__filemap_get_folio_mpol);
-- 
2.52.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v13 2/7] mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there
  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 ` Mike Snitzer
  2026-12-05  7:39   ` sashiko-bot
  2026-12-04  7:38 ` [PATCH v13 3/7] mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats Mike Snitzer
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

A cached lookup that finds a dropbehind folio clears the flag and
subtracts the folio from WB_DONTCACHE_DIRTY if PG_dirty is set.  That
test races with the dirtying of the folio.  filemap_dirty_folio() sets
PG_dirty before folio_account_dirtied() counts the folio, so a lookup in
between subtracts a folio that was never added.  The lookup also does
not hold the folio lock.  Every other path that subtracts a dirty folio
from the counter holds it, so the lookup can subtract the folio a second
time.  Either way the counter drifts, and once it is negative the
DONTCACHE writeback kick writes nothing.

Fix this by having filemap_clear_dropbehind() clear the dropbehind flag
of a folio that a cached lookup found, and take the folio off
WB_DONTCACHE_DIRTY if it is counted there.  With the folio locked and
the i_pages lock held, the folio is counted if both PG_dirty and the
dirty tag are set.  A folio that another task has locked keeps the
flag.

Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
 mm/filemap.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 0e7b02d5abb60..9ecd5060ea4c9 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1956,22 +1956,36 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
 	return folio;
 }
 
-/* Clear the dropbehind flag of a folio that a cached lookup found. */
+/*
+ * Clear the dropbehind flag of a folio that a cached lookup found, and take
+ * the folio off WB_DONTCACHE_DIRTY if it is counted there.  With the folio
+ * locked and the i_pages lock held, the folio is counted if both PG_dirty and
+ * the dirty tag are set.  A folio that another task has locked keeps the flag.
+ */
 static void filemap_clear_dropbehind(struct address_space *mapping,
-				     struct folio *folio)
+				     struct folio *folio, bool locked)
 {
-	if (!folio_test_clear_dropbehind(folio))
-		return;
-	if (folio_test_dirty(folio) && mapping_can_writeback(mapping)) {
-		struct inode *inode = mapping->host;
-		struct bdi_writeback *wb;
-		struct wb_lock_cookie cookie = {};
-		long nr = folio_nr_pages(folio);
+	unsigned long flags;
 
-		wb = unlocked_inode_to_wb_begin(inode, &cookie);
-		wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -nr);
-		unlocked_inode_to_wb_end(inode, &cookie);
+	if (!locked) {
+		if (!folio_trylock(folio))
+			return;
+		if (unlikely(folio->mapping != mapping))
+			goto out;
 	}
+	if (!mapping_can_writeback(mapping)) {
+		folio_clear_dropbehind(folio);
+		goto out;
+	}
+	xa_lock_irqsave(&mapping->i_pages, flags);
+	if (folio_test_clear_dropbehind(folio) && folio_test_dirty(folio) &&
+	    xa_get_mark(&mapping->i_pages, folio->index, PAGECACHE_TAG_DIRTY))
+		wb_stat_mod(inode_to_wb(mapping->host), WB_DONTCACHE_DIRTY,
+			    -(long)folio_nr_pages(folio));
+	xa_unlock_irqrestore(&mapping->i_pages, flags);
+out:
+	if (!locked)
+		folio_unlock(folio);
 }
 
 /**
@@ -2105,8 +2119,8 @@ struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
 	if (!folio)
 		return ERR_PTR(-ENOENT);
 	/* not an uncached lookup, clear uncached if set */
-	if (!(fgp_flags & FGP_DONTCACHE))
-		filemap_clear_dropbehind(mapping, folio);
+	if (!(fgp_flags & FGP_DONTCACHE) && folio_test_dropbehind(folio))
+		filemap_clear_dropbehind(mapping, folio, fgp_flags & FGP_LOCK);
 	return folio;
 }
 EXPORT_SYMBOL(__filemap_get_folio_mpol);
-- 
2.52.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v13 3/7] mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats
  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 ` Mike Snitzer
  2026-12-05  7:39   ` sashiko-bot
  2026-12-04  7:38 ` [PATCH v13 4/7] mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it Mike Snitzer
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

The per-bdi_writeback WB_DONTCACHE_DIRTY counter drives how much the
flusher writes back in response to an IOCB_DONTCACHE kick, but it is
not reported anywhere, so the DONTCACHE dirty backlog can only be
inferred from MemFree/Cached movement.

Report it as BdiDontcacheDirty in /sys/kernel/debug/bdi/<dev>/stats
and as WbDontcacheDirty in the per-wb stats, next to the
Reclaimable line.  Use wb_stat_sum() rather than wb_stat() to fold in
the per-cpu batches: the flusher targets the summed value and small
backlogs would otherwise read as zero.

Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
 mm/backing-dev.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index fad1495d3b200..a09c21759fb9f 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -46,6 +46,7 @@ struct wb_stats {
 	unsigned long nr_dirty_time;
 	unsigned long nr_writeback;
 	unsigned long nr_reclaimable;
+	unsigned long nr_dontcache_dirty;
 	unsigned long nr_dirtied;
 	unsigned long nr_written;
 	unsigned long dirty_thresh;
@@ -78,6 +79,7 @@ static void collect_wb_stats(struct wb_stats *stats,
 
 	stats->nr_writeback += wb_stat(wb, WB_WRITEBACK);
 	stats->nr_reclaimable += wb_stat(wb, WB_RECLAIMABLE);
+	stats->nr_dontcache_dirty += wb_stat_sum(wb, WB_DONTCACHE_DIRTY);
 	stats->nr_dirtied += wb_stat(wb, WB_DIRTIED);
 	stats->nr_written += wb_stat(wb, WB_WRITTEN);
 	stats->wb_thresh += wb_calc_thresh(wb, stats->dirty_thresh);
@@ -125,6 +127,7 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
 	seq_printf(m,
 		   "BdiWriteback:       %10lu kB\n"
 		   "BdiReclaimable:     %10lu kB\n"
+		   "BdiDontcacheDirty:  %10lu kB\n"
 		   "BdiDirtyThresh:     %10lu kB\n"
 		   "DirtyThresh:        %10lu kB\n"
 		   "BackgroundThresh:   %10lu kB\n"
@@ -139,6 +142,7 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
 		   "state:              %10lx\n",
 		   K(stats.nr_writeback),
 		   K(stats.nr_reclaimable),
+		   K(stats.nr_dontcache_dirty),
 		   K(stats.wb_thresh),
 		   K(dirty_thresh),
 		   K(background_thresh),
@@ -163,6 +167,7 @@ static void wb_stats_show(struct seq_file *m, struct bdi_writeback *wb,
 		   "WbCgIno:           %10lu\n"
 		   "WbWriteback:       %10lu kB\n"
 		   "WbReclaimable:     %10lu kB\n"
+		   "WbDontcacheDirty:  %10lu kB\n"
 		   "WbDirtyThresh:     %10lu kB\n"
 		   "WbDirtied:         %10lu kB\n"
 		   "WbWritten:         %10lu kB\n"
@@ -179,6 +184,7 @@ static void wb_stats_show(struct seq_file *m, struct bdi_writeback *wb,
 #endif
 		   K(stats->nr_writeback),
 		   K(stats->nr_reclaimable),
+		   K(stats->nr_dontcache_dirty),
 		   K(stats->wb_thresh),
 		   K(stats->nr_dirtied),
 		   K(stats->nr_written),
-- 
2.52.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v13 4/7] mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it
  2026-12-04  7:38 [PATCH v13 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages Mike Snitzer
                   ` (2 preceding siblings ...)
  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 ` Mike Snitzer
  2026-12-05  7:39   ` sashiko-bot
  2026-12-04  7:38 ` [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it Mike Snitzer
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

WB_DONTCACHE_DIRTY counts the dirty folios that are marked dropbehind.
When a dropbehind folio is dirtied, folio_account_dirtied() adds it to
the counter.  When the folio is cleaned, folio_clear_dirty_for_io() or
folio_account_cleaned() subtracts it again.  This balances only if the
folio is marked before it is dirtied, as it is for the folios that an
RWF_DONTCACHE write creates.

An RWF_DONTCACHE write that covers a folio only partially shares the
folio with the other writes that cover the rest.  With records smaller
than the folio there can be several.  For example, four or five
4000-byte records that append to a file share a 16 KiB folio.  The next
commit keeps such a folio until a write completes it: the write that
finds the rest of the folio written.  That write marks the folio
dropbehind.  It does this in iomap, so XFS and every other file system
that uses iomap for buffered writes get it.  The write marks the folio
after it dirties it, so the folio is already dirty when the write marks
it.  folio_set_dropbehind() only sets the flag.  With it, the clean
would subtract the folio from WB_DONTCACHE_DIRTY although it was never
added, and the counter would go negative.

Add folio_mark_dropbehind(), the inverse of filemap_clear_dropbehind().
With the folio locked, it sets PG_dropbehind.  Under the i_pages lock,
it adds the folio to WB_DONTCACHE_DIRTY if both PG_dirty and the dirty
tag are set.  A dirtier that has set PG_dirty but not yet counted the
folio finds the flag set and counts the folio itself.

Leave a mapped folio unmarked.  Through its page table, a mapped folio
can be dirtied without the folio lock, and could then be counted twice.
The folio lock keeps an unmapped folio unmapped.  A folio that is mapped
when a write completes it stays cached until reclaim.

The name follows folio_set_dirty() and folio_mark_dirty(): the set
variant only sets the flag, the mark variant also accounts for it.

Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 73af18a373670..3f5295d6fd73b 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -1306,6 +1306,7 @@ void end_page_writeback(struct page *page);
 void folio_end_writeback(struct folio *folio);
 void folio_end_writeback_no_dropbehind(struct folio *folio);
 void folio_end_dropbehind(struct folio *folio);
+void folio_mark_dropbehind(struct folio *folio);
 void folio_wait_stable(struct folio *folio);
 void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn);
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb);
diff --git a/mm/filemap.c b/mm/filemap.c
index 9ecd5060ea4c9..dc270b668258b 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1654,6 +1654,41 @@ void folio_end_dropbehind(struct folio *folio)
 }
 EXPORT_SYMBOL_GPL(folio_end_dropbehind);
 
+/**
+ * folio_mark_dropbehind - mark an existing folio dropbehind
+ * @folio: the folio, locked by the caller
+ *
+ * Mark @folio so that the writeback that cleans it drops it.  If the folio
+ * is already counted as dirty, which with the i_pages lock held means both
+ * PG_dirty and the dirty tag are set, add it to WB_DONTCACHE_DIRTY so that
+ * the clean that subtracts it leaves the counter balanced.  A mapped folio
+ * is left unmarked: a page table can dirty it without the folio lock.
+ */
+void folio_mark_dropbehind(struct folio *folio)
+{
+	struct address_space *mapping = folio->mapping;
+	unsigned long flags;
+
+	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
+
+	if (folio_test_dropbehind(folio))
+		return;
+	if (folio_mapped(folio))
+		return;
+	if (!mapping || !mapping_can_writeback(mapping)) {
+		folio_set_dropbehind(folio);
+		return;
+	}
+	xa_lock_irqsave(&mapping->i_pages, flags);
+	folio_set_dropbehind(folio);
+	if (folio_test_dirty(folio) &&
+	    xa_get_mark(&mapping->i_pages, folio->index, PAGECACHE_TAG_DIRTY))
+		wb_stat_mod(inode_to_wb(mapping->host), WB_DONTCACHE_DIRTY,
+			    folio_nr_pages(folio));
+	xa_unlock_irqrestore(&mapping->i_pages, flags);
+}
+EXPORT_SYMBOL_GPL(folio_mark_dropbehind);
+
 /**
  * folio_end_writeback_no_dropbehind - End writeback against a folio.
  * @folio: The folio.
-- 
2.52.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it
  2026-12-04  7:38 [PATCH v13 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages Mike Snitzer
                   ` (3 preceding siblings ...)
  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
  2026-12-05  7:39   ` sashiko-bot
  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
  6 siblings, 1 reply; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

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



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v13 6/7] fs: let a DONTCACHE writer that syncs the range itself skip the kick
  2026-12-04  7:38 [PATCH v13 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages Mike Snitzer
                   ` (4 preceding siblings ...)
  2026-12-04  7:38 ` [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it Mike Snitzer
@ 2026-12-04  7:38 ` 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
  6 siblings, 0 replies; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

generic_write_sync() kicks DONTCACHE writeback after every
IOCB_DONTCACHE write that is not IOCB_DSYNC.  Some callers sync once,
after writing several segments.  NFSD does this for a WRITE that the
client sends as FILE_SYNC or DATA_SYNC, and that NFSD splits into a
direct middle and buffered DONTCACHE ends.  It takes IOCB_DSYNC off each
segment and runs one vfs_fsync_range() over the whole WRITE.  Each
buffered segment then kicks writeback for folios that the caller's own
sync is about to write.  With the previous commit, the write that
completes a shared folio marks it while it is still dirty, so such a
kick also writes that folio.

Add IOCB_DONTCACHE_NOKICK.  The caller sets it with IOCB_DONTCACHE when
it writes the range back itself.  generic_write_sync() then does not
kick DONTCACHE writeback for that write.  Nothing else changes: the
folios are marked as before, and the caller's sync drops them.

IOCB_DONTCACHE_NOKICK enables NFSD, in a later commit, to avoid the kick
for a split FILE_SYNC or DATA_SYNC WRITE.

Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
 include/linux/fs.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5b3a100aba77b..bc79277c4bd02 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -358,6 +358,8 @@ struct readahead_control;
 /* kiocb is a read or write operation submitted by fs/aio.c. */
 #define IOCB_AIO_RW		(1 << 22)
 #define IOCB_HAS_METADATA	(1 << 23)
+/* With IOCB_DONTCACHE: the caller writes the range back itself, no kick */
+#define IOCB_DONTCACHE_NOKICK	(1 << 24)
 
 /* for use in trace events */
 #define TRACE_IOCB_STRINGS \
@@ -375,7 +377,8 @@ struct readahead_control;
 	{ IOCB_NOIO,		"NOIO" }, \
 	{ IOCB_ALLOC_CACHE,	"ALLOC_CACHE" }, \
 	{ IOCB_AIO_RW,		"AIO_RW" }, \
-	{ IOCB_HAS_METADATA,	"AIO_HAS_METADATA" }
+	{ IOCB_HAS_METADATA,	"AIO_HAS_METADATA" }, \
+	{ IOCB_DONTCACHE_NOKICK, "DONTCACHE_NOKICK" }
 
 struct kiocb {
 	struct file		*ki_filp;
@@ -2700,7 +2703,8 @@ static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count)
 				(iocb->ki_flags & IOCB_SYNC) ? 0 : 1);
 		if (ret)
 			return ret;
-	} else if (iocb->ki_flags & IOCB_DONTCACHE) {
+	} else if ((iocb->ki_flags & (IOCB_DONTCACHE | IOCB_DONTCACHE_NOKICK)) ==
+		   IOCB_DONTCACHE) {
 		filemap_dontcache_kick_writeback(iocb->ki_filp->f_mapping);
 	}
 
-- 
2.52.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v13 7/7] NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs
  2026-12-04  7:38 [PATCH v13 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages Mike Snitzer
                   ` (5 preceding siblings ...)
  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 ` Mike Snitzer
  2026-12-05  7:39   ` sashiko-bot
  6 siblings, 1 reply; 13+ messages in thread
From: Mike Snitzer @ 2026-12-04  7:38 UTC (permalink / raw)
  To: Matthew Wilcox, Jan Kara, linux-mm, Christian Brauner,
	linux-fsdevel

NFSD splits a misaligned direct-mode WRITE into a direct middle and
buffered DONTCACHE ends.  Each end covers its first or last unit, the
smallest folio the mapping allows, only partially.  The other WRITEs
that cover the rest of that unit share it.  The page cache keeps such a
unit until a write completes it, and the write that completes it marks
it dropbehind.

For a split WRITE that the client sends as FILE_SYNC or DATA_SYNC, NFSD
takes IOCB_DSYNC off each segment and runs one vfs_fsync_range() over
the whole WRITE.  Without IOCB_DSYNC, each buffered segment kicks
DONTCACHE writeback, for folios that NFSD's own sync is about to write.
Set IOCB_DONTCACHE_NOKICK on the segments of such a WRITE.

Describe in nfsd-io-modes.rst how the shared pages are kept and
dropped.

Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
 Documentation/filesystems/nfs/nfsd-io-modes.rst | 16 ++++++++++++++++
 fs/nfsd/vfs.c                                   |  5 ++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/Documentation/filesystems/nfs/nfsd-io-modes.rst b/Documentation/filesystems/nfs/nfsd-io-modes.rst
index 28235eceb0c7d..b59fa6e3fd44a 100644
--- a/Documentation/filesystems/nfs/nfsd-io-modes.rst
+++ b/Documentation/filesystems/nfs/nfsd-io-modes.rst
@@ -197,6 +197,22 @@ Misaligned WRITE:
     above. Set it to N for a workload that reads back or rewrites what
     it just wrote.
 
+    The page holding a start or end segment is shared with the other
+    WRITEs that cover the rest of the page, which may arrive before,
+    after or at the same time, from any client. So that the later WRITEs
+    to such a page do not have to read it back from disk, the page cache
+    keeps a page that a DONTCACHE write covers only partially until a
+    write completes it, and drops it once it is written back. A page
+    that no other WRITE completes stays cached until it is reclaimed.
+    The first and last pages of a WRITE that is not split are handled
+    the same way. A split FILE_SYNC or DATA_SYNC WRITE does not kick
+    DONTCACHE writeback for its buffered segments: its own sync writes
+    them back.
+
+    None of this applies when the start and end segments are normal
+    buffered IO, or for a filesystem that does not use iomap for
+    buffered writes.
+
 Tracing:
     The nfsd_read_direct trace event marks a READ that NFSD issues as
     O_DIRECT. It records the offset and length the client requested.
diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c
index a990c76dce0fc..67f4d3c7faaff 100644
--- a/fs/nfsd/vfs.c
+++ b/fs/nfsd/vfs.c
@@ -1556,7 +1556,8 @@ nfsd_direct_write(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	 * Sync a split WRITE once, after its last segment, instead of after
 	 * each segment that carries IOCB_DSYNC. A single segment keeps the
 	 * flag so a direct overwrite can complete with FUA instead of a cache
-	 * flush.
+	 * flush. The sync writes back the buffered DONTCACHE segments, so
+	 * they do not kick DONTCACHE writeback.
 	 */
 	sync_flags = kiocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC);
 	if (nsegs == 1)
@@ -1565,6 +1566,8 @@ nfsd_direct_write(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	*cnt = 0;
 	for (i = 0; i < nsegs; i++) {
 		kiocb->ki_flags = segments[i].flags & ~sync_flags;
+		if (sync_flags)
+			kiocb->ki_flags |= IOCB_DONTCACHE_NOKICK;
 		trace_nfsd_write_direct(rqstp, fhp, kiocb->ki_pos,
 					segments[i].iter.count,
 					kiocb->ki_flags);
-- 
2.52.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v13 2/7] mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there
  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-05  7:39   ` sashiko-bot
  0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05  7:39 UTC (permalink / raw)
  To: Mike Snitzer, Matthew Wilcox, Jan Kara, linux-mm,
	Christian Brauner

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: WB_DONTCACHE_DIRTY underflow race remains in
  filemap_clear_dropbehind()
--

commit 55f58463d6e9cc1fc8f21319f7b3162db9c5be66
Author: Mike Snitzer <snitzer@kernel.org>

mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there

This commit updates filemap_clear_dropbehind() to take the folio lock and
the i_pages lock before clearing PG_dropbehind, and to subtract the folio
from WB_DONTCACHE_DIRTY only when both PG_dirty and PAGECACHE_TAG_DIRTY
are set.

> diff --git a/mm/filemap.c b/mm/filemap.c
> index 0e7b02d5abb60..9ecd5060ea4c9 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -1956,22 +1956,36 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
>  	return folio;
>  }
>  
> -/* Clear the dropbehind flag of a folio that a cached lookup found. */
> +/*
> + * Clear the dropbehind flag of a folio that a cached lookup found, and take
> + * the folio off WB_DONTCACHE_DIRTY if it is counted there.  With the folio
> + * locked and the i_pages lock held, the folio is counted if both PG_dirty and
> + * the dirty tag are set.  A folio that another task has locked keeps the flag.
> + */
>  static void filemap_clear_dropbehind(struct address_space *mapping,
> -				     struct folio *folio)
> +				     struct folio *folio, bool locked)
>  {
> -	if (!folio_test_clear_dropbehind(folio))
> -		return;
> -	if (folio_test_dirty(folio) && mapping_can_writeback(mapping)) {
> -		struct inode *inode = mapping->host;
> -		struct bdi_writeback *wb;
> -		struct wb_lock_cookie cookie = {};
> -		long nr = folio_nr_pages(folio);
> +	unsigned long flags;
>  
> -		wb = unlocked_inode_to_wb_begin(inode, &cookie);
> -		wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -nr);
> -		unlocked_inode_to_wb_end(inode, &cookie);
> +	if (!locked) {
> +		if (!folio_trylock(folio))
> +			return;
> +		if (unlikely(folio->mapping != mapping))
> +			goto out;
> +	}
> +	if (!mapping_can_writeback(mapping)) {
> +		folio_clear_dropbehind(folio);
> +		goto out;
>  	}
> +	xa_lock_irqsave(&mapping->i_pages, flags);
> +	if (folio_test_clear_dropbehind(folio) && folio_test_dirty(folio) &&
> +	    xa_get_mark(&mapping->i_pages, folio->index, PAGECACHE_TAG_DIRTY))
> +		wb_stat_mod(inode_to_wb(mapping->host), WB_DONTCACHE_DIRTY,
> +			    -(long)folio_nr_pages(folio));
> +	xa_unlock_irqrestore(&mapping->i_pages, flags);
> +out:
> +	if (!locked)
> +		folio_unlock(folio);
>  }

[Severity: Medium]
Can WB_DONTCACHE_DIRTY still underflow here when a dirtying path runs
without the folio lock?

Paths such as mark_buffer_dirty() and zap_present_folio_ptes() ->
folio_mark_dirty() set PG_dirty via folio_test_set_dirty() without holding
the folio lock, before acquiring i_pages in __folio_mark_dirty().  Starting
from a dirty dropbehind folio (WB_DONTCACHE_DIRTY == nr):

CPU0 (writeback)         CPU1 (dirtyer)           CPU2 (lookup)
folio_lock()
folio_clear_dirty_for_io()
  PG_dirty=0, stat -= nr
                         folio_test_set_dirty()
                           PG_dirty=1
__folio_start_writeback()
  keeps TAG_DIRTY
folio_unlock()
                                                  filemap_clear_dropbehind()
                                                    folio_trylock()
                                                    xa_lock_irqsave()
                                                    clears PG_dropbehind
                                                    stat -= nr  <-- underflow
                         __folio_mark_dirty()
                           folio_account_dirtied()
                             dropbehind=0, no += nr

__folio_start_writeback() skips clearing PAGECACHE_TAG_DIRTY because it
sees PG_dirty set, leaving a stale tag for filemap_clear_dropbehind() to
observe alongside the uncounted PG_dirty.

The same underflow also happens without a stale tag if a cleaner such as
folio_clear_dirty_for_io() clears the lockless dirtyer's PG_dirty and
subtracts from WB_DONTCACHE_DIRTY before filemap_clear_dropbehind() clears
PG_dropbehind and before folio_account_dirtied() runs.

Once WB_DONTCACHE_DIRTY drifts negative, wb_stat_sum() clamps it to 0 and
wb_check_start_dontcache() writes nothing, stalling expedited RWF_DONTCACHE
writeback on that bdi_writeback until enough new dropbehind dirty pages
accumulate to offset the underflow.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796367526.git.snitzer@kernel.org?part=2


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v13 3/7] mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats
  2026-12-04  7:38 ` [PATCH v13 3/7] mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats Mike Snitzer
@ 2026-12-05  7:39   ` sashiko-bot
  0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05  7:39 UTC (permalink / raw)
  To: Mike Snitzer, Matthew Wilcox, Jan Kara, linux-mm,
	Christian Brauner

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm: inappropriate Fixes tag on debugfs observability addition
--

commit 5841700cd8241eea3d1a00583bfee49db3219ddb
Author: Mike Snitzer <snitzer@kernel.org>

> mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats
>
> The per-bdi_writeback WB_DONTCACHE_DIRTY counter drives how much the
> flusher writes back in response to an IOCB_DONTCACHE kick, but it is
> not reported anywhere, so the DONTCACHE dirty backlog can only be
> inferred from MemFree/Cached movement.
>
> Report it as BdiDontcacheDirty in /sys/kernel/debug/bdi/<dev>/stats
> and as WbDontcacheDirty in the per-wb stats, next to the
> Reclaimable line.  Use wb_stat_sum() rather than wb_stat() to fold in
> the per-cpu batches: the flusher targets the summed value and small
> backlogs would otherwise read as zero.
>
> Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> Reviewed-by: Jeff Layton <jlayton@kernel.org>
> Reviewed-by: Jan Kara <jack@suse.cz>
> Signed-off-by: Mike Snitzer <snitzer@kernel.org>

[Severity: Low]
Should the Fixes: tag be dropped here?  This patch adds debugfs
observability rather than fixing a bug in 88d6f128d06d ("mm: track
DONTCACHE dirty pages per bdi_writeback"), and a Fixes: tag can nominate
the change for stable backporting.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796367526.git.snitzer@kernel.org?part=3


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v13 4/7] mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it
  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-05  7:39   ` sashiko-bot
  0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05  7:39 UTC (permalink / raw)
  To: Mike Snitzer, Matthew Wilcox, Jan Kara, linux-mm,
	Christian Brauner

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: folio_mark_dropbehind() misses unlocked non-pte dirtier
  race
--

commit 58745295037799f6e54ed4a50a7c62fe9301b14c
Author: Mike Snitzer <snitzer@kernel.org>

mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it

This commit adds folio_mark_dropbehind() to mark an existing locked,
unmapped folio with PG_dropbehind and account it in WB_DONTCACHE_DIRTY if
it is already dirty.

> diff --git a/mm/filemap.c b/mm/filemap.c
> index 9ecd5060ea4c9..dc270b668258b 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -1654,6 +1654,41 @@ void folio_end_dropbehind(struct folio *folio)

[ ... ]

> +void folio_mark_dropbehind(struct folio *folio)
> +{
> +	struct address_space *mapping = folio->mapping;
> +	unsigned long flags;
> +
> +	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
> +
> +	if (folio_test_dropbehind(folio))
> +		return;
> +	if (folio_mapped(folio))
> +		return;
> +	if (!mapping || !mapping_can_writeback(mapping)) {
> +		folio_set_dropbehind(folio);
> +		return;
> +	}
> +	xa_lock_irqsave(&mapping->i_pages, flags);
> +	folio_set_dropbehind(folio);
> +	if (folio_test_dirty(folio) &&
> +	    xa_get_mark(&mapping->i_pages, folio->index, PAGECACHE_TAG_DIRTY))
> +		wb_stat_mod(inode_to_wb(mapping->host), WB_DONTCACHE_DIRTY,
> +			    folio_nr_pages(folio));
> +	xa_unlock_irqrestore(&mapping->i_pages, flags);
> +}

[Severity: Medium]
Can WB_DONTCACHE_DIRTY still be double-counted when an unlocked dirtier
races on an unmapped folio?

Checking folio_mapped(folio) does not exclude unlocked non-page-table
dirtiers.  For example, a GUP-pinned page from an O_DIRECT read (such as
nfs_direct_read_completion() calling set_page_dirty()) can be dirtied
without folio_lock() after userspace has called munmap() on the VMA:

CPU0 (unlocked dirtier)              CPU1 (writeback / RWF_DONTCACHE)
                                     folio_clear_dirty_for_io()
                                       /* clears PG_dirty, keeps tag */
nfs_direct_read_completion()
  set_page_dirty()
    folio_test_set_dirty() /* 0->1 */
    /* pauses before i_pages lock */
                                     __folio_start_writeback()
                                       /* sees PG_dirty, keeps tag */
                                     folio_unlock()
                                     ...
                                     folio_lock()
                                     folio_mark_dropbehind()
                                       xa_lock_irqsave(&mapping->i_pages)
                                       folio_set_dropbehind()
                                       wb_stat_mod(..., WB_DONTCACHE_DIRTY)
                                       xa_unlock_irqrestore(...)
    __folio_mark_dirty()
      xa_lock_irqsave(&mapping->i_pages)
      folio_account_dirtied()
        wb_stat_mod(..., WB_DONTCACHE_DIRTY)  <-- double-counted

Because folio_mark_dropbehind() sees both PG_dirty and PAGECACHE_TAG_DIRTY
set, it adds folio_nr_pages(folio) to WB_DONTCACHE_DIRTY, and the paused
__folio_mark_dirty() then sees PG_dropbehind and adds it a second time.

Cleaning the folio later subtracts it only once, leaving permanent upward
drift in WB_DONTCACHE_DIRTY and causing subsequent wb_check_start_dontcache()
kicks to pass an inflated target to wb_writeback() and flush unrelated
cached dirty pages on the same bdi_writeback.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796367526.git.snitzer@kernel.org?part=4


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it
  2026-12-04  7:38 ` [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it Mike Snitzer
@ 2026-12-05  7:39   ` sashiko-bot
  0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05  7:39 UTC (permalink / raw)
  To: Mike Snitzer, Matthew Wilcox, Jan Kara, linux-mm,
	Christian Brauner

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [Medium] iomap: partial RWF_DONTCACHE writes evict pre-existing cached
  folios
- [Medium] iomap: unaligned RWF_DONTCACHE writes clear dropbehind on
  large folios
- [Medium] iomap: iomap_write_begin() marks edge folio dropbehind when
  len < edge
- [Medium] iomap: iomap_get_folio() sets FGP_DONTCACHE on partial-folio
  writes
- [Medium] iomap: buffer_head type confusion in
  iomap_dontcache_edge_written()
- [Medium] iomap: iomap_get_folio() disables RWF_DONTCACHE for random
  sub-folio writes
- [Medium] iomap: uncompleted DONTCACHE edge writes trigger spurious
  flusher wakeups
- [Medium] iomap: WB_DONTCACHE_DIRTY race with unlocked buffer_head and
  PTE dirtying
- [Medium] iomap: iomap_get_folio() causes dropbehind xa_lock ping-pong
  on edge writes
--

commit e3882c700ba2d4aad3d4984efed64e1a1dcf1087
Author: Mike Snitzer <snitzer@kernel.org>

iomap: keep a DONTCACHE edge folio until a write completes it

This commit changes iomap buffered writes with IOMAP_DONTCACHE so that
partial writes to a minimum-size edge folio look up the folio without
FGP_DONTCACHE and only mark it dropbehind via folio_mark_dropbehind() once
the rest of the folio below EOF is already uptodate.

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

[Severity: Medium]
Can this evict pre-existing cached folios that were populated by normal
buffered I/O?

Because iomap_get_folio() looks up edge folios without FGP_DONTCACHE, an
uptodate min-order folio below EOF from an earlier cached read or write
cannot be distinguished from one created by an earlier RWF_DONTCACHE edge
write.  iomap_dontcache_edge_written() returns true, so iomap_write_iter()
marks it dropbehind and writeback evicts it, inverting the FGP_DONTCACHE
contract where an aligned write leaves a hot cached folio resident while a
partial-folio write evicts it.

[ ... ]

> +/*
> + * 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);
> +}

[Severity: Medium]
Should iomap_dontcache_edge() check the current iteration's len rather than
iter->len?

Even when pos is unit-aligned and iter->len >= unit, len can be smaller than
unit when a sub-folio extent ends mid-folio (iomap_length(iter) < unit) or
on a short-copy retry.  Returning 0 causes iomap_get_folio() to set
FGP_DONTCACHE and allocate a new folio already marked dropbehind, even
though this iteration only writes len bytes and unlocks the folio before the
rest of the unit is written.

[ ... ]

> @@ -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;
> +	}

[Severity: Medium]
Doesn't omitting FGP_DONTCACHE here strip PG_dropbehind from existing large
dropbehind folios?

When an unaligned or sub-unit RWF_DONTCACHE write lands in an existing large
folio marked PG_dropbehind (such as one from RWF_DONTCACHE readahead),
__filemap_get_folio_mpol() treats the lookup as a cached access and calls
filemap_clear_dropbehind().  iomap_dontcache_edge_written() then returns
false for every folio larger than mapping_min_folio_nrpages(), so the mark
is never restored and the large folio stays pinned in cache until reclaim,
contradicting the commit message's statement that larger folios are left
alone.

[Severity: Medium]
Does this disable RWF_DONTCACHE eviction altogether for random sub-folio
writes?

When every write is smaller than mapping_min_folio_nrbytes()—such as 4 KiB
random writes on 16 KiB/64 KiB page kernels or large-block-size XFS/ext4—
iomap_get_folio() omits FGP_DONTCACHE on every cold allocation, and
iomap_dontcache_edge_written() always returns false on the newly allocated
non-uptodate folio.  None of those folios get PG_dropbehind or
WB_DONTCACHE_DIRTY accounting, so 100% of touched folios remain cached until
memory reclaim.

[Severity: Medium]
Can uncompleted edge writes trigger repeated spurious flusher wakeups?

Because an uncompleted edge folio stays unmarked, WB_DONTCACHE_DIRTY remains
0 while dirtying the folio still sets WB_has_dirty_io.  generic_write_sync()
still calls filemap_dontcache_kick_writeback(), which checks
wb_has_dirty_io(wb) and wakes bdi_wq; wb_check_start_dontcache() then clears
WB_start_dontcache, runs an O(NR_CPUS) wb_stat_sum(wb, WB_DONTCACHE_DIRTY),
finds 0 pages, and re-arms the delayed timer under wb->work_lock—repeating
on every subsequent uncompleted sub-folio write.

[ ... ]

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

[Severity: Medium]
Should this check len == edge instead of treating iomap_dontcache_edge() as
a boolean?

When a mid-folio extent boundary (iomap_length(iter) < edge) or a short-copy
retry shrinks len below edge on an uptodate folio below EOF,
iomap_dontcache_edge_written() returns true and iomap_write_iter() marks the
folio dropbehind and unlocks it before the rest of this write's edge runs,
allowing writeback to drop it prematurely or leaving it marked dropbehind if
the remaining part fails.

[Severity: Medium]
Is it safe to call iomap_dontcache_edge_written() when IOMAP_F_BUFFER_HEAD
is set?

Block device buffered writes advertise FOP_DONTCACHE in def_blk_fops and set
IOMAP_F_BUFFER_HEAD in blkdev_iomap_begin(), so folio->private holds a
struct buffer_head * rather than a struct iomap_folio_state *.  On a
non-uptodate block-device folio, iomap_dontcache_edge_written() calls
iomap_is_partially_uptodate(), which casts folio->private to
struct iomap_folio_state * and reads unrelated buffer_head fields as an
uptodate bitmap, causing wrong dropbehind decisions and KCSAN data races.
Skipping iomap_dontcache_edge_written() (or checking buffer heads) when
IOMAP_F_BUFFER_HEAD is set would avoid the type confusion.

[ ... ]

> @@ -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);

[Severity: Medium]
Won't clearing dropbehind in iomap_get_folio() and re-marking it here cause
xa_lock ping-pong on edge overwrites?

When a sub-folio RWF_DONTCACHE overwrite hits an existing min-order
dropbehind folio below EOF, __filemap_get_folio_mpol() calls
filemap_clear_dropbehind() (taking xa_lock_irqsave(&mapping->i_pages) and
decrementing WB_DONTCACHE_DIRTY) and folio_mark_dropbehind() immediately
takes xa_lock_irqsave() again to re-set it and re-increment
WB_DONTCACHE_DIRTY.  On block devices where blkdev_buffered_write() holds
only inode_lock_shared(), concurrent sub-folio writers now contend on
mapping->i_pages twice per write where already-dirty overwrites previously
took no per-mapping spinlocks.

[Severity: Medium]
Can folio_mark_dropbehind() and filemap_clear_dropbehind() drift
WB_DONTCACHE_DIRTY when racing with unlocked buffer-head dirtying?

Both helpers assume that under folio_lock() and xa_lock, seeing PG_dirty and
PAGECACHE_TAG_DIRTY set proves folio_account_dirtied() has already run.  On
a block device with IOMAP_F_BUFFER_HEAD, an unlocked mark_buffer_dirty() can
set PG_dirty between folio_clear_dirty_for_io() and
__folio_start_writeback() (which then preserves PAGECACHE_TAG_DIRTY),
leaving both set before folio_account_dirtied() runs:

CPU0 (writeback)       CPU1 (buffer dirty)     CPU2 (DONTCACHE write)
blkdev_writepages()
  folio_lock(folio)
  folio_clear_dirty_for_io()
                         mark_buffer_dirty()
                           folio_test_set_dirty()
                           (preempted)
  __folio_start_writeback()
    (keeps DIRTY tag)
  folio_unlock(folio)
                                               iomap_write_iter()
                                                 folio_lock(folio)
                                                 folio_mark_dropbehind()
                                                   wb_stat_mod(+nr)
                                                 folio_unlock(folio)
                         __folio_mark_dirty()
                           folio_account_dirtied()
                             wb_stat_mod(+nr)  <-- double count

If PG_dropbehind was already set and the edge write clears it via
filemap_clear_dropbehind() without re-marking, the same window underflows
WB_DONTCACHE_DIRTY by -nr, causing wb_check_start_dontcache() to pass a
negative count and skip writeback.  A similar underflow occurs in
filemap_clear_dropbehind() on a mapped buffer-head folio when
try_to_free_buffers() calls folio_cancel_dirty() (leaving
PAGECACHE_TAG_DIRTY set) and zap_present_folio_ptes() races in
block_dirty_folio() before __folio_mark_dirty().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796367526.git.snitzer@kernel.org?part=5


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v13 7/7] NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs
  2026-12-04  7:38 ` [PATCH v13 7/7] NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs Mike Snitzer
@ 2026-12-05  7:39   ` sashiko-bot
  0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05  7:39 UTC (permalink / raw)
  To: Mike Snitzer, Matthew Wilcox, Jan Kara, linux-mm,
	Christian Brauner

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] nfsd: nfsd-io-modes.rst overbroadly excludes non-iomap fs from
  sync no-kick
- [Low] nfsd: nfsd_direct_write() sets IOCB_DONTCACHE_NOKICK without
  IOCB_DONTCACHE
--

commit 03778400aa9b1b1cb98cb34dfd15e5c34e3cade8
Author: Mike Snitzer <snitzer@kernel.org>

NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs

This commit sets IOCB_DONTCACHE_NOKICK on segments of a split synchronous
WRITE in nfsd_direct_write() and documents how partial DONTCACHE edge
pages are retained and written back.

> diff --git a/Documentation/filesystems/nfs/nfsd-io-modes.rst b/Documentation/filesystems/nfs/nfsd-io-modes.rst
> index 28235eceb0c7d..b59fa6e3fd44a 100644
> --- a/Documentation/filesystems/nfs/nfsd-io-modes.rst
> +++ b/Documentation/filesystems/nfs/nfsd-io-modes.rst
> @@ -197,6 +197,22 @@ Misaligned WRITE:
>      above. Set it to N for a workload that reads back or rewrites what
>      it just wrote.
>  
> +    The page holding a start or end segment is shared with the other
> +    WRITEs that cover the rest of the page, which may arrive before,
> +    after or at the same time, from any client. So that the later WRITEs
> +    to such a page do not have to read it back from disk, the page cache
> +    keeps a page that a DONTCACHE write covers only partially until a
> +    write completes it, and drops it once it is written back. A page
> +    that no other WRITE completes stays cached until it is reclaimed.
> +    The first and last pages of a WRITE that is not split are handled
> +    the same way. A split FILE_SYNC or DATA_SYNC WRITE does not kick
> +    DONTCACHE writeback for its buffered segments: its own sync writes
> +    them back.
> +
> +    None of this applies when the start and end segments are normal
> +    buffered IO, or for a filesystem that does not use iomap for
> +    buffered writes.

[Severity: Low]
Does "None of this applies ... for a filesystem that does not use iomap
for buffered writes" overstate the scope for the last sentence of the
preceding paragraph?

generic_write_sync() honors IOCB_DONTCACHE_NOKICK regardless of iomap, so
non-iomap FOP_DONTCACHE filesystems such as ext4 (on its
generic_perform_write() path) or NFS re-exports still skip the DONTCACHE
writeback kick on split FILE_SYNC and DATA_SYNC WRITEs.

Only the partial edge-page retention is iomap-specific.

[ ... ]

> diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c
> index a990c76dce0fc..67f4d3c7faaff 100644
> --- a/fs/nfsd/vfs.c
> +++ b/fs/nfsd/vfs.c

[ ... ]

> @@ -1565,6 +1566,8 @@ nfsd_direct_write(struct svc_rqst *rqstp, struct svc_fh *fhp,
>  	*cnt = 0;
>  	for (i = 0; i < nsegs; i++) {
>  		kiocb->ki_flags = segments[i].flags & ~sync_flags;
> +		if (sync_flags)
> +			kiocb->ki_flags |= IOCB_DONTCACHE_NOKICK;

[Severity: Low]
Should IOCB_DONTCACHE_NOKICK only be set when kiocb->ki_flags also has
IOCB_DONTCACHE?

In nfsd_write_dio_iters_init(), IOCB_DONTCACHE is unset when the exported
filesystem lacks FOP_DONTCACHE (such as f2fs) or on buffered edge segments
when nfsd_direct_misaligned_dontcache is disabled.

Setting IOCB_DONTCACHE_NOKICK alone violates its documented pairing in
include/linux/fs.h and causes tracepoints using TRACE_IOCB_STRINGS (such
as iomap_dio_rw_begin) to log DONTCACHE_NOKICK on non-DONTCACHE writes.
Checking sync_flags && (kiocb->ki_flags & IOCB_DONTCACHE) avoids this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796367526.git.snitzer@kernel.org?part=7


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-12-05  7:40 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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-05  7:39   ` sashiko-bot
2026-12-04  7:38 ` [PATCH v13 3/7] mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats Mike Snitzer
2026-12-05  7:39   ` sashiko-bot
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-05  7:39   ` sashiko-bot
2026-12-04  7:38 ` [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it Mike Snitzer
2026-12-05  7:39   ` sashiko-bot
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
2026-12-05  7:39   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox