On Tue, Oct 07, 2025 at 03:07:33AM +0100, Matthew Wilcox wrote:
> On Fri, Mar 14, 2025 at 08:15:45PM -0700, Kees Cook wrote:
> > +Performing open-coded kmalloc()-family allocation assignments prevents
> > +the kernel (and compiler) from being able to examine the type of the
> > +variable being assigned, which limits any related introspection that
> > +may help with alignment, wrap-around, or additional hardening. The
> > +kmalloc_obj()-family of macros provide this introspection, which can be
> > +used for the common code patterns for single, array, and flexible object
> > +allocations. For example, these open coded assignments::
> > +
> > + ptr = kmalloc(sizeof(*ptr), gfp);
> > + ptr = kmalloc(sizeof(struct the_type_of_ptr_obj), gfp);
> > + ptr = kzalloc(sizeof(*ptr), gfp);
> > + ptr = kmalloc_array(count, sizeof(*ptr), gfp);
> > + ptr = kcalloc(count, sizeof(*ptr), gfp);
> > + ptr = kmalloc(struct_size(ptr, flex_member, count), gfp);
> > +
> > +become, respectively::
> > +
> > + kmalloc_obj(ptr, gfp);
> > + kzalloc_obj(ptr, gfp);
> > + kmalloc_objs(ptr, count, gfp);
> > + kzalloc_objs(ptr, count, gfp);
> > + kmalloc_flex(ptr, flex_member, count, gfp);
>
> I'd like to propose a different approach for consideration.
>
> The first two are obvious. If we now believe that object type is the
> important thing, well we have an API for that:
>
> struct buffer_head { ... };
>
> bh_cache = KMEM_CACHE(buffer_head, SLAB_RECLAIM_ACCOUNT);
> ...
> ptr = kmem_cache_alloc(bh_cache, GFP_KERNEL);
>
> It's a little more verbose than what you're suggesting, but it does give
> us the chance to specify SLAB_ flags. It already does the alignment
> optimisation you're suggesting. Maybe we can use some macro sugar to
> simplify this.
I just don't think this is remotely feasible. We have tens of thousands
of kmalloc callers in the kernel and that's just not going to work. Also
it's wildly redundant given that the type info is present already in
the proposed series -- the point is ot make this something the allocator
can use (if it wants to) and not depend on the user to do it.
kmem_cache_alloc is just too inflexible.
Doing the mechanical transformation of these callsites is also largely
done already via a Coccinelle script I mentioned in earlier threads:
https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/kmalloc_objs.cocci
https://web.git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=dev/v6.14-rc2/alloc_obj/v4-treewide&id=106fd376feea1699868859e82416d5b7c50866ee
7568 files changed, 16342 insertions, 18580 deletions
> The array ones are a little tougher. Let's set them aside for the
> moment and tackle the really hard one; kmalloc_flex.
>
> Slab is fundamentally built on all objects in the slab being the same
> size. But maybe someone sufficiently enterprising could build a
> "variable sized" slab variant? If we're saying that object type is
> more important a discriminator than object size, then perhaps grouping
> objects of the same size together isn't nearly as useful as grouping
> objects of the same type together, even if they're different sizes.
>
> Might be a good GSoC/outreachy project?
I already did this (see kmem_buckets_create()) and expanded on it
with the per-call-site series I proposed:
https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/
But all of that is orthogonal to just _having_ the type info available.
-Kees
--
Kees Cook