Skip to contentSkip to conversation

drm/i915: fix gtt space allocated for tiled objects

PATCH Superseded Imre Deak 3 participants 24 messages Jan 4, 2013 – Jan 14, 2013

Subsystems (2)
lore
Share & tools
lore

Imre Deak

author
#0 lore raw

cover drm/i915: fix gtt space allocated for tiled objects

At the moment it's possible for user space to allocate a tiled buffer with a size not aligned to its tile row size and corrupt a buffer that follows this in the gtt space. More details on this in the last patch.

I'll also send an i-g-t test case for this.

Tested on Gen5,6.

Imre Deak (5):
drm/i915: merge get_gtt_alignment/get_unfenced_gtt_alignment() drm/i915: merge {i965,sandybridge}_write_fence_reg() drm/i915: use gtt_get_size() instead of open coding it drm/i915: factor out i915_gem_get_tile_width() drm/i915: fix gtt space allocated for tiled objects

 drivers/gpu/drm/i915/i915_drv.h        |   12 ++-
 drivers/gpu/drm/i915/i915_gem.c        |  144 +++++++++++++++-----------------
 drivers/gpu/drm/i915/i915_gem_tiling.c |   34 +++-----
 3 files changed, 89 insertions(+), 101 deletions(-)
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/5 drm/i915: merge get_gtt_alignment/get_unfenced_gtt_alignment()

The two functions are rather similar, so merge them.

---
 drivers/gpu/drm/i915/i915_drv.h        |    5 ++--
 drivers/gpu/drm/i915/i915_gem.c        |   44 +++++---------------------------
 drivers/gpu/drm/i915/i915_gem_tiling.c |    6 ++---
 3 files changed, 12 insertions(+), 43 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +2 -3
index b724688..9b70728 100644
@@ -1577,9 +1577,8 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
-i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
-				    uint32_t size,
-				    int tiling_mode);
+i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
+			    int tiling_mode, bool fenced);
 
 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
 				    enum i915_cache_level cache_level);
drivers/gpu/drm/i915/i915_gem.c +7 -37

69 lines not on this page · raw message

drivers/gpu/drm/i915/i915_gem_tiling.c +3 -3
index c77e40a..02bb2bc 100644
@@ -375,9 +375,9 @@ i915_gem_set_tiling(struct drm_device *dev, void *data,
 		/* Rebind if we need a change of alignment */
 		if (!obj->map_and_fenceable) {
 			u32 unfenced_alignment =
-				i915_gem_get_unfenced_gtt_alignment(dev,
-								    obj->base.size,
-								    args->tiling_mode);
+				i915_gem_get_gtt_alignment(dev, obj->base.size,
+							    args->tiling_mode,
+							    false);
 			if (obj->gtt_offset & (unfenced_alignment - 1))
 				ret = i915_gem_object_unbind(obj);
 		}
Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH 2/5 drm/i915: merge {i965, sandybridge}_write_fence_reg()

In reply to Imre Deak

The two functions are rather similar, so merge them.

---
 drivers/gpu/drm/i915/i915_gem.c |   44 +++++++++++++--------------------------
 1 file changed, 15 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +15 -29
index 240f798..b74e72f 100644
@@ -2537,52 +2537,38 @@ int i915_gpu_idle(struct drm_device *dev)
 	return 0;
 }
 
-static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
-					struct drm_i915_gem_object *obj)
-{
-	drm_i915_private_t *dev_priv = dev->dev_private;
-	uint64_t val;
-
-	if (obj) {
-		u32 size = obj->gtt_space->size;
-
-		val = (uint64_t)((obj->gtt_offset + size - 4096) &
-				 0xfffff000) << 32;
-		val |= obj->gtt_offset & 0xfffff000;
-		val |= (uint64_t)((obj->stride / 128) - 1) <<
-			SANDYBRIDGE_FENCE_PITCH_SHIFT;
-
-		if (obj->tiling_mode == I915_TILING_Y)
-			val |= 1 << I965_FENCE_TILING_Y_SHIFT;
-		val |= I965_FENCE_REG_VALID;
-	} else
-		val = 0;
-
-	I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
-	POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
-}
-
 static void i965_write_fence_reg(struct drm_device *dev, int reg,
 				 struct drm_i915_gem_object *obj)
 {
 	drm_i915_private_t *dev_priv = dev->dev_private;
+	int fence_reg;
+	int fence_pitch_shift;
 	uint64_t val;
 
+	if (INTEL_INFO(dev)->gen >= 6) {
+		fence_reg = FENCE_REG_SANDYBRIDGE_0;
+		fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
+	} else {
+		fence_reg = FENCE_REG_965_0;
+		fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
+	}
+
 	if (obj) {
 		u32 size = obj->gtt_space->size;
 
 		val = (uint64_t)((obj->gtt_offset + size - 4096) &
 				 0xfffff000) << 32;
 		val |= obj->gtt_offset & 0xfffff000;
-		val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
+		val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
 		if (obj->tiling_mode == I915_TILING_Y)
 			val |= 1 << I965_FENCE_TILING_Y_SHIFT;
 		val |= I965_FENCE_REG_VALID;
 	} else
 		val = 0;
 
-	I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
-	POSTING_READ(FENCE_REG_965_0 + reg * 8);
+	fence_reg += reg * 8;
+	I915_WRITE64(fence_reg, val);
+	POSTING_READ(fence_reg);
 }
 
 static void i915_write_fence_reg(struct drm_device *dev, int reg,
@@ -2666,7 +2652,7 @@ static void i915_gem_write_fence(struct drm_device *dev, int reg,
 {
 	switch (INTEL_INFO(dev)->gen) {
 	case 7:
-	case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
+	case 6:
 	case 5:
 	case 4: i965_write_fence_reg(dev, reg, obj); break;
 	case 3: i915_write_fence_reg(dev, reg, obj); break;
Signature · 1 line
1.7.9.5
author
#3 lore raw

PATCH 3/5 drm/i915: use gtt_get_size() instead of open coding it

In reply to Imre Deak
---
 drivers/gpu/drm/i915/i915_drv.h        |    2 ++
 drivers/gpu/drm/i915/i915_gem.c        |    2 +-
 drivers/gpu/drm/i915/i915_gem_tiling.c |   13 +------------
 3 files changed, 4 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +2 -0
index 9b70728..91838b8 100644
@@ -1577,6 +1577,8 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
+i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
+uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
 
drivers/gpu/drm/i915/i915_gem.c +1 -1
index b74e72f..77d4de9 100644
@@ -1463,7 +1463,7 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 	obj->fault_mappable = false;
 }
 
-static uint32_t
+uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
 	uint32_t gtt_size;
drivers/gpu/drm/i915/i915_gem_tiling.c +1 -12
index 02bb2bc..896ef41 100644
@@ -273,18 +273,7 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 			return false;
 	}
 
-	/*
-	 * Previous chips need to be aligned to the size of the smallest
-	 * fence register that can contain the object.
-	 */
-	if (INTEL_INFO(obj->base.dev)->gen == 3)
-		size = 1024*1024;
-	else
-		size = 512*1024;
-
-	while (size < obj->base.size)
-		size <<= 1;
-
+	size = i915_gem_get_gtt_size(obj->base.dev, obj->base.size, tiling_mode);
 	if (obj->gtt_space->size != size)
 		return false;
 
Signature · 1 line
1.7.9.5
author
#4 lore raw

PATCH 4/5 drm/i915: factor out i915_gem_get_tile_width()

In reply to Imre Deak

This will be needed by the upcoming patch.

---
 drivers/gpu/drm/i915/i915_drv.h        |    1 +
 drivers/gpu/drm/i915/i915_gem.c        |   12 ++++++++++++
 drivers/gpu/drm/i915/i915_gem_tiling.c |    8 ++------
 3 files changed, 15 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +1 -0
index 91838b8..a20edca 100644
@@ -1581,6 +1581,7 @@ i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
 uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
+int i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode);
 
 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
 				    enum i915_cache_level cache_level);
