Skip to contentSkip to conversation

drm/amdgpu: pass the device to the multi ring reset helpers

PATCH New Jesse Zhang 2 participants 23 messages Nov 26

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

PATCH v5 1/15 drm/amdgpu: pass the device to the multi ring reset helpers

amdgpu_multi_ring_reset_helper_begin() and _end() take the device from the guilty ring. A reset triggered by a hung user queue has no guilty kernel ring, so pass the device explicitly and allow a NULL guilty ring.

No functional change.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c   |  4 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 14 ++++++++------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h |  6 ++++--
 3 files changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c +2 -2
index 734f2a0bc79a..d0b0d3eaffc5 100644
@@ -465,11 +465,11 @@ int amdgpu_device_ip_soft_reset(struct amdgpu_ring *guilty_ring,
 	ring_type_mask = amdgpu_ring_mask_from_ip(ip_type);
 
 	amdgpu_device_lock_reset_domain(adev->reset_domain);
-	amdgpu_multi_ring_reset_helper_begin(ring_type_mask, guilty_ring, guilty_fence);
+	amdgpu_multi_ring_reset_helper_begin(adev, ring_type_mask, guilty_ring, guilty_fence);
 
 	r = ip_block->version->funcs->soft_reset(ip_block);
 
-	r = amdgpu_multi_ring_reset_helper_end(ring_type_mask, guilty_ring, r);
+	r = amdgpu_multi_ring_reset_helper_end(adev, ring_type_mask, guilty_ring, r);
 	amdgpu_device_unlock_reset_domain(adev->reset_domain);
 
 	if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +8 -6
index 411c74802d20..be91143d9aed 100644
@@ -928,8 +928,9 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
 /**
  * amdgpu_multi_ring_reset_helper_begin() - Prepare multiple rings for a reset.
  *
+ * @adev: amdgpu device
  * @ring_type_mask: Bitmask of affected ring types
- * @guilty_ring: The ring which is guilty of causing a reset.
+ * @guilty_ring: The ring which is guilty of causing a reset, or NULL.
  * @guilty_fence: The fence which didn't signal on the guilty ring.
  *
  * Useful when performing a GPU reset method that affects
@@ -947,11 +948,11 @@ int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
  * After the reset is complete, the caller should then call
  * amdgpu_multi_ring_reset_helper_end() to restore the rings.
  */
-void amdgpu_multi_ring_reset_helper_begin(const u32 ring_type_mask,
+void amdgpu_multi_ring_reset_helper_begin(struct amdgpu_device *adev,
+					  const u32 ring_type_mask,
 					  struct amdgpu_ring *guilty_ring,
 					  struct amdgpu_fence *guilty_fence)
 {
-	struct amdgpu_device *adev = guilty_ring->adev;
 	struct amdgpu_fence *ring_guilty_fence;
 	struct amdgpu_ring *ring;
 	bool rings_busy;
@@ -1043,8 +1044,9 @@ void amdgpu_multi_ring_reset_helper_begin(const u32 ring_type_mask,
 /**
  * amdgpu_multi_ring_reset_helper_end() - Prepare multiple rings for a reset.
  *
+ * @adev: amdgpu device
  * @ring_type_mask: Bitmask of affected ring types
- * @guilty_ring: The ring which is guilty of causing a reset.
+ * @guilty_ring: The ring which is guilty of causing a reset, or NULL.
  * @ret: Return code from the reset function.
  *
  * After calling amdgpu_multi_ring_reset_helper_begin()
@@ -1055,10 +1057,10 @@ void amdgpu_multi_ring_reset_helper_begin(const u32 ring_type_mask,
  * be called to restore preemption state, but it won't attempt to
  * fully restore the ring contents.
  */
-int amdgpu_multi_ring_reset_helper_end(const u32 ring_type_mask,
+int amdgpu_multi_ring_reset_helper_end(struct amdgpu_device *adev,
+				       const u32 ring_type_mask,
 				       struct amdgpu_ring *guilty_ring, int ret)
 {
-	struct amdgpu_device *adev = guilty_ring->adev;
 	struct amdgpu_ring *ring;
 	int i, r;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -2
index 6224a9f94826..db8fbe92a261 100644
@@ -638,10 +638,12 @@ void amdgpu_ring_reset_helper_begin(struct amdgpu_ring *ring,
 				    struct amdgpu_fence *guilty_fence);
 int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring,
 				 struct amdgpu_fence *guilty_fence);
-void amdgpu_multi_ring_reset_helper_begin(const u32 ring_type_mask,
+void amdgpu_multi_ring_reset_helper_begin(struct amdgpu_device *adev,
+					  const u32 ring_type_mask,
 					  struct amdgpu_ring *guilty_ring,
 					  struct amdgpu_fence *guilty_fence);
-int amdgpu_multi_ring_reset_helper_end(const u32 ring_type_mask,
+int amdgpu_multi_ring_reset_helper_end(struct amdgpu_device *adev,
+				       const u32 ring_type_mask,
 				       struct amdgpu_ring *guilty_ring, int ret);
 bool amdgpu_ring_is_reset_type_supported(struct amdgpu_ring *ring,
 					 u32 reset_type);
Signature · 1 line
2.50.1
#1 lore raw

PATCH v5 2/15 drm/amdgpu/ring: During multi-queue reset, only call begin helper if needed

From: Timur Kristóf <[email protected]>

This is necessary to make soft reset work after a failed queue reset. Otherwise the ring reset begin helper would clear out the guilty fence and the backed up ring contents.

v2: skip the begin helper only for the guilty ring already backed up by
the failed ring reset, and keep backing up the other rings (Jesse).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +3 -1
index be91143d9aed..40cf8342eee9 100644
@@ -1036,8 +1036,10 @@ void amdgpu_multi_ring_reset_helper_begin(struct amdgpu_device *adev,
 		 * Backup current contents of the ring.
 		 * The helper takes care to only reemit unsignalled fences
 		 * so we don't have to worry about that here.
+		 * A failed ring reset of the same job already backed it up.
 		 */
-		amdgpu_ring_reset_helper_begin(ring, ring_guilty_fence);
+		if (ring != guilty_ring || ring->guilty_fence != guilty_fence)
+			amdgpu_ring_reset_helper_begin(ring, ring_guilty_fence);
 	}
 }
 
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH v5 02/15] drm/amdgpu/ring: During multi-queue reset, only call begin helper if needed

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: ring->guilty_fence check in amdgpu_multi_ring_reset_helper_begin() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: ring->guilty_fence check in amdgpu_multi_ring_reset_helper_begin()
--

commit 142729efa2e6db09ea45e10147ee07f2ee131c88
Author: Timur Kristóf <[email protected]>

drm/amdgpu/ring: During multi-queue reset, only call begin helper if needed

This commit skips calling amdgpu_ring_reset_helper_begin() in amdgpu_multi_ring_reset_helper_begin() when ring == guilty_ring and ring->guilty_fence == guilty_fence, to preserve the ring backup from a prior failed per-queue reset.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index edae764eb0c42..c71e1c3b6d7bb 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c void amdgpu_multi_ring_reset_helper_begin(struct amdgpu_device *adev, · line 1035

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +3 -1
@@ -1027,8 +1027,10 @@ void amdgpu_multi_ring_reset_helper_begin(struct amdgpu_device *adev,
 		 * Backup current contents of the ring.
 		 * The helper takes care to only reemit unsignalled fences
 		 * so we don't have to worry about that here.
+		 * A failed ring reset of the same job already backed it up.
 		 */
-		amdgpu_ring_reset_helper_begin(ring, ring_guilty_fence);
+		if (ring != guilty_ring || ring->guilty_fence != guilty_fence)
+			amdgpu_ring_reset_helper_begin(ring, ring_guilty_fence);
 	}
 }

[Severity: High]
Can checking ring->guilty_fence == guilty_fence distinguish an unconsumed backup from a failed queue reset in the current timeout from a prior reset of the same fence that already completed?

amdgpu_ring_set_fence_errors_and_reemit() leaves ring->guilty_fence and ring->ring_backup_entries_to_copy unchanged after re-emitting a ring so that amdgpu_ring_backup_unprocessed_commands() can detect a second timeout of the same fence and stop replaying:

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c:amdgpu_ring_backup_unprocessed_commands() {
	...
	ring->ring_backup_entries_to_copy = 0;

	if (guilty_fence && ring->guilty_fence == guilty_fence) {
		ring->guilty_fence = NULL;
		return;
	}
	ring->guilty_fence = guilty_fence;
	...
}

When a soft reset succeeds and the re-emitted command stream times out again before guilty_fence signals, ring->guilty_fence is still guilty_fence. Skipping amdgpu_ring_reset_helper_begin() bypasses that check and leaves ring->ring_backup_entries_to_copy at its previous non-zero count, so amdgpu_multi_ring_reset_helper_end() keeps re-emitting the stale backup in an infinite soft-reset loop. If new jobs were submitted after drm_sched_wqueue_start() at the end of the first reset, their fences advance ring->fence_drv.sync_seq without being backed up; amdgpu_ring_set_fence_errors_and_reemit() then allocates the smaller stale ring_backup_entries_to_copy DWord count while re-emitting all unsignaled fences up to sync_seq, underflowing ring->count_dw and re-emitting stale commands from ring->ring_backup[0..] for the new jobs.

Conversely, if a second timeout runs a per-queue reset first, amdgpu_ring_backup_unprocessed_commands() sees ring->guilty_fence == guilty_fence and clears ring->guilty_fence to NULL; if that per-queue reset then fails and falls back to amdgpu_device_ip_soft_reset(), this condition sees ring->guilty_fence != guilty_fence and backs up the ring again instead of terminating the replay loop. Also, if an engine-wide per-ring reset callback (such as amdgpu_sdma_reset_queue_legacy() or amdgpu_vcn_ring_reset()) fails after calling amdgpu_ring_reset_helper_begin() on a collateral ring with a non-NULL ring_guilty_fence, this condition still calls amdgpu_ring_reset_helper_begin() for ring != guilty_ring and clears that collateral ring's backup.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#3 lore raw

PATCH v5 3/15 drm/amdgpu: add an engine scope to the IP block soft reset

In reply to Jesse Zhang

Some IP blocks have engines the hardware can reset on their own, eg. the graphics and compute engines of the GFX IP. Add an optional soft_reset_engine() callback, which resets the engine of a ring type and recovers its user queues. amdgpu_device_ip_soft_reset() uses it instead of soft_reset(), and then only stops and replays the rings of that type, holding the reset domain for read: KFD can still add and remove queues on the other engines.

A hung user queue has no kernel ring, so amdgpu_device_ip_soft_reset() now takes the device, the ring type and the instance of the engine, and an optional guilty ring and user queue. On a ring timeout, the instance is the XCC of the gfx and compute rings, and ring->me of the others.

v3: New patch, replaces the separate engine reset type and helper

    (Timur).

v4: Fold the engine reset into amdgpu_device_ip_soft_reset(), and take
the ring type instead of a new engine enum (Timur).
Pass the instance of the engine, needed for the user queues (Lijo).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c   | 60 ++++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ip.h   |  7 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c  | 16 ++++++-
 drivers/gpu/drm/amd/include/amd_shared.h |  7 +++
 4 files changed, 67 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c +40 -20
index d0b0d3eaffc5..79bc275e3e0f 100644
@@ -421,8 +421,12 @@ static u32 amdgpu_ring_mask_from_ip(const enum amd_ip_block_type ip_type)
 /**
  * amdgpu_device_ip_soft_reset() - Perform a graceful soft reset on an IP block.
  *
- * @guilty_ring: The ring which is guilty of causing a reset.
- * @guilty_fence: The fence which didn't signal.
+ * @adev: amdgpu device
+ * @ring_type: The type of the ring or user queue that hung.
+ * @inst: The instance of the engine that hung, eg. the XCC or SDMA instance.
+ * @guilty_ring: The ring which is guilty of causing a reset, or NULL.
+ * @guilty_fence: The fence which didn't signal on @guilty_ring.
+ * @guilty_queue: The user queue which is guilty of causing a reset, or NULL.
  *
  * IP block soft reset is used when attempting to recover
  * from a GPU hang in a situation where a more fine grained
@@ -430,55 +434,71 @@ static u32 amdgpu_ring_mask_from_ip(const enum amd_ip_block_type ip_type)
  * resets all rings that belong to the same device IP block
  * and re-initializes the device IP block.
  *
+ * IP blocks implementing soft_reset_engine() only reset the engine of
+ * @ring_type on instance @inst and its rings, and recover its user queues.
+ *
  * The reset is handled gracefully, meaning that we try to
  * minimize collateral damage (ie. avoid rejecting non-guilty jobs)
  * as well as back up and restore the contents of all rings
  * so that the system can move on from the hang.
  */
-int amdgpu_device_ip_soft_reset(struct amdgpu_ring *guilty_ring,
-				struct amdgpu_fence *guilty_fence)
+int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev,
+				enum amdgpu_ring_type ring_type, u32 inst,
+				struct amdgpu_ring *guilty_ring,
+				struct amdgpu_fence *guilty_fence,
+				struct amdgpu_usermode_queue *guilty_queue)
 {
-	struct amdgpu_device *adev = guilty_ring->adev;
+	const struct amd_ip_funcs *funcs;
 	struct amdgpu_ip_block *ip_block;
 	enum amd_ip_block_type ip_type;
 	u32 ring_type_mask;
+	bool engine;
 	int r;
 
-	ip_type = amdgpu_ip_from_ring(guilty_ring->funcs->type);
+	ip_type = amdgpu_ip_from_ring(ring_type);
 	ip_block = amdgpu_device_ip_get_ip_block(adev, ip_type);
 
 	if (unlikely(!ip_block)) {
-		dev_warn(adev->dev, "IP block not found for ring %s\n",
-			 guilty_ring->name);
+		dev_warn(adev->dev, "IP block not found for ring type %d\n",
+			 ring_type);
 		return -EOPNOTSUPP;
 	}
 
-	if (!ip_block->version->funcs->soft_reset) {
+	funcs = ip_block->version->funcs;
+	engine = funcs->soft_reset_engine;
+	if (!engine && (!funcs->soft_reset || !guilty_ring)) {
 		dev_warn(adev->dev, "IP block soft reset not supported on %s\n",
-			 ip_block->version->funcs->name);
+			 funcs->name);
 		return -EOPNOTSUPP;
 	}
 
-	dev_err(adev->dev, "Starting %s IP block soft reset\n",
-		ip_block->version->funcs->name);
-
-	ring_type_mask = amdgpu_ring_mask_from_ip(ip_type);
+	dev_err(adev->dev, "Starting %s IP block soft reset\n", funcs->name);
 
-	amdgpu_device_lock_reset_domain(adev->reset_domain);
+	/* An engine reset leaves the others running: let KFD add/remove queues. */
+	if (engine) {
+		ring_type_mask = BIT(ring_type);
+		down_read(&adev->reset_domain->sem);
+	} else {
+		ring_type_mask = amdgpu_ring_mask_from_ip(ip_type);
+		amdgpu_device_lock_reset_domain(adev->reset_domain);
+	}
 	amdgpu_multi_ring_reset_helper_begin(adev, ring_type_mask, guilty_ring, guilty_fence);
 
-	r = ip_block->version->funcs->soft_reset(ip_block);
+	r = engine ? funcs->soft_reset_engine(ip_block, ring_type, inst, guilty_queue) :
+		     funcs->soft_reset(ip_block);
 
 	r = amdgpu_multi_ring_reset_helper_end(adev, ring_type_mask, guilty_ring, r);
-	amdgpu_device_unlock_reset_domain(adev->reset_domain);
+	if (engine)
+		up_read(&adev->reset_domain->sem);
+	else
+		amdgpu_device_unlock_reset_domain(adev->reset_domain);
 
 	if (r) {
 		dev_err(adev->dev, "Failed %s IP block soft reset: %d\n",
-			ip_block->version->funcs->name, r);
+			funcs->name, r);
 		return r;
 	}
 
-	dev_err(adev->dev, "Successful %s IP block soft reset\n",
-		ip_block->version->funcs->name);
+	dev_err(adev->dev, "Successful %s IP block soft reset\n", funcs->name);
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ip.h +5 -2
index 5dd6273982c4..133e7c849a56 100644
@@ -158,7 +158,10 @@ int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
 				   enum amd_ip_block_type block_type);
 bool amdgpu_device_ip_is_valid(struct amdgpu_device *adev,
 			       enum amd_ip_block_type block_type);
-int amdgpu_device_ip_soft_reset(struct amdgpu_ring *guilty_ring,
-				struct amdgpu_fence *guilty_fence);
+int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev,
+				enum amdgpu_ring_type ring_type, u32 inst,
+				struct amdgpu_ring *guilty_ring,
+				struct amdgpu_fence *guilty_fence,
+				struct amdgpu_usermode_queue *guilty_queue);
 
 #endif /* __AMDGPU_IP_H__ */
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +15 -1
index 8689a3c933b2..8d33ae680c5e 100644
@@ -85,6 +85,18 @@ static void amdgpu_job_core_dump(struct amdgpu_device *adev,
 	}
 }
 
+/* The instance of the IP block engine the ring runs on. */
+static u32 amdgpu_job_ring_inst(struct amdgpu_ring *ring)
+{
+	switch (ring->funcs->type) {
+	case AMDGPU_RING_TYPE_GFX:
+	case AMDGPU_RING_TYPE_COMPUTE:
+		return ring->xcc_id;
+	default:
+		return ring->me;
+	}
+}
+
 static enum drm_gpu_sched_stat amdgpu_job_timedout(struct drm_sched_job *s_job)
 {
 	struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched);
@@ -154,7 +166,9 @@ static enum drm_gpu_sched_stat amdgpu_job_timedout(struct drm_sched_job *s_job)
 	/* Attempt an IP block soft reset, if supported. */
 	if (amdgpu_gpu_recovery &&
 	    amdgpu_ring_is_reset_type_supported(ring, AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET)) {
-		r = amdgpu_device_ip_soft_reset(ring, job->hw_fence);
+		r = amdgpu_device_ip_soft_reset(adev, ring->funcs->type,
+						amdgpu_job_ring_inst(ring), ring,
+						job->hw_fence, NULL);
 		if (!r) {
 			atomic_inc(&ring->adev->gpu_reset_counter);
 			drm_dev_wedged_event(adev_to_drm(adev), DRM_WEDGE_RECOVERY_NONE, info);
drivers/gpu/drm/amd/include/amd_shared.h +7 -0
index db24bacb8e0e..8087318c007d 100644
@@ -29,6 +29,7 @@
 
 #define AMD_MAX_USEC_TIMEOUT		1000000  /* 1000 ms */
 struct amdgpu_ip_block;
+struct amdgpu_usermode_queue;
 
 
 /*
@@ -421,6 +422,7 @@ enum DC_DEBUG_MASK {
 };
 
 enum amd_dpm_forced_level;
+enum amdgpu_ring_type;
 
 /**
  * struct amd_ip_funcs - general hooks for managing amdgpu IP Blocks
@@ -441,6 +443,8 @@ enum amd_dpm_forced_level;
  * @complete: handles IP specific changes after resume
  * @wait_for_idle: poll for idle
  * @soft_reset: soft reset the IP block
+ * @soft_reset_engine: soft reset one instance of the engine of a ring type and
+ *                     its user queues
  * @set_clockgating_state: enable/disable cg for the IP block
  * @set_powergating_state: enable/disable pg for the IP block
  * @get_clockgating_state: get current clockgating status
@@ -472,6 +476,9 @@ struct amd_ip_funcs {
 	void (*complete)(struct amdgpu_ip_block *ip_block);
 	int (*wait_for_idle)(struct amdgpu_ip_block *ip_block);
 	int (*soft_reset)(struct amdgpu_ip_block *ip_block);
+	int (*soft_reset_engine)(struct amdgpu_ip_block *ip_block,
+				 enum amdgpu_ring_type ring_type, u32 inst,
+				 struct amdgpu_usermode_queue *guilty_queue);
 	int (*set_clockgating_state)(struct amdgpu_ip_block *ip_block,
 				     enum amd_clockgating_state state);
 	int (*set_powergating_state)(struct amdgpu_ip_block *ip_block,
Signature · 1 line
2.50.1
#4 lore raw

Re: [PATCH v5 03/15] drm/amdgpu: add an engine scope to the IP block soft reset

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: race with user queues in amdgpu_device_ip_soft_reset() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: race with user queues in amdgpu_device_ip_soft_reset()
--

commit 2c34e267b776523348afd4f14e7ef41c33d20c0a
Author: Jesse Zhang <[email protected]>

drm/amdgpu: add an engine scope to the IP block soft reset

This commit adds an optional soft_reset_engine() callback to amd_ip_funcs and updates amdgpu_device_ip_soft_reset() to reset only the hung engine and recover its user queues while holding reset_domain->sem for read.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c +0 -0
index d0b0d3eaffc56..79bc275e3e0fa 100644

[ ... ]

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c +0 -0
@@ -430,55 +434,71 @@ static u32 amdgpu_ring_mask_from_ip(const enum amd_ip_block_type ip_type)

[ ... ]

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c +3 -1
-	amdgpu_device_lock_reset_domain(adev->reset_domain);
+	/* An engine reset leaves the others running: let KFD add/remove queues. */
+	if (engine) {
+		ring_type_mask = BIT(ring_type);

drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c static u32 amdgpu_ring_mask_from_ip(const enum amd_ip_block_type ip_type) · line 488

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ip.c +7 -1
+		down_read(&adev->reset_domain->sem);
+	} else {
+		ring_type_mask = amdgpu_ring_mask_from_ip(ip_type);
+		amdgpu_device_lock_reset_domain(adev->reset_domain);
+	}
 	amdgpu_multi_ring_reset_helper_begin(adev, ring_type_mask, guilty_ring, guilty_fence);
 
-	r = ip_block->version->funcs->soft_reset(ip_block);
+	r = engine ? funcs->soft_reset_engine(ip_block, ring_type, inst, guilty_queue) :
+		     funcs->soft_reset(ip_block);

[Severity: High]
Can holding adev->reset_domain->sem only for read during an engine reset race with concurrent user queue operations and cause a use-after-free on struct amdgpu_usermode_queue?

The user queue helpers in drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c (amdgpu_userq_unmap_helper(), amdgpu_userq_map_helper(), and amdgpu_userq_detach_doorbell()) also take adev->reset_domain->sem for read to serialize against GPU resets:

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c:amdgpu_userq_detach_doorbell() {
    ...
	down_read(&adev->reset_domain->sem);
	xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index);
	up_read(&adev->reset_domain->sem);
}

Because two readers do not exclude each other, user queue teardown, eviction, or restore can run concurrently while soft_reset_engine() is suspending MES and iterating adev->userq_doorbell_xa (via amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() in commit "drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset") without holding uq_mgr->userq_mutex:

CPU0 CPU1
amdgpu_device_ip_soft_reset()
down_read(&adev->reset_domain->sem)
funcs->soft_reset_engine()

    amdgpu_userq_save_hqds()
      xa_for_each(&adev->userq_doorbell_xa, ..., queue)
                                        amdgpu_userq_destroy()
                                          amdgpu_userq_detach_doorbell()
                                            down_read(&adev->reset_domain->sem)
                                            xa_erase_irq(&adev->userq_doorbell_xa, ...)
                                            up_read(&adev->reset_domain->sem)
                                          kfree(queue)
        queue->state = ...;  <-- use-after-free

Concurrent eviction or restore paths can likewise call into MES while it is suspended and race on queue->state. Note that soft_reset_engine() cannot simply acquire uq_mgr->userq_mutex either, because amdgpu_lockdep_init() defines userq_mutex as outer to reset_domain->sem.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#5 lore raw

PATCH v5 4/15 drm/amdgpu/gfx: add callbacks to save and restore a gfx HQD

In reply to Jesse Zhang

A gfx engine reset clears the HQD registers of the gfx queues, including the only copy of the ring position of the queues MES didn't preempt. MES doesn't save them in the MQD either when the engine hangs.

Add struct amdgpu_gfx_saved_hqd for that state, and gfx callbacks for the gfx IP, which knows its registers and the layout of its MQD:
- save_hqd() saves the state of the gfx HQD of a doorbell;
- restore_hqd() writes it into the MQD after the reset, for MES to load it into the HQD when it maps the queue again.

v4: Rename to save_hqd()/restore_hqd() and struct amdgpu_gfx_saved_hqd

    (Timur).
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +22 -0
index 99739b00c35f..ec77f24db0e2 100644
@@ -340,6 +340,17 @@ struct amdgpu_gfx_shadow_info {
 	u32 eop_alignment;
 };
 
+/* State of a gfx HQD that a gfx engine reset clears. */
+struct amdgpu_gfx_saved_hqd {
+	u32	doorbell_index;
+	u64	mqd_addr;	/* the MQD loaded in the HQD */
+	bool	on_hqd;
+	u64	wptr;
+	u32	offset;
+	u32	csmd_rptr;
+	bool	connected;	/* the queue the pipe executes */
+};
+
 struct amdgpu_gfx_funcs {
 	/* get the gpu clock counter */
 	uint64_t (*get_gpu_clock_counter)(struct amdgpu_device *adev);
@@ -374,6 +385,17 @@ struct amdgpu_gfx_funcs {
 	/* find the gfx HQD slot (me/pipe/queue) for a doorbell */
 	bool (*detect_hung_queue)(struct amdgpu_device *adev, u32 doorbell_index,
 				  u32 *me, u32 *pipe, u32 *queue);
+	/* save the state of the gfx HQD of a doorbell, if it has one */
+	bool (*save_hqd)(struct amdgpu_device *adev, u32 doorbell_index,
+			 struct amdgpu_gfx_saved_hqd *hqd);
+	/*
+	 * write the HQD state saved by save_hqd() into a gfx MQD, for MES to
+	 * load it into the HQD when it maps the queue again;
+	 * true if the work up to hqd->wptr was dropped
+	 */
+	bool (*restore_hqd)(struct amdgpu_device *adev, void *mqd,
+			    u32 ring_size,
+			    const struct amdgpu_gfx_saved_hqd *hqd);
 };
 
 struct sq_work {
Signature · 1 line
2.50.1
#6 lore raw

PATCH v5 5/15 drm/amdgpu/gfx12: save and restore a gfx HQD

In reply to Jesse Zhang

Implement the gfx callbacks to save and restore a gfx HQD for gfx12:
- save_hqd() saves the HQD offset, CSMD read pointer and write pointer of the gfx HQD of a doorbell, and whether the queue is the one connected to its pipe;
- restore_hqd() writes them into the MQD after a reset, for MES to map the queue again where it was. A queue switched out of the pipe resumes from the preemption status the PFP saved in its MQD, also in the middle of an IB. The queue the pipe was executing has its state in the PFP, which the reset cleared: it restarts after the work submitted so far instead, and that work is dropped.

v4: Rename to save_hqd()/restore_hqd() (Timur).

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 83 ++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +83 -0
index 09a27c3f6921..8ca8e3f8392d 100644
@@ -274,6 +274,11 @@ static void gfx_v12_0_set_imu_funcs(struct amdgpu_device *adev);
 static int gfx_v12_0_get_cu_info(struct amdgpu_device *adev,
 				 struct amdgpu_cu_info *cu_info);
 static uint64_t gfx_v12_0_get_gpu_clock_counter(struct amdgpu_device *adev);
+static bool gfx_v12_0_save_hqd(struct amdgpu_device *adev, u32 doorbell_index,
+			       struct amdgpu_gfx_saved_hqd *hqd);
+static bool gfx_v12_0_restore_hqd(struct amdgpu_device *adev, void *m,
+				  u32 ring_size,
+				  const struct amdgpu_gfx_saved_hqd *hqd);
 static void gfx_v12_0_select_se_sh(struct amdgpu_device *adev, u32 se_num,
 				   u32 sh_num, u32 instance, int xcc_id);
 static u32 gfx_v12_0_get_wgp_active_bitmap_per_sh(struct amdgpu_device *adev);
@@ -967,6 +972,8 @@ static const struct amdgpu_gfx_funcs gfx_v12_0_gfx_funcs = {
 	.set_perfmon_clk_override = &gfx_v12_0_set_perfmon_clk_override,
 	.get_gfx_shadow_info = &gfx_v12_0_get_gfx_shadow_info,
 	.get_hdp_flush_mask = &amdgpu_gfx_get_hdp_flush_mask,
+	.save_hqd = &gfx_v12_0_save_hqd,
+	.restore_hqd = &gfx_v12_0_restore_hqd,
 };
 
 static int gfx_v12_0_gpu_early_init(struct amdgpu_device *adev)
@@ -3861,6 +3868,82 @@ static int gfx_v12_0_wait_for_idle(struct amdgpu_ip_block *ip_block)
 	return -ETIMEDOUT;
 }
 
+#define GFX12_HPD_QUEUE_STATE_CONNECTED_MIN	3
+#define GFX12_HPD_QUEUE_STATE_CONNECTED_MAX	15
+
+static bool gfx_v12_0_save_hqd(struct amdgpu_device *adev, u32 doorbell_index,
+			       struct amdgpu_gfx_saved_hqd *hqd)
+{
+	u32 me, pipe, queue, status, state;
+
+	memset(hqd, 0, sizeof(*hqd));
+	hqd->doorbell_index = doorbell_index;
+	if (!gfx_v12_0_detect_hung_queue(adev, doorbell_index, &me, &pipe, &queue))
+		return true;
+
+	amdgpu_gfx_off_ctrl(adev, false);
+	mutex_lock(&adev->srbm_mutex);
+	soc24_grbm_select(adev, me, pipe, queue, 0);
+	hqd->on_hqd = RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE) &
+		      CP_GFX_HQD_ACTIVE__ACTIVE_MASK;
+	if (!hqd->on_hqd)
+		goto out;
+	hqd->mqd_addr = RREG32_SOC15(GC, 0, regCP_GFX_MQD_BASE_ADDR) |
+			(u64)RREG32_SOC15(GC, 0, regCP_GFX_MQD_BASE_ADDR_HI) << 32;
+	hqd->offset = RREG32_SOC15(GC, 0, regCP_GFX_HQD_OFFSET);
+	hqd->csmd_rptr = RREG32_SOC15(GC, 0, regCP_GFX_HQD_CSMD_RPTR);
+	hqd->wptr = RREG32_SOC15(GC, 0, regCP_GFX_HQD_WPTR) |
+		    (u64)RREG32_SOC15(GC, 0, regCP_GFX_HQD_WPTR_HI) << 32;
+	status = RREG32_SOC15(GC, 0, regCP_GFX_HPD_STATUS0);
+	state = REG_GET_FIELD(status, CP_GFX_HPD_STATUS0, QUEUE_STATE);
+	hqd->connected =
+		state >= GFX12_HPD_QUEUE_STATE_CONNECTED_MIN &&
+		state <= GFX12_HPD_QUEUE_STATE_CONNECTED_MAX &&
+		REG_GET_FIELD(status, CP_GFX_HPD_STATUS0, MAPPED_QUEUE) == queue;
+out:
+	soc24_grbm_select(adev, 0, 0, 0, 0);
+	mutex_unlock(&adev->srbm_mutex);
+	amdgpu_gfx_off_ctrl(adev, true);
+
+	return true;
+}
+
+static bool gfx_v12_0_restore_hqd(struct amdgpu_device *adev, void *m,
+				  u32 ring_size,
+				  const struct amdgpu_gfx_saved_hqd *hqd)
+{
+	struct v12_gfx_mqd *mqd = m;
+	u32 wptr = lower_32_bits(hqd->wptr) & ((ring_size >> 2) - 1);
+	u32 *status;
+
+	/*
+	 * MQD dwords 32-41 (reserved_32..41) are the PFP preemption status,
+	 * mcbp_status_pfp: the offset and chained IB size and address of IB1
+	 * (32-35) and IB2 (36-39), rb_preempt (40: preempted in the ring) and
+	 * completion_status (41: 0 IB not started, 1 preempted inside an IB,
+	 * 2 IB completed, 3 IB resumed). The PFP saves it when the queue is
+	 * switched out of the pipe and resumes from it when the queue runs
+	 * again, at the HQD offset. That survives the reset for a queue that
+	 * was switched out. The queue the pipe executed has its state in the
+	 * PFP, which the reset cleared: restart it at the write pointer with
+	 * no IB to resume, and drop its work in flight.
+	 */
+	if (hqd->connected) {
+		mqd->cp_gfx_hqd_offset = wptr;
+		mqd->cp_gfx_hqd_csmd_rptr = wptr;
+		for (status = &mqd->reserved_32; status <= &mqd->reserved_41; status++)
+			*status = 0;
+	} else {
+		mqd->cp_gfx_hqd_offset = hqd->offset;
+		mqd->cp_gfx_hqd_csmd_rptr = hqd->csmd_rptr;
+	}
+	mqd->cp_gfx_hqd_wptr = lower_32_bits(hqd->wptr);
+	mqd->cp_gfx_hqd_wptr_hi = upper_32_bits(hqd->wptr);
+	mqd->cp_gfx_hqd_active = 0;
+
+	return hqd->connected;
+}
+
 static uint64_t gfx_v12_0_get_gpu_clock_counter(struct amdgpu_device *adev)
 {
 	uint64_t clock = 0;
Signature · 1 line
2.50.1
#7 lore raw

PATCH v5 6/15 drm/amdgpu/gfx11: save and restore a gfx HQD

In reply to Jesse Zhang

Implement the gfx callbacks to save and restore a gfx HQD for gfx11:
- save_hqd() saves the HQD offset, CSMD read pointer and write pointer of the gfx HQD of a doorbell, and whether the queue is the one connected to its pipe;
- restore_hqd() writes them into the MQD after a reset, for MES to map the queue again where it was. A queue switched out of the pipe resumes from the preemption status the PFP saved in its MQD, also in the middle of an IB. The queue the pipe was executing has its state in the PFP, which the reset cleared: it restarts after the work submitted so far instead, and that work is dropped.

v4: Rename to save_hqd()/restore_hqd() (Timur).

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 83 ++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +83 -0
index 570f0f3a2ea5..ecf8a156b7ec 100644
@@ -382,6 +382,11 @@ static void gfx_v11_0_update_spm_vmid(struct amdgpu_device *adev, int xcc_id,
 				      struct amdgpu_ring *ring,
 				      unsigned int vmid);
 static void gfx_v11_0_userq_priv_fault_work(struct work_struct *work);
+static bool gfx_v11_0_save_hqd(struct amdgpu_device *adev, u32 doorbell_index,
+			       struct amdgpu_gfx_saved_hqd *hqd);
+static bool gfx_v11_0_restore_hqd(struct amdgpu_device *adev, void *m,
+				  u32 ring_size,
+				  const struct amdgpu_gfx_saved_hqd *hqd);
 static void gfx_v11_0_update_perf_clk(struct amdgpu_device *adev,
 				      bool enable);
 static bool gfx_v11_0_set_perfmon_clk_override(struct amdgpu_device *adev,
@@ -1171,6 +1176,8 @@ static const struct amdgpu_gfx_funcs gfx_v11_0_gfx_funcs = {
 	.get_gfx_shadow_info = &gfx_v11_0_get_gfx_shadow_info,
 	.get_hdp_flush_mask = &amdgpu_gfx_get_hdp_flush_mask,
 	.detect_hung_queue = &gfx_v11_0_detect_hung_queue,
+	.save_hqd = &gfx_v11_0_save_hqd,
+	.restore_hqd = &gfx_v11_0_restore_hqd,
 };
 
 static int gfx_v11_0_gpu_early_init(struct amdgpu_device *adev)
@@ -6813,6 +6820,82 @@ static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring)
 	amdgpu_ring_write(ring, gcr_cntl); /* GCR_CNTL */
 }
 
+#define GFX11_HPD_QUEUE_STATE_CONNECTED_MIN	3
+#define GFX11_HPD_QUEUE_STATE_CONNECTED_MAX	15
+
+static bool gfx_v11_0_save_hqd(struct amdgpu_device *adev, u32 doorbell_index,
+			       struct amdgpu_gfx_saved_hqd *hqd)
+{
+	u32 me, pipe, queue, status, state;
+
+	memset(hqd, 0, sizeof(*hqd));
+	hqd->doorbell_index = doorbell_index;
+	if (!gfx_v11_0_detect_hung_queue(adev, doorbell_index, &me, &pipe, &queue))
+		return true;
+
+	amdgpu_gfx_off_ctrl(adev, false);
+	mutex_lock(&adev->srbm_mutex);
+	soc21_grbm_select(adev, me, pipe, queue, 0);
+	hqd->on_hqd = RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE) &
+		      CP_GFX_HQD_ACTIVE__ACTIVE_MASK;
+	if (!hqd->on_hqd)
+		goto out;
+	hqd->mqd_addr = RREG32_SOC15(GC, 0, regCP_GFX_MQD_BASE_ADDR) |
+			(u64)RREG32_SOC15(GC, 0, regCP_GFX_MQD_BASE_ADDR_HI) << 32;
+	hqd->offset = RREG32_SOC15(GC, 0, regCP_GFX_HQD_OFFSET);
+	hqd->csmd_rptr = RREG32_SOC15(GC, 0, regCP_GFX_HQD_CSMD_RPTR);
+	hqd->wptr = RREG32_SOC15(GC, 0, regCP_GFX_HQD_WPTR) |
+		    (u64)RREG32_SOC15(GC, 0, regCP_GFX_HQD_WPTR_HI) << 32;
+	status = RREG32_SOC15(GC, 0, regCP_GFX_HPD_STATUS0);
+	state = REG_GET_FIELD(status, CP_GFX_HPD_STATUS0, QUEUE_STATE);
+	hqd->connected =
+		state >= GFX11_HPD_QUEUE_STATE_CONNECTED_MIN &&
+		state <= GFX11_HPD_QUEUE_STATE_CONNECTED_MAX &&
+		REG_GET_FIELD(status, CP_GFX_HPD_STATUS0, MAPPED_QUEUE) == queue;
+out:
+	soc21_grbm_select(adev, 0, 0, 0, 0);
+	mutex_unlock(&adev->srbm_mutex);
+	amdgpu_gfx_off_ctrl(adev, true);
+
+	return true;
+}
+
+static bool gfx_v11_0_restore_hqd(struct amdgpu_device *adev, void *m,
+				  u32 ring_size,
+				  const struct amdgpu_gfx_saved_hqd *hqd)
+{
+	struct v11_gfx_mqd *mqd = m;
+	u32 wptr = lower_32_bits(hqd->wptr) & ((ring_size >> 2) - 1);
+	u32 *status;
+
+	/*
+	 * MQD dwords 32-41 (reserved_32..41) are the PFP preemption status,
+	 * mcbp_status_pfp: the offset and chained IB size and address of IB1
+	 * (32-35) and IB2 (36-39), rb_preempt (40: preempted in the ring) and
+	 * completion_status (41: 0 IB not started, 1 preempted inside an IB,
+	 * 2 IB completed, 3 IB resumed). The PFP saves it when the queue is
+	 * switched out of the pipe and resumes from it when the queue runs
+	 * again, at the HQD offset. That survives the reset for a queue that
+	 * was switched out. The queue the pipe executed has its state in the
+	 * PFP, which the reset cleared: restart it at the write pointer with
+	 * no IB to resume, and drop its work in flight.
+	 */
+	if (hqd->connected) {
+		mqd->cp_gfx_hqd_offset = wptr;
+		mqd->cp_gfx_hqd_csmd_rptr = wptr;
+		for (status = &mqd->reserved_32; status <= &mqd->reserved_41; status++)
+			*status = 0;
+	} else {
+		mqd->cp_gfx_hqd_offset = hqd->offset;
+		mqd->cp_gfx_hqd_csmd_rptr = hqd->csmd_rptr;
+	}
+	mqd->cp_gfx_hqd_wptr = lower_32_bits(hqd->wptr);
+	mqd->cp_gfx_hqd_wptr_hi = upper_32_bits(hqd->wptr);
+	mqd->cp_gfx_hqd_active = 0;
+
+	return hqd->connected;
+}
+
 static int gfx_v11_0_reset_kgq(struct amdgpu_ring *ring,
 			       unsigned int vmid,
 			       struct amdgpu_fence *timedout_fence)
Signature · 1 line
2.50.1
#8 lore raw

PATCH v5 7/15 drm/amdgpu/userq: add a helper to signal the fences of a queue up to a point

In reply to Jesse Zhang

The fence value of a user queue is the write pointer of the queue when the work was submitted. amdgpu_userq_fence_driver_force_completion() signals all the fences of a queue, as after a reset that dropped all its work.

Add amdgpu_userq_fence_driver_force_completion_with_seqno(), which only signals the fences of the work up to a write pointer, with an error, for a reset that drops only part of the work of a queue: the work submitted after that write pointer still runs.

---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   | 31 +++++++++++++++++++
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.h   |  2 ++
 2 files changed, 33 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +31 -0
index dd976a50e053..d20dce32168b 100644
@@ -460,6 +460,37 @@ amdgpu_userq_fence_driver_force_completion(struct amdgpu_usermode_queue *userq)
 	}
 }
 
+/*
+ * Signal the fences of @userq with a seqno up to @seqno with -ECANCELED, eg.
+ * when a reset dropped the work that would have signaled them.
+ */
+void
+amdgpu_userq_fence_driver_force_completion_with_seqno(struct amdgpu_usermode_queue *userq,
+						      u64 seqno)
+{
+	struct amdgpu_userq_fence_driver *fence_drv = userq->fence_drv;
+	struct amdgpu_userq_fence *fence;
+	unsigned long flags;
+
+	if (!fence_drv)
+		return;
+
+	spin_lock_irqsave(&fence_drv->fence_list_lock, flags);
+	list_for_each_entry(fence, &fence_drv->fences, link) {
+		if (fence->base.seqno > seqno)
+			break;
+		spin_lock(dma_fence_spinlock(&fence->base));
+		if (!dma_fence_is_signaled_locked(&fence->base))
+			dma_fence_set_error(&fence->base, -ECANCELED);
+		spin_unlock(dma_fence_spinlock(&fence->base));
+	}
+	if (amdgpu_userq_fence_read(fence_drv) < seqno)
+		amdgpu_userq_fence_write(fence_drv, seqno);
+	spin_unlock_irqrestore(&fence_drv->fence_list_lock, flags);
+
+	amdgpu_userq_fence_driver_process(fence_drv);
+}
+
 int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 			      struct drm_file *filp)
 {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.h +2 -0
index 0bd51616cef1..0407c8c2575f 100644
@@ -65,6 +65,8 @@ int amdgpu_userq_fence_driver_alloc(struct amdgpu_device *adev,
 void amdgpu_userq_fence_driver_free(struct amdgpu_usermode_queue *userq);
 int amdgpu_userq_fence_driver_process(struct amdgpu_userq_fence_driver *fence_drv);
 void amdgpu_userq_fence_driver_force_completion(struct amdgpu_usermode_queue *userq);
+void amdgpu_userq_fence_driver_force_completion_with_seqno(struct amdgpu_usermode_queue *userq,
+							   u64 seqno);
 void amdgpu_userq_fence_driver_destroy(struct kref *ref);
 int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 			      struct drm_file *filp);
Signature · 1 line
2.50.1
#9 lore raw

PATCH v5 8/15 drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset

In reply to Jesse Zhang

An engine reset, eg. of the gfx engine, only resets the queues of one engine of the GFX IP; the queues of the other engines aren't reset. amdgpu_userq_pre_reset() and amdgpu_userq_post_reset() handle all the user queues for a full reset. Add separate helpers for the queues of one engine, which keep them in the state they had:

- amdgpu_userq_save_hqds(), called after the MES suspend, saves the HQD of the mapped queues still on a HQD: the reset clears their HQD, and MES couldn't preempt them into their MQD. The queues MES preempted have their state in their MQD already.
- amdgpu_userq_restore_hqds(), called after the reset but before MES resumes, unmaps those queues, writes the saved HQD into their MQD and maps them again: MES doesn't map a queue whose HQD the reset cleared again on its own. The queues keep their state and their work, except the one the pipe executed: its work in flight depends on pipe state the reset cleared, it is dropped and its fences signaled. The queue that hung, if any, is unmapped and hung.
- amdgpu_userq_clear_hqds() drops the saved HQDs for the full reset that follows a failed engine reset.

The helpers walk the queues and unmap and map them as amdgpu_userq_pre_reset() and amdgpu_userq_post_reset() do. The saved HQDs are kept in the gfx IP, in saved_gfx_hqds[], and matched to a queue by its doorbell and MQD address.

v2: Drop the user queue reset lock and the walk over the queues of one
type (Christian).
v4: Rename the helpers and the saved HQD array (Timur).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h   |   3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 107 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |   4 +
 3 files changed, 114 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index ec77f24db0e2..0d42ada0bc39 100644
@@ -513,6 +513,9 @@ struct amdgpu_gfx {
 
 	/* reset mask */
 	uint32_t 			gfx_supported_reset;
+	/* HQDs of the gfx user queues, saved by an engine reset */
+	struct amdgpu_gfx_saved_hqd	saved_gfx_hqds[AMDGPU_MAX_GFX_QUEUES];
+	u32				num_saved_gfx_hqds;
 	uint32_t 			compute_supported_reset;
 
 	/* gfx off */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +107 -0

110 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4 -0
index 488dc21d7c81..b5195031877d 100644
@@ -181,6 +181,10 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 void amdgpu_userq_reset_work(struct work_struct *work);
 void amdgpu_userq_pre_reset(struct amdgpu_device *adev);
 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost);
+void amdgpu_userq_save_hqds(struct amdgpu_device *adev, u32 queue_type);
+void amdgpu_userq_restore_hqds(struct amdgpu_device *adev, u32 queue_type,
+			       struct amdgpu_usermode_queue *guilty);
+void amdgpu_userq_clear_hqds(struct amdgpu_device *adev, u32 queue_type);
 void amdgpu_userq_start_hang_detect_work(struct amdgpu_usermode_queue *queue);
 void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell);
 
Signature · 1 line
2.50.1
#10 lore raw

Re: [PATCH v5 08/15] drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu/userq: unsynchronized xa_for_each() UAF and race in HQD helpers --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu/userq: unsynchronized xa_for_each() UAF and race in HQD helpers
--

commit f5832ab9e726e4c6325d50c80fa806f8412f97f7
Author: Jesse Zhang <[email protected]>

drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset

This commit adds helpers to save, restore, and clear the HQDs of user queues belonging to a single engine across a soft reset.

Quoted 85 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +77 -0
index ed6f3b67e8720..495198ba1b0b0 100644
@@ -1594,3 +1594,110 @@ int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost)
 
 	return ret;
 }
+
+/**
+ * amdgpu_userq_save_hqds - save the HQDs of the user queues before an engine reset
+ * @adev: amdgpu device
+ * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine
+ */
+void amdgpu_userq_save_hqds(struct amdgpu_device *adev, u32 queue_type)
+{
+	struct amdgpu_gfx_saved_hqd hqd;
+	struct amdgpu_usermode_queue *queue;
+	unsigned long queue_id;
+
+	adev->gfx.num_saved_gfx_hqds = 0;
+	if (!adev->gfx.funcs->save_hqd)
+		return;
+
+	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+		if (adev->gfx.num_saved_gfx_hqds >= AMDGPU_MAX_GFX_QUEUES)
+			break;
+		if (queue->queue_type != queue_type ||
+		    queue->state != AMDGPU_USERQ_STATE_MAPPED)
+			continue;
+		if (adev->gfx.funcs->save_hqd(adev, queue->doorbell_index, &hqd) &&
+		    hqd.on_hqd)
+			adev->gfx.saved_gfx_hqds[adev->gfx.num_saved_gfx_hqds++] = hqd;
+	}
+}
+
+static struct amdgpu_gfx_saved_hqd *
+amdgpu_userq_find_saved_hqd(struct amdgpu_device *adev,
+			    struct amdgpu_usermode_queue *queue)
+{
+	struct amdgpu_gfx_saved_hqd *hqd;
+	u32 i;
+
+	for (i = 0; i < adev->gfx.num_saved_gfx_hqds; i++) {
+		hqd = &adev->gfx.saved_gfx_hqds[i];
+		/* Check the MQD too: a new queue may reuse the doorbell. */
+		if (hqd->doorbell_index == queue->doorbell_index &&
+		    hqd->mqd_addr == queue->mqd.gpu_addr)
+			return hqd;
+	}
+	return NULL;
+}
+
+/**
+ * amdgpu_userq_restore_hqds - restore the user queues after an engine reset
+ * @adev: amdgpu device
+ * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine
+ * @guilty: the queue that hung, or NULL
+ *
+ * MES doesn't map a queue whose HQD the reset cleared again on its own: unmap
+ * it from MES, restore its MQD, and map it again.
+ */
+void amdgpu_userq_restore_hqds(struct amdgpu_device *adev, u32 queue_type,
+			       struct amdgpu_usermode_queue *guilty)
+{
+	const struct amdgpu_userq_funcs *userq_funcs = adev->userq_funcs[queue_type];
+	struct amdgpu_usermode_queue *queue;
+	struct amdgpu_gfx_saved_hqd *hqd;
+	unsigned long queue_id;
+	int r;
+
+	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+		if (queue->queue_type != queue_type ||
+		    queue->state != AMDGPU_USERQ_STATE_MAPPED)
+			continue;
+
+		if (queue == guilty) {
+			userq_funcs->unmap(queue);
+			amdgpu_userq_fence_driver_force_completion(queue);
+			trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG);
+			queue->state = AMDGPU_USERQ_STATE_HUNG;
+			continue;
+		}
+
+		hqd = amdgpu_userq_find_saved_hqd(adev, queue);

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost) · line 1683

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+		if (!hqd)
+			continue;
+
+		userq_funcs->unmap(queue);
+		if (adev->gfx.funcs->restore_hqd(adev, queue->mqd.cpu_ptr,
+						 queue->userq_prop->queue_size,
+						 hqd))
+			amdgpu_userq_fence_driver_force_completion_with_seqno(queue,
+									      hqd->wptr);
+		r = userq_funcs->map(queue);

