Skip to contentSkip to conversation

drm: Hide import_attach from GEM code and helpers

PATCH In review Thomas Zimmermann 3 participants 26 messages Feb 26, 2025 – Jun 26, 2025

Subsystems (2)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

cover drm: Hide import_attach from GEM code and helpers

Do not use struct drm_gem_object.import_attach unnecessarily. The field is set by the PRIME import helpers. GEM and other helpers use it for testing for imported dma-bufs and retrieving the respective dma-buf instance.

Replace all tests for import_attach with the new GEM helper function drm_gem_is_imported(). The helper avoids import_attach in favor of testing the imported dma-buf's object against the provided GEM object.

Also replace references to import_attach.dmabuf. The imported dma-buf is also available in the GEM object's dma_buf field.

This series cleans up the GEM core and helpers. Drivers also use import_attach and should later be updated as well.

There's a TODO item for making import_attach entirely optional. This series is a first step towards this goal.

Thomas Zimmermann (10):
drm/gem: Test for imported GEM buffers with helper drm/gem-dma: Test for imported buffers with drm_gem_is_imported() drm/gem-dma: Use dma_buf from GEM object instance drm/gem-shmem: Test for imported buffers with drm_gem_is_imported() drm/gem-shmem: Use dma_buf from GEM object instance drm/gem-framebuffer: Test for imported buffers with
drm_gem_is_imported()
drm/gem-framebuffer: Use dma_buf from GEM object instance drm/fb-dma-helper: Test for imported buffers with
drm_gem_is_imported()
drm/mipi-dbi: Test for imported buffers with drm_gem_is_imported() drm/prime: Use dma_buf from GEM object instance

 drivers/gpu/drm/drm_fb_dma_helper.c          |  2 +-
 drivers/gpu/drm/drm_gem.c                    |  4 +--
 drivers/gpu/drm/drm_gem_dma_helper.c         |  4 +--
 drivers/gpu/drm/drm_gem_framebuffer_helper.c | 12 +++-----
 drivers/gpu/drm/drm_gem_shmem_helper.c       | 30 ++++++++++----------
 drivers/gpu/drm/drm_mipi_dbi.c               |  2 +-
 drivers/gpu/drm/drm_prime.c                  |  8 +-----
 include/drm/drm_gem.h                        | 14 +++++++++
 include/drm/drm_gem_shmem_helper.h           |  2 +-
 9 files changed, 41 insertions(+), 37 deletions(-)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 1/10 drm/gem: Test for imported GEM buffers with helper

Add drm_gem_is_imported() that tests if a GEM object's buffer has been imported. Update the GEM code accordingly.

GEM code usually tests for imports if import_attach has been set in struct drm_gem_object. But attaching a dma-buf on import requires a DMA-capable importer device, which is not the case for many serial busses like USB or I2C. The new helper tests if a GEM object's dma-buf has been created from the GEM object.

---
 drivers/gpu/drm/drm_gem.c |  4 ++--
 include/drm/drm_gem.h     | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -2
index ee811764c3df..c6240bab3fa5 100644
@@ -348,7 +348,7 @@ int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
 		return -ENOENT;
 
 	/* Don't allow imported objects to be mapped */
-	if (obj->import_attach) {
+	if (drm_gem_is_imported(obj)) {
 		ret = -EINVAL;
 		goto out;
 	}
@@ -1178,7 +1178,7 @@ void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
 			  drm_vma_node_start(&obj->vma_node));
 	drm_printf_indent(p, indent, "size=%zu\n", obj->size);
 	drm_printf_indent(p, indent, "imported=%s\n",
-			  str_yes_no(obj->import_attach));
+			  str_yes_no(drm_gem_is_imported(obj)));
 
 	if (obj->funcs->print_info)
 		obj->funcs->print_info(p, indent, obj);
include/drm/drm_gem.h +14 -0
index fdae947682cd..2bf893eabb4b 100644
@@ -35,6 +35,7 @@
  */
 
 #include <linux/kref.h>
+#include <linux/dma-buf.h>
 #include <linux/dma-resv.h>
 #include <linux/list.h>
 #include <linux/mutex.h>
@@ -575,6 +576,19 @@ static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
 	return (obj->handle_count > 1) || obj->dma_buf;
 }
 
+/**
+ * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
+ * @obj: the GEM object
+ *
+ * Returns:
+ * True if the GEM object's buffer has been imported, false otherwise
+ */
+static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
+{
+	/* The dma-buf's priv field points to the original GEM object. */
+	return obj->dma_buf && (obj->dma_buf->priv != obj);
+}
+
 #ifdef CONFIG_LOCKDEP
 /**
  * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.
Signature · 1 line
2.48.1
Reviewed-by on v1 1/10
#2 lore raw

Re: [PATCH 01/10] drm/gem: Test for imported GEM buffers with helper

Quoted 60 lines · Thomas Zimmermann

Add drm_gem_is_imported() that tests if a GEM object's buffer has been imported. Update the GEM code accordingly.

GEM code usually tests for imports if import_attach has been set in struct drm_gem_object. But attaching a dma-buf on import requires a DMA-capable importer device, which is not the case for many serial busses like USB or I2C. The new helper tests if a GEM object's dma-buf has been created from the GEM object.

---
 drivers/gpu/drm/drm_gem.c |  4 ++--
 include/drm/drm_gem.h     | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem.c +1 -1
index ee811764c3df..c6240bab3fa5 100644
@@ -348,7 +348,7 @@ int drm_gem_dumb_map_offset(struct drm_file *file,
 struct drm_device *dev,
                return -ENOENT;
 
        /* Don't allow imported objects to be mapped */
-       if (obj->import_attach) {
+       if (drm_gem_is_imported(obj)) {
                ret = -EINVAL;
                goto out;
        }
drivers/gpu/drm/drm_gem.c +1 -1
@@ -1178,7 +1178,7 @@ void drm_gem_print_info(struct drm_printer *p,
 unsigned int indent,
                          drm_vma_node_start(&obj->vma_node));
        drm_printf_indent(p, indent, "size=%zu\n", obj->size);
        drm_printf_indent(p, indent, "imported=%s\n",
-                         str_yes_no(obj->import_attach));
+                         str_yes_no(drm_gem_is_imported(obj)));
 
        if (obj->funcs->print_info)
                obj->funcs->print_info(p, indent, obj);
include/drm/drm_gem.h +1 -0
index fdae947682cd..2bf893eabb4b 100644
@@ -35,6 +35,7 @@
  */
 
 #include <linux/kref.h>
+#include <linux/dma-buf.h>
 #include <linux/dma-resv.h>
 #include <linux/list.h>
 #include <linux/mutex.h>
@@ -575,6 +576,19 @@ static inline bool

drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje

        return (obj->handle_count > 1) || obj->dma_buf;

}

include/drm/drm_gem.h +3 -0
+/**
+ * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
+ * @obj: the GEM object

include/drm/drm_gem.h static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje · line 591

Quoted 10 lines
include/drm/drm_gem.h +10 -0
+ *
+ * Returns:
+ * True if the GEM object's buffer has been imported, false otherwise
+ */
+static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
+{
+       /* The dma-buf's priv field points to the original GEM object. */
+       return obj->dma_buf && (obj->dma_buf->priv != obj);
+}
+

Looks good to me.

Quoted 8 lines

#ifdef CONFIG_LOCKDEP
/**
* drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the
gpuva list.
--
2.48.1

author maintainer
#3 lore raw

Re: [PATCH 01/10] drm/gem: Test for imported GEM buffers with helper

Am 28.02.25 um 17:04 schrieb Anusha Srivatsa:

Quoted 78 lines · Thomas Zimmermann
    Add drm_gem_is_imported() that tests if a GEM object's buffer has
    been imported. Update the GEM code accordingly.

    GEM code usually tests for imports if import_attach has been set
    in struct drm_gem_object. But attaching a dma-buf on import requires
    a DMA-capable importer device, which is not the case for many serial
    busses like USB or I2C. The new helper tests if a GEM object's dma-buf
    has been created from the GEM object.

    Signed-off-by: Thomas Zimmermann <[email protected]>
    ---
     drivers/gpu/drm/drm_gem.c |  4 ++--
     include/drm/drm_gem.h     | 14 ++++++++++++++
     2 files changed, 16 insertions(+), 2 deletions(-)

    diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
    index ee811764c3df..c6240bab3fa5 100644
    --- a/drivers/gpu/drm/drm_gem.c
    +++ b/drivers/gpu/drm/drm_gem.c
    @@ -348,7 +348,7 @@ int drm_gem_dumb_map_offset(struct drm_file
    *file, struct drm_device *dev,
                    return -ENOENT;

            /* Don't allow imported objects to be mapped */
    -       if (obj->import_attach) {
    +       if (drm_gem_is_imported(obj)) {
                    ret = -EINVAL;
                    goto out;
            }
    @@ -1178,7 +1178,7 @@ void drm_gem_print_info(struct drm_printer
    *p, unsigned int indent,
    drm_vma_node_start(&obj->vma_node));
            drm_printf_indent(p, indent, "size=%zu\n", obj->size);
            drm_printf_indent(p, indent, "imported=%s\n",
    -  str_yes_no(obj->import_attach));
    +  str_yes_no(drm_gem_is_imported(obj)));

            if (obj->funcs->print_info)
                    obj->funcs->print_info(p, indent, obj);
    diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
    index fdae947682cd..2bf893eabb4b 100644
    --- a/include/drm/drm_gem.h
    +++ b/include/drm/drm_gem.h
    @@ -35,6 +35,7 @@
      */

     #include <linux/kref.h>
    +#include <linux/dma-buf.h>
     #include <linux/dma-resv.h>
     #include <linux/list.h>
     #include <linux/mutex.h>
    @@ -575,6 +576,19 @@ static inline bool
    drm_gem_object_is_shared_for_memory_stats(struct drm_gem_obje
            return (obj->handle_count > 1) || obj->dma_buf;
     }

    +/**
    + * drm_gem_is_imported() - Tests if GEM object's buffer has been
    imported
    + * @obj: the GEM object
    + *
    + * Returns:
    + * True if the GEM object's buffer has been imported, false otherwise
    + */
    +static inline bool drm_gem_is_imported(const struct
    drm_gem_object *obj)
    +{
    +       /* The dma-buf's priv field points to the original GEM
    object. */
    +       return obj->dma_buf && (obj->dma_buf->priv != obj);
    +}
    +
Quoted 2 lines

Looks good to me.

Thanks for reviewing.

Quoted 7 lines
     #ifdef CONFIG_LOCKDEP
     /**
      * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to
    the gpuva list.
    -- 
    2.48.1
Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author maintainer
#4 lore raw

PATCH 2/10 drm/gem-dma: Test for imported buffers with drm_gem_is_imported()

In reply to Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_gem_dma_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_gem_dma_helper.c +1 -1
index 16988d316a6d..4f0320df858f 100644
@@ -228,7 +228,7 @@ void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj)
 	struct drm_gem_object *gem_obj = &dma_obj->base;
 	struct iosys_map map = IOSYS_MAP_INIT_VADDR(dma_obj->vaddr);
 
-	if (gem_obj->import_attach) {
+	if (drm_gem_is_imported(gem_obj)) {
 		if (dma_obj->vaddr)
 			dma_buf_vunmap_unlocked(gem_obj->import_attach->dmabuf, &map);
 		drm_prime_gem_destroy(gem_obj, dma_obj->sgt);
Signature · 1 line
2.48.1
Reviewed-by on v1 2/10
#5 lore raw

Re: [PATCH 02/10] drm/gem-dma: Test for imported buffers with drm_gem_is_imported()

Quoted 22 lines · Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_gem_dma_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_gem_dma_helper.c +1 -1
index 16988d316a6d..4f0320df858f 100644
@@ -228,7 +228,7 @@ void drm_gem_dma_free(struct drm_gem_dma_object
 *dma_obj)
        struct drm_gem_object *gem_obj = &dma_obj->base;
        struct iosys_map map = IOSYS_MAP_INIT_VADDR(dma_obj->vaddr);
 
-       if (gem_obj->import_attach) {
+       if (drm_gem_is_imported(gem_obj)) {
                if (dma_obj->vaddr)
 
Quoted 3 lines
dma_buf_vunmap_unlocked(gem_obj->import_attach->dmabuf, &map);
                drm_prime_gem_destroy(gem_obj, dma_obj->sgt);
Quoted 4 lines

--
2.48.1

author maintainer
#6 lore raw

PATCH 3/10 drm/gem-dma: Use dma_buf from GEM object instance

In reply to Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_gem_dma_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_gem_dma_helper.c +1 -1
index 4f0320df858f..b7f033d4352a 100644
@@ -230,7 +230,7 @@ void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj)
 
 	if (drm_gem_is_imported(gem_obj)) {
 		if (dma_obj->vaddr)
-			dma_buf_vunmap_unlocked(gem_obj->import_attach->dmabuf, &map);
+			dma_buf_vunmap_unlocked(gem_obj->dma_buf, &map);
 		drm_prime_gem_destroy(gem_obj, dma_obj->sgt);
 	} else if (dma_obj->vaddr) {
 		if (dma_obj->map_noncoherent)
Signature · 1 line
2.48.1
Reviewed-by on v1 3/10
#7 lore raw

Re: [PATCH 03/10] drm/gem-dma: Use dma_buf from GEM object instance

Quoted 23 lines · Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_gem_dma_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_gem_dma_helper.c +1 -1
index 4f0320df858f..b7f033d4352a 100644
@@ -230,7 +230,7 @@ void drm_gem_dma_free(struct drm_gem_dma_object
 *dma_obj)
 
        if (drm_gem_is_imported(gem_obj)) {
                if (dma_obj->vaddr)
-
 dma_buf_vunmap_unlocked(gem_obj->import_attach->dmabuf, &map);
+                       dma_buf_vunmap_unlocked(gem_obj->dma_buf, &map);
                drm_prime_gem_destroy(gem_obj, dma_obj->sgt);
Quoted 3 lines
        } else if (dma_obj->vaddr) {
                if (dma_obj->map_noncoherent)
Quoted 4 lines

--
2.48.1

author maintainer
#8 lore raw

PATCH 4/10 drm/gem-shmem: Test for imported buffers with drm_gem_is_imported()

In reply to Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 24 ++++++++++++------------
 include/drm/drm_gem_shmem_helper.h     |  2 +-
 2 files changed, 13 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +12 -12
index 5ab351409312..7722cd720248 100644
@@ -160,7 +160,7 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem)
 {
 	struct drm_gem_object *obj = &shmem->base;
 
-	if (obj->import_attach) {
+	if (drm_gem_is_imported(obj)) {
 		drm_prime_gem_destroy(obj, shmem->sgt);
 	} else {
 		dma_resv_lock(shmem->base.resv, NULL);
@@ -255,7 +255,7 @@ int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem)
 
 	dma_resv_assert_held(shmem->base.resv);
 
-	drm_WARN_ON(shmem->base.dev, shmem->base.import_attach);
+	drm_WARN_ON(shmem->base.dev, drm_gem_is_imported(&shmem->base));
 
 	ret = drm_gem_shmem_get_pages(shmem);
 
@@ -286,7 +286,7 @@ int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem)
 	struct drm_gem_object *obj = &shmem->base;
 	int ret;
 
-	drm_WARN_ON(obj->dev, obj->import_attach);
+	drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
 	ret = dma_resv_lock_interruptible(shmem->base.resv, NULL);
 	if (ret)
@@ -309,7 +309,7 @@ void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem)
 {
 	struct drm_gem_object *obj = &shmem->base;
 
-	drm_WARN_ON(obj->dev, obj->import_attach);
+	drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
 	dma_resv_lock(shmem->base.resv, NULL);
 	drm_gem_shmem_unpin_locked(shmem);
@@ -338,7 +338,7 @@ int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
 	struct drm_gem_object *obj = &shmem->base;
 	int ret = 0;
 
-	if (obj->import_attach) {
+	if (drm_gem_is_imported(obj)) {
 		ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
 		if (!ret) {
 			if (drm_WARN_ON(obj->dev, map->is_iomem)) {
@@ -378,7 +378,7 @@ int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
 	return 0;
 
 err_put_pages:
-	if (!obj->import_attach)
+	if (!drm_gem_is_imported(obj))
 		drm_gem_shmem_put_pages(shmem);
 err_zero_use:
 	shmem->vmap_use_count = 0;
@@ -404,7 +404,7 @@ void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem,
 {
 	struct drm_gem_object *obj = &shmem->base;
 
-	if (obj->import_attach) {
+	if (drm_gem_is_imported(obj)) {
 		dma_buf_vunmap(obj->import_attach->dmabuf, map);
 	} else {
 		dma_resv_assert_held(shmem->base.resv);
@@ -566,7 +566,7 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
 	struct drm_gem_object *obj = vma->vm_private_data;
 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
 
-	drm_WARN_ON(obj->dev, obj->import_attach);
+	drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
 	dma_resv_lock(shmem->base.resv, NULL);
 
@@ -618,7 +618,7 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
 	struct drm_gem_object *obj = &shmem->base;
 	int ret;
 
-	if (obj->import_attach) {
+	if (drm_gem_is_imported(obj)) {
 		/* Reset both vm_ops and vm_private_data, so we don't end up with
 		 * vm_ops pointing to our implementation if the dma-buf backend
 		 * doesn't set those fields.
@@ -663,7 +663,7 @@ EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
 			      struct drm_printer *p, unsigned int indent)
 {
-	if (shmem->base.import_attach)
+	if (drm_gem_is_imported(&shmem->base))
 		return;
 
 	drm_printf_indent(p, indent, "pages_use_count=%u\n", shmem->pages_use_count);
@@ -690,7 +690,7 @@ struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem)
 {
 	struct drm_gem_object *obj = &shmem->base;
 
-	drm_WARN_ON(obj->dev, obj->import_attach);
+	drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
 	return drm_prime_pages_to_sg(obj->dev, shmem->pages, obj->size >> PAGE_SHIFT);
 }
@@ -705,7 +705,7 @@ static struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_
 	if (shmem->sgt)
 		return shmem->sgt;
 
-	drm_WARN_ON(obj->dev, obj->import_attach);
+	drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
 	ret = drm_gem_shmem_get_pages(shmem);
 	if (ret)
include/drm/drm_gem_shmem_helper.h +1 -1
index d22e3fb53631..cef5a6b5a4d6 100644
@@ -120,7 +120,7 @@ static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem
 {
 	return (shmem->madv > 0) &&
 		!shmem->vmap_use_count && shmem->sgt &&
-		!shmem->base.dma_buf && !shmem->base.import_attach;
+		!shmem->base.dma_buf && !drm_gem_is_imported(&shmem->base);
 }
 
 void drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem);
Signature · 1 line
2.48.1
#9 lore raw

Re: [PATCH 04/10] drm/gem-shmem: Test for imported buffers with drm_gem_is_imported()

Quoted 158 lines · Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 24 ++++++++++++------------
 include/drm/drm_gem_shmem_helper.h     |  2 +-
 2 files changed, 13 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
index 5ab351409312..7722cd720248 100644
@@ -160,7 +160,7 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object
 *shmem)
 {
        struct drm_gem_object *obj = &shmem->base;
 
-       if (obj->import_attach) {
+       if (drm_gem_is_imported(obj)) {
                drm_prime_gem_destroy(obj, shmem->sgt);
        } else {
                dma_resv_lock(shmem->base.resv, NULL);
drivers/gpu/drm/drm_gem_shmem_helper.c +2 -2
@@ -255,7 +255,7 @@ int drm_gem_shmem_pin_locked(struct
 drm_gem_shmem_object *shmem)
 
        dma_resv_assert_held(shmem->base.resv);
 
-       drm_WARN_ON(shmem->base.dev, shmem->base.import_attach);
+       drm_WARN_ON(shmem->base.dev, drm_gem_is_imported(&shmem->base));
 
        ret = drm_gem_shmem_get_pages(shmem);
@@ -286,7 +286,7 @@ int drm_gem_shmem_pin(struct drm_gem_shmem_object
 *shmem)
        struct drm_gem_object *obj = &shmem->base;
        int ret;
 
-       drm_WARN_ON(obj->dev, obj->import_attach);
+       drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
        ret = dma_resv_lock_interruptible(shmem->base.resv, NULL);

if (ret)

drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
@@ -309,7 +309,7 @@ void drm_gem_shmem_unpin(struct drm_gem_shmem_object
 *shmem)
 {
        struct drm_gem_object *obj = &shmem->base;
 
-       drm_WARN_ON(obj->dev, obj->import_attach);
+       drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
        dma_resv_lock(shmem->base.resv, NULL);
        drm_gem_shmem_unpin_locked(shmem);
drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
@@ -338,7 +338,7 @@ int drm_gem_shmem_vmap(struct drm_gem_shmem_object
 *shmem,
        struct drm_gem_object *obj = &shmem->base;
        int ret = 0;
 
-       if (obj->import_attach) {
+       if (drm_gem_is_imported(obj)) {
                ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
                if (!ret) {
                        if (drm_WARN_ON(obj->dev, map->is_iomem)) {
drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
@@ -378,7 +378,7 @@ int drm_gem_shmem_vmap(struct drm_gem_shmem_object
 *shmem,
        return 0;
 
 err_put_pages:
-       if (!obj->import_attach)
+       if (!drm_gem_is_imported(obj))
                drm_gem_shmem_put_pages(shmem);
 err_zero_use:
        shmem->vmap_use_count = 0;
drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
@@ -404,7 +404,7 @@ void drm_gem_shmem_vunmap(struct drm_gem_shmem_object
 *shmem,
 {
        struct drm_gem_object *obj = &shmem->base;
 
-       if (obj->import_attach) {
+       if (drm_gem_is_imported(obj)) {
                dma_buf_vunmap(obj->import_attach->dmabuf, map);
        } else {
                dma_resv_assert_held(shmem->base.resv);
drivers/gpu/drm/drm_gem_shmem_helper.c +2 -2
@@ -566,7 +566,7 @@ static void drm_gem_shmem_vm_open(struct
 vm_area_struct *vma)
        struct drm_gem_object *obj = vma->vm_private_data;
        struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
 
-       drm_WARN_ON(obj->dev, obj->import_attach);
+       drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
        dma_resv_lock(shmem->base.resv, NULL);
@@ -618,7 +618,7 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object
 *shmem, struct vm_area_struct
        struct drm_gem_object *obj = &shmem->base;
        int ret;
 
-       if (obj->import_attach) {
+       if (drm_gem_is_imported(obj)) {
                /* Reset both vm_ops and vm_private_data, so we don't end

up with
* vm_ops pointing to our implementation if the dma-buf
backend
* doesn't set those fields.

drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
@@ -663,7 +663,7 @@ EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
                              struct drm_printer *p, unsigned int indent)
 {
-       if (shmem->base.import_attach)
+       if (drm_gem_is_imported(&shmem->base))
                return;
 
        drm_printf_indent(p, indent, "pages_use_count=%u\n",

shmem->pages_use_count);

drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
@@ -690,7 +690,7 @@ struct sg_table *drm_gem_shmem_get_sg_table(struct
 drm_gem_shmem_object *shmem)
 {
        struct drm_gem_object *obj = &shmem->base;
 
-       drm_WARN_ON(obj->dev, obj->import_attach);
+       drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
        return drm_prime_pages_to_sg(obj->dev, shmem->pages, obj->size >>

PAGE_SHIFT);
}

drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
@@ -705,7 +705,7 @@ static struct sg_table
 *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_
        if (shmem->sgt)
                return shmem->sgt;
 
-       drm_WARN_ON(obj->dev, obj->import_attach);
+       drm_WARN_ON(obj->dev, drm_gem_is_imported(obj));
 
        ret = drm_gem_shmem_get_pages(shmem);

if (ret)

include/drm/drm_gem_shmem_helper.h +0 -0
index d22e3fb53631..cef5a6b5a4d6 100644
@@ -120,7 +120,7 @@ static inline bool drm_gem_shmem_is_purgeable(struct

drm_gem_shmem_object *shmem

 {
        return (shmem->madv > 0) &&
                !shmem->vmap_use_count && shmem->sgt &&
include/drm/drm_gem_shmem_helper.h +1 -1
-               !shmem->base.dma_buf && !shmem->base.import_attach;
+               !shmem->base.dma_buf && !drm_gem_is_imported(&shmem->base);
 }

void drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem);
--
2.48.1

author maintainer
#10 lore raw

PATCH 5/10 drm/gem-shmem: Use dma_buf from GEM object instance

In reply to Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +3 -3
index 7722cd720248..d99dee67353a 100644
@@ -339,10 +339,10 @@ int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
 	int ret = 0;
 
 	if (drm_gem_is_imported(obj)) {
-		ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
+		ret = dma_buf_vmap(obj->dma_buf, map);
 		if (!ret) {
 			if (drm_WARN_ON(obj->dev, map->is_iomem)) {
-				dma_buf_vunmap(obj->import_attach->dmabuf, map);
+				dma_buf_vunmap(obj->dma_buf, map);
 				return -EIO;
 			}
 		}
@@ -405,7 +405,7 @@ void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem,
 	struct drm_gem_object *obj = &shmem->base;
 
 	if (drm_gem_is_imported(obj)) {
-		dma_buf_vunmap(obj->import_attach->dmabuf, map);
+		dma_buf_vunmap(obj->dma_buf, map);
 	} else {
 		dma_resv_assert_held(shmem->base.resv);
 
Signature · 1 line
2.48.1
Reviewed-by on v1 5/10
#11 lore raw

Re: [PATCH 05/10] drm/gem-shmem: Use dma_buf from GEM object instance

Quoted 43 lines · Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +2 -2
index 7722cd720248..d99dee67353a 100644
@@ -339,10 +339,10 @@ int drm_gem_shmem_vmap(struct drm_gem_shmem_object
 *shmem,
        int ret = 0;
 
        if (drm_gem_is_imported(obj)) {
-               ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
+               ret = dma_buf_vmap(obj->dma_buf, map);
                if (!ret) {
                        if (drm_WARN_ON(obj->dev, map->is_iomem)) {
-                               dma_buf_vunmap(obj->import_attach->dmabuf,
 map);
+                               dma_buf_vunmap(obj->dma_buf, map);
                                return -EIO;
                        }
                }
drivers/gpu/drm/drm_gem_shmem_helper.c +1 -2
@@ -405,7 +405,7 @@ void drm_gem_shmem_vunmap(struct drm_gem_shmem_object
 *shmem,
        struct drm_gem_object *obj = &shmem->base;
 
        if (drm_gem_is_imported(obj)) {
-               dma_buf_vunmap(obj->import_attach->dmabuf, map);
+               dma_buf_vunmap(obj->dma_buf, map);
        } else {
                dma_resv_assert_held(shmem->base.resv);
 
--

2.48.1

author maintainer
#12 lore raw

Re: [PATCH 05/10] drm/gem-shmem: Use dma_buf from GEM object instance

Hi,

this patch triggers a NULL-pointer deref. Stack trace is below.

Am 28.02.25 um 17:26 schrieb Anusha Srivatsa:

Quoted 18 lines

On Wed, Feb 26, 2025 at 12:28 PM Thomas Zimmermann <[email protected]> wrote:

    Avoid dereferencing struct drm_gem_object.import_attach for the
    imported dma-buf. The dma_buf field in the GEM object instance refers
    to the same buffer. Prepares to make import_attach optional.

    Signed-off-by: Thomas Zimmermann <[email protected]>
    ---
     drivers/gpu/drm/drm_gem_shmem_helper.c | 6 +++---
     1 file changed, 3 insertions(+), 3 deletions(-)

    diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c
    b/drivers/gpu/drm/drm_gem_shmem_helper.c
    index 7722cd720248..d99dee67353a 100644
    --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
Quoted 9 lines
    +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
    @@ -339,10 +339,10 @@ int drm_gem_shmem_vmap(struct
    drm_gem_shmem_object *shmem,
            int ret = 0;

            if (drm_gem_is_imported(obj)) {
    -               ret = dma_buf_vmap(obj->import_attach->dmabuf, map);
    +               ret = dma_buf_vmap(obj->dma_buf, map);

What I see is that ->dma_buf is NULL at this point.  The sequence of events is

1) Import the dma-buf into a GEM object and set the pointer at [1]
2) Release the GEM object from user space and clear the dma-buf pointer at [2]
3) Use the GEM object for a plane and try to vmap the dma-buf here

[1] https://elixir.bootlin.com/linux/v6.15.3/source/drivers/gpu/drm/drm_prime.c#L406
[2] https://elixir.bootlin.com/linux/v6.15.3/source/drivers/gpu/drm/drm_gem.c#L241

Step 3) should likely not happen before 2). It looks like a problem with ref-counting? Or is this intentional behavior?

Best regards
Thomas

[  156.791968] ------------[ cut here ]------------
[  156.796830] WARNING: CPU: 2 PID: 2255 at drivers/dma-buf/dma-buf.c:1527 dma_buf_vmap+0x224/0x430
[  156.805923] Modules linked in: udl(E) snd_seq_dummy(E) snd_hrtimer(E) snd_seq(E) snd_seq_device(E) af_packet(E) nf_tables(E) iptable_filter(E) snd_hda_codec_hdmi(E) binfmt_misc(E) snd_hda_codec_realtek(E) snd_ hda_codec_generic(E) snd_hda_scodec_component(E) snd_hda_intel(E) snd_intel_dspcfg(E) snd_hda_codec(E) ee1004(E) snd_hda_core(E) iTCO_wdt(E) snd_hwdep(E) intel_rapl_msr(E) eeepc_wmi(E) iTCO_vendor_support(E) snd_ pcm(E) asus_wmi(E) intel_rapl_common(E) sparse_keymap(E) nls_iso8859_1(E) x86_pkg_temp_thermal(E) e1000e(E) platform_profile(E) nls_cp437(E) snd_timer(E) i2c_i801(E) intel_powerclamp(E) battery(E) i2c_smbus(E) pt p(E) snd(E) vfat(E) coretemp(E) rfkill(E) intel_wmi_thunderbolt(E) wmi_bmof(E) pcspkr(E) mxm_wmi(E) fat(E) soundcore(E) pps_core(E) i2c_mux(E) xfs(E) acpi_pad(E) button(E) mei_me(E) mei(E) joydev(E) loop(E) nvme_ fabrics(E) fuse(E) efi_pstore(E) dm_mod(E) configfs(E) nfnetlink(E) ip_tables(E) x_tables(E) i2c_algo_bit(E) ghash_clmulni_intel(E) drm_buddy(E) sha512_ssse3(E) video(E) hid_generic(E) [  156.806220]  sha1_ssse3(E) aesni_intel(E) crypto_simd(E) cryptd(E) usbhid(E) ttm(E) wmi(E) intel_gtt(E) drm_display_helper(E) cec(E) btrfs(E) blake2b_generic(E) xor(E) raid6_pq(E) msr(E) i2c_dev(E) efivarfs(E) dmi_sysfs(E)
[  156.916628] CPU: 2 UID: 1000 PID: 2255 Comm: KMS thread Tainted: G        W   E      6.14.0-rc4-00399-g1a148af06000-dirty #62
[  156.928010] Tainted: [W]=WARN, [E]=UNSIGNED_MODULE
[  156.933253] Hardware name: System manufacturer System Product Name/Z170-A, BIOS 3802 03/15/2018
[  156.942028] RIP: 0010:dma_buf_vmap+0x224/0x430
[  156.946529] Code: 48 83 c4 60 5b 41 5c 41 5d 5d c3 cc cc cc cc 80 3c 02 00 0f 85 5a 01 00 00 49 c7 04 24 00 00 00 00 48 85 db 0f 85 8b fe ff ff <0f> 0b b8 ea ff ff ff eb ba 48 85 f6 0f 85 51 01 00 00 48 89 54 24
[  156.965374] RSP: 0018:ffff88810a5cf8b0 EFLAGS: 00010246
[  156.971341] RAX: dffffc0000000000 RBX: 0000000000000000 RCX: dffffc0000000000
[  156.978539] RDX: 1ffff1103183cd19 RSI: ffff88818c1e68c8 RDI: ffff88818c1e68d0
[  156.985742] RBP: ffff88810a5cf928 R08: 0000000000000001 R09: fffffbfff754ad94
[  156.992928] R10: 0000000000000002 R11: 0000000000000000 R12: ffff88818c1e68c8
[  157.000385] R13: 1ffff110214b9f1a R14: dffffc0000000000 R15: ffffffffb95eb1e0
[  157.007599] FS:  00007f45327036c0(0000) GS:ffff888417000000(0000) knlGS:0000000000000000
[  157.015735] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  157.021527] CR2: 00007f9980a04cf0 CR3: 00000001263c4003 CR4: 00000000003706f0
[  157.028729] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[  157.036237] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[  157.043420] Call Trace:
[  157.045898]  <TASK>
[  157.048030]  ? show_trace_log_lvl+0x1af/0x2c0
[  157.052436]  ? show_trace_log_lvl+0x1af/0x2c0
[  157.056836]  ? show_trace_log_lvl+0x1af/0x2c0
[  157.061253]  ? drm_gem_shmem_vmap+0x74/0x710
[  157.065567]  ? dma_buf_vmap+0x224/0x430
[  157.069446]  ? __warn.cold+0x58/0xe4
[  157.073061]  ? dma_buf_vmap+0x224/0x430
[  157.077111]  ? report_bug+0x1dd/0x390
[  157.080842]  ? handle_bug+0x5e/0xa0
[  157.084389]  ? exc_invalid_op+0x14/0x50
[  157.088291]  ? asm_exc_invalid_op+0x16/0x20
[  157.092548]  ? dma_buf_vmap+0x224/0x430
[  157.096663]  ? dma_resv_get_singleton+0x6d/0x230
[  157.101341]  ? __pfx_dma_buf_vmap+0x10/0x10
[  157.105588]  ? __pfx_dma_resv_get_singleton+0x10/0x10
[  157.110697]  drm_gem_shmem_vmap+0x74/0x710
[  157.114866]  drm_gem_vmap+0xa9/0x1b0
[  157.118763]  drm_gem_vmap_unlocked+0x46/0xa0
[  157.123086]  drm_gem_fb_vmap+0xab/0x300
[  157.126979]  drm_atomic_helper_prepare_planes.part.0+0x487/0xb10
[  157.133032]  ? lockdep_init_map_type+0x19d/0x880
[  157.137701]  drm_atomic_helper_commit+0x13d/0x2e0
[  157.142671]  ? drm_atomic_nonblocking_commit+0xa0/0x180
[  157.147988]  drm_mode_atomic_ioctl+0x766/0xe40
[  157.152735]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.157810]  ? __pfx___lock_acquire+0x10/0x10
[  157.162229]  ? __pfx___lock_acquire+0x10/0x10
[  157.166680]  ? find_held_lock+0x2f/0x130
[  157.170728]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.175759]  ? do_raw_spin_unlock+0x55/0x230
[  157.180125]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.185152]  drm_ioctl_kernel+0x141/0x2b0
[  157.189223]  ? lock_acquire+0x1a2/0x210
[  157.193137]  ? __pfx_drm_ioctl_kernel+0x10/0x10
[  157.197750]  drm_ioctl+0x489/0xb30
[  157.201240]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.206287]  ? __pfx_drm_ioctl+0x10/0x10
[  157.210273]  ? __pfx_lock_acquire.part.0+0x10/0x10
[  157.215153]  ? __fget_files+0x1a2/0x2f0
[  157.219048]  ? lock_release+0x130/0x2a0
[  157.222947]  ? __fget_files+0x1ac/0x2f0
[  157.226868]  __x64_sys_ioctl+0x12e/0x190
[  157.230964]  do_syscall_64+0x66/0x150
[  157.234701]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  157.239813] RIP: 0033:0x7f454811916f
[  157.243425] Code: 00 48 89 44 24 18 31 c0 48 8d 44 24 60 c7 04 24 10 00 00 00 48 89 44 24 08 48 8d 44 24 20 48 89 44 24 10 b8 10 00 00 00 0f 05 <89> c2 3d 00 f0 ff ff 77 18 48 8b 44 24 18 64 48 2b 04 25 28 00 00
[  157.262274] RSP: 002b:00007f45327019f0 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
[  157.269929] RAX: ffffffffffffffda RBX: 0000560b36153720 RCX: 00007f454811916f
[  157.277138] RDX: 00007f4532701a90 RSI: 00000000c03864bc RDI: 0000000000000014
[  157.284321] RBP: 00007f4532701a90 R08: 00007f451402dd40 R09: 00007f4514000090
[  157.291552] R10: 0000000000000000 R11: 0000000000000246 R12: 00000000c03864bc
[  157.298761] R13: 0000000000000014 R14: 00007f4514018740 R15: 0000560b372a9c80
[  157.305985]  </TASK>
[  157.308223] irq event stamp: 16371
[  157.311672] hardirqs last  enabled at (16383): [<ffffffffb66c4c0e>] __up_console_sem+0x5e/0x70
[  157.320365] hardirqs last disabled at (16394): [<ffffffffb66c4bf3>] __up_console_sem+0x43/0x70
[  157.329049] softirqs last  enabled at (16410): [<ffffffffb650e832>] handle_softirqs+0x502/0x6b0
[  157.337831] softirqs last disabled at (16405): [<ffffffffb650eae7>] __irq_exit_rcu+0xf7/0x160
[  157.346424] ---[ end trace 0000000000000000 ]---

Quoted 20 lines
                    if (!ret) {
                            if (drm_WARN_ON(obj->dev, map->is_iomem)) {
    -  dma_buf_vunmap(obj->import_attach->dmabuf, map);
    +  dma_buf_vunmap(obj->dma_buf, map);
                                    return -EIO;
                            }
                    }
    @@ -405,7 +405,7 @@ void drm_gem_shmem_vunmap(struct
    drm_gem_shmem_object *shmem,
            struct drm_gem_object *obj = &shmem->base;

            if (drm_gem_is_imported(obj)) {
    -  dma_buf_vunmap(obj->import_attach->dmabuf, map);
    +               dma_buf_vunmap(obj->dma_buf, map);
            } else {
                    dma_resv_assert_held(shmem->base.resv);

    -- 
    2.48.1
Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
#13 lore raw

Re: [PATCH 05/10] drm/gem-shmem: Use dma_buf from GEM object instance

Quoted 36 lines · Thomas Zimmermann

Hi,

this patch triggers a NULL-pointer deref. Stack trace is below.

Am 28.02.25 um 17:26 schrieb Anusha Srivatsa:

Quoted 23 lines

On Wed, Feb 26, 2025 at 12:28 PM Thomas Zimmermann <[email protected]> wrote:

    Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

    ---
     drivers/gpu/drm/drm_gem_shmem_helper.c | 6 +++---

     1 file changed, 3 insertions(+), 3 deletions(-)

    diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c index 7722cd720248..d99dee67353a 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -339,10 +339,10 @@ int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
            int ret = 0;

Quoted 4 lines

            if (drm_gem_is_imported(obj)) {
    -               ret = dma_buf_vmap(obj->import_attach->dmabuf, map); +               ret = dma_buf_vmap(obj->dma_buf, map);

What I see is that ->dma_buf is NULL at this point.  The sequence of events is

Quoted 8 lines

1) Import the dma-buf into a GEM object and set the pointer at [1]
2) Release the GEM object from user space and clear the dma-buf pointer at [2]
3) Use the GEM object for a plane and try to vmap the dma-buf here

[1] https://elixir.bootlin.com/linux/v6.15.3/source/drivers/gpu/drm/drm_prime.c#L406
[2] https://elixir.bootlin.com/linux/v6.15.3/source/drivers/gpu/drm/drm_gem.c#L241

Step 3) should likely not happen before 2). It looks like a problem with ref-counting? Or is this intentional behavior?

Oh, well that again :) That is most likely a bug I've stumbled over before but in a different context.

The problem is that you can created a FB, close the buffers the FB is referencing and then try to display the FB.

The correct solution is most likel to increase the handle count when GEM objects are used in a FB object instead of just grabbing a GEM reference.

Regards,
Christian.

Quoted 117 lines

Best regards
Thomas

[  156.791968] ------------[ cut here ]------------
[  156.796830] WARNING: CPU: 2 PID: 2255 at drivers/dma-buf/dma-buf.c:1527 dma_buf_vmap+0x224/0x430
[  156.805923] Modules linked in: udl(E) snd_seq_dummy(E) snd_hrtimer(E) snd_seq(E) snd_seq_device(E) af_packet(E) nf_tables(E) iptable_filter(E) snd_hda_codec_hdmi(E) binfmt_misc(E) snd_hda_codec_realtek(E) snd_ hda_codec_generic(E) snd_hda_scodec_component(E) snd_hda_intel(E) snd_intel_dspcfg(E) snd_hda_codec(E) ee1004(E) snd_hda_core(E) iTCO_wdt(E) snd_hwdep(E) intel_rapl_msr(E) eeepc_wmi(E) iTCO_vendor_support(E) snd_ pcm(E) asus_wmi(E) intel_rapl_common(E) sparse_keymap(E) nls_iso8859_1(E) x86_pkg_temp_thermal(E) e1000e(E) platform_profile(E) nls_cp437(E) snd_timer(E) i2c_i801(E) intel_powerclamp(E) battery(E) i2c_smbus(E) pt p(E) snd(E) vfat(E) coretemp(E) rfkill(E) intel_wmi_thunderbolt(E) wmi_bmof(E) pcspkr(E) mxm_wmi(E) fat(E) soundcore(E) pps_core(E) i2c_mux(E) xfs(E) acpi_pad(E) button(E) mei_me(E) mei(E) joydev(E) loop(E) nvme_ fabrics(E) fuse(E) efi_pstore(E) dm_mod(E) configfs(E) nfnetlink(E) ip_tables(E) x_tables(E) i2c_algo_bit(E) ghash_clmulni_intel(E) drm_buddy(E) sha512_ssse3(E) video(E) hid_generic(E) [  156.806220]  sha1_ssse3(E) aesni_intel(E) crypto_simd(E) cryptd(E) usbhid(E) ttm(E) wmi(E) intel_gtt(E) drm_display_helper(E) cec(E) btrfs(E) blake2b_generic(E) xor(E) raid6_pq(E) msr(E) i2c_dev(E) efivarfs(E) dmi_sysfs(E)
[  156.916628] CPU: 2 UID: 1000 PID: 2255 Comm: KMS thread Tainted: G        W   E      6.14.0-rc4-00399-g1a148af06000-dirty #62
[  156.928010] Tainted: [W]=WARN, [E]=UNSIGNED_MODULE
[  156.933253] Hardware name: System manufacturer System Product Name/Z170-A, BIOS 3802 03/15/2018
[  156.942028] RIP: 0010:dma_buf_vmap+0x224/0x430
[  156.946529] Code: 48 83 c4 60 5b 41 5c 41 5d 5d c3 cc cc cc cc 80 3c 02 00 0f 85 5a 01 00 00 49 c7 04 24 00 00 00 00 48 85 db 0f 85 8b fe ff ff <0f> 0b b8 ea ff ff ff eb ba 48 85 f6 0f 85 51 01 00 00 48 89 54 24
[  156.965374] RSP: 0018:ffff88810a5cf8b0 EFLAGS: 00010246
[  156.971341] RAX: dffffc0000000000 RBX: 0000000000000000 RCX: dffffc0000000000
[  156.978539] RDX: 1ffff1103183cd19 RSI: ffff88818c1e68c8 RDI: ffff88818c1e68d0
[  156.985742] RBP: ffff88810a5cf928 R08: 0000000000000001 R09: fffffbfff754ad94
[  156.992928] R10: 0000000000000002 R11: 0000000000000000 R12: ffff88818c1e68c8
[  157.000385] R13: 1ffff110214b9f1a R14: dffffc0000000000 R15: ffffffffb95eb1e0
[  157.007599] FS:  00007f45327036c0(0000) GS:ffff888417000000(0000) knlGS:0000000000000000
[  157.015735] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  157.021527] CR2: 00007f9980a04cf0 CR3: 00000001263c4003 CR4: 00000000003706f0
[  157.028729] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[  157.036237] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[  157.043420] Call Trace:
[  157.045898]  <TASK>
[  157.048030]  ? show_trace_log_lvl+0x1af/0x2c0
[  157.052436]  ? show_trace_log_lvl+0x1af/0x2c0
[  157.056836]  ? show_trace_log_lvl+0x1af/0x2c0
[  157.061253]  ? drm_gem_shmem_vmap+0x74/0x710
[  157.065567]  ? dma_buf_vmap+0x224/0x430
[  157.069446]  ? __warn.cold+0x58/0xe4
[  157.073061]  ? dma_buf_vmap+0x224/0x430
[  157.077111]  ? report_bug+0x1dd/0x390
[  157.080842]  ? handle_bug+0x5e/0xa0
[  157.084389]  ? exc_invalid_op+0x14/0x50
[  157.088291]  ? asm_exc_invalid_op+0x16/0x20
[  157.092548]  ? dma_buf_vmap+0x224/0x430
[  157.096663]  ? dma_resv_get_singleton+0x6d/0x230
[  157.101341]  ? __pfx_dma_buf_vmap+0x10/0x10
[  157.105588]  ? __pfx_dma_resv_get_singleton+0x10/0x10
[  157.110697]  drm_gem_shmem_vmap+0x74/0x710
[  157.114866]  drm_gem_vmap+0xa9/0x1b0
[  157.118763]  drm_gem_vmap_unlocked+0x46/0xa0
[  157.123086]  drm_gem_fb_vmap+0xab/0x300
[  157.126979]  drm_atomic_helper_prepare_planes.part.0+0x487/0xb10
[  157.133032]  ? lockdep_init_map_type+0x19d/0x880
[  157.137701]  drm_atomic_helper_commit+0x13d/0x2e0
[  157.142671]  ? drm_atomic_nonblocking_commit+0xa0/0x180
[  157.147988]  drm_mode_atomic_ioctl+0x766/0xe40
[  157.152735]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.157810]  ? __pfx___lock_acquire+0x10/0x10
[  157.162229]  ? __pfx___lock_acquire+0x10/0x10
[  157.166680]  ? find_held_lock+0x2f/0x130
[  157.170728]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.175759]  ? do_raw_spin_unlock+0x55/0x230
[  157.180125]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.185152]  drm_ioctl_kernel+0x141/0x2b0
[  157.189223]  ? lock_acquire+0x1a2/0x210
[  157.193137]  ? __pfx_drm_ioctl_kernel+0x10/0x10
[  157.197750]  drm_ioctl+0x489/0xb30
[  157.201240]  ? __pfx_drm_mode_atomic_ioctl+0x10/0x10
[  157.206287]  ? __pfx_drm_ioctl+0x10/0x10
[  157.210273]  ? __pfx_lock_acquire.part.0+0x10/0x10
[  157.215153]  ? __fget_files+0x1a2/0x2f0
[  157.219048]  ? lock_release+0x130/0x2a0
[  157.222947]  ? __fget_files+0x1ac/0x2f0
[  157.226868]  __x64_sys_ioctl+0x12e/0x190
[  157.230964]  do_syscall_64+0x66/0x150
[  157.234701]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  157.239813] RIP: 0033:0x7f454811916f
[  157.243425] Code: 00 48 89 44 24 18 31 c0 48 8d 44 24 60 c7 04 24 10 00 00 00 48 89 44 24 08 48 8d 44 24 20 48 89 44 24 10 b8 10 00 00 00 0f 05 <89> c2 3d 00 f0 ff ff 77 18 48 8b 44 24 18 64 48 2b 04 25 28 00 00
[  157.262274] RSP: 002b:00007f45327019f0 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
[  157.269929] RAX: ffffffffffffffda RBX: 0000560b36153720 RCX: 00007f454811916f
[  157.277138] RDX: 00007f4532701a90 RSI: 00000000c03864bc RDI: 0000000000000014
[  157.284321] RBP: 00007f4532701a90 R08: 00007f451402dd40 R09: 00007f4514000090
[  157.291552] R10: 0000000000000000 R11: 0000000000000246 R12: 00000000c03864bc
[  157.298761] R13: 0000000000000014 R14: 00007f4514018740 R15: 0000560b372a9c80
[  157.305985]  </TASK>
[  157.308223] irq event stamp: 16371
[  157.311672] hardirqs last  enabled at (16383): [<ffffffffb66c4c0e>] __up_console_sem+0x5e/0x70
[  157.320365] hardirqs last disabled at (16394): [<ffffffffb66c4bf3>] __up_console_sem+0x43/0x70
[  157.329049] softirqs last  enabled at (16410): [<ffffffffb650e832>] handle_softirqs+0x502/0x6b0
[  157.337831] softirqs last disabled at (16405): [<ffffffffb650eae7>] __irq_exit_rcu+0xf7/0x160
[  157.346424] ---[ end trace 0000000000000000 ]---

Quoted 19 lines

                    if (!ret) {
                            if (drm_WARN_ON(obj->dev, map->is_iomem)) {
    -  dma_buf_vunmap(obj->import_attach->dmabuf, map);
    +  dma_buf_vunmap(obj->dma_buf, map);
                                    return -EIO;
                            }
                    }
    @@ -405,7 +405,7 @@ void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem,
            struct drm_gem_object *obj = &shmem->base;

            if (drm_gem_is_imported(obj)) {
    -  dma_buf_vunmap(obj->import_attach->dmabuf, map); +               dma_buf_vunmap(obj->dma_buf, map); } else { dma_resv_assert_held(shmem->base.resv);

    --     2.48.1

author maintainer
#14 lore raw

PATCH 6/10 drm/gem-framebuffer: Test for imported buffers with drm_gem_is_imported()

In reply to Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_gem_framebuffer_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem_framebuffer_helper.c +2 -2
index 185534f56bab..2bf606ba24cd 100644
@@ -429,7 +429,7 @@ static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_dat
 		if (!obj)
 			continue;
 		import_attach = obj->import_attach;
-		if (!import_attach)
+		if (!drm_gem_is_imported(obj))
 			continue;
 		ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
 		if (ret)
@@ -466,7 +466,7 @@ int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direct
 			goto err___drm_gem_fb_end_cpu_access;
 		}
 		import_attach = obj->import_attach;
-		if (!import_attach)
+		if (!drm_gem_is_imported(obj))
 			continue;
 		ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);
 		if (ret)
Signature · 1 line
2.48.1
Reviewed-by on v1 6/10
#15 lore raw

Re: [PATCH 06/10] drm/gem-framebuffer: Test for imported buffers with drm_gem_is_imported()

Quoted 37 lines · Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_gem_framebuffer_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_gem_framebuffer_helper.c +0 -0
index 185534f56bab..2bf606ba24cd 100644
@@ -429,7 +429,7 @@ static void __drm_gem_fb_end_cpu_access(struct

drm_framebuffer *fb, enum dma_dat

                if (!obj)
                        continue;
                import_attach = obj->import_attach;
drivers/gpu/drm/drm_gem_framebuffer_helper.c +2 -2
-               if (!import_attach)
+               if (!drm_gem_is_imported(obj))
                        continue;
                ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
                if (ret)
@@ -466,7 +466,7 @@ int drm_gem_fb_begin_cpu_access(struct drm_framebuffer
 *fb, enum dma_data_direct
                        goto err___drm_gem_fb_end_cpu_access;
                }
                import_attach = obj->import_attach;
-               if (!import_attach)
+               if (!drm_gem_is_imported(obj))
                        continue;
                ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);

if (ret)
--
2.48.1

author maintainer
#16 lore raw

PATCH 7/10 drm/gem-framebuffer: Use dma_buf from GEM object instance

In reply to Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_gem_framebuffer_helper.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_gem_framebuffer_helper.c +2 -6
index 2bf606ba24cd..0fbeb686e561 100644
@@ -419,7 +419,6 @@ EXPORT_SYMBOL(drm_gem_fb_vunmap);
 static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir,
 					unsigned int num_planes)
 {
-	struct dma_buf_attachment *import_attach;
 	struct drm_gem_object *obj;
 	int ret;
 
@@ -428,10 +427,9 @@ static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_dat
 		obj = drm_gem_fb_get_obj(fb, num_planes);
 		if (!obj)
 			continue;
-		import_attach = obj->import_attach;
 		if (!drm_gem_is_imported(obj))
 			continue;
-		ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
+		ret = dma_buf_end_cpu_access(obj->dma_buf, dir);
 		if (ret)
 			drm_err(fb->dev, "dma_buf_end_cpu_access(%u, %d) failed: %d\n",
 				ret, num_planes, dir);
@@ -454,7 +452,6 @@ static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum dma_dat
  */
 int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direction dir)
 {
-	struct dma_buf_attachment *import_attach;
 	struct drm_gem_object *obj;
 	unsigned int i;
 	int ret;
@@ -465,10 +462,9 @@ int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum dma_data_direct
 			ret = -EINVAL;
 			goto err___drm_gem_fb_end_cpu_access;
 		}
