]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
fs: push nr_cached_objects memcg gating into individual filesystems
authorUsama Arif <usama.arif@linux.dev>
Wed, 15 Jul 2026 10:35:16 +0000 (03:35 -0700)
committerChristian Brauner <brauner@kernel.org>
Thu, 23 Jul 2026 09:35:02 +0000 (11:35 +0200)
Commit 0baad6f9b997 ("fs/super: skip non-memcg-aware nr_cached_objects
in memcg slab shrink") added a check in fs/super.c that skipped every
->nr_cached_objects() hook whenever the shrinker was invoked for a
non-root memcg, on the assumption that none of them honour sc->memcg.

That assumption is wrong for XFS, whose inode-reclaim hook is
intentionally driven from per-memcg contexts to free memcg-charged
slab. Encoding a blanket "never memcg-aware" policy in fs/super.c
short-circuits that path.

Push the check down into the callbacks whose counters really are
irrelevant to per-memcg reclaim - btrfs_nr_cached_objects() and
shmem_unused_huge_count() - and drop the fs/super.c gate. Each
filesystem can now lift the restriction independently if its counter
later grows memcg awareness, without touching fs/super.c.

Introduce mem_cgroup_shrink_is_root() in <linux/memcontrol.h> so the
callbacks don't open-code "sc->memcg is NULL or root".

Fixes: 0baad6f9b997 ("fs/super: skip non-memcg-aware nr_cached_objects in memcg slab shrink")
Acked-by: Qi Zheng <qi.zheng@linux.dev>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Signed-off-by: Usama Arif <usama.arif@linux.dev>
Link: https://patch.msgid.link/20260715103516.2410175-1-usama.arif@linux.dev
Acked-by: David Sterba <dsterba@suse.com>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/btrfs/super.c
include/linux/memcontrol.h
mm/shmem.c

index a7d804219bec34a5de2ff5e7bbca547c2c51a173..cc453743539976a56c25f3a1a6447e67393658c2 100644 (file)
@@ -22,6 +22,7 @@
 #include <linux/namei.h>
 #include <linux/miscdevice.h>
 #include <linux/magic.h>
+#include <linux/memcontrol.h>
 #include <linux/slab.h>
 #include <linux/ratelimit.h>
 #include <linux/crc32c.h>
@@ -2434,6 +2435,15 @@ static long btrfs_nr_cached_objects(struct super_block *sb, struct shrink_contro
        struct btrfs_fs_info *fs_info = btrfs_sb(sb);
        const s64 nr = percpu_counter_read_positive(&fs_info->evictable_extent_maps);
 
+       /*
+        * The evictable extent map counter 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;
+
        trace_btrfs_extent_map_shrinker_count(fs_info, nr);
 
        return nr;
index e1f46a0016fcfd0269552795ca47ef696f59ab1f..5407e4200460a2092ccbd108421fb758b10ac1d0 100644 (file)
@@ -520,6 +520,22 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
        return (memcg == root_mem_cgroup);
 }
 
+/**
+ * mem_cgroup_shrink_is_root - is this a global or root-memcg shrink invocation?
+ * @sc: shrink_control describing the current shrinker call
+ *
+ * Returns true when @sc represents a global reclaim shrink (sc->memcg == NULL)
+ * or a root-memcg shrink, i.e. not a per-memcg iteration of
+ * shrink_slab_memcg(). Filesystems whose ->nr_cached_objects()/
+ * ->free_cached_objects() implementations operate on filesystem-global state
+ * and do not honour sc->memcg can use this to early-return 0 in per-memcg
+ * contexts.
+ */
+static inline bool mem_cgroup_shrink_is_root(struct shrink_control *sc)
+{
+       return !sc->memcg || mem_cgroup_is_root(sc->memcg);
+}
+
 static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
 {
        return objcg->is_root;
@@ -1071,6 +1087,11 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
        return true;
 }
 
+static inline bool mem_cgroup_shrink_is_root(struct shrink_control *sc)
+{
+       return true;
+}
+
 static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
 {
        return true;
index b51f83c970bb37340f2fbf646cb549991235253e..9001aaf3b7b943d23303611b67dce6858568290a 100644 (file)
@@ -846,6 +846,16 @@ static long shmem_unused_huge_count(struct super_block *sb,
                struct shrink_control *sc)
 {
        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);
 }
 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */