xref: /freebsd/sys/dev/drm2/ttm/ttm_object.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
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 #include <sys/cdefs.h>
39 /**
40  * struct ttm_object_file
41  *
42  * @tdev: Pointer to the ttm_object_device.
43  *
44  * @lock: Lock that protects the ref_list list and the
45  * ref_hash hash tables.
46  *
47  * @ref_list: List of ttm_ref_objects to be destroyed at
48  * file release.
49  *
50  * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
51  * for fast lookup of ref objects given a base object.
52  */
53 
54 #define pr_fmt(fmt) "[TTM] " fmt
55 
56 #include <dev/drm2/drmP.h>
57 #include <dev/drm2/drm.h>
58 #include <sys/rwlock.h>
59 #include <dev/drm2/ttm/ttm_object.h>
60 #include <dev/drm2/ttm/ttm_module.h>
61 
62 struct ttm_object_file {
63 	struct ttm_object_device *tdev;
64 	struct rwlock lock;
65 	struct list_head ref_list;
66 	struct drm_open_hash ref_hash[TTM_REF_NUM];
67 	u_int refcount;
68 };
69 
70 /**
71  * struct ttm_object_device
72  *
73  * @object_lock: lock that protects the object_hash hash table.
74  *
75  * @object_hash: hash table for fast lookup of object global names.
76  *
77  * @object_count: Per device object count.
78  *
79  * This is the per-device data structure needed for ttm object management.
80  */
81 
82 struct ttm_object_device {
83 	struct rwlock object_lock;
84 	struct drm_open_hash object_hash;
85 	atomic_t object_count;
86 	struct ttm_mem_global *mem_glob;
87 };
88 
89 /**
90  * struct ttm_ref_object
91  *
92  * @hash: Hash entry for the per-file object reference hash.
93  *
94  * @head: List entry for the per-file list of ref-objects.
95  *
96  * @kref: Ref count.
97  *
98  * @obj: Base object this ref object is referencing.
99  *
100  * @ref_type: Type of ref object.
101  *
102  * This is similar to an idr object, but it also has a hash table entry
103  * that allows lookup with a pointer to the referenced object as a key. In
104  * that way, one can easily detect whether a base object is referenced by
105  * a particular ttm_object_file. It also carries a ref count to avoid creating
106  * multiple ref objects if a ttm_object_file references the same base
107  * object more than once.
108  */
109 
110 struct ttm_ref_object {
111 	struct drm_hash_item hash;
112 	struct list_head head;
113 	u_int kref;
114 	enum ttm_ref_type ref_type;
115 	struct ttm_base_object *obj;
116 	struct ttm_object_file *tfile;
117 };
118 
119 MALLOC_DEFINE(M_TTM_OBJ_FILE, "ttm_obj_file", "TTM File Objects");
120 
121 static inline struct ttm_object_file *
122 ttm_object_file_ref(struct ttm_object_file *tfile)
123 {
124 	refcount_acquire(&tfile->refcount);
125 	return tfile;
126 }
127 
128 static void ttm_object_file_destroy(struct ttm_object_file *tfile)
129 {
130 
131 	free(tfile, M_TTM_OBJ_FILE);
132 }
133 
134 
135 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
136 {
137 	struct ttm_object_file *tfile = *p_tfile;
138 
139 	*p_tfile = NULL;
140 	if (refcount_release(&tfile->refcount))
141 		ttm_object_file_destroy(tfile);
142 }
143 
144 
145 int ttm_base_object_init(struct ttm_object_file *tfile,
146 			 struct ttm_base_object *base,
147 			 bool shareable,
148 			 enum ttm_object_type object_type,
149 			 void (*rcount_release) (struct ttm_base_object **),
150 			 void (*ref_obj_release) (struct ttm_base_object *,
151 						  enum ttm_ref_type ref_type))
152 {
153 	struct ttm_object_device *tdev = tfile->tdev;
154 	int ret;
155 
156 	base->shareable = shareable;
157 	base->tfile = ttm_object_file_ref(tfile);
158 	base->refcount_release = rcount_release;
159 	base->ref_obj_release = ref_obj_release;
160 	base->object_type = object_type;
161 	refcount_init(&base->refcount, 1);
162 	rw_init(&tdev->object_lock, "ttmbao");
163 	rw_wlock(&tdev->object_lock);
164 	ret = drm_ht_just_insert_please(&tdev->object_hash,
165 					    &base->hash,
166 					    (unsigned long)base, 31, 0, 0);
167 	rw_wunlock(&tdev->object_lock);
168 	if (unlikely(ret != 0))
169 		goto out_err0;
170 
171 	ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
172 	if (unlikely(ret != 0))
173 		goto out_err1;
174 
175 	ttm_base_object_unref(&base);
176 
177 	return 0;
178 out_err1:
179 	rw_wlock(&tdev->object_lock);
180 	(void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
181 	rw_wunlock(&tdev->object_lock);
182 out_err0:
183 	return ret;
184 }
185 
186 static void ttm_release_base(struct ttm_base_object *base)
187 {
188 	struct ttm_object_device *tdev = base->tfile->tdev;
189 
190 	(void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
191 	rw_wunlock(&tdev->object_lock);
192 	/*
193 	 * Note: We don't use synchronize_rcu() here because it's far
194 	 * too slow. It's up to the user to free the object using
195 	 * call_rcu() or ttm_base_object_kfree().
196 	 */
197 
198 	if (base->refcount_release) {
199 		ttm_object_file_unref(&base->tfile);
200 		base->refcount_release(&base);
201 	}
202 	rw_wlock(&tdev->object_lock);
203 }
204 
205 void ttm_base_object_unref(struct ttm_base_object **p_base)
206 {
207 	struct ttm_base_object *base = *p_base;
208 	struct ttm_object_device *tdev = base->tfile->tdev;
209 
210 	*p_base = NULL;
211 
212 	/*
213 	 * Need to take the lock here to avoid racing with
214 	 * users trying to look up the object.
215 	 */
216 
217 	rw_wlock(&tdev->object_lock);
218 	if (refcount_release(&base->refcount))
219 		ttm_release_base(base);
220 	rw_wunlock(&tdev->object_lock);
221 }
222 
223 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
224 					       uint32_t key)
225 {
226 	struct ttm_object_device *tdev = tfile->tdev;
227 	struct ttm_base_object *base;
228 	struct drm_hash_item *hash;
229 	int ret;
230 
231 	rw_rlock(&tdev->object_lock);
232 	ret = drm_ht_find_item(&tdev->object_hash, key, &hash);
233 
234 	if (ret == 0) {
235 		base = drm_hash_entry(hash, struct ttm_base_object, hash);
236 		refcount_acquire(&base->refcount);
237 	}
238 	rw_runlock(&tdev->object_lock);
239 
240 	if (unlikely(ret != 0))
241 		return NULL;
242 
243 	if (tfile != base->tfile && !base->shareable) {
244 		printf("[TTM] Attempted access of non-shareable object %p\n",
245 		    base);
246 		ttm_base_object_unref(&base);
247 		return NULL;
248 	}
249 
250 	return base;
251 }
252 
253 MALLOC_DEFINE(M_TTM_OBJ_REF, "ttm_obj_ref", "TTM Ref Objects");
254 
255 int ttm_ref_object_add(struct ttm_object_file *tfile,
256 		       struct ttm_base_object *base,
257 		       enum ttm_ref_type ref_type, bool *existed)
258 {
259 	struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
260 	struct ttm_ref_object *ref;
261 	struct drm_hash_item *hash;
262 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
263 	int ret = -EINVAL;
264 
265 	if (existed != NULL)
266 		*existed = true;
267 
268 	while (ret == -EINVAL) {
269 		rw_rlock(&tfile->lock);
270 		ret = drm_ht_find_item(ht, base->hash.key, &hash);
271 
272 		if (ret == 0) {
273 			ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
274 			refcount_acquire(&ref->kref);
275 			rw_runlock(&tfile->lock);
276 			break;
277 		}
278 
279 		rw_runlock(&tfile->lock);
280 		ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
281 					   false, false);
282 		if (unlikely(ret != 0))
283 			return ret;
284 		ref = malloc(sizeof(*ref), M_TTM_OBJ_REF, M_WAITOK);
285 		if (unlikely(ref == NULL)) {
286 			ttm_mem_global_free(mem_glob, sizeof(*ref));
287 			return -ENOMEM;
288 		}
289 
290 		ref->hash.key = base->hash.key;
291 		ref->obj = base;
292 		ref->tfile = tfile;
293 		ref->ref_type = ref_type;
294 		refcount_init(&ref->kref, 1);
295 
296 		rw_wlock(&tfile->lock);
297 		ret = drm_ht_insert_item(ht, &ref->hash);
298 
299 		if (ret == 0) {
300 			list_add_tail(&ref->head, &tfile->ref_list);
301 			refcount_acquire(&base->refcount);
302 			rw_wunlock(&tfile->lock);
303 			if (existed != NULL)
304 				*existed = false;
305 			break;
306 		}
307 
308 		rw_wunlock(&tfile->lock);
309 		MPASS(ret == -EINVAL);
310 
311 		ttm_mem_global_free(mem_glob, sizeof(*ref));
312 		free(ref, M_TTM_OBJ_REF);
313 	}
314 
315 	return ret;
316 }
317 
318 static void ttm_ref_object_release(struct ttm_ref_object *ref)
319 {
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 	rw_wunlock(&tfile->lock);
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 	free(ref, M_TTM_OBJ_REF);
336 	rw_wlock(&tfile->lock);
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 	rw_wlock(&tfile->lock);
348 	ret = drm_ht_find_item(ht, key, &hash);
349 	if (unlikely(ret != 0)) {
350 		rw_wunlock(&tfile->lock);
351 		return -EINVAL;
352 	}
353 	ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
354 	if (refcount_release(&ref->kref))
355 		ttm_ref_object_release(ref);
356 	rw_wunlock(&tfile->lock);
357 	return 0;
358 }
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 	rw_wlock(&tfile->lock);
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);
379 	}
380 
381 	for (i = 0; i < TTM_REF_NUM; ++i)
382 		drm_ht_remove(&tfile->ref_hash[i]);
383 
384 	rw_wunlock(&tfile->lock);
385 	ttm_object_file_unref(&tfile);
386 }
387 
388 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
389 					     unsigned int hash_order)
390 {
391 	struct ttm_object_file *tfile;
392 	unsigned int i;
393 	unsigned int j = 0;
394 	int ret;
395 
396 	tfile = malloc(sizeof(*tfile), M_TTM_OBJ_FILE, M_WAITOK);
397 	rw_init(&tfile->lock, "ttmfo");
398 	tfile->tdev = tdev;
399 	refcount_init(&tfile->refcount, 1);
400 	INIT_LIST_HEAD(&tfile->ref_list);
401 
402 	for (i = 0; i < TTM_REF_NUM; ++i) {
403 		ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
404 		if (ret) {
405 			j = i;
406 			goto out_err;
407 		}
408 	}
409 
410 	return tfile;
411 out_err:
412 	for (i = 0; i < j; ++i)
413 		drm_ht_remove(&tfile->ref_hash[i]);
414 
415 	free(tfile, M_TTM_OBJ_FILE);
416 
417 	return NULL;
418 }
419 
420 MALLOC_DEFINE(M_TTM_OBJ_DEV, "ttm_obj_dev", "TTM Device Objects");
421 
422 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
423 						 *mem_glob,
424 						 unsigned int hash_order)
425 {
426 	struct ttm_object_device *tdev;
427 	int ret;
428 
429 	tdev = malloc(sizeof(*tdev), M_TTM_OBJ_DEV, M_WAITOK);
430 	tdev->mem_glob = mem_glob;
431 	rw_init(&tdev->object_lock, "ttmdo");
432 	atomic_set(&tdev->object_count, 0);
433 	ret = drm_ht_create(&tdev->object_hash, hash_order);
434 
435 	if (ret == 0)
436 		return tdev;
437 
438 	free(tdev, M_TTM_OBJ_DEV);
439 	return NULL;
440 }
441 
442 void ttm_object_device_release(struct ttm_object_device **p_tdev)
443 {
444 	struct ttm_object_device *tdev = *p_tdev;
445 
446 	*p_tdev = NULL;
447 
448 	rw_wlock(&tdev->object_lock);
449 	drm_ht_remove(&tdev->object_hash);
450 	rw_wunlock(&tdev->object_lock);
451 
452 	free(tdev, M_TTM_OBJ_DEV);
453 }
454