Skip to content

Commit 464b1c1

Browse files
Qing WangVlastimil Babka (SUSE)
authored andcommitted
slab: fix memory leak when refill_sheaf() fails
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. 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: ed30c4a ("slab: add optimized sheaf refill from partial list") Signed-off-by: Qing Wang <wangqing7171@gmail.com> Link: https://patch.msgid.link/20260311093617.4155965-1-wangqing7171@gmail.com Reviewed-by: Harry Yoo <harry.yoo@oracle.com> Reviewed-by: Hao Li <hao.li@linux.dev> Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
1 parent 8dafa9f commit 464b1c1

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

mm/slub.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,6 +2790,7 @@ static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
27902790
if (s->flags & SLAB_KMALLOC)
27912791
mark_obj_codetag_empty(sheaf);
27922792

2793+
VM_WARN_ON_ONCE(sheaf->size > 0);
27932794
kfree(sheaf);
27942795

27952796
stat(s, SHEAF_FREE);
@@ -2821,6 +2822,7 @@ static int refill_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf,
28212822
return 0;
28222823
}
28232824

2825+
static void sheaf_flush_unused(struct kmem_cache *s, struct slab_sheaf *sheaf);
28242826

28252827
static struct slab_sheaf *alloc_full_sheaf(struct kmem_cache *s, gfp_t gfp)
28262828
{
@@ -2830,6 +2832,7 @@ static struct slab_sheaf *alloc_full_sheaf(struct kmem_cache *s, gfp_t gfp)
28302832
return NULL;
28312833

28322834
if (refill_sheaf(s, sheaf, gfp | __GFP_NOMEMALLOC | __GFP_NOWARN)) {
2835+
sheaf_flush_unused(s, sheaf);
28332836
free_empty_sheaf(s, sheaf);
28342837
return NULL;
28352838
}
@@ -4616,6 +4619,7 @@ __pcs_replace_empty_main(struct kmem_cache *s, struct slub_percpu_sheaves *pcs,
46164619
* we must be very low on memory so don't bother
46174620
* with the barn
46184621
*/
4622+
sheaf_flush_unused(s, empty);
46194623
free_empty_sheaf(s, empty);
46204624
}
46214625
} else {

0 commit comments

Comments
 (0)