Skip to contentSkip to conversation

GPU debug support (eudebug) v2

PATCH In review Mika Kuoppala 6 participants 40 messages Oct 1, 2024 – Oct 28, 2024

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

cover GPU debug support (eudebug) v2

Hi,

This is a continuation of the first submission for GPU debug support:

https://lists.freedesktop.org/archives/intel-xe/2024-July/043605.html

The most significant changes are:
1. Placing the code behind Kconfig switch

2. Using rw_semaphore to allow resource discovery to run without individual resource locks.

The page fault handling RFC will be based on this series but will be posted separately.

Thanks to all contributors for their feedback on v1 and to patch authors for their fixes. I hope I haven't missed anything.

Mika

Andrzej Hajda (1):
drm/xe/eudebug: implement userptr_vma access

Christoph Manszewski (3):
drm/xe/eudebug: Add vm bind and vm bind ops drm/xe/eudebug: Dynamically toggle debugger functionality drm/xe/eudebug_test: Introduce xe_eudebug wa kunit test

Dominik Grzegorzek (8):
drm/xe/eudebug: Introduce exec_queue events drm/xe/eudebug: hw enablement for eudebug drm/xe: Add EUDEBUG_ENABLE exec queue property drm/xe/eudebug: Introduce per device attention scan worker drm/xe/eudebug: Introduce EU control interface drm/xe: Debug metadata create/destroy ioctls drm/xe: Attach debug metadata to vma drm/xe/eudebug: Add debug metadata support for xe_eudebug

Mika Kuoppala (6):
ptrace: export ptrace_may_access drm/xe/eudebug: Introduce eudebug support drm/xe/eudebug: Introduce discovery for resources drm/xe/eudebug: Add UFENCE events with acks drm/xe/eudebug: vm open/pread/pwrite drm/xe/eudebug: Implement vm_bind_op discovery

 drivers/gpu/drm/xe/Kconfig                   |   10 +
 drivers/gpu/drm/xe/Makefile                  |    4 +
 drivers/gpu/drm/xe/regs/xe_engine_regs.h     |    7 +
 drivers/gpu/drm/xe/regs/xe_gt_regs.h         |   43 +
 drivers/gpu/drm/xe/tests/xe_eudebug.c        |  175 +
 drivers/gpu/drm/xe/tests/xe_live_test_mod.c  |    5 +
 drivers/gpu/drm/xe/xe_debug_metadata.c       |  233 ++
 drivers/gpu/drm/xe/xe_debug_metadata.h       |   98 +
 drivers/gpu/drm/xe/xe_debug_metadata_types.h |   28 +
 drivers/gpu/drm/xe/xe_device.c               |   26 +-
 drivers/gpu/drm/xe/xe_device.h               |   36 +
 drivers/gpu/drm/xe/xe_device_types.h         |   47 +
 drivers/gpu/drm/xe/xe_eudebug.c              | 3891 ++++++++++++++++++
 drivers/gpu/drm/xe/xe_eudebug.h              |   93 +
 drivers/gpu/drm/xe/xe_eudebug_types.h        |  328 ++
 drivers/gpu/drm/xe/xe_exec.c                 |    2 +-
 drivers/gpu/drm/xe/xe_exec_queue.c           |   56 +-
 drivers/gpu/drm/xe/xe_exec_queue.h           |    2 +
 drivers/gpu/drm/xe/xe_exec_queue_types.h     |    7 +
 drivers/gpu/drm/xe/xe_execlist.c             |    2 +-
 drivers/gpu/drm/xe/xe_gt_debug.c             |  148 +
 drivers/gpu/drm/xe/xe_gt_debug.h             |   27 +
 drivers/gpu/drm/xe/xe_hw_engine.c            |    1 +
 drivers/gpu/drm/xe/xe_lrc.c                  |   16 +-
 drivers/gpu/drm/xe/xe_lrc.h                  |    4 +-
 drivers/gpu/drm/xe/xe_reg_sr.c               |   21 +-
 drivers/gpu/drm/xe/xe_reg_sr.h               |    4 +-
 drivers/gpu/drm/xe/xe_rtp.c                  |    2 +-
 drivers/gpu/drm/xe/xe_sync.c                 |   45 +-
 drivers/gpu/drm/xe/xe_sync.h                 |    8 +-
 drivers/gpu/drm/xe/xe_sync_types.h           |   28 +-
 drivers/gpu/drm/xe/xe_vm.c                   |  173 +-
 drivers/gpu/drm/xe/xe_vm.h                   |    3 +
 drivers/gpu/drm/xe/xe_vm_types.h             |   40 +
 drivers/gpu/drm/xe/xe_wa_oob.rules           |    2 +
 include/uapi/drm/xe_drm.h                    |   96 +-
 include/uapi/drm/xe_drm_eudebug.h            |  225 +
 kernel/ptrace.c                              |    1 +
 38 files changed, 5887 insertions(+), 50 deletions(-)
 create mode 100644 drivers/gpu/drm/xe/tests/xe_eudebug.c
 create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.c
 create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.h
 create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata_types.h
 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 drivers/gpu/drm/xe/xe_gt_debug.c
 create mode 100644 drivers/gpu/drm/xe/xe_gt_debug.h
 create mode 100644 include/uapi/drm/xe_drm_eudebug.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/18 ptrace: export ptrace_may_access

xe driver would like to allow fine grained access control for GDB debugger using ptrace. Without this export, the only option would be to check for CAP_SYS_ADMIN.

The check intended for an ioctl to attach a GPU debugger is similar to the ptrace use case: allow a calling process to manipulate a target process if it has the necessary capabilities or the same permissions, as described in Documentation/process/adding-syscalls.rst.

Export ptrace_may_access function to allow GPU debugger to have identical access control for debugger(s)
as a CPU debugger.

v2: proper commit message (Lucas)

Cc Oleg Nesterov <[email protected]>
Cc Dave Airlie <[email protected]>
CC Lucas De Marchi <[email protected]>
Cc Matthew Brost <[email protected]>
CC Andi Shyti <[email protected]>
Cc Joonas Lahtinen <[email protected]>
CC Maciej Patelczyk <[email protected]>
Cc Dominik Grzegorzek <[email protected]>
---
 kernel/ptrace.c | 1 +
 1 file changed, 1 insertion(+)
kernel/ptrace.c +1 -0
index d5f89f9ef29f..86be1805ebd8 100644
@@ -354,6 +354,7 @@ bool ptrace_may_access(struct task_struct *task, unsigned int mode)
 	task_unlock(task);
 	return !err;
 }
+EXPORT_SYMBOL_GPL(ptrace_may_access);
 
 static int check_ptrace_options(unsigned long data)
 {
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/18 drm/xe/eudebug: Introduce eudebug support

In reply to Mika Kuoppala

With eudebug event interface, user space debugger process (like gdb) is able to keep track of resources created by another process (debuggee using drm/xe) and act upon these resources.

For example, debugger can find a client vm which contains isa/elf for a particular shader/eu-kernel and then inspect and modify it (for example installing a breakpoint).

Debugger first opens a connection to xe with a drm ioctl specifying target pid to connect. This returns an anon fd handle that can then be used to listen for events with dedicated ioctl.

This patch introduces eudebug connection and event queuing, adding client create/destroy and vm create/destroy events as a baseline. More events for full debugger operation are needed and those will be introduced in follow up patches.

The resource tracking parts are inspired by the work of Maciej Patelczyk on resource handling for i915. Chris Wilson suggested improvement of two ways mapping which makes it easy to use resource map as a definitive bookkeep of what resources are played to debugger in the discovery phase (on 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)

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]>
---
 drivers/gpu/drm/xe/Kconfig            |   10 +
 drivers/gpu/drm/xe/Makefile           |    2 +
 drivers/gpu/drm/xe/xe_device.c        |   11 +
 drivers/gpu/drm/xe/xe_device_types.h  |   28 +
 drivers/gpu/drm/xe/xe_eudebug.c       | 1114 +++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_eudebug.h       |   46 +
 drivers/gpu/drm/xe/xe_eudebug_types.h |  169 ++++
 drivers/gpu/drm/xe/xe_vm.c            |    7 +-
 include/uapi/drm/xe_drm.h             |   21 +
 include/uapi/drm/xe_drm_eudebug.h     |   56 ++
 10 files changed, 1463 insertions(+), 1 deletion(-)
 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
drivers/gpu/drm/xe/Kconfig +10 -0
index 7bbe46a98ff1..23f34e8e3151 100644
@@ -85,6 +85,16 @@ config DRM_XE_FORCE_PROBE
 
 	  Use "!*" to block the probe of the driver for all known devices.
 
+config DRM_XE_EUDEBUG
+	bool "Enable gdb debugger support (eudebug)"
+	depends on DRM_XE
+	default y
+	help
+	  Choose this option if you want to add support for debugger (gdb) to
+	  attach into process using Xe and debug the gpu/gpgpu programs.
+	  With debugger support, Xe will provide interface for a debugger to
+	  process to track, inspect and modify resources.
+
 menu "drm/Xe Debugging"
 depends on DRM_XE
 depends on EXPERT
drivers/gpu/drm/xe/Makefile +2 -0
index 8f1c5c329f79..a3cd467cdaa6 100644
@@ -113,6 +113,8 @@ xe-y += xe_bb.o \
 	xe_wa.o \
 	xe_wopcm.o
 
+xe-$(CONFIG_DRM_XE_EUDEBUG) += xe_eudebug.o
+
 xe-$(CONFIG_HMM_MIRROR) += xe_hmm.o
 
 # graphics hardware monitoring (HWMON) support
drivers/gpu/drm/xe/xe_device.c +11 -0
index 8e9b551c7033..5615e2c23bf6 100644
@@ -27,6 +27,7 @@
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
 #include "xe_drv.h"
+#include "xe_eudebug.h"
 #include "xe_exec.h"
 #include "xe_exec_queue.h"
 #include "xe_force_wake.h"
@@ -101,6 +102,8 @@ static int xe_file_open(struct drm_device *dev, struct drm_file *file)
 		put_task_struct(task);
 	}
 
+	xe_eudebug_file_open(xef);
+
 	return 0;
 }
 
@@ -159,6 +162,8 @@ static void xe_file_close(struct drm_device *dev, struct drm_file *file)
 
 	xe_pm_runtime_get(xe);
 
+	xe_eudebug_file_close(xef);
+
 	/*
 	 * No need for exec_queue.lock here as there is no contention for it
 	 * when FD is closing as IOCTLs presumably can't be modifying the
@@ -197,6 +202,7 @@ static const struct drm_ioctl_desc xe_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(XE_WAIT_USER_FENCE, xe_wait_user_fence_ioctl,
 			  DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(XE_EUDEBUG_CONNECT, xe_eudebug_connect_ioctl, DRM_RENDER_ALLOW),
 };
 
 static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
@@ -286,6 +292,8 @@ static void xe_device_destroy(struct drm_device *dev, void *dummy)
 {
 	struct xe_device *xe = to_xe_device(dev);
 
+	xe_eudebug_fini(xe);
+
 	if (xe->preempt_fence_wq)
 		destroy_workqueue(xe->preempt_fence_wq);
 
@@ -358,7 +366,10 @@ struct xe_device *xe_device_create(struct pci_dev *pdev,
 	INIT_LIST_HEAD(&xe->pinned.external_vram);
 	INIT_LIST_HEAD(&xe->pinned.evicted);
 
+	xe_eudebug_init(xe);
+
 	xe->preempt_fence_wq = alloc_ordered_workqueue("xe-preempt-fence-wq", 0);
+
 	xe->ordered_wq = alloc_ordered_workqueue("xe-ordered-wq", 0);
 	xe->unordered_wq = alloc_workqueue("xe-unordered-wq", 0, 0);
 	xe->destroy_wq = alloc_workqueue("xe-destroy-wq", 0, 0);
drivers/gpu/drm/xe/xe_device_types.h +28 -0
index 85bede4dd646..cb4b52888a4b 100644
@@ -386,6 +386,11 @@ struct xe_device {
 
 		/** @clients.count: number of drm clients */
 		u64 count;
+
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+		/** @xa: client list for eudebug discovery */
+		struct list_head list;
+#endif
 	} clients;
 
 	/** @usm: unified memory state */
@@ -524,6 +529,22 @@ struct xe_device {
 	u8 vm_inject_error_position;
 #endif
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+	/** @debugger connection list and globals for device */
+	struct {
+		/** @lock: protects the list of connections */
+		spinlock_t lock;
+		/** @list: list of connections, aka debuggers */
+		struct list_head list;
+
+		/** @session_count: session counter to track connections */
+		u64 session_count;
+
+		/** @available: is the debugging functionality available */
+		bool available;
+	} eudebug;
+#endif
+
 	/* private: */
 
 #if IS_ENABLED(CONFIG_DRM_XE_DISPLAY)
@@ -636,6 +657,13 @@ struct xe_file {
 
 	/** @refcount: ref count of this xe file */
 	struct kref refcount;
+
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+	struct {
+		/** @client_link: list entry in xe_device.clients.list */
+		struct list_head client_link;
+	} eudebug;
+#endif
 };
 
 #endif
drivers/gpu/drm/xe/xe_eudebug.c new file +1114 -0

1,114 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug.h new file +46 -0

46 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug_types.h new file +169 -0

169 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.c +6 -1
index ce9dca4d4e87..9701a7efe257 100644
@@ -26,6 +26,7 @@
 #include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_drm_client.h"
+#include "xe_eudebug.h"
 #include "xe_exec_queue.h"
 #include "xe_gt_pagefault.h"
 #include "xe_gt_tlb_invalidation.h"
@@ -1796,6 +1797,8 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
 
 	args->vm_id = id;
 
+	xe_eudebug_vm_create(xef, vm);
+
 	return 0;
 
 err_close_and_put:
@@ -1827,8 +1830,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;
 }
include/uapi/drm/xe_drm.h +21 -0
index b6fbe4988f2e..7e66e9117f49 100644
@@ -102,6 +102,7 @@ extern "C" {
 #define DRM_XE_EXEC			0x09
 #define DRM_XE_WAIT_USER_FENCE		0x0a
 #define DRM_XE_OBSERVATION		0x0b
+#define DRM_XE_EUDEBUG_CONNECT		0x0c
 
 /* Must be kept compact -- no holes */
 
@@ -117,6 +118,7 @@ extern "C" {
 #define DRM_IOCTL_XE_EXEC			DRM_IOW(DRM_COMMAND_BASE + DRM_XE_EXEC, struct drm_xe_exec)
 #define DRM_IOCTL_XE_WAIT_USER_FENCE		DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_WAIT_USER_FENCE, struct drm_xe_wait_user_fence)
 #define DRM_IOCTL_XE_OBSERVATION		DRM_IOW(DRM_COMMAND_BASE + DRM_XE_OBSERVATION, struct drm_xe_observation_param)
+#define DRM_IOCTL_XE_EUDEBUG_CONNECT		DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_EUDEBUG_CONNECT, struct drm_xe_eudebug_connect)
 
 /**
  * DOC: Xe IOCTL Extensions
@@ -1694,6 +1696,25 @@ struct drm_xe_oa_stream_info {
 	__u64 reserved[3];
 };
 
+/*
+ * Debugger ABI (ioctl and events) Version History:
+ * 0 - No debugger available
+ * 1 - Initial version
+ */
+#define DRM_XE_EUDEBUG_VERSION 1
+
+struct drm_xe_eudebug_connect {
+	/** @extensions: Pointer to the first extension struct, if any */
+	__u64 extensions;
+
+	__u64 pid; /* input: Target process ID */
+	__u32 flags; /* MBZ */
+
+	__u32 version; /* output: current ABI (ioctl / events) version */
+};
+
+#include "xe_drm_eudebug.h"
+
 #if defined(__cplusplus)
 }
 #endif
include/uapi/drm/xe_drm_eudebug.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/18 drm/xe/eudebug: Introduce discovery for resources

In reply to Mika Kuoppala

Debugger connection can happen way after the client has created and destroyed arbitrary number of resources.

We need to playback all currently existing resources for the debugger. The client is held until this so called discovery process, executed by workqueue, is complete.

This patch is based on discovery work by Maciej Patelczyk for i915 driver.

v2: - use rw_semaphore to block drm_ioctls during discovery (Matthew)
- only lock according to ioctl at play (Dominik)

---
 drivers/gpu/drm/xe/xe_device.c        |  10 +-
 drivers/gpu/drm/xe/xe_device.h        |  34 +++++++
 drivers/gpu/drm/xe/xe_device_types.h  |   6 ++
 drivers/gpu/drm/xe/xe_eudebug.c       | 135 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug_types.h |   7 ++
 5 files changed, 185 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_device.c +8 -2
index 5615e2c23bf6..ec5eedbbf320 100644
@@ -215,8 +215,11 @@ static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
@@ -233,8 +236,11 @@ static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned lo
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_compat_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
drivers/gpu/drm/xe/xe_device.h +34 -0
index 4c3f0ebe78a9..b2fc85b1d26e 100644
@@ -7,6 +7,7 @@
 #define _XE_DEVICE_H_
 
 #include <drm/drm_util.h>
+#include <drm/drm_ioctl.h>
 
 #include "xe_device_types.h"
 #include "xe_gt_types.h"
@@ -191,4 +192,37 @@ void xe_device_declare_wedged(struct xe_device *xe);
 struct xe_file *xe_file_get(struct xe_file *xef);
 void xe_file_put(struct xe_file *xef);
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+static inline int xe_eudebug_needs_lock(const unsigned int cmd)
+{
+	const unsigned int xe_cmd = DRM_IOCTL_NR(cmd) - DRM_COMMAND_BASE;
+
+	switch (xe_cmd) {
+	case DRM_XE_VM_CREATE:
+	case DRM_XE_VM_DESTROY:
+	case DRM_XE_VM_BIND:
+	case DRM_XE_EXEC_QUEUE_CREATE:
+	case DRM_XE_EXEC_QUEUE_DESTROY:
+	case DRM_XE_EUDEBUG_CONNECT:
+		return 1;
+	}
+
+	return 0;
+}
+
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		down_read(&xe->eudebug.discovery_lock);
+}
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		up_read(&xe->eudebug.discovery_lock);
+}
+#else
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd) { }
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd) { }
+#endif /* CONFIG_DRM_XE_EUDEBUG */
+
 #endif
drivers/gpu/drm/xe/xe_device_types.h +6 -0
index cb4b52888a4b..54ceeee7cf75 100644
@@ -542,6 +542,12 @@ struct xe_device {
 
 		/** @available: is the debugging functionality available */
 		bool available;
+
+		/** @ordered_wq: used to discovery */
+		struct workqueue_struct *ordered_wq;
+
+		/** discovery_lock: used for discovery to block xe ioctls */
+		struct rw_semaphore discovery_lock;
 	} eudebug;
 #endif
 
drivers/gpu/drm/xe/xe_eudebug.c +130 -5

219 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug_types.h +7 -0
index a5185f18f640..080a821db3e4 100644
@@ -19,6 +19,7 @@
 struct xe_device;
 struct task_struct;
 struct xe_eudebug_event;
+struct workqueue_struct;
 
 #define CONFIG_DRM_XE_DEBUGGER_EVENT_QUEUE_SIZE 64
 
@@ -96,6 +97,12 @@ struct xe_eudebug {
 	/** @session: session number for this connection (for logs) */
 	u64 session;
 
+	/** @discovery: completion to wait for discovery */
+	struct completion discovery;
+
+	/** @discovery_work: worker to discover resources for target_task */
+	struct work_struct discovery_work;
+
 	/** @events: kfifo queue of to-be-delivered events */
 	struct {
 		/** @lock: guards access to fifo */
Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH 03/18] drm/xe/eudebug: Introduce discovery for resources

Quoted 234 lines · Mika Kuoppala

Debugger connection can happen way after the client has created and destroyed arbitrary number of resources.

We need to playback all currently existing resources for the debugger. The client is held until this so called discovery process, executed by workqueue, is complete.

This patch is based on discovery work by Maciej Patelczyk for i915 driver.

v2: - use rw_semaphore to block drm_ioctls during discovery (Matthew)
- only lock according to ioctl at play (Dominik)

---
 drivers/gpu/drm/xe/xe_device.c        |  10 +-
 drivers/gpu/drm/xe/xe_device.h        |  34 +++++++
 drivers/gpu/drm/xe/xe_device_types.h  |   6 ++
 drivers/gpu/drm/xe/xe_eudebug.c       | 135 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug_types.h |   7 ++
 5 files changed, 185 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_device.c +8 -2
index 5615e2c23bf6..ec5eedbbf320 100644
@@ -215,8 +215,11 @@ static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
@@ -233,8 +236,11 @@ static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned lo
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_compat_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
drivers/gpu/drm/xe/xe_device.h +34 -0
index 4c3f0ebe78a9..b2fc85b1d26e 100644
@@ -7,6 +7,7 @@
 #define _XE_DEVICE_H_
 
 #include <drm/drm_util.h>
+#include <drm/drm_ioctl.h>
 
 #include "xe_device_types.h"
 #include "xe_gt_types.h"
@@ -191,4 +192,37 @@ void xe_device_declare_wedged(struct xe_device *xe);
 struct xe_file *xe_file_get(struct xe_file *xef);
 void xe_file_put(struct xe_file *xef);
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+static inline int xe_eudebug_needs_lock(const unsigned int cmd)
+{
+	const unsigned int xe_cmd = DRM_IOCTL_NR(cmd) - DRM_COMMAND_BASE;
+
+	switch (xe_cmd) {
+	case DRM_XE_VM_CREATE:
+	case DRM_XE_VM_DESTROY:
+	case DRM_XE_VM_BIND:
+	case DRM_XE_EXEC_QUEUE_CREATE:
+	case DRM_XE_EXEC_QUEUE_DESTROY:
+	case DRM_XE_EUDEBUG_CONNECT:
+		return 1;
+	}
+
+	return 0;
+}
+
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		down_read(&xe->eudebug.discovery_lock);
+}
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		up_read(&xe->eudebug.discovery_lock);
+}
+#else
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd) { }
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd) { }
+#endif /* CONFIG_DRM_XE_EUDEBUG */
+
 #endif
drivers/gpu/drm/xe/xe_device_types.h +6 -0
index cb4b52888a4b..54ceeee7cf75 100644
@@ -542,6 +542,12 @@ struct xe_device {
 
 		/** @available: is the debugging functionality available */
 		bool available;
+
+		/** @ordered_wq: used to discovery */
+		struct workqueue_struct *ordered_wq;
+
+		/** discovery_lock: used for discovery to block xe ioctls */
+		struct rw_semaphore discovery_lock;
 	} eudebug;
 #endif
 
drivers/gpu/drm/xe/xe_eudebug.c +38 -2
index ea0cfd7697aa..c294e2c6152b 100644
@@ -299,6 +299,8 @@ static bool xe_eudebug_detach(struct xe_device *xe,
 	}
 	spin_unlock(&d->connection.lock);
 
+	flush_work(&d->discovery_work);
+
 	if (!detached)
 		return false;
 
@@ -409,7 +411,7 @@ static struct task_struct *find_task_get(struct xe_file *xef)
 }
 
 static struct xe_eudebug *
-xe_eudebug_get(struct xe_file *xef)
+_xe_eudebug_get(struct xe_file *xef)
 {
 	struct task_struct *task;
 	struct xe_eudebug *d;
@@ -433,6 +435,24 @@ xe_eudebug_get(struct xe_file *xef)
 	return d;
 }
 
