summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Hellstrom <thellstrom@vmware.com>2018-09-18 10:04:55 +0200
committerThomas Hellstrom <thellstrom@vmware.com>2018-09-25 11:15:38 +0200
commitcf50092355f7ed2e2997630edd68f8be725c89d4 (patch)
treec7916edf329067e0ef4f4985aee72582d4d25cac
parent13cc85628e40d4ece8272bb46ed0b6d0b490253d (diff)
vmwgfx/ttm: Look up objects without taking a reference
Typically when we look up objects under the rcu lock, we take a reference to make sure the returned object pointer is valid. Now provide a function to look up an object and instead of taking a reference to it, keep the rcu lock held when returning the object pointer. This means that the object pointer is valid as long as the rcu lock is held, but the object may be doomed (its refcount may be zero). Any persistent usage of the object pointer outside of the rcu lock requires a reference to be taken using kref_get_unless_zero(). Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com> Reviewed-by: Sinclair Yeh <syeh@vmware.com>
-rw-r--r--ttm/ttm_object.c35
-rw-r--r--ttm/ttm_object.h15
2 files changed, 50 insertions, 0 deletions
diff --git a/ttm/ttm_object.c b/ttm/ttm_object.c
index c749e2d..c1bacb7 100644
--- a/ttm/ttm_object.c
+++ b/ttm/ttm_object.c
@@ -238,6 +238,41 @@ void ttm_base_object_unref(struct ttm_base_object **p_base)
}
EXPORT_SYMBOL(ttm_base_object_unref);
+/**
+ * ttm_base_object_noref_lookup - look up a base object without reference
+ * @tfile: The struct ttm_object_file the object is registered with.
+ * @key: The object handle.
+ *
+ * This function looks up a ttm base object and returns a pointer to it
+ * without refcounting the pointer. The returned pointer is only valid
+ * until ttm_base_object_noref_release() is called, and the object
+ * pointed to by the returned pointer may be doomed. Any persistent usage
+ * of the object requires a refcount to be taken using kref_get_unless_zero().
+ * Iff this function returns successfully it needs to be paired with
+ * ttm_base_object_noref_release() and no sleeping- or scheduling functions
+ * may be called inbetween these function callse.
+ *
+ * Return: A pointer to the object if successful or NULL otherwise.
+ */
+struct ttm_base_object *
+ttm_base_object_noref_lookup(struct ttm_object_file *tfile, uint32_t key)
+{
+ struct drm_hash_item *hash;
+ struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
+ int ret;
+
+ rcu_read_lock();
+ ret = drm_ht_find_item_rcu(ht, key, &hash);
+ if (ret) {
+ rcu_read_unlock();
+ return NULL;
+ }
+
+ __release(RCU);
+ return drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
+}
+EXPORT_SYMBOL(ttm_base_object_noref_lookup);
+
struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
uint32_t key)
{
diff --git a/ttm/ttm_object.h b/ttm/ttm_object.h
index a598385..8b1a694 100644
--- a/ttm/ttm_object.h
+++ b/ttm/ttm_object.h
@@ -367,4 +367,19 @@ extern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
* per idr.
*/
#define TTM_OBJ_EXTRA_SIZE 128
+
+struct ttm_base_object *
+ttm_base_object_noref_lookup(struct ttm_object_file *tfile, uint32_t key);
+
+/**
+ * ttm_base_object_noref_release - release a base object pointer looked up
+ * without reference
+ *
+ * Releases a base object pointer looked up with ttm_base_object_noref_lookup().
+ */
+static inline void ttm_base_object_noref_release(void)
+{
+ __acquire(RCU);
+ rcu_read_unlock();
+}
#endif