* [PATCH v2 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
@ 2026-10-13 1:06 ` Niranjana Vishwanathapura
2026-10-13 1:06 ` [PATCH v2 2/6] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import Niranjana Vishwanathapura
` (6 subsequent siblings)
7 siblings, 0 replies; 13+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-13 1:06 UTC (permalink / raw)
To: dri-devel, intel-xe, matthew.brost
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.
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 | 48 ++++++++++++++++++++++++++++++-------
include/drm/drm_prime.h | 4 ++++
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..588910c250b8 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);
@@ -1116,3 +1110,41 @@ void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
dma_buf_put(dma_buf);
}
EXPORT_SYMBOL(drm_prime_gem_destroy);
+
+/**
+ * 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.
+ *
+ * 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.
+ */
+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 our own gem increases
+ * refcount on gem itself instead of f_count of dmabuf.
+ */
+ drm_gem_object_get(obj);
+
+ return obj;
+}
+EXPORT_SYMBOL(drm_gem_prime_self_import);
diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h
index f50f862f0d8b..d378404f8ab4 100644
--- a/include/drm/drm_prime.h
+++ b/include/drm/drm_prime.h
@@ -54,6 +54,7 @@ struct device;
struct dma_buf_export_info;
struct dma_buf;
struct dma_buf_attachment;
+struct dma_buf_ops;
struct iosys_map;
enum dma_data_direction;
@@ -108,6 +109,9 @@ struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
struct device *attach_dev);
struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
struct dma_buf *dma_buf);
+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);
void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
--
2.43.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 2/6] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
2026-10-13 1:06 ` [PATCH v2 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
@ 2026-10-13 1:06 ` Niranjana Vishwanathapura
2026-10-13 1:06 ` [PATCH v2 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper Niranjana Vishwanathapura
` (5 subsequent siblings)
7 siblings, 0 replies; 13+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-13 1:06 UTC (permalink / raw)
To: dri-devel, intel-xe, matthew.brost
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] 13+ messages in thread* [PATCH v2 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
2026-10-13 1:06 ` [PATCH v2 1/6] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
2026-10-13 1:06 ` [PATCH v2 2/6] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import Niranjana Vishwanathapura
@ 2026-10-13 1:06 ` Niranjana Vishwanathapura
2026-10-13 1:36 ` Matthew Brost
2026-10-13 1:06 ` [PATCH v2 4/6] drm/panthor: use drm_gem_prime_self_import() helper Niranjana Vishwanathapura
` (4 subsequent siblings)
7 siblings, 1 reply; 13+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-13 1:06 UTC (permalink / raw)
To: dri-devel, intel-xe, matthew.brost
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 588910c250b8..275c5661528d 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 d378404f8ab4..02665205aed1 100644
--- a/include/drm/drm_prime.h
+++ b/include/drm/drm_prime.h
@@ -101,8 +101,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] 13+ messages in thread* Re: [PATCH v2 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper
2026-10-13 1:06 ` [PATCH v2 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper Niranjana Vishwanathapura
@ 2026-10-13 1:36 ` Matthew Brost
2026-10-13 7:17 ` Thomas Zimmermann
0 siblings, 1 reply; 13+ messages in thread
From: Matthew Brost @ 2026-10-13 1:36 UTC (permalink / raw)
To: Niranjana Vishwanathapura; +Cc: dri-devel, intel-xe
On Mon, Oct 12, 2026 at 06:06:15PM -0700, Niranjana Vishwanathapura wrote:
> 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
Assisted-by: LLM
I was going to suggest this last rev too as I noticed
drm_gem_shmem_prime_import_no_map called
drm_gem_is_prime_exported_dma_buf which should prefer the new helper but
shmem couldn't look at drm_gem_prime_dmabuf_ops.
This is a good cleanup:
Reviewed-by: Matthew Brost <matthew.brost@intel.com>
> 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 588910c250b8..275c5661528d 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 d378404f8ab4..02665205aed1 100644
> --- a/include/drm/drm_prime.h
> +++ b/include/drm/drm_prime.h
> @@ -101,8 +101,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 [flat|nested] 13+ messages in thread* Re: [PATCH v2 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper
2026-10-13 1:36 ` Matthew Brost
@ 2026-10-13 7:17 ` Thomas Zimmermann
2026-10-13 18:37 ` Matthew Brost
0 siblings, 1 reply; 13+ messages in thread
From: Thomas Zimmermann @ 2026-10-13 7:17 UTC (permalink / raw)
To: Matthew Brost, Niranjana Vishwanathapura; +Cc: dri-devel, intel-xe
Hi
Am 13.10.26 um 03:36 schrieb Matthew Brost:
> On Mon, Oct 12, 2026 at 06:06:15PM -0700, Niranjana Vishwanathapura wrote:
>> 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
> Assisted-by: LLM
>
> I was going to suggest this last rev too as I noticed
> drm_gem_shmem_prime_import_no_map called
> drm_gem_is_prime_exported_dma_buf which should prefer the new helper but
> shmem couldn't look at drm_gem_prime_dmabuf_ops.
>
> This is a good cleanup:
No it's not. Please see my reply to the cover letter.
Best regards
Thomas
>
> Reviewed-by: Matthew Brost <matthew.brost@intel.com>
>
>> 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 588910c250b8..275c5661528d 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 d378404f8ab4..02665205aed1 100644
>> --- a/include/drm/drm_prime.h
>> +++ b/include/drm/drm_prime.h
>> @@ -101,8 +101,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
>>
--
--
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] 13+ messages in thread* Re: [PATCH v2 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper
2026-10-13 7:17 ` Thomas Zimmermann
@ 2026-10-13 18:37 ` Matthew Brost
0 siblings, 0 replies; 13+ messages in thread
From: Matthew Brost @ 2026-10-13 18:37 UTC (permalink / raw)
To: Thomas Zimmermann; +Cc: Niranjana Vishwanathapura, dri-devel, intel-xe
On Tue, Oct 13, 2026 at 09:17:53AM +0200, Thomas Zimmermann wrote:
> Hi
>
> Am 13.10.26 um 03:36 schrieb Matthew Brost:
> > On Mon, Oct 12, 2026 at 06:06:15PM -0700, Niranjana Vishwanathapura wrote:
> > > 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
> > Assisted-by: LLM
> >
> > I was going to suggest this last rev too as I noticed
> > drm_gem_shmem_prime_import_no_map called
> > drm_gem_is_prime_exported_dma_buf which should prefer the new helper but
> > shmem couldn't look at drm_gem_prime_dmabuf_ops.
> >
> > This is a good cleanup:
>
> No it's not. Please see my reply to the cover letter.
>
Ok, I can't say I agree, but I'm not going to die on this hill. Still,
here's my two cents.
I'm not sure I agree that comparing a vfunc is a hack, but let's assume
it is. Is it better to have four drivers plus two cases in DRM core all
open-coding this logic, or to consolidate it in a single place so that
if we find a better approach later, we have a central point where it can
be changed?
To me, the answer is obvious, but again, I'm fine with ignoring this.
Matt
> Best regards
> Thomas
>
> >
> > Reviewed-by: Matthew Brost <matthew.brost@intel.com>
> >
> > > 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 588910c250b8..275c5661528d 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 d378404f8ab4..02665205aed1 100644
> > > --- a/include/drm/drm_prime.h
> > > +++ b/include/drm/drm_prime.h
> > > @@ -101,8 +101,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
> > >
>
> --
> --
> 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] 13+ messages in thread
* [PATCH v2 4/6] drm/panthor: use drm_gem_prime_self_import() helper
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
` (2 preceding siblings ...)
2026-10-13 1:06 ` [PATCH v2 3/6] drm/gem: add drm_gem_prime_dma_buf_to_gem() self-import helper Niranjana Vishwanathapura
@ 2026-10-13 1:06 ` Niranjana Vishwanathapura
2026-10-13 1:06 ` [PATCH v2 5/6] drm/panfrost: " Niranjana Vishwanathapura
` (3 subsequent siblings)
7 siblings, 0 replies; 13+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-13 1:06 UTC (permalink / raw)
To: dri-devel, intel-xe, matthew.brost
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] 13+ messages in thread* [PATCH v2 5/6] drm/panfrost: use drm_gem_prime_self_import() helper
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
` (3 preceding siblings ...)
2026-10-13 1:06 ` [PATCH v2 4/6] drm/panthor: use drm_gem_prime_self_import() helper Niranjana Vishwanathapura
@ 2026-10-13 1:06 ` Niranjana Vishwanathapura
2026-10-13 1:06 ` [PATCH v2 6/6] drm/amdgpu: " Niranjana Vishwanathapura
` (2 subsequent siblings)
7 siblings, 0 replies; 13+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-13 1:06 UTC (permalink / raw)
To: dri-devel, intel-xe, matthew.brost
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] 13+ messages in thread* [PATCH v2 6/6] drm/amdgpu: use drm_gem_prime_self_import() helper
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
` (4 preceding siblings ...)
2026-10-13 1:06 ` [PATCH v2 5/6] drm/panfrost: " Niranjana Vishwanathapura
@ 2026-10-13 1:06 ` Niranjana Vishwanathapura
2026-10-13 1:38 ` [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Matthew Brost
2026-10-13 7:17 ` Thomas Zimmermann
7 siblings, 0 replies; 13+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-13 1:06 UTC (permalink / raw)
To: dri-devel, intel-xe, matthew.brost
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] 13+ messages in thread* Re: [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
` (5 preceding siblings ...)
2026-10-13 1:06 ` [PATCH v2 6/6] drm/amdgpu: " Niranjana Vishwanathapura
@ 2026-10-13 1:38 ` Matthew Brost
2026-10-13 7:17 ` Thomas Zimmermann
7 siblings, 0 replies; 13+ messages in thread
From: Matthew Brost @ 2026-10-13 1:38 UTC (permalink / raw)
To: Niranjana Vishwanathapura; +Cc: dri-devel, intel-xe
On Mon, Oct 12, 2026 at 06:06:12PM -0700, Niranjana Vishwanathapura wrote:
> 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.
>
> 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
For the other driver patches:
Acked-by: Matthew Brost <matthew.brost@intel.com>
I'll leave the RB's to individual maintainers.
> 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 | 74 +++++++++++++++------
> 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_prime.h | 8 ++-
> 8 files changed, 91 insertions(+), 64 deletions(-)
>
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection
2026-10-13 1:06 [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Niranjana Vishwanathapura
` (6 preceding siblings ...)
2026-10-13 1:38 ` [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection Matthew Brost
@ 2026-10-13 7:17 ` Thomas Zimmermann
2026-10-13 18:29 ` Niranjana Vishwanathapura
7 siblings, 1 reply; 13+ messages in thread
From: Thomas Zimmermann @ 2026-10-13 7:17 UTC (permalink / raw)
To: Niranjana Vishwanathapura, dri-devel, intel-xe, matthew.brost
Hi,
I don't think we should merge this series.
Testing for the functions struct is a hack, which we do because it works
in practice. We don't any other good identifier for the source of the
imported dma_buf. We want to hide this from the outside world as much as
possible. Putting it into an exported interface goes in the wrong direction.
Best regards
Thomas
Am 13.10.26 um 03:06 schrieb Niranjana Vishwanathapura:
> 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.
>
> 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 | 74 +++++++++++++++------
> 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_prime.h | 8 ++-
> 8 files changed, 91 insertions(+), 64 deletions(-)
>
--
--
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] 13+ messages in thread* Re: [PATCH v2 0/6] drm: consolidate self-imported PRIME dma-buf detection
2026-10-13 7:17 ` Thomas Zimmermann
@ 2026-10-13 18:29 ` Niranjana Vishwanathapura
0 siblings, 0 replies; 13+ messages in thread
From: Niranjana Vishwanathapura @ 2026-10-13 18:29 UTC (permalink / raw)
To: Thomas Zimmermann; +Cc: dri-devel, intel-xe, matthew.brost
On Tue, Oct 13, 2026 at 09:17:17AM +0200, Thomas Zimmermann wrote:
>Hi,
>
>I don't think we should merge this series.
>
>Testing for the functions struct is a hack, which we do because it
>works in practice. We don't any other good identifier for the source
>of the imported dma_buf. We want to hide this from the outside world
>as much as possible. Putting it into an exported interface goes in the
>wrong direction.
>
Ok, I understand. Given it is just a utility function that is helpful
across drivers, would defining drm_gem_prime_self_import() as a static
inline helper in drm_prime.h is permissible?
Niranjana
>Best regards
>Thomas
>
>
>Am 13.10.26 um 03:06 schrieb Niranjana Vishwanathapura:
>>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.
>>
>>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 | 74 +++++++++++++++------
>> 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_prime.h | 8 ++-
>> 8 files changed, 91 insertions(+), 64 deletions(-)
>>
>
>--
>--
>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] 13+ messages in thread