+static struct xe_eudebug *
+xe_eudebug_get(struct xe_file *xef)
+{
+	struct xe_eudebug *d;
+
+	lockdep_assert_held(&xef->xe->eudebug.discovery_lock);
+
+	d = _xe_eudebug_get(xef);
+	if (d) {
+		if (!completion_done(&d->discovery)) {
+			xe_eudebug_put(d);
+			d = NULL;
+		}
+	}
+
+	return d;
+}
+
 static int xe_eudebug_queue_event(struct xe_eudebug *d,
 				  struct xe_eudebug_event *event)
 {
@@ -810,6 +830,10 @@ static long xe_eudebug_ioctl(struct file *file,
 	struct xe_eudebug * const d = file->private_data;
 	long ret;
 
+	if (cmd != DRM_XE_EUDEBUG_IOCTL_READ_EVENT &&
+	    !completion_done(&d->discovery))
+		return -EBUSY;
+
 	switch (cmd) {
 	case DRM_XE_EUDEBUG_IOCTL_READ_EVENT:
 		ret = xe_eudebug_read_event(d, arg,
@@ -832,6 +856,8 @@ static const struct file_operations fops = {
 	.unlocked_ioctl	= xe_eudebug_ioctl,
 };
 
+static void discovery_work_fn(struct work_struct *work);
+
 static int
 xe_eudebug_connect(struct xe_device *xe,
 		   struct drm_xe_eudebug_connect *param)
@@ -866,9 +892,11 @@ xe_eudebug_connect(struct xe_device *xe,
 	spin_lock_init(&d->connection.lock);
 	init_waitqueue_head(&d->events.write_done);
 	init_waitqueue_head(&d->events.read_done);
+	init_completion(&d->discovery);
 
 	spin_lock_init(&d->events.lock);
 	INIT_KFIFO(d->events.fifo);
+	INIT_WORK(&d->discovery_work, discovery_work_fn);
 
 	d->res = xe_eudebug_resources_alloc();
 	if (IS_ERR(d->res)) {
@@ -886,6 +914,9 @@ xe_eudebug_connect(struct xe_device *xe,
 		goto err_detach;
 	}
 
+	kref_get(&d->ref);
+	queue_work(xe->eudebug.ordered_wq, &d->discovery_work);
+
 	eu_dbg(d, "connected session %lld", d->session);
 
 	return fd;
@@ -918,13 +949,18 @@ void xe_eudebug_init(struct xe_device *xe)
 	spin_lock_init(&xe->eudebug.lock);
 	INIT_LIST_HEAD(&xe->eudebug.list);
 	INIT_LIST_HEAD(&xe->clients.list);
+	init_rwsem(&xe->eudebug.discovery_lock);
 
-	xe->eudebug.available = true;
+	xe->eudebug.ordered_wq = alloc_ordered_workqueue("xe-eudebug-ordered-wq", 0);
+	xe->eudebug.available = !!xe->eudebug.ordered_wq;
 }
 
 void xe_eudebug_fini(struct xe_device *xe)
 {
 	xe_assert(xe, list_empty_careful(&xe->eudebug.list));
+
+	if (xe->eudebug.ordered_wq)
+		destroy_workqueue(xe->eudebug.ordered_wq);
 }
 
 static int send_open_event(struct xe_eudebug *d, u32 flags, const u64 handle,
@@ -990,21 +1026,25 @@ void xe_eudebug_file_open(struct xe_file *xef)
 	struct xe_eudebug *d;

drivers/gpu/drm/xe/xe_eudebug.c void xe_eudebug_file_open(struct xe_file *xef) · line 1036

Quoted 10 lines
drivers/gpu/drm/xe/xe_eudebug.c +3 -0
 
 	INIT_LIST_HEAD(&xef->eudebug.client_link);
+
+	down_read(&xef->xe->eudebug.discovery_lock);
+
 	spin_lock(&xef->xe->clients.lock);
 	list_add_tail(&xef->eudebug.client_link, &xef->xe->clients.list);
 	spin_unlock(&xef->xe->clients.lock);
 
 	d = xe_eudebug_get(xef);

This looks like this could deadlock.

- discovery_work_fn is queued with &d->discovery not complete
- This function runs and grabs &xef->xe->eudebug.discovery_lock in read mode
- completion_done(&d->discovery) is waited on xe_eudebug_get
- discovery_work_fn can't complete d->discovery) on &xef->xe->eudebug.discovery_lock in write mode

The summary is - it not safe to wait on '&d->discovery' while holding &xef->xe->eudebug.discovery_lock.

Matt

Quoted 141 lines
drivers/gpu/drm/xe/xe_eudebug.c +89 -3
-	if (!d)
-		return;
+	if (d)
+		xe_eudebug_event_put(d, client_create_event(d, xef));
 
-	xe_eudebug_event_put(d, client_create_event(d, xef));
+	up_read(&xef->xe->eudebug.discovery_lock);
 }
 
 void xe_eudebug_file_close(struct xe_file *xef)
 {
 	struct xe_eudebug *d;
 
+	down_read(&xef->xe->eudebug.discovery_lock);
 	d = xe_eudebug_get(xef);
 	if (d)
 		xe_eudebug_event_put(d, client_destroy_event(d, xef));
@@ -1012,6 +1052,8 @@ void xe_eudebug_file_close(struct xe_file *xef)
 	spin_lock(&xef->xe->clients.lock);
 	list_del_init(&xef->eudebug.client_link);
 	spin_unlock(&xef->xe->clients.lock);
+
+	up_read(&xef->xe->eudebug.discovery_lock);
 }
 
 static int send_vm_event(struct xe_eudebug *d, u32 flags,
@@ -1112,3 +1154,86 @@ void xe_eudebug_vm_destroy(struct xe_file *xef, struct xe_vm *vm)
 
 	xe_eudebug_event_put(d, vm_destroy_event(d, xef, vm));
 }
+
+static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
+{
+	struct xe_vm *vm;
+	unsigned long i;
+	int err;
+
+	err = client_create_event(d, xef);
+	if (err)
+		return err;
+
+	xa_for_each(&xef->vm.xa, i, vm) {
+		err = vm_create_event(d, xef, vm);
+		if (err)
+			break;
+	}
+
+	return err;
+}
+
+static bool xe_eudebug_task_match(struct xe_eudebug *d, struct xe_file *xef)
+{
+	struct task_struct *task;
+	bool match;
+
+	task = find_task_get(xef);
+	if (!task)
+		return false;
+
+	match = same_thread_group(d->target_task, task);
+
+	put_task_struct(task);
+
+	return match;
+}
+
+static void discover_clients(struct xe_device *xe, struct xe_eudebug *d)
+{
+	struct xe_file *xef;
+	int err;
+
+	list_for_each_entry(xef, &xe->clients.list, eudebug.client_link) {
+		if (xe_eudebug_detached(d))
+			break;
+
+		if (xe_eudebug_task_match(d, xef))
+			err = discover_client(d, xef);
+		else
+			err = 0;
+
+		if (err) {
+			eu_dbg(d, "discover client %p: %d\n", xef, err);
+			break;
+		}
+	}
+}
+
+static void discovery_work_fn(struct work_struct *work)
+{
+	struct xe_eudebug *d = container_of(work, typeof(*d),
+					    discovery_work);
+	struct xe_device *xe = d->xe;
+
+	if (xe_eudebug_detached(d)) {
+		complete_all(&d->discovery);
+		xe_eudebug_put(d);
+		return;
+	}
+
+	down_write(&xe->eudebug.discovery_lock);
+
+	eu_dbg(d, "Discovery start for %lld\n", d->session);
+
+	discover_clients(xe, d);
+
+	eu_dbg(d, "Discovery end for %lld\n", d->session);
+
+	complete_all(&d->discovery);
+
+	up_write(&xe->eudebug.discovery_lock);
+
+	xe_eudebug_put(d);
+}
drivers/gpu/drm/xe/xe_eudebug_types.h +7 -0
index a5185f18f640..080a821db3e4 100644
@@ -19,6 +19,7 @@
 struct xe_device;
 struct task_struct;
 struct xe_eudebug_event;
+struct workqueue_struct;
 
 #define CONFIG_DRM_XE_DEBUGGER_EVENT_QUEUE_SIZE 64
 
@@ -96,6 +97,12 @@ struct xe_eudebug {
 	/** @session: session number for this connection (for logs) */
 	u64 session;
 
+	/** @discovery: completion to wait for discovery */
+	struct completion discovery;
+
+	/** @discovery_work: worker to discover resources for target_task */
+	struct work_struct discovery_work;
+
 	/** @events: kfifo queue of to-be-delivered events */
 	struct {
 		/** @lock: guards access to fifo */
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH 03/18] drm/xe/eudebug: Introduce discovery for resources

Quoted 244 lines · Matthew Brost
Quoted 243 lines · Mika Kuoppala

Debugger connection can happen way after the client has created and destroyed arbitrary number of resources.

We need to playback all currently existing resources for the debugger. The client is held until this so called discovery process, executed by workqueue, is complete.

This patch is based on discovery work by Maciej Patelczyk for i915 driver.

v2: - use rw_semaphore to block drm_ioctls during discovery (Matthew)
- only lock according to ioctl at play (Dominik)

---
 drivers/gpu/drm/xe/xe_device.c        |  10 +-
 drivers/gpu/drm/xe/xe_device.h        |  34 +++++++
 drivers/gpu/drm/xe/xe_device_types.h  |   6 ++
 drivers/gpu/drm/xe/xe_eudebug.c       | 135 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug_types.h |   7 ++
 5 files changed, 185 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_device.c +8 -2
index 5615e2c23bf6..ec5eedbbf320 100644
@@ -215,8 +215,11 @@ static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
@@ -233,8 +236,11 @@ static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned lo
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_compat_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
drivers/gpu/drm/xe/xe_device.h +34 -0
index 4c3f0ebe78a9..b2fc85b1d26e 100644
@@ -7,6 +7,7 @@
 #define _XE_DEVICE_H_
 
 #include <drm/drm_util.h>
+#include <drm/drm_ioctl.h>
 
 #include "xe_device_types.h"
 #include "xe_gt_types.h"
@@ -191,4 +192,37 @@ void xe_device_declare_wedged(struct xe_device *xe);
 struct xe_file *xe_file_get(struct xe_file *xef);
 void xe_file_put(struct xe_file *xef);
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+static inline int xe_eudebug_needs_lock(const unsigned int cmd)
+{
+	const unsigned int xe_cmd = DRM_IOCTL_NR(cmd) - DRM_COMMAND_BASE;
+
+	switch (xe_cmd) {
+	case DRM_XE_VM_CREATE:
+	case DRM_XE_VM_DESTROY:
+	case DRM_XE_VM_BIND:
+	case DRM_XE_EXEC_QUEUE_CREATE:
+	case DRM_XE_EXEC_QUEUE_DESTROY:
+	case DRM_XE_EUDEBUG_CONNECT:
+		return 1;
+	}
+
+	return 0;
+}
+
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		down_read(&xe->eudebug.discovery_lock);
+}
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		up_read(&xe->eudebug.discovery_lock);
+}
+#else
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd) { }
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd) { }
+#endif /* CONFIG_DRM_XE_EUDEBUG */
+
 #endif
drivers/gpu/drm/xe/xe_device_types.h +6 -0
index cb4b52888a4b..54ceeee7cf75 100644
@@ -542,6 +542,12 @@ struct xe_device {
 
 		/** @available: is the debugging functionality available */
 		bool available;
+
+		/** @ordered_wq: used to discovery */
+		struct workqueue_struct *ordered_wq;
+
+		/** discovery_lock: used for discovery to block xe ioctls */
+		struct rw_semaphore discovery_lock;
 	} eudebug;
 #endif
 
drivers/gpu/drm/xe/xe_eudebug.c +41 -2
index ea0cfd7697aa..c294e2c6152b 100644
@@ -299,6 +299,8 @@ static bool xe_eudebug_detach(struct xe_device *xe,
 	}
 	spin_unlock(&d->connection.lock);
 
+	flush_work(&d->discovery_work);
+
 	if (!detached)
 		return false;
 
@@ -409,7 +411,7 @@ static struct task_struct *find_task_get(struct xe_file *xef)
 }
 
 static struct xe_eudebug *
-xe_eudebug_get(struct xe_file *xef)
+_xe_eudebug_get(struct xe_file *xef)
 {
 	struct task_struct *task;
 	struct xe_eudebug *d;
@@ -433,6 +435,24 @@ xe_eudebug_get(struct xe_file *xef)
 	return d;
 }
 
+static struct xe_eudebug *
+xe_eudebug_get(struct xe_file *xef)
+{
+	struct xe_eudebug *d;
+
+	lockdep_assert_held(&xef->xe->eudebug.discovery_lock);
+
+	d = _xe_eudebug_get(xef);
+	if (d) {
+		if (!completion_done(&d->discovery)) {
+			xe_eudebug_put(d);
+			d = NULL;
+		}
+	}
+
+	return d;
+}
+
 static int xe_eudebug_queue_event(struct xe_eudebug *d,
 				  struct xe_eudebug_event *event)
 {
@@ -810,6 +830,10 @@ static long xe_eudebug_ioctl(struct file *file,
 	struct xe_eudebug * const d = file->private_data;
 	long ret;
 
+	if (cmd != DRM_XE_EUDEBUG_IOCTL_READ_EVENT &&
+	    !completion_done(&d->discovery))
+		return -EBUSY;
+
 	switch (cmd) {
 	case DRM_XE_EUDEBUG_IOCTL_READ_EVENT:
 		ret = xe_eudebug_read_event(d, arg,
@@ -832,6 +856,8 @@ static const struct file_operations fops = {
 	.unlocked_ioctl	= xe_eudebug_ioctl,
 };
 
+static void discovery_work_fn(struct work_struct *work);
+
 static int
 xe_eudebug_connect(struct xe_device *xe,
 		   struct drm_xe_eudebug_connect *param)
@@ -866,9 +892,11 @@ xe_eudebug_connect(struct xe_device *xe,
 	spin_lock_init(&d->connection.lock);
 	init_waitqueue_head(&d->events.write_done);
 	init_waitqueue_head(&d->events.read_done);
+	init_completion(&d->discovery);
 
 	spin_lock_init(&d->events.lock);
 	INIT_KFIFO(d->events.fifo);
+	INIT_WORK(&d->discovery_work, discovery_work_fn);
 
 	d->res = xe_eudebug_resources_alloc();
 	if (IS_ERR(d->res)) {
@@ -886,6 +914,9 @@ xe_eudebug_connect(struct xe_device *xe,
 		goto err_detach;
 	}
 
+	kref_get(&d->ref);
+	queue_work(xe->eudebug.ordered_wq, &d->discovery_work);
+
 	eu_dbg(d, "connected session %lld", d->session);
 
 	return fd;
@@ -918,13 +949,18 @@ void xe_eudebug_init(struct xe_device *xe)
 	spin_lock_init(&xe->eudebug.lock);
 	INIT_LIST_HEAD(&xe->eudebug.list);
 	INIT_LIST_HEAD(&xe->clients.list);
+	init_rwsem(&xe->eudebug.discovery_lock);
 
-	xe->eudebug.available = true;
+	xe->eudebug.ordered_wq = alloc_ordered_workqueue("xe-eudebug-ordered-wq", 0);
+	xe->eudebug.available = !!xe->eudebug.ordered_wq;
 }
 
 void xe_eudebug_fini(struct xe_device *xe)
 {
 	xe_assert(xe, list_empty_careful(&xe->eudebug.list));
+
+	if (xe->eudebug.ordered_wq)
+		destroy_workqueue(xe->eudebug.ordered_wq);
 }
 
 static int send_open_event(struct xe_eudebug *d, u32 flags, const u64 handle,
@@ -990,21 +1026,25 @@ void xe_eudebug_file_open(struct xe_file *xef)
 	struct xe_eudebug *d;
 
 	INIT_LIST_HEAD(&xef->eudebug.client_link);
+
+	down_read(&xef->xe->eudebug.discovery_lock);
+
 	spin_lock(&xef->xe->clients.lock);
 	list_add_tail(&xef->eudebug.client_link, &xef->xe->clients.list);
 	spin_unlock(&xef->xe->clients.lock);
 
Quoted 8 lines

drivers/gpu/drm/xe/xe_eudebug.c void xe_eudebug_file_open(struct xe_file *xef) · line 1036

Quoted 1 line
drivers/gpu/drm/xe/xe_eudebug.c +0 -0
 	d = xe_eudebug_get(xef);

This looks like this could deadlock.

- discovery_work_fn is queued with &d->discovery not complete
- This function runs and grabs &xef->xe->eudebug.discovery_lock in read mode
- completion_done(&d->discovery) is waited on xe_eudebug_get

If the completion is not done we just return NULL immediately.

Yes, previously when we actually used wait_for_completion() here it was a problem. But in this version completion_done() does not wait. Looking at completion_done(), the not done path is just !READ_ONCE(x->done). Did you mean the taking the spinlock to sync against the complete()?

-Mika

Quoted 149 lines

- discovery_work_fn can't complete d->discovery) on &xef->xe->eudebug.discovery_lock in write mode

The summary is - it not safe to wait on '&d->discovery' while holding &xef->xe->eudebug.discovery_lock.

Matt

Quoted 141 lines
drivers/gpu/drm/xe/xe_eudebug.c +89 -3
-	if (!d)
-		return;
+	if (d)
+		xe_eudebug_event_put(d, client_create_event(d, xef));
 
-	xe_eudebug_event_put(d, client_create_event(d, xef));
+	up_read(&xef->xe->eudebug.discovery_lock);
 }
 
 void xe_eudebug_file_close(struct xe_file *xef)
 {
 	struct xe_eudebug *d;
 
+	down_read(&xef->xe->eudebug.discovery_lock);
 	d = xe_eudebug_get(xef);
 	if (d)
 		xe_eudebug_event_put(d, client_destroy_event(d, xef));
@@ -1012,6 +1052,8 @@ void xe_eudebug_file_close(struct xe_file *xef)
 	spin_lock(&xef->xe->clients.lock);
 	list_del_init(&xef->eudebug.client_link);
 	spin_unlock(&xef->xe->clients.lock);
+
+	up_read(&xef->xe->eudebug.discovery_lock);
 }
 
 static int send_vm_event(struct xe_eudebug *d, u32 flags,
@@ -1112,3 +1154,86 @@ void xe_eudebug_vm_destroy(struct xe_file *xef, struct xe_vm *vm)
 
 	xe_eudebug_event_put(d, vm_destroy_event(d, xef, vm));
 }
+
+static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
+{
+	struct xe_vm *vm;
+	unsigned long i;
+	int err;
+
+	err = client_create_event(d, xef);
+	if (err)
+		return err;
+
+	xa_for_each(&xef->vm.xa, i, vm) {
+		err = vm_create_event(d, xef, vm);
+		if (err)
+			break;
+	}
+
+	return err;
+}
+
+static bool xe_eudebug_task_match(struct xe_eudebug *d, struct xe_file *xef)
+{
+	struct task_struct *task;
+	bool match;
+
+	task = find_task_get(xef);
+	if (!task)
+		return false;
+
+	match = same_thread_group(d->target_task, task);
+
+	put_task_struct(task);
+
+	return match;
+}
+
+static void discover_clients(struct xe_device *xe, struct xe_eudebug *d)
+{
+	struct xe_file *xef;
+	int err;
+
+	list_for_each_entry(xef, &xe->clients.list, eudebug.client_link) {
+		if (xe_eudebug_detached(d))
+			break;
+
+		if (xe_eudebug_task_match(d, xef))
+			err = discover_client(d, xef);
+		else
+			err = 0;
+
+		if (err) {
+			eu_dbg(d, "discover client %p: %d\n", xef, err);
+			break;
+		}
+	}
+}
+
+static void discovery_work_fn(struct work_struct *work)
+{
+	struct xe_eudebug *d = container_of(work, typeof(*d),
+					    discovery_work);
+	struct xe_device *xe = d->xe;
+
+	if (xe_eudebug_detached(d)) {
+		complete_all(&d->discovery);
+		xe_eudebug_put(d);
+		return;
+	}
+
+	down_write(&xe->eudebug.discovery_lock);
+
+	eu_dbg(d, "Discovery start for %lld\n", d->session);
+
+	discover_clients(xe, d);
+
+	eu_dbg(d, "Discovery end for %lld\n", d->session);
+
+	complete_all(&d->discovery);
+
+	up_write(&xe->eudebug.discovery_lock);
+
+	xe_eudebug_put(d);
+}
drivers/gpu/drm/xe/xe_eudebug_types.h +7 -0
index a5185f18f640..080a821db3e4 100644
@@ -19,6 +19,7 @@
 struct xe_device;
 struct task_struct;
 struct xe_eudebug_event;
+struct workqueue_struct;
 
 #define CONFIG_DRM_XE_DEBUGGER_EVENT_QUEUE_SIZE 64
 
@@ -96,6 +97,12 @@ struct xe_eudebug {
 	/** @session: session number for this connection (for logs) */
 	u64 session;
 
+	/** @discovery: completion to wait for discovery */
+	struct completion discovery;
+
+	/** @discovery_work: worker to discover resources for target_task */
+	struct work_struct discovery_work;
+
 	/** @events: kfifo queue of to-be-delivered events */
 	struct {
 		/** @lock: guards access to fifo */
Signature · 1 line
2.34.1
maintainer Acked-by
#6 lore raw

Re: [PATCH 03/18] drm/xe/eudebug: Introduce discovery for resources

Quoted 250 lines · Mika Kuoppala
Quoted 248 lines · Matthew Brost
Quoted 234 lines · Mika Kuoppala

Debugger connection can happen way after the client has created and destroyed arbitrary number of resources.

We need to playback all currently existing resources for the debugger. The client is held until this so called discovery process, executed by workqueue, is complete.

This patch is based on discovery work by Maciej Patelczyk for i915 driver.

v2: - use rw_semaphore to block drm_ioctls during discovery (Matthew)
- only lock according to ioctl at play (Dominik)

---
 drivers/gpu/drm/xe/xe_device.c        |  10 +-
 drivers/gpu/drm/xe/xe_device.h        |  34 +++++++
 drivers/gpu/drm/xe/xe_device_types.h  |   6 ++
 drivers/gpu/drm/xe/xe_eudebug.c       | 135 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug_types.h |   7 ++
 5 files changed, 185 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_device.c +8 -2
index 5615e2c23bf6..ec5eedbbf320 100644
@@ -215,8 +215,11 @@ static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
@@ -233,8 +236,11 @@ static long xe_drm_compat_ioctl(struct file *file, unsigned int cmd, unsigned lo
 		return -ECANCELED;
 
 	ret = xe_pm_runtime_get_ioctl(xe);
-	if (ret >= 0)
+	if (ret >= 0) {
+		xe_eudebug_discovery_lock(xe, cmd);
 		ret = drm_compat_ioctl(file, cmd, arg);
+		xe_eudebug_discovery_unlock(xe, cmd);
+	}
 	xe_pm_runtime_put(xe);
 
 	return ret;
drivers/gpu/drm/xe/xe_device.h +34 -0
index 4c3f0ebe78a9..b2fc85b1d26e 100644
@@ -7,6 +7,7 @@
 #define _XE_DEVICE_H_
 
 #include <drm/drm_util.h>
+#include <drm/drm_ioctl.h>
 
 #include "xe_device_types.h"
 #include "xe_gt_types.h"
@@ -191,4 +192,37 @@ void xe_device_declare_wedged(struct xe_device *xe);
 struct xe_file *xe_file_get(struct xe_file *xef);
 void xe_file_put(struct xe_file *xef);
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+static inline int xe_eudebug_needs_lock(const unsigned int cmd)
+{
+	const unsigned int xe_cmd = DRM_IOCTL_NR(cmd) - DRM_COMMAND_BASE;
+
+	switch (xe_cmd) {
+	case DRM_XE_VM_CREATE:
+	case DRM_XE_VM_DESTROY:
+	case DRM_XE_VM_BIND:
+	case DRM_XE_EXEC_QUEUE_CREATE:
+	case DRM_XE_EXEC_QUEUE_DESTROY:
+	case DRM_XE_EUDEBUG_CONNECT:
+		return 1;
+	}
+
+	return 0;
+}
+
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		down_read(&xe->eudebug.discovery_lock);
+}
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd)
+{
+	if (xe_eudebug_needs_lock(cmd))
+		up_read(&xe->eudebug.discovery_lock);
+}
+#else
+static inline void xe_eudebug_discovery_lock(struct xe_device *xe, unsigned int cmd) { }
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd) { }
+#endif /* CONFIG_DRM_XE_EUDEBUG */
+
 #endif
drivers/gpu/drm/xe/xe_device_types.h +6 -0
index cb4b52888a4b..54ceeee7cf75 100644
@@ -542,6 +542,12 @@ struct xe_device {
 
 		/** @available: is the debugging functionality available */
 		bool available;
+
+		/** @ordered_wq: used to discovery */
+		struct workqueue_struct *ordered_wq;
+
+		/** discovery_lock: used for discovery to block xe ioctls */
+		struct rw_semaphore discovery_lock;
 	} eudebug;
 #endif
 
drivers/gpu/drm/xe/xe_eudebug.c +38 -2
index ea0cfd7697aa..c294e2c6152b 100644
@@ -299,6 +299,8 @@ static bool xe_eudebug_detach(struct xe_device *xe,
 	}
 	spin_unlock(&d->connection.lock);
 
+	flush_work(&d->discovery_work);
+
 	if (!detached)
 		return false;
 
@@ -409,7 +411,7 @@ static struct task_struct *find_task_get(struct xe_file *xef)
 }
 
 static struct xe_eudebug *
-xe_eudebug_get(struct xe_file *xef)
+_xe_eudebug_get(struct xe_file *xef)
 {
 	struct task_struct *task;
 	struct xe_eudebug *d;
@@ -433,6 +435,24 @@ xe_eudebug_get(struct xe_file *xef)
 	return d;
 }
 
+static struct xe_eudebug *
+xe_eudebug_get(struct xe_file *xef)
+{
+	struct xe_eudebug *d;
+
+	lockdep_assert_held(&xef->xe->eudebug.discovery_lock);
+
+	d = _xe_eudebug_get(xef);
+	if (d) {
+		if (!completion_done(&d->discovery)) {
+			xe_eudebug_put(d);
+			d = NULL;
+		}
+	}
+
+	return d;
+}
+
 static int xe_eudebug_queue_event(struct xe_eudebug *d,
 				  struct xe_eudebug_event *event)
 {
@@ -810,6 +830,10 @@ static long xe_eudebug_ioctl(struct file *file,
 	struct xe_eudebug * const d = file->private_data;
 	long ret;
 
+	if (cmd != DRM_XE_EUDEBUG_IOCTL_READ_EVENT &&
+	    !completion_done(&d->discovery))
+		return -EBUSY;
+
 	switch (cmd) {
 	case DRM_XE_EUDEBUG_IOCTL_READ_EVENT:
 		ret = xe_eudebug_read_event(d, arg,
@@ -832,6 +856,8 @@ static const struct file_operations fops = {
 	.unlocked_ioctl	= xe_eudebug_ioctl,
 };
 
+static void discovery_work_fn(struct work_struct *work);
+
 static int
 xe_eudebug_connect(struct xe_device *xe,
 		   struct drm_xe_eudebug_connect *param)
@@ -866,9 +892,11 @@ xe_eudebug_connect(struct xe_device *xe,
 	spin_lock_init(&d->connection.lock);
 	init_waitqueue_head(&d->events.write_done);
 	init_waitqueue_head(&d->events.read_done);
+	init_completion(&d->discovery);
 
 	spin_lock_init(&d->events.lock);
 	INIT_KFIFO(d->events.fifo);
+	INIT_WORK(&d->discovery_work, discovery_work_fn);
 
 	d->res = xe_eudebug_resources_alloc();
 	if (IS_ERR(d->res)) {
@@ -886,6 +914,9 @@ xe_eudebug_connect(struct xe_device *xe,
 		goto err_detach;
 	}
 
+	kref_get(&d->ref);
+	queue_work(xe->eudebug.ordered_wq, &d->discovery_work);
+
 	eu_dbg(d, "connected session %lld", d->session);
 
 	return fd;
@@ -918,13 +949,18 @@ void xe_eudebug_init(struct xe_device *xe)
 	spin_lock_init(&xe->eudebug.lock);
 	INIT_LIST_HEAD(&xe->eudebug.list);
 	INIT_LIST_HEAD(&xe->clients.list);
+	init_rwsem(&xe->eudebug.discovery_lock);
 
-	xe->eudebug.available = true;
+	xe->eudebug.ordered_wq = alloc_ordered_workqueue("xe-eudebug-ordered-wq", 0);
+	xe->eudebug.available = !!xe->eudebug.ordered_wq;
 }
 
 void xe_eudebug_fini(struct xe_device *xe)
 {
 	xe_assert(xe, list_empty_careful(&xe->eudebug.list));
+
+	if (xe->eudebug.ordered_wq)
+		destroy_workqueue(xe->eudebug.ordered_wq);
 }
 
 static int send_open_event(struct xe_eudebug *d, u32 flags, const u64 handle,
@@ -990,21 +1026,25 @@ void xe_eudebug_file_open(struct xe_file *xef)
 	struct xe_eudebug *d;

drivers/gpu/drm/xe/xe_eudebug.c void xe_eudebug_file_open(struct xe_file *xef) · line 1036

Quoted 10 lines
drivers/gpu/drm/xe/xe_eudebug.c +3 -0
 
 	INIT_LIST_HEAD(&xef->eudebug.client_link);
+
+	down_read(&xef->xe->eudebug.discovery_lock);
+
 	spin_lock(&xef->xe->clients.lock);
 	list_add_tail(&xef->eudebug.client_link, &xef->xe->clients.list);
 	spin_unlock(&xef->xe->clients.lock);
 
 	d = xe_eudebug_get(xef);

This looks like this could deadlock.

Quoted 7 lines
Quoted 4 lines

- discovery_work_fn is queued with &d->discovery not complete
- This function runs and grabs &xef->xe->eudebug.discovery_lock in read mode
- completion_done(&d->discovery) is waited on xe_eudebug_get

If the completion is not done we just return NULL immediately.

I misread the code, I was thinking xe_eudebug_get blocked on the completion. This looks fine then - sorry the confusion noise.

The locking here looks much better than the previous revs.

From my review PoV - locking:

Quoted 157 lines

Yes, previously when we actually used wait_for_completion() here it was a problem. But in this version completion_done() does not wait. Looking at completion_done(), the not done path is just !READ_ONCE(x->done). Did you mean the taking the spinlock to sync against the complete()?

-Mika

Quoted 149 lines

- discovery_work_fn can't complete d->discovery) on &xef->xe->eudebug.discovery_lock in write mode

The summary is - it not safe to wait on '&d->discovery' while holding &xef->xe->eudebug.discovery_lock.

Matt

Quoted 141 lines
drivers/gpu/drm/xe/xe_eudebug.c +89 -3
-	if (!d)
-		return;
+	if (d)
+		xe_eudebug_event_put(d, client_create_event(d, xef));
 
-	xe_eudebug_event_put(d, client_create_event(d, xef));
+	up_read(&xef->xe->eudebug.discovery_lock);
 }
 
 void xe_eudebug_file_close(struct xe_file *xef)
 {
 	struct xe_eudebug *d;
 
+	down_read(&xef->xe->eudebug.discovery_lock);
 	d = xe_eudebug_get(xef);
 	if (d)
 		xe_eudebug_event_put(d, client_destroy_event(d, xef));
@@ -1012,6 +1052,8 @@ void xe_eudebug_file_close(struct xe_file *xef)
 	spin_lock(&xef->xe->clients.lock);
 	list_del_init(&xef->eudebug.client_link);
 	spin_unlock(&xef->xe->clients.lock);
+
+	up_read(&xef->xe->eudebug.discovery_lock);
 }
 
 static int send_vm_event(struct xe_eudebug *d, u32 flags,
@@ -1112,3 +1154,86 @@ void xe_eudebug_vm_destroy(struct xe_file *xef, struct xe_vm *vm)
 
 	xe_eudebug_event_put(d, vm_destroy_event(d, xef, vm));
 }
+
+static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
+{
+	struct xe_vm *vm;
+	unsigned long i;
+	int err;
+
+	err = client_create_event(d, xef);
+	if (err)
+		return err;
+
+	xa_for_each(&xef->vm.xa, i, vm) {
+		err = vm_create_event(d, xef, vm);
+		if (err)
+			break;
+	}
+
+	return err;
+}
+
+static bool xe_eudebug_task_match(struct xe_eudebug *d, struct xe_file *xef)
+{
+	struct task_struct *task;
+	bool match;
+
+	task = find_task_get(xef);
+	if (!task)
+		return false;
+
+	match = same_thread_group(d->target_task, task);
+
+	put_task_struct(task);
+
+	return match;
+}
+
+static void discover_clients(struct xe_device *xe, struct xe_eudebug *d)
+{
+	struct xe_file *xef;
+	int err;
+
+	list_for_each_entry(xef, &xe->clients.list, eudebug.client_link) {
+		if (xe_eudebug_detached(d))
+			break;
+
+		if (xe_eudebug_task_match(d, xef))
+			err = discover_client(d, xef);
+		else
+			err = 0;
+
+		if (err) {
+			eu_dbg(d, "discover client %p: %d\n", xef, err);
+			break;
+		}
+	}
+}
+
+static void discovery_work_fn(struct work_struct *work)
+{
+	struct xe_eudebug *d = container_of(work, typeof(*d),
+					    discovery_work);
+	struct xe_device *xe = d->xe;
+
+	if (xe_eudebug_detached(d)) {
+		complete_all(&d->discovery);
+		xe_eudebug_put(d);
+		return;
+	}
+
+	down_write(&xe->eudebug.discovery_lock);
+
+	eu_dbg(d, "Discovery start for %lld\n", d->session);
+
+	discover_clients(xe, d);
+
+	eu_dbg(d, "Discovery end for %lld\n", d->session);
+
+	complete_all(&d->discovery);
+
+	up_write(&xe->eudebug.discovery_lock);
+
+	xe_eudebug_put(d);
+}
drivers/gpu/drm/xe/xe_eudebug_types.h +7 -0
index a5185f18f640..080a821db3e4 100644
@@ -19,6 +19,7 @@
 struct xe_device;
 struct task_struct;
 struct xe_eudebug_event;
+struct workqueue_struct;
 
 #define CONFIG_DRM_XE_DEBUGGER_EVENT_QUEUE_SIZE 64
 
@@ -96,6 +97,12 @@ struct xe_eudebug {
 	/** @session: session number for this connection (for logs) */
 	u64 session;
 
+	/** @discovery: completion to wait for discovery */
+	struct completion discovery;
+
+	/** @discovery_work: worker to discover resources for target_task */
+	struct work_struct discovery_work;
+
 	/** @events: kfifo queue of to-be-delivered events */
 	struct {
 		/** @lock: guards access to fifo */
Signature · 1 line
2.34.1
#7 lore raw

PATCH 4/18 drm/xe/eudebug: Introduce exec_queue events

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Inform debugger about creation and destruction of exec_queues.

1) Use user engine class types instead of internal xe_engine_class enum in exec_queue event.

2) During discovery do not advertise every execqueue created, only ones with class render or compute.

v2: - Only track long running queues
- Checkpatch (Tilak)

---
 drivers/gpu/drm/xe/xe_eudebug.c       | 187 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug.h       |   7 +
 drivers/gpu/drm/xe/xe_eudebug_types.h |  31 ++++-
 drivers/gpu/drm/xe/xe_exec_queue.c    |   5 +
 include/uapi/drm/xe_drm_eudebug.h     |  12 ++
 5 files changed, 240 insertions(+), 2 deletions(-)
drivers/gpu/drm/xe/xe_eudebug.c +186 -1
index c294e2c6152b..4706320b39b6 100644
@@ -14,6 +14,7 @@
 #include "xe_device.h"
 #include "xe_eudebug.h"
 #include "xe_eudebug_types.h"
+#include "xe_exec_queue.h"
 #include "xe_macros.h"
 #include "xe_vm.h"
 
@@ -748,7 +749,7 @@ static long xe_eudebug_read_event(struct xe_eudebug *d,
 		u64_to_user_ptr(arg);
 	struct drm_xe_eudebug_event user_event;
 	struct xe_eudebug_event *event;
-	const unsigned int max_event = DRM_XE_EUDEBUG_EVENT_VM;
+	const unsigned int max_event = DRM_XE_EUDEBUG_EVENT_EXEC_QUEUE;
 	long ret = 0;
 
 	if (XE_IOCTL_DBG(xe, copy_from_user(&user_event, user_orig, sizeof(user_event))))
@@ -1155,8 +1156,183 @@ void xe_eudebug_vm_destroy(struct xe_file *xef, struct xe_vm *vm)
 	xe_eudebug_event_put(d, vm_destroy_event(d, xef, vm));
 }
 
+static bool exec_queue_class_is_tracked(enum xe_engine_class class)
+{
+	return class == XE_ENGINE_CLASS_COMPUTE ||
+		class == XE_ENGINE_CLASS_RENDER;
+}
+
+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 client_handle, u64 vm_handle,
+				 u64 exec_queue_handle, enum xe_engine_class class,
+				 u32 width, u64 *lrc_handles, u64 seqno)
+{
+	struct xe_eudebug_event *event;
+	struct xe_eudebug_event_exec_queue *e;
+	const u32 sz = struct_size(e, lrc_handle, width);
+	const u32 xe_engine_class = xe_to_user_engine_class[class];
+
+	if (!exec_queue_class_is_tracked(class))
+		return -EINVAL;
+
+	event = xe_eudebug_create_event(d, DRM_XE_EUDEBUG_EVENT_EXEC_QUEUE,
+					seqno, flags, sz, GFP_KERNEL);
+	if (!event)
+		return -ENOMEM;
+
+	e = cast_event(e, event);
+
+	write_member(struct drm_xe_eudebug_event_exec_queue, e, client_handle, client_handle);
+	write_member(struct drm_xe_eudebug_event_exec_queue, e, vm_handle, vm_handle);
+	write_member(struct drm_xe_eudebug_event_exec_queue, e, exec_queue_handle,
+		     exec_queue_handle);
+	write_member(struct drm_xe_eudebug_event_exec_queue, e, engine_class, xe_engine_class);
+	write_member(struct drm_xe_eudebug_event_exec_queue, e, width, width);
+
+	memcpy(e->lrc_handle, lrc_handles, width);
+
+	return xe_eudebug_queue_event(d, event);
+}
+
+static int exec_queue_create_event(struct xe_eudebug *d,
+				   struct xe_file *xef, struct xe_exec_queue *q)
+{
+	int h_c, h_vm, h_queue;
+	u64 h_lrc[XE_HW_ENGINE_MAX_INSTANCE], seqno;
+	int i;
+
+	if (!xe_exec_queue_is_lr(q))
+		return 0;
+
+	h_c = find_handle(d->res, XE_EUDEBUG_RES_TYPE_CLIENT, xef);
+	if (h_c < 0)
+		return h_c;
+
+	h_vm = find_handle(d->res, XE_EUDEBUG_RES_TYPE_VM, q->vm);
+	if (h_vm < 0)
+		return h_vm;
+
+	if (XE_WARN_ON(q->width >= XE_HW_ENGINE_MAX_INSTANCE))
+		return -EINVAL;
+
+	for (i = 0; i < q->width; i++) {
+		int h, ret;
+
+		ret = _xe_eudebug_add_handle(d,
+					     XE_EUDEBUG_RES_TYPE_LRC,
+					     q->lrc[i],
+					     NULL,
+					     &h);
+
+		if (ret < 0 && ret != -EEXIST)
+			return ret;
+
+		XE_WARN_ON(!h);
+
+		h_lrc[i] = h;
+	}
+
+	h_queue = xe_eudebug_add_handle(d, XE_EUDEBUG_RES_TYPE_EXEC_QUEUE, q, &seqno);
+	if (h_queue <= 0)
+		return h_queue;
+
+	/* No need to cleanup for added handles on error as if we fail
+	 * we disconnect
+	 */
+
+	return send_exec_queue_event(d, DRM_XE_EUDEBUG_EVENT_CREATE,
+				     h_c, h_vm, h_queue, q->class,
+				     q->width, h_lrc, seqno);
+}
+
+static int exec_queue_destroy_event(struct xe_eudebug *d,
+				    struct xe_file *xef,
+				    struct xe_exec_queue *q)
+{
+	int h_c, h_vm, h_queue;
+	u64 h_lrc[XE_HW_ENGINE_MAX_INSTANCE], seqno;
+	int i;
+
+	if (!xe_exec_queue_is_lr(q))
+		return 0;
+
+	h_c = find_handle(d->res, XE_EUDEBUG_RES_TYPE_CLIENT, xef);
+	if (h_c < 0)
+		return h_c;
+
+	h_vm = find_handle(d->res, XE_EUDEBUG_RES_TYPE_VM, q->vm);
+	if (h_vm < 0)
+		return h_vm;
+
+	if (XE_WARN_ON(q->width >= XE_HW_ENGINE_MAX_INSTANCE))
+		return -EINVAL;
+
+	h_queue = xe_eudebug_remove_handle(d,
+					   XE_EUDEBUG_RES_TYPE_EXEC_QUEUE,
+					   q,
+					   &seqno);
+	if (h_queue <= 0)
+		return h_queue;
+
+	for (i = 0; i < q->width; i++) {
+		int ret;
+
+		ret = _xe_eudebug_remove_handle(d,
+						XE_EUDEBUG_RES_TYPE_LRC,
+						q->lrc[i],
+						NULL);
+		if (ret < 0 && ret != -ENOENT)
+			return ret;
+
+		XE_WARN_ON(!ret);
+
+		h_lrc[i] = ret;
+	}
+
+	return send_exec_queue_event(d, DRM_XE_EUDEBUG_EVENT_DESTROY,
+				     h_c, h_vm, h_queue, q->class,
+				     q->width, h_lrc, seqno);
+}
+
+void xe_eudebug_exec_queue_create(struct xe_file *xef, struct xe_exec_queue *q)
+{
+	struct xe_eudebug *d;
+
+	if (!exec_queue_class_is_tracked(q->class))
+		return;
+
+	d = xe_eudebug_get(xef);
+	if (!d)
+		return;
+
+	xe_eudebug_event_put(d, exec_queue_create_event(d, xef, q));
+}
+
+void xe_eudebug_exec_queue_destroy(struct xe_file *xef, struct xe_exec_queue *q)
+{
+	struct xe_eudebug *d;
+
+	if (!exec_queue_class_is_tracked(q->class))
+		return;
+
+	d = xe_eudebug_get(xef);
+	if (!d)
+		return;
+
+	xe_eudebug_event_put(d, exec_queue_destroy_event(d, xef, q));
+}
+
 static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
 {
+	struct xe_exec_queue *q;
 	struct xe_vm *vm;
 	unsigned long i;
 	int err;
@@ -1171,6 +1347,15 @@ static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
 			break;
 	}
 
+	xa_for_each(&xef->exec_queue.xa, i, q) {
+		if (!exec_queue_class_is_tracked(q->class))
+			continue;
+
+		err = exec_queue_create_event(d, xef, q);
+		if (err)
+			break;
+	}
+
 	return err;
 }
 
drivers/gpu/drm/xe/xe_eudebug.h +7 -0
index e3247365f72f..326ddbd50651 100644
@@ -10,6 +10,7 @@ struct drm_file;
 struct xe_device;
 struct xe_file;
 struct xe_vm;
+struct xe_exec_queue;
 
 #if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
 
@@ -26,6 +27,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,
@@ -41,6 +45,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
drivers/gpu/drm/xe/xe_eudebug_types.h +30 -1
index 080a821db3e4..6428905a0557 100644
@@ -50,7 +50,9 @@ struct xe_eudebug_resource {
 
 #define XE_EUDEBUG_RES_TYPE_CLIENT	0
 #define XE_EUDEBUG_RES_TYPE_VM		1
-#define XE_EUDEBUG_RES_TYPE_COUNT	(XE_EUDEBUG_RES_TYPE_VM + 1)
+#define XE_EUDEBUG_RES_TYPE_EXEC_QUEUE	2
+#define XE_EUDEBUG_RES_TYPE_LRC		3
+#define XE_EUDEBUG_RES_TYPE_COUNT	(XE_EUDEBUG_RES_TYPE_LRC + 1)
 
 /**
  * struct xe_eudebug_resources - eudebug resources for all types
@@ -173,4 +175,31 @@ struct xe_eudebug_event_vm {
 	u64 vm_handle;
 };
 
+/**
+ * struct xe_eudebug_event_exec_queue - Internal event for
+ * exec_queue create/destroy
+ */
+struct xe_eudebug_event_exec_queue {
+	/** @base: base event */
+	struct xe_eudebug_event base;
+
+	/** @client_handle: client for the engine create/destroy */
+	u64 client_handle;
+
+	/** @vm_handle: vm handle for the engine create/destroy */
+	u64 vm_handle;
+
+	/** @exec_queue_handle: engine handle */
+	u64 exec_queue_handle;
+
+	/** @engine_handle: engine class */
+	u32 engine_class;
+
+	/** @width: submission width (number BB per exec) for this exec queue */
+	u32 width;
+
+	/** @lrc_handles: handles for each logical ring context created with this exec queue */
+	u64 lrc_handle[];
+};
+
 #endif
drivers/gpu/drm/xe/xe_exec_queue.c +5 -0
index d098d2dd1b2d..e6b2487de48e 100644
@@ -23,6 +23,7 @@
 #include "xe_ring_ops_types.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
+#include "xe_eudebug.h"
 
 enum xe_exec_queue_sched_prop {
 	XE_EXEC_QUEUE_JOB_TIMEOUT = 0,
@@ -644,6 +645,8 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
 
 	args->exec_queue_id = id;
 
+	xe_eudebug_exec_queue_create(xef, q);
+
 	return 0;
 
 kill_exec_queue:
@@ -827,6 +830,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);
include/uapi/drm/xe_drm_eudebug.h +12 -0
index acf6071c82bf..ac44e890152a 100644
@@ -26,6 +26,7 @@ struct drm_xe_eudebug_event {
 #define DRM_XE_EUDEBUG_EVENT_READ		1
 #define DRM_XE_EUDEBUG_EVENT_OPEN		2
 #define DRM_XE_EUDEBUG_EVENT_VM			3
+#define DRM_XE_EUDEBUG_EVENT_EXEC_QUEUE		4
 
 	__u16 flags;
 #define DRM_XE_EUDEBUG_EVENT_CREATE		(1 << 0)
@@ -49,6 +50,17 @@ struct drm_xe_eudebug_event_vm {
 	__u64 vm_handle;
 };
 
+struct drm_xe_eudebug_event_exec_queue {
+	struct drm_xe_eudebug_event base;
+
+	__u64 client_handle;
+	__u64 vm_handle;
+	__u64 exec_queue_handle;
+	__u32 engine_class;
+	__u32 width;
+	__u64 lrc_handle[];
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.34.1
#8 lore raw

PATCH 5/18 drm/xe/eudebug: hw enablement for eudebug

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

In order to turn on debug capabilities, (i.e. breakpoints), TD_CTL and some other registers needs to be programmed. Implement eudebug mode enabling including eudebug related workarounds.

v2: Move workarounds to xe_wa_oob. Use reg_sr directly instead of xe_rtp as it suits better for dynamic manipulation of those register we do later in the series.

---
 drivers/gpu/drm/xe/regs/xe_engine_regs.h |  4 ++
 drivers/gpu/drm/xe/regs/xe_gt_regs.h     | 10 ++++
 drivers/gpu/drm/xe/xe_eudebug.c          | 70 ++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_eudebug.h          |  3 +
 drivers/gpu/drm/xe/xe_hw_engine.c        |  2 +
 drivers/gpu/drm/xe/xe_wa_oob.rules       |  2 +
 6 files changed, 91 insertions(+)
drivers/gpu/drm/xe/regs/xe_engine_regs.h +4 -0
index 81b71903675e..e004423a66bf 100644
@@ -115,6 +115,10 @@
 
 #define INDIRECT_RING_STATE(base)		XE_REG((base) + 0x108)
 
+#define CS_DEBUG_MODE2(base)			XE_REG((base) + 0xd8, XE_REG_OPTION_MASKED)
+#define   INST_STATE_CACHE_INVALIDATE		REG_BIT(6)
+#define   GLOBAL_DEBUG_ENABLE			REG_BIT(5)
+
 #define RING_BBADDR(base)			XE_REG((base) + 0x140)
 #define RING_BBADDR_UDW(base)			XE_REG((base) + 0x168)
 
drivers/gpu/drm/xe/regs/xe_gt_regs.h +10 -0
index fb80042cbe0d..2780541feb67 100644
@@ -453,6 +453,14 @@
 #define   DG2_DISABLE_ROUND_ENABLE_ALLOW_FOR_SSLA	REG_BIT(15)
 #define   CLEAR_OPTIMIZATION_DISABLE			REG_BIT(6)
 
+#define TD_CTL					XE_REG_MCR(0xe400)
+#define   TD_CTL_FEH_AND_FEE_ENABLE		REG_BIT(7) /* forced halt and exception */
+#define   TD_CTL_FORCE_EXTERNAL_HALT		REG_BIT(6)
+#define   TD_CTL_FORCE_THREAD_BREAKPOINT_ENABLE	REG_BIT(4)
+#define   TD_CTL_FORCE_EXCEPTION		REG_BIT(3)
+#define   TD_CTL_BREAKPOINT_ENABLE		REG_BIT(2)
+#define   TD_CTL_GLOBAL_DEBUG_ENABLE		REG_BIT(0) /* XeHP */
+
 #define CACHE_MODE_SS				XE_REG_MCR(0xe420, XE_REG_OPTION_MASKED)
 #define   DISABLE_ECC				REG_BIT(5)
 #define   ENABLE_PREFETCH_INTO_IC		REG_BIT(3)
@@ -479,11 +487,13 @@
 #define   MDQ_ARBITRATION_MODE			REG_BIT(12)
 #define   STALL_DOP_GATING_DISABLE		REG_BIT(5)
 #define   EARLY_EOT_DIS				REG_BIT(1)
+#define   STALL_DOP_GATING_DISABLE		REG_BIT(5)
 
 #define ROW_CHICKEN2				XE_REG_MCR(0xe4f4, XE_REG_OPTION_MASKED)
 #define   DISABLE_READ_SUPPRESSION		REG_BIT(15)
 #define   DISABLE_EARLY_READ			REG_BIT(14)
 #define   ENABLE_LARGE_GRF_MODE			REG_BIT(12)
+#define   XEHPC_DISABLE_BTB			REG_BIT(11)
 #define   PUSH_CONST_DEREF_HOLD_DIS		REG_BIT(8)
 #define   DISABLE_TDL_SVHS_GATING		REG_BIT(1)
 #define   DISABLE_DOP_GATING			REG_BIT(0)
drivers/gpu/drm/xe/xe_eudebug.c +70 -0

89 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug.h +3 -0
index 326ddbd50651..3cd6bc7bb682 100644
@@ -11,6 +11,7 @@ struct xe_device;
 struct xe_file;
 struct xe_vm;
 struct xe_exec_queue;
+struct xe_hw_engine;
 
 #if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
 
@@ -20,6 +21,7 @@ int xe_eudebug_connect_ioctl(struct drm_device *dev,
 
 void xe_eudebug_init(struct xe_device *xe);
 void xe_eudebug_fini(struct xe_device *xe);
+void xe_eudebug_init_hw_engine(struct xe_hw_engine *hwe);
 
 void xe_eudebug_file_open(struct xe_file *xef);
 void xe_eudebug_file_close(struct xe_file *xef);
@@ -38,6 +40,7 @@ static inline int xe_eudebug_connect_ioctl(struct drm_device *dev,
 
 static inline void xe_eudebug_init(struct xe_device *xe) { }
 static inline void xe_eudebug_fini(struct xe_device *xe) { }
+static inline void xe_eudebug_init_hw_engine(struct xe_hw_engine *hwe) { }
 
 static inline void xe_eudebug_file_open(struct xe_file *xef) { }
 static inline void xe_eudebug_file_close(struct xe_file *xef) { }
drivers/gpu/drm/xe/xe_hw_engine.c +2 -0
index ea6d9ef7fab6..8e9a08395042 100644
@@ -16,6 +16,7 @@
 #include "xe_assert.h"
 #include "xe_bo.h"
 #include "xe_device.h"
+#include "xe_eudebug.h"
 #include "xe_execlist.h"
 #include "xe_force_wake.h"
 #include "xe_gsc.h"
@@ -557,6 +558,7 @@ static void hw_engine_init_early(struct xe_gt *gt, struct xe_hw_engine *hwe,
 	xe_tuning_process_engine(hwe);
 	xe_wa_process_engine(hwe);
 	hw_engine_setup_default_state(hwe);
+	xe_eudebug_init_hw_engine(hwe);
 
 	xe_reg_sr_init(&hwe->reg_whitelist, hwe->name, gt_to_xe(gt));
 	xe_reg_whitelist_process_engine(hwe);
drivers/gpu/drm/xe/xe_wa_oob.rules +2 -0
index 920ca5060146..da7d2c84298c 100644
@@ -37,3 +37,5 @@
 16023588340	GRAPHICS_VERSION(2001)
 14019789679	GRAPHICS_VERSION(1255)
 		GRAPHICS_VERSION_RANGE(1270, 2004)
+18022722726	GRAPHICS_VERSION_RANGE(1250, 1274)
+14015474168	PLATFORM(PVC)
Signature · 1 line
2.34.1
#9 lore raw

PATCH 6/18 drm/xe: Add EUDEBUG_ENABLE exec queue property

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Introduce exec queue immutable property of eudebug with a flags as value to enable eudebug specific feature(s).

For now engine lrc will use this flag to set up runalone hw feature. Runalone is used to ensure that only one hw engine of group [rcs0, ccs0-3] is active on a tile.

Note: unlike the i915, xe allows user to set runalone also on devices with single render/compute engine. It should not make much difference, but leave control to the user.

v2: - check CONFIG_DRM_XE_EUDEBUG and LR mode (Matthew)
- disable preempt (Dominik)
- lrc_create remove from engine init

---
 drivers/gpu/drm/xe/xe_eudebug.c          |  4 +--
 drivers/gpu/drm/xe/xe_exec_queue.c       | 46 ++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_exec_queue.h       |  2 ++
 drivers/gpu/drm/xe/xe_exec_queue_types.h |  7 ++++
 drivers/gpu/drm/xe/xe_execlist.c         |  2 +-
 drivers/gpu/drm/xe/xe_lrc.c              | 16 +++++++--
 drivers/gpu/drm/xe/xe_lrc.h              |  4 ++-
 include/uapi/drm/xe_drm.h                |  3 +-
 8 files changed, 74 insertions(+), 10 deletions(-)
drivers/gpu/drm/xe/xe_eudebug.c +2 -2
index f90b15fc4964..15a3f8b087a0 100644
@@ -1279,7 +1279,7 @@ static int exec_queue_create_event(struct xe_eudebug *d,
 	u64 h_lrc[XE_HW_ENGINE_MAX_INSTANCE], seqno;
 	int i;
 
-	if (!xe_exec_queue_is_lr(q))
+	if (!xe_exec_queue_is_debuggable(q))
 		return 0;
 
 	h_c = find_handle(d->res, XE_EUDEBUG_RES_TYPE_CLIENT, xef);
@@ -1331,7 +1331,7 @@ static int exec_queue_destroy_event(struct xe_eudebug *d,
 	u64 h_lrc[XE_HW_ENGINE_MAX_INSTANCE], seqno;
 	int i;
 
-	if (!xe_exec_queue_is_lr(q))
+	if (!xe_exec_queue_is_debuggable(q))
 		return 0;
 
 	h_c = find_handle(d->res, XE_EUDEBUG_RES_TYPE_CLIENT, xef);
drivers/gpu/drm/xe/xe_exec_queue.c +44 -2

77 lines not on this page · raw message

drivers/gpu/drm/xe/xe_exec_queue.h +2 -0
index 90c7f73eab88..421d8dc89814 100644
@@ -85,4 +85,6 @@ int xe_exec_queue_last_fence_test_dep(struct xe_exec_queue *q,
 				      struct xe_vm *vm);
 void xe_exec_queue_update_run_ticks(struct xe_exec_queue *q);
 
+int xe_exec_queue_is_debuggable(struct xe_exec_queue *q);
+
 #endif
drivers/gpu/drm/xe/xe_exec_queue_types.h +7 -0
index 7deb480e26af..9eced5867f07 100644
@@ -90,6 +90,13 @@ struct xe_exec_queue {
 	 */
 	unsigned long flags;
 
+	/**
+	 * @eudebug_flags: immutable eudebug flags for this exec queue.
+	 * Set up with DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG.
+	 */
+#define EXEC_QUEUE_EUDEBUG_FLAG_ENABLE		BIT(0)
+	unsigned long eudebug_flags;
+
 	union {
 		/** @multi_gt_list: list head for VM bind engines if multi-GT */
 		struct list_head multi_gt_list;
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index f3b71fe7a96d..b425c48ca49d 100644
@@ -265,7 +265,7 @@ struct xe_execlist_port *xe_execlist_port_create(struct xe_device *xe,
 
 	port->hwe = hwe;
 
-	port->lrc = xe_lrc_create(hwe, NULL, SZ_16K);
+	port->lrc = xe_lrc_create(hwe, NULL, SZ_16K, 0);
 	if (IS_ERR(port->lrc)) {
 		err = PTR_ERR(port->lrc);
 		goto err;
drivers/gpu/drm/xe/xe_lrc.c +13 -3
index f0976230012a..68a411722f09 100644
@@ -893,7 +893,7 @@ static void xe_lrc_finish(struct xe_lrc *lrc)
 #define PVC_CTX_ACC_CTR_THOLD	(0x2a + 1)
 
 static int xe_lrc_init(struct xe_lrc *lrc, struct xe_hw_engine *hwe,
-		       struct xe_vm *vm, u32 ring_size)
+		       struct xe_vm *vm, u32 ring_size, u32 flags)
 {
 	struct xe_gt *gt = hwe->gt;
 	struct xe_tile *tile = gt_to_tile(gt);
@@ -1010,6 +1010,16 @@ static int xe_lrc_init(struct xe_lrc *lrc, struct xe_hw_engine *hwe,
 	map = __xe_lrc_start_seqno_map(lrc);
 	xe_map_write32(lrc_to_xe(lrc), &map, lrc->fence_ctx.next_seqno - 1);
 
+	if (flags & LRC_CREATE_RUNALONE) {
+		u32 ctx_control = xe_lrc_read_ctx_reg(lrc, CTX_CONTEXT_CONTROL);
+
+		drm_dbg(&xe->drm, "read CTX_CONTEXT_CONTROL: 0x%x\n", ctx_control);
+		ctx_control |= _MASKED_BIT_ENABLE(CTX_CTRL_RUN_ALONE);
+		drm_dbg(&xe->drm, "written CTX_CONTEXT_CONTROL: 0x%x\n", ctx_control);
+
+		xe_lrc_write_ctx_reg(lrc, CTX_CONTEXT_CONTROL, ctx_control);
+	}
+
 	return 0;
 
 err_lrc_finish:
@@ -1029,7 +1039,7 @@ static int xe_lrc_init(struct xe_lrc *lrc, struct xe_hw_engine *hwe,
  * upon failure.
  */
 struct xe_lrc *xe_lrc_create(struct xe_hw_engine *hwe, struct xe_vm *vm,
-			     u32 ring_size)
+			     u32 ring_size, u32 flags)
 {
 	struct xe_lrc *lrc;
 	int err;
@@ -1038,7 +1048,7 @@ struct xe_lrc *xe_lrc_create(struct xe_hw_engine *hwe, struct xe_vm *vm,
 	if (!lrc)
 		return ERR_PTR(-ENOMEM);
 
-	err = xe_lrc_init(lrc, hwe, vm, ring_size);
+	err = xe_lrc_init(lrc, hwe, vm, ring_size, flags);
 	if (err) {
 		kfree(lrc);
 		return ERR_PTR(err);
drivers/gpu/drm/xe/xe_lrc.h +3 -1
index c24542e89318..16a9264fdcfa 100644
@@ -22,8 +22,10 @@ struct xe_vm;
 
 #define LRC_PPHWSP_SCRATCH_ADDR (0x34 * 4)
 
+#define LRC_CREATE_RUNALONE	BIT(0)
+
 struct xe_lrc *xe_lrc_create(struct xe_hw_engine *hwe, struct xe_vm *vm,
-			     u32 ring_size);
+			     u32 ring_size, u32 flags);
 void xe_lrc_destroy(struct kref *ref);
 
 /**
include/uapi/drm/xe_drm.h +2 -1
index 7e66e9117f49..985dcf852458 100644
@@ -1110,7 +1110,8 @@ struct drm_xe_exec_queue_create {
 #define DRM_XE_EXEC_QUEUE_EXTENSION_SET_PROPERTY		0
 #define   DRM_XE_EXEC_QUEUE_SET_PROPERTY_PRIORITY		0
 #define   DRM_XE_EXEC_QUEUE_SET_PROPERTY_TIMESLICE		1
-
+#define   DRM_XE_EXEC_QUEUE_SET_PROPERTY_EUDEBUG		2
+#define     DRM_XE_EXEC_QUEUE_EUDEBUG_FLAG_ENABLE		(1 << 0)
 	/** @extensions: Pointer to the first extension struct, if any */
 	__u64 extensions;
 
Signature · 1 line
2.34.1
#10 lore raw

PATCH 7/18 drm/xe/eudebug: Introduce per device attention scan worker

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Scan for EU debugging attention bits periodically to detect if some EU thread has entered the system routine (SIP) due to EU thread exception.

Make the scanning interval 10 times slower when there is no debugger connection open. Send attention event whenever we see attention with debugger presence. If there is no debugger connection active - reset.

Based on work by authors and other folks who were part of attentions in i915.

v2: - use xa_array for files
- null ptr deref fix for non-debugged context (Dominik)
- checkpatch (Tilak)
- use discovery_lock during list traversal

---
 drivers/gpu/drm/xe/Makefile              |   1 +
 drivers/gpu/drm/xe/regs/xe_engine_regs.h |   3 +
 drivers/gpu/drm/xe/regs/xe_gt_regs.h     |   7 +
 drivers/gpu/drm/xe/xe_device.c           |   2 +
 drivers/gpu/drm/xe/xe_device_types.h     |   3 +
 drivers/gpu/drm/xe/xe_eudebug.c          | 381 ++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug.h          |   2 +
 drivers/gpu/drm/xe/xe_eudebug_types.h    |  32 ++
 drivers/gpu/drm/xe/xe_gt_debug.c         | 148 +++++++++
 drivers/gpu/drm/xe/xe_gt_debug.h         |  21 ++
 include/uapi/drm/xe_drm_eudebug.h        |  13 +
 11 files changed, 612 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_gt_debug.c
 create mode 100644 drivers/gpu/drm/xe/xe_gt_debug.h
drivers/gpu/drm/xe/Makefile +1 -0
index a3cd467cdaa6..660c1dbba8d0 100644
@@ -48,6 +48,7 @@ xe-y += xe_bb.o \
 	xe_gt_clock.o \
 	xe_gt_freq.o \
 	xe_gt_idle.o \
+	xe_gt_debug.o \
 	xe_gt_mcr.o \
 	xe_gt_pagefault.o \
 	xe_gt_sysfs.o \
drivers/gpu/drm/xe/regs/xe_engine_regs.h +3 -0
index e004423a66bf..f2f136b1b0fa 100644
@@ -133,6 +133,9 @@
 #define RING_EXECLIST_STATUS_LO(base)		XE_REG((base) + 0x234)
 #define RING_EXECLIST_STATUS_HI(base)		XE_REG((base) + 0x234 + 4)
 
+#define RING_CURRENT_LRCA(base)			XE_REG((base) + 0x240)
+#define   CURRENT_LRCA_VALID			REG_BIT(0)
+
 #define RING_CONTEXT_CONTROL(base)		XE_REG((base) + 0x244, XE_REG_OPTION_MASKED)
 #define	  CTX_CTRL_OAC_CONTEXT_ENABLE		REG_BIT(8)
 #define	  CTX_CTRL_RUN_ALONE			REG_BIT(7)
drivers/gpu/drm/xe/regs/xe_gt_regs.h +7 -0
index 2780541feb67..0129480dbd9d 100644
@@ -465,6 +465,8 @@
 #define   DISABLE_ECC				REG_BIT(5)
 #define   ENABLE_PREFETCH_INTO_IC		REG_BIT(3)
 
+#define TD_ATT(x)				XE_REG_MCR(0xe470 + (x) * 4)
+
 #define ROW_CHICKEN4				XE_REG_MCR(0xe48c, XE_REG_OPTION_MASKED)
 #define   DISABLE_GRF_CLEAR			REG_BIT(13)
 #define   XEHP_DIS_BBL_SYSPIPE			REG_BIT(11)
@@ -545,6 +547,11 @@
 #define   CCS_MODE_CSLICE(cslice, ccs) \
 	((ccs) << ((cslice) * CCS_MODE_CSLICE_WIDTH))
 
+#define RCU_DEBUG_1				XE_REG(0x14a00)
+#define   RCU_DEBUG_1_ENGINE_STATUS		REG_GENMASK(2, 0)
+#define   RCU_DEBUG_1_RUNALONE_ACTIVE		REG_BIT(2)
+#define   RCU_DEBUG_1_CONTEXT_ACTIVE		REG_BIT(0)
+
 #define FORCEWAKE_ACK_GT			XE_REG(0x130044)
 
 /* Applicable for all FORCEWAKE_DOMAIN and FORCEWAKE_ACK_DOMAIN regs */
drivers/gpu/drm/xe/xe_device.c +2 -0
index ec5eedbbf320..77049c56c26c 100644
@@ -776,6 +776,8 @@ int xe_device_probe(struct xe_device *xe)
 
 	xe_debugfs_register(xe);
 
+	xe_eudebug_init_late(xe);
+
 	xe_hwmon_register(xe);
 
 	for_each_gt(gt, xe, id)
drivers/gpu/drm/xe/xe_device_types.h +3 -0
index 54ceeee7cf75..562cc8930533 100644
@@ -548,6 +548,9 @@ struct xe_device {
 
 		/** discovery_lock: used for discovery to block xe ioctls */
 		struct rw_semaphore discovery_lock;
+
+		/** @attention_scan: attention scan worker */
+		struct delayed_work attention_scan;
 	} eudebug;
 #endif
 
drivers/gpu/drm/xe/xe_eudebug.c +380 -1

427 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug.h +2 -0
index 3cd6bc7bb682..1fe86bec99e1 100644
@@ -20,6 +20,7 @@ int xe_eudebug_connect_ioctl(struct drm_device *dev,
 			     struct drm_file *file);
 
 void xe_eudebug_init(struct xe_device *xe);
+void xe_eudebug_init_late(struct xe_device *xe);
 void xe_eudebug_fini(struct xe_device *xe);
 void xe_eudebug_init_hw_engine(struct xe_hw_engine *hwe);
 
@@ -39,6 +40,7 @@ static inline int xe_eudebug_connect_ioctl(struct drm_device *dev,
 					   struct drm_file *file) { return 0; }
 
 static inline void xe_eudebug_init(struct xe_device *xe) { }
+static inline void xe_eudebug_init_late(struct xe_device *xe) { }
 static inline void xe_eudebug_fini(struct xe_device *xe) { }
 static inline void xe_eudebug_init_hw_engine(struct xe_hw_engine *hwe) { }
 
drivers/gpu/drm/xe/xe_eudebug_types.h +32 -0
index 6428905a0557..b885296fddbb 100644
@@ -105,6 +105,9 @@ struct xe_eudebug {
 	/** @discovery_work: worker to discover resources for target_task */
 	struct work_struct discovery_work;
 
+	/** eu_lock: guards operations on eus (eu thread control and attention) */
+	struct mutex eu_lock;
+
 	/** @events: kfifo queue of to-be-delivered events */
 	struct {
 		/** @lock: guards access to fifo */
@@ -202,4 +205,33 @@ struct xe_eudebug_event_exec_queue {
 	u64 lrc_handle[];
 };
 
+/**
+ * struct xe_eudebug_event_eu_attention - Internal event for EU attention
+ */
+struct xe_eudebug_event_eu_attention {
+	/** @base: base event */
+	struct xe_eudebug_event base;
+
+	/** @client_handle: client for the attention */
+	u64 client_handle;
+
+	/** @exec_queue_handle: handle of exec_queue which raised attention */
+	u64 exec_queue_handle;
+
+	/** @lrc_handle: lrc handle of the workload which raised attention */
+	u64 lrc_handle;
+
+	/** @flags: eu attention event flags, currently MBZ */
+	u32 flags;
+
+	/** @bitmask_size: size of the bitmask, specific to device */
+	u32 bitmask_size;
+
+	/**
+	 * @bitmask: reflects threads currently signalling attention,
+	 * starting from natural hardware order of DSS=0, eu=0
+	 */
+	u8 bitmask[];
+};
+
 #endif
drivers/gpu/drm/xe/xe_gt_debug.c new file +148 -0

148 lines not on this page · raw message

drivers/gpu/drm/xe/xe_gt_debug.h new file +21 -0
new file mode 100644
index 000000000000..3f13dbb17a5f
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __XE_GT_DEBUG_
+#define __XE_GT_DEBUG_
+
+#define TD_EU_ATTENTION_MAX_ROWS 2u
+
+#include "xe_gt_types.h"
+
+#define XE_GT_ATTENTION_TIMEOUT_MS 100
+
+int xe_gt_eu_threads_needing_attention(struct xe_gt *gt);
+
+int xe_gt_eu_attention_bitmap_size(struct xe_gt *gt);
+int xe_gt_eu_attention_bitmap(struct xe_gt *gt, u8 *bits,
+			      unsigned int bitmap_size);
+
+#endif
include/uapi/drm/xe_drm_eudebug.h +13 -0
index ac44e890152a..1e63bdede5f2 100644
@@ -27,12 +27,14 @@ struct drm_xe_eudebug_event {
 #define DRM_XE_EUDEBUG_EVENT_OPEN		2
 #define DRM_XE_EUDEBUG_EVENT_VM			3
 #define DRM_XE_EUDEBUG_EVENT_EXEC_QUEUE		4
+#define DRM_XE_EUDEBUG_EVENT_EU_ATTENTION	5
 
 	__u16 flags;
 #define DRM_XE_EUDEBUG_EVENT_CREATE		(1 << 0)
 #define DRM_XE_EUDEBUG_EVENT_DESTROY		(1 << 1)
 #define DRM_XE_EUDEBUG_EVENT_STATE_CHANGE	(1 << 2)
 #define DRM_XE_EUDEBUG_EVENT_NEED_ACK		(1 << 3)
+
 	__u64 seqno;
 	__u64 reserved;
 };
@@ -61,6 +63,17 @@ struct drm_xe_eudebug_event_exec_queue {
 	__u64 lrc_handle[];
 };
 
+struct drm_xe_eudebug_event_eu_attention {
+	struct drm_xe_eudebug_event base;
+
+	__u64 client_handle;
+	__u64 exec_queue_handle;
+	__u64 lrc_handle;
+	__u32 flags;
+	__u32 bitmask_size;
+	__u8 bitmask[];
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.34.1
#11 lore raw

PATCH 8/18 drm/xe/eudebug: Introduce EU control interface

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Introduce EU control functionality, which allows EU debugger to interrupt, resume, and inform about the current state of EU threads during execution. Provide an abstraction layer, so in the future guc will only need to provide appropriate callbacks.

Based on implementation created by authors and other folks within i915 driver.

v2: - checkpatch (Maciej)
- lrc index off by one fix (Mika)
- checkpatch (Tilak)
- 32bit fixes (Andrzej, Mika)
- find_resource_get for client (Mika)

---
 drivers/gpu/drm/xe/regs/xe_gt_regs.h  |   2 +
 drivers/gpu/drm/xe/xe_eudebug.c       | 516 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug_types.h |  24 ++
 drivers/gpu/drm/xe/xe_gt_debug.c      |  12 +-
 drivers/gpu/drm/xe/xe_gt_debug.h      |   6 +
 include/uapi/drm/xe_drm_eudebug.h     |  21 +-
 6 files changed, 561 insertions(+), 20 deletions(-)
drivers/gpu/drm/xe/regs/xe_gt_regs.h +2 -0
index 0129480dbd9d..a6225e4a8516 100644
@@ -476,6 +476,8 @@
 #define   THREAD_EX_ARB_MODE			REG_GENMASK(3, 2)
 #define   THREAD_EX_ARB_MODE_RR_AFTER_DEP	REG_FIELD_PREP(THREAD_EX_ARB_MODE, 0x2)
 
+#define TD_CLR(i)				XE_REG_MCR(0xe490 + (i) * 4)
+
 #define ROW_CHICKEN3				XE_REG_MCR(0xe49c, XE_REG_OPTION_MASKED)
 #define   XE2_EUPEND_CHK_FLUSH_DIS		REG_BIT(14)
 #define   DIS_FIX_EOT1_FLUSH			REG_BIT(9)
drivers/gpu/drm/xe/xe_eudebug.c +503 -13

589 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug_types.h +24 -0
index b885296fddbb..73b3f906da66 100644
@@ -18,8 +18,12 @@
 
 struct xe_device;
 struct task_struct;
+struct xe_eudebug;
 struct xe_eudebug_event;
+struct xe_hw_engine;
 struct workqueue_struct;
+struct xe_exec_queue;
+struct xe_lrc;
 
 #define CONFIG_DRM_XE_DEBUGGER_EVENT_QUEUE_SIZE 64
 
@@ -65,6 +69,24 @@ struct xe_eudebug_resources {
 	struct xe_eudebug_resource rt[XE_EUDEBUG_RES_TYPE_COUNT];
 };
 
+/**
+ * struct xe_eudebug_eu_control_ops - interface for eu thread
+ * state control backend
+ */
+struct xe_eudebug_eu_control_ops {
+	/** @interrupt_all: interrupts workload active on given hwe */
+	int (*interrupt_all)(struct xe_eudebug *e, struct xe_exec_queue *q,
+			     struct xe_lrc *lrc);
+
+	/** @resume: resumes threads reflected by bitmask active on given hwe */
+	int (*resume)(struct xe_eudebug *e, struct xe_exec_queue *q,
+		      struct xe_lrc *lrc, u8 *bitmap, unsigned int bitmap_size);
+
+	/** @stopped: returns bitmap reflecting threads which signal attention */
+	int (*stopped)(struct xe_eudebug *e, struct xe_exec_queue *q,
+		       struct xe_lrc *lrc, u8 *bitmap, unsigned int bitmap_size);
+};
+
 /**
  * struct xe_eudebug - Top level struct for eudebug: the connection
  */
@@ -128,6 +150,8 @@ struct xe_eudebug {
 		atomic_long_t seqno;
 	} events;
 
+	/** @ops operations for eu_control */
+	struct xe_eudebug_eu_control_ops *ops;
 };
 
 /**
drivers/gpu/drm/xe/xe_gt_debug.c +6 -6
index d36684c7514d..fcf32c7c77d8 100644
@@ -13,12 +13,12 @@
 #include "xe_pm.h"
 #include "xe_macros.h"
 
-static int xe_gt_foreach_dss_group_instance(struct xe_gt *gt,
-					    int (*fn)(struct xe_gt *gt,
-						      void *data,
-						      u16 group,
-						      u16 instance),
-					    void *data)
+int xe_gt_foreach_dss_group_instance(struct xe_gt *gt,
+				     int (*fn)(struct xe_gt *gt,
+					       void *data,
+					       u16 group,
+					       u16 instance),
+				     void *data)
 {
 	const enum xe_force_wake_domains fw_domains = XE_FW_GT | XE_FW_RENDER;
 	unsigned int dss;
drivers/gpu/drm/xe/xe_gt_debug.h +6 -0
index 3f13dbb17a5f..342082699ff6 100644
@@ -13,6 +13,12 @@
 #define XE_GT_ATTENTION_TIMEOUT_MS 100
 
 int xe_gt_eu_threads_needing_attention(struct xe_gt *gt);
+int xe_gt_foreach_dss_group_instance(struct xe_gt *gt,
+				     int (*fn)(struct xe_gt *gt,
+					       void *data,
+					       u16 group,
+					       u16 instance),
+				     void *data);
 
 int xe_gt_eu_attention_bitmap_size(struct xe_gt *gt);
 int xe_gt_eu_attention_bitmap(struct xe_gt *gt, u8 *bits,
include/uapi/drm/xe_drm_eudebug.h +20 -1
index 1e63bdede5f2..3c43aefefedd 100644
@@ -15,7 +15,8 @@ extern "C" {
  *
  * This ioctl is available in debug version 1.
  */
-#define DRM_XE_EUDEBUG_IOCTL_READ_EVENT _IO('j', 0x0)
+#define DRM_XE_EUDEBUG_IOCTL_READ_EVENT		_IO('j', 0x0)
+#define DRM_XE_EUDEBUG_IOCTL_EU_CONTROL		_IOWR('j', 0x2, struct drm_xe_eudebug_eu_control)
 
 /* XXX: Document events to match their internal counterparts when moved to xe_drm.h */
 struct drm_xe_eudebug_event {
@@ -74,6 +75,24 @@ struct drm_xe_eudebug_event_eu_attention {
 	__u8 bitmask[];
 };
 
+struct drm_xe_eudebug_eu_control {
+	__u64 client_handle;
+
+#define DRM_XE_EUDEBUG_EU_CONTROL_CMD_INTERRUPT_ALL	0
+#define DRM_XE_EUDEBUG_EU_CONTROL_CMD_STOPPED		1
+#define DRM_XE_EUDEBUG_EU_CONTROL_CMD_RESUME		2
+	__u32 cmd;
+	__u32 flags;
+
+	__u64 seqno;
+
+	__u64 exec_queue_handle;
+	__u64 lrc_handle;
+	__u32 reserved;
+	__u32 bitmask_size;
+	__u64 bitmask_ptr;
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.34.1
#12 lore raw

PATCH 9/18 drm/xe/eudebug: Add vm bind and vm bind ops

In reply to Mika Kuoppala

From: Christoph Manszewski <[email protected]>

Add events dedicated to track vma bind and vma unbind operations. The events are generated for operations performed on xe_vma MAP and UNMAP for boss and userptrs.

As one bind can result in multiple operations and fail in the middle, we want to store the events until full successful chain of operations can be relayed to debugger.

---
 drivers/gpu/drm/xe/xe_eudebug.c       | 316 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug.h       |  13 ++
 drivers/gpu/drm/xe/xe_eudebug_types.h |  29 +++
 drivers/gpu/drm/xe/xe_vm.c            |  16 +-
 drivers/gpu/drm/xe/xe_vm_types.h      |  13 ++
 include/uapi/drm/xe_drm_eudebug.h     |  64 ++++++
 6 files changed, 448 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_eudebug.c +315 -1
index ac41c2ac8001..785658b2e199 100644
@@ -824,7 +824,7 @@ static long xe_eudebug_read_event(struct xe_eudebug *d,
 		u64_to_user_ptr(arg);
 	struct drm_xe_eudebug_event user_event;
 	struct xe_eudebug_event *event;
-	const unsigned int max_event = DRM_XE_EUDEBUG_EVENT_EU_ATTENTION;
+	const unsigned int max_event = DRM_XE_EUDEBUG_EVENT_VM_BIND_OP;
 	long ret = 0;
 
 	if (XE_IOCTL_DBG(xe, copy_from_user(&user_event, user_orig, sizeof(user_event))))
@@ -2269,6 +2269,320 @@ void xe_eudebug_exec_queue_destroy(struct xe_file *xef, struct xe_exec_queue *q)
 	xe_eudebug_event_put(d, exec_queue_destroy_event(d, xef, q));
 }
 
+static int xe_eudebug_queue_bind_event(struct xe_eudebug *d,
+				       struct xe_vm *vm,
+				       struct xe_eudebug_event *event)
+{
+	struct xe_eudebug_event_envelope *env;
+
+	lockdep_assert_held_write(&vm->lock);
+
+	env = kmalloc(sizeof(*env), GFP_KERNEL);
+	if (!env)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&env->link);
+	env->event = event;
+
+	spin_lock(&vm->eudebug.lock);
+	list_add_tail(&env->link, &vm->eudebug.events);
+
+	if (event->type == DRM_XE_EUDEBUG_EVENT_VM_BIND_OP)
+		++vm->eudebug.ops;
+	spin_unlock(&vm->eudebug.lock);
+
+	return 0;
+}
+
+static int queue_vm_bind_event(struct xe_eudebug *d,
+			       struct xe_vm *vm,
+			       u64 client_handle,
+			       u64 vm_handle,
+			       u32 bind_flags,
+			       u32 num_ops, u64 *seqno)
+{
+	struct xe_eudebug_event_vm_bind *e;
+	struct xe_eudebug_event *event;
+	const u32 sz = sizeof(*e);
+	const u32 base_flags = DRM_XE_EUDEBUG_EVENT_STATE_CHANGE;
+
+	*seqno = atomic_long_inc_return(&d->events.seqno);
+
+	event = xe_eudebug_create_event(d, DRM_XE_EUDEBUG_EVENT_VM_BIND,
+					*seqno, base_flags, sz, GFP_KERNEL);
+	if (!event)
+		return -ENOMEM;
+
+	e = cast_event(e, event);
+	write_member(struct drm_xe_eudebug_event_vm_bind, e, client_handle, client_handle);
+	write_member(struct drm_xe_eudebug_event_vm_bind, e, vm_handle, vm_handle);
+	write_member(struct drm_xe_eudebug_event_vm_bind, e, flags, bind_flags);
+	write_member(struct drm_xe_eudebug_event_vm_bind, e, num_binds, num_ops);
+
+	/* If in discovery, no need to collect ops */
+	if (!completion_done(&d->discovery)) {
+		XE_WARN_ON(!num_ops);
+		return xe_eudebug_queue_event(d, event);
+	}
+
+	return xe_eudebug_queue_bind_event(d, vm, event);
+}
+
+static int vm_bind_event(struct xe_eudebug *d,
+			 struct xe_vm *vm,
+			 u32 num_ops,
+			 u64 *seqno)
+{
+	int h_c, h_vm;
+
+	h_c = find_handle(d->res, XE_EUDEBUG_RES_TYPE_CLIENT, vm->xef);
+	if (h_c < 0)
+		return h_c;
+
+	h_vm = find_handle(d->res, XE_EUDEBUG_RES_TYPE_VM, vm);
+	if (h_vm < 0)
+		return h_vm;
+
+	return queue_vm_bind_event(d, vm, h_c, h_vm, 0,
+				   num_ops, seqno);
+}
+
+static int vm_bind_op_event(struct xe_eudebug *d,
+			    struct xe_vm *vm,
+			    const u32 flags,
+			    const u64 bind_ref_seqno,
+			    const u64 num_extensions,
+			    u64 addr, u64 range,
+			    u64 *op_seqno)
+{
+	struct xe_eudebug_event_vm_bind_op *e;
+	struct xe_eudebug_event *event;
+	const u32 sz = sizeof(*e);
+
+	*op_seqno = atomic_long_inc_return(&d->events.seqno);
+
+	event = xe_eudebug_create_event(d, DRM_XE_EUDEBUG_EVENT_VM_BIND_OP,
+					*op_seqno, flags, sz, GFP_KERNEL);
+	if (!event)
+		return -ENOMEM;
+
+	e = cast_event(e, event);
+
+	write_member(struct drm_xe_eudebug_event_vm_bind_op, e, vm_bind_ref_seqno, bind_ref_seqno);
+	write_member(struct drm_xe_eudebug_event_vm_bind_op, e, num_extensions, num_extensions);
+	write_member(struct drm_xe_eudebug_event_vm_bind_op, e, addr, addr);
+	write_member(struct drm_xe_eudebug_event_vm_bind_op, e, range, range);
+
+	/* If in discovery, no need to collect ops */
+	if (!completion_done(&d->discovery))
+		return xe_eudebug_queue_event(d, event);
+
+	return xe_eudebug_queue_bind_event(d, vm, event);
+}
+
+static int vm_bind_op(struct xe_eudebug *d, struct xe_vm *vm,
+		      const u32 flags, const u64 bind_ref_seqno,
+		      u64 addr, u64 range)
+{
+	u64 op_seqno = 0;
+	u64 num_extensions = 0;
+	int ret;
+
+	ret = vm_bind_op_event(d, vm, flags, bind_ref_seqno, num_extensions,
+			       addr, range, &op_seqno);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+void xe_eudebug_vm_init(struct xe_vm *vm)
+{
+	INIT_LIST_HEAD(&vm->eudebug.events);
+	spin_lock_init(&vm->eudebug.lock);
+	vm->eudebug.ops = 0;
+	vm->eudebug.ref_seqno = 0;
+}
+
+void xe_eudebug_vm_bind_start(struct xe_vm *vm)
+{
+	struct xe_eudebug *d;
+	u64 seqno = 0;
+	int err;
+
+	if (!xe_vm_in_lr_mode(vm))
+		return;
+
+	d = xe_eudebug_get(vm->xef);
+	if (!d)
+		return;
+
+	lockdep_assert_held_write(&vm->lock);
+
+	if (XE_WARN_ON(!list_empty(&vm->eudebug.events)) ||
+	    XE_WARN_ON(vm->eudebug.ops) ||
+	    XE_WARN_ON(vm->eudebug.ref_seqno)) {
+		eu_err(d, "bind busy on %s",  __func__);
+		xe_eudebug_disconnect(d, -EINVAL);
+	}
+
+	err = vm_bind_event(d, vm, 0, &seqno);
+	if (err) {
+		eu_err(d, "error %d on %s", err, __func__);
+		xe_eudebug_disconnect(d, err);
+	}
+
+	spin_lock(&vm->eudebug.lock);
+	XE_WARN_ON(vm->eudebug.ref_seqno);
+	vm->eudebug.ref_seqno = seqno;
+	vm->eudebug.ops = 0;
+	spin_unlock(&vm->eudebug.lock);
+
+	xe_eudebug_put(d);
+}
+
+void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range)
+{
+	struct xe_eudebug *d;
+	u32 flags;
+
+	if (!xe_vm_in_lr_mode(vm))
+		return;
+
+	switch (op) {
+	case DRM_XE_VM_BIND_OP_MAP:
+	case DRM_XE_VM_BIND_OP_MAP_USERPTR:
+	{
+		flags = DRM_XE_EUDEBUG_EVENT_CREATE;
+		break;
+	}
+	case DRM_XE_VM_BIND_OP_UNMAP:
+	case DRM_XE_VM_BIND_OP_UNMAP_ALL:
+		flags = DRM_XE_EUDEBUG_EVENT_DESTROY;
+		break;
+	default:
+		flags = 0;
+		break;
+	}
+
+	if (!flags)
+		return;
+
+	d = xe_eudebug_get(vm->xef);
+	if (!d)
+		return;
+
+	xe_eudebug_event_put(d, vm_bind_op(d, vm, flags, 0, addr, range));
+}
+
+static struct xe_eudebug_event *fetch_bind_event(struct xe_vm * const vm)
+{
+	struct xe_eudebug_event_envelope *env;
+	struct xe_eudebug_event *e = NULL;
+
+	spin_lock(&vm->eudebug.lock);
+	env = list_first_entry_or_null(&vm->eudebug.events,
+				       struct xe_eudebug_event_envelope, link);
+	if (env) {
+		e = env->event;
+		list_del(&env->link);
+	}
+	spin_unlock(&vm->eudebug.lock);
+
+	kfree(env);
+
+	return e;
+}
+
+static void fill_vm_bind_fields(struct xe_vm *vm,
+				struct xe_eudebug_event *e,
+				bool ufence,
+				u32 bind_ops)
+{
+	struct xe_eudebug_event_vm_bind *eb = cast_event(eb, e);
+
+	eb->flags = ufence ?
+		DRM_XE_EUDEBUG_EVENT_VM_BIND_FLAG_UFENCE : 0;
+	eb->num_binds = bind_ops;
+}
+
+static void fill_vm_bind_op_fields(struct xe_vm *vm,
+				   struct xe_eudebug_event *e,
+				   u64 ref_seqno)
+{
+	struct xe_eudebug_event_vm_bind_op *op;
+
+	if (e->type != DRM_XE_EUDEBUG_EVENT_VM_BIND_OP)
+		return;
+
+	op = cast_event(op, e);
+	op->vm_bind_ref_seqno = ref_seqno;
+}
+
+void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int bind_err)
+{
+	struct xe_eudebug_event *e;
+	struct xe_eudebug *d;
+	u32 bind_ops;
+	u64 ref;
+
+	if (!xe_vm_in_lr_mode(vm))
+		return;
+
+	spin_lock(&vm->eudebug.lock);
+	ref = vm->eudebug.ref_seqno;
+	vm->eudebug.ref_seqno = 0;
+	bind_ops = vm->eudebug.ops;
+	vm->eudebug.ops = 0;
+	spin_unlock(&vm->eudebug.lock);
+
+	e = fetch_bind_event(vm);
+	if (!e)
+		return;
+
+	d = NULL;
+	if (!bind_err && ref) {
+		d = xe_eudebug_get(vm->xef);
+		if (d) {
+			if (bind_ops) {
+				fill_vm_bind_fields(vm, e, has_ufence, bind_ops);
+			} else {
+				/*
+				 * If there was no ops we are interested in,
+				 * we can omit the whole sequence
+				 */
+				xe_eudebug_put(d);
+				d = NULL;
+			}
+		}
+	}
+
+	while (e) {
+		int err = 0;
+
+		if (d) {
+			err = xe_eudebug_queue_event(d, e);
+			if (!err)
+				e = NULL;
+		}
+
+		if (err) {
+			xe_eudebug_disconnect(d, err);
+			xe_eudebug_put(d);
+			d = NULL;
+		}
+
+		kfree(e);
+
+		e = fetch_bind_event(vm);
+		if (e && ref)
+			fill_vm_bind_op_fields(vm, e, ref);
+	}
+
+	if (d)
+		xe_eudebug_put(d);
+}
+
 static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
 {
 	struct xe_exec_queue *q;
drivers/gpu/drm/xe/xe_eudebug.h +13 -0
index 1fe86bec99e1..ccc7202b3308 100644
@@ -5,11 +5,14 @@
 
 #ifndef _XE_EUDEBUG_H_
 
+#include <linux/types.h>
+
 struct drm_device;
 struct drm_file;
 struct xe_device;
 struct xe_file;
 struct xe_vm;
+struct xe_vma;
 struct xe_exec_queue;
 struct xe_hw_engine;
 
@@ -33,6 +36,11 @@ 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);
 
+void xe_eudebug_vm_init(struct xe_vm *vm);
+void xe_eudebug_vm_bind_start(struct xe_vm *vm);
+void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range);
+void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int err);
+
 #else
 
 static inline int xe_eudebug_connect_ioctl(struct drm_device *dev,
@@ -53,6 +61,11 @@ 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) { }
 
+static inline void xe_eudebug_vm_init(struct xe_vm *vm) { }
+static inline void xe_eudebug_vm_bind_start(struct xe_vm *vm) { }
+static inline void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range) { }
+static inline void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int err) { }
+
 #endif /* CONFIG_DRM_XE_EUDEBUG */
 
 #endif
drivers/gpu/drm/xe/xe_eudebug_types.h +29 -0
index 73b3f906da66..78739954967b 100644
@@ -177,6 +177,11 @@ struct xe_eudebug_event {
 	u8 data[];
 };
 
+struct xe_eudebug_event_envelope {
+	struct list_head link;
+	struct xe_eudebug_event *event;
+};
+
 /**
  * struct xe_eudebug_event_open - Internal event for client open/close
  */
@@ -258,4 +263,28 @@ struct xe_eudebug_event_eu_attention {
 	u8 bitmask[];
 };
 
+/**
+ * struct xe_eudebug_event_vm_bind - Internal event for vm bind/unbind operation
+ */
+struct xe_eudebug_event_vm_bind {
+	/** @base: base event */
+	struct xe_eudebug_event base;
+
+	u64 client_handle;
+	u64 vm_handle;
+
+	u32 flags;
+	u32 num_binds;
+};
+
+struct xe_eudebug_event_vm_bind_op {
+	/** @base: base event */
+	struct xe_eudebug_event base;
+	u64 vm_bind_ref_seqno;
+	u64 num_extensions;
+
+	u64 addr; /* Zero for unmap all ? */
+	u64 range; /* Zero for unmap all ? */
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +14 -2
index 9701a7efe257..eb10085bb455 100644
@@ -1412,6 +1412,8 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
 	for_each_tile(tile, xe, id)
 		xe_range_fence_tree_init(&vm->rftree[id]);
 
+	xe_eudebug_vm_init(vm);
+
 	vm->pt_ops = &xelp_pt_ops;
 
 	/*
@@ -1640,6 +1642,8 @@ static void vm_destroy_work_func(struct work_struct *w)
 	struct xe_tile *tile;
 	u8 id;
 
+	xe_eudebug_vm_bind_end(vm, 0, -ENOENT);
+
 	/* xe_vm_close_and_put was not called? */
 	xe_assert(xe, !vm->size);
 
@@ -2649,7 +2653,7 @@ static void vm_bind_ioctl_ops_fini(struct xe_vm *vm, struct xe_vma_ops *vops,
 				   struct dma_fence *fence)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, vops->q);
-	struct xe_user_fence *ufence;
+	struct xe_user_fence *ufence = NULL;
 	struct xe_vma_op *op;
 	int i;
 
@@ -2664,6 +2668,9 @@ static void vm_bind_ioctl_ops_fini(struct xe_vm *vm, struct xe_vma_ops *vops,
 			xe_vma_destroy(gpuva_to_vma(op->base.remap.unmap->va),
 				       fence);
 	}
+
+	xe_eudebug_vm_bind_end(vm, ufence, 0);
+
 	if (ufence)
 		xe_sync_ufence_put(ufence);
 	for (i = 0; i < vops->num_syncs; i++)
@@ -3072,6 +3079,8 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		if (err)
 			goto unwind_ops;
 
+		xe_eudebug_vm_bind_op_add(vm, op, addr, range);
+
 #ifdef TEST_VM_OPS_ERROR
 		if (flags & FORCE_OP_ERROR) {
 			vops.inject_error = true;
@@ -3095,8 +3104,11 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
 unwind_ops:
-	if (err && err != -ENODATA)
+	if (err && err != -ENODATA) {
+		xe_eudebug_vm_bind_end(vm, num_ufence > 0, err);
 		vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	}
+
 	xe_vma_ops_fini(&vops);
 	for (i = args->num_binds - 1; i >= 0; --i)
 		if (ops[i])
drivers/gpu/drm/xe/xe_vm_types.h +13 -0
index 7f9a303e51d8..557b047ebdd7 100644
@@ -282,6 +282,19 @@ struct xe_vm {
 	bool batch_invalidate_tlb;
 	/** @xef: XE file handle for tracking this VM's drm client */
 	struct xe_file *xef;
+
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+	struct {
+		/** @lock: Lock for eudebug_bind members */
+		spinlock_t lock;
+		/** @events: List of vm bind ops gathered */
+		struct list_head events;
+		/** @ops: How many operations we have stored */
+		u32 ops;
+		/** @ref_seqno: Reference to the VM_BIND that the ops relate */
+		u64 ref_seqno;
+	} eudebug;
+#endif
 };
 
 /** struct xe_vma_op_map - VMA map operation */
include/uapi/drm/xe_drm_eudebug.h +64 -0

76 lines not on this page · raw message

Signature · 1 line
2.34.1
#13 lore raw

PATCH 10/18 drm/xe/eudebug: Add UFENCE events with acks

In reply to Mika Kuoppala

When vma is in place, debugger needs to intercept before userspace proceeds with the workload. For example to install a breakpoint in a eu shader.

Attach debugger in xe_user_fence, send UFENCE event and stall normal user fence signal path to yield if there is debugger attached to ufence.

When ack (ioctl) is received for the corresponding seqno, signal ufence.

v2: - return err instead of 0 to guarantee signalling (Dominik)
- checkpatch (Tilak)
- Kconfig (Mika, Andrzej)
- use lock instead of cmpxchg (Mika)

---
 drivers/gpu/drm/xe/xe_eudebug.c       | 277 +++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug.h       |  15 ++
 drivers/gpu/drm/xe/xe_eudebug_types.h |  13 ++
 drivers/gpu/drm/xe/xe_exec.c          |   2 +-
 drivers/gpu/drm/xe/xe_sync.c          |  45 +++--
 drivers/gpu/drm/xe/xe_sync.h          |   8 +-
 drivers/gpu/drm/xe/xe_sync_types.h    |  28 ++-
 drivers/gpu/drm/xe/xe_vm.c            |   4 +-
 include/uapi/drm/xe_drm_eudebug.h     |  13 ++
 9 files changed, 377 insertions(+), 28 deletions(-)
drivers/gpu/drm/xe/xe_eudebug.c +273 -4
index 785658b2e199..68c33d2e8e77 100644
@@ -32,6 +32,7 @@
 #include "xe_reg_sr.h"
 #include "xe_rtp.h"
 #include "xe_sched_job.h"
+#include "xe_sync.h"
 #include "xe_vm.h"
 #include "xe_wa.h"
 
@@ -239,11 +240,119 @@ static void xe_eudebug_free(struct kref *ref)
 	kfree_rcu(d, rcu);
 }
 
-static void xe_eudebug_put(struct xe_eudebug *d)
+void xe_eudebug_put(struct xe_eudebug *d)
 {
 	kref_put(&d->ref, xe_eudebug_free);
 }
 
+struct xe_eudebug_ack {
+	struct rb_node rb_node;
+	u64 seqno;
+	u64 ts_insert;
+	struct xe_user_fence *ufence;
+};
+
+#define fetch_ack(x) rb_entry(x, struct xe_eudebug_ack, rb_node)
+
+static int compare_ack(const u64 a, const u64 b)
+{
+	if (a < b)
+		return -1;
+	else if (a > b)
+		return 1;
+
+	return 0;
+}
+
+static int ack_insert_cmp(struct rb_node * const node,
+			  const struct rb_node * const p)
+{
+	return compare_ack(fetch_ack(node)->seqno,
+			   fetch_ack(p)->seqno);
+}
+
+static int ack_lookup_cmp(const void * const key,
+			  const struct rb_node * const node)
+{
+	return compare_ack(*(const u64 *)key,
+			   fetch_ack(node)->seqno);
+}
+
+static struct xe_eudebug_ack *remove_ack(struct xe_eudebug *d, u64 seqno)
+{
+	struct rb_root * const root = &d->acks.tree;
+	struct rb_node *node;
+
+	spin_lock(&d->acks.lock);
+	node = rb_find(&seqno, root, ack_lookup_cmp);
+	if (node)
+		rb_erase(node, root);
+	spin_unlock(&d->acks.lock);
+
+	if (!node)
+		return NULL;
+
+	return rb_entry_safe(node, struct xe_eudebug_ack, rb_node);
+}
+
+static void ufence_signal_worker(struct work_struct *w)
+{
+	struct xe_user_fence * const ufence =
+		container_of(w, struct xe_user_fence, eudebug.worker);
+
+	if (READ_ONCE(ufence->signalled))
+		xe_sync_ufence_signal(ufence);
+
+	xe_sync_ufence_put(ufence);
+}
+
+static void kick_ufence_worker(struct xe_user_fence *f)
+{
+	queue_work(f->xe->eudebug.ordered_wq, &f->eudebug.worker);
+}
+
+static void handle_ack(struct xe_eudebug *d, struct xe_eudebug_ack *ack,
+		       bool on_disconnect)
+{
+	struct xe_user_fence *f = ack->ufence;
+	u64 signalled_by;
+	bool signal = false;
+
+	spin_lock(&f->eudebug.lock);
+	if (!f->eudebug.signalled_seqno) {
+		f->eudebug.signalled_seqno = ack->seqno;
+		signal = true;
+	}
+	signalled_by = f->eudebug.signalled_seqno;
+	spin_unlock(&f->eudebug.lock);
+
+	if (signal)
+		kick_ufence_worker(f);
+	else
+		xe_sync_ufence_put(f);
+
+	eu_dbg(d, "ACK: seqno=%llu: signalled by %llu (%s) (held %lluus)",
+	       ack->seqno, signalled_by,
+	       on_disconnect ? "disconnect" : "debugger",
+	       ktime_us_delta(ktime_get(), ack->ts_insert));
+
+	kfree(ack);
+}
+
+static void release_acks(struct xe_eudebug *d)
+{
+	struct xe_eudebug_ack *ack, *n;
+	struct rb_root root;
+
+	spin_lock(&d->acks.lock);
+	root = d->acks.tree;
+	d->acks.tree = RB_ROOT;
+	spin_unlock(&d->acks.lock);
+
+	rbtree_postorder_for_each_entry_safe(ack, n, &root, rb_node)
+		handle_ack(d, ack, true);
+}
+
 static struct task_struct *find_get_target(const pid_t nr)
 {
 	struct task_struct *task;
@@ -328,6 +437,8 @@ static bool xe_eudebug_detach(struct xe_device *xe,
 
 	eu_dbg(d, "session %lld detached with %d", d->session, err);
 
+	release_acks(d);
+
 	/* Our ref with the connection_link */
 	xe_eudebug_put(d);
 
@@ -453,7 +564,7 @@ _xe_eudebug_get(struct xe_file *xef)
 	return d;
 }
 
-static struct xe_eudebug *
+struct xe_eudebug *
 xe_eudebug_get(struct xe_file *xef)
 {
 	struct xe_eudebug *d;
@@ -824,7 +935,7 @@ static long xe_eudebug_read_event(struct xe_eudebug *d,
 		u64_to_user_ptr(arg);
 	struct drm_xe_eudebug_event user_event;
 	struct xe_eudebug_event *event;
-	const unsigned int max_event = DRM_XE_EUDEBUG_EVENT_VM_BIND_OP;
+	const unsigned int max_event = DRM_XE_EUDEBUG_EVENT_VM_BIND_UFENCE;
 	long ret = 0;
 
 	if (XE_IOCTL_DBG(xe, copy_from_user(&user_event, user_orig, sizeof(user_event))))
@@ -899,6 +1010,44 @@ static long xe_eudebug_read_event(struct xe_eudebug *d,
 	return ret;
 }
 
+static long
+xe_eudebug_ack_event_ioctl(struct xe_eudebug *d,
+			   const unsigned int cmd,
+			   const u64 arg)
+{
+	struct drm_xe_eudebug_ack_event __user * const user_ptr =
+		u64_to_user_ptr(arg);
+	struct drm_xe_eudebug_ack_event user_arg;
+	struct xe_eudebug_ack *ack;
+	struct xe_device *xe = d->xe;
+
+	if (XE_IOCTL_DBG(xe, _IOC_SIZE(cmd) < sizeof(user_arg)))
+		return -EINVAL;
+
+	/* Userland write */
+	if (XE_IOCTL_DBG(xe, !(_IOC_DIR(cmd) & _IOC_WRITE)))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, copy_from_user(&user_arg,
+					    user_ptr,
+					    sizeof(user_arg))))
+		return -EFAULT;
+
+	if (XE_IOCTL_DBG(xe, user_arg.flags))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, xe_eudebug_detached(d)))
+		return -ENOTCONN;
+
+	ack = remove_ack(d, user_arg.seqno);
+	if (XE_IOCTL_DBG(xe, !ack))
+		return -EINVAL;
+
+	handle_ack(d, ack, false);
+
+	return 0;
+}
+
 static int do_eu_control(struct xe_eudebug *d,
 			 const struct drm_xe_eudebug_eu_control * const arg,
 			 struct drm_xe_eudebug_eu_control __user * const user_ptr)
@@ -1090,7 +1239,10 @@ static long xe_eudebug_ioctl(struct file *file,
 		ret = xe_eudebug_eu_control(d, arg);
 		eu_dbg(d, "ioctl cmd=EU_CONTROL ret=%ld\n", ret);
 		break;
-
+	case DRM_XE_EUDEBUG_IOCTL_ACK_EVENT:
+		ret = xe_eudebug_ack_event_ioctl(d, cmd, arg);
+		eu_dbg(d, "ioctl cmd=EVENT_ACK ret=%ld\n", ret);
+		break;
 	default:
 		ret = -EINVAL;
 	}
@@ -1764,6 +1916,9 @@ xe_eudebug_connect(struct xe_device *xe,
 	INIT_KFIFO(d->events.fifo);
 	INIT_WORK(&d->discovery_work, discovery_work_fn);
 
+	spin_lock_init(&d->acks.lock);
+	d->acks.tree = RB_ROOT;
+
 	d->res = xe_eudebug_resources_alloc();
 	if (IS_ERR(d->res)) {
 		err = PTR_ERR(d->res);
@@ -2396,6 +2551,70 @@ static int vm_bind_op(struct xe_eudebug *d, struct xe_vm *vm,
 	return 0;
 }
 
+static int xe_eudebug_track_ufence(struct xe_eudebug *d,
+				   struct xe_user_fence *f,
+				   u64 seqno)
+{
+	struct xe_eudebug_ack *ack;
+	struct rb_node *old;
+
+	ack = kzalloc(sizeof(*ack), GFP_KERNEL);
+	if (!ack)
+		return -ENOMEM;
+
+	ack->seqno = seqno;
+	ack->ts_insert = ktime_get();
+
+	spin_lock(&d->acks.lock);
+	old = rb_find_add(&ack->rb_node,
+			  &d->acks.tree, ack_insert_cmp);
+	if (!old) {
+		kref_get(&f->refcount);
+		ack->ufence = f;
+	}
+	spin_unlock(&d->acks.lock);
+
+	if (old) {
+		eu_dbg(d, "ACK: seqno=%llu: already exists", seqno);
+		kfree(ack);
+		return -EEXIST;
+	}
+
+	eu_dbg(d, "ACK: seqno=%llu: tracking started", seqno);
+
+	return 0;
+}
+
+static int vm_bind_ufence_event(struct xe_eudebug *d,
+				struct xe_user_fence *ufence)
+{
+	struct xe_eudebug_event *event;
+	struct xe_eudebug_event_vm_bind_ufence *e;
+	const u32 sz = sizeof(*e);
+	const u32 flags = DRM_XE_EUDEBUG_EVENT_CREATE |
+		DRM_XE_EUDEBUG_EVENT_NEED_ACK;
+	u64 seqno;
+	int ret;
+
+	seqno = atomic_long_inc_return(&d->events.seqno);
+
+	event = xe_eudebug_create_event(d, DRM_XE_EUDEBUG_EVENT_VM_BIND_UFENCE,
+					seqno, flags, sz, GFP_KERNEL);
+	if (!event)
+		return -ENOMEM;
+
+	e = cast_event(e, event);
+
+	write_member(struct drm_xe_eudebug_event_vm_bind_ufence,
+		     e, vm_bind_ref_seqno, ufence->eudebug.bind_ref_seqno);
+
+	ret = xe_eudebug_track_ufence(d, ufence, seqno);
+	if (!ret)
+		ret = xe_eudebug_queue_event(d, event);
+
+	return ret;
+}
+
 void xe_eudebug_vm_init(struct xe_vm *vm)
 {
 	INIT_LIST_HEAD(&vm->eudebug.events);
@@ -2583,6 +2802,24 @@ void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int bind_err)
 		xe_eudebug_put(d);
 }
 
+int xe_eudebug_vm_bind_ufence(struct xe_user_fence *ufence)
+{
+	struct xe_eudebug *d;
+	int err;
+
+	d = ufence->eudebug.debugger;
+	if (!d || xe_eudebug_detached(d))
+		return -ENOTCONN;
+
+	err = vm_bind_ufence_event(d, ufence);
+	if (err) {
+		eu_err(d, "error %d on %s", err, __func__);
+		xe_eudebug_disconnect(d, err);
+	}
+
+	return err;
+}
+
 static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
 {
 	struct xe_exec_queue *q;
@@ -2675,3 +2912,35 @@ static void discovery_work_fn(struct work_struct *work)
 
 	xe_eudebug_put(d);
 }
+
+void xe_eudebug_ufence_init(struct xe_user_fence *ufence,
+			    struct xe_file *xef,
+			    struct xe_vm *vm)
+{
+	u64 bind_ref;
+
+	spin_lock(&vm->eudebug.lock);
+	bind_ref = vm->eudebug.ref_seqno;
+	spin_unlock(&vm->eudebug.lock);
+
+	spin_lock_init(&ufence->eudebug.lock);
+	INIT_WORK(&ufence->eudebug.worker, ufence_signal_worker);
+
+	ufence->eudebug.signalled_seqno = 0;
+
+	if (bind_ref) {
+		ufence->eudebug.debugger = xe_eudebug_get(xef);
+
+		if (ufence->eudebug.debugger)
+			ufence->eudebug.bind_ref_seqno = bind_ref;
+	}
+}
+
+void xe_eudebug_ufence_fini(struct xe_user_fence *ufence)
+{
+	if (!ufence->eudebug.debugger)
+		return;
+
+	xe_eudebug_put(ufence->eudebug.debugger);
+	ufence->eudebug.debugger = NULL;
+}
drivers/gpu/drm/xe/xe_eudebug.h +15 -0
index ccc7202b3308..ae720a1b05d9 100644
@@ -15,6 +15,7 @@ struct xe_vm;
 struct xe_vma;
 struct xe_exec_queue;
 struct xe_hw_engine;
+struct xe_user_fence;
 
 #if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
 
@@ -41,6 +42,13 @@ void xe_eudebug_vm_bind_start(struct xe_vm *vm);
 void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range);
 void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int err);
 
+int xe_eudebug_vm_bind_ufence(struct xe_user_fence *ufence);
+void xe_eudebug_ufence_init(struct xe_user_fence *ufence, struct xe_file *xef, struct xe_vm *vm);
+void xe_eudebug_ufence_fini(struct xe_user_fence *ufence);
+
+struct xe_eudebug *xe_eudebug_get(struct xe_file *xef);
+void xe_eudebug_put(struct xe_eudebug *d);
+
 #else
 
 static inline int xe_eudebug_connect_ioctl(struct drm_device *dev,
@@ -66,6 +74,13 @@ static inline void xe_eudebug_vm_bind_start(struct xe_vm *vm) { }
 static inline void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range) { }
 static inline void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int err) { }
 
+static inline int xe_eudebug_vm_bind_ufence(struct xe_user_fence *ufence) { return 0; }
+static inline void xe_eudebug_ufence_init(struct xe_user_fence *ufence, struct xe_file *xef, struct xe_vm *vm) { }
+static inline void xe_eudebug_ufence_fini(struct xe_user_fence *ufence) { }
+
+static inline struct xe_eudebug *xe_eudebug_get(struct xe_file *xef) { return NULL; }
+static inline void xe_eudebug_put(struct xe_eudebug *d) { }
+
 #endif /* CONFIG_DRM_XE_EUDEBUG */
 
 #endif
drivers/gpu/drm/xe/xe_eudebug_types.h +13 -0
index 78739954967b..aae7bc476c7e 100644
@@ -150,6 +150,14 @@ struct xe_eudebug {
 		atomic_long_t seqno;
 	} events;
 
+	/* user fences tracked by this debugger */
+	struct {
+		/** @lock: guards access to tree */
+		spinlock_t lock;
+
+		struct rb_root tree;
+	} acks;
+
 	/** @ops operations for eu_control */
 	struct xe_eudebug_eu_control_ops *ops;
 };
@@ -287,4 +295,9 @@ struct xe_eudebug_event_vm_bind_op {
 	u64 range; /* Zero for unmap all ? */
 };
 
+struct xe_eudebug_event_vm_bind_ufence {
+	struct xe_eudebug_event base;
+	u64 vm_bind_ref_seqno;
+};
+
 #endif
drivers/gpu/drm/xe/xe_exec.c +1 -1
index 7b38485817dc..052edd41dc4e 100644
@@ -160,7 +160,7 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	vm = q->vm;
 
 	for (num_syncs = 0; num_syncs < args->num_syncs; num_syncs++) {
-		err = xe_sync_entry_parse(xe, xef, &syncs[num_syncs],
+		err = xe_sync_entry_parse(xe, xef, vm, &syncs[num_syncs],
 					  &syncs_user[num_syncs], SYNC_PARSE_FLAG_EXEC |
 					  (xe_vm_in_lr_mode(vm) ?
 					   SYNC_PARSE_FLAG_LR_MODE : 0));
drivers/gpu/drm/xe/xe_sync.c +29 -16

102 lines not on this page · raw message

drivers/gpu/drm/xe/xe_sync.h +7 -1
index 256ffc1e54dc..f5bec2b1b4f6 100644
@@ -9,8 +9,12 @@
 #include "xe_sync_types.h"
 
 struct xe_device;
-struct xe_exec_queue;
 struct xe_file;
+struct xe_exec_queue;
+struct drm_syncobj;
+struct dma_fence;
+struct dma_fence_chain;
+struct drm_xe_sync;
 struct xe_sched_job;
 struct xe_vm;
 
@@ -19,6 +23,7 @@ struct xe_vm;
 #define SYNC_PARSE_FLAG_DISALLOW_USER_FENCE	BIT(2)
 
 int xe_sync_entry_parse(struct xe_device *xe, struct xe_file *xef,
+			struct xe_vm *vm,
 			struct xe_sync_entry *sync,
 			struct drm_xe_sync __user *sync_user,
 			unsigned int flags);
@@ -40,5 +45,6 @@ struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence);
 struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync);
 void xe_sync_ufence_put(struct xe_user_fence *ufence);
 int xe_sync_ufence_get_status(struct xe_user_fence *ufence);
+void xe_sync_ufence_signal(struct xe_user_fence *ufence);
 
 #endif
drivers/gpu/drm/xe/xe_sync_types.h +23 -5
index 30ac3f51993b..dcd3165e66a7 100644
@@ -6,13 +6,31 @@
 #ifndef _XE_SYNC_TYPES_H_
 #define _XE_SYNC_TYPES_H_
 
+#include <linux/dma-fence-array.h>
+#include <linux/kref.h>
+#include <linux/spinlock.h>
 #include <linux/types.h>
 
-struct drm_syncobj;
-struct dma_fence;
-struct dma_fence_chain;
-struct drm_xe_sync;
-struct user_fence;
+struct xe_user_fence {
+	struct xe_device *xe;
+	struct kref refcount;
+	struct dma_fence_cb cb;
+	struct work_struct worker;
+	struct mm_struct *mm;
+	u64 __user *addr;
+	u64 value;
+	int signalled;
+
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+	struct {
+		spinlock_t lock;
+		struct xe_eudebug *debugger;
+		u64 bind_ref_seqno;
+		u64 signalled_seqno;
+		struct work_struct worker;
+	} eudebug;
+#endif
+};
 
 struct xe_sync_entry {
 	struct drm_syncobj *syncobj;
drivers/gpu/drm/xe/xe_vm.c +3 -1
index eb10085bb455..a836dfc5a86f 100644
@@ -3031,9 +3031,11 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		}
 	}
 
+	xe_eudebug_vm_bind_start(vm);
+
 	syncs_user = u64_to_user_ptr(args->syncs);
 	for (num_syncs = 0; num_syncs < args->num_syncs; num_syncs++) {
-		err = xe_sync_entry_parse(xe, xef, &syncs[num_syncs],
+		err = xe_sync_entry_parse(xe, xef, vm, &syncs[num_syncs],
 					  &syncs_user[num_syncs],
 					  (xe_vm_in_lr_mode(vm) ?
 					   SYNC_PARSE_FLAG_LR_MODE : 0) |
include/uapi/drm/xe_drm_eudebug.h +13 -0
index 44cf1b699589..b2f6038ccc59 100644
@@ -17,6 +17,7 @@ extern "C" {
  */
 #define DRM_XE_EUDEBUG_IOCTL_READ_EVENT		_IO('j', 0x0)
 #define DRM_XE_EUDEBUG_IOCTL_EU_CONTROL		_IOWR('j', 0x2, struct drm_xe_eudebug_eu_control)
+#define DRM_XE_EUDEBUG_IOCTL_ACK_EVENT		_IOW('j', 0x4, struct drm_xe_eudebug_ack_event)
 
 /* XXX: Document events to match their internal counterparts when moved to xe_drm.h */
 struct drm_xe_eudebug_event {
@@ -31,6 +32,7 @@ struct drm_xe_eudebug_event {
 #define DRM_XE_EUDEBUG_EVENT_EU_ATTENTION	5
 #define DRM_XE_EUDEBUG_EVENT_VM_BIND		6
 #define DRM_XE_EUDEBUG_EVENT_VM_BIND_OP		7
+#define DRM_XE_EUDEBUG_EVENT_VM_BIND_UFENCE	8
 
 	__u16 flags;
 #define DRM_XE_EUDEBUG_EVENT_CREATE		(1 << 0)
@@ -157,6 +159,17 @@ struct drm_xe_eudebug_event_vm_bind_op {
 	__u64 range; /* XXX: Zero for unmap all? */
 };
 
+struct drm_xe_eudebug_event_vm_bind_ufence {
+	struct drm_xe_eudebug_event base;
+	__u64 vm_bind_ref_seqno; /* *_event_vm_bind.base.seqno */
+};
+
+struct drm_xe_eudebug_ack_event {
+	__u32 type;
+	__u32 flags; /* MBZ */
+	__u64 seqno;
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.34.1
#14 lore raw

PATCH 11/18 drm/xe/eudebug: vm open/pread/pwrite

In reply to Mika Kuoppala

Debugger needs access to the client's vm to read and write. For example inspecting ISA/ELF and setting up breakpoints.

Add ioctl to open target vm with debugger client and vm_handle and hook up pread/pwrite possibility.

Open will take timeout argument so that standard fsync can be used for explicit flushing between cpu/gpu for the target vm.

Implement this for bo backed storage. userptr will be done in following patch.

v2: - checkpatch (Maciej)
- 32bit fixes (Andrzej)
- bo_vmap (Mika)
- fix vm leak if can't allocate k_buffer (Mika)
- assert vm write held for vma (Matthew)

---
 drivers/gpu/drm/xe/regs/xe_gt_regs.h |  24 ++
 drivers/gpu/drm/xe/xe_eudebug.c      | 480 +++++++++++++++++++++++++++
 include/uapi/drm/xe_drm_eudebug.h    |  18 +
 3 files changed, 522 insertions(+)
drivers/gpu/drm/xe/regs/xe_gt_regs.h +24 -0
index a6225e4a8516..c304253090b2 100644
@@ -549,6 +549,30 @@
 #define   CCS_MODE_CSLICE(cslice, ccs) \
 	((ccs) << ((cslice) * CCS_MODE_CSLICE_WIDTH))
 
+#define RCU_ASYNC_FLUSH				XE_REG(0x149fc)
+#define   RCU_ASYNC_FLUSH_IN_PROGRESS	REG_BIT(31)
+#define   RCU_ASYNC_FLUSH_ENGINE_ID_SHIFT	28
+#define   RCU_ASYNC_FLUSH_ENGINE_ID_DECODE1 REG_BIT(26)
+#define   RCU_ASYNC_FLUSH_AMFS		REG_BIT(8)
+#define   RCU_ASYNC_FLUSH_PREFETCH	REG_BIT(7)
+#define   RCU_ASYNC_FLUSH_DATA_PORT	REG_BIT(6)
+#define   RCU_ASYNC_FLUSH_DATA_CACHE	REG_BIT(5)
+#define   RCU_ASYNC_FLUSH_HDC_PIPELINE	REG_BIT(4)
+#define   RCU_ASYNC_INVALIDATE_HDC_PIPELINE REG_BIT(3)
+#define   RCU_ASYNC_INVALIDATE_CONSTANT_CACHE REG_BIT(2)
+#define   RCU_ASYNC_INVALIDATE_TEXTURE_CACHE REG_BIT(1)
+#define   RCU_ASYNC_INVALIDATE_INSTRUCTION_CACHE REG_BIT(0)
+#define   RCU_ASYNC_FLUSH_AND_INVALIDATE_ALL ( \
+	RCU_ASYNC_FLUSH_AMFS | \
+	RCU_ASYNC_FLUSH_PREFETCH | \
+	RCU_ASYNC_FLUSH_DATA_PORT | \
+	RCU_ASYNC_FLUSH_DATA_CACHE | \
+	RCU_ASYNC_FLUSH_HDC_PIPELINE | \
+	RCU_ASYNC_INVALIDATE_HDC_PIPELINE | \
+	RCU_ASYNC_INVALIDATE_CONSTANT_CACHE | \
+	RCU_ASYNC_INVALIDATE_TEXTURE_CACHE | \
+	RCU_ASYNC_INVALIDATE_INSTRUCTION_CACHE)
+
 #define RCU_DEBUG_1				XE_REG(0x14a00)
 #define   RCU_DEBUG_1_ENGINE_STATUS		REG_GENMASK(2, 0)
 #define   RCU_DEBUG_1_RUNALONE_ACTIVE		REG_BIT(2)
drivers/gpu/drm/xe/xe_eudebug.c +480 -0

510 lines not on this page · raw message

include/uapi/drm/xe_drm_eudebug.h +18 -0
index b2f6038ccc59..9917280400d6 100644
@@ -18,6 +18,7 @@ extern "C" {
 #define DRM_XE_EUDEBUG_IOCTL_READ_EVENT		_IO('j', 0x0)
 #define DRM_XE_EUDEBUG_IOCTL_EU_CONTROL		_IOWR('j', 0x2, struct drm_xe_eudebug_eu_control)
 #define DRM_XE_EUDEBUG_IOCTL_ACK_EVENT		_IOW('j', 0x4, struct drm_xe_eudebug_ack_event)
+#define DRM_XE_EUDEBUG_IOCTL_VM_OPEN		_IOW('j', 0x1, struct drm_xe_eudebug_vm_open)
 
 /* XXX: Document events to match their internal counterparts when moved to xe_drm.h */
 struct drm_xe_eudebug_event {
@@ -170,6 +171,23 @@ struct drm_xe_eudebug_ack_event {
 	__u64 seqno;
 };
 
+struct drm_xe_eudebug_vm_open {
+	/** @extensions: Pointer to the first extension struct, if any */
+	__u64 extensions;
+
+	/** @client_handle: id of client */
+	__u64 client_handle;
+
+	/** @vm_handle: id of vm */
+	__u64 vm_handle;
+
+	/** @flags: flags */
+	__u64 flags;
+
+	/** @timeout_ns: Timeout value in nanoseconds operations (fsync) */
+	__u64 timeout_ns;
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.34.1
#15 lore raw

PATCH 12/18 drm/xe/eudebug: implement userptr_vma access

In reply to Mika Kuoppala

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

---
 drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
 drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_vm.h      |  3 +++
 3 files changed, 51 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index edad6d533d0b..b09d7414cfe3 100644
@@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
 		return ret;
 	}
 
-	return -EINVAL;
+	return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write);
 }
 
 static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +47 -0

50 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..99b9a9b011de 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
maintainer
#16 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

Quoted 36 lines · Mika Kuoppala

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

---
 drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
 drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_vm.h      |  3 +++
 3 files changed, 51 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index edad6d533d0b..b09d7414cfe3 100644
@@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
 		return ret;
 	}
 
-	return -EINVAL;
+	return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write);
 }
 
 static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +0 -0
index a836dfc5a86f..5f891e76993b 100644

drivers/gpu/drm/xe/xe_vm.c void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) · line 3426

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
@@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
 	}
 	kvfree(snap);
 }
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write)

Nit:

s/xe_uvma_access/xe_vm_uvma_access/

Matt

Quoted 58 lines
drivers/gpu/drm/xe/xe_vm.c +44 -0
+{
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	struct xe_userptr *up = &uvma->userptr;
+	struct xe_res_cursor cur = {};
+	int cur_len, ret = 0;
+
+	while (true) {
+		down_read(&vm->userptr.notifier_lock);
+		if (!xe_vma_userptr_check_repin(uvma))
+			break;
+
+		spin_lock(&vm->userptr.invalidated_lock);
+		list_del_init(&uvma->userptr.invalidate_link);
+		spin_unlock(&vm->userptr.invalidated_lock);
+
+		up_read(&vm->userptr.notifier_lock);
+		ret = xe_vma_userptr_pin_pages(uvma);
+		if (ret)
+			return ret;
+	}
+
+	if (!up->sg) {
+		ret = -EINVAL;
+		goto out_unlock_notifier;
+	}
+
+	for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining;
+	     xe_res_next(&cur, cur_len)) {
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
+
+		cur_len = min(cur.size, cur.remaining);
+		if (write)
+			memcpy(ptr, buf, cur_len);
+		else
+			memcpy(buf, ptr, cur_len);
+		kunmap_local(ptr);
+		buf += cur_len;
+	}
+	ret = len;
+
+out_unlock_notifier:
+	up_read(&vm->userptr.notifier_lock);
+	return ret;
+}
drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..99b9a9b011de 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
maintainer
#17 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

In reply to Mika Kuoppala
Quoted 60 lines · Mika Kuoppala

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

---
 drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
 drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_vm.h      |  3 +++
 3 files changed, 51 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index edad6d533d0b..b09d7414cfe3 100644
@@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
 		return ret;
 	}
 
-	return -EINVAL;
+	return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write);
 }
 
 static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +21 -0
index a836dfc5a86f..5f891e76993b 100644
@@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
 	}
 	kvfree(snap);
 }
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write)
+{
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	struct xe_userptr *up = &uvma->userptr;
+	struct xe_res_cursor cur = {};
+	int cur_len, ret = 0;
+
+	while (true) {
+		down_read(&vm->userptr.notifier_lock);
+		if (!xe_vma_userptr_check_repin(uvma))
+			break;
+
+		spin_lock(&vm->userptr.invalidated_lock);
+		list_del_init(&uvma->userptr.invalidate_link);
+		spin_unlock(&vm->userptr.invalidated_lock);
+
+		up_read(&vm->userptr.notifier_lock);
+		ret = xe_vma_userptr_pin_pages(uvma);
+		if (ret)

drivers/gpu/drm/xe/xe_vm.c void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) · line 3454

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +10 -0
+			return ret;
+	}
+
+	if (!up->sg) {
+		ret = -EINVAL;
+		goto out_unlock_notifier;
+	}
+
+	for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining;
+	     xe_res_next(&cur, cur_len)) {

This doesn't look right after reviewing [1].

A SG list is collection of IOVA which may be contain non-contiguous physical pages.

I'm pretty sure if the EU debugger is enable you are going to have to save off all the pages returned from hmm_range_fault and kmap each page individually.

Matt

[1] https://patchwork.freedesktop.org/patch/619324/?series=139780&rev=3

Quoted 30 lines
drivers/gpu/drm/xe/xe_vm.c +16 -0
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
+
+		cur_len = min(cur.size, cur.remaining);
+		if (write)
+			memcpy(ptr, buf, cur_len);
+		else
+			memcpy(buf, ptr, cur_len);
+		kunmap_local(ptr);
+		buf += cur_len;
+	}
+	ret = len;
+
+out_unlock_notifier:
+	up_read(&vm->userptr.notifier_lock);
+	return ret;
+}
drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..99b9a9b011de 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
maintainer
#18 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

Quoted 74 lines · Matthew Brost
Quoted 60 lines · Mika Kuoppala

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

---
 drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
 drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_vm.h      |  3 +++
 3 files changed, 51 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index edad6d533d0b..b09d7414cfe3 100644
@@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
 		return ret;
 	}
 
-	return -EINVAL;
+	return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write);
 }
 
 static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +21 -0
index a836dfc5a86f..5f891e76993b 100644
@@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
 	}
 	kvfree(snap);
 }
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write)
+{
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	struct xe_userptr *up = &uvma->userptr;
+	struct xe_res_cursor cur = {};
+	int cur_len, ret = 0;
+
+	while (true) {
+		down_read(&vm->userptr.notifier_lock);
+		if (!xe_vma_userptr_check_repin(uvma))
+			break;
+
+		spin_lock(&vm->userptr.invalidated_lock);
+		list_del_init(&uvma->userptr.invalidate_link);
+		spin_unlock(&vm->userptr.invalidated_lock);
+
+		up_read(&vm->userptr.notifier_lock);
+		ret = xe_vma_userptr_pin_pages(uvma);
+		if (ret)

drivers/gpu/drm/xe/xe_vm.c void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) · line 3454

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +10 -0
+			return ret;
+	}
+
+	if (!up->sg) {
+		ret = -EINVAL;
+		goto out_unlock_notifier;
+	}
+
+	for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining;
+	     xe_res_next(&cur, cur_len)) {

This doesn't look right after reviewing [1].

Quoted 3 lines

A SG list is collection of IOVA which may be contain non-contiguous physical pages.

This is unclear, let me try again.

A SG list is a collection of IOVA aka dma address. This is the the view from the device to CPU's memory. Each IOVA may be a non-contiguous set of physical pages if the IOMMU is on. Thus you can't just look at the first page of the IOVA and know what the 2nd page is.

Hope this makes a bit more sense.

Matt

Quoted 38 lines

I'm pretty sure if the EU debugger is enable you are going to have to save off all the pages returned from hmm_range_fault and kmap each page individually.

Matt

[1] https://patchwork.freedesktop.org/patch/619324/?series=139780&rev=3

Quoted 30 lines
drivers/gpu/drm/xe/xe_vm.c +16 -0
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
+
+		cur_len = min(cur.size, cur.remaining);
+		if (write)
+			memcpy(ptr, buf, cur_len);
+		else
+			memcpy(buf, ptr, cur_len);
+		kunmap_local(ptr);
+		buf += cur_len;
+	}
+	ret = len;
+
+out_unlock_notifier:
+	up_read(&vm->userptr.notifier_lock);
+	return ret;
+}
drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..99b9a9b011de 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
maintainer
#19 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

