1 /**************************************************************************
3 * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 /** @file ttm_ref_object.c
32 * Base- and reference object implementation for the various
33 * ttm objects. Implements reference counting, minimal security checks
34 * and release on file close.
38 * struct ttm_object_file
40 * @tdev: Pointer to the ttm_object_device.
42 * @lock: Lock that protects the ref_list list and the
43 * ref_hash hash tables.
45 * @ref_list: List of ttm_ref_objects to be destroyed at
48 * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
49 * for fast lookup of ref objects given a base object.
52 #define pr_fmt(fmt) "[TTM] " fmt
54 #include <drm/ttm/ttm_object.h>
55 #include <drm/ttm/ttm_module.h>
56 #include <linux/list.h>
57 #include <linux/spinlock.h>
58 #include <linux/slab.h>
59 #include <linux/module.h>
60 #include <linux/atomic.h>
62 struct ttm_object_file {
63 struct ttm_object_device *tdev;
65 struct list_head ref_list;
66 struct drm_open_hash ref_hash[TTM_REF_NUM];
71 * struct ttm_object_device
73 * @object_lock: lock that protects the object_hash hash table.
75 * @object_hash: hash table for fast lookup of object global names.
77 * @object_count: Per device object count.
79 * This is the per-device data structure needed for ttm object management.
82 struct ttm_object_device {
83 spinlock_t object_lock;
84 struct drm_open_hash object_hash;
85 atomic_t object_count;
86 struct ttm_mem_global *mem_glob;
90 * struct ttm_ref_object
92 * @hash: Hash entry for the per-file object reference hash.
94 * @head: List entry for the per-file list of ref-objects.
98 * @obj: Base object this ref object is referencing.
100 * @ref_type: Type of ref object.
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.
110 struct ttm_ref_object {
111 struct drm_hash_item hash;
112 struct list_head head;
114 enum ttm_ref_type ref_type;
115 struct ttm_base_object *obj;
116 struct ttm_object_file *tfile;
119 static inline struct ttm_object_file *
120 ttm_object_file_ref(struct ttm_object_file *tfile)
122 kref_get(&tfile->refcount);
126 static void ttm_object_file_destroy(struct kref *kref)
128 struct ttm_object_file *tfile =
129 container_of(kref, struct ttm_object_file, refcount);
135 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
137 struct ttm_object_file *tfile = *p_tfile;
140 kref_put(&tfile->refcount, ttm_object_file_destroy);
144 int ttm_base_object_init(struct ttm_object_file *tfile,
145 struct ttm_base_object *base,
147 enum ttm_object_type object_type,
148 void (*refcount_release) (struct ttm_base_object **),
149 void (*ref_obj_release) (struct ttm_base_object *,
150 enum ttm_ref_type ref_type))
152 struct ttm_object_device *tdev = tfile->tdev;
155 base->shareable = shareable;
156 base->tfile = ttm_object_file_ref(tfile);
157 base->refcount_release = refcount_release;
158 base->ref_obj_release = ref_obj_release;
159 base->object_type = object_type;
160 kref_init(&base->refcount);
161 spin_lock(&tdev->object_lock);
162 ret = drm_ht_just_insert_please_rcu(&tdev->object_hash,
164 (unsigned long)base, 31, 0, 0);
165 spin_unlock(&tdev->object_lock);
166 if (unlikely(ret != 0))
169 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
170 if (unlikely(ret != 0))
173 ttm_base_object_unref(&base);
177 spin_lock(&tdev->object_lock);
178 (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
179 spin_unlock(&tdev->object_lock);
183 EXPORT_SYMBOL(ttm_base_object_init);
185 static void ttm_release_base(struct kref *kref)
187 struct ttm_base_object *base =
188 container_of(kref, struct ttm_base_object, refcount);
189 struct ttm_object_device *tdev = base->tfile->tdev;
191 spin_lock(&tdev->object_lock);
192 (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
193 spin_unlock(&tdev->object_lock);
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().
201 if (base->refcount_release) {
202 ttm_object_file_unref(&base->tfile);
203 base->refcount_release(&base);
207 void ttm_base_object_unref(struct ttm_base_object **p_base)
209 struct ttm_base_object *base = *p_base;
213 kref_put(&base->refcount, ttm_release_base);
215 EXPORT_SYMBOL(ttm_base_object_unref);
217 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
220 struct ttm_object_device *tdev = tfile->tdev;
221 struct ttm_base_object *base;
222 struct drm_hash_item *hash;
226 ret = drm_ht_find_item_rcu(&tdev->object_hash, key, &hash);
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;
234 if (unlikely(ret != 0))
237 if (tfile != base->tfile && !base->shareable) {
238 pr_err("Attempted access of non-shareable object\n");
239 ttm_base_object_unref(&base);
245 EXPORT_SYMBOL(ttm_base_object_lookup);
247 int ttm_ref_object_add(struct ttm_object_file *tfile,
248 struct ttm_base_object *base,
249 enum ttm_ref_type ref_type, bool *existed)
251 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
252 struct ttm_ref_object *ref;
253 struct drm_hash_item *hash;
254 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
260 while (ret == -EINVAL) {
261 read_lock(&tfile->lock);
262 ret = drm_ht_find_item(ht, base->hash.key, &hash);
265 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
266 kref_get(&ref->kref);
267 read_unlock(&tfile->lock);
271 read_unlock(&tfile->lock);
272 ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
274 if (unlikely(ret != 0))
276 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
277 if (unlikely(ref == NULL)) {
278 ttm_mem_global_free(mem_glob, sizeof(*ref));
282 ref->hash.key = base->hash.key;
285 ref->ref_type = ref_type;
286 kref_init(&ref->kref);
288 write_lock(&tfile->lock);
289 ret = drm_ht_insert_item(ht, &ref->hash);
291 if (likely(ret == 0)) {
292 list_add_tail(&ref->head, &tfile->ref_list);
293 kref_get(&base->refcount);
294 write_unlock(&tfile->lock);
300 write_unlock(&tfile->lock);
301 BUG_ON(ret != -EINVAL);
303 ttm_mem_global_free(mem_glob, sizeof(*ref));
309 EXPORT_SYMBOL(ttm_ref_object_add);
311 static void ttm_ref_object_release(struct kref *kref)
313 struct ttm_ref_object *ref =
314 container_of(kref, struct ttm_ref_object, kref);
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;
320 ht = &tfile->ref_hash[ref->ref_type];
321 (void)drm_ht_remove_item(ht, &ref->hash);
322 list_del(&ref->head);
323 write_unlock(&tfile->lock);
325 if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
326 base->ref_obj_release(base, ref->ref_type);
328 ttm_base_object_unref(&ref->obj);
329 ttm_mem_global_free(mem_glob, sizeof(*ref));
331 write_lock(&tfile->lock);
334 int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
335 unsigned long key, enum ttm_ref_type ref_type)
337 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
338 struct ttm_ref_object *ref;
339 struct drm_hash_item *hash;
342 write_lock(&tfile->lock);
343 ret = drm_ht_find_item(ht, key, &hash);
344 if (unlikely(ret != 0)) {
345 write_unlock(&tfile->lock);
348 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
349 kref_put(&ref->kref, ttm_ref_object_release);
350 write_unlock(&tfile->lock);
353 EXPORT_SYMBOL(ttm_ref_object_base_unref);
355 void ttm_object_file_release(struct ttm_object_file **p_tfile)
357 struct ttm_ref_object *ref;
358 struct list_head *list;
360 struct ttm_object_file *tfile = *p_tfile;
363 write_lock(&tfile->lock);
366 * Since we release the lock within the loop, we have to
367 * restart it from the beginning each time.
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->kref);
376 for (i = 0; i < TTM_REF_NUM; ++i)
377 drm_ht_remove(&tfile->ref_hash[i]);
379 write_unlock(&tfile->lock);
380 ttm_object_file_unref(&tfile);
382 EXPORT_SYMBOL(ttm_object_file_release);
384 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
385 unsigned int hash_order)
387 struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
392 if (unlikely(tfile == NULL))
395 rwlock_init(&tfile->lock);
397 kref_init(&tfile->refcount);
398 INIT_LIST_HEAD(&tfile->ref_list);
400 for (i = 0; i < TTM_REF_NUM; ++i) {
401 ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
410 for (i = 0; i < j; ++i)
411 drm_ht_remove(&tfile->ref_hash[i]);
417 EXPORT_SYMBOL(ttm_object_file_init);
419 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
421 unsigned int hash_order)
423 struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
426 if (unlikely(tdev == NULL))
429 tdev->mem_glob = mem_glob;
430 spin_lock_init(&tdev->object_lock);
431 atomic_set(&tdev->object_count, 0);
432 ret = drm_ht_create(&tdev->object_hash, hash_order);
434 if (likely(ret == 0))
440 EXPORT_SYMBOL(ttm_object_device_init);
442 void ttm_object_device_release(struct ttm_object_device **p_tdev)
444 struct ttm_object_device *tdev = *p_tdev;
448 spin_lock(&tdev->object_lock);
449 drm_ht_remove(&tdev->object_hash);
450 spin_unlock(&tdev->object_lock);
454 EXPORT_SYMBOL(ttm_object_device_release);