Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Xavier Xia" <xavier.qyxia@gmail.com>,
	akpm@linux-foundation.org, hannes@cmpxchg.org,
	shakeel.butt@linux.dev
Cc: mhocko@kernel.org, roman.gushchin@linux.dev,
	muchun.song@linux.dev, willy@infradead.org, jack@suse.cz,
	david@kernel.org, brauner@kernel.org, linux-mm@kvack.org,
	tj@kernel.org, mkoutny@suse.com
Subject: Re: [PATCH] mm: batch per-task lruvec stat updates on the writeback path
Date: Sat, 21 Nov 2026 09:58:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-226889@kernel.org> (raw)
In-Reply-To: <20261120095559.429515-1-xavier.qyxia@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: misplaced dirty_stat_flush_current() in
  file_write_and_wait_range()
- [Medium] mm: unflushed stat_batch_mod() batches pin memcg CSS in idle
  kernel threads
- [Medium] mm: deferred positive stat_batch_mod() deltas cause node
  counter underflow
--

commit 511c77828659ec57163ac423dd938977a7ca7614
Author: Xavier Xia <xavier.qyxia@gmail.com>

mm: batch per-task lruvec stat updates on the writeback path

This commit batches per-task NR_FILE_DIRTY and NR_WRITEBACK lruvec stat
updates in task_struct up to 64 pages, flushing when switching lruvecs,
before dirty throttling, on writeback and task exit.

> diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
> index 15260cf2a29b0..46327617a7ad2 100644
> --- a/fs/fs-writeback.c
> +++ b/fs/fs-writeback.c
> @@ -2491,6 +2491,9 @@ static long wb_do_writeback(struct bdi_writeback *wb)
>  	wrote += wb_check_background_flush(wb);
>  	clear_bit(WB_writeback_running, &wb->state);
>  
> +	/* Don't pin another cgroup's memcg via a pending batch while idle. */
> +	dirty_stat_flush_current();
> +
>  	return wrote;
>  }

[Severity: Medium]
Can kernel threads that start writeback outside wb_do_writeback() pin a
removed cgroup's mem_cgroup css in current->stat_batch_lruvec while idle?

For example, kswapd calls shrink_folio_list() -> pageout() ->
__swap_writeout() -> folio_start_writeback() -> stat_batch_mod(), which
takes a css_tryget(&memcg->css) reference on non-root memcgs, and then
sleeps in kswapd_try_to_sleep() without calling dirty_stat_flush_current().
The same happens in khugepaged via khugepaged_scan_mm_slot() ->
filemap_write_and_wait_range() and in the workqueue rescuer branch of
wb_workfn(), which calls writeback_inodes_wb() directly.

Also, inside wb_do_writeback(), dirty_stat_flush_current() runs after
finish_writeback_work() has already woken completion waiters and after
wb_writeback() and wb_check_background_flush() have checked
wb_over_bg_thresh() with the flusher's own negative NR_FILE_DIRTY batch
still unapplied.

[ ... ]

