Skip to contentSkip to conversation

drm: add DRM_SET_NAME ioctl

PATCH Superseded Pierre-Eric Pelloux-Prayer 5 participants 11 messages Sep 16, 2024 – Sep 19, 2024

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 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   | 42 +++++++++++++++++++++++++++++++++++
 include/drm/drm_file.h        |  9 ++++++++
 include/uapi/drm/drm.h        | 14 ++++++++++++
 5 files changed, 78 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +8 -4
index 6b239a24f1df..b7492225ae88 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 ? 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 +42 -0

54 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 +14 -0
index 16122819edfe..fc62bb21f79e 100644
@@ -1024,6 +1024,12 @@ struct drm_crtc_queue_sequence {
 	__u64 user_data;	/* user data passed to event */
 };
 
+struct drm_set_name {
+	__u64 name_len;
+	__u64 name;
+};
+
+
 #if defined(__cplusplus)
 }
 #endif
@@ -1288,6 +1294,14 @@ 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.
+ */
+#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
#1 lore raw

PATCH v2 2/3 drm: use drm_file name in fdinfo

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
maintainer Reviewed-by on v2 2/3
#2 lore raw

Re: [PATCH v2 2/3] drm: use drm_file name in fdinfo

Am 16.09.24 um 15:32 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 3 lines

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

Quoted 37 lines
---
  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);
  }
Reviewed-by on v2 2/3
#3 lore raw

Re: [PATCH v2 2/3] drm: use drm_file name in fdinfo

In reply to Pierre-Eric Pelloux-Prayer
Quoted 30 lines · 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 +0 -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));

drivers/gpu/drm/drm_file.c void drm_show_fdinfo(struct seq_file *m, struct file *f) · line 965

Quoted 10 lines
drivers/gpu/drm/drm_file.c +5 -0
  	}
 
+	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);
  }

Regards,

Tvrtko

#4 lore raw

PATCH v2 3/3 drm/amdgpu: use drm_file name

In reply to Pierre-Eric Pelloux-Prayer

In debugfs gem_info/vm_info files, timeout handler and page fault reports.

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

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 16 +++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 25 +++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  4 +--
 5 files changed, 40 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
index 6d5fd371d5ce..1712feb2c238 100644
@@ -1577,7 +1577,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, NULL);
 
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index 1e475eb01417..d32dc547cc80 100644
@@ -310,7 +310,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
 	kvfree(chunk_array);
 
 	/* 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);
 
 	return 0;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13 -3
index 0e617dff8765..0c52168edbaf 100644
@@ -997,6 +997,10 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
 	if (r)
 		return r;
 
+	r = mutex_lock_interruptible(&file->name_lock);
+	if (r)
+		goto out;
+
 	list_for_each_entry(file, &dev->filelist, lhead) {
 		struct task_struct *task;
 		struct drm_gem_object *gobj;
@@ -1012,8 +1016,13 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
 		rcu_read_lock();
 		pid = rcu_dereference(file->pid);
 		task = pid_task(pid, PIDTYPE_TGID);
-		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
-			   task ? task->comm : "<unknown>");
+		seq_printf(m, "pid %8d command %s", pid_nr(pid),
+				   task ? task->comm : "<unknown>");
+		if (file->name) {
+			seq_putc(m, '/');
+			seq_puts(m, file->name);
+		}
+		seq_puts(m, ":\n");
 		rcu_read_unlock();
 
 		spin_lock(&file->table_lock);
@@ -1024,7 +1033,8 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
 		}
 		spin_unlock(&file->table_lock);
 	}
-
+	mutex_unlock(&file->name_lock);
+out:
 	mutex_unlock(&dev->filelist_mutex);
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +23 -2
index e20d19ae01b2..5701d74159d4 100644
@@ -2370,7 +2370,7 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
  *
  * @vm: vm for which to set the info
  */
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, struct drm_file *file)
 {
 	if (!vm->task_info)
 		return;
@@ -2385,7 +2385,28 @@ 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);
+	__get_task_comm(vm->task_info->process_name, TASK_COMM_LEN,
+			current->group_leader);
+	/* Append drm_client_name if set. */
+	if (file && file->name) {
+		mutex_lock(&file->name_lock);
+
+		/* Assert that process_name is big enough to store process_name,
+		 * so: (TASK_COMM_LEN - 1) + '/' + '\0'.
+		 * This way we can concat file->name without worrying about space.
+		 */
+		static_assert(sizeof(vm->task_info->process_name) >= TASK_COMM_LEN + 1);
+		if (file->name) {
+			int n;
+
+			n = strlen(vm->task_info->process_name);
+			vm->task_info->process_name[n] = '/';
+			strscpy_pad(&vm->task_info->process_name[n + 1],
+				    file->name,
+				    sizeof(vm->task_info->process_name) - (n + 1));
+		}
+		mutex_unlock(&file->name_lock);
+	}
 }
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +2 -2
index d12d66dca8e9..cabec384b4d4 100644
@@ -232,7 +232,7 @@ struct amdgpu_vm_pte_funcs {
 };
 
 struct amdgpu_task_info {
-	char		process_name[TASK_COMM_LEN];
+	char		process_name[NAME_MAX];
 	char		task_name[TASK_COMM_LEN];
 	pid_t		pid;
 	pid_t		tgid;
@@ -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 *file);
 
 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
 				struct amdgpu_vm *vm);
Signature · 1 line
2.40.1
maintainer
#5 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: use drm_file name

Am 16.09.24 um 15:32 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 103 lines

In debugfs gem_info/vm_info files, timeout handler and page fault reports.

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 ----------
---
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 16 +++++++++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 25 +++++++++++++++++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  4 +--
  5 files changed, 40 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
index 6d5fd371d5ce..1712feb2c238 100644
@@ -1577,7 +1577,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, NULL);
 
  	return 0;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index 1e475eb01417..d32dc547cc80 100644
@@ -310,7 +310,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
  	kvfree(chunk_array);
 
  	/* 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);
 
  	return 0;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13 -3
index 0e617dff8765..0c52168edbaf 100644
@@ -997,6 +997,10 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
  	if (r)
  		return r;
 
+	r = mutex_lock_interruptible(&file->name_lock);
+	if (r)
+		goto out;
+
  	list_for_each_entry(file, &dev->filelist, lhead) {
  		struct task_struct *task;
  		struct drm_gem_object *gobj;
@@ -1012,8 +1016,13 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
  		rcu_read_lock();
  		pid = rcu_dereference(file->pid);
  		task = pid_task(pid, PIDTYPE_TGID);
-		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
-			   task ? task->comm : "<unknown>");
+		seq_printf(m, "pid %8d command %s", pid_nr(pid),
+				   task ? task->comm : "<unknown>");
+		if (file->name) {
+			seq_putc(m, '/');
+			seq_puts(m, file->name);
+		}
+		seq_puts(m, ":\n");
  		rcu_read_unlock();
 
  		spin_lock(&file->table_lock);
@@ -1024,7 +1033,8 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
  		}
  		spin_unlock(&file->table_lock);
  	}
-
+	mutex_unlock(&file->name_lock);
+out:
  	mutex_unlock(&dev->filelist_mutex);
  	return 0;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -2
index e20d19ae01b2..5701d74159d4 100644
@@ -2370,7 +2370,7 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
   *
   * @vm: vm for which to set the info
   */
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, struct drm_file *file)
  {
  	if (!vm->task_info)
  		return;
@@ -2385,7 +2385,28 @@ 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);
+	__get_task_comm(vm->task_info->process_name, TASK_COMM_LEN,

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
+			current->group_leader);
+	/* Append drm_client_name if set. */
+	if (file && file->name) {
+		mutex_lock(&file->name_lock);
+
+		/* Assert that process_name is big enough to store process_name,
+		 * so: (TASK_COMM_LEN - 1) + '/' + '\0'.
+		 * This way we can concat file->name without worrying about space.
+		 */
+		static_assert(sizeof(vm->task_info->process_name) >= TASK_COMM_LEN + 1);

That won't work correctly.

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -0
+		if (file->name) {
+			int n;
+
+			n = strlen(vm->task_info->process_name);
+			vm->task_info->process_name[n] = '/';
+			strscpy_pad(&vm->task_info->process_name[n + 1],
+				    file->name,
+				    sizeof(vm->task_info->process_name) - (n + 1));
+		}
+		mutex_unlock(&file->name_lock);
+	}
  }
 
  /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index d12d66dca8e9..cabec384b4d4 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm_pte_funcs { · line 235

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
@@ -232,7 +232,7 @@ struct amdgpu_vm_pte_funcs {
  };
 
  struct amdgpu_task_info {
-	char		process_name[TASK_COMM_LEN];
+	char		process_name[NAME_MAX];

You would need here TASK_COMM_LEN + NAME_MAX + 1.

But I'm really wondering if we shouldn't rework amdgpu_task_info to not be based on fixed strings, but rather use a single dynamically allocated string.

Potentially even at the end of the structure after pid, tgid etc...

Regards,
Christian.

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
  	char		task_name[TASK_COMM_LEN];
  	pid_t		pid;
  	pid_t		tgid;
@@ -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 *file);
 
  void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
  				struct amdgpu_vm *vm);
#6 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: use drm_file name

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

In debugfs gem_info/vm_info files, timeout handler and page fault reports.

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 ----------
---
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 16 +++++++++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 25 +++++++++++++++++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  4 +--
  5 files changed, 40 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
index 6d5fd371d5ce..1712feb2c238 100644
@@ -1577,7 +1577,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, NULL);
 
  	return 0;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index 1e475eb01417..d32dc547cc80 100644
@@ -310,7 +310,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
  	kvfree(chunk_array);
 
  	/* 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);
 
  	return 0;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0
index 0e617dff8765..0c52168edbaf 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused) · line 1002

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -0
@@ -997,6 +997,10 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
  	if (r)
  		return r;
 
+	r = mutex_lock_interruptible(&file->name_lock);
+	if (r)
+		goto out;

Shouldn't this be in the below loop?

Quoted 54 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10 -3
+
  	list_for_each_entry(file, &dev->filelist, lhead) {
  		struct task_struct *task;
  		struct drm_gem_object *gobj;
@@ -1012,8 +1016,13 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
  		rcu_read_lock();
  		pid = rcu_dereference(file->pid);
  		task = pid_task(pid, PIDTYPE_TGID);
-		seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
-			   task ? task->comm : "<unknown>");
+		seq_printf(m, "pid %8d command %s", pid_nr(pid),
+				   task ? task->comm : "<unknown>");
+		if (file->name) {
+			seq_putc(m, '/');
+			seq_puts(m, file->name);
+		}
+		seq_puts(m, ":\n");
  		rcu_read_unlock();
 
  		spin_lock(&file->table_lock);
@@ -1024,7 +1033,8 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
  		}
  		spin_unlock(&file->table_lock);
  	}
-
+	mutex_unlock(&file->name_lock);
+out:
  	mutex_unlock(&dev->filelist_mutex);
  	return 0;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -2
index e20d19ae01b2..5701d74159d4 100644
@@ -2370,7 +2370,7 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
   *
   * @vm: vm for which to set the info
   */
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, struct drm_file *file)
  {
  	if (!vm->task_info)
  		return;
@@ -2385,7 +2385,28 @@ 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);
+	__get_task_comm(vm->task_info->process_name, TASK_COMM_LEN,
+			current->group_leader);
+	/* Append drm_client_name if set. */
+	if (file && file->name) {
+		mutex_lock(&file->name_lock);
+

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
+		/* Assert that process_name is big enough to store process_name,
+		 * so: (TASK_COMM_LEN - 1) + '/' + '\0'.
+		 * This way we can concat file->name without worrying about space.
+		 */
+		static_assert(sizeof(vm->task_info->process_name) >= TASK_COMM_LEN + 1);
+		if (file->name) {
+			int n;
+
+			n = strlen(vm->task_info->process_name);
+			vm->task_info->process_name[n] = '/';

I think runtime asserts are unavoidable here. To make sure no OOB write if someone gets creative and decreases the storage for task_info->process_name.

Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -0
+			strscpy_pad(&vm->task_info->process_name[n + 1],
+				    file->name,
+				    sizeof(vm->task_info->process_name) - (n + 1));
+		}
+		mutex_unlock(&file->name_lock);
+	}
  }
 
  /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index d12d66dca8e9..cabec384b4d4 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm_pte_funcs { · line 235

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
@@ -232,7 +232,7 @@ struct amdgpu_vm_pte_funcs {
  };
 
  struct amdgpu_task_info {
-	char		process_name[TASK_COMM_LEN];
+	char		process_name[NAME_MAX];

Like this we already know the whole string will not fit if both sides use their max allowed size. So why not size this pessimistically to include both components, slash and null termination?

Regards,

Tvrtko

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
  	char		task_name[TASK_COMM_LEN];
  	pid_t		pid;
  	pid_t		tgid;
@@ -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 *file);
 
  void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
  				struct amdgpu_vm *vm);
#7 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: use drm_file name

In reply to Pierre-Eric Pelloux-Prayer
kernel test robot noticed the following build warnings:

Hi Pierre-Eric,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm-exynos/exynos-drm-next]
[also build test WARNING on drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.11 next-20240919] [cannot apply to drm/drm-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Pierre-Eric-Pelloux-Prayer/drm-use-drm_file-name-in-fdinfo/20240916-213521
base:   https://git.kernel.org/pub/scm/linux/kernel/git/daeinki/drm-exynos.git exynos-drm-next
patch link:    https://lore.kernel.org/r/20240916133223.1023773-3-pierre-eric.pelloux-prayer%40amd.com
patch subject: [PATCH v2 3/3] drm/amdgpu: use drm_file name
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20240920/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240920/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:997:32: warning: variable 'file' is uninitialized when used here [-Wuninitialized]
     997 |         r = mutex_lock_interruptible(&file->name_lock);
         |                                       ^~~~
   include/linux/mutex.h:162:72: note: expanded from macro 'mutex_lock_interruptible'
     162 | #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
         |                                                                        ^~~~
   drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:990:23: note: initialize the variable 'file' to silence this warning
     990 |         struct drm_file *file;
         |                              ^
         |                               = NULL

1 warning generated.

vim +/file +997 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c

984
985 #if defined(CONFIG_DEBUG_FS)
986 static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
987 {
988 struct amdgpu_device *adev = m->private;
989 struct drm_device *dev = adev_to_drm(adev);
990 struct drm_file *file;
991 int r;
992
993 r = mutex_lock_interruptible(&dev->filelist_mutex);
994 if (r)
995 return r;
996

 > 997		r = mutex_lock_interruptible(&file->name_lock);

998 if (r)
999 goto out;
1000
1001 list_for_each_entry(file, &dev->filelist, lhead) {
1002 struct task_struct *task;
1003 struct drm_gem_object *gobj;
1004 struct pid *pid;
1005 int id;
1006
1007 /*
1008 * Although we have a valid reference on file->pid, that does
1009 * not guarantee that the task_struct who called get_pid() is
1010 * still alive (e.g. get_pid(current) => fork() => exit()).
1011 * Therefore, we need to protect this ->comm access using RCU.
1012 */
1013 rcu_read_lock();
1014 pid = rcu_dereference(file->pid);
1015 task = pid_task(pid, PIDTYPE_TGID);
1016 seq_printf(m, "pid %8d command %s", pid_nr(pid),
1017 task ? task->comm : "<unknown>");
1018 if (file->name) {
1019 seq_putc(m, '/');
1020 seq_puts(m, file->name);
1021 }
1022 seq_puts(m, ":\n");
1023 rcu_read_unlock();
1024
1025 spin_lock(&file->table_lock);
1026 idr_for_each_entry(&file->object_idr, gobj, id) {
1027 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1028
1029 amdgpu_bo_print_info(id, bo, m);
1030 }
1031 spin_unlock(&file->table_lock);
1032 }
1033 mutex_unlock(&file->name_lock);
1034 out:
1035 mutex_unlock(&dev->filelist_mutex);
1036 return 0;
1037 }
1038

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#8 lore raw
In reply to Pierre-Eric Pelloux-Prayer

Am 16.09.24 um 15:32 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 55 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   | 42 +++++++++++++++++++++++++++++++++++
  include/drm/drm_file.h        |  9 ++++++++
  include/uapi/drm/drm.h        | 14 ++++++++++++
  5 files changed, 78 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +4 -3
index 6b239a24f1df..b7492225ae88 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 ? priv->name : "");

