i915_gem_shrinker.c 15 KB

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