i915_gem_evict.c revision 1.3 1 /* $NetBSD: i915_gem_evict.c,v 1.3 2021/12/18 23:45:28 riastradh Exp $ */
2
3 /*
4 * Copyright 2008-2010 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 * Eric Anholt <eric (at) anholt.net>
27 * Chris Wilson <chris (at) chris-wilson.co.uuk>
28 *
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: i915_gem_evict.c,v 1.3 2021/12/18 23:45:28 riastradh Exp $");
33
34 #include <drm/i915_drm.h>
35
36 #include "gem/i915_gem_context.h"
37 #include "gt/intel_gt_requests.h"
38
39 #include "i915_drv.h"
40 #include "i915_trace.h"
41
42 I915_SELFTEST_DECLARE(static struct igt_evict_ctl {
43 bool fail_if_busy:1;
44 } igt_evict_ctl;)
45
46 static int ggtt_flush(struct intel_gt *gt)
47 {
48 /*
49 * Not everything in the GGTT is tracked via vma (otherwise we
50 * could evict as required with minimal stalling) so we are forced
51 * to idle the GPU and explicitly retire outstanding requests in
52 * the hopes that we can then remove contexts and the like only
53 * bound by their active reference.
54 */
55 return intel_gt_wait_for_idle(gt, MAX_SCHEDULE_TIMEOUT);
56 }
57
58 static bool
59 mark_free(struct drm_mm_scan *scan,
60 struct i915_vma *vma,
61 unsigned int flags,
62 struct list_head *unwind)
63 {
64 if (i915_vma_is_pinned(vma))
65 return false;
66
67 list_add(&vma->evict_link, unwind);
68 return drm_mm_scan_add_block(scan, &vma->node);
69 }
70
71 /**
72 * i915_gem_evict_something - Evict vmas to make room for binding a new one
73 * @vm: address space to evict from
74 * @min_size: size of the desired free space
75 * @alignment: alignment constraint of the desired free space
76 * @color: color for the desired space
77 * @start: start (inclusive) of the range from which to evict objects
78 * @end: end (exclusive) of the range from which to evict objects
79 * @flags: additional flags to control the eviction algorithm
80 *
81 * This function will try to evict vmas until a free space satisfying the
82 * requirements is found. Callers must check first whether any such hole exists
83 * already before calling this function.
84 *
85 * This function is used by the object/vma binding code.
86 *
87 * Since this function is only used to free up virtual address space it only
88 * ignores pinned vmas, and not object where the backing storage itself is
89 * pinned. Hence obj->pages_pin_count does not protect against eviction.
90 *
91 * To clarify: This is for freeing up virtual address space, not for freeing
92 * memory in e.g. the shrinker.
93 */
94 int
95 i915_gem_evict_something(struct i915_address_space *vm,
96 u64 min_size, u64 alignment,
97 unsigned long color,
98 u64 start, u64 end,
99 unsigned flags)
100 {
101 struct drm_mm_scan scan;
102 struct list_head eviction_list;
103 struct i915_vma *vma, *next;
104 struct drm_mm_node *node;
105 enum drm_mm_insert_mode mode;
106 struct i915_vma *active;
107 int ret;
108
109 lockdep_assert_held(&vm->mutex);
110 trace_i915_gem_evict(vm, min_size, alignment, flags);
111
112 /*
113 * The goal is to evict objects and amalgamate space in rough LRU order.
114 * Since both active and inactive objects reside on the same list,
115 * in a mix of creation and last scanned order, as we process the list
116 * we sort it into inactive/active, which keeps the active portion
117 * in a rough MRU order.
118 *
119 * The retirement sequence is thus:
120 * 1. Inactive objects (already retired, random order)
121 * 2. Active objects (will stall on unbinding, oldest scanned first)
122 */
123 mode = DRM_MM_INSERT_BEST;
124 if (flags & PIN_HIGH)
125 mode = DRM_MM_INSERT_HIGH;
126 if (flags & PIN_MAPPABLE)
127 mode = DRM_MM_INSERT_LOW;
128 drm_mm_scan_init_with_range(&scan, &vm->mm,
129 min_size, alignment, color,
130 start, end, mode);
131
132 intel_gt_retire_requests(vm->gt);
133
134 search_again:
135 active = NULL;
136 INIT_LIST_HEAD(&eviction_list);
137 list_for_each_entry_safe(vma, next, &vm->bound_list, vm_link) {
138 /*
139 * We keep this list in a rough least-recently scanned order
140 * of active elements (inactive elements are cheap to reap).
141 * New entries are added to the end, and we move anything we
142 * scan to the end. The assumption is that the working set
143 * of applications is either steady state (and thanks to the
144 * userspace bo cache it almost always is) or volatile and
145 * frequently replaced after a frame, which are self-evicting!
146 * Given that assumption, the MRU order of the scan list is
147 * fairly static, and keeping it in least-recently scan order
148 * is suitable.
149 *
150 * To notice when we complete one full cycle, we record the
151 * first active element seen, before moving it to the tail.
152 */
153 if (i915_vma_is_active(vma)) {
154 if (vma == active) {
155 if (flags & PIN_NONBLOCK)
156 break;
157
158 active = ERR_PTR(-EAGAIN);
159 }
160
161 if (active != ERR_PTR(-EAGAIN)) {
162 if (!active)
163 active = vma;
164
165 list_move_tail(&vma->vm_link, &vm->bound_list);
166 continue;
167 }
168 }
169
170 if (mark_free(&scan, vma, flags, &eviction_list))
171 goto found;
172 }
173
174 /* Nothing found, clean up and bail out! */
175 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
176 ret = drm_mm_scan_remove_block(&scan, &vma->node);
177 BUG_ON(ret);
178 }
179
180 /*
181 * Can we unpin some objects such as idle hw contents,
182 * or pending flips? But since only the GGTT has global entries
183 * such as scanouts, rinbuffers and contexts, we can skip the
184 * purge when inspecting per-process local address spaces.
185 */
186 if (!i915_is_ggtt(vm) || flags & PIN_NONBLOCK)
187 return -ENOSPC;
188
189 /*
190 * Not everything in the GGTT is tracked via VMA using
191 * i915_vma_move_to_active(), otherwise we could evict as required
192 * with minimal stalling. Instead we are forced to idle the GPU and
193 * explicitly retire outstanding requests which will then remove
194 * the pinning for active objects such as contexts and ring,
195 * enabling us to evict them on the next iteration.
196 *
197 * To ensure that all user contexts are evictable, we perform
198 * a switch to the perma-pinned kernel context. This all also gives
199 * us a termination condition, when the last retired context is
200 * the kernel's there is no more we can evict.
201 */
202 if (I915_SELFTEST_ONLY(igt_evict_ctl.fail_if_busy))
203 return -EBUSY;
204
205 ret = ggtt_flush(vm->gt);
206 if (ret)
207 return ret;
208
209 cond_resched();
210
211 flags |= PIN_NONBLOCK;
212 goto search_again;
213
214 found:
215 /* drm_mm doesn't allow any other other operations while
216 * scanning, therefore store to-be-evicted objects on a
217 * temporary list and take a reference for all before
218 * calling unbind (which may remove the active reference
219 * of any of our objects, thus corrupting the list).
220 */
221 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
222 if (drm_mm_scan_remove_block(&scan, &vma->node))
223 __i915_vma_pin(vma);
224 else
225 list_del(&vma->evict_link);
226 }
227
228 /* Unbinding will emit any required flushes */
229 ret = 0;
230 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
231 __i915_vma_unpin(vma);
232 if (ret == 0)
233 ret = __i915_vma_unbind(vma);
234 }
235
236 while (ret == 0 && (node = drm_mm_scan_color_evict(&scan))) {
237 vma = container_of(node, struct i915_vma, node);
238 ret = __i915_vma_unbind(vma);
239 }
240
241 return ret;
242 }
243
244 /**
245 * i915_gem_evict_for_vma - Evict vmas to make room for binding a new one
246 * @vm: address space to evict from
247 * @target: range (and color) to evict for
248 * @flags: additional flags to control the eviction algorithm
249 *
250 * This function will try to evict vmas that overlap the target node.
251 *
252 * To clarify: This is for freeing up virtual address space, not for freeing
253 * memory in e.g. the shrinker.
254 */
255 int i915_gem_evict_for_node(struct i915_address_space *vm,
256 struct drm_mm_node *target,
257 unsigned int flags)
258 {
259 LIST_HEAD(eviction_list);
260 struct drm_mm_node *node;
261 u64 start = target->start;
262 u64 end = start + target->size;
263 struct i915_vma *vma, *next;
264 int ret = 0;
265
266 lockdep_assert_held(&vm->mutex);
267 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
268 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
269
270 trace_i915_gem_evict_node(vm, target, flags);
271
272 /*
273 * Retire before we search the active list. Although we have
274 * reasonable accuracy in our retirement lists, we may have
275 * a stray pin (preventing eviction) that can only be resolved by
276 * retiring.
277 */
278 intel_gt_retire_requests(vm->gt);
279
280 if (i915_vm_has_cache_coloring(vm)) {
281 /* Expand search to cover neighbouring guard pages (or lack!) */
282 if (start)
283 start -= I915_GTT_PAGE_SIZE;
284
285 /* Always look at the page afterwards to avoid the end-of-GTT */
286 end += I915_GTT_PAGE_SIZE;
287 }
288 GEM_BUG_ON(start >= end);
289
290 drm_mm_for_each_node_in_range(node, &vm->mm, start, end) {
291 /* If we find any non-objects (!vma), we cannot evict them */
292 if (node->color == I915_COLOR_UNEVICTABLE) {
293 ret = -ENOSPC;
294 break;
295 }
296
297 GEM_BUG_ON(!drm_mm_node_allocated(node));
298 vma = container_of(node, typeof(*vma), node);
299
300 /* If we are using coloring to insert guard pages between
301 * different cache domains within the address space, we have
302 * to check whether the objects on either side of our range
303 * abutt and conflict. If they are in conflict, then we evict
304 * those as well to make room for our guard pages.
305 */
306 if (i915_vm_has_cache_coloring(vm)) {
307 if (node->start + node->size == target->start) {
308 if (node->color == target->color)
309 continue;
310 }
311 if (node->start == target->start + target->size) {
312 if (node->color == target->color)
313 continue;
314 }
315 }
316
317 if (flags & PIN_NONBLOCK &&
318 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))) {
319 ret = -ENOSPC;
320 break;
321 }
322
323 /* Overlap of objects in the same batch? */
324 if (i915_vma_is_pinned(vma)) {
325 ret = -ENOSPC;
326 if (vma->exec_flags &&
327 *vma->exec_flags & EXEC_OBJECT_PINNED)
328 ret = -EINVAL;
329 break;
330 }
331
332 /* Never show fear in the face of dragons!
333 *
334 * We cannot directly remove this node from within this
335 * iterator and as with i915_gem_evict_something() we employ
336 * the vma pin_count in order to prevent the action of
337 * unbinding one vma from freeing (by dropping its active
338 * reference) another in our eviction list.
339 */
340 __i915_vma_pin(vma);
341 list_add(&vma->evict_link, &eviction_list);
342 }
343
344 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
345 __i915_vma_unpin(vma);
346 if (ret == 0)
347 ret = __i915_vma_unbind(vma);
348 }
349
350 return ret;
351 }
352
353 /**
354 * i915_gem_evict_vm - Evict all idle vmas from a vm
355 * @vm: Address space to cleanse
356 *
357 * This function evicts all vmas from a vm.
358 *
359 * This is used by the execbuf code as a last-ditch effort to defragment the
360 * address space.
361 *
362 * To clarify: This is for freeing up virtual address space, not for freeing
363 * memory in e.g. the shrinker.
364 */
365 int i915_gem_evict_vm(struct i915_address_space *vm)
366 {
367 int ret = 0;
368
369 lockdep_assert_held(&vm->mutex);
370 trace_i915_gem_evict_vm(vm);
371
372 /* Switch back to the default context in order to unpin
373 * the existing context objects. However, such objects only
374 * pin themselves inside the global GTT and performing the
375 * switch otherwise is ineffective.
376 */
377 if (i915_is_ggtt(vm)) {
378 ret = ggtt_flush(vm->gt);
379 if (ret)
380 return ret;
381 }
382
383 do {
384 struct i915_vma *vma, *vn;
385 LIST_HEAD(eviction_list);
386
387 list_for_each_entry(vma, &vm->bound_list, vm_link) {
388 if (i915_vma_is_pinned(vma))
389 continue;
390
391 __i915_vma_pin(vma);
392 list_add(&vma->evict_link, &eviction_list);
393 }
394 if (list_empty(&eviction_list))
395 break;
396
397 ret = 0;
398 list_for_each_entry_safe(vma, vn, &eviction_list, evict_link) {
399 __i915_vma_unpin(vma);
400 if (ret == 0)
401 ret = __i915_vma_unbind(vma);
402 if (ret != -EINTR) /* "Get me out of here!" */
403 ret = 0;
404 }
405 } while (ret == 0);
406
407 return ret;
408 }
409
410 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
411 #include "selftests/i915_gem_evict.c"
412 #endif
413