> diff --git a/mm/filemap.c b/mm/filemap.c
> index b04be64a5069f..d837371ae9880 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -781,6 +781,8 @@ int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend)
>  	if (lend < lstart)
>  		return 0;
>  
> +	dirty_stat_flush_current();
> +
>  	if (mapping_needs_writeback(mapping)) {
>  		err = filemap_fdatawrite_range(mapping, lstart, lend);
>  		/* See comment of filemap_write_and_wait() */

[Severity: Medium]
Should dirty_stat_flush_current() be called after writeback submission
(for example in filemap_writeback() after do_writepages()) rather than at
the entry of file_write_and_wait_range()?

Nothing in mapping_needs_writeback() or filemap_fdatawrite_range() reads
NR_FILE_DIRTY or NR_WRITEBACK, so flushing before
filemap_fdatawrite_range() only prevents a small write's positive
NR_FILE_DIRTY delta from cancelling with folio_clear_dirty_for_io()'s
negative delta.  During filemap_fdatawrite_range(), current runs
do_writepages() and buffers up to 64 pages of -NR_FILE_DIRTY and
+NR_WRITEBACK in current->stat_batch_dirty and
current->stat_batch_writeback while pinning memcg->css in
current->stat_batch_lruvec.  When __filemap_fdatawait_range() completes,
__folio_end_writeback() decrements NR_WRITEBACK directly, and
file_write_and_wait_range() returns with those deltas still unflushed and
the memcg pinned.  Sibling callers of filemap_writeback() such as
filemap_write_and_wait_range() and filemap_flush() do not flush at all.

> diff --git a/mm/page-writeback.c b/mm/page-writeback.c
> index 0694ec4098f6d..516a8bb028f85 100644
> --- a/mm/page-writeback.c
> +++ b/mm/page-writeback.c

[ ... ]

> +static void stat_batch_mod(struct folio *folio, enum node_stat_item item,
> +			   long nr)
> +{
> +	struct task_struct *t = current;
> +	struct mem_cgroup *memcg;
> +	struct lruvec *lruvec;
> +	bool pinned;
> +	int *slot;
> +
> +	rcu_read_lock();
> +	memcg = folio_memcg(folio);
> +	/* An untracked folio has no lruvec, only a node. */
> +	if (!memcg) {
> +		rcu_read_unlock();
> +		mod_node_page_state(folio_pgdat(folio), item, nr);
> +		return;
> +	}
> +
> +	lruvec = mem_cgroup_lruvec(memcg, folio_pgdat(folio));
> +
> +	/* current is not the owner in interrupt context. */
> +	if (unlikely(!in_task() || nr > STAT_BATCH_MAX ||
> +		     nr < -STAT_BATCH_MAX)) {
> +		mod_lruvec_state(lruvec, item, nr);
> +		rcu_read_unlock();
> +		return;
> +	}
> +
> +	slot = item == NR_FILE_DIRTY ?
> +		&t->stat_batch_dirty : &t->stat_batch_writeback;
> +
> +	if (likely(t->stat_batch_lruvec == lruvec &&
> +		   *slot + nr <= STAT_BATCH_MAX &&
> +		   *slot + nr >= -STAT_BATCH_MAX)) {
> +		*slot += nr;
> +		rcu_read_unlock();
> +		return;
> +	}

[Severity: Medium]
Can buffering positive NR_FILE_DIRTY and NR_WRITEBACK increments in
task_struct drive the node and memcg counters negative when the matching
decrements are applied unbatched?

When a task dirties up to 64 pages and truncates them,
__folio_cancel_dirty() -> folio_account_cleaned() subtracts NR_FILE_DIRTY
directly via lruvec_stat_mod_folio() while the positive increment is still
held in current->stat_batch_dirty; similarly, __folio_end_writeback()
subtracts NR_WRITEBACK directly while current->stat_batch_writeback holds
the positive increment.

On !CONFIG_SMP kernels with CONFIG_MEMCG=y, global_node_page_state_pages(),
lruvec_page_state(), and memcg_page_state() compile out their negative-to-
zero clamp:

include/linux/vmstat.h:global_node_page_state_pages() {
	long x = atomic_long_read(&vm_node_stat[item]);
#ifdef CONFIG_SMP
	if (x < 0)
		x = 0;
#endif
	return x;
}

A negative counter then wraps to ~ULONG_MAX, corrupting dirty throttling
calculations in domain_dirty_avail() and balance_dirty_pages() as well as
reported stats.  Even on CONFIG_SMP, vmstat_refresh() only exempts
NR_WRITEBACK from negative-counter warnings and will warn on negative
vm_node_stat[NR_FILE_DIRTY], and cgroup v1 offline reparenting in
reparent_memcg_lruvec_state_local() clamps a dying child's negative local
counter to 0 while the task's later flush redirects its positive batch to
the parent via get_non_dying_memcg_start(), permanently inflating the
parent's local counter.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120095559.429515-1-xavier.qyxia@gmail.com?part=1


      parent reply	other threads:[~2026-11-21  9:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  9:55 [PATCH] mm: batch per-task lruvec stat updates on the writeback path Xavier Xia
2026-11-20 13:41 ` Matthew Wilcox
2026-11-21  9:58 ` sashiko-bot [this message]

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-226889@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=jack@suse.cz \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    --cc=willy@infradead.org \
    --cc=xavier.qyxia@gmail.com \
    /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