[PATCH v2] gpio: aspeed: Fix potential coprocessor bank map leak

From: Guangshuo Li

Date: Tue Nov 24 2026 - 08:58:52 EST


The coprocessor bank map is allocated lazily but is never freed when
the GPIO controller is removed. Its only owner is the device-managed
GPIO private data.

Allocate the map with the controller's managed resources before
registering the GPIO chip, so chip removal finishes before the map
is freed. Round up the bank count to cover a partial last bank.

This issue was found by our static analysis tool under development.

Fixes: a7ca13826e47 ("gpio: aspeed: Add interfaces for co-processor to grab GPIOs")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Guangshuo Li <liguangshuo@xxxxxxxxxxxxxxx>
---
Changes in v2:
- Use a managed array allocation instead of a cleanup action.
- Allocate before GPIO-chip registration so teardown callbacks finish
before the bank map is freed.
- Round up the array size to cover a partial last bank.

drivers/gpio/gpio-aspeed.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/gpio/gpio-aspeed.c b/drivers/gpio/gpio-aspeed.c
index 45e90ebaf078..8f66be7ff9b5 100644
--- a/drivers/gpio/gpio-aspeed.c
+++ b/drivers/gpio/gpio-aspeed.c
@@ -944,10 +944,6 @@ int aspeed_gpio_copro_grab_gpio(struct gpio_desc *desc,
if (!aspeed_gpio_support_copro(gpio))
return -EOPNOTSUPP;

- if (!gpio->cf_copro_bankmap)
- gpio->cf_copro_bankmap = kzalloc(gpio->chip.ngpio >> 3, GFP_KERNEL);
- if (!gpio->cf_copro_bankmap)
- return -ENOMEM;
if (offset < 0 || offset > gpio->chip.ngpio)
return -EINVAL;
bindex = offset >> 3;
@@ -1381,6 +1377,14 @@ static int aspeed_gpio_probe(struct platform_device *pdev)
gpio->config->llops->reg_bank_get(gpio, (i << 5), reg_rdata);
}

+ if (aspeed_gpio_support_copro(gpio)) {
+ gpio->cf_copro_bankmap = devm_kzalloc_objs(gpio->dev,
+ *gpio->cf_copro_bankmap,
+ DIV_ROUND_UP(gpio->chip.ngpio, 8));
+ if (!gpio->cf_copro_bankmap)
+ return -ENOMEM;
+ }
+
if (gpio->config->llops->privilege_init)
gpio->config->llops->privilege_init(gpio);


base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
--
2.43.0