Re: [PATCH] mm/vmstat: spread vmstat_update requeue across the stat interval

From: Dmitry Ilvokhin

Date: Thu Apr 09 2026 - 08:27:35 EST


[...]

>
> commit d725f0664b70aa5c677215b0fc1abc0117aaf114
> Author: Breno Leitao <leitao@xxxxxxxxxx>
> Date: Wed Apr 8 09:01:02 2026 -0700
>
> mm/vmstat: fix vmstat_shepherd double-scheduling vmstat_update
>
> vmstat_shepherd uses delayed_work_pending() to check whether
> vmstat_update is already scheduled for a given CPU before queuing it.
> However, delayed_work_pending() only tests WORK_STRUCT_PENDING_BIT,
> which is cleared the moment a worker thread picks up the work to
> execute it.
>
> This means that while vmstat_update is actively running on a CPU,
> delayed_work_pending() returns false. If need_update() also returns
> true at that point (per-cpu counters not yet zeroed mid-flush), the
> shepherd queues a second invocation with delay=0, causing vmstat_update
> to run again immediately after finishing.
>
> On a 72-CPU system this race is readily observable: before the fix,
> many CPUs show invocation gaps well below 500 jiffies (the minimum
> round_jiffies_relative() can produce), with the most extreme cases
> reaching 0 jiffies—vmstat_update called twice within the same jiffy.
>
> Fix this by replacing delayed_work_pending() with work_busy(), which
> returns non-zero for both WORK_BUSY_PENDING (timer armed or work
> queued) and WORK_BUSY_RUNNING (work currently executing). The shepherd
> now correctly skips a CPU in all busy states.

Great find. Thanks for taking the time to track this down.

>
> diff --git a/mm/vmstat.c b/mm/vmstat.c
> index d59eff1582547..5489549241b51 100644
> --- a/mm/vmstat.c
> +++ b/mm/vmstat.c
> @@ -2156,7 +2156,7 @@ static void vmstat_shepherd(struct work_struct *w)
> if (cpu_is_isolated(cpu))
> continue;
>
> - if (!delayed_work_pending(dw) && need_update(cpu))
> + if (!work_busy(&dw->work) && need_update(cpu))
> queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0);
> }
>

Reviewed-by: Dmitry Ilvokhin <d@xxxxxxxxxxxx>