i915_gem_evict.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "intel_drv.h"
  32. #include "i915_trace.h"
  33. I915_SELFTEST_DECLARE(static struct igt_evict_ctl {
  34. bool fail_if_busy:1;
  35. } igt_evict_ctl;)
  36. static bool ggtt_is_idle(struct drm_i915_private *i915)
  37. {
  38. struct intel_engine_cs *engine;
  39. enum intel_engine_id id;
  40. if (i915->gt.active_requests)
  41. return false;
  42. for_each_engine(engine, i915, id) {
  43. if (!intel_engine_has_kernel_context(engine))
  44. return false;
  45. }
  46. return true;
  47. }
  48. static int ggtt_flush(struct drm_i915_private *i915)
  49. {
  50. int err;
  51. /* Not everything in the GGTT is tracked via vma (otherwise we
  52. * could evict as required with minimal stalling) so we are forced
  53. * to idle the GPU and explicitly retire outstanding requests in
  54. * the hopes that we can then remove contexts and the like only
  55. * bound by their active reference.
  56. */
  57. err = i915_gem_switch_to_kernel_context(i915);
  58. if (err)
  59. return err;
  60. err = i915_gem_wait_for_idle(i915,
  61. I915_WAIT_INTERRUPTIBLE |
  62. I915_WAIT_LOCKED,
  63. MAX_SCHEDULE_TIMEOUT);
  64. if (err)
  65. return err;
  66. GEM_BUG_ON(!ggtt_is_idle(i915));
  67. return 0;
  68. }
  69. static bool
  70. mark_free(struct drm_mm_scan *scan,
  71. struct i915_vma *vma,
  72. unsigned int flags,
  73. struct list_head *unwind)
  74. {
  75. if (i915_vma_is_pinned(vma))
  76. return false;
  77. if (flags & PIN_NONFAULT && i915_vma_has_userfault(vma))
  78. return false;
  79. list_add(&vma->evict_link, unwind);
  80. return drm_mm_scan_add_block(scan, &vma->node);
  81. }
  82. /**
  83. * i915_gem_evict_something - Evict vmas to make room for binding a new one
  84. * @vm: address space to evict from
  85. * @min_size: size of the desired free space
  86. * @alignment: alignment constraint of the desired free space
  87. * @cache_level: cache_level for the desired space
  88. * @start: start (inclusive) of the range from which to evict objects
  89. * @end: end (exclusive) of the range from which to evict objects
  90. * @flags: additional flags to control the eviction algorithm
  91. *
  92. * This function will try to evict vmas until a free space satisfying the
  93. * requirements is found. Callers must check first whether any such hole exists
  94. * already before calling this function.
  95. *
  96. * This function is used by the object/vma binding code.
  97. *
  98. * Since this function is only used to free up virtual address space it only
  99. * ignores pinned vmas, and not object where the backing storage itself is
  100. * pinned. Hence obj->pages_pin_count does not protect against eviction.
  101. *
  102. * To clarify: This is for freeing up virtual address space, not for freeing
  103. * memory in e.g. the shrinker.
  104. */
  105. int
  106. i915_gem_evict_something(struct i915_address_space *vm,
  107. u64 min_size, u64 alignment,
  108. unsigned cache_level,
  109. u64 start, u64 end,
  110. unsigned flags)
  111. {
  112. struct drm_i915_private *dev_priv = vm->i915;
  113. struct drm_mm_scan scan;
  114. struct list_head eviction_list;
  115. struct list_head *phases[] = {
  116. &vm->inactive_list,
  117. &vm->active_list,
  118. NULL,
  119. }, **phase;
  120. struct i915_vma *vma, *next;
  121. struct drm_mm_node *node;
  122. enum drm_mm_insert_mode mode;
  123. int ret;
  124. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  125. trace_i915_gem_evict(vm, min_size, alignment, flags);
  126. /*
  127. * The goal is to evict objects and amalgamate space in LRU order.
  128. * The oldest idle objects reside on the inactive list, which is in
  129. * retirement order. The next objects to retire are those in flight,
  130. * on the active list, again in retirement order.
  131. *
  132. * The retirement sequence is thus:
  133. * 1. Inactive objects (already retired)
  134. * 2. Active objects (will stall on unbinding)
  135. *
  136. * On each list, the oldest objects lie at the HEAD with the freshest
  137. * object on the TAIL.
  138. */
  139. mode = DRM_MM_INSERT_BEST;
  140. if (flags & PIN_HIGH)
  141. mode = DRM_MM_INSERT_HIGH;
  142. if (flags & PIN_MAPPABLE)
  143. mode = DRM_MM_INSERT_LOW;
  144. drm_mm_scan_init_with_range(&scan, &vm->mm,
  145. min_size, alignment, cache_level,
  146. start, end, mode);
  147. /*
  148. * Retire before we search the active list. Although we have
  149. * reasonable accuracy in our retirement lists, we may have
  150. * a stray pin (preventing eviction) that can only be resolved by
  151. * retiring.
  152. */
  153. if (!(flags & PIN_NONBLOCK))
  154. i915_retire_requests(dev_priv);
  155. else
  156. phases[1] = NULL;
  157. search_again:
  158. INIT_LIST_HEAD(&eviction_list);
  159. phase = phases;
  160. do {
  161. list_for_each_entry(vma, *phase, vm_link)
  162. if (mark_free(&scan, vma, flags, &eviction_list))
  163. goto found;
  164. } while (*++phase);
  165. /* Nothing found, clean up and bail out! */
  166. list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
  167. ret = drm_mm_scan_remove_block(&scan, &vma->node);
  168. BUG_ON(ret);
  169. }
  170. /*
  171. * Can we unpin some objects such as idle hw contents,
  172. * or pending flips? But since only the GGTT has global entries
  173. * such as scanouts, rinbuffers and contexts, we can skip the
  174. * purge when inspecting per-process local address spaces.
  175. */
  176. if (!i915_is_ggtt(vm) || flags & PIN_NONBLOCK)
  177. return -ENOSPC;
  178. /*
  179. * Not everything in the GGTT is tracked via VMA using
  180. * i915_vma_move_to_active(), otherwise we could evict as required
  181. * with minimal stalling. Instead we are forced to idle the GPU and
  182. * explicitly retire outstanding requests which will then remove
  183. * the pinning for active objects such as contexts and ring,
  184. * enabling us to evict them on the next iteration.
  185. *
  186. * To ensure that all user contexts are evictable, we perform
  187. * a switch to the perma-pinned kernel context. This all also gives
  188. * us a termination condition, when the last retired context is
  189. * the kernel's there is no more we can evict.
  190. */
  191. if (!ggtt_is_idle(dev_priv)) {
  192. if (I915_SELFTEST_ONLY(igt_evict_ctl.fail_if_busy))
  193. return -EBUSY;
  194. ret = ggtt_flush(dev_priv);
  195. if (ret)
  196. return ret;
  197. cond_resched();
  198. goto search_again;
  199. }
  200. /*
  201. * If we still have pending pageflip completions, drop
  202. * back to userspace to give our workqueues time to
  203. * acquire our locks and unpin the old scanouts.
  204. */
  205. return intel_has_pending_fb_unpin(dev_priv) ? -EAGAIN : -ENOSPC;
  206. found:
  207. /* drm_mm doesn't allow any other other operations while
  208. * scanning, therefore store to-be-evicted objects on a
  209. * temporary list and take a reference for all before
  210. * calling unbind (which may remove the active reference
  211. * of any of our objects, thus corrupting the list).
  212. */
  213. list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
  214. if (drm_mm_scan_remove_block(&scan, &vma->node))
  215. __i915_vma_pin(vma);
  216. else
  217. list_del(&vma->evict_link);
  218. }
  219. /* Unbinding will emit any required flushes */
  220. ret = 0;
  221. list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
  222. __i915_vma_unpin(vma);
  223. if (ret == 0)
  224. ret = i915_vma_unbind(vma);
  225. }
  226. while (ret == 0 && (node = drm_mm_scan_color_evict(&scan))) {
  227. vma = container_of(node, struct i915_vma, node);
  228. ret = i915_vma_unbind(vma);
  229. }
  230. return ret;
  231. }
  232. /**
  233. * i915_gem_evict_for_vma - Evict vmas to make room for binding a new one
  234. * @vm: address space to evict from
  235. * @target: range (and color) to evict for
  236. * @flags: additional flags to control the eviction algorithm
  237. *
  238. * This function will try to evict vmas that overlap the target node.
  239. *
  240. * To clarify: This is for freeing up virtual address space, not for freeing
  241. * memory in e.g. the shrinker.
  242. */
  243. int i915_gem_evict_for_node(struct i915_address_space *vm,
  244. struct drm_mm_node *target,
  245. unsigned int flags)
  246. {
  247. LIST_HEAD(eviction_list);
  248. struct drm_mm_node *node;
  249. u64 start = target->start;
  250. u64 end = start + target->size;
  251. struct i915_vma *vma, *next;
  252. bool check_color;
  253. int ret = 0;
  254. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  255. GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  256. GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  257. trace_i915_gem_evict_node(vm, target, flags);
  258. /* Retire before we search the active list. Although we have
  259. * reasonable accuracy in our retirement lists, we may have
  260. * a stray pin (preventing eviction) that can only be resolved by
  261. * retiring.
  262. */
  263. if (!(flags & PIN_NONBLOCK))
  264. i915_retire_requests(vm->i915);
  265. check_color = vm->mm.color_adjust;
  266. if (check_color) {
  267. /* Expand search to cover neighbouring guard pages (or lack!) */
  268. if (start)
  269. start -= I915_GTT_PAGE_SIZE;
  270. /* Always look at the page afterwards to avoid the end-of-GTT */
  271. end += I915_GTT_PAGE_SIZE;
  272. }
  273. GEM_BUG_ON(start >= end);
  274. drm_mm_for_each_node_in_range(node, &vm->mm, start, end) {
  275. /* If we find any non-objects (!vma), we cannot evict them */
  276. if (node->color == I915_COLOR_UNEVICTABLE) {
  277. ret = -ENOSPC;
  278. break;
  279. }
  280. GEM_BUG_ON(!node->allocated);
  281. vma = container_of(node, typeof(*vma), node);
  282. /* If we are using coloring to insert guard pages between
  283. * different cache domains within the address space, we have
  284. * to check whether the objects on either side of our range
  285. * abutt and conflict. If they are in conflict, then we evict
  286. * those as well to make room for our guard pages.
  287. */
  288. if (check_color) {
  289. if (node->start + node->size == target->start) {
  290. if (node->color == target->color)
  291. continue;
  292. }
  293. if (node->start == target->start + target->size) {
  294. if (node->color == target->color)
  295. continue;
  296. }
  297. }
  298. if (flags & PIN_NONBLOCK &&
  299. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))) {
  300. ret = -ENOSPC;
  301. break;
  302. }
  303. if (flags & PIN_NONFAULT && i915_vma_has_userfault(vma)) {
  304. ret = -ENOSPC;
  305. break;
  306. }
  307. /* Overlap of objects in the same batch? */
  308. if (i915_vma_is_pinned(vma)) {
  309. ret = -ENOSPC;
  310. if (vma->exec_flags &&
  311. *vma->exec_flags & EXEC_OBJECT_PINNED)
  312. ret = -EINVAL;
  313. break;
  314. }
  315. /* Never show fear in the face of dragons!
  316. *
  317. * We cannot directly remove this node from within this
  318. * iterator and as with i915_gem_evict_something() we employ
  319. * the vma pin_count in order to prevent the action of
  320. * unbinding one vma from freeing (by dropping its active
  321. * reference) another in our eviction list.
  322. */
  323. __i915_vma_pin(vma);
  324. list_add(&vma->evict_link, &eviction_list);
  325. }
  326. list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
  327. __i915_vma_unpin(vma);
  328. if (ret == 0)
  329. ret = i915_vma_unbind(vma);
  330. }
  331. return ret;
  332. }
  333. /**
  334. * i915_gem_evict_vm - Evict all idle vmas from a vm
  335. * @vm: Address space to cleanse
  336. *
  337. * This function evicts all vmas from a vm.
  338. *
  339. * This is used by the execbuf code as a last-ditch effort to defragment the
  340. * address space.
  341. *
  342. * To clarify: This is for freeing up virtual address space, not for freeing
  343. * memory in e.g. the shrinker.
  344. */
  345. int i915_gem_evict_vm(struct i915_address_space *vm)
  346. {
  347. struct list_head *phases[] = {
  348. &vm->inactive_list,
  349. &vm->active_list,
  350. NULL
  351. }, **phase;
  352. struct list_head eviction_list;
  353. struct i915_vma *vma, *next;
  354. int ret;
  355. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  356. trace_i915_gem_evict_vm(vm);
  357. /* Switch back to the default context in order to unpin
  358. * the existing context objects. However, such objects only
  359. * pin themselves inside the global GTT and performing the
  360. * switch otherwise is ineffective.
  361. */
  362. if (i915_is_ggtt(vm)) {
  363. ret = ggtt_flush(vm->i915);
  364. if (ret)
  365. return ret;
  366. }
  367. INIT_LIST_HEAD(&eviction_list);
  368. phase = phases;
  369. do {
  370. list_for_each_entry(vma, *phase, vm_link) {
  371. if (i915_vma_is_pinned(vma))
  372. continue;
  373. __i915_vma_pin(vma);
  374. list_add(&vma->evict_link, &eviction_list);
  375. }
  376. } while (*++phase);
  377. ret = 0;
  378. list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
  379. __i915_vma_unpin(vma);
  380. if (ret == 0)
  381. ret = i915_vma_unbind(vma);
  382. }
  383. return ret;
  384. }
  385. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  386. #include "selftests/i915_gem_evict.c"
  387. #endif