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 4C83E3B0AD0 for ; Thu, 9 Apr 2026 17:01:08 +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=1775754068; cv=none; b=lxeHEX0F5GArAuQpV22+4bw9OXKG10ag/b/m4U/kf8TBNm1uJ2cLjzMYCH8r098EBGFP+Tj0/wYuRG2FLXGTCg6hqtPOROfxd+peaToDncQjTW5w5pt028/tk3mbx+VKLBx6WZwDE4W39Vzeeunr53oYnQUxR9upGa93q2R8QnU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775754068; c=relaxed/simple; bh=vpM1p9ulFwkMeX6amQbAGlMHW7aiCUgASpTF7AU0vA0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=kBNAMV+i4MzQGApJawf7V8jSAF8LVY02XCdRCdZ5veh3dwhN8n4DX2IVqCjDusA3VwNMZsYE99zC/ak8DFEJlEeoZAp86xTrQsIETmkHSrfZhpFJ2ZLgMJlrw23Khc1ZrC5IMhTL6sviythjHv3Y3FD+urW+OG/Ugnu2LLYT4LY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hw8QJZXj; 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="Hw8QJZXj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4EF1C4CEF7; Thu, 9 Apr 2026 17:01:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775754067; bh=vpM1p9ulFwkMeX6amQbAGlMHW7aiCUgASpTF7AU0vA0=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=Hw8QJZXjTX2OaAUte2QPQNb/SI5CU8gtmzAs5q1zKUAa9jrkl780bwqp5a+Rm2+jY TQS9ynebRxWSpO+4XyYObpmh8Iy6ywSsdXQH+ixBm2HZugeubmwNKDfyb9+xpvXIVe wtXVBuvFscwu6I2i/O7ItLjUrsAAia9QQs8zR50365kJ12ie3RQ/knWQcYWeuoU8bO gOVNIlwMk6X4Ie4k7X1CSa3vnZBXgoZD0zYvUaG1mqKzGyawxfUW50B11oWfnW/oDd vywsyo6MjEzmeoRZNjRWBHQ5kuPmT5GjTGSNYyPIV6JdAv14rCmCv9zEgEWEEOnlzN fXEEoS8OsPcUw== Message-ID: <7cf39b82-ff21-4d9e-b2fd-1636c3e203ce@kernel.org> Date: Thu, 9 Apr 2026 19:01:01 +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 v2] mm/vmstat: fix vmstat_shepherd double-scheduling vmstat_update Content-Language: en-US To: Breno Leitao , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Christoph Lameter , Dmitry Ilvokhin 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: <20260409-vmstat-v2-1-e9d9a6db08ad@debian.org> From: "Vlastimil Babka (SUSE)" In-Reply-To: <20260409-vmstat-v2-1-e9d9a6db08ad@debian.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 4/9/26 14:26, Breno Leitao wrote: > 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. > > After the fix, all sub-jiffy and most sub-100-jiffie gaps disappear. > The remaining early invocations have gaps in the 700–999 jiffie range, > attributable to round_jiffies_relative() aligning to a nearer > jiffie-second boundary rather than to this race. > > Each spurious vmstat_update invocation has a measurable side effect: > refresh_cpu_vm_stats() calls decay_pcp_high() for every zone, which > drains idle per-CPU pages back to the buddy allocator via > free_pcppages_bulk(), taking the zone spinlock each time. Eliminating > the double-scheduling therefore reduces zone lock contention directly. > On a 72-CPU stress-ng workload measured with perf lock contention: > > free_pcppages_bulk contention count: ~55% reduction > free_pcppages_bulk total wait time: ~57% reduction > free_pcppages_bulk max wait time: ~47% reduction > > Note: work_busy() is inherently racy—between the check and the > subsequent queue_delayed_work_on() call, vmstat_update can finish > execution, leaving the work neither pending nor running. In that > narrow window the shepherd can still queue a second invocation. > After the fix, this residual race is rare and produces only occasional > small gaps, a significant improvement over the systematic > double-scheduling seen with delayed_work_pending(). > > Fixes: 7b8da4c7f07774 ("vmstat: get rid of the ugly cpu_stat_off variable") > Signed-off-by: Breno Leitao > Reviewed-by: Vlastimil Babka (SUSE) Looks like v2 posting raced with Dmitry adding R-b tag to the posting in v1 thread: https://lore.kernel.org/all/adebKQaiAP4G3liv@shell.ilvokhin.com/ > --- > Changes in v2: > - Instead of changing the timings, do not double-schedule. > - Link to v1: https://patch.msgid.link/20260401-vmstat-v1-1-b68ce4a35055@debian.org > --- > mm/vmstat.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/vmstat.c b/mm/vmstat.c > index 2370c6fb1fcd6..cc5fdc0d0f298 100644 > --- a/mm/vmstat.c > +++ b/mm/vmstat.c > @@ -2139,7 +2139,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); > } > > > --- > base-commit: cf7c3c02fdd0dfccf4d6611714273dcb538af2cb > change-id: 20260401-vmstat-048e0feaf344 > > Best regards, > -- > Breno Leitao >