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 2/7] mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there
Date: Sat, 05 Dec 2026 07:39:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-251236@kernel.org> (raw)
In-Reply-To: <2a621c714b77f50436cbbd0edddbce0ec591b5bc.1796367526.git.snitzer@kernel.org>

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


  reply	other threads:[~2026-12-05  7:40 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 [this message]
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

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