* Re: [PATCH v2] mm: list_lru: fix NULL deref when a folio moves to the root memcg
2026-11-12 6:12 [PATCH v2] mm: list_lru: fix NULL deref when a folio moves to the root memcg Shakeel Butt
@ 2026-11-12 6:13 ` Muchun Song
2026-11-12 15:03 ` Harry Yoo
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Muchun Song @ 2026-11-12 6:13 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Dave Chinner, Johannes Weiner, David Hildenbrand,
Michal Hocko, Roman Gushchin, Qi Zheng, Meta kernel team,
linux-mm, linux-kernel, Chris Mason
> On Nov 12, 2026, at 14:12, Shakeel Butt <shakeel.butt@linux.dev> wrote:
>
> folio_memcg_list_lru_alloc() makes sure that a folio's memcg has its
> own lists in a list_lru, such as the THP deferred split queue. It looks
> up the folio's memcg twice: a fast path under RCU checks if the lists
> exist, and a slow path takes a reference and allocates them. The fast
> path returns early for the root memcg, which uses the global lists.
> The slow path does not check for the root.
>
> The memcg can change between the two lookups: when a cgroup is
> removed, its folios are moved to the parent memcg. If the folio is
> moved up to the root in between, the slow path gets the root, and
> __memcg_list_lru_alloc() dereferences the root's parent, which is NULL.
>
> Users see a NULL pointer dereference oops while a THP is allocated,
> such as in a page fault, in khugepaged or at swap-in. tmpfs huge pages
> are affected too, and for them the oops can also happen in reclaim,
> for example in kswapd. The task that hits the oops is killed, or the
> machine panics if panic_on_oops is set.
>
> The race is narrow: the folio has to be moved up to the root right
> between the two lookups. For example, its cgroup is directly below the
> root and is removed, or has its memory controller disabled, at that
> moment.
>
> Fix it by checking memcg_list_lru_allocated() again in the slow path,
> like the fast path does. It returns true for the root, which needs no
> lists of its own.
>
> Fixes: ae64f07a6a40 ("mm: list_lru: introduce folio_memcg_list_lru_alloc()")
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Muchun Song (ByteDance) <muchun.song@linux.dev>
Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] mm: list_lru: fix NULL deref when a folio moves to the root memcg
2026-11-12 6:12 [PATCH v2] mm: list_lru: fix NULL deref when a folio moves to the root memcg Shakeel Butt
2026-11-12 6:13 ` Muchun Song
@ 2026-11-12 15:03 ` Harry Yoo
2026-11-12 15:16 ` Johannes Weiner
2026-11-23 6:25 ` [merged mm-hotfixes-stable] mm-list_lru-fix-null-deref-when-a-folio-moves-to-the-root-memcg.patch removed from -mm tree Andrew Morton
3 siblings, 0 replies; 5+ messages in thread
From: Harry Yoo @ 2026-11-12 15:03 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Dave Chinner, Johannes Weiner, David Hildenbrand,
Michal Hocko, Roman Gushchin, Muchun Song, Qi Zheng,
Meta kernel team, linux-mm, linux-kernel, Chris Mason
On Wed, Nov 11, 2026 at 10:12:31PM -0800, Shakeel Butt wrote:
> folio_memcg_list_lru_alloc() makes sure that a folio's memcg has its
> own lists in a list_lru, such as the THP deferred split queue. It looks
> up the folio's memcg twice: a fast path under RCU checks if the lists
> exist, and a slow path takes a reference and allocates them. The fast
> path returns early for the root memcg, which uses the global lists.
> The slow path does not check for the root.
>
> The memcg can change between the two lookups: when a cgroup is
> removed, its folios are moved to the parent memcg. If the folio is
> moved up to the root in between, the slow path gets the root, and
> __memcg_list_lru_alloc() dereferences the root's parent, which is NULL.
>
> Users see a NULL pointer dereference oops while a THP is allocated,
> such as in a page fault, in khugepaged or at swap-in. tmpfs huge pages
> are affected too, and for them the oops can also happen in reclaim,
> for example in kswapd. The task that hits the oops is killed, or the
> machine panics if panic_on_oops is set.
>
> The race is narrow: the folio has to be moved up to the root right
> between the two lookups. For example, its cgroup is directly below the
> root and is removed, or has its memory controller disabled, at that
> moment.
>
> Fix it by checking memcg_list_lru_allocated() again in the slow path,
> like the fast path does. It returns true for the root, which needs no
> lists of its own.
>
> Fixes: ae64f07a6a40 ("mm: list_lru: introduce folio_memcg_list_lru_alloc()")
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
> ---
The fix looks good to me,
Reviewed-by: Harry Yoo (Meta) <harry@kernel.org>
> Changes in v2:
> - Check memcg_list_lru_allocated() instead of mem_cgroup_is_root(), like
> the fast path does, and update the comment to match (Muchun).
> - Link to v1: https://lore.kernel.org/20261112024140.648088-1-shakeel.butt@linux.dev
>
> mm/list_lru.c | 11 +++++++++--
> 1 file changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/mm/list_lru.c b/mm/list_lru.c
> index d1d256822ccc..ca8de3793166 100644
> --- a/mm/list_lru.c
> +++ b/mm/list_lru.c
> @@ -645,9 +645,16 @@ int folio_memcg_list_lru_alloc(struct folio *folio, struct list_lru *lru,
> if (likely(res))
> return 0;
>
> - /* Allocation may block, pin the memcg */
> + /*
> + * Allocation may block, so pin the memcg. If the folio's cgroup
> + * was removed after the check above, the folio has moved up to a
> + * parent memcg. That memcg may already have its lists, or may be
> + * the root, which uses the global lists and has no parent. So
> + * check again before allocating.
> + */
> memcg = get_mem_cgroup_from_folio(folio);
> - res = __memcg_list_lru_alloc(memcg, lru, gfp);
> + if (!memcg_list_lru_allocated(memcg, lru))
> + res = __memcg_list_lru_alloc(memcg, lru, gfp);
> mem_cgroup_put(memcg);
I was wondering how we make sure reparenting doesn't happen after this,
but apparently it's fine as __memcg_list_lru_alloc() populates all
list_lrus from the memcg up to the hierarchy before the root.
--
Cheers,
Harry / Hyeonggon
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] mm: list_lru: fix NULL deref when a folio moves to the root memcg
2026-11-12 6:12 [PATCH v2] mm: list_lru: fix NULL deref when a folio moves to the root memcg Shakeel Butt
2026-11-12 6:13 ` Muchun Song
2026-11-12 15:03 ` Harry Yoo
@ 2026-11-12 15:16 ` Johannes Weiner
2026-11-23 6:25 ` [merged mm-hotfixes-stable] mm-list_lru-fix-null-deref-when-a-folio-moves-to-the-root-memcg.patch removed from -mm tree Andrew Morton
3 siblings, 0 replies; 5+ messages in thread
From: Johannes Weiner @ 2026-11-12 15:16 UTC (permalink / raw)
To: Shakeel Butt
Cc: Andrew Morton, Dave Chinner, David Hildenbrand, Michal Hocko,
Roman Gushchin, Muchun Song, Qi Zheng, Meta kernel team, linux-mm,
linux-kernel, Chris Mason
On Wed, Nov 11, 2026 at 10:12:31PM -0800, Shakeel Butt wrote:
> folio_memcg_list_lru_alloc() makes sure that a folio's memcg has its
> own lists in a list_lru, such as the THP deferred split queue. It looks
> up the folio's memcg twice: a fast path under RCU checks if the lists
> exist, and a slow path takes a reference and allocates them. The fast
> path returns early for the root memcg, which uses the global lists.
> The slow path does not check for the root.
>
> The memcg can change between the two lookups: when a cgroup is
> removed, its folios are moved to the parent memcg. If the folio is
> moved up to the root in between, the slow path gets the root, and
> __memcg_list_lru_alloc() dereferences the root's parent, which is NULL.
>
> Users see a NULL pointer dereference oops while a THP is allocated,
> such as in a page fault, in khugepaged or at swap-in. tmpfs huge pages
> are affected too, and for them the oops can also happen in reclaim,
> for example in kswapd. The task that hits the oops is killed, or the
> machine panics if panic_on_oops is set.
>
> The race is narrow: the folio has to be moved up to the root right
> between the two lookups. For example, its cgroup is directly below the
> root and is removed, or has its memory controller disabled, at that
> moment.
>
> Fix it by checking memcg_list_lru_allocated() again in the slow path,
> like the fast path does. It returns true for the root, which needs no
> lists of its own.
>
> Fixes: ae64f07a6a40 ("mm: list_lru: introduce folio_memcg_list_lru_alloc()")
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Johannes Weiner <hannes@cmpxchg.org>
^ permalink raw reply [flat|nested] 5+ messages in thread* [merged mm-hotfixes-stable] mm-list_lru-fix-null-deref-when-a-folio-moves-to-the-root-memcg.patch removed from -mm tree
2026-11-12 6:12 [PATCH v2] mm: list_lru: fix NULL deref when a folio moves to the root memcg Shakeel Butt
` (2 preceding siblings ...)
2026-11-12 15:16 ` Johannes Weiner
@ 2026-11-23 6:25 ` Andrew Morton
3 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-11-23 6:25 UTC (permalink / raw)
To: mm-commits, stable, roman.gushchin, qi.zheng, muchun.song, mhocko,
mason, harry, hannes, david, david, shakeel.butt, akpm
The quilt patch titled
Subject: mm: list_lru: fix NULL deref when a folio moves to the root memcg
has been removed from the -mm tree. Its filename was
mm-list_lru-fix-null-deref-when-a-folio-moves-to-the-root-memcg.patch
This patch was dropped because it was merged into the mm-hotfixes-stable branch
of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
------------------------------------------------------
From: Shakeel Butt <shakeel.butt@linux.dev>
Subject: mm: list_lru: fix NULL deref when a folio moves to the root memcg
Date: Wed, 11 Nov 2026 22:12:31 -0800
folio_memcg_list_lru_alloc() makes sure that a folio's memcg has its own
lists in a list_lru, such as the THP deferred split queue. It looks up
the folio's memcg twice: a fast path under RCU checks if the lists exist,
and a slow path takes a reference and allocates them. The fast path
returns early for the root memcg, which uses the global lists. The slow
path does not check for the root.
The memcg can change between the two lookups: when a cgroup is removed,
its folios are moved to the parent memcg. If the folio is moved up to the
root in between, the slow path gets the root, and __memcg_list_lru_alloc()
dereferences the root's parent, which is NULL.
Users see a NULL pointer dereference oops while a THP is allocated, such
as in a page fault, in khugepaged or at swap-in. tmpfs huge pages are
affected too, and for them the oops can also happen in reclaim, for
example in kswapd. The task that hits the oops is killed, or the machine
panics if panic_on_oops is set.
The race is narrow: the folio has to be moved up to the root right between
the two lookups. For example, its cgroup is directly below the root and
is removed, or has its memory controller disabled, at that moment.
Fix it by checking memcg_list_lru_allocated() again in the slow path, like
the fast path does. It returns true for the root, which needs no lists of
its own.
Link: https://patch.msgid.link/20261112061231.2469026-1-shakeel.butt@linux.dev
Fixes: ae64f07a6a40 ("mm: list_lru: introduce folio_memcg_list_lru_alloc()")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: Chris Mason <mason@kernel.org>
Acked-by: Muchun Song (ByteDance) <muchun.song@linux.dev>
Reviewed-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Harry Yoo (Meta) <harry@kernel.org>
Cc: Dave Chinner <david@fromorbit.com>
Cc: David Hildenbrand <david@kernel.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Qi Zheng <qi.zheng@linux.dev>
Cc: <stable@vger.kernel.org>
mm/list_lru.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
--- a/mm/list_lru.c~mm-list_lru-fix-null-deref-when-a-folio-moves-to-the-root-memcg
+++ a/mm/list_lru.c
@@ -645,9 +645,16 @@ int folio_memcg_list_lru_alloc(struct fo
if (likely(res))
return 0;
- /* Allocation may block, pin the memcg */
+ /*
+ * Allocation may block, so pin the memcg. If the folio's cgroup
+ * was removed after the check above, the folio has moved up to a
+ * parent memcg. That memcg may already have its lists, or may be
+ * the root, which uses the global lists and has no parent. So
+ * check again before allocating.
+ */
memcg = get_mem_cgroup_from_folio(folio);
- res = __memcg_list_lru_alloc(memcg, lru, gfp);
+ if (!memcg_list_lru_allocated(memcg, lru))
+ res = __memcg_list_lru_alloc(memcg, lru, gfp);
mem_cgroup_put(memcg);
return res;
}
_
Patches currently in -mm which might be from shakeel.butt@linux.dev are
memcg-move-memoryhigh-enforcement-out-of-try_charge_memcg.patch
memcg-force-charges-from-reclaim-before-trying-the-limits.patch
memcg-use-high_work-for-kernel-threads.patch
memcg-use-high_work-for-remote-charges.patch
memcg-simplify-high-limit-handling-after-a-charge.patch
^ permalink raw reply [flat|nested] 5+ messages in thread