Skip to contentSkip to conversation

Use high priority scheduler workqueue for high priority rings

RFC New Tvrtko Ursulin 1 participant 5 messages Dec 1

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

v4 cover Use high priority scheduler workqueue for high priority rings

Last patch is the headline item while three preceding ones are hopefully fixes for problems I stumbled upon while adding WQ_HIGHPRI. On those three more eyes are definitely required since the init paths can be tricky.

Smoke tested on the Steam Deck and no explosions.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Timur Kristóf <[email protected]>

v2:
* Lets see if I fixed the sashiko bugs.. change log in patch 2.

v3:
* Fixed more sashiko reported issues in patch 2.

v4:
* New patch in the series to cleanup unused function return code.
* Squashed old patches 2 and 3 into 3 because the flow is much clearer, and fixed more sashiko reported bugs. :(

Tvrtko Ursulin (4):
drm/amdgpu: Free trail_fence_offs when freeing rings drm/amdgpu: Remove return value from amdgpu_fence_driver_start_ring() drm/amdgpu: Cleanup on ring failure properly
drm/amdgpu: Use high priority scheduler workqueue for high priority
rings

 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  | 60 ++++++++------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 96 ++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 11 ++-
 4 files changed, 108 insertions(+), 62 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

RFC v4 1/4 drm/amdgpu: Free trail_fence_offs when freeing rings

Looks like ring->trail_fence_offs writeback was never freed at ring destruction time. Free it in amdgpu_ring_fini() the same way as other offsets are.

Fixes ef3e13238b5f ("drm/amdgpu: add the trailing fence per ring")
Cc Hawking Zhang <[email protected]>
Cc Jack Xiao <[email protected]>
Cc Alex Deucher <[email protected]>
Cc "Timur Kristóf" <[email protected]>
Cc "Christian König" <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -0
index 7b67e8cecca2..71cabb884ac2 100644
@@ -386,6 +386,7 @@ void amdgpu_ring_fini(struct amdgpu_ring *ring)
 	amdgpu_wb_free(ring->adev, ring->wptr_offs);
 
 	amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
+	amdgpu_wb_free(ring->adev, ring->trail_fence_offs);
 	amdgpu_wb_free(ring->adev, ring->fence_offs);
 
 	amdgpu_bo_free_kernel(&ring->ring_obj,
Signature · 1 line
2.56.0
#2 lore raw

RFC v4 2/4 drm/amdgpu: Remove return value from amdgpu_fence_driver_start_ring()

In reply to Tvrtko Ursulin

amdgpu_fence_driver_start_ring() cannot fail so lets remove the return value from it.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Timur Kristóf <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 8 +++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c  | 6 +-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h  | 6 +++---
 3 files changed, 7 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +3 -5
index 58219aa04013..abef410979e3 100644
@@ -402,11 +402,10 @@ void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
  * Make the fence driver ready for processing (all asics).
  * Not all asics have all rings, so each asic will only
  * start the fence driver on the rings it has.
- * Returns 0 for success, errors for failure.
  */
-int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
-				   struct amdgpu_irq_src *irq_src,
-				   unsigned int irq_type)
+void amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
+				    struct amdgpu_irq_src *irq_src,
+				    unsigned int irq_type)
 {
 	struct amdgpu_device *adev = ring->adev;
 	uint64_t index;
@@ -428,7 +427,6 @@ int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
 
 	DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr 0x%016llx\n",
 		      ring->name, ring->fence_drv.gpu_addr);
-	return 0;
 }
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -5
index 71cabb884ac2..ea842d9da680 100644
@@ -305,11 +305,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	*ring->cond_exe_cpu_addr = 1;
 
 	if (ring->funcs->type != AMDGPU_RING_TYPE_CPER) {
-		r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
-		if (r) {
-			dev_err(adev->dev, "failed initializing fences (%d).\n", r);
-			return r;
-		}
+		amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
 
 		max_ibs_dw = ring->funcs->emit_frame_size +
 			     amdgpu_ring_max_ibs(ring->funcs->type) * ring->funcs->emit_ib_size;
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +3 -3
index b2040f80b7f2..81d0d1a0301b 100644
@@ -163,9 +163,9 @@ void amdgpu_ring_set_fence_errors_and_reemit(struct amdgpu_ring *ring,
 					     struct amdgpu_fence *guilty_fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
-int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
-				   struct amdgpu_irq_src *irq_src,
-				   unsigned irq_type);
+void amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
+				    struct amdgpu_irq_src *irq_src,
+				    unsigned irq_type);
 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev);
 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev);
 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev);
Signature · 1 line
2.56.0
#3 lore raw

RFC v4 3/4 drm/amdgpu: Cleanup on ring failure properly

In reply to Tvrtko Ursulin

Currently if ring init fails not all, or to better say not many, objects allocated before the failure point get freed. Failed ring can also free the writeback offset 0 it did not allocate. Since amdgpu_ring_fini() already somewhat handles the cleanup of partially constructed rings, lets make it handle it fully and call it when ring init fails.

This involves marking the unallocated writeback slots so only allocated ones can be freed and exporting new amdgpu_fence_driver_fini_ring() as a counterpart to existing amdgpu_fence_driver_init_ring().

We also remove the conditional part of the init process which was present ever since amdgpu was first added in commit
d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
where amdgpu_ring_init() was doing a subset of initialization steps based on the ring->adev being NULL or not.

I am not sure what was the situation back then, but certainly in todays code base I cannot see that any caller sets ring->adev to something before calling amdgpu_ring_init(). Furthermore, I do not see how that would be safe and sensible because it would leak objects which were already initialized.

