i915_gem_shrinker.c 14 KB

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