Skip to contentSkip to conversation

drm/amdgpu: reject oversized IBs on rings with 20-bit size fields

PATCH In review Candice Li 2 participants 2 messages Jul 30

Subsystems (2)
lore
Share & tools
lore

Candice Li

author
#0 lore raw

PATCH 1/1 drm/amdgpu: reject oversized IBs on rings with 20-bit size fields

PM4 INDIRECT_BUFFER control words encode IB size in 20 bits. On GFX rings amdgpu_cs_p2_ib() passed user-supplied ib_bytes through to ib->length_dw without a limit, and ring_emit_ib() ORed length_dw directly into the control dword.

Reject IB submissions whose length exceeds the hardware maximum in amdgpu_cs_p2_ib(), but only for rings whose emit_ib packet format documents a 20-bit size field (GFX, compute and SDMA).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +25 -1
index afc1c631d55aa6..0d06b6d166c576 100644
@@ -42,6 +42,25 @@
 #include "amdgpu_ras.h"
 #include "amdgpu_hmm.h"
 
+/*
+ * Maximum IB length (dwords) for rings whose emit_ib packet format
+ * documents a 20-bit size field, e.g. GFX PM4 INDIRECT_BUFFER ([19:0])
+ * and SDMA indirect packets.
+ */
+#define AMDGPU_IB_PACKET_SIZE_MAX_DW	0xFFFFF
+
+static bool amdgpu_cs_ib_has_packet_length_limit(enum amdgpu_ring_type type)
+{
+	switch (type) {
+	case AMDGPU_RING_TYPE_GFX:
+	case AMDGPU_RING_TYPE_COMPUTE:
+	case AMDGPU_RING_TYPE_SDMA:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p,
 				 struct amdgpu_device *adev,
 				 struct drm_file *filp,
@@ -340,7 +359,6 @@ static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p,
 
 	job = p->jobs[r];
 	ring = amdgpu_job_ring(job);
-	ib = &job->ibs[job->num_ibs++];
 
 	/* submissions to kernel queues are disabled */
 	if (ring->no_user_submission)
@@ -369,6 +387,12 @@ static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p,
 			return -EINVAL;
 	}
 
+	if (amdgpu_cs_ib_has_packet_length_limit(ring->funcs->type) &&
+	    chunk_ib->ib_bytes / 4 > AMDGPU_IB_PACKET_SIZE_MAX_DW)
+		return -EINVAL;
+
+	ib = &job->ibs[job->num_ibs++];
+
 	if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE)
 		job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Candice Li

PM4 INDIRECT_BUFFER control words encode IB size in 20 bits. On GFX rings amdgpu_cs_p2_ib() passed user-supplied ib_bytes through to ib->length_dw without a limit, and ring_emit_ib() ORed length_dw directly into the control dword.

As a follow on patch, it would be good to mask the length_dw in the gfx emit_ib() functions as well just to be safe.

Quoted 15 lines

Reject IB submissions whose length exceeds the hardware maximum in amdgpu_cs_p2_ib(), but only for rings whose emit_ib packet format documents a 20-bit size field (GFX, compute and SDMA).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
index afc1c631d55aa6..0d06b6d166c576 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c line 50

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -0
@@ -42,6 +42,25 @@
 #include "amdgpu_ras.h"
 #include "amdgpu_hmm.h"
 
+/*
+ * Maximum IB length (dwords) for rings whose emit_ib packet format
+ * documents a 20-bit size field, e.g. GFX PM4 INDIRECT_BUFFER ([19:0])
+ * and SDMA indirect packets.
+ */
+#define AMDGPU_IB_PACKET_SIZE_MAX_DW   0xFFFFF

#define AMDGPU_GFX_SDMA_IB_PACKET_SIZE_MAX_DW 0xFFFFF
#define AMDGPU_MM_IB_PACKET_SIZE_MAX_DW 0x7FFFF0

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c line 53

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
+
+static bool amdgpu_cs_ib_has_packet_length_limit(enum amdgpu_ring_type type)
+{

I would make this function return the max IB size is DWs.

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c line 57

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +4 -0
+       switch (type) {
+       case AMDGPU_RING_TYPE_GFX:
+       case AMDGPU_RING_TYPE_COMPUTE:
+       case AMDGPU_RING_TYPE_SDMA:

I suspect VPE also should be covered by this case since it's based on SDMA.

return AMDGPU_GFX_SDMA_IB_PACKET_SIZE_MAX_DW here.

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c line 59

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -0
+               return true;
+       default:

and

return AMDGPU_MM_IB_PACKET_SIZE_MAX_DW here.

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +4 -1
+               return false;
+       }
+}
+
 static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p,
                                 struct amdgpu_device *adev,
                                 struct drm_file *filp,
@@ -340,7 +359,6 @@ static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p,
 
        job = p->jobs[r];
        ring = amdgpu_job_ring(job);
-       ib = &job->ibs[job->num_ibs++];
 
        /* submissions to kernel queues are disabled */
        if (ring->no_user_submission)

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p, · line 391

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -0
@@ -369,6 +387,12 @@ static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p,
                        return -EINVAL;
        }
 
+       if (amdgpu_cs_ib_has_packet_length_limit(ring->funcs->type) &&
+           chunk_ib->ib_bytes / 4 > AMDGPU_IB_PACKET_SIZE_MAX_DW)

Then check the max size returned by the above function here.

Alex

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +4 -1
+               return -EINVAL;
+
+       ib = &job->ibs[job->num_ibs++];
+
        if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE)
                job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
 
--

2.25.1