Skip to contentSkip to conversation

DRM_SET_CLIENT_NAME ioctl

PATCH In review Pierre-Eric Pelloux-Prayer 4 participants 14 messages Oct 3, 2024 – Oct 9, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v5 cover DRM_SET_CLIENT_NAME ioctl

v5 of this series which is adding a new ioctl to let userspace associate a free-form name to a drm_file.

Having this name is useful when debugging or tracking resource usage; in particular when using virtio native-context driver, where a single process (say QEMU), uses 1 drm_file per-guest application.
With this change, fdinfo and other tools can map each drm_file activity to the guest application.

virglrenderer MR using this new ioctl to associate the context "debug_name" to the fd:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

Changes since v4:
* patches 1, 4 and 6 were slightly reworked based on Tvrtko' suggestions.

v4: https://lists.freedesktop.org/archives/dri-devel/2024-September/471695.html

Pierre-Eric Pelloux-Prayer (6):
drm: add DRM_SET_CLIENT_NAME ioctl
drm: use drm_file client_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        | 67 +++++++++++++------
 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                 | 14 ++--
 drivers/gpu/drm/drm_file.c                    | 10 +++
 drivers/gpu/drm/drm_ioctl.c                   | 51 ++++++++++++++
 include/drm/drm_file.h                        |  9 +++
 include/uapi/drm/drm.h                        | 17 +++++
 23 files changed, 172 insertions(+), 42 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v5 1/6 drm: add DRM_SET_CLIENT_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 client_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

If the string passed by userspace contains chars that would mess up output when it's going to be printed (in dmesg, fdinfo, etc), -EINVAL is returned.

A 0-length string is a valid use, and clears the existing name.

---
 drivers/gpu/drm/drm_debugfs.c | 14 +++++++---
 drivers/gpu/drm/drm_file.c    |  5 ++++
 drivers/gpu/drm/drm_ioctl.c   | 51 +++++++++++++++++++++++++++++++++++
 include/drm/drm_file.h        |  9 +++++++
 include/uapi/drm/drm.h        | 17 ++++++++++++
 5 files changed, 92 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_debugfs.c +10 -4
index 6b239a24f1df..5c99322a4c6f 100644
@@ -78,12 +78,14 @@ 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 %*s\n",
 		   "command",
 		   "tgid",
 		   "dev",
 		   "uid",
-		   "magic");
+		   "magic",
+		   DRM_CLIENT_NAME_MAX_LEN,
+		   "name");
 
 	/* dev->filelist is sorted youngest first, but we want to present
 	 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
@@ -94,19 +96,23 @@ static int drm_clients_info(struct seq_file *m, void *data)
 		struct task_struct *task;
 		struct pid *pid;
 
+		mutex_lock(&priv->client_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 %*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);
+			   priv->magic,
+			   DRM_CLIENT_NAME_MAX_LEN,
+			   priv->client_name ? priv->client_name : "<unset>");
 		rcu_read_unlock();
+		mutex_unlock(&priv->client_name_lock);
 	}
 	mutex_unlock(&dev->filelist_mutex);
 	return 0;
drivers/gpu/drm/drm_file.c +5 -0
index 01fde94fe2a9..64f5e15304e7 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->client_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->client_name_lock);
+	kfree(file->client_name);
+
 	kfree(file);
 }
 
drivers/gpu/drm/drm_ioctl.c +51 -0

63 lines not on this page · raw message

include/drm/drm_file.h +9 -0
index 8c0030c77308..d4f1c115ea0f 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;
+
+	/**
+	 * @client_name:
+	 *
+	 * Userspace-provided name; useful for accounting and debugging.
+	 */
+	const char *client_name;
+	/** @name_lock: Protects @client_name. */
+	struct mutex client_name_lock;
 };
 
 /**
include/uapi/drm/drm.h +17 -0
index 16122819edfe..7fba37b94401 100644
@@ -1024,6 +1024,13 @@ struct drm_crtc_queue_sequence {
 	__u64 user_data;	/* user data passed to event */
 };
 
+#define DRM_CLIENT_NAME_MAX_LEN		64
+struct drm_set_client_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_CLIENT_NAME - Attach a name to a drm_file
+ *
+ * Having a name allows for easier tracking and debugging.
+ * The length of the name (without null ending char) must be
+ * <= DRM_CLIENT_NAME_MAX_LEN.
+ * The call will fail if the name contains whitespaces or non-printable chars.
+ */
+#define DRM_IOCTL_SET_CLIENT_NAME	DRM_IOWR(0xD1, struct drm_set_client_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
#2 lore raw

PATCH v5 2/6 drm: use drm_file client_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..566e122e6e60 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_CLIENT_NAME.
+
+
 Utilization
 ^^^^^^^^^^^
 
drivers/gpu/drm/drm_file.c +5 -0
index 64f5e15304e7..1c9e03477a24 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->client_name_lock);
+	if (file->client_name)
+		drm_printf(&p, "drm-client-name:\t%s\n", file->client_name);
+	mutex_unlock(&file->client_name_lock);
+
 	if (dev->driver->show_fdinfo)
 		dev->driver->show_fdinfo(&p, file);
 }
Signature · 1 line
2.40.1
#3 lore raw

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

In reply to Pierre-Eric Pelloux-Prayer

At the point the VM is locked (through the root PDs dma_resv object), so it's safer to call amdgpu_vm_set_task_info.

The original place was not protected against concurrent access, but the risk was limited to mangled process/task name.

---
 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
#4 lore raw

PATCH v5 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 | 29 ++++++++++++--------------
 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +13 -16
index e20d19ae01b2..6cd5bd5362d4 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);
 	}
@@ -2361,6 +2361,10 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
 	if (!vm->task_info)
 		return -ENOMEM;
 
+	/* Set process attributes now. */
+	vm->task_info->tgid = current->group_leader->pid;
+	get_task_comm(vm->task_info->process_name, current->group_leader);
+
 	kref_init(&vm->task_info->refcount);
 	return 0;
 }
@@ -2372,20 +2376,16 @@ 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->pid == current->pid)
+	if (!vm->task_info) {
+		if (amdgpu_vm_create_task_info(vm))
+			return;
+	} else if (vm->task_info->pid == current->pid) {
 		return;
+	}
 
+	/* Update task attributes. */
 	vm->task_info->pid = current->pid;
 	get_task_comm(vm->task_info->task_name, current);
-
-	if (current->group_leader->mm != current->mm)
-		return;
-
-	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
 }
 
 /**
@@ -2482,10 +2482,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");
-
 	amdgpu_bo_unreserve(vm->root.bo);
 	amdgpu_bo_unref(&root_bo);
 
@@ -2608,7 +2604,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
maintainer
#5 lore raw

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

Am 03.10.24 um 14:43 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 74 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.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 29 ++++++++++++--------------
  1 file changed, 13 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -15
index e20d19ae01b2..6cd5bd5362d4 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);
  	}
@@ -2361,6 +2361,10 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
  	if (!vm->task_info)
  		return -ENOMEM;
 
+	/* Set process attributes now. */
+	vm->task_info->tgid = current->group_leader->pid;
+	get_task_comm(vm->task_info->process_name, current->group_leader);
+
  	kref_init(&vm->task_info->refcount);
  	return 0;
  }
@@ -2372,20 +2376,16 @@ 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->pid == current->pid)
+	if (!vm->task_info) {
+		if (amdgpu_vm_create_task_info(vm))
+			return;
+	} else if (vm->task_info->pid == current->pid) {
  		return;
+	}
 
+	/* Update task attributes. */
  	vm->task_info->pid = current->pid;
  	get_task_comm(vm->task_info->task_name, current);
-
-	if (current->group_leader->mm != current->mm)
-		return;
-
-	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
  }
 
  /**
@@ -2482,10 +2482,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");
-
  	amdgpu_bo_unreserve(vm->root.bo);
  	amdgpu_bo_unref(&root_bo);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) · line 2608

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -2608,7 +2604,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);

That NULL check should probably be moved into amdgpu_vm_put_task_info().

Apart from that the patch is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
  	amdgpu_vm_set_pasid(adev, vm, 0);
  	dma_fence_wait(vm->last_unlocked, false);
  	dma_fence_put(vm->last_unlocked);
Reviewed-by Christian König
#6 lore raw

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

Le 08/10/2024 à 10:13, Christian König a écrit :

Quoted 77 lines

Am 03.10.24 um 14:43 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 68 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.

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

  1 file changed, 13 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +11 -15
index e20d19ae01b2..6cd5bd5362d4 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);
       }
@@ -2361,6 +2361,10 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
       if (!vm->task_info)
           return -ENOMEM;
+    /* Set process attributes now. */
+    vm->task_info->tgid = current->group_leader->pid;
+    get_task_comm(vm->task_info->process_name, current->group_leader);
+
       kref_init(&vm->task_info->refcount);
       return 0;
   }
@@ -2372,20 +2376,16 @@ 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->pid == current->pid)
+    if (!vm->task_info) {
+        if (amdgpu_vm_create_task_info(vm))
+            return;
+    } else if (vm->task_info->pid == current->pid) {
           return;
+    }
+    /* Update task attributes. */
       vm->task_info->pid = current->pid;
       get_task_comm(vm->task_info->task_name, current);
-
-    if (current->group_leader->mm != current->mm)
-        return;
-
-    vm->task_info->tgid = current->group_leader->pid;
-    get_task_comm(vm->task_info->process_name, current->group_leader);
   }
   /**
@@ -2482,10 +2482,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");
-
       amdgpu_bo_unreserve(vm->root.bo);
       amdgpu_bo_unref(&root_bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) · line 2607

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -2608,7 +2604,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);

That NULL check should probably be moved into amdgpu_vm_put_task_info().

Quoted 1 line

Apart from that the patch is Reviewed-by: Christian König <[email protected]>.

Fixed in attached patch.

Pierre-Eric

Quoted 7 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
       amdgpu_vm_set_pasid(adev, vm, 0);
       dma_fence_wait(vm->last_unlocked, false);
       dma_fence_put(vm->last_unlocked);

From fbaf5615b6e0d9b6a080a55e51615c0216d33cf3 Mon Sep 17 00:00:00 2001
From: Pierre-Eric Pelloux-Prayer <[email protected]>
Date: Thu, 19 Sep 2024 14:39:46 +0200
Subject: [PATCH 4/6] drm/amdgpu: alloc and init vm::task_info from first
submit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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 | 29 ++++++++++++--------------
 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +13 -16
index 6005280f5f38..9cbccfabea9f 100644
@@ -2330,7 +2330,8 @@ amdgpu_vm_get_vm_from_pasid(struct amdgpu_device *adev, u32 pasid)
  */
 void amdgpu_vm_put_task_info(struct amdgpu_task_info *task_info)
 {
-	kref_put(&task_info->refcount, amdgpu_vm_destroy_task_info);
+	if (task_info)
+		kref_put(&task_info->refcount, amdgpu_vm_destroy_task_info);
 }
 
 /**
@@ -2346,7 +2347,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);
 	}
@@ -2376,6 +2377,10 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
 	if (!vm->task_info)
 		return -ENOMEM;
 
+	/* Set process attributes now. */
+	vm->task_info->tgid = current->group_leader->pid;
+	get_task_comm(vm->task_info->process_name, current->group_leader);
+
 	kref_init(&vm->task_info->refcount);
 	return 0;
 }
@@ -2387,20 +2392,16 @@ 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->pid == current->pid)
+	if (!vm->task_info) {
+		if (amdgpu_vm_create_task_info(vm))
+			return;
+	} else if (vm->task_info->pid == current->pid) {
 		return;
+	}
 
+	/* Update task attributes. */
 	vm->task_info->pid = current->pid;
 	get_task_comm(vm->task_info->task_name, current);
-
-	if (current->group_leader->mm != current->mm)
-		return;
-
-	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
 }
 
 /**
@@ -2490,10 +2491,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");
-
 	amdgpu_bo_unreserve(vm->root.bo);
 	amdgpu_bo_unref(&root_bo);
 
Signature · 1 line
2.40.1
#7 lore raw

PATCH v5 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           | 13 +++++++++++--
 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, 24 insertions(+), 15 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 +11 -2
index 6cd5bd5362d4..561ff832930e 100644
@@ -2357,13 +2357,22 @@ 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];
+	size_t pname_len;
+
+	get_task_comm(process_name, current->group_leader);
+	pname_len = strlen(process_name);
+
+	vm->task_info = kzalloc(
+		struct_size(vm->task_info, process_desc, pname_len + 1),
+		GFP_KERNEL);
+
 	if (!vm->task_info)
 		return -ENOMEM;
 
 	/* Set process attributes now. */
 	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
+	strscpy(vm->task_info->process_desc, process_name, pname_len + 1);
 
 	kref_init(&vm->task_info->refcount);
 	return 0;
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
maintainer Reviewed-by on v5 5/6
#8 lore raw

Re: [PATCH v5 5/6] drm/amdgpu: make process_name a flexible array

Am 03.10.24 um 14:43 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 4 lines

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

Quoted 207 lines
---
  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           | 13 +++++++++++--
  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, 24 insertions(+), 15 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 +11 -2
index 6cd5bd5362d4..561ff832930e 100644
@@ -2357,13 +2357,22 @@ 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];
+	size_t pname_len;
+
+	get_task_comm(process_name, current->group_leader);
+	pname_len = strlen(process_name);
+
+	vm->task_info = kzalloc(
+		struct_size(vm->task_info, process_desc, pname_len + 1),
+		GFP_KERNEL);
+
  	if (!vm->task_info)
  		return -ENOMEM;
 
  	/* Set process attributes now. */
  	vm->task_info->tgid = current->group_leader->pid;
-	get_task_comm(vm->task_info->process_name, current->group_leader);
+	strscpy(vm->task_info->process_desc, process_name, pname_len + 1);
 
  	kref_init(&vm->task_info->refcount);
  	return 0;
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);
  			}
  		}
#9 lore raw

PATCH v5 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        | 34 ++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  3 ++
 6 files changed, 34 insertions(+), 11 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 +26 -8
index 561ff832930e..920660c23e0f 100644
@@ -2355,24 +2355,41 @@ 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];
-	size_t pname_len;
+	size_t pname_len, pdesc_len;
 
 	get_task_comm(process_name, current->group_leader);
-	pname_len = strlen(process_name);
+	pdesc_len = pname_len = strlen(process_name);
+
+	mutex_lock(&filp->client_name_lock);
+	if (filp->client_name)
+		pdesc_len += strlen(filp->client_name) + 1;
+
+	/* Add 1 for the NUL char. */
+	pdesc_len += 1;
 
 	vm->task_info = kzalloc(
-		struct_size(vm->task_info, process_desc, pname_len + 1),
+		struct_size(vm->task_info, process_desc, pdesc_len),
 		GFP_KERNEL);
 
-	if (!vm->task_info)
+	if (!vm->task_info) {
+		mutex_unlock(&filp->client_name_lock);
 		return -ENOMEM;
+	}
 
 	/* Set process attributes now. */
 	vm->task_info->tgid = current->group_leader->pid;
-	strscpy(vm->task_info->process_desc, process_name, pname_len + 1);
+	strscpy(vm->task_info->process_desc, process_name, pdesc_len);
+
+	if (filp->client_name) {
+		/* Append the drm-client-name. */
+		vm->task_info->process_desc[pname_len] = '/';
+		strscpy(&vm->task_info->process_desc[pname_len + 1],
+			filp->client_name, pdesc_len - (pname_len + 1));
+	}
+	mutex_unlock(&filp->client_name_lock);
 
 	kref_init(&vm->task_info->refcount);
 	return 0;
@@ -2382,11 +2399,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;
 	} else if (vm->task_info->pid == current->pid) {
 		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
maintainer
#10 lore raw

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

Am 03.10.24 um 14:43 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 69 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        | 34 ++++++++++++++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  2 +-
  drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  3 ++
  6 files changed, 34 insertions(+), 11 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 +0 -0
index 561ff832930e..920660c23e0f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid) · line 2358

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -2355,24 +2355,41 @@ 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)

It would be nice to pass in only the const char *client_name here and lock/unlock the mutex outside of the VM code.

Only nice to have, but I would rather like to keep the drm_file internals outside of the VM code.

Regards,
Christian.

Quoted 93 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +25 -7
  {
  	char process_name[TASK_COMM_LEN];
-	size_t pname_len;
+	size_t pname_len, pdesc_len;
 
  	get_task_comm(process_name, current->group_leader);
-	pname_len = strlen(process_name);
+	pdesc_len = pname_len = strlen(process_name);
+
+	mutex_lock(&filp->client_name_lock);
+	if (filp->client_name)
+		pdesc_len += strlen(filp->client_name) + 1;
+
+	/* Add 1 for the NUL char. */
+	pdesc_len += 1;
 
  	vm->task_info = kzalloc(
-		struct_size(vm->task_info, process_desc, pname_len + 1),
+		struct_size(vm->task_info, process_desc, pdesc_len),
  		GFP_KERNEL);
 
-	if (!vm->task_info)
+	if (!vm->task_info) {
+		mutex_unlock(&filp->client_name_lock);
  		return -ENOMEM;
+	}
 
  	/* Set process attributes now. */
  	vm->task_info->tgid = current->group_leader->pid;
-	strscpy(vm->task_info->process_desc, process_name, pname_len + 1);
+	strscpy(vm->task_info->process_desc, process_name, pdesc_len);
+
+	if (filp->client_name) {
+		/* Append the drm-client-name. */
+		vm->task_info->process_desc[pname_len] = '/';
+		strscpy(&vm->task_info->process_desc[pname_len + 1],
+			filp->client_name, pdesc_len - (pname_len + 1));
+	}
+	mutex_unlock(&filp->client_name_lock);
 
  	kref_init(&vm->task_info->refcount);
  	return 0;
@@ -2382,11 +2399,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;
  	} else if (vm->task_info->pid == current->pid) {
  		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) {
Reviewed-by Christian König
#11 lore raw

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

Le 08/10/2024 à 10:24, Christian König a écrit :

Quoted 72 lines

Am 03.10.24 um 14:43 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 64 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        | 34 ++++++++++++++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  2 +-
  drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  3 ++
  6 files changed, 34 insertions(+), 11 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 +0 -0
index 561ff832930e..920660c23e0f 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid) · line 2357

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -2355,24 +2355,41 @@ 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)

It would be nice to pass in only the const char *client_name here and lock/unlock the mutex outside of the VM code.

Quoted 1 line

Only nice to have, but I would rather like to keep the drm_file internals outside of the VM code.

Fixed in the attached patch.

Pierre-Eric

Quoted 80 lines

Regards,
Christian.

Quoted 76 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -2
   {
       char process_name[TASK_COMM_LEN];
-    size_t pname_len;
+    size_t pname_len, pdesc_len;
       get_task_comm(process_name, current->group_leader);
-    pname_len = strlen(process_name);
+    pdesc_len = pname_len = strlen(process_name);
+
+    mutex_lock(&filp->client_name_lock);
+    if (filp->client_name)
+        pdesc_len += strlen(filp->client_name) + 1;
+
+    /* Add 1 for the NUL char. */
+    pdesc_len += 1;

      vm->task_info = kzalloc(
-        struct_size(vm->task_info, process_desc, pname_len + 1), +        struct_size(vm->task_info, process_desc, pdesc_len), GFP_KERNEL);
-    if (!vm->task_info)
+    if (!vm->task_info) {
+        mutex_unlock(&filp->client_name_lock);
          return -ENOMEM;
+    }
      /* Set process attributes now. */
      vm->task_info->tgid = current->group_leader->pid;
-    strscpy(vm->task_info->process_desc, process_name, pname_len + 1); +    strscpy(vm->task_info->process_desc, process_name, pdesc_len); +
+    if (filp->client_name) {
+        /* Append the drm-client-name. */
+        vm->task_info->process_desc[pname_len] = '/';
+        strscpy(&vm->task_info->process_desc[pname_len + 1], +            filp->client_name, pdesc_len - (pname_len + 1)); +    }
+    mutex_unlock(&filp->client_name_lock); kref_init(&vm->task_info->refcount);
      return 0;

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -2
@@ -2382,11 +2399,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;
       } else if (vm->task_info->pid == current->pid) {
           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 +1 -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,
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_process.c int kfd_process_device_init_vm(struct kfd_process_device *pdd, · line 1681

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
       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) {

From 38d40d25d24f94d2e5bb8b295789fb14671993e0 Mon Sep 17 00:00:00 2001
From: Pierre-Eric Pelloux-Prayer <[email protected]>
Date: Fri, 20 Sep 2024 11:06:48 +0200
Subject: [PATCH 6/6] drm/amdgpu: use drm_file::name in task_info::process_desc
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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  |  5 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  4 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 29 ++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  3 ++
 6 files changed, 34 insertions(+), 10 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 +4 -1
index ce5ca304dba9..a241e2e96c6c 100644
@@ -1552,6 +1552,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)
 {
@@ -1571,7 +1572,9 @@ int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,
 	if (ret)
 		return ret;
 
-	amdgpu_vm_set_task_info(avm);
+	mutex_lock(&filp->client_name_lock);
+	amdgpu_vm_set_task_info(avm, filp->client_name);
+	mutex_unlock(&filp->client_name_lock);
 
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -1
index 891128ecee6d..04f25a5a63f3 100644
@@ -1178,7 +1178,9 @@ 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);
+	mutex_lock(&p->filp->client_name_lock);
+	amdgpu_vm_set_task_info(vm, p->filp->client_name);
+	mutex_unlock(&p->filp->client_name_lock);
 
 	if (adev->debug_vm) {
 		/* Invalidate all BOs to test for userspace bugs */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +22 -7
index be149978447d..6363ad76d26f 100644
@@ -2371,16 +2371,23 @@ 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,
+				      const char *client_name)
 {
 	char process_name[TASK_COMM_LEN];
-	size_t pname_len;
+	size_t pname_len, pdesc_len;
 
 	get_task_comm(process_name, current->group_leader);
-	pname_len = strlen(process_name);
+	pdesc_len = pname_len = strlen(process_name);
+
+	if (client_name)
+		pdesc_len += strlen(client_name) + 1;
+
+	/* Add 1 for the NUL char. */
+	pdesc_len += 1;
 
 	vm->task_info = kzalloc(
-		struct_size(vm->task_info, process_desc, pname_len + 1),
+		struct_size(vm->task_info, process_desc, pdesc_len),
 		GFP_KERNEL);
 
 	if (!vm->task_info)
@@ -2388,7 +2395,14 @@ static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm)
 
 	/* Set process attributes now. */
 	vm->task_info->tgid = current->group_leader->pid;
-	strscpy(vm->task_info->process_desc, process_name, pname_len + 1);
+	strscpy(vm->task_info->process_desc, process_name, pdesc_len);
+
+	if (client_name) {
+		/* Append the drm-client-name. */
+		vm->task_info->process_desc[pname_len] = '/';
+		strscpy(&vm->task_info->process_desc[pname_len + 1],
+			client_name, pdesc_len - (pname_len + 1));
+	}
 
 	kref_init(&vm->task_info->refcount);
 	return 0;
@@ -2398,11 +2412,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
+ * @client_name: optional debug name attached
  */
-void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
+void amdgpu_vm_set_task_info(struct amdgpu_vm *vm, const char *client_name)
 {
 	if (!vm->task_info) {
-		if (amdgpu_vm_create_task_info(vm))
+		if (amdgpu_vm_create_task_info(vm, client_name))
 			return;
 	} else if (vm->task_info->pid == current->pid) {
 		return;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
index 8220469c05b3..10e1315bcdc6 100644
@@ -562,7 +562,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, const char *client_name);
 
 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 d07acf1b2f93..fc87966adb6a 100644
@@ -1680,6 +1680,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;
@@ -1699,8 +1700,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
maintainer
#12 lore raw
In reply to Pierre-Eric Pelloux-Prayer

Hi guys,

I've pushed the first two patches to drm-misc-next.

@Alex any objections to merge the amdgpu changes through drm-misc-next as well?

Thanks,
Christian.

Am 03.10.24 um 14:43 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 52 lines

v5 of this series which is adding a new ioctl to let userspace associate a free-form name to a drm_file.

Having this name is useful when debugging or tracking resource usage; in particular when using virtio native-context driver, where a single process (say QEMU), uses 1 drm_file per-guest application.
With this change, fdinfo and other tools can map each drm_file activity to the guest application.

virglrenderer MR using this new ioctl to associate the context "debug_name" to the fd:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1428

Changes since v4:
* patches 1, 4 and 6 were slightly reworked based on Tvrtko' suggestions.

v4: https://lists.freedesktop.org/archives/dri-devel/2024-September/471695.html

Pierre-Eric Pelloux-Prayer (6):
drm: add DRM_SET_CLIENT_NAME ioctl
drm: use drm_file client_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        | 67 +++++++++++++------
  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                 | 14 ++--
  drivers/gpu/drm/drm_file.c                    | 10 +++
  drivers/gpu/drm/drm_ioctl.c                   | 51 ++++++++++++++
  include/drm/drm_file.h                        |  9 +++
  include/uapi/drm/drm.h                        | 17 +++++
  23 files changed, 172 insertions(+), 42 deletions(-)
#13 lore raw

[Public]

Quoted 10 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, October 8, 2024 4:27 AM
To: Deucher, Alexander <[email protected]>
Cc: [email protected]; [email protected]; Pelloux-Prayer, Pierre- Eric <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 0/6] DRM_SET_CLIENT_NAME ioctl

Quoted 6 lines

Hi guys,

I've pushed the first two patches to drm-misc-next.

@Alex any objections to merge the amdgpu changes through drm-misc-next as well?

That's fine with me.

Alex

Quoted 60 lines

Thanks,
Christian.

Am 03.10.24 um 14:43 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 55 lines

v5 of this series which is adding a new ioctl to let userspace associate a free-form name to a drm_file.

Having this name is useful when debugging or tracking resource usage; in particular when using virtio native-context driver, where a single process (say QEMU), uses 1 drm_file per-guest application. With this change, fdinfo and other tools can map each drm_file activity to the guest application.

virglrenderer MR using this new ioctl to associate the context "debug_name" to the fd:
https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/14
28

Changes since v4:
* patches 1, 4 and 6 were slightly reworked based on Tvrtko' suggestions.

v4:
https://lists.freedesktop.org/archives/dri-devel/2024-September/471695
.html

Pierre-Eric Pelloux-Prayer (6):
drm: add DRM_SET_CLIENT_NAME ioctl
drm: use drm_file client_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        | 67 +++++++++++++------
  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                 | 14 ++--
  drivers/gpu/drm/drm_file.c                    | 10 +++
  drivers/gpu/drm/drm_ioctl.c                   | 51 ++++++++++++++
  include/drm/drm_file.h                        |  9 +++
  include/uapi/drm/drm.h                        | 17 +++++
  23 files changed, 172 insertions(+), 42 deletions(-)