From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80BF83A783C for ; Fri, 10 Apr 2026 09:05:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775811923; cv=none; b=uaPJRXhGcZWEX0pNuzk5KidTJJ9G9uj6MjsZB49okzp/U4/9k564M3+TBVUXh7ejIW3B1PuW2klP5Q/aN/G0pha7XghiFlk++CKkBsehTtvZbttjtf3m7vfuOosl6TO+yPgdCgo2eX14WPtlZzWiK4BoiegCoKxEqaBUaQFpUaI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775811923; c=relaxed/simple; bh=1Slfpcco3MNV6PmlYHDqKwtUYtfdqFSsbqwzfQOEOdc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ro0032jjhDAaZOJ6j4C2KTtXm38RQgvTmcMINawyUpYZMUMqrU9oC5YJlqiZfCBZ5to30LEyia7vsCWh7Aw0VqrfIr/2DI7I06AHEebcjix2UOOrzGgrOmr8c+jt0qt+qxuPNJRhFIXW/TWO8p+gCIKMgtSvOP1Mq+1BS+moBEw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=f/6PnKwI; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="f/6PnKwI" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-488aa77a06eso28230135e9.0 for ; Fri, 10 Apr 2026 02:05:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1775811920; x=1776416720; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=8XEvKQiPqvwQxNRmtSVZKQ+GJktAIkI23sY2qooZ+Ow=; b=f/6PnKwIn9FmdFpXTRDs1SLsHJVq5Ibd3OTevmInkMoehuRo0qYpTYsNIBHcloRVKF fSxb0LBjBAehsbNxeYk6k1tQuD0HAk9hrsS+koJmuq6Obg6F6PpIkuJydPB3O1cA9YmG zQnQWho62MndEvla4YZ3Mtzt2INiRfm3uhNZRyn9RwRwkFQ5EZbvH/lRFBot2OL7iu5g OWDRzcWJmk74ROz2biv9YpmYOCgyzif6SXuFmYINPc8xTSkKUHCI0CCxQa+ewWWGy++L IIzOBCYEiBmu9s1YsYEoMZ7lgpz5xcfdHBgw+BFZcp22UyJvuiJLN0/H46CxzWlJFc6l he3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775811920; x=1776416720; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8XEvKQiPqvwQxNRmtSVZKQ+GJktAIkI23sY2qooZ+Ow=; b=kDxMD5Jqf8z4MU5/Aby77YEDfTBQHVqXSunKBfjqmvCMtH1E0dtL2ggAONZ61E/vLA SudGwAk+ehrNqnXX+KHh+penUQmaJlGAs7nUeUputfC3M66QSI7utrcMOSDVQdDB2lDZ YDitBJoNYzGLyKiTLgvEOY4n8F6WrgjAYHb1/d3ofE+CS/uj1Y3XlMXuUrA8PLLpEcR6 Vy4tQv7SBIG/Vlmf1d75oB2OHmhXTZ3CT1FrxvKdcd/xdAYDjjO8GQdULmNkhUUV+V17 2qUuZehBaSy06r73yjQHPFIkvXCgeyfHBSxcuyQj7r+ofnID/Lc1J36op74QxUJU63nj iSNQ== X-Forwarded-Encrypted: i=1; AJvYcCXKG+5q+sy5B5ITATMd0TiV7410WVuVa5QVGfPbez4gdqL1KaNIF6hHdxoQM73bXB28342DXgknOF+xv1Q=@vger.kernel.org X-Gm-Message-State: AOJu0YweLQJQhC5yOsFfJL13+UgMRg4aaylOOQgCr2v2yhe8hfakzEzN bQOtMmbAv2fHP4MPhbXnQKNXa5Am0/R/gq+zURN3/Yduo9GY7yQFAleTyJWv70ylmK0= X-Gm-Gg: AeBDiesbklsa36LjfqGZVMuhMfrIDrpHPpeFJEBAYJ7Q2v6i6bwFCXaNufoMv8Gbk4b ukZWmnuiVn3c8WAM0X7eCgkJ1oTgnLDLlGQxuyzdE0+xtL6a0xLVyxXljA6xdWIvAtHDf9TmbLc MalNjpAcxG+3c5UzKPwT2jQS5s8XoGyQxMpgxrp0L2TRwDsJH7aLG6StcY5c9Ara0ynu+HiB6fj IUJUdZRn5r1XEFgEHReSvUPK1LmiUBL8jj6xP9ewbqXXh3J8vdHP3F6brSQiCEo38J0xKry8pDY FcWqUEhDxw+baHb//rtnecaAF0eqZHFL0tdo+zRQ2xBp8HBEnwvTzSYQGF0PrkRZcUBVYA32wp8 Z/3Z+QR7lT/7bK8HEhImdVRA8VoMOKWAouC5Z87v0g3wP78D28TnyukGT2oOpgOtyXWEuAEdw3q DOe1b81tGbvrQKJ6zPX72vHwzGZDHSAXPZ6g== X-Received: by 2002:a05:6000:268a:b0:43d:4daa:7a89 with SMTP id ffacd0b85a97d-43d642a50a2mr3222376f8f.19.1775811919772; Fri, 10 Apr 2026 02:05:19 -0700 (PDT) Received: from localhost (109-81-92-28.rct.o2.cz. [109.81.92.28]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43d63de2a53sm6028261f8f.5.2026.04.10.02.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2026 02:05:19 -0700 (PDT) Date: Fri, 10 Apr 2026 11:05:18 +0200 From: Michal Hocko To: Breno Leitao Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , 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 09-04-26 05:26:36, 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) I like this much better. Thanks for digging deeper into this. Acked-by: Michal Hocko > --- > 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 > -- Michal Hocko SUSE Labs