mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RESEND] drm/sched: Create faux device for KUnit tests
@ 2026-09-03  9:02 oushixiong1025
  2026-09-03  9:33 ` Philipp Stanner
  2026-09-03  9:36 ` Maxime Ripard
  0 siblings, 2 replies; 4+ messages in thread
From: oushixiong1025 @ 2026-09-03  9:02 UTC (permalink / raw)
  To: Matthew Brost
  Cc: Danilo Krummrich, Philipp Stanner, Christian König,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	David Airlie, Simona Vetter, dri-devel, linux-kernel,
	Shixiong Ou

From: Shixiong Ou <oushixiong@kylinos.cn>

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 <oushixiong@kylinos.cn>
---
 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);
 }
 
 /**
diff --git a/drivers/gpu/drm/scheduler/tests/sched_tests.h b/drivers/gpu/drm/scheduler/tests/sched_tests.h
index 553d45abd057..bbf4da585866 100644
--- a/drivers/gpu/drm/scheduler/tests/sched_tests.h
+++ b/drivers/gpu/drm/scheduler/tests/sched_tests.h
@@ -7,6 +7,7 @@
 #include <kunit/test.h>
 #include <linux/atomic.h>
 #include <linux/completion.h>
+#include <linux/device/faux.h>
 #include <linux/dma-fence.h>
 #include <linux/hrtimer.h>
 #include <linux/ktime.h>
@@ -44,6 +45,7 @@ struct drm_mock_scheduler {
 	struct drm_gpu_scheduler base;
 
 	struct kunit		*test;
+	struct faux_device	*faux_dev;
 
 	spinlock_t		lock;
 	struct list_head	job_list;
-- 
2.25.1


No virus found
		Checked by Hillstone Network AntiVirus


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH RESEND] drm/sched: Create faux device for KUnit tests
  2026-09-03  9:02 [PATCH RESEND] drm/sched: Create faux device for KUnit tests oushixiong1025
@ 2026-09-03  9:33 ` Philipp Stanner
  2026-09-03  9:36 ` Maxime Ripard
  1 sibling, 0 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-09-03  9:33 UTC (permalink / raw)
  To: oushixiong1025, Matthew Brost
  Cc: Danilo Krummrich, Philipp Stanner, Christian König,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	David Airlie, Simona Vetter, dri-devel, linux-kernel,
	Shixiong Ou

On Thu, 2026-09-03 at 17:02 +0800, oushixiong1025@163.com wrote:
> From: Shixiong Ou <oushixiong@kylinos.cn>
> 
> 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 <oushixiong@kylinos.cn>

Didn't you address a fault / bug with that?

Cc: stable …
Fixes:

?

> ---
> 

[…]

>  
> +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",

You could use args.name here.

> +		 atomic_inc_return(&drm_mock_sched_instance));

Couldn't that atomic be a `static unsigned int` inside this function?
Is simpler and limits the scope. And I wouldn't expect that we'll ever
call drm_mock_sched_new() multi-threaded, or would we?


Besides looks cool, thx

P.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH RESEND] drm/sched: Create faux device for KUnit tests
  2026-09-03  9:02 [PATCH RESEND] drm/sched: Create faux device for KUnit tests oushixiong1025
  2026-09-03  9:33 ` Philipp Stanner
@ 2026-09-03  9:36 ` Maxime Ripard
  2026-09-03  9:53   ` Danilo Krummrich
  1 sibling, 1 reply; 4+ messages in thread
From: Maxime Ripard @ 2026-09-03  9:36 UTC (permalink / raw)
  To: oushixiong1025
  Cc: Matthew Brost, Danilo Krummrich, Philipp Stanner,
	Christian König, Maarten Lankhorst, Thomas Zimmermann,
	David Airlie, Simona Vetter, dri-devel, linux-kernel,
	Shixiong Ou

[-- Attachment #1: Type: text/plain, Size: 2797 bytes --]

Hi,

On Thu, Sep 03, 2026 at 05:02:56PM +0800, oushixiong1025@163.com wrote:
> From: Shixiong Ou <oushixiong@kylinos.cn>
> 
> 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 <oushixiong@kylinos.cn>
> ---
>  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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH RESEND] drm/sched: Create faux device for KUnit tests
  2026-09-03  9:36 ` Maxime Ripard
@ 2026-09-03  9:53   ` Danilo Krummrich
  0 siblings, 0 replies; 4+ messages in thread
From: Danilo Krummrich @ 2026-09-03  9:53 UTC (permalink / raw)
  To: Maxime Ripard
  Cc: oushixiong1025, Matthew Brost, Philipp Stanner,
	Christian König, Maarten Lankhorst, Thomas Zimmermann,
	David Airlie, Simona Vetter, dri-devel, linux-kernel,
	Shixiong Ou

On Thu Sep 3, 2026 at 11:36 AM CEST, Maxime Ripard wrote:
> What's wrong with drm_kunit_helper_alloc_device(), or kunit_device_register()?

That's probably may bad, in a previous patch that tried to support a NULL device
in the scheduler instead I mistakenly mentioned faux while making the point that
we should use some mock device instead.

Thanks,
Danilo

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-03  9:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-03  9:02 [PATCH RESEND] drm/sched: Create faux device for KUnit tests oushixiong1025
2026-09-03  9:33 ` Philipp Stanner
2026-09-03  9:36 ` Maxime Ripard
2026-09-03  9:53   ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®