From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from stravinsky.debian.org (stravinsky.debian.org [82.195.75.108]) (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 717F7331222 for ; Wed, 1 Apr 2026 15:44:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=82.195.75.108 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775058256; cv=none; b=KUV06C0FjRLd8y/1auvFTUoTuhmiyEDrN3TUcYU/hXSZBcPfOXaFyLdhKgur2QEmxW4Pa7oInKpKwxfsP3CHDw20kPnq+9lze+xI2D3SXB2uHCH9rGc41m8HjzoZ+dCPCNq/SHshm/aI7KRj1JkOK2wd8XHkCgEd5Ndr+rKyS1A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775058256; c=relaxed/simple; bh=dPXCz9WpjshADJ7l55IoZyE+0JjM1P37eEeiazP+fAk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=M80+Nf2MO6sQfwAPgKqknPgQ6S8kI52uaMSHzM9Sw8YkhTRxLhH1pe9NF0GUAH/wwvx23dvYIxo4EBc9vhWnOOw56K+KD4J0J3zc/redprY+6ekTw39CL4uiy4ukNAGKeZHAqkXBa04hDzdh3NcmQTvJqDZaKSApdkxmDM/5dsY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=none smtp.mailfrom=debian.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b=HNEaaa3j; arc=none smtp.client-ip=82.195.75.108 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=debian.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b="HNEaaa3j" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debian.org; s=smtpauto.stravinsky; h=X-Debian-User:In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Reply-To:Content-ID:Content-Description; bh=MxBAS4YTt72TMZrDYJdny7nH8fv6qBkvO/lDqD+k4tA=; b=HNEaaa3jH/NeXpyDOGO153//jx 44xxgKRQoLjCe0iYy7Zz1Zl7KGxpfv4AlxvYbNBQmiqetlmWa6dJSjcAAaH52FwjEw+42AU7j65Ca fmmf1Is+7CSvnnDVZG6wK3+fvn595Xs8sazwT89P6vrcOY/M/nYn1Fc9iMH9RMxf+XKdO5nE2tvVO osPeMAFeSzNfmItI6sZPabpFxHCBqSZ0Z9umPw84fadx/ftjPISpWD+QuqnFuKIF0fJF4GMaDw2O7 q20WmxrlDGB6v8iC7xF/LwaYTADteT+ylMgj5xaFDYYRleOEcrXwaaVCwIr1sMCcf99FBdeayaSBK Z8uGRfwA==; Received: from authenticated user by stravinsky.debian.org with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1w7xjb-0035ak-0s; Wed, 01 Apr 2026 15:43:58 +0000 Date: Wed, 1 Apr 2026 08:43:52 -0700 From: Breno Leitao To: Usama Arif Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kas@kernel.org, shakeel.butt@linux.dev, kernel-team@meta.com Subject: Re: [PATCH] mm/vmstat: spread vmstat_update requeue across the stat interval Message-ID: References: <20260401-vmstat-v1-1-b68ce4a35055@debian.org> <20260401152343.3294686-1-usama.arif@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20260401152343.3294686-1-usama.arif@linux.dev> X-Debian-User: leitao On Wed, Apr 01, 2026 at 08:23:40AM -0700, Usama Arif wrote: > On Wed, 01 Apr 2026 06:57:50 -0700 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 > > --- > > 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; > > +} > > + > > 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()); > > This is awesome! Maybe this needs to be done to vmstat_shepherd() as well? > > vmstat_shepherd() still queues work with delay 0 on all CPUs that > need_update() in its for_each_online_cpu() loop: > > if (!delayed_work_pending(dw) && need_update(cpu)) > queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0); > > So when the shepherd fires, it kicks all dormant CPUs' vmstat workers > simultaneously. > > Under sustained memory pressure on a large system, I think the shepherd > fires every sysctl_stat_interval and could re-trigger the same lock > contention? Good point - incorporating similar spreading logic in vmstat_shepherd() would indeed address the simultaneous queueing issue you've described. Should I include this in a v2 of this patch, or would you prefer it as a separate follow-up patch?