All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Umesh Nerlige Ramappa" <umesh.nerlige.ramappa@intel.com>,
	intel-xe@lists.freedesktop.org
Subject: Re: [PATCH v10 7/7] drm/xe/oa: Quiesce OA streams on driver unbind and PCI reset
Date: Tue, 24 Nov 2026 21:02:24 +0000	[thread overview]
Message-ID: <sashiko-outbox-232014@kernel.org> (raw)
In-Reply-To: <20261124201150.405479-16-umesh.nerlige.ramappa@intel.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] drm/xe/oa: OA buffer BO refs in xe_oa_release() outlive
  ttm.wq flush
- [Medium] drm/xe/oa: drm_dev_is_unplugged() race in xe_oa_release()
  skips ttm.wq flush
- [Medium] drm/xe/oa: async queue teardown in xe_oa_quiesce() after
  xe_destroy_wq_flush()
--

commit 060135101d7eb021924f5ed68180ceaa519804a1
Author: Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>

drm/xe/oa: Quiesce OA streams on driver unbind and PCI reset

This commit quiesces active OA streams in xe_oa_fini() on driver unbind or
PCI reset by marking them dead, tearing down hardware and device-bound
resources, and deferring freeing the stream struct and OA buffer until
xe_oa_release().

> diff --git a/drivers/gpu/drm/xe/xe_oa.c b/drivers/gpu/drm/xe/xe_oa.c
> index d37724e280160..2b88f3fa210d9 100644
> --- a/drivers/gpu/drm/xe/xe_oa.c
> +++ b/drivers/gpu/drm/xe/xe_oa.c

[ ... ]

> @@ -1755,33 +1782,69 @@ static long xe_oa_ioctl(struct file *file,
>  	return ret;
>  }
>  
> +/*
> + * Disable the stream, restore the HW state and release everything but the OA buffer and the
> + * stream struct, the same whether the stream fd is closed or the device goes away, and
> + * whether or not the HW still responds: all HW waits are bounded. Afterwards the stream is
> + * dead and any further read, poll, mmap or ioctl on the stream fd fails with -ENODEV.
> + */
>  static void xe_oa_destroy_locked(struct xe_oa_stream *stream)
>  {
> +	unsigned long flags;
> +
> +	mutex_lock(&stream->stream_lock);
> +
> +	spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
> +	WRITE_ONCE(stream->dead, true);
> +	spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
> +
>  	if (stream->enabled)
>  		xe_oa_disable_locked(stream);
>  
> +	wake_up_all(&stream->poll_wq);
> +
>  	xe_oa_stream_destroy(stream);
>  
>  	if (stream->exec_q)
>  		xe_exec_queue_put(stream->exec_q);
>  
>  	drm_syncobj_put(stream->ufence_syncobj);
> +	mutex_unlock(&stream->stream_lock);
>  }
>  
>  static int xe_oa_release(struct inode *inode, struct file *file)
>  {
>  	struct xe_oa_stream *stream = file->private_data;
>  	struct xe_gt *gt = stream->gt;
> +	bool rpm;
>  
> -	xe_pm_runtime_get(gt_to_xe(gt));
> +	/*
> +	 * gt_lock serializes against xe_oa_fini(), which destroys the stream on device removal.
> +	 * Unless it did, the device stays bound while this holds gt_lock, so take a runtime PM
> +	 * reference only then, and don't wake a removed device. Free the OA buffer under gt_lock
> +	 * too, so that it is unpinned before xe_oa_fini() and xe_bo_pinned_fini() can run.
> +	 */
>  	mutex_lock(&gt->oa.gt_lock);
> -	xe_oa_destroy_locked(stream);
> -	mutex_unlock(&gt->oa.gt_lock);
> +	rpm = !stream->dead;
> +	if (rpm) {
> +		xe_pm_runtime_get(gt_to_xe(gt));
> +		xe_oa_destroy_locked(stream);
> +	}
>  
>  	xe_oa_free_oa_buffer(stream);
>  	mutex_destroy(&stream->stream_lock);
>  	kfree(stream);
> -	xe_pm_runtime_put(gt_to_xe(gt));
> +	if (rpm)
> +		xe_pm_runtime_put(gt_to_xe(gt));
> +	mutex_unlock(&gt->oa.gt_lock);
> +
> +	/*
> +	 * Freeing the OA buffer may defer the BO destroy to the TTM workqueue. Once the device
> +	 * is unplugged, this may be the last reference on the drm device, so let the destroy
> +	 * finish before drm_dev_put() releases the GGTT.
> +	 */
> +	if (drm_dev_is_unplugged(&gt_to_xe(gt)->drm))
> +		flush_workqueue(gt_to_xe(gt)->ttm.wq);

[Severity: Medium]
Can a temporary reference on stream->oa_buffer.bo outlive xe_oa_release()
and defer BO destruction past flush_workqueue(xe->ttm.wq)?

xe_oa_quiesce_gt() calls xe_bo_dma_unmap_pinned(stream->oa_buffer.bo),
which clears xe_tt->sg but leaves bo->pinned_link on
xe->pinned.early.kernel_bo_present.  During devres teardown,
xe_bo_pinned_fini() -> xe_bo_apply_to_pinned() visits that list and takes
a temporary reference outside gt->oa.gt_lock and xe->pinned.lock:

drivers/gpu/drm/xe/xe_bo_evict.c:xe_bo_apply_to_pinned() {
	...
		xe_bo_get(bo);
		list_move_tail(&bo->pinned_link, &still_in_list);
		spin_unlock(&xe->pinned.lock);

		ret = pinned_fn(bo);
		if (ret && unpinned_fn) {
			spin_lock(&xe->pinned.lock);
			if (!list_empty(&bo->pinned_link))
				list_move(&bo->pinned_link, new_list);
			spin_unlock(&xe->pinned.lock);
		}
		xe_bo_put(bo);
	...
}

This allows the final BO put to happen after flush_workqueue(xe->ttm.wq):

CPU0 (devres teardown)            CPU1 (close OA fd)
xe_oa_fini()
  xe_oa_quiesce_gt()
    xe_bo_dma_unmap_pinned(bo)
    unlock(&gt->oa.gt_lock)
xe_bo_pinned_fini()
  xe_bo_apply_to_pinned()
    xe_bo_get(bo)
                                  xe_oa_release()
                                    xe_oa_free_oa_buffer()
                                      xe_bo_unpin_map_no_vm(bo)
                                        xe_bo_unpin(bo)
                                        xe_bo_put(bo)
                                    flush_workqueue(xe->ttm.wq)
    xe_bo_put(bo)
      ttm_bo_release()
        queue_work(ttm.wq,
                   &bo->delayed_delete)
                                    drm_dev_put()
                                      drm_managed_release()
                                        ggtt_fini_early()
                                          drm_mm_takedown(&ggtt->mm)
                                          /* frees tile->mem.ggtt */

kworker (xe->ttm.wq):
ttm_bo_delayed_delete()
  xe_ttm_bo_destroy()
    xe_ggtt_remove_bo(tile->mem.ggtt, bo)  <-- use-after-free

In addition, xe_bo_unpin_map_no_vm() unpins the populated OA buffer and
unlocks bo's dma_resv before calling xe_bo_put(bo).  Because
xe_bo_unpin() calls xe_bo_account_unpin() (re-adding the BO's pages to
shrinker->shrinkable_pages) and returns the BO to the TTM LRU, a
concurrent xe_shrinker_scan() can likewise acquire a temporary BO
reference before xe_bo_put(bo), or call xe_shrinker_runtime_pm_get() and
queue shrinker->pm_worker on the unplugged device.

> +
> +	/* Release the reference the OA stream kept on the driver */
> +	drm_dev_put(&gt_to_xe(gt)->drm);
>  
>  	return 0;
>  }

[Severity: Medium]
Can drm_dev_is_unplugged() return false in xe_oa_release() when racing
with xe_device_remove(), skipping flush_workqueue(xe->ttm.wq) right before
the last drm_device reference is dropped?

drm_dev_is_unplugged() drops its SRCU read lock immediately, and
xe_device_remove() -> xe_bo_pci_dev_remove_all() does not flush
xe->ttm.wq for already-deleted BOs (ttm_bo_evict_first() only calls
ttm_bo_cleanup_memtype_use() when bo->deleted is true):

CPU0 (close OA fd)                CPU1 (driver unbind)
xe_oa_release()
  xe_oa_free_oa_buffer()
    xe_bo_put(bo)
      /* queues bo->delayed_delete
         on xe->ttm.wq */
  if (drm_dev_is_unplugged(...))
    /* false: flush skipped */
                                  xe_pci_remove()
                                    xe_device_remove()
                                      drm_dev_unplug()
                                      xe_bo_pci_dev_remove_all()
                                    ...
                                    devm_drm_dev_init_release()
                                      drm_dev_put()
  drm_dev_put()
    drm_managed_release()
      ggtt_fini_early()
        drm_mm_takedown(&ggtt->mm)

kworker (xe->ttm.wq):
ttm_bo_delayed_delete()
  xe_ttm_bo_destroy()
    xe_ggtt_remove_bo(tile->mem.ggtt, bo)  <-- use-after-free

Because ggtt_fini_early() is registered with drmm after
xe_device_destroy(), final drm_dev_put() runs ggtt_fini_early() and frees
tile->mem.ggtt before xe_device_destroy() calls ttm_device_fini(&xe->ttm)
to drain xe->ttm.wq.

[ ... ]

