ttm_object.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. * struct ttm_object_file
  38. *
  39. * @tdev: Pointer to the ttm_object_device.
  40. *
  41. * @lock: Lock that protects the ref_list list and the
  42. * ref_hash hash tables.
  43. *
  44. * @ref_list: List of ttm_ref_objects to be destroyed at
  45. * file release.
  46. *
  47. * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
  48. * for fast lookup of ref objects given a base object.
  49. */
  50. #include "ttm/ttm_object.h"
  51. #include "ttm/ttm_module.h"
  52. #include <linux/list.h>
  53. #include <linux/spinlock.h>
  54. #include <linux/slab.h>
  55. #include <linux/module.h>
  56. #include <asm/atomic.h>
  57. struct ttm_object_file {
  58. struct ttm_object_device *tdev;
  59. rwlock_t lock;
  60. struct list_head ref_list;
  61. struct drm_open_hash ref_hash[TTM_REF_NUM];
  62. struct kref refcount;
  63. };
  64. /**
  65. * struct ttm_object_device
  66. *
  67. * @object_lock: lock that protects the object_hash hash table.
  68. *
  69. * @object_hash: hash table for fast lookup of object global names.
  70. *
  71. * @object_count: Per device object count.
  72. *
  73. * This is the per-device data structure needed for ttm object management.
  74. */
  75. struct ttm_object_device {
  76. rwlock_t object_lock;
  77. struct drm_open_hash object_hash;
  78. atomic_t object_count;
  79. struct ttm_mem_global *mem_glob;
  80. };
  81. /**
  82. * struct ttm_ref_object
  83. *
  84. * @hash: Hash entry for the per-file object reference hash.
  85. *
  86. * @head: List entry for the per-file list of ref-objects.
  87. *
  88. * @kref: Ref count.
  89. *
  90. * @obj: Base object this ref object is referencing.
  91. *
  92. * @ref_type: Type of ref object.
  93. *
  94. * This is similar to an idr object, but it also has a hash table entry
  95. * that allows lookup with a pointer to the referenced object as a key. In
  96. * that way, one can easily detect whether a base object is referenced by
  97. * a particular ttm_object_file. It also carries a ref count to avoid creating
  98. * multiple ref objects if a ttm_object_file references the same base
  99. * object more than once.
  100. */
  101. struct ttm_ref_object {
  102. struct drm_hash_item hash;
  103. struct list_head head;
  104. struct kref kref;
  105. enum ttm_ref_type ref_type;
  106. struct ttm_base_object *obj;
  107. struct ttm_object_file *tfile;
  108. };
  109. static inline struct ttm_object_file *
  110. ttm_object_file_ref(struct ttm_object_file *tfile)
  111. {
  112. kref_get(&tfile->refcount);
  113. return tfile;
  114. }
  115. static void ttm_object_file_destroy(struct kref *kref)
  116. {
  117. struct ttm_object_file *tfile =
  118. container_of(kref, struct ttm_object_file, refcount);
  119. kfree(tfile);
  120. }
  121. static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
  122. {
  123. struct ttm_object_file *tfile = *p_tfile;
  124. *p_tfile = NULL;
  125. kref_put(&tfile->refcount, ttm_object_file_destroy);
  126. }
  127. int ttm_base_object_init(struct ttm_object_file *tfile,
  128. struct ttm_base_object *base,
  129. bool shareable,
  130. enum ttm_object_type object_type,
  131. void (*refcount_release) (struct ttm_base_object **),
  132. void (*ref_obj_release) (struct ttm_base_object *,
  133. enum ttm_ref_type ref_type))
  134. {
  135. struct ttm_object_device *tdev = tfile->tdev;
  136. int ret;
  137. base->shareable = shareable;
  138. base->tfile = ttm_object_file_ref(tfile);
  139. base->refcount_release = refcount_release;
  140. base->ref_obj_release = ref_obj_release;
  141. base->object_type = object_type;
  142. write_lock(&tdev->object_lock);
  143. kref_init(&base->refcount);
  144. ret = drm_ht_just_insert_please(&tdev->object_hash,
  145. &base->hash,
  146. (unsigned long)base, 31, 0, 0);
  147. write_unlock(&tdev->object_lock);
  148. if (unlikely(ret != 0))
  149. goto out_err0;
  150. ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
  151. if (unlikely(ret != 0))
  152. goto out_err1;
  153. ttm_base_object_unref(&base);
  154. return 0;
  155. out_err1:
  156. (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
  157. out_err0:
  158. return ret;
  159. }
  160. EXPORT_SYMBOL(ttm_base_object_init);
  161. static void ttm_release_base(struct kref *kref)
  162. {
  163. struct ttm_base_object *base =
  164. container_of(kref, struct ttm_base_object, refcount);
  165. struct ttm_object_device *tdev = base->tfile->tdev;
  166. (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
  167. write_unlock(&tdev->object_lock);
  168. if (base->refcount_release) {
  169. ttm_object_file_unref(&base->tfile);
  170. base->refcount_release(&base);
  171. }
  172. write_lock(&tdev->object_lock);
  173. }
  174. void ttm_base_object_unref(struct ttm_base_object **p_base)
  175. {
  176. struct ttm_base_object *base = *p_base;
  177. struct ttm_object_device *tdev = base->tfile->tdev;
  178. *p_base = NULL;
  179. /*
  180. * Need to take the lock here to avoid racing with
  181. * users trying to look up the object.
  182. */
  183. write_lock(&tdev->object_lock);
  184. kref_put(&base->refcount, ttm_release_base);
  185. write_unlock(&tdev->object_lock);
  186. }
  187. EXPORT_SYMBOL(ttm_base_object_unref);
  188. struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
  189. uint32_t key)
  190. {
  191. struct ttm_object_device *tdev = tfile->tdev;
  192. struct ttm_base_object *base;
  193. struct drm_hash_item *hash;
  194. int ret;
  195. read_lock(&tdev->object_lock);
  196. ret = drm_ht_find_item(&tdev->object_hash, key, &hash);
  197. if (likely(ret == 0)) {
  198. base = drm_hash_entry(hash, struct ttm_base_object, hash);
  199. kref_get(&base->refcount);
  200. }
  201. read_unlock(&tdev->object_lock);
  202. if (unlikely(ret != 0))
  203. return NULL;
  204. if (tfile != base->tfile && !base->shareable) {
  205. printk(KERN_ERR TTM_PFX
  206. "Attempted access of non-shareable object.\n");
  207. ttm_base_object_unref(&base);
  208. return NULL;
  209. }
  210. return base;
  211. }
  212. EXPORT_SYMBOL(ttm_base_object_lookup);
  213. int ttm_ref_object_add(struct ttm_object_file *tfile,
  214. struct ttm_base_object *base,
  215. enum ttm_ref_type ref_type, bool *existed)
  216. {
  217. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  218. struct ttm_ref_object *ref;
  219. struct drm_hash_item *hash;
  220. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  221. int ret = -EINVAL;
  222. if (existed != NULL)
  223. *existed = true;
  224. while (ret == -EINVAL) {
  225. read_lock(&tfile->lock);
  226. ret = drm_ht_find_item(ht, base->hash.key, &hash);
  227. if (ret == 0) {
  228. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  229. kref_get(&ref->kref);
  230. read_unlock(&tfile->lock);
  231. break;
  232. }
  233. read_unlock(&tfile->lock);
  234. ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
  235. false, false);
  236. if (unlikely(ret != 0))
  237. return ret;
  238. ref = kmalloc(sizeof(*ref), GFP_KERNEL);
  239. if (unlikely(ref == NULL)) {
  240. ttm_mem_global_free(mem_glob, sizeof(*ref));
  241. return -ENOMEM;
  242. }
  243. ref->hash.key = base->hash.key;
  244. ref->obj = base;
  245. ref->tfile = tfile;
  246. ref->ref_type = ref_type;
  247. kref_init(&ref->kref);
  248. write_lock(&tfile->lock);
  249. ret = drm_ht_insert_item(ht, &ref->hash);
  250. if (likely(ret == 0)) {
  251. list_add_tail(&ref->head, &tfile->ref_list);
  252. kref_get(&base->refcount);
  253. write_unlock(&tfile->lock);
  254. if (existed != NULL)
  255. *existed = false;
  256. break;
  257. }
  258. write_unlock(&tfile->lock);
  259. BUG_ON(ret != -EINVAL);
  260. ttm_mem_global_free(mem_glob, sizeof(*ref));
  261. kfree(ref);
  262. }
  263. return ret;
  264. }
  265. EXPORT_SYMBOL(ttm_ref_object_add);
  266. static void ttm_ref_object_release(struct kref *kref)
  267. {
  268. struct ttm_ref_object *ref =
  269. container_of(kref, struct ttm_ref_object, kref);
  270. struct ttm_base_object *base = ref->obj;
  271. struct ttm_object_file *tfile = ref->tfile;
  272. struct drm_open_hash *ht;
  273. struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
  274. ht = &tfile->ref_hash[ref->ref_type];
  275. (void)drm_ht_remove_item(ht, &ref->hash);
  276. list_del(&ref->head);
  277. write_unlock(&tfile->lock);
  278. if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
  279. base->ref_obj_release(base, ref->ref_type);
  280. ttm_base_object_unref(&ref->obj);
  281. ttm_mem_global_free(mem_glob, sizeof(*ref));
  282. kfree(ref);
  283. write_lock(&tfile->lock);
  284. }
  285. int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
  286. unsigned long key, enum ttm_ref_type ref_type)
  287. {
  288. struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
  289. struct ttm_ref_object *ref;
  290. struct drm_hash_item *hash;
  291. int ret;
  292. write_lock(&tfile->lock);
  293. ret = drm_ht_find_item(ht, key, &hash);
  294. if (unlikely(ret != 0)) {
  295. write_unlock(&tfile->lock);
  296. return -EINVAL;
  297. }
  298. ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
  299. kref_put(&ref->kref, ttm_ref_object_release);
  300. write_unlock(&tfile->lock);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL(ttm_ref_object_base_unref);
  304. void ttm_object_file_release(struct ttm_object_file **p_tfile)
  305. {
  306. struct ttm_ref_object *ref;
  307. struct list_head *list;
  308. unsigned int i;
  309. struct ttm_object_file *tfile = *p_tfile;
  310. *p_tfile = NULL;
  311. write_lock(&tfile->lock);
  312. /*
  313. * Since we release the lock within the loop, we have to
  314. * restart it from the beginning each time.
  315. */
  316. while (!list_empty(&tfile->ref_list)) {
  317. list = tfile->ref_list.next;
  318. ref = list_entry(list, struct ttm_ref_object, head);
  319. ttm_ref_object_release(&ref->kref);
  320. }
  321. for (i = 0; i < TTM_REF_NUM; ++i)
  322. drm_ht_remove(&tfile->ref_hash[i]);
  323. write_unlock(&tfile->lock);
  324. ttm_object_file_unref(&tfile);
  325. }
  326. EXPORT_SYMBOL(ttm_object_file_release);
  327. struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
  328. unsigned int hash_order)
  329. {
  330. struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  331. unsigned int i;
  332. unsigned int j = 0;
  333. int ret;
  334. if (unlikely(tfile == NULL))
  335. return NULL;
  336. rwlock_init(&tfile->lock);
  337. tfile->tdev = tdev;
  338. kref_init(&tfile->refcount);
  339. INIT_LIST_HEAD(&tfile->ref_list);
  340. for (i = 0; i < TTM_REF_NUM; ++i) {
  341. ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
  342. if (ret) {
  343. j = i;
  344. goto out_err;
  345. }
  346. }
  347. return tfile;
  348. out_err:
  349. for (i = 0; i < j; ++i)
  350. drm_ht_remove(&tfile->ref_hash[i]);
  351. kfree(tfile);
  352. return NULL;
  353. }
  354. EXPORT_SYMBOL(ttm_object_file_init);
  355. struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
  356. *mem_glob,
  357. unsigned int hash_order)
  358. {
  359. struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
  360. int ret;
  361. if (unlikely(tdev == NULL))
  362. return NULL;
  363. tdev->mem_glob = mem_glob;
  364. rwlock_init(&tdev->object_lock);
  365. atomic_set(&tdev->object_count, 0);
  366. ret = drm_ht_create(&tdev->object_hash, hash_order);
  367. if (likely(ret == 0))
  368. return tdev;
  369. kfree(tdev);
  370. return NULL;
  371. }
  372. EXPORT_SYMBOL(ttm_object_device_init);
  373. void ttm_object_device_release(struct ttm_object_device **p_tdev)
  374. {
  375. struct ttm_object_device *tdev = *p_tdev;
  376. *p_tdev = NULL;
  377. write_lock(&tdev->object_lock);
  378. drm_ht_remove(&tdev->object_hash);
  379. write_unlock(&tdev->object_lock);
  380. kfree(tdev);
  381. }
  382. EXPORT_SYMBOL(ttm_object_device_release);