drm/i915: Prevent negative relocation deltas from wrapping
Subsystems (2)
PATCH 1/1 drm/i915: Prevent negative relocation deltas from wrapping
From: Chris Wilson <[email protected]>
This is pure evil. Userspace, I'm looking at you SNA, repacks batch buffers on the fly after generation as they are being passed to the kernel for execution. These batches also contain self-referenced relocations as a single buffer encompasses the state commands, kernels, vertices and sampler. During generation the buffers are placed at known offsets within the full batch, and then the relocation deltas (as passed to the kernel) are tweaked as the batch is repacked into a smaller buffer. This means that userspace is passing negative relocations deltas, which subsequently wrap to large values if the batch is at a low address. The GPU hangs when it then tries to use the large value as a base for its address offsets, rather than wrapping back to the real value (as one would hope). As the GPU uses positive offsets from the base, we can treat the relocation address as the minimum address read by the GPU. For the upper bound, we trust that userspace will not read beyond the end of the buffer.
So, how do we fix negative relocations from wrapping? We can either check that every relocation looks valid when we write it, and then position each object such that we prevent the offset wraparound, or we just special-case the self-referential behaviour of SNA and force all batches to be above 256k. Daniel prefers the latter approach.
This fixes a GPU hang when it tries to use an address (relocation + offset) greater than the GTT size. The issue would occur quite easily with full-ppgtt as each fd gets its own VM space, so low offsets would often be handed out. However, with the rearrangement of the low GTT due to capturing the BIOS framebuffer, it is already affecting kernels 3.15 onwards. I think only IVB+ is susceptible to this bug, but the workaround should only kick in rarely, so it seems sensible to always apply it.
v3: Use a bias for batch buffers to prevent small negative delta relocations from wrapping.
v4 from Daniel:
- s/BIAS/BATCH_OFFSET_BIAS/
- Extract eb_vma_misplaced/i915_vma_misplaced since the conditions were growing rather cumbersome.
- Add a comment to eb_get_batch explaining why we do this.
- Apply the batch offset bias everywhere but mention that we've only observed it on gen7 gpus.
- Drop PIN_OFFSET_FIX for now, that slipped in from a feature patch.
v5: Add static to eb_get_batch, spotted by 0-day tester.
Testcase: igt/gem_bad_reloc
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=78533
--- drivers/gpu/drm/i915/i915_drv.h | 6 ++- drivers/gpu/drm/i915/i915_gem.c | 46 +++++++++++++----- drivers/gpu/drm/i915/i915_gem_evict.c | 9 ++-- drivers/gpu/drm/i915/i915_gem_execbuffer.c | 76 ++++++++++++++++++++++-------- drivers/gpu/drm/i915/i915_gem_gtt.c | 4 +- 5 files changed, 103 insertions(+), 38 deletions(-)
drivers/gpu/drm/i915/i915_drv.h +5 -1
@@ -2189,10 +2189,12 @@ void i915_gem_vma_destroy(struct i915_vma *vma);#define PIN_MAPPABLE 0x1#define PIN_NONBLOCK 0x2#define PIN_GLOBAL 0x4 +#define PIN_OFFSET_BIAS 0x8 +#define PIN_OFFSET_MASK (~4095)int __must_check i915_gem_object_pin (struct drm_i915_gem_object *obj,struct i915_address_space *vm,uint32_t alignment, -unsigned flags); +uint64_t flags);int __must_check i915_vma_unbind (struct i915_vma *vma);int i915_gem_object_put_pages (struct drm_i915_gem_object *obj);void i915_gem_release_all_mmaps (struct drm_i915_private *dev_priv);
@@ -2445,6 +2447,8 @@ int __must_check i915_gem_evict_something(struct drm_device *dev,int min_size, unsigned alignment, unsigned cache_level, + unsigned long start, + unsigned long end, unsigned flags);int i915_gem_evict_vm (struct i915_address_space *vm,bool do_idle);int i915_gem_evict_everything (struct drm_device *dev);
drivers/gpu/drm/i915/i915_gem.c +34 -12
95 lines not on this page · raw message
drivers/gpu/drm/i915/i915_gem_evict.c +4 -5
@@ -68,9 +68,9 @@ mark_free(struct i915_vma *vma, struct list_head *unwind)int i915_gem_evict_something (struct drm_device *dev,struct i915_address_space *vm,int min_size,unsigned alignment,unsigned cache_level, +unsigned long start,unsigned long end,unsigned flags) { -struct drm_i915_private *dev_priv = dev->dev_private;struct list_head eviction_list, unwind_list;struct i915_vma *vma;int ret =0 ;
@@ -102,11 +102,10 @@ i915_gem_evict_something(struct drm_device *dev, struct i915_address_space *vm,*/ INIT_LIST_HEAD (&unwind_list); -if (flags &PIN_MAPPABLE ) { -BUG_ON (!i915_is_ggtt (vm)); +if (start !=0 || end != vm->total) {drm_mm_init_scan_with_range (&vm->mm, min_size, - alignment, cache_level,0 , - dev_priv->gtt.mappable_end); + alignment, cache_level, + start, end); }else drm_mm_init_scan (&vm->mm, min_size, alignment, cache_level);
drivers/gpu/drm/i915/i915_gem_execbuffer.c +57 -19
121 lines not on this page · raw message
drivers/gpu/drm/i915/i915_gem_gtt.c +3 -1
@@ -1089,7 +1089,9 @@ alloc:if (ret == -ENOSPC && !retried) { ret =i915_gem_evict_something (dev, &dev_priv->gtt.base,GEN6_PD_SIZE ,GEN6_PD_ALIGN , -I915_CACHE_NONE ,0 ); +I915_CACHE_NONE , +0 , dev_priv->gtt.base.total, +0 );if (ret)return ret;
Signature · 1 line
1.9.2
Quoted 8 lines · Daniel Vetter
- Apply the batch offset bias everywhere but mention that we've only observed it on gen7 gpus.
drivers/gpu/drm/i915/i915_gem_execbuffer.c +5
-0+static struct drm_i915_gem_object * +eb_get_batch (struct eb_vmas *eb) +{ +struct i915_vma *vma =list_entry (eb->vmas.prev,typeof (*vma), exec_list); +
drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_reset_gen7_sol_offsets(struct drm_device *dev, · line 1035
Quoted 10 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +10
-0+/* +* SNA is doing fancy tricks with compressing batch buffers, which leads +* to negative relocation deltas. Usually that works out ok since the +* relocate address is still positive, except when the batch is placed +* very low in the GTT. Ensure this doesn't happen. +* +* Note that actual hangs have only been observed on gen7, but for +* paranoia do it everywhere. +*/ + vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
This actually conflicts with ilk and earlier generating EBUSY when mixed with pinned buffers.
-Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Quoted 14 lines · Chris Wilson
Quoted 13 lines · Daniel Vetter
- Apply the batch offset bias everywhere but mention that we've only observed it on gen7 gpus.
drivers/gpu/drm/i915/i915_gem_execbuffer.c +10
-0+static struct drm_i915_gem_object * +eb_get_batch (struct eb_vmas *eb) +{ +struct i915_vma *vma =list_entry (eb->vmas.prev,typeof (*vma), exec_list); + +/* +* SNA is doing fancy tricks with compressing batch buffers, which leads +* to negative relocation deltas. Usually that works out ok since the +* relocate address is still positive, except when the batch is placed +* very low in the GTT. Ensure this doesn't happen.
Quoted 8 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_reset_gen7_sol_offsets(struct drm_device *dev, · line 1035
Quoted 5 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +5
-0+* +* Note that actual hangs have only been observed on gen7, but for +* paranoia do it everywhere. +*/ + vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;This actually conflicts with ilk and earlier generating EBUSY when mixed with pinned buffers.
Hm, now I'm confused since I seem to completely lack context. With what kind of pinned buffers does this conflict and how? Can you please elaborate?
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Quoted 24 lines · Daniel Vetter
Quoted 14 lines · Chris Wilson
Quoted 13 lines · Daniel Vetter
- Apply the batch offset bias everywhere but mention that we've only observed it on gen7 gpus.
drivers/gpu/drm/i915/i915_gem_execbuffer.c +10
-0+static struct drm_i915_gem_object * +eb_get_batch (struct eb_vmas *eb) +{ +struct i915_vma *vma =list_entry (eb->vmas.prev,typeof (*vma), exec_list); + +/* +* SNA is doing fancy tricks with compressing batch buffers, which leads +* to negative relocation deltas. Usually that works out ok since the +* relocate address is still positive, except when the batch is placed +* very low in the GTT. Ensure this doesn't happen. Quoted 8 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c i915_reset_gen7_sol_offsets(struct drm_device *dev, · line 1035
Quoted 5 lines
drivers/gpu/drm/i915/i915_gem_execbuffer.c +5
-0+* +* Note that actual hangs have only been observed on gen7, but for +* paranoia do it everywhere. +*/ + vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;This actually conflicts with ilk and earlier generating EBUSY when mixed with pinned buffers.
Quoted 3 lines
Hm, now I'm confused since I seem to completely lack context. With what kind of pinned buffers does this conflict and how? Can you please elaborate?
A pinned buffer used as a batch in the low range. Depends on initial framebuffer placement and availability of the api.
-Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre