xref: /dflybsd-src/sys/dev/drm/i915/i915_gem_shrinker.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1477eb7f9SFrançois Tigeot /*
2477eb7f9SFrançois Tigeot  * Copyright © 2008-2015 Intel Corporation
3477eb7f9SFrançois Tigeot  *
4477eb7f9SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5477eb7f9SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6477eb7f9SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7477eb7f9SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8477eb7f9SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9477eb7f9SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10477eb7f9SFrançois Tigeot  *
11477eb7f9SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12477eb7f9SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13477eb7f9SFrançois Tigeot  * Software.
14477eb7f9SFrançois Tigeot  *
15477eb7f9SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16477eb7f9SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17477eb7f9SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18477eb7f9SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19477eb7f9SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20477eb7f9SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21477eb7f9SFrançois Tigeot  * IN THE SOFTWARE.
22477eb7f9SFrançois Tigeot  *
23477eb7f9SFrançois Tigeot  */
24477eb7f9SFrançois Tigeot 
254be47400SFrançois Tigeot #include <linux/oom.h>
26477eb7f9SFrançois Tigeot #include <linux/shmem_fs.h>
27bf017597SFrançois Tigeot #include <linux/slab.h>
28477eb7f9SFrançois Tigeot #include <linux/swap.h>
29477eb7f9SFrançois Tigeot #include <linux/pci.h>
30bf017597SFrançois Tigeot #include <linux/dma-buf.h>
318621f407SFrançois Tigeot #include <linux/vmalloc.h>
32477eb7f9SFrançois Tigeot #include <drm/drmP.h>
33477eb7f9SFrançois Tigeot #include <drm/i915_drm.h>
34477eb7f9SFrançois Tigeot 
35477eb7f9SFrançois Tigeot #include "i915_drv.h"
36477eb7f9SFrançois Tigeot #include "i915_trace.h"
37477eb7f9SFrançois Tigeot 
shrinker_lock(struct drm_i915_private * dev_priv,bool * unlock)38*3f2dd94aSFrançois Tigeot static bool shrinker_lock(struct drm_i915_private *dev_priv, bool *unlock)
39477eb7f9SFrançois Tigeot {
40*3f2dd94aSFrançois Tigeot 	switch (mutex_trylock_recursive(&dev_priv->drm.struct_mutex)) {
41*3f2dd94aSFrançois Tigeot 	case MUTEX_TRYLOCK_RECURSIVE:
42*3f2dd94aSFrançois Tigeot 		*unlock = false;
43*3f2dd94aSFrançois Tigeot 		return true;
44*3f2dd94aSFrançois Tigeot 
454be47400SFrançois Tigeot 	case MUTEX_TRYLOCK_FAILED:
46*3f2dd94aSFrançois Tigeot 		*unlock = false;
47*3f2dd94aSFrançois Tigeot 		preempt_disable();
48*3f2dd94aSFrançois Tigeot 		do {
49*3f2dd94aSFrançois Tigeot 			cpu_relax();
50*3f2dd94aSFrançois Tigeot 			if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
51*3f2dd94aSFrançois Tigeot 				*unlock = true;
52*3f2dd94aSFrançois Tigeot 				break;
53*3f2dd94aSFrançois Tigeot 			}
54*3f2dd94aSFrançois Tigeot 		} while (!need_resched());
55*3f2dd94aSFrançois Tigeot 		preempt_enable();
56*3f2dd94aSFrançois Tigeot 		return *unlock;
57477eb7f9SFrançois Tigeot 
584be47400SFrançois Tigeot 	case MUTEX_TRYLOCK_SUCCESS:
594be47400SFrançois Tigeot 		*unlock = true;
604be47400SFrançois Tigeot 		return true;
61477eb7f9SFrançois Tigeot 	}
624be47400SFrançois Tigeot 
634be47400SFrançois Tigeot 	BUG();
644be47400SFrançois Tigeot }
65477eb7f9SFrançois Tigeot 
shrinker_unlock(struct drm_i915_private * dev_priv,bool unlock)66*3f2dd94aSFrançois Tigeot static void shrinker_unlock(struct drm_i915_private *dev_priv, bool unlock)
67a85cb24fSFrançois Tigeot {
68a85cb24fSFrançois Tigeot 	if (!unlock)
69a85cb24fSFrançois Tigeot 		return;
70a85cb24fSFrançois Tigeot 
71*3f2dd94aSFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
72c0e85e96SFrançois Tigeot }
73c0e85e96SFrançois Tigeot 
swap_available(void)74c0e85e96SFrançois Tigeot static bool swap_available(void)
75c0e85e96SFrançois Tigeot {
76c0e85e96SFrançois Tigeot 	return get_nr_swap_pages() > 0;
77c0e85e96SFrançois Tigeot }
78c0e85e96SFrançois Tigeot 
can_release_pages(struct drm_i915_gem_object * obj)79c0e85e96SFrançois Tigeot static bool can_release_pages(struct drm_i915_gem_object *obj)
80c0e85e96SFrançois Tigeot {
814be47400SFrançois Tigeot 	/* Consider only shrinkable ojects. */
824be47400SFrançois Tigeot 	if (!i915_gem_object_is_shrinkable(obj))
838621f407SFrançois Tigeot 		return false;
848621f407SFrançois Tigeot 
85c0e85e96SFrançois Tigeot 	/* Only report true if by unbinding the object and putting its pages
86c0e85e96SFrançois Tigeot 	 * we can actually make forward progress towards freeing physical
87c0e85e96SFrançois Tigeot 	 * pages.
88c0e85e96SFrançois Tigeot 	 *
89c0e85e96SFrançois Tigeot 	 * If the pages are pinned for any other reason than being bound
90c0e85e96SFrançois Tigeot 	 * to the GPU, simply unbinding from the GPU is not going to succeed
91c0e85e96SFrançois Tigeot 	 * in releasing our pin count on the pages themselves.
92c0e85e96SFrançois Tigeot 	 */
934be47400SFrançois Tigeot 	if (atomic_read(&obj->mm.pages_pin_count) > obj->bind_count)
9471f41f3eSFrançois Tigeot 		return false;
9571f41f3eSFrançois Tigeot 
96*3f2dd94aSFrançois Tigeot 	/* If any vma are "permanently" pinned, it will prevent us from
97*3f2dd94aSFrançois Tigeot 	 * reclaiming the obj->mm.pages. We only allow scanout objects to claim
98*3f2dd94aSFrançois Tigeot 	 * a permanent pin, along with a few others like the context objects.
99*3f2dd94aSFrançois Tigeot 	 * To simplify the scan, and to avoid walking the list of vma under the
100*3f2dd94aSFrançois Tigeot 	 * object, we just check the count of its permanently pinned.
101*3f2dd94aSFrançois Tigeot 	 */
102*3f2dd94aSFrançois Tigeot 	if (READ_ONCE(obj->pin_global))
103c0e85e96SFrançois Tigeot 		return false;
104c0e85e96SFrançois Tigeot 
105c0e85e96SFrançois Tigeot 	/* We can only return physical pages to the system if we can either
106c0e85e96SFrançois Tigeot 	 * discard the contents (because the user has marked them as being
107c0e85e96SFrançois Tigeot 	 * purgeable) or if we can move their contents out to swap.
108c0e85e96SFrançois Tigeot 	 */
1094be47400SFrançois Tigeot 	return swap_available() || obj->mm.madv == I915_MADV_DONTNEED;
1104be47400SFrançois Tigeot }
1114be47400SFrançois Tigeot 
unsafe_drop_pages(struct drm_i915_gem_object * obj)1124be47400SFrançois Tigeot static bool unsafe_drop_pages(struct drm_i915_gem_object *obj)
1134be47400SFrançois Tigeot {
1144be47400SFrançois Tigeot 	if (i915_gem_object_unbind(obj) == 0)
1154be47400SFrançois Tigeot 		__i915_gem_object_put_pages(obj, I915_MM_SHRINKER);
116*3f2dd94aSFrançois Tigeot 	return !i915_gem_object_has_pages(obj);
117c0e85e96SFrançois Tigeot }
118c0e85e96SFrançois Tigeot 
119477eb7f9SFrançois Tigeot /**
120477eb7f9SFrançois Tigeot  * i915_gem_shrink - Shrink buffer object caches
121477eb7f9SFrançois Tigeot  * @dev_priv: i915 device
122477eb7f9SFrançois Tigeot  * @target: amount of memory to make available, in pages
123*3f2dd94aSFrançois Tigeot  * @nr_scanned: optional output for number of pages scanned (incremental)
124477eb7f9SFrançois Tigeot  * @flags: control flags for selecting cache types
125477eb7f9SFrançois Tigeot  *
126477eb7f9SFrançois Tigeot  * This function is the main interface to the shrinker. It will try to release
127477eb7f9SFrançois Tigeot  * up to @target pages of main memory backing storage from buffer objects.
128477eb7f9SFrançois Tigeot  * Selection of the specific caches can be done with @flags. This is e.g. useful
129477eb7f9SFrançois Tigeot  * when purgeable objects should be removed from caches preferentially.
130477eb7f9SFrançois Tigeot  *
131477eb7f9SFrançois Tigeot  * Note that it's not guaranteed that released amount is actually available as
132477eb7f9SFrançois Tigeot  * free system memory - the pages might still be in-used to due to other reasons
133477eb7f9SFrançois Tigeot  * (like cpu mmaps) or the mm core has reused them before we could grab them.
134477eb7f9SFrançois Tigeot  * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
135477eb7f9SFrançois Tigeot  * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
136477eb7f9SFrançois Tigeot  *
137477eb7f9SFrançois Tigeot  * Also note that any kind of pinning (both per-vma address space pins and
138477eb7f9SFrançois Tigeot  * backing storage pins at the buffer object level) result in the shrinker code
139477eb7f9SFrançois Tigeot  * having to skip the object.
140477eb7f9SFrançois Tigeot  *
141477eb7f9SFrançois Tigeot  * Returns:
142477eb7f9SFrançois Tigeot  * The number of pages of backing storage actually released.
143477eb7f9SFrançois Tigeot  */
144477eb7f9SFrançois Tigeot unsigned long
i915_gem_shrink(struct drm_i915_private * dev_priv,unsigned long target,unsigned long * nr_scanned,unsigned flags)145477eb7f9SFrançois Tigeot i915_gem_shrink(struct drm_i915_private *dev_priv,
146*3f2dd94aSFrançois Tigeot 		unsigned long target,
147*3f2dd94aSFrançois Tigeot 		unsigned long *nr_scanned,
148*3f2dd94aSFrançois Tigeot 		unsigned flags)
149477eb7f9SFrançois Tigeot {
150477eb7f9SFrançois Tigeot 	const struct {
151477eb7f9SFrançois Tigeot 		struct list_head *list;
152477eb7f9SFrançois Tigeot 		unsigned int bit;
153477eb7f9SFrançois Tigeot 	} phases[] = {
154477eb7f9SFrançois Tigeot 		{ &dev_priv->mm.unbound_list, I915_SHRINK_UNBOUND },
155477eb7f9SFrançois Tigeot 		{ &dev_priv->mm.bound_list, I915_SHRINK_BOUND },
156477eb7f9SFrançois Tigeot 		{ NULL, 0 },
157477eb7f9SFrançois Tigeot 	}, *phase;
158477eb7f9SFrançois Tigeot 	unsigned long count = 0;
159*3f2dd94aSFrançois Tigeot 	unsigned long scanned = 0;
1604be47400SFrançois Tigeot 	bool unlock;
1614be47400SFrançois Tigeot 
162*3f2dd94aSFrançois Tigeot 	if (!shrinker_lock(dev_priv, &unlock))
1634be47400SFrançois Tigeot 		return 0;
164477eb7f9SFrançois Tigeot 
165*3f2dd94aSFrançois Tigeot 	/*
166*3f2dd94aSFrançois Tigeot 	 * When shrinking the active list, also consider active contexts.
167*3f2dd94aSFrançois Tigeot 	 * Active contexts are pinned until they are retired, and so can
168*3f2dd94aSFrançois Tigeot 	 * not be simply unbound to retire and unpin their pages. To shrink
169*3f2dd94aSFrançois Tigeot 	 * the contexts, we must wait until the gpu is idle.
170*3f2dd94aSFrançois Tigeot 	 *
171*3f2dd94aSFrançois Tigeot 	 * We don't care about errors here; if we cannot wait upon the GPU,
172*3f2dd94aSFrançois Tigeot 	 * we will free as much as we can and hope to get a second chance.
173*3f2dd94aSFrançois Tigeot 	 */
174*3f2dd94aSFrançois Tigeot 	if (flags & I915_SHRINK_ACTIVE)
175*3f2dd94aSFrançois Tigeot 		i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
176*3f2dd94aSFrançois Tigeot 
177352ff8bdSFrançois Tigeot 	trace_i915_gem_shrink(dev_priv, target, flags);
1781487f786SFrançois Tigeot 	i915_gem_retire_requests(dev_priv);
1791487f786SFrançois Tigeot 
1801487f786SFrançois Tigeot 	/*
1811487f786SFrançois Tigeot 	 * Unbinding of objects will require HW access; Let us not wake the
1821487f786SFrançois Tigeot 	 * device just to recover a little memory. If absolutely necessary,
1831487f786SFrançois Tigeot 	 * we will force the wake during oom-notifier.
1841487f786SFrançois Tigeot 	 */
1851487f786SFrançois Tigeot 	if ((flags & I915_SHRINK_BOUND) &&
1861487f786SFrançois Tigeot 	    !intel_runtime_pm_get_if_in_use(dev_priv))
1871487f786SFrançois Tigeot 		flags &= ~I915_SHRINK_BOUND;
188352ff8bdSFrançois Tigeot 
189477eb7f9SFrançois Tigeot 	/*
190477eb7f9SFrançois Tigeot 	 * As we may completely rewrite the (un)bound list whilst unbinding
191477eb7f9SFrançois Tigeot 	 * (due to retiring requests) we have to strictly process only
192477eb7f9SFrançois Tigeot 	 * one element of the list at the time, and recheck the list
193477eb7f9SFrançois Tigeot 	 * on every iteration.
194477eb7f9SFrançois Tigeot 	 *
195477eb7f9SFrançois Tigeot 	 * In particular, we must hold a reference whilst removing the
196477eb7f9SFrançois Tigeot 	 * object as we may end up waiting for and/or retiring the objects.
197477eb7f9SFrançois Tigeot 	 * This might release the final reference (held by the active list)
198477eb7f9SFrançois Tigeot 	 * and result in the object being freed from under us. This is
199477eb7f9SFrançois Tigeot 	 * similar to the precautions the eviction code must take whilst
200477eb7f9SFrançois Tigeot 	 * removing objects.
201477eb7f9SFrançois Tigeot 	 *
202477eb7f9SFrançois Tigeot 	 * Also note that although these lists do not hold a reference to
203477eb7f9SFrançois Tigeot 	 * the object we can safely grab one here: The final object
204477eb7f9SFrançois Tigeot 	 * unreferencing and the bound_list are both protected by the
205477eb7f9SFrançois Tigeot 	 * dev->struct_mutex and so we won't ever be able to observe an
206477eb7f9SFrançois Tigeot 	 * object on the bound_list with a reference count equals 0.
207477eb7f9SFrançois Tigeot 	 */
208477eb7f9SFrançois Tigeot 	for (phase = phases; phase->list; phase++) {
209477eb7f9SFrançois Tigeot 		struct list_head still_in_list;
21071f41f3eSFrançois Tigeot 		struct drm_i915_gem_object *obj;
211477eb7f9SFrançois Tigeot 
212477eb7f9SFrançois Tigeot 		if ((flags & phase->bit) == 0)
213477eb7f9SFrançois Tigeot 			continue;
214477eb7f9SFrançois Tigeot 
215477eb7f9SFrançois Tigeot 		INIT_LIST_HEAD(&still_in_list);
216*3f2dd94aSFrançois Tigeot 
217*3f2dd94aSFrançois Tigeot 		/*
218*3f2dd94aSFrançois Tigeot 		 * We serialize our access to unreferenced objects through
219*3f2dd94aSFrançois Tigeot 		 * the use of the struct_mutex. While the objects are not
220*3f2dd94aSFrançois Tigeot 		 * yet freed (due to RCU then a workqueue) we still want
221*3f2dd94aSFrançois Tigeot 		 * to be able to shrink their pages, so they remain on
222*3f2dd94aSFrançois Tigeot 		 * the unbound/bound list until actually freed.
223*3f2dd94aSFrançois Tigeot 		 */
224*3f2dd94aSFrançois Tigeot 		lockmgr(&dev_priv->mm.obj_lock, LK_EXCLUSIVE);
22571f41f3eSFrançois Tigeot 		while (count < target &&
22671f41f3eSFrançois Tigeot 		       (obj = list_first_entry_or_null(phase->list,
22771f41f3eSFrançois Tigeot 						       typeof(*obj),
228*3f2dd94aSFrançois Tigeot 						       mm.link))) {
229*3f2dd94aSFrançois Tigeot 			list_move_tail(&obj->mm.link, &still_in_list);
230477eb7f9SFrançois Tigeot 
231477eb7f9SFrançois Tigeot 			if (flags & I915_SHRINK_PURGEABLE &&
2324be47400SFrançois Tigeot 			    obj->mm.madv != I915_MADV_DONTNEED)
233477eb7f9SFrançois Tigeot 				continue;
234477eb7f9SFrançois Tigeot 
2358621f407SFrançois Tigeot 			if (flags & I915_SHRINK_VMAPS &&
2364be47400SFrançois Tigeot 			    !is_vmalloc_addr(obj->mm.mapping))
2378621f407SFrançois Tigeot 				continue;
2388621f407SFrançois Tigeot 
2391e12ee3bSFrançois Tigeot 			if (!(flags & I915_SHRINK_ACTIVE) &&
2401e12ee3bSFrançois Tigeot 			    (i915_gem_object_is_active(obj) ||
241a85cb24fSFrançois Tigeot 			     i915_gem_object_is_framebuffer(obj)))
242352ff8bdSFrançois Tigeot 				continue;
243352ff8bdSFrançois Tigeot 
244c0e85e96SFrançois Tigeot 			if (!can_release_pages(obj))
245c0e85e96SFrançois Tigeot 				continue;
246c0e85e96SFrançois Tigeot 
247*3f2dd94aSFrançois Tigeot 			lockmgr(&dev_priv->mm.obj_lock, LK_RELEASE);
248*3f2dd94aSFrançois Tigeot 
2494be47400SFrançois Tigeot 			if (unsafe_drop_pages(obj)) {
2504be47400SFrançois Tigeot 				/* May arrive from get_pages on another bo */
2514be47400SFrançois Tigeot 				mutex_lock_nested(&obj->mm.lock,
2524be47400SFrançois Tigeot 						  I915_MM_SHRINKER);
253*3f2dd94aSFrançois Tigeot 				if (!i915_gem_object_has_pages(obj)) {
2544be47400SFrançois Tigeot 					__i915_gem_object_invalidate(obj);
255477eb7f9SFrançois Tigeot 					count += obj->base.size >> PAGE_SHIFT;
256477eb7f9SFrançois Tigeot 				}
2574be47400SFrançois Tigeot 				mutex_unlock(&obj->mm.lock);
2584be47400SFrançois Tigeot 			}
259*3f2dd94aSFrançois Tigeot 			scanned += obj->base.size >> PAGE_SHIFT;
260*3f2dd94aSFrançois Tigeot 
261*3f2dd94aSFrançois Tigeot 			lockmgr(&dev_priv->mm.obj_lock, LK_EXCLUSIVE);
2624be47400SFrançois Tigeot 		}
2634be47400SFrançois Tigeot 		list_splice_tail(&still_in_list, phase->list);
264*3f2dd94aSFrançois Tigeot 		lockmgr(&dev_priv->mm.obj_lock, LK_RELEASE);
265477eb7f9SFrançois Tigeot 	}
266477eb7f9SFrançois Tigeot 
2671487f786SFrançois Tigeot 	if (flags & I915_SHRINK_BOUND)
2681487f786SFrançois Tigeot 		intel_runtime_pm_put(dev_priv);
2691487f786SFrançois Tigeot 
2701487f786SFrançois Tigeot 	i915_gem_retire_requests(dev_priv);
2714be47400SFrançois Tigeot 
272*3f2dd94aSFrançois Tigeot 	shrinker_unlock(dev_priv, unlock);
273352ff8bdSFrançois Tigeot 
274*3f2dd94aSFrançois Tigeot 	if (nr_scanned)
275*3f2dd94aSFrançois Tigeot 		*nr_scanned += scanned;
276477eb7f9SFrançois Tigeot 	return count;
277477eb7f9SFrançois Tigeot }
278477eb7f9SFrançois Tigeot 
279477eb7f9SFrançois Tigeot /**
280a05eeebfSFrançois Tigeot  * i915_gem_shrink_all - Shrink buffer object caches completely
281477eb7f9SFrançois Tigeot  * @dev_priv: i915 device
282477eb7f9SFrançois Tigeot  *
283477eb7f9SFrançois Tigeot  * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
284477eb7f9SFrançois Tigeot  * caches completely. It also first waits for and retires all outstanding
285477eb7f9SFrançois Tigeot  * requests to also be able to release backing storage for active objects.
286477eb7f9SFrançois Tigeot  *
287477eb7f9SFrançois Tigeot  * This should only be used in code to intentionally quiescent the gpu or as a
288477eb7f9SFrançois Tigeot  * last-ditch effort when memory seems to have run out.
289477eb7f9SFrançois Tigeot  *
290477eb7f9SFrançois Tigeot  * Returns:
291477eb7f9SFrançois Tigeot  * The number of pages of backing storage actually released.
292477eb7f9SFrançois Tigeot  */
i915_gem_shrink_all(struct drm_i915_private * dev_priv)293477eb7f9SFrançois Tigeot unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
294477eb7f9SFrançois Tigeot {
29571f41f3eSFrançois Tigeot 	unsigned long freed;
29671f41f3eSFrançois Tigeot 
297a85cb24fSFrançois Tigeot 	intel_runtime_pm_get(dev_priv);
298*3f2dd94aSFrançois Tigeot 	freed = i915_gem_shrink(dev_priv, -1UL, NULL,
299352ff8bdSFrançois Tigeot 				I915_SHRINK_BOUND |
300352ff8bdSFrançois Tigeot 				I915_SHRINK_UNBOUND |
301352ff8bdSFrançois Tigeot 				I915_SHRINK_ACTIVE);
302a85cb24fSFrançois Tigeot 	intel_runtime_pm_put(dev_priv);
30371f41f3eSFrançois Tigeot 
30471f41f3eSFrançois Tigeot 	return freed;
305477eb7f9SFrançois Tigeot }
306477eb7f9SFrançois Tigeot 
307477eb7f9SFrançois Tigeot static unsigned long
i915_gem_shrinker_count(struct shrinker * shrinker,struct shrink_control * sc)308477eb7f9SFrançois Tigeot i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
309477eb7f9SFrançois Tigeot {
31080fbca37SFrançois Tigeot #if 0
311477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv =
312477eb7f9SFrançois Tigeot 		container_of(shrinker, struct drm_i915_private, mm.shrinker);
313477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj;
314*3f2dd94aSFrançois Tigeot 	unsigned long count = 0;
315477eb7f9SFrançois Tigeot 
316*3f2dd94aSFrançois Tigeot 	spin_lock(&dev_priv->mm.obj_lock);
317*3f2dd94aSFrançois Tigeot 	list_for_each_entry(obj, &dev_priv->mm.unbound_list, mm.link)
3188621f407SFrançois Tigeot 		if (can_release_pages(obj))
319477eb7f9SFrançois Tigeot 			count += obj->base.size >> PAGE_SHIFT;
320477eb7f9SFrançois Tigeot 
321*3f2dd94aSFrançois Tigeot 	list_for_each_entry(obj, &dev_priv->mm.bound_list, mm.link)
32271f41f3eSFrançois Tigeot 		if (!i915_gem_object_is_active(obj) && can_release_pages(obj))
323477eb7f9SFrançois Tigeot 			count += obj->base.size >> PAGE_SHIFT;
324*3f2dd94aSFrançois Tigeot 	spin_unlock(&dev_priv->mm.obj_lock);
325477eb7f9SFrançois Tigeot 
326477eb7f9SFrançois Tigeot 	return count;
32780fbca37SFrançois Tigeot #endif
32880fbca37SFrançois Tigeot 	return 0;
329477eb7f9SFrançois Tigeot }
330477eb7f9SFrançois Tigeot 
331477eb7f9SFrançois Tigeot static unsigned long
i915_gem_shrinker_scan(struct shrinker * shrinker,struct shrink_control * sc)332477eb7f9SFrançois Tigeot i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
333477eb7f9SFrançois Tigeot {
33480fbca37SFrançois Tigeot #if 0
335477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv =
336477eb7f9SFrançois Tigeot 		container_of(shrinker, struct drm_i915_private, mm.shrinker);
337477eb7f9SFrançois Tigeot 	unsigned long freed;
338477eb7f9SFrançois Tigeot 	bool unlock;
339477eb7f9SFrançois Tigeot 
340*3f2dd94aSFrançois Tigeot 	sc->nr_scanned = 0;
341*3f2dd94aSFrançois Tigeot 
342*3f2dd94aSFrançois Tigeot 	if (!shrinker_lock(dev_priv, &unlock))
343477eb7f9SFrançois Tigeot 		return SHRINK_STOP;
344477eb7f9SFrançois Tigeot 
345477eb7f9SFrançois Tigeot 	freed = i915_gem_shrink(dev_priv,
346477eb7f9SFrançois Tigeot 				sc->nr_to_scan,
347*3f2dd94aSFrançois Tigeot 				&sc->nr_scanned,
348477eb7f9SFrançois Tigeot 				I915_SHRINK_BOUND |
349477eb7f9SFrançois Tigeot 				I915_SHRINK_UNBOUND |
350477eb7f9SFrançois Tigeot 				I915_SHRINK_PURGEABLE);
351477eb7f9SFrançois Tigeot 	if (freed < sc->nr_to_scan)
352477eb7f9SFrançois Tigeot 		freed += i915_gem_shrink(dev_priv,
353*3f2dd94aSFrançois Tigeot 					 sc->nr_to_scan - sc->nr_scanned,
354*3f2dd94aSFrançois Tigeot 					 &sc->nr_scanned,
355477eb7f9SFrançois Tigeot 					 I915_SHRINK_BOUND |
356477eb7f9SFrançois Tigeot 					 I915_SHRINK_UNBOUND);
357*3f2dd94aSFrançois Tigeot 	if (freed < sc->nr_to_scan && current_is_kswapd()) {
358*3f2dd94aSFrançois Tigeot 		intel_runtime_pm_get(dev_priv);
359*3f2dd94aSFrançois Tigeot 		freed += i915_gem_shrink(dev_priv,
360*3f2dd94aSFrançois Tigeot 					 sc->nr_to_scan - sc->nr_scanned,
361*3f2dd94aSFrançois Tigeot 					 &sc->nr_scanned,
362*3f2dd94aSFrançois Tigeot 					 I915_SHRINK_ACTIVE |
363*3f2dd94aSFrançois Tigeot 					 I915_SHRINK_BOUND |
364*3f2dd94aSFrançois Tigeot 					 I915_SHRINK_UNBOUND);
365*3f2dd94aSFrançois Tigeot 		intel_runtime_pm_put(dev_priv);
366*3f2dd94aSFrançois Tigeot 	}
367a85cb24fSFrançois Tigeot 
368*3f2dd94aSFrançois Tigeot 	shrinker_unlock(dev_priv, unlock);
369477eb7f9SFrançois Tigeot 
370477eb7f9SFrançois Tigeot 	return freed;
37180fbca37SFrançois Tigeot #endif
37280fbca37SFrançois Tigeot 	return 0;
373477eb7f9SFrançois Tigeot }
374477eb7f9SFrançois Tigeot 
37580fbca37SFrançois Tigeot #if 0
3768621f407SFrançois Tigeot static bool
377*3f2dd94aSFrançois Tigeot shrinker_lock_uninterruptible(struct drm_i915_private *dev_priv, bool *unlock,
3788621f407SFrançois Tigeot 			      int timeout_ms)
3798621f407SFrançois Tigeot {
38071f41f3eSFrançois Tigeot 	unsigned long timeout = jiffies + msecs_to_jiffies_timeout(timeout_ms);
3818621f407SFrançois Tigeot 
38271f41f3eSFrançois Tigeot 	do {
3831e12ee3bSFrançois Tigeot 		if (i915_gem_wait_for_idle(dev_priv, 0) == 0 &&
384*3f2dd94aSFrançois Tigeot 		    shrinker_lock(dev_priv, unlock))
38571f41f3eSFrançois Tigeot 			break;
38671f41f3eSFrançois Tigeot 
3878621f407SFrançois Tigeot 		schedule_timeout_killable(1);
3888621f407SFrançois Tigeot 		if (fatal_signal_pending(current))
3898621f407SFrançois Tigeot 			return false;
39071f41f3eSFrançois Tigeot 
39171f41f3eSFrançois Tigeot 		if (time_after(jiffies, timeout)) {
3928621f407SFrançois Tigeot 			pr_err("Unable to lock GPU to purge memory.\n");
3938621f407SFrançois Tigeot 			return false;
3948621f407SFrançois Tigeot 		}
39571f41f3eSFrançois Tigeot 	} while (1);
3968621f407SFrançois Tigeot 
3978621f407SFrançois Tigeot 	return true;
3988621f407SFrançois Tigeot }
3998621f407SFrançois Tigeot 
400477eb7f9SFrançois Tigeot static int
401477eb7f9SFrançois Tigeot i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
402477eb7f9SFrançois Tigeot {
403477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv =
404477eb7f9SFrançois Tigeot 		container_of(nb, struct drm_i915_private, mm.oom_notifier);
405477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj;
4068621f407SFrançois Tigeot 	unsigned long unevictable, bound, unbound, freed_pages;
407477eb7f9SFrançois Tigeot 
408477eb7f9SFrançois Tigeot 	freed_pages = i915_gem_shrink_all(dev_priv);
409477eb7f9SFrançois Tigeot 
410477eb7f9SFrançois Tigeot 	/* Because we may be allocating inside our own driver, we cannot
411477eb7f9SFrançois Tigeot 	 * assert that there are no objects with pinned pages that are not
412477eb7f9SFrançois Tigeot 	 * being pointed to by hardware.
413477eb7f9SFrançois Tigeot 	 */
4148621f407SFrançois Tigeot 	unbound = bound = unevictable = 0;
415*3f2dd94aSFrançois Tigeot 	spin_lock(&dev_priv->mm.obj_lock);
416*3f2dd94aSFrançois Tigeot 	list_for_each_entry(obj, &dev_priv->mm.unbound_list, mm.link) {
4178621f407SFrançois Tigeot 		if (!can_release_pages(obj))
4188621f407SFrançois Tigeot 			unevictable += obj->base.size >> PAGE_SHIFT;
419477eb7f9SFrançois Tigeot 		else
4208621f407SFrançois Tigeot 			unbound += obj->base.size >> PAGE_SHIFT;
421477eb7f9SFrançois Tigeot 	}
422*3f2dd94aSFrançois Tigeot 	list_for_each_entry(obj, &dev_priv->mm.bound_list, mm.link) {
4238621f407SFrançois Tigeot 		if (!can_release_pages(obj))
4248621f407SFrançois Tigeot 			unevictable += obj->base.size >> PAGE_SHIFT;
425477eb7f9SFrançois Tigeot 		else
4268621f407SFrançois Tigeot 			bound += obj->base.size >> PAGE_SHIFT;
427477eb7f9SFrançois Tigeot 	}
428*3f2dd94aSFrançois Tigeot 	spin_unlock(&dev_priv->mm.obj_lock);
429477eb7f9SFrançois Tigeot 
430477eb7f9SFrançois Tigeot 	if (freed_pages || unbound || bound)
4318621f407SFrançois Tigeot 		pr_info("Purging GPU memory, %lu pages freed, "
4328621f407SFrançois Tigeot 			"%lu pages still pinned.\n",
4338621f407SFrançois Tigeot 			freed_pages, unevictable);
434477eb7f9SFrançois Tigeot 	if (unbound || bound)
4358621f407SFrançois Tigeot 		pr_err("%lu and %lu pages still available in the "
436477eb7f9SFrançois Tigeot 		       "bound and unbound GPU page lists.\n",
437477eb7f9SFrançois Tigeot 		       bound, unbound);
438477eb7f9SFrançois Tigeot 
439477eb7f9SFrançois Tigeot 	*(unsigned long *)ptr += freed_pages;
440477eb7f9SFrançois Tigeot 	return NOTIFY_DONE;
441477eb7f9SFrançois Tigeot }
4428621f407SFrançois Tigeot 
4438621f407SFrançois Tigeot static int
4448621f407SFrançois Tigeot i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
4458621f407SFrançois Tigeot {
4468621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv =
4478621f407SFrançois Tigeot 		container_of(nb, struct drm_i915_private, mm.vmap_notifier);
4481487f786SFrançois Tigeot 	struct i915_vma *vma, *next;
4491487f786SFrançois Tigeot 	unsigned long freed_pages = 0;
450*3f2dd94aSFrançois Tigeot 	bool unlock;
4511487f786SFrançois Tigeot 	int ret;
4528621f407SFrançois Tigeot 
453*3f2dd94aSFrançois Tigeot 	if (!shrinker_lock_uninterruptible(dev_priv, &unlock, 5000))
4548621f407SFrançois Tigeot 		return NOTIFY_DONE;
4558621f407SFrançois Tigeot 
4561487f786SFrançois Tigeot 	/* Force everything onto the inactive lists */
4571e12ee3bSFrançois Tigeot 	ret = i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
4581487f786SFrançois Tigeot 	if (ret)
4591487f786SFrançois Tigeot 		goto out;
4601487f786SFrançois Tigeot 
4611487f786SFrançois Tigeot 	intel_runtime_pm_get(dev_priv);
462*3f2dd94aSFrançois Tigeot 	freed_pages += i915_gem_shrink(dev_priv, -1UL, NULL,
4638621f407SFrançois Tigeot 				       I915_SHRINK_BOUND |
4648621f407SFrançois Tigeot 				       I915_SHRINK_UNBOUND |
4658621f407SFrançois Tigeot 				       I915_SHRINK_ACTIVE |
4668621f407SFrançois Tigeot 				       I915_SHRINK_VMAPS);
4671487f786SFrançois Tigeot 	intel_runtime_pm_put(dev_priv);
4688621f407SFrançois Tigeot 
4691487f786SFrançois Tigeot 	/* We also want to clear any cached iomaps as they wrap vmap */
4701487f786SFrançois Tigeot 	list_for_each_entry_safe(vma, next,
4711487f786SFrançois Tigeot 				 &dev_priv->ggtt.base.inactive_list, vm_link) {
4721487f786SFrançois Tigeot 		unsigned long count = vma->node.size >> PAGE_SHIFT;
4731487f786SFrançois Tigeot 		if (vma->iomap && i915_vma_unbind(vma) == 0)
4741487f786SFrançois Tigeot 			freed_pages += count;
4751487f786SFrançois Tigeot 	}
4761487f786SFrançois Tigeot 
4771487f786SFrançois Tigeot out:
478*3f2dd94aSFrançois Tigeot 	shrinker_unlock(dev_priv, unlock);
4798621f407SFrançois Tigeot 
4808621f407SFrançois Tigeot 	*(unsigned long *)ptr += freed_pages;
4818621f407SFrançois Tigeot 	return NOTIFY_DONE;
4828621f407SFrançois Tigeot }
483477eb7f9SFrançois Tigeot #endif
484477eb7f9SFrançois Tigeot 
485477eb7f9SFrançois Tigeot /**
486477eb7f9SFrançois Tigeot  * i915_gem_shrinker_init - Initialize i915 shrinker
487477eb7f9SFrançois Tigeot  * @dev_priv: i915 device
488477eb7f9SFrançois Tigeot  *
489477eb7f9SFrançois Tigeot  * This function registers and sets up the i915 shrinker and OOM handler.
490477eb7f9SFrançois Tigeot  */
i915_gem_shrinker_init(struct drm_i915_private * dev_priv)491477eb7f9SFrançois Tigeot void i915_gem_shrinker_init(struct drm_i915_private *dev_priv)
492477eb7f9SFrançois Tigeot {
493477eb7f9SFrançois Tigeot 	dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
494477eb7f9SFrançois Tigeot 	dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
495477eb7f9SFrançois Tigeot 	dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
49680fbca37SFrançois Tigeot #if 0
497c0e85e96SFrançois Tigeot 	WARN_ON(register_shrinker(&dev_priv->mm.shrinker));
498477eb7f9SFrançois Tigeot 
499477eb7f9SFrançois Tigeot 	dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
500c0e85e96SFrançois Tigeot 	WARN_ON(register_oom_notifier(&dev_priv->mm.oom_notifier));
5018621f407SFrançois Tigeot 
5028621f407SFrançois Tigeot 	dev_priv->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
5038621f407SFrançois Tigeot 	WARN_ON(register_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
504c0e85e96SFrançois Tigeot #endif
505c0e85e96SFrançois Tigeot }
506c0e85e96SFrançois Tigeot 
507c0e85e96SFrançois Tigeot /**
508c0e85e96SFrançois Tigeot  * i915_gem_shrinker_cleanup - Clean up i915 shrinker
509c0e85e96SFrançois Tigeot  * @dev_priv: i915 device
510c0e85e96SFrançois Tigeot  *
511c0e85e96SFrançois Tigeot  * This function unregisters the i915 shrinker and OOM handler.
512c0e85e96SFrançois Tigeot  */
i915_gem_shrinker_cleanup(struct drm_i915_private * dev_priv)513c0e85e96SFrançois Tigeot void i915_gem_shrinker_cleanup(struct drm_i915_private *dev_priv)
514c0e85e96SFrançois Tigeot {
515c0e85e96SFrançois Tigeot #if 0
5168621f407SFrançois Tigeot 	WARN_ON(unregister_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
517c0e85e96SFrançois Tigeot 	WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
518c0e85e96SFrançois Tigeot 	unregister_shrinker(&dev_priv->mm.shrinker);
519477eb7f9SFrançois Tigeot #endif
520477eb7f9SFrançois Tigeot }
521