ttm_object.c 11 KB

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