From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7033530B51A for ; Wed, 1 Apr 2026 17:46:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775065600; cv=none; b=hWs0x7fNiZvZToS2d5+xM4pOAR7/ViOdaDGSt5Sv/+oWuRRRy8RGBuHw4+9NVxLjncJ6JiSr6+3VGj3zGHmyu199VhpBw9Vv5jrdkq2Z6tZhDKtxAqe/pJk4a3IAzBvnPLq6kmpXlCGkfLjM5vceE/NQFhdwj+g8x2rZbDuhmFY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775065600; c=relaxed/simple; bh=fohMRa5K9g7ocVeE2BTtOtg64GbOaCSV2v/AyKbYIx0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=hVlC7j6BLzmCvG4NH2rXFvjDTRyBHpZAaw+Opd8YEKzR1eZYE5YQmCu5V6OC5Cr+pfJLnLdW0TZViyyNXoyL+A4c/21l86VpPPqrk73Ir0AZ++2s97tUU1JVZBrlxQL0+AlRr+4YFPmIu3j37XZQDkT8otSe3d/AA4EtJHDpIOI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=t4+8YjAY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="t4+8YjAY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6175BC4CEF7; Wed, 1 Apr 2026 17:46:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775065600; bh=fohMRa5K9g7ocVeE2BTtOtg64GbOaCSV2v/AyKbYIx0=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=t4+8YjAYNy57lJHDN6YAsvie1MhYiKifQRrUrzv0aY/Z8Sy2P2I2WuppajvkT+DBT V7hsQrh7uVwJB4VhjhM+pjZ3Mac84DefOTyzV6HBR9ghVshnzRwN9xXcLIr6zDiWqP 0JtJc/FAhn8bul6Hx0+DSqWlKXVp0BawzGSdrfyK2gNwlQaJFRIOszsJ2Uglajj77d 18B5UwXwc2HLWygT9Wl8X4PPf1GsQsfv/4Qc2eYOKaDNyeZ0Z+Uh3hYfEj2RRrbDKe xb76dPtJgfgXad/1r67uMO+4YTgQzOo7x0aCgfcAaFrOjqFcIG9DyugUAuHluRFhVv zZupFcILz0ZIA== Message-ID: Date: Wed, 1 Apr 2026 19:46:35 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm/vmstat: spread vmstat_update requeue across the stat interval Content-Language: en-US To: Breno Leitao , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Suren Baghdasaryan , Michal Hocko Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kas@kernel.org, shakeel.butt@linux.dev, usama.arif@linux.dev, kernel-team@meta.com References: <20260401-vmstat-v1-1-b68ce4a35055@debian.org> From: "Vlastimil Babka (SUSE)" In-Reply-To: <20260401-vmstat-v1-1-b68ce4a35055@debian.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 4/1/26 15:57, Breno Leitao wrote: > vmstat_update uses round_jiffies_relative() when re-queuing itself, > which aligns all CPUs' timers to the same second boundary. When many > CPUs have pending PCP pages to drain, they all call decay_pcp_high() -> > free_pcppages_bulk() simultaneously, serializing on zone->lock and > hitting contention. > > Introduce vmstat_spread_delay() which distributes each CPU's > vmstat_update evenly across the stat interval instead of aligning them. > > This does not increase the number of timer interrupts — each CPU still > fires once per interval. The timers are simply staggered rather than > aligned. Additionally, vmstat_work is DEFERRABLE_WORK, so it does not > wake idle CPUs regardless of scheduling; the spread only affects CPUs > that are already active > > `perf lock contention` shows 7.5x reduction in zone->lock contention > (872 -> 117 contentions, 199ms -> 81ms total wait) on a 72-CPU aarch64 > system under memory pressure. > > Tested on a 72-CPU aarch64 system using stress-ng --vm to generate > memory allocation bursts. Lock contention was measured with: > > perf lock contention -a -b -S free_pcppages_bulk > > Results with KASAN enabled: > > free_pcppages_bulk contention (KASAN): > +--------------+----------+----------+ > | Metric | No fix | With fix | > +--------------+----------+----------+ > | Contentions | 872 | 117 | > | Total wait | 199.43ms | 80.76ms | > | Max wait | 4.19ms | 35.76ms | > +--------------+----------+----------+ > > Results without KASAN: > > free_pcppages_bulk contention (no KASAN): > +--------------+----------+----------+ > | Metric | No fix | With fix | > +--------------+----------+----------+ > | Contentions | 240 | 133 | > | Total wait | 34.01ms | 24.61ms | > | Max wait | 965us | 1.35ms | > +--------------+----------+----------+ > > Signed-off-by: Breno Leitao Cool! I noticed __round_jiffies_relative() exists and the description looks like it's meant for exactly this use case? > --- > mm/vmstat.c | 25 ++++++++++++++++++++++++- > 1 file changed, 24 insertions(+), 1 deletion(-) > > diff --git a/mm/vmstat.c b/mm/vmstat.c > index 2370c6fb1fcd..2e94bd765606 100644 > --- a/mm/vmstat.c > +++ b/mm/vmstat.c > @@ -2032,6 +2032,29 @@ static int vmstat_refresh(const struct ctl_table *table, int write, > } > #endif /* CONFIG_PROC_FS */ > > +/* > + * Return a per-cpu delay that spreads vmstat_update work across the stat > + * interval. Without this, round_jiffies_relative() aligns every CPU's > + * timer to the same second boundary, causing a thundering-herd on > + * zone->lock when multiple CPUs drain PCP pages simultaneously via > + * decay_pcp_high() -> free_pcppages_bulk(). > + */ > +static unsigned long vmstat_spread_delay(void) > +{ > + unsigned long interval = sysctl_stat_interval; > + unsigned int nr_cpus = num_online_cpus(); > + > + if (nr_cpus <= 1) > + return round_jiffies_relative(interval); > + > + /* > + * Spread per-cpu vmstat work evenly across the interval. Don't > + * use round_jiffies_relative() here -- it would snap every CPU > + * back to the same second boundary, defeating the spread. > + */ > + return interval + (interval * (smp_processor_id() % nr_cpus)) / nr_cpus; Hm doesn't this mean that lower id cpus will consistently fire in shorter intervals and higher id in longer intervals? What we want is same interval but differently offset, no? > +} > + > static void vmstat_update(struct work_struct *w) > { > if (refresh_cpu_vm_stats(true)) { > @@ -2042,7 +2065,7 @@ static void vmstat_update(struct work_struct *w) > */ > queue_delayed_work_on(smp_processor_id(), mm_percpu_wq, > this_cpu_ptr(&vmstat_work), > - round_jiffies_relative(sysctl_stat_interval)); > + vmstat_spread_delay()); > } > } > > > --- > base-commit: cf7c3c02fdd0dfccf4d6611714273dcb538af2cb > change-id: 20260401-vmstat-048e0feaf344 > > Best regards, > -- > Breno Leitao >