[Severity: High]
Can amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() race with amdgpu_userq_destroy() and cause a use-after-free on queue?

Unlike a full GPU reset, which holds down_write(&adev->reset_domain->sem), an engine soft reset in amdgpu_device_ip_soft_reset() only acquires down_read(&adev->reset_domain->sem). amdgpu_userq_detach_doorbell() and amdgpu_userq_unmap_helper() in the queue destruction path also only acquire down_read(&adev->reset_domain->sem), so queue destruction is not blocked while these helpers walk userq_doorbell_xa (as the comment in amdgpu_userq_find_saved_hqd() also notes). Because xa_for_each() releases rcu_read_lock() before entering the loop body and nothing pins queue or holds queue->userq_mgr->userq_mutex:

CPU0 CPU1
amdgpu_userq_restore_hqds()
xa_for_each(..., queue)

                                      amdgpu_userq_destroy()
                                        amdgpu_userq_detach_doorbell(queue)
                                        ...
                                        uq_funcs->mqd_destroy(queue)
                                        kfree(queue)
    if (queue->queue_type != ...)
    userq_funcs->unmap(queue)  <-- use-after-free

Even when the queue is not destroyed, iterating and calling userq_funcs->unmap() and userq_funcs->map() without holding queue->userq_mgr->userq_mutex races with concurrent eviction and restore workers that also run under down_read(&adev->reset_domain->sem), which can corrupt queue->state or double-unmap and double-free queue->gang_ctx_array_index. In addition, if a queue is destroyed and a new queue is created between amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds(), both the freed doorbell index and the freed GTT MQD address (queue->mqd.gpu_addr) can be reallocated to the new queue, causing amdgpu_userq_find_saved_hqd() to match and overwrite the new queue's MQD with the old queue's saved HQD state.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#11 lore raw

PATCH v5 9/15 drm/amdgpu/gfx12: add a gfx engine soft reset

In reply to Jesse Zhang

Some hangs leave the gfx streaming engine of the CP front-end stuck, eg. waiting for packet data that never arrives. Neither a queue nor a pipe reset can stop it, so they end in a full GPU reset.

Implement soft_reset_engine() for the graphics engine: halt PFP/ME, pulse GRBM_SOFT_RESET.SOFT_RESET_CPF/CPG with CP_SOFT_RESET_CNTL.GFX_ONLY_SOFT_RESET and GFX_HQD_REG_RESET, restart PFP/ME and map the kernel gfx queue again.
MEC, MES and the compute queues keep running. The MES lock is held across the pulse, as MES fetches its queues through CPF, and the CP DMA is drained around it, as a request in flight leaves the ME busy. The hung PFP leaves a VMID reset pending, which the CP must not be doing while it is reset and which survives the pulse: clear CP_VMID_RESET before the pulse and before PFP boots again.

MES is suspended meanwhile, and the gfx user queues the reset clears are saved and restored around it. If the CP doesn't go idle, the reset fails and a full reset follows.

v4: Implement soft_reset_engine() directly instead of a reset_engine gfx
callback (Timur).
v5: Clear CP_VMID_RESET, before and after the pulse (Timur).
Don't restore CP_PFP_IB_CONTROL: RLC sets it again by itself (Timur).

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 132 +++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +132 -0
index 8ca8e3f8392d..69bdae805597 100644
@@ -3868,6 +3868,137 @@ static int gfx_v12_0_wait_for_idle(struct amdgpu_ip_block *ip_block)
 	return -ETIMEDOUT;
 }
 
+/* The CP DMA and the interfaces its memory requests go through */
+#define GFX12_CP_STAT_MEM_BUSY	(CP_STAT__DMA_BUSY_MASK | \
+				 CP_STAT__TCIU_BUSY_MASK | \
+				 CP_STAT__RCIU_BUSY_MASK | \
+				 CP_STAT__UTCL2IU_BUSY_MASK)
+
+/* Reset CPF/CPG, PFP/ME and the gfx HQDs, then boot PFP/ME again. */
+static int gfx_v12_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
+{
+	u32 tmp, cntl, me_cntl;
+	int i;
+
+	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
+	/* MES fetches its queues through CPF: keep it out of the reset. */
+	amdgpu_mes_lock(&adev->mes);
+
+	me_cntl = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+
+	tmp = REG_SET_FIELD(me_cntl, CP_ME_CNTL, ME_HALT, 1);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, 1);
+	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+	udelay(50);
+
+	/* A CP DMA in flight during the reset leaves the ME busy after it. */
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX12_CP_STAT_MEM_BUSY))
+			break;
+		udelay(1);
+	}
+
+	/* The CP must not be doing a VMID reset while it is being reset. */
+	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0);
+
+	/* GFX_ONLY keeps compute and MES out; CPF alone leaves the stream stuck. */
+	cntl = RREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL);
+	tmp = REG_SET_FIELD(cntl, CP_SOFT_RESET_CNTL, GFX_ONLY_SOFT_RESET, 1);
+	tmp = REG_SET_FIELD(tmp, CP_SOFT_RESET_CNTL, GFX_HQD_REG_RESET, 1);
+	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, tmp);
+
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
+	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	udelay(50);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
+	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+
+	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, cntl);
+
+	/* The halted PFP stays busy: only wait for the memory requests. */
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX12_CP_STAT_MEM_BUSY))
+			break;
+		udelay(1);
+	}
+	/* A VMID reset the hung PFP didn't handle survives the reset. */
+	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0);
+	amdgpu_mes_unlock(&adev->mes);
+
+	gfx_v12_0_set_pfp_ucode_start_addr(adev);
+	gfx_v12_0_set_me_ucode_start_addr(adev);
+	tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, CE_HALT,
+			    REG_GET_FIELD(me_cntl, CP_ME_CNTL, CE_HALT));
+	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+	amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
+
+	tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_HALT, 0);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, 0);
+	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!RREG32_SOC15(GC, 0, regCP_STAT))
+			return 0;
+		udelay(1);
+	}
+	return -ETIMEDOUT;
+}
+
+static int gfx_v12_0_soft_reset_engine(struct amdgpu_ip_block *ip_block,
+				       enum amdgpu_ring_type ring_type, u32 inst,
+				       struct amdgpu_usermode_queue *guilty_queue)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	int i, r;
+
+	if (ring_type != AMDGPU_RING_TYPE_GFX || inst)
+		return -EOPNOTSUPP;
+
+	mutex_lock(&adev->gfx.mec.reset_mutex);
+	amdgpu_gfx_off_ctrl(adev, false);
+
+	/* A hung gfx stream can keep MES from suspending: reset anyway. */
+	amdgpu_mes_suspend(adev, 0);
+	amdgpu_userq_save_hqds(adev, AMDGPU_HW_IP_GFX);
+
+	r = gfx_v12_0_soft_reset_gfx_cp(adev);
+	if (r) {
+		dev_err(adev->dev, "gfx CP still busy after reset, CP_STAT 0x%08x\n",
+			RREG32_SOC15(GC, 0, regCP_STAT));
+		goto fail;
+	}
+
+	/* The reset cleared the HQD of the kernel gfx queue: map it again. */
+	r = amdgpu_gfx_disable_kgq(adev, 0);
+	if (r)
+		goto fail;
+	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
+		amdgpu_gfx_mqd_reset_restore(&adev->gfx.gfx_ring[i]);
+	r = amdgpu_gfx_enable_kgq(adev, 0);
+	if (!r)
+		r = gfx_v12_0_cp_gfx_start(adev);
+	if (r)
+		goto fail;
+
+	amdgpu_userq_restore_hqds(adev, AMDGPU_HW_IP_GFX, guilty_queue);
+	r = amdgpu_mes_resume(adev, 0);
+	goto out;
+
+fail:
+	amdgpu_userq_clear_hqds(adev, AMDGPU_HW_IP_GFX);
+	amdgpu_mes_resume(adev, 0);
+out:
+	amdgpu_gfx_off_ctrl(adev, true);
+	mutex_unlock(&adev->gfx.mec.reset_mutex);
+	return r;
+}
+
 #define GFX12_HPD_QUEUE_STATE_CONNECTED_MIN	3
 #define GFX12_HPD_QUEUE_STATE_CONNECTED_MAX	15
 
