From: Dominik Grzegorzek <[email protected]>

Add events to inform the debugger about the creation and destruction of
exec_queues. Use user engine class types instead of the internal
xe_engine_class enum in exec_queue events. During discovery, only advertise
exec_queues with render or compute class, excluding others.

v2: - Only track long running queues
    - Checkpatch (Tilak)
v3: __counted_by added
v4: - use helpers for filtering engines (Mika)
v5: - fix lrc handles memcpy size bug (Mika)
v6: - lookup for create event (Sashiko)
    - defer engine class lookup (Sashiko)
    - avoid uninitialized read on adding handle (Sashiko)
    - too strict warn on q->width check (Sashiko)
    - create eudebug eq event early (Claude)
    - dont drop session if vm is already gone (Claude)
v7: - lookups under the debugger lock (Mika)

Signed-off-by: Dominik Grzegorzek <[email protected]>
Signed-off-by: Maciej Patelczyk <[email protected]>
Signed-off-by: Mika Kuoppala <[email protected]>
---
 Documentation/gpu/xe/xe_eudebug.rst   |   3 +
 drivers/gpu/drm/xe/xe_eudebug.c       | 282 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug.h       |   7 +
 drivers/gpu/drm/xe/xe_eudebug_types.h |   6 +-
 drivers/gpu/drm/xe/xe_exec_queue.c    |   8 +-
 include/uapi/drm/xe_drm_eudebug.h     |  31 +++
 6 files changed, 330 insertions(+), 7 deletions(-)

diff --git a/Documentation/gpu/xe/xe_eudebug.rst 
b/Documentation/gpu/xe/xe_eudebug.rst
index 9c6070784e84..f3152a13e250 100644
--- a/Documentation/gpu/xe/xe_eudebug.rst
+++ b/Documentation/gpu/xe/xe_eudebug.rst
@@ -65,3 +65,6 @@ Resource Event Types
 
 .. kernel-doc:: include/uapi/drm/xe_drm_eudebug.h
    :identifiers: drm_xe_eudebug_event_vm
+
+.. kernel-doc:: include/uapi/drm/xe_drm_eudebug.h
+   :identifiers: drm_xe_eudebug_event_exec_queue
diff --git a/drivers/gpu/drm/xe/xe_eudebug.c b/drivers/gpu/drm/xe/xe_eudebug.c
index e0ef34588d3d..0f8b0452f9ba 100644
--- a/drivers/gpu/drm/xe/xe_eudebug.c
+++ b/drivers/gpu/drm/xe/xe_eudebug.c
@@ -16,6 +16,8 @@
 #include "xe_device.h"
 #include "xe_eudebug.h"
 #include "xe_eudebug_types.h"
+#include "xe_exec_queue.h"
+#include "xe_hw_engine.h"
 #include "xe_macros.h"
 #include "xe_sriov_pf.h"
 #include "xe_vm.h"
@@ -486,6 +488,28 @@ __find_handle(struct xe_eudebug_resource *r,
        return h;
 }
 
