i915_gem_shrinker.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Copyright © 2008-2015 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. */
  24. #include <linux/oom.h>
  25. #include <linux/sched/mm.h>
  26. #include <linux/shmem_fs.h>
  27. #include <linux/slab.h>
  28. #include <linux/swap.h>
  29. #include <linux/pci.h>
  30. #include <linux/dma-buf.h>
  31. #include <linux/vmalloc.h>
  32. #include <drm/drmP.h>
  33. #include <drm/i915_drm.h>
  34. #include "i915_drv.h"
  35. #include "i915_trace.h"
  36. static bool shrinker_lock(struct drm_i915_private *i915, bool *unlock)
  37. {
  38. switch (mutex_trylock_recursive(&i915->drm.struct_mutex)) {
  39. case MUTEX_TRYLOCK_RECURSIVE:
  40. *unlock = false;
  41. return true;
  42. case MUTEX_TRYLOCK_FAILED:
  43. *unlock = false;
  44. preempt_disable();
  45. do {
  46. cpu_relax();
  47. if (mutex_trylock(&i915->drm.struct_mutex)) {
  48. *unlock = true;
  49. break;
  50. }
  51. } while (!need_resched());
  52. preempt_enable();
  53. return *unlock;
  54. case MUTEX_TRYLOCK_SUCCESS:
  55. *unlock = true;
  56. return true;
  57. }
  58. BUG();
  59. }
  60. static void shrinker_unlock(struct drm_i915_private *i915, bool unlock)
  61. {
  62. if (!unlock)
  63. return;
  64. mutex_unlock(&i915->drm.struct_mutex);
  65. }
  66. static bool swap_available(void)
  67. {
  68. return get_nr_swap_pages() > 0;
  69. }
  70. static bool can_release_pages(struct drm_i915_gem_object *obj)
  71. {
  72. /* Consider only shrinkable ojects. */
  73. if (!i915_gem_object_is_shrinkable(obj))
  74. return false;
  75. /* Only report true if by unbinding the object and putting its pages
  76. * we can actually make forward progress towards freeing physical
  77. * pages.
  78. *
  79. * If the pages are pinned for any other reason than being bound
  80. * to the GPU, simply unbinding from the GPU is not going to succeed
  81. * in releasing our pin count on the pages themselves.
  82. */
  83. if (atomic_read(&obj->mm.pages_pin_count) > obj->bind_count)
  84. return false;
  85. /* If any vma are "permanently" pinned, it will prevent us from
  86. * reclaiming the obj->mm.pages. We only allow scanout objects to claim
  87. * a permanent pin, along with a few others like the context objects.
  88. * To simplify the scan, and to avoid walking the list of vma under the
  89. * object, we just check the count of its permanently pinned.
  90. */
  91. if (READ_ONCE(obj->pin_global))
  92. return false;
  93. /* We can only return physical pages to the system if we can either
  94. * discard the contents (because the user has marked them as being
  95. * purgeable) or if we can move their contents out to swap.
  96. */
  97. return swap_available() || obj->mm.madv == I915_MADV_DONTNEED;
  98. }
  99. static bool unsafe_drop_pages(struct drm_i915_gem_object *obj)
  100. {
  101. if (i915_gem_object_unbind(obj) == 0)
  102. __i915_gem_object_put_pages(obj, I915_MM_SHRINKER);
  103. return !i915_gem_object_has_pages(obj);
  104. }
  105. /**
  106. * i915_gem_shrink - Shrink buffer object caches
  107. * @i915: i915 device
  108. * @target: amount of memory to make available, in pages
  109. * @nr_scanned: optional output for number of pages scanned (incremental)
  110. * @flags: control flags for selecting cache types
  111. *
  112. * This function is the main interface to the shrinker. It will try to release
  113. * up to @target pages of main memory backing storage from buffer objects.
  114. * Selection of the specific caches can be done with @flags. This is e.g. useful
  115. * when purgeable objects should be removed from caches preferentially.
  116. *
  117. * Note that it's not guaranteed that released amount is actually available as
  118. * free system memory - the pages might still be in-used to due to other reasons
  119. * (like cpu mmaps) or the mm core has reused them before we could grab them.
  120. * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
  121. * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
  122. *
  123. * Also note that any kind of pinning (both per-vma address space pins and
  124. * backing storage pins at the buffer object level) result in the shrinker code
  125. * having to skip the object.
  126. *
  127. * Returns:
  128. * The number of pages of backing storage actually released.
  129. */
  130. unsigned long
  131. i915_gem_shrink(struct drm_i915_private *i915,
  132. unsigned long target,
  133. unsigned long *nr_scanned,
  134. unsigned flags)
  135. {
  136. const struct {
  137. struct list_head *list;
  138. unsigned int bit;
  139. } phases[] = {
  140. { &i915->mm.unbound_list, I915_SHRINK_UNBOUND },
  141. { &i915->mm.bound_list, I915_SHRINK_BOUND },
  142. { NULL, 0 },
  143. }, *phase;
  144. unsigned long count = 0;
  145. unsigned long scanned = 0;
  146. bool unlock;
  147. if (!shrinker_lock(i915, &unlock))
  148. return 0;
  149. /*
  150. * When shrinking the active list, also consider active contexts.
  151. * Active contexts are pinned until they are retired, and so can
  152. * not be simply unbound to retire and unpin their pages. To shrink
  153. * the contexts, we must wait until the gpu is idle.
  154. *
  155. * We don't care about errors here; if we cannot wait upon the GPU,
  156. * we will free as much as we can and hope to get a second chance.
  157. */
  158. if (flags & I915_SHRINK_ACTIVE)
  159. i915_gem_wait_for_idle(i915,
  160. I915_WAIT_LOCKED,
  161. MAX_SCHEDULE_TIMEOUT);
  162. trace_i915_gem_shrink(i915, target, flags);
  163. i915_retire_requests(i915);
  164. /*
  165. * Unbinding of objects will require HW access; Let us not wake the
  166. * device just to recover a little memory. If absolutely necessary,
  167. * we will force the wake during oom-notifier.
  168. */
  169. if ((flags & I915_SHRINK_BOUND) &&
  170. !intel_runtime_pm_get_if_in_use(i915))
  171. flags &= ~I915_SHRINK_BOUND;
  172. /*
  173. * As we may completely rewrite the (un)bound list whilst unbinding
  174. * (due to retiring requests) we have to strictly process only
  175. * one element of the list at the time, and recheck the list
  176. * on every iteration.
  177. *
  178. * In particular, we must hold a reference whilst removing the
  179. * object as we may end up waiting for and/or retiring the objects.
  180. * This might release the final reference (held by the active list)
  181. * and result in the object being freed from under us. This is
  182. * similar to the precautions the eviction code must take whilst
  183. * removing objects.
  184. *
  185. * Also note that although these lists do not hold a reference to
  186. * the object we can safely grab one here: The final object
  187. * unreferencing and the bound_list are both protected by the
  188. * dev->struct_mutex and so we won't ever be able to observe an
  189. * object on the bound_list with a reference count equals 0.
  190. */
  191. for (phase = phases; phase->list; phase++) {
  192. struct list_head still_in_list;
  193. struct drm_i915_gem_object *obj;
  194. if ((flags & phase->bit) == 0)
  195. continue;
  196. INIT_LIST_HEAD(&still_in_list);
  197. /*
  198. * We serialize our access to unreferenced objects through
  199. * the use of the struct_mutex. While the objects are not
  200. * yet freed (due to RCU then a workqueue) we still want
  201. * to be able to shrink their pages, so they remain on
  202. * the unbound/bound list until actually freed.
  203. */
  204. spin_lock(&i915->mm.obj_lock);
  205. while (count < target &&
  206. (obj = list_first_entry_or_null(phase->list,
  207. typeof(*obj),
  208. mm.link))) {
  209. list_move_tail(&obj->mm.link, &still_in_list);
  210. if (flags & I915_SHRINK_PURGEABLE &&
  211. obj->mm.madv != I915_MADV_DONTNEED)
  212. continue;
  213. if (flags & I915_SHRINK_VMAPS &&
  214. !is_vmalloc_addr(obj->mm.mapping))
  215. continue;
  216. if (!(flags & I915_SHRINK_ACTIVE) &&
  217. (i915_gem_object_is_active(obj) ||
  218. i915_gem_object_is_framebuffer(obj)))
  219. continue;
  220. if (!can_release_pages(obj))
  221. continue;
  222. spin_unlock(&i915->mm.obj_lock);
  223. if (unsafe_drop_pages(obj)) {
  224. /* May arrive from get_pages on another bo */
  225. mutex_lock_nested(&obj->mm.lock,
  226. I915_MM_SHRINKER);
  227. if (!i915_gem_object_has_pages(obj)) {
  228. __i915_gem_object_invalidate(obj);
  229. count += obj->base.size >> PAGE_SHIFT;
  230. }
  231. mutex_unlock(&obj->mm.lock);
  232. }
  233. scanned += obj->base.size >> PAGE_SHIFT;
  234. spin_lock(&i915->mm.obj_lock);
  235. }
  236. list_splice_tail(&still_in_list, phase->list);
  237. spin_unlock(&i915->mm.obj_lock);
  238. }
  239. if (flags & I915_SHRINK_BOUND)
  240. intel_runtime_pm_put(i915);
  241. i915_retire_requests(i915);
  242. shrinker_unlock(i915, unlock);
  243. if (nr_scanned)
  244. *nr_scanned += scanned;
  245. return count;
  246. }
  247. /**
  248. * i915_gem_shrink_all - Shrink buffer object caches completely
  249. * @i915: i915 device
  250. *
  251. * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
  252. * caches completely. It also first waits for and retires all outstanding
  253. * requests to also be able to release backing storage for active objects.
  254. *
  255. * This should only be used in code to intentionally quiescent the gpu or as a
  256. * last-ditch effort when memory seems to have run out.
  257. *
  258. * Returns:
  259. * The number of pages of backing storage actually released.
  260. */
  261. unsigned long i915_gem_shrink_all(struct drm_i915_private *i915)
  262. {
  263. unsigned long freed;
  264. intel_runtime_pm_get(i915);
  265. freed = i915_gem_shrink(i915, -1UL, NULL,
  266. I915_SHRINK_BOUND |
  267. I915_SHRINK_UNBOUND |
  268. I915_SHRINK_ACTIVE);
  269. intel_runtime_pm_put(i915);
  270. return freed;
  271. }
  272. static unsigned long
  273. i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
  274. {
  275. struct drm_i915_private *i915 =
  276. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  277. struct drm_i915_gem_object *obj;
  278. unsigned long num_objects = 0;
  279. unsigned long count = 0;
  280. spin_lock(&i915->mm.obj_lock);
  281. list_for_each_entry(obj, &i915->mm.unbound_list, mm.link)
  282. if (can_release_pages(obj)) {
  283. count += obj->base.size >> PAGE_SHIFT;
  284. num_objects++;
  285. }
  286. list_for_each_entry(obj, &i915->mm.bound_list, mm.link)
  287. if (!i915_gem_object_is_active(obj) && can_release_pages(obj)) {
  288. count += obj->base.size >> PAGE_SHIFT;
  289. num_objects++;
  290. }
  291. spin_unlock(&i915->mm.obj_lock);
  292. /* Update our preferred vmscan batch size for the next pass.
  293. * Our rough guess for an effective batch size is roughly 2
  294. * available GEM objects worth of pages. That is we don't want
  295. * the shrinker to fire, until it is worth the cost of freeing an
  296. * entire GEM object.
  297. */
  298. if (num_objects) {
  299. unsigned long avg = 2 * count / num_objects;
  300. i915->mm.shrinker.batch =
  301. max((i915->mm.shrinker.batch + avg) >> 1,
  302. 128ul /* default SHRINK_BATCH */);
  303. }
  304. return count;
  305. }
  306. static unsigned long
  307. i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  308. {
  309. struct drm_i915_private *i915 =
  310. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  311. unsigned long freed;
  312. bool unlock;
  313. sc->nr_scanned = 0;
  314. if (!shrinker_lock(i915, &unlock))
  315. return SHRINK_STOP;
  316. freed = i915_gem_shrink(i915,
  317. sc->nr_to_scan,
  318. &sc->nr_scanned,
  319. I915_SHRINK_BOUND |
  320. I915_SHRINK_UNBOUND |
  321. I915_SHRINK_PURGEABLE);
  322. if (sc->nr_scanned < sc->nr_to_scan)
  323. freed += i915_gem_shrink(i915,
  324. sc->nr_to_scan - sc->nr_scanned,
  325. &sc->nr_scanned,
  326. I915_SHRINK_BOUND |
  327. I915_SHRINK_UNBOUND);
  328. if (sc->nr_scanned < sc->nr_to_scan && current_is_kswapd()) {
  329. intel_runtime_pm_get(i915);
  330. freed += i915_gem_shrink(i915,
  331. sc->nr_to_scan - sc->nr_scanned,
  332. &sc->nr_scanned,
  333. I915_SHRINK_ACTIVE |
  334. I915_SHRINK_BOUND |
  335. I915_SHRINK_UNBOUND);
  336. intel_runtime_pm_put(i915);
  337. }
  338. shrinker_unlock(i915, unlock);
  339. return sc->nr_scanned ? freed : SHRINK_STOP;
  340. }
  341. static bool
  342. shrinker_lock_uninterruptible(struct drm_i915_private *i915, bool *unlock,
  343. int timeout_ms)
  344. {
  345. unsigned long timeout = jiffies + msecs_to_jiffies_timeout(timeout_ms);
  346. do {
  347. if (i915_gem_wait_for_idle(i915,
  348. 0, MAX_SCHEDULE_TIMEOUT) == 0 &&
  349. shrinker_lock(i915, unlock))
  350. break;
  351. schedule_timeout_killable(1);
  352. if (fatal_signal_pending(current))
  353. return false;
  354. if (time_after(jiffies, timeout)) {
  355. pr_err("Unable to lock GPU to purge memory.\n");
  356. return false;
  357. }
  358. } while (1);
  359. return true;
  360. }
  361. static int
  362. i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
  363. {
  364. struct drm_i915_private *i915 =
  365. container_of(nb, struct drm_i915_private, mm.oom_notifier);
  366. struct drm_i915_gem_object *obj;
  367. unsigned long unevictable, bound, unbound, freed_pages;
  368. freed_pages = i915_gem_shrink_all(i915);
  369. /* Because we may be allocating inside our own driver, we cannot
  370. * assert that there are no objects with pinned pages that are not
  371. * being pointed to by hardware.
  372. */
  373. unbound = bound = unevictable = 0;
  374. spin_lock(&i915->mm.obj_lock);
  375. list_for_each_entry(obj, &i915->mm.unbound_list, mm.link) {
  376. if (!can_release_pages(obj))
  377. unevictable += obj->base.size >> PAGE_SHIFT;
  378. else
  379. unbound += obj->base.size >> PAGE_SHIFT;
  380. }
  381. list_for_each_entry(obj, &i915->mm.bound_list, mm.link) {
  382. if (!can_release_pages(obj))
  383. unevictable += obj->base.size >> PAGE_SHIFT;
  384. else
  385. bound += obj->base.size >> PAGE_SHIFT;
  386. }
  387. spin_unlock(&i915->mm.obj_lock);
  388. if (freed_pages || unbound || bound)
  389. pr_info("Purging GPU memory, %lu pages freed, "
  390. "%lu pages still pinned.\n",
  391. freed_pages, unevictable);
  392. if (unbound || bound)
  393. pr_err("%lu and %lu pages still available in the "
  394. "bound and unbound GPU page lists.\n",
  395. bound, unbound);
  396. *(unsigned long *)ptr += freed_pages;
  397. return NOTIFY_DONE;
  398. }
  399. static int
  400. i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
  401. {
  402. struct drm_i915_private *i915 =
  403. container_of(nb, struct drm_i915_private, mm.vmap_notifier);
  404. struct i915_vma *vma, *next;
  405. unsigned long freed_pages = 0;
  406. bool unlock;
  407. int ret;
  408. if (!shrinker_lock_uninterruptible(i915, &unlock, 5000))
  409. return NOTIFY_DONE;
  410. /* Force everything onto the inactive lists */
  411. ret = i915_gem_wait_for_idle(i915,
  412. I915_WAIT_LOCKED,
  413. MAX_SCHEDULE_TIMEOUT);
  414. if (ret)
  415. goto out;
  416. intel_runtime_pm_get(i915);
  417. freed_pages += i915_gem_shrink(i915, -1UL, NULL,
  418. I915_SHRINK_BOUND |
  419. I915_SHRINK_UNBOUND |
  420. I915_SHRINK_ACTIVE |
  421. I915_SHRINK_VMAPS);
  422. intel_runtime_pm_put(i915);
  423. /* We also want to clear any cached iomaps as they wrap vmap */
  424. list_for_each_entry_safe(vma, next,
  425. &i915->ggtt.vm.inactive_list, vm_link) {
  426. unsigned long count = vma->node.size >> PAGE_SHIFT;
  427. if (vma->iomap && i915_vma_unbind(vma) == 0)
  428. freed_pages += count;
  429. }
  430. out:
  431. shrinker_unlock(i915, unlock);
  432. *(unsigned long *)ptr += freed_pages;
  433. return NOTIFY_DONE;
  434. }
  435. /**
  436. * i915_gem_shrinker_register - Register the i915 shrinker
  437. * @i915: i915 device
  438. *
  439. * This function registers and sets up the i915 shrinker and OOM handler.
  440. */
  441. void i915_gem_shrinker_register(struct drm_i915_private *i915)
  442. {
  443. i915->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
  444. i915->mm.shrinker.count_objects = i915_gem_shrinker_count;
  445. i915->mm.shrinker.seeks = DEFAULT_SEEKS;
  446. i915->mm.shrinker.batch = 4096;
  447. WARN_ON(register_shrinker(&i915->mm.shrinker));
  448. i915->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
  449. WARN_ON(register_oom_notifier(&i915->mm.oom_notifier));
  450. i915->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
  451. WARN_ON(register_vmap_purge_notifier(&i915->mm.vmap_notifier));
  452. }
  453. /**
  454. * i915_gem_shrinker_unregister - Unregisters the i915 shrinker
  455. * @i915: i915 device
  456. *
  457. * This function unregisters the i915 shrinker and OOM handler.
  458. */
  459. void i915_gem_shrinker_unregister(struct drm_i915_private *i915)
  460. {
  461. WARN_ON(unregister_vmap_purge_notifier(&i915->mm.vmap_notifier));
  462. WARN_ON(unregister_oom_notifier(&i915->mm.oom_notifier));
  463. unregister_shrinker(&i915->mm.shrinker);
  464. }
  465. void i915_gem_shrinker_taints_mutex(struct mutex *mutex)
  466. {
  467. if (!IS_ENABLED(CONFIG_LOCKDEP))
  468. return;
  469. fs_reclaim_acquire(GFP_KERNEL);
  470. mutex_lock(mutex);
  471. mutex_unlock(mutex);
  472. fs_reclaim_release(GFP_KERNEL);
  473. }