[PATCH v2 0/7] gpiolib: scope-based management and cleanup for GPIO descriptor arrays

From: Dmitry Torokhov

Date: Tue Nov 03 2026 - 22:42:28 EST


This series fixes a GPIO descriptor leak and a fast-path cancellation
bug in gpiod_get_array(), fixes the !CONFIG_OF_GPIO stub for
of_gpio_count() to return -ENOENT instead of 0, introduces
DEFINE_FREE() scope-based cleanup helpers for GPIO descriptors
(gpiod_put and gpiod_put_array), converts gpiod_get_array() to use
them, reworks descriptor array allocation to avoid krealloc(), and
switches the fastpath bitmaps in struct gpio_array to bitmap_zalloc()
and bitmap_free().

In the final patch, each of the three fastpath bitmaps (get_mask,
set_mask, and invert_mask) is allocated with its own bitmap_zalloc()
call rather than packing all three into a single bitmap_zalloc()
allocation. While Andy suggested a single 3x bitmap_zalloc() call to
avoid multiple allocations, slicing one bitmap into three pointers
still requires rounding the bit count up to BITS_PER_LONG and doing
manual unsigned long pointer arithmetic (which would still break if
bitmaps ever gained a header or tail), while still adding a second
allocation and extra bitmap_free() paths on fastpath cancellation and
teardown. Using three separate bitmap_zalloc() calls achieves full
bitmap API abstraction at the cost of three extra slab allocations when
the fastpath is used, and is kept as a separate final patch so the
maintainers can decide whether they prefer the single contiguous
allocation in patch 6/7 or the strict bitmap abstraction in patch 7/7.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
Changes in v2:
- Added cover letter for the series.
- Added a patch fixing the !CONFIG_OF_GPIO of_gpio_count() stub to
return -ENOENT instead of 0, and dropped the count == 0 check in
gpiod_get_array().
- In patch 6/7, separated array_info assignment from declaration in
gpio_descs_check_fastpath() immediately before NULL check to preserve
reverse-christmas-tree order.
- Added patch 7/7 switching fastpath bitmaps in struct gpio_array to
individual bitmap_zalloc() and bitmap_free() calls per Andy's review
feedback.
- Link to v1: https://patch.msgid.link/20261021190212.2237804-1-dmitry.torokhov@xxxxxxxxx

---
Dmitry Torokhov (7):
gpiolib: fix GPIO descriptor leak in gpiod_get_array()
gpiolib: fix fast-path cancellation in gpiod_get_array()
gpiolib: of: return -ENOENT from of_gpio_count() stub
gpiolib: add support for scope-based management of GPIO descriptors
gpiolib: use __free(gpiod_put_array) in gpiod_get_array()
gpiolib: avoid krealloc() in gpiod_get_array()
gpiolib: use bitmap_zalloc() for fastpath bitmaps

drivers/gpio/gpiolib-of.h | 2 +-
drivers/gpio/gpiolib.c | 224 ++++++++++++++++++++++++------------------
drivers/gpio/gpiolib.h | 2 +-
include/linux/gpio/consumer.h | 4 +
4 files changed, 134 insertions(+), 98 deletions(-)
---
base-commit: d6bcfc94eb4da6e089c5473970cd96727292fd97
change-id: 20261021-b4-gpiolib-free-74d92ea91503

Thanks.

--
Dmitry