+static int find_handle(struct xe_eudebug *d,
+                      const int type,
+                      const void *p)
+{
+       const u64 key = (uintptr_t)p;
+       struct xe_eudebug_resource *r;
+       struct xe_eudebug_handle *h;
+       int id;
+
+       if (XE_WARN_ON(!key))
+               return -EINVAL;
+
+       r = resource_from_type(d, type);
+
+       mutex_lock(&d->lock);
+       h = __find_handle(r, key);
+       id = h ? h->id : -ENOENT;
+       mutex_unlock(&d->lock);
+
+       return id;
+}
+
 /*
  * Adding a handle is split around d->lock: handle_prepare() allocates the
  * handle and reserves its id, __handle_insert() publishes it and stamps
@@ -799,6 +823,244 @@ void xe_eudebug_vm_destroy(struct xe_file *xef, struct 
xe_vm *vm)
        xe_eudebug_put(d);
 }
 
+static const u16 xe_to_user_engine_class[] = {
+       [XE_ENGINE_CLASS_RENDER] = DRM_XE_ENGINE_CLASS_RENDER,
+       [XE_ENGINE_CLASS_COPY] = DRM_XE_ENGINE_CLASS_COPY,
+       [XE_ENGINE_CLASS_VIDEO_DECODE] = DRM_XE_ENGINE_CLASS_VIDEO_DECODE,
+       [XE_ENGINE_CLASS_VIDEO_ENHANCE] = DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE,
+       [XE_ENGINE_CLASS_COMPUTE] = DRM_XE_ENGINE_CLASS_COMPUTE,
+};
+
+static int send_exec_queue_event(struct xe_eudebug *d, u32 flags,
+                                u64 vm_handle, u64 exec_queue_handle,
+                                enum xe_engine_class class,
+                                u32 width, u64 *lrc_handles, u64 seqno)
+{
+       struct drm_xe_eudebug_event *event;
+       struct drm_xe_eudebug_event_exec_queue *e;
+       const u32 sz = struct_size(e, lrc_handle, width);
+       u32 xe_engine_class;
+       int err;
+
+       if (!xe_engine_supports_eudebug(class))
+               return -EINVAL;
+
+       xe_engine_class = xe_to_user_engine_class[class];
+
+       mutex_lock(&d->lock);
+       event = xe_eudebug_prepare_event(d, DRM_XE_EUDEBUG_EVENT_EXEC_QUEUE,
+                                        seqno, flags, sz);
+       e = cast_event(e, event);
+
+       e->vm_handle = vm_handle;
+       e->exec_queue_handle = exec_queue_handle;
+       e->engine_class = xe_engine_class;
+       e->width = width;
+
+       memcpy(e->lrc_handle, lrc_handles, width * sizeof(u64));
+
+       err = xe_eudebug_queue_event(d, event);
+       mutex_unlock(&d->lock);
+
+       return err;
+}
+
+/*
+ * A debuggable queue needs only an LR vm, and a fault mode vm can be
+ * destroyed while its queues live on: xe_vm_destroy_ioctl() only refuses
+ * a vm with preempt fence mode queues. The debugger has then already seen
+ * the vm go, so report such a queue against vm handle 0 rather than
+ * treating the missing handle as a diverged resource map.
+ *
+ * Looked up in the critical section that stamps the queue event's seqno.
+ * A vm destroy removes the handle and stamps its own seqno under the same
+ * lock, so the handle is gone exactly when the vm destroy came first in
+ * seqno order, and a debugger replaying the events never sees a queue
+ * event on a vm it has dropped, or vm 0 for one it has not.
+ */
+static int __exec_queue_vm_handle(struct xe_eudebug *d, struct xe_exec_queue 
*q)
+{
+       struct xe_eudebug_handle *h;
+
+       lockdep_assert_held(&d->lock);
+
+       h = __find_handle(resource_from_type(d, XE_EUDEBUG_RES_TYPE_VM),
+                         (uintptr_t)q->vm);
+
+       return h ? h->id : 0;
+}
+
+static int exec_queue_create_event(struct xe_eudebug *d,
+                                  struct xe_file *xef, struct xe_exec_queue *q)
+{
+       struct xe_eudebug_handle *h;
+       int h_vm, h_queue;
+       u64 h_lrc[XE_HW_ENGINE_MAX_INSTANCE], seqno;
+       int i;
+       int ret;
+
+       if (!xe_exec_queue_is_debuggable(q))
+               return 0;
+
+       if (XE_WARN_ON(q->width > XE_HW_ENGINE_MAX_INSTANCE))
+               return -EINVAL;
+
+       ret = 0;
+       for (i = 0; i < q->width; i++) {
+               ret = _xe_eudebug_add_handle(d,
+                                            XE_EUDEBUG_RES_TYPE_LRC,
+                                            q->lrc[i],
+                                            NULL);
+
+               if (ret < 0)
+                       break;
+
+               XE_WARN_ON(!ret);
+
+               h_lrc[i] = ret;
+       }
+
+       if (ret < 0)
+               return ret;
+
+       h = handle_prepare(d, XE_EUDEBUG_RES_TYPE_EXEC_QUEUE, q);
+       if (IS_ERR(h))
+               return PTR_ERR(h);
+
+       scoped_guard(mutex, &d->lock) {
+               h_queue = __handle_insert(d, XE_EUDEBUG_RES_TYPE_EXEC_QUEUE, h,
+                                         &seqno);
+               h_vm = __exec_queue_vm_handle(d, q);
+       }
+
+       eu_dbg(d, "exec queue %p added: %d, vm %d\n", q, h_queue, h_vm);
+
+       if (h_queue < 0) {
+               handle_abort(d, XE_EUDEBUG_RES_TYPE_EXEC_QUEUE, h);
+               return h_queue;
+       }
+
+       /* No need to cleanup for added handles on error as if we fail
+        * we disconnect
+        */
+
+       ret = send_exec_queue_event(d, DRM_XE_EUDEBUG_EVENT_CREATE,
+                                   h_vm, h_queue, q->class,
+                                   q->width, h_lrc, seqno);
+
+       if (ret)
+               eu_dbg(d, "send_exec_queue_event create error %d", ret);
+
+       return ret;
+}
+
+static int exec_queue_destroy_event(struct xe_eudebug *d,
+                                   struct xe_file *xef,
+                                   struct xe_exec_queue *q)
+{
+       int h_vm, h_queue;
+       u64 h_lrc[XE_HW_ENGINE_MAX_INSTANCE], seqno;
+       int i;
+       int ret, err = 0;
+
+       if (!xe_exec_queue_is_debuggable(q))
+               return 0;
+
+       /*
+        * A queue we never handed out to the debugger needs no destroy event.
+        * The queue handle is added last on create, so its presence implies
+        * the lrc handles are there too. Past this point a missing handle
+        * means our resource map and the client have diverged, and that has
+        * to drop the session. The exception is a concurrent detach, which
+        * empties the maps without the ioctl_lock, but only after marking us
+        * detached.
+        */
+       if (find_handle(d, XE_EUDEBUG_RES_TYPE_EXEC_QUEUE, q) == -ENOENT)
+               return 0;
+
+       if (XE_WARN_ON(q->width > XE_HW_ENGINE_MAX_INSTANCE))
+               return -EINVAL;
+
+       for (i = 0; i < q->width; i++) {
+               ret = _xe_eudebug_remove_handle(d,
+                                               XE_EUDEBUG_RES_TYPE_LRC,
+                                               q->lrc[i],
+                                               NULL);
+               if (ret < 0) {
+                       XE_WARN_ON(!xe_eudebug_is_detached(d));
+                       if (!err)
+                               err = ret;
+                       h_lrc[i] = 0;
+                       continue;
+               }
+
+               XE_WARN_ON(!ret);
+               h_lrc[i] = ret;
+       }
+
+       if (err)
+               return err;
+
+       scoped_guard(mutex, &d->lock) {
+               h_queue = __xe_eudebug_remove_handle(d,
+                                                    
XE_EUDEBUG_RES_TYPE_EXEC_QUEUE,
+                                                    q, &seqno);
+               h_vm = __exec_queue_vm_handle(d, q);
+       }
+
+       eu_dbg(d, "exec queue %p removed: %d, vm %d\n", q, h_queue, h_vm);
+
+       if (h_queue < 0)
+               return h_queue;
+
+       ret = send_exec_queue_event(d, DRM_XE_EUDEBUG_EVENT_DESTROY,
+                                   h_vm, h_queue, q->class,
+                                   q->width, h_lrc, seqno);
+
+       if (ret)
+               eu_dbg(d, "send_exec_queue_event destroy error %d\n", ret);
+
+       return ret;
+}
+
+void xe_eudebug_exec_queue_create(struct xe_file *xef, struct xe_exec_queue *q)
+{
+       struct xe_eudebug *d;
+       int err;
+
+       if (!xe_engine_supports_eudebug(q->class))
+               return;
+
+       d = xe_eudebug_get(xef);
+       if (!d)
+               return;
+
+       err = exec_queue_create_event(d, xef, q);
+       if (err)
+               xe_eudebug_disconnect(d, err);
+
+       xe_eudebug_put(d);
+}
+
+void xe_eudebug_exec_queue_destroy(struct xe_file *xef, struct xe_exec_queue 
*q)
+{
+       struct xe_eudebug *d;
+       int err;
+
+       if (!xe_engine_supports_eudebug(q->class))
+               return;
+
+       d = xe_eudebug_get(xef);
+       if (!d)
+               return;
+
+       err = exec_queue_destroy_event(d, xef, q);
+       if (err)
+               xe_eudebug_disconnect(d, err);
+
+       xe_eudebug_put(d);
+}
+
 /*
  * All the relevant ioctls that manipulate the xe_file resources eudebug
  * cares about are gated behind down_read(&xef->eudebug.ioctl_lock). The
@@ -807,9 +1069,10 @@ void xe_eudebug_vm_destroy(struct xe_file *xef, struct 
xe_vm *vm)
  */
 static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
 {
+       struct xe_exec_queue *q;
        struct xe_vm *vm;
        unsigned long i;
-       unsigned int vm_count = 0;
+       unsigned int vm_count = 0, eq_count = 0;
        int err = 0;
 
        lockdep_assert_held_write(&xef->eudebug.ioctl_lock);
@@ -828,10 +1091,21 @@ static int discover_client(struct xe_eudebug *d, struct 
xe_file *xef)
                vm_count++;
        }
 
