Skip to contentSkip to conversation

mm/shmem: don't use the memcg of an uncharged folio

PATCH Superseded Chris Mason 2 participants 4 messages Nov 19 – Nov 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mm/shmem: don't use the memcg of an uncharged folio

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/[email protected]/

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.

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 | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

--
2.55.0

#1 lore raw

PATCH 1/1 mm/shmem: don't use the memcg of an uncharged folio

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")
---
 mm/shmem.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
mm/shmem.c +22 -7
index 10cef89b00b0..4465a43d6f74 100644
@@ -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,32 +934,41 @@ 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(struct inode *inode, struct folio *folio)
 {
 	struct shmem_inode_info *info = SHMEM_I(inode);
 	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
-	struct obj_cgroup *objcg;
+	struct obj_cgroup *objcg, *old_objcg;
 	int nid = folio_nid(folio);
 
 	objcg = shmem_unused_huge_alloc_lru(sbinfo, folio, GFP_NOWAIT);
-	if (IS_ERR(objcg)) {
-		shmem_unused_huge_drop(inode);
-		return;
-	}
 
 	spin_lock(&info->lock);
+	old_objcg = shmem_get_and_clear_objcg(info);
+	/* The lists for old_objcg exist, the inode was queued under it */
+	if (IS_ERR(objcg)) {
+		objcg = old_objcg;
+		old_objcg = NULL;
+	}
+
 	/* 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;
 	spin_unlock(&info->lock);
+
+	obj_cgroup_put(old_objcg);
 }
 
 static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
Signature · 1 line
2.55.0
#2 lore raw
Quoted 10 lines · Chris 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:

Quoted 14 lines
     __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)

Thanks for catching this!

Some nits below.

Quoted 58 lines

Fix it by keeping the original objcg around while the inode is isolated.

Fixes 743e7d025d2f ("mm: shmem: make unused huge shrinker memcg aware")
---
  mm/shmem.c | 29 ++++++++++++++++++++++-------
  1 file changed, 22 insertions(+), 7 deletions(-)
mm/shmem.c +12 -2
index 10cef89b00b0..4465a43d6f74 100644
@@ -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,32 +934,41 @@ 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(struct inode *inode, struct folio *folio)
  {
  	struct shmem_inode_info *info = SHMEM_I(inode);

mm/shmem.c static bool is_shmem_unused_huge_match(struct folio *folio, · line 948

Quoted 10 lines
mm/shmem.c +1 -5
  	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
-	struct obj_cgroup *objcg;
+	struct obj_cgroup *objcg, *old_objcg;
  	int nid = folio_nid(folio);
 
  	objcg = shmem_unused_huge_alloc_lru(sbinfo, folio, GFP_NOWAIT);
-	if (IS_ERR(objcg)) {
-		shmem_unused_huge_drop(inode);
-		return;
-	}

Maybe it's better to just return early in the failure path.

How about this:

+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 +975,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);
}

Thanks,
Qi

Quoted 20 lines
mm/shmem.c +9 -0
  	spin_lock(&info->lock);
+	old_objcg = shmem_get_and_clear_objcg(info);
+	/* The lists for old_objcg exist, the inode was queued under it */
+	if (IS_ERR(objcg)) {
+		objcg = old_objcg;
+		old_objcg = NULL;
+	}
+
  	/* 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;
  	spin_unlock(&info->lock);
+
+	obj_cgroup_put(old_objcg);
  }
 
  static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
#3 lore raw
Quoted 1 line · Qi Zheng

Some nits below.

[ .. ]

These look good and test well, I'll spin a v2 with them included. Thanks!

-chris