Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794062352; b=pU2L5hNB8kbaJlDDAEDWjXHBJO99K9XW/Q+lgprq2LTgaEf0VMona/LdYXv92VdOHvl1 eftIJr/6TLmbe/X2Y0/h/ghyyQ/NTdcrl7sgxcTWjPK+03dMkoniuMN3q0Yb0fmh3BveW RvKnjjvev/uR/AGNEFzJRQ5V5o0AqqoblkqaHD2eEmPBbrhf7izQTzrJa9EAnCY2j4kYe tA3qYmLUO4gcnjPKRtIDzUQjPiBwu789AJoBCu0MzOx2LQIiyhDWEFxNL05a6teAgbZx3 aQztba0HC35IOYM2HoNk5DXq0WomooOAUtSS6Gte+yPz6JoZrNvBhpbOzXRq1ZWtN0Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794062352; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=jv26lVWFKUsN8x8HBzoRHv3zkXtKS7agpgkybqkGEPc=; b=cI1M0ajqmMSleQfD2oDZyHIRlMIEb7N+WrYRiuGGh3pgIaFTFUE/VYj5VViGiNmRCsm3 q0uFJtB3SEgJ2zGDM049NNV12i82dTBW1G9hT4+4YBFkuAhDRsGJg5uByWRbhhPOO4Ej7 OI7F6nvMtDZkahaMDQrX1RJDXCY01zgNix20rPcbYsykiG24jfFU5uc2C4XF/BHi171r/ QKSz41eKiiWRNtC8Qf/Ht3FBMNuaXplByJkVwEwysgKB6SOMz6uQSGEFKIwfVFwEWCVA5 bn+LbWRXJyX9msJukgawduFoI1312BuIaYf4J0+3waWaYXekyMSxeMA7q8KG5PUD49A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=frEIBTcu; arc=none 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 3D1B5606D82D; Sat, 7 Nov 2026 14:39:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=frEIBTcu Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD7561F0089F; Sat, 7 Nov 2026 14:39:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794062352; bh=jv26lVWFKUsN8x8HBzoRHv3zkXtKS7agpgkybqkGEPc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=frEIBTcuf5S6yWiK+h2LcdNpySgVWz9Z3pPm0ZYxgsKm7DH+PmdDjqmmdmj7GGC6V W59r1Lz1RDXUsf0+AodJBKqzC5hRXP00ko7qsXMHWU3HJ/uiX1JJ8yjA4hFvnqQ9Zu bvxbTwZK0rRrcLRYmvbBiSRmFWt4hDDtpySeY+/I= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Hongfu Li , Andrew Morton , Muchun Song , Colin Ian King , David Hildenbrand , Kees Cook , Mina Almasry , Oscar Salvador , Shakeel Butt , Sasha Levin Subject: [PATCH 6.12 1241/1536] mm/hugetlb_cgroup: move per-node usage on cross node migration Date: Sat, 7 Nov 2026 08:54:17 -0500 Message-ID: <20261107135032.616179938@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261107134957.653319350@linuxfoundation.org> References: <20261107134957.653319350@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Hongfu Li [ Upstream commit fe0ba1909c59660f078560f5130e7af7c6004f12 ] Patch series "mm/hugetlb_cgroup: move the per-node usage along with the folio", v2. The per-node usage reported by hugetlb..numa_stat is accounted against folio_nid() in __hugetlb_cgroup_commit_charge() and __hugetlb_cgroup_uncharge_folio(), so it is only correct while a folio stays charged on the same node and in the same hugetlb_cgroup. Two paths move a folio which stays charged, and neither moves the usage with it. hugetlb_cgroup_migrate() only moves the hugetlb_cgroup pointers of a folio migrated to another node, and hugetlb_cgroup_move_parent() only moves the page_counter charges and the hugetlb_cgroup pointer of the folios of a dying cgroup. In both cases the node (or cgroup) which was charged keeps a usage which never goes away, while the node (or cgroup) which ends up uncharging the folio underflows as soon as the folio is freed. Patch 1/2 moves the usage along with the folio on cross node migration, patch 2/2 does the same for the folios a dying cgroup reparents. This patch (of 2): hugetlb..numa_stat uses folio_nid() to account usage in __hugetlb_cgroup_commit_charge() and __hugetlb_cgroup_uncharge_folio(). hugetlb_cgroup_migrate() only moves hugetlb_cgroup pointers, leaving per-node usage behind on the source node during cross-node migration. When the migrated folio gets uncharged, we subtract usage from the destination node counter. This creates stale usage on the source node and unsigned long counter underflow on the destination node. The hugetlb..numa_stat interface exposes these incorrect per-node usage values to userspace. Add a hugetlb_cgroup_move_usage() helper which moves the usage from the old node to the new node, and call it from hugetlb_cgroup_migrate(). Link: https://lore.kernel.org/20260918-for-hugetlb-charge-v2-0-2b6d8c2bdc36@kylinos.cn Link: https://lore.kernel.org/20260918-for-hugetlb-charge-v2-1-2b6d8c2bdc36@kylinos.cn Fixes: f47761999052 ("hugetlb: add hugetlb.*.numa_stat file") Signed-off-by: Hongfu Li Signed-off-by: Andrew Morton Acked-by: Muchun Song Cc: Colin Ian King Cc: David Hildenbrand Cc: Kees Cook Cc: Mina Almasry Cc: Oscar Salvador Cc: Shakeel Butt Cc: Signed-off-by: Sasha Levin --- mm/hugetlb_cgroup.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c index e716c4671a159..c592cba90f464 100644 --- a/mm/hugetlb_cgroup.c +++ b/mm/hugetlb_cgroup.c @@ -187,6 +187,34 @@ static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css) hugetlb_cgroup_free(hugetlb_cgroup_from_css(css)); } +static void hugetlb_cgroup_move_usage(struct hugetlb_cgroup *from, + struct hugetlb_cgroup *to, + struct folio *from_folio, + struct folio *to_folio) +{ + int idx = hstate_index(folio_hstate(from_folio)); + unsigned long nr_pages = folio_nr_pages(from_folio); + int from_nid = folio_nid(from_folio); + int to_nid = folio_nid(to_folio); + unsigned long usage; + + lockdep_assert_held(&hugetlb_lock); + + if (!from || !to) + return; + + if (from == to && from_nid == to_nid) + return; + + usage = from->nodeinfo[from_nid]->usage[idx]; + if (WARN_ON_ONCE(usage < nr_pages)) + return; + WRITE_ONCE(from->nodeinfo[from_nid]->usage[idx], usage - nr_pages); + + usage = to->nodeinfo[to_nid]->usage[idx]; + WRITE_ONCE(to->nodeinfo[to_nid]->usage[idx], usage + nr_pages); +} + /* * Should be called with hugetlb_lock held. * Since we are holding hugetlb_lock, pages cannot get moved from @@ -915,6 +943,9 @@ void hugetlb_cgroup_migrate(struct folio *old_folio, struct folio *new_folio) /* move the h_cg details to new cgroup */ set_hugetlb_cgroup(new_folio, h_cg); set_hugetlb_cgroup_rsvd(new_folio, h_cg_rsvd); + + hugetlb_cgroup_move_usage(h_cg, h_cg, old_folio, new_folio); + list_move(&new_folio->lru, &h->hugepage_activelist); spin_unlock_irq(&hugetlb_lock); return; -- 2.53.0