drm/i915/shrinker: Hook up vmap allocation failure notifier
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gem_shrinker.c
CommitLineData
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>
e87666b5 31#include <linux/vmalloc.h>
be6a0376
DV
32#include <drm/drmP.h>
33#include <drm/i915_drm.h>
34
35#include "i915_drv.h"
36#include "i915_trace.h"
37
38static 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_SMP) || defined(CONFIG_DEBUG_MUTEXES)
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
c1a415e2
CW
51static int num_vma_bound(struct drm_i915_gem_object *obj)
52{
53 struct i915_vma *vma;
54 int count = 0;
55
1c7f4bca 56 list_for_each_entry(vma, &obj->vma_list, obj_link) {
c1a415e2
CW
57 if (drm_mm_node_allocated(&vma->node))
58 count++;
59 if (vma->pin_count)
60 count++;
61 }
62
63 return count;
64}
65
66static bool swap_available(void)
67{
68 return get_nr_swap_pages() > 0;
69}
70
71static bool can_release_pages(struct drm_i915_gem_object *obj)
72{
73 /* Only report true if by unbinding the object and putting its pages
74 * we can actually make forward progress towards freeing physical
75 * pages.
76 *
77 * If the pages are pinned for any other reason than being bound
78 * to the GPU, simply unbinding from the GPU is not going to succeed
79 * in releasing our pin count on the pages themselves.
80 */
81 if (obj->pages_pin_count != num_vma_bound(obj))
82 return false;
83
84 /* We can only return physical pages to the system if we can either
85 * discard the contents (because the user has marked them as being
86 * purgeable) or if we can move their contents out to swap.
87 */
88 return swap_available() || obj->madv == I915_MADV_DONTNEED;
89}
90
eb0b44ad
DV
91/**
92 * i915_gem_shrink - Shrink buffer object caches
93 * @dev_priv: i915 device
94 * @target: amount of memory to make available, in pages
95 * @flags: control flags for selecting cache types
96 *
97 * This function is the main interface to the shrinker. It will try to release
98 * up to @target pages of main memory backing storage from buffer objects.
99 * Selection of the specific caches can be done with @flags. This is e.g. useful
100 * when purgeable objects should be removed from caches preferentially.
101 *
102 * Note that it's not guaranteed that released amount is actually available as
103 * free system memory - the pages might still be in-used to due to other reasons
104 * (like cpu mmaps) or the mm core has reused them before we could grab them.
105 * Therefore code that needs to explicitly shrink buffer objects caches (e.g. to
106 * avoid deadlocks in memory reclaim) must fall back to i915_gem_shrink_all().
107 *
108 * Also note that any kind of pinning (both per-vma address space pins and
109 * backing storage pins at the buffer object level) result in the shrinker code
110 * having to skip the object.
111 *
112 * Returns:
113 * The number of pages of backing storage actually released.
114 */
be6a0376
DV
115unsigned long
116i915_gem_shrink(struct drm_i915_private *dev_priv,
14387540 117 unsigned long target, unsigned flags)
be6a0376
DV
118{
119 const struct {
120 struct list_head *list;
121 unsigned int bit;
122 } phases[] = {
123 { &dev_priv->mm.unbound_list, I915_SHRINK_UNBOUND },
124 { &dev_priv->mm.bound_list, I915_SHRINK_BOUND },
125 { NULL, 0 },
126 }, *phase;
127 unsigned long count = 0;
128
3abafa53 129 trace_i915_gem_shrink(dev_priv, target, flags);
c9c0f5ea 130 i915_gem_retire_requests(dev_priv->dev);
3abafa53 131
be6a0376
DV
132 /*
133 * As we may completely rewrite the (un)bound list whilst unbinding
134 * (due to retiring requests) we have to strictly process only
135 * one element of the list at the time, and recheck the list
136 * on every iteration.
137 *
138 * In particular, we must hold a reference whilst removing the
139 * object as we may end up waiting for and/or retiring the objects.
140 * This might release the final reference (held by the active list)
141 * and result in the object being freed from under us. This is
142 * similar to the precautions the eviction code must take whilst
143 * removing objects.
144 *
145 * Also note that although these lists do not hold a reference to
146 * the object we can safely grab one here: The final object
147 * unreferencing and the bound_list are both protected by the
148 * dev->struct_mutex and so we won't ever be able to observe an
149 * object on the bound_list with a reference count equals 0.
150 */
151 for (phase = phases; phase->list; phase++) {
152 struct list_head still_in_list;
153
154 if ((flags & phase->bit) == 0)
155 continue;
156
157 INIT_LIST_HEAD(&still_in_list);
158 while (count < target && !list_empty(phase->list)) {
159 struct drm_i915_gem_object *obj;
160 struct i915_vma *vma, *v;
161
162 obj = list_first_entry(phase->list,
163 typeof(*obj), global_list);
164 list_move_tail(&obj->global_list, &still_in_list);
165
166 if (flags & I915_SHRINK_PURGEABLE &&
167 obj->madv != I915_MADV_DONTNEED)
168 continue;
169
5763ff04
CW
170 if ((flags & I915_SHRINK_ACTIVE) == 0 && obj->active)
171 continue;
172
c1a415e2
CW
173 if (!can_release_pages(obj))
174 continue;
175
be6a0376
DV
176 drm_gem_object_reference(&obj->base);
177
178 /* For the unbound phase, this should be a no-op! */
179 list_for_each_entry_safe(vma, v,
1c7f4bca 180 &obj->vma_list, obj_link)
be6a0376
DV
181 if (i915_vma_unbind(vma))
182 break;
183
184 if (i915_gem_object_put_pages(obj) == 0)
185 count += obj->base.size >> PAGE_SHIFT;
186
187 drm_gem_object_unreference(&obj->base);
188 }
189 list_splice(&still_in_list, phase->list);
190 }
191
c9c0f5ea
CW
192 i915_gem_retire_requests(dev_priv->dev);
193
be6a0376
DV
194 return count;
195}
196
eb0b44ad 197/**
1f2449cd 198 * i915_gem_shrink_all - Shrink buffer object caches completely
eb0b44ad
DV
199 * @dev_priv: i915 device
200 *
201 * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
202 * caches completely. It also first waits for and retires all outstanding
203 * requests to also be able to release backing storage for active objects.
204 *
205 * This should only be used in code to intentionally quiescent the gpu or as a
206 * last-ditch effort when memory seems to have run out.
207 *
208 * Returns:
209 * The number of pages of backing storage actually released.
210 */
be6a0376
DV
211unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
212{
14387540 213 return i915_gem_shrink(dev_priv, -1UL,
5763ff04
CW
214 I915_SHRINK_BOUND |
215 I915_SHRINK_UNBOUND |
216 I915_SHRINK_ACTIVE);
be6a0376
DV
217}
218
219static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
220{
221 if (!mutex_trylock(&dev->struct_mutex)) {
222 if (!mutex_is_locked_by(&dev->struct_mutex, current))
223 return false;
224
225 if (to_i915(dev)->mm.shrinker_no_lock_stealing)
226 return false;
227
228 *unlock = false;
229 } else
230 *unlock = true;
231
232 return true;
233}
234
be6a0376
DV
235static unsigned long
236i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
237{
238 struct drm_i915_private *dev_priv =
239 container_of(shrinker, struct drm_i915_private, mm.shrinker);
240 struct drm_device *dev = dev_priv->dev;
241 struct drm_i915_gem_object *obj;
242 unsigned long count;
243 bool unlock;
244
245 if (!i915_gem_shrinker_lock(dev, &unlock))
246 return 0;
247
248 count = 0;
249 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
6f0ac204 250 if (can_release_pages(obj))
be6a0376
DV
251 count += obj->base.size >> PAGE_SHIFT;
252
253 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
c1a415e2 254 if (!obj->active && can_release_pages(obj))
be6a0376
DV
255 count += obj->base.size >> PAGE_SHIFT;
256 }
257
258 if (unlock)
259 mutex_unlock(&dev->struct_mutex);
260
261 return count;
262}
263
264static unsigned long
265i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
266{
267 struct drm_i915_private *dev_priv =
268 container_of(shrinker, struct drm_i915_private, mm.shrinker);
269 struct drm_device *dev = dev_priv->dev;
270 unsigned long freed;
271 bool unlock;
272
273 if (!i915_gem_shrinker_lock(dev, &unlock))
274 return SHRINK_STOP;
275
276 freed = i915_gem_shrink(dev_priv,
277 sc->nr_to_scan,
278 I915_SHRINK_BOUND |
279 I915_SHRINK_UNBOUND |
280 I915_SHRINK_PURGEABLE);
281 if (freed < sc->nr_to_scan)
282 freed += i915_gem_shrink(dev_priv,
283 sc->nr_to_scan - freed,
284 I915_SHRINK_BOUND |
285 I915_SHRINK_UNBOUND);
286 if (unlock)
287 mutex_unlock(&dev->struct_mutex);
288
289 return freed;
290}
291
292static int
293i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
294{
295 struct drm_i915_private *dev_priv =
296 container_of(nb, struct drm_i915_private, mm.oom_notifier);
297 struct drm_device *dev = dev_priv->dev;
298 struct drm_i915_gem_object *obj;
299 unsigned long timeout = msecs_to_jiffies(5000) + 1;
300 unsigned long pinned, bound, unbound, freed_pages;
301 bool was_interruptible;
302 bool unlock;
303
304 while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout) {
305 schedule_timeout_killable(1);
306 if (fatal_signal_pending(current))
307 return NOTIFY_DONE;
308 }
309 if (timeout == 0) {
310 pr_err("Unable to purge GPU memory due lock contention.\n");
311 return NOTIFY_DONE;
312 }
313
314 was_interruptible = dev_priv->mm.interruptible;
315 dev_priv->mm.interruptible = false;
316
317 freed_pages = i915_gem_shrink_all(dev_priv);
318
319 dev_priv->mm.interruptible = was_interruptible;
320
321 /* Because we may be allocating inside our own driver, we cannot
322 * assert that there are no objects with pinned pages that are not
323 * being pointed to by hardware.
324 */
325 unbound = bound = pinned = 0;
326 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
327 if (!obj->base.filp) /* not backed by a freeable object */
328 continue;
329
330 if (obj->pages_pin_count)
331 pinned += obj->base.size;
332 else
333 unbound += obj->base.size;
334 }
335 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
336 if (!obj->base.filp)
337 continue;
338
339 if (obj->pages_pin_count)
340 pinned += obj->base.size;
341 else
342 bound += obj->base.size;
343 }
344
345 if (unlock)
346 mutex_unlock(&dev->struct_mutex);
347
348 if (freed_pages || unbound || bound)
349 pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n",
350 freed_pages << PAGE_SHIFT, pinned);
351 if (unbound || bound)
352 pr_err("%lu and %lu bytes still available in the "
353 "bound and unbound GPU page lists.\n",
354 bound, unbound);
355
356 *(unsigned long *)ptr += freed_pages;
357 return NOTIFY_DONE;
358}
359
e87666b5
CW
360static int
361i915_gem_shrinker_vmap(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.vmap_notifier);
365 struct drm_device *dev = dev_priv->dev;
366 unsigned long timeout = msecs_to_jiffies(5000) + 1;
367 unsigned long freed_pages;
368 bool was_interruptible;
369 bool unlock;
370
371 while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout) {
372 schedule_timeout_killable(1);
373 if (fatal_signal_pending(current))
374 return NOTIFY_DONE;
375 }
376 if (timeout == 0) {
377 pr_err("Unable to purge GPU vmaps due to lock contention.\n");
378 return NOTIFY_DONE;
379 }
380
381 was_interruptible = dev_priv->mm.interruptible;
382 dev_priv->mm.interruptible = false;
383
384 freed_pages = i915_gem_shrink_all(dev_priv);
385
386 dev_priv->mm.interruptible = was_interruptible;
387 if (unlock)
388 mutex_unlock(&dev->struct_mutex);
389
390 *(unsigned long *)ptr += freed_pages;
391 return NOTIFY_DONE;
392}
393
eb0b44ad
DV
394/**
395 * i915_gem_shrinker_init - Initialize i915 shrinker
396 * @dev_priv: i915 device
397 *
398 * This function registers and sets up the i915 shrinker and OOM handler.
399 */
be6a0376
DV
400void i915_gem_shrinker_init(struct drm_i915_private *dev_priv)
401{
402 dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
403 dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
404 dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
a8a40589 405 WARN_ON(register_shrinker(&dev_priv->mm.shrinker));
be6a0376
DV
406
407 dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
a8a40589 408 WARN_ON(register_oom_notifier(&dev_priv->mm.oom_notifier));
e87666b5
CW
409
410 dev_priv->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
411 WARN_ON(register_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
a8a40589
ID
412}
413
414/**
415 * i915_gem_shrinker_cleanup - Clean up i915 shrinker
416 * @dev_priv: i915 device
417 *
418 * This function unregisters the i915 shrinker and OOM handler.
419 */
420void i915_gem_shrinker_cleanup(struct drm_i915_private *dev_priv)
421{
e87666b5 422 WARN_ON(unregister_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
a8a40589
ID
423 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
424 unregister_shrinker(&dev_priv->mm.shrinker);
be6a0376 425}
This page took 0.102021 seconds and 5 git commands to generate.