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