The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Harry Yoo <harry@kernel.org>
To: hu.shengming@zte.com.cn, vbabka@kernel.org, akpm@linux-foundation.org
Cc: 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: Sun, 5 Jul 2026 13:46:17 +0900	[thread overview]
Message-ID: <7f211fa8-e2d8-4878-a8db-fd275d4c6821@kernel.org> (raw)
In-Reply-To: <20260704160450302fnznThT3hBeBiwlhE-oGv@zte.com.cn>


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

Hi Shengming,

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.

> 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.

> 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-05  4:46 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 [this message]
2026-07-05 13:37   ` hu.shengming
2026-07-06  2:51     ` Harry Yoo
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=7f211fa8-e2d8-4878-a8db-fd275d4c6821@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