@@ -5403,6 +5534,7 @@ static const struct amd_ip_funcs gfx_v12_0_ip_funcs = {
 	.suspend = gfx_v12_0_suspend,
 	.resume = gfx_v12_0_resume,
 	.wait_for_idle = gfx_v12_0_wait_for_idle,
+	.soft_reset_engine = gfx_v12_0_soft_reset_engine,
 	.set_clockgating_state = gfx_v12_0_set_clockgating_state,
 	.set_powergating_state = gfx_v12_0_set_powergating_state,
 	.get_clockgating_state = gfx_v12_0_get_clockgating_state,
Signature · 1 line
2.50.1
#12 lore raw

Re: [PATCH v5 09/15] drm/amdgpu/gfx12: add a gfx engine soft reset

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu/gfx12: race and UAF on user queues in gfx_v12_0_soft_reset_engine() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu/gfx12: race and UAF on user queues in gfx_v12_0_soft_reset_engine()
--

commit 1eb59ae54b68b8bc41a2294b6011763c46222bdb
Author: Jesse Zhang <[email protected]>

drm/amdgpu/gfx12: add a gfx engine soft reset

This commit implements soft_reset_engine() for the GFX12 graphics engine to reset CPF/CPG, PFP/ME, and the GFX HQDs while saving and restoring user queues around the reset.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -0
index 8ca8e3f8392d3..69bdae805597c 100644
@@ -3868,6 +3868,137 @@ static int gfx_v12_0_wait_for_idle(struct amdgpu_ip_block *ip_block)

[ ... ]

Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +28 -0
+static int gfx_v12_0_soft_reset_engine(struct amdgpu_ip_block *ip_block,
+				       enum amdgpu_ring_type ring_type, u32 inst,
+				       struct amdgpu_usermode_queue *guilty_queue)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	int i, r;
+
+	if (ring_type != AMDGPU_RING_TYPE_GFX || inst)
+		return -EOPNOTSUPP;
+
+	mutex_lock(&adev->gfx.mec.reset_mutex);
+	amdgpu_gfx_off_ctrl(adev, false);
+
+	/* A hung gfx stream can keep MES from suspending: reset anyway. */
+	amdgpu_mes_suspend(adev, 0);
+	amdgpu_userq_save_hqds(adev, AMDGPU_HW_IP_GFX);
+
+	r = gfx_v12_0_soft_reset_gfx_cp(adev);
+	if (r) {
+		dev_err(adev->dev, "gfx CP still busy after reset, CP_STAT 0x%08x\n",
+			RREG32_SOC15(GC, 0, regCP_STAT));
+		goto fail;
+	}
+
+	/* The reset cleared the HQD of the kernel gfx queue: map it again. */
+	r = amdgpu_gfx_disable_kgq(adev, 0);
+	if (r)
+		goto fail;

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static int gfx_v12_0_wait_for_idle(struct amdgpu_ip_block *ip_block) · line 3990

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +10 -0
+	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
+		amdgpu_gfx_mqd_reset_restore(&adev->gfx.gfx_ring[i]);
+	r = amdgpu_gfx_enable_kgq(adev, 0);
+	if (!r)
+		r = gfx_v12_0_cp_gfx_start(adev);
+	if (r)
+		goto fail;
+
+	amdgpu_userq_restore_hqds(adev, AMDGPU_HW_IP_GFX, guilty_queue);
+	r = amdgpu_mes_resume(adev, 0);

[Severity: High]
Can amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() race with concurrent user queue destruction and trigger a use-after-free on queue?

When .soft_reset_engine is set, amdgpu_device_ip_soft_reset() only takes down_read(&adev->reset_domain->sem) rather than the exclusive write lock, and gfx_v12_0_soft_reset_engine() only holds adev->gfx.mec.reset_mutex. Meanwhile, amdgpu_userq_destroy() and amdgpu_userq_detach_doorbell() also use down_read(&adev->reset_domain->sem) to serialize against resets and do not take adev->gfx.mec.reset_mutex. Because xa_for_each() in amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() iterates adev->userq_doorbell_xa without holding userq_mutex or a queue reference:

CPU0 CPU1
amdgpu_device_ip_soft_reset()
down_read(&reset_domain->sem)
gfx_v12_0_soft_reset_engine()

    amdgpu_userq_restore_hqds()
      xa_for_each(..., queue)
                                      amdgpu_userq_destroy()
                                        amdgpu_userq_detach_doorbell()
                                          down_read(&reset_domain->sem)
                                          xa_erase_irq(...)
                                          up_read(&reset_domain->sem)
                                        uq_funcs->mqd_destroy(queue)
                                        kfree(queue)
        restore_hqd(...,
          queue->mqd.cpu_ptr, ...)  <-- use-after-free

For the same reason, amdgpu_userq_map_helper() and amdgpu_userq_unmap_helper() only take down_read(&adev->reset_domain->sem), so they can run concurrently with gfx_v12_0_soft_reset_engine(), racing on queue->state and issuing MES map/unmap commands while MES gangs are suspended.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#13 lore raw

PATCH v5 10/15 drm/amdgpu/gfx11: add a gfx engine soft reset

In reply to Jesse Zhang

Implement soft_reset_engine() for the graphics engine of gfx11, as for gfx12.

The CPF/CPG reset already puts PFP/ME back at their firmware start address, so unhalting them restarts the RS64 and F32 firmware alike.

v4: Implement soft_reset_engine() directly instead of a reset_engine gfx
callback, drop gfx_v11_0_restart_gfx_rs64(), and boot with CP_PFP_IB_CONTROL cleared for the F32 firmware (Timur).
v5: Don't save and restore CP_PFP_IB_CONTROL: the reset clears it, so
the F32 PFP boots as it should, and RLC sets it again by itself (Timur).

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 127 +++++++++++++++++++++++++
 1 file changed, 127 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +127 -0
index ecf8a156b7ec..a0ed3363f84d 100644
@@ -6820,6 +6820,132 @@ static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring)
 	amdgpu_ring_write(ring, gcr_cntl); /* GCR_CNTL */
 }
 
+/* The CP DMA and the interfaces its memory requests go through */
+#define GFX11_CP_STAT_MEM_BUSY	(CP_STAT__DMA_BUSY_MASK | \
+				 CP_STAT__TCIU_BUSY_MASK | \
+				 CP_STAT__RCIU_BUSY_MASK | \
+				 CP_STAT__UTCL2IU_BUSY_MASK)
+
+/* Reset CPF/CPG, PFP/ME and the gfx HQDs, then boot PFP/ME again. */
+static int gfx_v11_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
+{
+	u32 tmp, cntl, me_cntl;
+	int i;
+
+	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
+	/* MES fetches its queues through CPF: keep it out of the reset. */
+	amdgpu_mes_lock(&adev->mes);
+
+	me_cntl = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+
+	tmp = REG_SET_FIELD(me_cntl, CP_ME_CNTL, ME_HALT, 1);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, 1);
+	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+	udelay(50);
+
+	/* A CP DMA in flight during the reset leaves the ME busy after it. */
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX11_CP_STAT_MEM_BUSY))
+			break;
+		udelay(1);
+	}
+
+	/* GFX_ONLY keeps compute and MES out; CPF alone leaves the stream stuck. */
+	cntl = RREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL);
+	tmp = REG_SET_FIELD(cntl, CP_SOFT_RESET_CNTL, GFX_ONLY_SOFT_RESET, 1);
+	tmp = REG_SET_FIELD(tmp, CP_SOFT_RESET_CNTL, GFX_HQD_REG_RESET, 1);
+	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, tmp);
+
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
+	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	udelay(50);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
+	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+
+	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, cntl);
+
+	/* The halted PFP stays busy: only wait for the memory requests. */
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX11_CP_STAT_MEM_BUSY))
+			break;
+		udelay(1);
+	}
+	/* A VMID reset the hung PFP didn't handle survives the reset. */
+	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0);
+	amdgpu_mes_unlock(&adev->mes);
+
+	tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, CE_HALT,
+			    REG_GET_FIELD(me_cntl, CP_ME_CNTL, CE_HALT));
+	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+	amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
+
+	tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_HALT, 0);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, 0);
+	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!RREG32_SOC15(GC, 0, regCP_STAT))
+			return 0;
+		udelay(1);
+	}
+	return -ETIMEDOUT;
+}
+
+static int gfx_v11_0_soft_reset_engine(struct amdgpu_ip_block *ip_block,
+				       enum amdgpu_ring_type ring_type, u32 inst,
+				       struct amdgpu_usermode_queue *guilty_queue)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	int i, r;
+
+	if (ring_type != AMDGPU_RING_TYPE_GFX || inst)
+		return -EOPNOTSUPP;
+
+	mutex_lock(&adev->gfx.mec.reset_mutex);
+	amdgpu_gfx_off_ctrl(adev, false);
+
+	/* A hung gfx stream can keep MES from suspending: reset anyway. */
+	amdgpu_mes_suspend(adev, 0);
+	amdgpu_userq_save_hqds(adev, AMDGPU_HW_IP_GFX);
+
+	r = gfx_v11_0_soft_reset_gfx_cp(adev);
+	if (r) {
+		dev_err(adev->dev, "gfx CP still busy after reset, CP_STAT 0x%08x\n",
+			RREG32_SOC15(GC, 0, regCP_STAT));
+		goto fail;
+	}
+
+	/* The reset cleared the HQD of the kernel gfx queue: map it again. */
+	r = amdgpu_gfx_disable_kgq(adev, 0);
+	if (r)
+		goto fail;
+	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
+		amdgpu_gfx_mqd_reset_restore(&adev->gfx.gfx_ring[i]);
+	r = amdgpu_gfx_enable_kgq(adev, 0);
+	if (!r)
+		r = gfx_v11_0_cp_gfx_start(adev);
+	if (r)
+		goto fail;
+
+	amdgpu_userq_restore_hqds(adev, AMDGPU_HW_IP_GFX, guilty_queue);
+	r = amdgpu_mes_resume(adev, 0);
+	goto out;
+
+fail:
+	amdgpu_userq_clear_hqds(adev, AMDGPU_HW_IP_GFX);
+	amdgpu_mes_resume(adev, 0);
+out:
+	amdgpu_gfx_off_ctrl(adev, true);
+	mutex_unlock(&adev->gfx.mec.reset_mutex);
+	return r;
+}
+
 #define GFX11_HPD_QUEUE_STATE_CONNECTED_MIN	3
 #define GFX11_HPD_QUEUE_STATE_CONNECTED_MAX	15
 
@@ -7088,6 +7214,7 @@ static const struct amd_ip_funcs gfx_v11_0_ip_funcs = {
 	.suspend = gfx_v11_0_suspend,
 	.resume = gfx_v11_0_resume,
 	.wait_for_idle = gfx_v11_0_wait_for_idle,
+	.soft_reset_engine = gfx_v11_0_soft_reset_engine,
 	.soft_reset = gfx_v11_0_soft_reset,
 	.set_clockgating_state = gfx_v11_0_set_clockgating_state,
 	.set_powergating_state = gfx_v11_0_set_powergating_state,
Signature · 1 line
2.50.1
#14 lore raw

Re: [PATCH v5 10/15] drm/amdgpu/gfx11: add a gfx engine soft reset

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] amdgpu: missing pre-pulse CP_VMID_RESET clear in gfx_v11_0_soft_reset_gfx_cp() - [Medium] amdgpu…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] amdgpu: missing pre-pulse CP_VMID_RESET clear in gfx_v11_0_soft_reset_gfx_cp()
- [Medium] amdgpu: gfx_ring[0] left mapped on disable_kq in gfx_v11_0_soft_reset_engine()
--

commit 4f83c926ed3d12feae57fed7044a2f21e92280c8
Author: Jesse Zhang <[email protected]>

drm/amdgpu/gfx11: add a gfx engine soft reset

This commit implements the soft_reset_engine() callback for the graphics engine on GFX11 by resetting CPF/CPG, PFP/ME, and the GFX HQDs, then restarting the kernel and usermode GFX queues.

Quoted 55 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +47 -0
index ecf8a156b7ec4..a0ed3363f84d1 100644
@@ -6820,6 +6820,132 @@ static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring)
 	amdgpu_ring_write(ring, gcr_cntl); /* GCR_CNTL */
 }
 
