Skip to contentSkip to conversation

DRM_SET_NAME ioctl

PATCH Superseded Pierre-Eric Pelloux-Prayer 6 participants 23 messages Sep 20, 2024 – Sep 24, 2024

Subsystems (5)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover DRM_SET_NAME ioctl

v3 changelog:
* Added DRM_NAME_MAX_LEN instead of using NAME_MAX
* Fixed Tvrtko & Christian comments on patch 1
* Added R-b tags to patch 2
* Following Christian' suggestion patch 3 became 4 patches:
- amdgpu_task_info::process_name is now a flexible array
- amdgpu_vm::task_info is allocated on first submit
- amdgpu_task_info::process_name was renamed process_desc, and includes drm_file::name when set. Like task_info allocation this is done only once.

Pierre-Eric Pelloux-Prayer (6):
drm: add DRM_SET_NAME ioctl
drm: use drm_file name in fdinfo
drm/amdgpu: delay the use of amdgpu_vm_set_task_info drm/amdgpu: alloc and init vm::task_info from first submit drm/amdgpu: make process_name a flexible array drm/amdgpu: use drm_file::name in task_info::process_desc

 Documentation/gpu/drm-usage-stats.rst         |  5 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  1 +
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  6 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c   |  2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c  |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c       |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 48 +++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  4 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c        |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c        |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c        |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v8_0.c         |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |  2 +-
 drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c        |  2 +-
 drivers/gpu/drm/amd/amdgpu/sdma_v4_4_2.c      |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_events.c       |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  3 ++
 drivers/gpu/drm/drm_debugfs.c                 | 12 +++--
 drivers/gpu/drm/drm_file.c                    | 10 ++++
 drivers/gpu/drm/drm_ioctl.c                   | 48 +++++++++++++++++++
 include/drm/drm_file.h                        |  9 ++++
 include/uapi/drm/drm.h                        | 17 +++++++
 23 files changed, 159 insertions(+), 31 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v3 1/6 drm: add DRM_SET_NAME ioctl

Giving the opportunity to userspace to associate a free-form name with a drm_file struct is helpful for tracking and debugging.

This is similar to the existing DMA_BUF_SET_NAME ioctl.

Access to name is protected by a mutex, and the 'clients' debugfs file has been updated to print it.

Userspace MR to use this ioctl:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

The string passed by userspace is filtered a bit, to avoid messing output when it's going to be printed (in dmesg, fdinfo, etc):
* all chars failing isgraph() are replaced by '-'
* if a 0-length string is passed the name is cleared

---
 drivers/gpu/drm/drm_debugfs.c | 12 ++++++---
 drivers/gpu/drm/drm_file.c    |  5 ++++
 drivers/gpu/drm/drm_ioctl.c   | 48 +++++++++++++++++++++++++++++++++++
 include/drm/drm_file.h        |  9 +++++++
 include/uapi/drm/drm.h        | 17 +++++++++++++
 5 files changed, 87 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +8 -4
index 6b239a24f1df..482e71160544 100644
@@ -78,12 +78,13 @@ static int drm_clients_info(struct seq_file *m, void *data)
 	kuid_t uid;
 
 	seq_printf(m,
-		   "%20s %5s %3s master a %5s %10s\n",
+		   "%20s %5s %3s master a %5s %10s %20s\n",
 		   "command",
 		   "tgid",
 		   "dev",
 		   "uid",
-		   "magic");
+		   "magic",
+		   "name");
 
 	/* dev->filelist is sorted youngest first, but we want to present
 	 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
@@ -94,19 +95,22 @@ static int drm_clients_info(struct seq_file *m, void *data)
 		struct task_struct *task;
 		struct pid *pid;
 
+		mutex_lock(&priv->name_lock);
 		rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */
 		pid = rcu_dereference(priv->pid);
 		task = pid_task(pid, PIDTYPE_TGID);
 		uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
-		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n",
+		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %20s\n",
 			   task ? task->comm : "<unknown>",
 			   pid_vnr(pid),
 			   priv->minor->index,
 			   is_current_master ? 'y' : 'n',
 			   priv->authenticated ? 'y' : 'n',
 			   from_kuid_munged(seq_user_ns(m), uid),
-			   priv->magic);
+			   priv->magic,
+			   priv->name ?: "");
 		rcu_read_unlock();
+		mutex_unlock(&priv->name_lock);
 	}
 	mutex_unlock(&dev->filelist_mutex);
 	return 0;
drivers/gpu/drm/drm_file.c +5 -0
index 01fde94fe2a9..e9dd0e90a1f9 100644
@@ -158,6 +158,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
 
 	spin_lock_init(&file->master_lookup_lock);
 	mutex_init(&file->event_read_lock);
+	mutex_init(&file->name_lock);
 
 	if (drm_core_check_feature(dev, DRIVER_GEM))
 		drm_gem_open(dev, file);
@@ -259,6 +260,10 @@ void drm_file_free(struct drm_file *file)
 	WARN_ON(!list_empty(&file->event_list));
 
 	put_pid(rcu_access_pointer(file->pid));
+
+	mutex_destroy(&file->name_lock);
+	kfree(file->name);
+
 	kfree(file);
 }
 
drivers/gpu/drm/drm_ioctl.c +48 -0

60 lines not on this page · raw message

include/drm/drm_file.h +9 -0
index 8c0030c77308..df26eee8f79c 100644
@@ -388,6 +388,15 @@ struct drm_file {
 	 * Per-file buffer caches used by the PRIME buffer sharing code.
 	 */
 	struct drm_prime_file_private prime;
+
+	/**
+	 * @name:
+	 *
+	 * Userspace-provided name; useful for accounting and debugging.
+	 */
+	const char *name;
+	/** @name_lock: Protects @name. */
+	struct mutex name_lock;
 };
 
 /**
include/uapi/drm/drm.h +17 -0
index 16122819edfe..f5e92e4f909b 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {
 	__u64 user_data;	/* user data passed to event */
 };
 
+#define DRM_NAME_MAX_LEN	64
+struct drm_set_name {
+	__u64 name_len;
+	__u64 name;
+};
+
+
 #if defined(__cplusplus)
 }
 #endif
@@ -1288,6 +1295,16 @@ extern "C" {
  */
 #define DRM_IOCTL_MODE_CLOSEFB		DRM_IOWR(0xD0, struct drm_mode_closefb)
 
+/**
+ * DRM_IOCTL_SET_NAME - Attach a name to a drm_file
+ *
+ * This ioctl is similar to DMA_BUF_SET_NAME - it allows for easier tracking
+ * and debugging.
+ * The length of the name must <= DRM_NAME_MAX_LEN. All characters that are
+ * non-printable or whitespaces will be replaced by -.
+ */
+#define DRM_IOCTL_SET_NAME		DRM_IOWR(0xD1, struct drm_set_name)
+
 /*
  * Device specific ioctls should only be in their respective headers
  * The device specific ioctl range is from 0x40 to 0x9f.
Signature · 1 line
2.40.1
Reviewed-by on v3 1/6
#2 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

Quoted 30 lines · Pierre-Eric Pelloux-Prayer

Giving the opportunity to userspace to associate a free-form name with a drm_file struct is helpful for tracking and debugging.

This is similar to the existing DMA_BUF_SET_NAME ioctl.

Access to name is protected by a mutex, and the 'clients' debugfs file has been updated to print it.

Userspace MR to use this ioctl:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

The string passed by userspace is filtered a bit, to avoid messing output when it's going to be printed (in dmesg, fdinfo, etc):
* all chars failing isgraph() are replaced by '-'
* if a 0-length string is passed the name is cleared

---
  drivers/gpu/drm/drm_debugfs.c | 12 ++++++---
  drivers/gpu/drm/drm_file.c    |  5 ++++
  drivers/gpu/drm/drm_ioctl.c   | 48 +++++++++++++++++++++++++++++++++++
  include/drm/drm_file.h        |  9 +++++++
  include/uapi/drm/drm.h        | 17 +++++++++++++
  5 files changed, 87 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +0 -0
index 6b239a24f1df..482e71160544 100644

drivers/gpu/drm/drm_debugfs.c static int drm_clients_info(struct seq_file *m, void *data) · line 81

Quoted 5 lines
drivers/gpu/drm/drm_debugfs.c +1 -1
@@ -78,12 +78,13 @@ static int drm_clients_info(struct seq_file *m, void *data)
  	kuid_t uid;
 
  	seq_printf(m,
-		   "%20s %5s %3s master a %5s %10s\n",
+		   "%20s %5s %3s master a %5s %10s %20s\n",

Allow full DRM_NAME_MAX_LEN? Not sure, feels not very consequential either way.

Quoted 166 lines
drivers/gpu/drm/drm_debugfs.c +7 -3
  		   "command",
  		   "tgid",
  		   "dev",
  		   "uid",
-		   "magic");
+		   "magic",
+		   "name");
 
  	/* dev->filelist is sorted youngest first, but we want to present
  	 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
@@ -94,19 +95,22 @@ static int drm_clients_info(struct seq_file *m, void *data)
  		struct task_struct *task;
  		struct pid *pid;
 
+		mutex_lock(&priv->name_lock);
  		rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */
  		pid = rcu_dereference(priv->pid);
  		task = pid_task(pid, PIDTYPE_TGID);
  		uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
-		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n",
+		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %20s\n",
  			   task ? task->comm : "<unknown>",
  			   pid_vnr(pid),
  			   priv->minor->index,
  			   is_current_master ? 'y' : 'n',
  			   priv->authenticated ? 'y' : 'n',
  			   from_kuid_munged(seq_user_ns(m), uid),
-			   priv->magic);
+			   priv->magic,
+			   priv->name ?: "");
  		rcu_read_unlock();
+		mutex_unlock(&priv->name_lock);
  	}
  	mutex_unlock(&dev->filelist_mutex);
  	return 0;
drivers/gpu/drm/drm_file.c +5 -0
index 01fde94fe2a9..e9dd0e90a1f9 100644
@@ -158,6 +158,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
 
  	spin_lock_init(&file->master_lookup_lock);
  	mutex_init(&file->event_read_lock);
+	mutex_init(&file->name_lock);
 
  	if (drm_core_check_feature(dev, DRIVER_GEM))
  		drm_gem_open(dev, file);
@@ -259,6 +260,10 @@ void drm_file_free(struct drm_file *file)
  	WARN_ON(!list_empty(&file->event_list));
 
  	put_pid(rcu_access_pointer(file->pid));
+
+	mutex_destroy(&file->name_lock);
+	kfree(file->name);
+
  	kfree(file);
  }
 
drivers/gpu/drm/drm_ioctl.c +48 -0
index 51f39912866f..46dadbd1bb35 100644
@@ -540,6 +540,52 @@ int drm_version(struct drm_device *dev, void *data,
  	return err;
  }
 
+static int drm_set_name(struct drm_device *dev, void *data,
+			struct drm_file *file_priv)
+{
+	struct drm_set_name *name = data;
+	void __user *user_ptr;
+	char *new_name;
+	size_t i, len;
+
+	if (name->name_len > DRM_NAME_MAX_LEN)
+		return -EINVAL;
+
+	user_ptr = u64_to_user_ptr(name->name);
+
+	new_name = memdup_user_nul(user_ptr, name->name_len);
+	if (IS_ERR(new_name))
+		return PTR_ERR(new_name);
+
+	len = strlen(new_name);
+
+	if (len != name->name_len) {
+		kfree(new_name);
+		return -EINVAL;
+	}
+
+	/*
+	 * Filter out control char / spaces / new lines etc in the name
+	 * since it's going to be used in dmesg or fdinfo's output.
+	 */
+	for (i = 0; i < len; i++) {
+		if (!isgraph(new_name[i]))
+			new_name[i] = '-';
+	}
+
+	mutex_lock(&file_priv->name_lock);
+	kfree(file_priv->name);
+	if (len > 0) {
+		file_priv->name = new_name;
+	} else {
+		kfree(new_name);
+		file_priv->name = NULL;
+	}
+	mutex_unlock(&file_priv->name_lock);
+
+	return 0;
+}
+
  static int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
  {
  	/* ROOT_ONLY is only for CAP_SYS_ADMIN */
@@ -610,6 +656,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
  	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
 
+	DRM_IOCTL_DEF(DRM_IOCTL_SET_NAME, drm_set_name, DRM_RENDER_ALLOW),
+
  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
include/drm/drm_file.h +9 -0
index 8c0030c77308..df26eee8f79c 100644
@@ -388,6 +388,15 @@ struct drm_file {
  	 * Per-file buffer caches used by the PRIME buffer sharing code.
  	 */
  	struct drm_prime_file_private prime;
+
+	/**
+	 * @name:
+	 *
+	 * Userspace-provided name; useful for accounting and debugging.
+	 */
+	const char *name;
+	/** @name_lock: Protects @name. */
+	struct mutex name_lock;
  };
 
  /**
include/uapi/drm/drm.h +7 -0
index 16122819edfe..f5e92e4f909b 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {
  	__u64 user_data;	/* user data passed to event */
  };
 
+#define DRM_NAME_MAX_LEN	64
+struct drm_set_name {
+	__u64 name_len;
+	__u64 name;
+};
+
+
  #if defined(__cplusplus)
  }
  #endif
@@ -1288,6 +1295,16 @@ extern "C" {
   */
  #define DRM_IOCTL_MODE_CLOSEFB		DRM_IOWR(0xD0, struct drm_mode_closefb)
 

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

Quoted 10 lines
include/uapi/drm/drm.h +10 -0
+/**
+ * DRM_IOCTL_SET_NAME - Attach a name to a drm_file
+ *
+ * This ioctl is similar to DMA_BUF_SET_NAME - it allows for easier tracking
+ * and debugging.
+ * The length of the name must <= DRM_NAME_MAX_LEN. All characters that are
+ * non-printable or whitespaces will be replaced by -.
+ */
+#define DRM_IOCTL_SET_NAME		DRM_IOWR(0xD1, struct drm_set_name)
+

A comment, nice! :) Overal looks good to me.

I do however wish for more opinions (before merging) on whether strings with invalid characters should perhaps instead be rejected. I don't currently have a solid argument either way.

Perhaps the only argument against silent transformation is if someone sets some wild string, then greps for it somewhere, which would be a false negative without the understanding of what kind of remapping kernel does. It is weak but it is uapi so worth discussing every crazy possibility I think.

On the other hand it would create another annoying source of EINVAL. :shrug:

Also, how are with with testing the DRM core features? Add something for the uapi in IGT/tests/drm_client_name, or some such?

Regards,

Tvrtko

Quoted 3 lines
include/uapi/drm/drm.h +0 -0
  /*
   * Device specific ioctls should only be in their respective headers
   * The device specific ioctl range is from 0x40 to 0x9f.
#3 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

Le 23/09/2024 à 12:06, Tvrtko Ursulin a écrit :

Quoted 27 lines · Pierre-Eric Pelloux-Prayer
Quoted 25 lines

Giving the opportunity to userspace to associate a free-form name with a drm_file struct is helpful for tracking and debugging.

This is similar to the existing DMA_BUF_SET_NAME ioctl.

Access to name is protected by a mutex, and the 'clients' debugfs file has been updated to print it.

Userspace MR to use this ioctl:
    https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

The string passed by userspace is filtered a bit, to avoid messing output when it's going to be printed (in dmesg, fdinfo, etc):
   * all chars failing isgraph() are replaced by '-'
   * if a 0-length string is passed the name is cleared

---
  drivers/gpu/drm/drm_debugfs.c | 12 ++++++--- drivers/gpu/drm/drm_file.c    |  5 ++++ drivers/gpu/drm/drm_ioctl.c   | 48 +++++++++++++++++++++++++++++++++++ include/drm/drm_file.h        |  9 +++++++ include/uapi/drm/drm.h        | 17 +++++++++++++

  5 files changed, 87 insertions(+), 4 deletions(-)
Quoted 11 lines

drivers/gpu/drm/drm_debugfs.c static int drm_clients_info(struct seq_file *m, void *data) · line 80

Quoted 9 lines
drivers/gpu/drm/drm_debugfs.c +1 -1
index 6b239a24f1df..482e71160544 100644
@@ -78,12 +78,13 @@ static int drm_clients_info(struct seq_file *m, void *data)
       kuid_t uid;
       seq_printf(m,
-           "%20s %5s %3s master a %5s %10s\n",
+           "%20s %5s %3s master a %5s %10s %20s\n",

Allow full DRM_NAME_MAX_LEN? Not sure, feels not very consequential either way.

I'll switch to:

        seq_printf(m,
                   "%20s %5s %3s master a %5s %10s %*s\n",
                   "command",
                   "tgid",
                   "dev",
                   "uid",
                   "magic",
                   DRM_CLIENT_NAME_MAX_LEN,
                   "name");

And:

                seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %*s\n",
                           task ? task->comm : "<unknown>",
                           pid_vnr(pid),
                           priv->minor->index,
                           is_current_master ? 'y' : 'n',
                           priv->authenticated ? 'y' : 'n',
                           from_kuid_munged(seq_user_ns(m), uid),
                           priv->magic,
                           DRM_CLIENT_NAME_MAX_LEN,
                           priv->client_name ? priv->client_name : "<unset>");
Quoted 169 lines
Quoted 156 lines
drivers/gpu/drm/drm_debugfs.c +3 -1
              "command",
              "tgid",
              "dev",
              "uid",
-           "magic");
+           "magic",
+           "name");
       /* dev->filelist is sorted youngest first, but we want to present
        * oldest first (i.e. kernel, servers, clients), so walk backwardss.
@@ -94,19 +95,22 @@ static int drm_clients_info(struct seq_file *m, void *data)
           struct task_struct *task;
           struct pid *pid;
+        mutex_lock(&priv->name_lock);

          rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */ pid = rcu_dereference(priv->pid);
          task = pid_task(pid, PIDTYPE_TGID);
          uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID; -        seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n", +        seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %20s\n", task ? task->comm : "<unknown>", pid_vnr(pid),
                 priv->minor->index,
                 is_current_master ? 'y' : 'n', priv->authenticated ? 'y' : 'n', from_kuid_munged(seq_user_ns(m), uid),
-               priv->magic);
+               priv->magic,
+               priv->name ?: "");
          rcu_read_unlock();
+        mutex_unlock(&priv->name_lock);
      }
      mutex_unlock(&dev->filelist_mutex);
      return 0;

drivers/gpu/drm/drm_file.c +5 -0
index 01fde94fe2a9..e9dd0e90a1f9 100644
@@ -158,6 +158,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
       spin_lock_init(&file->master_lookup_lock);
       mutex_init(&file->event_read_lock);
+    mutex_init(&file->name_lock);
       if (drm_core_check_feature(dev, DRIVER_GEM))
           drm_gem_open(dev, file);
@@ -259,6 +260,10 @@ void drm_file_free(struct drm_file *file)
       WARN_ON(!list_empty(&file->event_list));
       put_pid(rcu_access_pointer(file->pid));
+
+    mutex_destroy(&file->name_lock);
+    kfree(file->name);
+
       kfree(file);
   }
drivers/gpu/drm/drm_ioctl.c +48 -0
index 51f39912866f..46dadbd1bb35 100644
@@ -540,6 +540,52 @@ int drm_version(struct drm_device *dev, void *data,
       return err;
   }
+static int drm_set_name(struct drm_device *dev, void *data,
+            struct drm_file *file_priv)
+{
+    struct drm_set_name *name = data;
+    void __user *user_ptr;
+    char *new_name;
+    size_t i, len;
+
+    if (name->name_len > DRM_NAME_MAX_LEN)
+        return -EINVAL;
+
+    user_ptr = u64_to_user_ptr(name->name);
+
+    new_name = memdup_user_nul(user_ptr, name->name_len);
+    if (IS_ERR(new_name))
+        return PTR_ERR(new_name);
+
+    len = strlen(new_name);
+
+    if (len != name->name_len) {
+        kfree(new_name);
+        return -EINVAL;
+    }
+
+    /*
+     * Filter out control char / spaces / new lines etc in the name
+     * since it's going to be used in dmesg or fdinfo's output.
+     */
+    for (i = 0; i < len; i++) {
+        if (!isgraph(new_name[i]))
+            new_name[i] = '-';
+    }
+
+    mutex_lock(&file_priv->name_lock);
+    kfree(file_priv->name);
+    if (len > 0) {
+        file_priv->name = new_name;
+    } else {
+        kfree(new_name);
+        file_priv->name = NULL;
+    }
+    mutex_unlock(&file_priv->name_lock);
+
+    return 0;
+}
+
   static int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
   {
       /* ROOT_ONLY is only for CAP_SYS_ADMIN */
@@ -610,6 +656,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
       DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
       DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
+    DRM_IOCTL_DEF(DRM_IOCTL_SET_NAME, drm_set_name, DRM_RENDER_ALLOW),
+
       DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
       DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
       DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
include/drm/drm_file.h +9 -0
index 8c0030c77308..df26eee8f79c 100644
@@ -388,6 +388,15 @@ struct drm_file {
        * Per-file buffer caches used by the PRIME buffer sharing code.
        */
       struct drm_prime_file_private prime;
+
+    /**
+     * @name:
+     *
+     * Userspace-provided name; useful for accounting and debugging.
+     */
+    const char *name;
+    /** @name_lock: Protects @name. */
+    struct mutex name_lock;
   };
   /**
include/uapi/drm/drm.h +0 -0
index 16122819edfe..f5e92e4f909b 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {

      __u64 user_data;    /* user data passed to event */ };

include/uapi/drm/drm.h +7 -0
+#define DRM_NAME_MAX_LEN    64
+struct drm_set_name {
+    __u64 name_len;
+    __u64 name;
+};
+
+

  #if defined(__cplusplus)
  }
  #endif

include/uapi/drm/drm.h +0 -0
@@ -1288,6 +1295,16 @@ extern "C" {
    */
   #define DRM_IOCTL_MODE_CLOSEFB        DRM_IOWR(0xD0, struct drm_mode_closefb)

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

Quoted 10 lines
include/uapi/drm/drm.h +10 -0
+/**
+ * DRM_IOCTL_SET_NAME - Attach a name to a drm_file
+ *
+ * This ioctl is similar to DMA_BUF_SET_NAME - it allows for easier tracking
+ * and debugging.
+ * The length of the name must <= DRM_NAME_MAX_LEN. All characters that are
+ * non-printable or whitespaces will be replaced by -.
+ */
+#define DRM_IOCTL_SET_NAME        DRM_IOWR(0xD1, struct drm_set_name)
+

A comment, nice! :) Overal looks good to me.

Quoted 7 lines

I do however wish for more opinions (before merging) on whether strings with invalid characters should perhaps instead be rejected. I don't currently have a solid argument either way.

Perhaps the only argument against silent transformation is if someone sets some wild string, then greps for it somewhere, which would be a false negative without the understanding of what kind of remapping kernel does. It is weak but it is uapi so worth discussing every crazy possibility I think.

On the other hand it would create another annoying source of EINVAL. :shrug:

No strong feelings either, but I agree that gathering more opinions before merging would be useful.

Quoted 2 lines

Also, how are with with testing the DRM core features? Add something for the uapi in IGT/tests/drm_client_name, or some such?

Something like using the ioctl and then reading back the name using /sys/kernel/debug/dri/0/clients? I can do that if it's required / useful.

Thanks,
Pierre-Eric

Quoted 8 lines

Regards,

Tvrtko

