xref: /dflybsd-src/sys/dev/drm/i915/i915_gem_shrinker.c (revision 7491e5ac85fbf1e3b9867ca39213d74c8f640ded)
1 /*
2  * Copyright © 2008-2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/shmem_fs.h>
26 #include <linux/slab.h>
27 #include <linux/swap.h>
28 #include <linux/pci.h>
29 #include <linux/dma-buf.h>
30 #include <linux/vmalloc.h>
31 #include <drm/drmP.h>
32 #include <drm/i915_drm.h>
33 
34 #include "i915_drv.h"
35 #include "i915_trace.h"
36 
37 #if 0
38 static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
39 {
40 	if (!mutex_is_locked(mutex))
41 		return false;
42 
43 #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
44 	return mutex->owner == task;
45 #else
46 	/* Since UP may be pre-empted, we cannot assume that we own the lock */
47 	return false;
48 #endif
49 }
50 #endif
51 
52 static int num_vma_bound(struct drm_i915_gem_object *obj)
53 {
54 	struct i915_vma *vma;
55 	int count = 0;
56 
57 	list_for_each_entry(vma, &obj->vma_list, obj_link) {
58 		if (drm_mm_node_allocated(&vma->node))
59 			count++;
60 		if (vma->pin_count)
61 			count++;
62 	}
63 
64 	return count;
65 }
66 
67 static bool swap_available(void)
68 {
69 	return get_nr_swap_pages() > 0;
70 }
71 
72 static bool can_release_pages(struct drm_i915_gem_object *obj)
73 {
74 	/* Only shmemfs objects are backed by swap */
75 	if (!obj->base.filp)
76 		return false;
77 
78 	/* Only report true if by unbinding the object and putting its pages
79 	 * we can actually make forward progress towards freeing physical
80 	 * pages.
81 	 *
82 	 * If the pages are pinned for any other reason than being bound
83 	 * to the GPU, simply unbinding from the GPU is not going to succeed
84 	 * in releasing our pin count on the pages themselves.
85 	 */
86 	if (obj->pages_pin_count != num_vma_bound(obj))
87 		return false;
88 
89 	/* We can only return physical pages to the system if we can either
90 	 * discard the contents (because the user has marked them as being
91 	 * purgeable) or if we can move their contents out to swap.
92 	 */
93 	return swap_available() || obj->madv == I915_MADV_DONTNEED;
94 }
95 
96 /**
97  * i915_gem_shrink - Shrink buffer object caches
98  * @dev_priv: i915 device
99  * @target: amount of memory to make available, in pages
100  * @flags: control flags for selecting cache types
101  *
102  * This function is the main interface to the shrinker. It will try to release
103  * up to @target pages of main memory backing storage from buffer objects.
104  * Selection of the specific caches can be done with @flags. This is e.g. useful
105  * when purgeable objects should be removed from caches preferentially.
106  *
107  * Note that it's not guaranteed that released amount is actually available as
108  * free system memory - the pages might still be in-used to due to other reasons
109  * (like cpu mmaps) or the mm core has reused them before we could grab them.
110  * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
111  * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
112  *
113  * Also note that any kind of pinning (both per-vma address space pins and
114  * backing storage pins at the buffer object level) result in the shrinker code
115  * having to skip the object.
116  *
117  * Returns:
118  * The number of pages of backing storage actually released.
119  */
120 unsigned long
121 i915_gem_shrink(struct drm_i915_private *dev_priv,
122 		unsigned long target, unsigned flags)
123 {
124 	const struct {
125 		struct list_head *list;
126 		unsigned int bit;
127 	} phases[] = {
128 		{ &dev_priv->mm.unbound_list, I915_SHRINK_UNBOUND },
129 		{ &dev_priv->mm.bound_list, I915_SHRINK_BOUND },
130 		{ NULL, 0 },
131 	}, *phase;
132 	unsigned long count = 0;
133 
134 	trace_i915_gem_shrink(dev_priv, target, flags);
135 	i915_gem_retire_requests(dev_priv);
136 
137 	/*
138 	 * Unbinding of objects will require HW access; Let us not wake the
139 	 * device just to recover a little memory. If absolutely necessary,
140 	 * we will force the wake during oom-notifier.
141 	 */
142 	if ((flags & I915_SHRINK_BOUND) &&
143 	    !intel_runtime_pm_get_if_in_use(dev_priv))
144 		flags &= ~I915_SHRINK_BOUND;
145 
146 	/*
147 	 * As we may completely rewrite the (un)bound list whilst unbinding
148 	 * (due to retiring requests) we have to strictly process only
149 	 * one element of the list at the time, and recheck the list
150 	 * on every iteration.
151 	 *
152 	 * In particular, we must hold a reference whilst removing the
153 	 * object as we may end up waiting for and/or retiring the objects.
154 	 * This might release the final reference (held by the active list)
155 	 * and result in the object being freed from under us. This is
156 	 * similar to the precautions the eviction code must take whilst
157 	 * removing objects.
158 	 *
159 	 * Also note that although these lists do not hold a reference to
160 	 * the object we can safely grab one here: The final object
161 	 * unreferencing and the bound_list are both protected by the
162 	 * dev->struct_mutex and so we won't ever be able to observe an
163 	 * object on the bound_list with a reference count equals 0.
164 	 */
165 	for (phase = phases; phase->list; phase++) {
166 		struct list_head still_in_list;
167 
168 		if ((flags & phase->bit) == 0)
169 			continue;
170 
171 		INIT_LIST_HEAD(&still_in_list);
172 		while (count < target && !list_empty(phase->list)) {
173 			struct drm_i915_gem_object *obj;
174 			struct i915_vma *vma, *v;
175 
176 			obj = list_first_entry(phase->list,
177 					       typeof(*obj), global_list);
178 			list_move_tail(&obj->global_list, &still_in_list);
179 
180 			if (flags & I915_SHRINK_PURGEABLE &&
181 			    obj->madv != I915_MADV_DONTNEED)
182 				continue;
183 
184 			if (flags & I915_SHRINK_VMAPS &&
185 			    !is_vmalloc_addr(obj->mapping))
186 				continue;
187 
188 			if ((flags & I915_SHRINK_ACTIVE) == 0 && obj->active)
189 				continue;
190 
191 			if (!can_release_pages(obj))
192 				continue;
193 
194 			drm_gem_object_reference(&obj->base);
195 
196 			/* For the unbound phase, this should be a no-op! */
197 			list_for_each_entry_safe(vma, v,
198 						 &obj->vma_list, obj_link)
199 				if (i915_vma_unbind(vma))
200 					break;
201 
202 			if (i915_gem_object_put_pages(obj) == 0)
203 				count += obj->base.size >> PAGE_SHIFT;
204 
205 			drm_gem_object_unreference(&obj->base);
206 		}
207 		list_splice(&still_in_list, phase->list);
208 	}
209 
210 	if (flags & I915_SHRINK_BOUND)
211 		intel_runtime_pm_put(dev_priv);
212 
213 	i915_gem_retire_requests(dev_priv);
214 
215 	return count;
216 }
217 
218 /**
219  * i915_gem_shrink_all - Shrink buffer object caches completely
220  * @dev_priv: i915 device
221  *
222  * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
223  * caches completely. It also first waits for and retires all outstanding
224  * requests to also be able to release backing storage for active objects.
225  *
226  * This should only be used in code to intentionally quiescent the gpu or as a
227  * last-ditch effort when memory seems to have run out.
228  *
229  * Returns:
230  * The number of pages of backing storage actually released.
231  */
232 unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
233 {
234 	return i915_gem_shrink(dev_priv, -1UL,
235 			       I915_SHRINK_BOUND |
236 			       I915_SHRINK_UNBOUND |
237 			       I915_SHRINK_ACTIVE);
238 }
239 
240 #if 0
241 static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
242 {
243 	if (!mutex_trylock(&dev->struct_mutex)) {
244 		if (!mutex_is_locked_by(&dev->struct_mutex, current))
245 			return false;
246 
247 		if (to_i915(dev)->mm.shrinker_no_lock_stealing)
248 			return false;
249 
250 		*unlock = false;
251 	} else
252 		*unlock = true;
253 
254 	return true;
255 }
256 #endif
257 
258 static unsigned long
259 i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
260 {
261 #if 0
262 	struct drm_i915_private *dev_priv =
263 		container_of(shrinker, struct drm_i915_private, mm.shrinker);
264 	struct drm_device *dev = &dev_priv->drm;
265 	struct drm_i915_gem_object *obj;
266 	unsigned long count;
267 	bool unlock;
268 
269 	if (!i915_gem_shrinker_lock(dev, &unlock))
270 		return 0;
271 
272 	i915_gem_retire_requests(dev_priv);
273 
274 	count = 0;
275 	list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
276 		if (can_release_pages(obj))
277 			count += obj->base.size >> PAGE_SHIFT;
278 
279 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
280 		if (!obj->active && can_release_pages(obj))
281 			count += obj->base.size >> PAGE_SHIFT;
282 	}
283 
284 	if (unlock)
285 		mutex_unlock(&dev->struct_mutex);
286 
287 	return count;
288 #endif
289 	return 0;
290 }
291 
292 static unsigned long
293 i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
294 {
295 #if 0
296 	struct drm_i915_private *dev_priv =
297 		container_of(shrinker, struct drm_i915_private, mm.shrinker);
298 	struct drm_device *dev = &dev_priv->drm;
299 	unsigned long freed;
300 	bool unlock;
301 
302 	if (!i915_gem_shrinker_lock(dev, &unlock))
303 		return SHRINK_STOP;
304 
305 	freed = i915_gem_shrink(dev_priv,
306 				sc->nr_to_scan,
307 				I915_SHRINK_BOUND |
308 				I915_SHRINK_UNBOUND |
309 				I915_SHRINK_PURGEABLE);
310 	if (freed < sc->nr_to_scan)
311 		freed += i915_gem_shrink(dev_priv,
312 					 sc->nr_to_scan - freed,
313 					 I915_SHRINK_BOUND |
314 					 I915_SHRINK_UNBOUND);
315 	if (unlock)
316 		mutex_unlock(&dev->struct_mutex);
317 
318 	return freed;
319 #endif
320 	return 0;
321 }
322 
323 #if 0
324 struct shrinker_lock_uninterruptible {
325 	bool was_interruptible;
326 	bool unlock;
327 };
328 
329 static bool
330 i915_gem_shrinker_lock_uninterruptible(struct drm_i915_private *dev_priv,
331 				       struct shrinker_lock_uninterruptible *slu,
332 				       int timeout_ms)
333 {
334 	unsigned long timeout = msecs_to_jiffies(timeout_ms) + 1;
335 
336 	while (!i915_gem_shrinker_lock(&dev_priv->drm, &slu->unlock)) {
337 		schedule_timeout_killable(1);
338 		if (fatal_signal_pending(current))
339 			return false;
340 		if (--timeout == 0) {
341 			pr_err("Unable to lock GPU to purge memory.\n");
342 			return false;
343 		}
344 	}
345 
346 	slu->was_interruptible = dev_priv->mm.interruptible;
347 	dev_priv->mm.interruptible = false;
348 	return true;
349 }
350 
351 static void
352 i915_gem_shrinker_unlock_uninterruptible(struct drm_i915_private *dev_priv,
353 					 struct shrinker_lock_uninterruptible *slu)
354 {
355 	dev_priv->mm.interruptible = slu->was_interruptible;
356 	if (slu->unlock)
357 		mutex_unlock(&dev_priv->drm.struct_mutex);
358 }
359 
360 static int
361 i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
362 {
363 	struct drm_i915_private *dev_priv =
364 		container_of(nb, struct drm_i915_private, mm.oom_notifier);
365 	struct shrinker_lock_uninterruptible slu;
366 	struct drm_i915_gem_object *obj;
367 	unsigned long unevictable, bound, unbound, freed_pages;
368 
369 	if (!i915_gem_shrinker_lock_uninterruptible(dev_priv, &slu, 5000))
370 		return NOTIFY_DONE;
371 
372 	intel_runtime_pm_get(dev_priv);
373 	freed_pages = i915_gem_shrink_all(dev_priv);
374 	intel_runtime_pm_put(dev_priv);
375 
376 	/* Because we may be allocating inside our own driver, we cannot
377 	 * assert that there are no objects with pinned pages that are not
378 	 * being pointed to by hardware.
379 	 */
380 	unbound = bound = unevictable = 0;
381 	list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
382 		if (!can_release_pages(obj))
383 			unevictable += obj->base.size >> PAGE_SHIFT;
384 		else
385 			unbound += obj->base.size >> PAGE_SHIFT;
386 	}
387 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
388 		if (!can_release_pages(obj))
389 			unevictable += obj->base.size >> PAGE_SHIFT;
390 		else
391 			bound += obj->base.size >> PAGE_SHIFT;
392 	}
393 
394 	i915_gem_shrinker_unlock_uninterruptible(dev_priv, &slu);
395 
396 	if (freed_pages || unbound || bound)
397 		pr_info("Purging GPU memory, %lu pages freed, "
398 			"%lu pages still pinned.\n",
399 			freed_pages, unevictable);
400 	if (unbound || bound)
401 		pr_err("%lu and %lu pages still available in the "
402 		       "bound and unbound GPU page lists.\n",
403 		       bound, unbound);
404 
405 	*(unsigned long *)ptr += freed_pages;
406 	return NOTIFY_DONE;
407 }
408 
409 static int
410 i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
411 {
412 	struct drm_i915_private *dev_priv =
413 		container_of(nb, struct drm_i915_private, mm.vmap_notifier);
414 	struct shrinker_lock_uninterruptible slu;
415 	struct i915_vma *vma, *next;
416 	unsigned long freed_pages = 0;
417 	int ret;
418 
419 	if (!i915_gem_shrinker_lock_uninterruptible(dev_priv, &slu, 5000))
420 		return NOTIFY_DONE;
421 
422 	/* Force everything onto the inactive lists */
423 	ret = i915_gem_wait_for_idle(dev_priv);
424 	if (ret)
425 		goto out;
426 
427 	intel_runtime_pm_get(dev_priv);
428 	freed_pages += i915_gem_shrink(dev_priv, -1UL,
429 				       I915_SHRINK_BOUND |
430 				       I915_SHRINK_UNBOUND |
431 				       I915_SHRINK_ACTIVE |
432 				       I915_SHRINK_VMAPS);
433 	intel_runtime_pm_put(dev_priv);
434 
435 	/* We also want to clear any cached iomaps as they wrap vmap */
436 	list_for_each_entry_safe(vma, next,
437 				 &dev_priv->ggtt.base.inactive_list, vm_link) {
438 		unsigned long count = vma->node.size >> PAGE_SHIFT;
439 		if (vma->iomap && i915_vma_unbind(vma) == 0)
440 			freed_pages += count;
441 	}
442 
443 out:
444 	i915_gem_shrinker_unlock_uninterruptible(dev_priv, &slu);
445 
446 	*(unsigned long *)ptr += freed_pages;
447 	return NOTIFY_DONE;
448 }
449 #endif
450 
451 /**
452  * i915_gem_shrinker_init - Initialize i915 shrinker
453  * @dev_priv: i915 device
454  *
455  * This function registers and sets up the i915 shrinker and OOM handler.
456  */
457 void i915_gem_shrinker_init(struct drm_i915_private *dev_priv)
458 {
459 	dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
460 	dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
461 	dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
462 #if 0
463 	WARN_ON(register_shrinker(&dev_priv->mm.shrinker));
464 
465 	dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
466 	WARN_ON(register_oom_notifier(&dev_priv->mm.oom_notifier));
467 
468 	dev_priv->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
469 	WARN_ON(register_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
470 #endif
471 }
472 
473 /**
474  * i915_gem_shrinker_cleanup - Clean up i915 shrinker
475  * @dev_priv: i915 device
476  *
477  * This function unregisters the i915 shrinker and OOM handler.
478  */
479 void i915_gem_shrinker_cleanup(struct drm_i915_private *dev_priv)
480 {
481 #if 0
482 	WARN_ON(unregister_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
483 	WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
484 	unregister_shrinker(&dev_priv->mm.shrinker);
485 #endif
486 }
487