Commit 6c085a72 authored by Chris Wilson's avatar Chris Wilson Committed by Daniel Vetter

drm/i915: Track unbound pages

When dealing with a working set larger than the GATT, or even the
mappable aperture when touching through the GTT, we end up with evicting
objects only to rebind them at a new offset again later. Moving an
object into and out of the GTT requires clflushing the pages, thus
causing a double-clflush penalty for rebinding.

To avoid having to clflush on rebinding, we can track the pages as they
are evicted from the GTT and only relinquish those pages on memory
pressure.

As usual, if it were not for the handling of out-of-memory condition and
having to manually shrink our own bo caches, it would be a net reduction
of code. Alas.

Note: The patch also contains a few changes to the last-hope
evict_everything logic in i916_gem_execbuffer.c - we no longer try to
only evict the purgeable stuff in a first try (since that's superflous
and only helps in OOM corner-cases, not fragmented-gtt trashing
situations).

Also, the extraction of the get_pages retry loop from bind_to_gtt (and
other callsites) to get_pages should imo have been a separate patch.

v2: Ditch the newly added put_pages (for unbound objects only) in
i915_gem_reset. A quick irc discussion hasn't revealed any important
reason for this, so if we need this, I'd like to have a git blame'able
explanation for it.

v3: Undo the s/drm_malloc_ab/kmalloc/ in get_pages that Chris noticed.
Signed-off-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
[danvet: Split out code movements and rant a bit in the commit message
with a few Notes. Done v2]
Signed-off-by: default avatarDaniel Vetter <daniel.vetter@ffwll.ch>
parent 225067ee
...@@ -211,7 +211,7 @@ static int i915_gem_object_info(struct seq_file *m, void* data) ...@@ -211,7 +211,7 @@ static int i915_gem_object_info(struct seq_file *m, void* data)
dev_priv->mm.object_memory); dev_priv->mm.object_memory);
size = count = mappable_size = mappable_count = 0; size = count = mappable_size = mappable_count = 0;
count_objects(&dev_priv->mm.gtt_list, gtt_list); count_objects(&dev_priv->mm.bound_list, gtt_list);
seq_printf(m, "%u [%u] objects, %zu [%zu] bytes in gtt\n", seq_printf(m, "%u [%u] objects, %zu [%zu] bytes in gtt\n",
count, mappable_count, size, mappable_size); count, mappable_count, size, mappable_size);
...@@ -225,8 +225,13 @@ static int i915_gem_object_info(struct seq_file *m, void* data) ...@@ -225,8 +225,13 @@ static int i915_gem_object_info(struct seq_file *m, void* data)
seq_printf(m, " %u [%u] inactive objects, %zu [%zu] bytes\n", seq_printf(m, " %u [%u] inactive objects, %zu [%zu] bytes\n",
count, mappable_count, size, mappable_size); count, mappable_count, size, mappable_size);
size = count = 0;
list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
size += obj->base.size, ++count;
seq_printf(m, "%u unbound objects, %zu bytes\n", count, size);
size = count = mappable_size = mappable_count = 0; size = count = mappable_size = mappable_count = 0;
list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) { list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
if (obj->fault_mappable) { if (obj->fault_mappable) {
size += obj->gtt_space->size; size += obj->gtt_space->size;
++count; ++count;
...@@ -264,7 +269,7 @@ static int i915_gem_gtt_info(struct seq_file *m, void* data) ...@@ -264,7 +269,7 @@ static int i915_gem_gtt_info(struct seq_file *m, void* data)
return ret; return ret;
total_obj_size = total_gtt_size = count = 0; total_obj_size = total_gtt_size = count = 0;
list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) { list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
if (list == PINNED_LIST && obj->pin_count == 0) if (list == PINNED_LIST && obj->pin_count == 0)
continue; continue;
...@@ -526,7 +531,8 @@ static int i915_gem_fence_regs_info(struct seq_file *m, void *data) ...@@ -526,7 +531,8 @@ static int i915_gem_fence_regs_info(struct seq_file *m, void *data)
for (i = 0; i < dev_priv->num_fence_regs; i++) { for (i = 0; i < dev_priv->num_fence_regs; i++) {
struct drm_i915_gem_object *obj = dev_priv->fence_regs[i].obj; struct drm_i915_gem_object *obj = dev_priv->fence_regs[i].obj;
seq_printf(m, "Fenced object[%2d] = ", i); seq_printf(m, "Fence %d, pin count = %d, object = ",
i, dev_priv->fence_regs[i].pin_count);
if (obj == NULL) if (obj == NULL)
seq_printf(m, "unused"); seq_printf(m, "unused");
else else
......
...@@ -685,7 +685,13 @@ typedef struct drm_i915_private { ...@@ -685,7 +685,13 @@ typedef struct drm_i915_private {
struct drm_mm gtt_space; struct drm_mm gtt_space;
/** List of all objects in gtt_space. Used to restore gtt /** List of all objects in gtt_space. Used to restore gtt
* mappings on resume */ * mappings on resume */
struct list_head gtt_list; struct list_head bound_list;
/**
* List of objects which are not bound to the GTT (thus
* are idle and not used by the GPU) but still have
* (presumably uncached) pages still attached.
*/
struct list_head unbound_list;
/** Usable portion of the GTT for GEM */ /** Usable portion of the GTT for GEM */
unsigned long gtt_start; unsigned long gtt_start;
...@@ -1306,8 +1312,7 @@ int __must_check i915_gem_object_unbind(struct drm_i915_gem_object *obj); ...@@ -1306,8 +1312,7 @@ int __must_check i915_gem_object_unbind(struct drm_i915_gem_object *obj);
void i915_gem_release_mmap(struct drm_i915_gem_object *obj); void i915_gem_release_mmap(struct drm_i915_gem_object *obj);
void i915_gem_lastclose(struct drm_device *dev); void i915_gem_lastclose(struct drm_device *dev);
int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj, int __must_check i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj);
gfp_t gfpmask);
int __must_check i915_mutex_lock_interruptible(struct drm_device *dev); int __must_check i915_mutex_lock_interruptible(struct drm_device *dev);
int i915_gem_object_sync(struct drm_i915_gem_object *obj, int i915_gem_object_sync(struct drm_i915_gem_object *obj,
struct intel_ring_buffer *to); struct intel_ring_buffer *to);
...@@ -1449,7 +1454,7 @@ int __must_check i915_gem_evict_something(struct drm_device *dev, int min_size, ...@@ -1449,7 +1454,7 @@ int __must_check i915_gem_evict_something(struct drm_device *dev, int min_size,
unsigned alignment, unsigned alignment,
unsigned cache_level, unsigned cache_level,
bool mappable); bool mappable);
int i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only); int i915_gem_evict_everything(struct drm_device *dev);
/* i915_gem_stolen.c */ /* i915_gem_stolen.c */
int i915_gem_init_stolen(struct drm_device *dev); int i915_gem_init_stolen(struct drm_device *dev);
......
This diff is collapsed.
...@@ -33,7 +33,7 @@ static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachme ...@@ -33,7 +33,7 @@ static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachme
struct drm_i915_gem_object *obj = attachment->dmabuf->priv; struct drm_i915_gem_object *obj = attachment->dmabuf->priv;
struct drm_device *dev = obj->base.dev; struct drm_device *dev = obj->base.dev;
int npages = obj->base.size / PAGE_SIZE; int npages = obj->base.size / PAGE_SIZE;
struct sg_table *sg = NULL; struct sg_table *sg;
int ret; int ret;
int nents; int nents;
...@@ -41,9 +41,9 @@ static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachme ...@@ -41,9 +41,9 @@ static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachme
if (ret) if (ret)
return ERR_PTR(ret); return ERR_PTR(ret);
if (!obj->pages) { ret = i915_gem_object_get_pages_gtt(obj);
ret = i915_gem_object_get_pages_gtt(obj, __GFP_NORETRY | __GFP_NOWARN); if (ret) {
if (ret) sg = ERR_PTR(ret);
goto out; goto out;
} }
...@@ -89,13 +89,11 @@ static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf) ...@@ -89,13 +89,11 @@ static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
goto out_unlock; goto out_unlock;
} }
if (!obj->pages) { ret = i915_gem_object_get_pages_gtt(obj);
ret = i915_gem_object_get_pages_gtt(obj, __GFP_NORETRY | __GFP_NOWARN);
if (ret) { if (ret) {
mutex_unlock(&dev->struct_mutex); mutex_unlock(&dev->struct_mutex);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
}
obj->dma_buf_vmapping = vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL); obj->dma_buf_vmapping = vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL);
if (!obj->dma_buf_vmapping) { if (!obj->dma_buf_vmapping) {
......
...@@ -148,7 +148,7 @@ i915_gem_evict_something(struct drm_device *dev, int min_size, ...@@ -148,7 +148,7 @@ i915_gem_evict_something(struct drm_device *dev, int min_size,
} }
int int
i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only) i915_gem_evict_everything(struct drm_device *dev)
{ {
drm_i915_private_t *dev_priv = dev->dev_private; drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_gem_object *obj, *next; struct drm_i915_gem_object *obj, *next;
...@@ -160,7 +160,7 @@ i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only) ...@@ -160,7 +160,7 @@ i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only)
if (lists_empty) if (lists_empty)
return -ENOSPC; return -ENOSPC;
trace_i915_gem_evict_everything(dev, purgeable_only); trace_i915_gem_evict_everything(dev);
/* The gpu_idle will flush everything in the write domain to the /* The gpu_idle will flush everything in the write domain to the
* active list. Then we must move everything off the active list * active list. Then we must move everything off the active list
...@@ -174,12 +174,9 @@ i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only) ...@@ -174,12 +174,9 @@ i915_gem_evict_everything(struct drm_device *dev, bool purgeable_only)
/* Having flushed everything, unbind() should never raise an error */ /* Having flushed everything, unbind() should never raise an error */
list_for_each_entry_safe(obj, next, list_for_each_entry_safe(obj, next,
&dev_priv->mm.inactive_list, mm_list) { &dev_priv->mm.inactive_list, mm_list)
if (!purgeable_only || obj->madv != I915_MADV_WILLNEED) {
if (obj->pin_count == 0) if (obj->pin_count == 0)
WARN_ON(i915_gem_object_unbind(obj)); WARN_ON(i915_gem_object_unbind(obj));
}
}
return 0; return 0;
} }
...@@ -502,17 +502,12 @@ i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring, ...@@ -502,17 +502,12 @@ i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
} }
} }
if (ret != -ENOSPC || retry > 1) if (ret != -ENOSPC || retry++)
return ret; return ret;
/* First attempt, just clear anything that is purgeable. ret = i915_gem_evict_everything(ring->dev);
* Second attempt, clear the entire GTT.
*/
ret = i915_gem_evict_everything(ring->dev, retry == 0);
if (ret) if (ret)
return ret; return ret;
retry++;
} while (1); } while (1);
err: err:
......
...@@ -348,7 +348,7 @@ void i915_gem_restore_gtt_mappings(struct drm_device *dev) ...@@ -348,7 +348,7 @@ void i915_gem_restore_gtt_mappings(struct drm_device *dev)
intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE, intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE,
(dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE); (dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE);
list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) { list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
i915_gem_clflush_object(obj); i915_gem_clflush_object(obj);
i915_gem_gtt_bind_object(obj, obj->cache_level); i915_gem_gtt_bind_object(obj, obj->cache_level);
} }
......
...@@ -1221,7 +1221,7 @@ static void i915_capture_error_state(struct drm_device *dev) ...@@ -1221,7 +1221,7 @@ static void i915_capture_error_state(struct drm_device *dev)
list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list)
i++; i++;
error->active_bo_count = i; error->active_bo_count = i;
list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
if (obj->pin_count) if (obj->pin_count)
i++; i++;
error->pinned_bo_count = i - error->active_bo_count; error->pinned_bo_count = i - error->active_bo_count;
...@@ -1246,7 +1246,7 @@ static void i915_capture_error_state(struct drm_device *dev) ...@@ -1246,7 +1246,7 @@ static void i915_capture_error_state(struct drm_device *dev)
error->pinned_bo_count = error->pinned_bo_count =
capture_pinned_bo(error->pinned_bo, capture_pinned_bo(error->pinned_bo,
error->pinned_bo_count, error->pinned_bo_count,
&dev_priv->mm.gtt_list); &dev_priv->mm.bound_list);
do_gettimeofday(&error->time); do_gettimeofday(&error->time);
......
...@@ -214,22 +214,18 @@ TRACE_EVENT(i915_gem_evict, ...@@ -214,22 +214,18 @@ TRACE_EVENT(i915_gem_evict,
); );
TRACE_EVENT(i915_gem_evict_everything, TRACE_EVENT(i915_gem_evict_everything,
TP_PROTO(struct drm_device *dev, bool purgeable), TP_PROTO(struct drm_device *dev),
TP_ARGS(dev, purgeable), TP_ARGS(dev),
TP_STRUCT__entry( TP_STRUCT__entry(
__field(u32, dev) __field(u32, dev)
__field(bool, purgeable)
), ),
TP_fast_assign( TP_fast_assign(
__entry->dev = dev->primary->index; __entry->dev = dev->primary->index;
__entry->purgeable = purgeable;
), ),
TP_printk("dev=%d%s", TP_printk("dev=%d", __entry->dev)
__entry->dev,
__entry->purgeable ? ", purgeable only" : "")
); );
TRACE_EVENT(i915_gem_ring_dispatch, TRACE_EVENT(i915_gem_ring_dispatch,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment