]>
Commit | Line | Data |
---|---|---|
be6a0376 DV |
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/oom.h> | |
26 | #include <linux/shmem_fs.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/swap.h> | |
29 | #include <linux/pci.h> | |
30 | #include <linux/dma-buf.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 | static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task) | |
38 | { | |
39 | if (!mutex_is_locked(mutex)) | |
40 | return false; | |
41 | ||
42 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES) | |
43 | return mutex->owner == task; | |
44 | #else | |
45 | /* Since UP may be pre-empted, we cannot assume that we own the lock */ | |
46 | return false; | |
47 | #endif | |
48 | } | |
49 | ||
eb0b44ad DV |
50 | /** |
51 | * i915_gem_shrink - Shrink buffer object caches | |
52 | * @dev_priv: i915 device | |
53 | * @target: amount of memory to make available, in pages | |
54 | * @flags: control flags for selecting cache types | |
55 | * | |
56 | * This function is the main interface to the shrinker. It will try to release | |
57 | * up to @target pages of main memory backing storage from buffer objects. | |
58 | * Selection of the specific caches can be done with @flags. This is e.g. useful | |
59 | * when purgeable objects should be removed from caches preferentially. | |
60 | * | |
61 | * Note that it's not guaranteed that released amount is actually available as | |
62 | * free system memory - the pages might still be in-used to due to other reasons | |
63 | * (like cpu mmaps) or the mm core has reused them before we could grab them. | |
64 | * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to | |
65 | * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all(). | |
66 | * | |
67 | * Also note that any kind of pinning (both per-vma address space pins and | |
68 | * backing storage pins at the buffer object level) result in the shrinker code | |
69 | * having to skip the object. | |
70 | * | |
71 | * Returns: | |
72 | * The number of pages of backing storage actually released. | |
73 | */ | |
be6a0376 DV |
74 | unsigned long |
75 | i915_gem_shrink(struct drm_i915_private *dev_priv, | |
14387540 | 76 | unsigned long target, unsigned flags) |
be6a0376 DV |
77 | { |
78 | const struct { | |
79 | struct list_head *list; | |
80 | unsigned int bit; | |
81 | } phases[] = { | |
82 | { &dev_priv->mm.unbound_list, I915_SHRINK_UNBOUND }, | |
83 | { &dev_priv->mm.bound_list, I915_SHRINK_BOUND }, | |
84 | { NULL, 0 }, | |
85 | }, *phase; | |
86 | unsigned long count = 0; | |
87 | ||
3abafa53 CW |
88 | trace_i915_gem_shrink(dev_priv, target, flags); |
89 | ||
be6a0376 DV |
90 | /* |
91 | * As we may completely rewrite the (un)bound list whilst unbinding | |
92 | * (due to retiring requests) we have to strictly process only | |
93 | * one element of the list at the time, and recheck the list | |
94 | * on every iteration. | |
95 | * | |
96 | * In particular, we must hold a reference whilst removing the | |
97 | * object as we may end up waiting for and/or retiring the objects. | |
98 | * This might release the final reference (held by the active list) | |
99 | * and result in the object being freed from under us. This is | |
100 | * similar to the precautions the eviction code must take whilst | |
101 | * removing objects. | |
102 | * | |
103 | * Also note that although these lists do not hold a reference to | |
104 | * the object we can safely grab one here: The final object | |
105 | * unreferencing and the bound_list are both protected by the | |
106 | * dev->struct_mutex and so we won't ever be able to observe an | |
107 | * object on the bound_list with a reference count equals 0. | |
108 | */ | |
109 | for (phase = phases; phase->list; phase++) { | |
110 | struct list_head still_in_list; | |
111 | ||
112 | if ((flags & phase->bit) == 0) | |
113 | continue; | |
114 | ||
115 | INIT_LIST_HEAD(&still_in_list); | |
116 | while (count < target && !list_empty(phase->list)) { | |
117 | struct drm_i915_gem_object *obj; | |
118 | struct i915_vma *vma, *v; | |
119 | ||
120 | obj = list_first_entry(phase->list, | |
121 | typeof(*obj), global_list); | |
122 | list_move_tail(&obj->global_list, &still_in_list); | |
123 | ||
124 | if (flags & I915_SHRINK_PURGEABLE && | |
125 | obj->madv != I915_MADV_DONTNEED) | |
126 | continue; | |
127 | ||
128 | drm_gem_object_reference(&obj->base); | |
129 | ||
130 | /* For the unbound phase, this should be a no-op! */ | |
131 | list_for_each_entry_safe(vma, v, | |
132 | &obj->vma_list, vma_link) | |
133 | if (i915_vma_unbind(vma)) | |
134 | break; | |
135 | ||
136 | if (i915_gem_object_put_pages(obj) == 0) | |
137 | count += obj->base.size >> PAGE_SHIFT; | |
138 | ||
139 | drm_gem_object_unreference(&obj->base); | |
140 | } | |
141 | list_splice(&still_in_list, phase->list); | |
142 | } | |
143 | ||
144 | return count; | |
145 | } | |
146 | ||
eb0b44ad | 147 | /** |
1f2449cd | 148 | * i915_gem_shrink_all - Shrink buffer object caches completely |
eb0b44ad DV |
149 | * @dev_priv: i915 device |
150 | * | |
151 | * This is a simple wraper around i915_gem_shrink() to aggressively shrink all | |
152 | * caches completely. It also first waits for and retires all outstanding | |
153 | * requests to also be able to release backing storage for active objects. | |
154 | * | |
155 | * This should only be used in code to intentionally quiescent the gpu or as a | |
156 | * last-ditch effort when memory seems to have run out. | |
157 | * | |
158 | * Returns: | |
159 | * The number of pages of backing storage actually released. | |
160 | */ | |
be6a0376 DV |
161 | unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv) |
162 | { | |
163 | i915_gem_evict_everything(dev_priv->dev); | |
14387540 | 164 | return i915_gem_shrink(dev_priv, -1UL, |
be6a0376 DV |
165 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND); |
166 | } | |
167 | ||
168 | static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock) | |
169 | { | |
170 | if (!mutex_trylock(&dev->struct_mutex)) { | |
171 | if (!mutex_is_locked_by(&dev->struct_mutex, current)) | |
172 | return false; | |
173 | ||
174 | if (to_i915(dev)->mm.shrinker_no_lock_stealing) | |
175 | return false; | |
176 | ||
177 | *unlock = false; | |
178 | } else | |
179 | *unlock = true; | |
180 | ||
181 | return true; | |
182 | } | |
183 | ||
184 | static int num_vma_bound(struct drm_i915_gem_object *obj) | |
185 | { | |
186 | struct i915_vma *vma; | |
187 | int count = 0; | |
188 | ||
f6234c1d | 189 | list_for_each_entry(vma, &obj->vma_list, vma_link) { |
be6a0376 DV |
190 | if (drm_mm_node_allocated(&vma->node)) |
191 | count++; | |
f6234c1d CW |
192 | if (vma->pin_count) |
193 | count++; | |
194 | } | |
be6a0376 DV |
195 | |
196 | return count; | |
197 | } | |
198 | ||
199 | static unsigned long | |
200 | i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc) | |
201 | { | |
202 | struct drm_i915_private *dev_priv = | |
203 | container_of(shrinker, struct drm_i915_private, mm.shrinker); | |
204 | struct drm_device *dev = dev_priv->dev; | |
205 | struct drm_i915_gem_object *obj; | |
206 | unsigned long count; | |
207 | bool unlock; | |
208 | ||
209 | if (!i915_gem_shrinker_lock(dev, &unlock)) | |
210 | return 0; | |
211 | ||
212 | count = 0; | |
213 | list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) | |
214 | if (obj->pages_pin_count == 0) | |
215 | count += obj->base.size >> PAGE_SHIFT; | |
216 | ||
217 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { | |
f6234c1d | 218 | if (obj->pages_pin_count == num_vma_bound(obj)) |
be6a0376 DV |
219 | count += obj->base.size >> PAGE_SHIFT; |
220 | } | |
221 | ||
222 | if (unlock) | |
223 | mutex_unlock(&dev->struct_mutex); | |
224 | ||
225 | return count; | |
226 | } | |
227 | ||
228 | static unsigned long | |
229 | i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc) | |
230 | { | |
231 | struct drm_i915_private *dev_priv = | |
232 | container_of(shrinker, struct drm_i915_private, mm.shrinker); | |
233 | struct drm_device *dev = dev_priv->dev; | |
234 | unsigned long freed; | |
235 | bool unlock; | |
236 | ||
237 | if (!i915_gem_shrinker_lock(dev, &unlock)) | |
238 | return SHRINK_STOP; | |
239 | ||
240 | freed = i915_gem_shrink(dev_priv, | |
241 | sc->nr_to_scan, | |
242 | I915_SHRINK_BOUND | | |
243 | I915_SHRINK_UNBOUND | | |
244 | I915_SHRINK_PURGEABLE); | |
245 | if (freed < sc->nr_to_scan) | |
246 | freed += i915_gem_shrink(dev_priv, | |
247 | sc->nr_to_scan - freed, | |
248 | I915_SHRINK_BOUND | | |
249 | I915_SHRINK_UNBOUND); | |
250 | if (unlock) | |
251 | mutex_unlock(&dev->struct_mutex); | |
252 | ||
253 | return freed; | |
254 | } | |
255 | ||
256 | static int | |
257 | i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr) | |
258 | { | |
259 | struct drm_i915_private *dev_priv = | |
260 | container_of(nb, struct drm_i915_private, mm.oom_notifier); | |
261 | struct drm_device *dev = dev_priv->dev; | |
262 | struct drm_i915_gem_object *obj; | |
263 | unsigned long timeout = msecs_to_jiffies(5000) + 1; | |
264 | unsigned long pinned, bound, unbound, freed_pages; | |
265 | bool was_interruptible; | |
266 | bool unlock; | |
267 | ||
268 | while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout) { | |
269 | schedule_timeout_killable(1); | |
270 | if (fatal_signal_pending(current)) | |
271 | return NOTIFY_DONE; | |
272 | } | |
273 | if (timeout == 0) { | |
274 | pr_err("Unable to purge GPU memory due lock contention.\n"); | |
275 | return NOTIFY_DONE; | |
276 | } | |
277 | ||
278 | was_interruptible = dev_priv->mm.interruptible; | |
279 | dev_priv->mm.interruptible = false; | |
280 | ||
281 | freed_pages = i915_gem_shrink_all(dev_priv); | |
282 | ||
283 | dev_priv->mm.interruptible = was_interruptible; | |
284 | ||
285 | /* Because we may be allocating inside our own driver, we cannot | |
286 | * assert that there are no objects with pinned pages that are not | |
287 | * being pointed to by hardware. | |
288 | */ | |
289 | unbound = bound = pinned = 0; | |
290 | list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) { | |
291 | if (!obj->base.filp) /* not backed by a freeable object */ | |
292 | continue; | |
293 | ||
294 | if (obj->pages_pin_count) | |
295 | pinned += obj->base.size; | |
296 | else | |
297 | unbound += obj->base.size; | |
298 | } | |
299 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { | |
300 | if (!obj->base.filp) | |
301 | continue; | |
302 | ||
303 | if (obj->pages_pin_count) | |
304 | pinned += obj->base.size; | |
305 | else | |
306 | bound += obj->base.size; | |
307 | } | |
308 | ||
309 | if (unlock) | |
310 | mutex_unlock(&dev->struct_mutex); | |
311 | ||
312 | if (freed_pages || unbound || bound) | |
313 | pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n", | |
314 | freed_pages << PAGE_SHIFT, pinned); | |
315 | if (unbound || bound) | |
316 | pr_err("%lu and %lu bytes still available in the " | |
317 | "bound and unbound GPU page lists.\n", | |
318 | bound, unbound); | |
319 | ||
320 | *(unsigned long *)ptr += freed_pages; | |
321 | return NOTIFY_DONE; | |
322 | } | |
323 | ||
eb0b44ad DV |
324 | /** |
325 | * i915_gem_shrinker_init - Initialize i915 shrinker | |
326 | * @dev_priv: i915 device | |
327 | * | |
328 | * This function registers and sets up the i915 shrinker and OOM handler. | |
329 | */ | |
be6a0376 DV |
330 | void i915_gem_shrinker_init(struct drm_i915_private *dev_priv) |
331 | { | |
332 | dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan; | |
333 | dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count; | |
334 | dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS; | |
335 | register_shrinker(&dev_priv->mm.shrinker); | |
336 | ||
337 | dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom; | |
338 | register_oom_notifier(&dev_priv->mm.oom_notifier); | |
339 | } |