All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/imagination: Don't hold queues.lock across drm_sched_stop() on reset
@ 2026-11-25 23:28 Ryan Brue
  2026-11-25 23:48 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ryan Brue @ 2026-11-25 23:28 UTC (permalink / raw)
  To: Alessio Belle, Luigi Santivetti, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter, Sarah Walker,
	Boris Brezillon, Donald Robson
  Cc: imagination, dri-devel, linux-kernel, Sashiko, stable, Ryan Brue

pvr_queue_device_pre_reset() calls drm_sched_stop() on every queue with
pvr_dev->queues.lock held. drm_sched_stop() waits for the scheduler's
run_job work, and that work takes queues.lock in
pvr_queue_update_active_state(). A hard reset that starts while a job is
being submitted therefore deadlocks: the reset and the queue stay stuck,
and lockdep reports the circular dependency.

Take a context reference on each queue under queues.lock, move the queues
to a list of their own, and stop them after dropping the lock, as the
timeout handler already does. A queue whose context is already being
released has no jobs left and is skipped. post_reset() starts the same
queues and drops the references from a work item, because the last
reference releases the context, which takes reset_sem while the reset
still holds it.

Fixes: eaf01ee5ba28 ("drm/imagination: Implement job submission and scheduling")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/r/20260630095216.498D61F000E9@smtp.kernel.org
Cc: stable@vger.kernel.org
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
Sashiko flagged this ABBA deadlock in June, in its review of the
drm_sched_entity_fini() fix, and I have hit it on an MT8173 tablet
(GX6250, BVNC 4.40.2.51), first through lockdep and once as a real hang.

Tested on that board with lockdep and a local debug hook that forces a
hard reset from the watchdog under glmark2, plus a 20 ms sleep in
run_job before it takes queues.lock to make the race certain. Without
this patch, the first forced reset gives lockdep's circular-dependency
report, the reset never completes, and the job timeout handler then
oopses on a NULL parent fence. With it, seven hard resets over two
boots (four forced, three that the watchdog started on its own) gave
no lockdep report and no hung task, and new GPU work ran correctly
afterwards.

This patch was developed with the help of an LLM.
---
 drivers/gpu/drm/imagination/pvr_device.h |  7 +++++
 drivers/gpu/drm/imagination/pvr_queue.c  | 50 +++++++++++++++++++++++++++-----
 drivers/gpu/drm/imagination/pvr_queue.h  | 13 +++++++++
 3 files changed, 62 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h
index c246122b4caf..f495ee9234e6 100644
--- a/drivers/gpu/drm/imagination/pvr_device.h
+++ b/drivers/gpu/drm/imagination/pvr_device.h
@@ -236,6 +236,13 @@ struct pvr_device {
 		/** @queues.lock: Lock protecting access to the active/idle
 		 *  lists. */
 		struct mutex lock;
+
+		/**
+		 * @queues.resetting: Queues stopped by a hard reset, linked by
+		 *  pvr_queue::reset_node. Only used with @reset_sem held for
+		 *  writing.
+		 */
+		struct list_head resetting;
 	} queues;
 
 	/**
diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c
index 34b826381aa2..56751c192ce6 100644
--- a/drivers/gpu/drm/imagination/pvr_queue.c
+++ b/drivers/gpu/drm/imagination/pvr_queue.c
@@ -1242,6 +1242,23 @@ static void reg_state_init(void *cpu_ptr, void *priv)
 	}
 }
 
+static void pvr_queue_reset_put_work(struct work_struct *w)
+{
+	struct pvr_queue *queue = container_of(w, struct pvr_queue, reset_put_work);
+
+	pvr_context_put(queue->ctx);
+}
+
+static void pvr_queue_reset_hold(struct pvr_queue *queue, struct list_head *list)
+{
+	/* A put still pending from the last reset hands its reference over. */
+	if (!cancel_work(&queue->reset_put_work) &&
+	    !pvr_context_get_if_referenced(queue->ctx))
+		return;
+
+	list_add_tail(&queue->reset_node, list);
+}
+
 /**
  * pvr_queue_create() - Create a queue object.
  * @ctx: The context this queue will be attached to.
@@ -1375,6 +1392,9 @@ struct pvr_queue *pvr_queue_create(struct pvr_context *ctx,
 	if (err)
 		goto err_sched_fini;
 
+	INIT_LIST_HEAD(&queue->reset_node);
+	INIT_WORK(&queue->reset_put_work, pvr_queue_reset_put_work);
+
 	mutex_lock(&pvr_dev->queues.lock);
 	list_add_tail(&queue->node, &pvr_dev->queues.idle);
 	mutex_unlock(&pvr_dev->queues.lock);
@@ -1402,26 +1422,39 @@ struct pvr_queue *pvr_queue_create(struct pvr_context *ctx,
 
 void pvr_queue_device_pre_reset(struct pvr_device *pvr_dev)
 {
+	struct list_head *resetting = &pvr_dev->queues.resetting;
 	struct pvr_queue *queue;
 
+	lockdep_assert_held_write(&pvr_dev->reset_sem);
+
+	/*
+	 * drm_sched_stop() waits for the run_job work, which takes
+	 * queues.lock, so it can't be called with queues.lock held. Pin the
+	 * queues and stop them without it. A queue whose context is already
+	 * being released has no jobs left, so it needs no stopping.
+	 */
 	mutex_lock(&pvr_dev->queues.lock);
 	list_for_each_entry(queue, &pvr_dev->queues.idle, node)
