diff mbox series

[v2,6/7] gpiolib: avoid krealloc() in gpiod_get_array()

Message ID 20261103-b4-gpiolib-free-v2-6-ea46767c9cf8@gmail.com
State New
Headers show
Series gpiolib: scope-based management and cleanup for GPIO descriptor arrays | expand

Commit Message

Dmitry Torokhov Nov. 4, 2026, 3:40 a.m. UTC
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@gmail.com>
---
 drivers/gpio/gpiolib.c | 181 ++++++++++++++++++++++++++-----------------------
 1 file changed, 98 insertions(+), 83 deletions(-)

Comments

sashiko-bot@kernel.org Nov. 4, 2026, 3:48 a.m. UTC | #1
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] gpiolib: delayed initialization of __free() variable in gpiod_get_array()
--

commit ab11568406b8d4aa5255ce95e09509f3e6c86285
Author: Dmitry Torokhov <dmitry.torokhov@gmail.com>

gpiolib: avoid krealloc() in gpiod_get_array()

This commit defers allocating the gpio_descs structure in gpiod_get_array()
until the first GPIO descriptor is retrieved so that it can be allocated in
a single pass without needing krealloc(). It also factors descriptor
allocation and fast-path checks into helper functions.

> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index e71f8865559f6..cf311c1a707c9 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c

[ ... ]

> @@ -5226,105 +5311,35 @@ struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
>  						const char *con_id,
>  						enum gpiod_flags flags)
>  {
> -	struct gpio_desc *desc;
> -	struct gpio_device *gdev;
> -	struct gpio_array *array_info = NULL;
> -	int i, count, bitmap_size;
> -	unsigned long dflags;
> +	struct gpio_descs *descs __free(gpiod_put_array) = NULL;
> +	int i, count;
>  
>  	count = gpiod_count(dev, con_id);
>  	if (count < 0)
>  		return ERR_PTR(count);
>  
> -	struct gpio_descs *descs __free(gpiod_put_array) =
> -		kzalloc_flex(*descs, desc, count);
> -	if (!descs)
> -		return ERR_PTR(-ENOMEM);
> -
>  	for (i = 0; i < count; i++) {
> -		desc = gpiod_get_index(dev, con_id, i, flags);
> +		struct gpio_desc *desc __free(gpiod_put) =
> +			gpiod_get_index(dev, con_id, i, flags);
>  		if (IS_ERR(desc))
>  			return ERR_CAST(desc);
>  
> -		descs->desc[descs->ndescs++] = desc;
> -
> -		gdev = gpiod_to_gpio_device(desc);
> -		/*
> -		 * If pin hardware number of array member 0 is also 0, select
> -		 * its chip as a candidate for fast bitmap processing path.
> -		 */
> -		if (i == 0 && gpiod_hwgpio(desc) == 0) {
> -			struct gpio_descs *array;
> -			size_t descs_size = struct_size(descs, desc, count);
> -
> -			bitmap_size = BITS_TO_LONGS(gdev->ngpio > count ?
> -						    gdev->ngpio : count);
> -
> -			array = krealloc(descs, descs_size +
> -					 struct_size(array_info, invert_mask, 3 * bitmap_size),
> -					 GFP_KERNEL | __GFP_ZERO);
> -			if (!array)
> +		if (i == 0) {
> +			descs = gpio_descs_alloc(count, desc);
> +			if (!descs)
>  				return ERR_PTR(-ENOMEM);
> -
> -			descs = array;
> -
> -			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);
> -			descs->info = array_info;
>  		}

[Severity: Low]
This isn't a bug, but in gpiod_get_array(), descs is declared at the top of
the function with __free(gpiod_put_array) = NULL and then assigned later
inside the loop when i == 0.

The cleanup.h conventions ask that __free() variables be defined and
initialized in a single statement at the point of resource acquisition
rather than initialized to NULL at the top of the function and assigned
later. Could the first descriptor be fetched before the loop, or the
allocation structured so that descs is defined and initialized in one
statement?
Andy Shevchenko Nov. 4, 2026, 12:20 p.m. UTC | #2
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?
Bartosz Golaszewski Nov. 4, 2026, 2:23 p.m. UTC | #3
On Wed, Nov 04, 2026 at 03:48:34AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Low] gpiolib: delayed initialization of __free() variable in gpiod_get_array()
> --
> 
> commit ab11568406b8d4aa5255ce95e09509f3e6c86285
> Author: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> 
> gpiolib: avoid krealloc() in gpiod_get_array()
> 
> This commit defers allocating the gpio_descs structure in gpiod_get_array()
> until the first GPIO descriptor is retrieved so that it can be allocated in
> a single pass without needing krealloc(). It also factors descriptor
> allocation and fast-path checks into helper functions.
>

...

> > +			descs = gpio_descs_alloc(count, desc);
> > +			if (!descs)
> >  				return ERR_PTR(-ENOMEM);
> > -
> > -			descs = array;
> > -
> > -			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);
> > -			descs->info = array_info;
> >  		}
> 
> [Severity: Low]
> This isn't a bug, but in gpiod_get_array(), descs is declared at the top of
> the function with __free(gpiod_put_array) = NULL and then assigned later
> inside the loop when i == 0.
> 
> The cleanup.h conventions ask that __free() variables be defined and
> initialized in a single statement at the point of resource acquisition
> rather than initialized to NULL at the top of the function and assigned
> later. Could the first descriptor be fetched before the loop, or the
> allocation structured so that descs is defined and initialized in one
> statement?
>

