xref: /linux/drivers/gpu/drm/i915/gem/i915_gem_shrinker.c (revision 15a1fbdcfb519c2bd291ed01c6c94e0b89537a77)
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2008-2015 Intel Corporation
5  */
6 
7 #include <linux/oom.h>
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>
15 #include <drm/i915_drm.h>
16 
17 #include "i915_trace.h"
18 
19 static bool swap_available(void)
20 {
21 	return get_nr_swap_pages() > 0;
22 }
23 
24 static bool can_release_pages(struct drm_i915_gem_object *obj)
25 {
26 	/* Consider only shrinkable ojects. */
27 	if (!i915_gem_object_is_shrinkable(obj))
28 		return false;
29 
30 	/*
31 	 * Only report true if by unbinding the object and putting its pages
32 	 * we can actually make forward progress towards freeing physical
33 	 * pages.
34 	 *
35 	 * If the pages are pinned for any other reason than being bound
36 	 * to the GPU, simply unbinding from the GPU is not going to succeed
37 	 * in releasing our pin count on the pages themselves.
38 	 */
39 	if (atomic_read(&obj->mm.pages_pin_count) > atomic_read(&obj->bind_count))
40 		return false;
41 
42 	/*
43 	 * We can only return physical pages to the system if we can either
44 	 * discard the contents (because the user has marked them as being
45 	 * purgeable) or if we can move their contents out to swap.
46 	 */
47 	return swap_available() || obj->mm.madv == I915_MADV_DONTNEED;
48 }
49 
50 static bool unsafe_drop_pages(struct drm_i915_gem_object *obj,
51 			      unsigned long shrink)
52 {
53 	unsigned long flags;
54 
55 	flags = 0;
56 	if (shrink & I915_SHRINK_ACTIVE)
57 		flags = I915_GEM_OBJECT_UNBIND_ACTIVE;
58 
59 	if (i915_gem_object_unbind(obj, flags) == 0)
60 		__i915_gem_object_put_pages(obj);
61 
62 	return !i915_gem_object_has_pages(obj);
63 }
64 
65 static void try_to_writeback(struct drm_i915_gem_object *obj,
66 			     unsigned int flags)
67 {
68 	switch (obj->mm.madv) {
69 	case I915_MADV_DONTNEED:
70 		i915_gem_object_truncate(obj);
71 	case __I915_MADV_PURGED:
72 		return;
73 	}
74 
75 	if (flags & I915_SHRINK_WRITEBACK)
76 		i915_gem_object_writeback(obj);
77 }
78 
79 /**
80  * i915_gem_shrink - Shrink buffer object caches
81  * @i915: i915 device
82  * @target: amount of memory to make available, in pages
83  * @nr_scanned: optional output for number of pages scanned (incremental)
84  * @shrink: control flags for selecting cache types
85  *
86  * This function is the main interface to the shrinker. It will try to release
87  * up to @target pages of main memory backing storage from buffer objects.
88  * Selection of the specific caches can be done with @flags. This is e.g. useful
89  * when purgeable objects should be removed from caches preferentially.
90  *
91  * Note that it's not guaranteed that released amount is actually available as
92  * free system memory - the pages might still be in-used to due to other reasons
93  * (like cpu mmaps) or the mm core has reused them before we could grab them.
94  * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
95  * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
96  *
97  * Also note that any kind of pinning (both per-vma address space pins and
98  * backing storage pins at the buffer object level) result in the shrinker code
99  * having to skip the object.
100  *
101  * Returns:
102  * The number of pages of backing storage actually released.
103  */
104 unsigned long
105 i915_gem_shrink(struct drm_i915_private *i915,
106 		unsigned long target,
107 		unsigned long *nr_scanned,
108 		unsigned int shrink)
109 {
110 	const struct {
111 		struct list_head *list;
112 		unsigned int bit;
113 	} phases[] = {
114 		{ &i915->mm.purge_list, ~0u },
115 		{
116 			&i915->mm.shrink_list,
117 			I915_SHRINK_BOUND | I915_SHRINK_UNBOUND
118 		},
119 		{ NULL, 0 },
120 	}, *phase;
121 	intel_wakeref_t wakeref = 0;
122 	unsigned long count = 0;
123 	unsigned long scanned = 0;
124 
125 	/*
126 	 * When shrinking the active list, we should also consider active
127 	 * contexts. Active contexts are pinned until they are retired, and
128 	 * so can not be simply unbound to retire and unpin their pages. To
129 	 * shrink the contexts, we must wait until the gpu is idle and
130 	 * completed its switch to the kernel context. In short, we do
131 	 * not have a good mechanism for idling a specific context.
132 	 */
133 
134 	trace_i915_gem_shrink(i915, target, shrink);
135 
136 	/*
137 	 * Unbinding of objects will require HW access; Let us not wake the
138 	 * device just to recover a little memory. If absolutely necessary,
139 	 * we will force the wake during oom-notifier.
140 	 */
141 	if (shrink & I915_SHRINK_BOUND) {
142 		wakeref = intel_runtime_pm_get_if_in_use(&i915->runtime_pm);
143 		if (!wakeref)
144 			shrink &= ~I915_SHRINK_BOUND;
145 	}
146 
147 	/*
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.
152 	 *
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
158 	 * removing objects.
159 	 *
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.
165 	 */
166 	for (phase = phases; phase->list; phase++) {
167 		struct list_head still_in_list;
168 		struct drm_i915_gem_object *obj;
169 		unsigned long flags;
170 
171 		if ((shrink & phase->bit) == 0)
172 			continue;
173 
174 		INIT_LIST_HEAD(&still_in_list);
175 
176 		/*
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.
182 		 */
183 		spin_lock_irqsave(&i915->mm.obj_lock, flags);
184 		while (count < target &&
185 		       (obj = list_first_entry_or_null(phase->list,
186 						       typeof(*obj),
187 						       mm.link))) {
188 			list_move_tail(&obj->mm.link, &still_in_list);
189 
190 			if (shrink & I915_SHRINK_VMAPS &&
191 			    !is_vmalloc_addr(obj->mm.mapping))
192 				continue;
193 
194 			if (!(shrink & I915_SHRINK_ACTIVE) &&
195 			    i915_gem_object_is_framebuffer(obj))
196 				continue;
197 
198 			if (!(shrink & I915_SHRINK_BOUND) &&
199 			    atomic_read(&obj->bind_count))
200 				continue;
201 
202 			if (!can_release_pages(obj))
203 				continue;
204 
205 			if (!kref_get_unless_zero(&obj->base.refcount))
206 				continue;
207 
208 			spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
209 
210 			if (unsafe_drop_pages(obj, shrink)) {
211 				/* May arrive from get_pages on another bo */
212 				mutex_lock(&obj->mm.lock);
213 				if (!i915_gem_object_has_pages(obj)) {
214 					try_to_writeback(obj, shrink);
215 					count += obj->base.size >> PAGE_SHIFT;
216 				}
217 				mutex_unlock(&obj->mm.lock);
218 			}
219 
220 			scanned += obj->base.size >> PAGE_SHIFT;
221 			i915_gem_object_put(obj);
222 
223 			spin_lock_irqsave(&i915->mm.obj_lock, flags);
224 		}
225 		list_splice_tail(&still_in_list, phase->list);
226 		spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
227 	}
228 
229 	if (shrink & I915_SHRINK_BOUND)
230 		intel_runtime_pm_put(&i915->runtime_pm, wakeref);
231 
232 	if (nr_scanned)
233 		*nr_scanned += scanned;
234 	return count;
235 }
236 
237 /**
238  * i915_gem_shrink_all - Shrink buffer object caches completely
239  * @i915: i915 device
240  *
241  * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
242  * caches completely. It also first waits for and retires all outstanding
243  * requests to also be able to release backing storage for active objects.
244  *
245  * This should only be used in code to intentionally quiescent the gpu or as a
246  * last-ditch effort when memory seems to have run out.
247  *
248  * Returns:
249  * The number of pages of backing storage actually released.
250  */
251 unsigned long i915_gem_shrink_all(struct drm_i915_private *i915)
252 {
253 	intel_wakeref_t wakeref;
254 	unsigned long freed = 0;
255 
256 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
257 		freed = i915_gem_shrink(i915, -1UL, NULL,
258 					I915_SHRINK_BOUND |
259 					I915_SHRINK_UNBOUND);
260 	}
261 
262 	return freed;
263 }
264 
265 static unsigned long
266 i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
267 {
268 	struct drm_i915_private *i915 =
269 		container_of(shrinker, struct drm_i915_private, mm.shrinker);
270 	unsigned long num_objects;
271 	unsigned long count;
272 
273 	count = READ_ONCE(i915->mm.shrink_memory) >> PAGE_SHIFT;
274 	num_objects = READ_ONCE(i915->mm.shrink_count);
275 
276 	/*
277 	 * Update our preferred vmscan batch size for the next pass.
278 	 * Our rough guess for an effective batch size is roughly 2
279 	 * available GEM objects worth of pages. That is we don't want
280 	 * the shrinker to fire, until it is worth the cost of freeing an
281 	 * entire GEM object.
282 	 */
283 	if (num_objects) {
284 		unsigned long avg = 2 * count / num_objects;
285 
286 		i915->mm.shrinker.batch =
287 			max((i915->mm.shrinker.batch + avg) >> 1,
288 			    128ul /* default SHRINK_BATCH */);
289 	}
290 
291 	return count;
292 }
293 
294 static unsigned long
295 i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
296 {
297 	struct drm_i915_private *i915 =
298 		container_of(shrinker, struct drm_i915_private, mm.shrinker);
299 	unsigned long freed;
300 
301 	sc->nr_scanned = 0;
302 
303 	freed = i915_gem_shrink(i915,
304 				sc->nr_to_scan,
305 				&sc->nr_scanned,
306 				I915_SHRINK_BOUND |
307 				I915_SHRINK_UNBOUND);
308 	if (sc->nr_scanned < sc->nr_to_scan && current_is_kswapd()) {
309 		intel_wakeref_t wakeref;
310 
311 		with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
312 			freed += i915_gem_shrink(i915,
313 						 sc->nr_to_scan - sc->nr_scanned,
314 						 &sc->nr_scanned,
315 						 I915_SHRINK_ACTIVE |
316 						 I915_SHRINK_BOUND |
317 						 I915_SHRINK_UNBOUND |
318 						 I915_SHRINK_WRITEBACK);
319 		}
320 	}
321 
322 	return sc->nr_scanned ? freed : SHRINK_STOP;
323 }
324 
325 static int
326 i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
327 {
328 	struct drm_i915_private *i915 =
329 		container_of(nb, struct drm_i915_private, mm.oom_notifier);
330 	struct drm_i915_gem_object *obj;
331 	unsigned long unevictable, available, freed_pages;
332 	intel_wakeref_t wakeref;
333 	unsigned long flags;
334 
335 	freed_pages = 0;
336 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
337 		freed_pages += i915_gem_shrink(i915, -1UL, NULL,
338 					       I915_SHRINK_BOUND |
339 					       I915_SHRINK_UNBOUND |
340 					       I915_SHRINK_WRITEBACK);
341 
342 	/* Because we may be allocating inside our own driver, we cannot
343 	 * assert that there are no objects with pinned pages that are not
344 	 * being pointed to by hardware.
345 	 */
346 	available = unevictable = 0;
347 	spin_lock_irqsave(&i915->mm.obj_lock, flags);
348 	list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
349 		if (!can_release_pages(obj))
350 			unevictable += obj->base.size >> PAGE_SHIFT;
351 		else
352 			available += obj->base.size >> PAGE_SHIFT;
353 	}
354 	spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
355 
356 	if (freed_pages || available)
357 		pr_info("Purging GPU memory, %lu pages freed, "
358 			"%lu pages still pinned, %lu pages left available.\n",
359 			freed_pages, unevictable, available);
360 
361 	*(unsigned long *)ptr += freed_pages;
362 	return NOTIFY_DONE;
363 }
364 
365 static int
366 i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
367 {
368 	struct drm_i915_private *i915 =
369 		container_of(nb, struct drm_i915_private, mm.vmap_notifier);
370 	struct i915_vma *vma, *next;
371 	unsigned long freed_pages = 0;
372 	intel_wakeref_t wakeref;
373 
374 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
375 		freed_pages += i915_gem_shrink(i915, -1UL, NULL,
376 					       I915_SHRINK_BOUND |
377 					       I915_SHRINK_UNBOUND |
378 					       I915_SHRINK_VMAPS);
379 
380 	/* We also want to clear any cached iomaps as they wrap vmap */
381 	mutex_lock(&i915->ggtt.vm.mutex);
382 	list_for_each_entry_safe(vma, next,
383 				 &i915->ggtt.vm.bound_list, vm_link) {
384 		unsigned long count = vma->node.size >> PAGE_SHIFT;
385 
386 		if (!vma->iomap || i915_vma_is_active(vma))
387 			continue;
388 
389 		if (__i915_vma_unbind(vma) == 0)
390 			freed_pages += count;
391 	}
392 	mutex_unlock(&i915->ggtt.vm.mutex);
393 
394 	*(unsigned long *)ptr += freed_pages;
395 	return NOTIFY_DONE;
396 }
397 
398 void i915_gem_driver_register__shrinker(struct drm_i915_private *i915)
399 {
400 	i915->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
401 	i915->mm.shrinker.count_objects = i915_gem_shrinker_count;
402 	i915->mm.shrinker.seeks = DEFAULT_SEEKS;
403 	i915->mm.shrinker.batch = 4096;
404 	drm_WARN_ON(&i915->drm, register_shrinker(&i915->mm.shrinker));
405 
406 	i915->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
407 	drm_WARN_ON(&i915->drm, register_oom_notifier(&i915->mm.oom_notifier));
408 
409 	i915->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
410 	drm_WARN_ON(&i915->drm,
411 		    register_vmap_purge_notifier(&i915->mm.vmap_notifier));
412 }
413 
414 void i915_gem_driver_unregister__shrinker(struct drm_i915_private *i915)
415 {
416 	drm_WARN_ON(&i915->drm,
417 		    unregister_vmap_purge_notifier(&i915->mm.vmap_notifier));
418 	drm_WARN_ON(&i915->drm,
419 		    unregister_oom_notifier(&i915->mm.oom_notifier));
420 	unregister_shrinker(&i915->mm.shrinker);
421 }
422 
423 void i915_gem_shrinker_taints_mutex(struct drm_i915_private *i915,
424 				    struct mutex *mutex)
425 {
426 	bool unlock = false;
427 
428 	if (!IS_ENABLED(CONFIG_LOCKDEP))
429 		return;
430 
431 	if (!lockdep_is_held_type(&i915->drm.struct_mutex, -1)) {
432 		mutex_acquire(&i915->drm.struct_mutex.dep_map,
433 			      I915_MM_NORMAL, 0, _RET_IP_);
434 		unlock = true;
435 	}
436 
437 	fs_reclaim_acquire(GFP_KERNEL);
438 
439 	mutex_acquire(&mutex->dep_map, 0, 0, _RET_IP_);
440 	mutex_release(&mutex->dep_map, _RET_IP_);
441 
442 	fs_reclaim_release(GFP_KERNEL);
443 
444 	if (unlock)
445 		mutex_release(&i915->drm.struct_mutex.dep_map, _RET_IP_);
446 }
447 
448 #define obj_to_i915(obj__) to_i915((obj__)->base.dev)
449 
450 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj)
451 {
452 	struct drm_i915_private *i915 = obj_to_i915(obj);
453 	unsigned long flags;
454 
455 	/*
456 	 * We can only be called while the pages are pinned or when
457 	 * the pages are released. If pinned, we should only be called
458 	 * from a single caller under controlled conditions; and on release
459 	 * only one caller may release us. Neither the two may cross.
460 	 */
461 	if (atomic_add_unless(&obj->mm.shrink_pin, 1, 0))
462 		return;
463 
464 	spin_lock_irqsave(&i915->mm.obj_lock, flags);
465 	if (!atomic_fetch_inc(&obj->mm.shrink_pin) &&
466 	    !list_empty(&obj->mm.link)) {
467 		list_del_init(&obj->mm.link);
468 		i915->mm.shrink_count--;
469 		i915->mm.shrink_memory -= obj->base.size;
470 	}
471 	spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
472 }
473 
474 static void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj,
475 					      struct list_head *head)
476 {
477 	struct drm_i915_private *i915 = obj_to_i915(obj);
478 	unsigned long flags;
479 
480 	GEM_BUG_ON(!i915_gem_object_has_pages(obj));
481 	if (!i915_gem_object_is_shrinkable(obj))
482 		return;
483 
484 	if (atomic_add_unless(&obj->mm.shrink_pin, -1, 1))
485 		return;
486 
487 	spin_lock_irqsave(&i915->mm.obj_lock, flags);
488 	GEM_BUG_ON(!kref_read(&obj->base.refcount));
489 	if (atomic_dec_and_test(&obj->mm.shrink_pin)) {
490 		GEM_BUG_ON(!list_empty(&obj->mm.link));
491 
492 		list_add_tail(&obj->mm.link, head);
493 		i915->mm.shrink_count++;
494 		i915->mm.shrink_memory += obj->base.size;
495 
496 	}
497 	spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
498 }
499 
500 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj)
501 {
502 	__i915_gem_object_make_shrinkable(obj,
503 					  &obj_to_i915(obj)->mm.shrink_list);
504 }
505 
506 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj)
507 {
508 	__i915_gem_object_make_shrinkable(obj,
509 					  &obj_to_i915(obj)->mm.purge_list);
510 }
511