All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [RFC PATCH] drm/sched/tests: Let the DRM scheduler manage job lifetimes
@ 2026-08-10  3:32 kernel test robot
  0 siblings, 0 replies; 9+ messages in thread
From: kernel test robot @ 2026-08-10  3:32 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp, Julia Lawall

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20260707114807.154572-1-marco.pagani@linux.dev>
References: <20260707114807.154572-1-marco.pagani@linux.dev>
TO: Marco Pagani <marco.pagani@linux.dev>

Hi Marco,

[This is a private test report for your RFC patch.]
kernel test robot noticed the following build warnings:

[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on linus/master v7.2-rc6 next-20260807]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Marco-Pagani/drm-sched-tests-Let-the-DRM-scheduler-manage-job-lifetimes/20260807-105436
base:   https://gitlab.freedesktop.org/drm/misc/kernel.git drm-misc-next
patch link:    https://lore.kernel.org/r/20260707114807.154572-1-marco.pagani%40linux.dev
patch subject: [RFC PATCH] drm/sched/tests: Let the DRM scheduler manage job lifetimes
:::::: branch date: 3 days ago
:::::: commit date: 3 days ago
config: mips-randconfig-r062-20260810 (https://download.01.org/0day-ci/archive/20260810/202608101032.caQmA50M-lkp@intel.com/config)
compiler: mips-linux-gcc (GCC) 11.5.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Julia Lawall <julia.lawall@inria.fr>
| Closes: https://lore.kernel.org/r/202608101032.caQmA50M-lkp@intel.com/

cocci warnings: (new ones prefixed by >>)
>> drivers/gpu/drm/scheduler/tests/mock_scheduler.c:139:18-21: ERROR: reference preceded by free on line 135

vim +139 drivers/gpu/drm/scheduler/tests/mock_scheduler.c

2e842124be0d553 Marco Pagani               2026-07-07  106  
5a99350794fec11 Tvrtko Ursulin             2025-03-24  107  /**
5a99350794fec11 Tvrtko Ursulin             2025-03-24  108   * drm_mock_sched_job_new - Create a new mock scheduler job
5a99350794fec11 Tvrtko Ursulin             2025-03-24  109   *
5a99350794fec11 Tvrtko Ursulin             2025-03-24  110   * @test: KUnit test owning the job
5a99350794fec11 Tvrtko Ursulin             2025-03-24  111   * @entity: Scheduler entity of the job
5a99350794fec11 Tvrtko Ursulin             2025-03-24  112   *
5a99350794fec11 Tvrtko Ursulin             2025-03-24  113   * Returns: New mock scheduler job with allocation managed by the test
5a99350794fec11 Tvrtko Ursulin             2025-03-24  114   */
5a99350794fec11 Tvrtko Ursulin             2025-03-24  115  struct drm_mock_sched_job *
5a99350794fec11 Tvrtko Ursulin             2025-03-24  116  drm_mock_sched_job_new(struct kunit *test,
5a99350794fec11 Tvrtko Ursulin             2025-03-24  117  		       struct drm_mock_sched_entity *entity)
5a99350794fec11 Tvrtko Ursulin             2025-03-24  118  {
5a99350794fec11 Tvrtko Ursulin             2025-03-24  119  	struct drm_mock_sched_job *job;
5a99350794fec11 Tvrtko Ursulin             2025-03-24  120  	int ret;
5a99350794fec11 Tvrtko Ursulin             2025-03-24  121  
2e842124be0d553 Marco Pagani               2026-07-07  122  	/* Let the DRM Scheduler manage the lifetime of the job */
2e842124be0d553 Marco Pagani               2026-07-07  123  	job = kzalloc_obj(*job, GFP_KERNEL);
5a99350794fec11 Tvrtko Ursulin             2025-03-24  124  	KUNIT_ASSERT_NOT_NULL(test, job);
5a99350794fec11 Tvrtko Ursulin             2025-03-24  125  
2e842124be0d553 Marco Pagani               2026-07-07  126  	kref_init(&job->refcount);
2e842124be0d553 Marco Pagani               2026-07-07  127  	job->test = test;
2e842124be0d553 Marco Pagani               2026-07-07  128  
5a99350794fec11 Tvrtko Ursulin             2025-03-24  129  	ret = drm_sched_job_init(&job->base,
5a99350794fec11 Tvrtko Ursulin             2025-03-24  130  				 &entity->base,
5a99350794fec11 Tvrtko Ursulin             2025-03-24  131  				 1,
2956554823cedb3 Pierre-Eric Pelloux-Prayer 2025-05-26  132  				 NULL,
2956554823cedb3 Pierre-Eric Pelloux-Prayer 2025-05-26  133  				 1);
2e842124be0d553 Marco Pagani               2026-07-07  134  	if (ret) {
2e842124be0d553 Marco Pagani               2026-07-07 @135  		kfree(job);
2e842124be0d553 Marco Pagani               2026-07-07  136  		KUNIT_ASSERT_EQ_MSG(test, ret, 0, "drm_sched_job_init failed");
2e842124be0d553 Marco Pagani               2026-07-07  137  	}
5a99350794fec11 Tvrtko Ursulin             2025-03-24  138  
5a99350794fec11 Tvrtko Ursulin             2025-03-24 @139  	init_completion(&job->done);
5a99350794fec11 Tvrtko Ursulin             2025-03-24  140  	INIT_LIST_HEAD(&job->link);
1afba39f9305fe4 Thomas Zimmermann          2025-04-07  141  	hrtimer_setup(&job->timer, drm_mock_sched_job_signal_timer,
1afba39f9305fe4 Thomas Zimmermann          2025-04-07  142  		      CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
5a99350794fec11 Tvrtko Ursulin             2025-03-24  143  
2e842124be0d553 Marco Pagani               2026-07-07  144  	ret = kunit_add_action(test, drm_mock_sched_job_cleanup_action, job);
2e842124be0d553 Marco Pagani               2026-07-07  145  	if (ret) {
2e842124be0d553 Marco Pagani               2026-07-07  146  		drm_mock_sched_job_put(job);
2e842124be0d553 Marco Pagani               2026-07-07  147  		KUNIT_ASSERT_EQ_MSG(test, ret, 0, "kunit_add_action failed");
2e842124be0d553 Marco Pagani               2026-07-07  148  	}
2e842124be0d553 Marco Pagani               2026-07-07  149  
5a99350794fec11 Tvrtko Ursulin             2025-03-24  150  	return job;
5a99350794fec11 Tvrtko Ursulin             2025-03-24  151  }
5a99350794fec11 Tvrtko Ursulin             2025-03-24  152  

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 9+ messages in thread
* [RFC PATCH] drm/sched/tests: Let the DRM scheduler manage job lifetimes
@ 2026-07-07 11:48 Marco Pagani
  2026-07-07 12:05 ` sashiko-bot
  2026-07-08  9:20 ` Tvrtko Ursulin
  0 siblings, 2 replies; 9+ messages in thread
From: Marco Pagani @ 2026-07-07 11:48 UTC (permalink / raw)
  To: Tvrtko Ursulin, Matthew Brost, Danilo Krummrich, Philipp Stanner,
	Christian König, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: Marco Pagani, dri-devel, linux-kernel

Currently, the mock scheduler uses KUnit-managed memory for jobs. This ties
the job's memory lifetime to the test suite rather than the DRM scheduler's
callbacks. This does not represent real driver behavior and can lead to
potential Use-After-Free bugs in the tests.

Update the mock scheduler to let the lifetime of jobs be managed by the DRM
scheduler's asynchronous callbacks instead of KUnit managed memory. Add a
kref reference counter to track the job's lifetime between test suites and
the scheduler.

Finally, to avoid memory leaks in the event of an early test abortion,
register a cleanup KUnit action that automatically puts the reference to
the job.

Signed-off-by: Marco Pagani <marco.pagani@linux.dev>
---
 .../gpu/drm/scheduler/tests/mock_scheduler.c  | 68 ++++++++++++++-----
 drivers/gpu/drm/scheduler/tests/sched_tests.h |  6 ++
 2 files changed, 57 insertions(+), 17 deletions(-)

diff --git a/drivers/gpu/drm/scheduler/tests/mock_scheduler.c b/drivers/gpu/drm/scheduler/tests/mock_scheduler.c
index 14403a762335..51f81082f37a 100644
--- a/drivers/gpu/drm/scheduler/tests/mock_scheduler.c
+++ b/drivers/gpu/drm/scheduler/tests/mock_scheduler.c
@@ -96,6 +96,14 @@ drm_mock_sched_job_signal_timer(struct hrtimer *hrtimer)
 	return HRTIMER_NORESTART;
 }
 
+static void drm_mock_sched_job_cleanup_action(void *ptr)
+{
+	struct drm_mock_sched_job *job = ptr;
+
+	job->test = NULL;
+	drm_mock_sched_job_put(job);
+}
+
 /**
  * drm_mock_sched_job_new - Create a new mock scheduler job
  *
@@ -111,23 +119,34 @@ drm_mock_sched_job_new(struct kunit *test,
 	struct drm_mock_sched_job *job;
 	int ret;
 
-	job = kunit_kzalloc(test, sizeof(*job), GFP_KERNEL);
+	/* Let the DRM Scheduler manage the lifetime of the job */
+	job = kzalloc_obj(*job, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_NULL(test, job);
 
+	kref_init(&job->refcount);
+	job->test = test;
+
 	ret = drm_sched_job_init(&job->base,
 				 &entity->base,
 				 1,
 				 NULL,
 				 1);
-	KUNIT_ASSERT_EQ(test, ret, 0);
-
-	job->test = test;
+	if (ret) {
+		kfree(job);
+		KUNIT_ASSERT_EQ_MSG(test, ret, 0, "drm_sched_job_init failed");
+	}
 
 	init_completion(&job->done);
 	INIT_LIST_HEAD(&job->link);
 	hrtimer_setup(&job->timer, drm_mock_sched_job_signal_timer,
 		      CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
 
+	ret = kunit_add_action(test, drm_mock_sched_job_cleanup_action, job);
+	if (ret) {
+		drm_mock_sched_job_put(job);
+		KUNIT_ASSERT_EQ_MSG(test, ret, 0, "kunit_add_action failed");
+	}
+
 	return job;
 }
 
@@ -152,7 +171,7 @@ static void drm_mock_sched_hw_fence_release(struct dma_fence *fence)
 
 	hrtimer_cancel(&job->timer);
 
-	/* Containing job is freed by the kunit framework */
+	drm_mock_sched_job_put(job);
 }
 
 static const struct dma_fence_ops drm_mock_sched_hw_fence_ops = {
@@ -173,6 +192,8 @@ static struct dma_fence *mock_sched_run_job(struct drm_sched_job *sched_job)
 		       sched->hw_timeline.context,
 		       atomic_inc_return(&sched->hw_timeline.next_seqno));
 
+	kref_get(&job->refcount);
+
 	dma_fence_get(&job->hw_fence); /* Reference for the job_list */
 
 	spin_lock_irq(&sched->lock);
@@ -200,6 +221,17 @@ static struct dma_fence *mock_sched_run_job(struct drm_sched_job *sched_job)
 	return &job->hw_fence;
 }
 
