Skip to contentSkip to conversation

FD close cleanup for XE

PATCH Superseded Daniel Charles 1 participant 5 messages Nov 19

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover FD close cleanup for XE

On FD close, serialized TDR cleanup can lag behind VM teardown. GuC may continue scheduling contexts while their page tables are being torn down, leading to CAT errors and repeated engine resets.

This series keeps the early scheduling disable outside the serialized TDR path and makes FD close wait before releasing queue references. FD close first kills all queues, then waits for each queue before dropping its reference.

Changes since v4:
- Patch 1 is unchanged.
- Patch 2 adds per-queue scheduling-state waitqueues as a standalone prerequisite. SCHED_DONE wakes waiters on the affected queue after updating its state, without waking unrelated CT waiters.
- Patch 3 restricts early group DISABLE to the primary's own KILL. It also allows pending disable on a killed queue in run_job and avoids issuing a second DISABLE from TDR if KILL already disabled scheduling.
- Patch 4 requires a KILL acknowledgement before accepting a disabled scheduling state, so FD close cannot finish the wait before KILL runs. Multi-queue members wait for their primary's acknowledgement and scheduling state on its per-queue waitqueue, replacing the shared kill_wq from v4.

v4: https://patchwork.freedesktop.org/series/175996/#rev4

Daniel Charles (2):
drm/xe: Notify scheduling-state waiters per exec queue drm/xe: Synchronously wait for queue kill on FD close

Matthew Brost (2):
drm/xe: Read scheduler message opcode under lock drm/xe: Disable scheduling early on FD close to avoid CAT error
cascade

 drivers/gpu/drm/xe/xe_device.c               |   5 +
 drivers/gpu/drm/xe/xe_exec_queue.c           |  13 ++
 drivers/gpu/drm/xe/xe_exec_queue.h           |   1 +
 drivers/gpu/drm/xe/xe_exec_queue_types.h     |   2 +
 drivers/gpu/drm/xe/xe_gpu_scheduler.c        |  13 +-
 drivers/gpu/drm/xe/xe_gpu_scheduler_types.h  |   2 +-
 drivers/gpu/drm/xe/xe_gt_sriov_vf.c          |   1 +
 drivers/gpu/drm/xe/xe_guc_exec_queue_types.h |  10 +-
 drivers/gpu/drm/xe/xe_guc_submit.c           | 180 +++++++++++++++++--
 drivers/gpu/drm/xe/xe_guc_submit.h           |   1 +
 10 files changed, 206 insertions(+), 22 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/4 drm/xe: Read scheduler message opcode under lock

From: Matthew Brost <[email protected]>

For static scheduler messages, the opcode is only guaranteed to remain stable while the message is on the pending message list. Read the opcode under the message lock before removing the message from the pending list, and pass it through the processing pipeline.

Fixes ff796870f5dd ("drm/xe/guc: rework exec queue teardown PM/unplug handling")
---
 drivers/gpu/drm/xe/xe_gpu_scheduler.c       | 13 +++++++++----
 drivers/gpu/drm/xe/xe_gpu_scheduler_types.h |  2 +-
 drivers/gpu/drm/xe/xe_guc_submit.c          |  7 ++++---
 3 files changed, 14 insertions(+), 8 deletions(-)
drivers/gpu/drm/xe/xe_gpu_scheduler.c +9 -4
index 9c8004d5dd915..d656db758cb3c 100644
@@ -23,15 +23,19 @@ static void xe_sched_process_msg_queue_if_ready(struct xe_gpu_scheduler *sched)
 }
 
 static struct xe_sched_msg *