drivers/gpu/drm/i915/i915_gem.c +12 -0
index 77d4de9..a878b9f 100644
@@ -1463,6 +1463,18 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 	obj->fault_mappable = false;
 }
 
+int
+i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode)
+{
+	BUG_ON(tiling_mode != I915_TILING_Y && tiling_mode != I915_TILING_X);
+
+	if (IS_GEN2(dev) ||
+	    (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
+		return 128;
+	else
+		return 512;
+}
+
 uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
drivers/gpu/drm/i915/i915_gem_tiling.c +2 -6
index 896ef41..7b0a3b3 100644
@@ -211,12 +211,6 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 	if (tiling_mode == I915_TILING_NONE)
 		return true;
 
-	if (IS_GEN2(dev) ||
-	    (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
-		tile_width = 128;
-	else
-		tile_width = 512;
-
 	/* check maximum stride & object size */
 	if (INTEL_INFO(dev)->gen >= 4) {
 		/* i965 stores the end address of the gtt mapping in the fence
@@ -236,6 +230,8 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 		}
 	}
 
+	tile_width = i915_gem_get_tile_width(dev, tiling_mode);
+
 	/* 965+ just needs multiples of tile width */
 	if (INTEL_INFO(dev)->gen >= 4) {
 		if (stride & (tile_width - 1))
Signature · 1 line
1.7.9.5
author
#5 lore raw

PATCH 5/5 drm/i915: fix gtt space allocated for tiled objects

In reply to Imre Deak

The gtt space needed for tiled objects might be bigger than the linear size programmed into the correpsonding fence register. For example for the following buffer on a Gen5+ HW:

- allocation size: 4096 bytes
- tiling mode: X tiled
- stride: 1536

we need (1536 / 512) * 4096 bytes of gtt space to cover all the pixels in the buffer, but at the moment we allocate only 4096. This means that any buffer following this tiled buffer in the gtt space will be corrupted if pixels belonging to the 2nd and 3rd tiles are written.

Fix this by rounding up the size of the allocated gtt space to the next tile row address. The page frames beyond the allocation size will be backed by the single gtt scratch page used already elsewhere for similar padding.

Note that this is more of a security/robustness problem and not fixing any reported issue that I know of. This is because applications will normally access only the part of the buffer that is tile row size aligned.

---
 drivers/gpu/drm/i915/i915_drv.h        |    6 ++++-
 drivers/gpu/drm/i915/i915_gem.c        |   42 ++++++++++++++++++++++++++------
 drivers/gpu/drm/i915/i915_gem_tiling.c |    9 ++++---
 3 files changed, 45 insertions(+), 12 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +5 -1
index a20edca..3a755e4 100644
@@ -1577,7 +1577,11 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
-i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
+i915_gem_get_gtt_linear_size(struct drm_device *dev, uint32_t size,
+			     int tiling_mode);
+uint32_t
+i915_gem_get_gtt_physical_size(struct drm_device *dev, uint32_t size,
+			       int tiling_mode, int stride);
 uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
drivers/gpu/drm/i915/i915_gem.c +34 -8

90 lines not on this page · raw message

drivers/gpu/drm/i915/i915_gem_tiling.c +6 -3
index 7b0a3b3..e738fcb 100644
@@ -251,7 +251,8 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 
 /* Is the current GTT allocation valid for the change in tiling? */
 static bool
-i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
+i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode,
+			 int stride)
 {
 	u32 size;
 
@@ -269,7 +270,8 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 			return false;
 	}
 
-	size = i915_gem_get_gtt_size(obj->base.dev, obj->base.size, tiling_mode);
+	size = i915_gem_get_gtt_physical_size(obj->base.dev, obj->base.size,
+					      tiling_mode, stride);
 	if (obj->gtt_space->size != size)
 		return false;
 
@@ -355,7 +357,8 @@ i915_gem_set_tiling(struct drm_device *dev, void *data,
 		obj->map_and_fenceable =
 			obj->gtt_space == NULL ||
 			(obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end &&
-			 i915_gem_object_fence_ok(obj, args->tiling_mode));
+			 i915_gem_object_fence_ok(obj, args->tiling_mode,
+						  args->stride));
 
 		/* Rebind if we need a change of alignment */
 		if (!obj->map_and_fenceable) {
Signature · 1 line
1.7.9.5
#6 lore raw
Quoted 13 lines · Imre Deak

The gtt space needed for tiled objects might be bigger than the linear size programmed into the correpsonding fence register. For example for the following buffer on a Gen5+ HW:

- allocation size: 4096 bytes
- tiling mode: X tiled
- stride: 1536

we need (1536 / 512) * 4096 bytes of gtt space to cover all the pixels in the buffer, but at the moment we allocate only 4096. This means that any buffer following this tiled buffer in the gtt space will be corrupted if pixels belonging to the 2nd and 3rd tiles are written.

Quoted 8 lines

Fix this by rounding up the size of the allocated gtt space to the next tile row address. The page frames beyond the allocation size will be backed by the single gtt scratch page used already elsewhere for similar padding.

Note that this is more of a security/robustness problem and not fixing any reported issue that I know of. This is because applications will normally access only the part of the buffer that is tile row size aligned.

There should not be any reported issues because all userspace already allocates up to the end of tile-row and stride should be enforced to be a multiple of tile-width. So the use of DIV_ROUND_UP implies a programming error that should have been reported back to userspace earlier. We can extend that by checking to make sure userspace has allocated a valid buffer, that is, it has allocated sufficient pages for the sampler access into the tiled buffer (or reject the set-tiling). -Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
author
#7 lore raw
Quoted 23 lines · Chris Wilson
Quoted 13 lines · Imre Deak

The gtt space needed for tiled objects might be bigger than the linear size programmed into the correpsonding fence register. For example for the following buffer on a Gen5+ HW:

- allocation size: 4096 bytes
- tiling mode: X tiled
- stride: 1536

we need (1536 / 512) * 4096 bytes of gtt space to cover all the pixels in the buffer, but at the moment we allocate only 4096. This means that any buffer following this tiled buffer in the gtt space will be corrupted if pixels belonging to the 2nd and 3rd tiles are written.

Quoted 8 lines

Fix this by rounding up the size of the allocated gtt space to the next tile row address. The page frames beyond the allocation size will be backed by the single gtt scratch page used already elsewhere for similar padding.

Note that this is more of a security/robustness problem and not fixing any reported issue that I know of. This is because applications will normally access only the part of the buffer that is tile row size aligned.

Quoted 8 lines

There should not be any reported issues because all userspace already allocates up to the end of tile-row and stride should be enforced to be a multiple of tile-width. So the use of DIV_ROUND_UP implies a programming error that should have been reported back to userspace earlier. We can extend that by checking to make sure userspace has allocated a valid buffer, that is, it has allocated sufficient pages for the sampler access into the tiled buffer (or reject the set-tiling). -Chris

Ok, I tested this with older UXA that still allocated non-aligned buffers. If that's not the case any more then rejecting set-tiling if it's called on a non tile-row size aligned buffer would work too.

--Imre

#8 lore raw
Quoted 33 lines · Imre Deak
Quoted 23 lines · Chris Wilson
Quoted 13 lines · Imre Deak

The gtt space needed for tiled objects might be bigger than the linear size programmed into the correpsonding fence register. For example for the following buffer on a Gen5+ HW:

- allocation size: 4096 bytes
- tiling mode: X tiled
- stride: 1536

we need (1536 / 512) * 4096 bytes of gtt space to cover all the pixels in the buffer, but at the moment we allocate only 4096. This means that any buffer following this tiled buffer in the gtt space will be corrupted if pixels belonging to the 2nd and 3rd tiles are written.

Quoted 8 lines

Fix this by rounding up the size of the allocated gtt space to the next tile row address. The page frames beyond the allocation size will be backed by the single gtt scratch page used already elsewhere for similar padding.

Note that this is more of a security/robustness problem and not fixing any reported issue that I know of. This is because applications will normally access only the part of the buffer that is tile row size aligned.

Quoted 8 lines

There should not be any reported issues because all userspace already allocates up to the end of tile-row and stride should be enforced to be a multiple of tile-width. So the use of DIV_ROUND_UP implies a programming error that should have been reported back to userspace earlier. We can extend that by checking to make sure userspace has allocated a valid buffer, that is, it has allocated sufficient pages for the sampler access into the tiled buffer (or reject the set-tiling). -Chris

Quoted 3 lines

Ok, I tested this with older UXA that still allocated non-aligned buffers. If that's not the case any more then rejecting set-tiling if it's called on a non tile-row size aligned buffer would work too.

Meep. A long time ago we got the calcuations wrong (slightly less for gen2), but it was and still is a userspace bug with the potential of handing the GPU.

A multiple of tile_height tall and a mutiple of tile_width across should always be an exact number of pages (and an exact multiple of tile-row pages). And we should have been obeying that since the introduction of set-tiling (ignoring the aforementioned bugs) - so I'd really like to see any evidence of userspace getting that wrong.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
author
#9 lore raw
Quoted 42 lines · Chris Wilson
Quoted 33 lines · Imre Deak
Quoted 23 lines · Chris Wilson
Quoted 13 lines · Imre Deak

The gtt space needed for tiled objects might be bigger than the linear size programmed into the correpsonding fence register. For example for the following buffer on a Gen5+ HW:

- allocation size: 4096 bytes
- tiling mode: X tiled
- stride: 1536

we need (1536 / 512) * 4096 bytes of gtt space to cover all the pixels in the buffer, but at the moment we allocate only 4096. This means that any buffer following this tiled buffer in the gtt space will be corrupted if pixels belonging to the 2nd and 3rd tiles are written.

Quoted 8 lines

Fix this by rounding up the size of the allocated gtt space to the next tile row address. The page frames beyond the allocation size will be backed by the single gtt scratch page used already elsewhere for similar padding.

Note that this is more of a security/robustness problem and not fixing any reported issue that I know of. This is because applications will normally access only the part of the buffer that is tile row size aligned.

Quoted 8 lines

There should not be any reported issues because all userspace already allocates up to the end of tile-row and stride should be enforced to be a multiple of tile-width. So the use of DIV_ROUND_UP implies a programming error that should have been reported back to userspace earlier. We can extend that by checking to make sure userspace has allocated a valid buffer, that is, it has allocated sufficient pages for the sampler access into the tiled buffer (or reject the set-tiling). -Chris

Quoted 3 lines

Ok, I tested this with older UXA that still allocated non-aligned buffers. If that's not the case any more then rejecting set-tiling if it's called on a non tile-row size aligned buffer would work too.

Meep. A long time ago we got the calcuations wrong (slightly less for gen2), but it was and still is a userspace bug with the potential of handing the GPU.

Quoted 5 lines

A multiple of tile_height tall and a mutiple of tile_width across should always be an exact number of pages (and an exact multiple of tile-row pages). And we should have been obeying that since the introduction of set-tiling (ignoring the aforementioned bugs) - so I'd really like to see any evidence of userspace getting that wrong.

On Ubuntu 12.10, with xf86-video-intel 2.20.9 I get the attached log with the following patch applied:

(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_gem_tiling.c
index 7b0a3b3..846e96f 100644

drivers/gpu/drm/i915/i915_gem_tiling.c +0 -0
@@ -236,9 +236,17 @@ i915_tiling_ok(struct drm_device *dev, int stride,

int size, int tiling_mode)
if (INTEL_INFO(dev)->gen >= 4) {
if (stride & (tile_width - 1))
return false;
- return true;
}

+ if (size % (stride / tile_width * PAGE_SIZE))
+ printk("unaligned tiling: comm %.*s size %u mode %c stride %d
size-mod-tilerow-size %zd\n",
+ (int)sizeof(current->comm), current->comm,
+ size, tiling_mode == I915_TILING_X ? 'X' : 'Y',
+ stride, size % (stride / tile_width * PAGE_SIZE));
+
+ if (INTEL_INFO(dev)->gen >= 4)
+ return true;
+
/* Pre-965 needs power of two tile widths */ if (stride < tile_width)
return false;

dmesg | grep unaligned | cut -c 16- | sort | uniq -c
2 unaligned tiling: comm compiz size 10485760 mode X stride 6656 size-mod-tilerow-size 49152 2 unaligned tiling: comm compiz size 10485760 mode Y stride 6400 size-mod-tilerow-size 40960 1 unaligned tiling: comm compiz size 1572864 mode Y stride 2944 size-mod-tilerow-size 65536 1 unaligned tiling: comm compiz size 163840 mode X stride 6144 size-mod-tilerow-size 16384 1 unaligned tiling: comm compiz size 163840 mode Y stride 1536 size-mod-tilerow-size 16384 2 unaligned tiling: comm compiz size 229376 mode Y stride 1152 size-mod-tilerow-size 8192 1 unaligned tiling: comm compiz size 2621440 mode Y stride 4736 size-mod-tilerow-size 45056 1 unaligned tiling: comm compiz size 262144 mode X stride 5120 size-mod-tilerow-size 16384 2 unaligned tiling: comm compiz size 327680 mode X stride 6144 size-mod-tilerow-size 32768 6 unaligned tiling: comm compiz size 327680 mode X stride 6656 size-mod-tilerow-size 8192 1 unaligned tiling: comm compiz size 327680 mode Y stride 3072 size-mod-tilerow-size 32768 1 unaligned tiling: comm compiz size 327680 mode Y stride 4736 size-mod-tilerow-size 24576 1 unaligned tiling: comm compiz size 3670016 mode X stride 3072 size-mod-tilerow-size 8192 1 unaligned tiling: comm compiz size 458752 mode Y stride 6400 size-mod-tilerow-size 49152 1 unaligned tiling: comm compiz size 524288 mode Y stride 384 size-mod-tilerow-size 8192 1 unaligned tiling: comm compiz size 6291456 mode X stride 6656 size-mod-tilerow-size 8192 2 unaligned tiling: comm compiz size 6291456 mode Y stride 6400 size-mod-tilerow-size 147456 6 unaligned tiling: comm compiz size 65536 mode Y stride 640 size-mod-tilerow-size 4096 1 unaligned tiling: comm unity_support_t size 6291456 mode Y stride 6400 size-mod-tilerow-size 147456
69 unaligned tiling: comm Xorg size 114688 mode X stride 2560 size-mod-tilerow-size 12288
152 unaligned tiling: comm Xorg size 1310720 mode X stride 1536 size-mod-tilerow-size 8192
3 unaligned tiling: comm Xorg size 131072 mode X stride 2560 size-mod-tilerow-size 8192
317 unaligned tiling: comm Xorg size 131072 mode X stride 3072 size-mod-tilerow-size 8192
4 unaligned tiling: comm Xorg size 163840 mode X stride 1536 size-mod-tilerow-size 4096
87 unaligned tiling: comm Xorg size 163840 mode X stride 3072 size-mod-tilerow-size 16384
1104 unaligned tiling: comm Xorg size 163840 mode X stride 3584 size-mod-tilerow-size 20480
1 unaligned tiling: comm Xorg size 163840 mode X stride 4608 size-mod-tilerow-size 16384 1 unaligned tiling: comm Xorg size 163840 mode X stride 6656 size-mod-tilerow-size 4096 2 unaligned tiling: comm Xorg size 196608 mode X stride 2560 size-mod-tilerow-size 12288
12 unaligned tiling: comm Xorg size 196608 mode X stride 3584 size-mod-tilerow-size 24576 20 unaligned tiling: comm Xorg size 196608 mode X stride 4608 size-mod-tilerow-size 12288
2 unaligned tiling: comm Xorg size 2097152 mode X stride 2560 size-mod-tilerow-size 8192
10 unaligned tiling: comm Xorg size 229376 mode X stride 3072 size-mod-tilerow-size 8192
193 unaligned tiling: comm Xorg size 2621440 mode X stride 4608 size-mod-tilerow-size 4096
4 unaligned tiling: comm Xorg size 262144 mode X stride 1536 size-mod-tilerow-size 4096
16 unaligned tiling: comm Xorg size 262144 mode X stride 4608 size-mod-tilerow-size 4096
1 unaligned tiling: comm Xorg size 3145728 mode X stride 5632 size-mod-tilerow-size 36864 3 unaligned tiling: comm Xorg size 327680 mode X stride 1536 size-mod-tilerow-size 8192 2 unaligned tiling: comm Xorg size 393216 mode X stride 4608 size-mod-tilerow-size 24576 3 unaligned tiling: comm Xorg size 40960 mode X stride 1536 size-mod-tilerow-size 4096 1 unaligned tiling: comm Xorg size 5242880 mode X stride 5632 size-mod-tilerow-size 16384
116 unaligned tiling: comm Xorg size 6291456 mode X stride 6656 size-mod-tilerow-size 8192 280 unaligned tiling: comm Xorg size 65536 mode X stride 1536 size-mod-tilerow-size 4096
6 unaligned tiling: comm Xorg size 65536 mode X stride 2560 size-mod-tilerow-size 4096

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#10 lore raw
Quoted 42 lines · Imre Deak
Quoted 9 lines · Chris Wilson

Meep. A long time ago we got the calcuations wrong (slightly less for gen2), but it was and still is a userspace bug with the potential of handing the GPU.

A multiple of tile_height tall and a mutiple of tile_width across should always be an exact number of pages (and an exact multiple of tile-row pages). And we should have been obeying that since the introduction of set-tiling (ignoring the aforementioned bugs) - so I'd really like to see any evidence of userspace getting that wrong.

On Ubuntu 12.10, with xf86-video-intel 2.20.9 I get the attached log with the following patch applied:

drivers/gpu/drm/i915/i915_gem_tiling.c +9 -1
index 7b0a3b3..846e96f 100644
@@ -236,9 +236,17 @@ i915_tiling_ok(struct drm_device *dev, int stride,
 int size, int tiling_mode)
 	if (INTEL_INFO(dev)->gen >= 4) {
 		if (stride & (tile_width - 1))
 			return false;
-		return true;
 	}
 
+	if (size % (stride / tile_width * PAGE_SIZE))
+		printk("unaligned tiling: comm %.*s size %u mode %c stride %d
 size-mod-tilerow-size %zd\n",
+			(int)sizeof(current->comm), current->comm,
+			size, tiling_mode == I915_TILING_X ? 'X' : 'Y',
+			stride, size % (stride / tile_width * PAGE_SIZE));
+
+	if (INTEL_INFO(dev)->gen >= 4)
+		return true;
+
 	/* Pre-965 needs power of two tile widths */
drivers/gpu/drm/i915/i915_gem_tiling.c +0 -0
 	if (stride < tile_width)
 		return false;
Quoted 4 lines

dmesg | grep unaligned | cut -c 16- | sort | uniq -c
2 unaligned tiling: comm compiz size 10485760 mode X stride 6656 size-mod-tilerow-size 49152 2 unaligned tiling: comm compiz size 10485760 mode Y stride 6400 size-mod-tilerow-size 40960 1 unaligned tiling: comm compiz size 1572864 mode Y stride 2944 size-mod-tilerow-size 65536

Hmm, I also forgot to mention we then pluck a bo out of cache returning a potentially larger than required size. However, looks like I need to double check userspace to make sure we are overallocating tile rows.

So we have a choice of overallocating the GTT for fenced regions, or rounding the fence region to a tile row which preserves the existing functional userspace behaviour. The right answer is indeed to modify the behaviour to overallocate physical space so the fence region doesn't overlap another bo. Next up you need to review what happens after a change of tiling and whether we rebind before the next fenced GTT access.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
author
#11 lore raw
Quoted 43 lines · Chris Wilson
Quoted 42 lines · Imre Deak
Quoted 9 lines · Chris Wilson

Meep. A long time ago we got the calcuations wrong (slightly less for gen2), but it was and still is a userspace bug with the potential of handing the GPU.

A multiple of tile_height tall and a mutiple of tile_width across should always be an exact number of pages (and an exact multiple of tile-row pages). And we should have been obeying that since the introduction of set-tiling (ignoring the aforementioned bugs) - so I'd really like to see any evidence of userspace getting that wrong.

On Ubuntu 12.10, with xf86-video-intel 2.20.9 I get the attached log with the following patch applied:

drivers/gpu/drm/i915/i915_gem_tiling.c +9 -1
index 7b0a3b3..846e96f 100644
@@ -236,9 +236,17 @@ i915_tiling_ok(struct drm_device *dev, int stride,
 int size, int tiling_mode)
 	if (INTEL_INFO(dev)->gen >= 4) {
 		if (stride & (tile_width - 1))
 			return false;
-		return true;
 	}
 
+	if (size % (stride / tile_width * PAGE_SIZE))
+		printk("unaligned tiling: comm %.*s size %u mode %c stride %d
 size-mod-tilerow-size %zd\n",
+			(int)sizeof(current->comm), current->comm,
+			size, tiling_mode == I915_TILING_X ? 'X' : 'Y',
+			stride, size % (stride / tile_width * PAGE_SIZE));
+
+	if (INTEL_INFO(dev)->gen >= 4)
+		return true;
+
 	/* Pre-965 needs power of two tile widths */
drivers/gpu/drm/i915/i915_gem_tiling.c +0 -0
 	if (stride < tile_width)
 		return false;
Quoted 8 lines
Quoted 4 lines

dmesg | grep unaligned | cut -c 16- | sort | uniq -c
2 unaligned tiling: comm compiz size 10485760 mode X stride 6656 size-mod-tilerow-size 49152 2 unaligned tiling: comm compiz size 10485760 mode Y stride 6400 size-mod-tilerow-size 40960 1 unaligned tiling: comm compiz size 1572864 mode Y stride 2944 size-mod-tilerow-size 65536

Hmm, I also forgot to mention we then pluck a bo out of cache returning a potentially larger than required size. However, looks like I need to double check userspace to make sure we are overallocating tile rows.

Ok, I checked now the result is somewhat similar with 2.20.17.

Quoted 3 lines

So we have a choice of overallocating the GTT for fenced regions, or rounding the fence region to a tile row which preserves the existing functional userspace behaviour.

Do you mean rounding the size down to the closest tile row address (and program that to the fence reg)? I had such a version, but I thought it got too complicated due to the old HW's power-of-two alignment requirement. Here we would still need to over allocate if the power-of-two size happens to be non tile-row size aligned.. But I guess it's still doable if we want to save some gtt space.

Quoted 5 lines

The right answer is indeed to modify the
behaviour to overallocate physical space so the fence region doesn't overlap another bo. Next up you need to review what happens after a change of tiling and whether we rebind before the next fenced GTT access.

Ah right, atm only an incorrect alignment will cause an unbind, but we would also need to check the new gtt size if we chose to over allocate.

--Imre

author
#12 lore raw

v2 cover drm/i915: fix gtt space allocated for tiled objects

In reply to Imre Deak

At the moment it's possible for user space to allocate a tiled buffer with a size not aligned to its tile row size and corrupt a buffer that follows this in the gtt space. More details on this in the last patch.

I'll also send a v2 i-g-t test case for this.

Tested on Gen5,6.

In v2:
- Instead of always overallocating for unaligned objects, limit the linear size - and overallocate only when necessary for POT fence sizes. Discussed this with Chris Wilson.
- Force a rebind if the GTT size changes after a tiling change. (Chris Wilson)
- Reject tiling if the object size is smaller than the tile row size.

Imre Deak (7):
drm/i915: merge get_gtt_alignment/get_unfenced_gtt_alignment() drm/i915: merge {i965,sandybridge}_write_fence_reg() drm/i915: use gtt_get_size() instead of open coding it drm/i915: factor out i915_gem_get_tile_width()
drm/i915: reject tiling for objects smaller than their tile row size drm/i915: check tile object alignment explicitly drm/i915: fix gtt space allocated for tiled objects

 drivers/gpu/drm/i915/i915_drv.h        |   13 ++-
 drivers/gpu/drm/i915/i915_gem.c        |  183 ++++++++++++++++++--------------
 drivers/gpu/drm/i915/i915_gem_tiling.c |   48 ++++-----
 3 files changed, 138 insertions(+), 106 deletions(-)
Signature · 1 line
1.7.10.4
author
#13 lore raw

PATCH v2 1/7 drm/i915: merge get_gtt_alignment/get_unfenced_gtt_alignment()

The two functions are rather similar, so merge them.

---
 drivers/gpu/drm/i915/i915_drv.h        |    5 ++--
 drivers/gpu/drm/i915/i915_gem.c        |   44 +++++---------------------------
 drivers/gpu/drm/i915/i915_gem_tiling.c |    6 ++---
 3 files changed, 12 insertions(+), 43 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +2 -3
index b1b1b73..154323a 100644
@@ -1566,9 +1566,8 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
-i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
-				    uint32_t size,
-				    int tiling_mode);
+i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
+			    int tiling_mode, bool fenced);
 
 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
 				    enum i915_cache_level cache_level);
drivers/gpu/drm/i915/i915_gem.c +7 -37

69 lines not on this page · raw message

drivers/gpu/drm/i915/i915_gem_tiling.c +3 -3
index 65f1d4f..cb71ded 100644
@@ -374,9 +374,9 @@ i915_gem_set_tiling(struct drm_device *dev, void *data,
 		/* Rebind if we need a change of alignment */
 		if (!obj->map_and_fenceable) {
 			u32 unfenced_alignment =
-				i915_gem_get_unfenced_gtt_alignment(dev,
-								    obj->base.size,
-								    args->tiling_mode);
+				i915_gem_get_gtt_alignment(dev, obj->base.size,
+							    args->tiling_mode,
+							    false);
 			if (obj->gtt_offset & (unfenced_alignment - 1))
 				ret = i915_gem_object_unbind(obj);
 		}
Signature · 1 line
1.7.10.4
author
#14 lore raw

PATCH v2 2/7 drm/i915: merge {i965, sandybridge}_write_fence_reg()

In reply to Imre Deak

The two functions are rather similar, so merge them.

---
 drivers/gpu/drm/i915/i915_gem.c |   44 +++++++++++++--------------------------
 1 file changed, 15 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +15 -29
index d47c143..5e00dc1 100644
@@ -2507,52 +2507,38 @@ int i915_gpu_idle(struct drm_device *dev)
 	return 0;
 }
 
-static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
-					struct drm_i915_gem_object *obj)
-{
-	drm_i915_private_t *dev_priv = dev->dev_private;
-	uint64_t val;
-
-	if (obj) {
-		u32 size = obj->gtt_space->size;
-
-		val = (uint64_t)((obj->gtt_offset + size - 4096) &
-				 0xfffff000) << 32;
-		val |= obj->gtt_offset & 0xfffff000;
-		val |= (uint64_t)((obj->stride / 128) - 1) <<
-			SANDYBRIDGE_FENCE_PITCH_SHIFT;
-
-		if (obj->tiling_mode == I915_TILING_Y)
-			val |= 1 << I965_FENCE_TILING_Y_SHIFT;
-		val |= I965_FENCE_REG_VALID;
-	} else
-		val = 0;
-
-	I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
-	POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
-}
-
 static void i965_write_fence_reg(struct drm_device *dev, int reg,
 				 struct drm_i915_gem_object *obj)
 {
 	drm_i915_private_t *dev_priv = dev->dev_private;
+	int fence_reg;
+	int fence_pitch_shift;
 	uint64_t val;
 
+	if (INTEL_INFO(dev)->gen >= 6) {
+		fence_reg = FENCE_REG_SANDYBRIDGE_0;
+		fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
+	} else {
+		fence_reg = FENCE_REG_965_0;
+		fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
+	}
+
 	if (obj) {
 		u32 size = obj->gtt_space->size;
 
 		val = (uint64_t)((obj->gtt_offset + size - 4096) &
 				 0xfffff000) << 32;
 		val |= obj->gtt_offset & 0xfffff000;
-		val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
+		val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
 		if (obj->tiling_mode == I915_TILING_Y)
 			val |= 1 << I965_FENCE_TILING_Y_SHIFT;
 		val |= I965_FENCE_REG_VALID;
 	} else
 		val = 0;
 
-	I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
-	POSTING_READ(FENCE_REG_965_0 + reg * 8);
+	fence_reg += reg * 8;
+	I915_WRITE64(fence_reg, val);
+	POSTING_READ(fence_reg);
 }
 
 static void i915_write_fence_reg(struct drm_device *dev, int reg,
@@ -2636,7 +2622,7 @@ static void i915_gem_write_fence(struct drm_device *dev, int reg,
 {
 	switch (INTEL_INFO(dev)->gen) {
 	case 7:
-	case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
+	case 6:
 	case 5:
 	case 4: i965_write_fence_reg(dev, reg, obj); break;
 	case 3: i915_write_fence_reg(dev, reg, obj); break;
Signature · 1 line
1.7.10.4
author
#15 lore raw

PATCH v2 3/7 drm/i915: use gtt_get_size() instead of open coding it

In reply to Imre Deak
---
 drivers/gpu/drm/i915/i915_drv.h        |    2 ++
 drivers/gpu/drm/i915/i915_gem.c        |    2 +-
 drivers/gpu/drm/i915/i915_gem_tiling.c |   13 +------------
 3 files changed, 4 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +2 -0
index 154323a..3b73615 100644
@@ -1566,6 +1566,8 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
+i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
+uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
 
drivers/gpu/drm/i915/i915_gem.c +1 -1
index 5e00dc1..aa6653d 100644
@@ -1435,7 +1435,7 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 	obj->fault_mappable = false;
 }
 
-static uint32_t
+uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
 	uint32_t gtt_size;
drivers/gpu/drm/i915/i915_gem_tiling.c +1 -12
index cb71ded..e76f0d8 100644
@@ -272,18 +272,7 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 			return false;
 	}
 
-	/*
-	 * Previous chips need to be aligned to the size of the smallest
-	 * fence register that can contain the object.
-	 */
-	if (INTEL_INFO(obj->base.dev)->gen == 3)
-		size = 1024*1024;
-	else
-		size = 512*1024;
-
-	while (size < obj->base.size)
-		size <<= 1;
-
+	size = i915_gem_get_gtt_size(obj->base.dev, obj->base.size, tiling_mode);
 	if (obj->gtt_space->size != size)
 		return false;
 
Signature · 1 line
1.7.10.4
#16 lore raw

Re: [PATCH v2 3/7] drm/i915: use gtt_get_size() instead of open coding it

Quoted 1 line · Imre Deak

I've applied patches 1-3 from this series, since they look like nice cleanups. Like discussed on irc, I'm not sold on the later ones since I don't see a clear upside ...
-Daniel

Quoted 63 lines
---
 drivers/gpu/drm/i915/i915_drv.h        |    2 ++
 drivers/gpu/drm/i915/i915_gem.c        |    2 +-
 drivers/gpu/drm/i915/i915_gem_tiling.c |   13 +------------
 3 files changed, 4 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +2 -0
index 154323a..3b73615 100644
@@ -1566,6 +1566,8 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
+i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
+uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
 
drivers/gpu/drm/i915/i915_gem.c +1 -1
index 5e00dc1..aa6653d 100644
@@ -1435,7 +1435,7 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 	obj->fault_mappable = false;
 }
 
-static uint32_t
+uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
 	uint32_t gtt_size;
drivers/gpu/drm/i915/i915_gem_tiling.c +1 -12
index cb71ded..e76f0d8 100644
@@ -272,18 +272,7 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 			return false;
 	}
 
-	/*
-	 * Previous chips need to be aligned to the size of the smallest
-	 * fence register that can contain the object.
-	 */
-	if (INTEL_INFO(obj->base.dev)->gen == 3)
-		size = 1024*1024;
-	else
-		size = 512*1024;
-
-	while (size < obj->base.size)
-		size <<= 1;
-
+	size = i915_gem_get_gtt_size(obj->base.dev, obj->base.size, tiling_mode);
 	if (obj->gtt_space->size != size)
 		return false;
 
Signature · 6 lines
1.7.10.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
author
#17 lore raw

Re: [PATCH v2 3/7] drm/i915: use gtt_get_size() instead of open coding it

Quoted 6 lines · Daniel Vetter
Quoted 1 line · Imre Deak

I've applied patches 1-3 from this series, since they look like nice cleanups. Like discussed on irc, I'm not sold on the later ones since I don't see a clear upside ...

I understood there are other ways to corrupt buffers than what this patch would fix, so I agree that it's not a security fix. I would still argue that eliminating this particular way makes things more robust against obscure application bugs.

--Imre

Quoted 65 lines

-Daniel

Quoted 63 lines
---
 drivers/gpu/drm/i915/i915_drv.h        |    2 ++
 drivers/gpu/drm/i915/i915_gem.c        |    2 +-
 drivers/gpu/drm/i915/i915_gem_tiling.c |   13 +------------
 3 files changed, 4 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +2 -0
index 154323a..3b73615 100644
@@ -1566,6 +1566,8 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
+i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
+uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
 
drivers/gpu/drm/i915/i915_gem.c +1 -1
index 5e00dc1..aa6653d 100644
@@ -1435,7 +1435,7 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 	obj->fault_mappable = false;
 }
 
-static uint32_t
+uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
 	uint32_t gtt_size;
drivers/gpu/drm/i915/i915_gem_tiling.c +1 -12
index cb71ded..e76f0d8 100644
@@ -272,18 +272,7 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 			return false;
 	}
 
-	/*
-	 * Previous chips need to be aligned to the size of the smallest
-	 * fence register that can contain the object.
-	 */
-	if (INTEL_INFO(obj->base.dev)->gen == 3)
-		size = 1024*1024;
-	else
-		size = 512*1024;
-
-	while (size < obj->base.size)
-		size <<= 1;
-
+	size = i915_gem_get_gtt_size(obj->base.dev, obj->base.size, tiling_mode);
 	if (obj->gtt_space->size != size)
 		return false;
 
Signature · 6 lines
1.7.10.4

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
author
#18 lore raw

PATCH v2 4/7 drm/i915: factor out i915_gem_get_tile_width()

In reply to Imre Deak

This will be needed by the upcoming patch.

---
 drivers/gpu/drm/i915/i915_drv.h        |    1 +
 drivers/gpu/drm/i915/i915_gem.c        |   12 ++++++++++++
 drivers/gpu/drm/i915/i915_gem_tiling.c |    8 ++------
 3 files changed, 15 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +1 -0
index 3b73615..c863b0f 100644
@@ -1570,6 +1570,7 @@ i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
 uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
+int i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode);
 
 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
 				    enum i915_cache_level cache_level);
