xref: /linux/drivers/gpu/drm/i915/gem/i915_gem_object.c (revision 94cad89ae4505672ae65457d12f77c44ca87655b)
1 /*
2  * Copyright © 2017 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/sched/mm.h>
26 
27 #include "display/intel_frontbuffer.h"
28 #include "gt/intel_gt.h"
29 #include "i915_drv.h"
30 #include "i915_gem_clflush.h"
31 #include "i915_gem_context.h"
32 #include "i915_gem_mman.h"
33 #include "i915_gem_object.h"
34 #include "i915_globals.h"
35 #include "i915_trace.h"
36 
37 static struct i915_global_object {
38 	struct i915_global base;
39 	struct kmem_cache *slab_objects;
40 } global;
41 
42 struct drm_i915_gem_object *i915_gem_object_alloc(void)
43 {
44 	return kmem_cache_zalloc(global.slab_objects, GFP_KERNEL);
45 }
46 
47 void i915_gem_object_free(struct drm_i915_gem_object *obj)
48 {
49 	return kmem_cache_free(global.slab_objects, obj);
50 }
51 
52 void i915_gem_object_init(struct drm_i915_gem_object *obj,
53 			  const struct drm_i915_gem_object_ops *ops,
54 			  struct lock_class_key *key)
55 {
56 	__mutex_init(&obj->mm.lock, ops->name ?: "obj->mm.lock", key);
57 
58 	spin_lock_init(&obj->vma.lock);
59 	INIT_LIST_HEAD(&obj->vma.list);
60 
61 	INIT_LIST_HEAD(&obj->mm.link);
62 
63 	INIT_LIST_HEAD(&obj->lut_list);
64 	spin_lock_init(&obj->lut_lock);
65 
66 	spin_lock_init(&obj->mmo.lock);
67 	obj->mmo.offsets = RB_ROOT;
68 
69 	init_rcu_head(&obj->rcu);
70 
71 	obj->ops = ops;
72 
73 	obj->mm.madv = I915_MADV_WILLNEED;
74 	INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
75 	mutex_init(&obj->mm.get_page.lock);
76 
77 	if (IS_ENABLED(CONFIG_LOCKDEP) && i915_gem_object_is_shrinkable(obj))
78 		i915_gem_shrinker_taints_mutex(to_i915(obj->base.dev),
79 					       &obj->mm.lock);
80 }
81 
82 /**
83  * Mark up the object's coherency levels for a given cache_level
84  * @obj: #drm_i915_gem_object
85  * @cache_level: cache level
86  */
87 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
88 					 unsigned int cache_level)
89 {
90 	obj->cache_level = cache_level;
91 
92 	if (cache_level != I915_CACHE_NONE)
93 		obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
94 				       I915_BO_CACHE_COHERENT_FOR_WRITE);
95 	else if (HAS_LLC(to_i915(obj->base.dev)))
96 		obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
97 	else
98 		obj->cache_coherent = 0;
99 
100 	obj->cache_dirty =
101 		!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE);
102 }
103 
104 void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
105 {
106 	struct drm_i915_gem_object *obj = to_intel_bo(gem);
107 	struct drm_i915_file_private *fpriv = file->driver_priv;
108 	struct i915_lut_handle bookmark = {};
109 	struct i915_mmap_offset *mmo, *mn;
110 	struct i915_lut_handle *lut, *ln;
111 	LIST_HEAD(close);
112 
113 	spin_lock(&obj->lut_lock);
114 	list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
115 		struct i915_gem_context *ctx = lut->ctx;
116 
117 		if (ctx && ctx->file_priv == fpriv) {
118 			i915_gem_context_get(ctx);
119 			list_move(&lut->obj_link, &close);
120 		}
121 
122 		/* Break long locks, and carefully continue on from this spot */
123 		if (&ln->obj_link != &obj->lut_list) {
124 			list_add_tail(&bookmark.obj_link, &ln->obj_link);
125 			if (cond_resched_lock(&obj->lut_lock))
126 				list_safe_reset_next(&bookmark, ln, obj_link);
127 			__list_del_entry(&bookmark.obj_link);
128 		}
129 	}
130 	spin_unlock(&obj->lut_lock);
131 
132 	spin_lock(&obj->mmo.lock);
133 	rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
134 		drm_vma_node_revoke(&mmo->vma_node, file);
135 	spin_unlock(&obj->mmo.lock);
136 
137 	list_for_each_entry_safe(lut, ln, &close, obj_link) {
138 		struct i915_gem_context *ctx = lut->ctx;
139 		struct i915_vma *vma;
140 
141 		/*
142 		 * We allow the process to have multiple handles to the same
143 		 * vma, in the same fd namespace, by virtue of flink/open.
144 		 */
145 
146 		mutex_lock(&ctx->mutex);
147 		vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
148 		if (vma) {
149 			GEM_BUG_ON(vma->obj != obj);
150 			GEM_BUG_ON(!atomic_read(&vma->open_count));
151 			i915_vma_close(vma);
152 		}
153 		mutex_unlock(&ctx->mutex);
154 
155 		i915_gem_context_put(lut->ctx);
156 		i915_lut_handle_free(lut);
157 		i915_gem_object_put(obj);
158 	}
159 }
160 
161 static void __i915_gem_free_object_rcu(struct rcu_head *head)
162 {
163 	struct drm_i915_gem_object *obj =
164 		container_of(head, typeof(*obj), rcu);
165 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
166 
167 	dma_resv_fini(&obj->base._resv);
168 	i915_gem_object_free(obj);
169 
170 	GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
171 	atomic_dec(&i915->mm.free_count);
172 }
173 
174 static void __i915_gem_free_objects(struct drm_i915_private *i915,
175 				    struct llist_node *freed)
176 {
177 	struct drm_i915_gem_object *obj, *on;
178 
179 	llist_for_each_entry_safe(obj, on, freed, freed) {
180 		struct i915_mmap_offset *mmo, *mn;
181 
182 		trace_i915_gem_object_destroy(obj);
183 
184 		if (!list_empty(&obj->vma.list)) {
185 			struct i915_vma *vma;
186 
187 			/*
188 			 * Note that the vma keeps an object reference while
189 			 * it is active, so it *should* not sleep while we
190 			 * destroy it. Our debug code errs insits it *might*.
191 			 * For the moment, play along.
192 			 */
193 			spin_lock(&obj->vma.lock);
194 			while ((vma = list_first_entry_or_null(&obj->vma.list,
195 							       struct i915_vma,
196 							       obj_link))) {
197 				GEM_BUG_ON(vma->obj != obj);
198 				spin_unlock(&obj->vma.lock);
199 
200 				__i915_vma_put(vma);
201 
202 				spin_lock(&obj->vma.lock);
203 			}
204 			spin_unlock(&obj->vma.lock);
205 		}
206 
207 		i915_gem_object_release_mmap(obj);
208 
209 		rbtree_postorder_for_each_entry_safe(mmo, mn,
210 						     &obj->mmo.offsets,
211 						     offset) {
212 			drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
213 					      &mmo->vma_node);
214 			kfree(mmo);
215 		}
216 		obj->mmo.offsets = RB_ROOT;
217 
218 		GEM_BUG_ON(obj->userfault_count);
219 		GEM_BUG_ON(!list_empty(&obj->lut_list));
220 
221 		atomic_set(&obj->mm.pages_pin_count, 0);
222 		__i915_gem_object_put_pages(obj);
223 		GEM_BUG_ON(i915_gem_object_has_pages(obj));
224 		bitmap_free(obj->bit_17);
225 
226 		if (obj->base.import_attach)
227 			drm_prime_gem_destroy(&obj->base, NULL);
228 
229 		drm_gem_free_mmap_offset(&obj->base);
230 
231 		if (obj->ops->release)
232 			obj->ops->release(obj);
233 
234 		/* But keep the pointer alive for RCU-protected lookups */
235 		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
236 		cond_resched();
237 	}
238 }
239 
240 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
241 {
242 	struct llist_node *freed = llist_del_all(&i915->mm.free_list);
243 
244 	if (unlikely(freed))
245 		__i915_gem_free_objects(i915, freed);
246 }
247 
248 static void __i915_gem_free_work(struct work_struct *work)
249 {
250 	struct drm_i915_private *i915 =
251 		container_of(work, struct drm_i915_private, mm.free_work);
252 
253 	i915_gem_flush_free_objects(i915);
254 }
255 
256 void i915_gem_free_object(struct drm_gem_object *gem_obj)
257 {
258 	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
259 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
260 
261 	GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
262 
263 	/*
264 	 * Before we free the object, make sure any pure RCU-only
265 	 * read-side critical sections are complete, e.g.
266 	 * i915_gem_busy_ioctl(). For the corresponding synchronized
267 	 * lookup see i915_gem_object_lookup_rcu().
268 	 */
269 	atomic_inc(&i915->mm.free_count);
270 
271 	/*
272 	 * This serializes freeing with the shrinker. Since the free
273 	 * is delayed, first by RCU then by the workqueue, we want the
274 	 * shrinker to be able to free pages of unreferenced objects,
275 	 * or else we may oom whilst there are plenty of deferred
276 	 * freed objects.
277 	 */
278 	i915_gem_object_make_unshrinkable(obj);
279 
280 	/*
281 	 * Since we require blocking on struct_mutex to unbind the freed
282 	 * object from the GPU before releasing resources back to the
283 	 * system, we can not do that directly from the RCU callback (which may
284 	 * be a softirq context), but must instead then defer that work onto a
285 	 * kthread. We use the RCU callback rather than move the freed object
286 	 * directly onto the work queue so that we can mix between using the
287 	 * worker and performing frees directly from subsequent allocations for
288 	 * crude but effective memory throttling.
289 	 */
290 	if (llist_add(&obj->freed, &i915->mm.free_list))
291 		queue_work(i915->wq, &i915->mm.free_work);
292 }
293 
294 static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
295 {
296 	return !(obj->cache_level == I915_CACHE_NONE ||
297 		 obj->cache_level == I915_CACHE_WT);
298 }
299 
300 void
301 i915_gem_object_flush_write_domain(struct drm_i915_gem_object *obj,
302 				   unsigned int flush_domains)
303 {
304 	struct i915_vma *vma;
305 
306 	assert_object_held(obj);
307 
308 	if (!(obj->write_domain & flush_domains))
309 		return;
310 
311 	switch (obj->write_domain) {
312 	case I915_GEM_DOMAIN_GTT:
313 		spin_lock(&obj->vma.lock);
314 		for_each_ggtt_vma(vma, obj) {
315 			if (i915_vma_unset_ggtt_write(vma))
316 				intel_gt_flush_ggtt_writes(vma->vm->gt);
317 		}
318 		spin_unlock(&obj->vma.lock);
319 
320 		i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
321 		break;
322 
323 	case I915_GEM_DOMAIN_WC:
324 		wmb();
325 		break;
326 
327 	case I915_GEM_DOMAIN_CPU:
328 		i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
329 		break;
330 
331 	case I915_GEM_DOMAIN_RENDER:
332 		if (gpu_write_needs_clflush(obj))
333 			obj->cache_dirty = true;
334 		break;
335 	}
336 
337 	obj->write_domain = 0;
338 }
339 
340 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
341 					 enum fb_op_origin origin)
342 {
343 	struct intel_frontbuffer *front;
344 
345 	front = __intel_frontbuffer_get(obj);
346 	if (front) {
347 		intel_frontbuffer_flush(front, origin);
348 		intel_frontbuffer_put(front);
349 	}
350 }
351 
352 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
353 					      enum fb_op_origin origin)
354 {
355 	struct intel_frontbuffer *front;
356 
357 	front = __intel_frontbuffer_get(obj);
358 	if (front) {
359 		intel_frontbuffer_invalidate(front, origin);
360 		intel_frontbuffer_put(front);
361 	}
362 }
363 
364 void i915_gem_init__objects(struct drm_i915_private *i915)
365 {
366 	INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
367 }
368 
369 static void i915_global_objects_shrink(void)
370 {
371 	kmem_cache_shrink(global.slab_objects);
372 }
373 
374 static void i915_global_objects_exit(void)
375 {
376 	kmem_cache_destroy(global.slab_objects);
377 }
378 
379 static struct i915_global_object global = { {
380 	.shrink = i915_global_objects_shrink,
381 	.exit = i915_global_objects_exit,
382 } };
383 
384 int __init i915_global_objects_init(void)
385 {
386 	global.slab_objects =
387 		KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
388 	if (!global.slab_objects)
389 		return -ENOMEM;
390 
391 	i915_global_register(&global.base);
392 	return 0;
393 }
394 
395 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
396 #include "selftests/huge_gem_object.c"
397 #include "selftests/huge_pages.c"
398 #include "selftests/i915_gem_object.c"
399 #include "selftests/i915_gem_coherency.c"
400 #endif
401