In reply to Mika Kuoppala
Quoted 36 lines · Mika Kuoppala

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

---
 drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
 drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_vm.h      |  3 +++
 3 files changed, 51 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index edad6d533d0b..b09d7414cfe3 100644
@@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
 		return ret;
 	}
 
-	return -EINVAL;
+	return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write);
 }
 
 static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +0 -0
index a836dfc5a86f..5f891e76993b 100644

drivers/gpu/drm/xe/xe_vm.c void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) · line 3427

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
@@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
 	}
 	kvfree(snap);
 }
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write)
+{

Maybe dump question but are we overthinking this here?

Can we just use kthread_use_mm, copy_to_user, copy_from_user?

If not then my previous comments still apply here.

Matt

Quoted 57 lines
drivers/gpu/drm/xe/xe_vm.c +43 -0
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	struct xe_userptr *up = &uvma->userptr;
+	struct xe_res_cursor cur = {};
+	int cur_len, ret = 0;
+
+	while (true) {
+		down_read(&vm->userptr.notifier_lock);
+		if (!xe_vma_userptr_check_repin(uvma))
+			break;
+
+		spin_lock(&vm->userptr.invalidated_lock);
+		list_del_init(&uvma->userptr.invalidate_link);
+		spin_unlock(&vm->userptr.invalidated_lock);
+
+		up_read(&vm->userptr.notifier_lock);
+		ret = xe_vma_userptr_pin_pages(uvma);
+		if (ret)
+			return ret;
+	}
+
+	if (!up->sg) {
+		ret = -EINVAL;
+		goto out_unlock_notifier;
+	}
+
+	for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining;
+	     xe_res_next(&cur, cur_len)) {
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
+
+		cur_len = min(cur.size, cur.remaining);
+		if (write)
+			memcpy(ptr, buf, cur_len);
+		else
+			memcpy(buf, ptr, cur_len);
+		kunmap_local(ptr);
+		buf += cur_len;
+	}
+	ret = len;
+
+out_unlock_notifier:
+	up_read(&vm->userptr.notifier_lock);
+	return ret;
+}
drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..99b9a9b011de 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
#20 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

