Skip to contentSkip to conversation

drm/amdgpu/mes: fix mes ring buffer overflow

PATCH In review Jack Xiao 3 participants 10 messages Jul 19, 2024 – Jul 23, 2024

Subsystems (2)
lore
Share & tools
lore

Jack Xiao

author
#0 lore raw

PATCH 1/1 drm/amdgpu/mes: fix mes ring buffer overflow

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
 2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	const char *op_str, *misc_op_str;
 	unsigned long flags;
 	u64 status_gpu_addr;
-	u32 status_offset;
+	u32 seq, status_offset;
 	u64 *status_ptr;
 	signed long r;
 	int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	if (r)
 		goto error_unlock_free;
 
+	seq = ++ring->fence_drv.sync_seq;
+	r = amdgpu_fence_wait_polling(ring,
+				      seq - ring->fence_drv.num_fences_mask,
+				      timeout);
+	if (r < 1)
+		goto error_undo;
+
 	api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
 	api_status->api_completion_fence_addr = status_gpu_addr;
 	api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
 	mes_status_pkt.api_status.api_completion_fence_addr =
 		ring->fence_drv.gpu_addr;
-	mes_status_pkt.api_status.api_completion_fence_value =
-		++ring->fence_drv.sync_seq;
+	mes_status_pkt.api_status.api_completion_fence_value = seq;
 
 	amdgpu_ring_write_multiple(ring, &mes_status_pkt,
 				   sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 		dev_dbg(adev->dev, "MES msg=%d was emitted\n",
 			x_pkt->header.opcode);
 
-	r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+	r = amdgpu_fence_wait_polling(ring, seq, timeout);
 	if (r < 1 || !*status_ptr) {
 
 		if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	amdgpu_device_wb_free(adev, status_offset);
 	return 0;
 
+error_undo:
+	dev_err(adev->dev, "MES ring buffer is full.\n");
+	amdgpu_ring_undo(ring);
+
 error_unlock_free:
 	spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +14 -4
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	const char *op_str, *misc_op_str;
 	unsigned long flags;
 	u64 status_gpu_addr;
-	u32 status_offset;
+	u32 seq, status_offset;
 	u64 *status_ptr;
 	signed long r;
 	int ret;
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	if (r)
 		goto error_unlock_free;
 
+	seq = ++ring->fence_drv.sync_seq;
+	r = amdgpu_fence_wait_polling(ring,
+				      seq - ring->fence_drv.num_fences_mask,
+				      timeout);
+	if (r < 1)
+		goto error_undo;
+
 	api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
 	api_status->api_completion_fence_addr = status_gpu_addr;
 	api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
 	mes_status_pkt.api_status.api_completion_fence_addr =
 		ring->fence_drv.gpu_addr;
-	mes_status_pkt.api_status.api_completion_fence_value =
-		++ring->fence_drv.sync_seq;
+	mes_status_pkt.api_status.api_completion_fence_value = seq;
 
 	amdgpu_ring_write_multiple(ring, &mes_status_pkt,
 				   sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 		dev_dbg(adev->dev, "MES msg=%d was emitted\n",
 			x_pkt->header.opcode);
 
-	r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+	r = amdgpu_fence_wait_polling(ring, seq, timeout);
 	if (r < 1 || !*status_ptr) {
 
 		if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
 	amdgpu_device_wb_free(adev, status_offset);
 	return 0;
 
+error_undo:
+	dev_err(adev->dev, "MES ring buffer is full.\n");
+	amdgpu_ring_undo(ring);
+
 error_unlock_free:
 	spin_unlock_irqrestore(&mes->ring_lock, flags);
 
Signature · 1 line
2.41.0
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Jack Xiao

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

Fixes de3246254156 ("drm/amdgpu: cleanup MES11 command submission")
Fixes fffe347e1478 ("drm/amdgpu: cleanup MES12 command submission")
Quoted 122 lines
---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
 2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        const char *op_str, *misc_op_str;
        unsigned long flags;
        u64 status_gpu_addr;
-       u32 status_offset;
+       u32 seq, status_offset;
        u64 *status_ptr;
        signed long r;
        int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        if (r)
                goto error_unlock_free;
 
+       seq = ++ring->fence_drv.sync_seq;
+       r = amdgpu_fence_wait_polling(ring,
+                                     seq - ring->fence_drv.num_fences_mask,
+                                     timeout);
+       if (r < 1)
+               goto error_undo;
+
        api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
        api_status->api_completion_fence_addr = status_gpu_addr;
        api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
        mes_status_pkt.api_status.api_completion_fence_addr =
                ring->fence_drv.gpu_addr;
-       mes_status_pkt.api_status.api_completion_fence_value =
-               ++ring->fence_drv.sync_seq;
+       mes_status_pkt.api_status.api_completion_fence_value = seq;
 
        amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                   sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
                dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                        x_pkt->header.opcode);
 
-       r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+       r = amdgpu_fence_wait_polling(ring, seq, timeout);
        if (r < 1 || !*status_ptr) {
 
                if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        amdgpu_device_wb_free(adev, status_offset);
        return 0;
 
+error_undo:
+       dev_err(adev->dev, "MES ring buffer is full.\n");
+       amdgpu_ring_undo(ring);
+
 error_unlock_free:
        spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +14 -5
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        const char *op_str, *misc_op_str;
        unsigned long flags;
        u64 status_gpu_addr;
-       u32 status_offset;
+       u32 seq, status_offset;
        u64 *status_ptr;
        signed long r;
        int ret;
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        if (r)
                goto error_unlock_free;
 
+       seq = ++ring->fence_drv.sync_seq;
+       r = amdgpu_fence_wait_polling(ring,
+                                     seq - ring->fence_drv.num_fences_mask,
+                                     timeout);
+       if (r < 1)
+               goto error_undo;
+
        api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
        api_status->api_completion_fence_addr = status_gpu_addr;
        api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
        mes_status_pkt.api_status.api_completion_fence_addr =
                ring->fence_drv.gpu_addr;
-       mes_status_pkt.api_status.api_completion_fence_value =
-               ++ring->fence_drv.sync_seq;
+       mes_status_pkt.api_status.api_completion_fence_value = seq;
 
        amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                   sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
                dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                        x_pkt->header.opcode);
 
-       r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+       r = amdgpu_fence_wait_polling(ring, seq, timeout);
        if (r < 1 || !*status_ptr) {
 
                if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
        amdgpu_device_wb_free(adev, status_offset);
        return 0;
 
+error_undo:
+       dev_err(adev->dev, "MES ring buffer is full.\n");
+       amdgpu_ring_undo(ring);
+
 error_unlock_free:
        spin_unlock_irqrestore(&mes->ring_lock, flags);
 
--

2.41.0

#2 lore raw
In reply to Jack Xiao

Am 19.07.24 um 11:16 schrieb Jack Xiao:

Quoted 80 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
  drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
  drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
  2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	const char *op_str, *misc_op_str;
  	unsigned long flags;
  	u64 status_gpu_addr;
-	u32 status_offset;
+	u32 seq, status_offset;
  	u64 *status_ptr;
  	signed long r;
  	int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	if (r)
  		goto error_unlock_free;
 
+	seq = ++ring->fence_drv.sync_seq;
+	r = amdgpu_fence_wait_polling(ring,
+				      seq - ring->fence_drv.num_fences_mask,
+				      timeout);
+	if (r < 1)
+		goto error_undo;
+
  	api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
  	api_status->api_completion_fence_addr = status_gpu_addr;
  	api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
  	mes_status_pkt.api_status.api_completion_fence_addr =
  		ring->fence_drv.gpu_addr;
-	mes_status_pkt.api_status.api_completion_fence_value =
-		++ring->fence_drv.sync_seq;
+	mes_status_pkt.api_status.api_completion_fence_value = seq;
 
  	amdgpu_ring_write_multiple(ring, &mes_status_pkt,
  				   sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  		dev_dbg(adev->dev, "MES msg=%d was emitted\n",
  			x_pkt->header.opcode);
 
-	r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+	r = amdgpu_fence_wait_polling(ring, seq, timeout);
  	if (r < 1 || !*status_ptr) {
 
  		if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	amdgpu_device_wb_free(adev, status_offset);
  	return 0;
 
+error_undo:
+	dev_err(adev->dev, "MES ring buffer is full.\n");
+	amdgpu_ring_undo(ring);
+
  error_unlock_free:
  	spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	const char *op_str, *misc_op_str;
  	unsigned long flags;
  	u64 status_gpu_addr;
-	u32 status_offset;
+	u32 seq, status_offset;
  	u64 *status_ptr;
  	signed long r;
  	int ret;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes, · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	if (r)
  		goto error_unlock_free;
 
+	seq = ++ring->fence_drv.sync_seq;
+	r = amdgpu_fence_wait_polling(ring,
+				      seq - ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +10 -3
+				      timeout);
+	if (r < 1)
+		goto error_undo;
+
  	api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
  	api_status->api_completion_fence_addr = status_gpu_addr;
  	api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
  	mes_status_pkt.api_status.api_completion_fence_addr =
  		ring->fence_drv.gpu_addr;
-	mes_status_pkt.api_status.api_completion_fence_value =
-		++ring->fence_drv.sync_seq;
+	mes_status_pkt.api_status.api_completion_fence_value = seq;
 
  	amdgpu_ring_write_multiple(ring, &mes_status_pkt,
  				   sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  		dev_dbg(adev->dev, "MES msg=%d was emitted\n",
  			x_pkt->header.opcode);
 
-	r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+	r = amdgpu_fence_wait_polling(ring, seq, timeout);
  	if (r < 1 || !*status_ptr) {
 
  		if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
  	amdgpu_device_wb_free(adev, status_offset);
  	return 0;
 
+error_undo:
+	dev_err(adev->dev, "MES ring buffer is full.\n");
+	amdgpu_ring_undo(ring);
+
  error_unlock_free:
  	spin_unlock_irqrestore(&mes->ring_lock, flags);
 
author
#3 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

I think we rather need to increase the MES ring size instead.

Unfortunately, it doesn't work. I guess mes firmware has limitation.

Regards,
Jack

________________________________
From: Christian König <[email protected]>
Sent: Friday, 19 July 2024 23:44
To: Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Am 19.07.24 um 11:16 schrieb Jack Xiao:

Quoted 80 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
  drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
  drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
  2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes, · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +10 -3
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
#4 lore raw

Thx, but in that case this patch here then won't help either it just mitigates the problem.

Can you try to reduce num_hw_submission for the MES ring?

Thanks,
Christian.

Am 22.07.24 um 05:27 schrieb Xiao, Jack:

Quoted 170 lines

[AMD Official Use Only - AMD Internal Distribution Only]

/>> I think we rather need to increase the MES ring size instead./

Unfortunately, it doesn't work. I guess mes firmware has limitation.

Regards,
Jack

------------------------------------------------------------------------
*From:* Christian König <[email protected]>
*Sent:* Friday, 19 July 2024 23:44
*To:* Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]> *Subject:* Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow
Am 19.07.24 um 11:16 schrieb Jack Xiao:

Review

Quoted 10 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
   drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++---- drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----

   2 files changed, 28 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 4 lines

index 8ce51b9236c1..68c74adf79f1 100644

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
@@ -168,7 +168,7 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -1
         const char *op_str, *misc_op_str;
         unsigned long flags;
         u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
         u64 *status_ptr;
         signed long r;
         int ret;
@@ -196,6 +196,13 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +3 -0
         if (r)
                 goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - 

ring->fence_drv.num_fences_mask,

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +4 -0
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+

        api_status = (struct MES_API_STATUS *)((char *)pkt +

api_status_off);

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 3 lines

        api_status->api_completion_fence_addr = status_gpu_addr; api_status->api_completion_fence_value = 1;

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
@@ -208,8 +215,7 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
         mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;

mes_status_pkt.api_status.api_completion_fence_addr = ring->fence_drv.gpu_addr;
- mes_status_pkt.api_status.api_completion_fence_value = -             ++ring->fence_drv.sync_seq;
+ mes_status_pkt.api_status.api_completion_fence_value = seq;

        amdgpu_ring_write_multiple(ring, &mes_status_pkt, sizeof(mes_status_pkt) / 4);

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
@@ -229,7 +235,7 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -1
                 dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                         x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, 

timeout);

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -0
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
         if (r < 1 || !*status_ptr) {
 
                 if (misc_op_str)
@@ -252,6 +258,10 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +4 -0
         amdgpu_device_wb_free(adev, status_offset);
         return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+

   error_unlock_free:
spin_unlock_irqrestore(&mes->ring_lock, flags);

(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/mes_v12_0.c

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 4 lines

index c9f74231ad59..48e01206bcc4 100644

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
@@ -154,7 +154,7 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
         const char *op_str, *misc_op_str;
         unsigned long flags;
         u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
         u64 *status_ptr;
         signed long r;
         int ret;
@@ -182,6 +182,13 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
         if (r)
                 goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - 

ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +4 -0
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+

        api_status = (struct MES_API_STATUS *)((char *)pkt +

api_status_off);

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 3 lines

        api_status->api_completion_fence_addr = status_gpu_addr; api_status->api_completion_fence_value = 1;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
@@ -194,8 +201,7 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
         mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;

mes_status_pkt.api_status.api_completion_fence_addr = ring->fence_drv.gpu_addr;
- mes_status_pkt.api_status.api_completion_fence_value = -             ++ring->fence_drv.sync_seq;
+ mes_status_pkt.api_status.api_completion_fence_value = seq;

        amdgpu_ring_write_multiple(ring, &mes_status_pkt, sizeof(mes_status_pkt) / 4);

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
@@ -215,7 +221,7 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int · line 218

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -1
                 dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                         x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, 

timeout);

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -0
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
         if (r < 1 || !*status_ptr) {
 
                 if (misc_op_str)
@@ -238,6 +244,10 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +4 -0
         amdgpu_device_wb_free(adev, status_offset);
         return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+

   error_unlock_free:
spin_unlock_irqrestore(&mes->ring_lock, flags);

author
#5 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

Can you try to reduce num_hw_submission for the MES ring?

Smaller num_hw_submission should not help for this issue, for Mes work without drm scheduler like legacy kiq. Smaller num_hw_submission will result in smaller mes ring size and more waiting time.

Regards,
Jack
________________________________
From: Christian König <[email protected]>
Sent: Monday, 22 July 2024 16:20
To: Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Thx, but in that case this patch here then won't help either it just mitigates the problem.

Can you try to reduce num_hw_submission for the MES ring?

Thanks,
Christian.

Am 22.07.24 um 05:27 schrieb Xiao, Jack:

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

I think we rather need to increase the MES ring size instead.

Unfortunately, it doesn't work. I guess mes firmware has limitation.

Regards,
Jack

________________________________
From: Christian König <[email protected]><mailto:[email protected]>
Sent: Friday, 19 July 2024 23:44
To: Xiao, Jack <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]> <[email protected]><mailto:[email protected]>; Deucher, Alexander <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Am 19.07.24 um 11:16 schrieb Jack Xiao:

Quoted 80 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
  drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
  drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
  2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes, · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +10 -3
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
#6 lore raw

What I meant is that the MES ring is now to small for the number of packets written to it.

Either reduce the num_hw_submission or increase the MES ring size.

E.g. see this code here in amdgpu_ring_init:

        if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ) sched_hw_submission = max(sched_hw_submission, 256); else if (ring == &adev->sdma.instance[0].page) sched_hw_submission = 256;

We are basically just missing a case for the MES here as far as I can see.

Regards,
Christian.

Am 22.07.24 um 10:46 schrieb Xiao, Jack:

Quoted 194 lines

[AMD Official Use Only - AMD Internal Distribution Only]

/>> Can you try to reduce num_hw_submission for the MES ring?
/
Smaller num_hw_submission should not help for this issue, for Mes work without drm scheduler like legacy kiq. Smaller num_hw_submission will result in smaller mes ring size and more waiting time.

Regards,
Jack
------------------------------------------------------------------------
*From:* Christian König <[email protected]>
*Sent:* Monday, 22 July 2024 16:20
*To:* Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]> *Subject:* Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow
Thx, but in that case this patch here then won't help either it just mitigates the problem.

Can you try to reduce num_hw_submission for the MES ring?

Thanks,
Christian.

Am 22.07.24 um 05:27 schrieb Xiao, Jack:

Quoted 170 lines

[AMD Official Use Only - AMD Internal Distribution Only]

/>> I think we rather need to increase the MES ring size instead./

Unfortunately, it doesn't work. I guess mes firmware has limitation.

Regards,
Jack

------------------------------------------------------------------------
*From:* Christian König <[email protected]> <mailto:[email protected]>
*Sent:* Friday, 19 July 2024 23:44
*To:* Xiao, Jack <[email protected]> <mailto:[email protected]>; [email protected] <mailto:[email protected]> <[email protected]> <mailto:[email protected]>; Deucher, Alexander <[email protected]> <mailto:[email protected]> *Subject:* Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow
Am 19.07.24 um 11:16 schrieb Jack Xiao:

Review

Quoted 10 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
   drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++---- drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----

   2 files changed, 28 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 4 lines

index 8ce51b9236c1..68c74adf79f1 100644

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
@@ -168,7 +168,7 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -1
         const char *op_str, *misc_op_str;
         unsigned long flags;
         u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
         u64 *status_ptr;
         signed long r;
         int ret;
@@ -196,6 +196,13 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +3 -0
         if (r)
                 goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - 

ring->fence_drv.num_fences_mask,

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +4 -0
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+

        api_status = (struct MES_API_STATUS *)((char *)pkt +

api_status_off);

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 3 lines

api_status->api_completion_fence_addr = status_gpu_addr; api_status->api_completion_fence_value = 1;

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
@@ -208,8 +215,7 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
         mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;

mes_status_pkt.api_status.api_completion_fence_addr = ring->fence_drv.gpu_addr;
- mes_status_pkt.api_status.api_completion_fence_value = -             ++ring->fence_drv.sync_seq;
+ mes_status_pkt.api_status.api_completion_fence_value = seq;

        amdgpu_ring_write_multiple(ring, &mes_status_pkt, sizeof(mes_status_pkt) / 4);

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -0
@@ -229,7 +235,7 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +0 -1
                 dev_dbg(adev->dev, "MES msg=%d was emitted\n",
 x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, 

timeout);

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c static int

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -0
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
         if (r < 1 || !*status_ptr) {
 
                 if (misc_op_str)
@@ -252,6 +258,10 @@ static int 

mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +4 -0
         amdgpu_device_wb_free(adev, status_offset);
         return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+

   error_unlock_free:
spin_unlock_irqrestore(&mes->ring_lock, flags);

(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/mes_v12_0.c

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 4 lines

index c9f74231ad59..48e01206bcc4 100644

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
@@ -154,7 +154,7 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
         const char *op_str, *misc_op_str;
         unsigned long flags;
         u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
         u64 *status_ptr;
         signed long r;
         int ret;
@@ -182,6 +182,13 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
         if (r)
                 goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - 

ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +4 -0
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+

        api_status = (struct MES_API_STATUS *)((char *)pkt +

api_status_off);

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 3 lines

api_status->api_completion_fence_addr = status_gpu_addr; api_status->api_completion_fence_value = 1;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
@@ -194,8 +201,7 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
         mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;

mes_status_pkt.api_status.api_completion_fence_addr = ring->fence_drv.gpu_addr;
- mes_status_pkt.api_status.api_completion_fence_value = -             ++ring->fence_drv.sync_seq;
+ mes_status_pkt.api_status.api_completion_fence_value = seq;

        amdgpu_ring_write_multiple(ring, &mes_status_pkt, sizeof(mes_status_pkt) / 4);

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -0
@@ -215,7 +221,7 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int · line 218

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +0 -1
                 dev_dbg(adev->dev, "MES msg=%d was emitted\n",
 x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, 

timeout);

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -0
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
         if (r < 1 || !*status_ptr) {
 
                 if (misc_op_str)
@@ -238,6 +244,10 @@ static int 

mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +4 -0
         amdgpu_device_wb_free(adev, status_offset);
         return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+

   error_unlock_free:
spin_unlock_irqrestore(&mes->ring_lock, flags);

#7 lore raw

Does this patch fix it?
https://patchwork.freedesktop.org/patch/605437/

Alex

Quoted 197 lines · Christian König

What I meant is that the MES ring is now to small for the number of packets written to it.

Either reduce the num_hw_submission or increase the MES ring size.

E.g. see this code here in amdgpu_ring_init:

        if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
                sched_hw_submission = max(sched_hw_submission, 256);
        else if (ring == &adev->sdma.instance[0].page)
                sched_hw_submission = 256;

We are basically just missing a case for the MES here as far as I can see.

Regards,
Christian.

Am 22.07.24 um 10:46 schrieb Xiao, Jack:

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

Can you try to reduce num_hw_submission for the MES ring?

Smaller num_hw_submission should not help for this issue, for Mes work without drm scheduler like legacy kiq. Smaller num_hw_submission will result in smaller mes ring size and more waiting time.

Regards,
Jack
________________________________
From: Christian König <[email protected]>
Sent: Monday, 22 July 2024 16:20
To: Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Thx, but in that case this patch here then won't help either it just mitigates the problem.

Can you try to reduce num_hw_submission for the MES ring?

Thanks,
Christian.

Am 22.07.24 um 05:27 schrieb Xiao, Jack:

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

I think we rather need to increase the MES ring size instead.

Unfortunately, it doesn't work. I guess mes firmware has limitation.

Regards,
Jack

________________________________
From: Christian König <[email protected]>
Sent: Friday, 19 July 2024 23:44
To: Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Am 19.07.24 um 11:16 schrieb Jack Xiao:

Quoted 80 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
  drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
  drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
  2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes, · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +10 -3
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
author
#8 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line

Does this patch fix it?

https://patchwork.freedesktop.org/patch/605437/

No, please do not check in the patch, it will make my fix not working.

Regards,
Jack
________________________________
From: Alex Deucher <[email protected]>
Sent: Tuesday, 23 July 2024 03:52
To: Christian König <[email protected]>
Cc: Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Does this patch fix it?
https://patchwork.freedesktop.org/patch/605437/

Alex

Quoted 197 lines · Christian König

What I meant is that the MES ring is now to small for the number of packets written to it.

Either reduce the num_hw_submission or increase the MES ring size.

E.g. see this code here in amdgpu_ring_init:

        if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
                sched_hw_submission = max(sched_hw_submission, 256);
        else if (ring == &adev->sdma.instance[0].page)
                sched_hw_submission = 256;

We are basically just missing a case for the MES here as far as I can see.

Regards,
Christian.

Am 22.07.24 um 10:46 schrieb Xiao, Jack:

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

Can you try to reduce num_hw_submission for the MES ring?

Smaller num_hw_submission should not help for this issue, for Mes work without drm scheduler like legacy kiq. Smaller num_hw_submission will result in smaller mes ring size and more waiting time.

Regards,
Jack
________________________________
From: Christian König <[email protected]>
Sent: Monday, 22 July 2024 16:20
To: Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Thx, but in that case this patch here then won't help either it just mitigates the problem.

Can you try to reduce num_hw_submission for the MES ring?

Thanks,
Christian.

Am 22.07.24 um 05:27 schrieb Xiao, Jack:

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

I think we rather need to increase the MES ring size instead.

Unfortunately, it doesn't work. I guess mes firmware has limitation.

Regards,
Jack

________________________________
From: Christian König <[email protected]>
Sent: Friday, 19 July 2024 23:44
To: Xiao, Jack <[email protected]>; [email protected] <[email protected]>; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Am 19.07.24 um 11:16 schrieb Jack Xiao:

Quoted 80 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
  drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
  drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
  2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes, · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +10 -3
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
author
#9 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

There is a max command number for mes queue in mes_api_def.h

enum {API_NUMBER_OF_COMMAND_MAX = 32};

It should explain why the patch can’t fix the issue.

I will send out another patch to refine code according to the firmware limitation.

Regards,
Jack

From: amd-gfx <[email protected]> On Behalf Of Xiao, Jack
Sent: Tuesday, July 23, 2024 11:08 AM
To: Alex Deucher <[email protected]>; Christian König <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

[AMD Official Use Only - AMD Internal Distribution Only]

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line

Does this patch fix it?

https://patchwork.freedesktop.org/patch/605437/

No, please do not check in the patch, it will make my fix not working.

Regards,
Jack
________________________________
From: Alex Deucher <[email protected]<mailto:[email protected]>>
Sent: Tuesday, 23 July 2024 03:52
To: Christian König <[email protected]<mailto:[email protected]>>
Cc: Xiao, Jack <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; Deucher, Alexander <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Does this patch fix it?
https://patchwork.freedesktop.org/patch/605437/

Alex

Quoted 197 lines · On Mon, Jul 22, 2024 at 7:21 AM Christian König…

What I meant is that the MES ring is now to small for the number of packets written to it.

Either reduce the num_hw_submission or increase the MES ring size.

E.g. see this code here in amdgpu_ring_init:

        if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
                sched_hw_submission = max(sched_hw_submission, 256);
        else if (ring == &adev->sdma.instance[0].page)
                sched_hw_submission = 256;

We are basically just missing a case for the MES here as far as I can see.

Regards,
Christian.

Am 22.07.24 um 10:46 schrieb Xiao, Jack:

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

Can you try to reduce num_hw_submission for the MES ring?

Smaller num_hw_submission should not help for this issue, for Mes work without drm scheduler like legacy kiq. Smaller num_hw_submission will result in smaller mes ring size and more waiting time.

Regards,
Jack
________________________________
From: Christian König <[email protected]<mailto:[email protected]>>
Sent: Monday, 22 July 2024 16:20
To: Xiao, Jack <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; Deucher, Alexander <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Thx, but in that case this patch here then won't help either it just mitigates the problem.

Can you try to reduce num_hw_submission for the MES ring?

Thanks,
Christian.

Am 22.07.24 um 05:27 schrieb Xiao, Jack:

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 1 line
Quoted 1 line

I think we rather need to increase the MES ring size instead.

Unfortunately, it doesn't work. I guess mes firmware has limitation.

Regards,
Jack

________________________________
From: Christian König <[email protected]<mailto:[email protected]>>
Sent: Friday, 19 July 2024 23:44
To: Xiao, Jack <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; Deucher, Alexander <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH] drm/amdgpu/mes: fix mes ring buffer overflow

Am 19.07.24 um 11:16 schrieb Jack Xiao:

Quoted 80 lines

wait memory room until enough before writing mes packets to avoid ring buffer overflow.

---
  drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 ++++++++++++++----
  drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 ++++++++++++++----
  2 files changed, 28 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +14 -4
index 8ce51b9236c1..68c74adf79f1 100644
@@ -168,7 +168,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;
@@ -196,6 +196,13 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -208,8 +215,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -229,7 +235,7 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -252,6 +258,10 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);
 
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -1
index c9f74231ad59..48e01206bcc4 100644
@@ -154,7 +154,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       const char *op_str, *misc_op_str;
       unsigned long flags;
       u64 status_gpu_addr;
-     u32 status_offset;
+     u32 seq, status_offset;
       u64 *status_ptr;
       signed long r;
       int ret;

drivers/gpu/drm/amd/amdgpu/mes_v12_0.c static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes, · line 187

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +3 -0
@@ -182,6 +182,13 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       if (r)
               goto error_unlock_free;
 
+     seq = ++ring->fence_drv.sync_seq;
+     r = amdgpu_fence_wait_polling(ring,
+                                   seq - ring->fence_drv.num_fences_mask,

That's what's amdgpu_fence_emit_polling() does anyway.

So this here just moves the polling a bit earlier.

I think we rather need to increase the MES ring size instead.

Regards,
Christian.

Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +10 -3
+                                   timeout);
+     if (r < 1)
+             goto error_undo;
+
       api_status = (struct MES_API_STATUS *)((char *)pkt + api_status_off);
       api_status->api_completion_fence_addr = status_gpu_addr;
       api_status->api_completion_fence_value = 1;
@@ -194,8 +201,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       mes_status_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
       mes_status_pkt.api_status.api_completion_fence_addr =
               ring->fence_drv.gpu_addr;
-     mes_status_pkt.api_status.api_completion_fence_value =
-             ++ring->fence_drv.sync_seq;
+     mes_status_pkt.api_status.api_completion_fence_value = seq;
 
       amdgpu_ring_write_multiple(ring, &mes_status_pkt,
                                  sizeof(mes_status_pkt) / 4);
@@ -215,7 +221,7 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
               dev_dbg(adev->dev, "MES msg=%d was emitted\n",
                       x_pkt->header.opcode);
 
-     r = amdgpu_fence_wait_polling(ring, ring->fence_drv.sync_seq, timeout);
+     r = amdgpu_fence_wait_polling(ring, seq, timeout);
       if (r < 1 || !*status_ptr) {
 
               if (misc_op_str)
@@ -238,6 +244,10 @@ static int mes_v12_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
       amdgpu_device_wb_free(adev, status_offset);
       return 0;
 
+error_undo:
+     dev_err(adev->dev, "MES ring buffer is full.\n");
+     amdgpu_ring_undo(ring);
+
  error_unlock_free:
       spin_unlock_irqrestore(&mes->ring_lock, flags);