Skip to contentSkip to conversation

Use high priority scheduler workqueue for high priority rings

RFC Superseded Tvrtko Ursulin 2 participants 9 messages Nov 30

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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]>

Tvrtko Ursulin (4):
drm/amdgpu: Free trail_fence_offs when freeing rings drm/amdgpu: Cleanup on ring failure
drm/amdgpu: Unconfuse amdgpu_ring_init()
drm/amdgpu: Use high priority scheduler workqueue for high priority
rings

 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 68 +++++++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |  1 +
 4 files changed, 45 insertions(+), 29 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

RFC 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 2/4 drm/amdgpu: Cleanup on ring failure

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.

amdgpu_ring_fini() already handles cleanup of partially constructed rings so lets just call it when things fail.

To be safe against multiple calls make sure ring_obj cannot be freed twice.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Cc Timur Kristóf <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 28 ++++++++++++++----------
 1 file changed, 16 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +16 -12
index 71cabb884ac2..92eb00fdc2a1 100644
@@ -249,31 +249,31 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	r = amdgpu_wb_get(adev, &ring->rptr_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->wptr_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->fence_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->trail_fence_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring trail_fence_offs wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->cond_exe_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	ring->fence_gpu_addr =
@@ -308,7 +308,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 		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;
+			goto err;
 		}
 
 		max_ibs_dw = ring->funcs->emit_frame_size +
@@ -333,7 +333,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	if (!ring->ring_backup) {
 		ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL);
 		if (!ring->ring_backup)
-			return -ENOMEM;
+			goto err;
 	}
 
 	/* Allocate ring buffer */
@@ -344,11 +344,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 					    &ring->ring_obj,
 					    &ring->gpu_addr,
 					    (void **)&ring->ring);
-		if (r) {
-			dev_err(adev->dev, "(%d) ring create failed\n", r);
-			kvfree(ring->ring_backup);
-			return r;
-		}
+		if (r)
+			goto err;
+
 		amdgpu_ring_clear_ring(ring);
 	}
 
@@ -364,6 +362,10 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	}
 
 	return 0;
+
+err:
+	amdgpu_ring_fini(ring);
+	return r;
 }
 
 /**
@@ -392,6 +394,8 @@ void amdgpu_ring_fini(struct amdgpu_ring *ring)
 	amdgpu_bo_free_kernel(&ring->ring_obj,
 			      &ring->gpu_addr,
 			      (void **)&ring->ring);
+	ring->ring_obj = NULL;
+
 	kvfree(ring->ring_backup);
 	ring->ring_backup = NULL;
 
Signature · 1 line
2.56.0
#3 lore raw

Re: [RFC 2/4] drm/amdgpu: Cleanup on ring failure

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/amdgpu: amdgpu_ring_init() returns 0 on ring_backup alloc failure - [Medium] drm/amdgpu…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/amdgpu: amdgpu_ring_init() returns 0 on ring_backup alloc failure
- [Medium] drm/amdgpu: amdgpu_ring_fini() frees WB slot 0 and double-frees WB slots
- [Medium] drm/amdgpu: incomplete fence_drv cleanup on error in amdgpu_ring_init()
--

commit 52137188f323e99fb1cb2c51c462dcc3580d61b7
Author: Tvrtko Ursulin <[email protected]>

drm/amdgpu: Cleanup on ring failure

This commit calls amdgpu_ring_fini() on the error path of amdgpu_ring_init() to clean up partially constructed rings, and clears ring->ring_obj in amdgpu_ring_fini().

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +3 -3
index 71cabb884ac20..92eb00fdc2a15 100644
@@ -249,31 +249,31 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	r = amdgpu_wb_get(adev, &ring->rptr_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->wptr_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->fence_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->trail_fence_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring trail_fence_offs wb alloc failed\n", r);

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 277

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +2 -2
-		return r;
+		goto err;
 	}
 
 	r = amdgpu_wb_get(adev, &ring->cond_exe_offs);
 	if (r) {
 		dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
-		return r;
+		goto err;
 	}

[Severity: Medium]
Can jumping to err when one of the amdgpu_wb_get() calls fails free writeback slot 0 out from under its owner, and double free all five slots when amdgpu_ring_fini() is called again during teardown?

Slot 0 in adev->wb.used is a valid writeback slot with offset 0, so when amdgpu_ring_fini() calls amdgpu_wb_free() on all five offsets, any offset that was not yet allocated is still 0 and clears bit 0 of adev->wb.used. Also, amdgpu_ring_fini() does not clear ring->adev or ring->adev->rings[ring->idx], so a later teardown call such as amdgpu_cper_fini() -> amdgpu_ring_fini() will run a second time and free all five writeback offsets again.

[ ... ]

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 337

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -1
@@ -333,7 +333,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	if (!ring->ring_backup) {
 		ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL);
 		if (!ring->ring_backup)
-			return -ENOMEM;
+			goto err;
 	}

[Severity: High]
Does amdgpu_ring_init() return 0 when allocating ring->ring_backup fails? At this point r holds 0 from the preceding call, so jumping to err tears down the ring via amdgpu_ring_fini() and returns success to the caller, leading to a NULL pointer dereference when commands are emitted to ring->ring.

Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +3 -5
 	/* Allocate ring buffer */
@@ -344,11 +344,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 					    &ring->ring_obj,
 					    &ring->gpu_addr,
 					    (void **)&ring->ring);
-		if (r) {
-			dev_err(adev->dev, "(%d) ring create failed\n", r);
-			kvfree(ring->ring_backup);
-			return r;
-		}
+		if (r)
+			goto err;
+
 		amdgpu_ring_clear_ring(ring);
 	}
 

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 369

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +4 -0
@@ -364,6 +362,10 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	}
 
 	return 0;
+
+err:
+	amdgpu_ring_fini(ring);
+	return r;
 }

[Severity: Medium]
Does the err path leave ring->fence_drv uncleaned when amdgpu_ring_init() fails after amdgpu_fence_driver_init_ring()?

amdgpu_ring_fini() does not free ring->fence_drv.fences or reset ring->fence_drv.initialized. If amdgpu_ring_init() fails before amdgpu_fence_driver_start_ring() (or after commit "drm/amdgpu: Unconfuse amdgpu_ring_init()" moves the adev->rings[ring->idx] assignment to the end of the function), amdgpu_fence_driver_sw_fini() skips the ring and leaks ring->fence_drv.fences; if it fails after amdgpu_fence_driver_start_ring() in this commit, the ring remains in adev->rings[] with fence_drv.initialized set and fence_drv.cpu_addr pointing to the freed ring->fence_offs writeback slot.

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

RFC 3/4 drm/amdgpu: Unconfuse amdgpu_ring_init()

In reply to Tvrtko Ursulin

Ever since amdgpu was first added in commit
d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)") 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.

References: d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
Cc: Alex Deucher <[email protected]>
Cc: Christian König <[email protected]>
Cc: Alex Deucher <[email protected]>
Cc: Timur Kristóf <[email protected]>

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 29 +++++++++++-------------
 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +13 -16
index 92eb00fdc2a1..2a96cb7d891b 100644
@@ -217,6 +217,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	unsigned int max_ibs_dw;
 	int r;
 
+	if (adev->num_rings >= AMDGPU_MAX_RINGS)
+		return -EINVAL;
+
 	/* Set the hw submission limit higher for KIQ because
 	 * it's used for a number of gfx/compute tasks by both
 	 * KFD and KGD which may have outstanding fences and
@@ -230,21 +233,13 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	else if (ring == &adev->sdma.instance[0].page)
 		sched_hw_submission = 256;
 
-	if (ring->adev == NULL) {
-		if (adev->num_rings >= AMDGPU_MAX_RINGS)
-			return -EINVAL;
+	ring->adev = adev;
+	ring->num_hw_submission = sched_hw_submission;
+	ring->sched_score = sched_score;
 
-		ring->adev = adev;
-		ring->num_hw_submission = sched_hw_submission;
-		ring->sched_score = sched_score;
-
-		ring->idx = adev->num_rings++;
-		adev->rings[ring->idx] = ring;
-
-		r = amdgpu_fence_driver_init_ring(ring);
-		if (r)
-			return r;
-	}
+	r = amdgpu_fence_driver_init_ring(ring);
+	if (r)
+		return r;
 
 	r = amdgpu_wb_get(adev, &ring->rptr_offs);
 	if (r) {
@@ -361,6 +356,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 		gpu_sched->sched[gpu_sched->num_scheds++] = &ring->sched;
 	}
 
+	ring->idx = adev->num_rings++;
+	adev->rings[ring->idx] = ring;
+
 	return 0;
 
 err:
@@ -378,8 +376,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 void amdgpu_ring_fini(struct amdgpu_ring *ring)
 {
 
-	/* Not to finish a ring which is not initialized */
-	if (!(ring->adev) || !(ring->adev->rings[ring->idx]))
+	if (!ring->adev)
 		return;
 
 	ring->sched.ready = false;
Signature · 1 line
2.56.0
#5 lore raw

Re: [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init()

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] drm/amdgpu: leak of ring->fence_drv.fences on amdgpu_ring_init() failure - [Medium] drm/amdgpu…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] drm/amdgpu: leak of ring->fence_drv.fences on amdgpu_ring_init() failure
- [Medium] drm/amdgpu: stale ring->adev allows repeated amdgpu_ring_fini() teardown
- [Medium] drm/amdgpu: amdgpu_ring_fini() frees WB slot 0 on partial WB init failure
- [Medium] drm/amdgpu: amdgpu_ring_init() returns 0 when ring_backup alloc fails
--

commit 30b04e0d84c8a20af2bf83c53f41bd8a634cd709
Author: Tvrtko Ursulin <[email protected]>

drm/amdgpu: Unconfuse amdgpu_ring_init()

This commit removes the ring->adev == NULL check in amdgpu_ring_init() and defers registering the ring in adev->rings[] until initialization succeeds.

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +9 -14
index 92eb00fdc2a15..2a96cb7d891b5 100644
@@ -217,6 +217,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	unsigned int max_ibs_dw;
 	int r;
 
