Re: [PATCH v6 4/5] slab: Introduce kmalloc_flex() and family

David Laight
2026-02-24T11:11:58Z

On Tue, 24 Feb 2026 10:26:36 +0000
Lorenzo Stoakes <lorenzo.stoakes@oracle.com> wrote:

> On Wed, Dec 03, 2025 at 03:30:34PM -0800, Kees Cook wrote:
...
> > +/**
> > + * __alloc_flex - Allocate an object that has a trailing flexible array
> > + * @KMALLOC: kmalloc wrapper function to use for allocation.
> > + * @GFP: GFP flags for the allocation.
> > + * @TYPE: type of structure to allocate space for.
> > + * @FAM: The name of the flexible array member of @TYPE structure.
> > + * @COUNT: how many @FAM elements to allocate space for.
> > + *
> > + * Returns: Newly allocated pointer to @TYPE with @COUNT-many trailing
> > + * @FAM elements, or NULL on failure or if @COUNT cannot be represented
> > + * by the member of @TYPE that counts the @FAM elements (annotated via
> > + * __counted_by()).
> > + */
> > +#define __alloc_flex(KMALLOC, GFP, TYPE, FAM, COUNT) \
> > +({ \
> > + const size_t __count = (COUNT); \
> > + const size_t __obj_size = struct_size_t(TYPE, FAM, __count); \
> > + TYPE *__obj_ptr; \
> > + if (WARN_ON_ONCE(overflows_flex_counter_type(TYPE, FAM, __count))) \
> > + __obj_ptr = NULL; \
...
> Annnd now I typed that I realise that Linus fixed this up in mainline and I was
> working with a stale version of this file :))

I think someone else mentioned it, but having a WARN_ON_ONCE() is there
is really a bad idea.
The code bloat must be stunning.

I won't ask why the #define parameters are all UPPER CASE.

David