i915_gem_shrinker.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. *
  4. * Copyright © 2008-2015 Intel Corporation
  5. */
  6. #include <linux/oom.h>
  7. #include <linux/sched/mm.h>
  8. #include <linux/shmem_fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/swap.h>
  11. #include <linux/pci.h>
  12. #include <linux/dma-buf.h>
  13. #include <linux/vmalloc.h>
  14. #include "gt/intel_gt_requests.h"
  15. #include "gt/intel_gt.h"
  16. #include "i915_trace.h"
  17. static bool swap_available(void)
  18. {
  19. return get_nr_swap_pages() > 0;
  20. }
  21. static bool can_release_pages(struct drm_i915_gem_object *obj)
  22. {
  23. /* Consider only shrinkable ojects. */
  24. if (!i915_gem_object_is_shrinkable(obj))
  25. return false;
  26. /*
  27. * We can only return physical pages to the system if we can either
  28. * discard the contents (because the user has marked them as being
  29. * purgeable) or if we can move their contents out to swap.
  30. */
  31. return swap_available() || obj->mm.madv == I915_MADV_DONTNEED;
  32. }
  33. static bool drop_pages(struct drm_i915_gem_object *obj,
  34. unsigned long shrink, bool trylock_vm)
  35. {
  36. unsigned long flags;
  37. flags = 0;
  38. if (shrink & I915_SHRINK_ACTIVE)
  39. flags |= I915_GEM_OBJECT_UNBIND_ACTIVE;
  40. if (!(shrink & I915_SHRINK_BOUND))
  41. flags |= I915_GEM_OBJECT_UNBIND_TEST;
  42. if (trylock_vm)
  43. flags |= I915_GEM_OBJECT_UNBIND_VM_TRYLOCK;
  44. if (i915_gem_object_unbind(obj, flags) == 0)
  45. return true;
  46. return false;
  47. }
  48. static int try_to_writeback(struct drm_i915_gem_object *obj, unsigned int flags)
  49. {
  50. if (obj->ops->shrink) {
  51. unsigned int shrink_flags = 0;
  52. if (!(flags & I915_SHRINK_ACTIVE))
  53. shrink_flags |= I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT;
  54. if (flags & I915_SHRINK_WRITEBACK)
  55. shrink_flags |= I915_GEM_OBJECT_SHRINK_WRITEBACK;
  56. return obj->ops->shrink(obj, shrink_flags);
  57. }
  58. return 0;
  59. }
  60. /**
  61. * i915_gem_shrink - Shrink buffer object caches
  62. * @ww: i915 gem ww acquire ctx, or NULL
  63. * @i915: i915 device
  64. * @target: amount of memory to make available, in pages
  65. * @nr_scanned: optional output for number of pages scanned (incremental)
  66. * @shrink: control flags for selecting cache types
  67. *
  68. * This function is the main interface to the shrinker. It will try to release
  69. * up to @target pages of main memory backing storage from buffer objects.
  70. * Selection of the specific caches can be done with @flags. This is e.g. useful
  71. * when purgeable objects should be removed from caches preferentially.
  72. *
  73. * Note that it's not guaranteed that released amount is actually available as
  74. * free system memory - the pages might still be in-used to due to other reasons
  75. * (like cpu mmaps) or the mm core has reused them before we could grab them.
  76. * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
  77. * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
  78. *
  79. * Also note that any kind of pinning (both per-vma address space pins and
  80. * backing storage pins at the buffer object level) result in the shrinker code
  81. * having to skip the object.
  82. *
  83. * Returns:
  84. * The number of pages of backing storage actually released.
  85. */
  86. unsigned long
  87. i915_gem_shrink(struct i915_gem_ww_ctx *ww,
  88. struct drm_i915_private *i915,
  89. unsigned long target,
  90. unsigned long *nr_scanned,
  91. unsigned int shrink)
  92. {
  93. const struct {
  94. struct list_head *list;
  95. unsigned int bit;
  96. } phases[] = {
  97. { &i915->mm.purge_list, ~0u },
  98. {
  99. &i915->mm.shrink_list,
  100. I915_SHRINK_BOUND | I915_SHRINK_UNBOUND
  101. },
  102. { NULL, 0 },
  103. }, *phase;
  104. intel_wakeref_t wakeref = 0;
  105. unsigned long count = 0;
  106. unsigned long scanned = 0;
  107. int err = 0, i = 0;
  108. struct intel_gt *gt;
  109. /* CHV + VTD workaround use stop_machine(); need to trylock vm->mutex */
  110. bool trylock_vm = !ww && intel_vm_no_concurrent_access_wa(i915);
  111. trace_i915_gem_shrink(i915, target, shrink);
  112. /*
  113. * Unbinding of objects will require HW access; Let us not wake the
  114. * device just to recover a little memory. If absolutely necessary,
  115. * we will force the wake during oom-notifier.
  116. */
  117. if (shrink & I915_SHRINK_BOUND) {
  118. wakeref = intel_runtime_pm_get_if_in_use(&i915->runtime_pm);
  119. if (!wakeref)
  120. shrink &= ~I915_SHRINK_BOUND;
  121. }
  122. /*
  123. * When shrinking the active list, we should also consider active
  124. * contexts. Active contexts are pinned until they are retired, and
  125. * so can not be simply unbound to retire and unpin their pages. To
  126. * shrink the contexts, we must wait until the gpu is idle and
  127. * completed its switch to the kernel context. In short, we do
  128. * not have a good mechanism for idling a specific context, but
  129. * what we can do is give them a kick so that we do not keep idle
  130. * contexts around longer than is necessary.
  131. */
  132. if (shrink & I915_SHRINK_ACTIVE) {
  133. for_each_gt(gt, i915, i)
  134. /* Retire requests to unpin all idle contexts */
  135. intel_gt_retire_requests(gt);
  136. }
  137. /*
  138. * As we may completely rewrite the (un)bound list whilst unbinding
  139. * (due to retiring requests) we have to strictly process only
  140. * one element of the list at the time, and recheck the list
  141. * on every iteration.
  142. *
  143. * In particular, we must hold a reference whilst removing the
  144. * object as we may end up waiting for and/or retiring the objects.
  145. * This might release the final reference (held by the active list)
  146. * and result in the object being freed from under us. This is
  147. * similar to the precautions the eviction code must take whilst
  148. * removing objects.
  149. *
  150. * Also note that although these lists do not hold a reference to
  151. * the object we can safely grab one here: The final object
  152. * unreferencing and the bound_list are both protected by the
  153. * dev->struct_mutex and so we won't ever be able to observe an
  154. * object on the bound_list with a reference count equals 0.
  155. */
  156. for (phase = phases; phase->list; phase++) {
  157. struct list_head still_in_list;
  158. struct drm_i915_gem_object *obj;
  159. unsigned long flags;
  160. if ((shrink & phase->bit) == 0)
  161. continue;
  162. INIT_LIST_HEAD(&still_in_list);
  163. /*
  164. * We serialize our access to unreferenced objects through
  165. * the use of the struct_mutex. While the objects are not
  166. * yet freed (due to RCU then a workqueue) we still want
  167. * to be able to shrink their pages, so they remain on
  168. * the unbound/bound list until actually freed.
  169. */
  170. spin_lock_irqsave(&i915->mm.obj_lock, flags);
  171. while (count < target &&
  172. (obj = list_first_entry_or_null(phase->list,
  173. typeof(*obj),
  174. mm.link))) {
  175. list_move_tail(&obj->mm.link, &still_in_list);
  176. if (shrink & I915_SHRINK_VMAPS &&
  177. !is_vmalloc_addr(obj->mm.mapping))
  178. continue;
  179. if (!(shrink & I915_SHRINK_ACTIVE) &&
  180. i915_gem_object_is_framebuffer(obj))
  181. continue;
  182. if (!can_release_pages(obj))
  183. continue;
  184. if (!kref_get_unless_zero(&obj->base.refcount))
  185. continue;
  186. spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
  187. /* May arrive from get_pages on another bo */
  188. if (!ww) {
  189. if (!i915_gem_object_trylock(obj, NULL))
  190. goto skip;
  191. } else {
  192. err = i915_gem_object_lock(obj, ww);
  193. if (err)
  194. goto skip;
  195. }
  196. if (drop_pages(obj, shrink, trylock_vm) &&
  197. !__i915_gem_object_put_pages(obj) &&
  198. !try_to_writeback(obj, shrink))
  199. count += obj->base.size >> PAGE_SHIFT;
  200. if (!ww)
  201. i915_gem_object_unlock(obj);
  202. scanned += obj->base.size >> PAGE_SHIFT;
  203. skip:
  204. i915_gem_object_put(obj);
  205. spin_lock_irqsave(&i915->mm.obj_lock, flags);
  206. if (err)
  207. break;
  208. }
  209. list_splice_tail(&still_in_list, phase->list);
  210. spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
  211. if (err)
  212. break;
  213. }
  214. if (shrink & I915_SHRINK_BOUND)
  215. intel_runtime_pm_put(&i915->runtime_pm, wakeref);
  216. if (err)
  217. return err;
  218. if (nr_scanned)
  219. *nr_scanned += scanned;
  220. return count;
  221. }
  222. /**
  223. * i915_gem_shrink_all - Shrink buffer object caches completely
  224. * @i915: i915 device
  225. *
  226. * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
  227. * caches completely. It also first waits for and retires all outstanding
  228. * requests to also be able to release backing storage for active objects.
  229. *
  230. * This should only be used in code to intentionally quiescent the gpu or as a
  231. * last-ditch effort when memory seems to have run out.
  232. *
  233. * Returns:
  234. * The number of pages of backing storage actually released.
  235. */
  236. unsigned long i915_gem_shrink_all(struct drm_i915_private *i915)
  237. {
  238. intel_wakeref_t wakeref;
  239. unsigned long freed = 0;
  240. with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
  241. freed = i915_gem_shrink(NULL, i915, -1UL, NULL,
  242. I915_SHRINK_BOUND |
  243. I915_SHRINK_UNBOUND);
  244. }
  245. return freed;
  246. }
  247. static unsigned long
  248. i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
  249. {
  250. struct drm_i915_private *i915 = shrinker->private_data;
  251. unsigned long num_objects;
  252. unsigned long count;
  253. count = READ_ONCE(i915->mm.shrink_memory) >> PAGE_SHIFT;
  254. num_objects = READ_ONCE(i915->mm.shrink_count);
  255. /*
  256. * Update our preferred vmscan batch size for the next pass.
  257. * Our rough guess for an effective batch size is roughly 2
  258. * available GEM objects worth of pages. That is we don't want
  259. * the shrinker to fire, until it is worth the cost of freeing an
  260. * entire GEM object.
  261. */
  262. if (num_objects) {
  263. unsigned long avg = 2 * count / num_objects;
  264. i915->mm.shrinker->batch =
  265. max((i915->mm.shrinker->batch + avg) >> 1,
  266. 128ul /* default SHRINK_BATCH */);
  267. }
  268. return count;
  269. }
  270. static unsigned long
  271. i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  272. {
  273. struct drm_i915_private *i915 = shrinker->private_data;
  274. unsigned long freed;
  275. sc->nr_scanned = 0;
  276. freed = i915_gem_shrink(NULL, i915,
  277. sc->nr_to_scan,
  278. &sc->nr_scanned,
  279. I915_SHRINK_BOUND |
  280. I915_SHRINK_UNBOUND);
  281. if (sc->nr_scanned < sc->nr_to_scan && current_is_kswapd()) {
  282. intel_wakeref_t wakeref;
  283. with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
  284. freed += i915_gem_shrink(NULL, i915,
  285. sc->nr_to_scan - sc->nr_scanned,
  286. &sc->nr_scanned,
  287. I915_SHRINK_ACTIVE |
  288. I915_SHRINK_BOUND |
  289. I915_SHRINK_UNBOUND |
  290. I915_SHRINK_WRITEBACK);
  291. }
  292. }
  293. return sc->nr_scanned ? freed : SHRINK_STOP;
  294. }
  295. static int
  296. i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
  297. {
  298. struct drm_i915_private *i915 =
  299. container_of(nb, struct drm_i915_private, mm.oom_notifier);
  300. struct drm_i915_gem_object *obj;
  301. unsigned long unevictable, available, freed_pages;
  302. intel_wakeref_t wakeref;
  303. unsigned long flags;
  304. freed_pages = 0;
  305. with_intel_runtime_pm(&i915->runtime_pm, wakeref)
  306. freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
  307. I915_SHRINK_BOUND |
  308. I915_SHRINK_UNBOUND |
  309. I915_SHRINK_WRITEBACK);
  310. /* Because we may be allocating inside our own driver, we cannot
  311. * assert that there are no objects with pinned pages that are not
  312. * being pointed to by hardware.
  313. */
  314. available = unevictable = 0;
  315. spin_lock_irqsave(&i915->mm.obj_lock, flags);
  316. list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
  317. if (!can_release_pages(obj))
  318. unevictable += obj->base.size >> PAGE_SHIFT;
  319. else
  320. available += obj->base.size >> PAGE_SHIFT;
  321. }
  322. spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
  323. if (freed_pages || available)
  324. pr_info("Purging GPU memory, %lu pages freed, "
  325. "%lu pages still pinned, %lu pages left available.\n",
  326. freed_pages, unevictable, available);
  327. *(unsigned long *)ptr += freed_pages;
  328. return NOTIFY_DONE;
  329. }
  330. static int
  331. i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
  332. {
  333. struct drm_i915_private *i915 =
  334. container_of(nb, struct drm_i915_private, mm.vmap_notifier);
  335. struct i915_vma *vma, *next;
  336. unsigned long freed_pages = 0;
  337. intel_wakeref_t wakeref;
  338. struct intel_gt *gt;
  339. int i;
  340. with_intel_runtime_pm(&i915->runtime_pm, wakeref)
  341. freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
  342. I915_SHRINK_BOUND |
  343. I915_SHRINK_UNBOUND |
  344. I915_SHRINK_VMAPS);
  345. /* We also want to clear any cached iomaps as they wrap vmap */
  346. for_each_gt(gt, i915, i) {
  347. mutex_lock(&gt->ggtt->vm.mutex);
  348. list_for_each_entry_safe(vma, next,
  349. &gt->ggtt->vm.bound_list, vm_link) {
  350. unsigned long count = i915_vma_size(vma) >> PAGE_SHIFT;
  351. struct drm_i915_gem_object *obj = vma->obj;
  352. if (!vma->iomap || i915_vma_is_active(vma))
  353. continue;
  354. if (!i915_gem_object_trylock(obj, NULL))
  355. continue;
  356. if (__i915_vma_unbind(vma) == 0)
  357. freed_pages += count;
  358. i915_gem_object_unlock(obj);
  359. }
  360. mutex_unlock(&gt->ggtt->vm.mutex);
  361. }
  362. *(unsigned long *)ptr += freed_pages;
  363. return NOTIFY_DONE;
  364. }
  365. void i915_gem_driver_register__shrinker(struct drm_i915_private *i915)
  366. {
  367. i915->mm.shrinker = shrinker_alloc(0, "drm-i915_gem");
  368. if (!i915->mm.shrinker) {
  369. drm_WARN_ON(&i915->drm, 1);
  370. } else {
  371. i915->mm.shrinker->scan_objects = i915_gem_shrinker_scan;
  372. i915->mm.shrinker->count_objects = i915_gem_shrinker_count;
  373. i915->mm.shrinker->batch = 4096;
  374. i915->mm.shrinker->private_data = i915;
  375. shrinker_register(i915->mm.shrinker);
  376. }
  377. i915->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
  378. drm_WARN_ON(&i915->drm, register_oom_notifier(&i915->mm.oom_notifier));
  379. i915->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
  380. drm_WARN_ON(&i915->drm,
  381. register_vmap_purge_notifier(&i915->mm.vmap_notifier));
  382. }
  383. void i915_gem_driver_unregister__shrinker(struct drm_i915_private *i915)
  384. {
  385. drm_WARN_ON(&i915->drm,
  386. unregister_vmap_purge_notifier(&i915->mm.vmap_notifier));
  387. drm_WARN_ON(&i915->drm,
  388. unregister_oom_notifier(&i915->mm.oom_notifier));
  389. shrinker_free(i915->mm.shrinker);
  390. }
  391. void i915_gem_shrinker_taints_mutex(struct drm_i915_private *i915,
  392. struct mutex *mutex)
  393. {
  394. if (!IS_ENABLED(CONFIG_LOCKDEP))
  395. return;
  396. fs_reclaim_acquire(GFP_KERNEL);
  397. mutex_acquire(&mutex->dep_map, 0, 0, _RET_IP_);
  398. mutex_release(&mutex->dep_map, _RET_IP_);
  399. fs_reclaim_release(GFP_KERNEL);
  400. }
  401. /**
  402. * i915_gem_object_make_unshrinkable - Hide the object from the shrinker. By
  403. * default all object types that support shrinking(see IS_SHRINKABLE), will also
  404. * make the object visible to the shrinker after allocating the system memory
  405. * pages.
  406. * @obj: The GEM object.
  407. *
  408. * This is typically used for special kernel internal objects that can't be
  409. * easily processed by the shrinker, like if they are perma-pinned.
  410. */
  411. void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj)
  412. {
  413. struct drm_i915_private *i915 = obj_to_i915(obj);
  414. unsigned long flags;
  415. /*
  416. * We can only be called while the pages are pinned or when
  417. * the pages are released. If pinned, we should only be called
  418. * from a single caller under controlled conditions; and on release
  419. * only one caller may release us. Neither the two may cross.
  420. */
  421. if (atomic_add_unless(&obj->mm.shrink_pin, 1, 0))
  422. return;
  423. spin_lock_irqsave(&i915->mm.obj_lock, flags);
  424. if (!atomic_fetch_inc(&obj->mm.shrink_pin) &&
  425. !list_empty(&obj->mm.link)) {
  426. list_del_init(&obj->mm.link);
  427. i915->mm.shrink_count--;
  428. i915->mm.shrink_memory -= obj->base.size;
  429. }
  430. spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
  431. }
  432. static void ___i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj,
  433. struct list_head *head)
  434. {
  435. struct drm_i915_private *i915 = obj_to_i915(obj);
  436. unsigned long flags;
  437. if (!i915_gem_object_is_shrinkable(obj))
  438. return;
  439. if (atomic_add_unless(&obj->mm.shrink_pin, -1, 1))
  440. return;
  441. spin_lock_irqsave(&i915->mm.obj_lock, flags);
  442. GEM_BUG_ON(!kref_read(&obj->base.refcount));
  443. if (atomic_dec_and_test(&obj->mm.shrink_pin)) {
  444. GEM_BUG_ON(!list_empty(&obj->mm.link));
  445. list_add_tail(&obj->mm.link, head);
  446. i915->mm.shrink_count++;
  447. i915->mm.shrink_memory += obj->base.size;
  448. }
  449. spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
  450. }
  451. /**
  452. * __i915_gem_object_make_shrinkable - Move the object to the tail of the
  453. * shrinkable list. Objects on this list might be swapped out. Used with
  454. * WILLNEED objects.
  455. * @obj: The GEM object.
  456. *
  457. * DO NOT USE. This is intended to be called on very special objects that don't
  458. * yet have mm.pages, but are guaranteed to have potentially reclaimable pages
  459. * underneath.
  460. */
  461. void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj)
  462. {
  463. ___i915_gem_object_make_shrinkable(obj,
  464. &obj_to_i915(obj)->mm.shrink_list);
  465. }
  466. /**
  467. * __i915_gem_object_make_purgeable - Move the object to the tail of the
  468. * purgeable list. Objects on this list might be swapped out. Used with
  469. * DONTNEED objects.
  470. * @obj: The GEM object.
  471. *
  472. * DO NOT USE. This is intended to be called on very special objects that don't
  473. * yet have mm.pages, but are guaranteed to have potentially reclaimable pages
  474. * underneath.
  475. */
  476. void __i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj)
  477. {
  478. ___i915_gem_object_make_shrinkable(obj,
  479. &obj_to_i915(obj)->mm.purge_list);
  480. }
  481. /**
  482. * i915_gem_object_make_shrinkable - Move the object to the tail of the
  483. * shrinkable list. Objects on this list might be swapped out. Used with
  484. * WILLNEED objects.
  485. * @obj: The GEM object.
  486. *
  487. * MUST only be called on objects which have backing pages.
  488. *
  489. * MUST be balanced with previous call to i915_gem_object_make_unshrinkable().
  490. */
  491. void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj)
  492. {
  493. GEM_BUG_ON(!i915_gem_object_has_pages(obj));
  494. __i915_gem_object_make_shrinkable(obj);
  495. }
  496. /**
  497. * i915_gem_object_make_purgeable - Move the object to the tail of the purgeable
  498. * list. Used with DONTNEED objects. Unlike with shrinkable objects, the
  499. * shrinker will attempt to discard the backing pages, instead of trying to swap
  500. * them out.
  501. * @obj: The GEM object.
  502. *
  503. * MUST only be called on objects which have backing pages.
  504. *
  505. * MUST be balanced with previous call to i915_gem_object_make_unshrinkable().
  506. */
  507. void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj)
  508. {
  509. GEM_BUG_ON(!i915_gem_object_has_pages(obj));
  510. __i915_gem_object_make_purgeable(obj);
  511. }