+	if (adev->num_rings >= AMDGPU_MAX_RINGS)
+		return -EINVAL;
+
 	/* Set the hw submission limit higher for KIQ because
 	 * it's used for a number of gfx/compute tasks by both
 	 * KFD and KGD which may have outstanding fences and
@@ -230,21 +233,13 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	else if (ring == &adev->sdma.instance[0].page)
 		sched_hw_submission = 256;
 
-	if (ring->adev == NULL) {
-		if (adev->num_rings >= AMDGPU_MAX_RINGS)
-			return -EINVAL;
-
-		ring->adev = adev;
-		ring->num_hw_submission = sched_hw_submission;
-		ring->sched_score = sched_score;
-
-		ring->idx = adev->num_rings++;
-		adev->rings[ring->idx] = ring;
+	ring->adev = adev;
+	ring->num_hw_submission = sched_hw_submission;
+	ring->sched_score = sched_score;
 
-		r = amdgpu_fence_driver_init_ring(ring);
-		if (r)
-			return r;
-	}
+	r = amdgpu_fence_driver_init_ring(ring);
+	if (r)
+		return r;
 
 	r = amdgpu_wb_get(adev, &ring->rptr_offs);
 	if (r) {

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 364

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +3 -0
@@ -361,6 +356,9 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 		gpu_sched->sched[gpu_sched->num_scheds++] = &ring->sched;
 	}
 
+	ring->idx = adev->num_rings++;
+	adev->rings[ring->idx] = ring;
+
 	return 0;
 
 err:

[Severity: Medium]
Does deferring adev->rings[ring->idx] = ring leak ring->fence_drv.fences when amdgpu_ring_init() fails after amdgpu_fence_driver_init_ring()?

amdgpu_fence_driver_init_ring() allocates ring->fence_drv.fences, which is only freed by amdgpu_fence_driver_sw_fini() while iterating over adev->rings[]. Because a ring that fails at a later step (such as amdgpu_bo_create_kernel()) is no longer added to adev->rings[], and amdgpu_ring_fini() does not free ring->fence_drv.fences, that allocation is leaked on error unwind.

[Severity: Medium]
Also in amdgpu_ring_init(), commit "drm/amdgpu: Cleanup on ring failure" in this series changed the error paths to jump to err: and call amdgpu_ring_fini(). Can a failure in one of the five amdgpu_wb_get() calls free active writeback slot 0?

The first slot allocated by amdgpu_wb_get() has bit index 0 in adev->wb.used and offset 0. When an amdgpu_wb_get() call in amdgpu_ring_init() fails, any remaining unallocated offset fields in ring are still 0, and amdgpu_ring_fini() passes all five offsets to amdgpu_wb_free(), which shifts 0 >> 3 and clears bit 0 of adev->wb.used.

[Severity: Medium]
Similarly in the err: path added by commit "drm/amdgpu: Cleanup on ring failure", does amdgpu_ring_init() return 0 when kvzalloc() for ring->ring_backup fails?

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c:amdgpu_ring_init() {
    ...
	if (!ring->ring_backup) {
		ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL);
		if (!ring->ring_backup)
			goto err;
	}
    ...
err:
	amdgpu_ring_fini(ring);
	return r;
}

At that point r holds 0 from the preceding amdgpu_fence_driver_start_ring() or amdgpu_wb_get() call, so jumping to err tears down the ring and returns 0 to the caller with ring->ring_obj and ring->ring left NULL.

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 382

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -2
@@ -378,8 +376,7 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 void amdgpu_ring_fini(struct amdgpu_ring *ring)
 {
 
-	/* Not to finish a ring which is not initialized */
-	if (!(ring->adev) || !(ring->adev->rings[ring->idx]))
+	if (!ring->adev)
 		return;
 
 	ring->sched.ready = false;

[Severity: Medium]
If ring->adev is neither cleared in amdgpu_ring_fini() nor rolled back when amdgpu_fence_driver_init_ring() fails, can a subsequent amdgpu_ring_fini() call during device teardown run on an uninitialized or already finalized ring?

For example, amdgpu_device_ip_fini() unconditionally calls amdgpu_cper_fini() -> amdgpu_ring_fini(&adev->cper.ring_buf). If amdgpu_ring_init() failed, ring->adev remains non-NULL and the second amdgpu_ring_fini() call will re-run teardown and pass unallocated (0) or already freed writeback offsets to amdgpu_wb_free().

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

RFC 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]>

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  4 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 10 ++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |  1 +
 4 files changed, 15 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index d0b08d2daea2..7638e4230307 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 +3 -1
index 58219aa04013..318826a16803 100644
@@ -593,8 +593,10 @@ void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev)
 		 * The natural check would be sched.ready, which is
 		 * set as drm_sched_init() finishes...
 		 */
-		if (ring->sched.ops)
+		if (ring->sched.ops) {
 			drm_sched_fini(&ring->sched);
+			destroy_workqueue(ring->sched_wq);
+		}
 
 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
 			dma_fence_put(ring->fence_drv.fences[j]);
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +10 -0
index 2a96cb7d891b..3fb4b5340ef5 100644
@@ -352,6 +352,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 b2040f80b7f2..d2424b53af4e 100644
@@ -306,6 +306,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
#7 lore raw

