[PATCH v3] mm/slub: free prefilled empty sheaves consistently

hu.shengming@zte.com.cn posted 1 patch 2 weeks ago
mm/slub.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
[PATCH v3] mm/slub: free prefilled empty sheaves consistently
Posted by hu.shengming@zte.com.cn 2 weeks ago
From: Shengming Hu <hu.shengming@zte.com.cn>

Empty sheaves allocated by alloc_empty_sheaf() should be released with
free_empty_sheaf().

Move the empty-sheaf capacity initialization into __alloc_empty_sheaf()
and use it for oversized prefilled sheaves as well.

This keeps the prefill paths consistent: after flushing an oversized or
pfmemalloc sheaf, release the now-empty sheaf through free_empty_sheaf()
instead of kfree().

Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn>
---
Changes in v2:
- Rework the change as suggested by Harry.
- Teach __alloc_empty_sheaf() to initialize capacity and pfmemalloc.
- Allocate oversized prefilled sheaves through __alloc_empty_sheaf().
- Free flushed oversized and pfmemalloc sheaves through free_empty_sheaf().
- Link to v1: https://lore.kernel.org/all/20260521195015105Y4zvKHj0TfPZEujixy9Vo@zte.com.cn/

Changes in v3:
- Address Hao's comments:
- Drop the redundant `pfmemalloc` initialization in
  __alloc_empty_sheaf().
- Keep initializing `capacity` and `pfmemalloc` in the normal-sized
  prefill path, since the sheaf may be reused rather than freshly
  allocated.
- Link to v2: https://lore.kernel.org/all/20260522145900248m-nBcy07_SCDk2ATDWfmg@zte.com.cn/

---
 mm/slub.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 04692a6f9128..f4c06bf43f2d 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2782,6 +2782,7 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,
 		return NULL;

 	sheaf->cache = s;
+	sheaf->capacity = capacity;

 	stat(s, SHEAF_ALLOC);