Hi Dmitry, please follow the cleanup.h conventions here and in the
subsequent patch.

Bart
Dmitry Torokhov Nov. 4, 2026, 5:13 p.m. UTC | #4
On Wed, Nov 04, 2026 at 03:23:26PM +0100, Bartosz Golaszewski wrote:
> On Wed, Nov 04, 2026 at 03:48:34AM +0000, sashiko-bot@kernel.org wrote:
> > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> > - [Low] gpiolib: delayed initialization of __free() variable in gpiod_get_array()
> > --
> > 
> > commit ab11568406b8d4aa5255ce95e09509f3e6c86285
> > Author: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > 
> > gpiolib: avoid krealloc() in gpiod_get_array()
> > 
> > This commit defers allocating the gpio_descs structure in gpiod_get_array()
> > until the first GPIO descriptor is retrieved so that it can be allocated in
> > a single pass without needing krealloc(). It also factors descriptor
> > allocation and fast-path checks into helper functions.
> >
> 
> ...
> 
> > > +			descs = gpio_descs_alloc(count, desc);
> > > +			if (!descs)
> > >  				return ERR_PTR(-ENOMEM);
> > > -
> > > -			descs = array;
> > > -
> > > -			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);
> > > -			descs->info = array_info;
> > >  		}
> > 
> > [Severity: Low]
> > This isn't a bug, but in gpiod_get_array(), descs is declared at the top of
> > the function with __free(gpiod_put_array) = NULL and then assigned later
> > inside the loop when i == 0.
> > 
> > The cleanup.h conventions ask that __free() variables be defined and
> > initialized in a single statement at the point of resource acquisition
> > rather than initialized to NULL at the top of the function and assigned
> > later. Could the first descriptor be fetched before the loop, or the
> > allocation structured so that descs is defined and initialized in one
> > statement?
> >
> 
> Hi Dmitry, please follow the cleanup.h conventions here and in the
> subsequent patch.

Hi Bart,

The convention (combining declaration with allocation) will not work
here because that would make the allocation have wrong scope.

If you want I can move declaration + NULL initialization just before the
loop. The topic of pulling allocation out of the loop was discussed with
Andy, it would result in duplicating the code and making the result much
worse.

Thanks.
Dmitry Torokhov Nov. 4, 2026, 6:14 p.m. UTC | #5
Hi Andy,

On Wed, Nov 04, 2026 at 02:20:53PM +0200, Andy Shevchenko wrote:
> On Tue, Nov 03, 2026 at 07:40:34PM -0800, Dmitry Torokhov wrote:
> > +	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.

bitmap_set() sets bits 0 .. count - 1 to 1, not 0. Both get_mask and
set_mask start with all count bits set (assuming all pins in the array
are eligible for the fast path), and then gpio_descs_check_fastpath()
clears individual bits for pins that do not qualify.

> >  		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?

Switching the debug format string from %lx to %*pb is orthogonal to this
change, so it is better done as a separate patch.

Thanks.
Andy Shevchenko Nov. 4, 2026, 9:40 p.m. UTC | #6
On Wed, Nov 04, 2026 at 10:14:49AM -0800, Dmitry Torokhov wrote:
> On Wed, Nov 04, 2026 at 02:20:53PM +0200, Andy Shevchenko wrote:
> > On Tue, Nov 03, 2026 at 07:40:34PM -0800, Dmitry Torokhov wrote:

...

> > > +	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.
> 
> bitmap_set() sets bits 0 .. count - 1 to 1, not 0. Both get_mask and
> set_mask start with all count bits set (assuming all pins in the array
> are eligible for the fast path), and then gpio_descs_check_fastpath()
> clears individual bits for pins that do not qualify.

Ah, indeed, but we still have bitmap_fill().

...

> > >  		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?
> 
> Switching the debug format string from %lx to %*pb is orthogonal to this
> change, so it is better done as a separate patch.

