]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
kunit: provide kunit_platform_device_unregister()
authorBartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Fri, 22 May 2026 13:42:17 +0000 (15:42 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Tue, 26 May 2026 09:19:54 +0000 (11:19 +0200)
Tests may want to unregister a platform device as part of the test case
logic. Using the regular platform_device_register() with kunit
assertions may result in a platform device leak or otherwise requires
cumbersome error handling. Provide a function that unregisters a
kunit-managed platform device and drops the release action from the
test's list.

Reviewed-by: David Gow <david@davidgow.net>
Reviewed-by: Linus Walleij <linusw@kernel.org>
Link: https://patch.msgid.link/20260522-gpiolib-kunit-v3-2-b15fe6987430@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
include/kunit/platform_device.h
lib/kunit/platform.c

index 8cad6e1c3e7efba862862b579089f2f317784a73..eee565d5d1d35c1d1bc82b45eb91d21d00c68428 100644 (file)
@@ -14,6 +14,8 @@ int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev);
 struct platform_device *
 kunit_platform_device_register_full(struct kunit *test,
                                    const struct platform_device_info *pdevinfo);
+void kunit_platform_device_unregister(struct kunit *test,
+                                     struct platform_device *pdev);
 
 int kunit_platform_device_prepare_wait_for_probe(struct kunit *test,
                                                 struct platform_device *pdev,
index 583b50b538c79599ebbf33e261fe2e9ced35efa9..737758d710b2839fab29c5cbcf3bc5ba00e20094 100644 (file)
@@ -161,6 +161,39 @@ kunit_platform_device_register_full(struct kunit *test,
 }
 EXPORT_SYMBOL_GPL(kunit_platform_device_register_full);
 
+static bool
+kunit_platform_device_add_match(struct kunit *test, struct kunit_resource *res,
+                               void *match_data)
+{
+       struct platform_device *pdev = match_data;
+
+       return res->data == pdev && res->free == kunit_platform_device_add_exit;
+}
+
+/**
+ * kunit_platform_device_unregister() - Unregister a KUnit-managed platform device
+ * @test: test context
+ * @pdev: platform device to unregister
+ *
+ * Unregister a test-managed platform device and cancel its release action.
+ */
+void kunit_platform_device_unregister(struct kunit *test,
+                                     struct platform_device *pdev)
+{
+       struct kunit_resource *res;
+
+       res = kunit_find_resource(test, kunit_platform_device_add_match, pdev);
+       if (res) {
+               res->free = NULL;
+               kunit_put_resource(res);
+       } else {
+               kunit_remove_action(test, platform_device_unregister_wrapper, pdev);
+       }
+
+       platform_device_unregister(pdev);
+}
+EXPORT_SYMBOL_GPL(kunit_platform_device_unregister);
+
 struct kunit_platform_device_probe_nb {
        struct completion *x;
        struct device *dev;