From: Dave Chinner <dgc@kernel.org>
To: Haifeng Xu <haifeng.xu@shopee.com>
Cc: akpm@linux-foundation.org, david@fromorbit.com,
roman.gushchin@linux.dev, zhengqi.arch@bytedance.com,
muchun.song@linux.dev, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH V2 3/4] mm: shrinker: optimize the allocation of shrinker_info when setting cgroup_memory_nokmem
Date: Thu, 12 Mar 2026 16:52:47 +1100 [thread overview]
Message-ID: <abJUr1bIs62nQC9Z@dread> (raw)
In-Reply-To: <bc08d009-fa43-44d0-880f-a37cc200a3b9@shopee.com>
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@kernel.org
next prev parent reply other threads:[~2026-03-12 5:53 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-10 3:12 [PATCH V2 0/4] record non-slab shrinkers for non-root memcgs when kmem is disabled Haifeng Xu
2026-03-10 3:12 ` [PATCH V2 1/4] mm: shrinker: add one more parameter in shrinker_id() Haifeng Xu
2026-03-10 3:12 ` [PATCH V2 2/4] mm: shrinker: move shrinker_id() code block below memcg_kmem_online() Haifeng Xu
2026-03-10 3:12 ` [PATCH V2 3/4] mm: shrinker: optimize the allocation of shrinker_info when setting cgroup_memory_nokmem Haifeng Xu
2026-03-10 11:05 ` Usama Arif
2026-03-11 2:21 ` Haifeng Xu
2026-03-11 22:14 ` Dave Chinner
[not found] ` <bc08d009-fa43-44d0-880f-a37cc200a3b9@shopee.com>
2026-03-12 5:52 ` Dave Chinner [this message]
2026-03-13 3:04 ` Haifeng Xu
2026-03-10 3:12 ` [PATCH V2 4/4] mm: shrinker: remove unnecessary check in shrink_slab_memcg() Haifeng Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=abJUr1bIs62nQC9Z@dread \
--to=dgc@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=david@fromorbit.com \
--cc=haifeng.xu@shopee.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=zhengqi.arch@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox