The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Harry Yoo <harry@kernel.org>
To: hu.shengming@zte.com.cn
Cc: vbabka@kernel.org, akpm@linux-foundation.org, hao.li@linux.dev,
	cl@gentwo.org, rientjes@google.com, roman.gushchin@linux.dev,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	zhang.run@zte.com.cn, cai.qu@zte.com.cn
Subject: Re: [PATCH] mm/slub: fix lost local objects when bulk remote free batch fills
Date: Mon, 6 Jul 2026 11:51:18 +0900	[thread overview]
Message-ID: <026e7be6-fa97-46b0-8daf-57fb890db0b4@kernel.org> (raw)
In-Reply-To: <20260705213758375Xf8iZzfLz92U9SASpvqTJ@zte.com.cn>


[-- Attachment #1.1: Type: text/plain, Size: 3920 bytes --]



On 7/5/26 10:37 PM, hu.shengming@zte.com.cn wrote:
> Harry wrote:
>> Hi Shengming,
> 
> Hi Harry,
> 
>> On 7/4/26 5:04 PM, hu.shengming@zte.com.cn wrote:
>>> From: Shengming Hu <hu.shengming@zte.com.cn>
>>>
>>> In free_to_pcs_bulk(), when remote_objects[] fills to PCS_BATCH_MAX,
>>> the code jumps to flush_remote to free the batch. If all remote entries
>>> have already been compacted out of p[] via tail swaps while local objects
>>> remain, the flush_remote path returns early since `i < size` no longer
>>> holds. The leftover local objects are then neither cached in the sheaf 
>>> nor returned to the slab freelist, causing a memory leak.
>>
>> Ouch.
>>
>> Interestingly, we fixed a similar memory leak in free_to_pcs_bulk()
>> during the v6.18 cycle; commit cbcff934fa7d ("mm/slub: fix memory leak
>> in free_to_pcs_bulk()").
>>
>> Hmm, I feel is a signal that the control flow is quite complicated and
>> worth revisiting if we could simplify the logic there. I'm not saying we
>> should do that to fix the bug, just mentioning.
> 
> Yes, this is also a memory leak. I agree that the control flow in
> free_to_pcs_bulk() is fairly subtle, especially considering the previous
> leak fix.
> 
> My intention with this patch is only to remove this problematic path with
> a localized change.

That's fair!

>>> For illustration:
>>>  size = 64, local objects at p[0..31], remote objects at p[32..63]
>>>  After scanning all remotes: i = 32, size = 32
>>>  p[0..31] local objects are dropped.
>>
>> I can see that could indeed happen.
>> Interesting. How has it been undiscovered until today?
>>
>> Well, for this to trigger, at least PCS_BATCH_MAX (hardcoded to 32)
>> objects in the sheaf should be from remote nodes.
>>
>> Looking at kmem_cache_free_bulk() users:
>> - maple_node has sheaf_capacity = 32
>> - skbuff_head_cache has sheaf_capacity = 28
>> - panthor and msm drivers have sheaf_capacity = 4
>>
>> The capacity is (at least for now) purely based on the object size
>> (with user-requested capacity as a minimum)
>>
>> Only maple_node cache (out of four users) has
>> sheaf_capacity >= PCS_BATCH_MAX.
>>
>> However, for this to trigger in maple_node cache, ALL objects in the
>> sheaf should have been from remote nodes. And that means there is no
>> local object to leak :)
>>
>> So, although the logic has a leak, you cannot trigger this in reality yet.
>>
> 
> Thanks for the detailed analysis!
> 
> Yes, this was found by code review rather than from a runtime report, and
> I can not verified it with an actual triggering workload.
>
> I agree that it does not seem triggerable with the current users. This fix
> is mainly intended to make the code robust against possible future users or
> changes to PCS_BATCH_MAX  :)

Could we have these explanations/analysis above
in the commit message please? :)

But yeah, we should fix this.

When fixing a bug usually I prefer to go through the process...

 1) Confirm the bug exists by reproducing it, and
 2) Confirm that the patch indeed fixes the bug

But this one is impossible to reproduce, uh.

In this case I think it's better not to Cc stable (which is already the
case). In case there is an error in the analysis, it's still
straightforward to backport later.

I will double check if the patch is indeed correct.

Thanks!

>>> Replace the goto-based flush with an in-place free of full remote batches
>>> during the scan, then resume processing the compacted array. All local
>>> objects now go through the normal fast path, and the tail path only
>>> handles any leftover partial remote batch. The redundant next_remote_batch
>>> jump label is removed as well.
>>>
>>> Fixes: <989b09b73978>("slab: skip percpu sheaves for remote object freeing")
>>> Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn

-- 
Cheers,
Harry / Hyeonggon

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

  reply	other threads:[~2026-07-06  2:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-04  8:04 [PATCH] mm/slub: fix lost local objects when bulk remote free batch fills hu.shengming
2026-07-05  4:46 ` Harry Yoo
2026-07-05 13:37   ` hu.shengming
2026-07-06  2:51     ` Harry Yoo [this message]
2026-07-06  4:36       ` hu.shengming

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=026e7be6-fa97-46b0-8daf-57fb890db0b4@kernel.org \
    --to=harry@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=cai.qu@zte.com.cn \
    --cc=cl@gentwo.org \
    --cc=hao.li@linux.dev \
    --cc=hu.shengming@zte.com.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=zhang.run@zte.com.cn \
    /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