Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 546DA606A31A for ; Fri, 30 Oct 2026 22:31:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=JoO3dvAL Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1B29F112A2B; Fri, 30 Oct 2026 22:31:24 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) by gabe.freedesktop.org (Postfix) with ESMTPS id C2DC3112A15 for ; Fri, 30 Oct 2026 22:31:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1793399475; x=1824935475; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=X2Wh3tdAFZYYvn4WDhyvp4zs+LEqsOm7p/VNIKJUbNw=; b=JoO3dvALbvDWUlGQlVeQIjPr65MhhtAmtw1nKXwfPqcB5kQE+mZO/9qD GJUkWHQ2A95GnZZ9XcIY3KcrYVhjYrtOI4b2gaf9LigYkK0zHA6MUkgZH J5GQwTBGINYbgrGEpyO2JTqbwiFGMIyy9uaCLfgJwN38qi5mua8uu+2KT 5qvNEe7Cy3xPAQZ62Y2Aw04wUS7nFPBhfWhZ2qj9mqFtvdOYK77+GQXdR onqblHBPqzU+FuJrh0OwOeUWfnedI1WwKhHeO4ZiDAd7YQK06Jt2yYZIn 2dxkOlEkYh/5rCPQIax52tTuYE46nlziwlHKjW8FjOx2KIZWrUEhkBqnP g==; X-CSE-ConnectionGUID: w/GUBZxFTX+Kiza3zNSTpg== X-CSE-MsgGUID: WialfPA+TeW0OfhrL3W0cA== X-IronPort-AV: E=McAfee;i="6800,10657,11951"; a="2049988" X-IronPort-AV: E=Sophos;i="6.27,186,1787036400"; d="scan'208";a="2049988" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2026 15:31:05 -0700 X-CSE-ConnectionGUID: g741zKc+SSiBqYFCQCF2mA== X-CSE-MsgGUID: us+7kZI9RjaIN4aVWfklYw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,186,1787036400"; d="scan'208";a="5550441" Received: from orsosgc001.jf.intel.com ([10.54.56.63]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2026 15:31:05 -0700 From: Umesh Nerlige Ramappa To: intel-xe@lists.freedesktop.org, Matthew Brost Cc: riana.tauro@intel.com, shekhar.chauhan@intel.com, harish.chegondi@intel.com Subject: [PATCH v2 3/3] drm/xe/oa: Quiesce OA streams on driver unbind and PCI reset Date: Fri, 30 Oct 2026 15:31:00 -0700 Message-ID: <20261030223056.2266638-8-umesh.nerlige.ramappa@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261030223056.2266638-5-umesh.nerlige.ramappa@intel.com> References: <20261030223056.2266638-5-umesh.nerlige.ramappa@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Intel-xe" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 An OA stream fd can outlive the device: after a PCI slot reset (which removes and re-probes the device) 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. Call xe_oa_quiesce() from xe_oa_fini(). That is a devm action, so it runs both on unbind and from devres_release_group() in xe_pci_error_slot_reset(), after xe_device_remove() and regardless of open stream fds. Everything the streams depend on (MMIO, irq, GuC, GT, GGTT, TTM) is set up before xe_oa_init() and hence torn down after xe_oa_fini(). For each active stream it disables the OA unit, cancels the poll hrtimer, marks the stream dead, shoots down userspace mappings of the OA buffer and releases all device-bound resources (OA buffer, exec queues, configs, references). Only the stream struct stays allocated, with stream_lock and poll_wq valid, until the fd is closed or the process owning the fd exits. Once dead, the hrtimer stops, read/mmap/ioctl return -ENODEV, poll reports EPOLLERR | EPOLLHUP, and xe_oa_release() only frees the struct and drops the drm device reference. A few details worth calling out: - Using stream_lock to serialize xe_oa_mmap against quiesce could run into a AB-BA deadlock as below. mmap path: mmap_lock -> stream_lock read path: stream_lock -> mmap_lock (if copy_to_user faults) Hence mmap is serialized against quiesce with a dedicated mmap_mutex rather than stream_lock. - drm_dev_unplug() in xe_device_remove() zaps the OA buffer mmaps before the stream is marked dead. Make the OA fault handler also return SIGBUS once the device is unplugged, otherwise the generic BO fault handler silently installs a dummy page until the stream is quiesced. - Unbind does not reset the GT or the device, so while the device is not wedged the stream is disabled with xe_oa_disable_locked() and xe_oa_stream_hw_teardown() runs before the SW resources are freed. That puts back the register whitelist, the metric set, forcewake and preemption/timeslice. If the device is wedged, as it is on a PCI slot reset, skip all HW access and only release the SW resources, since the HW may be unresponsive and a driver FLR runs on fini anyway. - drm_ioctl() only checks for unplug on entry, so a STREAM_OPEN ioctl that started before drm_dev_unplug() can still create a new live stream around xe_oa_quiesce(). Add a per-GT quiesced flag, set under gt_lock before the OA units are quiesced and checked under the same lock when opening a stream, which then fails with -ENODEV. Signed-off-by: Umesh Nerlige Ramappa --- drivers/gpu/drm/xe/xe_oa.c | 221 ++++++++++++++++++++++++++----- drivers/gpu/drm/xe/xe_oa_types.h | 9 ++ 2 files changed, 200 insertions(+), 30 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_oa.c b/drivers/gpu/drm/xe/xe_oa.c index e2198fdd2a09..513f013efda0 100644 --- a/drivers/gpu/drm/xe/xe_oa.c +++ b/drivers/gpu/drm/xe/xe_oa.c @@ -42,6 +42,7 @@ #include "xe_sriov.h" #include "xe_sync.h" #include "xe_wa.h" +#include "xe_wedge.h" #define DEFAULT_POLL_FREQUENCY_HZ 200 #define DEFAULT_POLL_PERIOD_NS (NSEC_PER_SEC / DEFAULT_POLL_FREQUENCY_HZ) @@ -259,13 +260,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; @@ -317,6 +323,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); @@ -528,12 +537,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 | \ @@ -562,6 +578,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; @@ -572,13 +591,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); } @@ -605,6 +625,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 @@ -874,33 +897,43 @@ static void xe_oa_disable_metric_set(struct xe_oa_stream *stream) xe_mmio_rmw32(mmio, OAM_COMPRESSION_T3_CONTROL, OAM_LAT_MEASURE_ENABLE, 0); } -static void xe_oa_stream_destroy(struct xe_oa_stream *stream) +static void xe_oa_stream_hw_teardown(struct xe_oa_stream *stream) { - struct xe_oa_unit *u = stream->oa_unit; - struct xe_gt *gt = stream->hwe->gt; - - if (WARN_ON(stream != u->exclusive_stream)) - return; - - WRITE_ONCE(u->exclusive_stream, NULL); - - mutex_destroy(&stream->stream_lock); - if (stream->sample) xe_reg_dewhitelist_oa_regs(stream->gt); xe_oa_disable_metric_set(stream); + xe_force_wake_put(gt_to_fw(stream->hwe->gt), stream->fw_ref); +} + +static void xe_oa_stream_free_resources(struct xe_oa_stream *stream) +{ xe_exec_queue_put(stream->k_exec_q); xe_oa_free_oa_buffer(stream); - xe_force_wake_put(gt_to_fw(gt), stream->fw_ref); xe_pm_runtime_put(stream->oa->xe); xe_oa_free_configs(stream); xe_file_put(stream->xef); } +static void xe_oa_stream_destroy(struct xe_oa_stream *stream) +{ + struct xe_oa_unit *u = stream->oa_unit; + + if (WARN_ON(stream != u->exclusive_stream)) + return; + + WRITE_ONCE(u->exclusive_stream, NULL); + + mutex_destroy(&stream->stream_lock); + mutex_destroy(&stream->mmap_mutex); + + xe_oa_stream_hw_teardown(stream); + xe_oa_stream_free_resources(stream); +} + static int xe_oa_alloc_oa_buffer(struct xe_oa_stream *stream, size_t size) { u32 vram = mert_wa_14026633728(stream) ? @@ -1661,6 +1694,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); @@ -1693,6 +1729,15 @@ static long xe_oa_ioctl(struct file *file, static void xe_oa_destroy_locked(struct xe_oa_stream *stream) { + if (stream->dead) { + mutex_destroy(&stream->stream_lock); + mutex_destroy(&stream->mmap_mutex); + kfree(stream); + return; + } + + xe_pm_runtime_get(gt_to_xe(stream->gt)); + if (stream->enabled) xe_oa_disable_locked(stream); @@ -1702,6 +1747,7 @@ static void xe_oa_destroy_locked(struct xe_oa_stream *stream) xe_exec_queue_put(stream->exec_q); drm_syncobj_put(stream->ufence_syncobj); + xe_pm_runtime_put(gt_to_xe(stream->gt)); kfree(stream); } @@ -1710,11 +1756,9 @@ 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; - xe_pm_runtime_get(gt_to_xe(gt)); mutex_lock(>->oa.gt_lock); xe_oa_destroy_locked(stream); mutex_unlock(>->oa.gt_lock); - xe_pm_runtime_put(gt_to_xe(gt)); /* Release the reference the OA stream kept on the driver */ drm_dev_put(>_to_xe(gt)->drm); @@ -1730,8 +1774,11 @@ static vm_fault_t xe_oa_buffer_fault(struct vm_fault *vmf) * Once the stream is dead its OA buffer may already be gone (or about * to be). Refuse to (re-)install a PTE rather than letting the * generic BO fault handler touch a buffer that's being torn down. + * drm_dev_unplug() zaps the mapping before the stream is marked dead, + * so also refuse once unplugged, the generic handler would install a + * dummy page. */ - if (READ_ONCE(stream->dead)) + if (READ_ONCE(stream->dead) || drm_dev_is_unplugged(&stream->oa->xe->drm)) return VM_FAULT_SIGBUS; return xe_bo_cpu_fault(vmf); @@ -1744,18 +1791,12 @@ static const struct vm_operations_struct xe_oa_vm_ops = { .access = xe_bo_vm_access, }; -static int xe_oa_mmap(struct file *file, struct vm_area_struct *vma) +static int xe_oa_mmap_locked(struct xe_oa_stream *stream, struct vm_area_struct *vma) { - struct xe_oa_stream *stream = file->private_data; - int ret = xe_observation_paranoid_check(); struct xe_bo *bo = stream->oa_buffer.bo; + int ret; - if (ret) { - drm_dbg(&stream->oa->xe->drm, "Insufficient privilege to map OA buffer\n"); - return ret; - } - - if (stream->dead) + if (READ_ONCE(stream->dead)) return -ENODEV; /* @@ -1793,6 +1834,27 @@ static int xe_oa_mmap(struct file *file, struct vm_area_struct *vma) return ret; } +static int xe_oa_mmap(struct file *file, struct vm_area_struct *vma) +{ + struct xe_oa_stream *stream = file->private_data; + int ret = xe_observation_paranoid_check(); + + if (ret) { + drm_dbg(>_to_xe(stream->gt)->drm, "Insufficient privilege to map OA buffer\n"); + return ret; + } + + /* + * Serialize against xe_oa_quiesce() freeing the OA buffer. Can't use stream_lock here, + * it is held across user copies which fault on mmap_lock, held by the caller. + */ + mutex_lock(&stream->mmap_mutex); + ret = xe_oa_mmap_locked(stream, vma); + mutex_unlock(&stream->mmap_mutex); + + return ret; +} + static const struct file_operations xe_oa_fops = { .owner = THIS_MODULE, .release = xe_oa_release, @@ -1898,6 +1960,7 @@ static int xe_oa_stream_init(struct xe_oa_stream *stream, spin_lock_init(&stream->oa_buffer.ptr_lock); mutex_init(&stream->stream_lock); + mutex_init(&stream->mmap_mutex); return 0; @@ -1924,6 +1987,12 @@ static int xe_oa_stream_open_ioctl_locked(struct xe_oa *oa, int stream_fd; int ret; + if (param->hwe->gt->oa.quiesced) { + drm_dbg(&oa->xe->drm, "OA is quiesced\n"); + ret = -ENODEV; + goto exit; + } + /* We currently only allow exclusive access */ if (param->oa_unit->exclusive_stream) { drm_dbg(&oa->xe->drm, "OA unit already in use\n"); @@ -2616,6 +2685,92 @@ static void xe_oa_unregister(void *arg) oa->metrics_kobj = NULL; } +static void xe_oa_quiesce_oa_unit(struct xe_oa_unit *u, bool hw_accessible) +{ + struct xe_oa_stream *stream = u->exclusive_stream; + unsigned long flags; + + if (!stream) + return; + + mutex_lock(&stream->stream_lock); + mutex_lock(&stream->mmap_mutex); + if (stream->enabled && hw_accessible) + xe_oa_disable_locked(stream); + + stream->enabled = false; + + 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->sample) + hrtimer_cancel(&stream->poll_check_timer); + + wake_up_all(&stream->poll_wq); + + /* Zap userspace mappings of this OA buffer only, others in the address_space stay */ + drm_vma_node_unmap(&stream->oa_buffer.bo->ttm.base.vma_node, + stream->oa->xe->drm.anon_inode->i_mapping); + + /* + * Restore the HW state while the stream still holds its runtime PM ref and exec_q. + * The device is not reset on unbind, so this can't be left to a later reset. + */ + if (hw_accessible) + xe_oa_stream_hw_teardown(stream); + + xe_oa_stream_free_resources(stream); + if (stream->exec_q) { + xe_exec_queue_put(stream->exec_q); + stream->exec_q = NULL; + } + drm_syncobj_put(stream->ufence_syncobj); + + WRITE_ONCE(u->exclusive_stream, NULL); + mutex_unlock(&stream->mmap_mutex); + mutex_unlock(&stream->stream_lock); +} + +static void xe_oa_quiesce_gt(struct xe_gt *gt, bool hw_accessible) +{ + int i; + + mutex_lock(>->oa.gt_lock); + /* Set before touching the units so a racing STREAM_OPEN can't slip in a new stream */ + gt->oa.quiesced = true; + for (i = 0; i < gt->oa.num_oa_units; i++) + xe_oa_quiesce_oa_unit(>->oa.oa_unit[i], hw_accessible); + mutex_unlock(>->oa.gt_lock); +} + +/** + * xe_oa_quiesce - Disable all active OA streams on driver unbind or PCI slot reset + * @xe: @xe_device + * + * Disables the OA unit and stops the poll timer of every active stream, marks it dead + * and releases all device-bound resources (OA buffer and its userspace mappings, exec + * queues, configs, references). Unless the device is wedged, the HW state (whitelist, + * metric set, forcewake, preemption) is restored first. Any further read, poll, mmap or + * ioctl on the stream fd is rejected (-ENODEV). Only the stream struct stays allocated + * until userspace closes the fd, at which point xe_oa_release() just frees it. + */ +static void xe_oa_quiesce(struct xe_device *xe) +{ + struct xe_gt *gt; + u8 id; + + if (!xe->oa.xe) + return; + + /* + * A wedged device may be unresponsive and a driver FLR follows on fini, so skip HW + * access there and only release the SW resources. + */ + for_each_gt(gt, xe, id) + xe_oa_quiesce_gt(gt, !xe_wedged(xe)); +} + /** * xe_oa_register - Xe OA registration * @xe: @xe_device @@ -2927,6 +3082,12 @@ static void xe_oa_fini(void *arg) if (!oa->xe) return; + /* + * Devm action, so this runs on unbind and PCI slot reset after xe_device_remove() even + * if streams are still open. Quiesce them before the configs they hold are destroyed. + */ + 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 e60a90bae1c6..f17e12079b9d 100644 --- a/drivers/gpu/drm/xe/xe_oa_types.h +++ b/drivers/gpu/drm/xe/xe_oa_types.h @@ -121,6 +121,9 @@ struct xe_oa_gt { /** @gt_lock: lock protecting create/destroy OA streams */ struct mutex gt_lock; + /** @quiesced: OA is being torn down, no new streams allowed. Protected by @gt_lock */ + bool quiesced; + /** @num_oa_units: number of oa units for each gt */ u32 num_oa_units; @@ -202,6 +205,12 @@ struct xe_oa_stream { /** @stream_lock: Lock serializing stream operations */ struct mutex stream_lock; + /** + * @mmap_mutex: Serializes mmap against quiesce freeing the OA buffer. Leaf lock, taken + * under mmap_lock in mmap, so never take stream_lock or fault on user memory under it + */ + struct mutex mmap_mutex; + /** @sample: true if DRM_XE_OA_PROP_SAMPLE_OA is provided */ bool sample; -- 2.55.0