[PATCH v2] mm: Do not allocate shrinker info with cgroup.memory=nokmem
From: Michal Koutný
Date: Mon Mar 09 2026 - 11:14:17 EST
There'd be no work for memcg-aware shrinkers when kernel memory is not
accounted per cgroup, so we can skip allocating per memcg shrinker data.
This saves some memory, avoids holding shrinker_mutex with O(nr_memcgs)
and saves work in shrink_slab_memcg().
Then there are SHRINKER_NONSLAB shrinkers which handle non-kernel memory
so nokmem should not disable their per-memcg behavior. Such shrinkers
(e.g. deferred_split_shrinker) still need access to per-memcg data (see
also commit 0a432dcbeb32e ("mm: shrinker: make shrinker not depend on
memcg kmem")).
The savings with this patch come on container hosts that create many
superblocks (each with own shrinker) but tracking and processing
per-memcg data is pointless with nokmem (shrink_slab_memcg() is
partially guarded with !memcg_kmem_online already).
The patch uses "boottime" predicate mem_cgroup_kmem_disabled() (not
memcg_kmem_online()) to avoid mistakenly un-MEMCG_AWARE-ing shrinkers
registered before first non-root memcg is mkdir'd.
Reviewed-by: Muchun Song <muchun.song@xxxxxxxxx>
Acked-by: Qi Zheng <zhengqi.arch@xxxxxxxxxxxxx>
Reviewed-by: Roman Gushchin <roman.gushchin@xxxxxxxxx>
Signed-off-by: Michal Koutný <mkoutny@xxxxxxxx>
---
Changes in v2:
- update comment (Qi)
- collect trailers
- rebase on master
- Link to v1: https://lore.kernel.org/r/20260225-cgroup-ml-nokmem-shrinker-v1-1-d703899bdda4@xxxxxxxx
---
mm/shrinker.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/shrinker.c b/mm/shrinker.c
index 7b61fc0ee78fb..167cd70ae4918 100644
--- a/mm/shrinker.c
+++ b/mm/shrinker.c
@@ -219,6 +219,8 @@ static int shrinker_memcg_alloc(struct shrinker *shrinker)
if (mem_cgroup_disabled())
return -ENOSYS;
+ if (mem_cgroup_kmem_disabled() && !(shrinker->flags & SHRINKER_NONSLAB))
+ return -ENOSYS;
mutex_lock(&shrinker_mutex);
id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
@@ -716,6 +718,7 @@ struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...)
* - non-memcg-aware shrinkers
* - !CONFIG_MEMCG
* - memcg is disabled by kernel command line
+ * - non-slab shrinkers: when memcg kmem is disabled
*/
size = sizeof(*shrinker->nr_deferred);
if (flags & SHRINKER_NUMA_AWARE)
---
base-commit: 1f318b96cc84d7c2ab792fcc0bfd42a7ca890681
change-id: 20260225-cgroup-ml-nokmem-shrinker-7da42fbcf8f2
Best regards,
--
Michal Koutný <mkoutny@xxxxxxxx>