Quoted 3 lines
include/uapi/drm/drm.h +0 -0
   /*
    * Device specific ioctls should only be in their respective headers
    * The device specific ioctl range is from 0x40 to 0x9f.
#4 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

Quoted 46 lines · Pierre-Eric Pelloux-Prayer

Le 23/09/2024 à 12:06, Tvrtko Ursulin a écrit :

Quoted 28 lines · Pierre-Eric Pelloux-Prayer
Quoted 26 lines

Giving the opportunity to userspace to associate a free-form name with a drm_file struct is helpful for tracking and debugging.

This is similar to the existing DMA_BUF_SET_NAME ioctl.

Access to name is protected by a mutex, and the 'clients' debugfs file has been updated to print it.

Userspace MR to use this ioctl:

https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

The string passed by userspace is filtered a bit, to avoid messing output when it's going to be printed (in dmesg, fdinfo, etc):
   * all chars failing isgraph() are replaced by '-'
   * if a 0-length string is passed the name is cleared

---
  drivers/gpu/drm/drm_debugfs.c | 12 ++++++--- drivers/gpu/drm/drm_file.c    |  5 ++++ drivers/gpu/drm/drm_ioctl.c   | 48 +++++++++++++++++++++++++++++++++++ include/drm/drm_file.h        |  9 +++++++ include/uapi/drm/drm.h        | 17 +++++++++++++

  5 files changed, 87 insertions(+), 4 deletions(-)
Quoted 12 lines

drivers/gpu/drm/drm_debugfs.c static int drm_clients_info(struct seq_file *m, · line 81

Quoted 10 lines
drivers/gpu/drm/drm_debugfs.c +1 -1
index 6b239a24f1df..482e71160544 100644
@@ -78,12 +78,13 @@ static int drm_clients_info(struct seq_file *m, 
 void *data)
       kuid_t uid;
       seq_printf(m,
-           "%20s %5s %3s master a %5s %10s\n",
+           "%20s %5s %3s master a %5s %10s %20s\n",

Allow full DRM_NAME_MAX_LEN? Not sure, feels not very consequential either way.

I'll switch to:

Quoted 10 lines

        seq_printf(m,
                   "%20s %5s %3s master a %5s %10s %*s\n", "command",
                   "tgid",
                   "dev",
                   "uid",
                   "magic", DRM_CLIENT_NAME_MAX_LEN, "name");

That works.

Quoted 13 lines

And:

                seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %*s\n", task ? task->comm : "<unknown>", pid_vnr(pid), priv->minor->index, is_current_master ? 'y' : 'n', priv->authenticated ? 'y' : 'n', from_kuid_munged(seq_user_ns(m), uid), priv->magic, DRM_CLIENT_NAME_MAX_LEN, priv->client_name ? priv->client_name :
"<unset>");

Also works for me although it will look a bit busy by default since every line will contain it.

I don't immediately see "parseability" is a concern (what Dmitry raised) because new code can detect if there is something there or not. For old code, or future changes, we do not care in debugfs.

Equally we don't care that much if it looks busy, hence why I said "<unset>" works for me.

I'd also be okay with repeating task->comm, but that perhaps complicates things too much when task is not available.

Quoted 175 lines
Quoted 175 lines
Quoted 168 lines
drivers/gpu/drm/drm_debugfs.c +3 -1
              "command",
              "tgid",
              "dev",
              "uid",
-           "magic");
+           "magic",
+           "name");
       /* dev->filelist is sorted youngest first, but we want to present
        * oldest first (i.e. kernel, servers, clients), so walk backwardss.
@@ -94,19 +95,22 @@ static int drm_clients_info(struct seq_file *m, 
 void *data)
           struct task_struct *task;
           struct pid *pid;
+        mutex_lock(&priv->name_lock);

          rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */ pid = rcu_dereference(priv->pid);
          task = pid_task(pid, PIDTYPE_TGID);
          uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID; -        seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n", +        seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %20s\n", task ? task->comm : "<unknown>", pid_vnr(pid),
                 priv->minor->index,
                 is_current_master ? 'y' : 'n', priv->authenticated ? 'y' : 'n', from_kuid_munged(seq_user_ns(m), uid),
-               priv->magic);
+               priv->magic,
+               priv->name ?: "");
          rcu_read_unlock();
+        mutex_unlock(&priv->name_lock);
      }
      mutex_unlock(&dev->filelist_mutex);
      return 0;

drivers/gpu/drm/drm_file.c +5 -0
index 01fde94fe2a9..e9dd0e90a1f9 100644
@@ -158,6 +158,7 @@ struct drm_file *drm_file_alloc(struct drm_minor 
 *minor)
       spin_lock_init(&file->master_lookup_lock);
       mutex_init(&file->event_read_lock);
+    mutex_init(&file->name_lock);
       if (drm_core_check_feature(dev, DRIVER_GEM))
           drm_gem_open(dev, file);
@@ -259,6 +260,10 @@ void drm_file_free(struct drm_file *file)
       WARN_ON(!list_empty(&file->event_list));
       put_pid(rcu_access_pointer(file->pid));
+
+    mutex_destroy(&file->name_lock);
+    kfree(file->name);
+
       kfree(file);
   }
drivers/gpu/drm/drm_ioctl.c +48 -0
index 51f39912866f..46dadbd1bb35 100644
@@ -540,6 +540,52 @@ int drm_version(struct drm_device *dev, void *data,
       return err;
   }
+static int drm_set_name(struct drm_device *dev, void *data,
+            struct drm_file *file_priv)
+{
+    struct drm_set_name *name = data;
+    void __user *user_ptr;
+    char *new_name;
+    size_t i, len;
+
+    if (name->name_len > DRM_NAME_MAX_LEN)
+        return -EINVAL;
+
+    user_ptr = u64_to_user_ptr(name->name);
+
+    new_name = memdup_user_nul(user_ptr, name->name_len);
+    if (IS_ERR(new_name))
+        return PTR_ERR(new_name);
+
+    len = strlen(new_name);
+
+    if (len != name->name_len) {
+        kfree(new_name);
+        return -EINVAL;
+    }
+
+    /*
+     * Filter out control char / spaces / new lines etc in the name
+     * since it's going to be used in dmesg or fdinfo's output.
+     */
+    for (i = 0; i < len; i++) {
+        if (!isgraph(new_name[i]))
+            new_name[i] = '-';
+    }
+
+    mutex_lock(&file_priv->name_lock);
+    kfree(file_priv->name);
+    if (len > 0) {
+        file_priv->name = new_name;
+    } else {
+        kfree(new_name);
+        file_priv->name = NULL;
+    }
+    mutex_unlock(&file_priv->name_lock);
+
+    return 0;
+}
+
   static int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
   {
       /* ROOT_ONLY is only for CAP_SYS_ADMIN */
@@ -610,6 +656,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
       DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
       DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
+    DRM_IOCTL_DEF(DRM_IOCTL_SET_NAME, drm_set_name, DRM_RENDER_ALLOW),
+
       DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
       DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
       DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
include/drm/drm_file.h +9 -0
index 8c0030c77308..df26eee8f79c 100644
@@ -388,6 +388,15 @@ struct drm_file {
        * Per-file buffer caches used by the PRIME buffer sharing code.
        */
       struct drm_prime_file_private prime;
+
+    /**
+     * @name:
+     *
+     * Userspace-provided name; useful for accounting and debugging.
+     */
+    const char *name;
+    /** @name_lock: Protects @name. */
+    struct mutex name_lock;
   };
   /**
include/uapi/drm/drm.h +0 -0
index 16122819edfe..f5e92e4f909b 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {

      __u64 user_data;    /* user data passed to event */ };

include/uapi/drm/drm.h +7 -0
+#define DRM_NAME_MAX_LEN    64
+struct drm_set_name {
+    __u64 name_len;
+    __u64 name;
+};
+
+

  #if defined(__cplusplus)
  }
  #endif

include/uapi/drm/drm.h +4 -0
@@ -1288,6 +1295,16 @@ extern "C" {
    */
   #define DRM_IOCTL_MODE_CLOSEFB        DRM_IOWR(0xD0, struct drm_mode_closefb)
+/**
+ * DRM_IOCTL_SET_NAME - Attach a name to a drm_file
+ *
+ * This ioctl is similar to DMA_BUF_SET_NAME - it allows for easier 

tracking
+ * and debugging.
+ * The length of the name must <= DRM_NAME_MAX_LEN. All characters that are
+ * non-printable or whitespaces will be replaced by -.
+ */

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

Quoted 2 lines
include/uapi/drm/drm.h +2 -0
+#define DRM_IOCTL_SET_NAME        DRM_IOWR(0xD1, struct drm_set_name)
+

A comment, nice! :) Overal looks good to me.

Quoted 7 lines
Quoted 5 lines

I do however wish for more opinions (before merging) on whether strings with invalid characters should perhaps instead be rejected. I don't currently have a solid argument either way.

Perhaps the only argument against silent transformation is if someone sets some wild string, then greps for it somewhere, which would be a false negative without the understanding of what kind of remapping kernel does. It is weak but it is uapi so worth discussing every crazy possibility I think.

On the other hand it would create another annoying source of EINVAL. :shrug:

No strong feelings either, but I agree that gathering more opinions before merging would be useful.

Right.

Quoted 5 lines
Quoted 2 lines

Also, how are with with testing the DRM core features? Add something for the uapi in IGT/tests/drm_client_name, or some such?

Something like using the ioctl and then reading back the name using /sys/kernel/debug/dri/0/clients? I can do that if it's required / useful.

That and making sure various invalid and evil inputs are rejected/handled. Would be nice, but looking at what's currently in IGT I don't see that it is a requirement.

Regards,

Tvrtko

Quoted 12 lines

Thanks,
Pierre-Eric

Quoted 8 lines

Regards,

Tvrtko

Quoted 3 lines

  /*
   * Device specific ioctls should only be in their respective headers
   * The device specific ioctl range is from 0x40 to 0x9f.

#5 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

In reply to Pierre-Eric Pelloux-Prayer
Quoted 55 lines · Pierre-Eric Pelloux-Prayer

Giving the opportunity to userspace to associate a free-form name with a drm_file struct is helpful for tracking and debugging.

This is similar to the existing DMA_BUF_SET_NAME ioctl.

Access to name is protected by a mutex, and the 'clients' debugfs file has been updated to print it.

Userspace MR to use this ioctl:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

The string passed by userspace is filtered a bit, to avoid messing output when it's going to be printed (in dmesg, fdinfo, etc):
* all chars failing isgraph() are replaced by '-'
* if a 0-length string is passed the name is cleared

---
 drivers/gpu/drm/drm_debugfs.c | 12 ++++++---
 drivers/gpu/drm/drm_file.c    |  5 ++++
 drivers/gpu/drm/drm_ioctl.c   | 48 +++++++++++++++++++++++++++++++++++
 include/drm/drm_file.h        |  9 +++++++
 include/uapi/drm/drm.h        | 17 +++++++++++++
 5 files changed, 87 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +4 -3
index 6b239a24f1df..482e71160544 100644
@@ -78,12 +78,13 @@ static int drm_clients_info(struct seq_file *m, void *data)
 	kuid_t uid;
 
 	seq_printf(m,
-		   "%20s %5s %3s master a %5s %10s\n",
+		   "%20s %5s %3s master a %5s %10s %20s\n",
 		   "command",
 		   "tgid",
 		   "dev",
 		   "uid",
-		   "magic");
+		   "magic",
+		   "name");
 
 	/* dev->filelist is sorted youngest first, but we want to present
 	 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
@@ -94,19 +95,22 @@ static int drm_clients_info(struct seq_file *m, void *data)
 		struct task_struct *task;
 		struct pid *pid;
 
+		mutex_lock(&priv->name_lock);
 		rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */
 		pid = rcu_dereference(priv->pid);
 		task = pid_task(pid, PIDTYPE_TGID);
 		uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
-		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n",

drivers/gpu/drm/drm_debugfs.c static int drm_clients_info(struct seq_file *m, void *data) · line 111

Quoted 10 lines
drivers/gpu/drm/drm_debugfs.c +3 -1
+		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %20s\n",
 			   task ? task->comm : "<unknown>",
 			   pid_vnr(pid),
 			   priv->minor->index,
 			   is_current_master ? 'y' : 'n',
 			   priv->authenticated ? 'y' : 'n',
 			   from_kuid_munged(seq_user_ns(m), uid),
-			   priv->magic);
+			   priv->magic,
+			   priv->name ?: "");

There should be a default name similar to task->comm, like "<undefined>" when not set. Perhaps also set name to task->comm by default.

Quoted 44 lines
drivers/gpu/drm/drm_debugfs.c +1 -0
 		rcu_read_unlock();
+		mutex_unlock(&priv->name_lock);
 	}
 	mutex_unlock(&dev->filelist_mutex);
 	return 0;
drivers/gpu/drm/drm_file.c +5 -0
index 01fde94fe2a9..e9dd0e90a1f9 100644
@@ -158,6 +158,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
 
 	spin_lock_init(&file->master_lookup_lock);
 	mutex_init(&file->event_read_lock);
+	mutex_init(&file->name_lock);
 
 	if (drm_core_check_feature(dev, DRIVER_GEM))
 		drm_gem_open(dev, file);
@@ -259,6 +260,10 @@ void drm_file_free(struct drm_file *file)
 	WARN_ON(!list_empty(&file->event_list));
 
 	put_pid(rcu_access_pointer(file->pid));
+
+	mutex_destroy(&file->name_lock);
+	kfree(file->name);
+
 	kfree(file);
 }
 
drivers/gpu/drm/drm_ioctl.c +8 -0
index 51f39912866f..46dadbd1bb35 100644
@@ -540,6 +540,52 @@ int drm_version(struct drm_device *dev, void *data,
 	return err;
 }
 
+static int drm_set_name(struct drm_device *dev, void *data,
+			struct drm_file *file_priv)
+{
+	struct drm_set_name *name = data;
+	void __user *user_ptr;
+	char *new_name;
+	size_t i, len;
+

drivers/gpu/drm/drm_ioctl.c int drm_version(struct drm_device *dev, void *data, · line 560

Quoted 10 lines
drivers/gpu/drm/drm_ioctl.c +10 -0
+	if (name->name_len > DRM_NAME_MAX_LEN)
+		return -EINVAL;
+
+	user_ptr = u64_to_user_ptr(name->name);
+
+	new_name = memdup_user_nul(user_ptr, name->name_len);
+	if (IS_ERR(new_name))
+		return PTR_ERR(new_name);
+
+	len = strlen(new_name);

strnlen

Quoted 44 lines
drivers/gpu/drm/drm_ioctl.c +29 -0
+	if (len != name->name_len) {
+		kfree(new_name);
+		return -EINVAL;
+	}
+
+	/*
+	 * Filter out control char / spaces / new lines etc in the name
+	 * since it's going to be used in dmesg or fdinfo's output.
+	 */
+	for (i = 0; i < len; i++) {
+		if (!isgraph(new_name[i]))
+			new_name[i] = '-';
+	}
+
+	mutex_lock(&file_priv->name_lock);
+	kfree(file_priv->name);
+	if (len > 0) {
+		file_priv->name = new_name;
+	} else {
+		kfree(new_name);
+		file_priv->name = NULL;
+	}
+	mutex_unlock(&file_priv->name_lock);
+
+	return 0;
+}
+
 static int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
 {
 	/* ROOT_ONLY is only for CAP_SYS_ADMIN */
@@ -610,6 +656,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
 	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
 
+	DRM_IOCTL_DEF(DRM_IOCTL_SET_NAME, drm_set_name, DRM_RENDER_ALLOW),
+
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
include/drm/drm_file.h +0 -0
index 8c0030c77308..df26eee8f79c 100644

include/drm/drm_file.h struct drm_file { · line 397

Quoted 10 lines
include/drm/drm_file.h +7 -0
@@ -388,6 +388,15 @@ struct drm_file {
 	 * Per-file buffer caches used by the PRIME buffer sharing code.
 	 */
 	struct drm_prime_file_private prime;
+
+	/**
+	 * @name:
+	 *
+	 * Userspace-provided name; useful for accounting and debugging.
+	 */
+	const char *name;

I'd make the "name" string static, i.e. char name[DRM_NAME_MAX_LEN + 1]. That will prevent pointer deref troubles and no additional MM code bloating will be needed.

include/drm/drm_file.h struct drm_file { · line 399

Quoted 2 lines
include/drm/drm_file.h +2 -0
+	/** @name_lock: Protects @name. */
+	struct mutex name_lock;

And then this lock isn't strictly needed anymore and can be removed if "name" string is static.

include/uapi/drm/drm.h struct drm_crtc_queue_sequence { · line 1027

Quoted 12 lines
include/drm/drm_file.h +0 -0
 };
 
 /**
include/uapi/drm/drm.h +1 -0
index 16122819edfe..f5e92e4f909b 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {
 	__u64 user_data;	/* user data passed to event */
 };
 
+#define DRM_NAME_MAX_LEN	64

What about 63, to align data size to 64 bytes including the NULL byte.

include/uapi/drm/drm.h struct drm_crtc_queue_sequence { · line 1028

Quoted 1 line
include/uapi/drm/drm.h +1 -0
+struct drm_set_name {

drm_set_name sounds very generic, IMO. Maybe drm_context_set_name?

Quoted 9 lines
include/uapi/drm/drm.h +5 -0
+	__u64 name_len;
+	__u64 name;
+};
+
+
 #if defined(__cplusplus)
 }
 #endif

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

Quoted 8 lines
include/uapi/drm/drm.h +5 -0
@@ -1288,6 +1295,16 @@ extern "C" {
  */
 #define DRM_IOCTL_MODE_CLOSEFB		DRM_IOWR(0xD0, struct drm_mode_closefb)
 
+/**
+ * DRM_IOCTL_SET_NAME - Attach a name to a drm_file
+ *
+ * This ioctl is similar to DMA_BUF_SET_NAME - it allows for easier tracking
+ * and debugging.

Don't refer to DMA_BUF_SET_NAME, explain what DRM_IOCTL_SET_NAME actually do. Tell that it sets the DRM context name and that chars are filtered.

Signature · 2 lines
Best regards,
Dmitry
#6 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

On 9/23/24 13:28, Dmitry Osipenko wrote:
...

Quoted 9 lines
Quoted 9 lines

/**

include/uapi/drm/drm.h +0 -0
index 16122819edfe..f5e92e4f909b 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {
 	__u64 user_data;	/* user data passed to event */
 };
 
Quoted 3 lines

include/uapi/drm/drm.h struct drm_crtc_queue_sequence { · line 1027

Quoted 1 line
include/uapi/drm/drm.h +1 -0
+#define DRM_NAME_MAX_LEN	64

What about 63, to align data size to 64 bytes including the NULL byte.

On the other hand, the string is copied without NULL byte, so it doesn't matter.

This leads to another question, why not using strndup_user like it's done for dma_buf_set_name?

Signature · 2 lines
Best regards,
Dmitry
#7 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

In reply to Dmitry Osipenko

Hi Dmitry,

Le 23/09/2024 à 12:28, Dmitry Osipenko a écrit :

Quoted 61 lines · Pierre-Eric Pelloux-Prayer
Quoted 60 lines

Giving the opportunity to userspace to associate a free-form name with a drm_file struct is helpful for tracking and debugging.

This is similar to the existing DMA_BUF_SET_NAME ioctl.

Access to name is protected by a mutex, and the 'clients' debugfs file has been updated to print it.

Userspace MR to use this ioctl:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

The string passed by userspace is filtered a bit, to avoid messing output when it's going to be printed (in dmesg, fdinfo, etc):
* all chars failing isgraph() are replaced by '-'
* if a 0-length string is passed the name is cleared

---
  drivers/gpu/drm/drm_debugfs.c | 12 ++++++---
  drivers/gpu/drm/drm_file.c    |  5 ++++
  drivers/gpu/drm/drm_ioctl.c   | 48 +++++++++++++++++++++++++++++++++++
  include/drm/drm_file.h        |  9 +++++++
  include/uapi/drm/drm.h        | 17 +++++++++++++
  5 files changed, 87 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +5 -3
index 6b239a24f1df..482e71160544 100644
@@ -78,12 +78,13 @@ static int drm_clients_info(struct seq_file *m, void *data)
  	kuid_t uid;
 
  	seq_printf(m,
-		   "%20s %5s %3s master a %5s %10s\n",
+		   "%20s %5s %3s master a %5s %10s %20s\n",
  		   "command",
  		   "tgid",
  		   "dev",
  		   "uid",
-		   "magic");
+		   "magic",
+		   "name");
 
  	/* dev->filelist is sorted youngest first, but we want to present
  	 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
@@ -94,19 +95,22 @@ static int drm_clients_info(struct seq_file *m, void *data)
  		struct task_struct *task;
  		struct pid *pid;
 
+		mutex_lock(&priv->name_lock);
  		rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */
  		pid = rcu_dereference(priv->pid);
  		task = pid_task(pid, PIDTYPE_TGID);
  		uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
-		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n",
+		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %20s\n",
  			   task ? task->comm : "<unknown>",
  			   pid_vnr(pid),
  			   priv->minor->index,
  			   is_current_master ? 'y' : 'n',
Quoted 8 lines

drivers/gpu/drm/drm_debugfs.c static int drm_clients_info(struct seq_file *m, void *data) · line 111

Quoted 5 lines
drivers/gpu/drm/drm_debugfs.c +2 -1
  			   priv->authenticated ? 'y' : 'n',
  			   from_kuid_munged(seq_user_ns(m), uid),
-			   priv->magic);
+			   priv->magic,
+			   priv->name ?: "");

There should be a default name similar to task->comm, like "<undefined>" when not set. Perhaps also set name to task->comm by default.

Honestly I don't see much value in printing "<undefined>" or any other default value (+ task->comm is already printed above).

Quoted 49 lines
Quoted 49 lines
drivers/gpu/drm/drm_debugfs.c +1 -0
  		rcu_read_unlock();
+		mutex_unlock(&priv->name_lock);
  	}
  	mutex_unlock(&dev->filelist_mutex);
  	return 0;
drivers/gpu/drm/drm_file.c +5 -0
index 01fde94fe2a9..e9dd0e90a1f9 100644
@@ -158,6 +158,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
 
  	spin_lock_init(&file->master_lookup_lock);
  	mutex_init(&file->event_read_lock);
+	mutex_init(&file->name_lock);
 
  	if (drm_core_check_feature(dev, DRIVER_GEM))
  		drm_gem_open(dev, file);
@@ -259,6 +260,10 @@ void drm_file_free(struct drm_file *file)
  	WARN_ON(!list_empty(&file->event_list));
 
  	put_pid(rcu_access_pointer(file->pid));
+
+	mutex_destroy(&file->name_lock);
+	kfree(file->name);
+
  	kfree(file);
  }
 
drivers/gpu/drm/drm_ioctl.c +12 -0
index 51f39912866f..46dadbd1bb35 100644
@@ -540,6 +540,52 @@ int drm_version(struct drm_device *dev, void *data,
  	return err;
  }
 
