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"
17 #include "gt/intel_gt.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 drop_pages(struct drm_i915_gem_object *obj,
41 unsigned long shrink, bool trylock_vm)
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 flags |= I915_GEM_OBJECT_UNBIND_VM_TRYLOCK;
53 if (i915_gem_object_unbind(obj, flags) == 0)
59 static int try_to_writeback(struct drm_i915_gem_object *obj, unsigned int flags)
61 if (obj->ops->shrink) {
62 unsigned int shrink_flags = 0;
64 if (!(flags & I915_SHRINK_ACTIVE))
65 shrink_flags |= I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT;
67 if (flags & I915_SHRINK_WRITEBACK)
68 shrink_flags |= I915_GEM_OBJECT_SHRINK_WRITEBACK;
70 return obj->ops->shrink(obj, shrink_flags);
77 * i915_gem_shrink - Shrink buffer object caches
78 * @ww: i915 gem ww acquire ctx, or NULL
80 * @target: amount of memory to make available, in pages
81 * @nr_scanned: optional output for number of pages scanned (incremental)
82 * @shrink: control flags for selecting cache types
84 * This function is the main interface to the shrinker. It will try to release
85 * up to @target pages of main memory backing storage from buffer objects.
86 * Selection of the specific caches can be done with @flags. This is e.g. useful
87 * when purgeable objects should be removed from caches preferentially.
89 * Note that it's not guaranteed that released amount is actually available as
90 * free system memory - the pages might still be in-used to due to other reasons
91 * (like cpu mmaps) or the mm core has reused them before we could grab them.
92 * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
93 * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
95 * Also note that any kind of pinning (both per-vma address space pins and
96 * backing storage pins at the buffer object level) result in the shrinker code
97 * having to skip the object.
100 * The number of pages of backing storage actually released.
103 i915_gem_shrink(struct i915_gem_ww_ctx *ww,
104 struct drm_i915_private *i915,
105 unsigned long target,
106 unsigned long *nr_scanned,
110 struct list_head *list;
113 { &i915->mm.purge_list, ~0u },
115 &i915->mm.shrink_list,
116 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND
120 intel_wakeref_t wakeref = 0;
121 unsigned long count = 0;
122 unsigned long scanned = 0;
126 /* CHV + VTD workaround use stop_machine(); need to trylock vm->mutex */
127 bool trylock_vm = !ww && intel_vm_no_concurrent_access_wa(i915);
129 trace_i915_gem_shrink(i915, target, shrink);
132 * Unbinding of objects will require HW access; Let us not wake the
133 * device just to recover a little memory. If absolutely necessary,
134 * we will force the wake during oom-notifier.
136 if (shrink & I915_SHRINK_BOUND) {
137 wakeref = intel_runtime_pm_get_if_in_use(&i915->runtime_pm);
139 shrink &= ~I915_SHRINK_BOUND;
143 * When shrinking the active list, we should also consider active
144 * contexts. Active contexts are pinned until they are retired, and
145 * so can not be simply unbound to retire and unpin their pages. To
146 * shrink the contexts, we must wait until the gpu is idle and
147 * completed its switch to the kernel context. In short, we do
148 * not have a good mechanism for idling a specific context, but
149 * what we can do is give them a kick so that we do not keep idle
150 * contexts around longer than is necessary.
152 if (shrink & I915_SHRINK_ACTIVE) {
153 for_each_gt(gt, i915, i)
154 /* Retire requests to unpin all idle contexts */
155 intel_gt_retire_requests(gt);
159 * As we may completely rewrite the (un)bound list whilst unbinding
160 * (due to retiring requests) we have to strictly process only
161 * one element of the list at the time, and recheck the list
162 * on every iteration.
164 * In particular, we must hold a reference whilst removing the
165 * object as we may end up waiting for and/or retiring the objects.
166 * This might release the final reference (held by the active list)
167 * and result in the object being freed from under us. This is
168 * similar to the precautions the eviction code must take whilst
171 * Also note that although these lists do not hold a reference to
172 * the object we can safely grab one here: The final object
173 * unreferencing and the bound_list are both protected by the
174 * dev->struct_mutex and so we won't ever be able to observe an
175 * object on the bound_list with a reference count equals 0.
177 for (phase = phases; phase->list; phase++) {
178 struct list_head still_in_list;
179 struct drm_i915_gem_object *obj;
182 if ((shrink & phase->bit) == 0)
185 INIT_LIST_HEAD(&still_in_list);
188 * We serialize our access to unreferenced objects through
189 * the use of the struct_mutex. While the objects are not
190 * yet freed (due to RCU then a workqueue) we still want
191 * to be able to shrink their pages, so they remain on
192 * the unbound/bound list until actually freed.
194 spin_lock_irqsave(&i915->mm.obj_lock, flags);
195 while (count < target &&
196 (obj = list_first_entry_or_null(phase->list,
199 list_move_tail(&obj->mm.link, &still_in_list);
201 if (shrink & I915_SHRINK_VMAPS &&
202 !is_vmalloc_addr(obj->mm.mapping))
205 if (!(shrink & I915_SHRINK_ACTIVE) &&
206 i915_gem_object_is_framebuffer(obj))
209 if (!can_release_pages(obj))
212 if (!kref_get_unless_zero(&obj->base.refcount))
215 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
217 /* May arrive from get_pages on another bo */
219 if (!i915_gem_object_trylock(obj, NULL))
222 err = i915_gem_object_lock(obj, ww);
227 if (drop_pages(obj, shrink, trylock_vm) &&
228 !__i915_gem_object_put_pages(obj) &&
229 !try_to_writeback(obj, shrink))
230 count += obj->base.size >> PAGE_SHIFT;
233 i915_gem_object_unlock(obj);
235 scanned += obj->base.size >> PAGE_SHIFT;
237 i915_gem_object_put(obj);
239 spin_lock_irqsave(&i915->mm.obj_lock, flags);
243 list_splice_tail(&still_in_list, phase->list);
244 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
249 if (shrink & I915_SHRINK_BOUND)
250 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
256 *nr_scanned += scanned;
261 * i915_gem_shrink_all - Shrink buffer object caches completely
264 * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
265 * caches completely. It also first waits for and retires all outstanding
266 * requests to also be able to release backing storage for active objects.
268 * This should only be used in code to intentionally quiescent the gpu or as a
269 * last-ditch effort when memory seems to have run out.
272 * The number of pages of backing storage actually released.
274 unsigned long i915_gem_shrink_all(struct drm_i915_private *i915)
276 intel_wakeref_t wakeref;
277 unsigned long freed = 0;
279 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
280 freed = i915_gem_shrink(NULL, i915, -1UL, NULL,
282 I915_SHRINK_UNBOUND);
289 i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
291 struct drm_i915_private *i915 = shrinker->private_data;
292 unsigned long num_objects;
295 count = READ_ONCE(i915->mm.shrink_memory) >> PAGE_SHIFT;
296 num_objects = READ_ONCE(i915->mm.shrink_count);
299 * Update our preferred vmscan batch size for the next pass.
300 * Our rough guess for an effective batch size is roughly 2
301 * available GEM objects worth of pages. That is we don't want
302 * the shrinker to fire, until it is worth the cost of freeing an
306 unsigned long avg = 2 * count / num_objects;
308 i915->mm.shrinker->batch =
309 max((i915->mm.shrinker->batch + avg) >> 1,
310 128ul /* default SHRINK_BATCH */);
317 i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
319 struct drm_i915_private *i915 = shrinker->private_data;
324 freed = i915_gem_shrink(NULL, i915,
328 I915_SHRINK_UNBOUND);
329 if (sc->nr_scanned < sc->nr_to_scan && current_is_kswapd()) {
330 intel_wakeref_t wakeref;
332 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
333 freed += i915_gem_shrink(NULL, i915,
334 sc->nr_to_scan - sc->nr_scanned,
338 I915_SHRINK_UNBOUND |
339 I915_SHRINK_WRITEBACK);
343 return sc->nr_scanned ? freed : SHRINK_STOP;
347 i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
349 struct drm_i915_private *i915 =
350 container_of(nb, struct drm_i915_private, mm.oom_notifier);
351 struct drm_i915_gem_object *obj;
352 unsigned long unevictable, available, freed_pages;
353 intel_wakeref_t wakeref;
357 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
358 freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
360 I915_SHRINK_UNBOUND |
361 I915_SHRINK_WRITEBACK);
363 /* Because we may be allocating inside our own driver, we cannot
364 * assert that there are no objects with pinned pages that are not
365 * being pointed to by hardware.
367 available = unevictable = 0;
368 spin_lock_irqsave(&i915->mm.obj_lock, flags);
369 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
370 if (!can_release_pages(obj))
371 unevictable += obj->base.size >> PAGE_SHIFT;
373 available += obj->base.size >> PAGE_SHIFT;
375 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
377 if (freed_pages || available)
378 pr_info("Purging GPU memory, %lu pages freed, "
379 "%lu pages still pinned, %lu pages left available.\n",
380 freed_pages, unevictable, available);
382 *(unsigned long *)ptr += freed_pages;
387 i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
389 struct drm_i915_private *i915 =
390 container_of(nb, struct drm_i915_private, mm.vmap_notifier);
391 struct i915_vma *vma, *next;
392 unsigned long freed_pages = 0;
393 intel_wakeref_t wakeref;
397 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
398 freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
400 I915_SHRINK_UNBOUND |
403 /* We also want to clear any cached iomaps as they wrap vmap */
404 for_each_gt(gt, i915, i) {
405 mutex_lock(>->ggtt->vm.mutex);
406 list_for_each_entry_safe(vma, next,
407 >->ggtt->vm.bound_list, vm_link) {
408 unsigned long count = i915_vma_size(vma) >> PAGE_SHIFT;
409 struct drm_i915_gem_object *obj = vma->obj;
411 if (!vma->iomap || i915_vma_is_active(vma))
414 if (!i915_gem_object_trylock(obj, NULL))
417 if (__i915_vma_unbind(vma) == 0)
418 freed_pages += count;
420 i915_gem_object_unlock(obj);
422 mutex_unlock(>->ggtt->vm.mutex);
425 *(unsigned long *)ptr += freed_pages;
429 void i915_gem_driver_register__shrinker(struct drm_i915_private *i915)
431 i915->mm.shrinker = shrinker_alloc(0, "drm-i915_gem");
432 if (!i915->mm.shrinker) {
433 drm_WARN_ON(&i915->drm, 1);
435 i915->mm.shrinker->scan_objects = i915_gem_shrinker_scan;
436 i915->mm.shrinker->count_objects = i915_gem_shrinker_count;
437 i915->mm.shrinker->batch = 4096;
438 i915->mm.shrinker->private_data = i915;
440 shrinker_register(i915->mm.shrinker);
443 i915->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
444 drm_WARN_ON(&i915->drm, register_oom_notifier(&i915->mm.oom_notifier));
446 i915->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
447 drm_WARN_ON(&i915->drm,
448 register_vmap_purge_notifier(&i915->mm.vmap_notifier));
451 void i915_gem_driver_unregister__shrinker(struct drm_i915_private *i915)
453 drm_WARN_ON(&i915->drm,
454 unregister_vmap_purge_notifier(&i915->mm.vmap_notifier));
455 drm_WARN_ON(&i915->drm,
456 unregister_oom_notifier(&i915->mm.oom_notifier));
457 shrinker_free(i915->mm.shrinker);
460 void i915_gem_shrinker_taints_mutex(struct drm_i915_private *i915,
463 if (!IS_ENABLED(CONFIG_LOCKDEP))
466 fs_reclaim_acquire(GFP_KERNEL);
468 mutex_acquire(&mutex->dep_map, 0, 0, _RET_IP_);
469 mutex_release(&mutex->dep_map, _RET_IP_);
471 fs_reclaim_release(GFP_KERNEL);
475 * i915_gem_object_make_unshrinkable - Hide the object from the shrinker. By
476 * default all object types that support shrinking(see IS_SHRINKABLE), will also
477 * make the object visible to the shrinker after allocating the system memory
479 * @obj: The GEM object.
481 * This is typically used for special kernel internal objects that can't be
482 * easily processed by the shrinker, like if they are perma-pinned.
484 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj)
486 struct drm_i915_private *i915 = obj_to_i915(obj);
490 * We can only be called while the pages are pinned or when
491 * the pages are released. If pinned, we should only be called
492 * from a single caller under controlled conditions; and on release
493 * only one caller may release us. Neither the two may cross.
495 if (atomic_add_unless(&obj->mm.shrink_pin, 1, 0))
498 spin_lock_irqsave(&i915->mm.obj_lock, flags);
499 if (!atomic_fetch_inc(&obj->mm.shrink_pin) &&
500 !list_empty(&obj->mm.link)) {
501 list_del_init(&obj->mm.link);
502 i915->mm.shrink_count--;
503 i915->mm.shrink_memory -= obj->base.size;
505 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
508 static void ___i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj,
509 struct list_head *head)
511 struct drm_i915_private *i915 = obj_to_i915(obj);
514 if (!i915_gem_object_is_shrinkable(obj))
517 if (atomic_add_unless(&obj->mm.shrink_pin, -1, 1))
520 spin_lock_irqsave(&i915->mm.obj_lock, flags);
521 GEM_BUG_ON(!kref_read(&obj->base.refcount));
522 if (atomic_dec_and_test(&obj->mm.shrink_pin)) {
523 GEM_BUG_ON(!list_empty(&obj->mm.link));
525 list_add_tail(&obj->mm.link, head);
526 i915->mm.shrink_count++;
527 i915->mm.shrink_memory += obj->base.size;
530 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
534 * __i915_gem_object_make_shrinkable - Move the object to the tail of the
535 * shrinkable list. Objects on this list might be swapped out. Used with
537 * @obj: The GEM object.
539 * DO NOT USE. This is intended to be called on very special objects that don't
540 * yet have mm.pages, but are guaranteed to have potentially reclaimable pages
543 void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj)
545 ___i915_gem_object_make_shrinkable(obj,
546 &obj_to_i915(obj)->mm.shrink_list);
550 * __i915_gem_object_make_purgeable - Move the object to the tail of the
551 * purgeable list. Objects on this list might be swapped out. Used with
553 * @obj: The GEM object.
555 * DO NOT USE. This is intended to be called on very special objects that don't
556 * yet have mm.pages, but are guaranteed to have potentially reclaimable pages
559 void __i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj)
561 ___i915_gem_object_make_shrinkable(obj,
562 &obj_to_i915(obj)->mm.purge_list);
566 * i915_gem_object_make_shrinkable - Move the object to the tail of the
567 * shrinkable list. Objects on this list might be swapped out. Used with
569 * @obj: The GEM object.
571 * MUST only be called on objects which have backing pages.
573 * MUST be balanced with previous call to i915_gem_object_make_unshrinkable().
575 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj)
577 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
578 __i915_gem_object_make_shrinkable(obj);
582 * i915_gem_object_make_purgeable - Move the object to the tail of the purgeable
583 * list. Used with DONTNEED objects. Unlike with shrinkable objects, the
584 * shrinker will attempt to discard the backing pages, instead of trying to swap
586 * @obj: The GEM object.
588 * MUST only be called on objects which have backing pages.
590 * MUST be balanced with previous call to i915_gem_object_make_unshrinkable().
592 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj)
594 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
595 __i915_gem_object_make_purgeable(obj);