diff mbox series

drm/ioctl: Add a ioctl to label GEM objects

Message ID 20190916103541.29015-1-rohan.garg@collabora.com (mailing list archive)
State New, archived
Headers show
Series drm/ioctl: Add a ioctl to label GEM objects | expand

Commit Message

Rohan Garg Sept. 16, 2019, 10:35 a.m. UTC
DRM_IOCTL_BO_SET_LABEL lets you label GEM objects, making it
easier to debug issues in userspace applications.

Signed-off-by: Rohan Garg <rohan.garg@collabora.com>
---
 drivers/gpu/drm/drm_gem.c      | 51 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  2 ++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 include/drm/drm_gem.h          |  7 +++++
 include/uapi/drm/drm.h         | 18 ++++++++++++
 5 files changed, 79 insertions(+)

Comments

Eric Engestrom Sept. 16, 2019, 12:20 p.m. UTC | #1
On Monday, 2019-09-16 12:35:41 +0200, Rohan Garg wrote:
> DRM_IOCTL_BO_SET_LABEL lets you label GEM objects, making it
> easier to debug issues in userspace applications.
> 
> Signed-off-by: Rohan Garg <rohan.garg@collabora.com>
> ---
>  drivers/gpu/drm/drm_gem.c      | 51 ++++++++++++++++++++++++++++++++++
>  drivers/gpu/drm/drm_internal.h |  2 ++
>  drivers/gpu/drm/drm_ioctl.c    |  1 +
>  include/drm/drm_gem.h          |  7 +++++
>  include/uapi/drm/drm.h         | 18 ++++++++++++
>  5 files changed, 79 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
> index 6854f5867d51..c116b242d679 100644
> --- a/drivers/gpu/drm/drm_gem.c
> +++ b/drivers/gpu/drm/drm_gem.c
> @@ -941,6 +941,52 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
>  	idr_destroy(&file_private->object_idr);
>  }
>  
> +/**
> + * drm_gem_label - label a given GEM object
> + * @dev: drm_device which is being closed by userspace
> + * @data: drm_label_bo struct with a label, label length and any relevant flags
> + * @file_private: drm file-private structure to clean up
> + */
> +
> +int drm_gem_label(struct drm_device *dev, void *data,
> +		       struct drm_file *file_priv)
> +{
> +	struct drm_label_object *args = data;
> +	struct drm_gem_object *gem_obj;
> +	int err = 0;
> +
> +	if (!args->len || !args->name)
> +		return -EINVAL;
> +
> +	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
> +	if (!gem_obj) {
> +		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
> +		err = -ENOENT;
> +		goto err;
> +	}
> +
> +	if ((args->len == 0 && args->name == NULL) || gem_obj->label) {
> +		kfree(gem_obj->label);
> +		gem_obj->label = NULL;
> +	}
> +
> +	gem_obj->label = strndup_user(u64_to_user_ptr(args->name), args->len);
> +
> +	drm_gem_object_put_unlocked(gem_obj);
> +
> +	if (IS_ERR(gem_obj->label)) {
> +		err = PTR_ERR(gem_obj->label);
> +		goto err;
> +	}
> +
> +err:
> +	if (err != 0)
> +		args->flags = err;
> +
> +	return err;
> +}
> +
> +
>  /**
>   * drm_gem_object_release - release GEM buffer object resources
>   * @obj: GEM buffer object
> @@ -958,6 +1004,11 @@ drm_gem_object_release(struct drm_gem_object *obj)
>  
>  	dma_resv_fini(&obj->_resv);
>  	drm_gem_free_mmap_offset(obj);
> +
> +	if (obj->label) {
> +		kfree(obj->label);
> +		obj->label = NULL;
> +	}
>  }
>  EXPORT_SYMBOL(drm_gem_object_release);
>  
> diff --git a/drivers/gpu/drm/drm_internal.h b/drivers/gpu/drm/drm_internal.h
> index 51a2055c8f18..c9854b237fb6 100644
> --- a/drivers/gpu/drm/drm_internal.h
> +++ b/drivers/gpu/drm/drm_internal.h
> @@ -137,6 +137,8 @@ int drm_gem_pin(struct drm_gem_object *obj);
>  void drm_gem_unpin(struct drm_gem_object *obj);
>  void *drm_gem_vmap(struct drm_gem_object *obj);
>  void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr);
> +int drm_gem_label(struct drm_device *dev, void *data,
> +		       struct drm_file *file_priv);
>  
>  /* drm_debugfs.c drm_debugfs_crc.c */
>  #if defined(CONFIG_DEBUG_FS)
> diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c
> index f675a3bb2c88..7b6f4b7dadb9 100644
> --- a/drivers/gpu/drm/drm_ioctl.c
> +++ b/drivers/gpu/drm/drm_ioctl.c
> @@ -709,6 +709,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
>  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER),
>  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER),
>  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER),
> +	DRM_IOCTL_DEF(DRM_IOCTL_BO_SET_LABEL, drm_gem_label, DRM_RENDER_ALLOW),
>  };
>  
>  #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
> diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
> index 6aaba14f5972..f801c054e972 100644
> --- a/include/drm/drm_gem.h
> +++ b/include/drm/drm_gem.h
> @@ -237,6 +237,13 @@ struct drm_gem_object {
>  	 */
>  	int name;
>  
> +	/**
> +	 * @label:
> +	 *
> +	 * Label for this object, should be a human readable string.
> +	 */
> +	char *label;
> +
>  	/**
>  	 * @dma_buf:
>  	 *
> diff --git a/include/uapi/drm/drm.h b/include/uapi/drm/drm.h
> index 8a5b2f8f8eb9..286167f880de 100644
> --- a/include/uapi/drm/drm.h
> +++ b/include/uapi/drm/drm.h
> @@ -626,6 +626,23 @@ struct drm_gem_open {
>  	__u64 size;
>  };
>  
> +/** struct drm_label_object - ioctl argument for labelling BOs.
> + *
> + * This label's a BO with a userspace label
> + *
> + */
> +struct drm_label_object {
> +	/** Handle for the object being labelled. */
> +	__u32 handle;
> +
> +	/** Label and label length. */
> +	__u32 len;
> +	__u64 name;

Might be worth mentioning in the comment here that `len` includes the
trailing NULL.

I have no opinion on the new ioctl itself (and don't know enough to
form one).

> +
> +	/** Flags */
> +	int flags;
> +};
> +
>  #define DRM_CAP_DUMB_BUFFER		0x1
>  #define DRM_CAP_VBLANK_HIGH_CRTC	0x2
>  #define DRM_CAP_DUMB_PREFERRED_DEPTH	0x3
> @@ -946,6 +963,7 @@ extern "C" {
>  #define DRM_IOCTL_SYNCOBJ_QUERY		DRM_IOWR(0xCB, struct drm_syncobj_timeline_array)
>  #define DRM_IOCTL_SYNCOBJ_TRANSFER	DRM_IOWR(0xCC, struct drm_syncobj_transfer)
>  #define DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL	DRM_IOWR(0xCD, struct drm_syncobj_timeline_array)
> +#define DRM_IOCTL_BO_SET_LABEL      DRM_IOWR(0xCE, struct drm_label_object)
>  
>  /**
>   * Device specific ioctls should only be in their respective headers
> -- 
> 2.17.1
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel
Thomas Hellström (Intel) Sept. 16, 2019, 12:28 p.m. UTC | #2
On 9/16/19 12:35 PM, Rohan Garg wrote:
> DRM_IOCTL_BO_SET_LABEL lets you label GEM objects, making it
> easier to debug issues in userspace applications.
>
> Signed-off-by: Rohan Garg <rohan.garg@collabora.com>
> ---
>   drivers/gpu/drm/drm_gem.c      | 51 ++++++++++++++++++++++++++++++++++
>   drivers/gpu/drm/drm_internal.h |  2 ++
>   drivers/gpu/drm/drm_ioctl.c    |  1 +
>   include/drm/drm_gem.h          |  7 +++++
>   include/uapi/drm/drm.h         | 18 ++++++++++++
>   5 files changed, 79 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
> index 6854f5867d51..c116b242d679 100644
> --- a/drivers/gpu/drm/drm_gem.c
> +++ b/drivers/gpu/drm/drm_gem.c
> @@ -941,6 +941,52 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
>   	idr_destroy(&file_private->object_idr);
>   }
>   
> +/**
> + * drm_gem_label - label a given GEM object
> + * @dev: drm_device which is being closed by userspace

We're not closing a device, are we?


> + * @data: drm_label_bo struct with a label, label length and any relevant flags
> + * @file_private: drm file-private structure to clean up
> + */
> +
> +int drm_gem_label(struct drm_device *dev, void *data,
> +		       struct drm_file *file_priv)
> +{
> +	struct drm_label_object *args = data;
> +	struct drm_gem_object *gem_obj;
> +	int err = 0;
> +
> +	if (!args->len || !args->name)
> +		return -EINVAL;
> +
> +	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
> +	if (!gem_obj) {
> +		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
> +		err = -ENOENT;
> +		goto err;
> +	}
> +
> +	if ((args->len == 0 && args->name == NULL) || gem_obj->label) {
> +		kfree(gem_obj->label);
> +		gem_obj->label = NULL;
> +	}
> +
> +	gem_obj->label = strndup_user(u64_to_user_ptr(args->name), args->len);

Do we have a mechanism in place to stop a malicious unprivileged app 
from allocating all kernel memory to gem labels?

> +
> +	drm_gem_object_put_unlocked(gem_obj);
> +
> +	if (IS_ERR(gem_obj->label)) {
> +		err = PTR_ERR(gem_obj->label);
> +		goto err;
> +	}
> +
> +err:
> +	if (err != 0)
> +		args->flags = err;
> +
> +	return err;
> +}
> +
> +
>   /**
>    * drm_gem_object_release - release GEM buffer object resources
>    * @obj: GEM buffer object
> @@ -958,6 +1004,11 @@ drm_gem_object_release(struct drm_gem_object *obj)
>   
>   	dma_resv_fini(&obj->_resv);
>   	drm_gem_free_mmap_offset(obj);
> +
> +	if (obj->label) {
> +		kfree(obj->label);
> +		obj->label = NULL;
> +	}
>   }
>   EXPORT_SYMBOL(drm_gem_object_release);
>   
> diff --git a/drivers/gpu/drm/drm_internal.h b/drivers/gpu/drm/drm_internal.h
> index 51a2055c8f18..c9854b237fb6 100644
> --- a/drivers/gpu/drm/drm_internal.h
> +++ b/drivers/gpu/drm/drm_internal.h
> @@ -137,6 +137,8 @@ int drm_gem_pin(struct drm_gem_object *obj);
>   void drm_gem_unpin(struct drm_gem_object *obj);
>   void *drm_gem_vmap(struct drm_gem_object *obj);
>   void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr);
> +int drm_gem_label(struct drm_device *dev, void *data,
> +		       struct drm_file *file_priv);
>   
>   /* drm_debugfs.c drm_debugfs_crc.c */
>   #if defined(CONFIG_DEBUG_FS)
> diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c
> index f675a3bb2c88..7b6f4b7dadb9 100644
> --- a/drivers/gpu/drm/drm_ioctl.c
> +++ b/drivers/gpu/drm/drm_ioctl.c
> @@ -709,6 +709,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
>   	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER),
>   	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER),
>   	DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER),
> +	DRM_IOCTL_DEF(DRM_IOCTL_BO_SET_LABEL, drm_gem_label, DRM_RENDER_ALLOW),

Not all drivers use GEM. Could we have a struct drm_driver entry for this?

Thanks,

Thomas



>   };
>   
>   #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
> diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
> index 6aaba14f5972..f801c054e972 100644
> --- a/include/drm/drm_gem.h
> +++ b/include/drm/drm_gem.h
> @@ -237,6 +237,13 @@ struct drm_gem_object {
>   	 */
>   	int name;
>   
> +	/**
> +	 * @label:
> +	 *
> +	 * Label for this object, should be a human readable string.
> +	 */
> +	char *label;
> +
>   	/**
>   	 * @dma_buf:
>   	 *
> diff --git a/include/uapi/drm/drm.h b/include/uapi/drm/drm.h
> index 8a5b2f8f8eb9..286167f880de 100644
> --- a/include/uapi/drm/drm.h
> +++ b/include/uapi/drm/drm.h
> @@ -626,6 +626,23 @@ struct drm_gem_open {
>   	__u64 size;
>   };
>   
> +/** struct drm_label_object - ioctl argument for labelling BOs.
> + *
> + * This label's a BO with a userspace label
> + *
> + */
> +struct drm_label_object {
> +	/** Handle for the object being labelled. */
> +	__u32 handle;
> +
> +	/** Label and label length. */
> +	__u32 len;
> +	__u64 name;
> +
> +	/** Flags */
> +	int flags;
> +};
> +
>   #define DRM_CAP_DUMB_BUFFER		0x1
>   #define DRM_CAP_VBLANK_HIGH_CRTC	0x2
>   #define DRM_CAP_DUMB_PREFERRED_DEPTH	0x3
> @@ -946,6 +963,7 @@ extern "C" {
>   #define DRM_IOCTL_SYNCOBJ_QUERY		DRM_IOWR(0xCB, struct drm_syncobj_timeline_array)
>   #define DRM_IOCTL_SYNCOBJ_TRANSFER	DRM_IOWR(0xCC, struct drm_syncobj_transfer)
>   #define DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL	DRM_IOWR(0xCD, struct drm_syncobj_timeline_array)
> +#define DRM_IOCTL_BO_SET_LABEL      DRM_IOWR(0xCE, struct drm_label_object)
>   
>   /**
>    * Device specific ioctls should only be in their respective headers
Rohan Garg Sept. 17, 2019, 3:05 p.m. UTC | #3
Hi

> 
> We're not closing a device, are we?
>

Ah, yes, I'll fix this in v2.
 
> 
> Do we have a mechanism in place to stop a malicious unprivileged app
> from allocating all kernel memory to gem labels?
> 

I'm unsure why this is a concern since a malicious app could also potentially 
allocate lots of GEM buffers and cause the same situation.

Cheers
Rohan Garg
Rohan Garg Sept. 17, 2019, 3:06 p.m. UTC | #4
Hi

> Might be worth mentioning in the comment here that `len` includes the
> trailing NULL.
> 

Ack, I'll address this in v2.

Cheers
Rohan Garg
Thomas Hellström (Intel) Sept. 17, 2019, 3:10 p.m. UTC | #5
On 9/17/19 5:05 PM, Rohan Garg wrote:
> Hi
>
>> We're not closing a device, are we?
>>
> Ah, yes, I'll fix this in v2.
>   
>> Do we have a mechanism in place to stop a malicious unprivileged app
>> from allocating all kernel memory to gem labels?
>>
> I'm unsure why this is a concern since a malicious app could also potentially
> allocate lots of GEM buffers and cause the same situation.

Hmm, yes in some drivers. In others not. But if we have a drm_driver 
hook for this, I guess drivers that care can do proper checking.

/Thomas


> Cheers
> Rohan Garg
>
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
index 6854f5867d51..c116b242d679 100644
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -941,6 +941,52 @@  drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
 	idr_destroy(&file_private->object_idr);
 }
 
+/**
+ * drm_gem_label - label a given GEM object
+ * @dev: drm_device which is being closed by userspace
+ * @data: drm_label_bo struct with a label, label length and any relevant flags
+ * @file_private: drm file-private structure to clean up
+ */
+
+int drm_gem_label(struct drm_device *dev, void *data,
+		       struct drm_file *file_priv)
+{
+	struct drm_label_object *args = data;
+	struct drm_gem_object *gem_obj;
+	int err = 0;
+
+	if (!args->len || !args->name)
+		return -EINVAL;
+
+	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
+	if (!gem_obj) {
+		DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
+		err = -ENOENT;
+		goto err;
+	}
+
+	if ((args->len == 0 && args->name == NULL) || gem_obj->label) {
+		kfree(gem_obj->label);
+		gem_obj->label = NULL;
+	}
+
+	gem_obj->label = strndup_user(u64_to_user_ptr(args->name), args->len);
+
+	drm_gem_object_put_unlocked(gem_obj);
+
+	if (IS_ERR(gem_obj->label)) {
+		err = PTR_ERR(gem_obj->label);
+		goto err;
+	}
+
+err:
+	if (err != 0)
+		args->flags = err;
+
+	return err;
+}
+
+
 /**
  * drm_gem_object_release - release GEM buffer object resources
  * @obj: GEM buffer object
@@ -958,6 +1004,11 @@  drm_gem_object_release(struct drm_gem_object *obj)
 
 	dma_resv_fini(&obj->_resv);
 	drm_gem_free_mmap_offset(obj);
+
+	if (obj->label) {
+		kfree(obj->label);
+		obj->label = NULL;
+	}
 }
 EXPORT_SYMBOL(drm_gem_object_release);
 
diff --git a/drivers/gpu/drm/drm_internal.h b/drivers/gpu/drm/drm_internal.h
index 51a2055c8f18..c9854b237fb6 100644
--- a/drivers/gpu/drm/drm_internal.h
+++ b/drivers/gpu/drm/drm_internal.h
@@ -137,6 +137,8 @@  int drm_gem_pin(struct drm_gem_object *obj);
 void drm_gem_unpin(struct drm_gem_object *obj);
 void *drm_gem_vmap(struct drm_gem_object *obj);
 void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr);
+int drm_gem_label(struct drm_device *dev, void *data,
+		       struct drm_file *file_priv);
 
 /* drm_debugfs.c drm_debugfs_crc.c */
 #if defined(CONFIG_DEBUG_FS)
diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c
index f675a3bb2c88..7b6f4b7dadb9 100644
--- a/drivers/gpu/drm/drm_ioctl.c
+++ b/drivers/gpu/drm/drm_ioctl.c
@@ -709,6 +709,7 @@  static const struct drm_ioctl_desc drm_ioctls[] = {
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER),
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER),
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER),
+	DRM_IOCTL_DEF(DRM_IOCTL_BO_SET_LABEL, drm_gem_label, DRM_RENDER_ALLOW),
 };
 
 #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
index 6aaba14f5972..f801c054e972 100644
--- a/include/drm/drm_gem.h
+++ b/include/drm/drm_gem.h
@@ -237,6 +237,13 @@  struct drm_gem_object {
 	 */
 	int name;
 
+	/**
+	 * @label:
+	 *
+	 * Label for this object, should be a human readable string.
+	 */
+	char *label;
+
 	/**
 	 * @dma_buf:
 	 *
diff --git a/include/uapi/drm/drm.h b/include/uapi/drm/drm.h
index 8a5b2f8f8eb9..286167f880de 100644
--- a/include/uapi/drm/drm.h
+++ b/include/uapi/drm/drm.h
@@ -626,6 +626,23 @@  struct drm_gem_open {
 	__u64 size;
 };
 
+/** struct drm_label_object - ioctl argument for labelling BOs.
+ *
+ * This label's a BO with a userspace label
+ *
+ */
+struct drm_label_object {
+	/** Handle for the object being labelled. */
+	__u32 handle;
+
+	/** Label and label length. */
+	__u32 len;
+	__u64 name;
+
+	/** Flags */
+	int flags;
+};
+
 #define DRM_CAP_DUMB_BUFFER		0x1
 #define DRM_CAP_VBLANK_HIGH_CRTC	0x2
 #define DRM_CAP_DUMB_PREFERRED_DEPTH	0x3
@@ -946,6 +963,7 @@  extern "C" {
 #define DRM_IOCTL_SYNCOBJ_QUERY		DRM_IOWR(0xCB, struct drm_syncobj_timeline_array)
 #define DRM_IOCTL_SYNCOBJ_TRANSFER	DRM_IOWR(0xCC, struct drm_syncobj_transfer)
 #define DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL	DRM_IOWR(0xCD, struct drm_syncobj_timeline_array)
+#define DRM_IOCTL_BO_SET_LABEL      DRM_IOWR(0xCE, struct drm_label_object)
 
 /**
  * Device specific ioctls should only be in their respective headers