Skip to contentSkip to conversation

drm/ioctl: Add a ioctl to label GEM objects

PATCH Superseded Rohan Garg 6 participants 12 messages Oct 11, 2019 – Oct 22, 2019

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Rohan Garg

author
#0 lore raw

PATCH v4 1/1 drm/ioctl: Add a ioctl to label GEM objects

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

Changes in v2:
- Hoist the IOCTL up into the drm_driver framework

Changes in v3:
- Introduce a drm_gem_set_label for drivers to use internally in order to label a GEM object
- Hoist string copying up into the IOCTL
- Fix documentation
- Move actual gem labeling into drm_gem_adopt_label

Changes in v4:
- Refactor IOCTL call to only perform string duplication and move all gem lookup logic into GEM specific call

---
 drivers/gpu/drm/drm_gem.c      | 70 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  8 ++++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 include/drm/drm_drv.h          | 16 ++++++++
 include/drm/drm_gem.h          |  7 ++++
 include/uapi/drm/drm.h         | 20 ++++++++++
 6 files changed, 122 insertions(+)
drivers/gpu/drm/drm_gem.c +70 -0
index 6854f5867d51..0fa4609b2817 100644
@@ -941,6 +941,71 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
 	idr_destroy(&file_private->object_idr);
 }
 
+int drm_dumb_set_label_ioctl(struct drm_device *dev,
+				void *data, struct drm_file *file_priv)
+{
+	char *label;
+	struct drm_dumb_set_label_object *args = data;
+	int ret = 0;
+
+	if (!args->len || !args->name)
+		return -EINVAL;
+
+	if (!dev->driver->label)
+		return -EOPNOTSUPP;
+
+	label = strndup_user(u64_to_user_ptr(args->name), args->len);
+
+	if (IS_ERR(label)) {
+		ret = PTR_ERR(label);
+		goto err;
+	}
+
+	ret = dev->driver->label(dev, file_priv, args->handle, label);
+
+err:
+	kfree(label);
+	return ret;
+}
+
+int drm_gem_set_label(struct drm_device *dev,
+		       struct drm_file *file_priv,
+			   uint32_t handle,
+			   const char *label)
+{
+	struct drm_gem_object *gem_obj;
+	int ret = 0;
+
+	gem_obj = drm_gem_object_lookup(file_priv, handle);
+	if (!gem_obj) {
+		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
+		ret = -ENOENT;
+		goto err;
+	}
+	drm_gem_adopt_label(gem_obj, label);
+
+err:
+	drm_gem_object_put_unlocked(gem_obj);
+	return ret;
+}
+EXPORT_SYMBOL(drm_gem_set_label);
+
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char *label)
+{
+	char *adopted_label = NULL;
+
+	if (label)
+		adopted_label = kstrdup(label, GFP_KERNEL);
+
+	if (gem_obj->label) {
+		kfree(gem_obj->label);
+		gem_obj->label = NULL;
+	}
+
+	gem_obj->label = adopted_label;
+}
+EXPORT_SYMBOL(drm_gem_adopt_label);
+
 /**
  * drm_gem_object_release - release GEM buffer object resources
  * @obj: GEM buffer object
@@ -958,6 +1023,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);
 
drivers/gpu/drm/drm_internal.h +8 -0
index 51a2055c8f18..cbc3f7b7fb9b 100644
@@ -137,6 +137,14 @@ 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_dumb_set_label_ioctl(struct drm_device *dev,
+			void *data,
+			struct drm_file *file_priv);
+int drm_gem_set_label(struct drm_device *dev,
+			struct drm_file *file_priv,
+			uint32_t handle,
+			const char *label);
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char *label);
 
 /* drm_debugfs.c drm_debugfs_crc.c */
 #if defined(CONFIG_DEBUG_FS)
drivers/gpu/drm/drm_ioctl.c +1 -0
index fcd728d7cf72..f34e1571d70f 100644
@@ -714,6 +714,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_DUMB_SET_LABEL, drm_dumb_set_label_ioctl, DRM_RENDER_ALLOW),
 };
 
 #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
include/drm/drm_drv.h +16 -0
index cf13470810a5..501a3924354a 100644
@@ -715,6 +715,22 @@ struct drm_driver {
 			    struct drm_device *dev,
 			    uint32_t handle);
 
+	/**
+	 * @label:
+	 *
+	 * This label's a buffer object.
+	 *
+	 * Called by the user via ioctl.
+	 *
+	 * Returns:
+	 *
+	 * Zero on success, negative errno on failure.
+	 */
+	int (*label)(struct drm_device *dev,
+				struct drm_file *file_priv,
+				uint32_t handle,
+				char *label);
+
 	/**
 	 * @gem_vm_ops: Driver private ops for this object
 	 *
include/drm/drm_gem.h +7 -0
index 6aaba14f5972..f801c054e972 100644
@@ -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:
 	 *
include/uapi/drm/drm.h +20 -0
index 8a5b2f8f8eb9..309c3c091385 100644
@@ -626,6 +626,25 @@ struct drm_gem_open {
 	__u64 size;
 };
 
+/** struct drm_dumb_set_label_object - ioctl argument for labelling BOs.
+ *
+ * This label's a BO with a userspace label
+ *
+ */
+struct drm_dumb_set_label_object {
+	/** Handle for the object being labelled. */
+	__u32 handle;
+
+	/** Label and label length.
+	 *  len includes the trailing NUL.
+	 */
+	__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 +965,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_DUMB_SET_LABEL      DRM_IOWR(0xCE, struct drm_dumb_set_label_object)
 
 /**
  * Device specific ioctls should only be in their respective headers
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

Hi Rohan,

Quoted 2 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

I'm not sure if this was pointed out already, but dumb buffers != GEM buffers. GEM buffers _can_ be dumb, but might not be.

Could you please rename to GEM_SET_LABEL?

Cheers,
Daniel
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw

Hi

Am 11.10.19 um 19:09 schrieb Daniel Stone:

Quoted 10 lines

Hi Rohan,

Quoted 2 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

I'm not sure if this was pointed out already, but dumb buffers != GEM buffers. GEM buffers _can_ be dumb, but might not be.

Could you please rename to GEM_SET_LABEL?

This change to build upon dumb buffers was suggested by me, as dumb buffers is the closes thing there is to a buffer management interface. Drivers with 'smart buffers' would have to add their own ioctl interfaces.

What I really miss here is a userspace application that uses this functionality. It would be much easier to discuss if there was an actual example.

Best regards

Quoted 7 lines

Cheers,
Daniel
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 9 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Linux GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah
HRB 21284 (AG Nürnberg)
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw
Quoted 9 lines · Thomas Zimmermann

Hi

Am 11.10.19 um 19:09 schrieb Daniel Stone:

Quoted 6 lines

Hi Rohan,

Quoted 2 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

Quoted 8 lines
Quoted 4 lines

I'm not sure if this was pointed out already, but dumb buffers != GEM buffers. GEM buffers _can_ be dumb, but might not be.

Could you please rename to GEM_SET_LABEL?

This change to build upon dumb buffers was suggested by me, as dumb buffers is the closes thing there is to a buffer management interface. Drivers with 'smart buffers' would have to add their own ioctl interfaces.

We already have some IOCTLs that apply to gem buffers, not just dumb buffers, so GEM_SET_LABEL seems a lot more reasonable to me, too.

Quoted 3 lines

What I really miss here is a userspace application that uses this functionality. It would be much easier to discuss if there was an actual example.

+1.

Cheers, Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#4 lore raw
In reply to Thomas Zimmermann

Hi Thomas

Quoted 14 lines · On viernes, 11 de octubre de 2019 19:55:36 (CEST) Thomas Zimmermann wrote:

Hi

Am 11.10.19 um 19:09 schrieb Daniel Stone:

Quoted 10 lines

Hi Rohan,

Quoted 2 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

I'm not sure if this was pointed out already, but dumb buffers != GEM buffers. GEM buffers _can_ be dumb, but might not be.

Could you please rename to GEM_SET_LABEL?

Quoted 8 lines

This change to build upon dumb buffers was suggested by me, as dumb buffers is the closes thing there is to a buffer management interface. Drivers with 'smart buffers' would have to add their own ioctl interfaces.

What I really miss here is a userspace application that uses this functionality. It would be much easier to discuss if there was an actual example.

I'm currently working on implementing something for Mesa. I'll send a v5 based on the lessons learnt from that patchset.

Quoted 8 lines

Best regards

Quoted 6 lines

Cheers,
Daniel
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author
#5 lore raw
In reply to Daniel Stone

Hey

Quoted 11 lines · On viernes, 11 de octubre de 2019 19:09:52 (CEST) Daniel Stone wrote:

Hi Rohan,

Quoted 2 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

I'm not sure if this was pointed out already, but dumb buffers != GEM buffers. GEM buffers _can_ be dumb, but might not be.

Could you please rename to GEM_SET_LABEL?

Daniel and I had a opportunity to talk about this in person and we agreed that HANDLE_SET_LABEL would be a more sensible name.

Cheers
Rohan Garg

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw
In reply to Rohan Garg

Hello Rohan,

Quoted 32 lines · On Fri, 11 Oct 2019 16:30:09 +0200 Rohan Garg <[email protected]> wrote:

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

Changes in v2:
- Hoist the IOCTL up into the drm_driver framework

Changes in v3:
- Introduce a drm_gem_set_label for drivers to use internally in order to label a GEM object
- Hoist string copying up into the IOCTL
- Fix documentation
- Move actual gem labeling into drm_gem_adopt_label

Changes in v4:
- Refactor IOCTL call to only perform string duplication and move all gem lookup logic into GEM specific call

---
 drivers/gpu/drm/drm_gem.c      | 70 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  8 ++++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 include/drm/drm_drv.h          | 16 ++++++++
 include/drm/drm_gem.h          |  7 ++++
 include/uapi/drm/drm.h         | 20 ++++++++++
 6 files changed, 122 insertions(+)
drivers/gpu/drm/drm_gem.c +0 -0
index 6854f5867d51..0fa4609b2817 100644

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 944

Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1 -0
@@ -941,6 +941,71 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
 	idr_destroy(&file_private->object_idr);
 }
 
+int drm_dumb_set_label_ioctl(struct drm_device *dev,

Why dumb? Not sure what a smart set_label_ioctl() function would do differently :-).

Oh, and if this function can be used to label TTM BOs it should be moved somewhere else (drm_ioctl.c ?).

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 945

Quoted 1 line
drivers/gpu/drm/drm_gem.c +1 -0
+				void *data, struct drm_file *file_priv)

Indentation is broken here.

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 951

Quoted 6 lines
drivers/gpu/drm/drm_gem.c +6 -0
+{
+	char *label;
+	struct drm_dumb_set_label_object *args = data;
+	int ret = 0;
+
+	if (!args->len || !args->name)

Hm, I thought args->len == 0 was a valid case and meant "free the existing label". Has it changed. The case that's not allowed is args->len && !args->name.

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 958

Quoted 7 lines
drivers/gpu/drm/drm_gem.c +7 -0
+		return -EINVAL;
+
+	if (!dev->driver->label)
+		return -EOPNOTSUPP;
+
+	label = strndup_user(u64_to_user_ptr(args->name), args->len);
+

Remove this blank line.

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 966

Quoted 8 lines
drivers/gpu/drm/drm_gem.c +8 -0
+	if (IS_ERR(label)) {
+		ret = PTR_ERR(label);
+		goto err;
+	}
+
+	ret = dev->driver->label(dev, file_priv, args->handle, label);
+
+err:

I would s/err/out/ as this is not an error-only path.

Quoted 11 lines
drivers/gpu/drm/drm_gem.c +11 -0
+	kfree(label);
+	return ret;
+}
+
+int drm_gem_set_label(struct drm_device *dev,
+		       struct drm_file *file_priv,
+			   uint32_t handle,
+			   const char *label)
+{
+	struct drm_gem_object *gem_obj;
+	int ret = 0;

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 987

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +10 -0
+
+	gem_obj = drm_gem_object_lookup(file_priv, handle);
+	if (!gem_obj) {
+		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
+		ret = -ENOENT;
+		goto err;
+	}
+	drm_gem_adopt_label(gem_obj, label);
+
+err:

Ditto: s/err/out/

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 998

Quoted 11 lines
drivers/gpu/drm/drm_gem.c +11 -0
+	drm_gem_object_put_unlocked(gem_obj);
+	return ret;
+}
+EXPORT_SYMBOL(drm_gem_set_label);
+
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char *label)
+{
+	char *adopted_label = NULL;
+
+	if (label)
+		adopted_label = kstrdup(label, GFP_KERNEL);

Add

		WARN_ON(adopted_label);

drivers/gpu/drm/drm_gem.c drm_gem_release(struct drm_device *dev, struct drm_file *file_private) · line 1002

Quoted 4 lines
drivers/gpu/drm/drm_gem.c +4 -0
+
+	if (gem_obj->label) {
+		kfree(gem_obj->label);
+		gem_obj->label = NULL;

This assignment is useless since gem_obj->label is assigned below.

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +6 -0
+	}
+
+	gem_obj->label = adopted_label;
+}
+EXPORT_SYMBOL(drm_gem_adopt_label);
+
 /**
  * drm_gem_object_release - release GEM buffer object resources
  * @obj: GEM buffer object

drivers/gpu/drm/drm_gem.c drm_gem_object_release(struct drm_gem_object *obj) · line 1030

Quoted 8 lines
drivers/gpu/drm/drm_gem.c +5 -0
@@ -958,6 +1023,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;
+	}

You can call kfree(obj->label) directly (kfree() checks for NULL vals), and no need to reset obj->label (obj should be free when the function returns).

Quoted 44 lines
drivers/gpu/drm/drm_gem.c +0 -0
 }
 EXPORT_SYMBOL(drm_gem_object_release);
 
drivers/gpu/drm/drm_internal.h +8 -0
index 51a2055c8f18..cbc3f7b7fb9b 100644
@@ -137,6 +137,14 @@ 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_dumb_set_label_ioctl(struct drm_device *dev,
+			void *data,
+			struct drm_file *file_priv);
+int drm_gem_set_label(struct drm_device *dev,
+			struct drm_file *file_priv,
+			uint32_t handle,
+			const char *label);
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char *label);
 
 /* drm_debugfs.c drm_debugfs_crc.c */
 #if defined(CONFIG_DEBUG_FS)
drivers/gpu/drm/drm_ioctl.c +1 -0
index fcd728d7cf72..f34e1571d70f 100644
@@ -714,6 +714,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_DUMB_SET_LABEL, drm_dumb_set_label_ioctl, DRM_RENDER_ALLOW),
 };
 
 #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
