Skip to contentSkip to conversation

drm/i915/migrate: Account for the reserved_space

PATCH Superseded Matthew Auld 3 participants 4 messages Nov 18, 2022

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/i915/migrate: Account for the reserved_space

From: Chris Wilson <[email protected]>

If the ring is nearly full when calling into emit_pte(), we might incorrectly trample the reserved_space when constructing the packet to emit the PTEs. This then triggers the GEM_BUG_ON(rq->reserved_space > ring->space) when later submitting the request, since the request itself doesn't have enough space left in the ring to emit things like workarounds, breadcrumbs etc.

Testcase: igt@i915_selftests@live_emit_pte_full_ring

---
 drivers/gpu/drm/i915/gt/intel_migrate.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gt/intel_migrate.c +12 -4
index b405a04135ca..48c3b5168558 100644
@@ -342,6 +342,16 @@ static int emit_no_arbitration(struct i915_request *rq)
 	return 0;
 }
 
+static int max_pte_pkt_size(struct i915_request *rq, int pkt)
+{
+       struct intel_ring *ring = rq->ring;
+
+       pkt = min_t(int, pkt, (ring->space - rq->reserved_space) / sizeof(u32) + 5);
+       pkt = min_t(int, pkt, (ring->size - ring->emit) / sizeof(u32) + 5);
+
+       return pkt;
+}
+
 static int emit_pte(struct i915_request *rq,
 		    struct sgt_dma *it,
 		    enum i915_cache_level cache_level,
@@ -388,8 +398,7 @@ static int emit_pte(struct i915_request *rq,
 		return PTR_ERR(cs);
 
 	/* Pack as many PTE updates as possible into a single MI command */
-	pkt = min_t(int, dword_length, ring->space / sizeof(u32) + 5);
-	pkt = min_t(int, pkt, (ring->size - ring->emit) / sizeof(u32) + 5);
+	pkt = max_pte_pkt_size(rq, dword_length);
 
 	hdr = cs;
 	*cs++ = MI_STORE_DATA_IMM | REG_BIT(21); /* as qword elements */
@@ -422,8 +431,7 @@ static int emit_pte(struct i915_request *rq,
 				}
 			}
 
-			pkt = min_t(int, dword_rem, ring->space / sizeof(u32) + 5);
-			pkt = min_t(int, pkt, (ring->size - ring->emit) / sizeof(u32) + 5);
+			pkt = max_pte_pkt_size(rq, dword_rem);
 
 			hdr = cs;
 			*cs++ = MI_STORE_DATA_IMM | REG_BIT(21);
Signature · 1 line
2.38.1
#1 lore raw

PATCH 2/2 drm/i915/selftests: exercise emit_pte() with nearly full ring

Simple regression test to check that we don't trample the rq->reserved_space when returning from emit_pte(), if the ring is nearly full.

References: https://gitlab.freedesktop.org/drm/intel/-/issues/7535
References: https://gitlab.freedesktop.org/drm/intel/-/issues/6889

Cc Chris Wilson <[email protected]>
Cc Andrzej Hajda <[email protected]>
Cc Nirmoy Das <[email protected]>
---
 drivers/gpu/drm/i915/gt/intel_migrate.c    |   6 +-
 drivers/gpu/drm/i915/gt/selftest_migrate.c | 153 +++++++++++++++++++++
 2 files changed, 157 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_migrate.c +4 -2
index 48c3b5168558..6df728b82a73 100644
@@ -352,6 +352,8 @@ static int max_pte_pkt_size(struct i915_request *rq, int pkt)
        return pkt;
 }
 
+#define I915_EMIT_PTE_NUM_DWORDS 6
+
 static int emit_pte(struct i915_request *rq,
 		    struct sgt_dma *it,
 		    enum i915_cache_level cache_level,
@@ -393,7 +395,7 @@ static int emit_pte(struct i915_request *rq,
 
 	offset += (u64)rq->engine->instance << 32;
 
-	cs = intel_ring_begin(rq, 6);
+	cs = intel_ring_begin(rq, I915_EMIT_PTE_NUM_DWORDS);
 	if (IS_ERR(cs))
 		return PTR_ERR(cs);
 
@@ -416,7 +418,7 @@ static int emit_pte(struct i915_request *rq,
 			intel_ring_advance(rq, cs);
 			intel_ring_update_space(ring);
 
-			cs = intel_ring_begin(rq, 6);
+			cs = intel_ring_begin(rq, I915_EMIT_PTE_NUM_DWORDS);
 			if (IS_ERR(cs))
 				return PTR_ERR(cs);
 
drivers/gpu/drm/i915/gt/selftest_migrate.c +153 -0

171 lines not on this page · raw message

Signature · 1 line
2.38.1
#2 lore raw

[Intel-gfx] ✗ Fi.CI.BUILD: failure for series starting with [1/2] drm/i915/migrate: Account for the reserved_space

In reply to Matthew Auld
== Series Details ==

== Series Details ==

Series: series starting with [1/2] drm/i915/migrate: Account for the reserved_space
URL : https://patchwork.freedesktop.org/series/111072/
State : failure

== Summary ==

Error: make failed
CALL scripts/checksyscalls.sh
DESCEND objtool
CC [M] drivers/gpu/drm/i915/gt/intel_migrate.o
In file included from drivers/gpu/drm/i915/gt/intel_migrate.c:1137:
drivers/gpu/drm/i915/gt/selftest_migrate.c:531:5: error: no previous prototype for ‘spinner_kill’ [-Werror=missing-prototypes]
int spinner_kill(void *arg)

     ^~~~~~~~~~~~

cc1: all warnings being treated as errors
scripts/Makefile.build:250: recipe for target 'drivers/gpu/drm/i915/gt/intel_migrate.o' failed make[5]: *** [drivers/gpu/drm/i915/gt/intel_migrate.o] Error 1 scripts/Makefile.build:500: recipe for target 'drivers/gpu/drm/i915' failed make[4]: *** [drivers/gpu/drm/i915] Error 2
scripts/Makefile.build:500: recipe for target 'drivers/gpu/drm' failed
make[3]: *** [drivers/gpu/drm] Error 2
scripts/Makefile.build:500: recipe for target 'drivers/gpu' failed
make[2]: *** [drivers/gpu] Error 2
scripts/Makefile.build:500: recipe for target 'drivers' failed
make[1]: *** [drivers] Error 2
Makefile:1992: recipe for target '.' failed
make: *** [.] Error 2

Tested-by Reviewed-by on v1 1/2
#3 lore raw
In reply to Matthew Auld
Quoted 9 lines · Matthew Auld

From: Chris Wilson <[email protected]>

If the ring is nearly full when calling into emit_pte(), we might incorrectly trample the reserved_space when constructing the packet to emit the PTEs. This then triggers the GEM_BUG_ON(rq->reserved_space > ring->space) when later submitting the request, since the request itself doesn't have enough space left in the ring to emit things like workarounds, breadcrumbs etc.

Quoted 8 lines

Testcase: igt@i915_selftests@live_emit_pte_full_ring

Tested this on ATS-M.

Quoted 46 lines
Cc <[email protected]> # v5.15+
---
  drivers/gpu/drm/i915/gt/intel_migrate.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gt/intel_migrate.c +12 -4
index b405a04135ca..48c3b5168558 100644
@@ -342,6 +342,16 @@ static int emit_no_arbitration(struct i915_request *rq)
  	return 0;
  }
 
+static int max_pte_pkt_size(struct i915_request *rq, int pkt)
+{
+       struct intel_ring *ring = rq->ring;
+
+       pkt = min_t(int, pkt, (ring->space - rq->reserved_space) / sizeof(u32) + 5);
+       pkt = min_t(int, pkt, (ring->size - ring->emit) / sizeof(u32) + 5);
+
+       return pkt;
+}
+
  static int emit_pte(struct i915_request *rq,
  		    struct sgt_dma *it,
  		    enum i915_cache_level cache_level,
@@ -388,8 +398,7 @@ static int emit_pte(struct i915_request *rq,
  		return PTR_ERR(cs);
 
  	/* Pack as many PTE updates as possible into a single MI command */
-	pkt = min_t(int, dword_length, ring->space / sizeof(u32) + 5);
-	pkt = min_t(int, pkt, (ring->size - ring->emit) / sizeof(u32) + 5);
+	pkt = max_pte_pkt_size(rq, dword_length);
 
  	hdr = cs;
  	*cs++ = MI_STORE_DATA_IMM | REG_BIT(21); /* as qword elements */
@@ -422,8 +431,7 @@ static int emit_pte(struct i915_request *rq,
  				}
  			}
 
-			pkt = min_t(int, dword_rem, ring->space / sizeof(u32) + 5);
-			pkt = min_t(int, pkt, (ring->size - ring->emit) / sizeof(u32) + 5);
+			pkt = max_pte_pkt_size(rq, dword_rem);
 
  			hdr = cs;
  			*cs++ = MI_STORE_DATA_IMM | REG_BIT(21);