-		import_attach = obj->import_attach;
 		if (!drm_gem_is_imported(obj))
 			continue;
-		ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);
+		ret = dma_buf_begin_cpu_access(obj->dma_buf, dir);
 		if (ret)
 			goto err___drm_gem_fb_end_cpu_access;
 	}
Signature · 1 line
2.48.1
Reviewed-by on v1 7/10
#17 lore raw

Re: [PATCH 07/10] drm/gem-framebuffer: Use dma_buf from GEM object instance

Quoted 64 lines · Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_gem_framebuffer_helper.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_gem_framebuffer_helper.c +0 -1
index 2bf606ba24cd..0fbeb686e561 100644
@@ -419,7 +419,6 @@ EXPORT_SYMBOL(drm_gem_fb_vunmap);
 static void __drm_gem_fb_end_cpu_access(struct drm_framebuffer *fb, enum
 dma_data_direction dir,
                                        unsigned int num_planes)
 {
-       struct dma_buf_attachment *import_attach;
        struct drm_gem_object *obj;
        int ret;
@@ -428,10 +427,9 @@ static void __drm_gem_fb_end_cpu_access(struct

drm_framebuffer *fb, enum dma_dat

                obj = drm_gem_fb_get_obj(fb, num_planes);
                if (!obj)
                        continue;
drivers/gpu/drm/drm_gem_framebuffer_helper.c +1 -2
-               import_attach = obj->import_attach;
                if (!drm_gem_is_imported(obj))
                        continue;
-               ret = dma_buf_end_cpu_access(import_attach->dmabuf, dir);
+               ret = dma_buf_end_cpu_access(obj->dma_buf, dir);
                if (ret)
                        drm_err(fb->dev, "dma_buf_end_cpu_access(%u, %d)

failed: %d\n",

                                ret, num_planes, dir);
drivers/gpu/drm/drm_gem_framebuffer_helper.c +0 -0
@@ -454,7 +452,6 @@ static void __drm_gem_fb_end_cpu_access(struct

drm_framebuffer *fb, enum dma_dat
*/
int drm_gem_fb_begin_cpu_access(struct drm_framebuffer *fb, enum
dma_data_direction dir)
{

drivers/gpu/drm/drm_gem_framebuffer_helper.c +0 -1
-       struct dma_buf_attachment *import_attach;
        struct drm_gem_object *obj;
        unsigned int i;
        int ret;
@@ -465,10 +462,9 @@ int drm_gem_fb_begin_cpu_access(struct

drm_framebuffer *fb, enum dma_data_direct

                        ret = -EINVAL;
                        goto err___drm_gem_fb_end_cpu_access;
                }
drivers/gpu/drm/drm_gem_framebuffer_helper.c +1 -3
-               import_attach = obj->import_attach;
                if (!drm_gem_is_imported(obj))
                        continue;
-               ret = dma_buf_begin_cpu_access(import_attach->dmabuf, dir);
+               ret = dma_buf_begin_cpu_access(obj->dma_buf, dir);
                if (ret)
                        goto err___drm_gem_fb_end_cpu_access;
        }
--

2.48.1

author maintainer
#18 lore raw

PATCH 8/10 drm/fb-dma-helper: Test for imported buffers with drm_gem_is_imported()

In reply to Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_fb_dma_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_fb_dma_helper.c +1 -1
index e1d61a65210b..2c4dc7ebc0c3 100644
@@ -178,7 +178,7 @@ int drm_fb_dma_get_scanout_buffer(struct drm_plane *plane,
 	dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
 
 	/* Buffer should be accessible from the CPU */
-	if (dma_obj->base.import_attach)
+	if (drm_gem_is_imported(&dma_obj->base))
 		return -ENODEV;
 
 	/* Buffer should be already mapped to CPU */
Signature · 1 line
2.48.1
Reviewed-by on v1 8/10
#19 lore raw

Re: [PATCH 08/10] drm/fb-dma-helper: Test for imported buffers with drm_gem_is_imported()

Quoted 15 lines · Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_fb_dma_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_fb_dma_helper.c +0 -0
index e1d61a65210b..2c4dc7ebc0c3 100644

drivers/gpu/drm/drm_fb_dma_helper.c int drm_fb_dma_get_scanout_buffer(struct drm_plane · line 184

Quoted 8 lines
drivers/gpu/drm/drm_fb_dma_helper.c +1 -1
@@ -178,7 +178,7 @@ int drm_fb_dma_get_scanout_buffer(struct drm_plane
 *plane,
        dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
 
        /* Buffer should be accessible from the CPU */
-       if (dma_obj->base.import_attach)
+       if (drm_gem_is_imported(&dma_obj->base))
                return -ENODEV;
 
        /* Buffer should be already mapped to CPU */
Quoted 4 lines

--
2.48.1

author maintainer
#20 lore raw

PATCH 9/10 drm/mipi-dbi: Test for imported buffers with drm_gem_is_imported()

In reply to Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_mipi_dbi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_mipi_dbi.c +1 -1
index 34bca7567576..89e05a5bed1d 100644
@@ -218,7 +218,7 @@ int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *
 	switch (fb->format->format) {
 	case DRM_FORMAT_RGB565:
 		if (swap)
-			drm_fb_swab(&dst_map, NULL, src, fb, clip, !gem->import_attach,
+			drm_fb_swab(&dst_map, NULL, src, fb, clip, !drm_gem_is_imported(gem),
 				    fmtcnv_state);
 		else
 			drm_fb_memcpy(&dst_map, NULL, src, fb, clip);
Signature · 1 line
2.48.1
Reviewed-by on v1 9/10
#21 lore raw

Re: [PATCH 09/10] drm/mipi-dbi: Test for imported buffers with drm_gem_is_imported()

Quoted 29 lines · Thomas Zimmermann

Instead of testing import_attach for imported GEM buffers, invoke drm_gem_is_imported() to do the test.

---
 drivers/gpu/drm/drm_mipi_dbi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_mipi_dbi.c +1 -1
index 34bca7567576..89e05a5bed1d 100644
@@ -218,7 +218,7 @@ int mipi_dbi_buf_copy(void *dst, struct iosys_map
 *src, struct drm_framebuffer *
        switch (fb->format->format) {
        case DRM_FORMAT_RGB565:
                if (swap)
-                       drm_fb_swab(&dst_map, NULL, src, fb, clip,
 !gem->import_attach,
+                       drm_fb_swab(&dst_map, NULL, src, fb, clip,
 !drm_gem_is_imported(gem),
                                    fmtcnv_state);
                else
                        drm_fb_memcpy(&dst_map, NULL, src, fb, clip);

--
2.48.1

author maintainer
#22 lore raw

PATCH 10/10 drm/prime: Use dma_buf from GEM object instance

In reply to Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_prime.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/gpu/drm/drm_prime.c +1 -7
index 32a8781cfd67..bdb51c8f262e 100644
@@ -453,13 +453,7 @@ struct dma_buf *drm_gem_prime_handle_to_dmabuf(struct drm_device *dev,
 	}
 
 	mutex_lock(&dev->object_name_lock);
-	/* re-export the original imported object */
-	if (obj->import_attach) {
-		dmabuf = obj->import_attach->dmabuf;
-		get_dma_buf(dmabuf);
-		goto out_have_obj;
-	}
-
+	/* re-export the original imported/exported object */
 	if (obj->dma_buf) {
 		get_dma_buf(obj->dma_buf);
 		dmabuf = obj->dma_buf;
Signature · 1 line
2.48.1
Reviewed-by on v1 10/10
#23 lore raw

Re: [PATCH 10/10] drm/prime: Use dma_buf from GEM object instance

Quoted 33 lines · Thomas Zimmermann

Avoid dereferencing struct drm_gem_object.import_attach for the imported dma-buf. The dma_buf field in the GEM object instance refers to the same buffer. Prepares to make import_attach optional.

---
 drivers/gpu/drm/drm_prime.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/gpu/drm/drm_prime.c +1 -7
index 32a8781cfd67..bdb51c8f262e 100644
@@ -453,13 +453,7 @@ struct dma_buf *drm_gem_prime_handle_to_dmabuf(struct
 drm_device *dev,
        }
 
        mutex_lock(&dev->object_name_lock);
-       /* re-export the original imported object */
-       if (obj->import_attach) {
-               dmabuf = obj->import_attach->dmabuf;
-               get_dma_buf(dmabuf);
-               goto out_have_obj;
-       }
-
+       /* re-export the original imported/exported object */
        if (obj->dma_buf) {
                get_dma_buf(obj->dma_buf);
                dmabuf = obj->dma_buf;

--
2.48.1

Reviewed-by on the v1 series
#24 lore raw
In reply to Thomas Zimmermann

Regards,
Christian.

Am 26.02.25 um 18:03 schrieb Thomas Zimmermann:

Quoted 43 lines

Do not use struct drm_gem_object.import_attach unnecessarily. The field is set by the PRIME import helpers. GEM and other helpers use it for testing for imported dma-bufs and retrieving the respective dma-buf instance.

Replace all tests for import_attach with the new GEM helper function drm_gem_is_imported(). The helper avoids import_attach in favor of testing the imported dma-buf's object against the provided GEM object.

Also replace references to import_attach.dmabuf. The imported dma-buf is also available in the GEM object's dma_buf field.

This series cleans up the GEM core and helpers. Drivers also use import_attach and should later be updated as well.

There's a TODO item for making import_attach entirely optional. This series is a first step towards this goal.

Thomas Zimmermann (10):
drm/gem: Test for imported GEM buffers with helper drm/gem-dma: Test for imported buffers with drm_gem_is_imported() drm/gem-dma: Use dma_buf from GEM object instance drm/gem-shmem: Test for imported buffers with drm_gem_is_imported() drm/gem-shmem: Use dma_buf from GEM object instance drm/gem-framebuffer: Test for imported buffers with
drm_gem_is_imported()
drm/gem-framebuffer: Use dma_buf from GEM object instance drm/fb-dma-helper: Test for imported buffers with
drm_gem_is_imported()
drm/mipi-dbi: Test for imported buffers with drm_gem_is_imported() drm/prime: Use dma_buf from GEM object instance

 drivers/gpu/drm/drm_fb_dma_helper.c          |  2 +-
 drivers/gpu/drm/drm_gem.c                    |  4 +--
 drivers/gpu/drm/drm_gem_dma_helper.c         |  4 +--
 drivers/gpu/drm/drm_gem_framebuffer_helper.c | 12 +++-----
 drivers/gpu/drm/drm_gem_shmem_helper.c       | 30 ++++++++++----------
 drivers/gpu/drm/drm_mipi_dbi.c               |  2 +-
 drivers/gpu/drm/drm_prime.c                  |  8 +-----
 include/drm/drm_gem.h                        | 14 +++++++++
 include/drm/drm_gem_shmem_helper.h           |  2 +-
 9 files changed, 41 insertions(+), 37 deletions(-)
author maintainer
#25 lore raw

Am 05.03.25 um 16:38 schrieb Christian König:

Quoted 1 line

Thanks. I have equivalent patches for drivers as well. I'll send them out once this series has propagated through the trees.

Best regards
Thomas

Quoted 48 lines

Regards,
Christian.

Am 26.02.25 um 18:03 schrieb Thomas Zimmermann:

Quoted 43 lines

Do not use struct drm_gem_object.import_attach unnecessarily. The field is set by the PRIME import helpers. GEM and other helpers use it for testing for imported dma-bufs and retrieving the respective dma-buf instance.

Replace all tests for import_attach with the new GEM helper function drm_gem_is_imported(). The helper avoids import_attach in favor of testing the imported dma-buf's object against the provided GEM object.

Also replace references to import_attach.dmabuf. The imported dma-buf is also available in the GEM object's dma_buf field.

This series cleans up the GEM core and helpers. Drivers also use import_attach and should later be updated as well.

There's a TODO item for making import_attach entirely optional. This series is a first step towards this goal.

Thomas Zimmermann (10):
drm/gem: Test for imported GEM buffers with helper drm/gem-dma: Test for imported buffers with drm_gem_is_imported() drm/gem-dma: Use dma_buf from GEM object instance drm/gem-shmem: Test for imported buffers with drm_gem_is_imported() drm/gem-shmem: Use dma_buf from GEM object instance drm/gem-framebuffer: Test for imported buffers with
drm_gem_is_imported()
drm/gem-framebuffer: Use dma_buf from GEM object instance drm/fb-dma-helper: Test for imported buffers with
drm_gem_is_imported()
drm/mipi-dbi: Test for imported buffers with drm_gem_is_imported() drm/prime: Use dma_buf from GEM object instance

  drivers/gpu/drm/drm_fb_dma_helper.c          |  2 +-
  drivers/gpu/drm/drm_gem.c                    |  4 +--
  drivers/gpu/drm/drm_gem_dma_helper.c         |  4 +--
  drivers/gpu/drm/drm_gem_framebuffer_helper.c | 12 +++-----
  drivers/gpu/drm/drm_gem_shmem_helper.c       | 30 ++++++++++----------
  drivers/gpu/drm/drm_mipi_dbi.c               |  2 +-
  drivers/gpu/drm/drm_prime.c                  |  8 +-----
  include/drm/drm_gem.h                        | 14 +++++++++
  include/drm/drm_gem_shmem_helper.h           |  2 +-
  9 files changed, 41 insertions(+), 37 deletions(-)
Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)