2 * SPDX-License-Identifier: MIT
4 * Copyright © 2008-2015 Intel Corporation
8 #include <linux/sched/mm.h>
9 #include <linux/shmem_fs.h>
10 #include <linux/slab.h>
11 #include <linux/swap.h>
12 #include <linux/pci.h>
13 #include <linux/dma-buf.h>
14 #include <linux/vmalloc.h>
16 #include "gt/intel_gt_requests.h"
18 #include "dma_resv_utils.h"
19 #include "i915_trace.h"
21 static bool swap_available(void)
23 return get_nr_swap_pages() > 0;
26 static bool can_release_pages(struct drm_i915_gem_object *obj)
28 /* Consider only shrinkable ojects. */
29 if (!i915_gem_object_is_shrinkable(obj))
33 * We can only return physical pages to the system if we can either
34 * discard the contents (because the user has marked them as being
35 * purgeable) or if we can move their contents out to swap.
37 return swap_available() || obj->mm.madv == I915_MADV_DONTNEED;
40 static bool unsafe_drop_pages(struct drm_i915_gem_object *obj,
46 if (shrink & I915_SHRINK_ACTIVE)
47 flags = I915_GEM_OBJECT_UNBIND_ACTIVE;
48 if (!(shrink & I915_SHRINK_BOUND))
49 flags = I915_GEM_OBJECT_UNBIND_TEST;
51 if (i915_gem_object_unbind(obj, flags) == 0)
57 static void try_to_writeback(struct drm_i915_gem_object *obj,
60 switch (obj->mm.madv) {
61 case I915_MADV_DONTNEED:
62 i915_gem_object_truncate(obj);
63 case __I915_MADV_PURGED:
67 if (flags & I915_SHRINK_WRITEBACK)
68 i915_gem_object_writeback(obj);
72 * i915_gem_shrink - Shrink buffer object caches
73 * @ww: i915 gem ww acquire ctx, or NULL
75 * @target: amount of memory to make available, in pages
76 * @nr_scanned: optional output for number of pages scanned (incremental)
77 * @shrink: control flags for selecting cache types
79 * This function is the main interface to the shrinker. It will try to release
80 * up to @target pages of main memory backing storage from buffer objects.
81 * Selection of the specific caches can be done with @flags. This is e.g. useful
82 * when purgeable objects should be removed from caches preferentially.
84 * Note that it's not guaranteed that released amount is actually available as
85 * free system memory - the pages might still be in-used to due to other reasons
86 * (like cpu mmaps) or the mm core has reused them before we could grab them.
87 * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
88 * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
90 * Also note that any kind of pinning (both per-vma address space pins and
91 * backing storage pins at the buffer object level) result in the shrinker code
92 * having to skip the object.
95 * The number of pages of backing storage actually released.
98 i915_gem_shrink(struct i915_gem_ww_ctx *ww,
99 struct drm_i915_private *i915,
100 unsigned long target,
101 unsigned long *nr_scanned,
105 struct list_head *list;
108 { &i915->mm.purge_list, ~0u },
110 &i915->mm.shrink_list,
111 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND
115 intel_wakeref_t wakeref = 0;
116 unsigned long count = 0;
117 unsigned long scanned = 0;
120 trace_i915_gem_shrink(i915, target, shrink);
123 * Unbinding of objects will require HW access; Let us not wake the
124 * device just to recover a little memory. If absolutely necessary,
125 * we will force the wake during oom-notifier.
127 if (shrink & I915_SHRINK_BOUND) {
128 wakeref = intel_runtime_pm_get_if_in_use(&i915->runtime_pm);
130 shrink &= ~I915_SHRINK_BOUND;
134 * When shrinking the active list, we should also consider active
135 * contexts. Active contexts are pinned until they are retired, and
136 * so can not be simply unbound to retire and unpin their pages. To
137 * shrink the contexts, we must wait until the gpu is idle and
138 * completed its switch to the kernel context. In short, we do
139 * not have a good mechanism for idling a specific context, but
140 * what we can do is give them a kick so that we do not keep idle
141 * contexts around longer than is necessary.
143 if (shrink & I915_SHRINK_ACTIVE)
144 /* Retire requests to unpin all idle contexts */
145 intel_gt_retire_requests(&i915->gt);
148 * As we may completely rewrite the (un)bound list whilst unbinding
149 * (due to retiring requests) we have to strictly process only
150 * one element of the list at the time, and recheck the list
151 * on every iteration.
153 * In particular, we must hold a reference whilst removing the
154 * object as we may end up waiting for and/or retiring the objects.
155 * This might release the final reference (held by the active list)
156 * and result in the object being freed from under us. This is
157 * similar to the precautions the eviction code must take whilst
160 * Also note that although these lists do not hold a reference to
161 * the object we can safely grab one here: The final object
162 * unreferencing and the bound_list are both protected by the
163 * dev->struct_mutex and so we won't ever be able to observe an
164 * object on the bound_list with a reference count equals 0.
166 for (phase = phases; phase->list; phase++) {
167 struct list_head still_in_list;
168 struct drm_i915_gem_object *obj;
171 if ((shrink & phase->bit) == 0)
174 INIT_LIST_HEAD(&still_in_list);
177 * We serialize our access to unreferenced objects through
178 * the use of the struct_mutex. While the objects are not
179 * yet freed (due to RCU then a workqueue) we still want
180 * to be able to shrink their pages, so they remain on
181 * the unbound/bound list until actually freed.
183 spin_lock_irqsave(&i915->mm.obj_lock, flags);
184 while (count < target &&
185 (obj = list_first_entry_or_null(phase->list,
188 list_move_tail(&obj->mm.link, &still_in_list);
190 if (shrink & I915_SHRINK_VMAPS &&
191 !is_vmalloc_addr(obj->mm.mapping))
194 if (!(shrink & I915_SHRINK_ACTIVE) &&
195 i915_gem_object_is_framebuffer(obj))
198 if (!can_release_pages(obj))
201 if (!kref_get_unless_zero(&obj->base.refcount))
204 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
207 if (unsafe_drop_pages(obj, shrink)) {
208 /* May arrive from get_pages on another bo */
210 if (!i915_gem_object_trylock(obj))
213 err = i915_gem_object_lock(obj, ww);
218 if (!__i915_gem_object_put_pages(obj)) {
219 try_to_writeback(obj, shrink);
220 count += obj->base.size >> PAGE_SHIFT;
223 i915_gem_object_unlock(obj);
226 dma_resv_prune(obj->base.resv);
228 scanned += obj->base.size >> PAGE_SHIFT;
230 i915_gem_object_put(obj);
232 spin_lock_irqsave(&i915->mm.obj_lock, flags);
236 list_splice_tail(&still_in_list, phase->list);
237 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
242 if (shrink & I915_SHRINK_BOUND)
243 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
246 *nr_scanned += scanned;
251 * i915_gem_shrink_all - Shrink buffer object caches completely
254 * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
255 * caches completely. It also first waits for and retires all outstanding
256 * requests to also be able to release backing storage for active objects.
258 * This should only be used in code to intentionally quiescent the gpu or as a
259 * last-ditch effort when memory seems to have run out.
262 * The number of pages of backing storage actually released.
264 unsigned long i915_gem_shrink_all(struct drm_i915_private *i915)
266 intel_wakeref_t wakeref;
267 unsigned long freed = 0;
269 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
270 freed = i915_gem_shrink(NULL, i915, -1UL, NULL,
272 I915_SHRINK_UNBOUND);
279 i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
281 struct drm_i915_private *i915 =
282 container_of(shrinker, struct drm_i915_private, mm.shrinker);
283 unsigned long num_objects;
286 count = READ_ONCE(i915->mm.shrink_memory) >> PAGE_SHIFT;
287 num_objects = READ_ONCE(i915->mm.shrink_count);
290 * Update our preferred vmscan batch size for the next pass.
291 * Our rough guess for an effective batch size is roughly 2
292 * available GEM objects worth of pages. That is we don't want
293 * the shrinker to fire, until it is worth the cost of freeing an
297 unsigned long avg = 2 * count / num_objects;
299 i915->mm.shrinker.batch =
300 max((i915->mm.shrinker.batch + avg) >> 1,
301 128ul /* default SHRINK_BATCH */);
308 i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
310 struct drm_i915_private *i915 =
311 container_of(shrinker, struct drm_i915_private, mm.shrinker);
316 freed = i915_gem_shrink(NULL, i915,
320 I915_SHRINK_UNBOUND);
321 if (sc->nr_scanned < sc->nr_to_scan && current_is_kswapd()) {
322 intel_wakeref_t wakeref;
324 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
325 freed += i915_gem_shrink(NULL, i915,
326 sc->nr_to_scan - sc->nr_scanned,
330 I915_SHRINK_UNBOUND |
331 I915_SHRINK_WRITEBACK);
335 return sc->nr_scanned ? freed : SHRINK_STOP;
339 i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
341 struct drm_i915_private *i915 =
342 container_of(nb, struct drm_i915_private, mm.oom_notifier);
343 struct drm_i915_gem_object *obj;
344 unsigned long unevictable, available, freed_pages;
345 intel_wakeref_t wakeref;
349 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
350 freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
352 I915_SHRINK_UNBOUND |
353 I915_SHRINK_WRITEBACK);
355 /* Because we may be allocating inside our own driver, we cannot
356 * assert that there are no objects with pinned pages that are not
357 * being pointed to by hardware.
359 available = unevictable = 0;
360 spin_lock_irqsave(&i915->mm.obj_lock, flags);
361 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
362 if (!can_release_pages(obj))
363 unevictable += obj->base.size >> PAGE_SHIFT;
365 available += obj->base.size >> PAGE_SHIFT;
367 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
369 if (freed_pages || available)
370 pr_info("Purging GPU memory, %lu pages freed, "
371 "%lu pages still pinned, %lu pages left available.\n",
372 freed_pages, unevictable, available);
374 *(unsigned long *)ptr += freed_pages;
379 i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
381 struct drm_i915_private *i915 =
382 container_of(nb, struct drm_i915_private, mm.vmap_notifier);
383 struct i915_vma *vma, *next;
384 unsigned long freed_pages = 0;
385 intel_wakeref_t wakeref;
387 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
388 freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
390 I915_SHRINK_UNBOUND |
393 /* We also want to clear any cached iomaps as they wrap vmap */
394 mutex_lock(&i915->ggtt.vm.mutex);
395 list_for_each_entry_safe(vma, next,
396 &i915->ggtt.vm.bound_list, vm_link) {
397 unsigned long count = vma->node.size >> PAGE_SHIFT;
399 if (!vma->iomap || i915_vma_is_active(vma))
402 if (__i915_vma_unbind(vma) == 0)
403 freed_pages += count;
405 mutex_unlock(&i915->ggtt.vm.mutex);
407 *(unsigned long *)ptr += freed_pages;
411 void i915_gem_driver_register__shrinker(struct drm_i915_private *i915)
413 i915->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
414 i915->mm.shrinker.count_objects = i915_gem_shrinker_count;
415 i915->mm.shrinker.seeks = DEFAULT_SEEKS;
416 i915->mm.shrinker.batch = 4096;
417 drm_WARN_ON(&i915->drm, register_shrinker(&i915->mm.shrinker));
419 i915->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
420 drm_WARN_ON(&i915->drm, register_oom_notifier(&i915->mm.oom_notifier));
422 i915->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
423 drm_WARN_ON(&i915->drm,
424 register_vmap_purge_notifier(&i915->mm.vmap_notifier));
427 void i915_gem_driver_unregister__shrinker(struct drm_i915_private *i915)
429 drm_WARN_ON(&i915->drm,
430 unregister_vmap_purge_notifier(&i915->mm.vmap_notifier));
431 drm_WARN_ON(&i915->drm,
432 unregister_oom_notifier(&i915->mm.oom_notifier));
433 unregister_shrinker(&i915->mm.shrinker);
436 void i915_gem_shrinker_taints_mutex(struct drm_i915_private *i915,
439 if (!IS_ENABLED(CONFIG_LOCKDEP))
442 fs_reclaim_acquire(GFP_KERNEL);
444 mutex_acquire(&mutex->dep_map, 0, 0, _RET_IP_);
445 mutex_release(&mutex->dep_map, _RET_IP_);
447 fs_reclaim_release(GFP_KERNEL);
450 #define obj_to_i915(obj__) to_i915((obj__)->base.dev)
452 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj)
454 struct drm_i915_private *i915 = obj_to_i915(obj);
458 * We can only be called while the pages are pinned or when
459 * the pages are released. If pinned, we should only be called
460 * from a single caller under controlled conditions; and on release
461 * only one caller may release us. Neither the two may cross.
463 if (atomic_add_unless(&obj->mm.shrink_pin, 1, 0))
466 spin_lock_irqsave(&i915->mm.obj_lock, flags);
467 if (!atomic_fetch_inc(&obj->mm.shrink_pin) &&
468 !list_empty(&obj->mm.link)) {
469 list_del_init(&obj->mm.link);
470 i915->mm.shrink_count--;
471 i915->mm.shrink_memory -= obj->base.size;
473 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
476 static void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj,
477 struct list_head *head)
479 struct drm_i915_private *i915 = obj_to_i915(obj);
482 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
483 if (!i915_gem_object_is_shrinkable(obj))
486 if (atomic_add_unless(&obj->mm.shrink_pin, -1, 1))
489 spin_lock_irqsave(&i915->mm.obj_lock, flags);
490 GEM_BUG_ON(!kref_read(&obj->base.refcount));
491 if (atomic_dec_and_test(&obj->mm.shrink_pin)) {
492 GEM_BUG_ON(!list_empty(&obj->mm.link));
494 list_add_tail(&obj->mm.link, head);
495 i915->mm.shrink_count++;
496 i915->mm.shrink_memory += obj->base.size;
499 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
502 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj)
504 __i915_gem_object_make_shrinkable(obj,
505 &obj_to_i915(obj)->mm.shrink_list);
508 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj)
510 __i915_gem_object_make_shrinkable(obj,
511 &obj_to_i915(obj)->mm.purge_list);