diff mbox series

drm/msm/gem: Add metadata

Message ID 20231027194537.408922-1-robdclark@gmail.com (mailing list archive)
State New, archived
Headers show
Series drm/msm/gem: Add metadata | expand

Commit Message

Rob Clark Oct. 27, 2023, 7:45 p.m. UTC
From: Rob Clark <robdclark@chromium.org>

The EXT_external_objects extension is a bit awkward as it doesn't pass
explicit modifiers, leaving the importer to guess with incomplete
information.  In the case of vk (turnip) exporting and gl (freedreno)
importing, the "OPTIMAL_TILING_EXT" layout depends on VkImageCreateInfo
flags (among other things), which the importer does not know.  Which
unfortunately leaves us with the need for a metadata back-channel.

The contents of the metadata are defined by userspace.  The
EXT_external_objects extension is only required to work between
compatible versions of gl and vk drivers, as defined by device and
driver UUIDs.

Signed-off-by: Rob Clark <robdclark@chromium.org>
---
 drivers/gpu/drm/msm/msm_drv.c | 59 +++++++++++++++++++++++++++++++++--
 drivers/gpu/drm/msm/msm_gem.h |  4 +++
 include/uapi/drm/msm_drm.h    |  2 ++
 3 files changed, 63 insertions(+), 2 deletions(-)

Comments

Dmitry Baryshkov Oct. 28, 2023, 1:16 a.m. UTC | #1
On Fri, 27 Oct 2023 at 22:45, Rob Clark <robdclark@gmail.com> wrote:
>
> From: Rob Clark <robdclark@chromium.org>
>
> The EXT_external_objects extension is a bit awkward as it doesn't pass
> explicit modifiers, leaving the importer to guess with incomplete
> information.  In the case of vk (turnip) exporting and gl (freedreno)
> importing, the "OPTIMAL_TILING_EXT" layout depends on VkImageCreateInfo
> flags (among other things), which the importer does not know.  Which
> unfortunately leaves us with the need for a metadata back-channel.
>
> The contents of the metadata are defined by userspace.  The
> EXT_external_objects extension is only required to work between
> compatible versions of gl and vk drivers, as defined by device and
> driver UUIDs.
>
> Signed-off-by: Rob Clark <robdclark@chromium.org>
> ---
>  drivers/gpu/drm/msm/msm_drv.c | 59 +++++++++++++++++++++++++++++++++--
>  drivers/gpu/drm/msm/msm_gem.h |  4 +++
>  include/uapi/drm/msm_drm.h    |  2 ++
>  3 files changed, 63 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/msm/msm_drv.c b/drivers/gpu/drm/msm/msm_drv.c
> index b61ccea05327..8fe2677ea37a 100644
> --- a/drivers/gpu/drm/msm/msm_drv.c
> +++ b/drivers/gpu/drm/msm/msm_drv.c
> @@ -37,9 +37,10 @@
>   * - 1.9.0 - Add MSM_SUBMIT_FENCE_SN_IN
>   * - 1.10.0 - Add MSM_SUBMIT_BO_NO_IMPLICIT
>   * - 1.11.0 - Add wait boost (MSM_WAIT_FENCE_BOOST, MSM_PREP_BOOST)
> + * - 1.12.0 - Add MSM_INFO_SET_METADATA and MSM_INFO_GET_METADATA
>   */
>  #define MSM_VERSION_MAJOR      1
> -#define MSM_VERSION_MINOR      10
> +#define MSM_VERSION_MINOR      12
>  #define MSM_VERSION_PATCHLEVEL 0
>
>  static void msm_deinit_vram(struct drm_device *ddev);
> @@ -566,6 +567,8 @@ static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
>                 break;
>         case MSM_INFO_SET_NAME:
>         case MSM_INFO_GET_NAME:
> +       case MSM_INFO_SET_METADATA:
> +       case MSM_INFO_GET_METADATA:
>                 break;
>         default:
>                 return -EINVAL;
> @@ -618,7 +621,7 @@ static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
>                 break;
>         case MSM_INFO_GET_NAME:
>                 if (args->value && (args->len < strlen(msm_obj->name))) {
> -                       ret = -EINVAL;
> +                       ret = -ETOOSMALL;

This is unrelated and it also slightly changes user interface, so it
IMO should come as a separate commit/

>                         break;
>                 }
>                 args->len = strlen(msm_obj->name);
> @@ -627,6 +630,58 @@ static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
>                                          msm_obj->name, args->len))
>                                 ret = -EFAULT;
>                 }
> +               break;
> +       case MSM_INFO_SET_METADATA:
> +               /* Impose a moderate upper bound on metadata size: */
> +               if (args->len > 128) {
> +                       ret = -EOVERFLOW;
> +                       break;
> +               }
> +
> +               ret = msm_gem_lock_interruptible(obj);
> +               if (ret)
> +                       break;
> +
> +               msm_obj->metadata =
> +                       krealloc(msm_obj->metadata, args->len, GFP_KERNEL);
> +               msm_obj->metadata_size = args->len;
> +
> +               if (copy_from_user(msm_obj->metadata, u64_to_user_ptr(args->value),
> +                                  args->len)) {
> +                       msm_obj->metadata_size = 0;
> +                       ret = -EFAULT;
> +               }
> +
> +               msm_gem_unlock(obj);
> +
> +               break;
> +       case MSM_INFO_GET_METADATA:
> +               if (!args->value) {
> +                       /*
> +                        * Querying the size is inherently racey, but
> +                        * EXT_external_objects expects the app to confirm
> +                        * via device and driver UUIDs that the exporter and
> +                        * importer versions match.  All we can do from the
> +                        * kernel side is check the length under obj lock
> +                        * when userspace tries to retrieve the metadata
> +                        */
> +                       args->len = msm_obj->metadata_size;
> +                       break;
> +               }
> +
> +               ret = msm_gem_lock_interruptible(obj);
> +               if (ret)
> +                       break;
> +
> +               if (args->len < msm_obj->metadata_size) {
> +                       ret = -ETOOSMALL;
> +               } else if (copy_to_user(u64_to_user_ptr(args->value),
> +                                       msm_obj->metadata, args->len)) {
> +                       ret = -EFAULT;
> +               }

Doesn't checkpwatch warn here about extra curly brackets?

> +
> +               msm_gem_unlock(obj);
> +
>                 break;
>         }
>
> diff --git a/drivers/gpu/drm/msm/msm_gem.h b/drivers/gpu/drm/msm/msm_gem.h
> index 7f34263048a3..8d414b072c29 100644
> --- a/drivers/gpu/drm/msm/msm_gem.h
> +++ b/drivers/gpu/drm/msm/msm_gem.h
> @@ -109,6 +109,10 @@ struct msm_gem_object {
>
>         char name[32]; /* Identifier to print for the debugfs files */
>
> +       /* userspace metadata backchannel */
> +       void *metadata;
> +       u32 metadata_size;
> +
>         /**
>          * pin_count: Number of times the pages are pinned
>          *
> diff --git a/include/uapi/drm/msm_drm.h b/include/uapi/drm/msm_drm.h
> index 6c34272a13fd..6f2a7ad04aa4 100644
> --- a/include/uapi/drm/msm_drm.h
> +++ b/include/uapi/drm/msm_drm.h
> @@ -139,6 +139,8 @@ struct drm_msm_gem_new {
>  #define MSM_INFO_GET_NAME      0x03   /* get debug name, returned by pointer */
>  #define MSM_INFO_SET_IOVA      0x04   /* set the iova, passed by value */
>  #define MSM_INFO_GET_FLAGS     0x05   /* get the MSM_BO_x flags */
> +#define MSM_INFO_SET_METADATA  0x06   /* set userspace metadata */
> +#define MSM_INFO_GET_METADATA  0x07   /* get userspace metadata */
>
>  struct drm_msm_gem_info {
>         __u32 handle;         /* in */
> --
> 2.41.0
>
Rob Clark Oct. 28, 2023, 3:35 p.m. UTC | #2
On Fri, Oct 27, 2023 at 6:16 PM Dmitry Baryshkov
<dmitry.baryshkov@linaro.org> wrote:
>
> On Fri, 27 Oct 2023 at 22:45, Rob Clark <robdclark@gmail.com> wrote:
> >
> > From: Rob Clark <robdclark@chromium.org>
> >
> > The EXT_external_objects extension is a bit awkward as it doesn't pass
> > explicit modifiers, leaving the importer to guess with incomplete
> > information.  In the case of vk (turnip) exporting and gl (freedreno)
> > importing, the "OPTIMAL_TILING_EXT" layout depends on VkImageCreateInfo
> > flags (among other things), which the importer does not know.  Which
> > unfortunately leaves us with the need for a metadata back-channel.
> >
> > The contents of the metadata are defined by userspace.  The
> > EXT_external_objects extension is only required to work between
> > compatible versions of gl and vk drivers, as defined by device and
> > driver UUIDs.

jfyi, userspace side of this at:
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25945

> >
> > Signed-off-by: Rob Clark <robdclark@chromium.org>
> > ---
> >  drivers/gpu/drm/msm/msm_drv.c | 59 +++++++++++++++++++++++++++++++++--
> >  drivers/gpu/drm/msm/msm_gem.h |  4 +++
> >  include/uapi/drm/msm_drm.h    |  2 ++
> >  3 files changed, 63 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/msm/msm_drv.c b/drivers/gpu/drm/msm/msm_drv.c
> > index b61ccea05327..8fe2677ea37a 100644
> > --- a/drivers/gpu/drm/msm/msm_drv.c
> > +++ b/drivers/gpu/drm/msm/msm_drv.c
> > @@ -37,9 +37,10 @@
> >   * - 1.9.0 - Add MSM_SUBMIT_FENCE_SN_IN
> >   * - 1.10.0 - Add MSM_SUBMIT_BO_NO_IMPLICIT
> >   * - 1.11.0 - Add wait boost (MSM_WAIT_FENCE_BOOST, MSM_PREP_BOOST)
> > + * - 1.12.0 - Add MSM_INFO_SET_METADATA and MSM_INFO_GET_METADATA
> >   */
> >  #define MSM_VERSION_MAJOR      1
> > -#define MSM_VERSION_MINOR      10
> > +#define MSM_VERSION_MINOR      12
> >  #define MSM_VERSION_PATCHLEVEL 0
> >
> >  static void msm_deinit_vram(struct drm_device *ddev);
> > @@ -566,6 +567,8 @@ static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
> >                 break;
> >         case MSM_INFO_SET_NAME:
> >         case MSM_INFO_GET_NAME:
> > +       case MSM_INFO_SET_METADATA:
> > +       case MSM_INFO_GET_METADATA:
> >                 break;
> >         default:
> >                 return -EINVAL;
> > @@ -618,7 +621,7 @@ static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
> >                 break;
> >         case MSM_INFO_GET_NAME:
> >                 if (args->value && (args->len < strlen(msm_obj->name))) {
> > -                       ret = -EINVAL;
> > +                       ret = -ETOOSMALL;
>
> This is unrelated and it also slightly changes user interface, so it
> IMO should come as a separate commit/

fair, although it was changed for consistency with GET_METADATA

> >                         break;
> >                 }
> >                 args->len = strlen(msm_obj->name);
> > @@ -627,6 +630,58 @@ static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
> >                                          msm_obj->name, args->len))
> >                                 ret = -EFAULT;
> >                 }
> > +               break;
> > +       case MSM_INFO_SET_METADATA:
> > +               /* Impose a moderate upper bound on metadata size: */
> > +               if (args->len > 128) {
> > +                       ret = -EOVERFLOW;
> > +                       break;
> > +               }
> > +
> > +               ret = msm_gem_lock_interruptible(obj);
> > +               if (ret)
> > +                       break;
> > +
> > +               msm_obj->metadata =
> > +                       krealloc(msm_obj->metadata, args->len, GFP_KERNEL);
> > +               msm_obj->metadata_size = args->len;
> > +
> > +               if (copy_from_user(msm_obj->metadata, u64_to_user_ptr(args->value),
> > +                                  args->len)) {
> > +                       msm_obj->metadata_size = 0;
> > +                       ret = -EFAULT;
> > +               }
> > +
> > +               msm_gem_unlock(obj);
> > +
> > +               break;
> > +       case MSM_INFO_GET_METADATA:
> > +               if (!args->value) {
> > +                       /*
> > +                        * Querying the size is inherently racey, but
> > +                        * EXT_external_objects expects the app to confirm
> > +                        * via device and driver UUIDs that the exporter and
> > +                        * importer versions match.  All we can do from the
> > +                        * kernel side is check the length under obj lock
> > +                        * when userspace tries to retrieve the metadata
> > +                        */
> > +                       args->len = msm_obj->metadata_size;
> > +                       break;
> > +               }
> > +
> > +               ret = msm_gem_lock_interruptible(obj);
> > +               if (ret)
> > +                       break;
> > +
> > +               if (args->len < msm_obj->metadata_size) {
> > +                       ret = -ETOOSMALL;
> > +               } else if (copy_to_user(u64_to_user_ptr(args->value),
> > +                                       msm_obj->metadata, args->len)) {
> > +                       ret = -EFAULT;
> > +               }
>
> Doesn't checkpwatch warn here about extra curly brackets?

It might.. it is one of the many checkpatch warnings that I dutifully ignore ;-)

BR,
-R

> > +
> > +               msm_gem_unlock(obj);
> > +
> >                 break;
> >         }
> >
> > diff --git a/drivers/gpu/drm/msm/msm_gem.h b/drivers/gpu/drm/msm/msm_gem.h
> > index 7f34263048a3..8d414b072c29 100644
> > --- a/drivers/gpu/drm/msm/msm_gem.h
> > +++ b/drivers/gpu/drm/msm/msm_gem.h
> > @@ -109,6 +109,10 @@ struct msm_gem_object {
> >
> >         char name[32]; /* Identifier to print for the debugfs files */
> >
> > +       /* userspace metadata backchannel */
> > +       void *metadata;
> > +       u32 metadata_size;
> > +
> >         /**
> >          * pin_count: Number of times the pages are pinned
> >          *
> > diff --git a/include/uapi/drm/msm_drm.h b/include/uapi/drm/msm_drm.h
> > index 6c34272a13fd..6f2a7ad04aa4 100644
> > --- a/include/uapi/drm/msm_drm.h
> > +++ b/include/uapi/drm/msm_drm.h
> > @@ -139,6 +139,8 @@ struct drm_msm_gem_new {
> >  #define MSM_INFO_GET_NAME      0x03   /* get debug name, returned by pointer */
> >  #define MSM_INFO_SET_IOVA      0x04   /* set the iova, passed by value */
> >  #define MSM_INFO_GET_FLAGS     0x05   /* get the MSM_BO_x flags */
> > +#define MSM_INFO_SET_METADATA  0x06   /* set userspace metadata */
> > +#define MSM_INFO_GET_METADATA  0x07   /* get userspace metadata */
> >
> >  struct drm_msm_gem_info {
> >         __u32 handle;         /* in */
> > --
> > 2.41.0
> >
>
>
> --
> With best wishes
> Dmitry
Rob Clark Oct. 29, 2023, 3:07 p.m. UTC | #3
From: Rob Clark <robdclark@chromium.org>

Add metadata mechanism to provide a back-channel to communicate image
layout information between vk and gl, because EXT_external_objects
doesn't support explicit modifiers and "OPTIMAL_TILING_EXT" is not
enough information for the importer to deduce the layout.

Rob Clark (2):
  drm/msm: Small uabi fixes
  drm/msm/gem: Add metadata

 drivers/gpu/drm/msm/msm_drv.c | 59 +++++++++++++++++++++++++++++++++--
 drivers/gpu/drm/msm/msm_gem.c |  1 +
 drivers/gpu/drm/msm/msm_gem.h |  4 +++
 include/uapi/drm/msm_drm.h    |  2 ++
 4 files changed, 64 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/drivers/gpu/drm/msm/msm_drv.c b/drivers/gpu/drm/msm/msm_drv.c
index b61ccea05327..8fe2677ea37a 100644
--- a/drivers/gpu/drm/msm/msm_drv.c
+++ b/drivers/gpu/drm/msm/msm_drv.c
@@ -37,9 +37,10 @@ 
  * - 1.9.0 - Add MSM_SUBMIT_FENCE_SN_IN
  * - 1.10.0 - Add MSM_SUBMIT_BO_NO_IMPLICIT
  * - 1.11.0 - Add wait boost (MSM_WAIT_FENCE_BOOST, MSM_PREP_BOOST)
+ * - 1.12.0 - Add MSM_INFO_SET_METADATA and MSM_INFO_GET_METADATA
  */
 #define MSM_VERSION_MAJOR	1
-#define MSM_VERSION_MINOR	10
+#define MSM_VERSION_MINOR	12
 #define MSM_VERSION_PATCHLEVEL	0
 
 static void msm_deinit_vram(struct drm_device *ddev);
@@ -566,6 +567,8 @@  static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
 		break;
 	case MSM_INFO_SET_NAME:
 	case MSM_INFO_GET_NAME:
+	case MSM_INFO_SET_METADATA:
+	case MSM_INFO_GET_METADATA:
 		break;
 	default:
 		return -EINVAL;
@@ -618,7 +621,7 @@  static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
 		break;
 	case MSM_INFO_GET_NAME:
 		if (args->value && (args->len < strlen(msm_obj->name))) {
-			ret = -EINVAL;
+			ret = -ETOOSMALL;
 			break;
 		}
 		args->len = strlen(msm_obj->name);
@@ -627,6 +630,58 @@  static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
 					 msm_obj->name, args->len))
 				ret = -EFAULT;
 		}
+		break;
+	case MSM_INFO_SET_METADATA:
+		/* Impose a moderate upper bound on metadata size: */
+		if (args->len > 128) {
+			ret = -EOVERFLOW;
+			break;
+		}
+
+		ret = msm_gem_lock_interruptible(obj);
+		if (ret)
+			break;
+
+		msm_obj->metadata =
+			krealloc(msm_obj->metadata, args->len, GFP_KERNEL);
+		msm_obj->metadata_size = args->len;
+
+		if (copy_from_user(msm_obj->metadata, u64_to_user_ptr(args->value),
+				   args->len)) {
+			msm_obj->metadata_size = 0;
+			ret = -EFAULT;
+		}
+
+		msm_gem_unlock(obj);
+
+		break;
+	case MSM_INFO_GET_METADATA:
+		if (!args->value) {
+			/*
+			 * Querying the size is inherently racey, but
+			 * EXT_external_objects expects the app to confirm
+			 * via device and driver UUIDs that the exporter and
+			 * importer versions match.  All we can do from the
+			 * kernel side is check the length under obj lock
+			 * when userspace tries to retrieve the metadata
+			 */
+			args->len = msm_obj->metadata_size;
+			break;
+		}
+
+		ret = msm_gem_lock_interruptible(obj);
+		if (ret)
+			break;
+
+		if (args->len < msm_obj->metadata_size) {
+			ret = -ETOOSMALL;
+		} else if (copy_to_user(u64_to_user_ptr(args->value),
+					msm_obj->metadata, args->len)) {
+			ret = -EFAULT;
+		}
+
+		msm_gem_unlock(obj);
+
 		break;
 	}
 
diff --git a/drivers/gpu/drm/msm/msm_gem.h b/drivers/gpu/drm/msm/msm_gem.h
index 7f34263048a3..8d414b072c29 100644
--- a/drivers/gpu/drm/msm/msm_gem.h
+++ b/drivers/gpu/drm/msm/msm_gem.h
@@ -109,6 +109,10 @@  struct msm_gem_object {
 
 	char name[32]; /* Identifier to print for the debugfs files */
 
+	/* userspace metadata backchannel */
+	void *metadata;
+	u32 metadata_size;
+
 	/**
 	 * pin_count: Number of times the pages are pinned
 	 *
diff --git a/include/uapi/drm/msm_drm.h b/include/uapi/drm/msm_drm.h
index 6c34272a13fd..6f2a7ad04aa4 100644
--- a/include/uapi/drm/msm_drm.h
+++ b/include/uapi/drm/msm_drm.h
@@ -139,6 +139,8 @@  struct drm_msm_gem_new {
 #define MSM_INFO_GET_NAME	0x03   /* get debug name, returned by pointer */
 #define MSM_INFO_SET_IOVA	0x04   /* set the iova, passed by value */
 #define MSM_INFO_GET_FLAGS	0x05   /* get the MSM_BO_x flags */
+#define MSM_INFO_SET_METADATA	0x06   /* set userspace metadata */
+#define MSM_INFO_GET_METADATA	0x07   /* get userspace metadata */
 
 struct drm_msm_gem_info {
 	__u32 handle;         /* in */