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 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_object.c 247835 2013-03-05 09:49:34Z kib $ 37 */ 38 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 <drm/drmP.h> 57 #include <dev/drm/ttm/ttm_object.h> 58 #include <dev/drm/ttm/ttm_module.h> 59 60 struct ttm_object_file { 61 struct ttm_object_device *tdev; 62 struct lock lock; 63 struct list_head ref_list; 64 struct drm_open_hash ref_hash[TTM_REF_NUM]; 65 u_int refcount; 66 }; 67 68 /** 69 * struct ttm_object_device 70 * 71 * @object_lock: lock that protects the object_hash hash table. 72 * 73 * @object_hash: hash table for fast lookup of object global names. 74 * 75 * @object_count: Per device object count. 76 * 77 * This is the per-device data structure needed for ttm object management. 78 */ 79 80 struct ttm_object_device { 81 struct lock object_lock; 82 struct drm_open_hash object_hash; 83 atomic_t object_count; 84 struct ttm_mem_global *mem_glob; 85 }; 86 87 /** 88 * struct ttm_ref_object 89 * 90 * @hash: Hash entry for the per-file object reference hash. 91 * 92 * @head: List entry for the per-file list of ref-objects. 93 * 94 * @kref: Ref count. 95 * 96 * @obj: Base object this ref object is referencing. 97 * 98 * @ref_type: Type of ref object. 99 * 100 * This is similar to an idr object, but it also has a hash table entry 101 * that allows lookup with a pointer to the referenced object as a key. In 102 * that way, one can easily detect whether a base object is referenced by 103 * a particular ttm_object_file. It also carries a ref count to avoid creating 104 * multiple ref objects if a ttm_object_file references the same base 105 * object more than once. 106 */ 107 108 struct ttm_ref_object { 109 struct drm_hash_item hash; 110 struct list_head head; 111 u_int kref; 112 enum ttm_ref_type ref_type; 113 struct ttm_base_object *obj; 114 struct ttm_object_file *tfile; 115 }; 116 117 MALLOC_DEFINE(M_TTM_OBJ_FILE, "ttm_obj_file", "TTM File Objects"); 118 119 static inline struct ttm_object_file * 120 ttm_object_file_ref(struct ttm_object_file *tfile) 121 { 122 refcount_acquire(&tfile->refcount); 123 return tfile; 124 } 125 126 static void ttm_object_file_destroy(struct ttm_object_file *tfile) 127 { 128 129 drm_free(tfile, M_TTM_OBJ_FILE); 130 } 131 132 133 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile) 134 { 135 struct ttm_object_file *tfile = *p_tfile; 136 137 *p_tfile = NULL; 138 if (refcount_release(&tfile->refcount)) 139 ttm_object_file_destroy(tfile); 140 } 141 142 143 int ttm_base_object_init(struct ttm_object_file *tfile, 144 struct ttm_base_object *base, 145 bool shareable, 146 enum ttm_object_type object_type, 147 void (*rcount_release) (struct ttm_base_object **), 148 void (*ref_obj_release) (struct ttm_base_object *, 149 enum ttm_ref_type ref_type)) 150 { 151 struct ttm_object_device *tdev = tfile->tdev; 152 int ret; 153 154 base->shareable = shareable; 155 base->tfile = ttm_object_file_ref(tfile); 156 base->refcount_release = rcount_release; 157 base->ref_obj_release = ref_obj_release; 158 base->object_type = object_type; 159 refcount_init(&base->refcount, 1); 160 lockinit(&tdev->object_lock, "ttmbao", 0, LK_CANRECURSE); 161 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 162 ret = drm_ht_just_insert_please(&tdev->object_hash, 163 &base->hash, 164 (unsigned long)base, 31, 0, 0); 165 lockmgr(&tdev->object_lock, LK_RELEASE); 166 if (unlikely(ret != 0)) 167 goto out_err0; 168 169 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL); 170 if (unlikely(ret != 0)) 171 goto out_err1; 172 173 ttm_base_object_unref(&base); 174 175 return 0; 176 out_err1: 177 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 178 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash); 179 lockmgr(&tdev->object_lock, LK_RELEASE); 180 out_err0: 181 return ret; 182 } 183 184 static void ttm_release_base(struct ttm_base_object *base) 185 { 186 struct ttm_object_device *tdev = base->tfile->tdev; 187 188 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash); 189 lockmgr(&tdev->object_lock, LK_RELEASE); 190 /* 191 * Note: We don't use synchronize_rcu() here because it's far 192 * too slow. It's up to the user to free the object using 193 * call_rcu() or ttm_base_object_kfree(). 194 */ 195 196 if (base->refcount_release) { 197 ttm_object_file_unref(&base->tfile); 198 base->refcount_release(&base); 199 } 200 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 201 } 202 203 void ttm_base_object_unref(struct ttm_base_object **p_base) 204 { 205 struct ttm_base_object *base = *p_base; 206 struct ttm_object_device *tdev = base->tfile->tdev; 207 208 *p_base = NULL; 209 210 /* 211 * Need to take the lock here to avoid racing with 212 * users trying to look up the object. 213 */ 214 215 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 216 if (refcount_release(&base->refcount)) 217 ttm_release_base(base); 218 lockmgr(&tdev->object_lock, LK_RELEASE); 219 } 220 221 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile, 222 uint32_t key) 223 { 224 struct ttm_object_device *tdev = tfile->tdev; 225 struct ttm_base_object *base; 226 struct drm_hash_item *hash; 227 int ret; 228 229 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 230 ret = drm_ht_find_item(&tdev->object_hash, key, &hash); 231 232 if (ret == 0) { 233 base = drm_hash_entry(hash, struct ttm_base_object, hash); 234 refcount_acquire(&base->refcount); 235 } 236 lockmgr(&tdev->object_lock, LK_RELEASE); 237 238 if (unlikely(ret != 0)) 239 return NULL; 240 241 if (tfile != base->tfile && !base->shareable) { 242 kprintf("[TTM] Attempted access of non-shareable object %p\n", 243 base); 244 ttm_base_object_unref(&base); 245 return NULL; 246 } 247 248 return base; 249 } 250 251 MALLOC_DEFINE(M_TTM_OBJ_REF, "ttm_obj_ref", "TTM Ref Objects"); 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 refcount_acquire(&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_TTM_OBJ_REF, 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 refcount_init(&ref->kref, 1); 293 294 lockmgr(&tfile->lock, LK_EXCLUSIVE); 295 ret = drm_ht_insert_item(ht, &ref->hash); 296 297 if (ret == 0) { 298 list_add_tail(&ref->head, &tfile->ref_list); 299 refcount_acquire(&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 KKASSERT(ret == -EINVAL); 308 309 ttm_mem_global_free(mem_glob, sizeof(*ref)); 310 drm_free(ref, M_TTM_OBJ_REF); 311 } 312 313 return ret; 314 } 315 316 static void ttm_ref_object_release(struct ttm_ref_object *ref) 317 { 318 struct ttm_base_object *base = ref->obj; 319 struct ttm_object_file *tfile = ref->tfile; 320 struct drm_open_hash *ht; 321 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob; 322 323 ht = &tfile->ref_hash[ref->ref_type]; 324 (void)drm_ht_remove_item(ht, &ref->hash); 325 list_del(&ref->head); 326 lockmgr(&tfile->lock, LK_RELEASE); 327 328 if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release) 329 base->ref_obj_release(base, ref->ref_type); 330 331 ttm_base_object_unref(&ref->obj); 332 ttm_mem_global_free(mem_glob, sizeof(*ref)); 333 drm_free(ref, M_TTM_OBJ_REF); 334 lockmgr(&tfile->lock, LK_EXCLUSIVE); 335 } 336 337 int ttm_ref_object_base_unref(struct ttm_object_file *tfile, 338 unsigned long key, enum ttm_ref_type ref_type) 339 { 340 struct drm_open_hash *ht = &tfile->ref_hash[ref_type]; 341 struct ttm_ref_object *ref; 342 struct drm_hash_item *hash; 343 int ret; 344 345 lockmgr(&tfile->lock, LK_EXCLUSIVE); 346 ret = drm_ht_find_item(ht, key, &hash); 347 if (unlikely(ret != 0)) { 348 lockmgr(&tfile->lock, LK_RELEASE); 349 return -EINVAL; 350 } 351 ref = drm_hash_entry(hash, struct ttm_ref_object, hash); 352 if (refcount_release(&ref->kref)) 353 ttm_ref_object_release(ref); 354 lockmgr(&tfile->lock, LK_RELEASE); 355 return 0; 356 } 357 358 void ttm_object_file_release(struct ttm_object_file **p_tfile) 359 { 360 struct ttm_ref_object *ref; 361 struct list_head *list; 362 unsigned int i; 363 struct ttm_object_file *tfile = *p_tfile; 364 365 *p_tfile = NULL; 366 lockmgr(&tfile->lock, LK_EXCLUSIVE); 367 368 /* 369 * Since we release the lock within the loop, we have to 370 * restart it from the beginning each time. 371 */ 372 373 while (!list_empty(&tfile->ref_list)) { 374 list = tfile->ref_list.next; 375 ref = list_entry(list, struct ttm_ref_object, head); 376 ttm_ref_object_release(ref); 377 } 378 379 for (i = 0; i < TTM_REF_NUM; ++i) 380 drm_ht_remove(&tfile->ref_hash[i]); 381 382 lockmgr(&tfile->lock, LK_RELEASE); 383 ttm_object_file_unref(&tfile); 384 } 385 386 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev, 387 unsigned int hash_order) 388 { 389 struct ttm_object_file *tfile; 390 unsigned int i; 391 unsigned int j = 0; 392 int ret; 393 394 tfile = kmalloc(sizeof(*tfile), M_TTM_OBJ_FILE, M_WAITOK); 395 lockinit(&tfile->lock, "ttmfo", 0, LK_CANRECURSE); 396 tfile->tdev = tdev; 397 refcount_init(&tfile->refcount, 1); 398 INIT_LIST_HEAD(&tfile->ref_list); 399 400 for (i = 0; i < TTM_REF_NUM; ++i) { 401 ret = drm_ht_create(&tfile->ref_hash[i], hash_order); 402 if (ret) { 403 j = i; 404 goto out_err; 405 } 406 } 407 408 return tfile; 409 out_err: 410 for (i = 0; i < j; ++i) 411 drm_ht_remove(&tfile->ref_hash[i]); 412 413 drm_free(tfile, M_TTM_OBJ_FILE); 414 415 return NULL; 416 } 417 418 MALLOC_DEFINE(M_TTM_OBJ_DEV, "ttm_obj_dev", "TTM Device Objects"); 419 420 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global 421 *mem_glob, 422 unsigned int hash_order) 423 { 424 struct ttm_object_device *tdev; 425 int ret; 426 427 tdev = kmalloc(sizeof(*tdev), M_TTM_OBJ_DEV, M_WAITOK); 428 tdev->mem_glob = mem_glob; 429 lockinit(&tdev->object_lock, "ttmdo", 0, LK_CANRECURSE); 430 atomic_set(&tdev->object_count, 0); 431 ret = drm_ht_create(&tdev->object_hash, hash_order); 432 433 if (ret == 0) 434 return tdev; 435 436 drm_free(tdev, M_TTM_OBJ_DEV); 437 return NULL; 438 } 439 440 void ttm_object_device_release(struct ttm_object_device **p_tdev) 441 { 442 struct ttm_object_device *tdev = *p_tdev; 443 444 *p_tdev = NULL; 445 446 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 447 drm_ht_remove(&tdev->object_hash); 448 lockmgr(&tdev->object_lock, LK_RELEASE); 449 450 drm_free(tdev, M_TTM_OBJ_DEV); 451 } 452