* [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem
@ 2026-02-25 18:38 Michal Koutný
2026-02-26 0:25 ` Roman Gushchin
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Michal Koutný @ 2026-02-25 18:38 UTC (permalink / raw)
To: Andrew Morton, Dave Chinner, Qi Zheng, Roman Gushchin, Muchun Song
Cc: Jan Kara, linux-mm, linux-kernel, Michal Koutný
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.
Signed-off-by: Michal Koutný <mkoutny@suse.com>
---
mm/shrinker.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/mm/shrinker.c b/mm/shrinker.c
index 4a93fd433689a..7d7302619b7f7 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);
---
base-commit: cd2e103d57e5615f9bb027d772f93b9efd567224
change-id: 20260225-cgroup-ml-nokmem-shrinker-7da42fbcf8f2
Best regards,
--
Michal Koutný <mkoutny@suse.com>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem
2026-02-25 18:38 [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem Michal Koutný
@ 2026-02-26 0:25 ` Roman Gushchin
2026-02-26 7:16 ` Qi Zheng
2026-02-26 7:19 ` Muchun Song
2 siblings, 0 replies; 4+ messages in thread
From: Roman Gushchin @ 2026-02-26 0:25 UTC (permalink / raw)
To: Michal Koutný
Cc: Andrew Morton, Dave Chinner, Qi Zheng, Muchun Song, Jan Kara,
linux-mm, linux-kernel
Michal Koutný <mkoutny@suse.com> writes:
> 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.
>
> Signed-off-by: Michal Koutný <mkoutny@suse.com>
Reviewed-by: Roman Gushchin <roman.gushchin@linux.dev>
Thanks!
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem
2026-02-25 18:38 [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem Michal Koutný
2026-02-26 0:25 ` Roman Gushchin
@ 2026-02-26 7:16 ` Qi Zheng
2026-02-26 7:19 ` Muchun Song
2 siblings, 0 replies; 4+ messages in thread
From: Qi Zheng @ 2026-02-26 7:16 UTC (permalink / raw)
To: Michal Koutný,
Andrew Morton, Dave Chinner, Qi Zheng, Roman Gushchin,
Muchun Song
Cc: Jan Kara, linux-mm, linux-kernel
On 2/26/26 2:38 AM, Michal Koutný wrote:
> 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.
>
> Signed-off-by: Michal Koutný <mkoutny@suse.com>
> ---
> mm/shrinker.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/mm/shrinker.c b/mm/shrinker.c
> index 4a93fd433689a..7d7302619b7f7 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;
Make sense, and can you help update the following comment in
shrinker_alloc() as well:
/*
* The nr_deferred is available on per memcg level for memcg aware
* shrinkers, so only allocate nr_deferred in the following cases:
* - non-memcg-aware shrinkers
* - !CONFIG_MEMCG
* - memcg is disabled by kernel command line
*/
Otherwise:
Acked-by: Qi Zheng <zhengqi.arch@bytedance.com>
Thanks,
Qi
>
> mutex_lock(&shrinker_mutex);
> id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
>
> ---
> base-commit: cd2e103d57e5615f9bb027d772f93b9efd567224
> change-id: 20260225-cgroup-ml-nokmem-shrinker-7da42fbcf8f2
>
> Best regards,
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem
2026-02-25 18:38 [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem Michal Koutný
2026-02-26 0:25 ` Roman Gushchin
2026-02-26 7:16 ` Qi Zheng
@ 2026-02-26 7:19 ` Muchun Song
2 siblings, 0 replies; 4+ messages in thread
From: Muchun Song @ 2026-02-26 7:19 UTC (permalink / raw)
To: Michal Koutný
Cc: Andrew Morton, Dave Chinner, Qi Zheng, Roman Gushchin, Jan Kara,
linux-mm, linux-kernel
> On Feb 26, 2026, at 02:38, Michal Koutný <mkoutny@suse.com> wrote:
>
> 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.
>
> Signed-off-by: Michal Koutný <mkoutny@suse.com>
Reviewed-by: Muchun Song <muchun.song@linux.dev>
Thanks
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-02-26 7:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-25 18:38 [PATCH] mm: Do not allocate shrinker info with cgroup.memory=nokmem Michal Koutný
2026-02-26 0:25 ` Roman Gushchin
2026-02-26 7:16 ` Qi Zheng
2026-02-26 7:19 ` Muchun Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox