drm/i915/shrinker: Refactor common uninterruptible locking
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gem_shrinker.c
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 <linux/vmalloc.h>
32 #include <drm/drmP.h>
33 #include <drm/i915_drm.h>
34
35 #include "i915_drv.h"
36 #include "i915_trace.h"
37
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_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
51 static int num_vma_bound(struct drm_i915_gem_object *obj)
52 {
53 struct i915_vma *vma;
54 int count = 0;
55
56 list_for_each_entry(vma, &obj->vma_list, obj_link) {
57 if (drm_mm_node_allocated(&vma->node))
58 count++;
59 if (vma->pin_count)
60 count++;
61 }
62
63 return count;
64 }
65
66 static bool swap_available(void)
67 {
68 return get_nr_swap_pages() > 0;
69 }
70
71 static 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
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 */
115 unsigned long
116 i915_gem_shrink(struct drm_i915_private *dev_priv,
117 unsigned long target, unsigned flags)
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
129 trace_i915_gem_shrink(dev_priv, target, flags);
130 i915_gem_retire_requests(dev_priv->dev);
131
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
170 if ((flags & I915_SHRINK_ACTIVE) == 0 && obj->active)
171 continue;
172
173 if (!can_release_pages(obj))
174 continue;
175
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,
180 &obj->vma_list, obj_link)
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
192 i915_gem_retire_requests(dev_priv->dev);
193
194 return count;
195 }
196
197 /**
198 * i915_gem_shrink_all - Shrink buffer object caches completely
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 */
211 unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
212 {
213 return i915_gem_shrink(dev_priv, -1UL,
214 I915_SHRINK_BOUND |
215 I915_SHRINK_UNBOUND |
216 I915_SHRINK_ACTIVE);
217 }
218
219 static 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
235 static unsigned long
236 i915_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)
250 if (can_release_pages(obj))
251 count += obj->base.size >> PAGE_SHIFT;
252
253 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
254 if (!obj->active && can_release_pages(obj))
255 count += obj->base.size >> PAGE_SHIFT;
256 }
257
258 if (unlock)
259 mutex_unlock(&dev->struct_mutex);
260
261 return count;
262 }
263
264 static unsigned long
265 i915_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
292 struct shrinker_lock_uninterruptible {
293 bool was_interruptible;
294 bool unlock;
295 };
296
297 static bool
298 i915_gem_shrinker_lock_uninterruptible(struct drm_i915_private *dev_priv,
299 struct shrinker_lock_uninterruptible *slu,
300 int timeout_ms)
301 {
302 unsigned long timeout = msecs_to_jiffies(timeout_ms) + 1;
303
304 while (!i915_gem_shrinker_lock(dev_priv->dev, &slu->unlock)) {
305 schedule_timeout_killable(1);
306 if (fatal_signal_pending(current))
307 return false;
308 if (--timeout == 0) {
309 pr_err("Unable to lock GPU to purge memory.\n");
310 return false;
311 }
312 }
313
314 slu->was_interruptible = dev_priv->mm.interruptible;
315 dev_priv->mm.interruptible = false;
316 return true;
317 }
318
319 static void
320 i915_gem_shrinker_unlock_uninterruptible(struct drm_i915_private *dev_priv,
321 struct shrinker_lock_uninterruptible *slu)
322 {
323 dev_priv->mm.interruptible = slu->was_interruptible;
324 if (slu->unlock)
325 mutex_unlock(&dev_priv->dev->struct_mutex);
326 }
327
328 static int
329 i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
330 {
331 struct drm_i915_private *dev_priv =
332 container_of(nb, struct drm_i915_private, mm.oom_notifier);
333 struct shrinker_lock_uninterruptible slu;
334 struct drm_i915_gem_object *obj;
335 unsigned long pinned, bound, unbound, freed_pages;
336
337 if (!i915_gem_shrinker_lock_uninterruptible(dev_priv, &slu, 5000))
338 return NOTIFY_DONE;
339
340 freed_pages = i915_gem_shrink_all(dev_priv);
341
342 /* Because we may be allocating inside our own driver, we cannot
343 * assert that there are no objects with pinned pages that are not
344 * being pointed to by hardware.
345 */
346 unbound = bound = pinned = 0;
347 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
348 if (!obj->base.filp) /* not backed by a freeable object */
349 continue;
350
351 if (obj->pages_pin_count)
352 pinned += obj->base.size;
353 else
354 unbound += obj->base.size;
355 }
356 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
357 if (!obj->base.filp)
358 continue;
359
360 if (obj->pages_pin_count)
361 pinned += obj->base.size;
362 else
363 bound += obj->base.size;
364 }
365
366 i915_gem_shrinker_unlock_uninterruptible(dev_priv, &slu);
367
368 if (freed_pages || unbound || bound)
369 pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n",
370 freed_pages << PAGE_SHIFT, pinned);
371 if (unbound || bound)
372 pr_err("%lu and %lu bytes still available in the "
373 "bound and unbound GPU page lists.\n",
374 bound, unbound);
375
376 *(unsigned long *)ptr += freed_pages;
377 return NOTIFY_DONE;
378 }
379
380 static int
381 i915_gem_shrinker_vmap(struct notifier_block *nb, unsigned long event, void *ptr)
382 {
383 struct drm_i915_private *dev_priv =
384 container_of(nb, struct drm_i915_private, mm.vmap_notifier);
385 struct shrinker_lock_uninterruptible slu;
386 unsigned long freed_pages;
387
388 if (!i915_gem_shrinker_lock_uninterruptible(dev_priv, &slu, 5000))
389 return NOTIFY_DONE;
390
391 freed_pages = i915_gem_shrink_all(dev_priv);
392
393 i915_gem_shrinker_unlock_uninterruptible(dev_priv, &slu);
394
395 *(unsigned long *)ptr += freed_pages;
396 return NOTIFY_DONE;
397 }
398
399 /**
400 * i915_gem_shrinker_init - Initialize i915 shrinker
401 * @dev_priv: i915 device
402 *
403 * This function registers and sets up the i915 shrinker and OOM handler.
404 */
405 void i915_gem_shrinker_init(struct drm_i915_private *dev_priv)
406 {
407 dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
408 dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
409 dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
410 WARN_ON(register_shrinker(&dev_priv->mm.shrinker));
411
412 dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
413 WARN_ON(register_oom_notifier(&dev_priv->mm.oom_notifier));
414
415 dev_priv->mm.vmap_notifier.notifier_call = i915_gem_shrinker_vmap;
416 WARN_ON(register_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
417 }
418
419 /**
420 * i915_gem_shrinker_cleanup - Clean up i915 shrinker
421 * @dev_priv: i915 device
422 *
423 * This function unregisters the i915 shrinker and OOM handler.
424 */
425 void i915_gem_shrinker_cleanup(struct drm_i915_private *dev_priv)
426 {
427 WARN_ON(unregister_vmap_purge_notifier(&dev_priv->mm.vmap_notifier));
428 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
429 unregister_shrinker(&dev_priv->mm.shrinker);
430 }
This page took 0.03998 seconds and 6 git commands to generate.