drivers/gpu/drm/i915/i915_gem.c +12 -0
index aa6653d..d029e9e 100644
@@ -1435,6 +1435,18 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 	obj->fault_mappable = false;
 }
 
+int
+i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode)
+{
+	BUG_ON(tiling_mode != I915_TILING_Y && tiling_mode != I915_TILING_X);
+
+	if (IS_GEN2(dev) ||
+	    (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
+		return 128;
+	else
+		return 512;
+}
+
 uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
drivers/gpu/drm/i915/i915_gem_tiling.c +2 -6
index e76f0d8..e2f2a71 100644
@@ -210,12 +210,6 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 	if (tiling_mode == I915_TILING_NONE)
 		return true;
 
-	if (IS_GEN2(dev) ||
-	    (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
-		tile_width = 128;
-	else
-		tile_width = 512;
-
 	/* check maximum stride & object size */
 	if (INTEL_INFO(dev)->gen >= 4) {
 		/* i965 stores the end address of the gtt mapping in the fence
@@ -235,6 +229,8 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 		}
 	}
 
+	tile_width = i915_gem_get_tile_width(dev, tiling_mode);
+
 	/* 965+ just needs multiples of tile width */
 	if (INTEL_INFO(dev)->gen >= 4) {
 		if (stride & (tile_width - 1))
Signature · 1 line
1.7.10.4
author
#19 lore raw

PATCH v2 5/7 drm/i915: reject tiling for objects smaller than their tile row size

In reply to Imre Deak

For these objects there isn't enough backing storage even for a single linear pixel line, so asking tiling for them is clearly a programming error.

i915_gem_get_tile_row_size() will be used by a later patch, so export it.

---
 drivers/gpu/drm/i915/i915_drv.h        |    4 ++++
 drivers/gpu/drm/i915/i915_gem.c        |    6 ++++++
 drivers/gpu/drm/i915/i915_gem_tiling.c |    3 +++
 3 files changed, 13 insertions(+)
drivers/gpu/drm/i915/i915_drv.h +4 -0
index c863b0f..e67332f 100644
@@ -1570,6 +1570,10 @@ i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
 uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
+
+size_t
+i915_gem_get_tile_row_size(struct drm_device *dev, int tiling_mode, int stride);
+
 int i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode);
 
 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
drivers/gpu/drm/i915/i915_gem.c +6 -0
index d029e9e..e51a4cd 100644
@@ -1447,6 +1447,12 @@ i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode)
 		return 512;
 }
 
+size_t
+i915_gem_get_tile_row_size(struct drm_device *dev, int tiling_mode, int stride)
+{
+	return stride / i915_gem_get_tile_width(dev, tiling_mode) * PAGE_SIZE;
+}
+
 uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
drivers/gpu/drm/i915/i915_gem_tiling.c +3 -0
index e2f2a71..1a03e41 100644
@@ -229,6 +229,9 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 		}
 	}
 
+	if (size < i915_gem_get_tile_row_size(dev, tiling_mode, stride))
+		return false;
+
 	tile_width = i915_gem_get_tile_width(dev, tiling_mode);
 
 	/* 965+ just needs multiples of tile width */
Signature · 1 line
1.7.10.4
author
#20 lore raw

PATCH v3 5/7 drm/i915: reject tiling for objects smaller than their tile row size

For these objects there isn't enough backing storage even for a single linear pixel line, so asking tiling for them is clearly a programming error.

i915_gem_get_tile_row_size() will be used by a later patch, so export it.

In v3:
- don't use PAGE_SIZE for the tile size as this is only coincidental and for Gen2 not even true (Chris Wilson)
- use the correct tile size of 2048 bytes for Gen2 (Chris Wilson, Daniel Vetter)

---
 drivers/gpu/drm/i915/i915_drv.h        |    4 ++++
 drivers/gpu/drm/i915/i915_gem.c        |    8 ++++++++
 drivers/gpu/drm/i915/i915_gem_tiling.c |    3 +++
 3 files changed, 15 insertions(+)

[ Sending v3 only for this patch, as the rest of the patchset is
unchanged. ]

drivers/gpu/drm/i915/i915_drv.h +4 -0
index c863b0f..e67332f 100644
@@ -1570,6 +1570,10 @@ i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
 uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
 			    int tiling_mode, bool fenced);
+
+size_t
+i915_gem_get_tile_row_size(struct drm_device *dev, int tiling_mode, int stride);
+
 int i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode);
 
 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
drivers/gpu/drm/i915/i915_gem.c +8 -0
index d029e9e..dd185b4 100644
@@ -1447,6 +1447,14 @@ i915_gem_get_tile_width(struct drm_device *dev, int tiling_mode)
 		return 512;
 }
 
+size_t
+i915_gem_get_tile_row_size(struct drm_device *dev, int tiling_mode, int stride)
+{
+	size_t tile_size = IS_GEN2(dev) ? 2048 : 4096;
+
+	return stride / i915_gem_get_tile_width(dev, tiling_mode) * tile_size;
+}
+
 uint32_t
 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
 {
drivers/gpu/drm/i915/i915_gem_tiling.c +3 -0
index e2f2a71..1a03e41 100644
@@ -229,6 +229,9 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 		}
 	}
 
+	if (size < i915_gem_get_tile_row_size(dev, tiling_mode, stride))
+		return false;
+
 	tile_width = i915_gem_get_tile_width(dev, tiling_mode);
 
 	/* 965+ just needs multiples of tile width */
Signature · 1 line
1.7.10.4
author
#21 lore raw

PATCH v2 6/7 drm/i915: check tile object alignment explicitly

