xref: /dragonfly/sys/dev/drm/ttm/ttm_object.c (revision b29f78b5)
1 /**************************************************************************
2  *
3  * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30 /** @file ttm_ref_object.c
31  *
32  * Base- and reference object implementation for the various
33  * ttm objects. Implements reference counting, minimal security checks
34  * and release on file close.
35  */
36 
37 /**
38  * struct ttm_object_file
39  *
40  * @tdev: Pointer to the ttm_object_device.
41  *
42  * @lock: Lock that protects the ref_list list and the
43  * ref_hash hash tables.
44  *
45  * @ref_list: List of ttm_ref_objects to be destroyed at
46  * file release.
47  *
48  * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
49  * for fast lookup of ref objects given a base object.
50  */
51 
52 #define pr_fmt(fmt) "[TTM] " fmt
53 
54 #include <drm/drmP.h>
55 #include <drm/ttm/ttm_object.h>
56 #include <drm/ttm/ttm_module.h>
57 #include <linux/export.h>
58 
59 struct ttm_object_file {
60 	struct ttm_object_device *tdev;
61 	struct lock lock;
62 	struct list_head ref_list;
63 	struct drm_open_hash ref_hash[TTM_REF_NUM];
64 	struct kref refcount;
65 };
66 
67 /**
68  * struct ttm_object_device
69  *
70  * @object_lock: lock that protects the object_hash hash table.
71  *
72  * @object_hash: hash table for fast lookup of object global names.
73  *
74  * @object_count: Per device object count.
75  *
76  * This is the per-device data structure needed for ttm object management.
77  */
78 
79 struct ttm_object_device {
80 	struct lock object_lock;
81 	struct drm_open_hash object_hash;
82 	atomic_t object_count;
83 	struct ttm_mem_global *mem_glob;
84 };
85 
86 /**
87  * struct ttm_ref_object
88  *
89  * @hash: Hash entry for the per-file object reference hash.
90  *
91  * @head: List entry for the per-file list of ref-objects.
92  *
93  * @kref: Ref count.
94  *
95  * @obj: Base object this ref object is referencing.
96  *
97  * @ref_type: Type of ref object.
98  *
99  * This is similar to an idr object, but it also has a hash table entry
100  * that allows lookup with a pointer to the referenced object as a key. In
101  * that way, one can easily detect whether a base object is referenced by
102  * a particular ttm_object_file. It also carries a ref count to avoid creating
103  * multiple ref objects if a ttm_object_file references the same base
104  * object more than once.
105  */
106 
107 struct ttm_ref_object {
108 	struct drm_hash_item hash;
109 	struct list_head head;
110 	struct kref kref;
111 	enum ttm_ref_type ref_type;
112 	struct ttm_base_object *obj;
113 	struct ttm_object_file *tfile;
114 };
115 
116 static inline struct ttm_object_file *
117 ttm_object_file_ref(struct ttm_object_file *tfile)
118 {
119 	kref_get(&tfile->refcount);
120 	return tfile;
121 }
122 
123 static void ttm_object_file_destroy(struct kref *kref)
124 {
125 	struct ttm_object_file *tfile =
126 		container_of(kref, struct ttm_object_file, refcount);
127 
128 	drm_free(tfile, M_DRM);
129 }
130 
131 
132 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
133 {
134 	struct ttm_object_file *tfile = *p_tfile;
135 
136 	*p_tfile = NULL;
137 	kref_put(&tfile->refcount, ttm_object_file_destroy);
138 }
139 
140 
141 int ttm_base_object_init(struct ttm_object_file *tfile,
142 			 struct ttm_base_object *base,
143 			 bool shareable,
144 			 enum ttm_object_type object_type,
145 			 void (*refcount_release) (struct ttm_base_object **),
146 			 void (*ref_obj_release) (struct ttm_base_object *,
147 						  enum ttm_ref_type ref_type))
148 {
149 	struct ttm_object_device *tdev = tfile->tdev;
150 	int ret;
151 
152 	base->shareable = shareable;
153 	base->tfile = ttm_object_file_ref(tfile);
154 	base->refcount_release = refcount_release;
155 	base->ref_obj_release = ref_obj_release;
156 	base->object_type = object_type;
157 	kref_init(&base->refcount);
158 	lockinit(&tdev->object_lock, "ttmbao", 0, LK_CANRECURSE);
159 	lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
160 	ret = drm_ht_just_insert_please(&tdev->object_hash,
161 					    &base->hash,
162 					    (unsigned long)base, 31, 0, 0);
163 	lockmgr(&tdev->object_lock, LK_RELEASE);
164 	if (unlikely(ret != 0))
165 		goto out_err0;
166 
167 	ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
168 	if (unlikely(ret != 0))
169 		goto out_err1;
170 
171 	ttm_base_object_unref(&base);
172 
173 	return 0;
174 out_err1:
175 	lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
176 	(void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
177 	lockmgr(&tdev->object_lock, LK_RELEASE);
178 out_err0:
179 	return ret;
180 }
181 EXPORT_SYMBOL(ttm_base_object_init);
182 
183 static void ttm_release_base(struct kref *kref)
184 {
185 	struct ttm_base_object *base =
186 	    container_of(kref, struct ttm_base_object, refcount);
187 	struct ttm_object_device *tdev = base->tfile->tdev;
188 
189 	if (atomic_read(&kref->refcount)) {
190 		lockmgr(&tdev->object_lock, LK_RELEASE);
191 		return;
192 	}
193 	(void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
194 	lockmgr(&tdev->object_lock, LK_RELEASE);
195 
196 	/*
197 	 * Note: We don't use synchronize_rcu() here because it's far
198 	 * too slow. It's up to the user to free the object using
199 	 * call_rcu() or ttm_base_object_kfree().
200 	 */
201 
202 	if (base->refcount_release) {
203 		ttm_object_file_unref(&base->tfile);
204 		base->refcount_release(&base);
205 	}
206 }
207 
208 void ttm_base_object_unref(struct ttm_base_object **p_base)
209 {
210 	struct ttm_base_object *base = *p_base;
211 	struct ttm_object_device *tdev = base->tfile->tdev;
212 
213 	*p_base = NULL;
214 
215 	lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
216 	if (kref_put(&base->refcount, ttm_release_base) == 0) {
217 		lockmgr(&tdev->object_lock, LK_RELEASE);
218 	}
219 }
220 EXPORT_SYMBOL(ttm_base_object_unref);
221 
222 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
223 					       uint32_t key)
224 {
225 	struct ttm_object_device *tdev = tfile->tdev;
226 	struct ttm_base_object *base;
227 	struct drm_hash_item *hash;
228 	int ret;
229 
230 	lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
231 	ret = drm_ht_find_item(&tdev->object_hash, key, &hash);
232 
233 	if (likely(ret == 0)) {
234 		base = drm_hash_entry(hash, struct ttm_base_object, hash);
235 		ret = kref_get_unless_zero(&base->refcount) ? 0 : -EINVAL;
236 	}
237 	lockmgr(&tdev->object_lock, LK_RELEASE);
238 
239 	if (unlikely(ret != 0))
240 		return NULL;
241 
242 	if (tfile != base->tfile && !base->shareable) {
243 		kprintf("[TTM] Attempted access of non-shareable object %p\n",
244 		    base);
245 		ttm_base_object_unref(&base);
246 		return NULL;
247 	}
248 
249 	return base;
250 }
251 EXPORT_SYMBOL(ttm_base_object_lookup);
252 
253 int ttm_ref_object_add(struct ttm_object_file *tfile,
254 		       struct ttm_base_object *base,
255 		       enum ttm_ref_type ref_type, bool *existed)
256 {
257 	struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
258 	struct ttm_ref_object *ref;
259 	struct drm_hash_item *hash;
260 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
261 	int ret = -EINVAL;
262 
263 	if (existed != NULL)
264 		*existed = true;
265 
266 	while (ret == -EINVAL) {
267 		lockmgr(&tfile->lock, LK_EXCLUSIVE);
268 		ret = drm_ht_find_item(ht, base->hash.key, &hash);
269 
270 		if (ret == 0) {
271 			ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
272 			kref_get(&ref->kref);
273 			lockmgr(&tfile->lock, LK_RELEASE);
274 			break;
275 		}
276 
277 		lockmgr(&tfile->lock, LK_RELEASE);
278 		ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
279 					   false, false);
280 		if (unlikely(ret != 0))
281 			return ret;
282 		ref = kmalloc(sizeof(*ref), M_DRM, M_WAITOK);
283 		if (unlikely(ref == NULL)) {
284 			ttm_mem_global_free(mem_glob, sizeof(*ref));
285 			return -ENOMEM;
286 		}
287 
288 		ref->hash.key = base->hash.key;
289 		ref->obj = base;
290 		ref->tfile = tfile;
291 		ref->ref_type = ref_type;
292 		kref_init(&ref->kref);
293 
294 		lockmgr(&tfile->lock, LK_EXCLUSIVE);
295 		ret = drm_ht_insert_item(ht, &ref->hash);
296 
297 		if (likely(ret == 0)) {
298 			list_add_tail(&ref->head, &tfile->ref_list);
299 			kref_get(&base->refcount);
300 			lockmgr(&tfile->lock, LK_RELEASE);
301 			if (existed != NULL)
302 				*existed = false;
303 			break;
304 		}
305 
306 		lockmgr(&tfile->lock, LK_RELEASE);
307 		BUG_ON(ret != -EINVAL);
308 
309 		ttm_mem_global_free(mem_glob, sizeof(*ref));
310 		drm_free(ref, M_DRM);
311 	}
312 
313 	return ret;
314 }
315 EXPORT_SYMBOL(ttm_ref_object_add);
316 
317 static void ttm_ref_object_release(struct kref *kref)
318 {
319 	struct ttm_ref_object *ref =
320 	    container_of(kref, struct ttm_ref_object, kref);
321 	struct ttm_base_object *base = ref->obj;
322 	struct ttm_object_file *tfile = ref->tfile;
323 	struct drm_open_hash *ht;
324 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
325 
326 	ht = &tfile->ref_hash[ref->ref_type];
327 	(void)drm_ht_remove_item(ht, &ref->hash);
328 	list_del(&ref->head);
329 	lockmgr(&tfile->lock, LK_RELEASE);
330 
331 	if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
332 		base->ref_obj_release(base, ref->ref_type);
333 
334 	ttm_base_object_unref(&ref->obj);
335 	ttm_mem_global_free(mem_glob, sizeof(*ref));
336 	drm_free(ref, M_DRM);
337 	lockmgr(&tfile->lock, LK_EXCLUSIVE);
338 }
339 
340 int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
341 			      unsigned long key, enum ttm_ref_type ref_type)
342 {
343 	struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
344 	struct ttm_ref_object *ref;
345 	struct drm_hash_item *hash;
346 	int ret;
347 
348 	lockmgr(&tfile->lock, LK_EXCLUSIVE);
349 	ret = drm_ht_find_item(ht, key, &hash);
350 	if (unlikely(ret != 0)) {
351 		lockmgr(&tfile->lock, LK_RELEASE);
352 		return -EINVAL;
353 	}
354 	ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
355 	kref_put(&ref->kref, ttm_ref_object_release);
356 	lockmgr(&tfile->lock, LK_RELEASE);
357 	return 0;
358 }
359 EXPORT_SYMBOL(ttm_ref_object_base_unref);
360 
361 void ttm_object_file_release(struct ttm_object_file **p_tfile)
362 {
363 	struct ttm_ref_object *ref;
364 	struct list_head *list;
365 	unsigned int i;
366 	struct ttm_object_file *tfile = *p_tfile;
367 
368 	*p_tfile = NULL;
369 	lockmgr(&tfile->lock, LK_EXCLUSIVE);
370 
371 	/*
372 	 * Since we release the lock within the loop, we have to
373 	 * restart it from the beginning each time.
374 	 */
375 
376 	while (!list_empty(&tfile->ref_list)) {
377 		list = tfile->ref_list.next;
378 		ref = list_entry(list, struct ttm_ref_object, head);
379 		ttm_ref_object_release(&ref->kref);
380 	}
381 
382 	for (i = 0; i < TTM_REF_NUM; ++i)
383 		drm_ht_remove(&tfile->ref_hash[i]);
384 
385 	lockmgr(&tfile->lock, LK_RELEASE);
386 	ttm_object_file_unref(&tfile);
387 }
388 EXPORT_SYMBOL(ttm_object_file_release);
389 
390 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
391 					     unsigned int hash_order)
392 {
393 	struct ttm_object_file *tfile;
394 	unsigned int i;
395 	unsigned int j = 0;
396 	int ret;
397 
398 	tfile = kmalloc(sizeof(*tfile), M_DRM, M_WAITOK);
399 	if (unlikely(tfile == NULL))
400 		return NULL;
401 
402 	lockinit(&tfile->lock, "ttmfo", 0, LK_CANRECURSE);
403 	tfile->tdev = tdev;
404 	kref_init(&tfile->refcount);
405 	INIT_LIST_HEAD(&tfile->ref_list);
406 
407 	for (i = 0; i < TTM_REF_NUM; ++i) {
408 		ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
409 		if (ret) {
410 			j = i;
411 			goto out_err;
412 		}
413 	}
414 
415 	return tfile;
416 out_err:
417 	for (i = 0; i < j; ++i)
418 		drm_ht_remove(&tfile->ref_hash[i]);
419 
420 	drm_free(tfile, M_DRM);
421 
422 	return NULL;
423 }
424 EXPORT_SYMBOL(ttm_object_file_init);
425 
426 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
427 						 *mem_glob,
428 						 unsigned int hash_order)
429 {
430 	struct ttm_object_device *tdev;
431 	int ret;
432 
433 	tdev = kmalloc(sizeof(*tdev), M_DRM, M_WAITOK);
434 	if (unlikely(tdev == NULL))
435 		return NULL;
436 
437 	tdev->mem_glob = mem_glob;
438 	lockinit(&tdev->object_lock, "ttmdo", 0, LK_CANRECURSE);
439 	atomic_set(&tdev->object_count, 0);
440 	ret = drm_ht_create(&tdev->object_hash, hash_order);
441 
442 	if (likely(ret == 0))
443 		return tdev;
444 
445 	drm_free(tdev, M_DRM);
446 	return NULL;
447 }
448 EXPORT_SYMBOL(ttm_object_device_init);
449 
450 void ttm_object_device_release(struct ttm_object_device **p_tdev)
451 {
452 	struct ttm_object_device *tdev = *p_tdev;
453 
454 	*p_tdev = NULL;
455 
456 	lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
457 	drm_ht_remove(&tdev->object_hash);
458 	lockmgr(&tdev->object_lock, LK_RELEASE);
459 
460 	drm_free(tdev, M_DRM);
461 }
462 EXPORT_SYMBOL(ttm_object_device_release);
463