From: Chris Mason <mason@kernel.org>
To: hughd@google.com, david@kernel.org,
baolin.wang@linux.alibaba.com, akpm@linux-foundation.org,
qi.zheng@linux.dev, qinyuntan@linux.alibaba.com,
shakeel.butt@linux.dev, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, mason@kernel.org
Subject: [PATCH v2 1/1] mm/shmem: don't use the memcg of an uncharged folio
Date: Fri, 20 Nov 2026 14:50:58 -0500 [thread overview]
Message-ID: <20261120195057.50031-2-mason@kernel.org> (raw)
In-Reply-To: <20261120195057.50031-1-mason@kernel.org>
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
prev parent reply other threads:[~2026-11-20 19:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 19:50 [PATCH v2 0/1] " Chris Mason
2026-11-20 19:50 ` Chris Mason [this message]
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=20261120195057.50031-2-mason@kernel.org \
--to=mason@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=hughd@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=qi.zheng@linux.dev \
--cc=qinyuntan@linux.alibaba.com \
--cc=shakeel.butt@linux.dev \
/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
all inboxes | Powered by JetHome®