+static void mock_sched_free_job(struct drm_sched_job *sched_job)
+{
+	struct drm_mock_sched_job *job = drm_sched_job_to_mock_job(sched_job);
+
+	/* Only if the fence has been successfully initialized */
+	if (job->hw_fence.ops)
+		dma_fence_put(&job->hw_fence);
+
+	drm_mock_sched_job_put(job);
+}
+
 /*
  * Normally, drivers would take appropriate measures in this callback, such as
  * killing the entity the faulty job is associated with, resetting the hardware
@@ -232,21 +264,26 @@ mock_sched_timedout_job(struct drm_sched_job *sched_job)
 	}
 	spin_unlock_irqrestore(&sched->lock, flags);
 
-	dma_fence_put(&job->hw_fence);
-	drm_sched_job_cleanup(sched_job);
-	/* Mock job itself is freed by the kunit framework. */
+	mock_sched_free_job(sched_job);
 
 	return DRM_GPU_SCHED_STAT_RESET;
 }
 
-static void mock_sched_free_job(struct drm_sched_job *sched_job)
+static void drm_mock_sched_job_release(struct kref *ref)
 {
-	struct drm_mock_sched_job *job = drm_sched_job_to_mock_job(sched_job);
+	struct drm_mock_sched_job *job;
+
+	job = container_of(ref, struct drm_mock_sched_job, refcount);
 
-	dma_fence_put(&job->hw_fence);
-	drm_sched_job_cleanup(sched_job);
+	drm_sched_job_cleanup(&job->base);
 
-	/* Mock job itself is freed by the kunit framework. */
+	kfree(job);
+}
+
+void drm_mock_sched_job_put(struct drm_mock_sched_job *job)
+{
+	if (job)
+		kref_put(&job->refcount, drm_mock_sched_job_release);
 }
 
 static void mock_sched_cancel_job(struct drm_sched_job *sched_job)