W dniu 20.10.2024 o 20:16, Matthew Brost pisze:

Quoted 47 lines · Mika Kuoppala
Quoted 36 lines

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

---
  drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
  drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
  drivers/gpu/drm/xe/xe_vm.h      |  3 +++
  3 files changed, 51 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index edad6d533d0b..b09d7414cfe3 100644
@@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
  		return ret;
  	}
 
-	return -EINVAL;
+	return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write);
  }
 
  static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +0 -0
index a836dfc5a86f..5f891e76993b 100644

drivers/gpu/drm/xe/xe_vm.c void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) · line 3427

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
@@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
  	}
  	kvfree(snap);
  }
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write)
+{

Maybe dump question but are we overthinking this here?

Quoted 3 lines

Can we just use kthread_use_mm, copy_to_user, copy_from_user?

If not then my previous comments still apply here.

This function is called from debugger process context and kthread_use_mm is allowed only from kthread. Spawning kthread just for this is an option but looks odd and suboptimal, could be kind of last resort, or not?

Another options:
1. Keep reference to remote task in xe_userptr and use access_process_vm(up->task, ...).

2. Pass xe_eudebug.target_task reference down from eudebug framework to this helper and use access_process_vm. Current call chain is:
__xe_eudebug_vm_access - has access to xe_eudebug.target_task
->__vm_read_write
--->xe_eudebug_vm_access
---->xe_eudebug_vm_access
----->xe_eudebug_vma_access
------>xe_vm_userptr_access
So to achieve this multiple changes are required, but maybe it is valid path to go?
One potential issue with 1 and 2 is that multiple UMD tests were failing when access_process_vm/access_remote_vm were used, they were not investigated as this approach was dropped due to different reasons.

3. Continue approach from this patch, but with corrected page iterator of up->sg sg list[1]. This was nacked by you(?) [2] but I have problem understanding why? I see lot of code in kernel mapping sg pages:
linux$ git grep ' kmap.*sg' | wc -l
61
Is it incorrect? Or our case is different?

4. As you suggested in [3](?), modify xe_hmm_userptr_populate_range to keep hmm_range.hmm_pfns(or sth similar) in xe_userptr and use it later (instead of up->sg) to iterate over pages.

[1]: https://lore.kernel.org/intel-xe/20241011-xe_res_cursor_add_page_iterator-v3-1-0f8b8d3ab021@intel.com/
[2]: https://lore.kernel.org/intel-xe/[email protected]/
[3]: https://patchwork.freedesktop.org/patch/617481/?series=136572&rev=2#comment_1126527

Regards
Andrzej

Quoted 60 lines

Matt

Quoted 57 lines
drivers/gpu/drm/xe/xe_vm.c +43 -0
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	struct xe_userptr *up = &uvma->userptr;
+	struct xe_res_cursor cur = {};
+	int cur_len, ret = 0;
+
+	while (true) {
+		down_read(&vm->userptr.notifier_lock);
+		if (!xe_vma_userptr_check_repin(uvma))
+			break;
+
+		spin_lock(&vm->userptr.invalidated_lock);
+		list_del_init(&uvma->userptr.invalidate_link);
+		spin_unlock(&vm->userptr.invalidated_lock);
+
+		up_read(&vm->userptr.notifier_lock);
+		ret = xe_vma_userptr_pin_pages(uvma);
+		if (ret)
+			return ret;
+	}
+
+	if (!up->sg) {
+		ret = -EINVAL;
+		goto out_unlock_notifier;
+	}
+
+	for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining;
+	     xe_res_next(&cur, cur_len)) {
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
+
+		cur_len = min(cur.size, cur.remaining);
+		if (write)
+			memcpy(ptr, buf, cur_len);
+		else
+			memcpy(buf, ptr, cur_len);
+		kunmap_local(ptr);
+		buf += cur_len;
+	}
+	ret = len;
+
+out_unlock_notifier:
+	up_read(&vm->userptr.notifier_lock);
+	return ret;
+}
drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..99b9a9b011de 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
  void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
  void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
  void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
