linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Harry Yoo <harry.yoo@oracle.com>
To: Vlastimil Babka <vbabka@kernel.org>
Cc: Qing Wang <wangqing7171@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Suren Baghdasaryan <surenb@google.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] slab: fix memory leak when refill_sheaf() fails
Date: Thu, 12 Mar 2026 12:28:53 +0900	[thread overview]
Message-ID: <abIy9XJkZ-j7KIfQ@hyeyoo> (raw)
In-Reply-To: <7791ab6a-4707-44b4-a868-d88b93502b1f@kernel.org>

On Wed, Mar 11, 2026 at 05:59:03PM +0100, Vlastimil Babka wrote:
> On 3/11/26 12:16, Harry Yoo wrote:
> > On Wed, Mar 11, 2026 at 05:36:17PM +0800, Qing Wang wrote:
> >> When refill_sheaf() partially fills one sheaf (e.g., fills 5 objects
> >> but need to fill 10), it will update sheaf->size and return -ENOMEM.
> >> However, the callers (alloc_full_sheaf() and __pcs_replace_empty_main())
> >> directly call free_empty_sheaf() on failure, which only does kfree(sheaf),
> >> causing the partially allocated objects memory in sheaf->objects[] leaked.
> > 
> > Nice catch, thanks!
> 
> Indeed, thanks!
> 
> > Probably the need to fail new_slab() made it quite hard to trigger and notice.
> 
> Agreed.
> 
> >> Fix this by calling sheaf_flush_unused() before free_empty_sheaf() to
> >> free objects of sheaf->objects[]. And also add a WARN_ON() in
> >> free_empty_sheaf() to catch any future cases where a non-empty sheaf is
> >> being freed.
> >> 
> >> Fixes: 2d517aa09bbc ("slab: add opt-in caching layer of percpu sheaves")
> 
> Actually I think that commit was fine as it was using bulk alloc to refill
> and that was undoing any partial successes. I think this one is correct and
> replaced it so:

Oops, I missed that.

Yeah, at first look I wondered "Well, refill_sheaf()" doesn't partially
fill sheaves!" Then I looked at the code right now and it did :/

In 2d517aa09bbc did not partially refill sheaves as it used
__kmem_cache_alloc_bulk() to refill sheaves,

> Fixes: ed30c4adfc2b ("slab: add optimized sheaf refill from partial list")

and this changed the behavior by replacing it with __refill_objects().
So this is the right Fixes: tag.

> > I think we need to add Cc: stable@vger.kernel.org
> 
> And therefore we don't, unless I'm mistaken.

You're right.

> >> Signed-off-by: Qing Wang <wangqing7171@gmail.com>
> >> ---
> >>  mm/slub.c | 4 ++++
> >>  1 file changed, 4 insertions(+)
> >> 
> >> diff --git a/mm/slub.c b/mm/slub.c
> >> index 20cb4f3b636d..73b2cfd0e123 100644
> >> --- a/mm/slub.c
> >> +++ b/mm/slub.c
> >> @@ -2797,6 +2797,7 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
> >>  	if (s->flags & SLAB_KMALLOC)
> >>  		mark_obj_codetag_empty(sheaf);
> >>  
> >> +	WARN_ON(sheaf->size > 0);
> > 
> > nit: perhaps VM_WARN_ON_ONCE(); will be enough?
> 
> Yep replaced it too.
> 
> Added to slab/for-next-fixes, thanks!

-- 
Cheers,
Harry / Hyeonggon


  reply	other threads:[~2026-03-12  3:29 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-11  9:36 Qing Wang
2026-03-11 11:16 ` Harry Yoo
2026-03-11 11:48   ` Harry Yoo
2026-03-12  2:21     ` Qing Wang
2026-03-12  3:35       ` Harry Yoo
2026-03-11 16:59   ` Vlastimil Babka
2026-03-12  3:28     ` Harry Yoo [this message]
2026-03-11 14:45 ` Hao Li
2026-03-11 16:30   ` Hao Li
2026-03-11 16:54     ` Vlastimil Babka
2026-03-12  4:40       ` Harry Yoo
2026-03-12  4:56         ` Hao Li

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=abIy9XJkZ-j7KIfQ@hyeyoo \
    --to=harry.yoo@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=hao.li@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=wangqing7171@gmail.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