xref: /dragonfly/sys/dev/drm/ttm/ttm_object.c (revision 65cc0652)
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 	kfree(tfile);
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 		pr_err("Attempted access of non-shareable object %p\n", base);
244 		ttm_base_object_unref(&base);
245 		return NULL;
246 	}
247 
248 	return base;
249 }
250 EXPORT_SYMBOL(ttm_base_object_lookup);
251 
252 int ttm_ref_object_add(struct ttm_object_file *tfile,
253 		       struct ttm_base_object *base,
254 		       enum ttm_ref_type ref_type, bool *existed)
255 {
256 	struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
257 	struct ttm_ref_object *ref;
258 	struct drm_hash_item *hash;
259 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
260 	int ret = -EINVAL;
261 
262 	if (existed != NULL)
263 		*existed = true;
264 
265 	while (ret == -EINVAL) {
266 		lockmgr(&tfile->lock, LK_EXCLUSIVE);
267 		ret = drm_ht_find_item(ht, base->hash.key, &hash);
268 
269 		if (ret == 0) {
270 			ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
271 			kref_get(&ref->kref);
272 			lockmgr(&tfile->lock, LK_RELEASE);
273 			break;
274 		}
275 
276 		lockmgr(&tfile->lock, LK_RELEASE);
277 		ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
278 					   false, false);
279 		if (unlikely(ret != 0))
280 			return ret;
281 		ref = kmalloc(sizeof(*ref), M_DRM, M_WAITOK);
282 		if (unlikely(ref == NULL)) {
283 			ttm_mem_global_free(mem_glob, sizeof(*ref));
284 			return -ENOMEM;
285 		}
286 
287 		ref->hash.key = base->hash.key;
288 		ref->obj = base;
289 		ref->tfile = tfile;
290 		ref->ref_type = ref_type;
291 		kref_init(&ref->kref);
292 
293 		lockmgr(&tfile->lock, LK_EXCLUSIVE);
294 		ret = drm_ht_insert_item(ht, &ref->hash);
295 
296 		if (likely(ret == 0)) {
297 			list_add_tail(&ref->head, &tfile->ref_list);
298 			kref_get(&base->refcount);
299 			lockmgr(&tfile->lock, LK_RELEASE);
300 			if (existed != NULL)
301 				*existed = false;
302 			break;
303 		}
304 
305 		lockmgr(&tfile->lock, LK_RELEASE);
306 		BUG_ON(ret != -EINVAL);
307 
308 		ttm_mem_global_free(mem_glob, sizeof(*ref));
309 		kfree(ref);
310 	}
311 
312 	return ret;
313 }
314 EXPORT_SYMBOL(ttm_ref_object_add);
315 
316 static void ttm_ref_object_release(struct kref *kref)
317 {
318 	struct ttm_ref_object *ref =
319 	    container_of(kref, struct ttm_ref_object, kref);
320 	struct ttm_base_object *base = ref->obj;
321 	struct ttm_object_file *tfile = ref->tfile;
322 	struct drm_open_hash *ht;
323 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
324 
325 	ht = &tfile->ref_hash[ref->ref_type];
326 	(void)drm_ht_remove_item(ht, &ref->hash);
327 	list_del(&ref->head);
328 	lockmgr(&tfile->lock, LK_RELEASE);
329 
330 	if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
331 		base->ref_obj_release(base, ref->ref_type);
332 
333 	ttm_base_object_unref(&ref->obj);
334 	ttm_mem_global_free(mem_glob, sizeof(*ref));
335 	kfree(ref);
336 	lockmgr(&tfile->lock, LK_EXCLUSIVE);
337 }
338 
339 int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
340 			      unsigned long key, enum ttm_ref_type ref_type)
341 {
342 	struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
343 	struct ttm_ref_object *ref;
344 	struct drm_hash_item *hash;
345 	int ret;
346 
347 	lockmgr(&tfile->lock, LK_EXCLUSIVE);
348 	ret = drm_ht_find_item(ht, key, &hash);
349 	if (unlikely(ret != 0)) {
350 		lockmgr(&tfile->lock, LK_RELEASE);
351 		return -EINVAL;
352 	}
353 	ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
354 	kref_put(&ref->kref, ttm_ref_object_release);
355 	lockmgr(&tfile->lock, LK_RELEASE);
356 	return 0;
357 }
358 EXPORT_SYMBOL(ttm_ref_object_base_unref);
359 
360 void ttm_object_file_release(struct ttm_object_file **p_tfile)
361 {
362 	struct ttm_ref_object *ref;
363 	struct list_head *list;
364 	unsigned int i;
365 	struct ttm_object_file *tfile = *p_tfile;
366 
367 	*p_tfile = NULL;
368 	lockmgr(&tfile->lock, LK_EXCLUSIVE);
369 
370 	/*
371 	 * Since we release the lock within the loop, we have to
372 	 * restart it from the beginning each time.
373 	 */
374 
375 	while (!list_empty(&tfile->ref_list)) {
376 		list = tfile->ref_list.next;
377 		ref = list_entry(list, struct ttm_ref_object, head);
378 		ttm_ref_object_release(&ref->kref);
379 	}
380 
381 	for (i = 0; i < TTM_REF_NUM; ++i)
382 		drm_ht_remove(&tfile->ref_hash[i]);
383 
384 	lockmgr(&tfile->lock, LK_RELEASE);
385 	ttm_object_file_unref(&tfile);
386 }
387 EXPORT_SYMBOL(ttm_object_file_release);
388 
389 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
390 					     unsigned int hash_order)
391 {
392 	struct ttm_object_file *tfile;
393 	unsigned int i;
394 	unsigned int j = 0;
395 	int ret;
396 
397 	tfile = kmalloc(sizeof(*tfile), M_DRM, M_WAITOK);
398 	if (unlikely(tfile == NULL))
399 		return NULL;
400 
401 	lockinit(&tfile->lock, "ttmfo", 0, LK_CANRECURSE);
402 	tfile->tdev = tdev;
403 	kref_init(&tfile->refcount);
404 	INIT_LIST_HEAD(&tfile->ref_list);
405 
406 	for (i = 0; i < TTM_REF_NUM; ++i) {
407 		ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
408 		if (ret) {
409 			j = i;
410 			goto out_err;
411 		}
412 	}
413 
414 	return tfile;
415 out_err:
416 	for (i = 0; i < j; ++i)
417 		drm_ht_remove(&tfile->ref_hash[i]);
418 
419 	kfree(tfile);
420 
421 	return NULL;
422 }
423 EXPORT_SYMBOL(ttm_object_file_init);
424 
425 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
426 						 *mem_glob,
427 						 unsigned int hash_order)
428 {
429 	struct ttm_object_device *tdev;
430 	int ret;
431 
432 	tdev = kmalloc(sizeof(*tdev), M_DRM, M_WAITOK);
433 	if (unlikely(tdev == NULL))
434 		return NULL;
435 
436 	tdev->mem_glob = mem_glob;
437 	lockinit(&tdev->object_lock, "ttmdo", 0, LK_CANRECURSE);
438 	atomic_set(&tdev->object_count, 0);
439 	ret = drm_ht_create(&tdev->object_hash, hash_order);
440 
441 	if (likely(ret == 0))
442 		return tdev;
443 
444 	kfree(tdev);
445 	return NULL;
446 }
447 EXPORT_SYMBOL(ttm_object_device_init);
448 
449 void ttm_object_device_release(struct ttm_object_device **p_tdev)
450 {
451 	struct ttm_object_device *tdev = *p_tdev;
452 
453 	*p_tdev = NULL;
454 
455 	lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
456 	drm_ht_remove(&tdev->object_hash);
457 	lockmgr(&tdev->object_lock, LK_RELEASE);
458 
459 	kfree(tdev);
460 }
461 EXPORT_SYMBOL(ttm_object_device_release);
462