1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2009-2013 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 * While no substantial code is shared, the prime code is inspired by
34 * Dave Airlie <airlied@redhat.com>
35 * Rob Clark <rob.clark@linaro.org>
37 /** @file ttm_ref_object.c
39 * Base- and reference object implementation for the various
40 * ttm objects. Implements reference counting, minimal security checks
41 * and release on file close.
46 * struct ttm_object_file
48 * @tdev: Pointer to the ttm_object_device.
50 * @lock: Lock that protects the ref_list list and the
51 * ref_hash hash tables.
53 * @ref_list: List of ttm_ref_objects to be destroyed at
56 * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
57 * for fast lookup of ref objects given a base object.
60 #define pr_fmt(fmt) "[TTM] " fmt
62 #include <drm/ttm/ttm_module.h>
63 #include <linux/list.h>
64 #include <linux/spinlock.h>
65 #include <linux/slab.h>
66 #include <linux/atomic.h>
67 #include "ttm_object.h"
69 struct ttm_object_file
{
70 struct ttm_object_device
*tdev
;
72 struct list_head ref_list
;
73 struct drm_open_hash ref_hash
[TTM_REF_NUM
];
78 * struct ttm_object_device
80 * @object_lock: lock that protects the object_hash hash table.
82 * @object_hash: hash table for fast lookup of object global names.
84 * @object_count: Per device object count.
86 * This is the per-device data structure needed for ttm object management.
89 struct ttm_object_device
{
90 spinlock_t object_lock
;
91 struct drm_open_hash object_hash
;
92 atomic_t object_count
;
93 struct ttm_mem_global
*mem_glob
;
94 struct dma_buf_ops ops
;
95 void (*dmabuf_release
)(struct dma_buf
*dma_buf
);
101 * struct ttm_ref_object
103 * @hash: Hash entry for the per-file object reference hash.
105 * @head: List entry for the per-file list of ref-objects.
109 * @obj: Base object this ref object is referencing.
111 * @ref_type: Type of ref object.
113 * This is similar to an idr object, but it also has a hash table entry
114 * that allows lookup with a pointer to the referenced object as a key. In
115 * that way, one can easily detect whether a base object is referenced by
116 * a particular ttm_object_file. It also carries a ref count to avoid creating
117 * multiple ref objects if a ttm_object_file references the same base
118 * object more than once.
121 struct ttm_ref_object
{
122 struct rcu_head rcu_head
;
123 struct drm_hash_item hash
;
124 struct list_head head
;
126 enum ttm_ref_type ref_type
;
127 struct ttm_base_object
*obj
;
128 struct ttm_object_file
*tfile
;
131 static void ttm_prime_dmabuf_release(struct dma_buf
*dma_buf
);
133 static inline struct ttm_object_file
*
134 ttm_object_file_ref(struct ttm_object_file
*tfile
)
136 kref_get(&tfile
->refcount
);
140 static void ttm_object_file_destroy(struct kref
*kref
)
142 struct ttm_object_file
*tfile
=
143 container_of(kref
, struct ttm_object_file
, refcount
);
149 static inline void ttm_object_file_unref(struct ttm_object_file
**p_tfile
)
151 struct ttm_object_file
*tfile
= *p_tfile
;
154 kref_put(&tfile
->refcount
, ttm_object_file_destroy
);
158 int ttm_base_object_init(struct ttm_object_file
*tfile
,
159 struct ttm_base_object
*base
,
161 enum ttm_object_type object_type
,
162 void (*refcount_release
) (struct ttm_base_object
**),
163 void (*ref_obj_release
) (struct ttm_base_object
*,
164 enum ttm_ref_type ref_type
))
166 struct ttm_object_device
*tdev
= tfile
->tdev
;
169 base
->shareable
= shareable
;
170 base
->tfile
= ttm_object_file_ref(tfile
);
171 base
->refcount_release
= refcount_release
;
172 base
->ref_obj_release
= ref_obj_release
;
173 base
->object_type
= object_type
;
174 kref_init(&base
->refcount
);
175 idr_preload(GFP_KERNEL
);
176 spin_lock(&tdev
->object_lock
);
177 ret
= idr_alloc(&tdev
->idr
, base
, 1, 0, GFP_NOWAIT
);
178 spin_unlock(&tdev
->object_lock
);
184 ret
= ttm_ref_object_add(tfile
, base
, TTM_REF_USAGE
, NULL
, false);
185 if (unlikely(ret
!= 0))
188 ttm_base_object_unref(&base
);
192 spin_lock(&tdev
->object_lock
);
193 idr_remove(&tdev
->idr
, base
->handle
);
194 spin_unlock(&tdev
->object_lock
);
198 static void ttm_release_base(struct kref
*kref
)
200 struct ttm_base_object
*base
=
201 container_of(kref
, struct ttm_base_object
, refcount
);
202 struct ttm_object_device
*tdev
= base
->tfile
->tdev
;
204 spin_lock(&tdev
->object_lock
);
205 idr_remove(&tdev
->idr
, base
->handle
);
206 spin_unlock(&tdev
->object_lock
);
209 * Note: We don't use synchronize_rcu() here because it's far
210 * too slow. It's up to the user to free the object using
211 * call_rcu() or ttm_base_object_kfree().
214 ttm_object_file_unref(&base
->tfile
);
215 if (base
->refcount_release
)
216 base
->refcount_release(&base
);
219 void ttm_base_object_unref(struct ttm_base_object
**p_base
)
221 struct ttm_base_object
*base
= *p_base
;
225 kref_put(&base
->refcount
, ttm_release_base
);
229 * ttm_base_object_noref_lookup - look up a base object without reference
230 * @tfile: The struct ttm_object_file the object is registered with.
231 * @key: The object handle.
233 * This function looks up a ttm base object and returns a pointer to it
234 * without refcounting the pointer. The returned pointer is only valid
235 * until ttm_base_object_noref_release() is called, and the object
236 * pointed to by the returned pointer may be doomed. Any persistent usage
237 * of the object requires a refcount to be taken using kref_get_unless_zero().
238 * Iff this function returns successfully it needs to be paired with
239 * ttm_base_object_noref_release() and no sleeping- or scheduling functions
240 * may be called inbetween these function callse.
242 * Return: A pointer to the object if successful or NULL otherwise.
244 struct ttm_base_object
*
245 ttm_base_object_noref_lookup(struct ttm_object_file
*tfile
, uint32_t key
)
247 struct drm_hash_item
*hash
;
248 struct drm_open_hash
*ht
= &tfile
->ref_hash
[TTM_REF_USAGE
];
252 ret
= drm_ht_find_item_rcu(ht
, key
, &hash
);
259 return drm_hash_entry(hash
, struct ttm_ref_object
, hash
)->obj
;
261 EXPORT_SYMBOL(ttm_base_object_noref_lookup
);
263 struct ttm_base_object
*ttm_base_object_lookup(struct ttm_object_file
*tfile
,
266 struct ttm_base_object
*base
= NULL
;
267 struct drm_hash_item
*hash
;
268 struct drm_open_hash
*ht
= &tfile
->ref_hash
[TTM_REF_USAGE
];
272 ret
= drm_ht_find_item_rcu(ht
, key
, &hash
);
274 if (likely(ret
== 0)) {
275 base
= drm_hash_entry(hash
, struct ttm_ref_object
, hash
)->obj
;
276 if (!kref_get_unless_zero(&base
->refcount
))
284 struct ttm_base_object
*
285 ttm_base_object_lookup_for_ref(struct ttm_object_device
*tdev
, uint32_t key
)
287 struct ttm_base_object
*base
;
290 base
= idr_find(&tdev
->idr
, key
);
292 if (base
&& !kref_get_unless_zero(&base
->refcount
))
300 * ttm_ref_object_exists - Check whether a caller has a valid ref object
301 * (has opened) a base object.
303 * @tfile: Pointer to a struct ttm_object_file identifying the caller.
304 * @base: Pointer to a struct base object.
306 * Checks wether the caller identified by @tfile has put a valid USAGE
307 * reference object on the base object identified by @base.
309 bool ttm_ref_object_exists(struct ttm_object_file
*tfile
,
310 struct ttm_base_object
*base
)
312 struct drm_open_hash
*ht
= &tfile
->ref_hash
[TTM_REF_USAGE
];
313 struct drm_hash_item
*hash
;
314 struct ttm_ref_object
*ref
;
317 if (unlikely(drm_ht_find_item_rcu(ht
, base
->handle
, &hash
) != 0))
321 * Verify that the ref object is really pointing to our base object.
322 * Our base object could actually be dead, and the ref object pointing
323 * to another base object with the same handle.
325 ref
= drm_hash_entry(hash
, struct ttm_ref_object
, hash
);
326 if (unlikely(base
!= ref
->obj
))
330 * Verify that the ref->obj pointer was actually valid!
333 if (unlikely(kref_read(&ref
->kref
) == 0))
344 int ttm_ref_object_add(struct ttm_object_file
*tfile
,
345 struct ttm_base_object
*base
,
346 enum ttm_ref_type ref_type
, bool *existed
,
347 bool require_existed
)
349 struct drm_open_hash
*ht
= &tfile
->ref_hash
[ref_type
];
350 struct ttm_ref_object
*ref
;
351 struct drm_hash_item
*hash
;
352 struct ttm_mem_global
*mem_glob
= tfile
->tdev
->mem_glob
;
353 struct ttm_operation_ctx ctx
= {
354 .interruptible
= false,
359 if (base
->tfile
!= tfile
&& !base
->shareable
)
365 while (ret
== -EINVAL
) {
367 ret
= drm_ht_find_item_rcu(ht
, base
->handle
, &hash
);
370 ref
= drm_hash_entry(hash
, struct ttm_ref_object
, hash
);
371 if (kref_get_unless_zero(&ref
->kref
)) {
381 ret
= ttm_mem_global_alloc(mem_glob
, sizeof(*ref
),
383 if (unlikely(ret
!= 0))
385 ref
= kmalloc(sizeof(*ref
), GFP_KERNEL
);
386 if (unlikely(ref
== NULL
)) {
387 ttm_mem_global_free(mem_glob
, sizeof(*ref
));
391 ref
->hash
.key
= base
->handle
;
394 ref
->ref_type
= ref_type
;
395 kref_init(&ref
->kref
);
397 spin_lock(&tfile
->lock
);
398 ret
= drm_ht_insert_item_rcu(ht
, &ref
->hash
);
400 if (likely(ret
== 0)) {
401 list_add_tail(&ref
->head
, &tfile
->ref_list
);
402 kref_get(&base
->refcount
);
403 spin_unlock(&tfile
->lock
);
409 spin_unlock(&tfile
->lock
);
410 BUG_ON(ret
!= -EINVAL
);
412 ttm_mem_global_free(mem_glob
, sizeof(*ref
));
419 static void __releases(tfile
->lock
) __acquires(tfile
->lock
)
420 ttm_ref_object_release(struct kref
*kref
)
422 struct ttm_ref_object
*ref
=
423 container_of(kref
, struct ttm_ref_object
, kref
);
424 struct ttm_base_object
*base
= ref
->obj
;
425 struct ttm_object_file
*tfile
= ref
->tfile
;
426 struct drm_open_hash
*ht
;
427 struct ttm_mem_global
*mem_glob
= tfile
->tdev
->mem_glob
;
429 ht
= &tfile
->ref_hash
[ref
->ref_type
];
430 (void)drm_ht_remove_item_rcu(ht
, &ref
->hash
);
431 list_del(&ref
->head
);
432 spin_unlock(&tfile
->lock
);
434 if (ref
->ref_type
!= TTM_REF_USAGE
&& base
->ref_obj_release
)
435 base
->ref_obj_release(base
, ref
->ref_type
);
437 ttm_base_object_unref(&ref
->obj
);
438 ttm_mem_global_free(mem_glob
, sizeof(*ref
));
439 kfree_rcu(ref
, rcu_head
);
440 spin_lock(&tfile
->lock
);
443 int ttm_ref_object_base_unref(struct ttm_object_file
*tfile
,
444 unsigned long key
, enum ttm_ref_type ref_type
)
446 struct drm_open_hash
*ht
= &tfile
->ref_hash
[ref_type
];
447 struct ttm_ref_object
*ref
;
448 struct drm_hash_item
*hash
;
451 spin_lock(&tfile
->lock
);
452 ret
= drm_ht_find_item(ht
, key
, &hash
);
453 if (unlikely(ret
!= 0)) {
454 spin_unlock(&tfile
->lock
);
457 ref
= drm_hash_entry(hash
, struct ttm_ref_object
, hash
);
458 kref_put(&ref
->kref
, ttm_ref_object_release
);
459 spin_unlock(&tfile
->lock
);
463 void ttm_object_file_release(struct ttm_object_file
**p_tfile
)
465 struct ttm_ref_object
*ref
;
466 struct list_head
*list
;
468 struct ttm_object_file
*tfile
= *p_tfile
;
471 spin_lock(&tfile
->lock
);
474 * Since we release the lock within the loop, we have to
475 * restart it from the beginning each time.
478 while (!list_empty(&tfile
->ref_list
)) {
479 list
= tfile
->ref_list
.next
;
480 ref
= list_entry(list
, struct ttm_ref_object
, head
);
481 ttm_ref_object_release(&ref
->kref
);
484 spin_unlock(&tfile
->lock
);
485 for (i
= 0; i
< TTM_REF_NUM
; ++i
)
486 drm_ht_remove(&tfile
->ref_hash
[i
]);
488 ttm_object_file_unref(&tfile
);
491 struct ttm_object_file
*ttm_object_file_init(struct ttm_object_device
*tdev
,
492 unsigned int hash_order
)
494 struct ttm_object_file
*tfile
= kmalloc(sizeof(*tfile
), GFP_KERNEL
);
499 if (unlikely(tfile
== NULL
))
502 spin_lock_init(&tfile
->lock
);
504 kref_init(&tfile
->refcount
);
505 INIT_LIST_HEAD(&tfile
->ref_list
);
507 for (i
= 0; i
< TTM_REF_NUM
; ++i
) {
508 ret
= drm_ht_create(&tfile
->ref_hash
[i
], hash_order
);
517 for (i
= 0; i
< j
; ++i
)
518 drm_ht_remove(&tfile
->ref_hash
[i
]);
525 struct ttm_object_device
*
526 ttm_object_device_init(struct ttm_mem_global
*mem_glob
,
527 unsigned int hash_order
,
528 const struct dma_buf_ops
*ops
)
530 struct ttm_object_device
*tdev
= kmalloc(sizeof(*tdev
), GFP_KERNEL
);
533 if (unlikely(tdev
== NULL
))
536 tdev
->mem_glob
= mem_glob
;
537 spin_lock_init(&tdev
->object_lock
);
538 atomic_set(&tdev
->object_count
, 0);
539 ret
= drm_ht_create(&tdev
->object_hash
, hash_order
);
541 goto out_no_object_hash
;
543 idr_init(&tdev
->idr
);
545 tdev
->dmabuf_release
= tdev
->ops
.release
;
546 tdev
->ops
.release
= ttm_prime_dmabuf_release
;
547 tdev
->dma_buf_size
= ttm_round_pot(sizeof(struct dma_buf
)) +
548 ttm_round_pot(sizeof(struct file
));
556 void ttm_object_device_release(struct ttm_object_device
**p_tdev
)
558 struct ttm_object_device
*tdev
= *p_tdev
;
562 WARN_ON_ONCE(!idr_is_empty(&tdev
->idr
));
563 idr_destroy(&tdev
->idr
);
564 drm_ht_remove(&tdev
->object_hash
);
570 * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
572 * @dma_buf: Non-refcounted pointer to a struct dma-buf.
574 * Obtain a file reference from a lookup structure that doesn't refcount
575 * the file, but synchronizes with its release method to make sure it has
576 * not been freed yet. See for example kref_get_unless_zero documentation.
577 * Returns true if refcounting succeeds, false otherwise.
579 * Nobody really wants this as a public API yet, so let it mature here
582 static bool __must_check
get_dma_buf_unless_doomed(struct dma_buf
*dmabuf
)
584 return atomic_long_inc_not_zero(&dmabuf
->file
->f_count
) != 0L;
588 * ttm_prime_refcount_release - refcount release method for a prime object.
590 * @p_base: Pointer to ttm_base_object pointer.
592 * This is a wrapper that calls the refcount_release founction of the
593 * underlying object. At the same time it cleans up the prime object.
594 * This function is called when all references to the base object we
595 * derive from are gone.
597 static void ttm_prime_refcount_release(struct ttm_base_object
**p_base
)
599 struct ttm_base_object
*base
= *p_base
;
600 struct ttm_prime_object
*prime
;
603 prime
= container_of(base
, struct ttm_prime_object
, base
);
604 BUG_ON(prime
->dma_buf
!= NULL
);
605 mutex_destroy(&prime
->mutex
);
606 if (prime
->refcount_release
)
607 prime
->refcount_release(&base
);
611 * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
615 * This function first calls the dma_buf release method the driver
616 * provides. Then it cleans up our dma_buf pointer used for lookup,
617 * and finally releases the reference the dma_buf has on our base
620 static void ttm_prime_dmabuf_release(struct dma_buf
*dma_buf
)
622 struct ttm_prime_object
*prime
=
623 (struct ttm_prime_object
*) dma_buf
->priv
;
624 struct ttm_base_object
*base
= &prime
->base
;
625 struct ttm_object_device
*tdev
= base
->tfile
->tdev
;
627 if (tdev
->dmabuf_release
)
628 tdev
->dmabuf_release(dma_buf
);
629 mutex_lock(&prime
->mutex
);
630 if (prime
->dma_buf
== dma_buf
)
631 prime
->dma_buf
= NULL
;
632 mutex_unlock(&prime
->mutex
);
633 ttm_mem_global_free(tdev
->mem_glob
, tdev
->dma_buf_size
);
634 ttm_base_object_unref(&base
);
638 * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
640 * @tfile: A struct ttm_object_file identifying the caller.
641 * @fd: The prime / dmabuf fd.
642 * @handle: The returned handle.
644 * This function returns a handle to an object that previously exported
645 * a dma-buf. Note that we don't handle imports yet, because we simply
646 * have no consumers of that implementation.
648 int ttm_prime_fd_to_handle(struct ttm_object_file
*tfile
,
651 struct ttm_object_device
*tdev
= tfile
->tdev
;
652 struct dma_buf
*dma_buf
;
653 struct ttm_prime_object
*prime
;
654 struct ttm_base_object
*base
;
657 dma_buf
= dma_buf_get(fd
);
659 return PTR_ERR(dma_buf
);
661 if (dma_buf
->ops
!= &tdev
->ops
)
664 prime
= (struct ttm_prime_object
*) dma_buf
->priv
;
666 *handle
= base
->handle
;
667 ret
= ttm_ref_object_add(tfile
, base
, TTM_REF_USAGE
, NULL
, false);
669 dma_buf_put(dma_buf
);
675 * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
677 * @tfile: Struct ttm_object_file identifying the caller.
678 * @handle: Handle to the object we're exporting from.
679 * @flags: flags for dma-buf creation. We just pass them on.
680 * @prime_fd: The returned file descriptor.
683 int ttm_prime_handle_to_fd(struct ttm_object_file
*tfile
,
684 uint32_t handle
, uint32_t flags
,
687 struct ttm_object_device
*tdev
= tfile
->tdev
;
688 struct ttm_base_object
*base
;
689 struct dma_buf
*dma_buf
;
690 struct ttm_prime_object
*prime
;
693 base
= ttm_base_object_lookup(tfile
, handle
);
694 if (unlikely(base
== NULL
||
695 base
->object_type
!= ttm_prime_type
)) {
700 prime
= container_of(base
, struct ttm_prime_object
, base
);
701 if (unlikely(!base
->shareable
)) {
706 ret
= mutex_lock_interruptible(&prime
->mutex
);
707 if (unlikely(ret
!= 0)) {
712 dma_buf
= prime
->dma_buf
;
713 if (!dma_buf
|| !get_dma_buf_unless_doomed(dma_buf
)) {
714 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
715 struct ttm_operation_ctx ctx
= {
716 .interruptible
= true,
719 exp_info
.ops
= &tdev
->ops
;
720 exp_info
.size
= prime
->size
;
721 exp_info
.flags
= flags
;
722 exp_info
.priv
= prime
;
725 * Need to create a new dma_buf, with memory accounting.
727 ret
= ttm_mem_global_alloc(tdev
->mem_glob
, tdev
->dma_buf_size
,
729 if (unlikely(ret
!= 0)) {
730 mutex_unlock(&prime
->mutex
);
734 dma_buf
= dma_buf_export(&exp_info
);
735 if (IS_ERR(dma_buf
)) {
736 ret
= PTR_ERR(dma_buf
);
737 ttm_mem_global_free(tdev
->mem_glob
,
739 mutex_unlock(&prime
->mutex
);
744 * dma_buf has taken the base object reference
747 prime
->dma_buf
= dma_buf
;
749 mutex_unlock(&prime
->mutex
);
751 ret
= dma_buf_fd(dma_buf
, flags
);
756 dma_buf_put(dma_buf
);
760 ttm_base_object_unref(&base
);
765 * ttm_prime_object_init - Initialize a ttm_prime_object
767 * @tfile: struct ttm_object_file identifying the caller
768 * @size: The size of the dma_bufs we export.
769 * @prime: The object to be initialized.
770 * @shareable: See ttm_base_object_init
771 * @type: See ttm_base_object_init
772 * @refcount_release: See ttm_base_object_init
773 * @ref_obj_release: See ttm_base_object_init
775 * Initializes an object which is compatible with the drm_prime model
776 * for data sharing between processes and devices.
778 int ttm_prime_object_init(struct ttm_object_file
*tfile
, size_t size
,
779 struct ttm_prime_object
*prime
, bool shareable
,
780 enum ttm_object_type type
,
781 void (*refcount_release
) (struct ttm_base_object
**),
782 void (*ref_obj_release
) (struct ttm_base_object
*,
783 enum ttm_ref_type ref_type
))
785 mutex_init(&prime
->mutex
);
786 prime
->size
= PAGE_ALIGN(size
);
787 prime
->real_type
= type
;
788 prime
->dma_buf
= NULL
;
789 prime
->refcount_release
= refcount_release
;
790 return ttm_base_object_init(tfile
, &prime
->base
, shareable
,
792 ttm_prime_refcount_release
,