maintainer
#21 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

Quoted 50 lines · Andrzej

W dniu 20.10.2024 o 20:16, Matthew Brost pisze:

Quoted 45 lines · Mika Kuoppala
Quoted 34 lines

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

---
  drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
  drivers/gpu/drm/xe/xe_vm.c      | 47 +++++++++++++++++++++++++++++++++
  drivers/gpu/drm/xe/xe_vm.h      |  3 +++
  3 files changed, 51 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index edad6d533d0b..b09d7414cfe3 100644
@@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
  		return ret;
  	}
-	return -EINVAL;
+	return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write);
  }
  static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +0 -0
index a836dfc5a86f..5f891e76993b 100644

drivers/gpu/drm/xe/xe_vm.c void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) · line 3427

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
@@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
  	}
  	kvfree(snap);
  }
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write)
+{

Maybe dump question but are we overthinking this here?

Quoted 3 lines

Can we just use kthread_use_mm, copy_to_user, copy_from_user?

If not then my previous comments still apply here.

Quoted 8 lines

This function is called from debugger process context and kthread_use_mm is allowed only from kthread. Spawning kthread just for this is an option but looks odd and suboptimal, could be kind of last resort, or not?

Another options:
1. Keep reference to remote task in xe_userptr and use access_process_vm(up->task, ...).

I think remote refs are generally a bad idea but admittedly don't fully understand what this would look like.

Quoted 14 lines

2. Pass xe_eudebug.target_task reference down from eudebug framework to this helper and use access_process_vm. Current call chain is:
__xe_eudebug_vm_access - has access to xe_eudebug.target_task ->__vm_read_write
--->xe_eudebug_vm_access
---->xe_eudebug_vm_access
----->xe_eudebug_vma_access
------>xe_vm_userptr_access
So to achieve this multiple changes are required, but maybe it is valid path to go?
One potential issue with 1 and 2 is that multiple UMD tests were failing when access_process_vm/access_remote_vm were used, they were not investigated as this approach was dropped due to different reasons.

Quoted 4 lines

3. Continue approach from this patch, but with corrected page iterator of up->sg sg list[1]. This was nacked by you(?) [2] but I have problem understanding why? I see lot of code in kernel mapping sg pages:
linux$ git grep ' kmap.*sg' | wc -l

I looked here every example I found has mapping and accessing 1 page at time not mapping 1 page and accessing many.

Quoted 3 lines

61
Is it incorrect? Or our case is different?

A sglist segments are dma-addresses (virtual address), thus every 4k in the segment can be a different physical page.

i.e., look at this snippet:

+ void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
+
+ cur_len = min(cur.size, cur.remaining);
+ if (write)
+ memcpy(ptr, buf, cur_len);
+ else
+ memcpy(buf, ptr, cur_len);
+ kunmap_local(ptr);

If 'cur.start' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

Likewise if 'cur_len' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

This loop would have to be changed to something like below which kmaps and accesses 1 page at a time...

	for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining;
	     xe_res_next(&cur, cur_len)) {
		int segment_len;
		int remain;		

		cur_len = min(cur.size, cur.remaining);
		remain = cur_len;

		for (i = 0; i < cur_len; i += segment_len) {
			phys_addr_t phys = iommu_iova_to_phys(sg_dma_address(cur.sgl) + i + cur.start);
			struct page *page = phys_to_page(phys); 
			void *ptr = kmap_local_page(page);
			int ptr_offset = offset & ~PAGE_MASK;

			segment_len = min(remain, PAGE_SIZE - ptr_offset);

			if (write)
				memcpy(ptr + ptr_offset, buf + i, segment_len);
			else
				memcpy(buf + i, ptr + ptr_offset, segment_len);
			kunmap_local(ptr);

			offset += segment_len;
			remain -= segment_len;		
		}
		
		buf += cur_len;
	}
Quoted 4 lines

4. As you suggested in [3](?), modify xe_hmm_userptr_populate_range to keep hmm_range.hmm_pfns(or sth similar) in xe_userptr and use it later (instead of up->sg) to iterate over pages.

Or just call hmm_range_fault directly here and operate on returned pages directly.

BTW eventually all the userptr stuff is going to change and be based on GPU SVM [4]. Calling hmm_range_fault directly will always work though and likely the safest option.

Matt

[4] https://patchwork.freedesktop.org/patch/619809/?series=137870&rev=2

Quoted 69 lines

[1]: https://lore.kernel.org/intel-xe/20241011-xe_res_cursor_add_page_iterator-v3-1-0f8b8d3ab021@intel.com/
[2]:
https://lore.kernel.org/intel-xe/[email protected]/
[3]: https://patchwork.freedesktop.org/patch/617481/?series=136572&rev=2#comment_1126527

Regards
Andrzej

Quoted 60 lines

Matt

Quoted 57 lines
drivers/gpu/drm/xe/xe_vm.c +43 -0
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	struct xe_userptr *up = &uvma->userptr;
+	struct xe_res_cursor cur = {};
+	int cur_len, ret = 0;
+
+	while (true) {
+		down_read(&vm->userptr.notifier_lock);
+		if (!xe_vma_userptr_check_repin(uvma))
+			break;
+
+		spin_lock(&vm->userptr.invalidated_lock);
+		list_del_init(&uvma->userptr.invalidate_link);
+		spin_unlock(&vm->userptr.invalidated_lock);
+
+		up_read(&vm->userptr.notifier_lock);
+		ret = xe_vma_userptr_pin_pages(uvma);
+		if (ret)
+			return ret;
+	}
+
+	if (!up->sg) {
+		ret = -EINVAL;
+		goto out_unlock_notifier;
+	}
+
+	for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining;
+	     xe_res_next(&cur, cur_len)) {
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start;
+
+		cur_len = min(cur.size, cur.remaining);
+		if (write)
+			memcpy(ptr, buf, cur_len);
+		else
+			memcpy(buf, ptr, cur_len);
+		kunmap_local(ptr);
+		buf += cur_len;
+	}
+	ret = len;
+
+out_unlock_notifier:
+	up_read(&vm->userptr.notifier_lock);
+	return ret;
+}
drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..99b9a9b011de 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
  void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
  void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
  void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset,
+		   void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
#22 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

W dniu 22.10.2024 o 00:34, Matthew Brost pisze:

Quoted 46 lines · Andrzej
Quoted 26 lines

W dniu 20.10.2024 o 20:16, Matthew Brost pisze:

Quoted 25 lines · Mika Kuoppala
Quoted 12 lines

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

Signed- off-by: Maciej Patelczyk <[email protected]>
Signed- off-by: Mika Kuoppala <[email protected]> Reviewed- by: Jonathan Cavitt <[email protected]> --- drivers/ gpu/drm/xe/xe_eudebug.c | 2 +- drivers/gpu/drm/xe/ xe_vm.c | 47 +++++++++++++++++++++++++++++++++ drivers/ gpu/drm/xe/xe_vm.h | 3 +++ 3 files changed, 51 insertions(+), 1 deletion(-)

drivers/gpu/ drm/ xe/xe_eudebug.c index edad6d533d0b..b09d7414cfe3 100644 renamed from drivers/gpu/drm/xe/xe_eudebug.c +0 -1
--- a/ drivers/gpu/drm/xe/xe_eudebug.c +++ b/drivers/gpu/drm/ 
Quoted 7 lines

xe/ xe_eudebug.c @@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset, return ret; } - return -EINVAL; + return xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes, write); } static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset, diff --git a/drivers/gpu/drm/xe/xe_vm.c b/
drivers/ gpu/drm/xe/xe_vm.c index a836dfc5a86f..5f891e76993b 100644 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/ xe/xe_vm.c @@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) } kvfree(snap); } + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write) +{

Maybe dump question but are we overthinking this here?

Can we just use kthread_use_mm, copy_to_user, copy_from_user?

Quoted 8 lines
Quoted 1 line

If not then my previous comments still apply here.

This function is called from debugger process context and kthread_use_mm is allowed only from kthread. Spawning kthread just for this is an option but looks odd and suboptimal, could be
kind of last resort, or not?

Another options: 1. Keep reference to remote task in xe_userptr and use access_process_vm(up->task, ...).

I think remote refs are generally a bad idea but admittedly don't fully understand what this would look like.

Quoted 7 lines

2. Pass xe_eudebug.target_task reference down from eudebug framework to this helper and use access_process_vm. Current call chain is: __xe_eudebug_vm_access - has access to xe_eudebug.target_task ->__vm_read_write --->xe_eudebug_vm_access ---->xe_eudebug_vm_access ----->xe_eudebug_vma_access ------

Quoted 1 line

xe_vm_userptr_access So to achieve this multiple changes are

required, but maybe it is valid path to go? One potential issue with 1 and 2 is that multiple UMD tests were failing when access_process_vm/access_remote_vm were used, they were not investigated as this approach was dropped due to different reasons.

3. Continue approach from this patch, but with corrected page iterator of up->sg sg list[1]. This was nacked by you(?) [2] but I have problem understanding why? I see lot of code in kernel mapping sg pages: linux$ git grep ' kmap.*sg' | wc -l

Quoted 8 lines

I looked here every example I found has mapping and accessing 1 page at time not mapping 1 page and accessing many.

Quoted 2 lines

61 Is it incorrect? Or our case is different?

A sglist segments are dma-addresses (virtual address), thus every 4k in the segment can be a different physical page.

sglist is also list of pages and their lengths (in case of consecutive pages, they are glued together), i.e. exactly what we need. And this is done in xe_build_sg: it calls sg_alloc_table_from_pages_segment which is documented as follows:
...
* Allocate and initialize an sg table from a list of pages. Contiguous
* ranges of the pages are squashed into a single scatterlist node up to the
* maximum size specified in @max_segment. A user may provide an offset at a
* start and a size of valid data in a buffer specified by the page array.
...
So sglist contains the same information as hmm_range->hmm_pfns[i] and little more.

So my concern is if mapping operation can destroy this info, but looking at the code this does not seems to be the case. For example iommu_dma_map_sg docs explicitly says about "preserve the original offsets and sizes for the caller".

Quoted 7 lines

i.e., look at this snippet:

(quoted diff) +1 -0
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start; + + 

cur_len = min(cur.size, cur.remaining); + if (write) + memcpy(ptr, buf, cur_len); + else + memcpy(buf, ptr, cur_len); + kunmap_local(ptr);

If 'cur.start' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

With added possibility to iterate over sgl pages in xe_res_cursor[1] it does not seems to be true.
Why? cur.start is limited by length of the segment (cur.sgl->length), if it happens to be more than 4k, it means sg_page(cur.sgl) points to consecutive pages and cur.start is correct.

Quoted 2 lines

Likewise if 'cur_len' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

Again, in case of consecutive pages it should be in range.

Anyway if there is an issue with consecutive pages, which I am not aware of, we can always build sg list with segments pointing to 4K pages by modyfing xe_build_sg to call sg_alloc_table_from_pages_segment with 4K max segment size.

[1]: https://lore.kernel.org/intel-xe/20241011-xe_res_cursor_add_page_iterator-v3-1-0f8b8d3ab021@intel.com/

Regards
Andrzej

Quoted 54 lines

This loop would have to be changed to something like below which kmaps and accesses 1 page at a time... for (xe_res_first_sg(up-

Quoted 1 line

sg, offset, len, &cur); cur.remaining; xe_res_next(&cur, cur_len))

{ int segment_len; int remain;

cur_len = min(cur.size, cur.remaining); remain = cur_len;

for (i = 0; i < cur_len; i += segment_len) { phys_addr_t phys = iommu_iova_to_phys(sg_dma_address(cur.sgl) + i + cur.start); struct page *page = phys_to_page(phys); void *ptr = kmap_local_page(page); int ptr_offset = offset & ~PAGE_MASK;

segment_len = min(remain, PAGE_SIZE - ptr_offset);

if (write) memcpy(ptr + ptr_offset, buf + i, segment_len); else memcpy(buf + i, ptr + ptr_offset, segment_len); kunmap_local(ptr);

offset += segment_len; remain -= segment_len; } buf += cur_len; }

Quoted 2 lines

4. As you suggested in [3](?), modify xe_hmm_userptr_populate_range to keep hmm_range.hmm_pfns(or sth similar) in xe_userptr and use it later (instead of up->sg) to iterate over pages.

Or just call hmm_range_fault directly here and operate on returned pages directly.

BTW eventually all the userptr stuff is going to change and be based on GPU SVM [4]. Calling hmm_range_fault directly will always work though and likely the safest option.

Matt

[4] https://patchwork.freedesktop.org/patch/619809/? series=137870&rev=2

Quoted 26 lines

[1]: https://lore.kernel.org/intel-xe/20241011- [email protected]/ [2]: https://lore.kernel.org/intel-xe/Zw32fauoUmB6Iojk@DUT025- TGLU.fm.intel.com/ [3]: https://patchwork.freedesktop.org/ patch/617481/?series=136572&rev=2#comment_1126527

Regards Andrzej

Quoted 21 lines

Matt

Quoted 18 lines
drivers/gpu/ drm/ xe/xe_eudebug.c index edad6d533d0b..b09d7414cfe3 100644 renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma); +	struct 

xe_userptr *up = &uvma->userptr; + struct xe_res_cursor cur = {}; + int cur_len, ret = 0; + + while (true) { +
down_read(&vm-

Quoted 1 line

userptr.notifier_lock); + if (!

xe_vma_userptr_check_repin(uvma)) + break; + +
spin_lock(&vm-

Quoted 1 line

userptr.invalidated_lock); + list_del_init(&uvma- userptr.invalidate_link); + spin_unlock(&vm- userptr.invalidated_lock); + + up_read(&vm- userptr.notifier_lock); + ret =

xe_vma_userptr_pin_pages(uvma); + if (ret) + return ret;

drivers/gpu/ drm/ xe/xe_eudebug.c index edad6d533d0b..b09d7414cfe3 100644 renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	} + +	if (!up->sg) { +		ret = -EINVAL; +		goto 

out_unlock_notifier; + } + + for (xe_res_first_sg(up->sg, offset, len, &cur); cur.remaining; + xe_res_next(&cur, cur_len)) { + void *ptr = kmap_local_page(sg_page(cur.sgl))
+ cur.start; + + cur_len = min(cur.size, cur.remaining); +
if (write) + memcpy(ptr, buf, cur_len); + else +
memcpy(buf, ptr, cur_len); + kunmap_local(ptr); + buf +=
cur_len; + } + ret = len; + +out_unlock_notifier: +
up_read(&vm-

Quoted 1 line

userptr.notifier_lock); + return ret; +} diff --git a/ drivers/

gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h index c864dba35e1d..99b9a9b011de 100644 --- a/drivers/gpu/drm/xe/ xe_vm.h +++ b/drivers/gpu/drm/xe/xe_vm.h @@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm); void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap); void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p); void xe_vm_snapshot_free(struct xe_vm_snapshot *snap); + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write); -- 2.34.1

maintainer
#23 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

Quoted 108 lines · Andrzej

W dniu 22.10.2024 o 00:34, Matthew Brost pisze:

Quoted 72 lines · Andrzej
Quoted 43 lines

W dniu 20.10.2024 o 20:16, Matthew Brost pisze:

Quoted 38 lines · Mika Kuoppala
Quoted 26 lines

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

Signed- off-by: Maciej Patelczyk <[email protected]> Signed- off-by: Mika Kuoppala <[email protected]> Reviewed- by: Jonathan Cavitt <[email protected]> --- drivers/ gpu/drm/xe/xe_eudebug.c | 2 +- drivers/gpu/drm/xe/ xe_vm.c | 47 +++++++++++++++++++++++++++++++++ drivers/
gpu/drm/xe/xe_vm.h | 3 +++ 3 files changed, 51
insertions(+), 1 deletion(-)

drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +0 -0

drm/ xe/xe_eudebug.c index edad6d533d0b..b09d7414cfe3 100644 --- a/ drivers/gpu/drm/xe/xe_eudebug.c +++ b/drivers/gpu/drm/ xe/ xe_eudebug.c @@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64
offset, return ret; } - return -EINVAL; + return
xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes,

Quoted 8 lines

write); } static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset, diff --git a/drivers/gpu/drm/xe/xe_vm.c b/ drivers/ gpu/drm/xe/xe_vm.c index a836dfc5a86f..5f891e76993b 100644 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/ xe/xe_vm.c @@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) } kvfree(snap); } + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write) +{

Maybe dump question but are we overthinking this here?

Quoted 3 lines

Can we just use kthread_use_mm, copy_to_user, copy_from_user?

If not then my previous comments still apply here.

Quoted 8 lines

This function is called from debugger process context and kthread_use_mm is allowed only from kthread. Spawning kthread just for this is an option but looks odd and suboptimal, could be kind of last resort, or not?

Another options: 1. Keep reference to remote task in xe_userptr and use access_process_vm(up->task, ...).

I think remote refs are generally a bad idea but admittedly don't fully understand what this would look like.

Quoted 11 lines

2. Pass xe_eudebug.target_task reference down from eudebug framework to this helper and use access_process_vm. Current call chain is:
__xe_eudebug_vm_access - has access to xe_eudebug.target_task ->__vm_read_write --->xe_eudebug_vm_access ---->xe_eudebug_vm_access ----->xe_eudebug_vma_access ------

Quoted 1 line

xe_vm_userptr_access So to achieve this multiple changes are

required, but maybe it is valid path to go? One potential issue with 1 and 2 is that multiple UMD tests were failing when access_process_vm/access_remote_vm were used, they were not investigated as this approach was dropped due to different reasons.

Quoted 4 lines

3. Continue approach from this patch, but with corrected page iterator of up->sg sg list[1]. This was nacked by you(?) [2] but I have problem understanding why? I see lot of code in kernel mapping sg pages: linux$ git grep ' kmap.*sg' | wc -l

Quoted 8 lines

I looked here every example I found has mapping and accessing 1 page at time not mapping 1 page and accessing many.

Quoted 2 lines

61 Is it incorrect? Or our case is different?

A sglist segments are dma-addresses (virtual address), thus every 4k in the segment can be a different physical page.

sglist is also list of pages and their lengths (in case of consecutive pages, they are glued together), i.e. exactly what we need. And this is done in xe_build_sg: it calls sg_alloc_table_from_pages_segment which is documented as follows:
...
* Allocate and initialize an sg table from a list of pages. Contiguous
* ranges of the pages are squashed into a single scatterlist node up to the
* maximum size specified in @max_segment. A user may provide an offset at a
* start and a size of valid data in a buffer specified by the page array.
...
So sglist contains the same information as hmm_range->hmm_pfns[i] and little more.

So my concern is if mapping operation can destroy this info, but looking at the code this does not seems to be the case. For example iommu_dma_map_sg docs explicitly says about "preserve the original offsets and sizes for the caller".

Quoted 7 lines

i.e., look at this snippet:

(quoted diff) +1 -0
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start; + +

cur_len = min(cur.size, cur.remaining); + if (write) + memcpy(ptr, buf,
cur_len); + else + memcpy(buf, ptr, cur_len); + kunmap_local(ptr);

Quoted 9 lines
Quoted 2 lines

If 'cur.start' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

With added possibility to iterate over sgl pages in xe_res_cursor[1] it does not seems to be true.
Why? cur.start is limited by length of the segment (cur.sgl->length), if it happens to be more than 4k, it means sg_page(cur.sgl) points to consecutive pages and cur.start is correct.

I suppose if the cursor is changed to walk the pages not the dma address, yea i guess this would work. Still my much prefered way would just call hmm_range_fault or optionally save off the pages in xe_vma_userptr_pin_pages given at some point we will ditch SG tables for userptr in favor of drm gpusvm which will be page based.

Matt

Quoted 99 lines
Quoted 3 lines

Likewise if 'cur_len' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

Again, in case of consecutive pages it should be in range.

Anyway if there is an issue with consecutive pages, which I am not aware of, we can always build sg list with segments pointing to 4K pages by modyfing xe_build_sg to call sg_alloc_table_from_pages_segment with 4K max segment size.

[1]: https://lore.kernel.org/intel-xe/20241011-xe_res_cursor_add_page_iterator-v3-1-0f8b8d3ab021@intel.com/

Regards
Andrzej

Quoted 82 lines

This loop would have to be changed to something like below which kmaps and accesses 1 page at a time... for (xe_res_first_sg(up-

Quoted 1 line

sg, offset, len, &cur); cur.remaining; xe_res_next(&cur, cur_len))

{ int segment_len; int remain;

cur_len = min(cur.size, cur.remaining); remain = cur_len;

for (i = 0; i < cur_len; i += segment_len) { phys_addr_t phys = iommu_iova_to_phys(sg_dma_address(cur.sgl) + i + cur.start); struct page *page = phys_to_page(phys); void *ptr = kmap_local_page(page); int ptr_offset = offset & ~PAGE_MASK;

segment_len = min(remain, PAGE_SIZE - ptr_offset);

if (write) memcpy(ptr + ptr_offset, buf + i, segment_len); else memcpy(buf + i, ptr + ptr_offset, segment_len); kunmap_local(ptr);

offset += segment_len; remain -= segment_len; } buf += cur_len; }

Quoted 4 lines

4. As you suggested in [3](?), modify xe_hmm_userptr_populate_range to keep hmm_range.hmm_pfns(or sth similar) in xe_userptr and use it later (instead of up->sg) to iterate over pages.

Or just call hmm_range_fault directly here and operate on returned pages directly.

BTW eventually all the userptr stuff is going to change and be based on GPU SVM [4]. Calling hmm_range_fault directly will always work though and likely the safest option.

Matt

[4] https://patchwork.freedesktop.org/patch/619809/? series=137870&rev=2

Quoted 46 lines

[1]: https://lore.kernel.org/intel-xe/20241011-
[email protected]/ [2]:
https://lore.kernel.org/intel-xe/Zw32fauoUmB6Iojk@DUT025-
TGLU.fm.intel.com/ [3]: https://patchwork.freedesktop.org/
patch/617481/?series=136572&rev=2#comment_1126527

Regards Andrzej

Quoted 37 lines

Matt

Quoted 34 lines
drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma); +	struct

xe_userptr *up = &uvma->userptr; + struct xe_res_cursor cur
= {}; + int cur_len, ret = 0; + + while (true) { +
down_read(&vm-

Quoted 1 line

userptr.notifier_lock); + if (!

xe_vma_userptr_check_repin(uvma)) + break; + +
spin_lock(&vm-

Quoted 4 lines

userptr.invalidated_lock); + list_del_init(&uvma-
userptr.invalidate_link); + spin_unlock(&vm-
userptr.invalidated_lock); + + up_read(&vm-
userptr.notifier_lock); + ret =

xe_vma_userptr_pin_pages(uvma); + if (ret) + return ret;

drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	} + +	if (!up->sg) { +		ret = -EINVAL; +		goto

out_unlock_notifier; + } + + for (xe_res_first_sg(up->sg,
offset, len, &cur); cur.remaining; + xe_res_next(&cur,
cur_len)) { + void *ptr = kmap_local_page(sg_page(cur.sgl))
+ cur.start; + + cur_len = min(cur.size, cur.remaining); +
if (write) + memcpy(ptr, buf, cur_len); + else +
memcpy(buf, ptr, cur_len); + kunmap_local(ptr); + buf +=
cur_len; + } + ret = len; + +out_unlock_notifier: +
up_read(&vm-

Quoted 2 lines

userptr.notifier_lock); + return ret; +} diff --git a/
drivers/

gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h index c864dba35e1d..99b9a9b011de 100644 --- a/drivers/gpu/drm/xe/ xe_vm.h +++ b/drivers/gpu/drm/xe/xe_vm.h @@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm); void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap); void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p); void xe_vm_snapshot_free(struct xe_vm_snapshot *snap); + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write); -- 2.34.1

#24 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

W dniu 24.10.2024 o 18:06, Matthew Brost pisze:

Quoted 110 lines · Andrzej
Quoted 101 lines

W dniu 22.10.2024 o 00:34, Matthew Brost pisze:

Quoted 68 lines · Andrzej
Quoted 40 lines

W dniu 20.10.2024 o 20:16, Matthew Brost pisze:

Quoted 39 lines · Mika Kuoppala
Quoted 26 lines

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

Signed- off-by: Maciej Patelczyk <[email protected]> Signed- off-by: Mika Kuoppala <[email protected]> Reviewed- by: Jonathan Cavitt <[email protected]> --- drivers/ gpu/drm/xe/xe_eudebug.c | 2 +- drivers/gpu/drm/xe/ xe_vm.c | 47 +++++++++++++++++++++++++++++++++ drivers/
gpu/drm/xe/xe_vm.h | 3 +++ 3 files changed, 51
insertions(+), 1 deletion(-)

drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +0 -0

drm/ xe/xe_eudebug.c index edad6d533d0b..b09d7414cfe3 100644 --- a/ drivers/gpu/drm/xe/xe_eudebug.c +++ b/drivers/gpu/drm/ xe/ xe_eudebug.c @@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64
offset, return ret; } - return -EINVAL; + return
xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes,

Quoted 8 lines

write); } static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset, diff --git a/drivers/gpu/drm/xe/xe_vm.c b/ drivers/ gpu/drm/xe/xe_vm.c index a836dfc5a86f..5f891e76993b 100644 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/ xe/xe_vm.c @@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) } kvfree(snap); } + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write) +{

Maybe dump question but are we overthinking this here?

Can we just use kthread_use_mm, copy_to_user, copy_from_user?

Quoted 9 lines
Quoted 1 line

If not then my previous comments still apply here.

This function is called from debugger process context and kthread_use_mm is allowed only from kthread. Spawning kthread just for this is an option but looks odd and suboptimal, could be kind of last resort, or not?

Another options: 1. Keep reference to remote task in xe_userptr and use access_process_vm(up->task, ...).

I think remote refs are generally a bad idea but admittedly don't fully understand what this would look like.

Quoted 11 lines

2. Pass xe_eudebug.target_task reference down from eudebug framework to this helper and use access_process_vm. Current call chain is:
__xe_eudebug_vm_access - has access to xe_eudebug.target_task ->__vm_read_write --->xe_eudebug_vm_access ---->xe_eudebug_vm_access ----->xe_eudebug_vma_access ------

Quoted 1 line

xe_vm_userptr_access So to achieve this multiple changes are

required, but maybe it is valid path to go? One potential issue with 1 and 2 is that multiple UMD tests were failing when access_process_vm/access_remote_vm were used, they were not investigated as this approach was dropped due to different reasons.

Quoted 4 lines

3. Continue approach from this patch, but with corrected page iterator of up->sg sg list[1]. This was nacked by you(?) [2] but I have problem understanding why? I see lot of code in kernel mapping sg pages: linux$ git grep ' kmap.*sg' | wc -l

Quoted 7 lines

I looked here every example I found has mapping and accessing 1 page at time not mapping 1 page and accessing many.

Quoted 2 lines

61 Is it incorrect? Or our case is different?

A sglist segments are dma-addresses (virtual address), thus every 4k in the segment can be a different physical page.

sglist is also list of pages and their lengths (in case of consecutive pages, they are glued together), i.e. exactly what we need. And this is done in xe_build_sg: it calls sg_alloc_table_from_pages_segment which is documented as follows:
...
* Allocate and initialize an sg table from a list of pages. Contiguous
* ranges of the pages are squashed into a single scatterlist node up to the
* maximum size specified in @max_segment. A user may provide an offset at a
* start and a size of valid data in a buffer specified by the page array.
...
So sglist contains the same information as hmm_range->hmm_pfns[i] and little more.

So my concern is if mapping operation can destroy this info, but looking at the code this does not seems to be the case. For example iommu_dma_map_sg docs explicitly says about "preserve the original offsets and sizes for the caller".

Quoted 6 lines

i.e., look at this snippet:

(quoted diff) +1 -0
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start; + +

cur_len = min(cur.size, cur.remaining); + if (write) + memcpy(ptr, buf,
cur_len); + else + memcpy(buf, ptr, cur_len); + kunmap_local(ptr);

Quoted 8 lines
Quoted 2 lines

If 'cur.start' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

With added possibility to iterate over sgl pages in xe_res_cursor[1] it does not seems to be true.
Why? cur.start is limited by length of the segment (cur.sgl->length), if it happens to be more than 4k, it means sg_page(cur.sgl) points to consecutive pages and cur.start is correct.

Quoted 5 lines

I suppose if the cursor is changed to walk the pages not the dma address, yea i guess this would work. Still my much prefered way would just call hmm_range_fault or optionally save off the pages in xe_vma_userptr_pin_pages given at some point we will ditch SG tables for userptr in favor of drm gpusvm which will be page based.

Both alternatives seems for me suboptimal, as their result is what we have already in sg table, for a price of extra call (hmm_range_fault) and extra allocations (both). Most important they will complicate the code without clear benefit.

I will try to implement version with hmm_range_fault but I am still confused why we need to complicate things.

Regards

Andrzej

Quoted 95 lines

Matt

Quoted 92 lines
Quoted 2 lines

Likewise if 'cur_len' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

Again, in case of consecutive pages it should be in range.

Anyway if there is an issue with consecutive pages, which I am not aware of, we can always build sg list with segments pointing to 4K pages by modyfing xe_build_sg to call sg_alloc_table_from_pages_segment with 4K max segment size.

[1]: https://lore.kernel.org/intel-xe/20241011-xe_res_cursor_add_page_iterator-v3-1-0f8b8d3ab021@intel.com/

Regards
Andrzej

Quoted 78 lines

This loop would have to be changed to something like below which kmaps and accesses 1 page at a time... for (xe_res_first_sg(up-

Quoted 1 line

sg, offset, len, &cur); cur.remaining; xe_res_next(&cur, cur_len))

{ int segment_len; int remain;

cur_len = min(cur.size, cur.remaining); remain = cur_len;

for (i = 0; i < cur_len; i += segment_len) { phys_addr_t phys = iommu_iova_to_phys(sg_dma_address(cur.sgl) + i + cur.start); struct page *page = phys_to_page(phys); void *ptr = kmap_local_page(page); int ptr_offset = offset & ~PAGE_MASK;

segment_len = min(remain, PAGE_SIZE - ptr_offset);

if (write) memcpy(ptr + ptr_offset, buf + i, segment_len); else memcpy(buf + i, ptr + ptr_offset, segment_len); kunmap_local(ptr);

offset += segment_len; remain -= segment_len; } buf += cur_len; }

Quoted 4 lines

4. As you suggested in [3](?), modify xe_hmm_userptr_populate_range to keep hmm_range.hmm_pfns(or sth similar) in xe_userptr and use it later (instead of up->sg) to iterate over pages.

Or just call hmm_range_fault directly here and operate on returned pages directly.

BTW eventually all the userptr stuff is going to change and be based on GPU SVM [4]. Calling hmm_range_fault directly will always work though and likely the safest option.

Matt

[4] https://patchwork.freedesktop.org/patch/619809/? series=137870&rev=2

Quoted 44 lines

[1]: https://lore.kernel.org/intel-xe/20241011-
[email protected]/ [2]:
https://lore.kernel.org/intel-xe/Zw32fauoUmB6Iojk@DUT025-
TGLU.fm.intel.com/ [3]: https://patchwork.freedesktop.org/
patch/617481/?series=136572&rev=2#comment_1126527

Regards Andrzej

Quoted 36 lines

Matt

Quoted 34 lines
drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma); +	struct

xe_userptr *up = &uvma->userptr; + struct xe_res_cursor cur
= {}; + int cur_len, ret = 0; + + while (true) { +
down_read(&vm-

Quoted 1 line

userptr.notifier_lock); + if (!

xe_vma_userptr_check_repin(uvma)) + break; + +
spin_lock(&vm-

Quoted 4 lines

userptr.invalidated_lock); + list_del_init(&uvma-
userptr.invalidate_link); + spin_unlock(&vm-
userptr.invalidated_lock); + + up_read(&vm-
userptr.notifier_lock); + ret =

xe_vma_userptr_pin_pages(uvma); + if (ret) + return ret;

drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	} + +	if (!up->sg) { +		ret = -EINVAL; +		goto

out_unlock_notifier; + } + + for (xe_res_first_sg(up->sg,
offset, len, &cur); cur.remaining; + xe_res_next(&cur,
cur_len)) { + void *ptr = kmap_local_page(sg_page(cur.sgl))
+ cur.start; + + cur_len = min(cur.size, cur.remaining); +
if (write) + memcpy(ptr, buf, cur_len); + else +
memcpy(buf, ptr, cur_len); + kunmap_local(ptr); + buf +=
cur_len; + } + ret = len; + +out_unlock_notifier: +
up_read(&vm-

Quoted 2 lines

userptr.notifier_lock); + return ret; +} diff --git a/
drivers/

gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h index c864dba35e1d..99b9a9b011de 100644 --- a/drivers/gpu/drm/xe/ xe_vm.h +++ b/drivers/gpu/drm/xe/xe_vm.h @@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm); void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap); void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p); void xe_vm_snapshot_free(struct xe_vm_snapshot *snap); + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write); -- 2.34.1

maintainer
#25 lore raw

Re: [PATCH 12/18] drm/xe/eudebug: implement userptr_vma access

Quoted 118 lines · Andrzej

W dniu 24.10.2024 o 18:06, Matthew Brost pisze:

Quoted 110 lines · Andrzej
Quoted 101 lines

W dniu 22.10.2024 o 00:34, Matthew Brost pisze:

Quoted 68 lines · Andrzej
Quoted 40 lines

W dniu 20.10.2024 o 20:16, Matthew Brost pisze:

Quoted 39 lines · Mika Kuoppala
Quoted 26 lines

From: Andrzej Hajda <[email protected]>

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)

Signed- off-by: Maciej Patelczyk <[email protected]> Signed- off-by: Mika Kuoppala <[email protected]> Reviewed- by: Jonathan Cavitt <[email protected]> --- drivers/ gpu/drm/xe/xe_eudebug.c | 2 +- drivers/gpu/drm/xe/ xe_vm.c | 47 +++++++++++++++++++++++++++++++++ drivers/
gpu/drm/xe/xe_vm.h | 3 +++ 3 files changed, 51
insertions(+), 1 deletion(-)

drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +0 -0

drm/ xe/xe_eudebug.c index edad6d533d0b..b09d7414cfe3 100644 --- a/ drivers/gpu/drm/xe/xe_eudebug.c +++ b/drivers/gpu/drm/ xe/ xe_eudebug.c @@ -3023,7 +3023,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64
offset, return ret; } - return -EINVAL; + return
xe_uvma_access(to_userptr_vma(vma), offset, buf, bytes,

Quoted 8 lines

write); } static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset, diff --git a/drivers/gpu/drm/xe/xe_vm.c b/ drivers/ gpu/drm/xe/xe_vm.c index a836dfc5a86f..5f891e76993b 100644 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/ xe/xe_vm.c @@ -3421,3 +3421,50 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap) } kvfree(snap); } + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write) +{

Maybe dump question but are we overthinking this here?

Can we just use kthread_use_mm, copy_to_user, copy_from_user?

Quoted 9 lines
Quoted 1 line

If not then my previous comments still apply here.

This function is called from debugger process context and kthread_use_mm is allowed only from kthread. Spawning kthread just for this is an option but looks odd and suboptimal, could be kind of last resort, or not?

Another options: 1. Keep reference to remote task in xe_userptr and use access_process_vm(up->task, ...).

I think remote refs are generally a bad idea but admittedly don't fully understand what this would look like.

Quoted 11 lines

2. Pass xe_eudebug.target_task reference down from eudebug framework to this helper and use access_process_vm. Current call chain is:
__xe_eudebug_vm_access - has access to xe_eudebug.target_task ->__vm_read_write --->xe_eudebug_vm_access ---->xe_eudebug_vm_access ----->xe_eudebug_vma_access ------

Quoted 1 line

xe_vm_userptr_access So to achieve this multiple changes are

required, but maybe it is valid path to go? One potential issue with 1 and 2 is that multiple UMD tests were failing when access_process_vm/access_remote_vm were used, they were not investigated as this approach was dropped due to different reasons.

Quoted 4 lines

3. Continue approach from this patch, but with corrected page iterator of up->sg sg list[1]. This was nacked by you(?) [2] but I have problem understanding why? I see lot of code in kernel mapping sg pages: linux$ git grep ' kmap.*sg' | wc -l

Quoted 7 lines

I looked here every example I found has mapping and accessing 1 page at time not mapping 1 page and accessing many.

Quoted 2 lines

61 Is it incorrect? Or our case is different?

A sglist segments are dma-addresses (virtual address), thus every 4k in the segment can be a different physical page.

sglist is also list of pages and their lengths (in case of consecutive pages, they are glued together), i.e. exactly what we need. And this is done in xe_build_sg: it calls sg_alloc_table_from_pages_segment which is documented as follows:
...
* Allocate and initialize an sg table from a list of pages. Contiguous
* ranges of the pages are squashed into a single scatterlist node up to the
* maximum size specified in @max_segment. A user may provide an offset at a
* start and a size of valid data in a buffer specified by the page array.
...
So sglist contains the same information as hmm_range->hmm_pfns[i] and little more.

So my concern is if mapping operation can destroy this info, but looking at the code this does not seems to be the case. For example iommu_dma_map_sg docs explicitly says about "preserve the original offsets and sizes for the caller".

Quoted 6 lines

i.e., look at this snippet:

(quoted diff) +1 -0
+		void *ptr = kmap_local_page(sg_page(cur.sgl)) + cur.start; + +

cur_len = min(cur.size, cur.remaining); + if (write) + memcpy(ptr, buf,
cur_len); + else + memcpy(buf, ptr, cur_len); + kunmap_local(ptr);

Quoted 8 lines
Quoted 2 lines

If 'cur.start' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

With added possibility to iterate over sgl pages in xe_res_cursor[1] it does not seems to be true.
Why? cur.start is limited by length of the segment (cur.sgl->length), if it happens to be more than 4k, it means sg_page(cur.sgl) points to consecutive pages and cur.start is correct.

Quoted 5 lines

I suppose if the cursor is changed to walk the pages not the dma address, yea i guess this would work. Still my much prefered way would just call hmm_range_fault or optionally save off the pages in xe_vma_userptr_pin_pages given at some point we will ditch SG tables for userptr in favor of drm gpusvm which will be page based.

Quoted 5 lines

Both alternatives seems for me suboptimal, as their result is what we have already in sg table, for a price of extra call (hmm_range_fault) and extra allocations (both). Most important they will complicate the code without clear benefit.

Again the SG is going to eventually be dropped in favor of a page array in GPU SVM. Also the general consensus among the Linux community is SG tables are terrible and wouldn't be surprised if eventually these get pulled out / deprecated in the kernel entirely. Fine if you want use a SG table I guess as we will just rewrite this once GPU SVM based userptr lands.

Quoted 3 lines

I will try to implement version with hmm_range_fault but I am still confused why we need to complicate things.

Correctness vs convenience...

It is a bit unfortunate our hmm_range_fault wrapper is built around a VMA argument - I suggested a more generic interface there to this easier in reviews but that feedback was ignored.

Matt

Quoted 101 lines

Regards

Andrzej

Quoted 95 lines

Matt

Quoted 92 lines
Quoted 2 lines

Likewise if 'cur_len' > 4k, then you are potentially pointing to an incorrect page and corrupting memory.

Again, in case of consecutive pages it should be in range.

Anyway if there is an issue with consecutive pages, which I am not aware of, we can always build sg list with segments pointing to 4K pages by modyfing xe_build_sg to call sg_alloc_table_from_pages_segment with 4K max segment size.

[1]: https://lore.kernel.org/intel-xe/20241011-xe_res_cursor_add_page_iterator-v3-1-0f8b8d3ab021@intel.com/

Regards
Andrzej

Quoted 78 lines

This loop would have to be changed to something like below which kmaps and accesses 1 page at a time... for (xe_res_first_sg(up-

Quoted 1 line

sg, offset, len, &cur); cur.remaining; xe_res_next(&cur, cur_len))

{ int segment_len; int remain;

cur_len = min(cur.size, cur.remaining); remain = cur_len;

for (i = 0; i < cur_len; i += segment_len) { phys_addr_t phys = iommu_iova_to_phys(sg_dma_address(cur.sgl) + i + cur.start); struct page *page = phys_to_page(phys); void *ptr = kmap_local_page(page); int ptr_offset = offset & ~PAGE_MASK;

segment_len = min(remain, PAGE_SIZE - ptr_offset);

if (write) memcpy(ptr + ptr_offset, buf + i, segment_len); else memcpy(buf + i, ptr + ptr_offset, segment_len); kunmap_local(ptr);

offset += segment_len; remain -= segment_len; } buf += cur_len; }

Quoted 4 lines

4. As you suggested in [3](?), modify xe_hmm_userptr_populate_range to keep hmm_range.hmm_pfns(or sth similar) in xe_userptr and use it later (instead of up->sg) to iterate over pages.

Or just call hmm_range_fault directly here and operate on returned pages directly.

BTW eventually all the userptr stuff is going to change and be based on GPU SVM [4]. Calling hmm_range_fault directly will always work though and likely the safest option.

Matt

[4] https://patchwork.freedesktop.org/patch/619809/? series=137870&rev=2

Quoted 44 lines

[1]: https://lore.kernel.org/intel-xe/20241011-
[email protected]/ [2]:
https://lore.kernel.org/intel-xe/Zw32fauoUmB6Iojk@DUT025-
TGLU.fm.intel.com/ [3]: https://patchwork.freedesktop.org/
patch/617481/?series=136572&rev=2#comment_1126527

Regards Andrzej

Quoted 36 lines

Matt

Quoted 34 lines
drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma); +	struct

xe_userptr *up = &uvma->userptr; + struct xe_res_cursor cur
= {}; + int cur_len, ret = 0; + + while (true) { +
down_read(&vm-

Quoted 1 line

userptr.notifier_lock); + if (!

xe_vma_userptr_check_repin(uvma)) + break; + +
spin_lock(&vm-

Quoted 4 lines

userptr.invalidated_lock); + list_del_init(&uvma-
userptr.invalidate_link); + spin_unlock(&vm-
userptr.invalidated_lock); + + up_read(&vm-
userptr.notifier_lock); + ret =

xe_vma_userptr_pin_pages(uvma); + if (ret) + return ret;

drivers/gpu/ renamed from drivers/gpu/drm/xe/xe_eudebug.c +1 -0
+	} + +	if (!up->sg) { +		ret = -EINVAL; +		goto

out_unlock_notifier; + } + + for (xe_res_first_sg(up->sg,
offset, len, &cur); cur.remaining; + xe_res_next(&cur,
cur_len)) { + void *ptr = kmap_local_page(sg_page(cur.sgl))
+ cur.start; + + cur_len = min(cur.size, cur.remaining); +
if (write) + memcpy(ptr, buf, cur_len); + else +
memcpy(buf, ptr, cur_len); + kunmap_local(ptr); + buf +=
cur_len; + } + ret = len; + +out_unlock_notifier: +
up_read(&vm-

Quoted 2 lines

userptr.notifier_lock); + return ret; +} diff --git a/
drivers/

gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h index c864dba35e1d..99b9a9b011de 100644 --- a/drivers/gpu/drm/xe/ xe_vm.h +++ b/drivers/gpu/drm/xe/xe_vm.h @@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm); void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap); void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p); void xe_vm_snapshot_free(struct xe_vm_snapshot *snap); + +int xe_uvma_access(struct xe_userptr_vma *uvma, u64 offset, + void *buf, u64 len, bool write); -- 2.34.1

#26 lore raw

PATCH 1/2 drm/xe: keep list of system pages in xe_userptr

In case of accessing data provided by userptr in the driver we need to have list of corresponding pages. This list is created by xe_hmm_userptr_populate_range and stored in xe_userptr.sgl sg list. Since we are not allowed to get pages from sg list, let's store them in separate field.

---
 drivers/gpu/drm/xe/xe_hmm.c      | 52 ++++++++++++++++----------------
 drivers/gpu/drm/xe/xe_vm_types.h |  2 ++
 2 files changed, 28 insertions(+), 26 deletions(-)
drivers/gpu/drm/xe/xe_hmm.c +26 -26
index 2c32dc46f7d4..6fc21e04ef2e 100644
@@ -44,13 +44,13 @@ static void xe_mark_range_accessed(struct hmm_range *range, bool write)
 }
 
 /*
- * xe_build_sg() - build a scatter gather table for all the physical pages/pfn
- * in a hmm_range. dma-map pages if necessary. dma-address is save in sg table
+ * xe_build_sg() - build a scatter gather table for given physical pages
+ * and perform dma-map. dma-address is saved in sg table
  * and will be used to program GPU page table later.
  *
  * @xe: the xe device who will access the dma-address in sg table
- * @range: the hmm range that we build the sg table from. range->hmm_pfns[]
- * has the pfn numbers of pages that back up this hmm address range.
+ * @pages: array of page pointers
+ * @npages: number of entries in @pages
  * @st: pointer to the sg table.
  * @write: whether we write to this range. This decides dma map direction
  * for system pages. If write we map it bi-diretional; otherwise
@@ -77,38 +77,22 @@ static void xe_mark_range_accessed(struct hmm_range *range, bool write)
  *
  * Returns 0 if successful; -ENOMEM if fails to allocate memory
  */
-static int xe_build_sg(struct xe_device *xe, struct hmm_range *range,
+static int xe_build_sg(struct xe_device *xe, struct page **pages, u64 npages,
 		       struct sg_table *st, bool write)
 {
 	struct device *dev = xe->drm.dev;
-	struct page **pages;
-	u64 i, npages;
 	int ret;
 
-	npages = xe_npages_in_range(range->start, range->end);
-	pages = kvmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
-	if (!pages)
-		return -ENOMEM;
-
-	for (i = 0; i < npages; i++) {
-		pages[i] = hmm_pfn_to_page(range->hmm_pfns[i]);
-		xe_assert(xe, !is_device_private_page(pages[i]));
-	}
-
 	ret = sg_alloc_table_from_pages_segment(st, pages, npages, 0, npages << PAGE_SHIFT,
 						xe_sg_segment_size(dev), GFP_KERNEL);
 	if (ret)
-		goto free_pages;
+		return ret;
 
 	ret = dma_map_sgtable(dev, st, write ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE,
 			      DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
-	if (ret) {
+	if (ret)
 		sg_free_table(st);
-		st = NULL;
-	}
 
-free_pages:
-	kvfree(pages);
 	return ret;
 }
 
@@ -136,6 +120,8 @@ void xe_hmm_userptr_free_sg(struct xe_userptr_vma *uvma)
 
 	sg_free_table(userptr->sg);
 	userptr->sg = NULL;
+	kvfree(userptr->pages);
+	userptr->pages = NULL;
 }
 
 /**
@@ -175,7 +161,7 @@ int xe_hmm_userptr_populate_range(struct xe_userptr_vma *uvma,
 	struct hmm_range hmm_range;
 	bool write = !xe_vma_read_only(vma);
 	unsigned long notifier_seq;
-	u64 npages;
+	u64 i, npages;
 	int ret;
 
 	userptr = &uvma->userptr;
@@ -238,9 +224,23 @@ int xe_hmm_userptr_populate_range(struct xe_userptr_vma *uvma,
 	if (ret)
 		goto free_pfns;
 
-	ret = xe_build_sg(vm->xe, &hmm_range, &userptr->sgt, write);
-	if (ret)
+	userptr->pages = kvmalloc_array(npages, sizeof(*userptr->pages), GFP_KERNEL);
+	if (!userptr->pages) {
+		ret = -ENOMEM;
+		goto free_pfns;
+	}
+
+	for (i = 0; i < npages; i++) {
+		userptr->pages[i] = hmm_pfn_to_page(hmm_range.hmm_pfns[i]);
+		xe_assert(vm->xe, !is_device_private_page(userptr->pages[i]));
+	}
+
+	ret = xe_build_sg(vm->xe, userptr->pages, npages, &userptr->sgt, write);
+	if (ret) {
+		kvfree(userptr->pages);
+		userptr->pages = NULL;
 		goto free_pfns;
+	}
 
 	xe_mark_range_accessed(&hmm_range, write);
 	userptr->sg = &userptr->sgt;
drivers/gpu/drm/xe/xe_vm_types.h +2 -0
index 557b047ebdd7..f1ec3925dea0 100644
@@ -53,6 +53,8 @@ struct xe_userptr {
 	 * @notifier: MMU notifier for user pointer (invalidation call back)
 	 */
 	struct mmu_interval_notifier notifier;
+	/** @pages: pointer to array of pointers to corresponding pages */
+	struct page **pages;
 	/** @sgt: storage for a scatter gather table */
 	struct sg_table sgt;
 	/** @sg: allocated scatter gather table */
Signature · 1 line
2.34.1
#27 lore raw

PATCH 2/2 drm/xe/eudebug: implement userptr_vma access

In reply to Matthew Brost

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)
v3: - iterate over system pages instead of DMA, fixes work with iommu enabled
- s/xe_uvma_access/xe_vm_uvma_access/ (Matt)
v4: use xe_userptr->pages, instead of sg to access pages (Matt)

---
 drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
 drivers/gpu/drm/xe/xe_vm.c      | 52 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_vm.h      |  3 ++
 3 files changed, 56 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index 1bff0a2cfaa1..569c8d0b2ef8 100644
@@ -3049,7 +3049,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
 		return ret;
 	}
 
-	return -EINVAL;
+	return xe_vm_userptr_access(to_userptr_vma(vma), offset, buf, bytes, write);
 }
 
 static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +52 -0

55 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..165eab494d59 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_vm_userptr_access(struct xe_userptr_vma *uvma, u64 offset,
+			 void *buf, u64 len, bool write);
Signature · 1 line
2.34.1
#28 lore raw

Re: [PATCH 2/2] drm/xe/eudebug: implement userptr_vma access

Hi Matt,

W dniu 28.10.2024 o 17:19, Andrzej Hajda pisze:

Quoted 11 lines

Debugger needs to read/write program's vmas including userptr_vma. Since hmm_range_fault is used to pin userptr vmas, it is possible to map those vmas from debugger context.

v2: pin pages vs notifier, move to vm.c (Matthew)
v3: - iterate over system pages instead of DMA, fixes work with iommu enabled
- s/xe_uvma_access/xe_vm_uvma_access/ (Matt)
v4: use xe_userptr->pages, instead of sg to access pages (Matt)

---

I have dropped hmm_range_fault approach, because it seems too complicated for me.

As I understand it requires whole locking dance as described in [1], probably also with added timeout handling.

[1]: https://www.kernel.org/doc/html/latest/mm/hmm.html#address-space-mirroring-implementation-and-api

Regards

Andrzej

Quoted 89 lines
  drivers/gpu/drm/xe/xe_eudebug.c |  2 +-
  drivers/gpu/drm/xe/xe_vm.c      | 52 +++++++++++++++++++++++++++++++++
  drivers/gpu/drm/xe/xe_vm.h      |  3 ++
  3 files changed, 56 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_eudebug.c +1 -1
index 1bff0a2cfaa1..569c8d0b2ef8 100644
@@ -3049,7 +3049,7 @@ static int xe_eudebug_vma_access(struct xe_vma *vma, u64 offset,
  		return ret;
  	}
 
-	return -EINVAL;
+	return xe_vm_userptr_access(to_userptr_vma(vma), offset, buf, bytes, write);
  }
 
  static int xe_eudebug_vm_access(struct xe_vm *vm, u64 offset,
drivers/gpu/drm/xe/xe_vm.c +52 -0
index e76a6df1eba1..8fd9eed41fe1 100644
@@ -3412,3 +3412,55 @@ void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
  	}
  	kvfree(snap);
  }
+
+int xe_vm_userptr_access(struct xe_userptr_vma *uvma, u64 offset,
+			 void *buf, u64 len, bool write)
+{
+	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	struct xe_userptr *up = &uvma->userptr;
+	struct page **page;
+	u64 left = len;
+	int ret = 0;
+
+	while (true) {
+		down_read(&vm->userptr.notifier_lock);
+		if (!xe_vma_userptr_check_repin(uvma))
+			break;
+
+		spin_lock(&vm->userptr.invalidated_lock);
+		list_del_init(&uvma->userptr.invalidate_link);
+		spin_unlock(&vm->userptr.invalidated_lock);
+
+		up_read(&vm->userptr.notifier_lock);
+		ret = xe_vma_userptr_pin_pages(uvma);
+		if (ret)
+			return ret;
+	}
+
+	if (!up->sg) {
+		ret = -EINVAL;
+		goto out_unlock_notifier;
+	}
+
+	page = &up->pages[offset >> PAGE_SHIFT];
+	offset &= ~PAGE_MASK;
+	for (;left > 0; ++page) {
+		u64 cur_len = min(PAGE_SIZE - offset, left);
+		void *ptr = kmap_local_page(page[0]);
+
+		if (write)
+			memcpy(ptr + offset, buf, cur_len);
+		else
+			memcpy(buf, ptr + offset, cur_len);
+		kunmap_local(ptr);
+		buf += cur_len;
+		left -= cur_len;
+		offset = 0;
+	}
+
+	ret = len;
+
+out_unlock_notifier:
+	up_read(&vm->userptr.notifier_lock);
+	return ret;
+}
drivers/gpu/drm/xe/xe_vm.h +3 -0
index c864dba35e1d..165eab494d59 100644
@@ -281,3 +281,6 @@ struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm);
  void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap);
  void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p);
  void xe_vm_snapshot_free(struct xe_vm_snapshot *snap);
+
+int xe_vm_userptr_access(struct xe_userptr_vma *uvma, u64 offset,
+			 void *buf, u64 len, bool write);
#29 lore raw

PATCH 13/18 drm/xe: Debug metadata create/destroy ioctls

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Ad a part of eu debug feature introduce debug metadata objects. These are to be used to pass metadata between client and debugger, by attaching them to vm_bind operations.

todo: WORK_IN_PROGRESS_* defines need to be reworded/refined when
the real usage and need is established by l0+gdb.

v2: - include uapi/drm/xe_drm.h
- metadata behind kconfig (Mika)

---
 drivers/gpu/drm/xe/Makefile                  |   3 +-
 drivers/gpu/drm/xe/xe_debug_metadata.c       | 108 +++++++++++++++++++
 drivers/gpu/drm/xe/xe_debug_metadata.h       |  50 +++++++++
 drivers/gpu/drm/xe/xe_debug_metadata_types.h |  28 +++++
 drivers/gpu/drm/xe/xe_device.c               |   5 +
 drivers/gpu/drm/xe/xe_device.h               |   2 +
 drivers/gpu/drm/xe/xe_device_types.h         |   7 ++
 drivers/gpu/drm/xe/xe_eudebug.c              |  13 +++
 include/uapi/drm/xe_drm.h                    |  53 ++++++++-
 9 files changed, 267 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.c
 create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.h
 create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata_types.h
drivers/gpu/drm/xe/Makefile +2 -1
index 660c1dbba8d0..a94f771529a4 100644
@@ -114,7 +114,8 @@ xe-y += xe_bb.o \
 	xe_wa.o \
 	xe_wopcm.o
 