+/* The CP DMA and the interfaces its memory requests go through */
+#define GFX11_CP_STAT_MEM_BUSY	(CP_STAT__DMA_BUSY_MASK | \
+				 CP_STAT__TCIU_BUSY_MASK | \
+				 CP_STAT__RCIU_BUSY_MASK | \
+				 CP_STAT__UTCL2IU_BUSY_MASK)
+
+/* Reset CPF/CPG, PFP/ME and the gfx HQDs, then boot PFP/ME again. */
+static int gfx_v11_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
+{
+	u32 tmp, cntl, me_cntl;
+	int i;
+
+	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
+	/* MES fetches its queues through CPF: keep it out of the reset. */
+	amdgpu_mes_lock(&adev->mes);
+
+	me_cntl = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+
+	tmp = REG_SET_FIELD(me_cntl, CP_ME_CNTL, ME_HALT, 1);
+	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, 1);
+	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+	udelay(50);
+
+	/* A CP DMA in flight during the reset leaves the ME busy after it. */
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX11_CP_STAT_MEM_BUSY))
+			break;
+		udelay(1);
+	}
+
+	/* GFX_ONLY keeps compute and MES out; CPF alone leaves the stream stuck. */
+	cntl = RREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL);
+	tmp = REG_SET_FIELD(cntl, CP_SOFT_RESET_CNTL, GFX_ONLY_SOFT_RESET, 1);
+	tmp = REG_SET_FIELD(tmp, CP_SOFT_RESET_CNTL, GFX_HQD_REG_RESET, 1);
+	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, tmp);
+
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
+	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	udelay(50);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
+	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
+	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring) · line 6879

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +10 -0
+	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, cntl);
+
+	/* The halted PFP stays busy: only wait for the memory requests. */
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX11_CP_STAT_MEM_BUSY))
+			break;
+		udelay(1);
+	}
+	/* A VMID reset the hung PFP didn't handle survives the reset. */
+	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0);

[Severity: Medium]
Should regCP_VMID_RESET also be cleared to 0 before asserting SOFT_RESET_CPF and SOFT_RESET_CPG in regGRBM_SOFT_RESET, as gfx_v12_0_soft_reset_gfx_cp() does?

When PFP is hung, the preceding per-queue and per-pipe MMIO resets in mes_v11_0_reset_queue_mmio() and mes_v11_0_reset_gfx_pipe_mmio() write a reset request to regCP_VMID_RESET and time out waiting for PFP to clear it. Pulsing regGRBM_SOFT_RESET while that VMID reset is still active can cause the CP soft reset to fail and escalate to a full GPU reset.

[ ... ]

Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +28 -0
+static int gfx_v11_0_soft_reset_engine(struct amdgpu_ip_block *ip_block,
+				       enum amdgpu_ring_type ring_type, u32 inst,
+				       struct amdgpu_usermode_queue *guilty_queue)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	int i, r;
+
+	if (ring_type != AMDGPU_RING_TYPE_GFX || inst)
+		return -EOPNOTSUPP;
+
+	mutex_lock(&adev->gfx.mec.reset_mutex);
+	amdgpu_gfx_off_ctrl(adev, false);
+
+	/* A hung gfx stream can keep MES from suspending: reset anyway. */
+	amdgpu_mes_suspend(adev, 0);
+	amdgpu_userq_save_hqds(adev, AMDGPU_HW_IP_GFX);
+
+	r = gfx_v11_0_soft_reset_gfx_cp(adev);
+	if (r) {
+		dev_err(adev->dev, "gfx CP still busy after reset, CP_STAT 0x%08x\n",
+			RREG32_SOC15(GC, 0, regCP_STAT));
+		goto fail;
+	}
+
+	/* The reset cleared the HQD of the kernel gfx queue: map it again. */
+	r = amdgpu_gfx_disable_kgq(adev, 0);
+	if (r)
+		goto fail;

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring) · line 6937

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +10 -0
+	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
+		amdgpu_gfx_mqd_reset_restore(&adev->gfx.gfx_ring[i]);
+	r = amdgpu_gfx_enable_kgq(adev, 0);
+	if (!r)
+		r = gfx_v11_0_cp_gfx_start(adev);
+	if (r)
+		goto fail;
+
+	amdgpu_userq_restore_hqds(adev, AMDGPU_HW_IP_GFX, guilty_queue);
+	r = amdgpu_mes_resume(adev, 0);

[Severity: Medium]
Does this leave adev->gfx.gfx_ring[0] mapped on HQD 0 when adev->gfx.disable_kq is true?

Unlike GFX12, gfx_v11_0_early_init() keeps adev->gfx.num_gfx_rings = 1 when disable_kq is set so gfx_v11_0_cp_gfx_start() can submit the clear state on gfx_ring[0], while amdgpu_mes_init() gives HQD 0 to MES for user queues. In gfx_v11_0_cp_resume(), the driver drains gfx_ring[0] and unmaps it before user queues run:

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c:gfx_v11_0_cp_resume() {
    ...
	if (adev->gfx.disable_kq) {
		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
			ring = &adev->gfx.gfx_ring[i];
			/* we don't want to set ring->ready */
			r = amdgpu_ring_test_ring(ring);
			if (r)
				return r;
		}
		amdgpu_gfx_disable_kgq(adev, 0);
	}
    ...
}

Here, gfx_v11_0_soft_reset_engine() calls amdgpu_gfx_disable_kgq() while gfx_ring[0] is already unmapped, then maps gfx_ring[0] and submits the clear state without testing the ring or calling amdgpu_gfx_disable_kgq() afterward, leaving the temporary kernel ring mapped on HQD 0 in conflict with restored MES usermode queues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#15 lore raw

PATCH v5 11/15 drm/amdgpu/gfx12: include SOFT_RESET_GFX in the gfx engine soft reset

In reply to Jesse Zhang

The gfx engine reset only pulses CPF/CPG, which leaves hangs of the fixed-function pipeline to a full reset. Pulse SOFT_RESET_GFX too.

SOFT_RESET_GFX drops the memory requests in flight through the blocks it resets, and their clients then wait for them forever: pulsed while RLC writes its DRAM log, which it still does in safe mode, RLC stays MC_BUSY and the next TLB invalidation of MES never completes; pulsed while MES fetches instructions, which it still does with its queues suspended, its next queue operation times out. Halt MES and RLC F32 across the reset and wait for the GC memory interfaces to drain before the pulse. A halted MES or F32 can stay busy, as long as it has no memory request in flight. If the GC doesn't drain, skip the pulse and fail the reset, so a full reset follows.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 100 +++++++++++++++++++++----
 1 file changed, 87 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +87 -13
index 69bdae805597..8485d1aabf3b 100644
@@ -3874,16 +3874,78 @@ static int gfx_v12_0_wait_for_idle(struct amdgpu_ip_block *ip_block)
 				 CP_STAT__RCIU_BUSY_MASK | \
 				 CP_STAT__UTCL2IU_BUSY_MASK)
 
-/* Reset CPF/CPG, PFP/ME and the gfx HQDs, then boot PFP/ME again. */
+/*
+ * SOFT_RESET_GFX drops the memory requests in flight through the blocks it
+ * resets, and their clients then wait for them forever: MES still fetches
+ * instructions with its queues suspended, and RLC still writes its DRAM log
+ * in safe mode. The reset halts both, then waits for these busy bits of the
+ * GC memory interfaces to clear.
+ */
+#define GFX12_GC_MEM_BUSY	(GRBM_STATUS2__EA_BUSY_MASK | \
+				 GRBM_STATUS2__EA_LINK_BUSY_MASK | \
+				 GRBM_STATUS2__UTCL2_BUSY_MASK | \
+				 GRBM_STATUS2__UTCL2_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__TCP_BUSY_MASK | \
+				 GRBM_STATUS2__CPC_BUSY_MASK | \
+				 GRBM_STATUS2__RLC_BUSY_MASK | \
+				 GRBM_STATUS2__RLC_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA_BUSY_MASK | \
+				 GRBM_STATUS2__SDMA_SCH_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA0_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA1_RQ_PENDING_MASK)
+
+/* The RLC busy bits of its memory requests */
+#define GFX12_RLC_MEM_BUSY	(RLC_STAT__MC_BUSY_MASK | \
+				 RLC_STAT__RLC_SRM_BUSY_MASK | \
+				 RLC_STAT__RLC_SPM_BUSY_MASK)
+
+/* Wait for the requests in flight in the GC, with MES and RLC F32 halted. */
+static int gfx_v12_0_wait_gc_idle(struct amdgpu_device *adev)
+{
+	u32 busy, cpc, rlc;
+	int i;
+
+	for (i = 0; i < adev->usec_timeout; i++) {
+		busy = RREG32_SOC15(GC, 0, regGRBM_STATUS2) & GFX12_GC_MEM_BUSY;
+		cpc = RREG32_SOC15(GC, 0, regCP_CPC_STATUS);
+		rlc = RREG32_SOC15(GC, 0, regRLC_STAT);
+
+		/* The halted MES and F32 can stay busy, not their memory requests. */
+		if (!(cpc & GENMASK(21, 0) & ~CP_CPC_STATUS__MES_BUSY_MASK))
+			busy &= ~GRBM_STATUS2__CPC_BUSY_MASK;
+		if (!(rlc & GFX12_RLC_MEM_BUSY))
+			busy &= ~GRBM_STATUS2__RLC_BUSY_MASK;
+
+		if (!busy && !(RREG32_SOC15(GC, 0, regGCVM_L2_STATUS) &
+			       GCVM_L2_STATUS__L2_BUSY_MASK))
+			return 0;
+		udelay(1);
+	}
+
+	dev_err(adev->dev, "GC still busy, GRBM_STATUS2 0x%08x CPC 0x%08x RLC 0x%08x\n",
+		RREG32_SOC15(GC, 0, regGRBM_STATUS2), cpc, rlc);
+	return -ETIMEDOUT;
+}
+
+/*
+ * Reset CPF/CPG, PFP/ME, the gfx HQDs and the fixed-function pipeline,
+ * then boot PFP/ME again.
+ */
 static int gfx_v12_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
 {
-	u32 tmp, cntl, me_cntl;
-	int i;
+	u32 tmp, cntl, me_cntl, mes_cntl, rlc_cntl;
+	int i, r = 0;
 
 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
 	/* MES fetches its queues through CPF: keep it out of the reset. */
 	amdgpu_mes_lock(&adev->mes);
 
+	/* SOFT_RESET_GFX needs the GC quiet: halt MES and RLC F32. */
+	mes_cntl = RREG32_SOC15(GC, 0, regCP_MES_CNTL);
+	WREG32_SOC15(GC, 0, regCP_MES_CNTL, mes_cntl | CP_MES_CNTL__MES_HALT_MASK);
+	rlc_cntl = RREG32_SOC15(GC, 0, regRLC_CNTL);
+	adev->gfx.rlc.funcs->stop(adev);
+
 	me_cntl = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
 
 	tmp = REG_SET_FIELD(me_cntl, CP_ME_CNTL, ME_HALT, 1);
@@ -3907,16 +3969,21 @@ static int gfx_v12_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
 	tmp = REG_SET_FIELD(tmp, CP_SOFT_RESET_CNTL, GFX_HQD_REG_RESET, 1);
 	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, tmp);
 
-	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
-	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
-	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
-	udelay(50);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
-	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
-	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	r = gfx_v12_0_wait_gc_idle(adev);
+	if (!r) {
+		tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_GFX, 1);
+		WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+		tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+		udelay(50);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_GFX, 0);
+		WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+		tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	}
 
 	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, cntl);
 
@@ -3928,7 +3995,14 @@ static int gfx_v12_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
 	}
 	/* A VMID reset the hung PFP didn't handle survives the reset. */
 	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0);
+	WREG32_SOC15(GC, 0, regRLC_CNTL, rlc_cntl);
+	WREG32_SOC15(GC, 0, regCP_MES_CNTL, mes_cntl);
 	amdgpu_mes_unlock(&adev->mes);
+	if (r) {
+		/* PFP/ME stay halted: a full reset follows. */
+		amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
+		return r;
+	}
 
 	gfx_v12_0_set_pfp_ucode_start_addr(adev);
 	gfx_v12_0_set_me_ucode_start_addr(adev);
Signature · 1 line
2.50.1
#16 lore raw

PATCH v5 12/15 drm/amdgpu/gfx11: include SOFT_RESET_GFX in the gfx engine soft reset

In reply to Jesse Zhang

As on gfx12, pulse SOFT_RESET_GFX with CPF/CPG in the gfx engine reset, with MES and RLC F32 halted and the GC drained, so that it recovers hangs of the fixed-function pipeline too.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 95 ++++++++++++++++++++++----
 1 file changed, 83 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +83 -12
index a0ed3363f84d..824d7914bece 100644
@@ -6827,15 +6827,74 @@ static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring)
 				 CP_STAT__UTCL2IU_BUSY_MASK)
 
 /* Reset CPF/CPG, PFP/ME and the gfx HQDs, then boot PFP/ME again. */