Lets just remove this special case and as a bonus only "publish" the ring to global struct amdgpu_device once it has been successfully constructed.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Timur Kristóf <[email protected]>

---
v2:
* Cleanup the fence driver.
* Do not double free the writeback slots.
* Return -ENOMEM when ring backup allocation fails.

v3:
* Always cleanup fence_drv.
* Mark writeback slots as un-allocated earlier.

v4:
* Track drm_sched_init called status explicitly.
* Check fences array has been allocated before dereferencing.
* Squashed with the conditional part-init removal patch.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  | 47 ++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 81 +++++++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |  4 ++
 4 files changed, 84 insertions(+), 50 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
index d0b08d2daea2..499ed9a3ba23 100644
@@ -2375,6 +2375,8 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
 				ring->name);
 			return r;
 		}
+		ring->sched_initialized = true;
+
 		r = amdgpu_uvd_entity_init(adev, ring);
 		if (r) {
 			dev_err(adev->dev,
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +29 -18

60 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +49 -32

159 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -0
index 81d0d1a0301b..cc91fe05ceaa 100644
@@ -66,6 +66,8 @@ enum amdgpu_ring_priority_level {
 /* Ensure the execution in case of preemption or reset */
 #define AMDGPU_FENCE_FLAG_EXEC          (1 << 3)
 
+#define AMDGPU_RING_WB_INVALID (~0u)
+
 #define to_amdgpu_ring(s) container_of((s), struct amdgpu_ring, sched)
 
 enum amdgpu_ring_type {
@@ -163,6 +165,7 @@ void amdgpu_ring_set_fence_errors_and_reemit(struct amdgpu_ring *ring,
 					     struct amdgpu_fence *guilty_fence);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
+void amdgpu_fence_driver_fini_ring(struct amdgpu_ring *ring);
 void amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
 				    struct amdgpu_irq_src *irq_src,
 				    unsigned irq_type);
@@ -311,6 +314,7 @@ struct amdgpu_ring {
 	/* backups for resets */
 	uint32_t		*ring_backup;
 	unsigned int		ring_backup_entries_to_copy;
+	bool			sched_initialized;
 	bool			reemit;
 	struct amdgpu_fence	*guilty_fence;
 	unsigned		rptr_offs;
Signature · 1 line
2.56.0
#4 lore raw

RFC v4 4/4 drm/amdgpu: Use high priority scheduler workqueue for high priority rings

In reply to Tvrtko Ursulin

Back when DRM scheduler was kthread based the submission threads were using SCHED_FIFO to minimize the delays in feeding the GPU. This was lost with the conversion to workqueues but has recently been shown on other drivers (panthor, xe) that modern userspace can still suffer severe submission latencies under CPU load.

Lets convert amdgpu to use own workqueues and make the high priority rings use WQ_HIGHPRI while at it.

This is not expected to be a significant latency improvement, but it paves the way to start using WQ_RT which is queued for 7.5. At that point we can switch to using it with a single line patch and bring submission latencies under CPU load back to what they were in the past.

References: a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
References: https://gitlab.freedesktop.org/panfrost/linux/-/work_items/49
References: https://lore.kernel.org/dri-devel/[email protected]/
References: https://lore.kernel.org/dri-devel/[email protected]/
Cc: Alex Deucher <[email protected]>
Cc: Christian König <[email protected]>
Cc: Timur Kristóf <[email protected]>
---
v2:
* Free the workqueue if it has been allocated.

v3:
* Prevent double destroy_workqueue.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  5 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 10 ++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |  1 +
 4 files changed, 17 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index 499ed9a3ba23..9b007ba6c05c 100644
@@ -2367,6 +2367,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
 		args.credit_limit = ring->num_hw_submission;
 		args.score = ring->sched_score;
 		args.name = ring->name;
+		args.submit_wq = ring->sched_wq;
 
 		r = drm_sched_init(&ring->sched, &args);
 		if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +5 -0
index c58d7c72eb30..52a15ac0fc0b 100644
@@ -602,6 +602,11 @@ void amdgpu_fence_driver_fini_ring(struct amdgpu_ring *ring)
 		ring->fence_drv.fences = NULL;
 	}
 
+	if (ring->sched_wq) {
+		destroy_workqueue(ring->sched_wq);
+		ring->sched_wq = NULL;
+	}
+
 	ring->fence_drv.initialized = false;
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +10 -0
index 692ed3bc3fbf..a1d8d59d5aa9 100644
@@ -357,6 +357,16 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	if (!ring->no_scheduler && ring->funcs->type < AMDGPU_HW_IP_NUM) {
 		struct amdgpu_sched *gpu_sched =
 			&adev->gpu_sched[ring->funcs->type][hw_prio];
+		unsigned int flags = WQ_MEM_RECLAIM;
+
+		if (hw_prio > AMDGPU_RING_PRIO_DEFAULT)
+			flags |= WQ_HIGHPRI;
+
+		ring->sched_wq = alloc_ordered_workqueue(ring->name, flags);
+		if (!ring->sched_wq) {
+			r = -ENOMEM;
+			goto err;
+		}
 
 		gpu_sched->sched[gpu_sched->num_scheds++] = &ring->sched;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -0
index cc91fe05ceaa..1f2c48623033 100644
@@ -309,6 +309,7 @@ struct amdgpu_ring {
 	struct amdgpu_fence_driver	fence_drv;
 	struct drm_gpu_scheduler	sched;
 
+	struct workqueue_struct	*sched_wq;
 	struct amdgpu_bo	*ring_obj;
 	uint32_t		*ring;
 	/* backups for resets */
Signature · 1 line
2.56.0