History

fs: push nr_cached_objects memcg gating into individual filesystems

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".

Usama Arif committed 4 months ago

Commit 0ef8faf1 parent 62d98533 files · +41 −0Browse files

Showing the changes to include/linux (1 of 3 files). Show all

Unified · Split

include/linux/memcontrol.h+21−0
@@ -520,6 +520,22 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) · 519 unmodified lines
520520 return (memcg == root_mem_cgroup);
521521 }
522522
523+/**
524+ * mem_cgroup_shrink_is_root - is this a global or root-memcg shrink invocation?
525+ * @sc: shrink_control describing the current shrinker call
526+ *
527+ * Returns true when @sc represents a global reclaim shrink (sc->memcg == NULL)
528+ * or a root-memcg shrink, i.e. not a per-memcg iteration of
529+ * shrink_slab_memcg(). Filesystems whose ->nr_cached_objects()/
530+ * ->free_cached_objects() implementations operate on filesystem-global state
531+ * and do not honour sc->memcg can use this to early-return 0 in per-memcg
532+ * contexts.
533+ */
534+static inline bool mem_cgroup_shrink_is_root(struct shrink_control *sc)
535+{
536+ return !sc->memcg || mem_cgroup_is_root(sc->memcg);
537+}
538+
523539 static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
524540 {
525541 return objcg->is_root;
@@ -1071,6 +1087,11 @@ static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) · 545 unmodified lines
10711087 return true;
10721088 }
10731089
1090+static inline bool mem_cgroup_shrink_is_root(struct shrink_control *sc)
1091+{
1092+ return true;
1093+}
1094+
10741095 static inline bool obj_cgroup_is_root(const struct obj_cgroup *objcg)
10751096 {
10761097 return true;