-xe-$(CONFIG_DRM_XE_EUDEBUG) += xe_eudebug.o
+xe-$(CONFIG_DRM_XE_EUDEBUG) += xe_eudebug.o \
+	xe_debug_metadata.o
 
 xe-$(CONFIG_HMM_MIRROR) += xe_hmm.o
 
drivers/gpu/drm/xe/xe_debug_metadata.c new file +108 -0

108 lines not on this page · raw message

drivers/gpu/drm/xe/xe_debug_metadata.h new file +50 -0

50 lines not on this page · raw message

drivers/gpu/drm/xe/xe_debug_metadata_types.h new file +28 -0
new file mode 100644
index 000000000000..508f2fdbbc42
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef _XE_DEBUG_METADATA_TYPES_H_
+#define _XE_DEBUG_METADATA_TYPES_H_
+
+#include <linux/kref.h>
+
+struct xe_debug_metadata {
+	/** @type: type of given metadata */
+	u64 type;
+
+	/** @ptr: copy of userptr, given as a metadata payload */
+	void *ptr;
+
+	/** @len: length, in bytes of the metadata */
+	u64 len;
+
+	/** @id */
+	u64 id;
+
+	/** @ref: reference count */
+	struct kref refcount;
+};
+
+#endif
drivers/gpu/drm/xe/xe_device.c +5 -0
index 77049c56c26c..e0d55e11ace6 100644
@@ -24,6 +24,7 @@
 #include "xe_bo.h"
 #include "xe_debugfs.h"
 #include "xe_devcoredump.h"
+#include "xe_debug_metadata.h"
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
 #include "xe_drv.h"
@@ -203,6 +204,10 @@ static const struct drm_ioctl_desc xe_ioctls[] = {
 			  DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(XE_OBSERVATION, xe_observation_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(XE_EUDEBUG_CONNECT, xe_eudebug_connect_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(XE_DEBUG_METADATA_CREATE, xe_debug_metadata_create_ioctl,
+			  DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(XE_DEBUG_METADATA_DESTROY, xe_debug_metadata_destroy_ioctl,
+			  DRM_RENDER_ALLOW),
 };
 
 static long xe_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
drivers/gpu/drm/xe/xe_device.h +2 -0
index b2fc85b1d26e..ce01e3fc9fdf 100644
@@ -204,6 +204,8 @@ static inline int xe_eudebug_needs_lock(const unsigned int cmd)
 	case DRM_XE_EXEC_QUEUE_CREATE:
 	case DRM_XE_EXEC_QUEUE_DESTROY:
 	case DRM_XE_EUDEBUG_CONNECT:
+	case DRM_XE_DEBUG_METADATA_CREATE:
+	case DRM_XE_DEBUG_METADATA_DESTROY:
 		return 1;
 	}
 
drivers/gpu/drm/xe/xe_device_types.h +7 -0
index 562cc8930533..40b0f61b64b8 100644
@@ -671,6 +671,13 @@ struct xe_file {
 	struct {
 		/** @client_link: list entry in xe_device.clients.list */
 		struct list_head client_link;
+
+		struct {
+			/** @xa: xarray to store debug metadata */
+			struct xarray xa;
+			/** @lock: protects debug metadata xarray */
+			struct mutex lock;
+		} metadata;
 	} eudebug;
 #endif
 };
drivers/gpu/drm/xe/xe_eudebug.c +13 -0
index b09d7414cfe3..6ee06a58b640 100644
@@ -21,6 +21,7 @@
 #include "xe_assert.h"
 #include "xe_bo.h"
 #include "xe_device.h"
+#include "xe_debug_metadata.h"
 #include "xe_eudebug.h"
 #include "xe_eudebug_types.h"
 #include "xe_exec_queue.h"
@@ -2132,6 +2133,8 @@ void xe_eudebug_file_open(struct xe_file *xef)
 	struct xe_eudebug *d;
 
 	INIT_LIST_HEAD(&xef->eudebug.client_link);
+	mutex_init(&xef->eudebug.metadata.lock);
+	xa_init_flags(&xef->eudebug.metadata.xa, XA_FLAGS_ALLOC1);
 
 	down_read(&xef->xe->eudebug.discovery_lock);
 
@@ -2149,12 +2152,22 @@ void xe_eudebug_file_open(struct xe_file *xef)
 void xe_eudebug_file_close(struct xe_file *xef)
 {
 	struct xe_eudebug *d;
+	unsigned long idx;
+	struct xe_debug_metadata *mdata;
 
 	down_read(&xef->xe->eudebug.discovery_lock);
 	d = xe_eudebug_get(xef);
 	if (d)
 		xe_eudebug_event_put(d, client_destroy_event(d, xef));
 
+	mutex_lock(&xef->eudebug.metadata.lock);
+	xa_for_each(&xef->eudebug.metadata.xa, idx, mdata)
+		xe_debug_metadata_put(mdata);
+	mutex_unlock(&xef->eudebug.metadata.lock);
+
+	xa_destroy(&xef->eudebug.metadata.xa);
+	mutex_destroy(&xef->eudebug.metadata.lock);
+
 	spin_lock(&xef->xe->clients.lock);
 	list_del_init(&xef->eudebug.client_link);
 	spin_unlock(&xef->xe->clients.lock);
include/uapi/drm/xe_drm.h +52 -1

71 lines not on this page · raw message

Signature · 1 line
2.34.1
#30 lore raw

Re: [PATCH 13/18] drm/xe: Debug metadata create/destroy ioctls

Hi,

Quoted 113 lines · Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Ad a part of eu debug feature introduce debug metadata objects. These are to be used to pass metadata between client and debugger, by attaching them to vm_bind operations.

todo: WORK_IN_PROGRESS_* defines need to be reworded/refined when
the real usage and need is established by l0+gdb.

v2: - include uapi/drm/xe_drm.h
- metadata behind kconfig (Mika)

---
  drivers/gpu/drm/xe/Makefile                  |   3 +-
  drivers/gpu/drm/xe/xe_debug_metadata.c       | 108 +++++++++++++++++++
  drivers/gpu/drm/xe/xe_debug_metadata.h       |  50 +++++++++
  drivers/gpu/drm/xe/xe_debug_metadata_types.h |  28 +++++
  drivers/gpu/drm/xe/xe_device.c               |   5 +
  drivers/gpu/drm/xe/xe_device.h               |   2 +
  drivers/gpu/drm/xe/xe_device_types.h         |   7 ++
  drivers/gpu/drm/xe/xe_eudebug.c              |  13 +++
  include/uapi/drm/xe_drm.h                    |  53 ++++++++-
  9 files changed, 267 insertions(+), 2 deletions(-)

create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.c create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.h create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata_types.h

drivers/gpu/drm/xe/Makefile +2 -1
index 660c1dbba8d0..a94f771529a4 100644
@@ -114,7 +114,8 @@ xe-y += xe_bb.o \
  	xe_wa.o \
  	xe_wopcm.o
 
-xe-$(CONFIG_DRM_XE_EUDEBUG) += xe_eudebug.o
+xe-$(CONFIG_DRM_XE_EUDEBUG) += xe_eudebug.o \
+	xe_debug_metadata.o
 
  xe-$(CONFIG_HMM_MIRROR) += xe_hmm.o
 
drivers/gpu/drm/xe/xe_debug_metadata.c new file +64 -0
new file mode 100644
index 000000000000..72a00b628475
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include "xe_debug_metadata.h"
+
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+#include <uapi/drm/xe_drm.h>
+
+#include "xe_device.h"
+#include "xe_macros.h"
+
+static void xe_debug_metadata_release(struct kref *ref)
+{
+	struct xe_debug_metadata *mdata = container_of(ref, struct xe_debug_metadata, refcount);
+
+	kvfree(mdata->ptr);
+	kfree(mdata);
+}
+
+void xe_debug_metadata_put(struct xe_debug_metadata *mdata)
+{
+	kref_put(&mdata->refcount, xe_debug_metadata_release);
+}
+
+int xe_debug_metadata_create_ioctl(struct drm_device *dev,
+				   void *data,
+				   struct drm_file *file)
+{
+	struct xe_device *xe = to_xe_device(dev);
+	struct xe_file *xef = to_xe_file(file);
+	struct drm_xe_debug_metadata_create *args = data;
+	struct xe_debug_metadata *mdata;
+	int err;
+	u32 id;
+
+	if (XE_IOCTL_DBG(xe, args->extensions))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, args->type > DRM_XE_DEBUG_METADATA_PROGRAM_MODULE))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, !args->user_addr || !args->len))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, !access_ok(u64_to_user_ptr(args->user_addr), args->len)))
+		return -EFAULT;
+
+	mdata = kzalloc(sizeof(*mdata), GFP_KERNEL);
+	if (!mdata)
+		return -ENOMEM;
+
+	mdata->len = args->len;
+	mdata->type = args->type;
+
+	mdata->ptr = kvmalloc(mdata->len, GFP_KERNEL);
+	if (!mdata->ptr) {
+		kfree(mdata);
+		return -ENOMEM;
+	}
+	kref_init(&mdata->refcount);
+
+	err = copy_from_user(mdata->ptr, u64_to_user_ptr(args->user_addr), mdata->len);

drivers/gpu/drm/xe/xe_debug_metadata.c line 74

Quoted 10 lines
drivers/gpu/drm/xe/xe_debug_metadata.c new file +10 -0
+	if (err) {
+		err = -EFAULT;
+		goto put_mdata;
+	}
+
+	mutex_lock(&xef->eudebug.metadata.lock);
+	err = xa_alloc(&xef->eudebug.metadata.xa, &id, mdata, xa_limit_32b, GFP_KERNEL);
+	mutex_unlock(&xef->eudebug.metadata.lock);
+
+	args->metadata_id = id;

Just some drive-by-comments. Potentially this is passing uninitialised data from the stack back to userspace, assuming the xa_alloc here fails? Maybe safer to leave metadata_id zeroed on err?

drivers/gpu/drm/xe/xe_debug_metadata.c line 75

Quoted 1 line
drivers/gpu/drm/xe/xe_debug_metadata.c new file +1 -0
+	mdata->id = id;

It looks like mdata can go out of scope since user can technically guess the id and call the destroy ioctl before this ioctl returns. I think publishing the id needs to go last or needs some kind of locking.

Also are we sure we need to store the user id in mdata in the first place? Usually for user id kmd doesn't need to track it in such a way.

#31 lore raw

Re: [PATCH 13/18] drm/xe: Debug metadata create/destroy ioctls

Quoted 122 lines · Matthew Auld

Hi,

Quoted 119 lines · Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Ad a part of eu debug feature introduce debug metadata objects. These are to be used to pass metadata between client and debugger, by attaching them to vm_bind operations.

todo: WORK_IN_PROGRESS_* defines need to be reworded/refined when
the real usage and need is established by l0+gdb.

v2: - include uapi/drm/xe_drm.h
- metadata behind kconfig (Mika)

---
  drivers/gpu/drm/xe/Makefile                  |   3 +-
  drivers/gpu/drm/xe/xe_debug_metadata.c       | 108 +++++++++++++++++++
  drivers/gpu/drm/xe/xe_debug_metadata.h       |  50 +++++++++
  drivers/gpu/drm/xe/xe_debug_metadata_types.h |  28 +++++
  drivers/gpu/drm/xe/xe_device.c               |   5 +
  drivers/gpu/drm/xe/xe_device.h               |   2 +
  drivers/gpu/drm/xe/xe_device_types.h         |   7 ++
  drivers/gpu/drm/xe/xe_eudebug.c              |  13 +++
  include/uapi/drm/xe_drm.h                    |  53 ++++++++-
  9 files changed, 267 insertions(+), 2 deletions(-)

create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.c create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata.h create mode 100644 drivers/gpu/drm/xe/xe_debug_metadata_types.h

drivers/gpu/drm/xe/Makefile +2 -1
index 660c1dbba8d0..a94f771529a4 100644
@@ -114,7 +114,8 @@ xe-y += xe_bb.o \
  	xe_wa.o \
  	xe_wopcm.o
 
-xe-$(CONFIG_DRM_XE_EUDEBUG) += xe_eudebug.o
+xe-$(CONFIG_DRM_XE_EUDEBUG) += xe_eudebug.o \
+	xe_debug_metadata.o
 
  xe-$(CONFIG_HMM_MIRROR) += xe_hmm.o
 
drivers/gpu/drm/xe/xe_debug_metadata.c new file +70 -0
new file mode 100644
index 000000000000..72a00b628475
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include "xe_debug_metadata.h"
+
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+#include <uapi/drm/xe_drm.h>
+
+#include "xe_device.h"
+#include "xe_macros.h"
+
+static void xe_debug_metadata_release(struct kref *ref)
+{
+	struct xe_debug_metadata *mdata = container_of(ref, struct xe_debug_metadata, refcount);
+
+	kvfree(mdata->ptr);
+	kfree(mdata);
+}
+
+void xe_debug_metadata_put(struct xe_debug_metadata *mdata)
+{
+	kref_put(&mdata->refcount, xe_debug_metadata_release);
+}
+
+int xe_debug_metadata_create_ioctl(struct drm_device *dev,
+				   void *data,
+				   struct drm_file *file)
+{
+	struct xe_device *xe = to_xe_device(dev);
+	struct xe_file *xef = to_xe_file(file);
+	struct drm_xe_debug_metadata_create *args = data;
+	struct xe_debug_metadata *mdata;
+	int err;
+	u32 id;
+
+	if (XE_IOCTL_DBG(xe, args->extensions))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, args->type > DRM_XE_DEBUG_METADATA_PROGRAM_MODULE))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, !args->user_addr || !args->len))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, !access_ok(u64_to_user_ptr(args->user_addr), args->len)))
+		return -EFAULT;
+
+	mdata = kzalloc(sizeof(*mdata), GFP_KERNEL);
+	if (!mdata)
+		return -ENOMEM;
+
+	mdata->len = args->len;
+	mdata->type = args->type;
+
+	mdata->ptr = kvmalloc(mdata->len, GFP_KERNEL);
+	if (!mdata->ptr) {
+		kfree(mdata);
+		return -ENOMEM;
+	}
+	kref_init(&mdata->refcount);
+
+	err = copy_from_user(mdata->ptr, u64_to_user_ptr(args->user_addr), mdata->len);
+	if (err) {
+		err = -EFAULT;
+		goto put_mdata;
+	}
+
+	mutex_lock(&xef->eudebug.metadata.lock);
Quoted 8 lines

drivers/gpu/drm/xe/xe_debug_metadata.c line 74

Quoted 4 lines
drivers/gpu/drm/xe/xe_debug_metadata.c new file +4 -0
+	err = xa_alloc(&xef->eudebug.metadata.xa, &id, mdata, xa_limit_32b, GFP_KERNEL);
+	mutex_unlock(&xef->eudebug.metadata.lock);
+
+	args->metadata_id = id;

Just some drive-by-comments. Potentially this is passing uninitialised data from the stack back to userspace, assuming the xa_alloc here fails? Maybe safer to leave metadata_id zeroed on err?

That's mine over oversight. Thanks for catching that!

Quoted 9 lines

drivers/gpu/drm/xe/xe_debug_metadata.c line 75

Quoted 2 lines
drivers/gpu/drm/xe/xe_debug_metadata.c new file +1 -0
+	mdata->id = id;

It looks like mdata can go out of scope since user can technically guess the id and call the destroy ioctl before this ioctl returns. I think publishing the id needs to go last or needs some kind of locking.

Also are we sure we need to store the user id in mdata in the first place? Usually for user id kmd doesn't need to track it in such a way.

I think we can get rid of it fairly easy, I will to that in next revision.

Regards,
Dominik

#32 lore raw

PATCH 14/18 drm/xe: Attach debug metadata to vma

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Introduces a vm_bind_op extension, enabling users to attach metadata objects to each [OP_MAP|OP_MAP_USERPTR] operation. This interface will be utilized by the EU debugger to relay information about the contents of specified VMAs from the debugee to the debugger process.

v2: move vma metadata handling behind Kconfig (Mika)

---
 drivers/gpu/drm/xe/xe_debug_metadata.c | 120 +++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_debug_metadata.h |  48 ++++++++++
 drivers/gpu/drm/xe/xe_vm.c             |  99 +++++++++++++++++++-
 drivers/gpu/drm/xe/xe_vm_types.h       |  27 ++++++
 include/uapi/drm/xe_drm.h              |  19 ++++
 5 files changed, 309 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/xe_debug_metadata.c +120 -0
index 72a00b628475..303640f8ba1f 100644
@@ -10,6 +10,113 @@
 
 #include "xe_device.h"
 #include "xe_macros.h"
+#include "xe_vm.h"
+
+void xe_eudebug_free_vma_metadata(struct xe_eudebug_vma_metadata *mdata)
+{
+	struct xe_vma_debug_metadata *vmad, *tmp;
+
+	list_for_each_entry_safe(vmad, tmp, &mdata->list, link) {
+		list_del(&vmad->link);
+		kfree(vmad);
+	}
+}
+
+static struct xe_vma_debug_metadata *
+vma_new_debug_metadata(u32 metadata_id, u64 cookie)
+{
+	struct xe_vma_debug_metadata *vmad;
+
+	vmad = kzalloc(sizeof(*vmad), GFP_KERNEL);
+	if (!vmad)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&vmad->link);
+
+	vmad->metadata_id = metadata_id;
+	vmad->cookie = cookie;
+
+	return vmad;
+}
+
+int xe_eudebug_copy_vma_metadata(struct xe_eudebug_vma_metadata *from,
+				 struct xe_eudebug_vma_metadata *to)
+{
+	struct xe_vma_debug_metadata *vmad, *vma;
+
+	list_for_each_entry(vmad, &from->list, link) {
+		vma = vma_new_debug_metadata(vmad->metadata_id, vmad->cookie);
+		if (IS_ERR(vma))
+			return PTR_ERR(vma);
+
+		list_add_tail(&vmad->link, &to->list);
+	}
+
+	return 0;
+}
+
+static int vma_new_debug_metadata_op(struct xe_vma_op *op,
+				     u32 metadata_id, u64 cookie,
+				     u64 flags)
+{
+	struct xe_vma_debug_metadata *vmad;
+
+	vmad = vma_new_debug_metadata(metadata_id, cookie);
+	if (IS_ERR(vmad))
+		return PTR_ERR(vmad);
+
+	list_add_tail(&vmad->link, &op->map.eudebug.metadata.list);
+
+	return 0;
+}
+
+int vm_bind_op_ext_attach_debug(struct xe_device *xe,
+				struct xe_file *xef,
+				struct drm_gpuva_ops *ops,
+				u32 operation, u64 extension)
+{
+	u64 __user *address = u64_to_user_ptr(extension);
+	struct drm_xe_vm_bind_op_ext_attach_debug ext;
+	struct xe_debug_metadata *mdata;
+	struct drm_gpuva_op *__op;
+	int err;
+
+	err = __copy_from_user(&ext, address, sizeof(ext));
+	if (XE_IOCTL_DBG(xe, err))
+		return -EFAULT;
+
+	if (XE_IOCTL_DBG(xe,
+			 operation != DRM_XE_VM_BIND_OP_MAP_USERPTR &&
+			 operation != DRM_XE_VM_BIND_OP_MAP))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, ext.flags))
+		return -EINVAL;
+
+	mdata = xe_debug_metadata_get(xef, (u32)ext.metadata_id);
+	if (XE_IOCTL_DBG(xe, !mdata))
+		return -ENOENT;
+
+	/* care about metadata existence only on the time of attach */
+	xe_debug_metadata_put(mdata);
+
+	if (!ops)
+		return 0;
+
+	drm_gpuva_for_each_op(__op, ops) {
+		struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
+
+		if (op->base.op == DRM_GPUVA_OP_MAP) {
+			err = vma_new_debug_metadata_op(op,
+							ext.metadata_id,
+							ext.cookie,
+							ext.flags);
+			if (err)
+				return err;
+		}
+	}
+	return 0;
+}
 
 static void xe_debug_metadata_release(struct kref *ref)
 {
@@ -24,6 +131,19 @@ void xe_debug_metadata_put(struct xe_debug_metadata *mdata)
 	kref_put(&mdata->refcount, xe_debug_metadata_release);
 }
 
+struct xe_debug_metadata *xe_debug_metadata_get(struct xe_file *xef, u32 id)
+{
+	struct xe_debug_metadata *mdata;
+
+	mutex_lock(&xef->eudebug.metadata.lock);
+	mdata = xa_load(&xef->eudebug.metadata.xa, id);
+	if (mdata)
+		kref_get(&mdata->refcount);
+	mutex_unlock(&xef->eudebug.metadata.lock);
+
+	return mdata;
+}
+
 int xe_debug_metadata_create_ioctl(struct drm_device *dev,
 				   void *data,
 				   struct drm_file *file)
drivers/gpu/drm/xe/xe_debug_metadata.h +48 -0

79 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.c +95 -4

190 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm_types.h +27 -0
index 557b047ebdd7..1c5776194e54 100644
@@ -70,6 +70,14 @@ struct xe_userptr {
 #endif
 };
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+struct xe_eudebug_vma_metadata {
+	struct list_head list;
+};
+#else
+struct xe_eudebug_vma_metadata { };
+#endif
+
 struct xe_vma {
 	/** @gpuva: Base GPUVA object */
 	struct drm_gpuva gpuva;
@@ -121,6 +129,11 @@ struct xe_vma {
 	 * Needs to be signalled before UNMAP can be processed.
 	 */
 	struct xe_user_fence *ufence;
+
+	struct {
+		/** @metadata: List of vma debug metadata */
+		struct xe_eudebug_vma_metadata metadata;
+	} eudebug;
 };
 
 /**
@@ -311,6 +324,10 @@ struct xe_vma_op_map {
 	bool dumpable;
 	/** @pat_index: The pat index to use for this operation. */
 	u16 pat_index;
+	struct  {
+		/** @vma_metadata: List of vma debug metadata */
+		struct xe_eudebug_vma_metadata metadata;
+	} eudebug;
 };
 
 /** struct xe_vma_op_remap - VMA remap operation */
@@ -388,4 +405,14 @@ struct xe_vma_ops {
 #endif
 };
 
+struct xe_vma_debug_metadata {
+	/** @debug.metadata: id of attached xe_debug_metadata */
+	u32 metadata_id;
+	/** @debug.cookie: user defined cookie */
+	u64 cookie;
+
+	/** @link: list of metadata attached to vma */
+	struct list_head link;
+};
+
 #endif
include/uapi/drm/xe_drm.h +19 -0
index 0d75a072b838..2416971fbf2d 100644
@@ -886,6 +886,23 @@ struct drm_xe_vm_destroy {
 	__u64 reserved[2];
 };
 
+struct drm_xe_vm_bind_op_ext_attach_debug {
+	/** @base: base user extension */
+	struct drm_xe_user_extension base;
+
+	/** @id: Debug object id from create metadata */
+	__u64 metadata_id;
+
+	/** @flags: Flags */
+	__u64 flags;
+
+	/** @cookie: Cookie */
+	__u64 cookie;
+
+	/** @reserved: Reserved */
+	__u64 reserved;
+};
+
 /**
  * struct drm_xe_vm_bind_op - run bind operations
  *
@@ -910,7 +927,9 @@ struct drm_xe_vm_destroy {
  *    handle MBZ, and the BO offset MBZ. This flag is intended to
  *    implement VK sparse bindings.
  */