+/*
+ * SOFT_RESET_GFX drops the memory requests in flight through the blocks it
+ * resets, and their clients then wait for them forever: MES still fetches
+ * instructions with its queues suspended, and RLC still writes its DRAM log
+ * in safe mode. The reset halts both, then waits for these busy bits of the
+ * GC memory interfaces to clear.
+ */
+#define GFX11_GC_MEM_BUSY	(GRBM_STATUS2__EA_BUSY_MASK | \
+				 GRBM_STATUS2__EA_LINK_BUSY_MASK | \
+				 GRBM_STATUS2__UTCL2_BUSY_MASK | \
+				 GRBM_STATUS2__UTCL2_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__TCP_BUSY_MASK | \
+				 GRBM_STATUS2__CPC_BUSY_MASK | \
+				 GRBM_STATUS2__RLC_BUSY_MASK | \
+				 GRBM_STATUS2__RLC_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA_BUSY_MASK | \
+				 GRBM_STATUS2__SDMA_SCH_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA0_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA1_RQ_PENDING_MASK)
+
+/* The RLC busy bits of its memory requests */
+#define GFX11_RLC_MEM_BUSY	(RLC_STAT__MC_BUSY_MASK | \
+				 RLC_STAT__RLC_SRM_BUSY_MASK | \
+				 RLC_STAT__RLC_SPM_BUSY_MASK)
+
+/* Wait for the requests in flight in the GC, with MES and RLC F32 halted. */
+static int gfx_v11_0_wait_gc_idle(struct amdgpu_device *adev)
+{
+	u32 busy, cpc, rlc;
+	int i;
+
+	for (i = 0; i < adev->usec_timeout; i++) {
+		busy = RREG32_SOC15(GC, 0, regGRBM_STATUS2) & GFX11_GC_MEM_BUSY;
+		cpc = RREG32_SOC15(GC, 0, regCP_CPC_STATUS);
+		rlc = RREG32_SOC15(GC, 0, regRLC_STAT);
+
+		/* The halted MES and F32 can stay busy, not their memory requests. */
+		if (!(cpc & GENMASK(21, 0) & ~CP_CPC_STATUS__MES_BUSY_MASK))
+			busy &= ~GRBM_STATUS2__CPC_BUSY_MASK;
+		if (!(rlc & GFX11_RLC_MEM_BUSY))
+			busy &= ~GRBM_STATUS2__RLC_BUSY_MASK;
+
+		if (!busy && !(RREG32_SOC15(GC, 0, regGCVM_L2_STATUS) &
+			       GCVM_L2_STATUS__L2_BUSY_MASK))
+			return 0;
+		udelay(1);
+	}
+
+	dev_err(adev->dev, "GC still busy, GRBM_STATUS2 0x%08x CPC 0x%08x RLC 0x%08x\n",
+		RREG32_SOC15(GC, 0, regGRBM_STATUS2), cpc, rlc);
+	return -ETIMEDOUT;
+}
+
 static int gfx_v11_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
 {
-	u32 tmp, cntl, me_cntl;
-	int i;
+	u32 tmp, cntl, me_cntl, mes_cntl, rlc_cntl;
+	int i, r = 0;
 
 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
 	/* MES fetches its queues through CPF: keep it out of the reset. */
 	amdgpu_mes_lock(&adev->mes);
 
+	/* SOFT_RESET_GFX needs the GC quiet: halt MES and RLC F32. */
+	mes_cntl = RREG32_SOC15(GC, 0, regCP_MES_CNTL);
+	WREG32_SOC15(GC, 0, regCP_MES_CNTL, mes_cntl | CP_MES_CNTL__MES_HALT_MASK);
+	rlc_cntl = RREG32_SOC15(GC, 0, regRLC_CNTL);
+	adev->gfx.rlc.funcs->stop(adev);
+
 	me_cntl = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
 
 	tmp = REG_SET_FIELD(me_cntl, CP_ME_CNTL, ME_HALT, 1);
@@ -6856,16 +6915,21 @@ static int gfx_v11_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
 	tmp = REG_SET_FIELD(tmp, CP_SOFT_RESET_CNTL, GFX_HQD_REG_RESET, 1);
 	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, tmp);
 
-	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
-	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
-	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
-	udelay(50);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
-	tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
-	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
-	tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	r = gfx_v11_0_wait_gc_idle(adev);
+	if (!r) {
+		tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_GFX, 1);
+		WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+		tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+		udelay(50);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
+		tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_GFX, 0);
+		WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+		tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+	}
 
 	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, cntl);
 
@@ -6877,7 +6941,14 @@ static int gfx_v11_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
 	}
 	/* A VMID reset the hung PFP didn't handle survives the reset. */
 	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0);
+	WREG32_SOC15(GC, 0, regRLC_CNTL, rlc_cntl);
+	WREG32_SOC15(GC, 0, regCP_MES_CNTL, mes_cntl);
 	amdgpu_mes_unlock(&adev->mes);
+	if (r) {
+		/* PFP/ME stay halted: a full reset follows. */
+		amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
+		return r;
+	}
 
 	tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, CE_HALT,
Signature · 1 line
2.50.1
#17 lore raw

Re: [PATCH v5 12/15] drm/amdgpu/gfx11: include SOFT_RESET_GFX in the gfx engine soft reset

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/amdgpu/gfx11: wrong regGCVM_L2_STATUS offset in gfx_v11_0_wait_gc_idle() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/amdgpu/gfx11: wrong regGCVM_L2_STATUS offset in gfx_v11_0_wait_gc_idle()
--

commit b092d369a399e18251a9318c45400741bdc05ea3
Author: Jesse Zhang <[email protected]>

drm/amdgpu/gfx11: include SOFT_RESET_GFX in the gfx engine soft reset

This commit halts MES and RLC F32, waits for in-flight GC memory requests to drain in gfx_v11_0_wait_gc_idle(), and pulses SOFT_RESET_GFX together with SOFT_RESET_CPF and SOFT_RESET_CPG during a gfx engine soft reset.

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +35 -0
index a0ed3363f84d1..824d7914bece9 100644
@@ -6827,15 +6827,74 @@ static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring)
 				 CP_STAT__UTCL2IU_BUSY_MASK)
 
 /* Reset CPF/CPG, PFP/ME and the gfx HQDs, then boot PFP/ME again. */
+/*
+ * SOFT_RESET_GFX drops the memory requests in flight through the blocks it
+ * resets, and their clients then wait for them forever: MES still fetches
+ * instructions with its queues suspended, and RLC still writes its DRAM log
+ * in safe mode. The reset halts both, then waits for these busy bits of the
+ * GC memory interfaces to clear.
+ */
+#define GFX11_GC_MEM_BUSY	(GRBM_STATUS2__EA_BUSY_MASK | \
+				 GRBM_STATUS2__EA_LINK_BUSY_MASK | \
+				 GRBM_STATUS2__UTCL2_BUSY_MASK | \
+				 GRBM_STATUS2__UTCL2_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__TCP_BUSY_MASK | \
+				 GRBM_STATUS2__CPC_BUSY_MASK | \
+				 GRBM_STATUS2__RLC_BUSY_MASK | \
+				 GRBM_STATUS2__RLC_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA_BUSY_MASK | \
+				 GRBM_STATUS2__SDMA_SCH_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA0_RQ_PENDING_MASK | \
+				 GRBM_STATUS2__SDMA1_RQ_PENDING_MASK)
+
+/* The RLC busy bits of its memory requests */
+#define GFX11_RLC_MEM_BUSY	(RLC_STAT__MC_BUSY_MASK | \
+				 RLC_STAT__RLC_SRM_BUSY_MASK | \
+				 RLC_STAT__RLC_SPM_BUSY_MASK)
+
+/* Wait for the requests in flight in the GC, with MES and RLC F32 halted. */
+static int gfx_v11_0_wait_gc_idle(struct amdgpu_device *adev)
+{
+	u32 busy, cpc, rlc;
+	int i;
+
+	for (i = 0; i < adev->usec_timeout; i++) {
+		busy = RREG32_SOC15(GC, 0, regGRBM_STATUS2) & GFX11_GC_MEM_BUSY;
+		cpc = RREG32_SOC15(GC, 0, regCP_CPC_STATUS);
+		rlc = RREG32_SOC15(GC, 0, regRLC_STAT);

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring) · line 6874

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +10 -0
+
+		/* The halted MES and F32 can stay busy, not their memory requests. */
+		if (!(cpc & GENMASK(21, 0) & ~CP_CPC_STATUS__MES_BUSY_MASK))
+			busy &= ~GRBM_STATUS2__CPC_BUSY_MASK;
+		if (!(rlc & GFX11_RLC_MEM_BUSY))
+			busy &= ~GRBM_STATUS2__RLC_BUSY_MASK;
+
+		if (!busy && !(RREG32_SOC15(GC, 0, regGCVM_L2_STATUS) &
+			       GCVM_L2_STATUS__L2_BUSY_MASK))
+			return 0;

[Severity: Medium]
Does RREG32_SOC15(GC, 0, regGCVM_L2_STATUS) read the wrong MMIO offset on GC 11.0.3, GC 11.5.x and GC 11.7.x?

gfx_v11_0.c includes gc/gc_11_0_0_offset.h, where regGCVM_L2_STATUS is 0x15bf (BASE_IDX 0). On GC 11.0.3 (gc/gc_11_0_3_offset.h) and GC 11.5.x / 11.7.x (gc/gc_11_5_0_offset.h), the gc_gcvml2pfdec block is shifted by 4 dwords (which is why gmc_v11_0_set_gfxhub_funcs() selects gfxhub_v3_0_3_funcs and gfxhub_v11_5_0_funcs for those IP versions):
regGCVM_L2_CNTL is at 0x15c0 and regGCVM_L2_STATUS is at 0x15c3, leaving 0x15bf in an unmapped gap before regGCVM_L2_CNTL.

On those ASICs (such as GC 11.5.1 and GC 11.5.3 where gfx engine soft reset is enabled), gfx_v11_0_wait_gc_idle() will not poll the real GCVM_L2_STATUS register and can pulse SOFT_RESET_GFX while L2 requests are still in flight, or time out if the unmapped read returns all-ones.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#18 lore raw

PATCH v5 13/15 drm/amdgpu/userq: reset the gfx engine when a gfx user queue reset fails

In reply to Jesse Zhang

When neither the queue nor the pipe reset recovered a gfx user queue, fall back to a soft reset of the gfx engine before a full reset, when the GFX IP supports it. A hung kernel gfx queue gets there through the IP block soft reset of the job timeout handler; a user queue has no kernel ring, so call amdgpu_device_ip_soft_reset() directly, with the gfx ring type, the master XCC of the queue partition as the engine instance, and no guilty ring.

The hung queue can't be unmapped before the reset: pass it to the engine reset, which unmaps it once the reset cleared its HQD, and hangs it, while the other gfx user queues keep their state.

v4: Call amdgpu_device_ip_soft_reset(), which now takes the ring type

    (Timur).

v5: Pass the XCC of the queue partition as the engine instance (Lijo).

---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 42 ++++++++++++++++++++--
 1 file changed, 39 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +39 -3
index 7abb9d95ef59..8d0cd4575844 100644
@@ -235,11 +235,40 @@ int mes_userq_reset_hw(struct amdgpu_usermode_queue *queue,
 	return 0;
 }
 
+/* The master XCC of the queue partition */
+static u32 mes_userq_gfx_inst(struct amdgpu_usermode_queue *queue)
+{
+	struct amdgpu_device *adev = queue->userq_mgr->adev;
+	u32 xcc_mask;
+
+	if (!adev->xcp_mgr ||
+	    amdgpu_xcp_get_inst_details(&adev->xcp_mgr->xcp[queue->xcp_id],
+					AMDGPU_XCP_GFX, &xcc_mask) || !xcc_mask)
+		return 0;
+
+	return ffs(xcc_mask) - 1;
+}
+
+static int mes_userq_reset_gfx_engine(struct amdgpu_usermode_queue *queue)
+{
+	return amdgpu_device_ip_soft_reset(queue->userq_mgr->adev,
+					   AMDGPU_RING_TYPE_GFX,
+					   mes_userq_gfx_inst(queue), NULL,
+					   NULL, queue);
+}
+
+static bool mes_userq_gfx_engine_reset_supported(struct amdgpu_device *adev)
+{
+	return adev->gfx.gfx_supported_reset &
+	       AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET;
+}
+
 int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
 	struct amdgpu_device *adev = uq_mgr->adev;
 	struct mes_reset_queue_input queue_input;
+	int r;
 
 	/* already reset by an earlier job's hang-detect; just signal and bail */
 	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
@@ -248,8 +277,10 @@ int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 	/* A GFX pipe reset also affects the kernel rings on that pipe. */
 	if (queue->queue_type == AMDGPU_HW_IP_GFX &&
 	    adev->gfx.funcs->detect_hung_queue &&
-	    amdgpu_gfx_me_pipe_reset_supported(adev))
-		return amdgpu_gfx_reset_mes_gfx(adev, NULL, NULL, queue, 0, true);
+	    amdgpu_gfx_me_pipe_reset_supported(adev)) {
+		r = amdgpu_gfx_reset_mes_gfx(adev, NULL, NULL, queue, 0, true);
+		goto out;
+	}
 
 	memset(&queue_input, 0x0, sizeof(struct mes_reset_queue_input));
 	queue_input.doorbell_offset = queue->doorbell_index;
@@ -277,7 +308,12 @@ int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 		}
 	}
 
