Skip to content

Commit 172ae5b

Browse files
committed
drm/i915: Fix i915_gem_evict_for_vma (soft-pinning)
Soft-pinning depends upon being able to check for availabilty of an interval and evict overlapping object from a drm_mm range manager very quickly. Currently it uses a linear list, and so performance is dire and not suitable as a general replacement. Worse, the current code will oops if it tries to evict an active buffer. It also helps if the routine reports the correct error codes as expected by its callers and emits a tracepoint upon use. For posterity since the wrong patch was pushed (i.e. that missed these key points and had known bugs), this is the changelog that should have been on commit 506a8e8 ("drm/i915: Add soft-pinning API for execbuffer"): Userspace can pass in an offset that it presumes the object is located at. The kernel will then do its utmost to fit the object into that location. The assumption is that userspace is handling its own object locations (for example along with full-ppgtt) and that the kernel will rarely have to make space for the user's requests. This extends the DRM_IOCTL_I915_GEM_EXECBUFFER2 to do the following: * if the user supplies a virtual address via the execobject->offset *and* sets the EXEC_OBJECT_PINNED flag in execobject->flags, then that object is placed at that offset in the address space selected by the context specifier in execbuffer. * the location must be aligned to the GTT page size, 4096 bytes * as the object is placed exactly as specified, it may be used by this execbuffer call without relocations pointing to it It may fail to do so if: * EINVAL is returned if the object does not have a 4096 byte aligned address * the object conflicts with another pinned object (either pinned by hardware in that address space, e.g. scanouts in the aliasing ppgtt) or within the same batch. EBUSY is returned if the location is pinned by hardware EINVAL is returned if the location is already in use by the batch * EINVAL is returned if the object conflicts with its own alignment (as meets the hardware requirements) or if the placement of the object does not fit within the address space All other execbuffer errors apply. Presence of this execbuf extension may be queried by passing I915_PARAM_HAS_EXEC_SOFTPIN to DRM_IOCTL_I915_GETPARAM and checking for a reported value of 1 (or greater). v2: Combine the hole/adjusted-hole ENOSPC checks v3: More color, more splitting, more blurb. Fixes: 506a8e8 ("drm/i915: Add soft-pinning API for execbuffer") Signed-off-by: Chris Wilson <[email protected]> Reviewed-by: Joonas Lahtinen <[email protected]> Link: http://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 85fd4f5 commit 172ae5b

File tree

5 files changed

+111
-27
lines changed

5 files changed

+111
-27
lines changed

drivers/gpu/drm/i915/i915_drv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3341,7 +3341,8 @@ int __must_check i915_gem_evict_something(struct i915_address_space *vm,
33413341
unsigned cache_level,
33423342
u64 start, u64 end,
33433343
unsigned flags);
3344-
int __must_check i915_gem_evict_for_vma(struct i915_vma *target);
3344+
int __must_check i915_gem_evict_for_vma(struct i915_vma *vma,
3345+
unsigned int flags);
33453346
int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle);
33463347

33473348
/* belongs in i915_gem_gtt.h */

drivers/gpu/drm/i915/i915_gem_evict.c

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -212,45 +212,99 @@ i915_gem_evict_something(struct i915_address_space *vm,
212212
return ret;
213213
}
214214

