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