In reply to Imre Deak

So far we used the object size for alignment check, since this matched the actual alignment requirement. An upcoming patch will introduce linear and physical fenced sizes making this connection less clear, so use instead the get-alignment function explicitly.

---
 drivers/gpu/drm/i915/i915_gem_tiling.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_gem_tiling.c +5 -1
index 1a03e41..6d69d06 100644
@@ -256,6 +256,7 @@ static bool
 i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 {
 	u32 size;
+	u32 align;
 
 	if (tiling_mode == I915_TILING_NONE)
 		return true;
@@ -275,7 +276,10 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 	if (obj->gtt_space->size != size)
 		return false;
 
-	if (obj->gtt_offset & (size - 1))
+	align = i915_gem_get_gtt_alignment(obj->base.dev, obj->base.size,
+					   tiling_mode, true);
+
+	if (obj->gtt_offset & (align - 1))
 		return false;
 
 	return true;
Signature · 1 line
1.7.10.4
author
#22 lore raw

PATCH v2 7/7 drm/i915: fix gtt space allocated for tiled objects

In reply to Imre Deak

The GTT space needed for tiled objects might be bigger than the linear size programmed into the correpsonding fence register. For example for the following buffer on a Gen5+ HW:

- allocation size: 4 * 4096 bytes
- tiling mode: X tiled
- stride: 1536

This needs 6 * 4096 bytes of GTT space to cover all the pixels in the buffer, but at the moment we allocate only 4 * 4096. This means that any buffer following this tiled buffer in the GTT space will be corrupted if pixels belonging to the last two tiles are written.

Fix this by limiting the linear size to the object's last full tile row offset (3 * 4096 bytes in the above example). Beyond this offset we don't have enough backing storage to hold even a single linear pixel line, so tiling wouldn't work properly anyway. For old HW with power-of-two fence regions we might have to also overallocate GTT space in case the POT offset happens to be not aligned to the tile row size. The page frames for the overallocated area will be backed by the single GTT scratch page used already for padding objects with a POT fence region.

Note that this is more of a security/robustness problem and not fixing any reported issue that I know of. This is because applications will normally access only the part of the buffer that is tile row size aligned.

In v2:
- Instead of always overallocating for unaligned objects, limit the linear size - and overallocate only when necessary for POT fence sizes. Discussed this with Chris Wilson.
- Force buffer-rebind if the GTT size changes after a tiling change. (Chris Wilson)

---
 drivers/gpu/drm/i915/i915_drv.h        |    5 +-
 drivers/gpu/drm/i915/i915_gem.c        |   85 ++++++++++++++++++++++++++------
 drivers/gpu/drm/i915/i915_gem_tiling.c |   18 ++++---
 3 files changed, 84 insertions(+), 24 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +3 -2
index e67332f..22ade39 100644
@@ -1566,10 +1566,11 @@ void i915_gem_free_all_phys_object(struct drm_device *dev);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
 uint32_t
-i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode);
+i915_gem_get_gtt_physical_size(struct drm_device *dev, uint32_t size,
+			       int tiling_mode, int stride);
 uint32_t
 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
