From 851bb4a42991666f515040e95e6f40191da9e899 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Wed, 14 Oct 2026 10:44:02 +0200 Subject: [PATCH] gpio: kunit: fix GPIO device reference leak with __free() 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 Closes: https://sashiko.dev/#/patchset/20261013-gpio-kunit-plat-lookup-v3-0-3907f913e964%40oss.qualcomm.com?part=1 Link: https://patch.msgid.link/20261014-gpio-kunit-autofree-leak-fix-v1-1-5bfa7be0eb1f@oss.qualcomm.com Signed-off-by: Bartosz Golaszewski --- 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 e6cb43a8df5b..6a2c8b34f34b 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 @@ -770,6 +772,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; @@ -789,10 +792,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);