diff mbox series

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

Message ID 20250226172457.217725-2-tzimmermann@suse.de (mailing list archive)
State New
Headers show
Series drm: Hide import_attach from GEM code and helpers | expand

Commit Message

Thomas Zimmermann Feb. 26, 2025, 5:03 p.m. UTC
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 <tzimmermann@suse.de>
---
 drivers/gpu/drm/drm_gem.c |  4 ++--
 include/drm/drm_gem.h     | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)

Comments

Anusha Srivatsa Feb. 28, 2025, 4:04 p.m. UTC | #1
On Wed, Feb 26, 2025 at 12:28 PM Thomas Zimmermann <tzimmermann@suse.de>
wrote:

> 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 <tzimmermann@suse.de>
> ---
>  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);
> +}
> +
>

Looks good to me.
Reviewed-by: Anusha Srivatsa <asrivats@redhat.com>

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

Patch

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);
+}
+
 #ifdef CONFIG_LOCKDEP
 /**
  * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.