215-
int
216-
i915_gem_evict_for_vma(struct i915_vma *target)
215+
/**
216+
* i915_gem_evict_for_vma - Evict vmas to make room for binding a new one
217+
* @target: address space and range to evict for
218+
* @flags: additional flags to control the eviction algorithm
219+
*
220+
* This function will try to evict vmas that overlap the target node.
221+
*
222+
* To clarify: This is for freeing up virtual address space, not for freeing
223+
* memory in e.g. the shrinker.
224+
*/
225+
int i915_gem_evict_for_vma(struct i915_vma *target, unsigned int flags)
217226
{
218-
struct drm_mm_node *node, *next;
227+
LIST_HEAD(eviction_list);
228+
struct drm_mm_node *node;
229+
u64 start = target->node.start;
230+
u64 end = start + target->node.size;
231+
struct i915_vma *vma, *next;
232+
bool check_color;
233+
int ret = 0;
219234

220235
lockdep_assert_held(&target->vm->i915->drm.struct_mutex);
236+
trace_i915_gem_evict_vma(target, flags);
237+
238+
check_color = target->vm->mm.color_adjust;
239+
if (check_color) {
240+
/* Expand search to cover neighbouring guard pages (or lack!) */
241+
if (start > target->vm->start)
242+
start -= 4096;
243+
if (end < target->vm->start + target->vm->total)
244+
end += 4096;
245+
}
221246

222-
list_for_each_entry_safe(node, next,
223-
&target->vm->mm.head_node.node_list,
224-
node_list) {
225-
struct i915_vma *vma;
226-
int ret;
227-
228-
if (node->start + node->size <= target->node.start)
229-
continue;
230-
if (node->start >= target->node.start + target->node.size)
247+
drm_mm_for_each_node_in_range(node, &target->vm->mm, start, end) {
248+
/* If we find any non-objects (!vma), we cannot evict them */
249+
if (node->color == I915_COLOR_UNEVICTABLE) {
250+
ret = -ENOSPC;
231251
break;
252+
}
232253

233254
vma = container_of(node, typeof(*vma), node);
234255

235-
if (i915_vma_is_pinned(vma)) {
236-
if (!vma->exec_entry || i915_vma_pin_count(vma) > 1)
237-
/* Object is pinned for some other use */
238-
return -EBUSY;
256+
/* If we are using coloring to insert guard pages between
257+
* different cache domains within the address space, we have
258+
* to check whether the objects on either side of our range
259+
* abutt and conflict. If they are in conflict, then we evict
260+
* those as well to make room for our guard pages.
261+
*/
262+
if (check_color) {
263+
if (vma->node.start + vma->node.size == target->node.start) {
264+
if (vma->node.color == target->node.color)
265+
continue;
266+
}
267+
if (vma->node.start == target->node.start + target->node.size) {
268+
if (vma->node.color == target->node.color)
269+
continue;
270+
}
271+
}
239272

240-
/* We need to evict a buffer in the same batch */
241-
if (vma->exec_entry->flags & EXEC_OBJECT_PINNED)
242-
/* Overlapping fixed objects in the same batch */
243-
return -EINVAL;
273+
if (flags & PIN_NONBLOCK &&
274+
(i915_vma_is_pinned(vma) || i915_vma_is_active(vma))) {
275+
ret = -ENOSPC;
276+
break;
277+
}
244278

245-
return -ENOSPC;
279+
/* Overlap of objects in the same batch? */
280+
if (i915_vma_is_pinned(vma)) {
281+
ret = -ENOSPC;
282+
if (vma->exec_entry &&
283+
vma->exec_entry->flags & EXEC_OBJECT_PINNED)
284+
ret = -EINVAL;
285+
break;
246286
}
247287

248-
ret = i915_vma_unbind(vma);
249-
if (ret)
250-
return ret;
288+
/* Never show fear in the face of dragons!
289+
*
290+
* We cannot directly remove this node from within this
291+
* iterator and as with i915_gem_evict_something() we employ
292+
* the vma pin_count in order to prevent the action of
293+
* unbinding one vma from freeing (by dropping its active
294+
* reference) another in our eviction list.
295+
*/
296+
__i915_vma_pin(vma);
297+
list_add(&vma->exec_list, &eviction_list);
251298
}
252299

253-
return 0;
300+
list_for_each_entry_safe(vma, next, &eviction_list, exec_list) {
301+
list_del_init(&vma->exec_list);
302+
__i915_vma_unpin(vma);
303+
if (ret == 0)
304+
ret = i915_vma_unbind(vma);
305+
}
306+
307+
return ret;
254308
}
255309

256310
/**

drivers/gpu/drm/i915/i915_gem_execbuffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ static void eb_destroy(struct eb_vmas *eb)
274274
exec_list);
275275
list_del_init(&vma->exec_list);
276276
i915_gem_execbuffer_unreserve_vma(vma);
277+
vma->exec_entry = NULL;
277278
i915_vma_put(vma);
278279
}
279280
kfree(eb);

drivers/gpu/drm/i915/i915_trace.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,34 @@ TRACE_EVENT(i915_gem_evict_vm,
450450
TP_printk("dev=%d, vm=%p", __entry->dev, __entry->vm)
451451
);
452452

453+
TRACE_EVENT(i915_gem_evict_vma,
454+
TP_PROTO(struct i915_vma *vma, unsigned int flags),
455+
TP_ARGS(vma, flags),
456+
457+
TP_STRUCT__entry(
458+
__field(u32, dev)
459+
__field(struct i915_address_space *, vm)
460+
__field(u64, start)
461+
__field(u64, size)
462+
__field(unsigned long, color)
463+
__field(unsigned int, flags)
464+
),
465+
466+
TP_fast_assign(
467+
__entry->dev = vma->vm->i915->drm.primary->index;
468+
__entry->vm = vma->vm;
469+
__entry->start = vma->node.start;
470+
__entry->size = vma->node.size;
471+
__entry->color = vma->node.color;
472+
__entry->flags = flags;
473+
),
474+
475+
TP_printk("dev=%d, vm=%p, start=%llx size=%llx, color=%lx, flags=%x",
476+
__entry->dev, __entry->vm,
477+
__entry->start, __entry->size,
478+
__entry->color, __entry->flags)
479+
);
480+
453481
TRACE_EVENT(i915_gem_ring_sync_to,
454482
TP_PROTO(struct drm_i915_gem_request *to,
455483
struct drm_i915_gem_request *from),

drivers/gpu/drm/i915/i915_vma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
401401
vma->node.color = obj->cache_level;
402402
ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
403403
if (ret) {
404-
ret = i915_gem_evict_for_vma(vma);
404+
ret = i915_gem_evict_for_vma(vma, flags);
405405
if (ret == 0)
406406
ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
407407
if (ret)

0 commit comments

Comments
 (0)