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 ref->hash.key = base->hash.key; 286 ref->obj = base; 287 ref->tfile = tfile; 288 ref->ref_type = ref_type; 289 refcount_init(&ref->kref, 1); 290 291 rw_wlock(&tfile->lock); 292 ret = drm_ht_insert_item(ht, &ref->hash); 293 294 if (ret == 0) { 295 list_add_tail(&ref->head, &tfile->ref_list); 296 refcount_acquire(&base->refcount); 297 rw_wunlock(&tfile->lock); 298 if (existed != NULL) 299 *existed = false; 300 break; 301 } 302 303 rw_wunlock(&tfile->lock); 304 MPASS(ret == -EINVAL); 305 306 ttm_mem_global_free(mem_glob, sizeof(*ref)); 307 free(ref, M_TTM_OBJ_REF); 308 } 309 310 return ret; 311 } 312 313 static void ttm_ref_object_release(struct ttm_ref_object *ref) 314 { 315 struct ttm_base_object *base = ref->obj; 316 struct ttm_object_file *tfile = ref->tfile; 317 struct drm_open_hash *ht; 318 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob; 319 320 ht = &tfile->ref_hash[ref->ref_type]; 321 (void)drm_ht_remove_item(ht, &ref->hash); 322 list_del(&ref->head); 323 rw_wunlock(&tfile->lock); 324 325 if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release) 326 base->ref_obj_release(base, ref->ref_type); 327 328 ttm_base_object_unref(&ref->obj); 329 ttm_mem_global_free(mem_glob, sizeof(*ref)); 330 free(ref, M_TTM_OBJ_REF); 331 rw_wlock(&tfile->lock); 332 } 333 334 int ttm_ref_object_base_unref(struct ttm_object_file *tfile, 335 unsigned long key, enum ttm_ref_type ref_type) 336 { 337 struct drm_open_hash *ht = &tfile->ref_hash[ref_type]; 338 struct ttm_ref_object *ref; 339 struct drm_hash_item *hash; 340 int ret; 341 342 rw_wlock(&tfile->lock); 343 ret = drm_ht_find_item(ht, key, &hash); 344 if (unlikely(ret != 0)) { 345 rw_wunlock(&tfile->lock); 346 return -EINVAL; 347 } 348 ref = drm_hash_entry(hash, struct ttm_ref_object, hash); 349 if (refcount_release(&ref->kref)) 350 ttm_ref_object_release(ref); 351 rw_wunlock(&tfile->lock); 352 return 0; 353 } 354 355 void ttm_object_file_release(struct ttm_object_file **p_tfile) 356 { 357 struct ttm_ref_object *ref; 358 struct list_head *list; 359 unsigned int i; 360 struct ttm_object_file *tfile = *p_tfile; 361 362 *p_tfile = NULL; 363 rw_wlock(&tfile->lock); 364 365 /* 366 * Since we release the lock within the loop, we have to 367 * restart it from the beginning each time. 368 */ 369 370 while (!list_empty(&tfile->ref_list)) { 371 list = tfile->ref_list.next; 372 ref = list_entry(list, struct ttm_ref_object, head); 373 ttm_ref_object_release(ref); 374 } 375 376 for (i = 0; i < TTM_REF_NUM; ++i) 377 drm_ht_remove(&tfile->ref_hash[i]); 378 379 rw_wunlock(&tfile->lock); 380 ttm_object_file_unref(&tfile); 381 } 382 383 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev, 384 unsigned int hash_order) 385 { 386 struct ttm_object_file *tfile; 387 unsigned int i; 388 unsigned int j = 0; 389 int ret; 390 391 tfile = malloc(sizeof(*tfile), M_TTM_OBJ_FILE, M_WAITOK); 392 rw_init(&tfile->lock, "ttmfo"); 393 tfile->tdev = tdev; 394 refcount_init(&tfile->refcount, 1); 395 INIT_LIST_HEAD(&tfile->ref_list); 396 397 for (i = 0; i < TTM_REF_NUM; ++i) { 398 ret = drm_ht_create(&tfile->ref_hash[i], hash_order); 399 if (ret) { 400 j = i; 401 goto out_err; 402 } 403 } 404 405 return tfile; 406 out_err: 407 for (i = 0; i < j; ++i) 408 drm_ht_remove(&tfile->ref_hash[i]); 409 410 free(tfile, M_TTM_OBJ_FILE); 411 412 return NULL; 413 } 414 415 MALLOC_DEFINE(M_TTM_OBJ_DEV, "ttm_obj_dev", "TTM Device Objects"); 416 417 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global 418 *mem_glob, 419 unsigned int hash_order) 420 { 421 struct ttm_object_device *tdev; 422 int ret; 423 424 tdev = malloc(sizeof(*tdev), M_TTM_OBJ_DEV, M_WAITOK); 425 tdev->mem_glob = mem_glob; 426 rw_init(&tdev->object_lock, "ttmdo"); 427 atomic_set(&tdev->object_count, 0); 428 ret = drm_ht_create(&tdev->object_hash, hash_order); 429 430 if (ret == 0) 431 return tdev; 432 433 free(tdev, M_TTM_OBJ_DEV); 434 return NULL; 435 } 436 437 void ttm_object_device_release(struct ttm_object_device **p_tdev) 438 { 439 struct ttm_object_device *tdev = *p_tdev; 440 441 *p_tdev = NULL; 442 443 rw_wlock(&tdev->object_lock); 444 drm_ht_remove(&tdev->object_hash); 445 rw_wunlock(&tdev->object_lock); 446 447 free(tdev, M_TTM_OBJ_DEV); 448 } 449