[PATCH] gpio: kunit: fix GPIO device reference leak with __free()
From: Bartosz Golaszewski
Date: Wed Oct 14 2026 - 04:47:27 EST
KUnit aborts the test thread immediately using kthread_exit() so the
variable annotated with __free() never really goes out of scope and the
associated callback is not called. Schedule a kunit action instead.
Fixes: a05cfd98c5ec ("gpio: kunit: add test cases for software node hogs")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://sashiko.dev/#/patchset/20261013-gpio-kunit-plat-lookup-v3-0-3907f913e964%40oss.qualcomm.com?part=1
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
---
drivers/gpio/gpiolib-kunit.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/gpio/gpiolib-kunit.c b/drivers/gpio/gpiolib-kunit.c
index 90b7e77ef0b51f5115d8a3afe53060a8f33992d2..f005854e7ff7c37dddb966ad25bcdc777e7d978d 100644
--- a/drivers/gpio/gpiolib-kunit.c
+++ b/drivers/gpio/gpiolib-kunit.c
@@ -28,6 +28,8 @@
#define GPIO_TEST_PROVIDER_NGPIO 4
+KUNIT_DEFINE_ACTION_WRAPPER(gpio_device_put_wrapper, gpio_device_put, struct gpio_device *);
+
/*
* The test provider tracks per-line direction and value so that lines can be
* driven as both inputs and outputs - this is needed to exercise input as well
@@ -760,6 +762,7 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
struct property_entry properties[2];
struct platform_device *pdev;
struct fwnode_handle *fwnode;
+ struct gpio_device *gdev;
struct gpio_desc *desc;
bool bound = true;
int ret;
@@ -779,10 +782,12 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
wait_for_device_probe();
/* The hog must have configured the line with the expected direction. */
- struct gpio_device *gdev __free(gpio_device_put) =
- gpio_device_find_by_label(GPIO_CONSUMER_NAME);
+ gdev = gpio_device_find_by_label(GPIO_CONSUMER_NAME);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gdev);
+ ret = kunit_add_action_or_reset(test, gpio_device_put_wrapper, gdev);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
desc = gpio_device_get_desc(gdev, offset);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
---
base-commit: 1fffefb00cc0b0da1f2ae24e3d33364530ea6515
change-id: 20261014-gpio-kunit-autofree-leak-fix-fd6e98cb7ae2
Best regards,
--
Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>