Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] drm/xe/guc: Avoid self-deadlock in queue destruction
@ 2026-06-10 12:09 Arvind Yadav
  2026-06-10 12:16 ` ✓ CI.KUnit: success for drm/xe/guc: Avoid self-deadlock in queue destruction (rev3) Patchwork
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Arvind Yadav @ 2026-06-10 12:09 UTC (permalink / raw)
  To: intel-xe
  Cc: matthew.brost, himal.prasad.ghimiray, thomas.hellstrom,
	rodrigo.vivi, tejas.upadhyay

GuC exec queue destruction can run on xe->destroy_wq. During queue
cleanup, xe_exec_queue_fini() can drop the last reference to objects
which eventually release the DRM device and run drmm cleanup actions.

guc_submit_sw_fini(), registered as a drmm action, drains
xe->destroy_wq. If the final drm_dev_put() happens from an
xe->destroy_wq worker, teardown can try to drain the same workqueue
the current worker is running on, causing a self-deadlock.

Fix this by taking an extra drm_device reference across async exec queue
cleanup and dropping it from system_dfl_wq. The deferred put work is
preallocated during queue init, so the destroy path does not need a
failing or nofail allocation.

This keeps the final drm_dev_put() out of xe->destroy_wq while preserving
exec queue teardown ordering.

v2:
   - rebase

Fixes: 2d2be279f1ca ("drm/xe: fix UAF around queue destruction")
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
Reviewed-by: Tejas Upadhyay <tejas.upadhyay@intel.com>
Signed-off-by: Arvind Yadav <arvind.yadav@intel.com>
---
 drivers/gpu/drm/xe/xe_guc_exec_queue_types.h |  3 +
 drivers/gpu/drm/xe/xe_guc_submit.c           | 62 ++++++++++++++++++--
 2 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_guc_exec_queue_types.h b/drivers/gpu/drm/xe/xe_guc_exec_queue_types.h
index e5e53b421f29..b5dfc9c9092d 100644
--- a/drivers/gpu/drm/xe/xe_guc_exec_queue_types.h
+++ b/drivers/gpu/drm/xe/xe_guc_exec_queue_types.h
@@ -13,6 +13,7 @@
 
 struct dma_fence;
 struct xe_exec_queue;
+struct xe_drm_dev_put_work;
 
 /**
  * struct xe_guc_exec_queue - GuC specific state for an xe_exec_queue
@@ -35,6 +36,8 @@ struct xe_guc_exec_queue {
 	struct xe_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
 	/** @destroy_async: do final destroy async from this worker */
 	struct work_struct destroy_async;
+	/** @dev_put_work: deferred drm_dev_put worker to avoid self-deadlock */
+	struct xe_drm_dev_put_work *dev_put_work;
 	/** @resume_time: time of last resume */
 	u64 resume_time;
 	/** @state: GuC specific state for this xe_exec_queue */
diff --git a/drivers/gpu/drm/xe/xe_guc_submit.c b/drivers/gpu/drm/xe/xe_guc_submit.c
index b29cc08e6291..d1ae4339f375 100644
--- a/drivers/gpu/drm/xe/xe_guc_submit.c
+++ b/drivers/gpu/drm/xe/xe_guc_submit.c
@@ -10,6 +10,7 @@
 #include <linux/circ_buf.h>
 #include <linux/dma-fence-array.h>
 
+#include <drm/drm_drv.h>
 #include <drm/drm_managed.h>
 
 #include "abi/guc_actions_abi.h"
@@ -47,6 +48,11 @@
 
 #define XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN		6
 
+struct xe_drm_dev_put_work {
+	struct work_struct work;
+	struct drm_device *drm;
+};
+
 static int guc_submit_reset_prepare(struct xe_guc *guc);
 
 static struct xe_guc *
@@ -1674,6 +1680,8 @@ static void guc_exec_queue_fini(struct xe_exec_queue *q)
 	xe_sched_entity_fini(&ge->entity);
 	xe_sched_fini(&ge->sched);
 
+	kfree(ge->dev_put_work);
+
 	/*
 	 * RCU free due sched being exported via DRM scheduler fences
 	 * (timeline name).
@@ -1681,20 +1689,55 @@ static void guc_exec_queue_fini(struct xe_exec_queue *q)
 	kfree_rcu(ge, rcu);
 }
 
-static void __guc_exec_queue_destroy_async(struct work_struct *w)
+static void xe_drm_dev_put_work_func(struct work_struct *work)
 {
-	struct xe_guc_exec_queue *ge =
-		container_of(w, struct xe_guc_exec_queue, destroy_async);
-	struct xe_exec_queue *q = ge->q;
+	struct xe_drm_dev_put_work *put =
+		container_of(work, struct xe_drm_dev_put_work, work);
+
+	drm_dev_put(put->drm);
+	kfree(put);
+}
+
+static void guc_exec_queue_do_destroy(struct xe_exec_queue *q, bool defer_dev_put)
+{
+	struct xe_guc_exec_queue *ge = q->guc;
 	struct xe_guc *guc = exec_queue_to_guc(q);
+	struct xe_device *xe = guc_to_xe(guc);
+	struct drm_device *drm = &xe->drm;
+	struct xe_drm_dev_put_work *put_work = NULL;
 
-	guard(xe_pm_runtime)(guc_to_xe(guc));
+	if (defer_dev_put) {
+		put_work = ge->dev_put_work;
+		ge->dev_put_work = NULL;
+
+		put_work->drm = drm;
+		INIT_WORK(&put_work->work, xe_drm_dev_put_work_func);
+
+		/*
+		 * Avoid running the final drm_dev_put() from xe->destroy_wq,
+		 * since guc_submit_sw_fini() drains that queue.
+		 */
+		drm_dev_get(drm);
+	}
+
+	guard(xe_pm_runtime)(xe);
 	trace_xe_exec_queue_destroy(q);
 
 	/* Confirm no work left behind accessing device structures */
 	cancel_delayed_work_sync(&ge->sched.base.work_tdr);
 
 	xe_exec_queue_fini(q);
+
+	if (put_work)
+		queue_work(system_dfl_wq, &put_work->work);
+}
+
+static void __guc_exec_queue_destroy_async(struct work_struct *w)
+{
+	struct xe_guc_exec_queue *ge =
+		container_of(w, struct xe_guc_exec_queue, destroy_async);
+
+	guc_exec_queue_do_destroy(ge->q, true);
 }
 
 static void guc_exec_queue_destroy_async(struct xe_exec_queue *q)
@@ -1706,7 +1749,7 @@ static void guc_exec_queue_destroy_async(struct xe_exec_queue *q)
 
 	/* We must block on kernel engines so slabs are empty on driver unload */
 	if (q->flags & EXEC_QUEUE_FLAG_PERMANENT || exec_queue_wedged(q))
-		__guc_exec_queue_destroy_async(&q->guc->destroy_async);
+		guc_exec_queue_do_destroy(q, false);
 	else
 		queue_work(xe->destroy_wq, &q->guc->destroy_async);
 }
@@ -1914,6 +1957,12 @@ static int guc_exec_queue_init(struct xe_exec_queue *q)
 	if (!ge)
 		return -ENOMEM;
 
+	ge->dev_put_work = kmalloc_obj(*ge->dev_put_work, GFP_KERNEL);
+	if (!ge->dev_put_work) {
+		kfree(ge);
+		return -ENOMEM;
+	}
+
 	q->guc = ge;
 	ge->q = q;
 	init_rcu_head(&ge->rcu);
@@ -1989,6 +2038,7 @@ static int guc_exec_queue_init(struct xe_exec_queue *q)
 err_release_id:
 	release_guc_id(guc, q);
 err_free:
+	kfree(ge->dev_put_work);
 	kfree(ge);
 
 	return err;
-- 
2.43.0


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

end of thread, other threads:[~2026-06-11  9:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-10 12:09 [PATCH v2] drm/xe/guc: Avoid self-deadlock in queue destruction Arvind Yadav
2026-06-10 12:16 ` ✓ CI.KUnit: success for drm/xe/guc: Avoid self-deadlock in queue destruction (rev3) Patchwork
2026-06-10 12:54 ` ✓ Xe.CI.BAT: " Patchwork
2026-06-10 16:07 ` [PATCH v2] drm/xe/guc: Avoid self-deadlock in queue destruction Matthew Brost
2026-06-10 16:23   ` Matthew Brost
2026-06-11  9:08     ` Yadav, Arvind
2026-06-10 17:04 ` ✓ Xe.CI.FULL: success for drm/xe/guc: Avoid self-deadlock in queue destruction (rev3) Patchwork

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