-	return mes_userq_reset_hw(queue, &queue_input);
+	r = mes_userq_reset_hw(queue, &queue_input);
+out:
+	if (r && queue->queue_type == AMDGPU_HW_IP_GFX &&
+	    mes_userq_gfx_engine_reset_supported(adev))
+		r = mes_userq_reset_gfx_engine(queue);
+	return r;
 }
 
 int mes_userq_reset_queue(struct amdgpu_device *adev,
Signature · 1 line
2.50.1
#19 lore raw

Re: [PATCH v5 13/15] drm/amdgpu/userq: reset the gfx engine when a gfx user queue reset fails

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/amdgpu/userq: mes_userq_reset() fallback overwrites ring_backup with NOPs - [High]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/amdgpu/userq: mes_userq_reset() fallback overwrites ring_backup with NOPs
- [High] drm/amdgpu/userq: UAF and race in mes_userq_reset_gfx_engine() under down_read
--

commit bc5fd65f8048fdf05e24850897e014a130300c03
Author: Jesse Zhang <[email protected]>

drm/amdgpu/userq: reset the gfx engine when a gfx user queue reset fails

This commit falls back to a soft reset of the GFX engine via amdgpu_device_ip_soft_reset() when a GFX user queue or pipe reset fails.

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +11 -0
index 7abb9d95ef59a..8d0cd45758444 100644
@@ -235,11 +235,40 @@ int mes_userq_reset_hw(struct amdgpu_usermode_queue *queue,
 	return 0;
 }
 
+/* The master XCC of the queue partition */
+static u32 mes_userq_gfx_inst(struct amdgpu_usermode_queue *queue)
+{
+	struct amdgpu_device *adev = queue->userq_mgr->adev;
+	u32 xcc_mask;
+
+	if (!adev->xcp_mgr ||
+	    amdgpu_xcp_get_inst_details(&adev->xcp_mgr->xcp[queue->xcp_id],
+					AMDGPU_XCP_GFX, &xcc_mask) || !xcc_mask)
+		return 0;
+

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c int mes_userq_reset_hw(struct amdgpu_usermode_queue *queue, · line 258

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +10 -0
+	return ffs(xcc_mask) - 1;
+}
+
+static int mes_userq_reset_gfx_engine(struct amdgpu_usermode_queue *queue)
+{
+	return amdgpu_device_ip_soft_reset(queue->userq_mgr->adev,
+					   AMDGPU_RING_TYPE_GFX,
+					   mes_userq_gfx_inst(queue), NULL,
+					   NULL, queue);
+}

[Severity: High]
Can mes_userq_reset_gfx_engine() race with concurrent user queue teardown or eviction and cause a use-after-free on collateral queues in adev->userq_doorbell_xa?

Because GFX v11 and v12 implement soft_reset_engine, amdgpu_device_ip_soft_reset() takes only down_read(&adev->reset_domain->sem) instead of the exclusive reset domain write lock. Inside soft_reset_engine, amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() walk adev->userq_doorbell_xa with xa_for_each() without taking xa_lock, pinning queue->refcount, or locking queue->userq_mgr->userq_mutex. Other user queue paths such as amdgpu_userq_destroy() and eviction/restore workers also only take down_read(&adev->reset_domain->sem):

CPU0 CPU1
mes_userq_reset_gfx_engine()
amdgpu_device_ip_soft_reset()

    down_read(&reset_domain->sem)
    ...
      amdgpu_userq_restore_hqds()
        xa_for_each(...)
                                      amdgpu_userq_destroy(queue)
                                        amdgpu_userq_detach_doorbell()
                                          down_read(&reset_domain->sem)
                                          xa_erase(&userq_doorbell_xa, ...)
                                          up_read(&reset_domain->sem)
                                        ...
                                        kfree(queue)
          userq_funcs->unmap(queue)  <-- use-after-free
Quoted 31 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +12 -2
+
+static bool mes_userq_gfx_engine_reset_supported(struct amdgpu_device *adev)
+{
+	return adev->gfx.gfx_supported_reset &
+	       AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET;
+}
+
 int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
 	struct amdgpu_device *adev = uq_mgr->adev;
 	struct mes_reset_queue_input queue_input;
+	int r;
 
 	/* already reset by an earlier job's hang-detect; just signal and bail */
 	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
@@ -248,8 +277,10 @@ int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 	/* A GFX pipe reset also affects the kernel rings on that pipe. */
 	if (queue->queue_type == AMDGPU_HW_IP_GFX &&
 	    adev->gfx.funcs->detect_hung_queue &&
-	    amdgpu_gfx_me_pipe_reset_supported(adev))
-		return amdgpu_gfx_reset_mes_gfx(adev, NULL, NULL, queue, 0, true);
+	    amdgpu_gfx_me_pipe_reset_supported(adev)) {
+		r = amdgpu_gfx_reset_mes_gfx(adev, NULL, NULL, queue, 0, true);
+		goto out;
+	}
 
 	memset(&queue_input, 0x0, sizeof(struct mes_reset_queue_input));
 	queue_input.doorbell_offset = queue->doorbell_index;
@@ -277,7 +308,12 @@ int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 		}

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c int mes_userq_reset(struct amdgpu_usermode_queue *queue) · line 317

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -1
 	}
 
-	return mes_userq_reset_hw(queue, &queue_input);
+	r = mes_userq_reset_hw(queue, &queue_input);
+out:
+	if (r && queue->queue_type == AMDGPU_HW_IP_GFX &&
+	    mes_userq_gfx_engine_reset_supported(adev))
+		r = mes_userq_reset_gfx_engine(queue);
+	return r;
 }

[Severity: High]
When amdgpu_gfx_reset_mes_gfx() fails after clearing a collateral kernel GFX ring and jumps to out:, does falling back to mes_userq_reset_gfx_engine() overwrite that ring's ring_backup[] with NOPs?

During amdgpu_gfx_reset_mes_gfx(adev, NULL, NULL, queue, 0, true), amdgpu_gfx_pipe_reset_prepare() calls amdgpu_ring_reset_helper_begin(cring, NULL) on collateral kernel GFX rings with pending fences, saving their unprocessed commands into cring->ring_backup[]. When pipe reset restores the kernel queue MQD via amdgpu_gfx_mqd_reset_restore(), it resets cring->wptr to 0 and fills cring->ring[] with NOPs. If a subsequent step in amdgpu_gfx_reset_mes_gfx() fails before amdgpu_ring_reset_helper_end() re-emits cring->ring_backup[] (for example in mes_userq_unmap(), amdgpu_mes_resume(), or amdgpu_ring_test_ring()), execution jumps to out:
and calls mes_userq_reset_gfx_engine(), which invokes amdgpu_device_ip_soft_reset() with guilty_ring = NULL:

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c:amdgpu_multi_ring_reset_helper_begin() {
    ...
		if (ring != guilty_ring || ring->guilty_fence != guilty_fence)
			amdgpu_ring_reset_helper_begin(ring, ring_guilty_fence);
    ...
}

Because guilty_ring is NULL, ring != guilty_ring is true for every kernel GFX ring, so amdgpu_ring_reset_helper_begin() runs a second time on cring and copies the NOP-filled cring->ring[] over cring->ring_backup[]. When the engine soft reset finishes, replaying cring->ring_backup[] emits only NOPs and all pending fences on the kernel GFX ring never signal.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#20 lore raw

PATCH v5 14/15 drm/amdgpu/gfx12: enable the gfx engine soft reset

In reply to Jesse Zhang

Report AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET for the gfx rings with MES, which the reset uses to map the kernel gfx queue again. Not on SR-IOV VFs.

The kernel gfx queue and gfx user queue resets then fall back to a soft reset of the gfx engine before a full reset.

v4: The soft_reset_engine() callback is set by the patch that adds it.
No async gfx ring check: they are always used now.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +4 -0
index 8485d1aabf3b..4b947f9416c5 100644
@@ -1601,6 +1601,10 @@ static int gfx_v12_0_sw_init(struct amdgpu_ip_block *ip_block)
 		break;
 	}
 
+	if (!amdgpu_sriov_vf(adev) && adev->enable_mes &&
+	    !adev->debug_disable_ip_block_soft_reset)
+		adev->gfx.gfx_supported_reset |= AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET;
+
 	if (!adev->enable_mes_kiq) {
 		r = amdgpu_gfx_kiq_init(adev, GFX12_MEC_HPD_SIZE, 0);
 		if (r) {
Signature · 1 line
2.50.1
#21 lore raw

PATCH v5 15/15 drm/amdgpu/gfx11: enable the gfx engine soft reset

In reply to Jesse Zhang

Report AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET for the gfx rings with MES, which the reset uses to map the kernel gfx queue again. Not on SR-IOV VFs.

The kernel gfx queue and gfx user queue resets then fall back to a soft reset of the gfx engine before a full reset. The IP block soft reset of gfx11 only resets the gfx engine then; the whole IP block soft_reset() isn't used.

Only on GC 11.5.1 (Strix Halo) and 11.5.3 (Krackan) for now, where the reset was verified to leave running compute work alone. On GC 11.0.0 (Navi31) SOFT_RESET_GFX resets the compute wave crawlers of SPI while the event/wave counters of CPC keep their value, and compute work that was running stops making progress. On GC 11.0.1 and 11.0.4 (Phoenix) PFP doesn't go idle after the reset. The other gfx11 IPs weren't tested.

v4: Not limited to the RS64 firmware anymore, the reset restarts the F32
PFP and ME of the APUs too (Timur). The soft_reset_engine() callback is set by the patch that adds it. No async gfx ring check: they are always used now.
v6: Only enable it on GC 11.5.1 and 11.5.3.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +7 -0
index 824d7914bece..a878aaee1b74 100644
@@ -1965,6 +1965,13 @@ static int gfx_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 		break;
 	}
 
+	/* Only on the GC versions it was verified on */
+	if ((amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 5, 1) ||
+	     amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 5, 3)) &&
+	    !amdgpu_sriov_vf(adev) && adev->enable_mes &&
+	    !adev->debug_disable_ip_block_soft_reset)
+		adev->gfx.gfx_supported_reset |= AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET;
+
 	if (!adev->enable_mes_kiq) {
 		r = amdgpu_gfx_kiq_init(adev, GFX11_MEC_HPD_SIZE, 0);
 		if (r) {
Signature · 1 line
2.50.1
#22 lore raw

Re: [PATCH v5 15/15] drm/amdgpu/gfx11: enable the gfx engine soft reset

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/amdgpu: user queue race and UAF during gfx_v11_0_soft_reset_engine() - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/amdgpu: user queue race and UAF during gfx_v11_0_soft_reset_engine()
- [Medium] drm/amdgpu/gfx11: missing pre-pulse CP_VMID_RESET clear in soft reset
- [Medium] drm/amdgpu/gfx11: kernel gfx queue left mapped when disable_kq is true
--

commit 37d8357ec5fb17ab702d211aa8fb7b20ac9e9a2a
Author: Jesse Zhang <[email protected]>

drm/amdgpu/gfx11: enable the gfx engine soft reset

This commit enables AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET for GFX rings on GC 11.5.1 and 11.5.3 when MES is enabled and SR-IOV VF is not active.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
index 824d7914bece9..a878aaee1b74b 100644
@@ -1965,6 +1965,13 @@ static int gfx_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 		break;
 	}
 

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static int gfx_v11_0_sw_init(struct amdgpu_ip_block *ip_block) · line 1977

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +7 -0
+	/* Only on the GC versions it was verified on */
+	if ((amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 5, 1) ||
+	     amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 5, 3)) &&
+	    !amdgpu_sriov_vf(adev) && adev->enable_mes &&
+	    !adev->debug_disable_ip_block_soft_reset)
+		adev->gfx.gfx_supported_reset |= AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET;
+
 	if (!adev->enable_mes_kiq) {
 		r = amdgpu_gfx_kiq_init(adev, GFX11_MEC_HPD_SIZE, 0);
 		if (r) {

[Severity: High]
Can enabling engine soft reset cause a use-after-free on struct amdgpu_usermode_queue in amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds()?

When funcs->soft_reset_engine is set, amdgpu_device_ip_soft_reset() only takes down_read(&adev->reset_domain->sem) before calling gfx_v11_0_soft_reset_engine(). Because amdgpu_userq_detach_doorbell(), amdgpu_userq_unmap_helper(), and amdgpu_userq_map_helper() also only take down_read(&adev->reset_domain->sem), they are not blocked during an engine soft reset.

Inside gfx_v11_0_soft_reset_engine(), amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() iterate adev->userq_doorbell_xa with xa_for_each() without taking a reference on queue or locking uq_mgr->userq_mutex:

CPU0 CPU1
amdgpu_device_ip_soft_reset()
down_read(&adev->reset_domain->sem)
gfx_v11_0_soft_reset_engine()

    amdgpu_userq_restore_hqds()
      xa_for_each(..., queue)
                                         amdgpu_userq_destroy()
                                           mutex_lock(&uq_mgr->userq_mutex)
                                           amdgpu_userq_detach_doorbell()
                                             down_read(&reset_domain->sem)
                                             xa_erase(&userq_doorbell_xa, ...)
                                           ...
                                           uq_funcs->mqd_destroy(queue)
                                           kfree(queue)
        userq_funcs->unmap(queue)
        ...                              <-- use-after-free

Concurrent eviction and restore workers (amdgpu_userq_evict_all() and amdgpu_userq_restore_all()) also only take a read lock on reset_domain->sem, so they can issue MES map/unmap requests and mutate queue->state while MES is suspended and amdgpu_userq_restore_hqds() is updating the queues without uq_mgr->userq_mutex.

[Severity: Medium]
Should gfx_v11_0_soft_reset_gfx_cp() clear regCP_VMID_RESET before pulsing regGRBM_SOFT_RESET, as gfx_v12_0_soft_reset_gfx_cp() does?

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c:gfx_v11_0_soft_reset_gfx_cp() {
	...
	/* A CP DMA in flight during the reset leaves the ME busy after it. */
	for (i = 0; i < adev->usec_timeout; i++) {
		if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX11_CP_STAT_MEM_BUSY))
			break;
		udelay(1);\
	}

	/* GFX_ONLY keeps compute and MES out; CPF alone leaves the stream stuck. */
	cntl = RREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL);
	...
}

When a prior per-queue or per-pipe reset via mes_v11_0_reset_queue_mmio() times out because PFP or ME is hung, the VMID reset request written to regCP_VMID_RESET remains pending when fallback reaches gfx_v11_0_soft_reset_gfx_cp(). gfx_v11_0_soft_reset_gfx_cp() only zeroes regCP_VMID_RESET after the reset pulse, leaving the VMID reset active during the CP soft reset and causing the soft reset to fail and escalate to a full GPU reset.

[Severity: Medium]
Does gfx_v11_0_soft_reset_engine() leave the kernel GFX queue mapped when adev->gfx.disable_kq is true?

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c:gfx_v11_0_soft_reset_engine() {
	...
	/* The reset cleared the HQD of the kernel gfx queue: map it again. */
	r = amdgpu_gfx_disable_kgq(adev, 0);
	if (r)
		goto fail;
	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
		amdgpu_gfx_mqd_reset_restore(&adev->gfx.gfx_ring[i]);
	r = amdgpu_gfx_enable_kgq(adev, 0);
	if (!r)
		r = gfx_v11_0_cp_gfx_start(adev);
	...
}

On GFX11, adev->gfx.num_gfx_rings is 1 even when adev->gfx.disable_kq is true so gfx_ring[0] can be used to submit clear-state initialization packets, after which gfx_v11_0_cp_resume() unmaps it via amdgpu_gfx_disable_kgq(adev, 0) so MES can use pipe 0 queue 0 for user queues. When a hung GFX user queue triggers gfx_v11_0_soft_reset_engine() with disable_kq set, gfx_ring[0] is mapped with amdgpu_gfx_enable_kgq() and never unmapped afterward, leaving the legacy kernel queue mapped on pipe 0 queue 0 in conflict with MES user queues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15