+static int drm_set_name(struct drm_device *dev, void *data,
+			struct drm_file *file_priv)
+{
+	struct drm_set_name *name = data;
+	void __user *user_ptr;
+	char *new_name;
+	size_t i, len;
+
+	if (name->name_len > DRM_NAME_MAX_LEN)
+		return -EINVAL;
+
+	user_ptr = u64_to_user_ptr(name->name);
Quoted 8 lines

drivers/gpu/drm/drm_ioctl.c int drm_version(struct drm_device *dev, void *data, · line 560

Quoted 6 lines
drivers/gpu/drm/drm_ioctl.c +6 -0
+
+	new_name = memdup_user_nul(user_ptr, name->name_len);
+	if (IS_ERR(new_name))
+		return PTR_ERR(new_name);
+
+	len = strlen(new_name);

strnlen

memdup_user_nul returns a NUL-terminated string so I don't see much need for using strnlen.

Quoted 51 lines
Quoted 51 lines
drivers/gpu/drm/drm_ioctl.c +29 -0
+	if (len != name->name_len) {
+		kfree(new_name);
+		return -EINVAL;
+	}
+
+	/*
+	 * Filter out control char / spaces / new lines etc in the name
+	 * since it's going to be used in dmesg or fdinfo's output.
+	 */
+	for (i = 0; i < len; i++) {
+		if (!isgraph(new_name[i]))
+			new_name[i] = '-';
+	}
+
+	mutex_lock(&file_priv->name_lock);
+	kfree(file_priv->name);
+	if (len > 0) {
+		file_priv->name = new_name;
+	} else {
+		kfree(new_name);
+		file_priv->name = NULL;
+	}
+	mutex_unlock(&file_priv->name_lock);
+
+	return 0;
+}
+
  static int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
  {
  	/* ROOT_ONLY is only for CAP_SYS_ADMIN */
@@ -610,6 +656,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
  	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
 
+	DRM_IOCTL_DEF(DRM_IOCTL_SET_NAME, drm_set_name, DRM_RENDER_ALLOW),
+
  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
  	DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
include/drm/drm_file.h +3 -0
index 8c0030c77308..df26eee8f79c 100644
@@ -388,6 +388,15 @@ struct drm_file {
  	 * Per-file buffer caches used by the PRIME buffer sharing code.
  	 */
  	struct drm_prime_file_private prime;
+
+	/**
+	 * @name:
Quoted 9 lines

include/drm/drm_file.h struct drm_file { · line 397

Quoted 4 lines
include/drm/drm_file.h +4 -0
+	 *
+	 * Userspace-provided name; useful for accounting and debugging.
+	 */
+	const char *name;

I'd make the "name" string static, i.e. char name[DRM_NAME_MAX_LEN + 1]. That will prevent pointer deref troubles and no additional MM code bloating will be needed.

Sure, I can do that if others prefer this way too.

Quoted 5 lines

include/drm/drm_file.h struct drm_file { · line 399

Quoted 2 lines
include/drm/drm_file.h +2 -0
+	/** @name_lock: Protects @name. */
+	struct mutex name_lock;

And then this lock isn't strictly needed anymore and can be removed if "name" string is static.

The locking also prevents concurrent modification.

Quoted 12 lines
Quoted 12 lines
include/drm/drm_file.h +0 -0
  };
 
  /**
include/uapi/drm/drm.h +0 -0
index 16122819edfe..f5e92e4f909b 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {
  	__u64 user_data;	/* user data passed to event */
  };
 
Quoted 3 lines

include/uapi/drm/drm.h struct drm_crtc_queue_sequence { · line 1027

Quoted 1 line
include/uapi/drm/drm.h +1 -0
+#define DRM_NAME_MAX_LEN	64

What about 63, to align data size to 64 bytes including the NULL byte.

Same as "const char *name;" vs "char name[...]": I don't mind updating the code as long as there's a consensus.

Quoted 4 lines

include/uapi/drm/drm.h struct drm_crtc_queue_sequence { · line 1028

Quoted 2 lines
include/uapi/drm/drm.h +1 -0
+struct drm_set_name {

drm_set_name sounds very generic, IMO. Maybe drm_context_set_name?

drm_client_set_name?
(since other places use client, like drm_clients_info())

Quoted 13 lines
Quoted 13 lines
include/uapi/drm/drm.h +5 -0
+	__u64 name_len;
+	__u64 name;
+};
+
+
  #if defined(__cplusplus)
  }
  #endif
@@ -1288,6 +1295,16 @@ extern "C" {
   */
  #define DRM_IOCTL_MODE_CLOSEFB		DRM_IOWR(0xD0, struct drm_mode_closefb)
 
Quoted 10 lines

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

Quoted 5 lines
include/uapi/drm/drm.h +5 -0
+/**
+ * DRM_IOCTL_SET_NAME - Attach a name to a drm_file
+ *
+ * This ioctl is similar to DMA_BUF_SET_NAME - it allows for easier tracking
+ * and debugging.

Don't refer to DMA_BUF_SET_NAME, explain what DRM_IOCTL_SET_NAME actually do. Tell that it sets the DRM context name and that chars are filtered.

OK, I'll update based on your suggestion.

Thanks,
Pierre-Eric

#8 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

On 9/23/24 19:29, Pierre-Eric Pelloux-Prayer wrote:
...

Quoted 41 lines
Quoted 32 lines
Quoted 32 lines
drivers/gpu/drm/drm_debugfs.c +3 -2
@@ -78,12 +78,13 @@ static int drm_clients_info(struct seq_file *m,
 void *data)
       kuid_t uid;
         seq_printf(m,
-           "%20s %5s %3s master a %5s %10s\n",
+           "%20s %5s %3s master a %5s %10s %20s\n",
              "command",
              "tgid",
              "dev",
              "uid",
-           "magic");
+           "magic",
+           "name");
         /* dev->filelist is sorted youngest first, but we want to

present
       * oldest first (i.e. kernel, servers, clients), so walk backwardss.

drivers/gpu/drm/drm_debugfs.c +0 -0
@@ -94,19 +95,22 @@ static int drm_clients_info(struct seq_file *m,
 void *data)
           struct task_struct *task;
           struct pid *pid;
   +        mutex_lock(&priv->name_lock);

          rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */ pid = rcu_dereference(priv->pid);
          task = pid_task(pid, PIDTYPE_TGID);
          uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID; -        seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n", +        seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u %20s\n", task ? task->comm : "<unknown>", pid_vnr(pid),
                 priv->minor->index,
                 is_current_master ? 'y' : 'n',

Quoted 8 lines
Quoted 5 lines

                 priv->authenticated ? 'y' : 'n', from_kuid_munged(seq_user_ns(m), uid), -               priv->magic);
+               priv->magic,
+               priv->name ?: "");

There should be a default name similar to task->comm, like "<undefined>" when not set. Perhaps also set name to task->comm by default.

Quoted 2 lines

Honestly I don't see much value in printing "<undefined>" or any other default value (+ task->comm is already printed above).

For a machine-parsed string in userspace there should be a value, otherwise it won't be parseable if you'll add another parameter after the name, AFAICT.

...

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines

  +static int drm_set_name(struct drm_device *dev, void *data, +            struct drm_file *file_priv)

drivers/gpu/drm/drm_ioctl.c +10 -0
+{
+    struct drm_set_name *name = data;
+    void __user *user_ptr;
+    char *new_name;
+    size_t i, len;
+
+    if (name->name_len > DRM_NAME_MAX_LEN)
+        return -EINVAL;
+
+    user_ptr = u64_to_user_ptr(name->name);
Quoted 8 lines

drivers/gpu/drm/drm_ioctl.c int drm_version(struct drm_device *dev, void *data, · line 560

Quoted 6 lines
drivers/gpu/drm/drm_ioctl.c +6 -0
+
+    new_name = memdup_user_nul(user_ptr, name->name_len);
+    if (IS_ERR(new_name))
+        return PTR_ERR(new_name);
+
+    len = strlen(new_name);

strnlen

Quoted 2 lines

memdup_user_nul returns a NUL-terminated string so I don't see much need for using strnlen.

Indeed

...

Quoted 37 lines
Quoted 27 lines
Quoted 27 lines

  static int drm_ioctl_permit(u32 flags, struct drm_file *file_priv) {
      /* ROOT_ONLY is only for CAP_SYS_ADMIN */

(quoted diff) +1 -0
@@ -610,6 +656,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
       DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD,
 drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
       DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE,
 drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
   +    DRM_IOCTL_DEF(DRM_IOCTL_SET_NAME, drm_set_name,
 DRM_RENDER_ALLOW),
+

      DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0), DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0), DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),

include/drm/drm_file.h +3 -0
index 8c0030c77308..df26eee8f79c 100644
@@ -388,6 +388,15 @@ struct drm_file {
        * Per-file buffer caches used by the PRIME buffer sharing code.
        */
       struct drm_prime_file_private prime;
+
+    /**
+     * @name:
Quoted 9 lines

include/drm/drm_file.h struct drm_file { · line 397

Quoted 4 lines
include/drm/drm_file.h +4 -0
+     *
+     * Userspace-provided name; useful for accounting and debugging.
+     */
+    const char *name;

I'd make the "name" string static, i.e. char name[DRM_NAME_MAX_LEN + 1]. That will prevent pointer deref troubles and no additional MM code bloating will be needed.

Quoted 1 line

Sure, I can do that if others prefer this way too.

Note that in the other email I suggested to use strndup_user(), that will remove the name-length limitation, but then the name var will remain to be a string pointer. To me best option would be to replicate how dma_buf_set_name works.

Quoted 7 lines
Quoted 5 lines

include/drm/drm_file.h struct drm_file { · line 399

Quoted 2 lines
include/drm/drm_file.h +2 -0
+    /** @name_lock: Protects @name. */
+    struct mutex name_lock;

And then this lock isn't strictly needed anymore and can be removed if "name" string is static.

The locking also prevents concurrent modification.

Right, locking still will be needed

Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
include/drm/drm_file.h +0 -0
   };
     /**
include/uapi/drm/drm.h +0 -0
index 16122819edfe..f5e92e4f909b 100644
Quoted 8 lines
Quoted 6 lines
include/uapi/drm/drm.h +0 -0
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {

      __u64 user_data;    /* user data passed to event */ };
  +#define DRM_NAME_MAX_LEN    64

What about 63, to align data size to 64 bytes including the NULL byte.

Same as "const char *name;" vs "char name[...]": I don't mind updating the code as long as there's a consensus.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+struct drm_set_name {

drm_set_name sounds very generic, IMO. Maybe drm_context_set_name?

drm_client_set_name?
(since other places use client, like drm_clients_info())

drm_client_set_name sounds good

Signature · 2 lines
Best regards,
Dmitry
#9 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

Quoted 5 lines · Dmitry Osipenko
Quoted 1 line

Sure, I can do that if others prefer this way too.

Note that in the other email I suggested to use strndup_user(), that will remove the name-length limitation, but then the name var will remain to be a string pointer. To me best option would be to replicate how dma_buf_set_name works.

My bad, strndup_user() is also size-limited. Then the point about static string remains.

Signature · 2 lines
Best regards,
Dmitry
#10 lore raw

Re: [PATCH v3 1/6] drm: add DRM_SET_NAME ioctl

Quoted 9 lines · Dmitry Osipenko
Quoted 5 lines · Dmitry Osipenko
Quoted 1 line

Sure, I can do that if others prefer this way too.

Note that in the other email I suggested to use strndup_user(), that will remove the name-length limitation, but then the name var will remain to be a string pointer. To me best option would be to replicate how dma_buf_set_name works.

My bad, strndup_user() is also size-limited. Then the point about static string remains.

To clarify a bit further, I'm fine with both variants. Having a consistent solution across kernel is also good if it's good enough. I.e. replicating the whole dma_buf_set_name using the name pointer is okay to me, though not having string pointers is more robust in general. Choose what you like more.

Signature · 2 lines
Best regards,
Dmitry
#11 lore raw

PATCH v3 2/6 drm: use drm_file name in fdinfo

In reply to Pierre-Eric Pelloux-Prayer

Add an optional drm-client-name field to drm fdinfo's output.

---
 Documentation/gpu/drm-usage-stats.rst | 5 +++++
 drivers/gpu/drm/drm_file.c            | 5 +++++
 2 files changed, 10 insertions(+)
Documentation/gpu/drm-usage-stats.rst +5 -0
index a80f95ca1b2f..ed1d7edbbc5f 100644
@@ -73,6 +73,11 @@ scope of each device, in which case `drm-pdev` shall be present as well.
 Userspace should make sure to not double account any usage statistics by using
 the above described criteria in order to associate data to individual clients.
 
+- drm-client-name: <valstr>
+
+String optionally set by userspace using DRM_IOCTL_SET_NAME.
+
+
 Utilization
 ^^^^^^^^^^^
 
drivers/gpu/drm/drm_file.c +5 -0
index e9dd0e90a1f9..6a3621f50784 100644
@@ -955,6 +955,11 @@ void drm_show_fdinfo(struct seq_file *m, struct file *f)
 			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
 	}
 
+	mutex_lock(&file->name_lock);
+	if (file->name)
+		drm_printf(&p, "drm-client-name:\t%s\n", file->name);
+	mutex_unlock(&file->name_lock);
+
 	if (dev->driver->show_fdinfo)
 		dev->driver->show_fdinfo(&p, file);
 }
Signature · 1 line
2.40.1
#12 lore raw

PATCH v3 3/6 drm/amdgpu: delay the use of amdgpu_vm_set_task_info

In reply to Pierre-Eric Pelloux-Prayer

At this point the vm is locked so we safely modify it without risk of concurrent access.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -3
index 1e475eb01417..891128ecee6d 100644
@@ -309,9 +309,6 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
 		p->gang_leader->uf_addr = uf_offset;
 	kvfree(chunk_array);
 
-	/* Use this opportunity to fill in task info for the vm */
-	amdgpu_vm_set_task_info(vm);
-
 	return 0;
 
 free_all_kdata:
@@ -1180,6 +1177,9 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 		job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
 	}
 
+	/* Use this opportunity to fill in task info for the vm */
+	amdgpu_vm_set_task_info(vm);
+
 	if (adev->debug_vm) {
 		/* Invalidate all BOs to test for userspace bugs */
 		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
Signature · 1 line
2.40.1
#13 lore raw

Re: [PATCH v3 3/6] drm/amdgpu: delay the use of amdgpu_vm_set_task_info

Quoted 2 lines · Pierre-Eric Pelloux-Prayer

At this point the vm is locked so we safely modify it without risk of concurrent access.

To which particular lock this is referring to and does this imply previous placement was unsafe?

Regards,

Tvrtko

Quoted 29 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -3
index 1e475eb01417..891128ecee6d 100644
@@ -309,9 +309,6 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
  		p->gang_leader->uf_addr = uf_offset;
  	kvfree(chunk_array);
 
-	/* Use this opportunity to fill in task info for the vm */
-	amdgpu_vm_set_task_info(vm);
-
  	return 0;
 
  free_all_kdata:
@@ -1180,6 +1177,9 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
  		job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
  	}
 
+	/* Use this opportunity to fill in task info for the vm */
+	amdgpu_vm_set_task_info(vm);
+
  	if (adev->debug_vm) {
  		/* Invalidate all BOs to test for userspace bugs */
  		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
maintainer
#14 lore raw

Re: [PATCH v3 3/6] drm/amdgpu: delay the use of amdgpu_vm_set_task_info

Am 23.09.24 um 12:25 schrieb Tvrtko Ursulin:

Quoted 6 lines · Pierre-Eric Pelloux-Prayer
Quoted 2 lines

At this point the vm is locked so we safely modify it without risk of concurrent access.

To which particular lock this is referring to and does this imply previous placement was unsafe?

We use the root PDs dma_resv object as VM lock to protect most field inside the VM structure, only a few are protected by an additional spinlock.

And yes, previously it was possible that you got a mangled process/task name because no lock was protecting the task_info structure.

Regards,
Christian.

Quoted 33 lines

Regards,

Tvrtko

Quoted 28 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
index 1e475eb01417..891128ecee6d 100644
@@ -309,9 +309,6 @@ static int amdgpu_cs_pass1(struct 
 amdgpu_cs_parser *p,
           p->gang_leader->uf_addr = uf_offset;
       kvfree(chunk_array);

  -    /* Use this opportunity to fill in task info for the vm */ -    amdgpu_vm_set_task_info(vm);
-
      return 0;
    free_all_kdata:

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1180,6 +1177,9 @@ static int amdgpu_cs_vm_handling(struct 
 amdgpu_cs_parser *p)
           job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
       }

  +    /* Use this opportunity to fill in task info for the vm */ +    amdgpu_vm_set_task_info(vm);
+
      if (adev->debug_vm) {
          /* Invalidate all BOs to test for userspace bugs */ amdgpu_bo_list_for_each_entry(e, p->bo_list) {

#15 lore raw

Re: [PATCH v3 3/6] drm/amdgpu: delay the use of amdgpu_vm_set_task_info

Quoted 11 lines · Christian König

Am 23.09.24 um 12:25 schrieb Tvrtko Ursulin:

Quoted 6 lines · Pierre-Eric Pelloux-Prayer
Quoted 2 lines

At this point the vm is locked so we safely modify it without risk of concurrent access.

To which particular lock this is referring to and does this imply previous placement was unsafe?

We use the root PDs dma_resv object as VM lock to protect most field inside the VM structure, only a few are protected by an additional spinlock.

And yes, previously it was possible that you got a mangled process/task name because no lock was protecting the task_info structure.

Got it, thanks Christian!

In this case I only suggest to be more explicit in the commit message and clearly say it is fixing an existing bug. Like it stands I wasn't sure if it was that, or the movement was just enabling the changes which come later in the series.

Regards,

Tvrtko

Quoted 29 lines
Quoted 28 lines
Quoted 28 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
index 1e475eb01417..891128ecee6d 100644
@@ -309,9 +309,6 @@ static int amdgpu_cs_pass1(struct 
 amdgpu_cs_parser *p,
           p->gang_leader->uf_addr = uf_offset;
       kvfree(chunk_array);

  -    /* Use this opportunity to fill in task info for the vm */ -    amdgpu_vm_set_task_info(vm);
-
      return 0;
    free_all_kdata:

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1180,6 +1177,9 @@ static int amdgpu_cs_vm_handling(struct 
 amdgpu_cs_parser *p)
           job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
       }

  +    /* Use this opportunity to fill in task info for the vm */ +    amdgpu_vm_set_task_info(vm);
+
      if (adev->debug_vm) {
          /* Invalidate all BOs to test for userspace bugs */ amdgpu_bo_list_for_each_entry(e, p->bo_list) {

#16 lore raw

Re: [PATCH v3 3/6] drm/amdgpu: delay the use of amdgpu_vm_set_task_info

Le 24/09/2024 à 10:43, Tvrtko Ursulin a écrit :

Quoted 10 lines · Christian König
Quoted 8 lines

Am 23.09.24 um 12:25 schrieb Tvrtko Ursulin:

Quoted 6 lines · Pierre-Eric Pelloux-Prayer
Quoted 2 lines

At this point the vm is locked so we safely modify it without risk of concurrent access.

To which particular lock this is referring to and does this imply previous placement was unsafe?

Quoted 7 lines
Quoted 3 lines

We use the root PDs dma_resv object as VM lock to protect most field inside the VM structure, only a few are protected by an additional spinlock.

And yes, previously it was possible that you got a mangled process/task name because no lock was protecting the task_info structure.

Got it, thanks Christian!

In this case I only suggest to be more explicit in the commit message and clearly say it is fixing an existing bug. Like it stands I wasn't sure if it was that, or the movement was just enabling the changes which come later in the series.

Good idea, will do.

Pierre-Eric

Quoted 32 lines

Regards,

Tvrtko

Quoted 27 lines
Quoted 26 lines
Quoted 26 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
index 1e475eb01417..891128ecee6d 100644
@@ -309,9 +309,6 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
           p->gang_leader->uf_addr = uf_offset;
       kvfree(chunk_array);

  -    /* Use this opportunity to fill in task info for the vm */ -    amdgpu_vm_set_task_info(vm);
-
      return 0;
    free_all_kdata:

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1180,6 +1177,9 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
           job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
       }

  +    /* Use this opportunity to fill in task info for the vm */ +    amdgpu_vm_set_task_info(vm);
+
      if (adev->debug_vm) {
          /* Invalidate all BOs to test for userspace bugs */ amdgpu_bo_list_for_each_entry(e, p->bo_list) {

#17 lore raw

PATCH v3 4/6 drm/amdgpu: alloc and init vm::task_info from first submit

In reply to Pierre-Eric Pelloux-Prayer

This will allow to use flexible array to store the process name and other information.

This also means that process name will be determined once and for all, instead of at each submit.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -6
index e20d19ae01b2..690676cab022 100644
@@ -2331,7 +2331,7 @@ amdgpu_vm_get_task_info_vm(struct amdgpu_vm *vm)
 {
 	struct amdgpu_task_info *ti = NULL;
 
-	if (vm) {
+	if (vm && vm->task_info) {
 		ti = vm->task_info;
 		kref_get(&vm->task_info->refcount);
 	}
@@ -2372,8 +2372,12 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
  */
 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
 {
-	if (!vm->task_info)
-		return;
+	if (!vm->task_info) {
+		if (amdgpu_vm_create_task_info(vm))
+			return;
+
+		get_task_comm(vm->task_info->process_name, current->group_leader);
+	}
 
 	if (vm->task_info->pid == current->pid)
 		return;
@@ -2385,7 +2389,6 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
 		return;
 
 	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
 }
 
 /**
@@ -2482,7 +2485,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto error_free_root;
 
-	r = amdgpu_vm_create_task_info(vm);
 	if (r)
 		DRM_DEBUG("Failed to create task info for VM\n");
 
@@ -2608,7 +2610,8 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 
 	root = amdgpu_bo_ref(vm->root.bo);
 	amdgpu_bo_reserve(root, true);
-	amdgpu_vm_put_task_info(vm->task_info);
+	if (vm->task_info)
+		amdgpu_vm_put_task_info(vm->task_info);
 	amdgpu_vm_set_pasid(adev, vm, 0);
 	dma_fence_wait(vm->last_unlocked, false);
 	dma_fence_put(vm->last_unlocked);
Signature · 1 line
2.40.1
#18 lore raw

Re: [PATCH v3 4/6] drm/amdgpu: alloc and init vm::task_info from first submit

Quoted 5 lines · Pierre-Eric Pelloux-Prayer

This will allow to use flexible array to store the process name and other information.

This also means that process name will be determined once and for all, instead of at each submit.

But the pid and others can still change? By design?

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index e20d19ae01b2..690676cab022 100644
@@ -2331,7 +2331,7 @@ amdgpu_vm_get_task_info_vm(struct amdgpu_vm *vm)
  {
  	struct amdgpu_task_info *ti = NULL;
 
-	if (vm) {
+	if (vm && vm->task_info) {
  		ti = vm->task_info;
  		kref_get(&vm->task_info->refcount);
  	}
@@ -2372,8 +2372,12 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
   */
  void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
  {

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm) · line 2382

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -2
-	if (!vm->task_info)
-		return;
+	if (!vm->task_info) {
+		if (amdgpu_vm_create_task_info(vm))
+			return;
+
+		get_task_comm(vm->task_info->process_name, current->group_leader);
+	}
 
  	if (vm->task_info->pid == current->pid)

This ends up relying on vm->task_info->pid being zero due kzalloc right?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) · line 2392

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
  		return;
@@ -2385,7 +2389,6 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
  		return;
 
  	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
  }

I wonder how many of the task_info fields you want to set once instead of per submission. Like a fully one shot like the below be what you want?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +17 -19
index a060c28f0877..da492223a8b5 100644
@@ -2349,16 +2349,6 @@ amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid)
 			amdgpu_vm_get_vm_from_pasid(adev, pasid));
 }
 
-static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
-{
-	vm->task_info = kzalloc(sizeof(struct amdgpu_task_info), GFP_KERNEL);
-	if (!vm->task_info)
-		return -ENOMEM;
-
-	kref_init(&vm->task_info->refcount);
-	return 0;
-}
-
 /**
  * amdgpu_vm_set_task_info - Sets VMs task info.
  *
@@ -2366,20 +2356,28 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
  */
 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
 {
-	if (!vm->task_info)
-		return;
+	struct amdgpu_task_info *task_info = vm->task_info;
+
+	if (!task_info) {
+		task_info = kzalloc(sizeof(struct amdgpu_task_info),
+				    GFP_KERNEL);
+		if (!task_info)
+			return;
 
-	if (vm->task_info->pid == current->pid)
+		kref_init(&task_info->refcount);
+	} else {
 		return;
+	}
 
-	vm->task_info->pid = current->pid;
-	get_task_comm(vm->task_info->task_name, current);
+	task_info->pid = current->pid;
+	get_task_comm(task_info->task_name, current);
 
-	if (current->group_leader->mm != current->mm)
-		return;
+	if (current->group_leader->mm == current->mm) {
+		task_info->tgid = current->group_leader->pid;
+		get_task_comm(task_info->process_name, current->group_leader);
+	}
 
-	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
+	vm->task_info = task_info;
 }
 
 /**

End result is code like this:

void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)

{
	struct amdgpu_task_info *task_info = vm->task_info;

	if (!task_info) {
		task_info = kzalloc(sizeof(struct amdgpu_task_info),
				    GFP_KERNEL);
		if (!task_info)
			return;

		kref_init(&task_info->refcount);
	} else {
		return;
	}

	task_info->pid = current->pid;
	get_task_comm(task_info->task_name, current);

	if (current->group_leader->mm == current->mm) {
		task_info->tgid = current->group_leader->pid;
		get_task_comm(task_info->process_name, current->group_leader);
	}

	vm->task_info = task_info;
}

?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 2489

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
  /**
@@ -2482,7 +2485,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	if (r)
  		goto error_free_root;
 
-	r = amdgpu_vm_create_task_info(vm);
  	if (r)
  		DRM_DEBUG("Failed to create task info for VM\n");

Two more lines to delete here.

Regards,

Tvrtko

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -2608,7 +2610,8 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 
  	root = amdgpu_bo_ref(vm->root.bo);
  	amdgpu_bo_reserve(root, true);
-	amdgpu_vm_put_task_info(vm->task_info);
+	if (vm->task_info)
+		amdgpu_vm_put_task_info(vm->task_info);
  	amdgpu_vm_set_pasid(adev, vm, 0);
  	dma_fence_wait(vm->last_unlocked, false);
  	dma_fence_put(vm->last_unlocked);
#19 lore raw

Re: [PATCH v3 4/6] drm/amdgpu: alloc and init vm::task_info from first submit

Le 23/09/2024 à 12:58, Tvrtko Ursulin a écrit :

Quoted 9 lines · Pierre-Eric Pelloux-Prayer
Quoted 5 lines

This will allow to use flexible array to store the process name and other information.

This also means that process name will be determined once and for all, instead of at each submit.

But the pid and others can still change? By design?

pid and task_name can change, yes.
tgid could be set once and for all I think.

Quoted 25 lines
Quoted 25 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 15 +++++++++------

  1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -3
index e20d19ae01b2..690676cab022 100644
@@ -2331,7 +2331,7 @@ amdgpu_vm_get_task_info_vm(struct amdgpu_vm *vm)
   {
       struct amdgpu_task_info *ti = NULL;
-    if (vm) {
+    if (vm && vm->task_info) {
           ti = vm->task_info;
           kref_get(&vm->task_info->refcount);
       }
@@ -2372,8 +2372,12 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
    */
   void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
   {
-    if (!vm->task_info)
-        return;
+    if (!vm->task_info) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm) · line 2381

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
+        if (amdgpu_vm_create_task_info(vm))
+            return;
+
+        get_task_comm(vm->task_info->process_name, current->group_leader);
+    }
       if (vm->task_info->pid == current->pid)

This ends up relying on vm->task_info->pid being zero due kzalloc right?

Yes.

Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) · line 2391

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
           return;
@@ -2385,7 +2389,6 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
           return;
       vm->task_info->tgid = current->group_leader->pid;
-    get_task_comm(vm->task_info->process_name, current->group_leader);
   }

I wonder how many of the task_info fields you want to set once instead of per submission. Like a fully one shot like the below be what you want?

As written above, process name, drm client name and pid (tgid) can be set once. Task name + tid are updated on submit.
I've updated slightly this part, so v4 should hopefully be clearer.

Quoted 91 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +17 -19
index a060c28f0877..da492223a8b5 100644
@@ -2349,16 +2349,6 @@ amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid)
              amdgpu_vm_get_vm_from_pasid(adev, pasid));
  }
 
-static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
-{
-    vm->task_info = kzalloc(sizeof(struct amdgpu_task_info), GFP_KERNEL);
-    if (!vm->task_info)
-        return -ENOMEM;
-
-    kref_init(&vm->task_info->refcount);
-    return 0;
-}
-
  /**
   * amdgpu_vm_set_task_info - Sets VMs task info.
   *
@@ -2366,20 +2356,28 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
   */
  void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
  {
-    if (!vm->task_info)
-        return;
+    struct amdgpu_task_info *task_info = vm->task_info;
+
+    if (!task_info) {
+        task_info = kzalloc(sizeof(struct amdgpu_task_info),
+                    GFP_KERNEL);
+        if (!task_info)
+            return;
 
-    if (vm->task_info->pid == current->pid)
+        kref_init(&task_info->refcount);
+    } else {
          return;
+    }
 
-    vm->task_info->pid = current->pid;
-    get_task_comm(vm->task_info->task_name, current);
+    task_info->pid = current->pid;
+    get_task_comm(task_info->task_name, current);
 
-    if (current->group_leader->mm != current->mm)
-        return;
+    if (current->group_leader->mm == current->mm) {
+        task_info->tgid = current->group_leader->pid;
+        get_task_comm(task_info->process_name, current->group_leader);
+    }
 
-    vm->task_info->tgid = current->group_leader->pid;
-    get_task_comm(vm->task_info->process_name, current->group_leader);
+    vm->task_info = task_info;
  }
 
  /**

End result is code like this:

void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) {
    struct amdgpu_task_info *task_info = vm->task_info;

    if (!task_info) {
        task_info = kzalloc(sizeof(struct amdgpu_task_info), GFP_KERNEL);
        if (!task_info)
            return;

        kref_init(&task_info->refcount);
    } else {
        return;
    }

    task_info->pid = current->pid; get_task_comm(task_info->task_name, current);

    if (current->group_leader->mm == current->mm) { task_info->tgid = current->group_leader->pid; get_task_comm(task_info->process_name, current->group_leader); }

    vm->task_info = task_info;
}

?

Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 2488

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
   /**
@@ -2482,7 +2485,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
       if (r)
           goto error_free_root;
-    r = amdgpu_vm_create_task_info(vm);
       if (r)
           DRM_DEBUG("Failed to create task info for VM\n");

Two more lines to delete here.

Done, thanks.

Pierre-Eric

Quoted 14 lines

Regards,

Tvrtko

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -2608,7 +2610,8 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
       root = amdgpu_bo_ref(vm->root.bo);
       amdgpu_bo_reserve(root, true);
-    amdgpu_vm_put_task_info(vm->task_info);
+    if (vm->task_info)
+        amdgpu_vm_put_task_info(vm->task_info);
       amdgpu_vm_set_pasid(adev, vm, 0);
       dma_fence_wait(vm->last_unlocked, false);
       dma_fence_put(vm->last_unlocked);
#20 lore raw

PATCH v3 5/6 drm/amdgpu: make process_name a flexible array

In reply to Pierre-Eric Pelloux-Prayer

And rename it process_desc, since it will soon contain more than just the process_name.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c      |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c |  4 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c          |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c           | 15 ++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h           |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c           |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c           |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c           |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v8_0.c            |  2 +-
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c            |  2 +-
 drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c           |  2 +-
 drivers/gpu/drm/amd/amdgpu/sdma_v4_4_2.c         |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_events.c          |  2 +-
 13 files changed, 25 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +1 -1
index cbef720de779..c2185e43e38d 100644
@@ -1786,7 +1786,7 @@ static int amdgpu_debugfs_vm_info_show(struct seq_file *m, void *unused)
 
 		ti = amdgpu_vm_get_task_info_vm(vm);
 		if (ti) {
-			seq_printf(m, "pid:%d\tProcess:%s ----------\n", ti->pid, ti->process_name);
+			seq_printf(m, "pid:%d\tProcess:%s ----------\n", ti->pid, ti->process_desc);
 			amdgpu_vm_put_task_info(ti);
 		}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c +2 -2
index 5ac59b62020c..4ca0a372984b 100644
@@ -220,8 +220,8 @@ amdgpu_devcoredump_read(char *buffer, loff_t offset, size_t count,
 		   coredump->reset_time.tv_nsec);
 
 	if (coredump->reset_task_info.pid)
-		drm_printf(&p, "process_name: %s PID: %d\n",
-			   coredump->reset_task_info.process_name,
+		drm_printf(&p, "process: %s PID: %d\n",
+			   coredump->reset_task_info.process_desc,
 			   coredump->reset_task_info.pid);
 
 	/* SOC Information */
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +1 -1
index ad6bf5d4e0a9..d1678eebbff3 100644
@@ -125,7 +125,7 @@ static enum drm_gpu_sched_stat amdgpu_job_timedout(struct drm_sched_job *s_job)
 	if (ti) {
 		dev_err(adev->dev,
 			"Process information: process %s pid %d thread %s pid %d\n",
-			ti->process_name, ti->tgid, ti->task_name, ti->pid);
+			ti->process_desc, ti->tgid, ti->task_name, ti->pid);
 		amdgpu_vm_put_task_info(ti);
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -3
index 690676cab022..7f852029d6e1 100644
@@ -2357,10 +2357,21 @@ amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid)
 
 static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
 {
-	vm->task_info = kzalloc(sizeof(struct amdgpu_task_info), GFP_KERNEL);
+	char process_name[TASK_COMM_LEN];
+	int desc_len;
+
+	get_task_comm(process_name, current->group_leader);
+	desc_len = strlen(process_name);
+
+	vm->task_info = kzalloc(
+		struct_size(vm->task_info, process_desc, desc_len + 1),
+		GFP_KERNEL);
+
 	if (!vm->task_info)
 		return -ENOMEM;
 
+	strscpy(vm->task_info->process_desc, process_name, desc_len + 1);
+
 	kref_init(&vm->task_info->refcount);
 	return 0;
 }
@@ -2375,8 +2386,6 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
 	if (!vm->task_info) {
 		if (amdgpu_vm_create_task_info(vm))
 			return;
-
-		get_task_comm(vm->task_info->process_name, current->group_leader);
 	}
 
 	if (vm->task_info->pid == current->pid)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index d12d66dca8e9..44da250217be 100644
@@ -232,11 +232,11 @@ struct amdgpu_vm_pte_funcs {
 };
 
 struct amdgpu_task_info {
-	char		process_name[TASK_COMM_LEN];
 	char		task_name[TASK_COMM_LEN];
 	pid_t		pid;
 	pid_t		tgid;
 	struct kref	refcount;
+	char		process_desc[];
 };
 
 /**
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +1 -1
index 9784a2892185..c82364e43a15 100644
@@ -166,7 +166,7 @@ static int gmc_v10_0_process_interrupt(struct amdgpu_device *adev,
 	if (task_info) {
 		dev_err(adev->dev,
 			" in process %s pid %d thread %s pid %d\n",
-			task_info->process_name, task_info->tgid,
+			task_info->process_desc, task_info->tgid,
 			task_info->task_name, task_info->pid);
 		amdgpu_vm_put_task_info(task_info);
 	}
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +1 -1
index 2797fd84432b..4dace3de1def 100644
@@ -136,7 +136,7 @@ static int gmc_v11_0_process_interrupt(struct amdgpu_device *adev,
 		if (task_info) {
 			dev_err(adev->dev,
 				" in process %s pid %d thread %s pid %d)\n",
-				task_info->process_name, task_info->tgid,
+				task_info->process_desc, task_info->tgid,
 				task_info->task_name, task_info->pid);
 			amdgpu_vm_put_task_info(task_info);
 		}
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +1 -1
index edcb5351f8cc..e56d702fbfed 100644
@@ -129,7 +129,7 @@ static int gmc_v12_0_process_interrupt(struct amdgpu_device *adev,
 		if (task_info) {
 			dev_err(adev->dev,
 				" in process %s pid %d thread %s pid %d)\n",
-				task_info->process_name, task_info->tgid,
+				task_info->process_desc, task_info->tgid,
 				task_info->task_name, task_info->pid);
 			amdgpu_vm_put_task_info(task_info);
 		}
drivers/gpu/drm/amd/amdgpu/gmc_v8_0.c +1 -1
index 86488c052f82..5fca64e71ada 100644
@@ -1451,7 +1451,7 @@ static int gmc_v8_0_process_interrupt(struct amdgpu_device *adev,
 		task_info = amdgpu_vm_get_task_info_pasid(adev, entry->pasid);
 		if (task_info) {
 			dev_err(adev->dev, " for process %s pid %d thread %s pid %d\n",
-				task_info->process_name, task_info->tgid,
+				task_info->process_desc, task_info->tgid,
 				task_info->task_name, task_info->pid);
 			amdgpu_vm_put_task_info(task_info);
 		}
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +1 -1
index c76ac0dfe572..c54c86dac14f 100644
@@ -635,7 +635,7 @@ static int gmc_v9_0_process_interrupt(struct amdgpu_device *adev,
 	if (task_info) {
 		dev_err(adev->dev,
 			" for process %s pid %d thread %s pid %d)\n",
-			task_info->process_name, task_info->tgid,
+			task_info->process_desc, task_info->tgid,
 			task_info->task_name, task_info->pid);
 		amdgpu_vm_put_task_info(task_info);
 	}
drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c +1 -1
index 23ef4eb36b40..ea1990c19803 100644
@@ -2186,7 +2186,7 @@ static int sdma_v4_0_print_iv_entry(struct amdgpu_device *adev,
 	if (task_info) {
 		dev_dbg_ratelimited(adev->dev,
 				    " for process %s pid %d thread %s pid %d\n",
-				    task_info->process_name, task_info->tgid,
+				    task_info->process_desc, task_info->tgid,
 				    task_info->task_name, task_info->pid);
 		amdgpu_vm_put_task_info(task_info);
 	}
drivers/gpu/drm/amd/amdgpu/sdma_v4_4_2.c +1 -1
index c77889040760..df00bf480dcf 100644
@@ -1701,7 +1701,7 @@ static int sdma_v4_4_2_print_iv_entry(struct amdgpu_device *adev,
 	task_info = amdgpu_vm_get_task_info_pasid(adev, entry->pasid);
 	if (task_info) {
 		dev_dbg_ratelimited(adev->dev, " for process %s pid %d thread %s pid %d\n",
-				    task_info->process_name, task_info->tgid,
+				    task_info->process_desc, task_info->tgid,
 				    task_info->task_name, task_info->pid);
 		amdgpu_vm_put_task_info(task_info);
 	}
drivers/gpu/drm/amd/amdkfd/kfd_events.c +1 -1
index ea3792249209..95ba07ae3b89 100644
@@ -1267,7 +1267,7 @@ void kfd_signal_reset_event(struct kfd_node *dev)
 			if (ti) {
 				dev_err(dev->adev->dev,
 					"Queues reset on process %s tid %d thread %s pid %d\n",
-					ti->process_name, ti->tgid, ti->task_name, ti->pid);
+					ti->process_desc, ti->tgid, ti->task_name, ti->pid);
 				amdgpu_vm_put_task_info(ti);
 			}
 		}
Signature · 1 line
2.40.1
#21 lore raw

PATCH v3 6/6 drm/amdgpu: use drm_file::name in task_info::process_desc

In reply to Pierre-Eric Pelloux-Prayer

If a drm_file name is set append it to the process name.

This information is useful with the virtio/native-context driver: this allows the guest applications identifier to visible in amdgpu's output.

The output in amdgpu_vm_info/amdgpu_gem_info looks like this:
pid:12255 Process:glxgears/test-set-fd-name ----------

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  1 +
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 26 ++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  3 +++
 6 files changed, 30 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
index f9d119448442..ad909173e419 100644
@@ -299,6 +299,7 @@ int amdgpu_amdkfd_gpuvm_set_vm_pasid(struct amdgpu_device *adev,
 				     struct amdgpu_vm *avm, u32 pasid);
 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
 					struct amdgpu_vm *avm,
+					struct drm_file *filp,
 					void **process_info,
 					struct dma_fence **ef);
 void amdgpu_amdkfd_gpuvm_release_process_vm(struct amdgpu_device *adev,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -1
index 6d5fd371d5ce..172882af6705 100644
@@ -1558,6 +1558,7 @@ int amdgpu_amdkfd_gpuvm_set_vm_pasid(struct amdgpu_device *adev,
 
 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
 					   struct amdgpu_vm *avm,
+					   struct drm_file *filp,
 					   void **process_info,
 					   struct dma_fence **ef)
 {
@@ -1577,7 +1578,7 @@ int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
 	if (ret)
 		return ret;
 
-	amdgpu_vm_set_task_info(avm);
+	amdgpu_vm_set_task_info(avm, filp);
 
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index 891128ecee6d..5d43e24906d2 100644
@@ -1178,7 +1178,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	}
 
 	/* Use this opportunity to fill in task info for the vm */
-	amdgpu_vm_set_task_info(vm);
+	amdgpu_vm_set_task_info(vm, p->filp);
 
 	if (adev->debug_vm) {
 		/* Invalidate all BOs to test for userspace bugs */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +22 -4
index 7f852029d6e1..a2b12f0c3253 100644
@@ -2355,14 +2355,20 @@ amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid)
 			amdgpu_vm_get_vm_from_pasid(adev, pasid));
 }
 
-static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
+static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm, struct drm_file *filp)
 {
 	char process_name[TASK_COMM_LEN];
-	int desc_len;
+	size_t desc_len;
 
 	get_task_comm(process_name, current->group_leader);
 	desc_len = strlen(process_name);
 
+	if (filp) {
+		mutex_lock(&filp->name_lock);
+		if (filp->name)
+			desc_len += 1 + strlen(filp->name);
+	}
+
 	vm->task_info = kzalloc(
 		struct_size(vm->task_info, process_desc, desc_len + 1),
 		GFP_KERNEL);
@@ -2371,6 +2377,17 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
 		return -ENOMEM;
 
 	strscpy(vm->task_info->process_desc, process_name, desc_len + 1);
+	if (filp) {
+		if (filp->name) {
+			size_t p_len = strlen(process_name);
+
+			vm->task_info->process_desc[p_len] = '/';
+			strscpy(&vm->task_info->process_desc[p_len + 1],
+				filp->name, (desc_len + 1) - (p_len + 1));
+		}
+		mutex_unlock(&filp->name_lock);
+	}
+
 
 	kref_init(&vm->task_info->refcount);
 	return 0;
@@ -2380,11 +2397,12 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
  * amdgpu_vm_set_task_info - Sets VMs task info.
  *
  * @vm: vm for which to set the info
+ * @filp: drm_file instance
  */
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, struct drm_file *filp)
 {
 	if (!vm->task_info) {
-		if (amdgpu_vm_create_task_info(vm))
+		if (amdgpu_vm_create_task_info(vm, filp))
 			return;
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index 44da250217be..8df3dece54c2 100644
@@ -561,7 +561,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 			    u32 vmid, u32 node_id, uint64_t addr, uint64_t ts,
 			    bool write_fault);
 
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, struct drm_file *filp);
 
 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
 				struct amdgpu_vm *vm);
drivers/gpu/drm/amd/amdkfd/kfd_process.c +3 -0
index a902950cc060..e473fe433d3f 100644
@@ -1654,6 +1654,7 @@ int kfd_process_device_init_vm(struct kfd_process_device *pdd,
 			       struct file *drm_file)
 {
 	struct amdgpu_fpriv *drv_priv;
+	struct drm_file *filp;
 	struct amdgpu_vm *avm;
 	struct kfd_process *p;
 	struct dma_fence *ef;
@@ -1673,8 +1674,10 @@ int kfd_process_device_init_vm(struct kfd_process_device *pdd,
 
 	p = pdd->process;
 	dev = pdd->dev;
+	filp = drm_file->private_data;
 
 	ret = amdgpu_amdkfd_gpuvm_acquire_process_vm(dev->adev, avm,
+						     filp,
 						     &p->kgd_process_info,
 						     &ef);
 	if (ret) {
Signature · 1 line
2.40.1
#22 lore raw

Re: [PATCH v3 6/6] drm/amdgpu: use drm_file::name in task_info::process_desc

Am 20.09.24 um 11:06 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 92 lines

If a drm_file name is set append it to the process name.

This information is useful with the virtio/native-context driver: this allows the guest applications identifier to visible in amdgpu's output.

The output in amdgpu_vm_info/amdgpu_gem_info looks like this:

    pid:12255	Process:glxgears/test-set-fd-name ----------
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  1 +
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  3 ++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 26 ++++++++++++++++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  2 +-
  drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  3 +++
  6 files changed, 30 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
index f9d119448442..ad909173e419 100644
@@ -299,6 +299,7 @@ int amdgpu_amdkfd_gpuvm_set_vm_pasid(struct amdgpu_device *adev,
  				     struct amdgpu_vm *avm, u32 pasid);
  int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
  					struct amdgpu_vm *avm,
+					struct drm_file *filp,
  					void **process_info,
  					struct dma_fence **ef);
  void amdgpu_amdkfd_gpuvm_release_process_vm(struct amdgpu_device *adev,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -1
index 6d5fd371d5ce..172882af6705 100644
@@ -1558,6 +1558,7 @@ int amdgpu_amdkfd_gpuvm_set_vm_pasid(struct amdgpu_device *adev,
 
  int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
  					   struct amdgpu_vm *avm,
+					   struct drm_file *filp,
  					   void **process_info,
  					   struct dma_fence **ef)
  {
@@ -1577,7 +1578,7 @@ int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
  	if (ret)
  		return ret;
 
-	amdgpu_vm_set_task_info(avm);
+	amdgpu_vm_set_task_info(avm, filp);
 
  	return 0;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index 891128ecee6d..5d43e24906d2 100644
@@ -1178,7 +1178,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
  	}
 
  	/* Use this opportunity to fill in task info for the vm */
-	amdgpu_vm_set_task_info(vm);
+	amdgpu_vm_set_task_info(vm, p->filp);
 
  	if (adev->debug_vm) {
  		/* Invalidate all BOs to test for userspace bugs */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8 -2
index 7f852029d6e1..a2b12f0c3253 100644
@@ -2355,14 +2355,20 @@ amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid)
  			amdgpu_vm_get_vm_from_pasid(adev, pasid));
  }
 
-static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
+static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm, struct drm_file *filp)
  {
  	char process_name[TASK_COMM_LEN];
-	int desc_len;
+	size_t desc_len;
 
  	get_task_comm(process_name, current->group_leader);
  	desc_len = strlen(process_name);
 
+	if (filp) {
+		mutex_lock(&filp->name_lock);
+		if (filp->name)
+			desc_len += 1 + strlen(filp->name);
+	}
+
  	vm->task_info = kzalloc(
  		struct_size(vm->task_info, process_desc, desc_len + 1),
  		GFP_KERNEL);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm) · line 2380

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
@@ -2371,6 +2377,17 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
  		return -ENOMEM;
 
  	strscpy(vm->task_info->process_desc, process_name, desc_len + 1);
+	if (filp) {

I think we can drop those checks now, filp is now a mandatory argument.

Apart from that the series looks good to me.

Regards,
Christian.

Quoted 64 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +13 -2
+		if (filp->name) {
+			size_t p_len = strlen(process_name);
+
+			vm->task_info->process_desc[p_len] = '/';
+			strscpy(&vm->task_info->process_desc[p_len + 1],
+				filp->name, (desc_len + 1) - (p_len + 1));
+		}
+		mutex_unlock(&filp->name_lock);
+	}
+
 
  	kref_init(&vm->task_info->refcount);
  	return 0;
@@ -2380,11 +2397,12 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
   * amdgpu_vm_set_task_info - Sets VMs task info.
   *
   * @vm: vm for which to set the info
+ * @filp: drm_file instance
   */
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, struct drm_file *filp)
  {
  	if (!vm->task_info) {
-		if (amdgpu_vm_create_task_info(vm))
+		if (amdgpu_vm_create_task_info(vm, filp))
  			return;
  	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index 44da250217be..8df3dece54c2 100644
@@ -561,7 +561,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
  			    u32 vmid, u32 node_id, uint64_t addr, uint64_t ts,
  			    bool write_fault);
 
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, struct drm_file *filp);
 
  void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
  				struct amdgpu_vm *vm);
drivers/gpu/drm/amd/amdkfd/kfd_process.c +3 -0
index a902950cc060..e473fe433d3f 100644
@@ -1654,6 +1654,7 @@ int kfd_process_device_init_vm(struct kfd_process_device *pdd,
  			       struct file *drm_file)
  {
  	struct amdgpu_fpriv *drv_priv;
+	struct drm_file *filp;
  	struct amdgpu_vm *avm;
  	struct kfd_process *p;
  	struct dma_fence *ef;
@@ -1673,8 +1674,10 @@ int kfd_process_device_init_vm(struct kfd_process_device *pdd,
 
  	p = pdd->process;
  	dev = pdd->dev;
+	filp = drm_file->private_data;
 
  	ret = amdgpu_amdkfd_gpuvm_acquire_process_vm(dev->adev, avm,
+						     filp,
  						     &p->kgd_process_info,
  						     &ef);
  	if (ret) {