All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection
@ 2026-10-14 19:02 Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-14 19:02 UTC (permalink / raw)
  To: dri-devel, intel-xe, matthew.brost, tzimmermann

Detecting that a PRIME dma-buf being imported was exported by the same
DRM device is a pattern currently open-coded in many drivers (compare
dma_buf->ops against the driver's own ops, check obj->dev == dev, then
return the GEM object with an extra reference). This series adds generic
helpers for it and converts several drivers to use them.

Patch 1 adds drm_gem_prime_self_import(dev, dma_buf, ops): for a dma-buf
self-exported via the given ops, it returns the backing GEM object with
a reference, letting a driver short-circuit the generic PRIME import and
skip the extra dma-buf file reference. Patch 2 converts Xe to it.

Patch 3 adds drm_gem_prime_dma_buf_to_gem(), a thin wrapper that uses the
core PRIME ops, and replaces the drm_gem_is_prime_exported_dma_buf() bool
predicate with it so the shmem importer resolves the object directly
(the sole caller already wanted the referenced object).

Patches 4-6 convert panthor, panfrost and amdgpu to
drm_gem_prime_self_import(), removing the duplicated open-coded checks.

No intended functional change.

v2: Add patches 4-6
v2: Move drm_gem_prime_self_import() to drm_gem.h as a static inline
    helper routine

Assisted-by: GitHub-Copilot:claude-opus-4.8
Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>

Farah Kassabri (2):
  drm/gem: add helper to detect self-imported PRIME dma-bufs
  drm/xe/dma-buf: Use drm helper to detect dma-buf self-import

Niranjana Vishwanathapura (4):
  drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper
  drm/panthor: use drm_gem_prime_self_import() helper
  drm/panfrost: use drm_gem_prime_self_import() helper
  drm/amdgpu: use drm_gem_prime_self_import() helper

 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 14 ++-----
 drivers/gpu/drm/drm_gem_shmem_helper.c      | 14 +++----
 drivers/gpu/drm/drm_prime.c                 | 36 +++++++++---------
 drivers/gpu/drm/panfrost/panfrost_gem.c     | 10 ++---
 drivers/gpu/drm/panthor/panthor_gem.c       | 10 ++---
 drivers/gpu/drm/xe/xe_dma_buf.c             | 23 ++++++-----
 drivers/gpu/drm/xe/xe_dma_buf.h             |  2 +
 include/drm/drm_gem.h                       | 42 +++++++++++++++++++++
 include/drm/drm_prime.h                     |  4 +-
 9 files changed, 91 insertions(+), 64 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs
  2026-10-14 19:02 [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
@ 2026-10-14 19:02 ` Niranjana Vishwanathapura
  2026-10-15  7:27   ` Thomas Zimmermann
  2026-10-14 19:02 ` [PATCH v3 2/6] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import Niranjana Vishwanathapura
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-14 19:02 UTC (permalink / raw)
  To: dri-devel, intel-xe, matthew.brost, tzimmermann

From: Farah Kassabri <farah.kassabri@intel.com>

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 <farah.kassabri@intel.com>
Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
---
 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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 2/6] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import
  2026-10-14 19:02 [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
@ 2026-10-14 19:02 ` Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper Niranjana Vishwanathapura
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-14 19:02 UTC (permalink / raw)
  To: dri-devel, intel-xe, matthew.brost, tzimmermann

From: Farah Kassabri <farah.kassabri@intel.com>

Instead of driver-specific code, use the new drm helper
drm_gem_prime_self_import() to detect self-import of a dma-buf in Xe.

Expose the helper within Xe as xe_dma_buf_to_gem() for future use.

Signed-off-by: Farah Kassabri <farah.kassabri@intel.com>
Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
---
 drivers/gpu/drm/xe/xe_dma_buf.c | 23 +++++++++++++----------
 drivers/gpu/drm/xe/xe_dma_buf.h |  2 ++
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_dma_buf.c b/drivers/gpu/drm/xe/xe_dma_buf.c
index 6a85b292dee7..4e1b447f4235 100644
--- a/drivers/gpu/drm/xe/xe_dma_buf.c
+++ b/drivers/gpu/drm/xe/xe_dma_buf.c
@@ -239,6 +239,12 @@ static const struct dma_buf_ops xe_dmabuf_ops = {
 	.vunmap = drm_gem_dmabuf_vunmap,
 };
 
+struct drm_gem_object *xe_dma_buf_to_gem(struct drm_device *dev,
+					 struct dma_buf *dma_buf)
+{
+	return drm_gem_prime_self_import(dev, dma_buf, &xe_dmabuf_ops);
+}
+
 struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags)
 {
 	struct xe_bo *bo = gem_to_xe_bo(obj);
@@ -372,17 +378,14 @@ struct drm_gem_object *xe_gem_prime_import(struct drm_device *dev,
 	struct dma_buf_attachment *attach;
 	struct drm_gem_object *obj;
 
-	if (dma_buf->ops == &xe_dmabuf_ops) {
-		obj = dma_buf->priv;
-		if (obj->dev == dev &&
-		    !XE_TEST_ONLY(test && test->force_different_devices)) {
-			/*
-			 * Importing dmabuf exported from out own gem increases
-			 * refcount on gem itself instead of f_count of dmabuf.
-			 */
-			drm_gem_object_get(obj);
+	obj = xe_dma_buf_to_gem(dev, dma_buf);
+	if (obj) {
+		if (!XE_TEST_ONLY(test && test->force_different_devices))
 			return obj;
-		}
+
+		/* Test only: force different devices */
+		drm_gem_object_put(obj);
+		obj = NULL;
 	}
 
 	/*
diff --git a/drivers/gpu/drm/xe/xe_dma_buf.h b/drivers/gpu/drm/xe/xe_dma_buf.h
index 861dd28a862c..d0c7893a5a6f 100644
--- a/drivers/gpu/drm/xe/xe_dma_buf.h
+++ b/drivers/gpu/drm/xe/xe_dma_buf.h
@@ -8,6 +8,8 @@
 
 #include <drm/drm_gem.h>
 
+struct drm_gem_object *xe_dma_buf_to_gem(struct drm_device *dev,
+					 struct dma_buf *dma_buf);
 struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags);
 struct drm_gem_object *xe_gem_prime_import(struct drm_device *dev,
 					   struct dma_buf *dma_buf);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper
  2026-10-14 19:02 [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 2/6] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import Niranjana Vishwanathapura
@ 2026-10-14 19:02 ` Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 4/6] drm/panthor: use drm_gem_prime_self_import() helper Niranjana Vishwanathapura
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-14 19:02 UTC (permalink / raw)
  To: dri-devel, intel-xe, matthew.brost, tzimmermann

drm_gem_is_prime_exported_dma_buf() is a bool predicate that open-codes
the core PRIME self-import check. Its only caller, the shmem importer,
then repeats the dma_buf->priv lookup and the reference get.

Replace it with drm_gem_prime_dma_buf_to_gem(), a thin wrapper around
drm_gem_prime_self_import() that returns the backing GEM object -- with
a reference taken -- for a core-PRIME self-export, or NULL otherwise.
This consolidates self-import handling onto drm_gem_prime_self_import()
and lets drm_gem_shmem_prime_import_no_map() reuse the object directly.

Unlike the removed predicate, the helper takes a reference on the
returned object; the sole caller already wanted the referenced object.

Assisted-by: GitHub-Copilot:claude-opus-4.8
Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 14 ++++++--------
 drivers/gpu/drm/drm_prime.c            | 26 +++++++++++++++-----------
 include/drm/drm_prime.h                |  4 ++--
 3 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index d654a22a30de..a0e161f2333e 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -954,15 +954,13 @@ struct drm_gem_object *drm_gem_shmem_prime_import_no_map(struct drm_device *dev,
 	size_t size;
 	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);
+	/*
+	 * Importing a dmabuf exported from our own gem increases the refcount
+	 * on the gem itself instead of the f_count of the dmabuf.
+	 */
+	obj = drm_gem_prime_dma_buf_to_gem(dev, dma_buf);
+	if (obj)
 		return obj;
-	}
 
 	attach = dma_buf_attach(dma_buf, dev->dev);
 	if (IS_ERR(attach))
diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index 7a15c29052eb..d4adc732e7f6 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -933,23 +933,27 @@ EXPORT_SYMBOL(drm_gem_prime_export);
 
 
 /**
- * drm_gem_is_prime_exported_dma_buf -
- * checks if the DMA-BUF was exported from a GEM object belonging to @dev.
+ * drm_gem_prime_dma_buf_to_gem - GEM object backing a self-exported PRIME dma-buf
  * @dev: drm_device to check against
  * @dma_buf: dma-buf object to import
  *
- * Return: true if the DMA-BUF was exported from a GEM object belonging
- * to @dev, false otherwise.
+ * If @dma_buf is a PRIME export of a GEM object that belongs to @dev and uses
+ * the core PRIME dma-buf ops, return that GEM object with an extra reference
+ * taken, so a self-import reuses the GEM object instead of referencing the
+ * dma-buf file. Returns NULL for a foreign dma-buf or one using driver-private
+ * ops; such drivers call drm_gem_prime_self_import() with their own ops.
+ *
+ * The returned object carries a reference the caller must drop with
+ * drm_gem_object_put().
+ *
+ * Return: the referenced GEM object on self-import, or NULL otherwise.
  */
-
-bool drm_gem_is_prime_exported_dma_buf(struct drm_device *dev,
-				       struct dma_buf *dma_buf)
+struct drm_gem_object *drm_gem_prime_dma_buf_to_gem(struct drm_device *dev,
+						    struct dma_buf *dma_buf)
 {
-	struct drm_gem_object *obj = dma_buf->priv;
-
-	return (dma_buf->ops == &drm_gem_prime_dmabuf_ops) && (obj->dev == dev);
+	return drm_gem_prime_self_import(dev, dma_buf, &drm_gem_prime_dmabuf_ops);
 }
-EXPORT_SYMBOL(drm_gem_is_prime_exported_dma_buf);
+EXPORT_SYMBOL(drm_gem_prime_dma_buf_to_gem);
 
 /**
  * drm_gem_prime_import_dev - core implementation of the import callback
diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h
index f50f862f0d8b..a012da82069a 100644
--- a/include/drm/drm_prime.h
+++ b/include/drm/drm_prime.h
@@ -100,8 +100,8 @@ struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
 unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt);
 
 /* helper functions for importing */
-bool drm_gem_is_prime_exported_dma_buf(struct drm_device *dev,
-				       struct dma_buf *dma_buf);
+struct drm_gem_object *drm_gem_prime_dma_buf_to_gem(struct drm_device *dev,
+						    struct dma_buf *dma_buf);
 
 struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
 						struct dma_buf *dma_buf,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 4/6] drm/panthor: use drm_gem_prime_self_import() helper
  2026-10-14 19:02 [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
                   ` (2 preceding siblings ...)
  2026-10-14 19:02 ` [PATCH v3 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper Niranjana Vishwanathapura
@ 2026-10-14 19:02 ` Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 5/6] drm/panfrost: " Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 6/6] drm/amdgpu: " Niranjana Vishwanathapura
  5 siblings, 0 replies; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-14 19:02 UTC (permalink / raw)
  To: dri-devel, intel-xe, matthew.brost, tzimmermann

Replace the open-coded self-import check with the drm core
helper drm_gem_prime_self_import(), which performs the same ops and
device match and returns the backing GEM object with a reference.

Assisted-by: GitHub-Copilot:claude-opus-4.8
Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
---
 drivers/gpu/drm/panthor/panthor_gem.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/panthor/panthor_gem.c b/drivers/gpu/drm/panthor/panthor_gem.c
index 72908be5e144..7f713c5f19cb 100644
--- a/drivers/gpu/drm/panthor/panthor_gem.c
+++ b/drivers/gpu/drm/panthor/panthor_gem.c
@@ -584,15 +584,11 @@ struct drm_gem_object *
 panthor_gem_prime_import(struct drm_device *dev,
 			 struct dma_buf *dma_buf)
 {
-	struct drm_gem_object *obj = dma_buf->priv;
+	struct drm_gem_object *obj;
 
-	if (dma_buf->ops == &panthor_dma_buf_ops && obj->dev == dev) {
-		/* Importing dmabuf exported from our own gem increases
-		 * refcount on gem itself instead of f_count of dmabuf.
-		 */
-		drm_gem_object_get(obj);
+	obj = drm_gem_prime_self_import(dev, dma_buf, &panthor_dma_buf_ops);
+	if (obj)
 		return obj;
-	}
 
 	return drm_gem_prime_import(dev, dma_buf);
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 5/6] drm/panfrost: use drm_gem_prime_self_import() helper
  2026-10-14 19:02 [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
                   ` (3 preceding siblings ...)
  2026-10-14 19:02 ` [PATCH v3 4/6] drm/panthor: use drm_gem_prime_self_import() helper Niranjana Vishwanathapura
@ 2026-10-14 19:02 ` Niranjana Vishwanathapura
  2026-10-14 19:02 ` [PATCH v3 6/6] drm/amdgpu: " Niranjana Vishwanathapura
  5 siblings, 0 replies; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-14 19:02 UTC (permalink / raw)
  To: dri-devel, intel-xe, matthew.brost, tzimmermann

Replace the open-coded self-import check with the drm core
helper drm_gem_prime_self_import(), which performs the same ops and
device match and returns the backing GEM object with a reference.

Assisted-by: GitHub-Copilot:claude-opus-4.8
Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
---
 drivers/gpu/drm/panfrost/panfrost_gem.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c
index 3a7fce428898..19fcbffb3e94 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.c
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
@@ -386,15 +386,11 @@ struct drm_gem_object *
 panfrost_gem_prime_import(struct drm_device *dev,
 			  struct dma_buf *dma_buf)
 {
-	struct drm_gem_object *obj = dma_buf->priv;
+	struct drm_gem_object *obj;
 
-	if (dma_buf->ops == &panfrost_dma_buf_ops && obj->dev == dev) {
-		/* Importing dmabuf exported from our own gem increases
-		 * refcount on gem itself instead of f_count of dmabuf.
-		 */
-		drm_gem_object_get(obj);
+	obj = drm_gem_prime_self_import(dev, dma_buf, &panfrost_dma_buf_ops);
+	if (obj)
 		return obj;
-	}
 
 	return drm_gem_prime_import(dev, dma_buf);
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3 6/6] drm/amdgpu: use drm_gem_prime_self_import() helper
  2026-10-14 19:02 [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
                   ` (4 preceding siblings ...)
  2026-10-14 19:02 ` [PATCH v3 5/6] drm/panfrost: " Niranjana Vishwanathapura
@ 2026-10-14 19:02 ` Niranjana Vishwanathapura
  5 siblings, 0 replies; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-14 19:02 UTC (permalink / raw)
  To: dri-devel, intel-xe, matthew.brost, tzimmermann

Replace the open-coded self-import check with the drm core
helper drm_gem_prime_self_import(), which performs the same ops and
device match and returns the backing GEM object with a reference.

Assisted-by: GitHub-Copilot:claude-opus-4.8
Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c
index b33c300e26e2..5863f6605308 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c
@@ -549,17 +549,9 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
 	struct dma_buf_attachment *attach;
 	struct drm_gem_object *obj;
 
-	if (dma_buf->ops == &amdgpu_dmabuf_ops) {
-		obj = dma_buf->priv;
-		if (obj->dev == dev) {
-			/*
-			 * Importing dmabuf exported from out own gem increases
-			 * refcount on gem itself instead of f_count of dmabuf.
-			 */
-			drm_gem_object_get(obj);
-			return obj;
-		}
-	}
+	obj = drm_gem_prime_self_import(dev, dma_buf, &amdgpu_dmabuf_ops);
+	if (obj)
+		return obj;
 
 	obj = amdgpu_dma_buf_create_obj(dev, dma_buf);
 	if (IS_ERR(obj))
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs
  2026-10-14 19:02 ` [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
@ 2026-10-15  7:27   ` Thomas Zimmermann
  2026-10-16  6:26     ` Niranjana Vishwanathapura
  0 siblings, 1 reply; 11+ messages in thread
From: Thomas Zimmermann @ 2026-10-15  7:27 UTC (permalink / raw)
  To: Niranjana Vishwanathapura, dri-devel, intel-xe, matthew.brost

Hi

Am 14.10.26 um 21:02 schrieb Niranjana Vishwanathapura:
> From: Farah Kassabri <farah.kassabri@intel.com>
>
> 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

Why even bother submitting this? It is the same series that I already 
declined with a few cosmetic changes.

The abstract entry point for importing is drm_driver.gem_prime_import. 
The implementation is driver specific. No need to over abstract. Keeping 
drm_gem_object_get() separate from the test is a feature, not a bug BTW.

If you want to improve the status quo, change dma-buf such that prime 
works without relying on the vtable and ->priv. I outlined this in my 
earlier replies.

Best regards
Thomas



>
> Signed-off-by: Farah Kassabri <farah.kassabri@intel.com>
> Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
> Reviewed-by: Matthew Brost <matthew.brost@intel.com>
> ---
>   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) ? \

-- 
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs
  2026-10-15  7:27   ` Thomas Zimmermann
@ 2026-10-16  6:26     ` Niranjana Vishwanathapura
  2026-10-16  7:46       ` Thomas Zimmermann
  0 siblings, 1 reply; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-16  6:26 UTC (permalink / raw)
  To: Thomas Zimmermann; +Cc: dri-devel, intel-xe, matthew.brost

On Thu, Oct 15, 2026 at 09:27:08AM +0200, Thomas Zimmermann wrote:
>Hi
>
>Am 14.10.26 um 21:02 schrieb Niranjana Vishwanathapura:
>>From: Farah Kassabri <farah.kassabri@intel.com>
>>
>>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
>
>Why even bother submitting this? It is the same series that I already 
>declined with a few cosmetic changes.
>
>The abstract entry point for importing is drm_driver.gem_prime_import. 
>The implementation is driver specific. No need to over abstract. 
>Keeping drm_gem_object_get() separate from the test is a feature, not 
>a bug BTW.
>
>If you want to improve the status quo, change dma-buf such that prime 
>works without relying on the vtable and ->priv. I outlined this in my 
>earlier replies.
>

Hi Thomas,

As mentioned in other thread, your earlier suggesion of
'__drm_gem_is_exported_dma_buf(dev, ops, dma_buf)' is not fully helpful
as it would require drivers to still take object reference. Besides,
this also depends on 'ops'.

To me the 'dma_buf_ops as export identifer' seems to be well accepted
idiom, though we may say it shouldn't be in stricter sense.

But if we were not to relay on vtable and ->priv, then it looks like
the best alternative will be to have DRM subsystem maintain a list of
exported dma-buf objects per drm device. Then during import, parse this
list to determine if the import is self-import or not.

I am not sure if it is worth to add this list just for this simple helper
routine. If using dma_buf ops is no go, then yah, let us drop this series,
keep current code and may be look into this alternative later through an RFC.

Thanks,
Niranjana

>Best regards
>Thomas
>
>
>
>>
>>Signed-off-by: Farah Kassabri <farah.kassabri@intel.com>
>>Signed-off-by: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
>>Reviewed-by: Matthew Brost <matthew.brost@intel.com>
>>---
>>  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) ? \
>
>-- 
>--
>Thomas Zimmermann
>Graphics Driver Developer
>SUSE Software Solutions Germany GmbH
>Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
>GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
>
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs
  2026-10-16  6:26     ` Niranjana Vishwanathapura
@ 2026-10-16  7:46       ` Thomas Zimmermann
  2026-10-18 20:25         ` Niranjana Vishwanathapura
  0 siblings, 1 reply; 11+ messages in thread
From: Thomas Zimmermann @ 2026-10-16  7:46 UTC (permalink / raw)
  To: Niranjana Vishwanathapura; +Cc: dri-devel, intel-xe, matthew.brost

Hi

Am 16.10.26 um 08:26 schrieb Niranjana Vishwanathapura:
> On Thu, Oct 15, 2026 at 09:27:08AM +0200, Thomas Zimmermann wrote:
>> Hi
>>
>> Am 14.10.26 um 21:02 schrieb Niranjana Vishwanathapura:
>>> From: Farah Kassabri <farah.kassabri@intel.com>
>>>
>>> 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
>>
>> Why even bother submitting this? It is the same series that I already 
>> declined with a few cosmetic changes.
>>
>> The abstract entry point for importing is 
>> drm_driver.gem_prime_import. The implementation is driver specific. 
>> No need to over abstract. Keeping drm_gem_object_get() separate from 
>> the test is a feature, not a bug BTW.
>>
>> If you want to improve the status quo, change dma-buf such that prime 
>> works without relying on the vtable and ->priv. I outlined this in my 
>> earlier replies.
>>
>
> Hi Thomas,
>
> As mentioned in other thread, your earlier suggesion of
> '__drm_gem_is_exported_dma_buf(dev, ops, dma_buf)' is not fully helpful
> as it would require drivers to still take object reference. Besides,
> this also depends on 'ops'.

This is a feature. These are two distinct operations. Taking the object 
ref is another of those implementation detail where drivers might want 
to do something else.

>
> To me the 'dma_buf_ops as export identifer' seems to be well accepted
> idiom, though we may say it shouldn't be in stricter sense.
>
> But if we were not to relay on vtable and ->priv, then it looks like
> the best alternative will be to have DRM subsystem maintain a list of
> exported dma-buf objects per drm device. Then during import, parse this
> list to determine if the import is self-import or not.
>
> I am not sure if it is worth to add this list just for this simple helper
> routine. If using dma_buf ops is no go, then yah, let us drop this 
> series,
> keep current code and may be look into this alternative later through 
> an RFC.

As I mention before, the abstraction is drm_driver.gem_prime_import. 
Anything behind that is driver-specific. A real improvement should 
provide a driver-generic implementation of gem_prime_import. This needs 
changes to dma_buf and to the prime helpers. OTOH gem_prime_import is 
already fairly abstract. There are no low-hanging fruits here IMHO.

Best regards
Thomas


>
> Thanks,
> Niranjana
>
>> Best regards
>> Thomas
>>
>>
>>
>>>
>>> Signed-off-by: Farah Kassabri <farah.kassabri@intel.com>
>>> Signed-off-by: Niranjana Vishwanathapura 
>>> <niranjana.vishwanathapura@intel.com>
>>> Reviewed-by: Matthew Brost <matthew.brost@intel.com>
>>> ---
>>>  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) ? \
>>
>> -- 
>> -- 
>> Thomas Zimmermann
>> Graphics Driver Developer
>> SUSE Software Solutions Germany GmbH
>> Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
>> GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
>>
>>

-- 
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs
  2026-10-16  7:46       ` Thomas Zimmermann
@ 2026-10-18 20:25         ` Niranjana Vishwanathapura
  0 siblings, 0 replies; 11+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-18 20:25 UTC (permalink / raw)
  To: Thomas Zimmermann; +Cc: dri-devel, intel-xe, matthew.brost

On Fri, Oct 16, 2026 at 09:46:02AM +0200, Thomas Zimmermann wrote:
>Hi
>
>Am 16.10.26 um 08:26 schrieb Niranjana Vishwanathapura:
>>On Thu, Oct 15, 2026 at 09:27:08AM +0200, Thomas Zimmermann wrote:
>>>Hi
>>>
>>>Am 14.10.26 um 21:02 schrieb Niranjana Vishwanathapura:
>>>>From: Farah Kassabri <farah.kassabri@intel.com>
>>>>
>>>>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
>>>
>>>Why even bother submitting this? It is the same series that I 
>>>already declined with a few cosmetic changes.
>>>
>>>The abstract entry point for importing is 
>>>drm_driver.gem_prime_import. The implementation is driver 
>>>specific. No need to over abstract. Keeping drm_gem_object_get() 
>>>separate from the test is a feature, not a bug BTW.
>>>
>>>If you want to improve the status quo, change dma-buf such that 
>>>prime works without relying on the vtable and ->priv. I outlined 
>>>this in my earlier replies.
>>>
>>
>>Hi Thomas,
>>
>>As mentioned in other thread, your earlier suggesion of
>>'__drm_gem_is_exported_dma_buf(dev, ops, dma_buf)' is not fully helpful
>>as it would require drivers to still take object reference. Besides,
>>this also depends on 'ops'.
>
>This is a feature. These are two distinct operations. Taking the 
>object ref is another of those implementation detail where drivers 
>might want to do something else.
>
>>
>>To me the 'dma_buf_ops as export identifer' seems to be well accepted
>>idiom, though we may say it shouldn't be in stricter sense.
>>

Ok

>>But if we were not to relay on vtable and ->priv, then it looks like
>>the best alternative will be to have DRM subsystem maintain a list of
>>exported dma-buf objects per drm device. Then during import, parse this
>>list to determine if the import is self-import or not.
>>
>>I am not sure if it is worth to add this list just for this simple helper
>>routine. If using dma_buf ops is no go, then yah, let us drop this 
>>series,
>>keep current code and may be look into this alternative later 
>>through an RFC.
>
>As I mention before, the abstraction is drm_driver.gem_prime_import. 
>Anything behind that is driver-specific. A real improvement should 
>provide a driver-generic implementation of gem_prime_import. This 
>needs changes to dma_buf and to the prime helpers. OTOH 
>gem_prime_import is already fairly abstract. There are no low-hanging 
>fruits here IMHO.
>

Ok, will drop this and keep any abstration within the xe driver if
required.

Thanks,
Niranjana 

>Best regards
>Thomas
>
>
>>
>>Thanks,
>>Niranjana
>>
>>>Best regards
>>>Thomas
>>>
>>>
>>>
>>>>
>>>>Signed-off-by: Farah Kassabri <farah.kassabri@intel.com>
>>>>Signed-off-by: Niranjana Vishwanathapura 
>>>><niranjana.vishwanathapura@intel.com>
>>>>Reviewed-by: Matthew Brost <matthew.brost@intel.com>
>>>>---
>>>> 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) ? \
>>>
>>>-- 
>>>-- 
>>>Thomas Zimmermann
>>>Graphics Driver Developer
>>>SUSE Software Solutions Germany GmbH
>>>Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
>>>GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
>>>
>>>
>
>-- 
>--
>Thomas Zimmermann
>Graphics Driver Developer
>SUSE Software Solutions Germany GmbH
>Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
>GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
>
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-10-18 20:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-14 19:02 [PATCH v3 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
2026-10-14 19:02 ` [PATCH v3 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
2026-10-15  7:27   ` Thomas Zimmermann
2026-10-16  6:26     ` Niranjana Vishwanathapura
2026-10-16  7:46       ` Thomas Zimmermann
2026-10-18 20:25         ` Niranjana Vishwanathapura
2026-10-14 19:02 ` [PATCH v3 2/6] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import Niranjana Vishwanathapura
2026-10-14 19:02 ` [PATCH v3 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper Niranjana Vishwanathapura
2026-10-14 19:02 ` [PATCH v3 4/6] drm/panthor: use drm_gem_prime_self_import() helper Niranjana Vishwanathapura
2026-10-14 19:02 ` [PATCH v3 5/6] drm/panfrost: " Niranjana Vishwanathapura
2026-10-14 19:02 ` [PATCH v3 6/6] drm/amdgpu: " Niranjana Vishwanathapura

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.