Skip to contentSkip to conversation

drm: move debugfs functionality from drm_drv.c to drm_debugfs.c

PATCH In review Sunil Khatri 4 participants 13 messages Jun 18, 2025 – Jun 23, 2025

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

PATCH v4 1/4 drm: move debugfs functionality from drm_drv.c to drm_debugfs.c

move the functions from drm_drv.c which uses the static drm_debugfs_root as parent node in the debugfs by drm.

move this root node to the debugfs for easily handling of future requirements to add more information in the root directory and one of which is planned to have directories for each client in the root directory which is dri.

---
 drivers/gpu/drm/drm_debugfs.c  | 22 ++++++++++++++++++----
 drivers/gpu/drm/drm_drv.c      | 11 ++++-------
 drivers/gpu/drm/drm_internal.h |  6 ++----
 include/drm/drm_drv.h          | 10 ++++++++++
 4 files changed, 34 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_debugfs.c +18 -4
index 2d43bda82887..5a33ec299c04 100644
@@ -44,6 +44,8 @@
 #include "drm_crtc_internal.h"
 #include "drm_internal.h"
 
+static struct dentry *drm_debugfs_root;
+
 /***************************************************
  * Initialization, etc.
  **************************************************/
@@ -286,6 +288,16 @@ int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
 }
 EXPORT_SYMBOL(drm_debugfs_remove_files);
 
+void drm_debugfs_create_dir(void)
+{
+	drm_debugfs_root = debugfs_create_dir("dri", NULL);
+}
+
+void drm_debugfs_remove_dir(void)
+{
+	debugfs_remove(drm_debugfs_root);
+}
+
 /**
  * drm_debugfs_dev_init - create debugfs directory for the device
  * @dev: the device which we want to create the directory for
@@ -295,7 +307,10 @@ EXPORT_SYMBOL(drm_debugfs_remove_files);
  */
 void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
 {
-	dev->debugfs_root = debugfs_create_dir(dev->unique, root);
+	if (!root)
+		dev->debugfs_root = debugfs_create_dir(dev->unique, drm_debugfs_root);
+	else
+		dev->debugfs_root = debugfs_create_dir(dev->unique, root);
 }
 
 /**
@@ -322,14 +337,13 @@ void drm_debugfs_dev_register(struct drm_device *dev)
 		drm_atomic_debugfs_init(dev);
 }
 
-int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-			 struct dentry *root)
+int drm_debugfs_register(struct drm_minor *minor, int minor_id)
 {
 	struct drm_device *dev = minor->dev;
 	char name[64];
 
 	sprintf(name, "%d", minor_id);
-	minor->debugfs_symlink = debugfs_create_symlink(name, root,
+	minor->debugfs_symlink = debugfs_create_symlink(name, drm_debugfs_root,
 							dev->unique);
 
 	/* TODO: Only for compatibility with drivers */
drivers/gpu/drm/drm_drv.c +4 -7
index 17fc5dc708f4..8abc52eac8f3 100644
@@ -69,8 +69,6 @@ DEFINE_XARRAY_ALLOC(drm_minors_xa);
  */
 static bool drm_core_init_complete;
 
-static struct dentry *drm_debugfs_root;
-
 DEFINE_STATIC_SRCU(drm_unplug_srcu);
 
 /*
@@ -183,8 +181,7 @@ static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type)
 		return 0;
 
 	if (minor->type != DRM_MINOR_ACCEL) {
-		ret = drm_debugfs_register(minor, minor->index,
-					   drm_debugfs_root);
+		ret = drm_debugfs_register(minor, minor->index);
 		if (ret) {
 			DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
 			goto err_debugfs;
@@ -754,7 +751,7 @@ static int drm_dev_init(struct drm_device *dev,
 	if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
 		accel_debugfs_init(dev);
 	else
-		drm_debugfs_dev_init(dev, drm_debugfs_root);
+		drm_debugfs_dev_init(dev, NULL);
 
 	return 0;
 
@@ -1168,7 +1165,7 @@ static void drm_core_exit(void)
 	drm_panic_exit();
 	accel_core_exit();
 	unregister_chrdev(DRM_MAJOR, "drm");
-	debugfs_remove(drm_debugfs_root);
+	drm_debugfs_remove_dir();
 	drm_sysfs_destroy();
 	WARN_ON(!xa_empty(&drm_minors_xa));
 	drm_connector_ida_destroy();
@@ -1187,7 +1184,7 @@ static int __init drm_core_init(void)
 		goto error;
 	}
 
-	drm_debugfs_root = debugfs_create_dir("dri", NULL);
+	drm_debugfs_create_dir();
 
 	ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
 	if (ret < 0)
drivers/gpu/drm/drm_internal.h +2 -4
index b2b6a8e49dda..d2d8e72f32d9 100644
@@ -186,8 +186,7 @@ void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
 #if defined(CONFIG_DEBUG_FS)
 void drm_debugfs_dev_fini(struct drm_device *dev);
 void drm_debugfs_dev_register(struct drm_device *dev);
-int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-			 struct dentry *root);
+int drm_debugfs_register(struct drm_minor *minor, int minor_id);
 void drm_debugfs_unregister(struct drm_minor *minor);
 void drm_debugfs_connector_add(struct drm_connector *connector);
 void drm_debugfs_connector_remove(struct drm_connector *connector);
@@ -205,8 +204,7 @@ static inline void drm_debugfs_dev_register(struct drm_device *dev)
 {
 }
 
-static inline int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-				       struct dentry *root)
+static inline int drm_debugfs_register(struct drm_minor *minor, int minor_id)
 {
 	return 0;
 }
include/drm/drm_drv.h +10 -0
index a43d707b5f36..4e77a0c4a7f9 100644
@@ -567,10 +567,20 @@ static inline bool drm_firmware_drivers_only(void)
 
 #if defined(CONFIG_DEBUG_FS)
 void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root);
+void drm_debugfs_create_dir(void);
+void drm_debugfs_remove_dir(void);
 #else
 static inline void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
 {
 }
+
+static inline void drm_debugfs_create_dir(void)
+{
+}
+
+static inline void drm_debugfs_remove_dir(void)
+{
+}
 #endif
 
 #endif
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 2/4 drm: add debugfs support on per client-id basis

add support to add a directory for each client-id with root at the dri level. Since the clients are unique and not just related to one single drm device, so it makes more sense to add all the client based nodes with root as dri.

Also create a symlink back to the parent drm device from each client.

---
 drivers/gpu/drm/drm_debugfs.c | 32 ++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_file.c    | 10 ++++++++++
 include/drm/drm_debugfs.h     | 12 ++++++++++++
 include/drm/drm_file.h        |  7 +++++++
 4 files changed, 61 insertions(+)
drivers/gpu/drm/drm_debugfs.c +32 -0
index 5a33ec299c04..875276d5fb9f 100644
@@ -298,6 +298,38 @@ void drm_debugfs_remove_dir(void)
 	debugfs_remove(drm_debugfs_root);
 }
 
+int drm_debugfs_clients_add(struct drm_file *file)
+{
+	struct drm_device *dev;
+	char *client_dir, *symlink;
+
+	dev = file->minor->dev;
+
+	client_dir = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
+	if (!client_dir)
+		return -ENOMEM;
+
+	/* Create a debugfs directory for the client in root on drm debugfs */
+	file->debugfs_client = debugfs_create_dir(client_dir, drm_debugfs_root);
+	kfree(client_dir);
+
+	symlink = kasprintf(GFP_KERNEL, "../%s", dev->unique);
+	if (!symlink)
+		return -ENOMEM;
+
+	/* Create a link from client_id to the drm device this client id belongs to */
+	debugfs_create_symlink("device", file->debugfs_client, symlink);
+	kfree(symlink);
+
+	return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+	debugfs_remove_recursive(file->debugfs_client);
+	file->debugfs_client = NULL;
+}
+
 /**
  * drm_debugfs_dev_init - create debugfs directory for the device
  * @dev: the device which we want to create the directory for
drivers/gpu/drm/drm_file.c +10 -0
index 06ba6dcbf5ae..8502c5a630b1 100644
@@ -39,12 +39,14 @@
 #include <linux/poll.h>
 #include <linux/slab.h>
 #include <linux/vga_switcheroo.h>
+#include <linux/debugfs.h>
 
 #include <drm/drm_client_event.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_print.h>
+#include <drm/drm_debugfs.h>
 
 #include "drm_crtc_internal.h"
 #include "drm_internal.h"
@@ -143,6 +145,13 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
 	rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
 	file->minor = minor;
 
+	ret = drm_debugfs_clients_add(file);
+	if (ret) {
+		put_pid(rcu_access_pointer(file->pid));
+		kfree(file);
+		return ERR_PTR(ret);
+	}
+
 	/* for compatibility root is always authenticated */
 	file->authenticated = capable(CAP_SYS_ADMIN);
 
@@ -236,6 +245,7 @@ void drm_file_free(struct drm_file *file)
 		     atomic_read(&dev->open_count));
 
 	drm_events_release(file);
+	drm_debugfs_clients_remove(file);
 
 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
 		drm_fb_release(file);
