From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796317382; b=Y9csLLgF6jxaG+poZtl/YX7GSKQaQzfBvAHmhpbOB5SA+vL2Qlmg+Zd2A9pE9PMypT+J JzkpDM+gHqC6UVqVe77Fi92XkCDPvJFv2kum3K/QcVuxyMhplMdxoF3eI5qqK+uvS95Z4 d3kNNKAbhRE1OaYiJLoWtuoRaGJZjOlKDFuOx31kxEdql5kSUeNrN75GuQZy9qDg7woSy KpE63OfbV4tNPJCqI3Up9bXkf5mjeEI3FKvsM6LwFC5VpW/SuJjb424p9xKHh2rnbdQlU Shf03/XrWE3jDxOPczMJrTNVKXfmDi2htFP45PrjeTc/JPhrcamUB6vSFS4iGxEUBHg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796317382; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=5YH+J/jb0lm8KmIhhkTBWLKhsgYVw6dPO+2dfk+maxY=; b=Rv8K8pNfgqsxYzZgTKU2qqHD5/+aFi5os9xLTj4+SnuQMcPehxVYND8lanYtPmBi6iu3 40NsAdPRpopi/z6WOI1AKgfzieh71hJppCIYiOCNJ7F6Bj0c6aH7C3w9DYdm4bup+PKf8 LtvbH2HS24tV2ngx9T3RnQrfJ4qPM/n2IATIb43lTJ5cZYYp2nhsevAJlQYucHJXkCx40 9yrbTRb321ibh8zhIueEQw5QdKn+bCiZSYa+K2ea4kIPJqSqQhH0FgDuMU62zN5rrA5Cw 6E70GCWv6OLOoMyf/Vds8THZVY89SPDXs+MU+ch+z7W9iJVLqtE5Wq/9rdNIo2kuoTA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SlCnhvdv; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30C31606A338; Thu, 3 Dec 2026 17:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=SlCnhvdv Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796317381; b=MMvVPXALWwODMhODgTbcxFoCqAKsqPyuMkE4nYeFO1E8xzRD9bEqmkJmFvQRKb7redv8 kfEqXFdUd1Hs8SkwDLFs5HzNkjt4EYieRTsqhfzfXY/RWame4VuvLcqFCifEDCdTTz7TM jPoNBi8mmvoNgh5cPbGPRKWwC2a//7bUrSQtdRQlIlcp7vUFQae+s3REHxKHpPtuWWnL+ LUbdKjq9+YaQ3RiVgIRsVfW9TmZ9sZHqiKZ2bxVgyio7BagiH5nOJ2yluXuWgkZWwCjyX 1bCs7PmhAGg/lQ0SQhi07P/X9CItBV+cnNoUmfzIkV8weqYQiUeG4Tb5d6zIEgnZfPA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796317381; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=5YH+J/jb0lm8KmIhhkTBWLKhsgYVw6dPO+2dfk+maxY=; b=BMT6P4f182cEX2YUM4rwD4fDuA8w7XP86oK5uOXTB7HQD3qhX1MzBPKjxe4LVtfKnkIA 0g8t6nNmnUSNZD2RKJyI7FusRD3IVfuX8dgxyzJUKUFbtneKBDrxUTNYB1u4ilKPitYhl q5eSIMMQ6e50eebbJCf58h+QvhQ3gtPyLnhNcJ2Lox/J2F+GMGryjMOwGlc7DSGUo/i9a WeI68Y3PznafUdDn/uacqnPZH3jUoO/146HH5+CPxYi38XDOPDse9y02Aw238zO2a1KiU 0PUhBrshMYarI++0veW/Q6MC0QlJGToFn4CkhVu70gIrUrwYTXJcr56NZa9QEvWp0Kw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id DF1B21F000C1; Thu, 3 Dec 2026 17:02:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796317381; bh=5YH+J/jb0lm8KmIhhkTBWLKhsgYVw6dPO+2dfk+maxY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=SlCnhvdvAoioV9Yqs4b27Hath9ws7cemShnyGgHUqIMm5XJkeL6HV0LHdqey5bwHF PX0u0KObqREmdsIaI5dpv6RB56pSusiU23qFS+rF3X4qJGR5HJsWyiQwtz7+IHUsRY hFxfkaF2dCRgWVMq658kLyfMhEPN0oeJNoyCfbIu/SuTzfl6DruxCBrycGzMddmumP vAW7N7LrcehuO17rkZ8KKvPB+u5A2eZaDCuGPOXXzWWW5go8KDIs0tj76DPcu2/IZQ YpJPz+w5rNezCYRJXrZ6HDSL6fH/uBTGb53ykEmoaHSL63h6aYW7RlrWS+CTev33Hz J5PUmvhi1M7Pg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 5AC3D4FA3F; Thu, 3 Dec 2026 17:02:50 +0000 (UTC) From: Kairui Song via B4 Relay Date: Fri, 04 Dec 2026 01:02:33 +0800 Subject: [PATCH v8 02/19] mm/memcontrol: avoid potential counter leak when reparenting local stats X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261204-mglru-fg-v8-2-64f81c876c15@tencent.com> References: <20261204-mglru-fg-v8-0-64f81c876c15@tencent.com> In-Reply-To: <20261204-mglru-fg-v8-0-64f81c876c15@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Johannes Weiner , Muchun Song , Qi Zheng , Ying Huang , Chris Li , Baoquan He , Nico Pache , Usama Arif , Michal Hocko , Roman Gushchin , Shakeel Butt , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SJ Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Tal Zussman , Kairui Song , Xiang Liu , Ehab Ababneh , Zhao Zhengzhuo , Lian Wang , Joanne Koong , Kunwu Chan , Hui Zhu , Maxim Muzafarov , Tianyi Chen , Aveline Noir , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Kairui Song , Ridong Chen , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796317365; l=3543; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=HhIgm7kbmg392KUqchNv7aRfD7tIM1GUt4SiBxG7mHY=; b=M6d63W/ch4Ptps0Y+TVuc6XwvZbUxCH9WqV8DsKAnuFq+M3zTs/YYNrvUkXqBHuPte8EYV1Jp yIk5nI9qwznBVhfURTl+sXit5QKgyZzmSk4dJd3PG2gOKbRg3gmqBLp X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com Content-Transfer-Encoding: 7bit From: Kairui Song lruvec_page_state_local() clamps negative values to zero on SMP. That is right for readers reporting page counts, but can be wrong for reparenting once MGLRU updates the counters without the lruvec lock. Reparenting runs after the forced stats flush, so a negative is a real balance that has to move to the parent, not be discarded. We will introduce lockless MGLRU promotion which may cause following problem: 1. A refs CAS on a folio on the child's lruvec moves it from inactive to active, and its active/inactive counter update follows as a separate step. 2. A concurrent isolation of the folio reads the folio as active, debits the child's NR_ACTIVE_* by delta before CSS_DYING is visible to it. 3. The child is killed, and the two __update_lru_size() calls from step 1 land on the parent: NR_INACTIVE_* -delta, then NR_ACTIVE_* +delta. The child is left with NR_ACTIVE_* -delta while the parent holds the matching +delta, and reparenting the child's raw value is what cancels the parent's credit. However, if the child happens to hold few enough folios for that counter to be negative, lruvec_page_state_local() clamps the child's negative to zero, which drops the debit instead, so the parent keeps NR_ACTIVE_* overcounted by delta, and its active + inactive total overcounted as well, permanently: the child's local counters are gone once reparenting is done. So use a raw signed helper and reparent with that. Ordinary readers still keep the clamp. Reported-by: Kunwu Chan Closes: https://lore.kernel.org/linux-mm/CAN_Opa-+nb0vxQ+HsENCG2fEEMZoxG9Sy_DBC4CLkWjNLzDWKw@mail.gmail.com/ Reviewed-by: Kunwu Chan Reviewed-by: SJ Park Reviewed-by: Ridong Chen Signed-off-by: Kairui Song --- mm/memcontrol.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 475201bf762e..b75a61ddeded 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -569,11 +569,10 @@ unsigned long lruvec_page_state_monotonic(const struct lruvec *lruvec, return (unsigned long)READ_ONCE(pn->lruvec_stats->state[i]); } -unsigned long lruvec_page_state_local(const struct lruvec *lruvec, +static long __lruvec_page_state_local(const struct lruvec *lruvec, enum node_stat_item idx) { const struct mem_cgroup_per_node *pn; - long x; int i; if (mem_cgroup_disabled()) @@ -584,7 +583,14 @@ unsigned long lruvec_page_state_local(const struct lruvec *lruvec, return 0; pn = container_of_const(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats->state_local[i]); + return READ_ONCE(pn->lruvec_stats->state_local[i]); +} + +unsigned long lruvec_page_state_local(const struct lruvec *lruvec, + enum node_stat_item idx) +{ + long x = __lruvec_page_state_local(lruvec, idx); + #ifdef CONFIG_SMP if (x < 0) x = 0; @@ -602,7 +608,8 @@ void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg, for_each_node(nid) { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); - unsigned long value = lruvec_page_state_local(lruvec, idx); + /* Reparent the raw value: the clamp drops the child's debit. */ + long value = __lruvec_page_state_local(lruvec, idx); __mod_memcg_lruvec_state(memcg, nid, idx, -value); __mod_memcg_lruvec_state(parent, nid, idx, value); -- 2.56.1