Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Brost <matthew.brost@intel.com>
To: Arvind Yadav <arvind.yadav@intel.com>
Cc: <intel-xe@lists.freedesktop.org>,
	<himal.prasad.ghimiray@intel.com>,
	<thomas.hellstrom@linux.intel.com>, <rodrigo.vivi@intel.com>,
	<tejas.upadhyay@intel.com>
Subject: Re: [PATCH v2] drm/xe/guc: Avoid self-deadlock in queue destruction
Date: Wed, 10 Jun 2026 09:23:14 -0700	[thread overview]
Message-ID: <aimPcuW9zjRuFOzD@gsse-cloud1.jf.intel.com> (raw)
In-Reply-To: <aimLsPSqWY7rUjCS@gsse-cloud1.jf.intel.com>

On Wed, Jun 10, 2026 at 09:07:12AM -0700, Matthew Brost wrote:
> On Wed, Jun 10, 2026 at 05:39:45PM +0530, Arvind Yadav wrote:
> > 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.
> > 
> 
> I’m convinced an extra worker is required here.
> 
> The teardown sequence is a mess, but in the DRM dep work [1] I was able
> to move xe->destroy_wq into the DRM dep layer [2] (see
> drm_dep_queue_free_work). I also called drm_dev_get on each queue
> creation and dropped it in drm_dep_queue_free_work via
> drm_dep_queue_free(). It all seemed to work without an additional
> worker.
> 
> In [3], I refactored the Xe object model on top of DRM dep using the DRM
> worker. I was also able to drop the wait_event_timeout in
> guc_submit_sw_fini [4] around the async release of GuC IDs, because the
> drm_dep_get on the queue prevented guc_submit_sw_fini from running until
> all queues had dropped their drm_dev reference.
> 
> I do plan to respin DRM dep in the next few weeks, but given that this
> is common DRM code, it may take a while to merge.
> 
> That said, I don’t see why we can’t merge the DRM dep drm_dev_get/put
> model ahead that, hook a drm_dev_get into exec queue creation, drop it
> as the final action of guc_exec_queue_fini, and remove the
> wait_event_timeout in guc_submit_sw_fini. I think that would work and is
> simpler than what this patch is doing (i.e., it should only be 2 lines
> of code for drm_dev_get/put + delete some unused code).
> 

Sorry for the double reply—I missed a key part of this.

The DRM dep worker that executes drm_dep_queue_free_work is a DRM dep
module worker (see dep_free_wq in [1]), which is created and destroyed
during module load/unlaod. We would need to move xe->destroy_wq into the
Xe module as well, or simply drop it and use system_dfl_wq for the async
queue teardown step. So I guess it’s a little more than two lines of
code, but still relatively straightforward.

Matt

> Matt
> 
> [1] https://patchwork.freedesktop.org/series/163245/
> [2] https://patchwork.freedesktop.org/patch/711933/?series=163245&rev=1
> [3] https://patchwork.freedesktop.org/patch/711937/?series=163245&rev=1
> [4] https://patchwork.freedesktop.org/patch/711934/?series=163245&rev=1
> 
> > 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
> > 

  reply	other threads:[~2026-06-10 16:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aimPcuW9zjRuFOzD@gsse-cloud1.jf.intel.com \
    --to=matthew.brost@intel.com \
    --cc=arvind.yadav@intel.com \
    --cc=himal.prasad.ghimiray@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=tejas.upadhyay@intel.com \
    --cc=thomas.hellstrom@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox