All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Brost <matthew.brost@intel.com>
To: Niranjana Vishwanathapura <niranjana.vishwanathapura@intel.com>
Cc: <dri-devel@lists.freedesktop.org>, <intel-xe@lists.freedesktop.org>
Subject: Re: [PATCH 1/2] drm/gem: add helper to detect self-imported PRIME dma-bufs
Date: Mon, 12 Oct 2026 14:58:31 -0700	[thread overview]
Message-ID: <as1YBxIyBNLaChEp@gsse-cloud1.jf.intel.com> (raw)
In-Reply-To: <20261012180543.1488703-5-niranjana.vishwanathapura@intel.com>

On Mon, Oct 12, 2026 at 11:05:42AM -0700, Niranjana Vishwanathapura wrote:
> 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>

I would suggest for this series at least one other driver be fixed to
use this helper or all drivers (a decent model should be able to do
this easily) to show this isn't just an Xe idea.

> ---
>  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
> 

  reply	other threads:[~2026-10-12 21:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-12 18:05 [PATCH 0/2] drm: add a self-import PRIME dma-buf helper and use it in Xe Niranjana Vishwanathapura
2026-10-12 18:05 ` [PATCH 1/2] drm/gem: add helper to detect self-imported PRIME dma-bufs Niranjana Vishwanathapura
2026-10-12 21:58   ` Matthew Brost [this message]
2026-10-12 18:05 ` [PATCH 2/2] drm/xe/dma-buf: Use drm helper to detect dma-buf self-import Niranjana Vishwanathapura
2026-10-12 21:59   ` Matthew Brost
2026-10-12 20:47 ` ✓ CI.KUnit: success for drm: add a self-import PRIME dma-buf helper and use it in Xe Patchwork
2026-10-13  0:40 ` ✓ Xe.CI.BAT: " Patchwork
2026-10-13  8:22 ` ✓ Xe.CI.FULL: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=as1YBxIyBNLaChEp@gsse-cloud1.jf.intel.com \
    --to=matthew.brost@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=niranjana.vishwanathapura@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.