+       xa_for_each(&xef->exec_queue.xa, i, q) {
+               if (!xe_engine_supports_eudebug(q->class))
+                       continue;
+
+               err = exec_queue_create_event(d, xef, q);
+               if (err)
+                       goto out;
+
+               eq_count++;
+       }
+
 out:
-       if (vm_count)
-               eu_dbg(d, "Discovery found %u vms",
-                      vm_count);
+       if (vm_count || eq_count)
+               eu_dbg(d, "Discovery found %u vms, %u exec_queues",
+                      vm_count, eq_count);
 
        eu_dbg(d, "Discovery end for %llu: %d", d->session, err);
 
diff --git a/drivers/gpu/drm/xe/xe_eudebug.h b/drivers/gpu/drm/xe/xe_eudebug.h
index 413a45129193..ec07c567356d 100644
--- a/drivers/gpu/drm/xe/xe_eudebug.h
+++ b/drivers/gpu/drm/xe/xe_eudebug.h
@@ -14,6 +14,7 @@ struct drm_file;
 struct xe_device;
 struct xe_file;
 struct xe_vm;
+struct xe_exec_queue;
 struct xe_eudebug;
 
 #if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
@@ -49,6 +50,9 @@ void xe_eudebug_file_close(struct xe_file *xef);
 void xe_eudebug_vm_create(struct xe_file *xef, struct xe_vm *vm);
 void xe_eudebug_vm_destroy(struct xe_file *xef, struct xe_vm *vm);
 
