From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ilvokhin.com (mail.ilvokhin.com [178.62.254.231]) (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 32E2A386561 for ; Fri, 10 Apr 2026 10:43:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.62.254.231 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775817799; cv=none; b=hKFFzNTKv5kKFdhx0hSRQskmjKWR+VFyEADEGGfaiHUaNnOgLleGqgZAsv7LqNvtVDe2VBtCuyLuXSFahdE7PagBtnZpHWbeFHSZWfiJsJ8e+AU5jzt8j6WlY8vSS/G6GPNzgOeDS7VfOuqV6TdufkVTyG+xkN4OqIlj2O30taA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775817799; c=relaxed/simple; bh=VBA4+Nuiv6KZKSkszPkm8AfUqvu7dY5Za9+yBUn3gtk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=GRfvj3lfgbylFljVcvbrrfBEzJnzqkIjZfXtwN4XwYgqeAu/KAau9PqrEFqq9VurY20BDUbAF1eHXmGAc6h32oXDhduQmhRMWTAHotalzF4XY24435iPoxjPFA7iRwim1TqSvk8U/pBccwEOF5faOZlUpag71H2LyU+lXIaU6PM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ilvokhin.com; spf=pass smtp.mailfrom=ilvokhin.com; dkim=pass (1024-bit key) header.d=ilvokhin.com header.i=@ilvokhin.com header.b=TLF2iwZ9; arc=none smtp.client-ip=178.62.254.231 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ilvokhin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ilvokhin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ilvokhin.com header.i=@ilvokhin.com header.b="TLF2iwZ9" Received: from shell.ilvokhin.com (shell.ilvokhin.com [138.68.190.75]) (Authenticated sender: d@ilvokhin.com) by mail.ilvokhin.com (Postfix) with ESMTPSA id 296BEBE886; Fri, 10 Apr 2026 10:43:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilvokhin.com; s=mail; t=1775817789; bh=CSG2xsdDRphI9FEycXoH8kdn7Dk+kPIVj1MEg4OQrU8=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=TLF2iwZ9keVMUtLfNwChan/MK4VXF2b0Q70mM4jIFeW/sl/58NbHVD5+E4dVQDKiV aT7DykZBxO/UPFFoLChIZcqb2ghX9uVWRam0kXqx4OFLrQjHVOOxlLlZRUh50nZVC6 ChtPBvGi/9VAAf2MjVDhYcTC3JIGCMoCiLC53Gg4= Date: Fri, 10 Apr 2026 10:43:05 +0000 From: Dmitry Ilvokhin To: Breno Leitao Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Christoph Lameter , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kas@kernel.org, shakeel.butt@linux.dev, usama.arif@linux.dev, kernel-team@meta.com Subject: Re: [PATCH v2] mm/vmstat: fix vmstat_shepherd double-scheduling vmstat_update Message-ID: References: <20260409-vmstat-v2-1-e9d9a6db08ad@debian.org> 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: <20260409-vmstat-v2-1-e9d9a6db08ad@debian.org> On Thu, Apr 09, 2026 at 05:26:36AM -0700, 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) Reviewed-by: Dmitry Ilvokhin