* [PATCH v4 0/4] make unused huge shrinker memcg aware
@ 2026-08-17 9:03 Qi Zheng
2026-08-17 9:03 ` [PATCH v4 1/4] fs: fix missed removal of super_fs_objects_eligible() Qi Zheng
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Qi Zheng @ 2026-08-17 9:03 UTC (permalink / raw)
To: hughd, baolin.wang, usama.arif, brauner, akpm, david
Cc: linux-mm, linux-kernel, Qi Zheng
From: Qi Zheng <zhengqi.arch@bytedance.com>
Changes in v4:
- add [PATCH v4 2/4] to make obj_cgroup_memcg() handle NULL objcg
- store obj_cgroup instead of mem_cgroup in shmem_inode_info to avoid
pinning a dying memcg through a long-lived CSS reference
(pointed by sashiko)
- fix is_shmem_unused_huge_match() to always check the NUMA node for
shrinker reclaim, not only for non-root memcg reclaim
(pointed by sashiko)
- collect Reviewed-by
- rebase onto the next-20260814
Note: [PATCH v4 1/4] should ideally be folded into commit 0ef8faff490be
("fs: push nr_cached_objects memcg gating into individual filesystems") in
linux-next.
Changes in v3:
- add a fix patch to fix missed removal of super_fs_objects_eligible()
- move the original shrinklist addition logic after all checks are completed,
and split it into a separate patch. (suggested by Baolin)
- simplify the shmem_unused_huge_requeue() (suggested by Baolin)
- keep the move_back label in shmem_unused_huge_shrink() (suggested by Baolin)
- rebase onto the next-20260731
Changes in v2:
- temporarily add the dependent patch from Usama to the series for review
convenience
- remove shrinklist_scan and shrinklist_isolated from struct shmem_inode_info,
and re-implement the logic by resuing the same info->shrinklist
(suggested by Baolin)
- add more comments (suggested by Andrew)
- fix missing initialization of info->shrinklist_memcg (pointed by sashiko)
- rebase onto the next-20260717
Hi all,
The shmem unused huge shrinker maintains a per-superblock list of inodes
whose tail huge folio extends beyond i_size. Because this list is not
memcg aware, reclaim triggered by memcg A can scan inodes across the
entire superblock and split huge folios charged to unrelated memcg B,
causing unexpected impact on it.
In the worst case, memcg A has no reclaimable shmem at all, making the
reclaim entirely useless and incurring unnecessary latency. We observed
this in production, where page lock contention during split caused
multi-hundred-millisecond stalls:
tid 11340 comm scanner locked a page for 182264 us! kstack:
unlock_page+1
split_huge_page_to_list+3135
shmem_unused_huge_shrink+767
super_cache_scan+329
do_shrink_slab+291
shrink_slab+533
shrink_node+400
do_try_to_free_pages+206
try_to_free_mem_cgroup_pages+262
try_charge_memcg+591
mem_cgroup_charge+136
__handle_mm_fault+2431
handle_mm_fault+194
do_user_addr_fault+462
__do_page_fault+176
do_page_fault+48
page_fault+62
Usama's recent patch [1] prevents the shmem unused shrinker from being
invoked during memcg-level reclaim altogether, but this is overly
conservative: we can do better by reclaiming only the shmem charged to
the reclaiming memcg.
This series converts the shrinker list to a memcg-aware list_lru, so
that non-root memcg reclaim walks only candidates charged to the
reclaiming memcg. Global reclaim, root memcg reclaim and shmem quota
reclaim retain their existing global semantics.
To avoid pinning a dying memcg through a long-lived CSS reference, each
inode stores an obj_cgroup reference instead of a mem_cgroup reference.
The list_lru add/delete paths resolve the current memcg from the objcg
under RCU, staying consistent with list_lru's own memcg migration on
offline.
Thanks,
Qi
[1]. https://lore.kernel.org/all/20260715103516.2410175-1-usama.arif@linux.dev/
Qi Zheng (4):
fs: fix missed removal of super_fs_objects_eligible()
mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg
mm: shmem: move unused huge shrinklist queuing past the truncation
check
mm: shmem: make unused huge shrinker memcg aware
fs/super.c | 18 +-
include/linux/memcontrol.h | 11 +-
include/linux/shmem_fs.h | 12 +-
mm/page_owner.c | 2 +-
mm/shmem.c | 382 ++++++++++++++++++++++++++++---------
mm/zswap.c | 17 +-
6 files changed, 315 insertions(+), 127 deletions(-)
--
2.54.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v4 1/4] fs: fix missed removal of super_fs_objects_eligible()
2026-08-17 9:03 [PATCH v4 0/4] make unused huge shrinker memcg aware Qi Zheng
@ 2026-08-17 9:03 ` Qi Zheng
2026-08-17 9:03 ` [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg Qi Zheng
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Qi Zheng @ 2026-08-17 9:03 UTC (permalink / raw)
To: hughd, baolin.wang, usama.arif, brauner, akpm, david
Cc: linux-mm, linux-kernel, Qi Zheng
From: Qi Zheng <zhengqi.arch@bytedance.com>
Commit 0ef8faff490be ("fs: push nr_cached_objects memcg gating into
individual filesystems") forgot to drop the super_fs_objects_eligible(),
just fix it.
Fixes: 0ef8faff490be ("fs: push nr_cached_objects memcg gating into individual filesystems")
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
fs/super.c | 18 ++----------------
1 file changed, 2 insertions(+), 16 deletions(-)
diff --git a/fs/super.c b/fs/super.c
index 05e4431730387..3ecce24328f67 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -171,19 +171,6 @@ static void super_wake(struct super_block *sb, unsigned int flag)
wake_up_var(&sb->s_flags);
}
-/*
- * The s_op->nr_cached_objects hooks (used for example by btrfs and xfs)
- * operate on filesystem-global state and ignore sc->memcg. Driving them
- * from per-memcg shrink_slab_memcg() invocations only burns CPU walking
- * per-cpu counters and queueing duplicate work: the actual reclaim happens on
- * the global path (kswapd or root direct reclaim) regardless. Restrict them
- * to that path.
- */
-static inline bool super_fs_objects_eligible(struct shrink_control *sc)
-{
- return !sc->memcg || mem_cgroup_is_root(sc->memcg);
-}
-
/*
* One thing we have to be careful of with a per-sb shrinker is that we don't
* drop the last active reference to the superblock from within the shrinker.
@@ -213,7 +200,7 @@ static unsigned long super_cache_scan(struct shrinker *shrink,
if (!super_trylock_shared(sb))
return SHRINK_STOP;
- if (sb->s_op->nr_cached_objects && super_fs_objects_eligible(sc))
+ if (sb->s_op->nr_cached_objects)
fs_objects = sb->s_op->nr_cached_objects(sb, sc);
inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
@@ -274,8 +261,7 @@ static unsigned long super_cache_count(struct shrinker *shrink,
return 0;
smp_rmb();
- if (sb->s_op && sb->s_op->nr_cached_objects &&
- super_fs_objects_eligible(sc))
+ if (sb->s_op && sb->s_op->nr_cached_objects)
total_objects = sb->s_op->nr_cached_objects(sb, sc);
total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
--
2.54.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg
2026-08-17 9:03 [PATCH v4 0/4] make unused huge shrinker memcg aware Qi Zheng
2026-08-17 9:03 ` [PATCH v4 1/4] fs: fix missed removal of super_fs_objects_eligible() Qi Zheng
@ 2026-08-17 9:03 ` Qi Zheng
2026-08-17 11:29 ` Qi Zheng
2026-08-17 16:05 ` Shakeel Butt
2026-08-17 9:03 ` [PATCH v4 3/4] mm: shmem: move unused huge shrinklist queuing past the truncation check Qi Zheng
2026-08-17 9:03 ` [PATCH v4 4/4] mm: shmem: make unused huge shrinker memcg aware Qi Zheng
3 siblings, 2 replies; 7+ messages in thread
From: Qi Zheng @ 2026-08-17 9:03 UTC (permalink / raw)
To: hughd, baolin.wang, usama.arif, brauner, akpm, david
Cc: linux-mm, linux-kernel, Qi Zheng
From: Qi Zheng <zhengqi.arch@bytedance.com>
obj_cgroup_memcg() currently requires a non-NULL objcg, so callers that
may hold a NULL objcg must guard the call with an explicit NULL check.
This pattern is duplicated in folio_memcg(), folio_memcg_check(),
mm/page_owner.c, and mm/zswap.c.
Teach obj_cgroup_memcg() to accept NULL and return NULL in that case, then
remove the redundant NULL checks at the call sites. Also remove the
mem_cgroup_from_entry() wrapper in zswap, which existed solely to provide
this NULL-safe behaviour, and replace its two callers with direct
obj_cgroup_memcg() calls.
No functional change intended.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
include/linux/memcontrol.h | 11 ++++++++---
mm/page_owner.c | 2 +-
mm/zswap.c | 17 ++---------------
3 files changed, 11 insertions(+), 19 deletions(-)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index cba35690c21aa..5107c227ae9b8 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -375,7 +375,7 @@ enum objext_flags {
static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
{
lockdep_assert_once(rcu_read_lock_held() || lockdep_is_held(&cgroup_mutex));
- return READ_ONCE(objcg->memcg);
+ return objcg ? READ_ONCE(objcg->memcg) : NULL;
}
/*
@@ -428,7 +428,7 @@ static inline struct mem_cgroup *folio_memcg(struct folio *folio)
{
struct obj_cgroup *objcg = folio_objcg(folio);
- return objcg ? obj_cgroup_memcg(objcg) : NULL;
+ return obj_cgroup_memcg(objcg);
}
/*
@@ -471,7 +471,7 @@ static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)
objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
- return objcg ? obj_cgroup_memcg(objcg) : NULL;
+ return obj_cgroup_memcg(objcg);
}
static inline struct mem_cgroup *page_memcg_check(struct page *page)
@@ -1045,6 +1045,11 @@ void mem_cgroup_flush_workqueue(void);
extern int mem_cgroup_init(void);
#else /* CONFIG_MEMCG */
+static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
+{
+ return NULL;
+}
+
#define MEM_CGROUP_ID_SHIFT 0
#define root_mem_cgroup (NULL)
diff --git a/mm/page_owner.c b/mm/page_owner.c
index fbbda7ba914ba..3fc37d9b908ef 100644
--- a/mm/page_owner.c
+++ b/mm/page_owner.c
@@ -575,7 +575,7 @@ static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret,
}
objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK);
- memcg = objcg ? obj_cgroup_memcg(objcg) : NULL;
+ memcg = obj_cgroup_memcg(objcg);
if (!memcg)
goto out_unlock;
diff --git a/mm/zswap.c b/mm/zswap.c
index 37f34e406c8e3..c1dc60926bad9 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -647,19 +647,6 @@ static int zswap_enabled_param_set(const char *val,
* lru functions
**********************************/
-/* should be called under RCU */
-#ifdef CONFIG_MEMCG
-static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry)
-{
- return entry->objcg ? obj_cgroup_memcg(entry->objcg) : NULL;
-}
-#else
-static inline struct mem_cgroup *mem_cgroup_from_entry(struct zswap_entry *entry)
-{
- return NULL;
-}
-#endif
-
static inline int entry_to_nid(struct zswap_entry *entry)
{
return page_to_nid(virt_to_page(entry));
@@ -682,7 +669,7 @@ static void zswap_lru_add(struct zswap_entry *entry)
* Similar reasoning holds for list_lru_del().
*/
rcu_read_lock();
- memcg = mem_cgroup_from_entry(entry);
+ memcg = obj_cgroup_memcg(entry->objcg);
/* will always succeed */
list_lru_add(&zswap_list_lru, &entry->lru, nid, memcg);
rcu_read_unlock();
@@ -694,7 +681,7 @@ static void zswap_lru_del(struct zswap_entry *entry)
struct mem_cgroup *memcg;
rcu_read_lock();
- memcg = mem_cgroup_from_entry(entry);
+ memcg = obj_cgroup_memcg(entry->objcg);
/* will always succeed */
list_lru_del(&zswap_list_lru, &entry->lru, nid, memcg);
rcu_read_unlock();
--
2.54.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 3/4] mm: shmem: move unused huge shrinklist queuing past the truncation check
2026-08-17 9:03 [PATCH v4 0/4] make unused huge shrinker memcg aware Qi Zheng
2026-08-17 9:03 ` [PATCH v4 1/4] fs: fix missed removal of super_fs_objects_eligible() Qi Zheng
2026-08-17 9:03 ` [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg Qi Zheng
@ 2026-08-17 9:03 ` Qi Zheng
2026-08-17 9:03 ` [PATCH v4 4/4] mm: shmem: make unused huge shrinker memcg aware Qi Zheng
3 siblings, 0 replies; 7+ messages in thread
From: Qi Zheng @ 2026-08-17 9:03 UTC (permalink / raw)
To: hughd, baolin.wang, usama.arif, brauner, akpm, david
Cc: linux-mm, linux-kernel, Qi Zheng
From: Qi Zheng <zhengqi.arch@bytedance.com>
The shmem_get_folio_gfp() adds the inode to the unused huge shrinker list
at the alloced label, but a subsequent truncation check may still fail and
remove the folio, leaving the inode on the list with a stale folio.
The original code works because the shrinker re-looks-up the folio and
drops stale entries, but it is cleaner to queue the inode only after all
checks that might remove the folio have passed.
So just make the pure structural move with no functional change, and it
serves as preparation for the memcg-aware shrinker conversion.
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
---
mm/shmem.c | 48 +++++++++++++++++++++++++++---------------------
1 file changed, 27 insertions(+), 21 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 897fa2b61346f..81eff314cfab8 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2536,27 +2536,6 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
alloced:
alloced = true;
- if (folio_test_large(folio) &&
- DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
- folio_next_index(folio)) {
- struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
- struct shmem_inode_info *info = SHMEM_I(inode);
- /*
- * Part of the large folio is beyond i_size: subject
- * to shrink under memory pressure.
- */
- spin_lock(&sbinfo->shrinklist_lock);
- /*
- * _careful to defend against unlocked access to
- * ->shrink_list in shmem_unused_huge_shrink()
- */
- if (list_empty_careful(&info->shrinklist)) {
- list_add_tail(&info->shrinklist,
- &sbinfo->shrinklist);
- sbinfo->shrinklist_len++;
- }
- spin_unlock(&sbinfo->shrinklist_lock);
- }
if (sgp == SGP_WRITE)
folio_set_referenced(folio);
@@ -2586,6 +2565,33 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
error = -EINVAL;
goto unlock;
}
+
+ /*
+ * Queue the inode on the shrink list only after all checks that might
+ * remove the folio have passed. Otherwise the inode could be left on
+ * the shrinker list with a stale folio.
+ */
+ if (alloced && folio_test_large(folio) &&
+ DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < folio_next_index(folio)) {
+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
+ struct shmem_inode_info *info = SHMEM_I(inode);
+ /*
+ * Part of the large folio is beyond i_size: subject
+ * to shrink under memory pressure.
+ */
+ spin_lock(&sbinfo->shrinklist_lock);
+ /*
+ * _careful to defend against unlocked access to
+ * ->shrink_list in shmem_unused_huge_shrink()
+ */
+ if (list_empty_careful(&info->shrinklist)) {
+ list_add_tail(&info->shrinklist,
+ &sbinfo->shrinklist);
+ sbinfo->shrinklist_len++;
+ }
+ spin_unlock(&sbinfo->shrinklist_lock);
+ }
+
out:
*foliop = folio;
return 0;
--
2.54.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 4/4] mm: shmem: make unused huge shrinker memcg aware
2026-08-17 9:03 [PATCH v4 0/4] make unused huge shrinker memcg aware Qi Zheng
` (2 preceding siblings ...)
2026-08-17 9:03 ` [PATCH v4 3/4] mm: shmem: move unused huge shrinklist queuing past the truncation check Qi Zheng
@ 2026-08-17 9:03 ` Qi Zheng
3 siblings, 0 replies; 7+ messages in thread
From: Qi Zheng @ 2026-08-17 9:03 UTC (permalink / raw)
To: hughd, baolin.wang, usama.arif, brauner, akpm, david
Cc: linux-mm, linux-kernel, Qi Zheng
From: Qi Zheng <zhengqi.arch@bytedance.com>
The shmem unused huge shrinker keeps a per-superblock list of inodes whose
tail huge folio extends beyond i_size. Since that list is not memcg aware,
reclaim triggered by one memcg can scan inodes from the whole superblock
and split shmem huge folios charged to unrelated memcgs.
Convert the shrink list to a memcg-aware list_lru. Queue each inode on the
list_lru sublist matching the memcg and node of the current tail huge
folio, so non-root memcg reclaim only walks candidates charged to the
reclaiming memcg. Global reclaim, root memcg reclaim and shmem quota
reclaim keep global semantics.
Rather than pinning a struct mem_cgroup reference in shmem_inode_info,
store a struct obj_cgroup reference instead. The list_lru add and delete
paths resolve the current memcg from the objcg under RCU, so that memcg
offline and list_lru entry migration remain consistent: list_lru migrates
entries to the parent memcg sublist on offline, and obj_cgroup_memcg()
follows the same reparenting, ensuring the correct sublist is always found
at delete time. This avoids pinning a dying memcg through a long-lived
CSS reference.
The list_lru still tracks inodes while the actual split target is the
current tail huge folio, so validate the folio memcg/node during scan. If
the folio no longer matches the reclaim context or splitting cannot
proceed, requeue the inode according to the current tail folio; if the
inode is no longer shrinkable, drop the scan entry.
This can be tested with the shrinker debugfs interface by allocating 32
tmpfs tail THPs in each of two memcgs, then scanning the sb-tmpfs shrinker
with memcg A's cgroup id:
before A scan after A scan
base A=64M, B=64M A=64M, B=64M (per-memcg count is skipped)
patched A=64M, B=64M A=0, B=64M
Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
---
include/linux/shmem_fs.h | 12 +-
mm/shmem.c | 362 ++++++++++++++++++++++++++++++---------
2 files changed, 289 insertions(+), 85 deletions(-)
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index 5663dff53186e..a7c7a96a7cbf9 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -11,6 +11,7 @@
#include <linux/fs_parser.h>
#include <linux/userfaultfd_k.h>
#include <linux/bits.h>
+#include <linux/list_lru.h>
/* inode in-kernel data */
@@ -54,6 +55,11 @@ struct shmem_inode_info {
struct dquot __rcu *i_dquot[MAXQUOTAS];
#endif
struct inode vfs_inode;
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+ struct obj_cgroup *shrinklist_objcg;
+ int shrinklist_nid;
+#endif
};
#define SHMEM_FL_USER_VISIBLE (FS_FL_USER_VISIBLE | FS_CASEFOLD_FL)
@@ -83,9 +89,9 @@ struct shmem_sb_info {
ino_t next_ino; /* The next per-sb inode number to use */
ino_t __percpu *ino_batch; /* The next per-cpu inode number to use */
struct mempolicy *mpol; /* default memory policy for mappings */
- spinlock_t shrinklist_lock; /* Protects shrinklist */
- struct list_head shrinklist; /* List of shinkable inodes */
- unsigned long shrinklist_len; /* Length of shrinklist */
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+ struct list_lru shrinklist; /* List of shrinkable inodes */
+#endif
struct shmem_quota_limits qlimits; /* Default quota limits */
struct simple_xattr_cache xa_cache;
};
diff --git a/mm/shmem.c b/mm/shmem.c
index 81eff314cfab8..bd5b53ee3a189 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -725,51 +725,258 @@ static const char *shmem_format_huge(int huge)
}
#endif
-static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
- struct shrink_control *sc, unsigned long nr_to_free)
+static bool is_shmem_unused_huge_isolated(struct shmem_inode_info *info)
{
- LIST_HEAD(list), *pos, *next;
- struct inode *inode;
+
+ return info->shrinklist_nid == -1;
+}
+
+static void set_shmem_unused_huge_isolated(struct shmem_inode_info *info)
+{
+ info->shrinklist_nid = -1;
+}
+
+static struct obj_cgroup *shmem_get_and_clear_objcg(struct shmem_inode_info *info)
+{
+ struct obj_cgroup *objcg = info->shrinklist_objcg;
+
+ info->shrinklist_objcg = NULL;
+
+ return objcg;
+}
+
+#ifdef CONFIG_MEMCG
+static struct obj_cgroup *
+shmem_unused_huge_alloc_lru(struct shmem_sb_info *sbinfo, struct folio *folio,
+ gfp_t gfp)
+{
+ int ret;
+
+ ret = folio_memcg_list_lru_alloc(folio, &sbinfo->shrinklist, gfp);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return get_obj_cgroup_from_folio(folio);
+}
+#else
+static struct obj_cgroup *
+shmem_unused_huge_alloc_lru(struct shmem_sb_info *sbinfo, struct folio *folio,
+ gfp_t gfp)
+{
+ return NULL;
+}
+#endif
+
+static void shmem_unused_huge_lru_add(struct shmem_sb_info *sbinfo,
+ struct list_head *item, int nid,
+ struct obj_cgroup *objcg)
+{
+ struct mem_cgroup *memcg;
+
+ rcu_read_lock();
+ memcg = obj_cgroup_memcg(objcg);
+ list_lru_add(&sbinfo->shrinklist, item, nid, memcg);
+ rcu_read_unlock();
+}
+
+static void shmem_unused_huge_lru_del(struct shmem_sb_info *sbinfo,
+ struct list_head *item, int nid,
+ struct obj_cgroup *objcg)
+{
+ struct mem_cgroup *memcg;
+
+ rcu_read_lock();
+ memcg = obj_cgroup_memcg(objcg);
+ list_lru_del(&sbinfo->shrinklist, item, nid, memcg);
+ rcu_read_unlock();
+}
+
+static void shmem_unused_huge_add(struct inode *inode, struct folio *folio,
+ gfp_t gfp)
+{
+ struct shmem_inode_info *info = SHMEM_I(inode);
+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
+ int nid = folio_nid(folio);
+ struct obj_cgroup *objcg = NULL, *old_objcg = NULL;
+
+ objcg = shmem_unused_huge_alloc_lru(sbinfo, folio, gfp);
+ if (IS_ERR(objcg))
+ return;
+
+ spin_lock(&info->lock);
+ if (!list_empty(&info->shrinklist)) {
+ /* isolated on scan list, let shrink handle it */
+ if (is_shmem_unused_huge_isolated(info))
+ goto unlock;
+
+ if (info->shrinklist_nid == nid &&
+ info->shrinklist_objcg == objcg)
+ goto unlock;
+
+ shmem_unused_huge_lru_del(sbinfo, &info->shrinklist,
+ info->shrinklist_nid,
+ info->shrinklist_objcg);
+ old_objcg = shmem_get_and_clear_objcg(info);
+ }
+
+ info->shrinklist_objcg = objcg;
+ info->shrinklist_nid = nid;
+ shmem_unused_huge_lru_add(sbinfo, &info->shrinklist, nid, objcg);
+ objcg = NULL;
+unlock:
+ spin_unlock(&info->lock);
+ obj_cgroup_put(old_objcg);
+ obj_cgroup_put(objcg);
+}
+
+static void shmem_unused_huge_del(struct inode *inode)
+{
+ struct shmem_inode_info *info = SHMEM_I(inode);
+ struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
+ struct obj_cgroup *objcg = NULL;
+
+ spin_lock(&info->lock);
+ if (!list_empty(&info->shrinklist)) {
+ shmem_unused_huge_lru_del(sbinfo, &info->shrinklist,
+ info->shrinklist_nid,
+ info->shrinklist_objcg);
+ objcg = shmem_get_and_clear_objcg(info);
+ }
+ spin_unlock(&info->lock);
+
+ obj_cgroup_put(objcg);
+}
+
+struct shmem_unused_huge_scan {
+ struct list_head list;
+ struct shrink_control *sc;
+};
+
+static enum lru_status shmem_unused_huge_isolate(struct list_head *item,
+ struct list_lru_one *lru,
+ void *arg)
+{
+ struct shmem_unused_huge_scan *scan = arg;
struct shmem_inode_info *info;
- struct folio *folio;
- unsigned long batch = sc ? sc->nr_to_scan : 128;
- unsigned long split = 0, freed = 0;
+ struct inode *inode;
+ struct obj_cgroup *objcg = NULL;
- if (list_empty(&sbinfo->shrinklist))
- return SHRINK_STOP;
+ info = list_entry(item, struct shmem_inode_info, shrinklist);
- spin_lock(&sbinfo->shrinklist_lock);
- list_for_each_safe(pos, next, &sbinfo->shrinklist) {
- info = list_entry(pos, struct shmem_inode_info, shrinklist);
+ /*
+ * Use trylock to avoid ABBA deadlock: add/del path takes info->lock
+ * before the list_lru bucket lock, while here the order is reversed.
+ */
+ if (!spin_trylock(&info->lock))
+ return LRU_SKIP;
- /* pin the inode */
- inode = igrab(&info->vfs_inode);
+ /* pin the inode */
+ inode = igrab(&info->vfs_inode);
+ /* inode is about to be evicted */
+ if (!inode) {
+ list_lru_isolate(lru, item);
+ objcg = shmem_get_and_clear_objcg(info);
+ spin_unlock(&info->lock);
+ obj_cgroup_put(objcg);
+ return LRU_REMOVED;
+ }
- /* inode is about to be evicted */
- if (!inode) {
- list_del_init(&info->shrinklist);
- goto next;
- }
+ 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);
- list_move(&info->shrinklist, &list);
-next:
- sbinfo->shrinklist_len--;
- if (!--batch)
- break;
+ return LRU_REMOVED;
+}
+
+static bool is_shmem_unused_huge_match(struct folio *folio,
+ struct shrink_control *sc)
+{
+ struct mem_cgroup *memcg = NULL;
+ bool match;
+
+ /* shmem quota reclaim has no NUMA node or memcg restriction */
+ if (!sc)
+ return true;
+
+ if (folio_nid(folio) != sc->nid)
+ return false;
+
+ /*
+ * Only non-root memcg reclaim needs to match the folio charge against
+ * sc->memcg. Skip the folio memcg check for global shrinker reclaim and
+ * root memcg reclaim.
+ */
+ if (!sc->memcg || mem_cgroup_is_root(sc->memcg))
+ return true;
+
+ memcg = get_mem_cgroup_from_folio(folio);
+ match = memcg == sc->memcg;
+ mem_cgroup_put(memcg);
+
+ return match;
+}
+
+static void shmem_unused_huge_drop(struct inode *inode)
+{
+ struct shmem_inode_info *info = SHMEM_I(inode);
+
+ spin_lock(&info->lock);
+ list_del_init(&info->shrinklist);
+ spin_unlock(&info->lock);
+}
+
+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;
+ 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_unlock(&sbinfo->shrinklist_lock);
- list_for_each_safe(pos, next, &list) {
- pgoff_t next, end;
+ 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;
+ spin_unlock(&info->lock);
+}
+
+static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
+ struct shrink_control *sc, unsigned long nr_to_free)
+{
+ struct shmem_unused_huge_scan scan;
+ struct inode *inode;
+ struct shmem_inode_info *info;
+ struct folio *folio;
+ struct list_head *pos, *next;
+ unsigned long split = 0, freed = 0;
+
+ INIT_LIST_HEAD(&scan.list);
+ scan.sc = sc;
+ if (sc)
+ list_lru_shrink_walk(&sbinfo->shrinklist, sc,
+ shmem_unused_huge_isolate, &scan);
+ else
+ list_lru_walk(&sbinfo->shrinklist, shmem_unused_huge_isolate,
+ &scan, 128);
+
+ list_for_each_safe(pos, next, &scan.list) {
+ pgoff_t folio_end, end;
loff_t i_size;
int ret;
info = list_entry(pos, struct shmem_inode_info, shrinklist);
inode = &info->vfs_inode;
- if (nr_to_free && freed >= nr_to_free)
- goto move_back;
-
i_size = i_size_read(inode);
folio = filemap_get_entry(inode->i_mapping, i_size / PAGE_SIZE);
if (!folio || xa_is_value(folio))
@@ -782,13 +989,19 @@ static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
}
/* Check if there is anything to gain from splitting */
- next = folio_next_index(folio);
+ folio_end = folio_next_index(folio);
end = shmem_fallocend(inode, DIV_ROUND_UP(i_size, PAGE_SIZE));
- if (end <= folio->index || end >= next) {
+ if (end <= folio->index || end >= folio_end) {
folio_put(folio);
goto drop;
}
+ if (!is_shmem_unused_huge_match(folio, scan.sc))
+ goto move_back;
+
+ if (nr_to_free && freed >= nr_to_free)
+ goto move_back;
+
/*
* Move the inode on the list back to shrinklist if we failed
* to lock the page at this time.
@@ -796,35 +1009,30 @@ static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
* Waiting for the lock may lead to deadlock in the
* reclaim path.
*/
- if (!folio_trylock(folio)) {
- folio_put(folio);
+ if (!folio_trylock(folio))
+ goto move_back;
+
+ if (!is_shmem_unused_huge_match(folio, scan.sc)) {
+ folio_unlock(folio);
goto move_back;
}
ret = split_folio(folio);
folio_unlock(folio);
- folio_put(folio);
/* If split failed move the inode on the list back to shrinklist */
if (ret)
goto move_back;
- freed += next - end;
+ freed += folio_end - end;
split++;
+ folio_put(folio);
drop:
- list_del_init(&info->shrinklist);
+ shmem_unused_huge_drop(inode);
goto put;
move_back:
- /*
- * Make sure the inode is either on the global list or deleted
- * from any local list before iput() since it could be deleted
- * in another thread once we put the inode (then the local list
- * is corrupted).
- */
- spin_lock(&sbinfo->shrinklist_lock);
- list_move(&info->shrinklist, &sbinfo->shrinklist);
- sbinfo->shrinklist_len++;
- spin_unlock(&sbinfo->shrinklist_lock);
+ shmem_unused_huge_requeue(inode, folio);
+ folio_put(folio);
put:
iput(inode);
}
@@ -837,7 +1045,7 @@ static long shmem_unused_huge_scan(struct super_block *sb,
{
struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
- if (!READ_ONCE(sbinfo->shrinklist_len))
+ if (!list_lru_shrink_count(&sbinfo->shrinklist, sc))
return SHRINK_STOP;
return shmem_unused_huge_shrink(sbinfo, sc, 0);
@@ -848,21 +1056,21 @@ static long shmem_unused_huge_count(struct super_block *sb,
{
struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
- /*
- * The per-superblock shrinklist is filesystem-global and does not
- * honour sc->memcg, so it is only meaningful on the global (kswapd or
- * root direct reclaim) shrink path. Skip the per-memcg iterations of
- * shrink_slab_memcg() to avoid queueing duplicate global work.
- */
- if (!mem_cgroup_shrink_is_root(sc))
- return 0;
-
- return READ_ONCE(sbinfo->shrinklist_len);
+ return list_lru_shrink_count(&sbinfo->shrinklist, sc);
}
#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
#define shmem_huge SHMEM_HUGE_DENY
+static void shmem_unused_huge_add(struct inode *inode, struct folio *folio,
+ gfp_t gfp)
+{
+}
+
+static void shmem_unused_huge_del(struct inode *inode)
+{
+}
+
static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
struct shrink_control *sc, unsigned long nr_to_free)
{
@@ -1418,14 +1626,7 @@ static void shmem_evict_inode(struct inode *inode)
inode->i_size = 0;
mapping_set_exiting(inode->i_mapping);
shmem_truncate_range(inode, 0, (loff_t)-1);
- if (!list_empty(&info->shrinklist)) {
- spin_lock(&sbinfo->shrinklist_lock);
- if (!list_empty(&info->shrinklist)) {
- list_del_init(&info->shrinklist);
- sbinfo->shrinklist_len--;
- }
- spin_unlock(&sbinfo->shrinklist_lock);
- }
+ shmem_unused_huge_del(inode);
while (!list_empty(&info->swaplist)) {
/* Wait while shmem_unuse() is scanning this inode... */
wait_var_event(&info->stop_eviction,
@@ -2573,25 +2774,12 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
*/
if (alloced && folio_test_large(folio) &&
DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < folio_next_index(folio)) {
- struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
- struct shmem_inode_info *info = SHMEM_I(inode);
/*
* Part of the large folio is beyond i_size: subject
* to shrink under memory pressure.
*/
- spin_lock(&sbinfo->shrinklist_lock);
- /*
- * _careful to defend against unlocked access to
- * ->shrink_list in shmem_unused_huge_shrink()
- */
- if (list_empty_careful(&info->shrinklist)) {
- list_add_tail(&info->shrinklist,
- &sbinfo->shrinklist);
- sbinfo->shrinklist_len++;
- }
- spin_unlock(&sbinfo->shrinklist_lock);
+ shmem_unused_huge_add(inode, folio, gfp);
}
-
out:
*foliop = folio;
return 0;
@@ -3068,6 +3256,10 @@ static struct inode *__shmem_get_inode(struct mnt_idmap *idmap,
if (info->fsflags)
shmem_set_inode_flags(inode, info->fsflags, NULL);
INIT_LIST_HEAD(&info->shrinklist);
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+ info->shrinklist_objcg = NULL;
+ info->shrinklist_nid = -1;
+#endif
INIT_LIST_HEAD(&info->swaplist);
cache_no_acl(inode);
if (sbinfo->noswap)
@@ -4942,6 +5134,9 @@ static void shmem_put_super(struct super_block *sb)
#endif
free_percpu(sbinfo->ino_batch);
percpu_counter_destroy(&sbinfo->used_blocks);
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+ list_lru_destroy(&sbinfo->shrinklist);
+#endif
mpol_put(sbinfo->mpol);
#ifdef CONFIG_TMPFS_XATTR
simple_xattr_cache_cleanup(&sbinfo->xa_cache);
@@ -5035,8 +5230,11 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
raw_spin_lock_init(&sbinfo->stat_lock);
if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
goto failed;
- spin_lock_init(&sbinfo->shrinklist_lock);
- INIT_LIST_HEAD(&sbinfo->shrinklist);
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+ if (list_lru_init_memcg(&sbinfo->shrinklist, sb->s_shrink))
+ goto failed;
+#endif
sb->s_maxbytes = MAX_LFS_FILESIZE;
sb->s_blocksize = PAGE_SIZE;
--
2.54.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg
2026-08-17 9:03 ` [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg Qi Zheng
@ 2026-08-17 11:29 ` Qi Zheng
2026-08-17 16:05 ` Shakeel Butt
1 sibling, 0 replies; 7+ messages in thread
From: Qi Zheng @ 2026-08-17 11:29 UTC (permalink / raw)
To: hughd, baolin.wang, usama.arif, brauner, akpm, david, Qi Zheng,
Johannes Weiner, Michal Hocko, Muchun Song, Roman Gushchin,
Shakeel Butt
Cc: linux-mm, linux-kernel
CC the maintainers of MEMCG who were missed in the previous email.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg
2026-08-17 9:03 ` [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg Qi Zheng
2026-08-17 11:29 ` Qi Zheng
@ 2026-08-17 16:05 ` Shakeel Butt
1 sibling, 0 replies; 7+ messages in thread
From: Shakeel Butt @ 2026-08-17 16:05 UTC (permalink / raw)
To: Qi Zheng
Cc: hughd, baolin.wang, usama.arif, brauner, akpm, david, linux-mm,
linux-kernel, Qi Zheng
On Mon, Aug 17, 2026 at 05:03:26PM +0800, Qi Zheng wrote:
> From: Qi Zheng <zhengqi.arch@bytedance.com>
>
> obj_cgroup_memcg() currently requires a non-NULL objcg, so callers that
> may hold a NULL objcg must guard the call with an explicit NULL check.
> This pattern is duplicated in folio_memcg(), folio_memcg_check(),
> mm/page_owner.c, and mm/zswap.c.
>
> Teach obj_cgroup_memcg() to accept NULL and return NULL in that case, then
> remove the redundant NULL checks at the call sites. Also remove the
> mem_cgroup_from_entry() wrapper in zswap, which existed solely to provide
> this NULL-safe behaviour, and replace its two callers with direct
> obj_cgroup_memcg() calls.
>
> No functional change intended.
>
> Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-08-17 16:05 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-17 9:03 [PATCH v4 0/4] make unused huge shrinker memcg aware Qi Zheng
2026-08-17 9:03 ` [PATCH v4 1/4] fs: fix missed removal of super_fs_objects_eligible() Qi Zheng
2026-08-17 9:03 ` [PATCH v4 2/4] mm: memcontrol: make obj_cgroup_memcg() handle NULL objcg Qi Zheng
2026-08-17 11:29 ` Qi Zheng
2026-08-17 16:05 ` Shakeel Butt
2026-08-17 9:03 ` [PATCH v4 3/4] mm: shmem: move unused huge shrinklist queuing past the truncation check Qi Zheng
2026-08-17 9:03 ` [PATCH v4 4/4] mm: shmem: make unused huge shrinker memcg aware Qi Zheng
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.