* [PATCH v2 1/2] mm/hugetlb_cgroup: move per-node usage on cross node migration
2026-09-18 2:56 [PATCH v2 0/2] mm/hugetlb_cgroup: move the per-node usage along with the folio Hongfu Li
@ 2026-09-18 2:56 ` Hongfu Li
2026-09-18 3:13 ` Muchun Song
2026-09-18 2:56 ` [PATCH v2 2/2] mm/hugetlb_cgroup: move per-node usage on cgroup reparenting Hongfu Li
2026-09-18 3:49 ` [PATCH v2 0/2] mm/hugetlb_cgroup: move the per-node usage along with the folio Andrew Morton
2 siblings, 1 reply; 5+ messages in thread
From: Hongfu Li @ 2026-09-18 2:56 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, David Hildenbrand, Andrew Morton,
Kees Cook, Colin Ian King, Shakeel Butt, Mina Almasry
Cc: Mike Kravetz, linux-mm, linux-kernel, hongfu.li, Hongfu Li, stable
From: Hongfu Li <lihongfu@kylinos.cn>
hugetlb.<size>.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.<size>.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().
Fixes: f47761999052 ("hugetlb: add hugetlb.*.numa_stat file")
Cc: stable@vger.kernel.org
Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
---
mm/hugetlb_cgroup.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c
index ecb6e0b7819a..7cf7c18119b4 100644
--- a/mm/hugetlb_cgroup.c
+++ b/mm/hugetlb_cgroup.c
@@ -179,6 +179,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
@@ -906,6 +934,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);
}
--
2.54.0
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2 1/2] mm/hugetlb_cgroup: move per-node usage on cross node migration
2026-09-18 2:56 ` [PATCH v2 1/2] mm/hugetlb_cgroup: move per-node usage on cross node migration Hongfu Li
@ 2026-09-18 3:13 ` Muchun Song
0 siblings, 0 replies; 5+ messages in thread
From: Muchun Song @ 2026-09-18 3:13 UTC (permalink / raw)
To: Hongfu Li
Cc: Oscar Salvador, David Hildenbrand, Andrew Morton, Kees Cook,
Colin Ian King, Shakeel Butt, Mina Almasry, Mike Kravetz,
linux-mm, linux-kernel, Hongfu Li, stable
> On Sep 18, 2026, at 10:56, Hongfu Li <hongfu.li@linux.dev> wrote:
>
> From: Hongfu Li <lihongfu@kylinos.cn>
>
> hugetlb.<size>.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.<size>.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().
>
> Fixes: f47761999052 ("hugetlb: add hugetlb.*.numa_stat file")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
Acked-by: Muchun Song <muchun.song@linux.dev>
Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] mm/hugetlb_cgroup: move per-node usage on cgroup reparenting
2026-09-18 2:56 [PATCH v2 0/2] mm/hugetlb_cgroup: move the per-node usage along with the folio Hongfu Li
2026-09-18 2:56 ` [PATCH v2 1/2] mm/hugetlb_cgroup: move per-node usage on cross node migration Hongfu Li
@ 2026-09-18 2:56 ` Hongfu Li
2026-09-18 3:49 ` [PATCH v2 0/2] mm/hugetlb_cgroup: move the per-node usage along with the folio Andrew Morton
2 siblings, 0 replies; 5+ messages in thread
From: Hongfu Li @ 2026-09-18 2:56 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, David Hildenbrand, Andrew Morton,
Kees Cook, Colin Ian King, Shakeel Butt, Mina Almasry
Cc: Mike Kravetz, linux-mm, linux-kernel, hongfu.li, Hongfu Li, stable
From: Hongfu Li <lihongfu@kylinos.cn>
hugetlb_cgroup_css_offline() hands the folios of a dying cgroup over to
its parent with hugetlb_cgroup_move_parent(), which moves the page_counter
charges and the hugetlb_cgroup pointer of the folio but not its per-node
usage. The parent's hugetlb.<size>.numa_stat is short by that usage while
they are charged, and underflows once they are freed, exposing incorrect
per-node usage values to userspace.
Move the per-node usage to the parent as well. The folios keep their node
here, so only the cgroup which holds the usage changes.
Fixes: f47761999052 ("hugetlb: add hugetlb.*.numa_stat file")
Cc: stable@vger.kernel.org
Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
Acked-by: Muchun Song <muchun.song@linux.dev>
---
mm/hugetlb_cgroup.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c
index 7cf7c18119b4..3fb41311e4c7 100644
--- a/mm/hugetlb_cgroup.c
+++ b/mm/hugetlb_cgroup.c
@@ -241,6 +241,8 @@ static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,
/* Take the pages off the local counter */
page_counter_cancel(counter, nr_pages);
+ hugetlb_cgroup_move_usage(h_cg, parent, folio, folio);
+
set_hugetlb_cgroup(folio, parent);
out:
return;
--
2.54.0
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2 0/2] mm/hugetlb_cgroup: move the per-node usage along with the folio
2026-09-18 2:56 [PATCH v2 0/2] mm/hugetlb_cgroup: move the per-node usage along with the folio Hongfu Li
2026-09-18 2:56 ` [PATCH v2 1/2] mm/hugetlb_cgroup: move per-node usage on cross node migration Hongfu Li
2026-09-18 2:56 ` [PATCH v2 2/2] mm/hugetlb_cgroup: move per-node usage on cgroup reparenting Hongfu Li
@ 2026-09-18 3:49 ` Andrew Morton
2 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-09-18 3:49 UTC (permalink / raw)
To: Hongfu Li
Cc: Muchun Song, Oscar Salvador, David Hildenbrand, Kees Cook,
Colin Ian King, Shakeel Butt, Mina Almasry, Mike Kravetz,
linux-mm, linux-kernel, Hongfu Li, stable
On Fri, 18 Sep 2026 10:56:09 +0800 Hongfu Li <hongfu.li@linux.dev> wrote:
> The per-node usage reported by hugetlb.<size>.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.
Thanks.
> Fixes: f47761999052 ("hugetlb: add hugetlb.*.numa_stat file")
> Cc: stable@vger.kernel.org
I'll queue these for the next merge window, with cc:stable. I don't
see a hurry to fix a four year old bug.
^ permalink raw reply [flat|nested] 5+ messages in thread