Sure.
Dmitry Torokhov Nov. 4, 2026, 9:51 p.m. UTC | #7
On Wed, Nov 04, 2026 at 11:40:18PM +0200, Andy Shevchenko wrote:
> On Wed, Nov 04, 2026 at 10:14:49AM -0800, Dmitry Torokhov wrote:
> > On Wed, Nov 04, 2026 at 02:20:53PM +0200, Andy Shevchenko wrote:
> > > On Tue, Nov 03, 2026 at 07:40:34PM -0800, Dmitry Torokhov wrote:
> > > > +	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.
> > 
> > bitmap_set() sets bits 0 .. count - 1 to 1, not 0. Both get_mask and
> > set_mask start with all count bits set (assuming all pins in the array
> > are eligible for the fast path), and then gpio_descs_check_fastpath()
> > clears individual bits for pins that do not qualify.
>
> Ah, indeed, but we still have bitmap_fill().

bitmap_fill(dst, count) fills whole unsigned long words with 0xff
without masking the tail bits in the last word. Here get_mask and
set_mask are sized to max(gdev->ngpio, count) bits and passed to
gpio_chip_get_multiple() and gpiochip_set_multiple(), which iterate up
to gc->ngpio bits.

When count < gdev->ngpio, bitmap_fill(mask, count) would leave bits
count .. round_up(count, BITS_PER_LONG) - 1 set to 1, causing
get_multiple/set_multiple to touch unrelated pins on the chip and access
value_bitmap beyond count bits. So we really need bitmap_set(..., 0,
count) here to set only the first count bits.

Thanks.
Andy Shevchenko Nov. 4, 2026, 10:05 p.m. UTC | #8
On Wed, Nov 04, 2026 at 01:51:52PM -0800, Dmitry Torokhov wrote:
> On Wed, Nov 04, 2026 at 11:40:18PM +0200, Andy Shevchenko wrote:
> > On Wed, Nov 04, 2026 at 10:14:49AM -0800, Dmitry Torokhov wrote:
> > > On Wed, Nov 04, 2026 at 02:20:53PM +0200, Andy Shevchenko wrote:
> > > > On Tue, Nov 03, 2026 at 07:40:34PM -0800, Dmitry Torokhov wrote:

...

> > > > > +	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.
> > > 
> > > bitmap_set() sets bits 0 .. count - 1 to 1, not 0. Both get_mask and
> > > set_mask start with all count bits set (assuming all pins in the array
> > > are eligible for the fast path), and then gpio_descs_check_fastpath()
> > > clears individual bits for pins that do not qualify.
> >
> > Ah, indeed, but we still have bitmap_fill().
> 
> bitmap_fill(dst, count) fills whole unsigned long words with 0xff
> without masking the tail bits in the last word. Here get_mask and
> set_mask are sized to max(gdev->ngpio, count) bits and passed to
> gpio_chip_get_multiple() and gpiochip_set_multiple(), which iterate up
> to gc->ngpio bits.
> 
> When count < gdev->ngpio, bitmap_fill(mask, count) would leave bits
> count .. round_up(count, BITS_PER_LONG) - 1 set to 1, causing
> get_multiple/set_multiple to touch unrelated pins on the chip and access
> value_bitmap beyond count bits. So we really need bitmap_set(..., 0,
> count) here to set only the first count bits.

I see the point, thanks for elaboration.
diff mbox series

Patch

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index e71f8865559f..cf311c1a707c 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -5208,6 +5208,91 @@  static void gpiochip_free_hogs(struct gpio_chip *gc)
 		gpiochip_free_own_desc(desc);
 }
 
