On 09/11/2026 13:55, Mika Kuoppala wrote:
Add the eudebug interface to the Xe driver, enabling
user-space debuggers (e.g., GDB) to track and interact with GPU resources
of a DRM client. Debuggers can inspect or modify these resources,
for example, to locate ISA/ELF sections and install breakpoints in a
shader's instruction stream.
A debugger opens a connection to the Xe driver via a DRM ioctl, specifying
the target DRM client's file descriptor. This returns an anonymous file
descriptor for the connection, which can be used to listen for resource
creation/destruction events. The same file descriptor can also be used to
receive hardware state change events and control execution flow by
interrupting EU threads on the GPU (in follow-up patches).
Introduce the eudebug connection and event queuing,
adding VM create/destroy events as a baseline. Additional events and
hardware control for full debugger operation are needed and will be
introduced in follow-up patches.
The resource tracking components are inspired by Maciej Patelczyk's work on
resource handling for i915. Chris Wilson suggested a two-way mapping
approach, which simplifies using the resource map as definitive
bookkeeping for resources relayed to the debugger during the discovery
phase (in a follow-up patch).
v2: - Kconfig support (Matthew)
- ptraced access control (Lucas)
- pass expected event length to user (Zbigniew)
- only track long running VMs
- checkpatch (Tilak)
- include order (Andrzej)
- 32bit fixes (Andrzej)
- cleaner get_task_struct
- remove xa_array and use clients.list for tracking (Mika)
v3: - adapt to removal of clients.lock (Mika)
- create_event cleanup (Christoph)
v4: - add proper header guards (Christoph)
- better read_event fault handling (Christoph, Mika)
- simplify attach (Mika)
- connect using target file descriptors
- avoid event->seqno after queue as it can UAF (Mika)
- use drmm for eudebug_fini (Maciej)
- squash dynamic enable
v6: - drm->authenticated is overzealous for render (Mika)
v7: - struct member documentation (Mika)
- enforce seqno mbz (Mika)
v8: - head->seqno fix (Mika)
- resource alloc and removal cleanup (Mika)
- s/wait_interruptible_timeout/wait_timeout (Mika)
- use fd_install in connect (Mika)
v9: - fix xef vs debugger race (Sashiko)
- assign d->xe early (Sashiko)
- simplify locking (Mika)
- fix pending->len outside lock (Sashiko)
- remove version from connect to address fd_install leak (Sashiko)
- create vm by id to avoid use after free (Sashiko)
- use xe_eudebug_detach on connection error path (Sashiko)
- don't sleep on kzalloc as we can disconnect (Sashiko)
- check if enabled on connection (Sashiko)
- preallocated event fifo for lockless allocs (Sashiko, Mika)
v10:
- wakeup after detach, take account of occupied (Sashiko)
- GFP_ACCOUNT for fifo (Sashiko)
- set O_CLOEXEC on connection fd (Sashiko)
- docstrings (Maciej)
- use guard and __kfree (Mika)
- init_early (Maciej)
- remove unused struct members
- no need to send destroy if vm was not announced (Claude)
- avoid dereference of xef on eu_print (Claude)
- don't silently truncate target fd (Claude)
- preallocate resource handles
- zero as target fd (Claude)
- hold drm_device (Claude)
v11: - fix eudebug=n build (Claude)
- document seqno as timeline (Sashiko)
v12: - drain nothing on detach, say so in the uapi (Sashiko)
- drop resource handles on detach to avoid uaf (Sashiko)
- ioctl naming, sysfs abi (Claude)
v13: - create enable_eudebug last, so init cannot undo an enable
(review-prompts)
v14: - one mutex for all debugger state, no lockless accesses (Mika)
v15: - __free() variables defined where they are assigned (Sashiko)
- poll in one lock section, no EPOLLIN once detached (Sashiko)
- connect in the uapi ioctl list (Sashiko)
- copy the event out of @events.pending directly, no bounce
allocation per read (Sashiko)
Cc: Maarten Lankhorst <[email protected]>
Cc: Lucas De Marchi <[email protected]>
Cc: Dominik Grzegorzek <[email protected]>
Cc: Andi Shyti <[email protected]>
Cc: Matt Roper <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Zbigniew Kempczyński <[email protected]>
Cc: Andrzej Hajda <[email protected]>
Signed-off-by: Mika Kuoppala <[email protected]>
Signed-off-by: Maciej Patelczyk <[email protected]>
Signed-off-by: Dominik Grzegorzek <[email protected]>
Signed-off-by: Jonathan Cavitt <[email protected]>
Signed-off-by: Christoph Manszewski <[email protected]>
---
.../ABI/testing/sysfs-driver-intel-xe-eudebug | 36 +
Documentation/gpu/driver-uapi.rst | 2 +
.../userspace-api/ioctl/ioctl-number.rst | 1 +
MAINTAINERS | 2 +
drivers/gpu/drm/xe/Kconfig | 10 +
drivers/gpu/drm/xe/Makefile | 3 +
drivers/gpu/drm/xe/xe_device.c | 8 +
drivers/gpu/drm/xe/xe_device_types.h | 29 +
drivers/gpu/drm/xe/xe_eudebug.c | 1212 +++++++++++++++++
drivers/gpu/drm/xe/xe_eudebug.h | 68 +
drivers/gpu/drm/xe/xe_eudebug_types.h | 168 +++
drivers/gpu/drm/xe/xe_vm.c | 15 +-
include/uapi/drm/xe_drm.h | 24 +
include/uapi/drm/xe_drm_eudebug.h | 131 ++
14 files changed, 1707 insertions(+), 2 deletions(-)
create mode 100644 Documentation/ABI/testing/sysfs-driver-intel-xe-eudebug
create mode 100644 drivers/gpu/drm/xe/xe_eudebug.c
create mode 100644 drivers/gpu/drm/xe/xe_eudebug.h
create mode 100644 drivers/gpu/drm/xe/xe_eudebug_types.h
create mode 100644 include/uapi/drm/xe_drm_eudebug.h
<snip>
+
+static int
+xe_eudebug_attach(struct xe_device *xe, struct drm_file *parent_file,
+ struct xe_eudebug *d, u64 target_fd)
+{
+ struct drm_file *drm_file;
+ struct xe_file *target_xef;
+ int ret;
+
+ if (XE_IOCTL_DBG(xe, target_fd > INT_MAX))
Should target_fd not just be __s32 in the uapi or something? That would
also match with the rest of the drm uapi AFAICT.
+ return -EBADF;
+
+ struct file *file __free(fput) = fget(target_fd);
+ if (XE_IOCTL_DBG(xe, !file))
+ return -EBADF;
+
+ drm_file = file->private_data;
+ if (XE_IOCTL_DBG(xe, !drm_file))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, parent_file->filp->f_op != file->f_op))
xe_is_xe_file() ?
+ return -EINVAL;
+
+ target_xef = drm_file->driver_priv;
+ if (XE_IOCTL_DBG(xe, !target_xef))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, xe != target_xef->xe))
+ return -EINVAL;
+
+ ret = add_debugger(xe, d, drm_file);
+ if (XE_IOCTL_DBG(xe, ret))
+ return ret;
+
+ eu_dbg(d, "session %llu attached to %s", d->session,
+ parent_file == drm_file ? "self" : "remote");
+
+ return 0;
+}
+
+static int xe_eudebug_release(struct inode *inode, struct file *file)
+{
+ struct xe_eudebug *d = file->private_data;
+ struct drm_device *drm = &d->xe->drm;
+
+ xe_eudebug_disconnect(d, 0);
+ xe_eudebug_put(d);
+
+ drm_dev_put(drm);
+
+ return 0;
+}
+
+static bool __event_fifo_has_events(struct xe_eudebug *d)
+{
+ lockdep_assert_held(&d->lock);
+
+ if (d->events.pending_occupied)
+ return true;
+
+ return kfifo_len(&d->events.fifo) > sizeof(struct drm_xe_eudebug_event);
+}
+
+static bool event_fifo_has_events(struct xe_eudebug *d)
+{
+ guard(mutex)(&d->lock);
+
+ /* Allow all waiters to proceed to check their state */
+ return __xe_eudebug_is_detached(d) || __event_fifo_has_events(d);
+}
+
+/*
+ * wait_event_*() evaluates its condition with the task already marked as
+ * sleeping, where the lock cannot be taken. wait_woken() lets us check
+ * the condition while running, without losing a wakeup in between.
+ *
+ * Return: as wait_event_interruptible_timeout()
+ */
+static long wait_for_events(struct xe_eudebug *d, long timeout)
+{
+ DEFINE_WAIT_FUNC(wait, woken_wake_function);
+
+ add_wait_queue(&d->events.write_done, &wait);
+ while (!event_fifo_has_events(d)) {
+ if (signal_pending(current)) {
+ timeout = -ERESTARTSYS;
+ break;
+ }
+
+ if (!timeout)
+ break;
+
+ timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
+ }
+ remove_wait_queue(&d->events.write_done, &wait);
+
+ return timeout;
+}
+
+static __poll_t xe_eudebug_poll(struct file *file, poll_table *wait)
+{
+ struct xe_eudebug * const d = file->private_data;
+ __poll_t ret = 0;
+
+ poll_wait(file, &d->events.write_done, wait);
+
+ guard(mutex)(&d->lock);
+
+ if (__xe_eudebug_is_detached(d)) {
+ /* Queued events went with the detach, READ_EVENT only fails */
+ ret = EPOLLHUP;
+ if (d->target.err)
+ ret |= EPOLLERR;
+ } else if (__event_fifo_has_events(d)) {
+ ret = EPOLLIN;
+ }
+
+ return ret;
+}
+
+static void xe_eudebug_reader_clear(struct xe_eudebug *d)
+{
+ if (!d)
+ return;
+
+ guard(mutex)(&d->lock);
+ d->events.reader_active = false;
+}
+
+DEFINE_FREE(reader_active, struct xe_eudebug *, xe_eudebug_reader_clear(_T))
+
+static long xe_eudebug_read_event(struct xe_eudebug *d,
+ const u64 arg,
+ const bool wait)
+{
+ struct xe_device *xe = d->xe;
+ struct drm_xe_eudebug_event __user * const user_orig =
+ u64_to_user_ptr(arg);
+ struct drm_xe_eudebug_event user_event;
+ struct drm_xe_eudebug_event *pending;
+ long ret = 0;
+ int pending_len = 0;
+ int fifo_ret;
+
+ if (XE_IOCTL_DBG(xe, copy_from_user(&user_event, user_orig,
sizeof(user_event))))
+ return -EFAULT;
+
+ if (XE_IOCTL_DBG(xe, user_event.type != DRM_XE_EUDEBUG_EVENT_READ))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, user_event.len < sizeof(*user_orig)))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, user_event.flags))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, user_event.seqno))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, user_event.reserved))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, xe_eudebug_is_detached(d)))
+ return -ENOTCONN;
+
+ scoped_guard(mutex, &d->lock) {
+ if (d->events.reader_active)
+ return -EBUSY;
+
+ d->events.reader_active = true;
+ }
+
+ struct xe_eudebug *reader __free(reader_active) = d;
+
+ /* XXX: define wait time in connect arguments ? */
+ if (wait) {
+ ret = wait_for_events(d, secs_to_jiffies(5));
+
+ if (XE_IOCTL_DBG(xe, ret < 0))
+ return ret;
+ }
+
+ mutex_lock(&d->lock);
+ fifo_ret = event_fifo_pending(d, &pending);
+ if (fifo_ret == 0) {
+ if (user_event.len < pending->len) {
+ pending_len = pending->len;
+ ret = -EMSGSIZE;
+ } else if (!access_ok(user_orig, pending->len)) {
+ ret = -EFAULT;
+ } else {
+ eu_dbg(d, "event read: type=%u, flags=0x%x, seqno=%llu",
+ pending->type, pending->flags, pending->seqno);
+ ret = 0;
+ }
+ } else if (fifo_ret == -ENOENT) {
+ ret = wait ? -ETIMEDOUT : -EAGAIN;
+ } else {
+ ret = fifo_ret; /* -ENOTCONN or -EIO */
+ }
+ mutex_unlock(&d->lock);
+
+ /* disconnect takes xe->eudebug.lock, so do it only after dropping ours
*/
+ if (fifo_ret == -EIO)
+ xe_eudebug_disconnect(d, -EIO);
+
+ if (ret == -EMSGSIZE) {
+ if (XE_IOCTL_DBG(xe, put_user(pending_len, &user_orig->len)))
+ ret = -EFAULT;
+ }
+
+ /*
+ * Copied without d->lock, so that faulting on the user pointer does not
+ * happen under it. Nothing else touches @events.pending meanwhile: only
+ * event_fifo_pending() writes it, only the reader marked by
+ * @events.reader_active calls that, and the event stays occupied until
+ * the copy has succeeded.
+ */
+ if (!ret && __copy_to_user(user_orig, pending, pending->len))
+ ret = -EFAULT;
+
+ if (!ret) {
+ mutex_lock(&d->lock);
+ d->events.pending_occupied = false;
+ mutex_unlock(&d->lock);
+ }
+ if (ret)
+ eu_dbg(d, "event read=%ld", ret);
+
+ return ret;
+}
+
+/**
+ * xe_eudebug_ioctl - Issue a command to eudebug interface
+ *
+ * @file : eudebug file (returned from connect)
+ * @cmd : cmd
+ * @arg : arguments depending on cmd
+ *
+ * Issue a eudebug command
+ *
+ * Return: 0 on success, negative error code on failure. -ENOTTY for an
+ * unknown command.
+ */
+static long xe_eudebug_ioctl(struct file *file,
+ unsigned int cmd,
+ unsigned long arg)
+{
+ struct xe_eudebug * const d = file->private_data;
+ long ret;
+
Is this missing some kind of drm_dev_is_unplugged() and wedged checks
somewhere?
+ switch (cmd) {
+ case DRM_XE_EUDEBUG_IOCTL_READ_EVENT:
+ ret = xe_eudebug_read_event(d, arg,
+ !(file->f_flags & O_NONBLOCK));
+ break;
+ default:
+ ret = -ENOTTY;
+ }
+
+ return ret;
+}
+
+static const struct file_operations fops = {
+ .owner = THIS_MODULE,
+ .release = xe_eudebug_release,
+ .poll = xe_eudebug_poll,
+ .unlocked_ioctl = xe_eudebug_ioctl,
+ .compat_ioctl = compat_ptr_ioctl,
+};
+
+/*
+ * Tell lockdep up front where the lock sits, so that a violation is
+ * reported on the first run rather than once both paths have been seen.
+ * Reclaim never takes it, so allocating under it is fine.
+ */
+static void xe_eudebug_lock_prime(struct xe_eudebug *d)
+{
+ if (!IS_ENABLED(CONFIG_LOCKDEP))
+ return;
+
+ mutex_lock(&d->lock);
+ fs_reclaim_acquire(GFP_KERNEL);
+ fs_reclaim_release(GFP_KERNEL);
+ mutex_unlock(&d->lock);
+}
+
+static int
+xe_eudebug_connect(struct xe_device *xe,
+ struct drm_file *drm_file,
+ struct drm_xe_eudebug_connect *param)
+{
+ const u64 known_open_flags = 0;
+ struct xe_eudebug *d;
+ struct file *file;
+ int fd, err;
+
+ if (XE_IOCTL_DBG(xe, param->extensions))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, param->flags & ~known_open_flags))
+ return -EINVAL;
+
+ if (XE_IOCTL_DBG(xe, param->reserved))
+ return -EINVAL;
+
+ if (!xe_eudebug_is_enabled(xe))
+ return -EOPNOTSUPP;
+
+ d = kzalloc_obj(*d, GFP_KERNEL | __GFP_ACCOUNT);
+ if (XE_IOCTL_DBG(xe, !d))
+ return -ENOMEM;
+
+ d->xe = xe;
+
+ kref_init(&d->ref);
+ mutex_init(&d->lock);
+ xe_eudebug_lock_prime(d);
+ init_waitqueue_head(&d->events.write_done);
+
+ err = xe_eudebug_resources_init(d);
+ if (XE_IOCTL_DBG(xe, err)) {
+ mutex_destroy(&d->lock);
+ kfree(d);
+ return err;
+ }
+
+ d->events.pending = kvzalloc(XE_EUDEBUG_EVENT_MAX_SIZE,
+ GFP_KERNEL | __GFP_ACCOUNT);
+ if (!d->events.pending) {
+ err = -ENOMEM;
+ goto err_put;
+ }
+
+ d->events.staging = kvzalloc(XE_EUDEBUG_EVENT_MAX_SIZE,
+ GFP_KERNEL | __GFP_ACCOUNT);
+ if (!d->events.staging) {
+ err = -ENOMEM;
+ goto err_put;
+ }
+
+ d->events.fifo_buf = kvmalloc(XE_EUDEBUG_FIFO_SIZE,
+ GFP_KERNEL | __GFP_ACCOUNT);
+ if (!d->events.fifo_buf) {
+ err = -ENOMEM;
+ goto err_put;
+ }
+
+ err = kfifo_init(&d->events.fifo, d->events.fifo_buf,
XE_EUDEBUG_FIFO_SIZE);
+ if (XE_IOCTL_DBG(xe, err))
+ goto err_put;
+
+ err = xe_eudebug_attach(xe, drm_file, d, param->fd);
+ if (XE_IOCTL_DBG(xe, err))
+ goto err_put;
+
+ fd = get_unused_fd_flags(O_CLOEXEC);
+ if (fd < 0) {
+ err = fd;
+ goto err_detach;
+ }
+
+ file = anon_inode_getfile("[xe_eudebug]", &fops, d, 0);
+ if (IS_ERR(file)) {
+ err = PTR_ERR(file);
+ goto err_fd;
+ }
+
+ eu_dbg(d, "connected session %llu", d->session);
+
+ drm_dev_get(&xe->drm);
+
+ fd_install(fd, file);
+
+ return fd;
+
+err_fd:
+ put_unused_fd(fd);
+err_detach:
+ xe_eudebug_detach(d, err);
+err_put:
+ xe_eudebug_put(d);
+
+ return err;
+}
+
+void xe_eudebug_file_close(struct xe_file *xef)
+{
+ struct xe_eudebug *d;
+
+ d = xe_eudebug_get(xef);
+ if (d) {
+ xe_eudebug_detach(d, 0);
+ xe_eudebug_put(d);
+ }
+}
+
+static int xe_eudebug_enable(struct xe_device *xe, bool enable)
+{
+ guard(mutex)(&xe->eudebug.lock);
+
+ if (xe->eudebug.cap_state == XE_EUDEBUG_CAP_NOT_SUPPORTED)
+ return -EPERM;
+
+ if (!enable && xe->eudebug.attached)
+ return -EBUSY;
+
+ if (enable == __xe_eudebug_is_enabled(xe))
+ return 0;
+
+ set_cap_state(xe, enable ?
+ XE_EUDEBUG_CAP_ENABLED : XE_EUDEBUG_CAP_DISABLED);
+
+ return 0;
+}
+
+static ssize_t enable_eudebug_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
+
+ return sysfs_emit(buf, "%u\n", xe_eudebug_is_enabled(xe));
+}
+
+static ssize_t enable_eudebug_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
+ bool enable;
+ int ret;
+
+ ret = kstrtobool(buf, &enable);
+ if (ret)
+ return ret;
+
+ ret = xe_eudebug_enable(xe, enable);
+ if (ret)
+ return ret;
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(enable_eudebug);
+
+static void xe_eudebug_sysfs_fini(void *arg)
+{
+ struct xe_device *xe = arg;
+ struct drm_device *dev = &xe->drm;
+
+ sysfs_remove_file(&dev->dev->kobj,
+ &dev_attr_enable_eudebug.attr);
+}
+
+static void xe_eudebug_lock_fini(struct drm_device *dev, void *__unused)
+{
+ mutex_destroy(&to_xe_device(dev)->eudebug.lock);
+}
+
+void xe_eudebug_init_early(struct xe_device *xe)
+{
+ /*
+ * Not drmm_mutex_init(), which can fail and leave the lock destroyed:
+ * the state is read under it from here on. Nothing can reach the
+ * device yet, so the state itself is set without it. Without the
+ * release action the lock is merely never destroyed, which only
+ * costs the mutex debug checks.
+ */
+ mutex_init(&xe->eudebug.lock);
+ if (drmm_add_action(&xe->drm, xe_eudebug_lock_fini, NULL))
+ drm_dbg(&xe->drm, "eudebug lock will not be destroyed\n");
+
+ xe->eudebug.cap_state = XE_EUDEBUG_CAP_DISABLED;
+}
+
+void xe_eudebug_init(struct xe_device *xe)
+{
+ struct drm_device *dev = &xe->drm;
+ int err;
+
+ /* early init failed */
+ if (xe->eudebug.cap_state == XE_EUDEBUG_CAP_NOT_SUPPORTED)
+ return;
+
+ err = devm_add_action_or_reset(dev->dev, xe_eudebug_sysfs_fini, xe);
+ if (err)
+ goto out_err;
+
+ /*
+ * Last: once the attribute exists it can enable us, and nothing may
+ * fail and overwrite cap_state after that. Removing a file that was
+ * never created is a no-op, so the remove action can go first.
+ */
+ err = sysfs_create_file(&dev->dev->kobj,
+ &dev_attr_enable_eudebug.attr);
+ if (err)
+ goto out_err;
+
+ return;
+
+out_err:
+ drm_warn(&xe->drm, "eudebug disabled, init fail: %d\n", err);
+
+ scoped_guard(mutex, &xe->eudebug.lock)
+ set_cap_state(xe, XE_EUDEBUG_CAP_NOT_SUPPORTED);
+}
+
+/**
+ * xe_eudebug_connect_ioctl - Connect to eudebug interface
+ * @dev : DRM device
+ * @data : ioctl data, (struct drm_xe_eudebug_connect)
+ * @file : DRM file
+ *
+ * Connect to the eudebug interface.
+ *
+ * Return: eudebug filedesriptor on success, negative error code on failure.
+ *
+ */
+int xe_eudebug_connect_ioctl(struct drm_device *dev,
+ void *data,
+ struct drm_file *file)
+{
+ struct xe_device *xe = to_xe_device(dev);
+ struct drm_xe_eudebug_connect * const param = data;
+
+ return xe_eudebug_connect(xe, file, param);
+}
diff --git a/drivers/gpu/drm/xe/xe_eudebug.h b/drivers/gpu/drm/xe/xe_eudebug.h
new file mode 100644
index 000000000000..91e604c39814
--- /dev/null
+++ b/drivers/gpu/drm/xe/xe_eudebug.h
@@ -0,0 +1,68 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023-2025 Intel Corporation
+ */
+
+#ifndef _XE_EUDEBUG_H_
+#define _XE_EUDEBUG_H_
+
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_device;
+struct drm_file;
+struct xe_device;
+struct xe_file;
+struct xe_vm;
+
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+
+#define XE_EUDEBUG_DBG_STR "eudbg: %llu:%lld (%d/%d) -> (%d): "
+
+#define __eu_print(d, func, fmt, ...) \
+ do { \
+ struct xe_eudebug *__pd = (d); \
+ func(&__pd->xe->drm, XE_EUDEBUG_DBG_STR fmt, \
+ __pd->session, \
+ atomic64_read(&__pd->events.seqno), \
+ current->pid, \
+ task_tgid_nr(current), \
+ __pd->target.pid, \
+ ##__VA_ARGS__); \
+ } while (0)
+
+#define eu_dbg(d, fmt, ...) __eu_print(d, drm_dbg, fmt, ##__VA_ARGS__)
+
+#define xe_eudebug_assert(d, ...) xe_assert((d)->xe, ##__VA_ARGS__)
+
+int xe_eudebug_connect_ioctl(struct drm_device *dev,
+ void *data,
+ struct drm_file *file);
+
+void xe_eudebug_init(struct xe_device *xe);
+void xe_eudebug_init_early(struct xe_device *xe);
+bool xe_eudebug_is_enabled(struct xe_device *xe);
+
+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);
+
+#else
+
+static inline int xe_eudebug_connect_ioctl(struct drm_device *dev,
+ void *data,
+ struct drm_file *file) { return
-EOPNOTSUPP; }
+
+static inline void xe_eudebug_init(struct xe_device *xe) { }
+static inline void xe_eudebug_init_early(struct xe_device *xe) { }
+static inline bool xe_eudebug_is_enabled(struct xe_device *xe) { return false;
}
+
+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) { }
+
+#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
new file mode 100644
index 000000000000..598abb249195
--- /dev/null
+++ b/drivers/gpu/drm/xe/xe_eudebug_types.h
@@ -0,0 +1,168 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023-2025 Intel Corporation
+ */
+
+#ifndef _XE_EUDEBUG_TYPES_H_
+#define _XE_EUDEBUG_TYPES_H_
+
+#include <linux/kfifo.h>
+#include <linux/kref.h>
+#include <linux/mutex.h>
+#include <linux/rhashtable.h>
+#include <linux/sizes.h>
+#include <linux/wait.h>
+#include <linux/xarray.h>
+
+#include <uapi/drm/xe_drm_eudebug.h>
+
+struct xe_device;
+
+/**
+ * enum xe_eudebug_cap_state - eudebug capability state
+ *
+ * @XE_EUDEBUG_CAP_NOT_SUPPORTED: eudebug feature support off
+ * @XE_EUDEBUG_CAP_DISABLED: eudebug feature supported but disabled
+ * @XE_EUDEBUG_CAP_ENABLED: eudebug enabled
+ */
+enum xe_eudebug_cap_state {
+ XE_EUDEBUG_CAP_NOT_SUPPORTED = 0,
+ XE_EUDEBUG_CAP_DISABLED,
+ XE_EUDEBUG_CAP_ENABLED,
+};
+
+#define XE_EUDEBUG_MAX_EVENT_TYPE DRM_XE_EUDEBUG_EVENT_VM
+
+/**
+ * struct xe_eudebug_handle - eudebug resource handle
+ */
+struct xe_eudebug_handle {
+ /** @key: key value in rhashtable <key:id> */
+ u64 key;
+
+ /** @id: opaque handle id for xarray <id:key> */
+ u32 id;
+
+ /** @rh_head: rhashtable head */
+ struct rhash_head rh_head;
+};
+
+/**
+ * struct xe_eudebug_resource - Resource map for one resource
+ */
+struct xe_eudebug_resource {
+ /** @xa: xarrays for <id->key> */
+ struct xarray xa;
+
+ /** @next_id: next id to try in @xa, protected by the @xa lock */
+ u32 next_id;
+
+ /** @rh: rhashtable for <key->id> */
+ struct rhashtable rh;
+};
+
+#define XE_EUDEBUG_RES_TYPE_VM 0
+#define XE_EUDEBUG_RES_TYPE_COUNT (XE_EUDEBUG_RES_TYPE_VM + 1)
+
+/**
+ * struct xe_eudebug - Top level struct for eudebug: the connection
+ */
+struct xe_eudebug {
+ /** @ref: kref counter for this struct */
+ struct kref ref;
+
+ /**
+ * @lock: guards all the mutable state of this struct: the target
+ * link and error, the resource maps, the event queue with its
+ * buffers and reader, the ack tree and the async disconnect error.
+ *
+ * Nests inside the vm reservation of a vm bind and inside
+ * xe->eudebug.lock, and outside the user fence and xe_file eudebug
+ * spinlocks. Never touch user memory, disconnect, or drop what may
+ * be the last reference to this struct or to a user fence while
+ * holding it.
+ */
+ struct mutex lock;
+
+ /** @target: debug target specifics */
+ struct {
+ /**
+ * @target.xef: the target xe_file that we are debugging, NULL
+ * once detached. Written under both xe->eudebug.lock and
+ * @lock, so either is enough to read it.
+ */
+ struct xe_file *xef;
+
+ /** @target.pid: pid of target */
+ pid_t pid;
+
+ /** @target.err: error code on disconnect */
+ int err;
+
+ /** @target.res: resource maps for all types */
+ struct xe_eudebug_resource res[XE_EUDEBUG_RES_TYPE_COUNT];
+ } target;
+
+ /** @xe: the parent device we are serving */
+ struct xe_device *xe;
+
+ /** @session: session number for this connection (for logs) */
+ u64 session;
+
+ /** @events: kfifo queue of to-be-delivered events */
+ struct {
+#define XE_EUDEBUG_EVENT_MAX_SIZE SZ_64K
+ /**
+ * @events.pending: pending event bounce buffer, preallocated.
+ * Owned by the reader marked by @events.reader_active, which
+ * copies it out without @lock while @events.pending_occupied is
+ * set.
+ */
+ struct drm_xe_eudebug_event *pending;
+
+ /**
+ * @events.pending_occupied: @events.pending holds an event
+ * that was popped from the fifo but not yet delivered
+ */
+ bool pending_occupied;
+
+ /** @events.reader_active: a read is in progress, only one at a
time */
+ bool reader_active;
+
+ /** @events.staging: write side staging buffer, preallocated */
+ struct drm_xe_eudebug_event *staging;
+
+ /** @events.fifo: queue of events pending */
+ struct kfifo fifo;
+
+ /** @events.fifo_buf: memory for the fifo */
+ void *fifo_buf;
+#define XE_EUDEBUG_FIFO_SIZE SZ_4M
+
+ /** @events.write_done: waitqueue for signalling write to fifo
*/
+ wait_queue_head_t write_done;
+
+ /**
+ * @events.seqno: seqno counter to stamp events for fifo
+ *
+ * Stamped when an event is generated, which is not the same
+ * moment it reaches the fifo, so concurrent producers can
+ * take N and N+1 and then queue them the other way round.
+ *
+ * That is deliberate. The number is the client timeline the
+ * debugger reconstructs, not a delivery order. Making the two
+ * agree would mean holding @lock across event generation,
+ * serialising the producers and losing the very ordering the
+ * debugger is after.
+ *
+ * See drm_xe_eudebug_event.seqno for what this promises
+ * userspace.
+ *
+ * Atomic, so that it can also be read without @lock, as
+ * __eu_print() does for every debug message.
+ */
+ atomic64_t seqno;
+ } events;
+};
+
+#endif /* _XE_EUDEBUG_TYPES_H_ */
diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
index dd855876f571..b9b851adc9bf 100644
--- a/drivers/gpu/drm/xe/xe_vm.c
+++ b/drivers/gpu/drm/xe/xe_vm.c
@@ -28,6 +28,7 @@
#include "xe_cpu_bind.h"
#include "xe_device.h"
#include "xe_drm_client.h"
+#include "xe_eudebug.h"
#include "xe_exec_queue.h"
#include "xe_gt.h"
#include "xe_migrate.h"
@@ -2226,10 +2227,18 @@ int xe_vm_create_ioctl(struct drm_device *dev, void
*data,
args->reserved[0] = xe_bo_main_addr(vm->pt_root[0]->bo, XE_PAGE_SIZE);
#endif
+ /*
+ * Announce to the debugger before the id is published, as from that
+ * point on a concurrent destroy can race us to the resource map.
+ */
+ xe_eudebug_vm_create(xef, vm);
+
/* user id alloc must always be last in ioctl to prevent UAF */
err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
- if (err)
+ if (err) {
+ xe_eudebug_vm_destroy(xef, vm);
goto err_close_and_put;
+ }
args->vm_id = id;
@@ -2264,8 +2273,10 @@ int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
xa_erase(&xef->vm.xa, args->vm_id);
mutex_unlock(&xef->vm.lock);
- if (!err)
+ if (!err) {
+ xe_eudebug_vm_destroy(xef, vm);
xe_vm_close_and_put(vm);
+ }
return err;
}
diff --git a/include/uapi/drm/xe_drm.h b/include/uapi/drm/xe_drm.h
index 578f0c1eb43e..942a7fe81f4b 100644
--- a/include/uapi/drm/xe_drm.h
+++ b/include/uapi/drm/xe_drm.h
@@ -85,6 +85,7 @@ extern "C" {
* - &DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS
* - &DRM_IOCTL_XE_EXEC_QUEUE_SET_PROPERTY
* - &DRM_IOCTL_XE_VM_GET_PROPERTY
+ * - &DRM_IOCTL_XE_EUDEBUG_CONNECT
*/
/*
@@ -110,6 +111,7 @@ extern "C" {
#define DRM_XE_VM_QUERY_MEM_RANGE_ATTRS 0x0d
#define DRM_XE_EXEC_QUEUE_SET_PROPERTY 0x0e
#define DRM_XE_VM_GET_PROPERTY 0x0f
+#define DRM_XE_EUDEBUG_CONNECT 0x10
/* Must be kept compact -- no holes */
@@ -129,6 +131,7 @@ extern "C" {
#define DRM_IOCTL_XE_VM_QUERY_MEM_RANGE_ATTRS DRM_IOWR(DRM_COMMAND_BASE +
DRM_XE_VM_QUERY_MEM_RANGE_ATTRS, struct drm_xe_vm_query_mem_range_attr)
#define DRM_IOCTL_XE_EXEC_QUEUE_SET_PROPERTY DRM_IOW(DRM_COMMAND_BASE +
DRM_XE_EXEC_QUEUE_SET_PROPERTY, struct drm_xe_exec_queue_set_property)
#define DRM_IOCTL_XE_VM_GET_PROPERTY DRM_IOWR(DRM_COMMAND_BASE +
DRM_XE_VM_GET_PROPERTY, struct drm_xe_vm_get_property)
+#define DRM_IOCTL_XE_EUDEBUG_CONNECT DRM_IOW(DRM_COMMAND_BASE +
DRM_XE_EUDEBUG_CONNECT, struct drm_xe_eudebug_connect)
/**
* DOC: Xe IOCTL Extensions
@@ -2634,6 +2637,27 @@ enum drm_xe_ras_error_component {
[DRM_XE_RAS_ERR_COMP_FABRIC] = "fabric", \
}
+/**
+ * struct drm_xe_eudebug_connect - Input of &DRM_IOCTL_XE_EUDEBUG_CONNECT
+ *
+ * This structure is used to connect to an eudebug interface of target drm
file.
+ */
+struct drm_xe_eudebug_connect {
+ /** @extensions: Pointer to the first extension struct, if any */
+ __u64 extensions;
+
+ /** @fd: Debug target DRM client fd */
+ __u64 fd;
+
+ /** @flags: Flags, MBZ */
+ __u64 flags;
+
+ /** @reserved: MBZ */
+ __u64 reserved;
+};
+
+#include "xe_drm_eudebug.h"
+
#if defined(__cplusplus)
}
#endif
diff --git a/include/uapi/drm/xe_drm_eudebug.h
b/include/uapi/drm/xe_drm_eudebug.h
new file mode 100644
index 000000000000..63f4a6142a1a
--- /dev/null
+++ b/include/uapi/drm/xe_drm_eudebug.h
@@ -0,0 +1,131 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
Should we refresh this?
+ */
+
+#ifndef _UAPI_XE_DRM_EUDEBUG_H_
+#define _UAPI_XE_DRM_EUDEBUG_H_
+
+#include "drm.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+ * DOC: DRM_XE_EUDEBUG_IOCTL_READ_EVENT
+ *
+ * Receive one event from the connection returned by
+ * &DRM_IOCTL_XE_EUDEBUG_CONNECT. The argument is a pointer to a
+ * &struct drm_xe_eudebug_event filled in as described there.
+ *
+ * A connection starts out blocking, and then waits up to five seconds for
+ * an event to arrive; O_NONBLOCK can be set on it with fcntl(). Only one
+ * reader at a time is allowed on a connection.
+ *
+ * Return: 0 on success. Negative error code on failure:
+ *
+ * - -EMSGSIZE if the pending event is larger than the supplied len. len is
+ * updated with the size needed and the event stays queued.
+ * - -ETIMEDOUT if the blocking wait expired with no event.
+ * - -EAGAIN if O_NONBLOCK was set and no event was queued.
+ * - -EBUSY if another thread is already reading on this connection.
+ * - -ENOTCONN if the debug target is gone.
+ *
+ * Detaching discards whatever is still queued: the events describe resources
+ * of a client that no longer exists, and the handles in them can no longer be
+ * resolved. A debugger that gets -ENOTCONN, or EPOLLHUP from poll(), should
+ * treat the event stream as having ended at an unspecified point rather than
+ * assume it saw everything the target did.
+ */
+#define DRM_XE_EUDEBUG_IOCTL_READ_EVENT _IOWR('j', 0x40, struct
drm_xe_eudebug_event)
+
+/**
+ * struct drm_xe_eudebug_event - Base type of event delivered by xe_eudebug.
+ *
+ * Base event for xe_eudebug interface.
+ *
+ * For receiving events :c:member:`drm_xe_eudebug_event.type` has to
+ * be DRM_XE_EUDEBUG_EVENT_READ. On return, this is set to the type
+ * of event received. :c:member:`drm_xe_eudebug_event.len` has to be
+ * set to maximum size that can be received. On return, len will be set
+ * to the event size. If the pending event was larger than this size,
+ * -EMSGSIZE is returned instead of 0 and the caller should retry with a larger
+ * allocated receive length.
+ *
+ * :c:member:`drm_xe_eudebug_event.seqno` can be used to form a timeline
+ * as event delivery order does not guarantee event creation
+ * order. Must be zero in the read request.
+ *
+ * :c:member:`drm_xe_eudebug_event.flags` will indicate if a resource was
+ * created or destroyed. Must be zero in the read request.
+ *
+ * %DRM_XE_EUDEBUG_EVENT_NONE and %DRM_XE_EUDEBUG_EVENT_READ are never
+ * delivered: the first is unused and the second is the request type for
+ * the read ioctl.
+ *
+ * :c:member:`drm_xe_eudebug_event.len` is authoritative for the event that
+ * was read, and events may grow fields in later kernels. Step to the next
+ * event and skip a type this debugger does not know by @len rather than by
+ * the size of the matching struct in the header it was built against.
+ */
+struct drm_xe_eudebug_event {
+ /** @len: Length */
+ __u32 len;
+
+ /** @type: Type */
+ __u16 type;
+#define DRM_XE_EUDEBUG_EVENT_NONE 0
+#define DRM_XE_EUDEBUG_EVENT_READ 1
+#define DRM_XE_EUDEBUG_EVENT_VM 2
Could maybe add some more explicit kernel-doc for each of these event
types? At least for DRM_XE_EUDEBUG_EVENT_VM there is no explicit mention
of what that is.
DRM_XE_EUDEBUG_EVENT_NONE - ...
DRM_XE_EUDEBUG_EVENT_READ - ...
DRM_XE_EUDEBUG_EVENT_VM - ...
Also, for DRM_XE_EUDEBUG_EVENT_VM maybe link that to
drm_xe_eudebug_event_vm below to make the relationship clear?
+
+ /** @flags: Flags */
+ __u16 flags;
+#define DRM_XE_EUDEBUG_EVENT_CREATE (1 << 0)
+#define DRM_XE_EUDEBUG_EVENT_DESTROY (1 << 1)
Should these be something like DRM_XE_EUDEBUG_EVENT_*FLAG*_CREATE etc ?
The namespacing here makes these look like exactly like event types
DRM_XE_EUDEBUG_EVENT_VM etc. but these are very different.
Also, maybe some kernel-doc for these?
+
+ /**
+ * @seqno: Sequence number, for the debugger to construct a timeline.
+ *
+ * Allocated per connection and monotonically increasing, taken at the
+ * point the event is generated rather than when it is queued.
+ *
+ * Event generation is asynchronous: threads in the target produce
+ * events concurrently, so events are not guaranteed to arrive in
+ * seqno order. The numbering may also contain gaps, as a number can
+ * be allocated for an event that is never queued. Neither a lower
+ * seqno arriving after a higher one nor a gap means an event was
+ * lost; if an event cannot be queued the connection is disconnected
+ * instead of silently dropping it.
+ *
+ * Use this to order and correlate the events that have been read,
+ * which is what timeline means here. Do not use it as a delivery
+ * order guarantee.
+ */
+ __u64 seqno;
+
+ /** @reserved: Reserved field, must be zero. */
+ __u64 reserved;
+};
+
+/**
+ * struct drm_xe_eudebug_event_vm - VM event
+ *
+ * VM event is delivered when vm is created or destroyed. Only vms created
+ * with %DRM_XE_VM_CREATE_FLAG_FAULT_MODE, the ones debuggable exec queues
+ * can run on, are reported; other vms of the target are not visible to the
+ * debugger.
+ */
+struct drm_xe_eudebug_event_vm {
+ /** @base: base event */
+ struct drm_xe_eudebug_event base;
+
+ /** @vm_handle: unique handle for vm */
+ __u64 vm_handle;
+};
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* _UAPI_XE_DRM_EUDEBUG_H_ */