blob: 03cf3ccc415257dbfb4bc78604db3cc84b2ae85d [file] [log] [blame]
Daniel Vetterbe6a0372015-03-18 10:46:04 +01001/*
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
37static 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
Daniel Vettereb0b44a2015-03-18 14:47:59 +010050/**
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 */
Daniel Vetterbe6a0372015-03-18 10:46:04 +010074unsigned long
75i915_gem_shrink(struct drm_i915_private *dev_priv,
Chris Wilson14387542015-10-01 12:18:25 +010076 unsigned long target, unsigned flags)
Daniel Vetterbe6a0372015-03-18 10:46:04 +010077{
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
Chris Wilson3abafa52015-10-01 12:18:26 +010088 trace_i915_gem_shrink(dev_priv, target, flags);
Chris Wilsonc9c0f5e2015-10-01 12:18:27 +010089 i915_gem_retire_requests(dev_priv->dev);
Chris Wilson3abafa52015-10-01 12:18:26 +010090
Daniel Vetterbe6a0372015-03-18 10:46:04 +010091 /*
92 * As we may completely rewrite the (un)bound list whilst unbinding
93 * (due to retiring requests) we have to strictly process only
94 * one element of the list at the time, and recheck the list
95 * on every iteration.
96 *
97 * In particular, we must hold a reference whilst removing the
98 * object as we may end up waiting for and/or retiring the objects.
99 * This might release the final reference (held by the active list)
100 * and result in the object being freed from under us. This is
101 * similar to the precautions the eviction code must take whilst
102 * removing objects.
103 *
104 * Also note that although these lists do not hold a reference to
105 * the object we can safely grab one here: The final object
106 * unreferencing and the bound_list are both protected by the
107 * dev->struct_mutex and so we won't ever be able to observe an
108 * object on the bound_list with a reference count equals 0.
109 */
110 for (phase = phases; phase->list; phase++) {
111 struct list_head still_in_list;
112
113 if ((flags & phase->bit) == 0)
114 continue;
115
116 INIT_LIST_HEAD(&still_in_list);
117 while (count < target && !list_empty(phase->list)) {
118 struct drm_i915_gem_object *obj;
119 struct i915_vma *vma, *v;
120
121 obj = list_first_entry(phase->list,
122 typeof(*obj), global_list);
123 list_move_tail(&obj->global_list, &still_in_list);
124
125 if (flags & I915_SHRINK_PURGEABLE &&
126 obj->madv != I915_MADV_DONTNEED)
127 continue;
128
129 drm_gem_object_reference(&obj->base);
130
131 /* For the unbound phase, this should be a no-op! */
132 list_for_each_entry_safe(vma, v,
133 &obj->vma_list, vma_link)
134 if (i915_vma_unbind(vma))
135 break;
136
137 if (i915_gem_object_put_pages(obj) == 0)
138 count += obj->base.size >> PAGE_SHIFT;
139
140 drm_gem_object_unreference(&obj->base);
141 }
142 list_splice(&still_in_list, phase->list);
143 }
144
Chris Wilsonc9c0f5e2015-10-01 12:18:27 +0100145 i915_gem_retire_requests(dev_priv->dev);
146
Daniel Vetterbe6a0372015-03-18 10:46:04 +0100147 return count;
148}
149
Daniel Vettereb0b44a2015-03-18 14:47:59 +0100150/**
Daniel Vetter1f2449c2015-10-06 14:47:55 +0200151 * i915_gem_shrink_all - Shrink buffer object caches completely
Daniel Vettereb0b44a2015-03-18 14:47:59 +0100152 * @dev_priv: i915 device
153 *
154 * This is a simple wraper around i915_gem_shrink() to aggressively shrink all
155 * caches completely. It also first waits for and retires all outstanding
156 * requests to also be able to release backing storage for active objects.
157 *
158 * This should only be used in code to intentionally quiescent the gpu or as a
159 * last-ditch effort when memory seems to have run out.
160 *
161 * Returns:
162 * The number of pages of backing storage actually released.
163 */
Daniel Vetterbe6a0372015-03-18 10:46:04 +0100164unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv)
165{
Chris Wilson14387542015-10-01 12:18:25 +0100166 return i915_gem_shrink(dev_priv, -1UL,
Daniel Vetterbe6a0372015-03-18 10:46:04 +0100167 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND);
168}
169
170static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
171{
172 if (!mutex_trylock(&dev->struct_mutex)) {
173 if (!mutex_is_locked_by(&dev->struct_mutex, current))
174 return false;
175
176 if (to_i915(dev)->mm.shrinker_no_lock_stealing)
177 return false;
178
179 *unlock = false;
180 } else
181 *unlock = true;
182
183 return true;
184}
185
186static int num_vma_bound(struct drm_i915_gem_object *obj)
187{
188 struct i915_vma *vma;
189 int count = 0;
190
Chris Wilsonf6234c12015-04-07 17:28:17 +0100191 list_for_each_entry(vma, &obj->vma_list, vma_link) {
Daniel Vetterbe6a0372015-03-18 10:46:04 +0100192 if (drm_mm_node_allocated(&vma->node))
193 count++;
Chris Wilsonf6234c12015-04-07 17:28:17 +0100194 if (vma->pin_count)
195 count++;
196 }
Daniel Vetterbe6a0372015-03-18 10:46:04 +0100197
198 return count;
199}
200
201static unsigned long
202i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
203{
204 struct drm_i915_private *dev_priv =
205 container_of(shrinker, struct drm_i915_private, mm.shrinker);
206 struct drm_device *dev = dev_priv->dev;
207 struct drm_i915_gem_object *obj;
208 unsigned long count;
209 bool unlock;
210
211 if (!i915_gem_shrinker_lock(dev, &unlock))
212 return 0;
213
214 count = 0;
215 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
216 if (obj->pages_pin_count == 0)
217 count += obj->base.size >> PAGE_SHIFT;
218
219 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Chris Wilsonf6234c12015-04-07 17:28:17 +0100220 if (obj->pages_pin_count == num_vma_bound(obj))
Daniel Vetterbe6a0372015-03-18 10:46:04 +0100221 count += obj->base.size >> PAGE_SHIFT;
222 }
223
224 if (unlock)
225 mutex_unlock(&dev->struct_mutex);
226
227 return count;
228}
229
230static unsigned long
231i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
232{
233 struct drm_i915_private *dev_priv =
234 container_of(shrinker, struct drm_i915_private, mm.shrinker);
235 struct drm_device *dev = dev_priv->dev;
236 unsigned long freed;
237 bool unlock;
238
239 if (!i915_gem_shrinker_lock(dev, &unlock))
240 return SHRINK_STOP;
241
242 freed = i915_gem_shrink(dev_priv,
243 sc->nr_to_scan,
244 I915_SHRINK_BOUND |
245 I915_SHRINK_UNBOUND |
246 I915_SHRINK_PURGEABLE);
247 if (freed < sc->nr_to_scan)
248 freed += i915_gem_shrink(dev_priv,
249 sc->nr_to_scan - freed,
250 I915_SHRINK_BOUND |
251 I915_SHRINK_UNBOUND);
252 if (unlock)
253 mutex_unlock(&dev->struct_mutex);
254
255 return freed;
256}
257
258static int
259i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
260{
261 struct drm_i915_private *dev_priv =
262 container_of(nb, struct drm_i915_private, mm.oom_notifier);
263 struct drm_device *dev = dev_priv->dev;
264 struct drm_i915_gem_object *obj;
265 unsigned long timeout = msecs_to_jiffies(5000) + 1;
266 unsigned long pinned, bound, unbound, freed_pages;
267 bool was_interruptible;
268 bool unlock;
269
270 while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout) {
271 schedule_timeout_killable(1);
272 if (fatal_signal_pending(current))
273 return NOTIFY_DONE;
274 }
275 if (timeout == 0) {
276 pr_err("Unable to purge GPU memory due lock contention.\n");
277 return NOTIFY_DONE;
278 }
279
280 was_interruptible = dev_priv->mm.interruptible;
281 dev_priv->mm.interruptible = false;
282
283 freed_pages = i915_gem_shrink_all(dev_priv);
284
285 dev_priv->mm.interruptible = was_interruptible;
286
287 /* Because we may be allocating inside our own driver, we cannot
288 * assert that there are no objects with pinned pages that are not
289 * being pointed to by hardware.
290 */
291 unbound = bound = pinned = 0;
292 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
293 if (!obj->base.filp) /* not backed by a freeable object */
294 continue;
295
296 if (obj->pages_pin_count)
297 pinned += obj->base.size;
298 else
299 unbound += obj->base.size;
300 }
301 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
302 if (!obj->base.filp)
303 continue;
304
305 if (obj->pages_pin_count)
306 pinned += obj->base.size;
307 else
308 bound += obj->base.size;
309 }
310
311 if (unlock)
312 mutex_unlock(&dev->struct_mutex);
313
314 if (freed_pages || unbound || bound)
315 pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n",
316 freed_pages << PAGE_SHIFT, pinned);
317 if (unbound || bound)
318 pr_err("%lu and %lu bytes still available in the "
319 "bound and unbound GPU page lists.\n",
320 bound, unbound);
321
322 *(unsigned long *)ptr += freed_pages;
323 return NOTIFY_DONE;
324}
325
Daniel Vettereb0b44a2015-03-18 14:47:59 +0100326/**
327 * i915_gem_shrinker_init - Initialize i915 shrinker
328 * @dev_priv: i915 device
329 *
330 * This function registers and sets up the i915 shrinker and OOM handler.
331 */
Daniel Vetterbe6a0372015-03-18 10:46:04 +0100332void i915_gem_shrinker_init(struct drm_i915_private *dev_priv)
333{
334 dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
335 dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
336 dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
337 register_shrinker(&dev_priv->mm.shrinker);
338
339 dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
340 register_oom_notifier(&dev_priv->mm.oom_notifier);
341}