+void xe_eudebug_exec_queue_create(struct xe_file *xef, struct xe_exec_queue 
*q);
+void xe_eudebug_exec_queue_destroy(struct xe_file *xef, struct xe_exec_queue 
*q);
+
 #else
 
 static inline int xe_eudebug_connect_ioctl(struct drm_device *dev,
@@ -64,6 +68,9 @@ static inline void xe_eudebug_file_close(struct xe_file *xef) 
{ }
 static inline void xe_eudebug_vm_create(struct xe_file *xef, struct xe_vm *vm) 
{ }
 static inline void xe_eudebug_vm_destroy(struct xe_file *xef, struct xe_vm 
*vm) { }
 
+static inline void xe_eudebug_exec_queue_create(struct xe_file *xef, struct 
xe_exec_queue *q) { }
+static inline void xe_eudebug_exec_queue_destroy(struct xe_file *xef, struct 
xe_exec_queue *q) { }
+
 #endif /* CONFIG_DRM_XE_EUDEBUG */
 
 #endif /* _XE_EUDEBUG_H_ */
diff --git a/drivers/gpu/drm/xe/xe_eudebug_types.h 
b/drivers/gpu/drm/xe/xe_eudebug_types.h
index 598abb249195..0cb1b3b678e4 100644
--- a/drivers/gpu/drm/xe/xe_eudebug_types.h
+++ b/drivers/gpu/drm/xe/xe_eudebug_types.h
@@ -31,7 +31,7 @@ enum xe_eudebug_cap_state {
        XE_EUDEBUG_CAP_ENABLED,
 };
 
-#define XE_EUDEBUG_MAX_EVENT_TYPE DRM_XE_EUDEBUG_EVENT_VM
+#define XE_EUDEBUG_MAX_EVENT_TYPE DRM_XE_EUDEBUG_EVENT_EXEC_QUEUE
 
 /**
  * struct xe_eudebug_handle - eudebug resource handle
@@ -62,7 +62,9 @@ struct xe_eudebug_resource {
 };
 
 #define XE_EUDEBUG_RES_TYPE_VM         0
-#define XE_EUDEBUG_RES_TYPE_COUNT      (XE_EUDEBUG_RES_TYPE_VM + 1)
+#define XE_EUDEBUG_RES_TYPE_EXEC_QUEUE  1
+#define XE_EUDEBUG_RES_TYPE_LRC                2
+#define XE_EUDEBUG_RES_TYPE_COUNT      (XE_EUDEBUG_RES_TYPE_LRC + 1)
 
 /**
  * struct xe_eudebug - Top level struct for eudebug: the connection
diff --git a/drivers/gpu/drm/xe/xe_exec_queue.c 
b/drivers/gpu/drm/xe/xe_exec_queue.c
index e65bda492e58..3eeb8a108b40 100644
--- a/drivers/gpu/drm/xe/xe_exec_queue.c
+++ b/drivers/gpu/drm/xe/xe_exec_queue.c
@@ -1548,10 +1548,14 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, 
void *data,
        if (eci[0].engine_class != DRM_XE_ENGINE_CLASS_VM_BIND)
                xe_vm_add_exec_queue(vm, q);
 
+       xe_eudebug_exec_queue_create(xef, q);
+
        /* user id alloc must always be last in ioctl to prevent UAF */
        err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
-       if (err)
+       if (err) {
+               xe_eudebug_exec_queue_destroy(xef, q);
                goto del_hw_engine_group;
+       }
 
        args->exec_queue_id = id;
 
@@ -1736,6 +1740,8 @@ int xe_exec_queue_destroy_ioctl(struct drm_device *dev, 
void *data,
        if (q->vm && q->hwe->hw_engine_group)
                xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
 
+       xe_eudebug_exec_queue_destroy(xef, q);
+
        xe_exec_queue_kill(q);
 
        trace_xe_exec_queue_close(q);
diff --git a/include/uapi/drm/xe_drm_eudebug.h 
b/include/uapi/drm/xe_drm_eudebug.h
index 63f4a6142a1a..12e905441f1d 100644
--- a/include/uapi/drm/xe_drm_eudebug.h
+++ b/include/uapi/drm/xe_drm_eudebug.h
@@ -78,6 +78,7 @@ struct drm_xe_eudebug_event {
 #define DRM_XE_EUDEBUG_EVENT_NONE              0
 #define DRM_XE_EUDEBUG_EVENT_READ              1
 #define DRM_XE_EUDEBUG_EVENT_VM                        2
+#define DRM_XE_EUDEBUG_EVENT_EXEC_QUEUE                3
 
        /** @flags: Flags */
        __u16 flags;
@@ -124,6 +125,36 @@ struct drm_xe_eudebug_event_vm {
        __u64 vm_handle;
 };
 
+/**
+ * struct drm_xe_eudebug_event_exec_queue - Exec Queue resource event
+ *
+ * Resource creation/destruction event for an Exec Queue
+ */
+struct drm_xe_eudebug_event_exec_queue {
+       /** @base: base event */
+       struct drm_xe_eudebug_event base;
+
+       /**
+        * @vm_handle: the vm handle this exec queue belongs to, or 0 if the
+        * vm was destroyed before this event. A vm can be destroyed while
+        * its exec queues live on, so a destroy event can carry 0 where the
+        * create event carried the vm handle.
+        */
+       __u64 vm_handle;
+
+       /** @exec_queue_handle: unique handle for this exec queue */
+       __u64 exec_queue_handle;
+
+       /** @engine_class: engine class, a %DRM_XE_ENGINE_CLASS_* value */
+       __u32 engine_class;
+
+       /** @width: width of exec queue, how many lrcs (handles) it has */
+       __u32 width;
+
+       /** @lrc_handle: array of lrc handles */
+       __u64 lrc_handle[] __counted_by(width);
+};
+
 #if defined(__cplusplus)
 }
 #endif
-- 
2.53.0

Reply via email to