-		pvr_queue_stop(queue, NULL);
+		pvr_queue_reset_hold(queue, resetting);
 	list_for_each_entry(queue, &pvr_dev->queues.active, node)
-		pvr_queue_stop(queue, NULL);
+		pvr_queue_reset_hold(queue, resetting);
 	mutex_unlock(&pvr_dev->queues.lock);
+
+	list_for_each_entry(queue, resetting, reset_node)
+		pvr_queue_stop(queue, NULL);
 }
 
 void pvr_queue_device_post_reset(struct pvr_device *pvr_dev)
 {
-	struct pvr_queue *queue;
+	struct pvr_queue *queue, *tmp;
 
-	mutex_lock(&pvr_dev->queues.lock);
-	list_for_each_entry(queue, &pvr_dev->queues.active, node)
-		pvr_queue_start(queue);
-	list_for_each_entry(queue, &pvr_dev->queues.idle, node)
+	lockdep_assert_held_write(&pvr_dev->reset_sem);
+
+	list_for_each_entry_safe(queue, tmp, &pvr_dev->queues.resetting, reset_node) {
 		pvr_queue_start(queue);
-	mutex_unlock(&pvr_dev->queues.lock);
+		list_del_init(&queue->reset_node);
+		queue_work(pvr_dev->sched_wq, &queue->reset_put_work);
+	}
 }
 
 /**
@@ -1489,6 +1522,7 @@ int pvr_queue_device_init(struct pvr_device *pvr_dev)
 
 	INIT_LIST_HEAD(&pvr_dev->queues.active);
 	INIT_LIST_HEAD(&pvr_dev->queues.idle);
+	INIT_LIST_HEAD(&pvr_dev->queues.resetting);
 	err = drmm_mutex_init(from_pvr_device(pvr_dev), &pvr_dev->queues.lock);
 	if (err)
 		return err;
diff --git a/drivers/gpu/drm/imagination/pvr_queue.h b/drivers/gpu/drm/imagination/pvr_queue.h
index 149cc6d124bf..d46e9bb7b4f8 100644
--- a/drivers/gpu/drm/imagination/pvr_queue.h
+++ b/drivers/gpu/drm/imagination/pvr_queue.h
@@ -90,6 +90,19 @@ struct pvr_queue {
 	/** @node: Used to add the queue to the active/idle queue list. */
 	struct list_head node;
 