BTW that can also be written as: priv->name ?: ""

And I think that is now the preferred kernel coding style, but not 100% sure.

Apart from that feel free to add Reviewed-by: Christian König <[email protected]>

Regards,
Christian.

Quoted 140 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 +42 -0
index 51f39912866f..b7d7bede0ab3 100644
@@ -540,6 +540,46 @@ 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_ptr;
+	char *new_name;
+	size_t i, len;
+
+	if (name->name_len >= NAME_MAX)
+		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);
+
+	/* Filter out control char / spaces / new lines etc in the name
+	 * since it's going to be used in dmesg or fdinfo's output.
+	 */
+	len = strlen(new_name);
+	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 +650,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 +14 -0
index 16122819edfe..fc62bb21f79e 100644
@@ -1024,6 +1024,12 @@ struct drm_crtc_queue_sequence {
  	__u64 user_data;	/* user data passed to event */
  };
 
+struct drm_set_name {
+	__u64 name_len;
+	__u64 name;
+};
+
+
  #if defined(__cplusplus)
  }
  #endif
@@ -1288,6 +1294,14 @@ 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.
+ */
+#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.
#9 lore raw
In reply to Pierre-Eric Pelloux-Prayer
Quoted 98 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   | 42 +++++++++++++++++++++++++++++++++++
  include/drm/drm_file.h        |  9 ++++++++
  include/uapi/drm/drm.h        | 14 ++++++++++++
  5 files changed, 78 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +8 -4
index 6b239a24f1df..b7492225ae88 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 ? 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 +0 -0
index 51f39912866f..b7d7bede0ab3 100644

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

Quoted 8 lines
drivers/gpu/drm/drm_ioctl.c +5 -0
@@ -540,6 +540,46 @@ 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_ptr;

__user as kernel test robot reminds us.

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

Quoted 5 lines
drivers/gpu/drm/drm_ioctl.c +5 -0
+	char *new_name;
+	size_t i, len;
+
+	if (name->name_len >= NAME_MAX)
+		return -EINVAL;

Maybe it is a bit unsubstantiated, but I am leaning towards a feeling of lets define own smaller limit, like dma-buf does. If 32 is deemed too restrictive make it larger but 255 feels unnecessary. But I don't feel to strongly about this so if people insist we need the names this long then so be it.

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

Quoted 5 lines
drivers/gpu/drm/drm_ioctl.c +5 -0
+
+	user_ptr = u64_to_user_ptr(name->name);
+
+	new_name = memdup_user_nul(user_ptr, name->name_len);
+

Nit: I'd zap this blank line since it is breaking a logical group.

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

Quoted 6 lines
drivers/gpu/drm/drm_ioctl.c +6 -0
+	if (IS_ERR(new_name))
+		return PTR_ERR(new_name);
+
+	/* Filter out control char / spaces / new lines etc in the name
+	 * since it's going to be used in dmesg or fdinfo's output.
+	 */

Nit: Preferred kernel style for multi-line comments is:

/*
* Comment
*/

Unless amdgpu preferes to stick with the above. I think I've seen both used so don't know.

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

Quoted 1 line
drivers/gpu/drm/drm_ioctl.c +1 -0
+	len = strlen(new_name);

Made me think if it is worth cross-checking against name->name_len and reject on mismatch? Probably yes.

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

Quoted 4 lines
drivers/gpu/drm/drm_ioctl.c +4 -0
+	for (i = 0; i < len; i++) {
+		if (!isgraph(new_name[i]))
+			new_name[i] = '-';
+	}

For completeness, alternative is to -EINVAL it. I have no strong opinions either way.

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

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

FWIW you could not bother allocating new_file in this case.

Regards,

Tvrtko

Quoted 70 lines
drivers/gpu/drm/drm_ioctl.c +8 -0
+	}
+	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 +650,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 +14 -0
index 16122819edfe..fc62bb21f79e 100644
@@ -1024,6 +1024,12 @@ struct drm_crtc_queue_sequence {
  	__u64 user_data;	/* user data passed to event */
  };
 
+struct drm_set_name {
+	__u64 name_len;
+	__u64 name;
+};
+
+
  #if defined(__cplusplus)
  }
  #endif
@@ -1288,6 +1294,14 @@ 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.
+ */
+#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.
#10 lore raw

Le 16/09/2024 à 16:35, Tvrtko Ursulin a écrit :

Quoted 94 lines · Pierre-Eric Pelloux-Prayer
Quoted 92 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   | 42 +++++++++++++++++++++++++++++++++++ include/drm/drm_file.h        |  9 ++++++++ include/uapi/drm/drm.h        | 14 ++++++++++++

  5 files changed, 78 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +4 -2
index 6b239a24f1df..b7492225ae88 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 ? 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 +0 -0
index 51f39912866f..b7d7bede0ab3 100644
@@ -540,6 +540,46 @@ int drm_version(struct drm_device *dev, void *data,
       return err;
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/drm_ioctl.c +5 -0
   }
+static int drm_set_name(struct drm_device *dev, void *data,
+            struct drm_file *file_priv)
+{
+    struct drm_set_name *name = data;
+    void *user_ptr;

__user as kernel test robot reminds us.

Fixed in v3.

Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/drm_ioctl.c +5 -0
+    char *new_name;
+    size_t i, len;
+
+    if (name->name_len >= NAME_MAX)
+        return -EINVAL;

Maybe it is a bit unsubstantiated, but I am leaning towards a feeling of lets define own smaller limit, like dma-buf does. If 32 is deemed too restrictive make it larger but 255 feels unnecessary. But I don't feel to strongly about this so if people insist we need the names this long then so be it.

v3 has "#define DRM_NAME_MAX_LEN 64".

Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/drm_ioctl.c +5 -0
+
+    user_ptr = u64_to_user_ptr(name->name);
+
+    new_name = memdup_user_nul(user_ptr, name->name_len);
+

Nit: I'd zap this blank line since it is breaking a logical group.

Done in v3.

Quoted 8 lines

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

Quoted 7 lines
drivers/gpu/drm/drm_ioctl.c +6 -0
+    if (IS_ERR(new_name))
+        return PTR_ERR(new_name);
+
+    /* Filter out control char / spaces / new lines etc in the name
+     * since it's going to be used in dmesg or fdinfo's output.
+     */
Quoted 7 lines

Nit: Preferred kernel style for multi-line comments is:

/*
 * Comment
 */

Unless amdgpu preferes to stick with the above. I think I've seen both used so don't know.

Done in v3.

Quoted 4 lines

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

Quoted 2 lines
drivers/gpu/drm/drm_ioctl.c +1 -0
+    len = strlen(new_name);

Made me think if it is worth cross-checking against name->name_len and reject on mismatch? Probably yes.

Added to v3.

Quoted 7 lines

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

Quoted 5 lines
drivers/gpu/drm/drm_ioctl.c +4 -0
+    for (i = 0; i < len; i++) {
+        if (!isgraph(new_name[i]))
+            new_name[i] = '-';
+    }

For completeness, alternative is to -EINVAL it. I have no strong opinions either way.

For now I've kept the filtering approach and added a comment about it in include/uapi/drm/drm.h.

Quoted 11 lines

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

Quoted 9 lines
drivers/gpu/drm/drm_ioctl.c +8 -0
+
+    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;

FWIW you could not bother allocating new_file in this case.

I've kept the code as-is, I kind of prefer it done the way it is.

Thanks for your review,
Pierre-Eric

Quoted 71 lines

Regards,

Tvrtko

Quoted 66 lines
drivers/gpu/drm/drm_ioctl.c +8 -0
+    }
+    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 +650,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..fc62bb21f79e 100644
@@ -1024,6 +1024,12 @@ struct drm_crtc_queue_sequence {

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

include/uapi/drm/drm.h +6 -0
+struct drm_set_name {
+    __u64 name_len;
+    __u64 name;
+};
+
+

  #if defined(__cplusplus)
  }
  #endif

include/uapi/drm/drm.h +8 -0
@@ -1288,6 +1294,14 @@ 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.
+ */
+#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.