@@ -265,10 +302,7 @@ static void mock_sched_cancel_job(struct drm_sched_job *sched_job)
 	}
 	spin_unlock_irqrestore(&sched->lock, flags);
 
-	/*
-	 * The GPU Scheduler will call drm_sched_backend_ops.free_job(), still.
-	 * Mock job itself is freed by the kunit framework.
-	 */
+	/* The GPU Scheduler will call drm_sched_backend_ops.free_job() */
 }
 
 static const struct drm_sched_backend_ops drm_mock_scheduler_ops = {
diff --git a/drivers/gpu/drm/scheduler/tests/sched_tests.h b/drivers/gpu/drm/scheduler/tests/sched_tests.h
index 553d45abd057..e4d33f0bf935 100644
--- a/drivers/gpu/drm/scheduler/tests/sched_tests.h
+++ b/drivers/gpu/drm/scheduler/tests/sched_tests.h
@@ -13,6 +13,7 @@
 #include <linux/list.h>
 #include <linux/mutex.h>
 #include <linux/types.h>
+#include <linux/kref.h>
 
 #include <drm/gpu_scheduler.h>
 
@@ -90,6 +91,7 @@ struct drm_mock_sched_entity {
  */
 struct drm_mock_sched_job {
 	struct drm_sched_job	base;
+	struct kref		refcount;
 
 	struct completion	done;
 
@@ -144,6 +146,8 @@ struct drm_mock_sched_job *
 drm_mock_sched_job_new(struct kunit *test,
 		       struct drm_mock_sched_entity *entity);
 
+void drm_mock_sched_job_put(struct drm_mock_sched_job *job);
+
 /**
  * drm_mock_sched_job_submit - Arm and submit a job in one go
  *
@@ -151,6 +155,8 @@ drm_mock_sched_job_new(struct kunit *test,
  */
 static inline void drm_mock_sched_job_submit(struct drm_mock_sched_job *job)
 {
+	kref_get(&job->refcount);
+
 	drm_sched_job_arm(&job->base);
 	drm_sched_entity_push_job(&job->base);
 }
-- 
2.55.0


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

end of thread, other threads:[~2026-08-10  3:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-10  3:32 [RFC PATCH] drm/sched/tests: Let the DRM scheduler manage job lifetimes kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2026-07-07 11:48 Marco Pagani
2026-07-07 12:05 ` sashiko-bot
2026-07-08  9:20 ` Tvrtko Ursulin
2026-07-09 21:53   ` Marco Pagani
2026-07-10 13:20     ` Tvrtko Ursulin
2026-07-15 16:02       ` Marco Pagani
2026-07-16 12:14         ` Tvrtko Ursulin
2026-07-21 11:55           ` Marco Pagani

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.