Re: [PATCH v5 5/6] mm/slab: allow freeptr_offset to be used with ctor

From: Vlastimil Babka
Date: Tue Dec 10 2024 - 06:01:44 EST


On 12/6/24 23:52, Suren Baghdasaryan wrote:
> There is no real reason to prevent freeptr_offset usage when a slab
> cache has a ctor. The only real limitation is that any field unioned
> with the free pointer and initialized by ctor will be overwritten since
> free pointer is set after @ctor invocation. Document this limitation
> and enable usage of freeptr_offset with ctor.
>
> Suggested-by: Vlastimil Babka <vbabka@xxxxxxx>
> Signed-off-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>

Reviewed-by: Vlastimil Babka <vbabka@xxxxxxx>

> ---
> include/linux/slab.h | 5 +++--
> mm/slub.c | 2 +-
> 2 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index 681b685b6c4e..6bad744bef5e 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -305,8 +305,9 @@ struct kmem_cache_args {
> * Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset
> * is specified, %use_freeptr_offset must be set %true.
> *
> - * Note that @ctor currently isn't supported with custom free pointers
> - * as a @ctor requires an external free pointer.
> + * Note that fields unioned with free pointer cannot be initialized by
> + * @ctor since free pointer is set after @ctor invocation, so those
> + * values will be overwritten.
> */
> unsigned int freeptr_offset;
> /**
> diff --git a/mm/slub.c b/mm/slub.c
> index 870a1d95521d..f62c829b7b6b 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -5462,7 +5462,7 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)
> s->inuse = size;
>
> if (((flags & SLAB_TYPESAFE_BY_RCU) && !args->use_freeptr_offset) ||
> - (flags & SLAB_POISON) || s->ctor ||
> + (flags & SLAB_POISON) || (s->ctor && !args->use_freeptr_offset) ||
> ((flags & SLAB_RED_ZONE) &&
> (s->object_size < sizeof(void *) || slub_debug_orig_size(s)))) {
> /*