+	/**
+	 * @reset_node: Used to add the queue to pvr_device::queues::resetting
+	 * while a hard reset has it stopped. The queue holds a reference on
+	 * its context while it is on that list.
+	 */
+	struct list_head reset_node;
+
+	/**
+	 * @reset_put_work: Drops the context reference taken by a hard reset.
+	 * Context release can't run under pvr_device::reset_sem.
+	 */
+	struct work_struct reset_put_work;
+
 	/**
 	 * @in_flight_job_count: Number of jobs submitted to the CCCB that
 	 * have not been processed yet.

---
base-commit: 479e381e77dcde0dfcc9d51e89ff3273da199804
change-id: 20261125-rbrue-suez-pvr-pre-reset-lock-e2d7f7a44211

Best regards,
-- 
Ryan Brue <ryanbrue.dev@gmail.com>


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

* Re: [PATCH] drm/imagination: Don't hold queues.lock across drm_sched_stop() on reset
  2026-11-25 23:28 [PATCH] drm/imagination: Don't hold queues.lock across drm_sched_stop() on reset Ryan Brue
@ 2026-11-25 23:48 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25 23:48 UTC (permalink / raw)
  To: Ryan Brue; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/imagination: UAF when pvr_context_create() fails during
  hard reset
--

commit 56c699205a21c02f74c49f4ededa3a23cd759daf
Author: Ryan Brue <ryanbrue.dev@gmail.com>

drm/imagination: Don't hold queues.lock across drm_sched_stop() on reset

This commit pins each queue's context and moves the queues onto
pvr_dev->queues.resetting under pvr_dev->queues.lock so
pvr_queue_device_pre_reset() and pvr_queue_device_post_reset() can stop and
restart the schedulers without holding queues.lock.

> diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c
> index 34b826381aa2b..56751c192ce68 100644
> --- a/drivers/gpu/drm/imagination/pvr_queue.c
> +++ b/drivers/gpu/drm/imagination/pvr_queue.c
> @@ -1242,6 +1242,23 @@ static void reg_state_init(void *cpu_ptr, void *priv)
>  	}
>  }
>  
> +static void pvr_queue_reset_put_work(struct work_struct *w)
> +{
> +	struct pvr_queue *queue = container_of(w, struct pvr_queue, reset_put_work);
> +
> +	pvr_context_put(queue->ctx);
> +}
> +
> +static void pvr_queue_reset_hold(struct pvr_queue *queue, struct list_head *list)
> +{
> +	/* A put still pending from the last reset hands its reference over. */
> +	if (!cancel_work(&queue->reset_put_work) &&
> +	    !pvr_context_get_if_referenced(queue->ctx))
> +		return;
> +
> +	list_add_tail(&queue->reset_node, list);
> +}
> +
[ ... ]
> @@ -1402,26 +1422,39 @@ struct pvr_queue *pvr_queue_create(struct pvr_context *ctx,
>  
>  void pvr_queue_device_pre_reset(struct pvr_device *pvr_dev)
>  {
> +	struct list_head *resetting = &pvr_dev->queues.resetting;
>  	struct pvr_queue *queue;
>  
> +	lockdep_assert_held_write(&pvr_dev->reset_sem);
> +
> +	/*
> +	 * drm_sched_stop() waits for the run_job work, which takes
> +	 * queues.lock, so it can't be called with queues.lock held. Pin the
> +	 * queues and stop them without it. A queue whose context is already
> +	 * being released has no jobs left, so it needs no stopping.
> +	 */
>  	mutex_lock(&pvr_dev->queues.lock);
>  	list_for_each_entry(queue, &pvr_dev->queues.idle, node)
> -		pvr_queue_stop(queue, NULL);
> +		pvr_queue_reset_hold(queue, resetting);
>  	list_for_each_entry(queue, &pvr_dev->queues.active, node)
> -		pvr_queue_stop(queue, NULL);
> +		pvr_queue_reset_hold(queue, resetting);
>  	mutex_unlock(&pvr_dev->queues.lock);
> +
> +	list_for_each_entry(queue, resetting, reset_node)
> +		pvr_queue_stop(queue, NULL);
>  }

[Severity: High]
Can this cause a use-after-free on queue and ctx if pvr_context_create()
fails after pvr_queue_create() has added a queue to pvr_dev->queues.idle?

pvr_context_create() initializes ctx->ref_count to 1 and calls
pvr_context_create_queues(), where pvr_queue_create() publishes queue->node
onto pvr_dev->queues.idle under pvr_dev->queues.lock.  If a hard reset runs
before pvr_context_create() finishes and a later step such as init_fw_objs()
fails, pvr_context_create() unwinds by calling pvr_context_destroy_queues()
and kfree(ctx) directly instead of dropping ctx->ref_count:

drivers/gpu/drm/imagination/pvr_context.c:pvr_context_create() {
	...
	err = pvr_context_create_queues(ctx, args, ctx->data);
	if (err)
		goto err_free_ctx_id;

	err = init_fw_objs(ctx, args, ctx->data);
	if (err)
		goto err_destroy_queues;
	...
err_destroy_queues:
	pvr_context_destroy_queues(ctx, true);
	...
err_free_ctx:
	kfree(ctx);
	return err;
}

Because ctx->fw_obj is still NULL when init_fw_objs() fails,
pvr_queue_cleanup_fw_context() returns without taking pvr_dev->reset_sem,
and pvr_queue_destroy() only removes queue->node under pvr_dev->queues.lock
before freeing queue:

CPU0                                  CPU1
pvr_context_create()
  kref_init(&ctx->ref_count)
  pvr_context_create_queues()
    pvr_queue_create()
      list_add_tail(&queue->node,
                    &queues.idle)
                                      pvr_queue_device_pre_reset()
                                        mutex_lock(&queues.lock)
                                        pvr_queue_reset_hold()
                                          pvr_context_get_if_referenced()
                                          list_add_tail(&queue->reset_node,
                                                        resetting)
                                        mutex_unlock(&queues.lock)
  init_fw_objs() -> fails
  pvr_context_destroy_queues()
    pvr_queue_destroy()
      list_del_init(&queue->node)
      kfree(queue)
  kfree(ctx)
                                        pvr_queue_stop(queue, NULL)
                                          <-- use-after-free

pvr_queue_device_post_reset() and pvr_queue_reset_put_work() will then also
access the freed queue and ctx.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-rbrue-suez-pvr-pre-reset-lock-v1-1-0207245c5109@gmail.com?part=1

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

end of thread, other threads:[~2026-11-25 23:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 23:28 [PATCH] drm/imagination: Don't hold queues.lock across drm_sched_stop() on reset Ryan Brue
2026-11-25 23:48 ` sashiko-bot

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.