> @@ -2674,6 +2743,49 @@ static void xe_oa_unregister(void *arg)
>  	oa->metrics_kobj = NULL;
>  }
>  
> +static void xe_oa_quiesce_gt(struct xe_gt *gt)
> +{
> +	int i;
> +
> +	mutex_lock(&gt->oa.gt_lock);
> +	for (i = 0; i < gt->oa.num_oa_units; i++) {
> +		struct xe_oa_stream *stream = gt->oa.oa_unit[i].exclusive_stream;
> +
> +		if (!stream)
> +			continue;
> +
> +		xe_oa_destroy_locked(stream);
> +
> +		/*
> +		 * The OA buffer stays pinned until the stream fd is released, an mmap may still
> +		 * hold a reference on it. Drop its DMA mapping now: the fd may be released, and
> +		 * the BO unpinned, while xe_bo_pinned_fini() DMA-unmaps it without the BO lock.
> +		 */
> +		xe_bo_dma_unmap_pinned(stream->oa_buffer.bo);
> +	}
> +	mutex_unlock(&gt->oa.gt_lock);
> +}

[Severity: Medium]
Does dropping stream->k_exec_q and stream->exec_q in xe_oa_quiesce_gt()
queue asynchronous GuC exec queue destruction after xe_pci_remove() has
already called xe_destroy_wq_flush() and xe_pm_fini()?

In xe_pci_remove(), xe_destroy_wq_flush() and xe_pm_fini() run before
devres invokes xe_oa_fini() -> xe_oa_quiesce() -> xe_oa_quiesce_gt().
When xe_oa_destroy_locked() calls xe_exec_queue_put() on stream->k_exec_q
and stream->exec_q, guc_exec_queue_destroy() queues cleanup onto
gt->ordered_wq and then guc_exec_queue_do_destroy() onto xe_destroy_wq:

CPU0 (devres teardown)            kworker (xe_destroy_wq)
xe_oa_fini()
  xe_oa_quiesce_gt()
    xe_oa_destroy_locked()
      xe_exec_queue_put(k_exec_q)
        /* queues cleanup to
           gt->ordered_wq ->
           xe_destroy_wq */
xe_bo_pinned_fini()
  xe_bo_dma_unmap_pinned(lrc->bo)
                                  guc_exec_queue_do_destroy()
                                    xe_exec_queue_fini()
                                      xe_lrc_finish()
                                        xe_bo_unpin_map_no_vm(lrc->bo)
                                    drm_dev_put(drm)

Because xe_destroy_wq is not flushed again after xe_oa_quiesce(),
xe_lrc_finish() can unpin lrc->bo and lrc->seqno_bo while
xe_bo_pinned_fini() accesses xe_tt->sg without the BO lock.  Furthermore,
if xe_oa_release() finishes while guc_exec_queue_do_destroy() is still in
flight, guc_exec_queue_do_destroy() holds the last drm_device reference
and drops it in drm_dev_put(drm) without flushing xe->ttm.wq, allowing
ggtt_fini_early() to tear down tile->mem.ggtt before deferred LRC BO
destruction on xe->ttm.wq removes its GGTT nodes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124201150.405479-9-umesh.nerlige.ramappa@intel.com?part=7

  reply	other threads:[~2026-11-24 21:02 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 20:11 [PATCH v10 0/7] Cleanup OA stream on PCI unbind/remove Umesh Nerlige Ramappa
2026-11-24 20:11 ` [PATCH v10 1/7] drm/xe/oa: Ignore a HW tail pointer outside the OA buffer Umesh Nerlige Ramappa
2026-11-24 20:11 ` [PATCH v10 2/7] drm/xe/oa: Use the drm device address_space for the OA stream fd Umesh Nerlige Ramappa
2026-11-24 20:11 ` [PATCH v10 3/7] drm/xe: Reject vm_ops->access on BO mappings of an unplugged device Umesh Nerlige Ramappa
2026-11-24 20:11 ` [PATCH v10 4/7] drm/xe/oa: Wait for in-flight OA batches before stream HW teardown Umesh Nerlige Ramappa
2026-11-24 20:22   ` sashiko-bot
2026-11-24 20:11 ` [PATCH v10 5/7] drm/xe/oa: Fence OA ioctls and mmap against device unplug and wedging Umesh Nerlige Ramappa
2026-11-24 20:35   ` sashiko-bot
2026-11-30 18:07     ` Umesh Nerlige Ramappa
2026-11-25  6:34   ` Shekhar Chauhan
2026-11-24 20:11 ` [PATCH v10 6/7] drm/xe/oa: Free the OA buffer and the stream after the stream destroy Umesh Nerlige Ramappa
2026-11-24 20:11 ` [PATCH v10 7/7] drm/xe/oa: Quiesce OA streams on driver unbind and PCI reset Umesh Nerlige Ramappa
2026-11-24 21:02   ` sashiko-bot [this message]
2026-11-25  6:36   ` Shekhar Chauhan
2026-11-24 21:29 ` ✓ CI.KUnit: success for Cleanup OA stream on PCI unbind/remove (rev10) Patchwork
2026-11-24 22:21 ` ✓ Xe.CI.BAT: " Patchwork
2026-11-25  1:00 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-11-30 18:14   ` Umesh Nerlige Ramappa

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=sashiko-outbox-232014@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=umesh.nerlige.ramappa@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 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.