i915_gem_shrinker.c 14 KB

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