-			    int tiling_mode, bool fenced);
+			    int tiling_mode, int stride, bool fenced);
 
 size_t
 i915_gem_get_tile_row_size(struct drm_device *dev, int tiling_mode, int stride);
drivers/gpu/drm/i915/i915_gem.c +70 -15

149 lines not on this page · raw message

drivers/gpu/drm/i915/i915_gem_tiling.c +11 -7
index 6d69d06..1af6f60 100644
@@ -253,7 +253,8 @@ i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
 
 /* Is the current GTT allocation valid for the change in tiling? */
 static bool
-i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
+i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode,
+			 int stride)
 {
 	u32 size;
 	u32 align;
@@ -261,6 +262,11 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 	if (tiling_mode == I915_TILING_NONE)
 		return true;
 
+	size = i915_gem_get_gtt_physical_size(obj->base.dev, obj->base.size,
+					      tiling_mode, stride);
+	if (obj->gtt_space->size != size)
+		return false;
+
 	if (INTEL_INFO(obj->base.dev)->gen >= 4)
 		return true;
 
@@ -272,12 +278,8 @@ i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
 			return false;
 	}
 
-	size = i915_gem_get_gtt_size(obj->base.dev, obj->base.size, tiling_mode);
-	if (obj->gtt_space->size != size)
-		return false;
-
 	align = i915_gem_get_gtt_alignment(obj->base.dev, obj->base.size,
-					   tiling_mode, true);
+					   tiling_mode, stride, true);
 
 	if (obj->gtt_offset & (align - 1))
 		return false;
