i915_gem_shrinker.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <drm/drmP.h>
  31. #include <drm/i915_drm.h>
  32. #include "i915_drv.h"
  33. #include "i915_trace.h"
  34. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  35. {
  36. if (!mutex_is_locked(mutex))
  37. return false;
  38. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  39. return mutex->owner == task;
  40. #else
  41. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  42. return false;
  43. #endif
  44. }
  45. static int num_vma_bound(struct drm_i915_gem_object *obj)
  46. {
  47. struct i915_vma *vma;
  48. int count = 0;
  49. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  50. if (drm_mm_node_allocated(&vma->node))
  51. count++;
  52. if (vma->pin_count)
  53. count++;
  54. }
  55. return count;
  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. /* Only report true if by unbinding the object and putting its pages
  64. * we can actually make forward progress towards freeing physical
  65. * pages.
  66. *
  67. * If the pages are pinned for any other reason than being bound
  68. * to the GPU, simply unbinding from the GPU is not going to succeed
  69. * in releasing our pin count on the pages themselves.
  70. */
  71. if (obj->pages_pin_count != num_vma_bound(obj))
  72. return false;
  73. /* We can only return physical pages to the system if we can either
  74. * discard the contents (because the user has marked them as being
  75. * purgeable) or if we can move their contents out to swap.
  76. */
  77. return swap_available() || obj->madv == I915_MADV_DONTNEED;
  78. }
  79. /**
  80. * i915_gem_shrink - Shrink buffer object caches
  81. * @dev_priv: i915 device
  82. * @target: amount of memory to make available, in pages
  83. * @flags: control flags for selecting cache types
  84. *
  85. * This function is the main interface to the shrinker. It will try to release
  86. * up to @target pages of main memory backing storage from buffer objects.
  87. * Selection of the specific caches can be done with @flags. This is e.g. useful
  88. * when purgeable objects should be removed from caches preferentially.
  89. *
  90. * Note that it's not guaranteed that released amount is actually available as
  91. * free system memory - the pages might still be in-used to due to other reasons
  92. * (like cpu mmaps) or the mm core has reused them before we could grab them.
  93. * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
  94. * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
  95. *
  96. * Also note that any kind of pinning (both per-vma address space pins and
  97. * backing storage pins at the buffer object level) result in the shrinker code
  98. * having to skip the object.
  99. *
  100. * Returns:
  101. * The number of pages of backing storage actually released.
  102. */
  103. unsigned long
  104. i915_gem_shrink(struct drm_i915_private *dev_priv,
  105. unsigned long target, unsigned flags)
  106. {
  107. const struct {
  108. struct list_head *list;
  109. unsigned int bit;
  110. } phases[] = {
  111. { &dev_priv->mm.unbound_list, I915_SHRINK_UNBOUND },
  112. { &dev_priv->mm.bound_list, I915_SHRINK_BOUND },
  113. { NULL, 0 },
  114. }, *phase;
  115. unsigned long count = 0;
  116. trace_i915_gem_shrink(dev_priv, target, flags);
  117. i915_gem_retire_requests(dev_priv->dev);
  118. /*
  119. * As we may completely rewrite the (un)bound list whilst unbinding
  120. * (due to retiring requests) we have to strictly process only
  121. * one element of the list at the time, and recheck the list
  122. * on every iteration.
  123. *
  124. * In particular, we must hold a reference whilst removing the
  125. * object as we may end up waiting for and/or retiring the objects.
  126. * This might release the final reference (held by the active list)
  127. * and result in the object being freed from under us. This is
  128. * similar to the precautions the eviction code must take whilst
  129. * removing objects.
  130. *
  131. * Also note that although these lists do not hold a reference to
  132. * the object we can safely grab one here: The final object
  133. * unreferencing and the bound_list are both protected by the
  134. * dev->struct_mutex and so we won't ever be able to observe an
  135. * object on the bound_list with a reference count equals 0.
  136. */
  137. for (phase = phases; phase->list; phase++) {
  138. struct list_head still_in_list;
  139. if ((flags & phase->bit) == 0)
  140. continue;
  141. INIT_LIST_HEAD(&still_in_list);
  142. while (count < target && !list_empty(phase->list)) {
  143. struct drm_i915_gem_object *obj;
  144. struct i915_vma *vma, *v;
  145. obj = list_first_entry(phase->list,
  146. typeof(*obj), global_list);
  147. list_move_tail(&obj->global_list, &still_in_list);
  148. if (flags & I915_SHRINK_PURGEABLE &&
  149. obj->madv != I915_MADV_DONTNEED)
  150. continue;
  151. if ((flags & I915_SHRINK_ACTIVE) == 0 && obj->active)
  152. continue;
  153. if (!can_release_pages(obj))
  154. continue;
  155. drm_gem_object_reference(&obj->base);
  156. /* For the unbound phase, this should be a no-op! */
  157. list_for_each_entry_safe(vma, v,
  158. &obj->vma_list, obj_link)
  159. if (i915_vma_unbind(vma))
  160. break;
  161. if (i915_gem_object_put_pages(obj) == 0)
  162. count += obj->base.size >> PAGE_SHIFT;
  163. drm_gem_object_unreference(&obj->base);
  164. }
  165. list_splice(&still_in_list, phase->list);
  166. }
  167. i915_gem_retire_requests(dev_priv->dev);
  168. return count;
  169. }
  170. /**
  171. * i915_gem_shrink_all - Shrink buffer object caches completely
  172. * @dev_priv: i915 device
  173. *
  174. * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
  175. * caches completely. It also first waits for and retires all outstanding
  176. * requests to also be able to release backing storage for active objects.
  177. *
  178. * This should only be used in code to intentionally quiescent the gpu or as a
  179. * last-ditch effort when memory seems to have run out.
  180. *
  181. * Returns:
  182. * The number of pages of backing storage actually released.
  183. */
  184. unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  185. {
  186. return i915_gem_shrink(dev_priv, -1UL,
  187. I915_SHRINK_BOUND |
  188. I915_SHRINK_UNBOUND |
  189. I915_SHRINK_ACTIVE);
  190. }
  191. static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
  192. {
  193. if (!mutex_trylock(&dev->struct_mutex)) {
  194. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  195. return false;
  196. if (to_i915(dev)->mm.shrinker_no_lock_stealing)
  197. return false;
  198. *unlock = false;
  199. } else
  200. *unlock = true;
  201. return true;
  202. }
  203. static unsigned long
  204. i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
  205. {
  206. struct drm_i915_private *dev_priv =
  207. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  208. struct drm_device *dev = dev_priv->dev;
  209. struct drm_i915_gem_object *obj;
  210. unsigned long count;
  211. bool unlock;
  212. if (!i915_gem_shrinker_lock(dev, &unlock))
  213. return 0;
  214. count = 0;
  215. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  216. if (obj->pages_pin_count == 0)
  217. count += obj->base.size >> PAGE_SHIFT;
  218. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  219. if (!obj->active && can_release_pages(obj))
  220. count += obj->base.size >> PAGE_SHIFT;
  221. }
  222. if (unlock)
  223. mutex_unlock(&dev->struct_mutex);
  224. return count;
  225. }
  226. static unsigned long
  227. i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  228. {
  229. struct drm_i915_private *dev_priv =
  230. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  231. struct drm_device *dev = dev_priv->dev;
  232. unsigned long freed;
  233. bool unlock;
  234. if (!i915_gem_shrinker_lock(dev, &unlock))
  235. return SHRINK_STOP;
  236. freed = i915_gem_shrink(dev_priv,
  237. sc->nr_to_scan,
  238. I915_SHRINK_BOUND |
  239. I915_SHRINK_UNBOUND |
  240. I915_SHRINK_PURGEABLE);
  241. if (freed < sc->nr_to_scan)
  242. freed += i915_gem_shrink(dev_priv,
  243. sc->nr_to_scan - freed,
  244. I915_SHRINK_BOUND |
  245. I915_SHRINK_UNBOUND);
  246. if (unlock)
  247. mutex_unlock(&dev->struct_mutex);
  248. return freed;
  249. }
  250. static int
  251. i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
  252. {
  253. struct drm_i915_private *dev_priv =
  254. container_of(nb, struct drm_i915_private, mm.oom_notifier);
  255. struct drm_device *dev = dev_priv->dev;
  256. struct drm_i915_gem_object *obj;
  257. unsigned long timeout = msecs_to_jiffies(5000) + 1;
  258. unsigned long pinned, bound, unbound, freed_pages;
  259. bool was_interruptible;
  260. bool unlock;
  261. while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout) {
  262. schedule_timeout_killable(1);
  263. if (fatal_signal_pending(current))
  264. return NOTIFY_DONE;
  265. }
  266. if (timeout == 0) {
  267. pr_err("Unable to purge GPU memory due lock contention.\n");
  268. return NOTIFY_DONE;
  269. }
  270. was_interruptible = dev_priv->mm.interruptible;
  271. dev_priv->mm.interruptible = false;
  272. freed_pages = i915_gem_shrink_all(dev_priv);
  273. dev_priv->mm.interruptible = was_interruptible;
  274. /* Because we may be allocating inside our own driver, we cannot
  275. * assert that there are no objects with pinned pages that are not
  276. * being pointed to by hardware.
  277. */
  278. unbound = bound = pinned = 0;
  279. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  280. if (!obj->base.filp) /* not backed by a freeable object */
  281. continue;
  282. if (obj->pages_pin_count)
  283. pinned += obj->base.size;
  284. else
  285. unbound += obj->base.size;
  286. }
  287. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  288. if (!obj->base.filp)
  289. continue;
  290. if (obj->pages_pin_count)
  291. pinned += obj->base.size;
  292. else
  293. bound += obj->base.size;
  294. }
  295. if (unlock)
  296. mutex_unlock(&dev->struct_mutex);
  297. if (freed_pages || unbound || bound)
  298. pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n",
  299. freed_pages << PAGE_SHIFT, pinned);
  300. if (unbound || bound)
  301. pr_err("%lu and %lu bytes still available in the "
  302. "bound and unbound GPU page lists.\n",
  303. bound, unbound);
  304. *(unsigned long *)ptr += freed_pages;
  305. return NOTIFY_DONE;
  306. }
  307. /**
  308. * i915_gem_shrinker_init - Initialize i915 shrinker
  309. * @dev_priv: i915 device
  310. *
  311. * This function registers and sets up the i915 shrinker and OOM handler.
  312. */
  313. void i915_gem_shrinker_init(struct drm_i915_private *dev_priv)
  314. {
  315. dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
  316. dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
  317. dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
  318. WARN_ON(register_shrinker(&dev_priv->mm.shrinker));
  319. dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
  320. WARN_ON(register_oom_notifier(&dev_priv->mm.oom_notifier));
  321. }
  322. /**
  323. * i915_gem_shrinker_cleanup - Clean up i915 shrinker
  324. * @dev_priv: i915 device
  325. *
  326. * This function unregisters the i915 shrinker and OOM handler.
  327. */
  328. void i915_gem_shrinker_cleanup(struct drm_i915_private *dev_priv)
  329. {
  330. WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
  331. unregister_shrinker(&dev_priv->mm.shrinker);
  332. }