include/drm/drm_drv.h +2 -0
index cf13470810a5..501a3924354a 100644
@@ -715,6 +715,22 @@ struct drm_driver {
 			    struct drm_device *dev,
 			    uint32_t handle);
 
+	/**
+	 * @label:

include/drm/drm_drv.h struct drm_driver { · line 729

Quoted 10 lines
include/drm/drm_drv.h +10 -0
+	 *
+	 * This label's a buffer object.
+	 *
+	 * Called by the user via ioctl.
+	 *
+	 * Returns:
+	 *
+	 * Zero on success, negative errno on failure.
+	 */
+	int (*label)(struct drm_device *dev,

Maybe label_bo or set_bo_label instead of just label, just to make it clear that the label is applied to a buffer object.

include/drm/drm_drv.h struct drm_driver { · line 732

Quoted 3 lines
include/drm/drm_drv.h +3 -0
+				struct drm_file *file_priv,
+				uint32_t handle,
+				char *label);

Indentation issue here as well.

Quoted 32 lines
include/drm/drm_drv.h +1 -0
+
 	/**
 	 * @gem_vm_ops: Driver private ops for this object
 	 *
include/drm/drm_gem.h +7 -0
index 6aaba14f5972..f801c054e972 100644
@@ -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:
 	 *
include/uapi/drm/drm.h +2 -0
index 8a5b2f8f8eb9..309c3c091385 100644
@@ -626,6 +626,25 @@ struct drm_gem_open {
 	__u64 size;
 };
 
+/** struct drm_dumb_set_label_object - ioctl argument for labelling BOs.
+ *

include/uapi/drm/drm.h struct drm_gem_open { · line 640

Quoted 10 lines
include/uapi/drm/drm.h +10 -0
+ * This label's a BO with a userspace label
+ *
+ */
+struct drm_dumb_set_label_object {
+	/** Handle for the object being labelled. */
+	__u32 handle;
+
+	/** Label and label length.
+	 *  len includes the trailing NUL.
+	 */

Nitpick: the comment fits on a single line.

	/** Label and label length (len includes the trailing NUL). */
Quoted 11 lines
include/uapi/drm/drm.h +7 -0
+	__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

include/uapi/drm/drm.h extern "C" { · line 968

Quoted 4 lines
include/uapi/drm/drm.h +1 -0
@@ -946,6 +965,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_DUMB_SET_LABEL      DRM_IOWR(0xCE, struct drm_dumb_set_label_object)

Maybe s/DRM_IOCTL_DUMB_SET_LABEL/DRM_IOCTL_SET_BO_LABEL/ and s/drm_dumb_set_label_object/drm_set_bo_label/

include/uapi/drm/drm.h extern "C" { · line 971

Quoted 3 lines
include/uapi/drm/drm.h +0 -0
 /**
  * Device specific ioctls should only be in their respective headers

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw
In reply to Rohan Garg

Hi Rohan,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[cannot apply to v5.4-rc2 next-20191011]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Rohan-Garg/drm-ioctl-Add-a-ioctl-to-label-GEM-objects/20191012-062955

If you fix the issue, kindly add following tag

smatch warnings:
drivers/gpu/drm/drm_gem.c:967 drm_dumb_set_label_ioctl() error: 'label' dereferencing possible ERR_PTR()

# https://github.com/0day-ci/linux/commit/0f0cd7ef9f3b1623ab982f12dc748998f31e10b4
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 0f0cd7ef9f3b1623ab982f12dc748998f31e10b4
vim +/label +967 drivers/gpu/drm/drm_gem.c

673a394b1e3b69 Eric Anholt 2008-07-30  943  
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  944  int drm_dumb_set_label_ioctl(struct drm_device *dev,
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  945  				void *data, struct drm_file *file_priv)
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  946  {
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  947  	char *label;
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  948  	struct drm_dumb_set_label_object *args = data;
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  949  	int ret = 0;
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  950  
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  951  	if (!args->len || !args->name)
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  952  		return -EINVAL;
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  953  
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  954  	if (!dev->driver->label)
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  955  		return -EOPNOTSUPP;
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  956  
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  957  	label = strndup_user(u64_to_user_ptr(args->name), args->len);
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  958  
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  959  	if (IS_ERR(label)) {
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  960  		ret = PTR_ERR(label);
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  961  		goto err;
                                                        ^^^^^^^^

Just return PTR_ERR(label);

0f0cd7ef9f3b16 Rohan Garg  2019-10-11  962  	}
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  963  
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  964  	ret = dev->driver->label(dev, file_priv, args->handle, label);
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  965  
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  966  err:
0f0cd7ef9f3b16 Rohan Garg  2019-10-11 @967  	kfree(label);
                                                ^^^^^^^^^^^^

This will Oops.

0f0cd7ef9f3b16 Rohan Garg  2019-10-11  968  	return ret;
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  969  }
0f0cd7ef9f3b16 Rohan Garg  2019-10-11  970  

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#8 lore raw
In reply to Rohan Garg
Quoted 13 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

Changes in v2:
- Hoist the IOCTL up into the drm_driver framework

Changes in v3:
- Introduce a drm_gem_set_label for drivers to use internally in order to label a GEM object
- Hoist string copying up into the IOCTL
- Fix documentation
- Move actual gem labeling into drm_gem_adopt_label

Quoted 3 lines

Changes in v4:
- Refactor IOCTL call to only perform string duplication and move all gem lookup logic into GEM specific call

I still think we should just make this GEM-only and avoid the indirection. Except if your userspace actually does run on vmwgfx, and you absolutely want/need it to run there. Everything else is GEM-only. -Daniel

Quoted 220 lines
---
 drivers/gpu/drm/drm_gem.c      | 70 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  8 ++++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 include/drm/drm_drv.h          | 16 ++++++++
 include/drm/drm_gem.h          |  7 ++++
 include/uapi/drm/drm.h         | 20 ++++++++++
 6 files changed, 122 insertions(+)
drivers/gpu/drm/drm_gem.c +70 -0
index 6854f5867d51..0fa4609b2817 100644
@@ -941,6 +941,71 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
 	idr_destroy(&file_private->object_idr);
 }
 
+int drm_dumb_set_label_ioctl(struct drm_device *dev,
+				void *data, struct drm_file *file_priv)
+{
+	char *label;
+	struct drm_dumb_set_label_object *args = data;
+	int ret = 0;
+
+	if (!args->len || !args->name)
+		return -EINVAL;
+
+	if (!dev->driver->label)
+		return -EOPNOTSUPP;
+
+	label = strndup_user(u64_to_user_ptr(args->name), args->len);
+
+	if (IS_ERR(label)) {
+		ret = PTR_ERR(label);
+		goto err;
+	}
+
+	ret = dev->driver->label(dev, file_priv, args->handle, label);
+
+err:
+	kfree(label);
+	return ret;
+}
+
+int drm_gem_set_label(struct drm_device *dev,
+		       struct drm_file *file_priv,
+			   uint32_t handle,
+			   const char *label)
+{
+	struct drm_gem_object *gem_obj;
+	int ret = 0;
+
+	gem_obj = drm_gem_object_lookup(file_priv, handle);
+	if (!gem_obj) {
+		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
+		ret = -ENOENT;
+		goto err;
+	}
+	drm_gem_adopt_label(gem_obj, label);
+
+err:
+	drm_gem_object_put_unlocked(gem_obj);
+	return ret;
+}
+EXPORT_SYMBOL(drm_gem_set_label);
+
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char *label)
+{
+	char *adopted_label = NULL;
+
+	if (label)
+		adopted_label = kstrdup(label, GFP_KERNEL);
+
+	if (gem_obj->label) {
+		kfree(gem_obj->label);
+		gem_obj->label = NULL;
+	}
+
+	gem_obj->label = adopted_label;
+}
+EXPORT_SYMBOL(drm_gem_adopt_label);
+
 /**
  * drm_gem_object_release - release GEM buffer object resources
  * @obj: GEM buffer object
@@ -958,6 +1023,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);
 
drivers/gpu/drm/drm_internal.h +8 -0
index 51a2055c8f18..cbc3f7b7fb9b 100644
@@ -137,6 +137,14 @@ 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_dumb_set_label_ioctl(struct drm_device *dev,
+			void *data,
+			struct drm_file *file_priv);
+int drm_gem_set_label(struct drm_device *dev,
+			struct drm_file *file_priv,
+			uint32_t handle,
+			const char *label);
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char *label);
 
 /* drm_debugfs.c drm_debugfs_crc.c */
 #if defined(CONFIG_DEBUG_FS)
drivers/gpu/drm/drm_ioctl.c +1 -0
index fcd728d7cf72..f34e1571d70f 100644
@@ -714,6 +714,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_DUMB_SET_LABEL, drm_dumb_set_label_ioctl, DRM_RENDER_ALLOW),
 };
 
 #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
include/drm/drm_drv.h +16 -0
index cf13470810a5..501a3924354a 100644
@@ -715,6 +715,22 @@ struct drm_driver {
 			    struct drm_device *dev,
 			    uint32_t handle);
 
+	/**
+	 * @label:
+	 *
+	 * This label's a buffer object.
+	 *
+	 * Called by the user via ioctl.
+	 *
+	 * Returns:
+	 *
+	 * Zero on success, negative errno on failure.
+	 */
+	int (*label)(struct drm_device *dev,
+				struct drm_file *file_priv,
+				uint32_t handle,
+				char *label);
+
 	/**
 	 * @gem_vm_ops: Driver private ops for this object
 	 *
include/drm/drm_gem.h +7 -0
index 6aaba14f5972..f801c054e972 100644
@@ -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:
 	 *
include/uapi/drm/drm.h +20 -0
index 8a5b2f8f8eb9..309c3c091385 100644
@@ -626,6 +626,25 @@ struct drm_gem_open {
 	__u64 size;
 };
 
+/** struct drm_dumb_set_label_object - ioctl argument for labelling BOs.
+ *
+ * This label's a BO with a userspace label
+ *
+ */
+struct drm_dumb_set_label_object {
+	/** Handle for the object being labelled. */
+	__u32 handle;
+
+	/** Label and label length.
+	 *  len includes the trailing NUL.
+	 */
+	__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 +965,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_DUMB_SET_LABEL      DRM_IOWR(0xCE, struct drm_dumb_set_label_object)
 
 /**
  * Device specific ioctls should only be in their respective headers
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#9 lore raw

Hey Daniel

Quoted 19 lines · On lunes, 14 de octubre de 2019 10:59:38 (CEST) Daniel Vetter wrote:
Quoted 18 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

Changes in v2:
- Hoist the IOCTL up into the drm_driver framework

Changes in v3:
- Introduce a drm_gem_set_label for drivers to use internally

in order to label a GEM object

- Hoist string copying up into the IOCTL
- Fix documentation
- Move actual gem labeling into drm_gem_adopt_label

Changes in v4:
- Refactor IOCTL call to only perform string duplication and move

Quoted 7 lines
Quoted 1 line

all gem lookup logic into GEM specific call

I still think we should just make this GEM-only and avoid the indirection. Except if your userspace actually does run on vmwgfx, and you absolutely want/need it to run there. Everything else is GEM-only. -Daniel

The plan for TTM drivers is to call drm_gem_adopt_label internally. So in practice, there really won't be too much TTM specific code.

This approach also future proof's us to be able to label any handles, not just GEM handle.

For the reasons above, I'd like to stick with my approach.

Cheers
Rohan Garg

Quoted 19 lines
Quoted 19 lines

---

 drivers/gpu/drm/drm_gem.c      | 70 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  8 ++++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 include/drm/drm_drv.h          | 16 ++++++++
 include/drm/drm_gem.h          |  7 ++++
 include/uapi/drm/drm.h         | 20 ++++++++++
 6 files changed, 122 insertions(+)
drivers/gpu/drm/drm_gem.c +0 -0
index 6854f5867d51..0fa4609b2817 100644
@@ -941,6 +941,71 @@ drm_gem_release(struct drm_device *dev, struct

drm_file *file_private)>
idr_destroy(&file_private->object_idr);

Quoted 4 lines

drivers/gpu/drm/drm_gem.c

Quoted 4 lines

}

drivers/gpu/drm/drm_gem.c +2 -0
+int drm_dumb_set_label_ioctl(struct drm_device *dev,
+				void *data, struct drm_file 

*file_priv)

Quoted 105 lines
Quoted 105 lines
drivers/gpu/drm/drm_gem.c +48 -0
+{
+	char *label;
+	struct drm_dumb_set_label_object *args = data;
+	int ret = 0;
+
+	if (!args->len || !args->name)
+		return -EINVAL;
+
+	if (!dev->driver->label)
+		return -EOPNOTSUPP;
+
+	label = strndup_user(u64_to_user_ptr(args->name), args->len);
+
+	if (IS_ERR(label)) {
+		ret = PTR_ERR(label);
+		goto err;
+	}
+
+	ret = dev->driver->label(dev, file_priv, args->handle, label);
+
+err:
+	kfree(label);
+	return ret;
+}
+
+int drm_gem_set_label(struct drm_device *dev,
+		       struct drm_file *file_priv,
+			   uint32_t handle,
+			   const char *label)
+{
+	struct drm_gem_object *gem_obj;
+	int ret = 0;
+
+	gem_obj = drm_gem_object_lookup(file_priv, handle);
+	if (!gem_obj) {
+		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
+		ret = -ENOENT;
+		goto err;
+	}
+	drm_gem_adopt_label(gem_obj, label);
+
+err:
+	drm_gem_object_put_unlocked(gem_obj);
+	return ret;
+}
+EXPORT_SYMBOL(drm_gem_set_label);
+
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char

*label) +{

drivers/gpu/drm/drm_gem.c +19 -0
+	char *adopted_label = NULL;
+
+	if (label)
+		adopted_label = kstrdup(label, GFP_KERNEL);
+
+	if (gem_obj->label) {
+		kfree(gem_obj->label);
+		gem_obj->label = NULL;
+	}
+
+	gem_obj->label = adopted_label;
+}
+EXPORT_SYMBOL(drm_gem_adopt_label);
+
 
 /**
 
  * drm_gem_object_release - release GEM buffer object resources
  * @obj: GEM buffer object
@@ -958,6 +1023,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);

(quoted diff) +0 -0

b/drivers/gpu/drm/drm_internal.h index 51a2055c8f18..cbc3f7b7fb9b 100644

drivers/gpu/drm/drm_internal.h +8 -0
@@ -137,6 +137,14 @@ 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_dumb_set_label_ioctl(struct drm_device *dev,
+			void *data,
+			struct drm_file *file_priv);
+int drm_gem_set_label(struct drm_device *dev,
+			struct drm_file *file_priv,
+			uint32_t handle,
+			const char *label);
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char
 *label);> 

/* drm_debugfs.c drm_debugfs_crc.c */
#if defined(CONFIG_DEBUG_FS)

Quoted 7 lines

drivers/gpu/drm/drm_ioctl.c static const struct drm_ioctl_desc drm_ioctls[] = { · line 715

Quoted 7 lines
drivers/gpu/drm/drm_ioctl.c +0 -0
index fcd728d7cf72..f34e1571d70f 100644
@@ -714,6 +714,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
 
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, 

drm_mode_list_lessees_ioctl,

Quoted 3 lines

drivers/gpu/drm/drm_ioctl.c static const struct drm_ioctl_desc drm_ioctls[] = { · line 718

Quoted 3 lines
drivers/gpu/drm/drm_ioctl.c +0 -0
 	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,

Quoted 106 lines
Quoted 106 lines
drivers/gpu/drm/drm_ioctl.c +1 -0
 	DRM_MASTER),> 
+	DRM_IOCTL_DEF(DRM_IOCTL_DUMB_SET_LABEL, drm_dumb_set_label_ioctl,

DRM_RENDER_ALLOW),>
};

#define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )

include/drm/drm_drv.h +16 -0
index cf13470810a5..501a3924354a 100644
@@ -715,6 +715,22 @@ struct drm_driver {
 
 			    struct drm_device *dev,
 			    uint32_t handle);
 
+	/**
+	 * @label:
+	 *
+	 * This label's a buffer object.
+	 *
+	 * Called by the user via ioctl.
+	 *
+	 * Returns:
+	 *
+	 * Zero on success, negative errno on failure.
+	 */
+	int (*label)(struct drm_device *dev,
+				struct drm_file *file_priv,
+				uint32_t handle,
+				char *label);
+
 
 	/**
include/drm/drm_drv.h +0 -0
 	 * @gem_vm_ops: Driver private ops for this object
 	 *
include/drm/drm_gem.h +7 -0
index 6aaba14f5972..f801c054e972 100644
@@ -237,6 +237,13 @@ struct drm_gem_object {
 
 	 */
 
 	int name;
 
+	/**
+	 * @label:
+	 *
+	 * Label for this object, should be a human readable string.
+	 */
+	char *label;
+
 
include/drm/drm_gem.h +0 -0
 	/**
 
 	 * @dma_buf:
 	 *
include/uapi/drm/drm.h +19 -0
index 8a5b2f8f8eb9..309c3c091385 100644
@@ -626,6 +626,25 @@ struct drm_gem_open {
 
 	__u64 size;
 
 };
 
+/** struct drm_dumb_set_label_object - ioctl argument for labelling BOs.
+ *
+ * This label's a BO with a userspace label
+ *
+ */
+struct drm_dumb_set_label_object {
+	/** Handle for the object being labelled. */
+	__u32 handle;
+
+	/** Label and label length.
+	 *  len includes the trailing NUL.
+	 */
+	__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

include/uapi/drm/drm.h +1 -0
@@ -946,6 +965,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_DUMB_SET_LABEL      DRM_IOWR(0xCE, struct

drm_dumb_set_label_object)>
/**

Quoted 1 line
Quoted 1 line

* Device specific ioctls should only be in their respective headers

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw
Quoted 29 lines · Rohan Garg

Hey Daniel

Quoted 19 lines · On lunes, 14 de octubre de 2019 10:59:38 (CEST) Daniel Vetter wrote:
Quoted 18 lines · Rohan Garg

DRM_IOCTL_DUMB_SET_LABEL lets you label GEM objects, making it easier to debug issues in userspace applications.

Changes in v2:
- Hoist the IOCTL up into the drm_driver framework

Changes in v3:
- Introduce a drm_gem_set_label for drivers to use internally

in order to label a GEM object

- Hoist string copying up into the IOCTL
- Fix documentation
- Move actual gem labeling into drm_gem_adopt_label

Changes in v4:
- Refactor IOCTL call to only perform string duplication and move

Quoted 7 lines
Quoted 1 line

all gem lookup logic into GEM specific call

I still think we should just make this GEM-only and avoid the indirection. Except if your userspace actually does run on vmwgfx, and you absolutely want/need it to run there. Everything else is GEM-only. -Daniel

Quoted 5 lines

The plan for TTM drivers is to call drm_gem_adopt_label internally. So in practice, there really won't be too much TTM specific code.

This approach also future proof's us to be able to label any handles, not just GEM handle.

I don't expect we'll ever merge any non-gem drivers in the future anymore. Hence this really only makes sense if vmwgfx wants it, that's the only use-case for this generic-ism here. If vmwgfx doesn't have a use or jump on board with this, imo better to just make this specific to gem and be done.
-Daniel

Quoted 260 lines

For the reasons above, I'd like to stick with my approach.

Cheers
Rohan Garg

Quoted 19 lines
Quoted 19 lines

---

 drivers/gpu/drm/drm_gem.c      | 70 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  8 ++++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 include/drm/drm_drv.h          | 16 ++++++++
 include/drm/drm_gem.h          |  7 ++++
 include/uapi/drm/drm.h         | 20 ++++++++++
 6 files changed, 122 insertions(+)
drivers/gpu/drm/drm_gem.c +0 -0
index 6854f5867d51..0fa4609b2817 100644
@@ -941,6 +941,71 @@ drm_gem_release(struct drm_device *dev, struct

drm_file *file_private)>
idr_destroy(&file_private->object_idr);

Quoted 4 lines

drivers/gpu/drm/drm_gem.c

Quoted 4 lines

}

drivers/gpu/drm/drm_gem.c +2 -0
+int drm_dumb_set_label_ioctl(struct drm_device *dev,
+				void *data, struct drm_file 

*file_priv)

Quoted 105 lines
Quoted 105 lines
drivers/gpu/drm/drm_gem.c +48 -0
+{
+	char *label;
+	struct drm_dumb_set_label_object *args = data;
+	int ret = 0;
+
+	if (!args->len || !args->name)
+		return -EINVAL;
+
+	if (!dev->driver->label)
+		return -EOPNOTSUPP;
+
+	label = strndup_user(u64_to_user_ptr(args->name), args->len);
+
+	if (IS_ERR(label)) {
+		ret = PTR_ERR(label);
+		goto err;
+	}
+
+	ret = dev->driver->label(dev, file_priv, args->handle, label);
+
+err:
+	kfree(label);
+	return ret;
+}
+
+int drm_gem_set_label(struct drm_device *dev,
+		       struct drm_file *file_priv,
+			   uint32_t handle,
+			   const char *label)
+{
+	struct drm_gem_object *gem_obj;
+	int ret = 0;
+
+	gem_obj = drm_gem_object_lookup(file_priv, handle);
+	if (!gem_obj) {
+		DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
+		ret = -ENOENT;
+		goto err;
+	}
+	drm_gem_adopt_label(gem_obj, label);
+
+err:
+	drm_gem_object_put_unlocked(gem_obj);
+	return ret;
+}
+EXPORT_SYMBOL(drm_gem_set_label);
+
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char

*label) +{

drivers/gpu/drm/drm_gem.c +19 -0
+	char *adopted_label = NULL;
+
+	if (label)
+		adopted_label = kstrdup(label, GFP_KERNEL);
+
+	if (gem_obj->label) {
+		kfree(gem_obj->label);
+		gem_obj->label = NULL;
+	}
+
+	gem_obj->label = adopted_label;
+}
+EXPORT_SYMBOL(drm_gem_adopt_label);
+
 
 /**
 
  * drm_gem_object_release - release GEM buffer object resources
  * @obj: GEM buffer object
@@ -958,6 +1023,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);

(quoted diff) +0 -0

b/drivers/gpu/drm/drm_internal.h index 51a2055c8f18..cbc3f7b7fb9b 100644

drivers/gpu/drm/drm_internal.h +8 -0
@@ -137,6 +137,14 @@ 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_dumb_set_label_ioctl(struct drm_device *dev,
+			void *data,
+			struct drm_file *file_priv);
+int drm_gem_set_label(struct drm_device *dev,
+			struct drm_file *file_priv,
+			uint32_t handle,
+			const char *label);
+void drm_gem_adopt_label(struct drm_gem_object *gem_obj, const char
 *label);> 

/* drm_debugfs.c drm_debugfs_crc.c */
#if defined(CONFIG_DEBUG_FS)

Quoted 7 lines

drivers/gpu/drm/drm_ioctl.c static const struct drm_ioctl_desc drm_ioctls[] = { · line 715

Quoted 7 lines
drivers/gpu/drm/drm_ioctl.c +0 -0
index fcd728d7cf72..f34e1571d70f 100644
@@ -714,6 +714,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
 
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, 

drm_mode_list_lessees_ioctl,

Quoted 3 lines

drivers/gpu/drm/drm_ioctl.c static const struct drm_ioctl_desc drm_ioctls[] = { · line 718

Quoted 3 lines
drivers/gpu/drm/drm_ioctl.c +0 -0
 	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,

Quoted 106 lines
Quoted 106 lines
drivers/gpu/drm/drm_ioctl.c +1 -0
 	DRM_MASTER),> 
+	DRM_IOCTL_DEF(DRM_IOCTL_DUMB_SET_LABEL, drm_dumb_set_label_ioctl,

DRM_RENDER_ALLOW),>
};

#define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )

include/drm/drm_drv.h +16 -0
index cf13470810a5..501a3924354a 100644
@@ -715,6 +715,22 @@ struct drm_driver {
 
 			    struct drm_device *dev,
 			    uint32_t handle);
 
+	/**
+	 * @label:
+	 *
+	 * This label's a buffer object.
+	 *
+	 * Called by the user via ioctl.
+	 *
+	 * Returns:
+	 *
+	 * Zero on success, negative errno on failure.
+	 */
+	int (*label)(struct drm_device *dev,
+				struct drm_file *file_priv,
+				uint32_t handle,
+				char *label);
+
 
 	/**
include/drm/drm_drv.h +0 -0
 	 * @gem_vm_ops: Driver private ops for this object
 	 *
include/drm/drm_gem.h +7 -0
index 6aaba14f5972..f801c054e972 100644
@@ -237,6 +237,13 @@ struct drm_gem_object {
 
 	 */
 
 	int name;
 
+	/**
+	 * @label:
+	 *
+	 * Label for this object, should be a human readable string.
+	 */
+	char *label;
+
 
include/drm/drm_gem.h +0 -0
 	/**
 
 	 * @dma_buf:
 	 *
include/uapi/drm/drm.h +19 -0
index 8a5b2f8f8eb9..309c3c091385 100644
@@ -626,6 +626,25 @@ struct drm_gem_open {
 
 	__u64 size;
 
 };
 
+/** struct drm_dumb_set_label_object - ioctl argument for labelling BOs.
+ *
+ * This label's a BO with a userspace label
+ *
+ */
+struct drm_dumb_set_label_object {
+	/** Handle for the object being labelled. */
+	__u32 handle;
+
+	/** Label and label length.
+	 *  len includes the trailing NUL.
+	 */
+	__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

include/uapi/drm/drm.h +1 -0
@@ -946,6 +965,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_DUMB_SET_LABEL      DRM_IOWR(0xCE, struct

drm_dumb_set_label_object)>
/**

Quoted 1 line
Quoted 1 line

* Device specific ioctls should only be in their respective headers

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#11 lore raw

Hey,

Quoted 9 lines · Daniel Vetter
Quoted 2 lines · Rohan Garg

This approach also future proof's us to be able to label any handles, not just GEM handle.

I don't expect we'll ever merge any non-gem drivers in the future anymore. Hence this really only makes sense if vmwgfx wants it, that's the only use-case for this generic-ism here. If vmwgfx doesn't have a use or jump on board with this, imo better to just make this specific to gem and be done.

VMware were the exact people who asked it for to be handle-agnostic and not GEM-specific ...

Given that we already have handle-agnostic calls like FBs in particular, I think it makes sense to have this one just follow that. It's not much code and doesn't really imply any heavy change for the rest of DRM.

Cheers,
Daniel
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel