[PATCH v2] drm/amdgpu: count FRAME_CONTROL in the gfx ring frame and IB sizes

From: Eren Özen via B4 Relay

Date: Wed Dec 02 2026 - 21:02:01 EST


From: Eren Özen <erenozen@xxxxxxxxxxxxxx>

On rings with emit_frame_cntl, amdgpu_ib_schedule() writes a
FRAME_CONTROL start before the first IB and an end after the last one,
and whenever AMDGPU_IB_FLAGS_SECURE changes between two IBs of a job it
writes an end and a start back to back. Each FRAME_CONTROL is 2 dwords
on gfx9, gfx10 and gfx11, but neither the gfx emit_frame_size nor
emit_ib_size of these rings counts them. Userspace sets the secure
flag per IB (amdgpu_cs.c copies the IB flags unchanged), and nothing
requires the IBs of a job to agree, so a job that switches the flag
often enough between its IBs writes more than amdgpu_ring_alloc()
reserved for it.

On gfx9, one job of 63 NOP IBs that alternate the flag makes
amdgpu_ring_commit() report "writing more dwords to the ring than
expected!". A job of 192 such IBs, the gfx limit, needs up to about 1740
dwords, while the reservation and max_dw are 1024. With the default
sched_hw_submission of 2 the gfx ring holds 2048 dwords, so such a job
can overwrite commands of another job that is still in flight
(inferred, not tested).

Count 4 dwords for the start and end in emit_frame_size, and 4 more per
IB in emit_ib_size for the end and start of a switch. The worst cases
become 208 + 192 * 8 = 1744 dwords on gfx9 (also for the gfx9 software
rings used with MCBP), 1741 on gfx10 and 1765 on gfx11, so raise the
max_dw of these gfx rings from 1024 to 2048 (otherwise
amdgpu_ring_init() WARNs). With the default sched_hw_submission of 2
that makes each gfx ring 16 KiB instead of 8 KiB. gfx13 is not
changed: it supports TMZ only through secure queues, and its frame
control is removed by "drm/amdgpu/gfx13: remove frame cntl".

Tested on a Ryzen 7 5800H (Cezanne, GC 9.3.0) with one job per IB
count on the gfx ring, every other IB secure. Before this change every
job of 53 to 63 IBs made amdgpu_ring_commit() report the error (larger
jobs were not submitted on that kernel); with it, no job of 1 to 192
IBs did. The same jobs without secure IBs stayed clean on both kernels.
No WARNs at boot, the gfx ring is 16 KiB (debugfs amdgpu_ring_gfx is
16396 bytes), and the IB tests in debugfs amdgpu_test_ib pass. gfx10,
gfx11 and the gfx9 MCBP software rings are not tested.

Fixes: 0bb5d5b03f78 ("drm/amdgpu: Move to a per-IB secure flag (TMZ)")
Fixes: f77c9aff85ca ("drm/amdgpu: Fix per-IB secure flag GFX hang")
Signed-off-by: Eren Özen <erenozen@xxxxxxxxxxxxxx>
---
An alternative is to limit the secure-flag switches per job in
amdgpu_cs and count only that limit. Rejecting mixed flags outright
would break RADV, which does not set the flag on its postamble IBs, so
a secure RADV submission can already switch once. I chose the static
count because the ring has to be sized for the worst case anyway, but I
can rework it if you prefer a limit.

With "drm/amdgpu/gfx11: no need to emit frame cntl on rs64", the gfx11
count only matters on parts without RS64 firmware. I can drop the gfx11
hunk if none of those use TMZ.

The test kernel also carried three unrelated amdgpu compute ring
patches, one of which is now f0e06b8eb4bb in the base.

