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,
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 void try_to_writeback(struct drm_i915_gem_object *obj,
62 switch (obj->mm.madv) {
63 case I915_MADV_DONTNEED:
64 i915_gem_object_truncate(obj);
66 case __I915_MADV_PURGED:
70 if (flags & I915_SHRINK_WRITEBACK)
71 i915_gem_object_writeback(obj);
75 * i915_gem_shrink - Shrink buffer object caches
76 * @ww: i915 gem ww acquire ctx, or NULL
78 * @target: amount of memory to make available, in pages
79 * @nr_scanned: optional output for number of pages scanned (incremental)
80 * @shrink: control flags for selecting cache types
82 * This function is the main interface to the shrinker. It will try to release
83 * up to @target pages of main memory backing storage from buffer objects.
84 * Selection of the specific caches can be done with @flags. This is e.g. useful
85 * when purgeable objects should be removed from caches preferentially.
87 * Note that it's not guaranteed that released amount is actually available as
88 * free system memory - the pages might still be in-used to due to other reasons
89 * (like cpu mmaps) or the mm core has reused them before we could grab them.
90 * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
91 * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
93 * Also note that any kind of pinning (both per-vma address space pins and
94 * backing storage pins at the buffer object level) result in the shrinker code
95 * having to skip the object.
98 * The number of pages of backing storage actually released.
101 i915_gem_shrink(struct i915_gem_ww_ctx *ww,
102 struct drm_i915_private *i915,
103 unsigned long target,
104 unsigned long *nr_scanned,
108 struct list_head *list;
111 { &i915->mm.purge_list, ~0u },
113 &i915->mm.shrink_list,
114 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND
118 intel_wakeref_t wakeref = 0;
119 unsigned long count = 0;
120 unsigned long scanned = 0;
123 /* CHV + VTD workaround use stop_machine(); need to trylock vm->mutex */
124 bool trylock_vm = !ww && intel_vm_no_concurrent_access_wa(i915);
126 trace_i915_gem_shrink(i915, target, shrink);
129 * Unbinding of objects will require HW access; Let us not wake the
130 * device just to recover a little memory. If absolutely necessary,
131 * we will force the wake during oom-notifier.
133 if (shrink & I915_SHRINK_BOUND) {
134 wakeref = intel_runtime_pm_get_if_in_use(&i915->runtime_pm);
136 shrink &= ~I915_SHRINK_BOUND;
140 * When shrinking the active list, we should also consider active
141 * contexts. Active contexts are pinned until they are retired, and
142 * so can not be simply unbound to retire and unpin their pages. To
143 * shrink the contexts, we must wait until the gpu is idle and
144 * completed its switch to the kernel context. In short, we do
145 * not have a good mechanism for idling a specific context, but
146 * what we can do is give them a kick so that we do not keep idle
147 * contexts around longer than is necessary.
149 if (shrink & I915_SHRINK_ACTIVE)
150 /* Retire requests to unpin all idle contexts */
151 intel_gt_retire_requests(&i915->gt);
154 * As we may completely rewrite the (un)bound list whilst unbinding
155 * (due to retiring requests) we have to strictly process only
156 * one element of the list at the time, and recheck the list
157 * on every iteration.
159 * In particular, we must hold a reference whilst removing the
160 * object as we may end up waiting for and/or retiring the objects.
161 * This might release the final reference (held by the active list)
162 * and result in the object being freed from under us. This is
163 * similar to the precautions the eviction code must take whilst
166 * Also note that although these lists do not hold a reference to
167 * the object we can safely grab one here: The final object
168 * unreferencing and the bound_list are both protected by the
169 * dev->struct_mutex and so we won't ever be able to observe an
170 * object on the bound_list with a reference count equals 0.
172 for (phase = phases; phase->list; phase++) {
173 struct list_head still_in_list;
174 struct drm_i915_gem_object *obj;
177 if ((shrink & phase->bit) == 0)
180 INIT_LIST_HEAD(&still_in_list);
183 * We serialize our access to unreferenced objects through
184 * the use of the struct_mutex. While the objects are not
185 * yet freed (due to RCU then a workqueue) we still want
186 * to be able to shrink their pages, so they remain on
187 * the unbound/bound list until actually freed.
189 spin_lock_irqsave(&i915->mm.obj_lock, flags);
190 while (count < target &&
191 (obj = list_first_entry_or_null(phase->list,
194 list_move_tail(&obj->mm.link, &still_in_list);
196 if (shrink & I915_SHRINK_VMAPS &&
197 !is_vmalloc_addr(obj->mm.mapping))
200 if (!(shrink & I915_SHRINK_ACTIVE) &&
201 i915_gem_object_is_framebuffer(obj))
204 if (!can_release_pages(obj))
207 if (!kref_get_unless_zero(&obj->base.refcount))
210 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
213 if (unsafe_drop_pages(obj, shrink, trylock_vm)) {
214 /* May arrive from get_pages on another bo */
216 if (!i915_gem_object_trylock(obj))
219 err = i915_gem_object_lock(obj, ww);
224 if (!__i915_gem_object_put_pages(obj)) {
225 try_to_writeback(obj, shrink);
226 count += obj->base.size >> PAGE_SHIFT;
229 i915_gem_object_unlock(obj);
232 dma_resv_prune(obj->base.resv);
234 scanned += obj->base.size >> PAGE_SHIFT;
236 i915_gem_object_put(obj);
238 spin_lock_irqsave(&i915->mm.obj_lock, flags);
242 list_splice_tail(&still_in_list, phase->list);
243 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
248 if (shrink & I915_SHRINK_BOUND)
249 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
252 *nr_scanned += scanned;
257 * i915_gem_shrink_all - Shrink buffer object caches completely
260 * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
261 * caches completely. It also first waits for and retires all outstanding
262 * requests to also be able to release backing storage for active objects.
264 * This should only be used in code to intentionally quiescent the gpu or as a
265 * last-ditch effort when memory seems to have run out.
268 * The number of pages of backing storage actually released.
270 unsigned long i915_gem_shrink_all(struct drm_i915_private *i915)
272 intel_wakeref_t wakeref;
273 unsigned long freed = 0;
275 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
276 freed = i915_gem_shrink(NULL, i915, -1UL, NULL,
278 I915_SHRINK_UNBOUND);
285 i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
287 struct drm_i915_private *i915 =
288 container_of(shrinker, struct drm_i915_private, mm.shrinker);
289 unsigned long num_objects;
292 count = READ_ONCE(i915->mm.shrink_memory) >> PAGE_SHIFT;
293 num_objects = READ_ONCE(i915->mm.shrink_count);
296 * Update our preferred vmscan batch size for the next pass.
297 * Our rough guess for an effective batch size is roughly 2
298 * available GEM objects worth of pages. That is we don't want
299 * the shrinker to fire, until it is worth the cost of freeing an
303 unsigned long avg = 2 * count / num_objects;
305 i915->mm.shrinker.batch =
306 max((i915->mm.shrinker.batch + avg) >> 1,
307 128ul /* default SHRINK_BATCH */);
314 i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
316 struct drm_i915_private *i915 =
317 container_of(shrinker, struct drm_i915_private, mm.shrinker);
322 freed = i915_gem_shrink(NULL, i915,
326 I915_SHRINK_UNBOUND);
327 if (sc->nr_scanned < sc->nr_to_scan && current_is_kswapd()) {
328 intel_wakeref_t wakeref;
330 with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
331 freed += i915_gem_shrink(NULL, i915,
332 sc->nr_to_scan - sc->nr_scanned,
336 I915_SHRINK_UNBOUND |
337 I915_SHRINK_WRITEBACK);
341 return sc->nr_scanned ? freed : SHRINK_STOP;
345 i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
347 struct drm_i915_private *i915 =
348 container_of(nb, struct drm_i915_private, mm.oom_notifier);
349 struct drm_i915_gem_object *obj;
350 unsigned long unevictable, available, freed_pages;
351 intel_wakeref_t wakeref;
355 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
356 freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
358 I915_SHRINK_UNBOUND |
359 I915_SHRINK_WRITEBACK);
361 /* Because we may be allocating inside our own driver, we cannot
362 * assert that there are no objects with pinned pages that are not
363 * being pointed to by hardware.
365 available = unevictable = 0;
366 spin_lock_irqsave(&i915->mm.obj_lock, flags);
367 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
368 if (!can_release_pages(obj))
369 unevictable += obj->base.size >> PAGE_SHIFT;
371 available += obj->base.size >> PAGE_SHIFT;
373 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
375 if (freed_pages || available)
376 pr_info("Purging GPU memory, %lu pages freed, "
377 "%lu pages still pinned, %lu pages left available.\n",
378 freed_pages, unevictable, available);
380 *(unsigned long *)ptr += freed_pages;
385 i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
387 struct drm_i915_private *i915 =
388 container_of(nb, struct drm_i915_private, mm.vmap_notifier);
389 struct i915_vma *vma, *next;
390 unsigned long freed_pages = 0;
391 intel_wakeref_t wakeref;
393 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
394 freed_pages += i915_gem_shrink(NULL, i915, -1UL, NULL,
396 I915_SHRINK_UNBOUND |
399 /* We also want to clear any cached iomaps as they wrap vmap */
400 mutex_lock(&i915->ggtt.vm.mutex);
401 list_for_each_entry_safe(vma, next,
402 &i915->ggtt.vm.bound_list, vm_link) {
403 unsigned long count = vma->node.size >> PAGE_SHIFT;
405 if (!vma->iomap || i915_vma_is_active(vma))
408 if (__i915_vma_unbind(vma) == 0)
409 freed_pages += count;
411 mutex_unlock(&i915->ggtt.vm.mutex);
413 *(unsigned long *)ptr += freed_pages;
417 void i915_gem_driver_register__shrinker(struct drm_i915_private *i915)
419 i915->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
420 i915->mm.shrinker.count_objects = i915_gem_shrinker_count;
421 i915->mm.shrinker.seeks = DEFAULT_SEEKS;
422 i915->mm.shrinker.batch = 4096;
423 drm_WARN_ON(&i915->drm, register_shrinker(&i915->mm.shrinker));
425 i915->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
426 drm_WARN_ON(&i915->drm, register_oom_notifier(&i915->mm.oom_notifier));
428 i915->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
429 drm_WARN_ON(&i915->drm,
430 register_vmap_purge_notifier(&i915->mm.vmap_notifier));
433 void i915_gem_driver_unregister__shrinker(struct drm_i915_private *i915)
435 drm_WARN_ON(&i915->drm,
436 unregister_vmap_purge_notifier(&i915->mm.vmap_notifier));
437 drm_WARN_ON(&i915->drm,
438 unregister_oom_notifier(&i915->mm.oom_notifier));
439 unregister_shrinker(&i915->mm.shrinker);
442 void i915_gem_shrinker_taints_mutex(struct drm_i915_private *i915,
445 if (!IS_ENABLED(CONFIG_LOCKDEP))
448 fs_reclaim_acquire(GFP_KERNEL);
450 mutex_acquire(&mutex->dep_map, 0, 0, _RET_IP_);
451 mutex_release(&mutex->dep_map, _RET_IP_);
453 fs_reclaim_release(GFP_KERNEL);
456 #define obj_to_i915(obj__) to_i915((obj__)->base.dev)
458 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj)
460 struct drm_i915_private *i915 = obj_to_i915(obj);
464 * We can only be called while the pages are pinned or when
465 * the pages are released. If pinned, we should only be called
466 * from a single caller under controlled conditions; and on release
467 * only one caller may release us. Neither the two may cross.
469 if (atomic_add_unless(&obj->mm.shrink_pin, 1, 0))
472 spin_lock_irqsave(&i915->mm.obj_lock, flags);
473 if (!atomic_fetch_inc(&obj->mm.shrink_pin) &&
474 !list_empty(&obj->mm.link)) {
475 list_del_init(&obj->mm.link);
476 i915->mm.shrink_count--;
477 i915->mm.shrink_memory -= obj->base.size;
479 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
482 static void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj,
483 struct list_head *head)
485 struct drm_i915_private *i915 = obj_to_i915(obj);
488 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
489 if (!i915_gem_object_is_shrinkable(obj))
492 if (atomic_add_unless(&obj->mm.shrink_pin, -1, 1))
495 spin_lock_irqsave(&i915->mm.obj_lock, flags);
496 GEM_BUG_ON(!kref_read(&obj->base.refcount));
497 if (atomic_dec_and_test(&obj->mm.shrink_pin)) {
498 GEM_BUG_ON(!list_empty(&obj->mm.link));
500 list_add_tail(&obj->mm.link, head);
501 i915->mm.shrink_count++;
502 i915->mm.shrink_memory += obj->base.size;
505 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
508 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj)
510 __i915_gem_object_make_shrinkable(obj,
511 &obj_to_i915(obj)->mm.shrink_list);
514 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj)
516 __i915_gem_object_make_shrinkable(obj,
517 &obj_to_i915(obj)->mm.purge_list);