i915_gem_shrinker.c 10 KB

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