+
 struct drm_xe_vm_bind_op {
+#define XE_VM_BIND_OP_EXTENSIONS_ATTACH_DEBUG 0
 	/** @extensions: Pointer to the first extension struct, if any */
 	__u64 extensions;
 
Signature · 1 line
2.34.1
#33 lore raw

PATCH 15/18 drm/xe/eudebug: Add debug metadata support for xe_eudebug

In reply to Mika Kuoppala

From: Dominik Grzegorzek <[email protected]>

Reflect debug metadata resource creation/destroy as events passed to the debugger. Introduce ioctl allowing to read metadata content on demand.

Each VMA can have multiple metadata attached and it is passed from user on BIND or it's copied on internal remap.

Xe EU Debugger on VM BIND will inform about VMA metadata attachements during bind IOCTL sending proper OP event.

v2: - checkpatch (Maciej, Tilak)
- struct alignment (Matthew)
- Kconfig (Mika)

---
 drivers/gpu/drm/xe/xe_debug_metadata.c |   7 +-
 drivers/gpu/drm/xe/xe_eudebug.c        | 328 ++++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_eudebug.h        |  13 +-
 drivers/gpu/drm/xe/xe_eudebug_types.h  |  27 +-
 drivers/gpu/drm/xe/xe_vm.c             |   2 +-
 include/uapi/drm/xe_drm_eudebug.h      |  30 +++
 6 files changed, 396 insertions(+), 11 deletions(-)
drivers/gpu/drm/xe/xe_debug_metadata.c +6 -1
index 303640f8ba1f..d02ec99f7ca1 100644
@@ -9,6 +9,7 @@
 #include <uapi/drm/xe_drm.h>
 
 #include "xe_device.h"
+#include "xe_eudebug.h"
 #include "xe_macros.h"
 #include "xe_vm.h"
 
@@ -158,7 +159,7 @@ int xe_debug_metadata_create_ioctl(struct drm_device *dev,
 	if (XE_IOCTL_DBG(xe, args->extensions))
 		return -EINVAL;
 
-	if (XE_IOCTL_DBG(xe, args->type > DRM_XE_DEBUG_METADATA_PROGRAM_MODULE))
+	if (XE_IOCTL_DBG(xe, args->type >= WORK_IN_PROGRESS_DRM_XE_DEBUG_METADATA_NUM))
 		return -EINVAL;
 
 	if (XE_IOCTL_DBG(xe, !args->user_addr || !args->len))
@@ -197,6 +198,8 @@ int xe_debug_metadata_create_ioctl(struct drm_device *dev,
 	if (err)
 		goto put_mdata;
 
+	xe_eudebug_debug_metadata_create(xef, mdata);
+
 	return 0;
 
 put_mdata:
@@ -222,6 +225,8 @@ int xe_debug_metadata_destroy_ioctl(struct drm_device *dev,
 	if (XE_IOCTL_DBG(xe, !mdata))
 		return -ENOENT;
 
+	xe_eudebug_debug_metadata_destroy(xef, mdata);
+
 	xe_debug_metadata_put(mdata);
 
 	return 0;
drivers/gpu/drm/xe/xe_eudebug.c +322 -6

413 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug.h +11 -2
index ae720a1b05d9..766d0a94d200 100644
@@ -16,6 +16,8 @@ struct xe_vma;
 struct xe_exec_queue;
 struct xe_hw_engine;
 struct xe_user_fence;
+struct xe_debug_metadata;
+struct drm_gpuva_ops;
 
 #if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
 
@@ -39,7 +41,8 @@ void xe_eudebug_exec_queue_destroy(struct xe_file *xef, struct xe_exec_queue *q)
 
 void xe_eudebug_vm_init(struct xe_vm *vm);
 void xe_eudebug_vm_bind_start(struct xe_vm *vm);
-void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range);
+void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range,
+			       struct drm_gpuva_ops *ops);
 void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int err);
 
 int xe_eudebug_vm_bind_ufence(struct xe_user_fence *ufence);
@@ -49,6 +52,9 @@ void xe_eudebug_ufence_fini(struct xe_user_fence *ufence);
 struct xe_eudebug *xe_eudebug_get(struct xe_file *xef);
 void xe_eudebug_put(struct xe_eudebug *d);
 
+void xe_eudebug_debug_metadata_create(struct xe_file *xef, struct xe_debug_metadata *m);
+void xe_eudebug_debug_metadata_destroy(struct xe_file *xef, struct xe_debug_metadata *m);
+
 #else
 
 static inline int xe_eudebug_connect_ioctl(struct drm_device *dev,
@@ -71,7 +77,7 @@ static inline void xe_eudebug_exec_queue_destroy(struct xe_file *xef, struct xe_
 
 static inline void xe_eudebug_vm_init(struct xe_vm *vm) { }
 static inline void xe_eudebug_vm_bind_start(struct xe_vm *vm) { }
-static inline void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range) { }
+static inline void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range, struct drm_gpuva_ops *ops) { }
 static inline void xe_eudebug_vm_bind_end(struct xe_vm *vm, bool has_ufence, int err) { }
 
 static inline int xe_eudebug_vm_bind_ufence(struct xe_user_fence *ufence) { return 0; }
@@ -81,6 +87,9 @@ static inline void xe_eudebug_ufence_fini(struct xe_user_fence *ufence) { }
 static inline struct xe_eudebug *xe_eudebug_get(struct xe_file *xef) { return NULL; }
 static inline void xe_eudebug_put(struct xe_eudebug *d) { }
 
+static inline void xe_eudebug_debug_metadata_create(struct xe_file *xef, struct xe_debug_metadata *m) { }
+static inline void xe_eudebug_debug_metadata_destroy(struct xe_file *xef, struct xe_debug_metadata *m) { }
+
 #endif /* CONFIG_DRM_XE_EUDEBUG */
 
 #endif
drivers/gpu/drm/xe/xe_eudebug_types.h +26 -1
index aae7bc476c7e..b79d4c078216 100644
@@ -56,7 +56,8 @@ struct xe_eudebug_resource {
 #define XE_EUDEBUG_RES_TYPE_VM		1
 #define XE_EUDEBUG_RES_TYPE_EXEC_QUEUE	2
 #define XE_EUDEBUG_RES_TYPE_LRC		3
-#define XE_EUDEBUG_RES_TYPE_COUNT	(XE_EUDEBUG_RES_TYPE_LRC + 1)
+#define XE_EUDEBUG_RES_TYPE_METADATA	4
+#define XE_EUDEBUG_RES_TYPE_COUNT	(XE_EUDEBUG_RES_TYPE_METADATA + 1)
 
 /**
  * struct xe_eudebug_resources - eudebug resources for all types
@@ -300,4 +301,28 @@ struct xe_eudebug_event_vm_bind_ufence {
 	u64 vm_bind_ref_seqno;
 };
 
+struct xe_eudebug_event_metadata {
+	struct xe_eudebug_event base;
+
+	/** @client_handle: client for the attention */
+	u64 client_handle;
+
+	/** @metadata_handle: debug metadata handle it's created/destroyed */
+	u64 metadata_handle;
+
+	/* @type: metadata type, refer to xe_drm.h for options */
+	u64 type;
+
+	/* @len: size of metadata paylad */
+	u64 len;
+};
+
+struct xe_eudebug_event_vm_bind_op_metadata {
+	struct xe_eudebug_event base;
+	u64 vm_bind_op_ref_seqno;
+
+	u64 metadata_handle;
+	u64 metadata_cookie;
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +1 -1
index 8c48223892c6..24a7ee1a6e58 100644
@@ -3172,7 +3172,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		if (err)
 			goto unwind_ops;
 
-		xe_eudebug_vm_bind_op_add(vm, op, addr, range);
+		xe_eudebug_vm_bind_op_add(vm, op, addr, range, ops[i]);
 
 #ifdef TEST_VM_OPS_ERROR
 		if (flags & FORCE_OP_ERROR) {
include/uapi/drm/xe_drm_eudebug.h +30 -0
index 9917280400d6..2ebf21e15f5b 100644
@@ -19,6 +19,7 @@ extern "C" {
 #define DRM_XE_EUDEBUG_IOCTL_EU_CONTROL		_IOWR('j', 0x2, struct drm_xe_eudebug_eu_control)
 #define DRM_XE_EUDEBUG_IOCTL_ACK_EVENT		_IOW('j', 0x4, struct drm_xe_eudebug_ack_event)
 #define DRM_XE_EUDEBUG_IOCTL_VM_OPEN		_IOW('j', 0x1, struct drm_xe_eudebug_vm_open)
+#define DRM_XE_EUDEBUG_IOCTL_READ_METADATA	_IOWR('j', 0x3, struct drm_xe_eudebug_read_metadata)
 
 /* XXX: Document events to match their internal counterparts when moved to xe_drm.h */
 struct drm_xe_eudebug_event {
@@ -34,6 +35,8 @@ struct drm_xe_eudebug_event {
 #define DRM_XE_EUDEBUG_EVENT_VM_BIND		6
 #define DRM_XE_EUDEBUG_EVENT_VM_BIND_OP		7
 #define DRM_XE_EUDEBUG_EVENT_VM_BIND_UFENCE	8
+#define DRM_XE_EUDEBUG_EVENT_METADATA		9
+#define DRM_XE_EUDEBUG_EVENT_VM_BIND_OP_METADATA 10
 
 	__u16 flags;
 #define DRM_XE_EUDEBUG_EVENT_CREATE		(1 << 0)
@@ -188,6 +191,33 @@ struct drm_xe_eudebug_vm_open {
 	__u64 timeout_ns;
 };
 
+struct drm_xe_eudebug_read_metadata {
+	__u64 client_handle;
+	__u64 metadata_handle;
+	__u32 flags;
+	__u32 reserved;
+	__u64 ptr;
+	__u64 size;
+};
+
+struct drm_xe_eudebug_event_metadata {
+	struct drm_xe_eudebug_event base;
+
+	__u64 client_handle;
+	__u64 metadata_handle;
+	/* XXX: Refer to xe_drm.h for fields */
+	__u64 type;
+	__u64 len;
+};
+
+struct drm_xe_eudebug_event_vm_bind_op_metadata {
+	struct drm_xe_eudebug_event base;
+	__u64 vm_bind_op_ref_seqno; /* *_event_vm_bind_op.base.seqno */
+
+	__u64 metadata_handle;
+	__u64 metadata_cookie;
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.34.1
#34 lore raw

PATCH 16/18 drm/xe/eudebug: Implement vm_bind_op discovery

In reply to Mika Kuoppala

Follow the vm bind, vm_bind op sequence for discovery process of a vm with the vmas it has. Send events for ops and attach metadata if available.

v2: - Fix bad op ref seqno (Christoph)
- with discovery semaphore, we dont need vm lock (Matthew)

---
 drivers/gpu/drm/xe/xe_eudebug.c | 45 +++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
drivers/gpu/drm/xe/xe_eudebug.c +45 -0
index 368e1a987f7a..faa49ba55b78 100644
@@ -3153,6 +3153,47 @@ void xe_eudebug_debug_metadata_destroy(struct xe_file *xef, struct xe_debug_meta
 	xe_eudebug_event_put(d, debug_metadata_destroy_event(d, xef, m));
 }
 
+static int vm_discover_binds(struct xe_eudebug *d, struct xe_vm *vm)
+{
+	struct drm_gpuva *va;
+	unsigned int num_ops = 0, send_ops = 0;
+	u64 ref_seqno = 0;
+	int err;
+
+	/*
+	 * Currently only vm_bind_ioctl inserts vma's
+	 * and with discovery lock, we have exclusivity.
+	 */
+	lockdep_assert_held_write(&d->xe->eudebug.discovery_lock);
+
+	drm_gpuvm_for_each_va(va, &vm->gpuvm)
+		num_ops++;
+
+	if (!num_ops)
+		return 0;
+
+	err = vm_bind_event(d, vm, num_ops, &ref_seqno);
+	if (err)
+		return err;
+
+	drm_gpuvm_for_each_va(va, &vm->gpuvm) {
+		struct xe_vma *vma = container_of(va, struct xe_vma, gpuva);
+
+		if (send_ops >= num_ops)
+			break;
+
+		err = vm_bind_op(d, vm, DRM_XE_EUDEBUG_EVENT_CREATE, ref_seqno,
+				 xe_vma_start(vma), xe_vma_size(vma),
+				 &vma->eudebug.metadata.list);
+		if (err)
+			return err;
+
+		send_ops++;
+	}
+
+	return num_ops == send_ops ? 0 : -EINVAL;
+}
+
 static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
 {
 	struct xe_debug_metadata *m;
@@ -3175,6 +3216,10 @@ static int discover_client(struct xe_eudebug *d, struct xe_file *xef)
 		err = vm_create_event(d, xef, vm);
 		if (err)
 			break;
+
+		err = vm_discover_binds(d, vm);
+		if (err)
+			break;
 	}
 
 	xa_for_each(&xef->exec_queue.xa, i, q) {
Signature · 1 line
2.34.1
#35 lore raw

PATCH 17/18 drm/xe/eudebug: Dynamically toggle debugger functionality

In reply to Mika Kuoppala

From: Christoph Manszewski <[email protected]>

Make it possible to dynamically enable/disable debugger funtionality, including the setting and unsetting of required hw register values via a sysfs entry located at '/sys/class/drm/card<X>/device/enable_eudebug'.

This entry uses 'kstrtobool' and as such it accepts inputs as documented by this function, in particular '0' and '1'.

v2: use new discovery_lock to gain exclusivity (Mika)

---
 drivers/gpu/drm/xe/xe_device.c       |   2 -
 drivers/gpu/drm/xe/xe_device_types.h |   3 +
 drivers/gpu/drm/xe/xe_eudebug.c      | 125 +++++++++++++++++++++++----
 drivers/gpu/drm/xe/xe_eudebug.h      |   2 -
 drivers/gpu/drm/xe/xe_exec_queue.c   |   5 ++
 drivers/gpu/drm/xe/xe_hw_engine.c    |   1 -
 drivers/gpu/drm/xe/xe_reg_sr.c       |  21 +++--
 drivers/gpu/drm/xe/xe_reg_sr.h       |   4 +-
 drivers/gpu/drm/xe/xe_rtp.c          |   2 +-
 9 files changed, 133 insertions(+), 32 deletions(-)
drivers/gpu/drm/xe/xe_device.c +0 -2
index e0d55e11ace6..0647c17a79dd 100644
@@ -781,8 +781,6 @@ int xe_device_probe(struct xe_device *xe)
 
 	xe_debugfs_register(xe);
 
-	xe_eudebug_init_late(xe);
-
 	xe_hwmon_register(xe);
 
 	for_each_gt(gt, xe, id)
drivers/gpu/drm/xe/xe_device_types.h +3 -0
index 40b0f61b64b8..fc448d44752e 100644
@@ -549,6 +549,9 @@ struct xe_device {
 		/** discovery_lock: used for discovery to block xe ioctls */
 		struct rw_semaphore discovery_lock;
 
+		/** @enable: is the debugging functionality enabled */
+		bool enable;
+
 		/** @attention_scan: attention scan worker */
 		struct delayed_work attention_scan;
 	} eudebug;
drivers/gpu/drm/xe/xe_eudebug.c +106 -19

195 lines not on this page · raw message

drivers/gpu/drm/xe/xe_eudebug.h +0 -2
index 766d0a94d200..403f52148da3 100644
@@ -26,9 +26,7 @@ int xe_eudebug_connect_ioctl(struct drm_device *dev,
 			     struct drm_file *file);
 
 void xe_eudebug_init(struct xe_device *xe);
-void xe_eudebug_init_late(struct xe_device *xe);
 void xe_eudebug_fini(struct xe_device *xe);
-void xe_eudebug_init_hw_engine(struct xe_hw_engine *hwe);
 
 void xe_eudebug_file_open(struct xe_file *xef);
 void xe_eudebug_file_close(struct xe_file *xef);
drivers/gpu/drm/xe/xe_exec_queue.c +5 -0
index c2d6993ac103..011d8bb477d4 100644
@@ -422,6 +422,11 @@ static int exec_queue_set_eudebug(struct xe_device *xe, struct xe_exec_queue *q,
 			 !(value & DRM_XE_EXEC_QUEUE_EUDEBUG_FLAG_ENABLE)))
 		return -EINVAL;
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+	if (XE_IOCTL_DBG(xe, !xe->eudebug.enable))
+		return -EPERM;
+#endif
+
 	q->eudebug_flags = EXEC_QUEUE_EUDEBUG_FLAG_ENABLE;
 	q->sched_props.preempt_timeout_us = 0;
 
drivers/gpu/drm/xe/xe_hw_engine.c +0 -1
index 8e9a08395042..28c8cabf508c 100644
@@ -558,7 +558,6 @@ static void hw_engine_init_early(struct xe_gt *gt, struct xe_hw_engine *hwe,
 	xe_tuning_process_engine(hwe);
 	xe_wa_process_engine(hwe);
 	hw_engine_setup_default_state(hwe);
-	xe_eudebug_init_hw_engine(hwe);
 
 	xe_reg_sr_init(&hwe->reg_whitelist, hwe->name, gt_to_xe(gt));
 	xe_reg_whitelist_process_engine(hwe);
drivers/gpu/drm/xe/xe_reg_sr.c +15 -6
index 191cb4121acd..023989adbe05 100644
@@ -93,22 +93,31 @@ static void reg_sr_inc_error(struct xe_reg_sr *sr)
 
 int xe_reg_sr_add(struct xe_reg_sr *sr,
 		  const struct xe_reg_sr_entry *e,
-		  struct xe_gt *gt)
+		  struct xe_gt *gt,
+		  bool overwrite)
 {
 	unsigned long idx = e->reg.addr;
 	struct xe_reg_sr_entry *pentry = xa_load(&sr->xa, idx);
 	int ret;
 
 	if (pentry) {
-		if (!compatible_entries(pentry, e)) {
+		if (overwrite && e->set_bits) {
+			pentry->clr_bits |= e->clr_bits;
+			pentry->set_bits |= e->set_bits;
+			pentry->read_mask |= e->read_mask;
+		} else if (overwrite && !e->set_bits) {
+			pentry->clr_bits |= e->clr_bits;
+			pentry->set_bits &= ~e->clr_bits;
+			pentry->read_mask |= e->read_mask;
+		} else if (!compatible_entries(pentry, e)) {
 			ret = -EINVAL;
 			goto fail;
+		} else {
+			pentry->clr_bits |= e->clr_bits;
+			pentry->set_bits |= e->set_bits;
+			pentry->read_mask |= e->read_mask;
 		}
 
-		pentry->clr_bits |= e->clr_bits;
-		pentry->set_bits |= e->set_bits;
-		pentry->read_mask |= e->read_mask;
-
 		return 0;
 	}
 
drivers/gpu/drm/xe/xe_reg_sr.h +3 -1
index 51fbba423e27..d67fafdcd847 100644
@@ -6,6 +6,8 @@
 #ifndef _XE_REG_SR_
 #define _XE_REG_SR_
 
+#include <linux/types.h>
+
 /*
  * Reg save/restore bookkeeping
  */
@@ -21,7 +23,7 @@ int xe_reg_sr_init(struct xe_reg_sr *sr, const char *name, struct xe_device *xe)
 void xe_reg_sr_dump(struct xe_reg_sr *sr, struct drm_printer *p);
 
 int xe_reg_sr_add(struct xe_reg_sr *sr, const struct xe_reg_sr_entry *e,
-		  struct xe_gt *gt);
+		  struct xe_gt *gt, bool overwrite);
 void xe_reg_sr_apply_mmio(struct xe_reg_sr *sr, struct xe_gt *gt);
 void xe_reg_sr_apply_whitelist(struct xe_hw_engine *hwe);
 
drivers/gpu/drm/xe/xe_rtp.c +1 -1
index b13d4d62f0b1..6006f7c90cac 100644
@@ -153,7 +153,7 @@ static void rtp_add_sr_entry(const struct xe_rtp_action *action,
 	};
 
 	sr_entry.reg.addr += mmio_base;
-	xe_reg_sr_add(sr, &sr_entry, gt);
+	xe_reg_sr_add(sr, &sr_entry, gt, false);
 }
 
 static bool rtp_process_one_sr(const struct xe_rtp_entry_sr *entry,
Signature · 1 line
2.34.1
#36 lore raw

PATCH 18/18 drm/xe/eudebug_test: Introduce xe_eudebug wa kunit test

In reply to Mika Kuoppala

From: Christoph Manszewski <[email protected]>

Introduce kunit test for eudebug. For now it checks the dynamic application of WAs.

v2: adapt to removal of call_for_each_device (Mika)

---
 drivers/gpu/drm/xe/tests/xe_eudebug.c       | 175 ++++++++++++++++++++
 drivers/gpu/drm/xe/tests/xe_live_test_mod.c |   5 +
 drivers/gpu/drm/xe/xe_eudebug.c             |   4 +
 3 files changed, 184 insertions(+)
 create mode 100644 drivers/gpu/drm/xe/tests/xe_eudebug.c
drivers/gpu/drm/xe/tests/xe_eudebug.c new file +175 -0
new file mode 100644
index 000000000000..3e78b6a9d056
@@ -0,0 +1,175 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <kunit/visibility.h>
+
+#include "tests/xe_kunit_helpers.h"
+#include "tests/xe_pci_test.h"
+#include "tests/xe_test.h"
+
+#undef XE_REG_MCR
+#define XE_REG_MCR(r_, ...)	((const struct xe_reg_mcr){					\
+				 .__reg = XE_REG_INITIALIZER(r_,  ##__VA_ARGS__, .mcr = 1)	\
+				 })
+
+static const char *reg_to_str(struct xe_reg reg)
+{
+	if (reg.raw == TD_CTL.__reg.raw)
+		return "TD_CTL";
+	else if (reg.raw == CS_DEBUG_MODE2(RENDER_RING_BASE).raw)
+		return "CS_DEBUG_MODE2";
+	else if (reg.raw == ROW_CHICKEN.__reg.raw)
+		return "ROW_CHICKEN";
+	else if (reg.raw == ROW_CHICKEN2.__reg.raw)
+		return "ROW_CHICKEN2";
+	else if (reg.raw == ROW_CHICKEN3.__reg.raw)
+		return "ROW_CHICKEN3";
+	else
+		return "UNKNOWN REG";
+}
+
+static u32 get_reg_mask(struct xe_device *xe, struct xe_reg reg)
+{
+	struct kunit *test = kunit_get_current_test();
+	u32 val = 0;
+
+	if (reg.raw == TD_CTL.__reg.raw) {
+		val = TD_CTL_BREAKPOINT_ENABLE |
+		      TD_CTL_FORCE_THREAD_BREAKPOINT_ENABLE |
+		      TD_CTL_FEH_AND_FEE_ENABLE;
+
+		if (GRAPHICS_VERx100(xe) >= 1250)
+			val |= TD_CTL_GLOBAL_DEBUG_ENABLE;
+
+	} else if (reg.raw == CS_DEBUG_MODE2(RENDER_RING_BASE).raw) {
+		val = GLOBAL_DEBUG_ENABLE;
+	} else if (reg.raw == ROW_CHICKEN.__reg.raw) {
+		val = STALL_DOP_GATING_DISABLE;
+	} else if (reg.raw == ROW_CHICKEN2.__reg.raw) {
+		val = XEHPC_DISABLE_BTB;
+	} else if (reg.raw == ROW_CHICKEN3.__reg.raw) {
+		val = XE2_EUPEND_CHK_FLUSH_DIS;
+	} else {
+		kunit_warn(test, "Invalid register selection: %u\n", reg.raw);
+	}
+
+	return val;
+}
+
+static u32 get_reg_expected(struct xe_device *xe, struct xe_reg reg, bool enable_eudebug)
+{
+	u32 reg_mask = get_reg_mask(xe, reg);
+	u32 reg_bits = 0;
+
+	if (enable_eudebug || reg.raw == ROW_CHICKEN3.__reg.raw)
+		reg_bits = reg_mask;
+	else
+		reg_bits = 0;
+
+	return reg_bits;
+}
+
+static void check_reg(struct xe_gt *gt, bool enable_eudebug, struct xe_reg reg)
+{
+	struct kunit *test = kunit_get_current_test();
+	struct xe_device *xe = gt_to_xe(gt);
+	u32 reg_bits_expected = get_reg_expected(xe, reg, enable_eudebug);
+	u32 reg_mask = get_reg_mask(xe, reg);
+	u32 reg_bits = 0;
+
+	if (reg.mcr)
+		reg_bits = xe_gt_mcr_unicast_read_any(gt, (struct xe_reg_mcr){.__reg = reg});
+	else
+		reg_bits = xe_mmio_read32(&gt->mmio, reg);
+
+	reg_bits &= reg_mask;
+
+	kunit_printk(KERN_DEBUG, test, "%s bits: expected == 0x%x; actual == 0x%x\n",
+		     reg_to_str(reg), reg_bits_expected, reg_bits);
+	KUNIT_EXPECT_EQ_MSG(test, reg_bits_expected, reg_bits,
+			    "Invalid bits set for %s\n", reg_to_str(reg));
+}
+
+static void __check_regs(struct xe_gt *gt, bool enable_eudebug)
+{
+	struct xe_device *xe = gt_to_xe(gt);
+
+	if (GRAPHICS_VERx100(xe) >= 1200)
+		check_reg(gt, enable_eudebug, TD_CTL.__reg);
+
+	if (GRAPHICS_VERx100(xe) >= 1250 && GRAPHICS_VERx100(xe) <= 1274)
+		check_reg(gt, enable_eudebug, ROW_CHICKEN.__reg);
+
+	if (xe->info.platform == XE_PVC)
+		check_reg(gt, enable_eudebug, ROW_CHICKEN2.__reg);
+
+	if (GRAPHICS_VERx100(xe) >= 2000 && GRAPHICS_VERx100(xe) <= 2004)
+		check_reg(gt, enable_eudebug, ROW_CHICKEN3.__reg);
+}
+
+static void check_regs(struct xe_device *xe, bool enable_eudebug)
+{
+	struct kunit *test = kunit_get_current_test();
+	struct xe_gt *gt;
+	u8 id;
+	int ret = 0;
+
+	kunit_printk(KERN_DEBUG, test, "Check regs for eudebug %s\n",
+		     enable_eudebug ? "enabled" : "disabled");
+
+	xe_pm_runtime_get(xe);
+	for_each_gt(gt, xe, id) {
+		if (xe_gt_is_media_type(gt))
+			continue;
+
+		ret = xe_force_wake_get(gt_to_fw(gt), XE_FW_RENDER);
+		KUNIT_ASSERT_EQ_MSG(test, ret, 0, "Forcewake failed.\n");
+
+		__check_regs(gt, enable_eudebug);
+
+		xe_force_wake_put(gt_to_fw(gt), XE_FW_RENDER);
+	}
+	xe_pm_runtime_put(xe);
+}
+
+static int toggle_reg_value(struct xe_device *xe)
+{
+	struct kunit *test = kunit_get_current_test();
+	bool enable_eudebug = xe->eudebug.enable;
+
+	kunit_printk(KERN_DEBUG, test, "Test eudebug WAs for graphics version: %u\n",
+		     GRAPHICS_VERx100(xe));
+
+	check_regs(xe, enable_eudebug);
+
+	xe_eudebug_enable(xe, !enable_eudebug);
+	check_regs(xe, !enable_eudebug);
+
+	xe_eudebug_enable(xe, enable_eudebug);
+	check_regs(xe, enable_eudebug);
+
+	return 0;
+}
+
+static void xe_eudebug_toggle_reg_kunit(struct kunit *test)
+{
+	struct xe_device *xe = test->priv;
+
+	toggle_reg_value(xe);
+}
+
+static struct kunit_case xe_eudebug_tests[] = {
+	KUNIT_CASE_PARAM(xe_eudebug_toggle_reg_kunit,
+			 xe_pci_live_device_gen_param),
+	{}
+};
+
+VISIBLE_IF_KUNIT
+struct kunit_suite xe_eudebug_test_suite = {
+	.name = "xe_eudebug",
+	.test_cases = xe_eudebug_tests,
+	.init = xe_kunit_helper_xe_device_live_test_init,
+};
+EXPORT_SYMBOL_IF_KUNIT(xe_eudebug_test_suite);
drivers/gpu/drm/xe/tests/xe_live_test_mod.c +5 -0
index 5f14737c8210..7dd8a0a4bdfd 100644
@@ -15,6 +15,11 @@ kunit_test_suite(xe_dma_buf_test_suite);
 kunit_test_suite(xe_migrate_test_suite);
 kunit_test_suite(xe_mocs_test_suite);
 
+#if IS_ENABLED(CONFIG_DRM_XE_EUDEBUG)
+extern struct kunit_suite xe_eudebug_test_suite;
+kunit_test_suite(xe_eudebug_test_suite);
+#endif
+
 MODULE_AUTHOR("Intel Corporation");
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("xe live kunit tests");
drivers/gpu/drm/xe/xe_eudebug.c +4 -0
index 6e26245556d4..4d81955d0169 100644
@@ -3885,3 +3885,7 @@ xe_eudebug_vm_open_ioctl(struct xe_eudebug *d, unsigned long arg)
 
 	return ret;
 }
+
+#if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
+#include "tests/xe_eudebug.c"
+#endif
Signature · 1 line
2.34.1
#37 lore raw

✓ CI.Patch_applied: success for GPU debug support (eudebug) (rev2)

In reply to Mika Kuoppala
== Series Details ==

== Series Details ==

Series: GPU debug support (eudebug) (rev2)
URL : https://patchwork.freedesktop.org/series/136572/
State : success

== Summary ==

=== Applying kernel patches on branch 'drm-tip' with base: ===
Base commit: f3f61bfc1cdd drm-tip: 2024y-10m-01d-11h-59m-39s UTC integration manifest === git am output follows ===
Applying: ptrace: export ptrace_may_access
Applying: drm/xe/eudebug: Introduce eudebug support
Applying: drm/xe/eudebug: Introduce discovery for resources
Applying: drm/xe/eudebug: Introduce exec_queue events
Applying: drm/xe/eudebug: hw enablement for eudebug
Applying: drm/xe: Add EUDEBUG_ENABLE exec queue property
Applying: drm/xe/eudebug: Introduce per device attention scan worker
Applying: drm/xe/eudebug: Introduce EU control interface
Applying: drm/xe/eudebug: Add vm bind and vm bind ops
Applying: drm/xe/eudebug: Add UFENCE events with acks
Applying: drm/xe/eudebug: vm open/pread/pwrite
Applying: drm/xe/eudebug: implement userptr_vma access
Applying: drm/xe: Debug metadata create/destroy ioctls
Applying: drm/xe: Attach debug metadata to vma
Applying: drm/xe/eudebug: Add debug metadata support for xe_eudebug
Applying: drm/xe/eudebug: Implement vm_bind_op discovery
Applying: drm/xe/eudebug: Dynamically toggle debugger functionality
Applying: drm/xe/eudebug_test: Introduce xe_eudebug wa kunit test

#38 lore raw

✗ CI.checkpatch: warning for GPU debug support (eudebug) (rev2)

In reply to Mika Kuoppala
== Series Details ==

== Series Details ==

Series: GPU debug support (eudebug) (rev2)
URL : https://patchwork.freedesktop.org/series/136572/
State : warning

== Summary ==

+ KERNEL=/kernel
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools mt Cloning into 'mt'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ git -C mt rev-list -n1 origin/master 30ab6715fc09baee6cc14cb3c89ad8858688d474
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ git log -n1
commit fefc940eb695b6ace9e906fbe1869fd9b7f658dd

Author: Christoph Manszewski <[email protected]>
Date:   Tue Oct 1 17:43:06 2024 +0300

drm/xe/eudebug_test: Introduce xe_eudebug wa kunit test

Introduce kunit test for eudebug. For now it checks the dynamic application of WAs.

v2: adapt to removal of call_for_each_device (Mika)

+ /mt/dim checkpatch f3f61bfc1cddded910b3916aa2c576a14bf62f71 drm-intel cfddad185c79 ptrace: export ptrace_may_access
6de3ddf0c385 drm/xe/eudebug: Introduce eudebug support
-:202: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #202:
new file mode 100644

-:240: ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parentheses #240: FILE: drivers/gpu/drm/xe/xe_eudebug.c:34:
+#define XE_EUDEBUG_DBG_ARGS(d) (d)->session, \
+ atomic_long_read(&(d)->events.seqno), \
+ READ_ONCE(d->connection.status) <= 0 ? "disconnected" : "", \
+ current->pid, \
+ task_tgid_nr(current), \
+ (d)->target_task->pid, \
+ task_tgid_nr((d)->target_task)

-:240: CHECK:MACRO_ARG_REUSE: Macro argument reuse 'd' - possible side-effects? #240: FILE: drivers/gpu/drm/xe/xe_eudebug.c:34:
+#define XE_EUDEBUG_DBG_ARGS(d) (d)->session, \
+ atomic_long_read(&(d)->events.seqno), \
+ READ_ONCE(d->connection.status) <= 0 ? "disconnected" : "", \
+ current->pid, \
+ task_tgid_nr(current), \
+ (d)->target_task->pid, \
+ task_tgid_nr((d)->target_task)

-:248: CHECK:MACRO_ARG_REUSE: Macro argument reuse 'd' - possible side-effects? #248: FILE: drivers/gpu/drm/xe/xe_eudebug.c:42:
+#define eu_err(d, fmt, ...) drm_err(&(d)->xe->drm, XE_EUDEBUG_DBG_STR # fmt, \
+ XE_EUDEBUG_DBG_ARGS(d), ##__VA_ARGS__)

-:250: CHECK:MACRO_ARG_REUSE: Macro argument reuse 'd' - possible side-effects? #250: FILE: drivers/gpu/drm/xe/xe_eudebug.c:44:
+#define eu_warn(d, fmt, ...) drm_warn(&(d)->xe->drm, XE_EUDEBUG_DBG_STR # fmt, \
+ XE_EUDEBUG_DBG_ARGS(d), ##__VA_ARGS__)

-:252: CHECK:MACRO_ARG_REUSE: Macro argument reuse 'd' - possible side-effects? #252: FILE: drivers/gpu/drm/xe/xe_eudebug.c:46:
+#define eu_dbg(d, fmt, ...) drm_dbg(&(d)->xe->drm, XE_EUDEBUG_DBG_STR # fmt, \
+ XE_EUDEBUG_DBG_ARGS(d), ##__VA_ARGS__)

-:257: CHECK:MACRO_ARG_PRECEDENCE: Macro argument 'T' may be better as '(T)' to avoid precedence issues #257: FILE: drivers/gpu/drm/xe/xe_eudebug.c:51:
+#define struct_member(T, member) (((T *)0)->member)

-:257: CHECK:MACRO_ARG_PRECEDENCE: Macro argument 'member' may be better as '(member)' to avoid precedence issues #257: FILE: drivers/gpu/drm/xe/xe_eudebug.c:51:
+#define struct_member(T, member) (((T *)0)->member)

-:260: CHECK:MACRO_ARG_REUSE: Macro argument reuse 'ptr' - possible side-effects? #260: FILE: drivers/gpu/drm/xe/xe_eudebug.c:54:
+#define write_member(T_out, ptr, member, value) { \
+ BUILD_BUG_ON(sizeof(*ptr) != sizeof(T_out)); \
+ BUILD_BUG_ON(offsetof(typeof(*ptr), member) != \
+ offsetof(typeof(T_out), member)); \
+ BUILD_BUG_ON(sizeof(ptr->member) != sizeof(value)); \
+ BUILD_BUG_ON(sizeof(struct_member(T_out, member)) != sizeof(value)); \
+ BUILD_BUG_ON(!typecheck(typeof((ptr)->member), value)); \
+ (ptr)->member = (value); \
+ }

-:260: CHECK:MACRO_ARG_PRECEDENCE: Macro argument 'ptr' may be better as '(ptr)' to avoid precedence issues #260: FILE: drivers/gpu/drm/xe/xe_eudebug.c:54:
+#define write_member(T_out, ptr, member, value) { \
+ BUILD_BUG_ON(sizeof(*ptr) != sizeof(T_out)); \
+ BUILD_BUG_ON(offsetof(typeof(*ptr), member) != \
+ offsetof(typeof(T_out), member)); \
+ BUILD_BUG_ON(sizeof(ptr->member) != sizeof(value)); \
+ BUILD_BUG_ON(sizeof(struct_member(T_out, member)) != sizeof(value)); \
+ BUILD_BUG_ON(!typecheck(typeof((ptr)->member), value)); \
+ (ptr)->member = (value); \
+ }

-:260: CHECK:MACRO_ARG_REUSE: Macro argument reuse 'member' - possible side-effects? #260: FILE: drivers/gpu/drm/xe/xe_eudebug.c:54:
+#define write_member(T_out, ptr, member, value) { \
+ BUILD_BUG_ON(sizeof(*ptr) != sizeof(T_out)); \
+ BUILD_BUG_ON(offsetof(typeof(*ptr), member) != \
+ offsetof(typeof(T_out), member)); \
+ BUILD_BUG_ON(sizeof(ptr->member) != sizeof(value)); \
+ BUILD_BUG_ON(sizeof(struct_member(T_out, member)) != sizeof(value)); \
+ BUILD_BUG_ON(!typecheck(typeof((ptr)->member), value)); \
+ (ptr)->member = (value); \
+ }

-:260: CHECK:MACRO_ARG_REUSE: Macro argument reuse 'value' - possible side-effects? #260: FILE: drivers/gpu/drm/xe/xe_eudebug.c:54:
+#define write_member(T_out, ptr, member, value) { \
+ BUILD_BUG_ON(sizeof(*ptr) != sizeof(T_out)); \
+ BUILD_BUG_ON(offsetof(typeof(*ptr), member) != \
+ offsetof(typeof(T_out), member)); \
+ BUILD_BUG_ON(sizeof(ptr->member) != sizeof(value)); \
+ BUILD_BUG_ON(sizeof(struct_member(T_out, member)) != sizeof(value)); \
+ BUILD_BUG_ON(!typecheck(typeof((ptr)->member), value)); \
+ (ptr)->member = (value); \
+ }

-:532: CHECK:MACRO_ARG_REUSE: Macro argument reuse '_err' - possible side-effects? #532: FILE: drivers/gpu/drm/xe/xe_eudebug.c:326:
+#define xe_eudebug_disconnect(_d, _err) ({ \
+ if (_xe_eudebug_disconnect((_d), (_err))) { \
+ if ((_err) == 0 || (_err) == -ETIMEDOUT) \
+ eu_dbg(d, "Session closed (%d)", (_err)); \
+ else \
+ eu_err(d, "Session disconnected, err = %d (%s:%d)", \
+ (_err), __func__, __LINE__); \
+ } \
+})

-:1188: CHECK:MACRO_ARG_REUSE: Macro argument reuse '_d' - possible side-effects? #1188: FILE: drivers/gpu/drm/xe/xe_eudebug.c:982:
+#define xe_eudebug_event_put(_d, _err) ({ \
+ if ((_err)) \
+ xe_eudebug_disconnect((_d), (_err)); \
+ xe_eudebug_put((_d)); \
+ })

-:1188: CHECK:MACRO_ARG_REUSE: Macro argument reuse '_err' - possible side-effects? #1188: FILE: drivers/gpu/drm/xe/xe_eudebug.c:982:
+#define xe_eudebug_event_put(_d, _err) ({ \
+ if ((_err)) \
+ xe_eudebug_disconnect((_d), (_err)); \
+ xe_eudebug_put((_d)); \
+ })

-:1597: WARNING:LONG_LINE: line length of 130 exceeds 100 columns #1597: FILE: include/uapi/drm/xe_drm.h:121:
+#define DRM_IOCTL_XE_EUDEBUG_CONNECT DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_EUDEBUG_CONNECT, struct drm_xe_eudebug_connect)

total: 1 errors, 2 warnings, 13 checks, 1568 lines checked
c85f0d674e4c drm/xe/eudebug: Introduce discovery for resources
-:94: CHECK:LINE_SPACING: Please use a blank line after function/struct/union/enum declarations #94: FILE: drivers/gpu/drm/xe/xe_device.h:218:
+}
+static inline void xe_eudebug_discovery_unlock(struct xe_device *xe, unsigned int cmd)

total: 0 errors, 0 warnings, 1 checks, 318 lines checked 31f53dce7293 drm/xe/eudebug: Introduce exec_queue events 3524200dba1a drm/xe/eudebug: hw enablement for eudebug 5606c41b22a2 drm/xe: Add EUDEBUG_ENABLE exec queue property eab05032a20b drm/xe/eudebug: Introduce per device attention scan worker -:390: CHECK:LINE_SPACING: Please don't use multiple blank lines #390: FILE: drivers/gpu/drm/xe/xe_eudebug.c:1127:
+
+

-:610: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #610:
new file mode 100644

total: 0 errors, 1 warnings, 1 checks, 735 lines checked
4b37794314ac drm/xe/eudebug: Introduce EU control interface
4aaf27fb08d6 drm/xe/eudebug: Add vm bind and vm bind ops
904c12faa332 drm/xe/eudebug: Add UFENCE events with acks
-:415: WARNING:LONG_LINE: line length of 114 exceeds 100 columns
#415: FILE: drivers/gpu/drm/xe/xe_eudebug.h:78:
+static inline void xe_eudebug_ufence_init(struct xe_user_fence *ufence, struct xe_file *xef, struct xe_vm *vm) { }

-:642: CHECK:UNCOMMENTED_DEFINITION: spinlock_t definition without comment #642: FILE: drivers/gpu/drm/xe/xe_sync_types.h:26:
+ spinlock_t lock;

total: 0 errors, 1 warnings, 1 checks, 615 lines checked d24f5ef48159 drm/xe/eudebug: vm open/pread/pwrite
-:116: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #116: FILE: drivers/gpu/drm/xe/xe_eudebug.c:2960:
+static int xe_eudebug_bovma_access(struct xe_bo *bo, u64 offset,
+ void *buf, u64 len, bool write)

total: 0 errors, 0 warnings, 1 checks, 570 lines checked
b6bda0e13538 drm/xe/eudebug: implement userptr_vma access
d85eae9bd2b1 drm/xe: Debug metadata create/destroy ioctls
-:34: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #34:
new file mode 100644

-:201: CHECK:LINE_SPACING: Please don't use multiple blank lines #201: FILE: drivers/gpu/drm/xe/xe_debug_metadata.h:49:
+
+

-:353: WARNING:LONG_LINE: line length of 143 exceeds 100 columns #353: FILE: include/uapi/drm/xe_drm.h:123:
+#define DRM_IOCTL_XE_DEBUG_METADATA_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_XE_DEBUG_METADATA_CREATE, struct drm_xe_debug_metadata_create)

-:354: WARNING:LONG_LINE: line length of 144 exceeds 100 columns #354: FILE: include/uapi/drm/xe_drm.h:124:
+#define DRM_IOCTL_XE_DEBUG_METADATA_DESTROY DRM_IOW(DRM_COMMAND_BASE + DRM_XE_DEBUG_METADATA_DESTROY, struct drm_xe_debug_metadata_destroy)

total: 0 errors, 3 warnings, 1 checks, 341 lines checked
1e7e3d39885d drm/xe: Attach debug metadata to vma
-:6: WARNING:COMMIT_LOG_LONG_LINE: Prefer a maximum 75 chars per line (possible unwrapped commit description?) #6:
Introduces a vm_bind_op extension, enabling users to attach metadata objects

-:207: WARNING:LONG_LINE: line length of 107 exceeds 100 columns
#207: FILE: drivers/gpu/drm/xe/xe_debug_metadata.h:55:
+static inline struct xe_debug_metadata *xe_debug_metadata_get(struct xe_file *xef, u32 id) { return NULL; }

-:312: WARNING:LONG_LINE: line length of 109 exceeds 100 columns #312: FILE: drivers/gpu/drm/xe/xe_vm.c:2259:
+ err = xe_eudebug_copy_vma_metadata(&op->remap.prev->eudebug.metadata,

total: 0 errors, 3 warnings, 0 checks, 482 lines checked
8765e631476b drm/xe/eudebug: Add debug metadata support for xe_eudebug
-:527: WARNING:LONG_LINE: line length of 122 exceeds 100 columns
#527: FILE: drivers/gpu/drm/xe/xe_eudebug.h:80:
+static inline void xe_eudebug_vm_bind_op_add(struct xe_vm *vm, u32 op, u64 addr, u64 range, struct drm_gpuva_ops *ops) { }

-:535: WARNING:LONG_LINE: line length of 105 exceeds 100 columns
#535: FILE: drivers/gpu/drm/xe/xe_eudebug.h:90:
+static inline void xe_eudebug_debug_metadata_create(struct xe_file *xef, struct xe_debug_metadata *m) { }

-:536: WARNING:LONG_LINE: line length of 106 exceeds 100 columns
#536: FILE: drivers/gpu/drm/xe/xe_eudebug.h:91:
+static inline void xe_eudebug_debug_metadata_destroy(struct xe_file *xef, struct xe_debug_metadata *m) { }

total: 0 errors, 3 warnings, 0 checks, 580 lines checked
6de2c5528aa1 drm/xe/eudebug: Implement vm_bind_op discovery
a3740d54ebdd drm/xe/eudebug: Dynamically toggle debugger functionality fefc940eb695 drm/xe/eudebug_test: Introduce xe_eudebug wa kunit test
-:15: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #15:
new file mode 100644

total: 0 errors, 1 warnings, 0 checks, 193 lines checked

#39 lore raw

✗ CI.KUnit: failure for GPU debug support (eudebug) (rev2)

In reply to Mika Kuoppala
== Series Details ==

== Series Details ==

Series: GPU debug support (eudebug) (rev2)
URL : https://patchwork.freedesktop.org/series/136572/
State : failure

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig ERROR:root:../lib/iomap.c:156:5: warning: no previous prototype for ‘ioread64_lo_hi’ [-Wmissing-prototypes] 156 | u64 ioread64_lo_hi(const void __iomem *addr)

      |     ^~~~~~~~~~~~~~
../lib/iomap.c:163:5: warning: no previous prototype for ‘ioread64_hi_lo’ [-Wmissing-prototypes]
  163 | u64 ioread64_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~
../lib/iomap.c:170:5: warning: no previous prototype for ‘ioread64be_lo_hi’ [-Wmissing-prototypes]
  170 | u64 ioread64be_lo_hi(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:178:5: warning: no previous prototype for ‘ioread64be_hi_lo’ [-Wmissing-prototypes]
  178 | u64 ioread64be_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:264:6: warning: no previous prototype for ‘iowrite64_lo_hi’ [-Wmissing-prototypes]
  264 | void iowrite64_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:272:6: warning: no previous prototype for ‘iowrite64_hi_lo’ [-Wmissing-prototypes]
  272 | void iowrite64_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:280:6: warning: no previous prototype for ‘iowrite64be_lo_hi’ [-Wmissing-prototypes]
  280 | void iowrite64be_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~
../lib/iomap.c:288:6: warning: no previous prototype for ‘iowrite64be_hi_lo’ [-Wmissing-prototypes]
  288 | void iowrite64be_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_eudebug.c:1357:27: error: ‘no_llseek’ undeclared here (not in a function); did you mean ‘noop_llseek’?
 1357 |         .llseek         = no_llseek,
      |                           ^~~~~~~~~
      |                           noop_llseek

make[7]: *** [../scripts/Makefile.build:229: drivers/gpu/drm/xe/xe_eudebug.o] Error 1 make[7]: *** Waiting for unfinished jobs....
make[6]: *** [../scripts/Makefile.build:478: drivers/gpu/drm/xe] Error 2 make[5]: *** [../scripts/Makefile.build:478: drivers/gpu/drm] Error 2 make[4]: *** [../scripts/Makefile.build:478: drivers/gpu] Error 2 make[3]: *** [../scripts/Makefile.build:478: drivers] Error 2
make[2]: *** [/kernel/Makefile:1936: .] Error 2
make[1]: *** [/kernel/Makefile:224: __sub-make] Error 2
make: *** [Makefile:224: __sub-make] Error 2

[15:03:19] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[15:03:24] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json ARCH=um O=.kunit --jobs=48
+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel