Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Johannes Weiner <hannes@cmpxchg.org>
To: sashiko-bot@kernel.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Chris Mason <mason@kernel.org>,
	David Hildenbrand <david@kernel.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	linux-mm@kvack.org, Baolin Wang <baolin.wang@linux.alibaba.com>,
	Dave Chinner <david@fromorbit.com>, Harry Yoo <harry@kernel.org>,
	Hugh Dickins <hughd@google.com>, Kairui Song <kasong@tencent.com>,
	Matthew Wilcox <willy@infradead.org>,
	Muchun Song <muchun.song@linux.dev>,
	Qi Zheng <qi.zheng@linux.dev>,
	Qinyun Tan <qinyuntan@linux.alibaba.com>,
	Vlastimil Babka <vbabka@kernel.org>, Zi Yan <ziy@nvidia.com>,
	tj@kernel.org, mkoutny@suse.com
Subject: Re: [PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes
Date: Sun, 6 Dec 2026 10:36:11 -0500	[thread overview]
Message-ID: <axWA65SJiqGFl1ac@cmpxchg.org> (raw)
In-Reply-To: <sashiko-outbox-250630@kernel.org>

On Fri, Dec 04, 2026 at 08:33:55PM +0000, sashiko-bot@kernel.org wrote:
> 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.

Failing an order-0 is an OOM, doesn't really matter. If we fall back
gracefully it would be to an order-0 page and OOM then. If this was an
entirely optional path, it would be different. But it's a fault path -
something needs to get allocated. order-0 is the smallest unit.

> > 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.

Here it's actually desirable. If that allocation fails, we're
basically leaking that unused large folio space from the
shrinker. noretry is the opposite of what we want here.

> [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.

zswap_store() itself has long entered the charge path for the
*compressed space* it allocates. This sequence isn't new. There is
actually a warning in obj_cgroup_charge_zswap() on !PF_MEMALLOC.

I think i915 should probably use PF_MEMALLOC, since it is in fact
reclaiming memory. But that's a separate discussion.

> > @@ -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.

Yes, there is a pending patch for that in mm-unstable:

https://lore.kernel.org/all/20261122031143.3639941-1-songmuchun@bytedance.com/


  reply	other threads:[~2026-12-06 15:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-12-06 15:36     ` Johannes Weiner [this message]
2026-12-06  2:57 ` [PATCH 1/2] mm: list_lru: fix XArray node leak at destroy Muchun Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axWA65SJiqGFl1ac@cmpxchg.org \
    --to=hannes@cmpxchg.org \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@fromorbit.com \
    --cc=david@kernel.org \
    --cc=harry@kernel.org \
    --cc=hughd@google.com \
    --cc=kasong@tencent.com \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=qi.zheng@linux.dev \
    --cc=qinyuntan@linux.alibaba.com \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-bot@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=willy@infradead.org \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox