Hi,

On Thu, Sep 03, 2026 at 05:02:56PM +0800, [email protected] wrote:
> From: Shixiong Ou <[email protected]>
> 
> The DRM scheduler KUnit tests currently pass NULL for the dev field in
> drm_sched_init_args, which causes a NULL pointer dereference in the
> drm_sched_job trace event when it calls dev_name() on sched->dev.
> 
> Use faux_device_create() to create a fake device for the mock scheduler,
> so the scheduler always has a valid device pointer. This avoids the
> trace event crash without requiring the production code to accept a NULL
> device pointer, which conceptually makes no sense for a scheduler.
> 
> An atomic counter is used to generate unique device names, since
> multiple mock schedulers can exist simultaneously across different
> test suites.
> 
> Signed-off-by: Shixiong Ou <[email protected]>
> ---
>  drivers/gpu/drm/scheduler/tests/mock_scheduler.c | 14 ++++++++++++++
>  drivers/gpu/drm/scheduler/tests/sched_tests.h    |  2 ++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/drivers/gpu/drm/scheduler/tests/mock_scheduler.c 
> b/drivers/gpu/drm/scheduler/tests/mock_scheduler.c
> index 14403a762335..cd87e405bb1e 100644
> --- a/drivers/gpu/drm/scheduler/tests/mock_scheduler.c
> +++ b/drivers/gpu/drm/scheduler/tests/mock_scheduler.c
> @@ -1,6 +1,8 @@
>  // SPDX-License-Identifier: GPL-2.0
>  /* Copyright (c) 2025 Valve Corporation */
>  
> +#include <linux/device/faux.h>
> +
>  #include "sched_tests.h"
>  
>  /*
> @@ -10,6 +12,8 @@
>   * Test cases are implemented in a separate file.
>   */
>  
> +static atomic_t drm_mock_sched_instance = ATOMIC_INIT(0);
> +
>  /**
>   * drm_mock_sched_entity_new - Create a new mock scheduler entity
>   *
> @@ -296,11 +300,20 @@ struct drm_mock_scheduler *drm_mock_sched_new(struct 
> kunit *test, long timeout)
>               .name           = "drm-mock-scheduler",
>       };
>       struct drm_mock_scheduler *sched;
> +     char name[64];
>       int ret;
>  
>       sched = kunit_kzalloc(test, sizeof(*sched), GFP_KERNEL);
>       KUNIT_ASSERT_NOT_NULL(test, sched);
>  
> +     snprintf(name, sizeof(name), "drm-mock-scheduler-%d",
> +              atomic_inc_return(&drm_mock_sched_instance));
> +
> +     sched->faux_dev = faux_device_create(name, NULL, NULL);
> +     KUNIT_ASSERT_NOT_NULL(test, sched->faux_dev);
> +
> +     args.dev = &sched->faux_dev->dev;
> +
>       ret = drm_sched_init(&sched->base, &args);
>       KUNIT_ASSERT_EQ(test, ret, 0);
>  
> @@ -323,6 +336,7 @@ struct drm_mock_scheduler *drm_mock_sched_new(struct 
> kunit *test, long timeout)
>  void drm_mock_sched_fini(struct drm_mock_scheduler *sched)
>  {
>       drm_sched_fini(&sched->base);
> +     faux_device_destroy(sched->faux_dev);
>  }

What's wrong with drm_kunit_helper_alloc_device(), or kunit_device_register()?

Maxime

Attachment: signature.asc
Description: PGP signature

Reply via email to