* [RFC 1/4] drm/amdgpu: Free trail_fence_offs when freeing rings
2026-11-30 16:10 [RFC 0/4] Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
@ 2026-11-30 16:10 ` Tvrtko Ursulin
2026-11-30 16:10 ` [RFC 2/4] drm/amdgpu: Cleanup on ring failure Tvrtko Ursulin
` (3 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Tvrtko Ursulin @ 2026-11-30 16:10 UTC (permalink / raw)
To: amd-gfx
Cc: kernel-dev, Tvrtko Ursulin, Hawking Zhang, Jack Xiao,
Alex Deucher, Timur Kristóf, Christian König
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.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Fixes: ef3e13238b5f ("drm/amdgpu: add the trailing fence per ring")
Cc: Hawking Zhang <Hawking.Zhang@amd.com>
Cc: Jack Xiao <Jack.Xiao@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: "Timur Kristóf" <timur.kristof@gmail.com>
Cc: "Christian König" <christian.koenig@amd.com>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
index 7b67e8cecca2..71cabb884ac2 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
@@ -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,
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [RFC 2/4] drm/amdgpu: Cleanup on ring failure
2026-11-30 16:10 [RFC 0/4] Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
2026-11-30 16:10 ` [RFC 1/4] drm/amdgpu: Free trail_fence_offs when freeing rings Tvrtko Ursulin
@ 2026-11-30 16:10 ` Tvrtko Ursulin
2026-11-30 16:32 ` sashiko-bot
2026-11-30 16:10 ` [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init() Tvrtko Ursulin
` (2 subsequent siblings)
4 siblings, 1 reply; 9+ messages in thread
From: Tvrtko Ursulin @ 2026-11-30 16:10 UTC (permalink / raw)
To: amd-gfx
Cc: kernel-dev, Tvrtko Ursulin, Alex Deucher, Christian König,
Timur Kristóf
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.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Timur Kristóf <timur.kristof@gmail.com>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 28 ++++++++++++++----------
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
index 71cabb884ac2..92eb00fdc2a1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
@@ -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;
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [RFC 2/4] drm/amdgpu: Cleanup on ring failure
2026-11-30 16:10 ` [RFC 2/4] drm/amdgpu: Cleanup on ring failure Tvrtko Ursulin
@ 2026-11-30 16:32 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-30 16:32 UTC (permalink / raw)
To: Tvrtko Ursulin, amd-gfx
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 <tvrtko.ursulin@igalia.com>
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().
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> index 71cabb884ac20..92eb00fdc2a15 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> @@ -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;
> }
[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.
[ ... ]
> @@ -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.
>
> /* 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;
> }
[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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130161016.64542-1-tvrtko.ursulin@igalia.com?part=2
^ permalink raw reply [flat|nested] 9+ messages in thread
* [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init()
2026-11-30 16:10 [RFC 0/4] Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
2026-11-30 16:10 ` [RFC 1/4] drm/amdgpu: Free trail_fence_offs when freeing rings Tvrtko Ursulin
2026-11-30 16:10 ` [RFC 2/4] drm/amdgpu: Cleanup on ring failure Tvrtko Ursulin
@ 2026-11-30 16:10 ` Tvrtko Ursulin
2026-11-30 16:28 ` sashiko-bot
2026-11-30 16:10 ` [RFC 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
2026-11-30 19:50 ` [RFC 0/4] " Tvrtko Ursulin
4 siblings, 1 reply; 9+ messages in thread
From: Tvrtko Ursulin @ 2026-11-30 16:10 UTC (permalink / raw)
To: amd-gfx
Cc: kernel-dev, Tvrtko Ursulin, Alex Deucher, Christian König,
Timur Kristóf
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.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
References: d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Timur Kristóf <timur.kristof@gmail.com>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 29 +++++++++++-------------
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
index 92eb00fdc2a1..2a96cb7d891b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
@@ -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;
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init()
2026-11-30 16:10 ` [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init() Tvrtko Ursulin
@ 2026-11-30 16:28 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-30 16:28 UTC (permalink / raw)
To: Tvrtko Ursulin, amd-gfx
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 <tvrtko.ursulin@igalia.com>
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.
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> index 92eb00fdc2a15..2a96cb7d891b5 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> @@ -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) {
> @@ -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.
> @@ -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().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130161016.64542-1-tvrtko.ursulin@igalia.com?part=3
^ permalink raw reply [flat|nested] 9+ messages in thread
* [RFC 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings
2026-11-30 16:10 [RFC 0/4] Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
` (2 preceding siblings ...)
2026-11-30 16:10 ` [RFC 3/4] drm/amdgpu: Unconfuse amdgpu_ring_init() Tvrtko Ursulin
@ 2026-11-30 16:10 ` Tvrtko Ursulin
2026-11-30 16:26 ` sashiko-bot
2026-11-30 19:50 ` [RFC 0/4] " Tvrtko Ursulin
4 siblings, 1 reply; 9+ messages in thread
From: Tvrtko Ursulin @ 2026-11-30 16:10 UTC (permalink / raw)
To: amd-gfx
Cc: kernel-dev, Tvrtko Ursulin, Alex Deucher, Christian König,
Timur Kristóf
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.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
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/20261104095225.32654-1-tvrtko.ursulin@igalia.com/
References: https://lore.kernel.org/dri-devel/5582fb8a609eb8a2348811ecb6f539a7@kernel.org/
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Timur Kristóf <timur.kristof@gmail.com>
---
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(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
index d0b08d2daea2..7638e4230307 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
@@ -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) {
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
index 58219aa04013..318826a16803 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
@@ -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]);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
index 2a96cb7d891b..3fb4b5340ef5 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
@@ -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;
}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
index b2040f80b7f2..d2424b53af4e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h
@@ -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 */
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [RFC 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings
2026-11-30 16:10 ` [RFC 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
@ 2026-11-30 16:26 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-30 16:26 UTC (permalink / raw)
To: Tvrtko Ursulin, amd-gfx
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 <tvrtko.ursulin@igalia.com>
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().
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> index d0a6901532fd6..1984e31627bbc 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> @@ -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]);
[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.
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> index 2a96cb7d891b5..3fb4b5340ef52 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
> @@ -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;
> }
[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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130161016.64542-1-tvrtko.ursulin@igalia.com?part=4
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [RFC 0/4] Use high priority scheduler workqueue for high priority rings
2026-11-30 16:10 [RFC 0/4] Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
` (3 preceding siblings ...)
2026-11-30 16:10 ` [RFC 4/4] drm/amdgpu: Use high priority scheduler workqueue for high priority rings Tvrtko Ursulin
@ 2026-11-30 19:50 ` Tvrtko Ursulin
4 siblings, 0 replies; 9+ messages in thread
From: Tvrtko Ursulin @ 2026-11-30 19:50 UTC (permalink / raw)
To: amd-gfx; +Cc: kernel-dev, Alex Deucher, Christian König,
Timur Kristóf
On 30/11/2026 16:10, Tvrtko Ursulin wrote:
> 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
> Cc: Alex Deucher <alexander.deucher@amd.com>
> Cc: Christian König <christian.koenig@amd.com>
> Cc: Timur Kristóf <timur.kristof@gmail.com>
>
> 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(-)
>
^ permalink raw reply [flat|nested] 9+ messages in thread