Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mike Snitzer" <snitzer@kernel.org>,
	"Matthew Wilcox" <willy@infradead.org>, "Jan Kara" <jack@suse.cz>,
	linux-mm@kvack.org, "Christian Brauner" <brauner@kernel.org>
Subject: Re: [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it
Date: Sat, 05 Dec 2026 07:39:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-251239@kernel.org> (raw)
In-Reply-To: <077b73a253ca413d615b8d53be147efee84f78c8.1796367526.git.snitzer@kernel.org>

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


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

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  7:38 [PATCH v13 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 1/7] mm: move clearing the dropbehind flag of a found folio into a helper Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 2/7] mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there Mike Snitzer
2026-12-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 [this message]
2026-12-04  7:38 ` [PATCH v13 6/7] fs: let a DONTCACHE writer that syncs the range itself skip the kick Mike Snitzer
2026-12-04  7:38 ` [PATCH v13 7/7] NFSD: do not kick DONTCACHE writeback for a WRITE that NFSD syncs Mike Snitzer
2026-12-05  7:39   ` sashiko-bot

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=sashiko-outbox-251239@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-mm@kvack.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=snitzer@kernel.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

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

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