From: Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>
To: intel-xe@lists.freedesktop.org, shekhar.chauhan@intel.com,
harish.chegondi@intel.com
Cc: matthew.brost@intel.com
Subject: [PATCH v9 7/7] drm/xe/oa: Quiesce OA streams on driver unbind and PCI reset
Date: Fri, 20 Nov 2026 18:01:53 -0800 [thread overview]
Message-ID: <20261121020145.147384-16-umesh.nerlige.ramappa@intel.com> (raw)
In-Reply-To: <20261121020145.147384-9-umesh.nerlige.ramappa@intel.com>
An OA stream fd can outlive the device: after a PCI slot reset or after
unbind. The poll hrtimer and userspace calls on the fd could then access
MMIO and other device-bound objects that were released on remove,
causing a NULL pointer dereference, and xe_oa_release() would tear down
against freed objects.
Destroy a stream the same way whether its fd is closed or the device
goes away: xe_oa_destroy_locked() marks the stream dead, disables it,
restores the HW state and releases everything but the OA buffer and the
stream struct, whether or not the HW still responds. xe_oa_fini() calls
xe_oa_quiesce() to destroy the streams of a removed device, and
xe_oa_release() skips the destroy of such a dead stream. Only the
stream struct, with stream_lock and poll_wq valid, and the pinned OA
buffer stay allocated until the fd is closed or the process owning the
fd exits. The quiesce drops the DMA mapping of the OA buffer, which
stays pinned past xe_bo_pinned_fini(). On a wedged device, pending OA
batches aren't waited for, nothing in the ring runs anymore.
The quiesce needs no runtime PM reference of its own: xe_oa_fini() runs
after xe_pm_fini(), which keeps the device resumed with runtime suspend
forbidden. xe_oa_release() doesn't take one for a stream destroyed by
the quiesce either, so that it doesn't wake a removed device.
Once dead, OA interfaces return -ENODEV, poll reports EPOLLERR |
EPOLLHUP, and xe_oa_release() frees the stream struct and the OA
buffer, and drops the drm device reference.
Signed-off-by: Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>
---
v2: Include Sashiko's comments
v3: Fix circular lockdep issue due to scope of stream->mmap_mutex
v4: Rely on drm_dev_unplug() to zap OA buffer mmaps and on the dummy page
for refaults. Drop the OA specific fault handler and mmap_mutex. Split
out the stream destroy refactor and the runtime PM move into separate
patches. DMA-unmap the OA buffer on quiesce and run the OA ioctls in a
drm_dev_enter() section instead of the per-GT quiesced flag. Don't
restore the context state on quiesce, wait for pending OA batches on
quiesce, don't block unplug on a synchronous OA config wait and
quiesce a stream released during removal (Sashiko).
v5: Keep the OA buffer pinned until the stream fd is released instead of
DMA-unmapping it on quiesce, so the shrinker can't drop the last
reference after the device is gone (Sashiko).
v6: Run the stream fd ioctls in a drm_dev_enter() section and reject them
on a wedged device. Stop the synchronous OA config wait on a wedged
device too. Flush the TTM workqueue before dropping the drm device
reference on release of an unplugged device (Sashiko).
v7: Return -ENODEV rather than -ECANCELED from stream fd ioctls once the
device is unplugged, also when it is wedged. Fail the synchronous OA
config wait when it is cut short. DMA-unmap the OA buffer on quiesce,
as xe_bo_pinned_fini() can race with its release. Fix the
xe_oa_quiesce() comment (Sashiko).
v8: Destroy a stream the same way on close and on quiesce, whether or
not the device is wedged or still responds, all HW waits are
bounded. This also restores preemption and the context state on
quiesce. Drop the release time drm_dev_enter() section, gt_lock
already serializes release against the quiesce. Replace the stream
destroy split with a patch that only moves freeing the OA buffer and
the stream to the callers. Split out the ioctl and mmap
unplug/wedged fencing and the synchronous config wait into a
separate patch. Drop the runtime PM move, xe_oa_release() keeps the
runtime PM reference across freeing the OA buffer, so that unpinning
it can't race with the pinned BO eviction of a D3cold runtime
suspend (Sashiko).
v9: DMA-unmap the OA buffer on quiesce only, not on every stream destroy,
so that a pinned OA buffer is never left without its DMA mapping
across a suspend/resume. Don't wait for OA batches on a wedged
device. Drain the OA batches in the stream init error path too
(Sashiko).
---
drivers/gpu/drm/xe/xe_oa.c | 132 ++++++++++++++++++++++++++++---
drivers/gpu/drm/xe/xe_oa_types.h | 3 +
2 files changed, 125 insertions(+), 10 deletions(-)
diff --git a/drivers/gpu/drm/xe/xe_oa.c b/drivers/gpu/drm/xe/xe_oa.c
index 9c4c4548c4a0..ea08acbac326 100644
--- a/drivers/gpu/drm/xe/xe_oa.c
+++ b/drivers/gpu/drm/xe/xe_oa.c
@@ -261,13 +261,18 @@ static bool mert_wa_14026633728(struct xe_oa_stream *s)
static bool xe_oa_buffer_check_unlocked(struct xe_oa_stream *stream)
{
- u32 gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo);
- u32 tail, hw_tail, partial_report_size, available;
+ u32 gtt_offset, tail, hw_tail, partial_report_size, available;
int report_size = stream->oa_buffer.format->size;
unsigned long flags;
spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
+ if (stream->dead) {
+ spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
+ return false;
+ }
+
+ gtt_offset = xe_bo_ggtt_addr(stream->oa_buffer.bo);
hw_tail = xe_oa_hw_tail_read(stream);
hw_tail -= gtt_offset;
@@ -328,6 +333,9 @@ static enum hrtimer_restart xe_oa_poll_check_timer_cb(struct hrtimer *hrtimer)
struct xe_oa_stream *stream =
container_of(hrtimer, typeof(*stream), poll_check_timer);
+ if (READ_ONCE(stream->dead))
+ return HRTIMER_NORESTART;
+
if (xe_oa_buffer_check_unlocked(stream))
wake_up(&stream->poll_wq);
@@ -539,12 +547,19 @@ static void xe_oa_disable(struct xe_oa_stream *stream)
static int xe_oa_wait_unlocked(struct xe_oa_stream *stream)
{
+ int ret;
+
/* We might wait indefinitely if periodic sampling is not enabled */
if (!stream->periodic)
return -EINVAL;
- return wait_event_interruptible(stream->poll_wq,
- xe_oa_buffer_check_unlocked(stream));
+ ret = wait_event_interruptible(stream->poll_wq,
+ READ_ONCE(stream->dead) ||
+ xe_oa_buffer_check_unlocked(stream));
+ if (ret)
+ return ret;
+
+ return READ_ONCE(stream->dead) ? -ENODEV : 0;
}
#define OASTATUS_RELEVANT_BITS (OASTATUS_MMIO_TRG_Q_FULL | OASTATUS_COUNTER_OVERFLOW | \
@@ -573,6 +588,9 @@ static ssize_t xe_oa_read(struct file *file, char __user *buf,
size_t offset = 0;
int ret;
+ if (READ_ONCE(stream->dead))
+ return -ENODEV;
+
if (!stream->sample)
return -EINVAL;
@@ -583,13 +601,14 @@ static ssize_t xe_oa_read(struct file *file, char __user *buf,
return ret;
mutex_lock(&stream->stream_lock);
- ret = __xe_oa_read(stream, buf, count, &offset);
+ ret = stream->dead ? -ENODEV :
+ __xe_oa_read(stream, buf, count, &offset);
mutex_unlock(&stream->stream_lock);
} while (!offset && !ret);
} else {
xe_oa_buffer_check_unlocked(stream);
mutex_lock(&stream->stream_lock);
- ret = __xe_oa_read(stream, buf, count, &offset);
+ ret = stream->dead ? -ENODEV : __xe_oa_read(stream, buf, count, &offset);
mutex_unlock(&stream->stream_lock);
}
@@ -616,6 +635,9 @@ static __poll_t xe_oa_poll_locked(struct xe_oa_stream *stream,
poll_wait(file, &stream->poll_wq, wait);
+ if (stream->dead)
+ return EPOLLERR | EPOLLHUP;
+
/*
* We don't explicitly check whether there's something to read here since this
* path may be hot depending on what else userspace is polling, or on the timeout
@@ -865,7 +887,9 @@ static void xe_oa_stream_drain(struct xe_oa_stream *stream)
struct xe_exec_queue *q = stream->exec_q ?: stream->k_exec_q;
long timeout;
- if (!stream->last_fence || dma_fence_is_signaled(stream->last_fence))
+ /* Nothing in the ring runs anymore on a wedged device */
+ if (xe_wedged(stream->oa->xe) || !stream->last_fence ||
+ dma_fence_is_signaled(stream->last_fence))
return;
/*
@@ -1715,6 +1739,9 @@ static long xe_oa_ioctl_locked(struct xe_oa_stream *stream,
unsigned int cmd,
unsigned long arg)
{
+ if (stream->dead)
+ return -ENODEV;
+
switch (cmd) {
case DRM_XE_OBSERVATION_IOCTL_ENABLE:
return xe_oa_enable_locked(stream);
@@ -1754,33 +1781,67 @@ 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;
+ /*
+ * Don't wake a removed device for a stream xe_oa_fini() destroyed. If xe_oa_fini()
+ * marks the stream dead only after this, the device is still bound while it does.
+ */
+ bool rpm = !READ_ONCE(stream->dead);
- xe_pm_runtime_get(gt_to_xe(gt));
+ if (rpm)
+ xe_pm_runtime_get(gt_to_xe(gt));
+ /* gt_lock serializes against xe_oa_fini(), which destroys the stream on device removal */
mutex_lock(>->oa.gt_lock);
- xe_oa_destroy_locked(stream);
+ if (!stream->dead)
+ xe_oa_destroy_locked(stream);
mutex_unlock(>->oa.gt_lock);
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));
+
+ /*
+ * 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(>_to_xe(gt)->drm))
+ flush_workqueue(gt_to_xe(gt)->ttm.wq);
/* Release the reference the OA stream kept on the driver */
drm_dev_put(>_to_xe(gt)->drm);
@@ -1809,6 +1870,11 @@ static int xe_oa_mmap(struct file *file, struct vm_area_struct *vma)
if (!drm_dev_enter(&xe->drm, &idx))
return -ENODEV;
+ if (READ_ONCE(stream->dead)) {
+ ret = -ENODEV;
+ goto exit;
+ }
+
/*
* Userspace always maps the OA buffer at offset 0, the mapping is moved to the OA
* buffer's own range in the drm device's address_space below
@@ -1957,6 +2023,7 @@ static int xe_oa_stream_init(struct xe_oa_stream *stream,
return 0;
err_put_k_exec_q:
+ xe_oa_stream_drain(stream);
xe_oa_disable_metric_set(stream);
xe_exec_queue_put(stream->k_exec_q);
err_free_oa_buf:
@@ -2673,6 +2740,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(>->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(>->oa.gt_lock);
+}
+
+/**
+ * xe_oa_quiesce - Disable all active OA streams on driver unbind or PCI slot reset
+ * @xe: @xe_device
+ *
+ * Destroys every active stream like closing its fd would, see xe_oa_destroy_locked().
+ * Only the stream struct and the pinned OA buffer stay allocated until userspace closes
+ * the fd, at which point xe_oa_release() frees them.
+ */
+static void xe_oa_quiesce(struct xe_device *xe)
+{
+ struct xe_gt *gt;
+ u8 id;
+
+ if (!xe->oa.xe)
+ return;
+
+ for_each_gt(gt, xe, id)
+ xe_oa_quiesce_gt(gt);
+}
+
/**
* xe_oa_register - Xe OA registration
* @xe: @xe_device
@@ -2984,6 +3094,8 @@ static void xe_oa_fini(void *arg)
if (!oa->xe)
return;
+ xe_oa_quiesce(xe);
+
idr_for_each(&oa->metrics_idr, destroy_config, oa);
idr_destroy(&oa->metrics_idr);
diff --git a/drivers/gpu/drm/xe/xe_oa_types.h b/drivers/gpu/drm/xe/xe_oa_types.h
index e876e9be92ba..e60a90bae1c6 100644
--- a/drivers/gpu/drm/xe/xe_oa_types.h
+++ b/drivers/gpu/drm/xe/xe_oa_types.h
@@ -214,6 +214,9 @@ struct xe_oa_stream {
/** @enabled: Whether the stream is currently enabled */
bool enabled;
+ /** @dead: Gate HW access when device is wedged or going away */
+ bool dead;
+
/** @oa_config: OA configuration used by the stream */
struct xe_oa_config *oa_config;
--
2.55.0
next prev parent reply other threads:[~2026-11-21 2:02 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 2:01 [PATCH v9 0/7] Cleanup OA stream on PCI unbind/remove Umesh Nerlige Ramappa
2026-11-21 2:01 ` [PATCH v9 1/7] drm/xe/oa: Ignore a HW tail pointer outside the OA buffer Umesh Nerlige Ramappa
2026-11-21 2:01 ` [PATCH v9 2/7] drm/xe/oa: Use the drm device address_space for the OA stream fd Umesh Nerlige Ramappa
2026-11-21 2:01 ` [PATCH v9 3/7] drm/xe: Reject vm_ops->access on BO mappings of an unplugged device Umesh Nerlige Ramappa
2026-11-21 2:01 ` [PATCH v9 4/7] drm/xe/oa: Wait for in-flight OA batches before stream HW teardown Umesh Nerlige Ramappa
2026-11-21 2:01 ` [PATCH v9 5/7] drm/xe/oa: Fence OA ioctls and mmap against device unplug and wedging Umesh Nerlige Ramappa
2026-11-21 2:01 ` [PATCH v9 6/7] drm/xe/oa: Free the OA buffer and the stream after the stream destroy Umesh Nerlige Ramappa
2026-11-21 2:01 ` Umesh Nerlige Ramappa [this message]
2026-11-21 2:09 ` ✗ CI.checkpatch: warning for Cleanup OA stream on PCI unbind/remove (rev9) 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=20261121020145.147384-16-umesh.nerlige.ramappa@intel.com \
--to=umesh.nerlige.ramappa@intel.com \
--cc=harish.chegondi@intel.com \
--cc=intel-xe@lists.freedesktop.org \
--cc=matthew.brost@intel.com \
--cc=shekhar.chauhan@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.