-xe_sched_get_msg(struct xe_gpu_scheduler *sched)
+xe_sched_get_msg(struct xe_gpu_scheduler *sched, unsigned int *opcode)
 {
 	struct xe_sched_msg *msg;
 
 	xe_sched_msg_lock(sched);
 	msg = list_first_entry_or_null(&sched->msgs,
 				       struct xe_sched_msg, link);
-	if (msg)
+	if (msg) {
+		/* The opcode is only stable under lock for static messages */
+		*opcode = msg->opcode;
+
 		list_del_init(&msg->link);
+	}
 	xe_sched_msg_unlock(sched);
 
 	return msg;
@@ -42,13 +46,14 @@ static void xe_sched_process_msg_work(struct work_struct *w)
 	struct xe_gpu_scheduler *sched =
 		container_of(w, struct xe_gpu_scheduler, work_process_msg);
 	struct xe_sched_msg *msg;
+	unsigned int opcode;
 
 	if (drm_sched_is_stopped(&sched->base))
 		return;
 
-	msg = xe_sched_get_msg(sched);
+	msg = xe_sched_get_msg(sched, &opcode);
 	if (msg) {
-		sched->ops->process_msg(msg);
+		sched->ops->process_msg(msg, opcode);
 
 		xe_sched_process_msg_queue_if_ready(sched);
 	}
drivers/gpu/drm/xe/xe_gpu_scheduler_types.h +1 -1
index 78ef2e8ded4f3..163a0d85e3cb9 100644
@@ -34,7 +34,7 @@ struct xe_sched_backend_ops {
 	 * @process_msg: Process a message. Allowed to block, it is this
 	 * function's responsibility to free message if dynamically allocated.
 	 */
-	void (*process_msg)(struct xe_sched_msg *msg);
+	void (*process_msg)(struct xe_sched_msg *msg, unsigned int opcode);
 };
 
 /**
drivers/gpu/drm/xe/xe_guc_submit.c +4 -3
index 7ce73489c35fc..c4ef3e9bf7a18 100644
@@ -2056,16 +2056,17 @@ static void __guc_exec_queue_process_msg_cgp_sync(struct xe_sched_msg *msg,
 #define MSG_HEAD	BIT(9)
 #define MSG_PM_REF	BIT(10)
 
-static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
+static void guc_exec_queue_process_msg(struct xe_sched_msg *msg,
+				       unsigned int opcode)
 {
 	struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data));
 	int idx;
-	bool pm_ref = !!(msg->opcode & MSG_PM_REF);
+	bool pm_ref = !!(opcode & MSG_PM_REF);
 	bool bound = drm_dev_enter(&xe->drm, &idx);
 
 	trace_xe_sched_msg_recv(msg);
 
-	switch (msg->opcode & OPCODE_MASK) {
+	switch (opcode & OPCODE_MASK) {
 	case CLEANUP:
 		__guc_exec_queue_process_msg_cleanup(msg, bound);
 		break;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v5 2/4 drm/xe: Notify scheduling-state waiters per exec queue

In reply to Daniel Charles

Wait for GuC scheduling state changes on the exec queue whose state is being inspected. Wake its waiters after processing SCHED_DONE instead of relying on a CT interrupt that can wake them before state changes. Keep the existing CT notifications for their remaining users.

On submission stop, wedge and VF recovery, notify published queues. Hold the lookup XArray lock with interrupts saved to prevent queue removal during the walk, and skip parallel queue aliases.

v5: New prerequisite for early KILL and FD-close waits.

---
 drivers/gpu/drm/xe/xe_gt_sriov_vf.c          |  1 +
 drivers/gpu/drm/xe/xe_guc_exec_queue_types.h |  2 +
 drivers/gpu/drm/xe/xe_guc_submit.c           | 39 +++++++++++++++++---
 drivers/gpu/drm/xe/xe_guc_submit.h           |  1 +
 4 files changed, 38 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_gt_sriov_vf.c +1 -0
index 37899fcf5b227..fc853c195fdb8 100644
@@ -910,6 +910,7 @@ static void vf_start_migration_recovery(struct xe_gt *gt)
 		smp_wmb();	/* Ensure above writes visible before wake */
 
 		xe_guc_ct_wake_waiters(&gt->uc.guc.ct);
+		xe_guc_submit_wake_state_waiters(&gt->uc.guc);
 
 		started = queue_work(gt->ordered_wq, &gt->sriov.vf.migration.worker);
 		xe_gt_sriov_info(gt, "VF migration recovery %s\n", started ?
drivers/gpu/drm/xe/xe_guc_exec_queue_types.h +2 -0
index 74b711abe2572..2b1a40e288f17 100644
@@ -52,6 +52,8 @@ struct xe_guc_exec_queue {
 	u16 id;
 	/** @suspend_wait: wait queue used to wait on pending suspends */
 	wait_queue_head_t suspend_wait;
+	/** @state_wq: wait queue for scheduling enable/disable state changes */
+	wait_queue_head_t state_wq;
 	/**
 	 * @suspend_pending: a suspend of the exec_queue is pending.
 	 * Protected by @sched.msg_lock.
drivers/gpu/drm/xe/xe_guc_submit.c +34 -5
index c4ef3e9bf7a18..a3c2abb9254ff 100644
@@ -551,6 +551,30 @@ static bool vf_recovery(struct xe_guc *guc)
 	return xe_gt_recovery_pending(guc_to_gt(guc));
 }
 
+/**
+ * xe_guc_submit_wake_state_waiters() - Wake all scheduling-state waiters
+ * @guc: the &xe_guc
+ *
+ * Notify per-queue waiters when a GuC-wide stop or recovery changes their
+ * wait conditions. May be called from interrupt context.
+ */
+void xe_guc_submit_wake_state_waiters(struct xe_guc *guc)
+{
+	struct xarray *lookup = &guc->submission_state.exec_queue_lookup;
+	struct xe_exec_queue *q;
+	unsigned long index, flags;
+
+	if (!guc->submission_state.initialized)
+		return;
+
+	/* Prevent removal and freeing of queues while waking their waiters. */
+	xa_lock_irqsave(lookup, flags);
+	xa_for_each(lookup, index, q)
+		if (q->guc->id == index)
+			wake_up_all(&q->guc->state_wq);
+	xa_unlock_irqrestore(lookup, flags);
+}
+
 static void xe_guc_exec_queue_trigger_cleanup(struct xe_exec_queue *q)
 {
 	struct xe_guc *guc = exec_queue_to_guc(q);
@@ -1372,7 +1396,7 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
 		set_min_preemption_timeout(guc, q);
 
 	smp_rmb();
-	ret = wait_event_timeout(guc->ct.wq,
+	ret = wait_event_timeout(q->guc->state_wq,
 				 (!exec_queue_pending_enable(q) &&
 				  !exec_queue_pending_disable(q)) ||
 					 xe_guc_read_stopped(guc) ||
@@ -1433,6 +1457,7 @@ void xe_guc_submit_wedge(struct xe_guc *guc)
 			if (xe_exec_queue_get_unless_zero(q))
 				set_exec_queue_wedged(q);
 		mutex_unlock(&guc->submission_state.lock);
+		xe_guc_submit_wake_state_waiters(guc);
 	} else {
 		/* Forcefully kill any remaining exec queues, signal fences */
 		guc_submit_reset_prepare(guc);
@@ -1537,7 +1562,7 @@ static void enable_scheduling(struct xe_exec_queue *q)
 		xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
 			       G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
 
-	ret = wait_event_timeout(guc->ct.wq,
+	ret = wait_event_timeout(q->guc->state_wq,
 				 !exec_queue_pending_enable(q) ||
 				 xe_guc_read_stopped(guc) ||
 				 vf_recovery(guc), HZ * 5);
@@ -1683,7 +1708,7 @@ guc_exec_queue_timedout_job(struct drm_sched_job *drm_job)
 			 * Wait for any pending G2H to flush out before
 			 * modifying state
 			 */
-			ret = wait_event_timeout(guc->ct.wq,
+			ret = wait_event_timeout(primary->guc->state_wq,
 						 (!exec_queue_pending_enable(primary) &&
 						  !exec_queue_pending_disable(primary)) ||
 						 xe_guc_read_stopped(guc) ||
@@ -1705,7 +1730,7 @@ guc_exec_queue_timedout_job(struct drm_sched_job *drm_job)
 		 * lost. If this occurs, trigger a GT reset to recover.
 		 */
 		smp_rmb();
-		ret = wait_event_timeout(guc->ct.wq,
+		ret = wait_event_timeout(primary->guc->state_wq,
 					 !xe_uc_fw_is_running(&guc->fw) ||
 					 !exec_queue_pending_disable(primary) ||
 					 xe_guc_read_stopped(guc) ||
@@ -1980,7 +2005,7 @@ static void __guc_exec_queue_process_msg_suspend(struct xe_sched_msg *msg,
 
 	if (guc_exec_queue_allowed_to_change_state(q) && !exec_queue_suspended(q) &&
 	    exec_queue_enabled(q) && bound) {
-		wait_event(guc->ct.wq, vf_recovery(guc) ||
+		wait_event(q->guc->state_wq, vf_recovery(guc) ||
 			   ((q->guc->resume_time != RESUME_PENDING ||
 			   xe_guc_read_stopped(guc)) && !exec_queue_pending_disable(q)));
 
@@ -2129,6 +2154,7 @@ static int guc_exec_queue_init(struct xe_exec_queue *q)
 	ge->q = q;
 	init_rcu_head(&ge->rcu);
 	init_waitqueue_head(&ge->suspend_wait);
+	init_waitqueue_head(&ge->state_wq);
 
 	for (i = 0; i < MAX_STATIC_MSG_TYPE; ++i)
 		INIT_LIST_HEAD(&ge->static_msgs[i].link);
@@ -2787,6 +2813,7 @@ static int guc_submit_reset_prepare(struct xe_guc *guc)
 	ret = atomic_fetch_or(1, &guc->submission_state.stopped);
 	smp_wmb();
 	wake_up_all(&guc->ct.wq);
+	xe_guc_submit_wake_state_waiters(guc);
 
 	return ret;
 }
@@ -3350,6 +3377,7 @@ static void handle_sched_done(struct xe_guc *guc, struct xe_exec_queue *q,
 		clear_exec_queue_pending_enable(q);
 		smp_wmb();
 		wake_up_all(&guc->ct.wq);
+		wake_up_all(&q->guc->state_wq);
 	} else {
 		bool was_pending;
 
@@ -3385,6 +3413,7 @@ static void handle_sched_done(struct xe_guc *guc, struct xe_exec_queue *q,
 				clear_exec_queue_pending_disable(q);
 			}
 		}
+		wake_up_all(&q->guc->state_wq);
 	}
 }
 
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 29abf07d8f041..0749f7c400b63 100644
@@ -28,6 +28,7 @@ void xe_guc_submit_unpause(struct xe_guc *guc);
 void xe_guc_submit_unpause_vf(struct xe_guc *guc);
 void xe_guc_submit_unpause_prepare_vf(struct xe_guc *guc);
 void xe_guc_submit_wedge(struct xe_guc *guc);
+void xe_guc_submit_wake_state_waiters(struct xe_guc *guc);
 
 int xe_guc_read_stopped(struct xe_guc *guc);
 int xe_guc_sched_done_handler(struct xe_guc *guc, u32 *msg, u32 len);
Signature · 1 line
2.43.0
#3 lore raw

PATCH v5 3/4 drm/xe: Disable scheduling early on FD close to avoid CAT error cascade

In reply to Daniel Charles

From: Matthew Brost <[email protected]>

When an FD is closed with many exec queues, teardown relies on the TDR path to clean up scheduling. However, the TDR handling is serialized (i.e., only one exec queue is processed at a time), which can make it too slow compared to GuC scheduling activity.

In this window, GuC may continue to schedule contexts backed by invalid page tables, leading to a cascade of CAT errors and repeated engine resets. This significantly increases recovery time and can degrade system stability.

To mitigate this, eagerly disable scheduling by sending a self-message outside of the TDR path. This prevents further scheduling of invalid contexts and avoids the CAT error/reset cascade.

Wait for pending enable/disable operations to complete before sending the early disable. If the queue has no references when the KILL message is processed, let the following CLEANUP message handle teardown. Assert that the queue is referenced when kill is entered.

Early KILL waits on the per-queue scheduling-state waitqueue. Only the primary can issue the early DISABLE for a multi-queue group. Allow pending disable on killed queues in run_job and have TDR recheck whether scheduling is still enabled after waiting for a pending transition.

v5: Updated the description for the waitqueue, primary-only group KILL, and the run_job and TDR fixes.

---
 drivers/gpu/drm/xe/xe_guc_exec_queue_types.h |  2 +-
 drivers/gpu/drm/xe/xe_guc_submit.c           | 87 ++++++++++++++++++--
 2 files changed, 80 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_guc_exec_queue_types.h +1 -1
index 2b1a40e288f17..3f5b1950a248a 100644
@@ -36,7 +36,7 @@ struct xe_guc_exec_queue {
 	 * a message needs to sent through the GPU scheduler but memory
 	 * allocations are not allowed.
 	 */
-#define MAX_STATIC_MSG_TYPE	4
+#define MAX_STATIC_MSG_TYPE	5
 	struct xe_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
 	/** @destroy_async: do final destroy async from this worker */
 	struct work_struct destroy_async;
drivers/gpu/drm/xe/xe_guc_submit.c +79 -8

140 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH v5 4/4 drm/xe: Synchronously wait for queue kill on FD close

In reply to Daniel Charles

GuC may still schedule contexts while FD close tears down their page tables, producing CAT errors and engine resets. Split FD close into two passes: kill every queue, then wait for each queue before dropping its reference.

Require the KILL handler to acknowledge processing before accepting a single scheduling-state snapshot. For multi-queue groups, all members wait for the primary KILL acknowledgement and scheduling state on the primary state waitqueue.

The wait also exits on GuC submission stop, VF recovery, or wedge. On timeout, request a GT reset asynchronously; the wait does not establish hardware quiescence on those paths.

v2:
- Bound the wait with a timeout, GT reset on timeout (Sashiko, Matt B)
- Drop the flush_work() loop (Matt B)
- Use xe_assert() for the killed-queue check (Matt B)
- Merge the put loop into the kill_sync loop (Matt B)
v3:
- Wait on a dedicated kill_wq instead of waking ct.wq on every scheduling-disable SCHED_DONE (CI)
v5: Rewrote the description for the primary KILL acknowledgement and per-queue state waitqueue.

Assisted-by LLM
---
 drivers/gpu/drm/xe/xe_device.c               |  5 +++
 drivers/gpu/drm/xe/xe_exec_queue.c           | 13 ++++++
 drivers/gpu/drm/xe/xe_exec_queue.h           |  1 +
 drivers/gpu/drm/xe/xe_exec_queue_types.h     |  2 +
 drivers/gpu/drm/xe/xe_guc_exec_queue_types.h |  6 +++
 drivers/gpu/drm/xe/xe_guc_submit.c           | 47 ++++++++++++++++++++
 6 files changed, 74 insertions(+)
drivers/gpu/drm/xe/xe_device.c +5 -0
index 8392c9fad3c48..cf75fa3b8fa4b 100644
@@ -190,8 +190,13 @@ static void xe_file_close(struct drm_device *dev, struct drm_file *file)
 		if (q->vm && q->hwe->hw_engine_group)
 			xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
 		xe_exec_queue_kill(q);
+	}
+
+	xa_for_each(&xef->exec_queue.xa, idx, q) {
+		xe_exec_queue_kill_sync(q);
 		xe_exec_queue_put(q);
 	}
+
 	xa_for_each(&xef->vm.xa, idx, vm)
 		xe_vm_close_and_put(vm);
 
drivers/gpu/drm/xe/xe_exec_queue.c +13 -0
index 9a8efa2bf2bfb..8ba2cf45694dd 100644
@@ -1622,6 +1622,19 @@ void xe_exec_queue_kill(struct xe_exec_queue *q)
 	xe_vm_remove_compute_exec_queue(q->vm, q);
 }
 
+/**
+ * xe_exec_queue_kill_sync() - Wait for queue scheduling to stop
+ * @q: The exec queue
+ *
+ * This function waits for the backend to finish stopping the queue after
+ * xe_exec_queue_kill(). It is safe to call multiple times.
+ */
+void xe_exec_queue_kill_sync(struct xe_exec_queue *q)
+{
+	if (q->ops->kill_sync)
+		q->ops->kill_sync(q);
+}
+
 /**
  * xe_exec_queue_destroy_ioctl() - Destroy an exec queue via IOCTL
  * @dev: DRM device
drivers/gpu/drm/xe/xe_exec_queue.h +1 -0
index e8963f85cabd9..4be023b9a4c6c 100644
@@ -119,6 +119,7 @@ bool xe_exec_queue_is_lr(struct xe_exec_queue *q);
 bool xe_exec_queue_is_idle(struct xe_exec_queue *q, int extra_jobs);
 
 void xe_exec_queue_kill(struct xe_exec_queue *q);
+void xe_exec_queue_kill_sync(struct xe_exec_queue *q);
 
 int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
 			       struct drm_file *file);
drivers/gpu/drm/xe/xe_exec_queue_types.h +2 -0
index 48c05398b0168..4882eeccb9bd5 100644
@@ -294,6 +294,8 @@ struct xe_exec_queue_ops {
 	int (*init)(struct xe_exec_queue *q);
 	/** @kill: Kill inflight submissions for backend */
 	void (*kill)(struct xe_exec_queue *q);
+	/** @kill_sync: Wait for the backend to stop queue scheduling */
+	void (*kill_sync)(struct xe_exec_queue *q);
 	/** @fini: Undoes the init() for submission backend */
 	void (*fini)(struct xe_exec_queue *q);
 	/**
drivers/gpu/drm/xe/xe_guc_exec_queue_types.h +6 -0
index 3f5b1950a248a..cb0e460f4762f 100644
@@ -54,6 +54,12 @@ struct xe_guc_exec_queue {
 	wait_queue_head_t suspend_wait;
 	/** @state_wq: wait queue for scheduling enable/disable state changes */
 	wait_queue_head_t state_wq;
+	/**
+	 * @kill_processed: KILL handler has drained earlier submission work and
+	 * issued any required disable. Published with release semantics and
+	 * never cleared by repeated kill requests.
+	 */
+	bool kill_processed;
 	/**
 	 * @suspend_pending: a suspend of the exec_queue is pending.
 	 * Protected by @sched.msg_lock.
drivers/gpu/drm/xe/xe_guc_submit.c +47 -0

71 lines not on this page · raw message

Signature · 1 line
2.43.0