Skip to contentSkip to conversation

drm/amdgpu: WAIT_EVENT notifications and lifetime updates

PATCH New Jesse Zhang 1 participant 12 messages Dec 1

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover drm/amdgpu: WAIT_EVENT notifications and lifetime updates

AMDGPU currently provides EVENTFD notifications and WAIT_EVENT metadata retrieval for render-node clients.

Previous work introduced the EVENTFD infrastructure, EVENTFD event producers, and WAIT_EVENT metadata retrieval support.

References
==========

EVENTFD infrastructure:
https://patchwork.freedesktop.org/series/164618/

GPU event producers for EVENTFD notifications:
https://patchwork.freedesktop.org/series/170501/

WAIT_EVENT metadata retrieval and consumption:
https://patchwork.freedesktop.org/series/164813/

Queue-scoped USERQ_EOP and QUEUE_RESET WAIT_EVENT notifications are already integrated into the WAIT_EVENT framework.

This series extends WAIT_EVENT support for additional event types and addresses several lifetime, teardown, and robustness issues discovered during development and testing.

What this series adds
=====================

This series adds:

- Documentation for the EVENTFD and WAIT_EVENT interfaces.
- GPU_RESET WAIT_EVENT notifications.
- MEMORY_EXCEPTION EVENTFD notifications.
- MEMORY_EXCEPTION WAIT_EVENT notifications, with the faulting address and the fault status in the record.
- Support for process-scoped WAIT_EVENT records.

What this series fixes
======================

This series also improves behavior during queue teardown, device unplug, PASID ownership cleanup, and DRM file shutdown.

Specifically it:

- Stops new notifications once queue cleanup begins.
- Cancels WAIT_EVENT waits when a queue is removed.
- Prevents PASID lookups from reaching event managers being destroyed.
- Wakes blocked WAIT_EVENT callers during device unplug.
- Ensures USERQ destructors complete before manager teardown.
- Prevents unbounded WAIT_EVENT record growth and reports event loss. Records are bounded per event type and a loss is reported for the source of the dropped event, so the events of one type which nobody consumes cannot cost a DRM file the events of another type.

Patch overview
==============

01. drm/amdgpu: Document render-node EVENTFD subscriptions

Document EVENTFD subscriptions, event types, usage model, lifetime handling, and userspace examples.

02. drm/amdgpu: Stop queue notifications before event cleanup

Prevent producers from creating new notifications after queue cleanup begins.

03. drm/amdgpu: Cancel WAIT_EVENT waits when a queue is removed

Wake queue waiters and return -ENOENT when the target queue disappears.

04. drm/amdgpu: Support process-scoped WAIT_EVENT records

Allow WAIT_EVENT records that are not associated with a USERQ.

05. drm/amdgpu: Record GPU_RESET WAIT_EVENT notifications

Add WAIT_EVENT metadata delivery for GPU reset events.

06. drm/amdgpu: Signal MEMORY_EXCEPTION EVENTFD notifications

Add EVENTFD notification delivery for memory exception events.

07. drm/amdgpu: Record MEMORY_EXCEPTION WAIT_EVENT notifications

Add WAIT_EVENT metadata delivery for memory exception events, including the faulting address and the fault status.

08. drm/amdgpu: Clear PASID ownership before event manager teardown

Prevent new PASID-based event lookups from reaching a closing file.

09. drm/amdgpu: Wake WAIT_EVENT callers on device unplug

Wake blocked waiters and return -ENODEV after unplug.

10. drm/amdgpu: Wait for USERQ destructors on close

Ensure queue destruction completes before manager teardown.

11. drm/amdgpu: Bound WAIT_EVENT records and report event loss

Bound pending records per event type and report metadata loss per event source when records cannot be stored.

Changes in v2
=============

- Patch 07: report the faulting address and the fault status in the MEMORY_EXCEPTION record; they were always returned as 0. (Jesse)

- Patch 11: bound the pending records per event type and report a loss per event source (the queue for a queue-scoped event, the DRM file for a device-scoped one) instead of per DRM file, so unconsumed USERQ_EOP records no longer drop the records of other event types. The queues of a DRM file still share the limit of a queue-scoped event type. (Jesse)

- No changes in the other patches.

Testing
=======

IGT amd_eventfd_wait_event on Navi48 (GFX 12.0) with compute, GFX and SDMA user queues: event producers (USERQ_EOP, QUEUE_RESET, GPU_RESET, MEMORY_EXCEPTION), sequence numbers, record limit and loss reporting, queue and DRM file teardown with subscriptions, pending records and blocked waiters, and stress runs up to 100000 events.

Base
====

This series is based on:

amd-unified-interface

Thanks,

Srinivasan Shanmugam (11):
drm/amdgpu: Document render-node EVENTFD subscriptions drm/amdgpu: Stop queue notifications before event cleanup drm/amdgpu: Cancel WAIT_EVENT waits when a queue is removed drm/amdgpu: Support process-scoped WAIT_EVENT records drm/amdgpu: Record GPU_RESET WAIT_EVENT notifications drm/amdgpu: Signal MEMORY_EXCEPTION EVENTFD notifications drm/amdgpu: Record MEMORY_EXCEPTION WAIT_EVENT notifications drm/amdgpu: Clear PASID ownership before event manager teardown drm/amdgpu: Wake WAIT_EVENT callers on device unplug drm/amdgpu: Wait for USERQ destructors on close drm/amdgpu: Bound WAIT_EVENT records and report event loss

 Documentation/gpu/amdgpu/eventfd.rst          | 134 ++++++++++++
 Documentation/gpu/amdgpu/index.rst            |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c    |  15 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c       |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c   |  12 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c       |   8 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h       |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c       |   3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     |  67 +++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  21 +-
 .../drm/amd/amdgpu/amdgpu_userq_internal.h    |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        |   6 +
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.c    | 207 +++++++++++++++---
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.h    |  20 +-
 include/uapi/drm/amdgpu_drm.h                 |  48 +++-
 15 files changed, 474 insertions(+), 73 deletions(-)
 create mode 100644 Documentation/gpu/amdgpu/eventfd.rst
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/11 drm/amdgpu: Document render-node EVENTFD subscriptions

From: Srinivasan Shanmugam <[email protected]>

Add documentation describing the render-node EVENTFD interface.

Document the supported event types, subscription model, queue-scoped versus device-scoped events, subscription lifetime and cleanup, and example userspace usage. Also add the document to the AMDGPU documentation index.

The documentation matches the current EVENTFD UAPI and is intended to serve as a reference for userspace applications using the interface.

---
 Documentation/gpu/amdgpu/eventfd.rst | 134 +++++++++++++++++++++++++++
 Documentation/gpu/amdgpu/index.rst   |   1 +
 include/uapi/drm/amdgpu_drm.h        |  24 ++---
 3 files changed, 148 insertions(+), 11 deletions(-)
 create mode 100644 Documentation/gpu/amdgpu/eventfd.rst
Documentation/gpu/amdgpu/eventfd.rst new file +134 -0
new file mode 100644
index 000000000000..1ad34abf1322
@@ -0,0 +1,134 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==============================
+AMDGPU render-node EVENTFD API
+==============================
+
+Overview
+========
+
+The AMDGPU EVENTFD ioctl lets a render-node client subscribe an eventfd to
+driver-defined GPU events.
+
+EVENTFD is a notification interface. It wakes userspace when a matching event
+occurs, but it does not carry event metadata. Event-specific metadata, when
+available through a separate interface, is not returned through the eventfd
+counter.
+
+The ioctl is available on an AMDGPU render-node file descriptor and uses
+``struct drm_amdgpu_eventfd``.
+
+Creating a subscription
+=======================
+
+Userspace first creates an eventfd with ``eventfd(2)``. It then fills
+``struct drm_amdgpu_eventfd`` as follows:
+
+``op``
+  Set to ``DRM_AMDGPU_EVENTFD_OP_BIND``.
+
+``event_type``
+  Set to one of the values in ``enum drm_amdgpu_event_type``.
+
+``eventfd``
+  Set to the file descriptor returned by ``eventfd(2)``. File descriptor zero
+  is valid.
+
+``queue_id``
+  For queue-scoped events, set this to the user queue handle returned by the
+  AMDGPU USERQ create ioctl. For device-scoped events, set this to zero.
+
+``flags``
+  Must be zero.
+
+Userspace then calls ``DRM_IOCTL_AMDGPU_EVENTFD`` on the AMDGPU render-node
+file descriptor.
+
+The driver resolves ``queue_id`` to the corresponding queue object during the
+ioctl.
+
+Repeating the same bind for the same event type, queue, and eventfd is
+idempotent.
+
+Queue-scoped events
+===================
+
+Queue-scoped events are associated with a user queue and require a non-zero
+``queue_id`` belonging to the same render-node file.
+
+Examples of queue-scoped events include
+``DRM_AMDGPU_EVENT_TYPE_USERQ_EOP`` and
+``DRM_AMDGPU_EVENT_TYPE_QUEUE_RESET``.
+
+The event-type definitions indicate which events are queue-scoped.
+
+Device-scoped events
+====================
+
+Device-scoped events are not associated with a user queue and require
+``queue_id`` to be zero.
+
+Examples of device-scoped events include
+``DRM_AMDGPU_EVENT_TYPE_MEMORY_EXCEPTION`` and
+``DRM_AMDGPU_EVENT_TYPE_GPU_RESET``.
+
+The event-type definitions indicate which events are device-scoped. Event
+producers may be added independently of the subscription infrastructure, so
+the presence of an event type in the UAPI does not by itself guarantee that
+every GPU generation produces that event.
+
+Receiving a notification
+========================
+
+When a matching event occurs, the driver signals the subscribed eventfd.
+
+Userspace reads or polls the eventfd using the normal eventfd API.
+
+The value read from the eventfd is the eventfd counter. It does not identify
+the event type or contain an AMDGPU-specific payload. Applications should
+maintain their own mapping between eventfd descriptors and subscriptions.
+
+Removing a subscription
+=======================
+
+To remove a subscription, userspace submits the same event type, queue ID, and
+eventfd with ``op`` set to ``DRM_AMDGPU_EVENTFD_OP_UNBIND``.
+
+The unbind operation removes only the matching association.
+
+Lifetime and cleanup
+====================
+
+A queue-scoped subscription retains a reference to its queue. The driver
+removes all subscriptions associated with a queue when that queue is removed
+from the USERQ manager.
+
+The driver also removes all remaining subscriptions when the render-node file
+is closed.
+
+Usage example
+=============
+
+The following example omits error handling for clarity::
+
+    int efd;
+    struct drm_amdgpu_eventfd args = {
+        .op = DRM_AMDGPU_EVENTFD_OP_BIND,
+        .event_type = DRM_AMDGPU_EVENT_TYPE_USERQ_EOP,
+        .queue_id = queue_id,
+        .flags = 0,
+    };
+
+    efd = eventfd(0, EFD_CLOEXEC);
+    args.eventfd = efd;
+
+    ioctl(render_fd, DRM_IOCTL_AMDGPU_EVENTFD, &args);
+
+    /*
+     * poll() or read() efd. The returned value is the eventfd counter,
+     * not an AMDGPU event record.
+     */
+
+    args.op = DRM_AMDGPU_EVENTFD_OP_UNBIND;
+    ioctl(render_fd, DRM_IOCTL_AMDGPU_EVENTFD, &args);
+    close(efd);
Documentation/gpu/amdgpu/index.rst +1 -0
index ba2ee7327867..41ab045dfa5f 100644
@@ -25,3 +25,4 @@ Next (GCN), Radeon DNA (RDNA), and Compute DNA (CDNA) architectures.
    amdgpu-glossary
    ualink
    ptl
+   eventfd
include/uapi/drm/amdgpu_drm.h +13 -11
index 8a546e0ae0a1..543f698b83cf 100644
@@ -223,6 +223,13 @@ union drm_amdgpu_gem_create {
 	struct drm_amdgpu_gem_create_out	out;
 };
 
+/**
+ * enum drm_amdgpu_event_type - AMDGPU render-node event types
+ * @DRM_AMDGPU_EVENT_TYPE_USERQ_EOP: user queue reached an end-of-pipe event
+ * @DRM_AMDGPU_EVENT_TYPE_QUEUE_RESET: user queue was reset
+ * @DRM_AMDGPU_EVENT_TYPE_MEMORY_EXCEPTION: GPU memory exception
+ * @DRM_AMDGPU_EVENT_TYPE_GPU_RESET: device-wide GPU reset
+ */
 enum drm_amdgpu_event_type {
 	DRM_AMDGPU_EVENT_TYPE_USERQ_EOP = 1,
 	DRM_AMDGPU_EVENT_TYPE_QUEUE_RESET = 2,
@@ -230,6 +237,11 @@ enum drm_amdgpu_event_type {
 	DRM_AMDGPU_EVENT_TYPE_GPU_RESET = 4,
 };
 
+/**
+ * enum drm_amdgpu_eventfd_op - EVENTFD subscription operations
+ * @DRM_AMDGPU_EVENTFD_OP_BIND: create an EVENTFD subscription
+ * @DRM_AMDGPU_EVENTFD_OP_UNBIND: remove an EVENTFD subscription
+ */
 enum drm_amdgpu_eventfd_op {
 	DRM_AMDGPU_EVENTFD_OP_BIND = 0,
 	DRM_AMDGPU_EVENTFD_OP_UNBIND = 1,
@@ -241,18 +253,8 @@ enum drm_amdgpu_eventfd_op {
  * @event_type: kernel-defined AMDGPU event type
  * @eventfd: eventfd file descriptor
  * @queue_id: queue identifier for queue-scoped subscriptions, or 0 for
- *	      device/GPU-scoped subscriptions
+ *	      device-scoped subscriptions
  * @flags: must be 0
- *
- * This ioctl lets userspace register or unregister eventfd notifications
- * for a render-node event.
- *
- * Eventfd signaling is notification-only.
- *
- * USERQ_EOP and QUEUE_RESET are queue-scoped events.
- * Userspace specifies @queue_id when registering these subscriptions.
- *
- * MEMORY_EXCEPTION is currently GPU-scoped and requires @queue_id = 0.
  */
 struct drm_amdgpu_eventfd {
 	__u32 op;
Signature · 1 line
2.50.1
#2 lore raw

PATCH v2 2/11 drm/amdgpu: Stop queue notifications before event cleanup

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

Queue interrupts can publish WAIT_EVENT records after cleanup has removed existing records, potentially acquiring a queue reference after its reference count has reached zero.

Close queue notifications under the existing doorbell and EVENTFD locks before removing subscriptions and pending records. Check this state in the EOP and queue-reset producers while preserving fence completion.

Use the common cleanup helper for queue deletion, file teardown and creation failures after doorbell publication.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c   | 12 +++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 50 +++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  6 +++
 .../drm/amd/amdgpu/amdgpu_userq_internal.h    |  2 +
 4 files changed, 56 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eventfd.c +9 -3
index 4288ee6b5409..9a60b5a73fe3 100644
@@ -272,10 +272,15 @@ int amdgpu_eventfd_bind(struct amdgpu_eventfd_mgr *mgr,
 	e->queue = queue;
 
 	/*
-	 * Re-check duplicate under lock to close the race with another bind()
-	 * for the same (event_type, queue, ctx).
+	 * Serialize subscription insertion with queue cleanup and competing
+	 * binds for the same (event_type, queue, ctx).
 	 */
 	xa_lock_irqsave(&mgr->ids, flags);
+	if (queue && amdgpu_userq_events_closed(queue)) {
+		xa_unlock_irqrestore(&mgr->ids, flags);
+		r = -ENOENT;
+		goto out_free_entry;
+	}
 	hlist_for_each_entry(it, &id->entries, hnode) {
 		if (it->ctx == ctx && it->queue == queue) {
 			dup = true;
@@ -385,7 +390,8 @@ int amdgpu_eventfd_unbind(struct amdgpu_eventfd_mgr *mgr,
  * @mgr: eventfd manager
  * @queue: queue being destroyed/released
  *
- * Remove all subscriptions that hold a reference to @queue.
+ * Remove all subscriptions referencing @queue. The caller must first
+ * stop new binds for this queue.
  * This is called when the queue id is released so EVENTFD can drop
  * its queue references before the queue is finally destroyed.
  */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +39 -11

105 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -0
index 3cd0c97698a8..f2788f1eb2f1 100644
@@ -101,6 +101,12 @@ struct amdgpu_usermode_queue {
 	struct delayed_work	hang_detect_work;
 	struct kref		refcount;
 
+	/*
+	 * Readers hold userq_doorbell_xa or the EVENTFD manager's ids lock.
+	 * Writers hold both, in that order.
+	 */
+	bool			events_closed;
+
 	union {
 		struct {
 			u64 queue_rb;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_internal.h +2 -0
index 4279b1470b3c..5d7634bd745f 100644
@@ -35,4 +35,6 @@ amdgpu_userq_get(struct amdgpu_userq_mgr *userq_mgr, u32 queue_id);
 
 void amdgpu_userq_put(struct amdgpu_usermode_queue *queue);
 
+bool amdgpu_userq_events_closed(struct amdgpu_usermode_queue *queue);
+
 #endif /* __AMDGPU_USERQ_INTERNAL_H__ */
Signature · 1 line
2.50.1
#3 lore raw

PATCH v2 3/11 drm/amdgpu: Cancel WAIT_EVENT waits when a queue is removed

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

Queue removal wakes WAIT_EVENT callers, but their wait condition does not observe the closed notification state. They can sleep again and retain their queue reference until timeout.

Check events_closed in the wait condition and return -ENOENT when no matching record is available. Use READ_ONCE and WRITE_ONCE for the cancellation flag access and document the removal behaviour.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c      |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h      |  5 +++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.c | 15 +++++++++++----
 3 files changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
index f60a9b437cff..310f254d23ce 100644
@@ -95,7 +95,7 @@ static void amdgpu_userq_stop_events(struct amdgpu_usermode_queue *queue)
 	/* Match the IRQ producer's lock order. */
 	xa_lock_irqsave(xa, flags);
 	xa_lock(&eventfd_mgr->ids);
-	queue->events_closed = true;
+	WRITE_ONCE(queue->events_closed, true);
 	xa_unlock(&eventfd_mgr->ids);
 	xa_unlock_irqrestore(xa, flags);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -2
index f2788f1eb2f1..6fb350a83328 100644
@@ -102,8 +102,9 @@ struct amdgpu_usermode_queue {
 	struct kref		refcount;
 
 	/*
-	 * Readers hold userq_doorbell_xa or the EVENTFD manager's ids lock.
-	 * Writers hold both, in that order.
+	 * Bind/IRQ readers hold userq_doorbell_xa or the EVENTFD ids lock.
+	 * Writers hold both, in that order, and use WRITE_ONCE.
+	 * WAIT_EVENT readers hold a queue reference and use READ_ONCE.
 	 */
 	bool			events_closed;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.c +11 -4
index c21a10c4c5c1..5558446128f7 100644
@@ -447,6 +447,11 @@ int amdgpu_wait_event_drm_ioctl(struct drm_device *dev, void *data,
 			goto out_put_queue;
 		}
 
+		if (queue && READ_ONCE(queue->events_closed)) {
+			ret = -ENOENT;
+			goto out_put_queue;
+		}
+
 		if (signal_pending(current)) {
 			ret = -ERESTARTSYS;
 			goto out_put_queue;
@@ -458,10 +463,12 @@ int amdgpu_wait_event_drm_ioctl(struct drm_device *dev, void *data,
 			goto out_put_queue;
 		}
 
-		remaining = wait_event_interruptible_timeout(mgr->wq,
-							     READ_ONCE(mgr->dead) ||
-				amdgpu_wait_event_has_match(mgr, args, queue),
-				remaining);
+		remaining = wait_event_interruptible_timeout(
+			mgr->wq,
+			READ_ONCE(mgr->dead) ||
+			(queue && READ_ONCE(queue->events_closed)) ||
+			amdgpu_wait_event_has_match(mgr, args, queue),
+			remaining);
 		if (remaining < 0) {
 			ret = remaining;
 			goto out_put_queue;
Signature · 1 line
2.50.1
#4 lore raw

PATCH v2 4/11 drm/amdgpu: Support process-scoped WAIT_EVENT records

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

WAIT_EVENT currently assumes that every event is associated with a userspace queue. This is true for USERQ_EOP, QUEUE_RESET and SCRATCH, but not for process-scoped events such as MEMORY_EXCEPTION.

Allow amdgpu_wait_event_add() to accept a NULL queue. Take and release a queue reference only when the event carries a queue object.

This prepares the WAIT_EVENT manager to record process-scoped events without manufacturing a queue association.

---
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.c    | 22 +++++++++++--------
 1 file changed, 13 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.c +13 -9
index 5558446128f7..ca9ae06e1d3c 100644
@@ -109,7 +109,8 @@ amdgpu_wait_event_set_queue_id(struct amdgpu_wait_event_record *rec,
  * amdgpu_wait_event_record_free - release a pending event record
  * @rec: record to release
  *
- * Drop the queue reference owned by the record and free the record.
+ * Drop the queue reference owned by a queue-scoped record and free the
+ * record.
  */
 static void
 amdgpu_wait_event_record_free(struct amdgpu_wait_event_record *rec)
@@ -117,7 +118,9 @@ amdgpu_wait_event_record_free(struct amdgpu_wait_event_record *rec)
 	if (!rec)
 		return;
 
-	amdgpu_userq_put(rec->queue);
+	if (rec->queue)
+		amdgpu_userq_put(rec->queue);
+
 	kfree(rec);
 }
 
@@ -277,17 +280,17 @@ void amdgpu_wait_event_mgr_init(struct amdgpu_wait_event_mgr *mgr)
 }
 
 /**
- * amdgpu_wait_event_add - append a queue-scoped WAIT_EVENT record
+ * amdgpu_wait_event_add - append a WAIT_EVENT record
  * @mgr: per-file WAIT_EVENT manager
  * @event_type: kernel-defined AMDGPU event type
- * @queue: queue which generated the event
+ * @queue: queue which generated a queue-scoped event, or NULL
  *
  * This helper can be called from interrupt context. Allocate the record
- * with GFP_ATOMIC and take a queue reference before publishing it on the
- * manager's pending list.
+ * with GFP_ATOMIC and take a queue reference for queue-scoped events before
+ * publishing it on the manager's pending list.
  *
- * The queue pointer is retained as the internal routing key. queue_id is
- * copied from the producing queue only as metadata returned to userspace.
+ * Queue-scoped records retain the queue pointer as the internal routing
+ * key. Process-scoped records store a NULL queue.
  */
 void amdgpu_wait_event_add(struct amdgpu_wait_event_mgr *mgr,
 			   u32 event_type,
@@ -300,7 +303,8 @@ void amdgpu_wait_event_add(struct amdgpu_wait_event_mgr *mgr,
 	if (!rec)
 		return;
 
-	kref_get(&queue->refcount);
+	if (queue)
+		kref_get(&queue->refcount);
 	rec->queue = queue;
 	rec->data.event_type = event_type;
 	rec->data.seqno = atomic64_inc_return(&mgr->seqno);
Signature · 1 line
2.50.1
#5 lore raw

PATCH v2 5/11 drm/amdgpu: Record GPU_RESET WAIT_EVENT notifications

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

GPU_RESET currently signals EVENTFD subscribers without adding a corresponding WAIT_EVENT record.

Record a device-scoped event for each open DRM file before signalling its EVENTFD subscriptions. Use a NULL queue and retain the existing reset notification points and file-list locking.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +9 -5
index 6a4089b43dfe..8ec3db980812 100644
@@ -48,6 +48,7 @@
 #include <linux/vga_switcheroo.h>
 #include <linux/efi.h>
 #include "amdgpu.h"
+#include "amdgpu_wait_event.h"
 #include "amdgpu_trace.h"
 #include "amdgpu_i2c.h"
 #include "atom.h"
@@ -5667,11 +5668,10 @@ static void amdgpu_device_halt_activities(struct amdgpu_device *adev,
 }
 
 /*
- * Signal GPU_RESET EVENTFD subscriptions for all open DRM files.
+ * Record GPU_RESET for all open DRM files and signal EVENTFD subscribers.
  *
- * GPU reset is a device-wide event rather than being associated with a
- * specific user queue. Notify every process that subscribed to the
- * GPU_RESET event on this device.
+ * GPU reset is device-wide, so the WAIT_EVENT record has no queue
+ * association. Record the event before notifying subscribers.
  */
 static void amdgpu_device_eventfd_signal_gpu_reset(struct amdgpu_device *adev)
 {
@@ -5683,10 +5683,14 @@ static void amdgpu_device_eventfd_signal_gpu_reset(struct amdgpu_device *adev)
 	list_for_each_entry(file, &ddev->filelist, lhead) {
 		struct amdgpu_fpriv *fpriv = file->driver_priv;
 
-		if (fpriv)
+		if (fpriv) {
+			amdgpu_wait_event_add(&fpriv->wait_event_mgr,
+					      DRM_AMDGPU_EVENT_TYPE_GPU_RESET,
+					      NULL);
 			amdgpu_eventfd_signal(&fpriv->eventfd_mgr,
 					      DRM_AMDGPU_EVENT_TYPE_GPU_RESET,
 					      NULL);
+		}
 	}
 
 	mutex_unlock(&ddev->filelist_mutex);
Signature · 1 line
2.50.1
#6 lore raw

PATCH v2 6/11 drm/amdgpu: Signal MEMORY_EXCEPTION EVENTFD notifications

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

Signal MEMORY_EXCEPTION EVENTFD subscriptions when a GPUVM fault is recorded for a DRM PASID.

The PASID ownership mapping now resolves the DRM file-private object directly, allowing the fault path to notify only the process which owns the faulting address space.

Perform the notification from amdgpu_vm_update_fault_cache(), as part of the existing PASID-locked fault handling. This avoids taking sleeping locks or walking the DRM file list from interrupt context.

MEMORY_EXCEPTION is process-scoped and does not use a queue object.

EVENTFD remains notification-only. Fault details continue to be available through the corresponding WAIT_EVENT and fault-query paths.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
index 96603f6a80c0..27a6a2c3540f 100644
@@ -3420,6 +3420,9 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 		} else {
 			WARN_ONCE(1, "Invalid vmhub %u\n", vmhub);
 		}
+
+		amdgpu_eventfd_signal(&fpriv->eventfd_mgr,
+				      DRM_AMDGPU_EVENT_TYPE_MEMORY_EXCEPTION, NULL);
 	}
 	amdgpu_pasid_unlock(flags);
 }
Signature · 1 line
2.50.1
#7 lore raw

PATCH v2 7/11 drm/amdgpu: Record MEMORY_EXCEPTION WAIT_EVENT notifications

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

Record a MEMORY_EXCEPTION WAIT_EVENT notification when a valid GPUVM fault is cached for a DRM PASID.

The PASID ownership mapping resolves the amdgpu_fpriv associated with the faulting address space. Use its per-file WAIT_EVENT manager so that the notification is delivered only to the process which owns the faulting PASID.

Keep the notification in amdgpu_vm_update_fault_cache(), alongside the fault-cache update and MEMORY_EXCEPTION EVENTFD notification.

MEMORY_EXCEPTION is process-scoped, so no userspace queue is associated with the record. The record carries the faulting address and the protection fault status of the VM hub in struct drm_amdgpu_wait_event_memory.

v2: Report the faulting address and the fault status in the record,
they were always returned as 0. (Jesse)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        |  3 ++
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.c    | 50 +++++++++++++++++--
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.h    |  3 ++
 3 files changed, 52 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -0
index 27a6a2c3540f..34b72f08cd06 100644
@@ -40,6 +40,7 @@
 #include "amdgpu_vm.h"
 #include "amdgpu_vm_internal.h"
 #include "amdgpu_trace.h"
+#include "amdgpu_wait_event.h"
 #include "amdgpu_amdkfd.h"
 #include "amdgpu_gmc.h"
 #include "amdgpu_xgmi.h"
@@ -3421,6 +3422,8 @@ void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
 			WARN_ONCE(1, "Invalid vmhub %u\n", vmhub);
 		}
 
+		amdgpu_wait_event_add_memory_exception(&fpriv->wait_event_mgr,
+						       addr, status);
 		amdgpu_eventfd_signal(&fpriv->eventfd_mgr,
 				      DRM_AMDGPU_EVENT_TYPE_MEMORY_EXCEPTION, NULL);
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.c +46 -4

77 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.h +3 -0
index 83d9a59a4a45..75cf1c616a06 100644
@@ -76,6 +76,9 @@ void amdgpu_wait_event_add(struct amdgpu_wait_event_mgr *mgr,
 			   u32 event_type,
 			   struct amdgpu_usermode_queue *queue);
 
+void amdgpu_wait_event_add_memory_exception(struct amdgpu_wait_event_mgr *mgr,
+					    u64 va, u32 fault_status);
+
 int amdgpu_wait_event_drm_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *file_priv);
 
Signature · 1 line
2.50.1
#8 lore raw

PATCH v2 8/11 drm/amdgpu: Clear PASID ownership before event manager teardown

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

Memory-fault notifications resolve fpriv through the PASID mapping. Postclose currently destroys the event managers before the delayed PASID free path clears that mapping.

Expose the existing owner-clear helper and call it before manager teardown. Its PASID lock synchronizes with active fault handlers and prevents subsequent lookups from reaching the closing file.

Preserve delayed PASID release and its fence-based reuse protection.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 8 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h | 1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 3 +++
 3 files changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4 -4
index 9c879ac68352..f35d47e1ea20 100644
@@ -112,11 +112,11 @@ static void amdgpu_pasid_free_cb(struct dma_fence *fence,
  *
  * Replace the stored owner with NULL while keeping the PASID allocated.
  *
- * This is used by the delayed PASID free path so that future PASID
- * lookups cannot resolve a stale DRM file-private object while the PASID
- * is still waiting for outstanding fences before being released.
+ * Serialize with PASID-locked users and prevent new lookups from
+ * finding the owner. Call before tearing down per-file event managers.
+ * The PASID remains allocated until the normal free path releases it.
  */
-static void amdgpu_pasid_clear_owner(u32 pasid)
+void amdgpu_pasid_clear_owner(u32 pasid)
 {
 	unsigned long flags;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.h +1 -0
index c134000eecfd..af9947e7e0e2 100644
@@ -117,6 +117,7 @@ struct amdgpu_fpriv *amdgpu_pasid_get_fpriv_locked(u32 pasid);
 int amdgpu_pasid_drain_irq(struct amdgpu_device *adev, u32 pasid,
 			   u32 *ih_fence, unsigned long timeout);
 void amdgpu_pasid_drain_irq_done(u32 pasid);
+void amdgpu_pasid_clear_owner(u32 pasid);
 void amdgpu_pasid_free(u32 pasid);
 void amdgpu_pasid_free_delayed(struct dma_resv *resv,
 			       u32 pasid);
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +3 -0
index 42fe741ef9ca..45061d5a8e0c 100644
@@ -1826,6 +1826,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	if (!fpriv)
 		return;
 
+	/* Stop PASID-based notifications before tearing down the managers. */
+	amdgpu_pasid_clear_owner(fpriv->vm.pasid);
+
 	/* Drop eventfd subscriptions and pending wait-event records. */
 	amdgpu_eventfd_mgr_fini(&fpriv->eventfd_mgr);
 	amdgpu_wait_event_mgr_fini(&fpriv->wait_event_mgr);
Signature · 1 line
2.50.1
#9 lore raw

PATCH v2 9/11 drm/amdgpu: Wake WAIT_EVENT callers on device unplug

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

A thread already waiting in WAIT_EVENT can remain asleep when the DRM device is unplugged. Wake each per-file waitqueue after unplug and return -ENODEV from the ioctl wait loop.

Document queue removal and device unplug cancellation semantics.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c    |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c       |  2 ++
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.c    | 28 +++++++++++++++++++
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.h    |  1 +
 include/uapi/drm/amdgpu_drm.h                 |  7 +++++
 5 files changed, 39 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index 8ec3db980812..b3509a6b834c 100644
@@ -6742,6 +6742,7 @@ void amdgpu_device_halt(struct amdgpu_device *adev)
 
 	amdgpu_xcp_dev_unplug(adev);
 	drm_dev_unplug(ddev);
+	amdgpu_wait_event_wake_all(ddev);
 
 	amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
 	amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index a6ee6bf1c225..c31b20008cad 100644
@@ -54,6 +54,7 @@
 #include "amdgpu_userq.h"
 #include "amdgpu_trap.h"
 #include "amdgpu_userq_fence.h"
+#include "amdgpu_wait_event.h"
 #include "../amdxcp/amdgpu_xcp_drv.h"
 #include "amdgpu_ualink.h"
 #include "amdgpu_profiler.h"
@@ -2574,6 +2575,7 @@ amdgpu_pci_remove(struct pci_dev *pdev)
 	amdgpu_xcp_dev_unplug(adev);
 	amdgpu_gmc_prepare_nps_mode_change(adev);
 	drm_dev_unplug(dev);
+	amdgpu_wait_event_wake_all(dev);
 
 	if (adev->pm.rpm_mode != AMDGPU_RUNPM_NONE) {
 		pm_runtime_get_sync(dev->dev);
drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.c +28 -0
index 3ae131ce6b8c..5135bdd90880 100644
@@ -27,6 +27,7 @@
 #include <linux/slab.h>
 #include <linux/timekeeping.h>
 #include <linux/uaccess.h>
+#include <drm/drm_drv.h>
 
 #include "amdgpu.h"
 #include "amdgpu_userq_internal.h"
@@ -435,6 +436,27 @@ void amdgpu_wait_event_remove_queue(struct amdgpu_wait_event_mgr *mgr,
 	wake_up_interruptible_all(&mgr->wq);
 }
 
+/**
+ * amdgpu_wait_event_wake_all - wake WAIT_EVENT callers after device unplug
+ * @dev: unplugged DRM device
+ *
+ * Call after drm_dev_unplug() publishes the terminal state checked by waiters.
+ * The file-list mutex keeps each fpriv alive while its waitqueue is accessed.
+ */
+void amdgpu_wait_event_wake_all(struct drm_device *dev)
+{
+	struct drm_file *file;
+
+	mutex_lock(&dev->filelist_mutex);
+	list_for_each_entry(file, &dev->filelist, lhead) {
+		struct amdgpu_fpriv *fpriv = file->driver_priv;
+
+		if (fpriv)
+			wake_up_interruptible_all(&fpriv->wait_event_mgr.wq);
+	}
+	mutex_unlock(&dev->filelist_mutex);
+}
+
 /**
  * amdgpu_wait_event_drm_ioctl - wait for an AMDGPU render-node event
  * @dev: DRM device
@@ -484,6 +506,11 @@ int amdgpu_wait_event_drm_ioctl(struct drm_device *dev, void *data,
 	mgr = &fpriv->wait_event_mgr;
 
 	for (;;) {
+		if (drm_dev_is_unplugged(dev)) {
+			ret = -ENODEV;
+			goto out_put_queue;
+		}
+
 		rec = amdgpu_wait_event_pop_match(mgr, args, queue);
 		if (rec)
 			break;
@@ -511,6 +538,7 @@ int amdgpu_wait_event_drm_ioctl(struct drm_device *dev, void *data,
 
 		remaining = wait_event_interruptible_timeout(
 			mgr->wq,
+			drm_dev_is_unplugged(dev) ||
 			READ_ONCE(mgr->dead) ||
 			(queue && READ_ONCE(queue->events_closed)) ||
 			amdgpu_wait_event_has_match(mgr, args, queue),
drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.h +1 -0
index 75cf1c616a06..de0fc583d51f 100644
@@ -68,6 +68,7 @@ struct amdgpu_wait_event_mgr {
 
 void amdgpu_wait_event_mgr_init(struct amdgpu_wait_event_mgr *mgr);
 void amdgpu_wait_event_mgr_fini(struct amdgpu_wait_event_mgr *mgr);
+void amdgpu_wait_event_wake_all(struct drm_device *dev);
 
 void amdgpu_wait_event_remove_queue(struct amdgpu_wait_event_mgr *mgr,
 				    struct amdgpu_usermode_queue *queue);
include/uapi/drm/amdgpu_drm.h +7 -0
index 543f698b83cf..6bb2a75bf7fa 100644
@@ -346,6 +346,13 @@ struct drm_amdgpu_wait_event_data {
  * event record to userspace. Matching records are consumed by a single
  * waiter.
  *
+ * Removing the selected queue cancels a queue-scoped wait with -ENOENT
+ * when no matching record is available. A wait that obtains a record
+ * concurrently with removal may still succeed.
+ *
+ * Device unplug wakes pending waits, which return -ENODEV. A wait
+ * already completing with a record may still succeed.
+ *
  * @deadline_ns uses the same time base as CLOCK_MONOTONIC and ktime_get().
  * If the deadline has already passed, or if it is 0, the ioctl returns
  * immediately when no matching record is pending.
Signature · 1 line
2.50.1
#10 lore raw

PATCH v2 10/11 drm/amdgpu: Wait for USERQ destructors on close

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

A reset can retain a queue reference after file close removes the queue from the per-file xarray. Its final put could then run the destructor after the queue manager and fpriv have been freed.

Track a lifetime reference for each created queue. During file close, wait for all queue destructors to finish before destroying the manager.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 17 ++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  7 ++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16 -1
index 310f254d23ce..c4b5d14da89e 100644
@@ -773,6 +773,9 @@ static void amdgpu_userq_kref_destroy(struct kref *kref)
 	r = amdgpu_userq_destroy(uq_mgr, queue);
 	if (r)
 		drm_file_err(uq_mgr->file, "Failed to destroy usermode queue %d\n", r);
+
+	if (atomic_dec_and_test(&uq_mgr->queue_lifetime_refs))
+		complete_all(&uq_mgr->queues_gone);
 }
 
 struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr, u32 qid)
@@ -1042,6 +1045,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	}
 
 	atomic_inc(&uq_mgr->userq_count[queue->queue_type]);
+	atomic_inc(&uq_mgr->queue_lifetime_refs);
 	mutex_unlock(&uq_mgr->userq_mutex);
 
 	r = xa_alloc(&uq_mgr->userq_xa, &qid, queue,
@@ -1687,7 +1691,8 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr)
 	amdgpu_userq_evict_all(uq_mgr);
 }
 
-int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
+int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr,
+			  struct drm_file *file_priv,
 			  struct amdgpu_device *adev)
 {
 	mutex_init(&userq_mgr->userq_mutex);
@@ -1696,6 +1701,8 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
 	userq_mgr->file = file_priv;
 	userq_mgr->proc_ctx_allocated = false;
 	mutex_init(&userq_mgr->proc_ctx_lock);
+	atomic_set(&userq_mgr->queue_lifetime_refs, 1);
+	init_completion(&userq_mgr->queues_gone);
 
 	INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
 	INIT_WORK(&userq_mgr->reset_work, amdgpu_userq_mgr_reset_work);
@@ -1743,6 +1750,14 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 		amdgpu_userq_put(queue);
 	}
 
+	/*
+	 * Reset may still hold a queue after its xarray entry is removed.
+	 * Keep the manager alive until the final destructor completes.
+	 */
+	if (atomic_dec_and_test(&userq_mgr->queue_lifetime_refs))
+		complete_all(&userq_mgr->queues_gone);
+	wait_for_completion(&userq_mgr->queues_gone);
+
 	xa_destroy(&userq_mgr->userq_xa);
 
 	/*
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -1
index 6fb350a83328..d0c808986b8c 100644
@@ -24,6 +24,9 @@
 
 #ifndef AMDGPU_USERQ_H_
 #define AMDGPU_USERQ_H_
+
+#include <linux/completion.h>
+
 #include "amdgpu_eviction_fence.h"
 
 #define AMDGPU_MAX_USERQ_COUNT 512
@@ -160,7 +163,9 @@ struct amdgpu_userq_mgr {
 	 * Reset work which is used when eviction fails.
 	 */
 	struct work_struct		reset_work;
-	atomic_t                        userq_count[AMDGPU_RING_TYPE_MAX];
+	atomic_t			userq_count[AMDGPU_RING_TYPE_MAX];
+	atomic_t			queue_lifetime_refs;
+	struct completion		queues_gone;
 };
 
 struct amdgpu_db_info {
Signature · 1 line
2.50.1
#11 lore raw

PATCH v2 11/11 drm/amdgpu: Bound WAIT_EVENT records and report event loss

In reply to Jesse Zhang

From: Srinivasan Shanmugam <[email protected]>

Limit pending WAIT_EVENT records to 1024 per DRM file and event type so an undrained event stream cannot grow the record list without bound. The queues of a DRM file share the limit of a queue-scoped event type.

When the limit is reached or allocation fails, preserve existing records and set a loss indication for the source of the dropped event:
the queue for a queue-scoped event, the DRM file for a device-scoped one. Return -EOVERFLOW to one WAIT_EVENT caller for that event type and source when no matching record is pending.

Publish the record or loss indication before signalling EVENTFD. Document that an EVENTFD wakeup may have no matching metadata record.

v2: Bound the records per event type and report a loss per event
source, so unconsumed USERQ_EOP records no longer drop the records of other event types. (Jesse)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  7 ++
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.c    | 96 +++++++++++++++----
 .../gpu/drm/amd/amdgpu/amdgpu_wait_event.h    | 16 +++-
 include/uapi/drm/amdgpu_drm.h                 | 17 ++++
 4 files changed, 114 insertions(+), 22 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
index d0c808986b8c..3158fd946d8f 100644
@@ -111,6 +111,13 @@ struct amdgpu_usermode_queue {
 	 */
 	bool			events_closed;
 
+	/*
+	 * Mask of the queue-scoped event types which dropped a WAIT_EVENT
+	 * record of this queue. Protected by the lock of the file's
+	 * WAIT_EVENT manager.
+	 */
+	unsigned long		events_lost;
+
 	union {
 		struct {
 			u64 queue_rb;
drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.c +76 -20

197 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_wait_event.h +14 -2
index de0fc583d51f..74cee98ceae8 100644
@@ -37,6 +37,9 @@
 
 struct amdgpu_usermode_queue;
 
+/* Event types index the per-type state, 0 is not an event type. */
+#define AMDGPU_WAIT_EVENT_TYPE_COUNT (DRM_AMDGPU_EVENT_TYPE_GPU_RESET + 1)
+
 /**
  * struct amdgpu_wait_event_record - pending WAIT_EVENT record
  * @node: link in the manager's pending-record list
@@ -51,18 +54,27 @@ struct amdgpu_wait_event_record {
 
 /**
  * struct amdgpu_wait_event_mgr - per-file WAIT_EVENT manager
- * @lock: protects @pending and @dead
+ * @lock: protects @pending, @pending_count, @overflow, @dead and the
+ *	  events_lost mask of the file's user queues
  * @wq: waitqueue used by blocked WAIT_EVENT ioctls
  * @pending: pending event records
+ * @pending_count: number of records on @pending, per event type
  * @seqno: per-file monotonically increasing event sequence number
+ * @overflow: mask of the device-scoped event types which dropped one or
+ *	      more records; awaiting loss reporting
  * @dead: prevents new records from being queued during teardown
  */
 struct amdgpu_wait_event_mgr {
-	/* Protects @pending and @dead. */
+	/*
+	 * Protects @pending, @pending_count, @overflow, @dead and the
+	 * events_lost mask of the file's user queues.
+	 */
 	spinlock_t lock;
 	wait_queue_head_t wq;
 	struct list_head pending;
+	u32 pending_count[AMDGPU_WAIT_EVENT_TYPE_COUNT];
 	atomic64_t seqno;
+	unsigned long overflow;
 	bool dead;
 };
 
include/uapi/drm/amdgpu_drm.h +17 -0
index 6bb2a75bf7fa..ecaadb04cab0 100644
@@ -346,6 +346,23 @@ struct drm_amdgpu_wait_event_data {
  * event record to userspace. Matching records are consumed by a single
  * waiter.
  *
+ * Pending storage is bounded per DRM file and event type, so records of
+ * one event type which are not consumed do not take the place of another
+ * event type. The queues of a DRM file share the limit of a queue-scoped
+ * event type: the unconsumed records of one queue can use it up, and
+ * another queue is then told about its own dropped records with
+ * -EOVERFLOW. When a new record cannot be stored because the limit is
+ * reached or allocation fails, existing records are preserved and a loss
+ * indication is set for the source of the event: the queue for a
+ * queue-scoped event type, the DRM file for a device-scoped one.
+ * If no matching record is pending, a WAIT_EVENT for that event type and
+ * source consumes the indication and returns -EOVERFLOW without writing
+ * event metadata. The indication is consumed by one waiter. It reports
+ * that one or more of the requested records were lost, not how many.
+ * Userspace may continue draining records after -EOVERFLOW.
+ * Matching EVENTFD subscribers are notified when loss is recorded; an
+ * eventfd wakeup does not guarantee a matching metadata record.
+ *
  * Removing the selected queue cancels a queue-scoped wait with -ENOENT
  * when no matching record is available. A wait that obtains a record
  * concurrently with removal may still succeed.
Signature · 1 line
2.50.1