I found this with an AI coding assistant while reviewing the ring frame
sizes. A small test program that triggers the message is available on
request.
---
Changes in v2:
- Drop the gfx13 hunks: "drm/amdgpu/gfx13: remove frame cntl" removes
gfx13 frame control, which supports TMZ only via secure queues
(Alex Deucher).
- Rebase onto amd-staging-drm-next f0e06b8eb4bb. The gfx9, gfx10 and
gfx11 hunks are unchanged from v1, so the v1 test results still apply.
- Link to v1: https://lore.kernel.org/r/20261202-gfx-frame-cntl-v1-1-a3a8891e01ed@xxxxxxxxxxxxxx
---
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c | 8 +++++---
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 8 +++++---
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c | 16 ++++++++++------
3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
index b28dd5eaf1bb..fb87fd059b3f 100644
--- a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
@@ -4680,7 +4680,7 @@ static int gfx_v10_0_gfx_ring_init(struct amdgpu_device *adev, int ring_id,
irq_type = AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP + ring->pipe;
hw_prio = amdgpu_gfx_is_high_priority_graphics_queue(adev, ring) ?
AMDGPU_GFX_PIPE_PRIO_HIGH : AMDGPU_GFX_PIPE_PRIO_NORMAL;
- return amdgpu_ring_init(adev, ring, 1024, &adev->gfx.eop_irq, irq_type,
+ return amdgpu_ring_init(adev, ring, 2048, &adev->gfx.eop_irq, irq_type,
hw_prio, NULL);
}

@@ -9724,8 +9724,10 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_gfx = {
8 + 8 + /* FENCE x2 */
2 + /* SWITCH_BUFFER */
8 + /* gfx_v10_0_emit_mem_sync */
- 2, /* gfx_v10_0_ring_emit_cleaner_shader */
- .emit_ib_size = 4, /* gfx_v10_0_ring_emit_ib_gfx */
+ 2 + /* gfx_v10_0_ring_emit_cleaner_shader */
+ 4, /* FRAME_CONTROL start and end */
+ .emit_ib_size = 4 + /* gfx_v10_0_ring_emit_ib_gfx */
+ 4, /* FRAME_CONTROL end and start when the secure flag changes */
.emit_ib = gfx_v10_0_ring_emit_ib_gfx,
.emit_fence = gfx_v10_0_ring_emit_fence,
.emit_pipeline_sync = gfx_v10_0_ring_emit_pipeline_sync,
diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
index 16b211174c8d..24495d07fd6a 100644
--- a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
@@ -1247,7 +1247,7 @@ static int gfx_v11_0_gfx_ring_init(struct amdgpu_device *adev, int ring_id,
irq_type = AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP + ring->pipe;
hw_prio = amdgpu_gfx_is_high_priority_graphics_queue(adev, ring) ?
AMDGPU_GFX_PIPE_PRIO_HIGH : AMDGPU_GFX_PIPE_PRIO_NORMAL;
- return amdgpu_ring_init(adev, ring, 1024, &adev->gfx.eop_irq, irq_type,
+ return amdgpu_ring_init(adev, ring, 2048, &adev->gfx.eop_irq, irq_type,
hw_prio, NULL);
}

@@ -7047,8 +7047,10 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_gfx = {
22 + /* SET_Q_PREEMPTION_MODE */
8 + 8 + /* FENCE x2 */
8 + /* gfx_v11_0_emit_mem_sync */
- 2, /* gfx_v11_0_ring_emit_cleaner_shader */
- .emit_ib_size = 4, /* gfx_v11_0_ring_emit_ib_gfx */
+ 2 + /* gfx_v11_0_ring_emit_cleaner_shader */
+ 4, /* FRAME_CONTROL start and end */
+ .emit_ib_size = 4 + /* gfx_v11_0_ring_emit_ib_gfx */
+ 4, /* FRAME_CONTROL end and start when the secure flag changes */
.emit_ib = gfx_v11_0_ring_emit_ib_gfx,
.emit_fence = gfx_v11_0_ring_emit_fence,
.emit_pipeline_sync = gfx_v11_0_ring_emit_pipeline_sync,
diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
index 68282c8a2035..a55df4a155d3 100644
--- a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
@@ -2355,7 +2355,7 @@ static int gfx_v9_0_sw_init(struct amdgpu_ip_block *ip_block)
/* disable scheduler on the real ring */
ring->no_scheduler = adev->gfx.mcbp;
ring->vm_hub = AMDGPU_GFXHUB(0);
- r = amdgpu_ring_init(adev, ring, 1024, &adev->gfx.eop_irq,
+ r = amdgpu_ring_init(adev, ring, 2048, &adev->gfx.eop_irq,
AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP,
AMDGPU_RING_PRIO_DEFAULT, NULL);
if (r)
@@ -2373,7 +2373,7 @@ static int gfx_v9_0_sw_init(struct amdgpu_ip_block *ip_block)
ring->is_sw_ring = true;
hw_prio = amdgpu_sw_ring_priority(i);
ring->vm_hub = AMDGPU_GFXHUB(0);
- r = amdgpu_ring_init(adev, ring, 1024, &adev->gfx.eop_irq,
+ r = amdgpu_ring_init(adev, ring, 2048, &adev->gfx.eop_irq,
AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP, hw_prio,
NULL);
if (r)
@@ -7631,8 +7631,10 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_gfx = {
8 + 8 + /* FENCE x2 */
2 + /* SWITCH_BUFFER */
7 + /* gfx_v9_0_emit_mem_sync */
- 2, /* gfx_v9_0_ring_emit_cleaner_shader */
- .emit_ib_size = 4, /* gfx_v9_0_ring_emit_ib_gfx */
+ 2 + /* gfx_v9_0_ring_emit_cleaner_shader */
+ 4, /* FRAME_CONTROL start and end */
+ .emit_ib_size = 4 + /* gfx_v9_0_ring_emit_ib_gfx */
+ 4, /* FRAME_CONTROL end and start when the secure flag changes */
.emit_ib = gfx_v9_0_ring_emit_ib_gfx,
.emit_fence = gfx_v9_0_ring_emit_fence,
.emit_pipeline_sync = gfx_v9_0_ring_emit_pipeline_sync,
@@ -7689,8 +7691,10 @@ static const struct amdgpu_ring_funcs gfx_v9_0_sw_ring_funcs_gfx = {
8 + 8 + /* FENCE x2 */
2 + /* SWITCH_BUFFER */
7 + /* gfx_v9_0_emit_mem_sync */
- 2, /* gfx_v9_0_ring_emit_cleaner_shader */
- .emit_ib_size = 4, /* gfx_v9_0_ring_emit_ib_gfx */
+ 2 + /* gfx_v9_0_ring_emit_cleaner_shader */
+ 4, /* FRAME_CONTROL start and end */
+ .emit_ib_size = 4 + /* gfx_v9_0_ring_emit_ib_gfx */
+ 4, /* FRAME_CONTROL end and start when the secure flag changes */
.emit_ib = gfx_v9_0_ring_emit_ib_gfx,
.emit_fence = gfx_v9_0_ring_emit_fence,
.emit_pipeline_sync = gfx_v9_0_ring_emit_pipeline_sync,

---
base-commit: f0e06b8eb4bb207e5742f9e1566621f0962878e0
change-id: 20261202-gfx-frame-cntl-d0b829405080

Best regards,
--
Eren Özen <erenozen@xxxxxxxxxxxxxx>