i915_gem_shrinker.c 10 KB

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