From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 D914536829B for ; Wed, 15 Jul 2026 14:10:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784124625; cv=none; b=qIxR+PObGgWk/0PYQ63v/Ty8rTRnVC3QSzKQamqmAMhAqBMLyq+hAcNvlXr/KLc7jJCvrEBNJ+imRdK2bAIY1gMo4uBhENSIIO4p2Sl09TQQ16gUk45gIBwb0ruFKFIPsp6G3UCJhzaPVFrAVUhOZVxvzPnP+5B+phHkQFIJYuQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784124625; c=relaxed/simple; bh=hc8yMDX9dOnwEROGH/nbWb/48BForaOXcHScoSyTMwI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DEg8HZLBIo0LiM4MUElHavCXEWCDDUuNpjJ3hzaVMyQbwa9/dtSyy1IFjBdcFUgwhLP3pyBXVXUX0f+DjcQdeivwZb0piVSxKJt5q6SE3Nl0XjMrmDrUi3XWIonfS6NxQ4elx8EsyTn3BZuR2WcXnPMvNGbdidFJdJLf++Z/Ed0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b=cnEFkVeQ; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b="cnEFkVeQ" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-493e497643fso10538065e9.0 for ; Wed, 15 Jul 2026 07:10:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1784124618; x=1784729418; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=pIsfZleXeVRVmzFEqb2XavBzUBRe+/S77RcLpiiBE+s=; b=cnEFkVeQh40WkpFOyO8gGgcQJ8QaxhIKVHKQcPJ2+h/lUkPQVr89UsRe+DYnqB8bpF bGSx0/PCIRbI+M8ChEzL77mPdbRGqtMU1e92w9zhoU7lM10jxOvLa0m1N1zu6nnCpN3w HCNynbx8460WojiyOqFkEYpD8X14+744RSqGQyMRI/rLE/TiRzsReXzkYkugF8Mrrvug NIs3jjhSLT/Q/owIZAMVYdUoeHFnrIuXOAQGdUmsEs4G/k3zt52cpfWNuUD+9mYwrNeg aWE9Ql8IBOG9gpcfofpd2y7V2HzafkVMXXGE2CVFfpJmCENhjP0uePAMQDd7aiOtiBby WR3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784124618; x=1784729418; h=in-reply-to:content-disposition:content-type: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 :content-type; bh=pIsfZleXeVRVmzFEqb2XavBzUBRe+/S77RcLpiiBE+s=; b=DpzIvznDIyIGDOrR2MQjqf20pxuzt8LeTTW+E1oDIWcE4DIQUxj1vK3xDbfvEv8Y/B yO5Na5FR0DTBQdKfN5y122zakXc+vLlvuwRLGMEZAprwyD7oASn3fM98f59B7sVbUDD3 Jd3TOo8SgbEWoYedoCdpMvDRJSj4ffBI0rHMYpE5Xd4aaxqaAndO7rfI6x+gczlTcQs1 mF6GkehCc4tiBX65ZsQyWPhcJSrho5i9B/ommjvVjLX5uo76fHSh94UtJ5W+m6RWVZSx kCYakUongPDKMrESSJjNR4M0FdGxEIvm3WsvlPkicvDItk1O8xruIKZXwggHdZPYy3Ed YFUA== X-Forwarded-Encrypted: i=1; AHgh+RoSzU1P6BWDInOvh1eFtVFdhhOqB7SqeJyPV71VqeDHkwBqJ8uCT26tAXp6IlZu1xMTREzyxI8ibKqhoC4=@vger.kernel.org X-Gm-Message-State: AOJu0YzuFbzUwZLyvov610gF0pdCkuMM/Wztxrm/d33C69kcbaZz9ecQ 3Fd14fb22vQSHWane0novTCrzKJcxrIGpNF+rPO2GRCWF853IGJON6222ZLJ06c3JT4= X-Gm-Gg: AfdE7clTrvB3KGmAHFwTGiARvEs0xYX2QRFJ+mTijsBcWmq9u0Jma0U3di1mRQZqssU wcYYNg6An62FPj9i6qmI7LHQjvveGnhiS/hjjeLlbE2NzkL+1DFn7X2IZaa1b7EFNyFZxg69tku TPi/EpPdCmlUxoQmtrin0xTS5usy1J4h1Mq2hBAsW74pLvR4RdVb0XD5uovCO9P6Rot6lbfv0NO selZFvOTbSYu87C6J5M8Swrw075uqMcUmwZHhCJKkv577CdXMUwl6i3hqd4wdxb6AFweEblvTaa tIA8s7uqVf1J2LzlTPCrdfuP6+/aIHC2sfHFJrIbXOh7fEt/P4tbqQK2d9/dNCOY1zbbda1fYXS XvF1RiVqK6Fxn7jsFWQi+JR+vhWMpkaBiLcGMAm9+w+6LczBBer5ze/ewCqElNXenX12XBBPQgn WtwD96AzaWBoK/Mw== X-Received: by 2002:a05:600c:e548:10b0:493:b877:fbc with SMTP id 5b1f17b1804b1-493f881d146mr126677095e9.21.1784124617927; Wed, 15 Jul 2026 07:10:17 -0700 (PDT) Received: from localhost ([2a02:8071:8280:d6e0:1353:8eb8:c84a:b6d4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4953f643acdsm24454195e9.3.2026.07.15.07.10.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2026 07:10:17 -0700 (PDT) Date: Wed, 15 Jul 2026 16:10:15 +0200 From: Johannes Weiner To: Usama Arif Cc: Andrew Morton , david@kernel.org, ljs@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, kasong@tencent.com, qi.zheng@linux.dev, shakeel.butt@linux.dev, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, chrisl@kernel.org, nphamcs@gmail.com, baoquan.he@linux.dev, youngjun.park@lge.com, roman.gushchin@linux.dev, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, rientjes@google.com, kernel-team@meta.com Subject: Re: [PATCH v2 1/2] mm/vmstat, mm/memcontrol: add _monotonic vmstat readers Message-ID: <20260715141015.GP276793@cmpxchg.org> References: <20260713163443.3562378-1-usama.arif@linux.dev> <20260713163443.3562378-2-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=us-ascii Content-Disposition: inline In-Reply-To: <20260713163443.3562378-2-usama.arif@linux.dev> On Mon, Jul 13, 2026 at 09:34:16AM -0700, Usama Arif wrote: > lruvec_page_state(), node_page_state(), and global_node_page_state() > all clamp negative reads to zero on CONFIG_SMP so that a transient > per-CPU delta skew presents as zero pages rather than > as a garbage unsigned value. This is the right behaviour for > non-monotonic page-count readers. > > It is however incorrect for callers that snapshot a monotonically- > incremented event counter and compute a delta from two samples. > Once the underlying signed long wraps past LONG_MAX, the clamped read > drops to zero while the previously-recorded snapshot still holds the > pre-wrap value; the unsigned subtraction then underflows into a > ~2^31 spurious delta for 32-bit architecture and corrupts the > caller's accumulator. > > Add non-clamping siblings that return the underlying state value > cast to unsigned long: > > global_node_page_state_monotonic() > node_page_state_monotonic() > lruvec_page_state_monotonic() > > With both samples read via the _monotonic variant, unsigned modular > subtraction stays correct across a signed-long wraparound as long > as the true growth between two samples fits in unsigned long > (< 2^32 on 32-bit, < 2^64 on 64-bit); the 32-bit bound is the > practically-reachable one that motivates this helper. > > The variants are only safe for monotonically-incremented counters. > Non-monotonic page-count readers must keep using the existing > clamped helpers so transient negative reads still present as zero. > > This is a prerequisite for the following patch which > replaces the producer-side anon_cost/file_cost accumulators with a > read-side accumulator in prepare_scan_control() that samples > monotonic per-LRU vmstat counters (PGROTATE_*, PGRECLAIM_PAGEOUT_*, > WORKINGSET_RESTORE_*) via lruvec_page_state_monotonic() and folds > the unsigned modular delta into a per-lruvec cost_accum[]. > > Signed-off-by: Usama Arif Acked-by: Johannes Weiner It's unfortunate that we mix state counts with monotonic events in node_stat_item. We have vm_event_state for monotonics, but they aren't tracked per-node (which we need here and for other places in vmscan), and they can't easily be made so because there are "global" events in there that don't easily map to a specific node. So this seems like the best solution for now.