mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages
Subsystems (8)
v13 cover mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages
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/[email protected]/
- 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/[email protected]).
- 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/[email protected]/
v11: https://lore.kernel.org/linux-nfs/[email protected]/
v10: https://lore.kernel.org/linux-nfs/[email protected]/
v9: https://lore.kernel.org/linux-nfs/[email protected]/
v8: https://lore.kernel.org/linux-nfs/[email protected]/
v7: https://lore.kernel.org/linux-nfs/[email protected]/
v6: https://lore.kernel.org/linux-nfs/[email protected]/
v5: https://lore.kernel.org/linux-nfs/[email protected]/
v4: https://lore.kernel.org/linux-nfs/[email protected]/
v3: https://lore.kernel.org/linux-nfs/[email protected]/
v2: https://lore.kernel.org/linux-nfs/[email protected]/
v1: https://lore.kernel.org/linux-nfs/[email protected]/
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
Signature · 1 line
2.52.0
PATCH v13 1/7 mm: move clearing the dropbehind flag of a found folio into a helper
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.
--- mm/filemap.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-)
mm/filemap.c +20 -13
@@ -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);
Signature · 1 line
2.52.0
PATCH v13 2/7 mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there
In reply to Mike SnitzerA 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.
--- mm/filemap.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)
mm/filemap.c +28 -14
@@ -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);
Signature · 1 line
2.52.0
PATCH v13 3/7 mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats
In reply to Mike SnitzerThe 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.
--- mm/backing-dev.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/backing-dev.c +6 -0
@@ -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),
Signature · 1 line
2.52.0
PATCH v13 4/7 mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it
In reply to Mike SnitzerWB_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.
--- include/linux/pagemap.h | 1 + mm/filemap.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)
include/linux/pagemap.h +1 -0
@@ -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);
mm/filemap.c +35 -0
@@ -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.
Signature · 1 line
2.52.0
PATCH v13 5/7 iomap: keep a DONTCACHE edge folio until a write completes it
In reply to Mike SnitzerAn 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.
--- fs/iomap/buffered-io.c | 103 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 7 deletions(-)
fs/iomap/buffered-io.c +96 -7
@@ -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 )
Signature · 1 line
2.52.0
PATCH v13 6/7 fs: let a DONTCACHE writer that syncs the range itself skip the kick
In reply to Mike Snitzergeneric_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.
--- include/linux/fs.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/fs.h +6 -2
@@ -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); }
Signature · 1 line
2.52.0
PATCH v13 7/7 NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs
In reply to Mike SnitzerNFSD 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.
--- Documentation/filesystems/nfs/nfsd-io-modes.rst | 16 ++++++++++++++++ fs/nfsd/vfs.c | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-)
Documentation/filesystems/nfs/nfsd-io-modes.rst +16 -0
@@ -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.
fs/nfsd/vfs.c +4 -1
@@ -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);
Signature · 1 line
2.52.0