Re: [PATCH v3 6/8] gpiolib: avoid krealloc() in gpiod_get_array()

From: Andy Shevchenko

Date: Sat Nov 07 2026 - 06:16:48 EST


On Fri, Nov 6, 2026 at 3:56 PM Linus Walleij <linusw@xxxxxxxxxx> wrote:
> On Fri, Nov 6, 2026 at 7:30 AM Dmitry Torokhov
> <dmitry.torokhov@xxxxxxxxx> wrote:
>
> > Instead of allocating descs before the loop and reallocating it via
> > krealloc() when candidate hardware array matching is detected on the
> > first iteration, defer allocation of descs until the first descriptor
> > is retrieved.
> >
> > Factor descriptor allocation and fast-path validation into helper
> > functions gpio_descs_alloc() and gpio_descs_check_fastpath(). Inside
> > the loop, acquire the descriptor using __free(gpiod_put), allocate
> > descs once on the first iteration, check fast-path candidate
> > suitability, and transfer descriptor ownership using no_free_ptr().
> >
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
>
> The kernel clearly looks better after this patch than before so:
> Reviewed-by: Linus Walleij <linusw@xxxxxxxxxx>
>
> > + descs_size = struct_size(descs, desc, count);
> > + bitmap_size = BITS_TO_LONGS(max(gdev->ngpio, count));
> > + alloc_size = descs_size + struct_size(array_info, invert_mask, 3 * bitmap_size);
>
> Nitpick:
> I know this is not your code, but if you respin try to add a comment
> here explaining this 3 * bitmap_size.

And the very next patch will remove that as it makes obvious what's
going on. Since it's not a fix, I don't think we need such a
short-live comment.


--
With Best Regards,
Andy Shevchenko