Re: [RFC 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/amdgpu: sched_wq workqueue leak when drm_sched_init() is not called - [Medium] drm/amdgpu…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/amdgpu: sched_wq workqueue leak when drm_sched_init() is not called
- [Medium] drm/amdgpu: leak of ring->fence_drv.fences on amdgpu_ring_init() failure
--

commit 9a9b2f95cd45c2fb7c2ee758afeb1983b998825f
Author: Tvrtko Ursulin <[email protected]>

drm/amdgpu: Use high priority scheduler workqueue for high priority rings

This commit allocates a dedicated ordered submit workqueue per ring in amdgpu_ring_init() (using WQ_HIGHPRI for high-priority rings), passes it to drm_sched_init(), and destroys it in amdgpu_fence_driver_sw_fini().

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +0 -0
index d0a6901532fd6..1984e31627bbc 100644
@@ -593,8 +593,10 @@ void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev)
 		 * The natural check would be sched.ready, which is

drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev) · line 602

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +3 -1
 		 * set as drm_sched_init() finishes...
 		 */
-		if (ring->sched.ops)
+		if (ring->sched.ops) {
 			drm_sched_fini(&ring->sched);
+			destroy_workqueue(ring->sched_wq);
+		}
 
 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
 			dma_fence_put(ring->fence_drv.fences[j]);

[Severity: Medium]
Can gating destroy_workqueue(ring->sched_wq) on ring->sched.ops leak ring->sched_wq when drm_sched_init() is never called for the ring?

amdgpu_ring_init() allocates ring->sched_wq during IP block sw_init, while ring->sched.ops is only populated later by amdgpu_device_init_schedulers() at the end of amdgpu_device_ip_init(). On SR-IOV VFs with VCN 3.0, vcn_v3_0_hw_init() runs in between and sets ring->no_scheduler = true for hypervisor-disabled rings:

drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c:vcn_v3_0_hw_init() {
    ...
			if (amdgpu_vcn_is_disabled_vcn(adev, VCN_DECODE_RING, i)) {
				ring->sched.ready = false;
				ring->no_scheduler = true;
				dev_info(adev->dev, "ring %s is disabled by hypervisor\n", ring->name);
			}
    ...
}

amdgpu_device_init_schedulers() then skips the ring, leaving ring->sched.ops NULL (the same happens if amdgpu_device_ip_init() fails before amdgpu_device_init_schedulers() finishes), and neither amdgpu_fence_driver_sw_fini() nor amdgpu_ring_fini() destroys ring->sched_wq.

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +3 -0
index 2a96cb7d891b5..3fb4b5340ef52 100644
@@ -352,6 +352,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)

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 367

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +7 -0
+			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;
 	}

[Severity: Medium]
Does jumping to err when alloc_ordered_workqueue() fails leak ring->fence_drv.fences?

amdgpu_fence_driver_init_ring() allocates ring->fence_drv.fences at the start of amdgpu_ring_init(), but ring is only added to adev->rings[] after alloc_ordered_workqueue() succeeds:

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c:amdgpu_ring_init() {
    ...
	ring->idx = adev->num_rings++;
	adev->rings[ring->idx] = ring;

	return 0;

err:

	amdgpu_ring_fini(ring);
	return r;
}

amdgpu_ring_fini() does not free ring->fence_drv.fences, and amdgpu_fence_driver_sw_fini() only walks adev->rings[], so the fences array is leaked whenever amdgpu_ring_init() fails via err.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#8 lore raw
In reply to Tvrtko Ursulin
Quoted 5 lines · Tvrtko Ursulin

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.

Okay plenty of sashiko reported blunders. That's the thing in the AI world - why bother thinking when it will let you know. Sorry folks, I need to respin most of it - stay tuned. Although, what could be useful is feedback on the high level.

Tvrtko

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

Tvrtko Ursulin (4):
drm/amdgpu: Free trail_fence_offs when freeing rings drm/amdgpu: Cleanup on ring failure
drm/amdgpu: Unconfuse amdgpu_ring_init()
drm/amdgpu: Use high priority scheduler workqueue for high priority
rings

  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  |  4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 68 +++++++++++++---------
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |  1 +
  4 files changed, 45 insertions(+), 29 deletions(-)