Re: [PATCH v3 2/3] kunit: provide kunit_platform_device_unregister()
From: David Gow
Date: Tue May 26 2026 - 05:07:57 EST
Le 22/05/2026 à 9:42 PM, Bartosz Golaszewski a écrit :
> 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.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
> ---
Looks good, thanks.
Reviewed-by: David Gow <david@xxxxxxxxxxxx>
Cheers,
-- David
> include/kunit/platform_device.h | 2 ++
> lib/kunit/platform.c | 33 +++++++++++++++++++++++++++++++++
> 2 files changed, 35 insertions(+)
>
> diff --git a/include/kunit/platform_device.h b/include/kunit/platform_device.h
> index 8cad6e1c3e7efba862862b579089f2f317784a73..eee565d5d1d35c1d1bc82b45eb91d21d00c68428 100644
> --- a/include/kunit/platform_device.h
> +++ b/include/kunit/platform_device.h
> @@ -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,
> diff --git a/lib/kunit/platform.c b/lib/kunit/platform.c
> index 583b50b538c79599ebbf33e261fe2e9ced35efa9..737758d710b2839fab29c5cbcf3bc5ba00e20094 100644
> --- a/lib/kunit/platform.c
> +++ b/lib/kunit/platform.c
> @@ -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;
>