* [PATCH 1/2] mm: list_lru: fix XArray node leak at destroy
@ 2026-12-04 17:39 Johannes Weiner
2026-12-04 17:39 ` [PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes Johannes Weiner
0 siblings, 1 reply; 3+ messages in thread
From: Johannes Weiner @ 2026-12-04 17:39 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Mason, David Hildenbrand, Michal Hocko, Roman Gushchin,
Shakeel Butt, linux-mm, cgroups, linux-kernel, Dave Chinner,
Kairui Song, Muchun Song, Qi Zheng
From: Chris Mason <mason@kernel.org>
memcg_destroy_list_lru() tears down lru->xa by erasing its entries,
and relies on the XArray to free each node once its last entry is
gone. That is not how an XArray is torn down. A node can exist
without entries, and nothing frees that one; it also keeps the count
of its parent up, so the parent and everything above it stay
allocated as well. The API has xa_destroy() for this: free the
entries, then call it to free the nodes.
Empty nodes come from a store that gave up partway. xas_create()
builds the path to a new index one level at a time, and links each
node into the tree before it allocates the next. If that allocation
fails, the linked node stays. This is by design: once xas_nomem()
has refilled the cache, the retry descends into the node and
completes the path, and later stores reuse it too. But if the retry
cannot allocate either, the store returns -ENOMEM and the node stays
empty. __memcg_list_lru_alloc() gets there with GFP_NOWAIT when the
page cache allocates a node under memory pressure.
The leak is small, a few nodes per list_lru, and shows at umount.
Fix it by calling xa_destroy() once the entries are freed.
Fixes: bbca91cca9a9 ("mm: list_lru: replace linear array with xarray")
Cc: Dave Chinner <david@fromorbit.com>
Cc: Kairui Song <kasong@tencent.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Qi Zheng <qi.zheng@linux.dev>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
---
mm/list_lru.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/list_lru.c b/mm/list_lru.c
index 651983d0bf8b..5d4b660f9fe4 100644
--- a/mm/list_lru.c
+++ b/mm/list_lru.c
@@ -499,6 +499,9 @@ static void memcg_destroy_list_lru(struct list_lru *lru)
xas_store(&xas, NULL);
}
xas_unlock_irq(&xas);
+
+ /* Erasing the entries leaves nodes that never held one; free those too. */
+ xa_destroy(&lru->xa);
}
static void memcg_reparent_list_lru_one(struct list_lru *lru, int nid,
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes
2026-12-04 17:39 [PATCH 1/2] mm: list_lru: fix XArray node leak at destroy Johannes Weiner
@ 2026-12-04 17:39 ` Johannes Weiner
2026-12-04 20:33 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Johannes Weiner @ 2026-12-04 17:39 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Mason, David Hildenbrand, Michal Hocko, Roman Gushchin,
Shakeel Butt, linux-mm, cgroups, linux-kernel, Baolin Wang,
Dave Chinner, Harry Yoo, Hugh Dickins, Kairui Song,
Matthew Wilcox, Muchun Song, Qi Zheng, Qinyun Tan,
Vlastimil Babka, Zi Yan
From: Chris Mason <mason@kernel.org>
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@linux.alibaba.com>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Harry Yoo <harry@kernel.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Kairui Song <kasong@tencent.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Qi Zheng <qi.zheng@linux.dev>
Cc: Qinyun Tan <qinyuntan@linux.alibaba.com>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
[hannes@cmpxchg.org: charge all callers, pass the folio's gfp for the deferred split lists]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
---
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes
2026-12-04 17:39 ` [PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes Johannes Weiner
@ 2026-12-04 20:33 ` sashiko-bot
0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-04 20:33 UTC (permalink / raw)
To: Johannes Weiner, Andrew Morton
Cc: Chris Mason, David Hildenbrand, Michal Hocko, Roman Gushchin,
Shakeel Butt, linux-mm, Baolin Wang, Dave Chinner, Harry Yoo,
Hugh Dickins, Kairui Song, Matthew Wilcox, Muchun Song, Qi Zheng,
Qinyun Tan, Vlastimil Babka, Zi Yan, tj, mkoutny
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Critical] mm: list_lru: infinite loop in __memcg_list_lru_alloc() on
XArray -ENOMEM
- [High] mm: huge_memory: folio_memcg_alloc_deferred() still triggers
memcg OOM
- [Medium] mm: shmem_unused_huge_add() can trigger memcg OOM after
charging list_lru
- [Medium] mm: zswap_store() GFP_KERNEL list_lru charge can deadlock on
oom_lock
--
commit 6f3d882240ba7c4819b5952c2c4c0480a5d5931b
Author: Chris Mason <mason@kernel.org>
mm: list_lru: charge per-memcg lists and XArray nodes
This commit charges per-memcg list_lru_memcg allocations and lru->xa
XArray nodes to the target memcg, and passes the folio's gfp mask into
folio_memcg_alloc_deferred() instead of GFP_KERNEL.
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index d065b26a94dfa..329da86c388ef 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);
> }
[Severity: High]
Can folio_memcg_alloc_deferred() still invoke the memcg OOM killer when
called with GFP_TRANSHUGE?
GFP_TRANSHUGE includes __GFP_DIRECT_RECLAIM | __GFP_IO | __GFP_FS and sets
neither __GFP_NORETRY nor __GFP_RETRY_MAYFAIL. A large folio charge with
GFP_TRANSHUGE (for example from collapse_huge_page() or an MADV_HUGEPAGE
fault) avoids mem_cgroup_oom() only because its order exceeds
PAGE_ALLOC_COSTLY_ORDER.
Here, __memcg_list_lru_alloc() charges order-0 slab and XArray nodes, so
that order check does not apply. If the just-charged folio pushed the
cgroup to memory.max, this can OOM-kill a task in the cgroup (even from
khugepaged) instead of falling back. Adding __GFP_NORETRY avoids this.
[ ... ]
> diff --git a/mm/list_lru.c b/mm/list_lru.c
> index 5d4b660f9fe42..a00cd1f4ceeca 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);
[Severity: Medium]
Does charging list_lru allocations here also allow shmem_unused_huge_add()
to trigger a memcg OOM kill?
In shmem_get_folio_gfp(), right after charging a large folio with a tail
past i_size, shmem_unused_huge_add() passes the base GFP_HIGHUSER_MOVABLE
mask into folio_memcg_list_lru_alloc() while holding folio_lock(folio).
Because shmem_unused_huge_add() treats shrinklist allocation failure as
non-fatal, hitting memory.max on this order-0 charge can spuriously
OOM-kill a task in the cgroup. Adding __GFP_NORETRY in
shmem_unused_huge_alloc_lru() would avoid invoking the OOM killer.
[Severity: Medium]
Can zswap_store()'s GFP_KERNEL call to memcg_list_lru_alloc() now
self-deadlock on oom_lock?
During a global OOM (or SysRq-f via moom_callback()), out_of_memory()
runs under oom_lock without PF_MEMALLOC and invokes the i915 OOM
notifier:
__alloc_pages_may_oom() (holds oom_lock, no PF_MEMALLOC)
out_of_memory()
i915_gem_shrinker_oom()
... -> __shmem_writeback() (holds folio_lock)
shmem_write_folio() -> swap_writeout() -> zswap_store()
memcg_list_lru_alloc(..., &zswap_list_lru, GFP_KERNEL)
try_charge_memcg()
mem_cgroup_out_of_memory()
mutex_lock_killable(&oom_lock) <-- self-deadlock
Because PF_MEMALLOC is clear, try_charge_memcg() enters reclaim and OOM
when the folio's memcg is at its limit; from moom_callback() (an
unkillable kworker), oom_lock deadlocks permanently. Passing
GFP_NOWAIT or __GFP_NORETRY in zswap_store() falls back to swap writeout.
[ ... ]
> @@ -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;
> }
[Severity: Critical]
Will this outer loop spin forever if xas_nomem() fails with -ENOMEM while
populating an ancestor cgroup?
In a nested hierarchy (root -> A -> B) where ancestor A is not yet in
lru->xa and B is at memory.max, memcg_init_list_lru_one() can succeed
while xas_store() and xas_nomem() fail to charge a radix_tree_node with
GFP_NOWAIT (e.g. __filemap_add_folio() -> xas_store() for shadow_nodes).
Because the outer while condition omits !xas_error(&xas) and pos != memcg,
the loop restarts on the same ancestor A with mlru still non-NULL and
calls xas_set(&xas, pos->kmemcg_id), clearing XA_ERROR(-ENOMEM). It then
retries and fails forever without sleeping.
When reached from __filemap_add_folio() under mapping->i_pages.xa_lock
with IRQs disabled, this causes an immediate hard lockup (and an
unkillable soft lockup on blocking paths like THP faults). Checking
!xas_error(&xas) in the outer while condition exits the loop on error.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204173929.2355221-1-hannes@cmpxchg.org?part=2
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 20:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 17:39 [PATCH 1/2] mm: list_lru: fix XArray node leak at destroy Johannes Weiner
2026-12-04 17:39 ` [PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes Johannes Weiner
2026-12-04 20:33 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox