Hi,

On Wed, Apr 09, 2025 at 04:50:35PM +0200, Luca Ceresoli wrote:
> Add a basic KUnit test for the newly introduced drm_bridge_alloc().
> 
> Signed-off-by: Luca Ceresoli <luca.ceres...@bootlin.com>
> 
> ---
> 
> Changed in v7:
>  - rebase on current drm-misc-next, which now has a drm_bridge_test.c file
>  - cleanup commit message
> 
> Changed in v6:
>  - update to new devm_drm_bridge_alloc() API
>  - remove drm_test_drm_bridge_put test, not straightforward to write with
>    the new API and the current notification mechanism
>  - do not allocate a drm_device: a bridge is allocated without one
>  - rename some identifiers for easier code reading
> 
> This patch was added in v5.
> ---
>  drivers/gpu/drm/tests/drm_bridge_test.c | 60 
> +++++++++++++++++++++++++++++++++
>  1 file changed, 60 insertions(+)
> 
> diff --git a/drivers/gpu/drm/tests/drm_bridge_test.c 
> b/drivers/gpu/drm/tests/drm_bridge_test.c
> index 
> ff88ec2e911c9cc9a718483f09d4c764f45f991a..87fb64744b67f0780457a546aba77ba945a0ce67
>  100644
> --- a/drivers/gpu/drm/tests/drm_bridge_test.c
> +++ b/drivers/gpu/drm/tests/drm_bridge_test.c
> @@ -8,6 +8,7 @@
>  #include <drm/drm_bridge_helper.h>
>  #include <drm/drm_kunit_helpers.h>
>  
> +#include <kunit/device.h>
>  #include <kunit/test.h>
>  
>  struct drm_bridge_init_priv {
> @@ -407,11 +408,70 @@ static struct kunit_suite 
> drm_bridge_helper_reset_crtc_test_suite = {
>       .test_cases = drm_bridge_helper_reset_crtc_tests,
>  };
>  
> +struct drm_bridge_alloc_test_ctx {
> +     struct device *dev;
> +};

You don't need a struct there then, you can just pass the device pointer.

> +/*
> + * Mimick the typical struct defined by a bridge driver, which embeds a
> + * bridge plus other fields.
> + */
> +struct dummy_drm_bridge {
> +     int dummy; // ensure we test non-zero @bridge offset
> +     struct drm_bridge bridge;
> +};

drm_bridge_init_priv gives you that already.

> +static const struct drm_bridge_funcs drm_bridge_dummy_funcs = {
> +};
> +
> +static int drm_test_bridge_alloc_init(struct kunit *test)
> +{
> +     struct drm_bridge_alloc_test_ctx *ctx;
> +
> +     ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
> +     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
> +
> +     ctx->dev = kunit_device_register(test, "drm-bridge-dev");
> +     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dev);
> +
> +     test->priv = ctx;
> +
> +     return 0;
> +}
> +
> +/*
> + * Test that the allocation and initialization of a bridge works as
> + * expected and doesn't report any error.
> + */
> +static void drm_test_drm_bridge_alloc(struct kunit *test)
> +{
> +     struct drm_bridge_alloc_test_ctx *ctx = test->priv;
> +     struct dummy_drm_bridge *dummy;
> +
> +     dummy = devm_drm_bridge_alloc(ctx->dev, struct dummy_drm_bridge, bridge,
> +                                   &drm_bridge_dummy_funcs);
> +     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy);

Why did you need the dummy value in dummy_drm_bridge if you're not using
it?

We'd need a couple more tests, in particular some to make sure the
bridge pointer is properly cleaned up when the device goes away, but not
when we have called drm_bridge_get pointer on it, etc.

Maxime

Attachment: signature.asc
Description: PGP signature

Reply via email to