| Message ID | 20261103-b4-gpiolib-free-v2-7-ea46767c9cf8@gmail.com |
|---|---|
| State | New |
| Headers | show |
| Series | gpiolib: scope-based management and cleanup for GPIO descriptor arrays | expand |
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] gpiolib: define and initialize __free() variable in gpio_descs_alloc() -- commit 014858138dec2ff00e5cb0fd3fa050587447ef44 Author: Dmitry Torokhov <dmitry.torokhov@gmail.com> gpiolib: use bitmap_zalloc() for fastpath bitmaps This commit converts invert_mask in struct gpio_array to a pointer and allocates the get_mask, set_mask, and invert_mask fastpath bitmaps individually via bitmap_zalloc() instead of a single combined kzalloc() with manual pointer arithmetic. > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c > index cf311c1a707c9..bba4e5bb125f9 100644 > --- a/drivers/gpio/gpiolib.c > +++ b/drivers/gpio/gpiolib.c > @@ -5208,13 +5208,28 @@ static void gpiochip_free_hogs(struct gpio_chip *gc) [ ... ] > +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_descs *descs __free(gpiod_put_array) = NULL; > struct gpio_array *array_info; > - struct gpio_descs *descs; > size_t descs_size, alloc_size; > - unsigned int bitmap_size; > + unsigned int nbits; > > /* > * If pin hardware number of array member 0 is also 0, select > @@ -5224,25 +5239,36 @@ static struct gpio_descs *gpio_descs_alloc(unsigned int count, struct gpio_desc > 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); > + alloc_size = descs_size + sizeof(*array_info); > > descs = kzalloc(alloc_size, GFP_KERNEL); [Severity: Low] This isn't a bug, but cleanup.h conventions prefer defining and initializing __free() variables in a single statement rather than declaring them as NULL at the top of the function and assigning them later. In gpio_descs_alloc(), could descs be defined and initialized directly at the kzalloc() call site after computing descs_size (using struct_size_t(struct gpio_descs, desc, count)) and alloc_size?
On Tue, Nov 03, 2026 at 07:40:35PM -0800, Dmitry Torokhov wrote: > Currently, struct gpio_array embeds invert_mask[] as a flexible array > member, and gpio_descs_alloc() allocates the descriptor array, > struct gpio_array, and all three fastpath bitmaps in a single kzalloc() > call using manual BITS_TO_LONGS() calculations and pointer arithmetic. > This exposes gpiolib to the underlying unsigned long representation > used by bitmaps rather than respecting the bitmap abstraction. > > Convert invert_mask in struct gpio_array to a pointer and allocate each > of the three fastpath bitmaps individually via bitmap_zalloc(), freeing > them via bitmap_free() on fastpath cancellation and array disposal. > > Although this adds three slab allocations when the fastpath is active, > packing all three masks into a single bitmap_zalloc() call would still > require rounding the bit count up to BITS_PER_LONG and slicing the > buffer with unsigned long pointer arithmetic, adding a second > allocation and extra free paths without achieving bitmap API > abstraction. Yes, thanks! Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
On Wed, Nov 04, 2026 at 03:50:46AM +0000, sashiko-bot@kernel.org wrote: > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: > - [Low] gpiolib: define and initialize __free() variable in gpio_descs_alloc() > -- > > commit 014858138dec2ff00e5cb0fd3fa050587447ef44 > Author: Dmitry Torokhov <dmitry.torokhov@gmail.com> > > gpiolib: use bitmap_zalloc() for fastpath bitmaps > > This commit converts invert_mask in struct gpio_array to a pointer and > allocates the get_mask, set_mask, and invert_mask fastpath bitmaps > individually via bitmap_zalloc() instead of a single combined kzalloc() > with manual pointer arithmetic. > > > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c > > index cf311c1a707c9..bba4e5bb125f9 100644 > > --- a/drivers/gpio/gpiolib.c > > +++ b/drivers/gpio/gpiolib.c > > @@ -5208,13 +5208,28 @@ static void gpiochip_free_hogs(struct gpio_chip *gc) > > [ ... ] > > > +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_descs *descs __free(gpiod_put_array) = NULL; > > struct gpio_array *array_info; > > - struct gpio_descs *descs; > > size_t descs_size, alloc_size; > > - unsigned int bitmap_size; > > + unsigned int nbits; > > > > /* > > * If pin hardware number of array member 0 is also 0, select > > @@ -5224,25 +5239,36 @@ static struct gpio_descs *gpio_descs_alloc(unsigned int count, struct gpio_desc > > 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); > > + alloc_size = descs_size + sizeof(*array_info); > > > > descs = kzalloc(alloc_size, GFP_KERNEL); > > [Severity: Low] > This isn't a bug, but cleanup.h conventions prefer defining and initializing > __free() variables in a single statement rather than declaring them as NULL > at the top of the function and assigning them later. > > In gpio_descs_alloc(), could descs be defined and initialized directly at the > kzalloc() call site after computing descs_size (using struct_size_t(struct > gpio_descs, desc, count)) and alloc_size? > Yeah, will fix.
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index cf311c1a707c..bba4e5bb125f 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -5208,13 +5208,28 @@ 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) +static void gpio_descs_free_fastpath(struct gpio_descs *descs) +{ + struct gpio_array *array_info; + + array_info = descs->info; + if (!array_info) + return; + + bitmap_free(array_info->get_mask); + bitmap_free(array_info->set_mask); + bitmap_free(array_info->invert_mask); + descs->info = NULL; +} + +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_descs *descs __free(gpiod_put_array) = NULL; struct gpio_array *array_info; - struct gpio_descs *descs; size_t descs_size, alloc_size; - unsigned int bitmap_size; + unsigned int nbits; /* * If pin hardware number of array member 0 is also 0, select @@ -5224,25 +5239,36 @@ static struct gpio_descs *gpio_descs_alloc(unsigned int count, struct gpio_desc 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); + alloc_size = descs_size + sizeof(*array_info); 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; + descs->info = array_info; + + nbits = max(gdev->ngpio, count); + + array_info->get_mask = bitmap_zalloc(nbits, GFP_KERNEL); + if (!array_info->get_mask) + return NULL; + + array_info->set_mask = bitmap_zalloc(nbits, GFP_KERNEL); + if (!array_info->set_mask) + return NULL; + + array_info->invert_mask = bitmap_zalloc(nbits, GFP_KERNEL); + if (!array_info->invert_mask) + return NULL; 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; + return_ptr(descs); } static void gpio_descs_check_fastpath(struct gpio_descs *descs, @@ -5274,7 +5300,7 @@ static void gpio_descs_check_fastpath(struct gpio_descs *descs, * hardware number is different from its array index. */ if (bitmap_full(array_info->get_mask, idx)) { - descs->info = NULL; + gpio_descs_free_fastpath(descs); } else { __clear_bit(idx, array_info->get_mask); __clear_bit(idx, array_info->set_mask); @@ -5397,6 +5423,7 @@ void gpiod_put_array(struct gpio_descs *descs) for (i = 0; i < descs->ndescs; i++) gpiod_put(descs->desc[i]); + gpio_descs_free_fastpath(descs); kfree(descs); } EXPORT_SYMBOL_GPL(gpiod_put_array); diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 650a702741df..de1795c9be3d 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -135,7 +135,7 @@ struct gpio_array { struct gpio_device *gdev; unsigned long *get_mask; unsigned long *set_mask; - unsigned long invert_mask[]; + unsigned long *invert_mask; }; #define for_each_gpio_desc(gc, desc) \
Currently, struct gpio_array embeds invert_mask[] as a flexible array member, and gpio_descs_alloc() allocates the descriptor array, struct gpio_array, and all three fastpath bitmaps in a single kzalloc() call using manual BITS_TO_LONGS() calculations and pointer arithmetic. This exposes gpiolib to the underlying unsigned long representation used by bitmaps rather than respecting the bitmap abstraction. Convert invert_mask in struct gpio_array to a pointer and allocate each of the three fastpath bitmaps individually via bitmap_zalloc(), freeing them via bitmap_free() on fastpath cancellation and array disposal. Although this adds three slab allocations when the fastpath is active, packing all three masks into a single bitmap_zalloc() call would still require rounding the bit count up to BITS_PER_LONG and slicing the buffer with unsigned long pointer arithmetic, adding a second allocation and extra free paths without achieving bitmap API abstraction. Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> --- drivers/gpio/gpiolib.c | 47 +++++++++++++++++++++++++++++++++++++---------- drivers/gpio/gpiolib.h | 2 +- 2 files changed, 38 insertions(+), 11 deletions(-)