[PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes
From: Johannes Weiner
Date: Fri Dec 04 2026 - 12:39:43 EST
From: Chris Mason <mason@xxxxxxxxxx>
If many child cgroups each create files on many filesystems, they can
consume several times their parent's memory.max in kernel memory that is
not charged to any memory cgroup.
A memcg-aware list_lru needs one struct list_lru_memcg per memory
cgroup, and each superblock has two such list_lrus: its dentry LRU and
its inode LRU. __memcg_list_lru_alloc() allocates the struct when the
cgroup first allocates an object for that list_lru, and stores it in the
XArray lru->xa. The struct stays until the cgroup goes offline or the
list_lru is destroyed, even if the object is freed right away. So N
child cgroups that each create a file on each of M filesystems leave
2 * N * M structs behind, plus the XArray nodes that hold them.
None of this memory is charged: __memcg_list_lru_alloc() masks
__GFP_ACCOUNT out of its gfp mask, and lru->xa lacks XA_FLAGS_ACCOUNT.
Charge the structs and the XArray nodes to the cgroup they belong to.
__memcg_list_lru_alloc() makes that cgroup the active memcg for the
duration and adds __GFP_ACCOUNT to its gfp mask, so the struct and
the nodes, which the XArray allocates itself, are billed to it rather
than to whichever task is running. This covers every list_lru,
whether the object came from kmem_cache_alloc_lru() or the caller
allocated the lists directly.
The deferred split lists are allocated right after a large folio was
charged. They now take the folio's gfp mask instead of GFP_KERNEL, so
a failed charge fails the way the folio's would, and the fault falls
back to smaller pages instead of invoking the OOM killer.
Cc: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
Cc: Dave Chinner <david@xxxxxxxxxxxxx>
Cc: Harry Yoo <harry@xxxxxxxxxx>
Cc: Hugh Dickins <hughd@xxxxxxxxxx>
Cc: Kairui Song <kasong@xxxxxxxxxxx>
Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxxxx>
Cc: Muchun Song <muchun.song@xxxxxxxxx>
Cc: Qi Zheng <qi.zheng@xxxxxxxxx>
Cc: Qinyun Tan <qinyuntan@xxxxxxxxxxxxxxxxx>
Cc: Roman Gushchin <roman.gushchin@xxxxxxxxx>
Cc: Shakeel Butt <shakeel.butt@xxxxxxxxx>
Cc: Vlastimil Babka <vbabka@xxxxxxxxxx>
Cc: Zi Yan <ziy@xxxxxxxxxx>
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
[hannes@xxxxxxxxxxx: charge all callers, pass the folio's gfp for the deferred split lists]
Signed-off-by: Johannes Weiner <hannes@xxxxxxxxxxx>
---
include/linux/huge_mm.h | 4 ++--
mm/huge_memory.c | 7 ++++---
mm/khugepaged.c | 2 +-
mm/list_lru.c | 21 ++++++++++++++-------
mm/memory.c | 2 +-
mm/migrate_device.c | 2 +-
mm/swap_state.c | 2 +-
7 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index fac05120ab77..6dd1e2d30f56 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -424,7 +424,7 @@ static inline int split_huge_page(struct page *page)
return split_huge_page_to_list_to_order(page, NULL, 0);
}
-int folio_memcg_alloc_deferred(struct folio *folio);
+int folio_memcg_alloc_deferred(struct folio *folio, gfp_t gfp);
void deferred_split_folio(struct folio *folio, bool partially_mapped);
@@ -678,7 +678,7 @@ static inline int folio_split(struct folio *folio, unsigned int new_order,
return -EINVAL;
}
-static inline int folio_memcg_alloc_deferred(struct folio *folio)
+static inline int folio_memcg_alloc_deferred(struct folio *folio, gfp_t gfp)
{
return 0;
}
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index d065b26a94df..329da86c388e 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1048,11 +1048,12 @@ static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
}
#endif /* CONFIG_SYSFS */
-int folio_memcg_alloc_deferred(struct folio *folio)
+int folio_memcg_alloc_deferred(struct folio *folio, gfp_t gfp)
{
if (mem_cgroup_disabled())
return 0;
- return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL);
+ /* The folio's gfp: a failed charge falls back the way the folio's does. */
+ return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, gfp);
}
static int __init thp_shrinker_init(void)
@@ -1366,7 +1367,7 @@ static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma,
return NULL;
}
- if (folio_memcg_alloc_deferred(folio)) {
+ if (folio_memcg_alloc_deferred(folio, gfp)) {
folio_put(folio);
count_vm_event(THP_FAULT_FALLBACK);
count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 964f78c3856b..00e4270d5ec3 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1273,7 +1273,7 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm,
if (result != SCAN_SUCCEED)
goto out_nolock;
- if (folio_memcg_alloc_deferred(folio)) {
+ if (folio_memcg_alloc_deferred(folio, cc->policy.gfp)) {
result = SCAN_ALLOC_HUGE_PAGE_FAIL;
goto out_nolock;
}
diff --git a/mm/list_lru.c b/mm/list_lru.c
index 5d4b660f9fe4..a00cd1f4ceec 100644
--- a/mm/list_lru.c
+++ b/mm/list_lru.c
@@ -481,7 +481,7 @@ static struct list_lru_memcg *memcg_init_list_lru_one(struct list_lru *lru, gfp_
static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
{
if (memcg_aware)
- xa_init_flags(&lru->xa, XA_FLAGS_LOCK_IRQ);
+ xa_init_flags(&lru->xa, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
lru->memcg_aware = memcg_aware;
}
@@ -591,10 +591,13 @@ static int __memcg_list_lru_alloc(struct mem_cgroup *memcg,
{
unsigned long flags;
struct list_lru_memcg *mlru = NULL;
- struct mem_cgroup *pos, *parent;
+ struct mem_cgroup *pos, *parent, *old_memcg;
XA_STATE(xas, &lru->xa, 0);
+ int ret;
- gfp &= GFP_RECLAIM_MASK;
+ /* Charge the heads and the XArray nodes to @memcg, not to current. */
+ gfp = (gfp & GFP_RECLAIM_MASK) | __GFP_ACCOUNT;
+ old_memcg = set_active_memcg(memcg);
/*
* Because the list_lru can be reparented to the parent cgroup's
* list_lru, we should make sure that this cgroup and all its
@@ -614,8 +617,10 @@ static int __memcg_list_lru_alloc(struct mem_cgroup *memcg,
if (!mlru) {
mlru = memcg_init_list_lru_one(lru, gfp);
- if (!mlru)
- return -ENOMEM;
+ if (!mlru) {
+ ret = -ENOMEM;
+ goto out;
+ }
}
/* Set before the lists are published, and never changed after. */
mlru->memcg = pos;
@@ -631,10 +636,12 @@ static int __memcg_list_lru_alloc(struct mem_cgroup *memcg,
} while (xas_nomem(&xas, gfp));
} while (pos != memcg && !css_is_dying(&pos->css));
+ ret = xas_error(&xas);
if (unlikely(mlru))
kfree(mlru);
-
- return xas_error(&xas);
+out:
+ set_active_memcg(old_memcg);
+ return ret;
}
int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
diff --git a/mm/memory.c b/mm/memory.c
index 7d09749c2676..74ee41255154 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5486,7 +5486,7 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
folio_put(folio);
goto next;
}
- if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+ if (order > 1 && folio_memcg_alloc_deferred(folio, gfp)) {
folio_put(folio);
goto fallback;
}
diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index b4660eeb975a..f39a27e70dff 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -843,7 +843,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
goto abort;
}
- if (folio_memcg_alloc_deferred(folio)) {
+ if (folio_memcg_alloc_deferred(folio, gfp)) {
count_vm_event(THP_FAULT_FALLBACK);
count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_FALLBACK);
ret = -ENOMEM;
diff --git a/mm/swap_state.c b/mm/swap_state.c
index d9e36c505398..9e0058fe3b5c 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -489,7 +489,7 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
return ERR_PTR(-ENOMEM);
}
- if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+ if (order > 1 && folio_memcg_alloc_deferred(folio, gfp)) {
spin_lock(&ci->lock);
__swap_cache_do_del_folio(ci, folio, entry, shadow);
spin_unlock(&ci->lock);
--
2.56.1