@@ -5015,21 +5016,15 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size)

 	if (unlikely(size > s->sheaf_capacity)) {

-		sheaf = kzalloc_flex(*sheaf, objects, size, gfp);
+		sheaf = __alloc_empty_sheaf(s, gfp, size);
 		if (!sheaf)
 			return NULL;

 		stat(s, SHEAF_PREFILL_OVERSIZE);
-		sheaf->cache = s;
-		sheaf->capacity = size;

-		/*
-		 * we do not need to care about pfmemalloc here because oversize
-		 * sheaves area always flushed and freed when returned
-		 */
 		if (!__kmem_cache_alloc_bulk(s, gfp, size,
 					     &sheaf->objects[0])) {
-			kfree(sheaf);
+			free_empty_sheaf(s, sheaf);
 			return NULL;
 		}

@@ -5097,7 +5092,7 @@ void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
 	if (unlikely((sheaf->capacity != s->sheaf_capacity)
 		     || sheaf->pfmemalloc)) {
 		sheaf_flush_unused(s, sheaf);
-		kfree(sheaf);
+		free_empty_sheaf(s, sheaf);
 		return;
 	}

-- 
2.25.1
Re: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
Posted by kernel test robot 1 week, 4 days ago
Hi,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/hu-shengming-zte-com-cn/mm-slub-free-prefilled-empty-sheaves-consistently/20260525-211805
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20260525211000387LYqTHmxYL900XIB8qwV3h%40zte.com.cn
patch subject: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
config: powerpc-randconfig-002-20260529 (https://download.01.org/0day-ci/archive/20260529/202605290406.mjAKJqbo-lkp@intel.com/config)
compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/202605290406.mjAKJqbo-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202605290406.mjAKJqbo-lkp@intel.com/

All errors (new ones prefixed by >>):

>> ld.lld: error: relocation R_PPC_ADDR16_HA cannot be used against symbol 'decrementers_next_tb'; recompile with -fPIC
   >>> defined in vmlinux.a(arch/powerpc/kernel/time.o)
   >>> referenced by time.c
   >>>               arch/powerpc/kernel/time.o:(decrementer_set_next_event) in archive vmlinux.a
--
>> ld.lld: error: relocation R_PPC_ADDR16_HA cannot be used against symbol 'ppc_emulated'; recompile with -fPIC
   >>> defined in vmlinux.a(arch/powerpc/kernel/traps.o)
   >>> referenced by align.c
   >>>               arch/powerpc/kernel/align.o:(fix_alignment) in archive vmlinux.a

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
Posted by Vlastimil Babka (SUSE) 1 week, 3 days ago
On 5/28/26 22:29, kernel test robot wrote:
> Hi,
> 
> kernel test robot noticed the following build errors:
> 
> [auto build test ERROR on akpm-mm/mm-everything]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/hu-shengming-zte-com-cn/mm-slub-free-prefilled-empty-sheaves-consistently/20260525-211805
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
> patch link:    https://lore.kernel.org/r/20260525211000387LYqTHmxYL900XIB8qwV3h%40zte.com.cn
> patch subject: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
> config: powerpc-randconfig-002-20260529 (https://download.01.org/0day-ci/archive/20260529/202605290406.mjAKJqbo-lkp@intel.com/config)
> compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90)
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/202605290406.mjAKJqbo-lkp@intel.com/reproduce)
> 
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202605290406.mjAKJqbo-lkp@intel.com/
> 
> All errors (new ones prefixed by >>):
> 
>>> ld.lld: error: relocation R_PPC_ADDR16_HA cannot be used against symbol 'decrementers_next_tb'; recompile with -fPIC
>    >>> defined in vmlinux.a(arch/powerpc/kernel/time.o)
>    >>> referenced by time.c
>    >>>               arch/powerpc/kernel/time.o:(decrementer_set_next_event) in archive vmlinux.a
> --
>>> ld.lld: error: relocation R_PPC_ADDR16_HA cannot be used against symbol 'ppc_emulated'; recompile with -fPIC
>    >>> defined in vmlinux.a(arch/powerpc/kernel/traps.o)
>    >>> referenced by align.c
>    >>>               arch/powerpc/kernel/align.o:(fix_alignment) in archive vmlinux.a

Looks like a false positive, the patch didn't touch any of that.

> --
> 0-DAY CI Kernel Test Service
> https://github.com/intel/lkp-tests/wiki
Re: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
Posted by Harry Yoo 1 week, 6 days ago

On 5/25/26 10:10 PM, hu.shengming@zte.com.cn wrote:
> From: Shengming Hu <hu.shengming@zte.com.cn>
> 
> Empty sheaves allocated by alloc_empty_sheaf() should be released with
> free_empty_sheaf().
> 
> Move the empty-sheaf capacity initialization into __alloc_empty_sheaf()
> and use it for oversized prefilled sheaves as well.
> 
> This keeps the prefill paths consistent: after flushing an oversized or
> pfmemalloc sheaf, release the now-empty sheaf through free_empty_sheaf()
> instead of kfree().
> 
> Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn>
> ---
>   mm/slub.c | 13 ++++---------
>   1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index 04692a6f9128..f4c06bf43f2d 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -2782,6 +2782,7 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,
>   		return NULL;
> 
>   	sheaf->cache = s;
> +	sheaf->capacity = capacity;

the capacity field is only used by oversized sheaves (for now).
It should be outside __alloc_empty_sheaf().

>   	stat(s, SHEAF_ALLOC);
> 
> @@ -5015,21 +5016,15 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size)
> 
>   	if (unlikely(size > s->sheaf_capacity)) {
> 
> -		sheaf = kzalloc_flex(*sheaf, objects, size, gfp);
> +		sheaf = __alloc_empty_sheaf(s, gfp, size);
>   		if (!sheaf)
>   			return NULL;
> 
>   		stat(s, SHEAF_PREFILL_OVERSIZE);

So now SHEAF_ALLOC and SHEAF_FREE include oversized sheaves.
I think that's fine, but can we update the commit message and the comment?

the comment:
SHEAF_ALLOC, /* Allocation of an empty sheaf including oversized ones */
SHEAF_FREE,  /* Freeing of an empty sheaf including oversized ones */

> -		sheaf->cache = s;
> -		sheaf->capacity = size;
> -		/*
> -		 * we do not need to care about pfmemalloc here because oversize
> -		 * sheaves area always flushed and freed when returned
> -		 */

the comment is still worth having?

>   		if (!__kmem_cache_alloc_bulk(s, gfp, size,
>   					     &sheaf->objects[0])) {
> -			kfree(sheaf);
> +			free_empty_sheaf(s, sheaf);
>   			return NULL;
>   		}

-- 
Cheers,
Harry / Hyeonggon
Re: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
Posted by hu.shengming@zte.com.cn 1 week, 6 days ago
Harry worte:
> On 5/25/26 10:10 PM, hu.shengming@zte.com.cn wrote:
> > From: Shengming Hu <hu.shengming@zte.com.cn>
> > 
> > Empty sheaves allocated by alloc_empty_sheaf() should be released with
> > free_empty_sheaf().
> > 
> > Move the empty-sheaf capacity initialization into __alloc_empty_sheaf()
> > and use it for oversized prefilled sheaves as well.
> > 
> > This keeps the prefill paths consistent: after flushing an oversized or
> > pfmemalloc sheaf, release the now-empty sheaf through free_empty_sheaf()
> > instead of kfree().
> > 
> > Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn>
> > ---
> >   mm/slub.c | 13 ++++---------
> >   1 file changed, 4 insertions(+), 9 deletions(-)
> > 
> > diff --git a/mm/slub.c b/mm/slub.c
> > index 04692a6f9128..f4c06bf43f2d 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -2782,6 +2782,7 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,
> >           return NULL;
> > 
> >       sheaf->cache = s;
> > +    sheaf->capacity = capacity;
> 
> the capacity field is only used by oversized sheaves (for now).
> It should be outside __alloc_empty_sheaf().
> 

Thanks for the review.

That makes sense. I will keep the capacity initialization in the
oversized prefill path.

> >       stat(s, SHEAF_ALLOC);
> > 
> > @@ -5015,21 +5016,15 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size)
> > 
> >       if (unlikely(size > s->sheaf_capacity)) {
> > 
> > -        sheaf = kzalloc_flex(*sheaf, objects, size, gfp);
> > +        sheaf = __alloc_empty_sheaf(s, gfp, size);
> >           if (!sheaf)
> >               return NULL;
> > 
> >           stat(s, SHEAF_PREFILL_OVERSIZE);
> 
> So now SHEAF_ALLOC and SHEAF_FREE include oversized sheaves.
> I think that's fine, but can we update the commit message and the comment?
> 
> the comment:
> SHEAF_ALLOC, /* Allocation of an empty sheaf including oversized ones */
> SHEAF_FREE,  /* Freeing of an empty sheaf including oversized ones */
> 

Yes, I will update the stat comments and mention this in the commit message.

> > -        sheaf->cache = s;
> > -        sheaf->capacity = size;
> > -        /*
> > -         * we do not need to care about pfmemalloc here because oversize
> > -         * sheaves area always flushed and freed when returned
> > -         */
> 
> the comment is still worth having?
> 

The pfmemalloc comment is still useful as well. I dropped it too
aggressively, so I will restore it in v4.

> >           if (!__kmem_cache_alloc_bulk(s, gfp, size,
> >                            &sheaf->objects[0])) {
> > -            kfree(sheaf);
> > +            free_empty_sheaf(s, sheaf);
> >               return NULL;
> >           }
> 
> -- 
> Cheers,
> Harry / Hyeonggon

--
With Best Regards,
Shengming
Re: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
Posted by Hao Li 1 week, 6 days ago
On Mon, May 25, 2026 at 09:10:00PM +0800, hu.shengming@zte.com.cn wrote:
> From: Shengming Hu <hu.shengming@zte.com.cn>
> 
> Empty sheaves allocated by alloc_empty_sheaf() should be released with
> free_empty_sheaf().
> 
> Move the empty-sheaf capacity initialization into __alloc_empty_sheaf()
> and use it for oversized prefilled sheaves as well.
> 
> This keeps the prefill paths consistent: after flushing an oversized or
> pfmemalloc sheaf, release the now-empty sheaf through free_empty_sheaf()
> instead of kfree().
> 
> Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn>
> ---
> Changes in v2:
> - Rework the change as suggested by Harry.
> - Teach __alloc_empty_sheaf() to initialize capacity and pfmemalloc.
> - Allocate oversized prefilled sheaves through __alloc_empty_sheaf().
> - Free flushed oversized and pfmemalloc sheaves through free_empty_sheaf().
> - Link to v1: https://lore.kernel.org/all/20260521195015105Y4zvKHj0TfPZEujixy9Vo@zte.com.cn/
> 
> Changes in v3:
> - Address Hao's comments:
> - Drop the redundant `pfmemalloc` initialization in
>   __alloc_empty_sheaf().
> - Keep initializing `capacity` and `pfmemalloc` in the normal-sized
>   prefill path, since the sheaf may be reused rather than freshly
>   allocated.
> - Link to v2: https://lore.kernel.org/all/20260522145900248m-nBcy07_SCDk2ATDWfmg@zte.com.cn/
> 
> ---
>  mm/slub.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index 04692a6f9128..f4c06bf43f2d 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -2782,6 +2782,7 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,
>  		return NULL;
> 
>  	sheaf->cache = s;
> +	sheaf->capacity = capacity;
> 
>  	stat(s, SHEAF_ALLOC);
> 
> @@ -5015,21 +5016,15 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size)
> 
>  	if (unlikely(size > s->sheaf_capacity)) {
> 
> -		sheaf = kzalloc_flex(*sheaf, objects, size, gfp);
> +		sheaf = __alloc_empty_sheaf(s, gfp, size);
>  		if (!sheaf)
>  			return NULL;
> 
>  		stat(s, SHEAF_PREFILL_OVERSIZE);
> -		sheaf->cache = s;
> -		sheaf->capacity = size;
> 
> -		/*
> -		 * we do not need to care about pfmemalloc here because oversize
> -		 * sheaves area always flushed and freed when returned
> -		 */

just a bit curious, why did you decide to delete these comments?

>  		if (!__kmem_cache_alloc_bulk(s, gfp, size,
>  					     &sheaf->objects[0])) {
> -			kfree(sheaf);
> +			free_empty_sheaf(s, sheaf);
>  			return NULL;
>  		}
> 
> @@ -5097,7 +5092,7 @@ void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
>  	if (unlikely((sheaf->capacity != s->sheaf_capacity)
>  		     || sheaf->pfmemalloc)) {
>  		sheaf_flush_unused(s, sheaf);
> -		kfree(sheaf);
> +		free_empty_sheaf(s, sheaf);
>  		return;
>  	}
> 
> -- 
> 2.25.1
Re: [PATCH v3] mm/slub: free prefilled empty sheaves consistently
Posted by hu.shengming@zte.com.cn 1 week, 6 days ago
Hao wrote:
> On Mon, May 25, 2026 at 09:10:00PM +0800, hu.shengming@zte.com.cn wrote:
> > From: Shengming Hu <hu.shengming@zte.com.cn>
> > 
> > Empty sheaves allocated by alloc_empty_sheaf() should be released with
> > free_empty_sheaf().
> > 
> > Move the empty-sheaf capacity initialization into __alloc_empty_sheaf()
> > and use it for oversized prefilled sheaves as well.
> > 
> > This keeps the prefill paths consistent: after flushing an oversized or
> > pfmemalloc sheaf, release the now-empty sheaf through free_empty_sheaf()
> > instead of kfree().
> > 
> > Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn>
> > ---
> > Changes in v2:
> > - Rework the change as suggested by Harry.
> > - Teach __alloc_empty_sheaf() to initialize capacity and pfmemalloc.
> > - Allocate oversized prefilled sheaves through __alloc_empty_sheaf().
> > - Free flushed oversized and pfmemalloc sheaves through free_empty_sheaf().
> > - Link to v1: https://lore.kernel.org/all/20260521195015105Y4zvKHj0TfPZEujixy9Vo@zte.com.cn/
> > 
> > Changes in v3:
> > - Address Hao's comments:
> > - Drop the redundant `pfmemalloc` initialization in
> >   __alloc_empty_sheaf().
> > - Keep initializing `capacity` and `pfmemalloc` in the normal-sized
> >   prefill path, since the sheaf may be reused rather than freshly
> >   allocated.
> > - Link to v2: https://lore.kernel.org/all/20260522145900248m-nBcy07_SCDk2ATDWfmg@zte.com.cn/
> > 
> > ---
> >  mm/slub.c | 13 ++++---------
> >  1 file changed, 4 insertions(+), 9 deletions(-)
> > 
> > diff --git a/mm/slub.c b/mm/slub.c
> > index 04692a6f9128..f4c06bf43f2d 100644
> > --- a/mm/slub.c
> > +++ b/mm/slub.c
> > @@ -2782,6 +2782,7 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,
> >          return NULL;
> > 
> >      sheaf->cache = s;
> > +    sheaf->capacity = capacity;
> > 
> >      stat(s, SHEAF_ALLOC);
> > 
> > @@ -5015,21 +5016,15 @@ kmem_cache_prefill_sheaf(struct kmem_cache *s, gfp_t gfp, unsigned int size)
> > 
> >      if (unlikely(size > s->sheaf_capacity)) {
> > 
> > -        sheaf = kzalloc_flex(*sheaf, objects, size, gfp);
> > +        sheaf = __alloc_empty_sheaf(s, gfp, size);
> >          if (!sheaf)
> >              return NULL;
> > 
> >          stat(s, SHEAF_PREFILL_OVERSIZE);
> > -        sheaf->cache = s;
> > -        sheaf->capacity = size;
> > 
> > -        /*
> > -         * we do not need to care about pfmemalloc here because oversize
> > -         * sheaves area always flushed and freed when returned
> > -         */
> 
> just a bit curious, why did you decide to delete these comments?
> 

Sorry about that.

I removed the comment while moving the oversized sheaf allocation to
__alloc_empty_sheaf(), because the local cache/capacity initialization was
removed from this path. However, looking at it again, the comment is still
worth.

I will restore it in v4.

Thanks a lot.

> >          if (!__kmem_cache_alloc_bulk(s, gfp, size,
> >                           &sheaf->objects[0])) {
> > -            kfree(sheaf);
> > +            free_empty_sheaf(s, sheaf);
> >              return NULL;
> >          }
> > 
> > @@ -5097,7 +5092,7 @@ void kmem_cache_return_sheaf(struct kmem_cache *s, gfp_t gfp,
> >      if (unlikely((sheaf->capacity != s->sheaf_capacity)
> >               || sheaf->pfmemalloc)) {
> >          sheaf_flush_unused(s, sheaf);
> > -        kfree(sheaf);
> > +        free_empty_sheaf(s, sheaf);
> >          return;
> >      }
> > 
> > -- 
> > 2.25.1

--
With Best Regards,
Shengming