@@ -361,13 +363,15 @@ i915_gem_set_tiling(struct drm_device *dev, void *data,
 		obj->map_and_fenceable =
 			obj->gtt_space == NULL ||
 			(obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end &&
-			 i915_gem_object_fence_ok(obj, args->tiling_mode));
+			 i915_gem_object_fence_ok(obj, args->tiling_mode,
+						  args->stride));
 
 		/* Rebind if we need a change of alignment */
 		if (!obj->map_and_fenceable) {
 			u32 unfenced_alignment =
 				i915_gem_get_gtt_alignment(dev, obj->base.size,
 							    args->tiling_mode,
+							    args->stride,
 							    false);
 			if (obj->gtt_offset & (unfenced_alignment - 1))
 				ret = i915_gem_object_unbind(obj);
Signature · 1 line
1.7.10.4
#23 lore raw

drivers/gpu/drm/i915/i915_gem_tiling.c i915_gem_set_tiling(struct drm_device *dev, void *data, · line 367

Quoted 7 lines · Imre Deak
drivers/gpu/drm/i915/i915_gem_tiling.c +2 -1
@@ -361,13 +363,15 @@ i915_gem_set_tiling(struct drm_device *dev, void *data,
 		obj->map_and_fenceable =
 			obj->gtt_space == NULL ||
 			(obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end &&
-			 i915_gem_object_fence_ok(obj, args->tiling_mode));
+			 i915_gem_object_fence_ok(obj, args->tiling_mode,
+						  args->stride));

This is a big problem as this will now cause stalls where the code expects none.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre