From: Farah Kassabri <[email protected]>
Add drm_gem_prime_self_import() to detect and handle the case where a
PRIME dma-buf being imported was exported by the same DRM device.
When the dma-buf originates from the importing device and uses the
expected dma-buf ops, the helper returns the associated GEM object and
takes an extra reference on it. This allows drivers to bypass the
generic PRIME import path and avoid taking an additional reference on
the dma-buf file.
The helper simplifies self-import handling and avoids duplication of
this pattern across DRM drivers.
v2: Move drm_gem_prime_self_import() to drm_gem.h as a static inline
helper routine
Signed-off-by: Farah Kassabri <[email protected]>
Signed-off-by: Niranjana Vishwanathapura <[email protected]>
Reviewed-by: Matthew Brost <[email protected]>
---
drivers/gpu/drm/drm_prime.c | 10 ++-------
include/drm/drm_gem.h | 42 +++++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index 9b44c78cd77f..7a15c29052eb 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -974,15 +974,9 @@ struct drm_gem_object *drm_gem_prime_import_dev(struct
drm_device *dev,
struct drm_gem_object *obj;
int ret;
- if (drm_gem_is_prime_exported_dma_buf(dev, dma_buf)) {
- /*
- * Importing dmabuf exported from our own gem increases
- * refcount on gem itself instead of f_count of dmabuf.
- */
- obj = dma_buf->priv;
- drm_gem_object_get(obj);
+ obj = drm_gem_prime_self_import(dev, dma_buf,
&drm_gem_prime_dmabuf_ops);
+ if (obj)
return obj;
- }
if (!dev->driver->gem_prime_import_sg_table)
return ERR_PTR(-EINVAL);
diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
index 885244e375d3..238ec08f0d81 100644
--- a/include/drm/drm_gem.h
+++ b/include/drm/drm_gem.h
@@ -646,6 +646,48 @@ static inline bool drm_gem_is_imported(const struct
drm_gem_object *obj)
return !!obj->import_attach;
}
+/**
+ * drm_gem_prime_self_import - Attempt to import a dma-buf exported by this
DRM device
+ * @dev: DRM device performing the import
+ * @dma_buf: dma-buf to import
+ * @expected_ops: dma-buf ops used by this driver
+ *
+ * If @dma_buf was exported by this DRM device using @expected_ops, return the
+ * corresponding GEM object and take an extra reference on it. In this case,
+ * the import avoids taking a reference on the dma-buf file and instead bumps
+ * the GEM object's refcount directly.
+ *
+ * Comparing @dma_buf->ops against @expected_ops is plumbing: dma-buf has no
+ * first-class exporter identifier, so the ops pointer is used as a stand-in to
+ * recognise a buffer this driver exported. A dedicated exporter identity would
+ * let this be done without reaching into @dma_buf->ops.
+ *
+ * Returns:
+ * A referenced GEM object on success, or %NULL if the dma-buf was not exported
+ * by this device or does not match @expected_ops.
+ */
+static inline struct drm_gem_object *
+drm_gem_prime_self_import(struct drm_device *dev, struct dma_buf *dma_buf,
+ const struct dma_buf_ops *expected_ops)
+{
+ struct drm_gem_object *obj;
+
+ if (dma_buf->ops != expected_ops)
+ return NULL;
+
+ obj = dma_buf->priv;
+ if (obj->dev != dev)
+ return NULL;
+
+ /*
+ * Importing dmabuf exported from the driver's own gem increases
+ * refcount on gem itself instead of f_count of dmabuf.
+ */
+ drm_gem_object_get(obj);
+
+ return obj;
+}
+
#ifdef CONFIG_LOCKDEP
#define drm_gem_gpuva_assert_lock_held(gpuvm, obj) \
lockdep_assert(drm_gpuvm_immediate_mode(gpuvm) ? \
--
2.43.0