i915_gem_shrinker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 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->dev);
  122. /*
  123. * As we may completely rewrite the (un)bound list whilst unbinding
  124. * (due to retiring requests) we have to strictly process only
  125. * one element of the list at the time, and recheck the list
  126. * on every iteration.
  127. *
  128. * In particular, we must hold a reference whilst removing the
  129. * object as we may end up waiting for and/or retiring the objects.
  130. * This might release the final reference (held by the active list)
  131. * and result in the object being freed from under us. This is
  132. * similar to the precautions the eviction code must take whilst
  133. * removing objects.
  134. *
  135. * Also note that although these lists do not hold a reference to
  136. * the object we can safely grab one here: The final object
  137. * unreferencing and the bound_list are both protected by the
  138. * dev->struct_mutex and so we won't ever be able to observe an
  139. * object on the bound_list with a reference count equals 0.
  140. */
  141. for (phase = phases; phase->list; phase++) {
  142. struct list_head still_in_list;
  143. if ((flags & phase->bit) == 0)
  144. continue;
  145. INIT_LIST_HEAD(&still_in_list);
  146. while (count < target && !list_empty(phase->list)) {
  147. struct drm_i915_gem_object *obj;
  148. struct i915_vma *vma, *v;
  149. obj = list_first_entry(phase->list,
  150. typeof(*obj), global_list);
  151. list_move_tail(&obj->global_list, &still_in_list);
  152. if (flags & I915_SHRINK_PURGEABLE &&
  153. obj->madv != I915_MADV_DONTNEED)
  154. continue;
  155. if (flags & I915_SHRINK_VMAPS &&
  156. !is_vmalloc_addr(obj->mapping))
  157. continue;
  158. if ((flags & I915_SHRINK_ACTIVE) == 0 && obj->active)
  159. continue;
  160. if (!can_release_pages(obj))
  161. continue;
  162. drm_gem_object_reference(&obj->base);
  163. /* For the unbound phase, this should be a no-op! */
  164. list_for_each_entry_safe(vma, v,
  165. &obj->vma_list, obj_link)
  166. if (i915_vma_unbind(vma))
  167. break;
  168. if (i915_gem_object_put_pages(obj) == 0)
  169. count += obj->base.size >> PAGE_SHIFT;
  170. drm_gem_object_unreference(&obj->base);
  171. }
  172. list_splice(&still_in_list, phase->list);
  173. }
  174. i915_gem_retire_requests(dev_priv->dev);
  175. return count;
  176. }
  177. /**
  178. * i915_gem_shrink_all - Shrink buffer object caches completely
  179. * @dev_priv: i915 device
  180. *
  181. * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
  182. * caches completely. It also first waits for and retires all outstanding
  183. * requests to also be able to release backing storage for active objects.
  184. *
  185. * This should only be used in code to intentionally quiescent the gpu or as a
  186. * last-ditch effort when memory seems to have run out.
  187. *
  188. * Returns:
  189. * The number of pages of backing storage actually released.
  190. */
  191. unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  192. {
  193. return i915_gem_shrink(dev_priv, -1UL,
  194. I915_SHRINK_BOUND |
  195. I915_SHRINK_UNBOUND |
  196. I915_SHRINK_ACTIVE);
  197. }
  198. static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
  199. {
  200. if (!mutex_trylock(&dev->struct_mutex)) {
  201. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  202. return false;
  203. if (to_i915(dev)->mm.shrinker_no_lock_stealing)
  204. return false;
  205. *unlock = false;
  206. } else
  207. *unlock = true;
  208. return true;
  209. }
  210. static unsigned long
  211. i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
  212. {
  213. struct drm_i915_private *dev_priv =
  214. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  215. struct drm_device *dev = dev_priv->dev;
  216. struct drm_i915_gem_object *obj;
  217. unsigned long count;
  218. bool unlock;
  219. if (!i915_gem_shrinker_lock(dev, &unlock))
  220. return 0;
  221. count = 0;
  222. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  223. if (can_release_pages(obj))
  224. count += obj->base.size >> PAGE_SHIFT;
  225. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  226. if (!obj->active && can_release_pages(obj))
  227. count += obj->base.size >> PAGE_SHIFT;
  228. }
  229. if (unlock)
  230. mutex_unlock(&dev->struct_mutex);
  231. return count;
  232. }
  233. static unsigned long
  234. i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  235. {
  236. struct drm_i915_private *dev_priv =
  237. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  238. struct drm_device *dev = dev_priv->dev;
  239. unsigned long freed;
  240. bool unlock;
  241. if (!i915_gem_shrinker_lock(dev, &unlock))
  242. return SHRINK_STOP;
  243. freed = i915_gem_shrink(dev_priv,
  244. sc->nr_to_scan,
  245. I915_SHRINK_BOUND |
  246. I915_SHRINK_UNBOUND |
  247. I915_SHRINK_PURGEABLE);
  248. if (freed < sc->nr_to_scan)
  249. freed += i915_gem_shrink(dev_priv,
  250. sc->nr_to_scan - freed,
  251. I915_SHRINK_BOUND |
  252. I915_SHRINK_UNBOUND);
  253. if (unlock)
  254. mutex_unlock(&dev->struct_mutex);
  255. return freed;
  256. }
  257. struct shrinker_lock_uninterruptible {
  258. bool was_interruptible;
  259. bool unlock;
  260. };
  261. static bool
  262. i915_gem_shrinker_lock_uninterruptible(struct drm_i915_private *dev_priv,
  263. struct shrinker_lock_uninterruptible *slu,
  264. int timeout_ms)
  265. {
  266. unsigned long timeout = msecs_to_jiffies(timeout_ms) + 1;
  267. while (!i915_gem_shrinker_lock(dev_priv->dev, &slu->unlock)) {
  268. schedule_timeout_killable(1);
  269. if (fatal_signal_pending(current))
  270. return false;
  271. if (--timeout == 0) {
  272. pr_err("Unable to lock GPU to purge memory.\n");
  273. return false;
  274. }
  275. }
  276. slu->was_interruptible = dev_priv->mm.interruptible;
  277. dev_priv->mm.interruptible = false;
  278. return true;
  279. }
  280. static void
  281. i915_gem_shrinker_unlock_uninterruptible(struct drm_i915_private *dev_priv,
  282. struct shrinker_lock_uninterruptible *slu)
  283. {
  284. dev_priv->mm.interruptible = slu->was_interruptible;
  285. if (slu->unlock)
  286. mutex_unlock(&dev_priv->dev->struct_mutex);
  287. }
  288. static int
  289. i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
  290. {
  291. struct drm_i915_private *dev_priv =
  292. container_of(nb, struct drm_i915_private, mm.oom_notifier);
  293. struct shrinker_lock_uninterruptible slu;
  294. struct drm_i915_gem_object *obj;
  295. unsigned long unevictable, bound, unbound, freed_pages;
  296. if (!i915_gem_shrinker_lock_uninterruptible(dev_priv, &slu, 5000))
  297. return NOTIFY_DONE;
  298. freed_pages = i915_gem_shrink_all(dev_priv);
  299. /* Because we may be allocating inside our own driver, we cannot
  300. * assert that there are no objects with pinned pages that are not
  301. * being pointed to by hardware.
  302. */
  303. unbound = bound = unevictable = 0;
  304. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  305. if (!can_release_pages(obj))
  306. unevictable += obj->base.size >> PAGE_SHIFT;
  307. else
  308. unbound += obj->base.size >> PAGE_SHIFT;
  309. }
  310. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  311. if (!can_release_pages(obj))
  312. unevictable += obj->base.size >> PAGE_SHIFT;
  313. else
  314. bound += obj->base.size >> PAGE_SHIFT;
  315. }
  316. i915_gem_shrinker_unlock_uninterruptible(dev_priv, &slu);
  317. if (freed_pages || unbound || bound)
  318. pr_info("Purging GPU memory, %lu pages freed, "
  319. "%lu pages still pinned.\n",
  320. freed_pages, unevictable);
  321. if (unbound || bound)
  322. pr_err("%lu and %lu pages still available in the "
  323. "bound and unbound GPU page lists.\n",
  324. bound, unbound);
  325. *(unsigned long *)ptr += freed_pages;
  326. return NOTIFY_DONE;
  327. }
  328. static int
  329. i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
  330. {
  331. struct drm_i915_private *dev_priv =
  332. container_of(nb, struct drm_i915_private, mm.vmap_notifier);
  333. struct shrinker_lock_uninterruptible slu;
  334. unsigned long freed_pages;
  335. if (!i915_gem_shrinker_lock_uninterruptible(dev_priv, &slu, 5000))
  336. return NOTIFY_DONE;
  337. freed_pages = i915_gem_shrink(dev_priv, -1UL,
  338. I915_SHRINK_BOUND |
  339. I915_SHRINK_UNBOUND |
  340. I915_SHRINK_ACTIVE |
  341. I915_SHRINK_VMAPS);
  342. i915_gem_shrinker_unlock_uninterruptible(dev_priv, &slu);
  343. *(unsigned long *)ptr += freed_pages;
  344. return NOTIFY_DONE;
  345. }
  346. /**
  347. * i915_gem_shrinker_init - Initialize i915 shrinker
  348. * @dev_priv: i915 device
  349. *
  350. * This function registers and sets up the i915 shrinker and OOM handler.
  351. */
  352. void i915_gem_shrinker_init(struct drm_i915_private *dev_priv)
  353. {
  354. dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
  355. dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
  356. dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
  357. WARN_ON(register_shrinker(&dev_priv->mm.shrinker));
  358. dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
  359. WARN_ON(register_oom_notifier(&dev_priv->mm.oom_notifier));
  360. dev_priv->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
  361. WARN_ON(register_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
  362. }
  363. /**
  364. * i915_gem_shrinker_cleanup - Clean up i915 shrinker
  365. * @dev_priv: i915 device
  366. *
  367. * This function unregisters the i915 shrinker and OOM handler.
  368. */
  369. void i915_gem_shrinker_cleanup(struct drm_i915_private *dev_priv)
  370. {
  371. WARN_ON(unregister_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
  372. WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
  373. unregister_shrinker(&dev_priv->mm.shrinker);
  374. }