Re: [PATCH] skbuff: Reallocate to ksize() in __build_skb_around()

From: Eric Dumazet
Date: Wed Dec 07 2022 - 05:31:12 EST


On Wed, Dec 7, 2022 at 2:56 AM Jakub Kicinski <kuba@xxxxxxxxxx> wrote:
>
> On Tue, 6 Dec 2022 15:17:14 -0800 Kees Cook wrote:
> > - unsigned int size = frag_size ? : ksize(data);
> > + unsigned int size = frag_size;
> > +
> > + /* When frag_size == 0, the buffer came from kmalloc, so we
> > + * must find its true allocation size (and grow it to match).
> > + */
> > + if (unlikely(size == 0)) {
> > + void *resized;
> > +
> > + size = ksize(data);
> > + /* krealloc() will immediate return "data" when
> > + * "ksize(data)" is requested: it is the existing upper
> > + * bounds. As a result, GFP_ATOMIC will be ignored.
> > + */
> > + resized = krealloc(data, size, GFP_ATOMIC);
> > + if (WARN_ON(resized != data))
> > + data = resized;
> > + }
> >
>
> Aammgh. build_skb(0) is plain silly, AFAIK. The performance hit of
> using kmalloc()'ed heads is large because GRO can't free the metadata.
> So we end up carrying per-MTU skbs across to the application and then
> freeing them one by one. With pages we just aggregate up to 64k of data
> in a single skb.
>
> I can only grep out 3 cases of build_skb(.. 0), could we instead
> convert them into a new build_skb_slab(), and handle all the silliness
> in such a new helper? That'd be a win both for the memory safety and one
> fewer branch for the fast path.
>
> I think it's worth doing, so LMK if you're okay to do this extra work,
> otherwise I can help (unless e.g. Eric tells me I'm wrong..).

I totally agree, I would indeed remove ksize() use completely,
let callers give us the size, and the head_frag boolean,
instead of inferring from size==0