include/drm/drm_debugfs.h +12 -0
index cf06cee4343f..4bd6cc1d0900 100644
@@ -153,6 +153,9 @@ void drm_debugfs_add_files(struct drm_device *dev,
 
 int drm_debugfs_gpuva_info(struct seq_file *m,
 			   struct drm_gpuvm *gpuvm);
+
+int drm_debugfs_clients_add(struct drm_file *file);
+void drm_debugfs_clients_remove(struct drm_file *file);
 #else
 static inline void drm_debugfs_create_files(const struct drm_info_list *files,
 					    int count, struct dentry *root,
@@ -181,6 +184,15 @@ static inline int drm_debugfs_gpuva_info(struct seq_file *m,
 {
 	return 0;
 }
+
+int drm_debugfs_clients_add(struct drm_file *file)
+{
+	return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+}
 #endif
 
 #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_file.h +7 -0
index 5c3b2aa3e69d..eab7546aad79 100644
@@ -400,6 +400,13 @@ struct drm_file {
 	 * @client_name_lock: Protects @client_name.
 	 */
 	struct mutex client_name_lock;
+
+	/**
+	 * @debugfs_client:
+	 *
+	 * debugfs directory for each client under a drm node.
+	 */
+	struct dentry *debugfs_client;
 };
 
 /**
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v4 2/4] drm: add debugfs support on per client-id basis

Quoted 27 lines · Sunil Khatri

add support to add a directory for each client-id with root at the dri level. Since the clients are unique and not just related to one single drm device, so it makes more sense to add all the client based nodes with root as dri.

Also create a symlink back to the parent drm device from each client.

---
 drivers/gpu/drm/drm_debugfs.c | 32 ++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_file.c    | 10 ++++++++++
 include/drm/drm_debugfs.h     | 12 ++++++++++++
 include/drm/drm_file.h        |  7 +++++++
 4 files changed, 61 insertions(+)
drivers/gpu/drm/drm_debugfs.c +2 -0
index 5a33ec299c04..875276d5fb9f 100644
@@ -298,6 +298,38 @@ void drm_debugfs_remove_dir(void)
 	debugfs_remove(drm_debugfs_root);
 }
 
+int drm_debugfs_clients_add(struct drm_file *file)
+{

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 312

Quoted 10 lines
drivers/gpu/drm/drm_debugfs.c +10 -0
+	struct drm_device *dev;
+	char *client_dir, *symlink;
+
+	dev = file->minor->dev;
+
+	client_dir = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
+	if (!client_dir)
+		return -ENOMEM;
+
+	/* Create a debugfs directory for the client in root on drm debugfs */

That comments on what is done, so probably not a good idea.

But we should certainly have some explanation why we do this! Probably best to add some overview under Documentation/gpu.

Apart from that this works technically for me.

Regards,
Christian.

Quoted 112 lines
drivers/gpu/drm/drm_debugfs.c +20 -0
+	file->debugfs_client = debugfs_create_dir(client_dir, drm_debugfs_root);
+	kfree(client_dir);
+
+	symlink = kasprintf(GFP_KERNEL, "../%s", dev->unique);
+	if (!symlink)
+		return -ENOMEM;
+
+	/* Create a link from client_id to the drm device this client id belongs to */
+	debugfs_create_symlink("device", file->debugfs_client, symlink);
+	kfree(symlink);
+
+	return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+	debugfs_remove_recursive(file->debugfs_client);
+	file->debugfs_client = NULL;
+}
+
 /**
  * drm_debugfs_dev_init - create debugfs directory for the device
  * @dev: the device which we want to create the directory for
drivers/gpu/drm/drm_file.c +10 -0
index 06ba6dcbf5ae..8502c5a630b1 100644
@@ -39,12 +39,14 @@
 #include <linux/poll.h>
 #include <linux/slab.h>
 #include <linux/vga_switcheroo.h>
+#include <linux/debugfs.h>
 
 #include <drm/drm_client_event.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_print.h>
+#include <drm/drm_debugfs.h>
 
 #include "drm_crtc_internal.h"
 #include "drm_internal.h"
@@ -143,6 +145,13 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
 	rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
 	file->minor = minor;
 
+	ret = drm_debugfs_clients_add(file);
+	if (ret) {
+		put_pid(rcu_access_pointer(file->pid));
+		kfree(file);
+		return ERR_PTR(ret);
+	}
+
 	/* for compatibility root is always authenticated */
 	file->authenticated = capable(CAP_SYS_ADMIN);
 
@@ -236,6 +245,7 @@ void drm_file_free(struct drm_file *file)
 		     atomic_read(&dev->open_count));
 
 	drm_events_release(file);
+	drm_debugfs_clients_remove(file);
 
 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
 		drm_fb_release(file);
include/drm/drm_debugfs.h +12 -0
index cf06cee4343f..4bd6cc1d0900 100644
@@ -153,6 +153,9 @@ void drm_debugfs_add_files(struct drm_device *dev,
 
 int drm_debugfs_gpuva_info(struct seq_file *m,
 			   struct drm_gpuvm *gpuvm);
+
+int drm_debugfs_clients_add(struct drm_file *file);
+void drm_debugfs_clients_remove(struct drm_file *file);
 #else
 static inline void drm_debugfs_create_files(const struct drm_info_list *files,
 					    int count, struct dentry *root,
@@ -181,6 +184,15 @@ static inline int drm_debugfs_gpuva_info(struct seq_file *m,
 {
 	return 0;
 }
+
+int drm_debugfs_clients_add(struct drm_file *file)
+{
+	return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+}
 #endif
 
 #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_file.h +7 -0
index 5c3b2aa3e69d..eab7546aad79 100644
@@ -400,6 +400,13 @@ struct drm_file {
 	 * @client_name_lock: Protects @client_name.
 	 */
 	struct mutex client_name_lock;
+
+	/**
+	 * @debugfs_client:
+	 *
+	 * debugfs directory for each client under a drm node.
+	 */
+	struct dentry *debugfs_client;
 };
 
 /**
#3 lore raw

Re: [PATCH v4 2/4] drm: add debugfs support on per client-id basis

In reply to Sunil Khatri
Quoted 8 lines · Sunil Khatri

add support to add a directory for each client-id with root at the dri level. Since the clients are unique and not just related to one single drm device, so it makes more sense to add all the client based nodes with root as dri.

Also create a symlink back to the parent drm device from each client.

TBH I can see an use case for both clients at DRI level and clients under DRM devices. I guess you have an use case for global and per device can be added later if it becomes needed.

Quoted 14 lines
---
  drivers/gpu/drm/drm_debugfs.c | 32 ++++++++++++++++++++++++++++++++
  drivers/gpu/drm/drm_file.c    | 10 ++++++++++
  include/drm/drm_debugfs.h     | 12 ++++++++++++
  include/drm/drm_file.h        |  7 +++++++
  4 files changed, 61 insertions(+)
drivers/gpu/drm/drm_debugfs.c +0 -0
index 5a33ec299c04..875276d5fb9f 100644

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 306

Quoted 9 lines
drivers/gpu/drm/drm_debugfs.c +6 -0
@@ -298,6 +298,38 @@ void drm_debugfs_remove_dir(void)
  	debugfs_remove(drm_debugfs_root);
  }
 
+int drm_debugfs_clients_add(struct drm_file *file)
+{
+	struct drm_device *dev;
+	char *client_dir, *symlink;
+
+	dev = file->minor->dev;

FWIW, as dev is only used once and string locals are not overlapping, you could reduce to a single local variable like char *name and re-use it. Up to you.

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 310

Quoted 4 lines
drivers/gpu/drm/drm_debugfs.c +4 -0
+
+	client_dir = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
+	if (!client_dir)
+		return -ENOMEM;

It is a bit more work, but I think a clients/ directory with numerical client id subdirs would be nicer.

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 318

Quoted 8 lines
drivers/gpu/drm/drm_debugfs.c +8 -0
+
+	/* Create a debugfs directory for the client in root on drm debugfs */
+	file->debugfs_client = debugfs_create_dir(client_dir, drm_debugfs_root);
+	kfree(client_dir);
+
+	symlink = kasprintf(GFP_KERNEL, "../%s", dev->unique);
+	if (!symlink)
+		return -ENOMEM;

Worth removing the partial construction?

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 321

Quoted 3 lines
drivers/gpu/drm/drm_debugfs.c +3 -0
+
+	/* Create a link from client_id to the drm device this client id belongs to */
+	debugfs_create_symlink("device", file->debugfs_client, symlink);

This can also fail.

Quoted 34 lines
drivers/gpu/drm/drm_debugfs.c +11 -0
+	kfree(symlink);
+
+	return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+	debugfs_remove_recursive(file->debugfs_client);
+	file->debugfs_client = NULL;
+}
+
  /**
   * drm_debugfs_dev_init - create debugfs directory for the device
   * @dev: the device which we want to create the directory for
drivers/gpu/drm/drm_file.c +2 -0
index 06ba6dcbf5ae..8502c5a630b1 100644
@@ -39,12 +39,14 @@
  #include <linux/poll.h>
  #include <linux/slab.h>
  #include <linux/vga_switcheroo.h>
+#include <linux/debugfs.h>
 
  #include <drm/drm_client_event.h>
  #include <drm/drm_drv.h>
  #include <drm/drm_file.h>
  #include <drm/drm_gem.h>
  #include <drm/drm_print.h>
+#include <drm/drm_debugfs.h>
 
  #include "drm_crtc_internal.h"
  #include "drm_internal.h"

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 148

Quoted 4 lines
drivers/gpu/drm/drm_file.c +1 -0
@@ -143,6 +145,13 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
  	rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
  	file->minor = minor;
 
+	ret = drm_debugfs_clients_add(file);

Slightly tricky part is that as soon as this runs userspace can enter debugfs. If in the future any debufs clients file is added which can dereference any of the drm_file fields not yet initialized it has the potential to explode and/or be exploited.

Hence I think to be safe the usual pattern of exposing drm_file to userspace at the end, only _after_ drm_file has been *fully* initialized.

Slightly annoying part with that might be undoing dev->driver->open() but maybe it is not that bad.

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 152

Quoted 4 lines
drivers/gpu/drm/drm_file.c +4 -0
+	if (ret) {
+		put_pid(rcu_access_pointer(file->pid));
+		kfree(file);
+		return ERR_PTR(ret);

Onion unwind already exists in the function so could have used it. (Add a new label and here simply "goto out_put_pid".) But as above we discuss tweaking the order lets see how that goes first.

Quoted 30 lines
drivers/gpu/drm/drm_file.c +3 -0
+	}
+
  	/* for compatibility root is always authenticated */
  	file->authenticated = capable(CAP_SYS_ADMIN);
 
@@ -236,6 +245,7 @@ void drm_file_free(struct drm_file *file)
  		     atomic_read(&dev->open_count));
 
  	drm_events_release(file);
+	drm_debugfs_clients_remove(file);
 
  	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  		drm_fb_release(file);
include/drm/drm_debugfs.h +3 -0
index cf06cee4343f..4bd6cc1d0900 100644
@@ -153,6 +153,9 @@ void drm_debugfs_add_files(struct drm_device *dev,
 
  int drm_debugfs_gpuva_info(struct seq_file *m,
  			   struct drm_gpuvm *gpuvm);
+
+int drm_debugfs_clients_add(struct drm_file *file);
+void drm_debugfs_clients_remove(struct drm_file *file);
  #else
  static inline void drm_debugfs_create_files(const struct drm_info_list *files,
  					    int count, struct dentry *root,
@@ -181,6 +184,15 @@ static inline int drm_debugfs_gpuva_info(struct seq_file *m,
  {
  	return 0;

include/drm/drm_debugfs.h static inline int drm_debugfs_gpuva_info(struct seq_file *m, · line 195

Quoted 10 lines
include/drm/drm_debugfs.h +9 -0
  }
+
+int drm_debugfs_clients_add(struct drm_file *file)
+{
+	return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+}

Static inline for the two above.

Quoted 8 lines
include/drm/drm_debugfs.h +0 -0
  #endif
 
  #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_file.h +0 -0
index 5c3b2aa3e69d..eab7546aad79 100644

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

Quoted 10 lines
include/drm/drm_file.h +7 -0
@@ -400,6 +400,13 @@ struct drm_file {
  	 * @client_name_lock: Protects @client_name.
  	 */
  	struct mutex client_name_lock;
+
+	/**
+	 * @debugfs_client:
+	 *
+	 * debugfs directory for each client under a drm node.
+	 */
+	struct dentry *debugfs_client;

Is it worth idefing this out if !CONFIG_DEBUG_FS?

Regards,

Tvrtko

Quoted 3 lines
include/drm/drm_file.h +0 -0
  };
 
  /**
#4 lore raw

Re: [PATCH v4 2/4] drm: add debugfs support on per client-id basis

Quoted 23 lines · Tvrtko Ursulin
Quoted 8 lines · Sunil Khatri

add support to add a directory for each client-id with root at the dri level. Since the clients are unique and not just related to one single drm device, so it makes more sense to add all the client based nodes with root as dri.

Also create a symlink back to the parent drm device from each client.

TBH I can see an use case for both clients at DRI level and clients under DRM devices. I guess you have an use case for global and per device can be added later if it becomes needed.

Quoted 9 lines

---
  drivers/gpu/drm/drm_debugfs.c | 32 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_file.c    | 10 ++++++++++ include/drm/drm_debugfs.h     | 12 ++++++++++++ include/drm/drm_file.h        |  7 +++++++

  4 files changed, 61 insertions(+)
Quoted 16 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 305

Quoted 13 lines
drivers/gpu/drm/drm_debugfs.c +5 -0
index 5a33ec299c04..875276d5fb9f 100644
@@ -298,6 +298,38 @@ void drm_debugfs_remove_dir(void)
       debugfs_remove(drm_debugfs_root);
   }
   +int drm_debugfs_clients_add(struct drm_file *file)
+{
+    struct drm_device *dev;
+    char *client_dir, *symlink;
+
+    dev = file->minor->dev;

FWIW, as dev is only used once and string locals are not overlapping, you could reduce to a single local variable like char *name and re-use it. Up to you.

Let me see what i could do with that. But yes can reduce locals.

regards

Sunil

Quoted 6 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 310

Quoted 4 lines
drivers/gpu/drm/drm_debugfs.c +4 -0
+
+    client_dir = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
+    if (!client_dir)
+        return -ENOMEM;

It is a bit more work, but I think a clients/ directory with numerical client id subdirs would be nicer.

It was with the id only first but with feedback from Christian i moved it with client-$. Also since we want it in main root directory along with nodes like 0 and 128, it makes sense to differentiate and make a clear

representation of clients.

Quoted 13 lines
Quoted 11 lines
drivers/gpu/drm/drm_debugfs.c +2 -0
+
+    /* Create a debugfs directory for the client in root on drm 

debugfs */
+    file->debugfs_client = debugfs_create_dir(client_dir, drm_debugfs_root);
+    kfree(client_dir);
+
+    symlink = kasprintf(GFP_KERNEL, "../%s", dev->unique); +    if (!symlink)
+        return -ENOMEM;

Worth removing the partial construction?

Ideally it should never fail and but yes makes sense to clean up.

Quoted 7 lines
Quoted 5 lines

+
+    /* Create a link from client_id to the drm device this client id belongs to */
+    debugfs_create_symlink("device", file->debugfs_client, symlink);

This can also fail.

sure. Noted

Quoted 40 lines
Quoted 33 lines

+    kfree(symlink);

drivers/gpu/drm/drm_debugfs.c +10 -0
+
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+    debugfs_remove_recursive(file->debugfs_client);
+    file->debugfs_client = NULL;
+}
+

  /**
   * drm_debugfs_dev_init - create debugfs directory for the device
   * @dev: the device which we want to create the directory for

drivers/gpu/drm/drm_file.c +2 -0
index 06ba6dcbf5ae..8502c5a630b1 100644
@@ -39,12 +39,14 @@
   #include <linux/poll.h>
   #include <linux/slab.h>
   #include <linux/vga_switcheroo.h>
+#include <linux/debugfs.h>
     #include <drm/drm_client_event.h>
   #include <drm/drm_drv.h>
   #include <drm/drm_file.h>
   #include <drm/drm_gem.h>
   #include <drm/drm_print.h>
+#include <drm/drm_debugfs.h>
     #include "drm_crtc_internal.h"
   #include "drm_internal.h"

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor · line 148

Quoted 4 lines
drivers/gpu/drm/drm_file.c +0 -0
@@ -143,6 +145,13 @@ struct drm_file *drm_file_alloc(struct drm_minor 
 *minor)
       rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
       file->minor = minor;
   +    ret = drm_debugfs_clients_add(file);

Slightly tricky part is that as soon as this runs userspace can enter debugfs. If in the future any debufs clients file is added which can dereference any of the drm_file fields not yet initialized it has the potential to explode and/or be exploited.

Quoted 3 lines

Hence I think to be safe the usual pattern of exposing drm_file to userspace at the end, only _after_ drm_file has been *fully* initialized.

Slightly annoying part with that might be undoing dev->driver->open() but maybe it is not that bad.

I need this before driver open as the entry is accessed in driver->open in amdgpu to add files to the directory.

So, i could see to move it just before the open but not after. Anyways if we reach till driver open surely file is fully initialized. Nothing else is done in that function after that.

Quoted 7 lines

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 152

Quoted 5 lines
drivers/gpu/drm/drm_file.c +4 -0
+    if (ret) {
+        put_pid(rcu_access_pointer(file->pid));
+        kfree(file);
+        return ERR_PTR(ret);

Onion unwind already exists in the function so could have used it. (Add a new label and here simply "goto out_put_pid".) But as above we discuss tweaking the order lets see how that goes first.

Sure.

Quoted 32 lines
Quoted 32 lines
drivers/gpu/drm/drm_file.c +2 -0
+    }
+
       /* for compatibility root is always authenticated */
       file->authenticated = capable(CAP_SYS_ADMIN);

  @@ -236,6 +245,7 @@ void drm_file_free(struct drm_file *file) atomic_read(&dev->open_count)); drm_events_release(file);
+    drm_debugfs_clients_remove(file);
        if (drm_core_check_feature(dev, DRIVER_MODESET)) { drm_fb_release(file);

include/drm/drm_debugfs.h +6 -0
index cf06cee4343f..4bd6cc1d0900 100644
@@ -153,6 +153,9 @@ void drm_debugfs_add_files(struct drm_device *dev,
     int drm_debugfs_gpuva_info(struct seq_file *m,
                  struct drm_gpuvm *gpuvm);
+
+int drm_debugfs_clients_add(struct drm_file *file);
+void drm_debugfs_clients_remove(struct drm_file *file);
   #else
   static inline void drm_debugfs_create_files(const struct drm_info_list *files,
                           int count, struct dentry *root,
@@ -181,6 +184,15 @@ static inline int drm_debugfs_gpuva_info(struct 
 seq_file *m,
   {
       return 0;
   }
+
+int drm_debugfs_clients_add(struct drm_file *file)
+{
Quoted 8 lines

include/drm/drm_debugfs.h static inline int drm_debugfs_gpuva_info(struct · line 196

Quoted 6 lines
include/drm/drm_debugfs.h +6 -0
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+}

Static inline for the two above.

Noted

Quoted 12 lines
Quoted 12 lines
include/drm/drm_debugfs.h +0 -0
   #endif
     #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_file.h +1 -0
index 5c3b2aa3e69d..eab7546aad79 100644
@@ -400,6 +400,13 @@ struct drm_file {
        * @client_name_lock: Protects @client_name.
        */
       struct mutex client_name_lock;
+
Quoted 8 lines

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

Quoted 6 lines
include/drm/drm_file.h +6 -0
+    /**
+     * @debugfs_client:
+     *
+     * debugfs directory for each client under a drm node.
+     */
+    struct dentry *debugfs_client;

Is it worth idefing this out if !CONFIG_DEBUG_FS?

Surprisingly i dont see CONFIG_DEBUG_FS used in drm much.  So keeping it same for this one variable too. Need a whole new change to keep debugfs related things under the if.

Regards
Sunil Khatri

Quoted 8 lines

Regards,

Tvrtko

Quoted 2 lines
include/drm/drm_file.h +0 -0
   };
     /**
#5 lore raw

Re: [PATCH v4 2/4] drm: add debugfs support on per client-id basis

Quoted 41 lines · Sunil
Quoted 23 lines · Tvrtko Ursulin
Quoted 8 lines · Sunil Khatri

add support to add a directory for each client-id with root at the dri level. Since the clients are unique and not just related to one single drm device, so it makes more sense to add all the client based nodes with root as dri.

Also create a symlink back to the parent drm device from each client.

TBH I can see an use case for both clients at DRI level and clients under DRM devices. I guess you have an use case for global and per device can be added later if it becomes needed.

Quoted 9 lines

---
  drivers/gpu/drm/drm_debugfs.c | 32 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_file.c    | 10 ++++++++++ include/drm/drm_debugfs.h     | 12 ++++++++++++ include/drm/drm_file.h        |  7 +++++++

  4 files changed, 61 insertions(+)
Quoted 16 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 305

Quoted 13 lines
drivers/gpu/drm/drm_debugfs.c +5 -0
index 5a33ec299c04..875276d5fb9f 100644
@@ -298,6 +298,38 @@ void drm_debugfs_remove_dir(void)
       debugfs_remove(drm_debugfs_root);
   }
   +int drm_debugfs_clients_add(struct drm_file *file)
+{
+    struct drm_device *dev;
+    char *client_dir, *symlink;
+
+    dev = file->minor->dev;

FWIW, as dev is only used once and string locals are not overlapping, you could reduce to a single local variable like char *name and re-use it. Up to you.

Quoted 1 line

Let me see what i could do with that. But yes can reduce locals.

Ok.

Quoted 3 lines

regards

Sunil

Usually when you sign people stop reading. In this case I accidentaly spotted there is more below.

Quoted 11 lines
Quoted 7 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 310

Quoted 5 lines
drivers/gpu/drm/drm_debugfs.c +4 -0
+
+    client_dir = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
+    if (!client_dir)
+        return -ENOMEM;

It is a bit more work, but I think a clients/ directory with numerical client id subdirs would be nicer.

It was with the id only first but with feedback from Christian i moved it with client-$. Also since we want it in main root directory along with nodes like 0 and 128, it makes sense to differentiate and make a clear

representation of clients.

I don't mean id only in the root dir, but add a clients subdir in the root, where clients subdir contains more subdirs for individual clients. Maybe it is personal but for me $dri_root/clients/1/something feels nicer, less cluttered and potentially easier to handle in scripts and/or code that $dri_root/client-1/something.

Quoted 63 lines
Quoted 14 lines
Quoted 4 lines
drivers/gpu/drm/drm_debugfs.c +2 -0
+
+    /* Create a debugfs directory for the client in root on drm 
Quoted 8 lines

debugfs */
+    file->debugfs_client = debugfs_create_dir(client_dir, drm_debugfs_root);
+    kfree(client_dir);
+
+    symlink = kasprintf(GFP_KERNEL, "../%s", dev->unique); +    if (!symlink)
+        return -ENOMEM;

Worth removing the partial construction?

Ideally it should never fail and but yes makes sense to clean up.

Quoted 7 lines
Quoted 5 lines

+
+    /* Create a link from client_id to the drm device this client id belongs to */
+    debugfs_create_symlink("device", file->debugfs_client, symlink);

This can also fail.

sure. Noted

Quoted 40 lines
Quoted 33 lines

+    kfree(symlink);

drivers/gpu/drm/drm_debugfs.c +10 -0
+
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+    debugfs_remove_recursive(file->debugfs_client);
+    file->debugfs_client = NULL;
+}
+

  /**
   * drm_debugfs_dev_init - create debugfs directory for the device
   * @dev: the device which we want to create the directory for

drivers/gpu/drm/drm_file.c +2 -0
index 06ba6dcbf5ae..8502c5a630b1 100644
@@ -39,12 +39,14 @@
   #include <linux/poll.h>
   #include <linux/slab.h>
   #include <linux/vga_switcheroo.h>
+#include <linux/debugfs.h>
     #include <drm/drm_client_event.h>
   #include <drm/drm_drv.h>
   #include <drm/drm_file.h>
   #include <drm/drm_gem.h>
   #include <drm/drm_print.h>
+#include <drm/drm_debugfs.h>
     #include "drm_crtc_internal.h"
   #include "drm_internal.h"

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor · line 148

Quoted 4 lines
drivers/gpu/drm/drm_file.c +0 -0
@@ -143,6 +145,13 @@ struct drm_file *drm_file_alloc(struct drm_minor 
 *minor)
       rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
       file->minor = minor;
   +    ret = drm_debugfs_clients_add(file);

Slightly tricky part is that as soon as this runs userspace can enter debugfs. If in the future any debufs clients file is added which can dereference any of the drm_file fields not yet initialized it has the potential to explode and/or be exploited.

Quoted 7 lines
Quoted 3 lines

Hence I think to be safe the usual pattern of exposing drm_file to userspace at the end, only _after_ drm_file has been *fully* initialized.

Slightly annoying part with that might be undoing dev->driver->open() but maybe it is not that bad.

I need this before driver open as the entry is accessed in driver->open in amdgpu to add files to the directory.

So, i could see to move it just before the open but not after. Anyways if we reach till driver open surely file is fully initialized. Nothing else is done in that function after that.

I guess it is fine as long as dev->driver->open() will be the only place which will be adding files. If one day DRM core decides to add some common file it will need to make things it can dereference are fully initialized.

Perhaps what makes sense today to make this more robust, and it is not hard, is to simply move drm_debugfs_clients_add to just before dev->driver->open()?

Quoted 71 lines
Quoted 8 lines

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 152

Quoted 6 lines
drivers/gpu/drm/drm_file.c +4 -0
+    if (ret) {
+        put_pid(rcu_access_pointer(file->pid));
+        kfree(file);
+        return ERR_PTR(ret);

Onion unwind already exists in the function so could have used it. (Add a new label and here simply "goto out_put_pid".) But as above we discuss tweaking the order lets see how that goes first.

Sure.

Quoted 32 lines
Quoted 32 lines
drivers/gpu/drm/drm_file.c +2 -0
+    }
+
       /* for compatibility root is always authenticated */
       file->authenticated = capable(CAP_SYS_ADMIN);

  @@ -236,6 +245,7 @@ void drm_file_free(struct drm_file *file) atomic_read(&dev->open_count)); drm_events_release(file);
+    drm_debugfs_clients_remove(file);
        if (drm_core_check_feature(dev, DRIVER_MODESET)) { drm_fb_release(file);

include/drm/drm_debugfs.h +6 -0
index cf06cee4343f..4bd6cc1d0900 100644
@@ -153,6 +153,9 @@ void drm_debugfs_add_files(struct drm_device *dev,
     int drm_debugfs_gpuva_info(struct seq_file *m,
                  struct drm_gpuvm *gpuvm);
+
+int drm_debugfs_clients_add(struct drm_file *file);
+void drm_debugfs_clients_remove(struct drm_file *file);
   #else
   static inline void drm_debugfs_create_files(const struct drm_info_list *files,
                           int count, struct dentry *root,
@@ -181,6 +184,15 @@ static inline int drm_debugfs_gpuva_info(struct 
 seq_file *m,
   {
       return 0;
   }
+
+int drm_debugfs_clients_add(struct drm_file *file)
+{
Quoted 8 lines

include/drm/drm_debugfs.h static inline int drm_debugfs_gpuva_info(struct · line 196

Quoted 6 lines
include/drm/drm_debugfs.h +6 -0
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+}

Static inline for the two above.

Noted

Quoted 12 lines
Quoted 12 lines
include/drm/drm_debugfs.h +0 -0
   #endif
     #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_file.h +1 -0
index 5c3b2aa3e69d..eab7546aad79 100644
@@ -400,6 +400,13 @@ struct drm_file {
        * @client_name_lock: Protects @client_name.
        */
       struct mutex client_name_lock;
+
Quoted 8 lines

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

Quoted 6 lines
include/drm/drm_file.h +6 -0
+    /**
+     * @debugfs_client:
+     *
+     * debugfs directory for each client under a drm node.
+     */
+    struct dentry *debugfs_client;

Is it worth idefing this out if !CONFIG_DEBUG_FS?

Quoted 1 line

Surprisingly i dont see CONFIG_DEBUG_FS used in drm much.  So keeping it same for this one variable too. Need a whole new change to keep debugfs related things under the if.

Ah struct drm_device.. I see what you mean. I guess the waste if progressively worse as the unused fields move from structs with fewer instances to ones which can be a lot more.

Regards,

Tvrtko

Quoted 12 lines

Regards
Sunil Khatri

Quoted 8 lines

Regards,

Tvrtko

Quoted 2 lines
include/drm/drm_file.h +0 -0
   };
     /**
maintainer
#6 lore raw

Re: [PATCH v4 2/4] drm: add debugfs support on per client-id basis

Quoted 13 lines · Tvrtko Ursulin
Quoted 11 lines · Sunil
Quoted 9 lines · Tvrtko Ursulin
Quoted 6 lines · Sunil Khatri

add support to add a directory for each client-id with root at the dri level. Since the clients are unique and not just related to one single drm device, so it makes more sense to add all the client based nodes with root as dri.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Also create a symlink back to the parent drm device from each client.

TBH I can see an use case for both clients at DRI level and clients under DRM devices. I guess you have an use case for global and per device can be added later if it becomes needed.

We already have a "clients" file in the driver directory giving you a list of clients who use this driver instance.

Quoted 42 lines
Quoted 26 lines
Quoted 10 lines
Quoted 10 lines

---
  drivers/gpu/drm/drm_debugfs.c | 32 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_file.c    | 10 ++++++++++ include/drm/drm_debugfs.h     | 12 ++++++++++++ include/drm/drm_file.h        |  7 +++++++

  4 files changed, 61 insertions(+)
Quoted 16 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 305

Quoted 13 lines
drivers/gpu/drm/drm_debugfs.c +5 -0
index 5a33ec299c04..875276d5fb9f 100644
@@ -298,6 +298,38 @@ void drm_debugfs_remove_dir(void)
       debugfs_remove(drm_debugfs_root);
   }
   +int drm_debugfs_clients_add(struct drm_file *file)
+{
+    struct drm_device *dev;
+    char *client_dir, *symlink;
+
+    dev = file->minor->dev;

FWIW, as dev is only used once and string locals are not overlapping, you could reduce to a single local variable like char *name and re-use it. Up to you.

Quoted 1 line

Let me see what i could do with that. But yes can reduce locals.

Ok.

Quoted 3 lines

regards

Sunil

Usually when you sign people stop reading. In this case I accidentaly spotted there is more below.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
drivers/gpu/drm/drm_debugfs.c +4 -0
+
+    client_dir = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
+    if (!client_dir)
+        return -ENOMEM;
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

It is a bit more work, but I think a clients/ directory with numerical client id subdirs would be nicer.

It was with the id only first but with feedback from Christian i moved it with client-$. Also since we want it in main root directory along with nodes like 0 and 128, it makes sense to differentiate and make a clear

representation of clients.

I don't mean id only in the root dir, but add a clients subdir in the root, where clients subdir contains more subdirs for individual clients. Maybe it is personal but for me $dri_root/clients/1/something feels nicer, less cluttered and potentially easier to handle in scripts and/or code that $dri_root/client-1/something.

I've played around with that idea as well, but then abandoned it as only an extra step. But it might indeed be nicer.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 318

Quoted 10 lines
drivers/gpu/drm/drm_debugfs.c +8 -0
+
+    /* Create a debugfs directory for the client in root on drm debugfs */
+    file->debugfs_client = debugfs_create_dir(client_dir, drm_debugfs_root);
+    kfree(client_dir);
+
+    symlink = kasprintf(GFP_KERNEL, "../%s", dev->unique);
+    if (!symlink)
+        return -ENOMEM;

Worth removing the partial construction?

Quoted 8 lines
Quoted 8 lines

Ideally it should never fail and but yes makes sense to clean up.

Quoted 6 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 321

Quoted 4 lines
drivers/gpu/drm/drm_debugfs.c +3 -0
+
+    /* Create a link from client_id to the drm device this client id belongs to */
+    debugfs_create_symlink("device", file->debugfs_client, symlink);

This can also fail.

sure. Noted

Keep in mind that the results of debugfs functions should *not* be checked and failures should *not* be fatal.

Otherwise Greg comes out and beats your code into shape :)

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
drivers/gpu/drm/drm_debugfs.c +11 -0
+    kfree(symlink);
+
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+    debugfs_remove_recursive(file->debugfs_client);
+    file->debugfs_client = NULL;
+}
+
   /**
    * drm_debugfs_dev_init - create debugfs directory for the device
    * @dev: the device which we want to create the directory for
drivers/gpu/drm/drm_file.c +1 -0
index 06ba6dcbf5ae..8502c5a630b1 100644
@@ -39,12 +39,14 @@
   #include <linux/poll.h>
   #include <linux/slab.h>
   #include <linux/vga_switcheroo.h>
+#include <linux/debugfs.h>
     #include <drm/drm_client_event.h>
   #include <drm/drm_drv.h>
   #include <drm/drm_file.h>
   #include <drm/drm_gem.h>
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 147

Quoted 8 lines
drivers/gpu/drm/drm_file.c +1 -0
   #include <drm/drm_print.h>
+#include <drm/drm_debugfs.h>
     #include "drm_crtc_internal.h"
   #include "drm_internal.h"
@@ -143,6 +145,13 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
       rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
       file->minor = minor;
   +    ret = drm_debugfs_clients_add(file);

No error handling for debugfs functions please!

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Slightly tricky part is that as soon as this runs userspace can enter debugfs. If in the future any debufs clients file is added which can dereference any of the drm_file fields not yet initialized it has the potential to explode and/or be exploited.

Hence I think to be safe the usual pattern of exposing drm_file to userspace at the end, only _after_ drm_file has been *fully* initialized.

Slightly annoying part with that might be undoing dev->driver->open() but maybe it is not that bad.

Quoted 7 lines
Quoted 3 lines

I need this before driver open as the entry is accessed in driver->open in amdgpu to add files to the directory.

So, i could see to move it just before the open but not after. Anyways if we reach till driver open surely file is fully initialized. Nothing else is done in that function after that.

I guess it is fine as long as dev->driver->open() will be the only place which will be adding files. If one day DRM core decides to add some common file it will need to make things it can dereference are fully initialized.

Perhaps what makes sense today to make this more robust, and it is not hard, is to simply move drm_debugfs_clients_add to just before dev->driver->open()?

Well the common files should always work and never crash on read, don't they? So it should be save to have them created as soon as the drm_file exists.

The question is rather how do we handle teardown? Removing the directory as soon as possible?

Regards,
Christian.

Quoted 92 lines
Quoted 71 lines
Quoted 9 lines

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 152

Quoted 7 lines
drivers/gpu/drm/drm_file.c +4 -0
+    if (ret) {
+        put_pid(rcu_access_pointer(file->pid));
+        kfree(file);
+        return ERR_PTR(ret);

Onion unwind already exists in the function so could have used it. (Add a new label and here simply "goto out_put_pid".) But as above we discuss tweaking the order lets see how that goes first.

Sure.

Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/drm_file.c +2 -0
+    }
+
       /* for compatibility root is always authenticated */
       file->authenticated = capable(CAP_SYS_ADMIN);
   @@ -236,6 +245,7 @@ void drm_file_free(struct drm_file *file)

               atomic_read(&dev->open_count)); drm_events_release(file);
+    drm_debugfs_clients_remove(file);
        if (drm_core_check_feature(dev, DRIVER_MODESET)) { drm_fb_release(file);

include/drm/drm_debugfs.h +6 -0
index cf06cee4343f..4bd6cc1d0900 100644
@@ -153,6 +153,9 @@ void drm_debugfs_add_files(struct drm_device *dev,
     int drm_debugfs_gpuva_info(struct seq_file *m,
                  struct drm_gpuvm *gpuvm);
+
+int drm_debugfs_clients_add(struct drm_file *file);
+void drm_debugfs_clients_remove(struct drm_file *file);
   #else
   static inline void drm_debugfs_create_files(const struct drm_info_list *files,
                           int count, struct dentry *root,
@@ -181,6 +184,15 @@ static inline int drm_debugfs_gpuva_info(struct seq_file *m,
   {
       return 0;
   }
+
+int drm_debugfs_clients_add(struct drm_file *file)
+{
Quoted 8 lines

include/drm/drm_debugfs.h static inline int drm_debugfs_gpuva_info(struct seq_file *m, · line 195

Quoted 6 lines
include/drm/drm_debugfs.h +6 -0
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+}

Static inline for the two above.

Noted

Quoted 12 lines
Quoted 12 lines
include/drm/drm_debugfs.h +0 -0
   #endif
     #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_file.h +1 -0
index 5c3b2aa3e69d..eab7546aad79 100644
@@ -400,6 +400,13 @@ struct drm_file {
        * @client_name_lock: Protects @client_name.
        */
       struct mutex client_name_lock;
+
Quoted 8 lines

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

Quoted 6 lines
include/drm/drm_file.h +6 -0
+    /**
+     * @debugfs_client:
+     *
+     * debugfs directory for each client under a drm node.
+     */
+    struct dentry *debugfs_client;

Is it worth idefing this out if !CONFIG_DEBUG_FS?

Quoted 1 line

Surprisingly i dont see CONFIG_DEBUG_FS used in drm much.  So keeping it same for this one variable too. Need a whole new change to keep debugfs related things under the if.

Ah struct drm_device.. I see what you mean. I guess the waste if progressively worse as the unused fields move from structs with fewer instances to ones which can be a lot more.

Regards,

Tvrtko

Quoted 12 lines

Regards
Sunil Khatri

Quoted 8 lines

Regards,

Tvrtko

Quoted 2 lines
include/drm/drm_file.h +0 -0
   };
     /**
#7 lore raw

Re: [PATCH v4 2/4] drm: add debugfs support on per client-id basis

Quoted 14 lines · Christian König
Quoted 13 lines · Tvrtko Ursulin
Quoted 11 lines · Sunil
Quoted 9 lines · Tvrtko Ursulin
Quoted 6 lines · Sunil Khatri

add support to add a directory for each client-id with root at the dri level. Since the clients are unique and not just related to one single drm device, so it makes more sense to add all the client based nodes with root as dri.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Also create a symlink back to the parent drm device from each client.

TBH I can see an use case for both clients at DRI level and clients under DRM devices. I guess you have an use case for global and per device can be added later if it becomes needed.

We already have a "clients" file in the driver directory giving you a list of clients who use this driver instance.

Okay, that won't work then.

Quoted 68 lines
Quoted 40 lines
Quoted 24 lines
Quoted 8 lines
Quoted 8 lines
---
   drivers/gpu/drm/drm_debugfs.c | 32 ++++++++++++++++++++++++++++++++
   drivers/gpu/drm/drm_file.c    | 10 ++++++++++
   include/drm/drm_debugfs.h     | 12 ++++++++++++
   include/drm/drm_file.h        |  7 +++++++
   4 files changed, 61 insertions(+)
Quoted 16 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 305

Quoted 13 lines
drivers/gpu/drm/drm_debugfs.c +5 -0
index 5a33ec299c04..875276d5fb9f 100644
@@ -298,6 +298,38 @@ void drm_debugfs_remove_dir(void)
       debugfs_remove(drm_debugfs_root);
   }
   +int drm_debugfs_clients_add(struct drm_file *file)
+{
+    struct drm_device *dev;
+    char *client_dir, *symlink;
+
+    dev = file->minor->dev;

FWIW, as dev is only used once and string locals are not overlapping, you could reduce to a single local variable like char *name and re-use it. Up to you.

Quoted 1 line

Let me see what i could do with that. But yes can reduce locals.

Ok.

Quoted 3 lines

regards

Sunil

Usually when you sign people stop reading. In this case I accidentaly spotted there is more below.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
drivers/gpu/drm/drm_debugfs.c +4 -0
+
+    client_dir = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
+    if (!client_dir)
+        return -ENOMEM;
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

It is a bit more work, but I think a clients/ directory with numerical client id subdirs would be nicer.

It was with the id only first but with feedback from Christian i moved it with client-$. Also since we want it in main root directory along with nodes like 0 and 128, it makes sense to differentiate and make a clear

representation of clients.

I don't mean id only in the root dir, but add a clients subdir in the root, where clients subdir contains more subdirs for individual clients. Maybe it is personal but for me $dri_root/clients/1/something feels nicer, less cluttered and potentially easier to handle in scripts and/or code that $dri_root/client-1/something.

I've played around with that idea as well, but then abandoned it as only an extra step. But it might indeed be nicer.

Quoted 18 lines
Quoted 18 lines
Quoted 12 lines

drivers/gpu/drm/drm_debugfs.c void drm_debugfs_remove_dir(void) · line 318

Quoted 10 lines
drivers/gpu/drm/drm_debugfs.c +8 -0
+
+    /* Create a debugfs directory for the client in root on drm debugfs */
+    file->debugfs_client = debugfs_create_dir(client_dir, drm_debugfs_root);
+    kfree(client_dir);
+
+    symlink = kasprintf(GFP_KERNEL, "../%s", dev->unique);
+    if (!symlink)
+        return -ENOMEM;

Worth removing the partial construction?

Ideally it should never fail and but yes makes sense to clean up.

Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/drm_debugfs.c +3 -0
+
+    /* Create a link from client_id to the drm device this client id belongs to */
+    debugfs_create_symlink("device", file->debugfs_client, symlink);
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

This can also fail.

sure. Noted

Keep in mind that the results of debugfs functions should *not* be checked and failures should *not* be fatal.

Otherwise Greg comes out and beats your code into shape :)

Oops, my bad!

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
drivers/gpu/drm/drm_debugfs.c +11 -0
+    kfree(symlink);
+
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+    debugfs_remove_recursive(file->debugfs_client);
+    file->debugfs_client = NULL;
+}
+
   /**
    * drm_debugfs_dev_init - create debugfs directory for the device
    * @dev: the device which we want to create the directory for
drivers/gpu/drm/drm_file.c +2 -0
index 06ba6dcbf5ae..8502c5a630b1 100644
@@ -39,12 +39,14 @@
   #include <linux/poll.h>
   #include <linux/slab.h>
   #include <linux/vga_switcheroo.h>
+#include <linux/debugfs.h>
     #include <drm/drm_client_event.h>
   #include <drm/drm_drv.h>
   #include <drm/drm_file.h>
   #include <drm/drm_gem.h>
   #include <drm/drm_print.h>
+#include <drm/drm_debugfs.h>
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 147

Quoted 6 lines
drivers/gpu/drm/drm_file.c +0 -0
     #include "drm_crtc_internal.h"
   #include "drm_internal.h"
@@ -143,6 +145,13 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
       rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
       file->minor = minor;
   +    ret = drm_debugfs_clients_add(file);

No error handling for debugfs functions please!

Hmm but then any userspace will have a hard time figuring out what clients exist.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines

Slightly tricky part is that as soon as this runs userspace can enter debugfs. If in the future any debufs clients file is added which can dereference any of the drm_file fields not yet initialized it has the potential to explode and/or be exploited.

Hence I think to be safe the usual pattern of exposing drm_file to userspace at the end, only _after_ drm_file has been *fully* initialized.

Slightly annoying part with that might be undoing dev->driver->open() but maybe it is not that bad.

I need this before driver open as the entry is accessed in driver->open in amdgpu to add files to the directory.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

So, i could see to move it just before the open but not after. Anyways if we reach till driver open surely file is fully initialized. Nothing else is done in that function after that.

I guess it is fine as long as dev->driver->open() will be the only place which will be adding files. If one day DRM core decides to add some common file it will need to make things it can dereference are fully initialized.

Perhaps what makes sense today to make this more robust, and it is not hard, is to simply move drm_debugfs_clients_add to just before dev->driver->open()?

Well the common files should always work and never crash on read, don't they? So it should be save to have them created as soon as the drm_file exists.

Yes they should not. :)

My concern is one day someone adds a common DRM file in here which maybe accesses file->client_name_lock. So the debugfs register must not be before the mutex_init.

Second option is a common file which would call some DRM helper, which in turn could dereference into driver ops, where those driver ops assume dev->driver->open() has already set up the relevant state.

For now it is theoretical but if any of the two happens it would be a timing sensitive null ptr deref, or worse.

Whether or not it is worth protecting against that at this stage I am not sure. One way would be two stage setup. First set up the client dir, then after dev->driver->open call a new hook into the driver to set up the files under it.

Quoted 1 line

The question is rather how do we handle teardown? Removing the directory as soon as possible?

You think the placent in the patch is too late?

Regards,

Tvrtko

Quoted 91 lines
Quoted 90 lines
Quoted 69 lines
Quoted 7 lines

drivers/gpu/drm/drm_file.c struct drm_file *drm_file_alloc(struct drm_minor *minor) · line 152

Quoted 5 lines
drivers/gpu/drm/drm_file.c +4 -0
+    if (ret) {
+        put_pid(rcu_access_pointer(file->pid));
+        kfree(file);
+        return ERR_PTR(ret);

Onion unwind already exists in the function so could have used it. (Add a new label and here simply "goto out_put_pid".) But as above we discuss tweaking the order lets see how that goes first.

Sure.

Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/drm_file.c +3 -0
+    }
+
       /* for compatibility root is always authenticated */
       file->authenticated = capable(CAP_SYS_ADMIN);
   @@ -236,6 +245,7 @@ void drm_file_free(struct drm_file *file)
                atomic_read(&dev->open_count));
         drm_events_release(file);
+    drm_debugfs_clients_remove(file);
         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
           drm_fb_release(file);
include/drm/drm_debugfs.h +6 -0
index cf06cee4343f..4bd6cc1d0900 100644
@@ -153,6 +153,9 @@ void drm_debugfs_add_files(struct drm_device *dev,
     int drm_debugfs_gpuva_info(struct seq_file *m,
                  struct drm_gpuvm *gpuvm);
+
+int drm_debugfs_clients_add(struct drm_file *file);
+void drm_debugfs_clients_remove(struct drm_file *file);
   #else
   static inline void drm_debugfs_create_files(const struct drm_info_list *files,
                           int count, struct dentry *root,
@@ -181,6 +184,15 @@ static inline int drm_debugfs_gpuva_info(struct seq_file *m,
   {
       return 0;
   }
+
+int drm_debugfs_clients_add(struct drm_file *file)
+{
Quoted 8 lines

include/drm/drm_debugfs.h static inline int drm_debugfs_gpuva_info(struct seq_file *m, · line 195

Quoted 6 lines
include/drm/drm_debugfs.h +6 -0
+    return 0;
+}
+
+void drm_debugfs_clients_remove(struct drm_file *file)
+{
+}

Static inline for the two above.

Noted

Quoted 12 lines
Quoted 12 lines
include/drm/drm_debugfs.h +0 -0
   #endif
     #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_file.h +1 -0
index 5c3b2aa3e69d..eab7546aad79 100644
@@ -400,6 +400,13 @@ struct drm_file {
        * @client_name_lock: Protects @client_name.
        */
       struct mutex client_name_lock;
+
Quoted 8 lines

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

Quoted 6 lines
include/drm/drm_file.h +6 -0
+    /**
+     * @debugfs_client:
+     *
+     * debugfs directory for each client under a drm node.
+     */
+    struct dentry *debugfs_client;

Is it worth idefing this out if !CONFIG_DEBUG_FS?

Quoted 1 line

Surprisingly i dont see CONFIG_DEBUG_FS used in drm much.  So keeping it same for this one variable too. Need a whole new change to keep debugfs related things under the if.

Ah struct drm_device.. I see what you mean. I guess the waste if progressively worse as the unused fields move from structs with fewer instances to ones which can be a lot more.

Regards,

Tvrtko

Quoted 12 lines

Regards
Sunil Khatri

Quoted 8 lines

Regards,

Tvrtko

Quoted 2 lines
include/drm/drm_file.h +0 -0
   };
     /**
#8 lore raw

PATCH v4 3/4 drm/amdgpu: add debugfs support for VM pagetable per client

In reply to Sunil Khatri

Each drm node is associated with a unique client-id. Create a directory for each drm-file in the dri root directory. This directory is unique to hold information related to a client id which is unique in the system irrespective of how many drm devices are on the system.

Adding root page table base address of the VM under the client-id node along with the process information in debugfs.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 58 ++++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  |  4 +-
 3 files changed, 61 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1 -1
index d2ce7d86dbc8..aa912168fd68 100644
@@ -1395,7 +1395,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	if (r)
 		goto error_pasid;
 
-	r = amdgpu_vm_init(adev, &fpriv->vm, fpriv->xcp_id);
+	r = amdgpu_vm_init(adev, &fpriv->vm, fpriv->xcp_id, file_priv);
 	if (r)
 		goto error_pasid;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +57 -1

82 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -1
index f3ad687125ad..555afaf867c4 100644
@@ -487,7 +487,9 @@ int amdgpu_vm_set_pasid(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			u32 pasid);
 
 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout);
-int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, int32_t xcp_id);
+int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, int32_t xcp_id,
+		   struct drm_file *file);
+
 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec,
Signature · 1 line
2.34.1
#9 lore raw

PATCH v4 4/4 drm/amdgpu: add support of debugfs for mqd information

In reply to Sunil Khatri

add mqd support based on queue of for each client-id so the gpu address could be used to dump the mqd.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 50 +++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +50 -0
index 295e7186e156..50b9df9caf00 100644
@@ -343,6 +343,46 @@ static int amdgpu_userq_priority_permit(struct drm_file *filp,
 	return -EACCES;
 }
 
+#if defined(CONFIG_DEBUG_FS)
+static int amdgpu_mqd_info_read(struct seq_file *m, void *unused)
+{
+	struct amdgpu_usermode_queue *queue = (struct amdgpu_usermode_queue *)m->private;
+	struct amdgpu_bo *bo;
+	int r;
+
+	if (!queue || !queue->mqd.obj)
+		return -EINVAL;
+
+	bo = amdgpu_bo_ref(queue->mqd.obj);
+	r = amdgpu_bo_reserve(bo, true);
+	if (r) {
+		amdgpu_bo_unref(&bo);
+		return 0;
+	}
+
+	seq_printf(m, "queue_type %d\n", queue->queue_type);
+	seq_printf(m, "mqd_gpu_address: 0x%llx\n", amdgpu_bo_gpu_offset(queue->mqd.obj));
+
+	amdgpu_bo_unreserve(bo);
+	amdgpu_bo_unref(&bo);
+
+	return 0;
+}
+
+static int amdgpu_mqd_info_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, amdgpu_mqd_info_read, inode->i_private);
+}
+
+static const struct file_operations amdgpu_mqd_info_fops = {
+	.owner = THIS_MODULE,
+	.open = amdgpu_mqd_info_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = single_release,
+};
+#endif
+
 static int
 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 {
@@ -352,6 +392,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	const struct amdgpu_userq_funcs *uq_funcs;
 	struct amdgpu_usermode_queue *queue;
 	struct amdgpu_db_info db_info;
+	char *queue_name;
+	struct dentry *debugfs_queue;
 	bool skip_map_queue;
 	uint64_t index;
 	int qid, r = 0;
@@ -475,6 +517,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		}
 	}
 
+	queue_name = kasprintf(GFP_KERNEL, "queue-%d", qid);
+	if (!queue_name)
+		return -ENOMEM;
+
+	/* Create a debugfs directory for the client in root on drm debugfs */
+	debugfs_queue = debugfs_create_dir(queue_name, filp->debugfs_client);
+	debugfs_create_file("mqd_info", 0444, debugfs_queue, queue, &amdgpu_mqd_info_fops);
+	kfree(queue_name);
 
 	args->out.queue_id = qid;
 
Signature · 1 line
2.34.1
maintainer
#10 lore raw
In reply to Sunil Khatri
Quoted 32 lines · Sunil Khatri

move the functions from drm_drv.c which uses the static drm_debugfs_root as parent node in the debugfs by drm.

move this root node to the debugfs for easily handling of future requirements to add more information in the root directory and one of which is planned to have directories for each client in the root directory which is dri.

---
 drivers/gpu/drm/drm_debugfs.c  | 22 ++++++++++++++++++----
 drivers/gpu/drm/drm_drv.c      | 11 ++++-------
 drivers/gpu/drm/drm_internal.h |  6 ++----
 include/drm/drm_drv.h          | 10 ++++++++++
 4 files changed, 34 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_debugfs.c +2 -0
index 2d43bda82887..5a33ec299c04 100644
@@ -44,6 +44,8 @@
 #include "drm_crtc_internal.h"
 #include "drm_internal.h"
 
+static struct dentry *drm_debugfs_root;
+
 /***************************************************
  * Initialization, etc.
  **************************************************/

drivers/gpu/drm/drm_debugfs.c int drm_debugfs_remove_files(const struct drm_info_list *files, int count, · line 291

Quoted 4 lines
drivers/gpu/drm/drm_debugfs.c +1 -0
@@ -286,6 +288,16 @@ int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
 }
 EXPORT_SYMBOL(drm_debugfs_remove_files);
 
+void drm_debugfs_create_dir(void)

I think we need a better name for this. drm_debugfs_init_root maybe? Ideas welcome.

Quoted 13 lines
drivers/gpu/drm/drm_debugfs.c +9 -0
+{
+	drm_debugfs_root = debugfs_create_dir("dri", NULL);
+}
+
+void drm_debugfs_remove_dir(void)
+{
+	debugfs_remove(drm_debugfs_root);
+}
+
 /**
  * drm_debugfs_dev_init - create debugfs directory for the device
  * @dev: the device which we want to create the directory for

drivers/gpu/drm/drm_debugfs.c EXPORT_SYMBOL(drm_debugfs_remove_files); · line 313

Quoted 8 lines
drivers/gpu/drm/drm_debugfs.c +4 -1
@@ -295,7 +307,10 @@ EXPORT_SYMBOL(drm_debugfs_remove_files);
  */
 void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
 {
-	dev->debugfs_root = debugfs_create_dir(dev->unique, root);
+	if (!root)
+		dev->debugfs_root = debugfs_create_dir(dev->unique, drm_debugfs_root);
+	else
+		dev->debugfs_root = debugfs_create_dir(dev->unique, root);

Ah! That is also used from the accel subsystem, isn't it?

Probably best to move accel_debugfs_root into this here as well and distinct based on drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL) where to create the debugfs directory.

We probably need the same distinction for other use cases as well, so probably best to create a helper function for that.

Apart from that looks really good to me.

Regards,
Christian.

Quoted 119 lines
drivers/gpu/drm/drm_debugfs.c +2 -3
 }
 
 /**
@@ -322,14 +337,13 @@ void drm_debugfs_dev_register(struct drm_device *dev)
 		drm_atomic_debugfs_init(dev);
 }
 
-int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-			 struct dentry *root)
+int drm_debugfs_register(struct drm_minor *minor, int minor_id)
 {
 	struct drm_device *dev = minor->dev;
 	char name[64];
 
 	sprintf(name, "%d", minor_id);
-	minor->debugfs_symlink = debugfs_create_symlink(name, root,
+	minor->debugfs_symlink = debugfs_create_symlink(name, drm_debugfs_root,
 							dev->unique);
 
 	/* TODO: Only for compatibility with drivers */
drivers/gpu/drm/drm_drv.c +4 -7
index 17fc5dc708f4..8abc52eac8f3 100644
@@ -69,8 +69,6 @@ DEFINE_XARRAY_ALLOC(drm_minors_xa);
  */
 static bool drm_core_init_complete;
 
-static struct dentry *drm_debugfs_root;
-
 DEFINE_STATIC_SRCU(drm_unplug_srcu);
 
 /*
@@ -183,8 +181,7 @@ static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type)
 		return 0;
 
 	if (minor->type != DRM_MINOR_ACCEL) {
-		ret = drm_debugfs_register(minor, minor->index,
-					   drm_debugfs_root);
+		ret = drm_debugfs_register(minor, minor->index);
 		if (ret) {
 			DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
 			goto err_debugfs;
@@ -754,7 +751,7 @@ static int drm_dev_init(struct drm_device *dev,
 	if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
 		accel_debugfs_init(dev);
 	else
-		drm_debugfs_dev_init(dev, drm_debugfs_root);
+		drm_debugfs_dev_init(dev, NULL);
 
 	return 0;
 
@@ -1168,7 +1165,7 @@ static void drm_core_exit(void)
 	drm_panic_exit();
 	accel_core_exit();
 	unregister_chrdev(DRM_MAJOR, "drm");
-	debugfs_remove(drm_debugfs_root);
+	drm_debugfs_remove_dir();
 	drm_sysfs_destroy();
 	WARN_ON(!xa_empty(&drm_minors_xa));
 	drm_connector_ida_destroy();
@@ -1187,7 +1184,7 @@ static int __init drm_core_init(void)
 		goto error;
 	}
 
-	drm_debugfs_root = debugfs_create_dir("dri", NULL);
+	drm_debugfs_create_dir();
 
 	ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
 	if (ret < 0)
drivers/gpu/drm/drm_internal.h +2 -4
index b2b6a8e49dda..d2d8e72f32d9 100644
@@ -186,8 +186,7 @@ void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
 #if defined(CONFIG_DEBUG_FS)
 void drm_debugfs_dev_fini(struct drm_device *dev);
 void drm_debugfs_dev_register(struct drm_device *dev);
-int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-			 struct dentry *root);
+int drm_debugfs_register(struct drm_minor *minor, int minor_id);
 void drm_debugfs_unregister(struct drm_minor *minor);
 void drm_debugfs_connector_add(struct drm_connector *connector);
 void drm_debugfs_connector_remove(struct drm_connector *connector);
@@ -205,8 +204,7 @@ static inline void drm_debugfs_dev_register(struct drm_device *dev)
 {
 }
 
-static inline int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-				       struct dentry *root)
+static inline int drm_debugfs_register(struct drm_minor *minor, int minor_id)
 {
 	return 0;
 }
include/drm/drm_drv.h +10 -0
index a43d707b5f36..4e77a0c4a7f9 100644
@@ -567,10 +567,20 @@ static inline bool drm_firmware_drivers_only(void)
 
 #if defined(CONFIG_DEBUG_FS)
 void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root);
+void drm_debugfs_create_dir(void);
+void drm_debugfs_remove_dir(void);
 #else
 static inline void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
 {
 }
+
+static inline void drm_debugfs_create_dir(void)
+{
+}
+
+static inline void drm_debugfs_remove_dir(void)
+{
+}
 #endif
 
 #endif
#11 lore raw
Quoted 36 lines · Christian König
Quoted 35 lines · Sunil Khatri

move the functions from drm_drv.c which uses the static drm_debugfs_root as parent node in the debugfs by drm.

move this root node to the debugfs for easily handling of future requirements to add more information in the root directory and one of which is planned to have directories for each client in the root directory which is dri.

---
  drivers/gpu/drm/drm_debugfs.c  | 22 ++++++++++++++++++----
  drivers/gpu/drm/drm_drv.c      | 11 ++++-------
  drivers/gpu/drm/drm_internal.h |  6 ++----
  include/drm/drm_drv.h          | 10 ++++++++++
  4 files changed, 34 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_debugfs.c +2 -0
index 2d43bda82887..5a33ec299c04 100644
@@ -44,6 +44,8 @@
  #include "drm_crtc_internal.h"
  #include "drm_internal.h"
 
+static struct dentry *drm_debugfs_root;
+
  /***************************************************
   * Initialization, etc.
   **************************************************/
@@ -286,6 +288,16 @@ int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
  }
  EXPORT_SYMBOL(drm_debugfs_remove_files);
 
Quoted 2 lines

drivers/gpu/drm/drm_debugfs.c int drm_debugfs_remove_files(const struct drm_info_list *files, int count, · line 291

Quoted 1 line
drivers/gpu/drm/drm_debugfs.c +1 -0
+void drm_debugfs_create_dir(void)

I think we need a better name for this. drm_debugfs_init_root maybe? Ideas welcome.

Sounds good to me.

Regards
Sunil Khatri

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/drm_debugfs.c +9 -0
+{
+	drm_debugfs_root = debugfs_create_dir("dri", NULL);
+}
+
+void drm_debugfs_remove_dir(void)
+{
+	debugfs_remove(drm_debugfs_root);
+}
+
  /**
   * drm_debugfs_dev_init - create debugfs directory for the device
   * @dev: the device which we want to create the directory for
@@ -295,7 +307,10 @@ EXPORT_SYMBOL(drm_debugfs_remove_files);
   */
Quoted 8 lines

drivers/gpu/drm/drm_debugfs.c EXPORT_SYMBOL(drm_debugfs_remove_files); · line 313

Quoted 7 lines
drivers/gpu/drm/drm_debugfs.c +4 -1
  void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
  {
-	dev->debugfs_root = debugfs_create_dir(dev->unique, root);
+	if (!root)
+		dev->debugfs_root = debugfs_create_dir(dev->unique, drm_debugfs_root);
+	else
+		dev->debugfs_root = debugfs_create_dir(dev->unique, root);

Ah! That is also used from the accel subsystem, isn't it?

Yes

Quoted 4 lines

Probably best to move accel_debugfs_root into this here as well and distinct based on drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL) where to create the debugfs directory.

We probably need the same distinction for other use cases as well, so probably best to create a helper function for that.

I did explore that accel_debugfs_init in file drivers/accel/drm_accel.c and i dont think of a clear and acceptable design. It is a different module all together and we dont want to create debugfs directory for it in drm code. If the module fail for some reason and also removing the debugfs directory is another problem. We do not know when we want to init/exit the module.

The approach that i used is that drm_debugfs_dev_init is used by other module(right now there is one but we never know in future) as well as drm itself. The parent node is passed by the caller and for drm itself the parent node is defined statically.  So with the approach i took is if parent node is NULL its an internal call within drm and we can directly use the the static dentry i.e drm_debugfs_root.

Can we move below call from accel_core_init to drm_debugfs.c although its a different module all together for accel ??

accel_debugfs_root = debugfs_create_dir("accel", NULL);

Quoted 125 lines

Apart from that looks really good to me.

Regards,
Christian.

Quoted 119 lines
drivers/gpu/drm/drm_debugfs.c +2 -3
  }
 
  /**
@@ -322,14 +337,13 @@ void drm_debugfs_dev_register(struct drm_device *dev)
  		drm_atomic_debugfs_init(dev);
  }
 
-int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-			 struct dentry *root)
+int drm_debugfs_register(struct drm_minor *minor, int minor_id)
  {
  	struct drm_device *dev = minor->dev;
  	char name[64];
 
  	sprintf(name, "%d", minor_id);
-	minor->debugfs_symlink = debugfs_create_symlink(name, root,
+	minor->debugfs_symlink = debugfs_create_symlink(name, drm_debugfs_root,
  							dev->unique);
 
  	/* TODO: Only for compatibility with drivers */
drivers/gpu/drm/drm_drv.c +4 -7
index 17fc5dc708f4..8abc52eac8f3 100644
@@ -69,8 +69,6 @@ DEFINE_XARRAY_ALLOC(drm_minors_xa);
   */
  static bool drm_core_init_complete;
 
-static struct dentry *drm_debugfs_root;
-
  DEFINE_STATIC_SRCU(drm_unplug_srcu);
 
  /*
@@ -183,8 +181,7 @@ static int drm_minor_register(struct drm_device *dev, enum drm_minor_type type)
  		return 0;
 
  	if (minor->type != DRM_MINOR_ACCEL) {
-		ret = drm_debugfs_register(minor, minor->index,
-					   drm_debugfs_root);
+		ret = drm_debugfs_register(minor, minor->index);
  		if (ret) {
  			DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  			goto err_debugfs;
@@ -754,7 +751,7 @@ static int drm_dev_init(struct drm_device *dev,
  	if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
  		accel_debugfs_init(dev);
  	else
-		drm_debugfs_dev_init(dev, drm_debugfs_root);
+		drm_debugfs_dev_init(dev, NULL);
 
  	return 0;
 
@@ -1168,7 +1165,7 @@ static void drm_core_exit(void)
  	drm_panic_exit();
  	accel_core_exit();
  	unregister_chrdev(DRM_MAJOR, "drm");
-	debugfs_remove(drm_debugfs_root);
+	drm_debugfs_remove_dir();
  	drm_sysfs_destroy();
  	WARN_ON(!xa_empty(&drm_minors_xa));
  	drm_connector_ida_destroy();
@@ -1187,7 +1184,7 @@ static int __init drm_core_init(void)
  		goto error;
  	}
 
-	drm_debugfs_root = debugfs_create_dir("dri", NULL);
+	drm_debugfs_create_dir();
 
  	ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
  	if (ret < 0)
drivers/gpu/drm/drm_internal.h +2 -4
index b2b6a8e49dda..d2d8e72f32d9 100644
@@ -186,8 +186,7 @@ void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
  #if defined(CONFIG_DEBUG_FS)
  void drm_debugfs_dev_fini(struct drm_device *dev);
  void drm_debugfs_dev_register(struct drm_device *dev);
-int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-			 struct dentry *root);
+int drm_debugfs_register(struct drm_minor *minor, int minor_id);
  void drm_debugfs_unregister(struct drm_minor *minor);
  void drm_debugfs_connector_add(struct drm_connector *connector);
  void drm_debugfs_connector_remove(struct drm_connector *connector);
@@ -205,8 +204,7 @@ static inline void drm_debugfs_dev_register(struct drm_device *dev)
  {
  }
 
-static inline int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-				       struct dentry *root)
+static inline int drm_debugfs_register(struct drm_minor *minor, int minor_id)
  {
  	return 0;
  }
include/drm/drm_drv.h +10 -0
index a43d707b5f36..4e77a0c4a7f9 100644
@@ -567,10 +567,20 @@ static inline bool drm_firmware_drivers_only(void)
 
  #if defined(CONFIG_DEBUG_FS)
  void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root);
+void drm_debugfs_create_dir(void);
+void drm_debugfs_remove_dir(void);
  #else
  static inline void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
  {
  }
+
+static inline void drm_debugfs_create_dir(void)
+{
+}
+
+static inline void drm_debugfs_remove_dir(void)
+{
+}
  #endif
 
  #endif
#12 lore raw
Quoted 31 lines · Sunil
Quoted 29 lines · Christian König
Quoted 28 lines · Sunil Khatri

move the functions from drm_drv.c which uses the static drm_debugfs_root as parent node in the debugfs by drm.

move this root node to the debugfs for easily handling of future requirements to add more information in the root directory and one of which is planned to have directories for each client in the root directory which is dri.

---
  drivers/gpu/drm/drm_debugfs.c  | 22 ++++++++++++++++++---- drivers/gpu/drm/drm_drv.c      | 11 ++++------- drivers/gpu/drm/drm_internal.h |  6 ++---- include/drm/drm_drv.h          | 10 ++++++++++

  4 files changed, 34 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_debugfs.c +1 -0
index 2d43bda82887..5a33ec299c04 100644
@@ -44,6 +44,8 @@
   #include "drm_crtc_internal.h"
   #include "drm_internal.h"
   +static struct dentry *drm_debugfs_root;
+
   /***************************************************
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/drm_debugfs.c int drm_debugfs_remove_files(const struct · line 291

Quoted 7 lines
drivers/gpu/drm/drm_debugfs.c +0 -0
    * Initialization, etc.
    **************************************************/
@@ -286,6 +288,16 @@ int drm_debugfs_remove_files(const struct 
 drm_info_list *files, int count,
   }
   EXPORT_SYMBOL(drm_debugfs_remove_files);
   +void drm_debugfs_create_dir(void)

I think we need a better name for this. drm_debugfs_init_root maybe? Ideas welcome.

You mean the function name drm_debugfs_create_dir ??

Quoted 30 lines

Sounds good to me.

Regards
Sunil Khatri

Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/drm_debugfs.c +9 -0
+{
+    drm_debugfs_root = debugfs_create_dir("dri", NULL);
+}
+
+void drm_debugfs_remove_dir(void)
+{
+    debugfs_remove(drm_debugfs_root);
+}
+
   /**
    * drm_debugfs_dev_init - create debugfs directory for the device

   * @dev: the device which we want to create the directory for

drivers/gpu/drm/drm_debugfs.c +0 -0
@@ -295,7 +307,10 @@ EXPORT_SYMBOL(drm_debugfs_remove_files);
    */
   void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
Quoted 8 lines

drivers/gpu/drm/drm_debugfs.c EXPORT_SYMBOL(drm_debugfs_remove_files); · line 313

Quoted 7 lines
drivers/gpu/drm/drm_debugfs.c +4 -1
   {
-    dev->debugfs_root = debugfs_create_dir(dev->unique, root);
+    if (!root)
+        dev->debugfs_root = debugfs_create_dir(dev->unique, 
 drm_debugfs_root);
+    else
+        dev->debugfs_root = debugfs_create_dir(dev->unique, root);

Ah! That is also used from the accel subsystem, isn't it?

Quoted 3 lines

Yes

Quoted 2 lines

Probably best to move accel_debugfs_root into this here as well and distinct based on drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL) where to create the debugfs directory.

I checked this code again and we could not use drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL) as at that time as there is no drm_device as its done too early and done before any drm_device is created.

I have something in mind as i found that accel_core_init is called from drm_drv and we can do something there itself. I will push another patch set and lets see how you find that.

Regards
Sunil khatri

Quoted 129 lines
Quoted 2 lines

We probably need the same distinction for other use cases as well, so probably best to create a helper function for that.

I did explore that accel_debugfs_init in file drivers/accel/drm_accel.c and i dont think of a clear and acceptable design. It is a different module all together and we dont want to create debugfs directory for it in drm code. If the module fail for some reason and also removing the debugfs directory is another problem. We do not know when we want to init/exit the module.

The approach that i used is that drm_debugfs_dev_init is used by other module(right now there is one but we never know in future) as well as drm itself. The parent node is passed by the caller and for drm itself the parent node is defined statically.  So with the approach i took is if parent node is NULL its an internal call within drm and we can directly use the the static dentry i.e drm_debugfs_root.

Can we move below call from accel_core_init to drm_debugfs.c although its a different module all together for accel ??

accel_debugfs_root = debugfs_create_dir("accel", NULL);

Quoted 118 lines

Apart from that looks really good to me.

Regards,
Christian.

Quoted 112 lines
drivers/gpu/drm/drm_debugfs.c +2 -2
   }
     /**
@@ -322,14 +337,13 @@ void drm_debugfs_dev_register(struct 
 drm_device *dev)
           drm_atomic_debugfs_init(dev);
   }
   -int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-             struct dentry *root)
+int drm_debugfs_register(struct drm_minor *minor, int minor_id)
   {
       struct drm_device *dev = minor->dev;
       char name[64];
         sprintf(name, "%d", minor_id);
-    minor->debugfs_symlink = debugfs_create_symlink(name, root,
+    minor->debugfs_symlink = debugfs_create_symlink(name, 
 drm_debugfs_root,
                               dev->unique);
         /* TODO: Only for compatibility with drivers */
drivers/gpu/drm/drm_drv.c +1 -3
index 17fc5dc708f4..8abc52eac8f3 100644
@@ -69,8 +69,6 @@ DEFINE_XARRAY_ALLOC(drm_minors_xa);
    */
   static bool drm_core_init_complete;
   -static struct dentry *drm_debugfs_root;
-
   DEFINE_STATIC_SRCU(drm_unplug_srcu);
     /*
@@ -183,8 +181,7 @@ static int drm_minor_register(struct drm_device 
 *dev, enum drm_minor_type type)
           return 0;
         if (minor->type != DRM_MINOR_ACCEL) {
-        ret = drm_debugfs_register(minor, minor->index,
-                       drm_debugfs_root);
+        ret = drm_debugfs_register(minor, minor->index);
           if (ret) {
               DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
               goto err_debugfs;
@@ -754,7 +751,7 @@ static int drm_dev_init(struct drm_device *dev,
       if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
           accel_debugfs_init(dev);

      else
-        drm_debugfs_dev_init(dev, drm_debugfs_root); +        drm_debugfs_dev_init(dev, NULL); return 0;
  @@ -1168,7 +1165,7 @@ static void drm_core_exit(void) drm_panic_exit();
      accel_core_exit(); unregister_chrdev(DRM_MAJOR, "drm");
-    debugfs_remove(drm_debugfs_root);
+    drm_debugfs_remove_dir(); drm_sysfs_destroy(); WARN_ON(!xa_empty(&drm_minors_xa)); drm_connector_ida_destroy();

drivers/gpu/drm/drm_drv.c +1 -0
@@ -1187,7 +1184,7 @@ static int __init drm_core_init(void)
           goto error;
       }
   -    drm_debugfs_root = debugfs_create_dir("dri", NULL);
+    drm_debugfs_create_dir();
         ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
       if (ret < 0)
drivers/gpu/drm/drm_internal.h +1 -2
index b2b6a8e49dda..d2d8e72f32d9 100644
@@ -186,8 +186,7 @@ void drm_gem_vunmap(struct drm_gem_object *obj, 
 struct iosys_map *map);
   #if defined(CONFIG_DEBUG_FS)
   void drm_debugfs_dev_fini(struct drm_device *dev);
   void drm_debugfs_dev_register(struct drm_device *dev);
-int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-             struct dentry *root);
+int drm_debugfs_register(struct drm_minor *minor, int minor_id);
   void drm_debugfs_unregister(struct drm_minor *minor);
   void drm_debugfs_connector_add(struct drm_connector *connector);

  void drm_debugfs_connector_remove(struct drm_connector *connector);

drivers/gpu/drm/drm_internal.h +1 -1
@@ -205,8 +204,7 @@ static inline void 
 drm_debugfs_dev_register(struct drm_device *dev)
   {
   }
   -static inline int drm_debugfs_register(struct drm_minor *minor, int minor_id,
-                       struct dentry *root)
+static inline int drm_debugfs_register(struct drm_minor *minor, int 
 minor_id)
   {

      return 0;
  }

include/drm/drm_drv.h +10 -0
index a43d707b5f36..4e77a0c4a7f9 100644
@@ -567,10 +567,20 @@ static inline bool 
 drm_firmware_drivers_only(void)
     #if defined(CONFIG_DEBUG_FS)
   void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root);
+void drm_debugfs_create_dir(void);
+void drm_debugfs_remove_dir(void);
   #else
   static inline void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
   {
   }
+
+static inline void drm_debugfs_create_dir(void)
+{
+}
+
+static inline void drm_debugfs_remove_dir(void)
+{
+}
   #endif
     #endif