i915_gem_shrinker.c 15 KB

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