* [PATCH v2 0/1] mm/shmem: don't use the memcg of an uncharged folio
@ 2026-11-20 19:50 Chris Mason
2026-11-20 19:50 ` [PATCH v2 1/1] " Chris Mason
2026-11-21 13:01 ` [PATCH v2 0/1] " David Hildenbrand
0 siblings, 2 replies; 8+ messages in thread
From: Chris Mason @ 2026-11-20 19:50 UTC (permalink / raw)
To: hughd, david, baolin.wang, akpm, qi.zheng, qinyuntan,
shakeel.butt, linux-mm, linux-kernel, mason
Hi everyone,
This is relatively niche, but actually new with 7.4-rc1, and I would
expect it to trigger under heavy load with huge=always on tmpfs. We'd
need to run out of room in the tmpfs or reclaim memory while huge folios
are migrated.
Qinyun Tan's pending series fixes the oops:
[PATCH v4 RESEND 2/5] mm: memcontrol: drop kmemcg_id and use
mem_cgroup_id() for list_lru indexing
https://lore.kernel.org/all/20261105092233.3558950-3-qinyuntan@linux.alibaba.com/
It makes memcg_list_lru_allocated() accept a NULL memcg, but I think we
can still race and corrupt lists instead:
shmem_unused_huge_requeue() __migrate_folio()
=========================== =================
folio_memcg_list_lru_alloc()
folio_memcg() is NULL
no lists are allocated
mem_cgroup_migrate()
commit_charge(newfolio)
get_obj_cgroup_from_folio()
returns the objcg of memcg M
list_lru_add(M)
if M has no lists, the inode
goes on the parent's list
[ later, M gets lists of its own ]
list_lru_del(M)
locks M's list
unlinks from the parent's list
I haven't tried to reproduce on top of the kmemcg_id series, so that's
all just guessing from the code.
v1->v2: adopt Qi Zheng's suggestion to return early in the failure path
v1: https://lore.kernel.org/all/20261119193656.88678-1-mason@kernel.org/
This is on top of v7.4-rc3.
Chris Mason (1):
mm/shmem: don't use the memcg of an uncharged folio
mm/shmem.c | 39 +++++++++++++++++++++++++++++++--------
1 file changed, 31 insertions(+), 8 deletions(-)
--
2.55.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v2 1/1] mm/shmem: don't use the memcg of an uncharged folio 2026-11-20 19:50 [PATCH v2 0/1] mm/shmem: don't use the memcg of an uncharged folio Chris Mason @ 2026-11-20 19:50 ` Chris Mason 2026-11-21 10:50 ` Qi Zheng ` (2 more replies) 2026-11-21 13:01 ` [PATCH v2 0/1] " David Hildenbrand 1 sibling, 3 replies; 8+ messages in thread From: Chris Mason @ 2026-11-20 19:50 UTC (permalink / raw) To: hughd, david, baolin.wang, akpm, qi.zheng, qinyuntan, shakeel.butt, linux-mm, linux-kernel, mason The shrinker for unused huge tmpfs folios can dereference a NULL memcg if it races with the migration of the folio at the end of a file. folio_memcg_list_lru_alloc() assumes that folio_memcg() is never NULL. But it is NULL until the folio is charged, and a migration target can be in the page cache before charging. shmem_unused_huge_shrink() looks the folio up without the folio lock, so we can race with migration: __migrate_folio() shmem_unused_huge_shrink() ================= ========================== __folio_migrate_mapping() xas_store(newfolio) filemap_get_entry() returns newfolio folio_trylock() fails shmem_unused_huge_requeue() folio_memcg_list_lru_alloc() folio_memcg() is NULL memcg->kmemcg_id folio_migrate_flags() mem_cgroup_migrate() commit_charge(newfolio) Fix it by keeping the original objcg around while the inode is isolated. Fixes: 743e7d025d2f ("mm: shmem: make unused huge shrinker memcg aware") Suggested-by: Qi Zheng <qi.zheng@linux.dev> Signed-off-by: Chris Mason <mason@kernel.org> --- Notes: v1->v2: adopt Qi Zheng's suggestion to return early in the failure path mm/shmem.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 10cef89b00b08..901cfd15bdbd0 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -758,6 +758,13 @@ shmem_unused_huge_alloc_lru(struct shmem_sb_info *sbinfo, struct folio *folio, { int ret; + /* + * A migration target is in the page cache before it is charged. Once + * charged, its objcg can't change while we hold a folio reference. + */ + if (!mem_cgroup_disabled() && !folio_memcg_charged(folio)) + return ERR_PTR(-EAGAIN); + ret = folio_memcg_list_lru_alloc(folio, &sbinfo->shrinklist, gfp); if (ret) return ERR_PTR(ret); @@ -887,12 +894,11 @@ static enum lru_status shmem_unused_huge_isolate(struct list_head *item, return LRU_REMOVED; } + /* keep shrinklist_objcg, requeue may need it */ list_lru_isolate(lru, item); - objcg = shmem_get_and_clear_objcg(info); set_shmem_unused_huge_isolated(info); list_add_tail(&info->shrinklist, &scan->list); spin_unlock(&info->lock); - obj_cgroup_put(objcg); return LRU_REMOVED; } @@ -928,10 +934,26 @@ static bool is_shmem_unused_huge_match(struct folio *folio, static void shmem_unused_huge_drop(struct inode *inode) { struct shmem_inode_info *info = SHMEM_I(inode); + struct obj_cgroup *objcg; spin_lock(&info->lock); list_del_init(&info->shrinklist); + objcg = shmem_get_and_clear_objcg(info); spin_unlock(&info->lock); + + obj_cgroup_put(objcg); +} + +static void shmem_unused_huge_requeue_locked(struct shmem_inode_info *info, + struct shmem_sb_info *sbinfo, + int nid) +{ + lockdep_assert_held(&info->lock); + + list_del_init(&info->shrinklist); + shmem_unused_huge_lru_add(sbinfo, &info->shrinklist, nid, + info->shrinklist_objcg); + info->shrinklist_nid = nid; } static void shmem_unused_huge_requeue(struct inode *inode, struct folio *folio) @@ -943,17 +965,18 @@ static void shmem_unused_huge_requeue(struct inode *inode, struct folio *folio) objcg = shmem_unused_huge_alloc_lru(sbinfo, folio, GFP_NOWAIT); if (IS_ERR(objcg)) { - shmem_unused_huge_drop(inode); + spin_lock(&info->lock); + shmem_unused_huge_requeue_locked(info, sbinfo, nid); + spin_unlock(&info->lock); return; } spin_lock(&info->lock); - /* Requeue the inode to shrinklist */ - list_del_init(&info->shrinklist); - shmem_unused_huge_lru_add(sbinfo, &info->shrinklist, nid, objcg); - info->shrinklist_objcg = objcg; - info->shrinklist_nid = nid; + swap(info->shrinklist_objcg, objcg); + shmem_unused_huge_requeue_locked(info, sbinfo, nid); spin_unlock(&info->lock); + + obj_cgroup_put(objcg); } static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, -- 2.55.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/1] mm/shmem: don't use the memcg of an uncharged folio 2026-11-20 19:50 ` [PATCH v2 1/1] " Chris Mason @ 2026-11-21 10:50 ` Qi Zheng 2026-11-21 12:18 ` David Hildenbrand (Arm) 2026-11-21 12:59 ` Gregory Price (Meta) 2 siblings, 0 replies; 8+ messages in thread From: Qi Zheng @ 2026-11-21 10:50 UTC (permalink / raw) To: Chris Mason, hughd, david, baolin.wang, akpm, qinyuntan, shakeel.butt, linux-mm, linux-kernel On 11/21/26 3:50 AM, Chris Mason wrote: > The shrinker for unused huge tmpfs folios can dereference a NULL memcg > if it races with the migration of the folio at the end of a file. > > folio_memcg_list_lru_alloc() assumes that folio_memcg() is never NULL. > But it is NULL until the folio is charged, and a migration target can be > in the page cache before charging. > > shmem_unused_huge_shrink() looks the folio up without the folio lock, > so we can race with migration: > > __migrate_folio() shmem_unused_huge_shrink() > ================= ========================== > __folio_migrate_mapping() > xas_store(newfolio) > filemap_get_entry() > returns newfolio > folio_trylock() fails > shmem_unused_huge_requeue() > folio_memcg_list_lru_alloc() > folio_memcg() is NULL > memcg->kmemcg_id > folio_migrate_flags() > mem_cgroup_migrate() > commit_charge(newfolio) > > Fix it by keeping the original objcg around while the inode is isolated. > > Fixes: 743e7d025d2f ("mm: shmem: make unused huge shrinker memcg aware") > Suggested-by: Qi Zheng <qi.zheng@linux.dev> > Signed-off-by: Chris Mason <mason@kernel.org> > --- > > Notes: > v1->v2: adopt Qi Zheng's suggestion to return early in the > failure path > > mm/shmem.c | 39 +++++++++++++++++++++++++++++++-------- > 1 file changed, 31 insertions(+), 8 deletions(-) > LGTM. Reviewed-by: Qi Zheng <qi.zheng@linux.dev> Thanks! ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/1] mm/shmem: don't use the memcg of an uncharged folio 2026-11-20 19:50 ` [PATCH v2 1/1] " Chris Mason 2026-11-21 10:50 ` Qi Zheng @ 2026-11-21 12:18 ` David Hildenbrand (Arm) 2026-11-21 12:59 ` Gregory Price (Meta) 2 siblings, 0 replies; 8+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-21 12:18 UTC (permalink / raw) To: Chris Mason, hughd, baolin.wang, akpm, qi.zheng, qinyuntan, shakeel.butt, linux-mm, linux-kernel On 11/20/26 20:50, Chris Mason wrote: > The shrinker for unused huge tmpfs folios can dereference a NULL memcg > if it races with the migration of the folio at the end of a file. > > folio_memcg_list_lru_alloc() assumes that folio_memcg() is never NULL. > But it is NULL until the folio is charged, and a migration target can be > in the page cache before charging. > > shmem_unused_huge_shrink() looks the folio up without the folio lock, > so we can race with migration: > > __migrate_folio() shmem_unused_huge_shrink() > ================= ========================== > __folio_migrate_mapping() > xas_store(newfolio) > filemap_get_entry() > returns newfolio > folio_trylock() fails > shmem_unused_huge_requeue() > folio_memcg_list_lru_alloc() > folio_memcg() is NULL > memcg->kmemcg_id > folio_migrate_flags() > mem_cgroup_migrate() > commit_charge(newfolio) > > Fix it by keeping the original objcg around while the inode is isolated. > > Fixes: 743e7d025d2f ("mm: shmem: make unused huge shrinker memcg aware") > Suggested-by: Qi Zheng <qi.zheng@linux.dev> > Signed-off-by: Chris Mason <mason@kernel.org> > --- Thanks! I'll wait for more review and will then queue this as a hotfix. -- Cheers, David ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/1] mm/shmem: don't use the memcg of an uncharged folio 2026-11-20 19:50 ` [PATCH v2 1/1] " Chris Mason 2026-11-21 10:50 ` Qi Zheng 2026-11-21 12:18 ` David Hildenbrand (Arm) @ 2026-11-21 12:59 ` Gregory Price (Meta) 2026-11-21 13:00 ` David Hildenbrand (Arm) 2 siblings, 1 reply; 8+ messages in thread From: Gregory Price (Meta) @ 2026-11-21 12:59 UTC (permalink / raw) To: Chris Mason Cc: hughd, david, baolin.wang, akpm, qi.zheng, qinyuntan, shakeel.butt, linux-mm, linux-kernel On 2026-11-20 14:50 -0500, Chris Mason wrote: > The shrinker for unused huge tmpfs folios can dereference a NULL memcg > if it races with the migration of the folio at the end of a file. > > folio_memcg_list_lru_alloc() assumes that folio_memcg() is never NULL. > But it is NULL until the folio is charged, and a migration target can be > in the page cache before charging. > > shmem_unused_huge_shrink() looks the folio up without the folio lock, > so we can race with migration: > > __migrate_folio() shmem_unused_huge_shrink() > ================= ========================== > __folio_migrate_mapping() > xas_store(newfolio) > filemap_get_entry() > returns newfolio > folio_trylock() fails > shmem_unused_huge_requeue() > folio_memcg_list_lru_alloc() > folio_memcg() is NULL > memcg->kmemcg_id > folio_migrate_flags() > mem_cgroup_migrate() > commit_charge(newfolio) > > Fix it by keeping the original objcg around while the inode is isolated. > > Fixes: 743e7d025d2f ("mm: shmem: make unused huge shrinker memcg aware") > Suggested-by: Qi Zheng <qi.zheng@linux.dev> > Signed-off-by: Chris Mason <mason@kernel.org> Reproduced and validated the fix. Changes look ok. Tested-by: Gregory Price (Meta) <gourry@gourry.net> Reviewed-by: Gregory Price (Meta) <gourry@gourry.net> -- ~Gregory ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/1] mm/shmem: don't use the memcg of an uncharged folio 2026-11-21 12:59 ` Gregory Price (Meta) @ 2026-11-21 13:00 ` David Hildenbrand (Arm) 2026-11-21 13:01 ` David Hildenbrand (Arm) 0 siblings, 1 reply; 8+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-21 13:00 UTC (permalink / raw) To: Gregory Price (Meta), Chris Mason Cc: hughd, baolin.wang, akpm, qi.zheng, qinyuntan, shakeel.butt, linux-mm, linux-kernel On 11/21/26 13:59, Gregory Price (Meta) wrote: > On 2026-11-20 14:50 -0500, Chris Mason wrote: >> The shrinker for unused huge tmpfs folios can dereference a NULL memcg >> if it races with the migration of the folio at the end of a file. >> >> folio_memcg_list_lru_alloc() assumes that folio_memcg() is never NULL. >> But it is NULL until the folio is charged, and a migration target can be >> in the page cache before charging. >> >> shmem_unused_huge_shrink() looks the folio up without the folio lock, >> so we can race with migration: >> >> __migrate_folio() shmem_unused_huge_shrink() >> ================= ========================== >> __folio_migrate_mapping() >> xas_store(newfolio) >> filemap_get_entry() >> returns newfolio >> folio_trylock() fails >> shmem_unused_huge_requeue() >> folio_memcg_list_lru_alloc() >> folio_memcg() is NULL >> memcg->kmemcg_id >> folio_migrate_flags() >> mem_cgroup_migrate() >> commit_charge(newfolio) >> >> Fix it by keeping the original objcg around while the inode is isolated. >> >> Fixes: 743e7d025d2f ("mm: shmem: make unused huge shrinker memcg aware") >> Suggested-by: Qi Zheng <qi.zheng@linux.dev> >> Signed-off-by: Chris Mason <mason@kernel.org> > > Reproduced and validated the fix. Changes look ok. Thanks, I'll throw this into for-next-fixes right away and cc stable. -- Cheers, David ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/1] mm/shmem: don't use the memcg of an uncharged folio 2026-11-21 13:00 ` David Hildenbrand (Arm) @ 2026-11-21 13:01 ` David Hildenbrand (Arm) 0 siblings, 0 replies; 8+ messages in thread From: David Hildenbrand (Arm) @ 2026-11-21 13:01 UTC (permalink / raw) To: Gregory Price (Meta), Chris Mason Cc: hughd, baolin.wang, akpm, qi.zheng, qinyuntan, shakeel.butt, linux-mm, linux-kernel On 11/21/26 14:00, David Hildenbrand (Arm) wrote: > On 11/21/26 13:59, Gregory Price (Meta) wrote: >> On 2026-11-20 14:50 -0500, Chris Mason wrote: >>> The shrinker for unused huge tmpfs folios can dereference a NULL memcg >>> if it races with the migration of the folio at the end of a file. >>> >>> folio_memcg_list_lru_alloc() assumes that folio_memcg() is never NULL. >>> But it is NULL until the folio is charged, and a migration target can be >>> in the page cache before charging. >>> >>> shmem_unused_huge_shrink() looks the folio up without the folio lock, >>> so we can race with migration: >>> >>> __migrate_folio() shmem_unused_huge_shrink() >>> ================= ========================== >>> __folio_migrate_mapping() >>> xas_store(newfolio) >>> filemap_get_entry() >>> returns newfolio >>> folio_trylock() fails >>> shmem_unused_huge_requeue() >>> folio_memcg_list_lru_alloc() >>> folio_memcg() is NULL >>> memcg->kmemcg_id >>> folio_migrate_flags() >>> mem_cgroup_migrate() >>> commit_charge(newfolio) >>> >>> Fix it by keeping the original objcg around while the inode is isolated. >>> >>> Fixes: 743e7d025d2f ("mm: shmem: make unused huge shrinker memcg aware") >>> Suggested-by: Qi Zheng <qi.zheng@linux.dev> >>> Signed-off-by: Chris Mason <mason@kernel.org> >> >> Reproduced and validated the fix. Changes look ok. > > Thanks, I'll throw this into for-next-fixes right away and cc stable. scrap the latter ... just a hotfix. -- Cheers, David ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 0/1] mm/shmem: don't use the memcg of an uncharged folio 2026-11-20 19:50 [PATCH v2 0/1] mm/shmem: don't use the memcg of an uncharged folio Chris Mason 2026-11-20 19:50 ` [PATCH v2 1/1] " Chris Mason @ 2026-11-21 13:01 ` David Hildenbrand 1 sibling, 0 replies; 8+ messages in thread From: David Hildenbrand @ 2026-11-21 13:01 UTC (permalink / raw) To: hughd, david, baolin.wang, akpm, qi.zheng, qinyuntan, shakeel.butt, linux-mm, linux-kernel, Chris Mason On Fri, 20 Nov 2026 14:50:57 -0500, Chris Mason wrote: > This is relatively niche, but actually new with 7.4-rc1, and I would > expect it to trigger under heavy load with huge=always on tmpfs. We'd > need to run out of room in the tmpfs or reclaim memory while huge folios > are migrated. > > Qinyun Tan's pending series fixes the oops: > > [...] Applied, thanks! tree: mm/core.git branch: for-next-fixes Applied patches will appear soon in mm-next, followed by linux-next. Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up. It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated. Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch. [1/1] mm/shmem: don't use the memcg of an uncharged folio https://git.kernel.org/mm/core/c/eb01811c -- Cheers, David ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-21 13:02 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-20 19:50 [PATCH v2 0/1] mm/shmem: don't use the memcg of an uncharged folio Chris Mason 2026-11-20 19:50 ` [PATCH v2 1/1] " Chris Mason 2026-11-21 10:50 ` Qi Zheng 2026-11-21 12:18 ` David Hildenbrand (Arm) 2026-11-21 12:59 ` Gregory Price (Meta) 2026-11-21 13:00 ` David Hildenbrand (Arm) 2026-11-21 13:01 ` David Hildenbrand (Arm) 2026-11-21 13:01 ` [PATCH v2 0/1] " David Hildenbrand
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).