Re: [PATCH v4 05/10] platform: Add test managed platform_device/driver APIs

From: Stephen Boyd
Date: Wed Apr 24 2024 - 14:11:33 EST


Quoting Stephen Boyd (2024-04-22 16:23:58)
> diff --git a/drivers/base/test/platform_kunit.c b/drivers/base/test/platform_kunit.c
> new file mode 100644
> index 000000000000..54af6db2a6d8
> --- /dev/null
> +++ b/drivers/base/test/platform_kunit.c
> @@ -0,0 +1,174 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Test managed platform driver
> + */
> +
[...]
> +
> +/**
> + * platform_driver_register_kunit() - Register a KUnit test managed platform driver
> + * @test: test context
> + * @drv: platform driver to register
> + *
> + * Register a test managed platform driver. This allows callers to embed the
> + * @drv in a container structure and use container_of() in the probe function
> + * to pass information to KUnit tests. It can be assumed that the driver has
> + * probed when this function returns.
> + *
> + * Example
> + *
> + * .. code-block:: c
> + *
> + * struct kunit_test_context {
> + * struct platform_driver pdrv;
> + * const char *data;
> + * };
> + *
> + * static inline struct kunit_test_context *
> + * to_test_context(struct platform_device *pdev)
> + * {
> + * return container_of(to_platform_driver(pdev->dev.driver),
> + * struct kunit_test_context,
> + * pdrv);
> + * }
> + *
> + * static int kunit_platform_driver_probe(struct platform_device *pdev)
> + * {
> + * struct kunit_test_context *ctx;
> + *
> + * ctx = to_test_context(pdev);
> + * ctx->data = "test data";
> + *
> + * return 0;
> + * }
> + *
> + * static void kunit_platform_driver_test(struct kunit *test)
> + * {
> + * struct kunit_test_context *ctx;
> + *
> + * ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
> + * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
> + *
> + * ctx->pdrv.probe = kunit_platform_driver_probe;
> + * ctx->pdrv.driver.name = "kunit-platform";
> + * ctx->pdrv.driver.owner = THIS_MODULE;
> + *
> + * KUNIT_EXPECT_EQ(test, 0, platform_driver_register_kunit(test, &ctx->pdrv));
> + * KUNIT_EXPECT_STREQ(test, ctx->data, "test data");
> + * }
> + *
> + * Return: 0 on success, negative errno on failure.
> + */
> +int platform_driver_register_kunit(struct kunit *test,
> + struct platform_driver *drv)
> +{
> + int ret;
> +
> + ret = platform_driver_register(drv);
> + if (ret)
> + return ret;
> +
> + /*
> + * Wait for the driver to probe (or at least flush out of the deferred
> + * workqueue)
> + */
> + wait_for_device_probe();

Should this be removed? I was thinking that this isn't a pure wrapper
around platform_driver_register() because it has this wait call. Maybe
it's better to have some other kunit API that can wait for a specific
device to probe and timeout if it doesn't happen in that amount of time.
That API would use the bus notifiers and look for
BUS_NOTIFY_BOUND_DRIVER. Or maybe that function could setup a completion
that the test can wait on.

> +
> + return kunit_add_action_or_reset(test,
> + (kunit_action_t *)&platform_driver_unregister,
> + drv);
> +}
> +EXPORT_SYMBOL_GPL(platform_driver_register_kunit);