+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);
+	descs->info = array_info;
+
+	return descs;
+}
+
+static void gpio_descs_check_fastpath(struct gpio_descs *descs,
+				      struct gpio_desc *desc)
+{
+	unsigned int idx = descs->ndescs;
+	struct gpio_array *array_info;
+	unsigned long dflags;
+
+	array_info = descs->info;
+	if (!array_info)
+		return;
+
+	/* Unmark array members which don't belong to the 'fast' chip */
+	if (array_info->gdev != gpiod_to_gpio_device(desc)) {
+		__clear_bit(idx, array_info->get_mask);
+		__clear_bit(idx, array_info->set_mask);
+		return;
+	}
+
+	/*
+	 * Detect array members which belong to the 'fast' chip
+	 * but their pins are not in hardware order.
+	 */
+	if (gpiod_hwgpio(desc) != idx) {
+		/*
+		 * Don't use fast path if all array members processed so
+		 * far belong to the same chip as this one but its pin
+		 * hardware number is different from its array index.
+		 */
+		if (bitmap_full(array_info->get_mask, idx)) {
+			descs->info = NULL;
+		} else {
+			__clear_bit(idx, array_info->get_mask);
+			__clear_bit(idx, array_info->set_mask);
+		}
+		return;
+	}
+
+	dflags = READ_ONCE(desc->flags);
+	/* Exclude open drain or open source from fast output */
+	if (test_bit(GPIOD_FLAG_OPEN_DRAIN, &dflags) ||
+	    test_bit(GPIOD_FLAG_OPEN_SOURCE, &dflags))
+		__clear_bit(idx, array_info->set_mask);
+
+	/* Identify 'fast' pins which require invertion */
+	if (gpiod_is_active_low(desc))
+		__set_bit(idx, array_info->invert_mask);
+}
+
 /**
  * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
  * @dev:	GPIO consumer, can be NULL for system-global GPIOs
@@ -5226,105 +5311,35 @@  struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
 						const char *con_id,
 						enum gpiod_flags flags)
 {
-	struct gpio_desc *desc;
-	struct gpio_device *gdev;
-	struct gpio_array *array_info = NULL;
-	int i, count, bitmap_size;
-	unsigned long dflags;
+	struct gpio_descs *descs __free(gpiod_put_array) = NULL;
+	int i, count;
 
 	count = gpiod_count(dev, con_id);
 	if (count < 0)
 		return ERR_PTR(count);
 
-	struct gpio_descs *descs __free(gpiod_put_array) =
-		kzalloc_flex(*descs, desc, count);
-	if (!descs)
-		return ERR_PTR(-ENOMEM);
-
 	for (i = 0; i < count; i++) {
-		desc = gpiod_get_index(dev, con_id, i, flags);
+		struct gpio_desc *desc __free(gpiod_put) =
+			gpiod_get_index(dev, con_id, i, flags);
 		if (IS_ERR(desc))
 			return ERR_CAST(desc);
 
-		descs->desc[descs->ndescs++] = desc;
-
-		gdev = gpiod_to_gpio_device(desc);
-		/*
-		 * If pin hardware number of array member 0 is also 0, select
-		 * its chip as a candidate for fast bitmap processing path.
-		 */
-		if (i == 0 && gpiod_hwgpio(desc) == 0) {
-			struct gpio_descs *array;
-			size_t descs_size = struct_size(descs, desc, count);
-
-			bitmap_size = BITS_TO_LONGS(gdev->ngpio > count ?
-						    gdev->ngpio : count);
-
-			array = krealloc(descs, descs_size +
-					 struct_size(array_info, invert_mask, 3 * bitmap_size),
-					 GFP_KERNEL | __GFP_ZERO);
-			if (!array)
+		if (i == 0) {
+			descs = gpio_descs_alloc(count, desc);
+			if (!descs)
 				return ERR_PTR(-ENOMEM);
-
-			descs = array;
-
-			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);
-			descs->info = array_info;
 		}
 
-		/* If there is no cache for fast bitmap processing path, continue */
-		if (!array_info)
-			continue;
+		gpio_descs_check_fastpath(descs, desc);
 
-		/* Unmark array members which don't belong to the 'fast' chip */
-		if (array_info->gdev != gdev) {
-			__clear_bit(i, array_info->get_mask);
-			__clear_bit(i, array_info->set_mask);
-		}
-		/*
-		 * Detect array members which belong to the 'fast' chip
-		 * but their pins are not in hardware order.
-		 */
-		else if (gpiod_hwgpio(desc) != i) {
-			/*
-			 * Don't use fast path if all array members processed so
-			 * far belong to the same chip as this one but its pin
-			 * hardware number is different from its array index.
-			 */
-			if (bitmap_full(array_info->get_mask, i)) {
-				descs->info = NULL;
-				array_info = NULL;
-			} else {
-				__clear_bit(i, array_info->get_mask);
-				__clear_bit(i, array_info->set_mask);
-			}
-		} else {
-			dflags = READ_ONCE(desc->flags);
-			/* Exclude open drain or open source from fast output */
-			if (test_bit(GPIOD_FLAG_OPEN_DRAIN, &dflags) ||
-			    test_bit(GPIOD_FLAG_OPEN_SOURCE, &dflags))
-				__clear_bit(i, array_info->set_mask);
-			/* Identify 'fast' pins which require invertion */
-			if (gpiod_is_active_low(desc))
-				__set_bit(i, array_info->invert_mask);
-		}
+		descs->desc[descs->ndescs++] = no_free_ptr(desc);
 	}
-	if (array_info)
+	if (descs->info)
 		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);
 	return_ptr(descs);
 }
 EXPORT_SYMBOL_GPL(gpiod_get_array);