Re: [PATCH V2 3/4] mm: shrinker: optimize the allocation of shrinker_info when setting cgroup_memory_nokmem
From: Dave Chinner
Date: Thu Mar 12 2026 - 01:53:04 EST
On Thu, Mar 12, 2026 at 12:08:42PM +0800, Haifeng Xu wrote:
> On 2026/3/12 06:14, Dave Chinner wrote:
> > On Tue, Mar 10, 2026 at 11:12:49AM +0800, Haifeng Xu wrote:
> >> @@ -78,15 +79,25 @@ int alloc_shrinker_info(struct mem_cgroup *memcg)
> >> {
> >> int nid, ret = 0;
> >> int array_size = 0;
> >> + int alloc_nr_max;
> >> +
> >> + if (memcg_kmem_online()) {
> >> + alloc_nr_max = shrinker_nr_max;
> >> + } else {
> >> + if (memcg == root_mem_cgroup)
> >> + alloc_nr_max = shrinker_nr_max;
> >> + else
> >> + alloc_nr_max = shrinker_nonslab_nr_max;
> >> + }
> >
> > What does this do and why does it exist? Why do we need two
> > different indexes and tracking structures when memcg is disabled?
> >
> > If I look at this code outside of this commit context, I have -zero-
> > idea of what all this ... complexity does or is needed for.
> >
> > AFAICT, the code is trying to reduce memcg-aware shrinker
> > registration overhead, yes?
> >
> > If so, please explain where all the overhead is in the first place -
> > if there's a time saving of hundreds of seconds in your workload,
> > then whatever is causing the overhead is going to show up in CPU
> > profiles. What, exactly, is causing all the registration overhead?
> >
> > i.e. there are lots of workloads that create large numbers of
> > containers when memcg is actually enabled, so if registration is
> > costly then the right thing to do here is fix the registration
> > overhead problem.
> >
> > Hacking custom logic into the code to avoid the overhead in your
> > specific special case so you can ignore the problem is not the way
> > we solve problems. We need to solve problems like this in a way that
> > benefits -everyone- regardless of whether they are using memcgs or
> > not.
> >
> > So, please identify where all the overhead in memcg shrinker
> > registration is, and then we can take steps to improve the
> > registration code -for everyone-.
> >
> > -Dave.
>
> When creating containers, we found many threads got stuck and wait shrinker
> lock in our machine with kmem disabled. And we found that the shrinker lock
> was held for a long time when expanding shrinker info. As the number of
> containers increases,the lock holding time can increase from a few milliseconds
> to over one hundred milliseconds.
Ok, but...
> Call stack can be seen as below(based on stable kernel 6.6.102).
>
>
> PID: 4462 TASK: ffff8eff5ca0b500 CPU: 79 COMMAND: "runc:[2:INIT]"
> #0 [ffffc9005b213b10] __schedule at ffffffffa3ad84c0
> #1 [ffffc9005b213bb8] schedule at ffffffffa3ad8988
> #2 [ffffc9005b213bd8] schedule_preempt_disabled at ffffffffa3ad8bae
> #3 [ffffc9005b213be8] rwsem_down_write_slowpath at ffffffffa3adcc5e
> #4 [ffffc9005b213ca8] down_write at ffffffffa3adcf3c
> #5 [ffffc9005b213cc0] __prealloc_shrinker at ffffffffa2db3bf0
> #6 [ffffc9005b213d08] prealloc_shrinker at ffffffffa2db9e0e
> #7 [ffffc9005b213d18] alloc_super at ffffffffa2ebec49
> #8 [ffffc9005b213d48] sget_fc at ffffffffa2ebff48
.... this is exactly why you need to show your working, not just
present your solution.
That is, prealloc_shrinker() API no longer exists in TOT. The way
shrinkers are registered and run was significantly changed in ...
2023 by commit c42d50aefd17 ("mm: shrinker: add infrastructure for
dynamically allocating shrinker").
IOWs, the problem problem you are reporting may not even exist on
TOT kernels. Have you tested your problematic workload on a TOT
kernel, and if so, what were the results?
> We use perf tool to record the cpu consuming. I have posted it in the attachment.
> From the Flame Graph, we can see that the clear_page_erms() and memcpy() in
> expand_one_shrinker_info() are the main sources of overhead.
expand_one_shrinker_info() was somewhat simplified by the above
series, so even if it is still an issue on TOT kernels, it still
likely costs less than on old LTS kernels.
> Therefore, the more shrinkers and memcgs exsit,the process of expanding shrinker
> info takes longer. This is because when expanding shrinker info, we will traverse
> all memcgs an record all shrinkers for them.
Only if the shrinker_info arrays need expanding. This is happening
frequently because the expansion code only expands the shrinker info
arrays by one ID at a time. hence if you create a container that has
half a dozen new filesystems mount in it, the array is being
expanded at least half a dozen times. Maybe multiples of this more,
if the filesystem has multiple memcg-aware shrinkers that it
registers per mount...
IOWs, we need to make the expansion case the rare slow path, not the
common fast path here.
It should be trivial to batch the array expansion (e.g. expand in
multiples of 8/16/32 slots) and then shrinker instantiation overhead
should scale down linearly with increasing expansion batch sizes.
Such an improvement will reduce the memcg aware shrinker
registration overhead on -all- configurations, not just the specific
one you run....
> However, with kmem disabled, memcg slab shrink only call non-slab shrinkers, that's
> to say, we only need to record non-slab shrinker for non-root memcgs. For root
> memcg, we still need to record all shrinkers because global shrink call all shrinkers.
Yes, I know what your patch does - it should be clear that it does
not address the root cause of the problem you are reporting, merely
works around it for your specific use case.
-Dave.
--
Dave Chinner
dgc@xxxxxxxxxx