Re: [PATCH v2 6/7] gpiolib: avoid krealloc() in gpiod_get_array()
From: Andy Shevchenko
Date: Wed Nov 04 2026 - 07:21:23 EST
On Tue, Nov 03, 2026 at 07:40:34PM -0800, Dmitry Torokhov 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().
...
> +static struct gpio_descs *gpio_descs_alloc(unsigned int count, struct gpio_desc *first_desc)
> +{
> + struct gpio_device *gdev = gpiod_to_gpio_device(first_desc);
> + struct gpio_array *array_info;
> + struct gpio_descs *descs;
> + size_t descs_size, alloc_size;
> + unsigned int bitmap_size;
> +
> + /*
> + * If pin hardware number of array member 0 is also 0, select
> + * its chip as a candidate for fast bitmap processing path.
> + */
> + if (gpiod_hwgpio(first_desc) != 0)
> + return kzalloc_flex(*descs, desc, count);
> +
> + 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);
> +
> + descs = kzalloc(alloc_size, GFP_KERNEL);
> + if (!descs)
> + return NULL;
> +
> + array_info = (void *)descs + descs_size;
> + array_info->get_mask = array_info->invert_mask + bitmap_size;
> + array_info->set_mask = array_info->get_mask + bitmap_size;
> +
> + array_info->desc = descs->desc;
> + array_info->size = count;
> + array_info->gdev = gdev;
> + bitmap_set(array_info->get_mask, 0, count);
> + bitmap_set(array_info->set_mask, 0, count);
You don't need these, as memory is nullified.
Also note that we have bitmap_clear() IIRC.
> + descs->info = array_info;
> +
> + return descs;
> +}
...
> dev_dbg(dev,
> "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
> - array_info->gdev->label, array_info->size,
> - *array_info->get_mask, *array_info->set_mask,
> - *array_info->invert_mask);
> + descs->info->gdev->label, descs->info->size,
> + *descs->info->get_mask, *descs->info->set_mask,
> + *descs->info->invert_mask);
Why not using %*pb for masks?
--
With Best Regards,
Andy Shevchenko