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 MALLOC_DEFINE(M_TTM_OBJ_FILE, "ttm_obj_file", "TTM File Objects"); 117 118 static inline struct ttm_object_file * 119 ttm_object_file_ref(struct ttm_object_file *tfile) 120 { 121 kref_get(&tfile->refcount); 122 return tfile; 123 } 124 125 static void ttm_object_file_destroy(struct kref *kref) 126 { 127 struct ttm_object_file *tfile = 128 container_of(kref, struct ttm_object_file, refcount); 129 130 drm_free(tfile, M_TTM_OBJ_FILE); 131 } 132 133 134 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile) 135 { 136 struct ttm_object_file *tfile = *p_tfile; 137 138 *p_tfile = NULL; 139 kref_put(&tfile->refcount, ttm_object_file_destroy); 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 (*refcount_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 = refcount_release; 157 base->ref_obj_release = ref_obj_release; 158 base->object_type = object_type; 159 kref_init(&base->refcount); 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 EXPORT_SYMBOL(ttm_base_object_init); 184 185 static void ttm_release_base(struct kref *kref) 186 { 187 struct ttm_base_object *base = 188 container_of(kref, struct ttm_base_object, refcount); 189 struct ttm_object_device *tdev = base->tfile->tdev; 190 191 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 192 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash); 193 lockmgr(&tdev->object_lock, LK_RELEASE); 194 195 /* 196 * Note: We don't use synchronize_rcu() here because it's far 197 * too slow. It's up to the user to free the object using 198 * call_rcu() or ttm_base_object_kfree(). 199 */ 200 201 if (base->refcount_release) { 202 ttm_object_file_unref(&base->tfile); 203 base->refcount_release(&base); 204 } 205 } 206 207 void ttm_base_object_unref(struct ttm_base_object **p_base) 208 { 209 struct ttm_base_object *base = *p_base; 210 211 *p_base = NULL; 212 213 kref_put(&base->refcount, ttm_release_base); 214 } 215 EXPORT_SYMBOL(ttm_base_object_unref); 216 217 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile, 218 uint32_t key) 219 { 220 struct ttm_object_device *tdev = tfile->tdev; 221 struct ttm_base_object *base; 222 struct drm_hash_item *hash; 223 int ret; 224 225 lockmgr(&tdev->object_lock, LK_EXCLUSIVE); 226 ret = drm_ht_find_item(&tdev->object_hash, key, &hash); 227 228 if (likely(ret == 0)) { 229 base = drm_hash_entry(hash, struct ttm_base_object, hash); 230 ret = kref_get_unless_zero(&base->refcount) ? 0 : -EINVAL; 231 } 232 lockmgr(&tdev->object_lock, LK_RELEASE); 233 234 if (unlikely(ret != 0)) 235 return NULL; 236 237 if (tfile != base->tfile && !base->shareable) { 238 kprintf("[TTM] Attempted access of non-shareable object %p\n", 239 base); 240 ttm_base_object_unref(&base); 241 return NULL; 242 } 243 244 return base; 245 } 246 EXPORT_SYMBOL(ttm_base_object_lookup); 247 248 MALLOC_DEFINE(M_TTM_OBJ_REF, "ttm_obj_ref", "TTM Ref Objects"); 249 250 int ttm_ref_object_add(struct ttm_object_file *tfile, 251 struct ttm_base_object *base, 252 enum ttm_ref_type ref_type, bool *existed) 253 { 254 struct drm_open_hash *ht = &tfile->ref_hash[ref_type]; 255 struct ttm_ref_object *ref; 256 struct drm_hash_item *hash; 257 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob; 258 int ret = -EINVAL; 259 260 if (existed != NULL) 261 *existed = true; 262 263 while (ret == -EINVAL) { 264 lockmgr(&tfile->lock, LK_EXCLUSIVE); 265 ret = drm_ht_find_item(ht, base->hash.key, &hash); 266 267 if (ret == 0) { 268 ref = drm_hash_entry(hash, struct ttm_ref_object, hash); 269 kref_get(&ref->kref); 270 lockmgr(&tfile->lock, LK_RELEASE); 271 break; 272 } 273 274 lockmgr(&tfile->lock, LK_RELEASE); 275 ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref), 276 false, false); 277 if (unlikely(ret != 0)) 278 return ret; 279 ref = kmalloc(sizeof(*ref), M_TTM_OBJ_REF, M_WAITOK); 280 if (unlikely(ref == NULL)) { 281 ttm_mem_global_free(mem_glob, sizeof(*ref)); 282 return -ENOMEM; 283 } 284 285 ref->hash.key = base->hash.key; 286 ref->obj = base; 287 ref->tfile = tfile; 288 ref->ref_type = ref_type; 289 kref_init(&ref->kref); 290 291 lockmgr(&tfile->lock, LK_EXCLUSIVE); 292 ret = drm_ht_insert_item(ht, &ref->hash); 293 294 if (likely(ret == 0)) { 295 list_add_tail(&ref->head, &tfile->ref_list); 296 kref_get(&base->refcount); 297 lockmgr(&tfile->lock, LK_RELEASE); 298 if (existed != NULL) 299 *existed = false; 300 break; 301 } 302 303 lockmgr(&tfile->lock, LK_RELEASE); 304 BUG_ON(ret != -EINVAL); 305 306 ttm_mem_global_free(mem_glob, sizeof(*ref)); 307 drm_free(ref, M_TTM_OBJ_REF); 308 } 309 310 return ret; 311 } 312 EXPORT_SYMBOL(ttm_ref_object_add); 313 314 static void ttm_ref_object_release(struct kref *kref) 315 { 316 struct ttm_ref_object *ref = 317 container_of(kref, struct ttm_ref_object, kref); 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 kref_put(&ref->kref, ttm_ref_object_release); 353 lockmgr(&tfile->lock, LK_RELEASE); 354 return 0; 355 } 356 EXPORT_SYMBOL(ttm_ref_object_base_unref); 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->kref); 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 EXPORT_SYMBOL(ttm_object_file_release); 386 387 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev, 388 unsigned int hash_order) 389 { 390 struct ttm_object_file *tfile; 391 unsigned int i; 392 unsigned int j = 0; 393 int ret; 394 395 tfile = kmalloc(sizeof(*tfile), M_TTM_OBJ_FILE, M_WAITOK); 396 if (unlikely(tfile == NULL)) 397 return NULL; 398 399 lockinit(&tfile->lock, "ttmfo", 0, LK_CANRECURSE); 400 tfile->tdev = tdev; 401 kref_init(&tfile->refcount); 402 INIT_LIST_HEAD(&tfile->ref_list); 403 404 for (i = 0; i < TTM_REF_NUM; ++i) { 405 ret = drm_ht_create(&tfile->ref_hash[i], hash_order); 406 if (ret) { 407 j = i; 408 goto out_err; 409 } 410 } 411 412 return tfile; 413 out_err: 414 for (i = 0; i < j; ++i) 415 drm_ht_remove(&tfile->ref_hash[i]); 416 417 drm_free(tfile, M_TTM_OBJ_FILE); 418 419 return NULL; 420 } 421 EXPORT_SYMBOL(ttm_object_file_init); 422 423 MALLOC_DEFINE(M_TTM_OBJ_DEV, "ttm_obj_dev", "TTM Device Objects"); 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_TTM_OBJ_DEV, 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 drm_free(tdev, M_TTM_OBJ_DEV); 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 drm_free(tdev, M_TTM_OBJ_DEV); 460 } 461 EXPORT_SYMBOL(ttm_object_device_release); 462