Skip to contentSkip to conversation

drm: fdinfo memory stats

PATCH Superseded Rob Clark 7 participants 20 messages Apr 12, 2023 – Apr 21, 2023

Subsystems (8)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Rob Clark

author
#0 lore raw

v4 cover drm: fdinfo memory stats

From: Rob Clark <[email protected]>

Similar motivation to other similar recent attempt[1]. But with an attempt to have some shared code for this. As well as documentation.

It is probably a bit UMA-centric, I guess devices with VRAM might want some placement stats as well. But this seems like a reasonable start.

Basic gputop support: https://patchwork.freedesktop.org/series/116236/
And already nvtop support: https://github.com/Syllo/nvtop/pull/204

[1] https://patchwork.freedesktop.org/series/112397/

Rob Clark (6):
drm: Add common fdinfo helper
drm/msm: Switch to fdinfo helper
drm/amdgpu: Switch to fdinfo helper
drm/i915: Switch to fdinfo helper
drm: Add fdinfo memory stats
drm/msm: Add memory stats to fdinfo

 Documentation/gpu/drm-usage-stats.rst      |  31 +++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  16 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h |   2 +-
 drivers/gpu/drm/drm_file.c                 | 111 +++++++++++++++++++++
 drivers/gpu/drm/i915/i915_driver.c         |   3 +-
 drivers/gpu/drm/i915/i915_drm_client.c     |  18 +---
 drivers/gpu/drm/i915/i915_drm_client.h     |   2 +-
 drivers/gpu/drm/msm/msm_drv.c              |  11 +-
 drivers/gpu/drm/msm/msm_gem.c              |  15 +++
 drivers/gpu/drm/msm/msm_gpu.c              |   2 -
 include/drm/drm_drv.h                      |   7 ++
 include/drm/drm_file.h                     |   5 +
 include/drm/drm_gem.h                      |  30 ++++++
 14 files changed, 220 insertions(+), 36 deletions(-)
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH v4 1/6 drm: Add common fdinfo helper

From: Rob Clark <[email protected]>

Handle a bit of the boiler-plate in a single case, and make it easier to add some core tracked stats.

v2: Update drm-usage-stats.rst, 64b client-id, rename drm_show_fdinfo

---
 Documentation/gpu/drm-usage-stats.rst | 10 +++++++-
 drivers/gpu/drm/drm_file.c            | 35 +++++++++++++++++++++++++++
 include/drm/drm_drv.h                 |  7 ++++++
 include/drm/drm_file.h                |  4 +++
 4 files changed, 55 insertions(+), 1 deletion(-)
Documentation/gpu/drm-usage-stats.rst +9 -1
index b46327356e80..2ab32c40e93c 100644
@@ -126,7 +126,15 @@ percentage utilization of the engine, whereas drm-engine-<str> only reflects
 time active without considering what frequency the engine is operating as a
 percentage of it's maximum frequency.
 
+Implementation Details
+======================
+
+Drivers should use drm_show_fdinfo() in their `struct file_operations`, and
+implement &drm_driver.show_fdinfo if they wish to provide any stats which
+are not provided by drm_show_fdinfo().  But even driver specific stats should
+be documented above and where possible, aligned with other drivers.
+
 Driver specific implementations
-===============================
+-------------------------------
 
 :ref:`i915-usage-stats`
drivers/gpu/drm/drm_file.c +35 -0
index a51ff8cee049..6d5bdd684ae2 100644
@@ -148,6 +148,7 @@ bool drm_dev_needs_global_mutex(struct drm_device *dev)
  */
 struct drm_file *drm_file_alloc(struct drm_minor *minor)
 {
+	static atomic64_t ident = ATOMIC_INIT(0);
 	struct drm_device *dev = minor->dev;
 	struct drm_file *file;
 	int ret;
@@ -156,6 +157,8 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
 	if (!file)
 		return ERR_PTR(-ENOMEM);
 
+	/* Get a unique identifier for fdinfo: */
+	file->client_id = atomic64_inc_return(&ident);
 	file->pid = get_pid(task_pid(current));
 	file->minor = minor;
 
@@ -868,6 +871,38 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
 }
 EXPORT_SYMBOL(drm_send_event);
 
+/**
+ * drm_show_fdinfo - helper for drm file fops
+ * @seq_file: output stream
+ * @f: the device file instance
+ *
+ * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
+ * process using the GPU.  See also &drm_driver.show_fdinfo.
+ *
+ * For text output format description please see Documentation/gpu/drm-usage-stats.rst
+ */
+void drm_show_fdinfo(struct seq_file *m, struct file *f)
+{
+	struct drm_file *file = f->private_data;
+	struct drm_device *dev = file->minor->dev;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
+	drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
+
+	if (dev_is_pci(dev->dev)) {
+		struct pci_dev *pdev = to_pci_dev(dev->dev);
+
+		drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
+			   pci_domain_nr(pdev->bus), pdev->bus->number,
+			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+	}
+
+	if (dev->driver->show_fdinfo)
+		dev->driver->show_fdinfo(&p, file);
+}
+EXPORT_SYMBOL(drm_show_fdinfo);
+
 /**
  * mock_drm_getfile - Create a new struct file for the drm device
  * @minor: drm minor to wrap (e.g. #drm_device.primary)
include/drm/drm_drv.h +7 -0
index 5b86bb7603e7..5edf2a13733b 100644
@@ -401,6 +401,13 @@ struct drm_driver {
 			       struct drm_device *dev, uint32_t handle,
 			       uint64_t *offset);
 
+	/**
+	 * @show_fdinfo:
+	 *
+	 * Print device specific fdinfo.  See Documentation/gpu/drm-usage-stats.rst.
+	 */
+	void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f);
+
 	/** @major: driver major number */
 	int major;
 	/** @minor: driver minor number */
include/drm/drm_file.h +4 -0
index 0d1f853092ab..6de6d0e9c634 100644
@@ -258,6 +258,9 @@ struct drm_file {
 	/** @pid: Process that opened this file. */
 	struct pid *pid;
 
+	/** @client_id: A unique id for fdinfo */
+	u64 client_id;
+
 	/** @magic: Authentication magic, see @authenticated. */
 	drm_magic_t magic;
 
@@ -437,6 +440,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
 void drm_send_event_timestamp_locked(struct drm_device *dev,
 				     struct drm_pending_event *e,
 				     ktime_t timestamp);
+void drm_show_fdinfo(struct seq_file *m, struct file *f);
 
 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
 
Signature · 1 line
2.39.2
maintainer
#2 lore raw

Re: [PATCH v4 1/6] drm: Add common fdinfo helper

Am 13.04.23 um 00:42 schrieb Rob Clark:

Quoted 21 lines

From: Rob Clark <[email protected]>

Handle a bit of the boiler-plate in a single case, and make it easier to add some core tracked stats.

v2: Update drm-usage-stats.rst, 64b client-id, rename drm_show_fdinfo

---
  Documentation/gpu/drm-usage-stats.rst | 10 +++++++-
  drivers/gpu/drm/drm_file.c            | 35 +++++++++++++++++++++++++++
  include/drm/drm_drv.h                 |  7 ++++++
  include/drm/drm_file.h                |  4 +++
  4 files changed, 55 insertions(+), 1 deletion(-)
Documentation/gpu/drm-usage-stats.rst +0 -0
index b46327356e80..2ab32c40e93c 100644

Documentation/gpu/drm-usage-stats.rst percentage utilization of the engine, whereas drm-engine-<str> only reflects · line 135

Quoted 10 lines
Documentation/gpu/drm-usage-stats.rst +7 -0
@@ -126,7 +126,15 @@ percentage utilization of the engine, whereas drm-engine-<str> only reflects
  time active without considering what frequency the engine is operating as a
  percentage of it's maximum frequency.
 
+Implementation Details
+======================
+
+Drivers should use drm_show_fdinfo() in their `struct file_operations`, and
+implement &drm_driver.show_fdinfo if they wish to provide any stats which
+are not provided by drm_show_fdinfo().  But even driver specific stats should
+be documented above and where possible, aligned with other drivers.

I'm really wondering if it wouldn't be less mid-layering if we let the drivers call the drm function to print the common values instead of the other way around?

Apart from that question the patch looks good to me.

Christian.

Quoted 106 lines
Documentation/gpu/drm-usage-stats.rst +2 -1
+
  Driver specific implementations
-===============================
+-------------------------------
 
  :ref:`i915-usage-stats`
drivers/gpu/drm/drm_file.c +35 -0
index a51ff8cee049..6d5bdd684ae2 100644
@@ -148,6 +148,7 @@ bool drm_dev_needs_global_mutex(struct drm_device *dev)
   */
  struct drm_file *drm_file_alloc(struct drm_minor *minor)
  {
+	static atomic64_t ident = ATOMIC_INIT(0);
  	struct drm_device *dev = minor->dev;
  	struct drm_file *file;
  	int ret;
@@ -156,6 +157,8 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
  	if (!file)
  		return ERR_PTR(-ENOMEM);
 
+	/* Get a unique identifier for fdinfo: */
+	file->client_id = atomic64_inc_return(&ident);
  	file->pid = get_pid(task_pid(current));
  	file->minor = minor;
 
@@ -868,6 +871,38 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  }
  EXPORT_SYMBOL(drm_send_event);
 
+/**
+ * drm_show_fdinfo - helper for drm file fops
+ * @seq_file: output stream
+ * @f: the device file instance
+ *
+ * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
+ * process using the GPU.  See also &drm_driver.show_fdinfo.
+ *
+ * For text output format description please see Documentation/gpu/drm-usage-stats.rst
+ */
+void drm_show_fdinfo(struct seq_file *m, struct file *f)
+{
+	struct drm_file *file = f->private_data;
+	struct drm_device *dev = file->minor->dev;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
+	drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
+
+	if (dev_is_pci(dev->dev)) {
+		struct pci_dev *pdev = to_pci_dev(dev->dev);
+
+		drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
+			   pci_domain_nr(pdev->bus), pdev->bus->number,
+			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+	}
+
+	if (dev->driver->show_fdinfo)
+		dev->driver->show_fdinfo(&p, file);
+}
+EXPORT_SYMBOL(drm_show_fdinfo);
+
  /**
   * mock_drm_getfile - Create a new struct file for the drm device
   * @minor: drm minor to wrap (e.g. #drm_device.primary)
include/drm/drm_drv.h +7 -0
index 5b86bb7603e7..5edf2a13733b 100644
@@ -401,6 +401,13 @@ struct drm_driver {
  			       struct drm_device *dev, uint32_t handle,
  			       uint64_t *offset);
 
+	/**
+	 * @show_fdinfo:
+	 *
+	 * Print device specific fdinfo.  See Documentation/gpu/drm-usage-stats.rst.
+	 */
+	void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f);
+
  	/** @major: driver major number */
  	int major;
  	/** @minor: driver minor number */
include/drm/drm_file.h +4 -0
index 0d1f853092ab..6de6d0e9c634 100644
@@ -258,6 +258,9 @@ struct drm_file {
  	/** @pid: Process that opened this file. */
  	struct pid *pid;
 
+	/** @client_id: A unique id for fdinfo */
+	u64 client_id;
+
  	/** @magic: Authentication magic, see @authenticated. */
  	drm_magic_t magic;
 
@@ -437,6 +440,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
  void drm_send_event_timestamp_locked(struct drm_device *dev,
  				     struct drm_pending_event *e,
  				     ktime_t timestamp);
+void drm_show_fdinfo(struct seq_file *m, struct file *f);
 
  struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
 
#3 lore raw

Re: [PATCH v4 1/6] drm: Add common fdinfo helper

Quoted 27 lines · Christian König

Am 13.04.23 um 00:42 schrieb Rob Clark:

Quoted 26 lines

From: Rob Clark <[email protected]>

Handle a bit of the boiler-plate in a single case, and make it easier to add some core tracked stats.

v2: Update drm-usage-stats.rst, 64b client-id, rename drm_show_fdinfo

---
  Documentation/gpu/drm-usage-stats.rst | 10 +++++++-
  drivers/gpu/drm/drm_file.c            | 35 +++++++++++++++++++++++++++
  include/drm/drm_drv.h                 |  7 ++++++
  include/drm/drm_file.h                |  4 +++
  4 files changed, 55 insertions(+), 1 deletion(-)
Documentation/gpu/drm-usage-stats.rst +3 -0
index b46327356e80..2ab32c40e93c 100644
@@ -126,7 +126,15 @@ percentage utilization of the engine, whereas drm-engine-<str> only reflects
  time active without considering what frequency the engine is operating as a
  percentage of it's maximum frequency.
+Implementation Details
+======================
+
Quoted 8 lines

Documentation/gpu/drm-usage-stats.rst percentage utilization of the engine, whereas drm-engine-<str> only reflects · line 135

Quoted 4 lines
Documentation/gpu/drm-usage-stats.rst +4 -0
+Drivers should use drm_show_fdinfo() in their `struct file_operations`, and
+implement &drm_driver.show_fdinfo if they wish to provide any stats which
+are not provided by drm_show_fdinfo().  But even driver specific stats should
+be documented above and where possible, aligned with other drivers.

I'm really wondering if it wouldn't be less mid-layering if we let the drivers call the drm function to print the common values instead of the other way around?

The idea is that we plug this into DRM_GEM_FOPS and then everyone gets it by default. So it's a bit a tradeoff between midlayering and having inconsistent uapi between drivers. And there's generic tools that parse this, so consistency across drivers is good.

My gut feeling was that after a bit of experimenting with lots of different drivers for fdinfo stuff it's time to push for a bit more standardization and less fragmentation.

We can of course later on course-correct and shuffle things around again, e.g. by pushing more things into the gem_bo_fops->status hook (ttm and other memory manager libs could implement a decent one by default), or moving more into the drm_driver->show_fdinfo callback again.

If you look at kms we also shuffle things back&forth between core (for more consistency) and drivers (for more flexibility where needed).

The important part here imo is that we start with some scaffolding to be able to do this. Like another thing that I think we want is some drm_fdinfo_print functions that make sure the formatting is guaranteed consistents and we don't trip up parsers (like some drivers use " \t" as separator instead of just "\t", I guess by accident).

Quoted 1 line

Apart from thatquestion the patch looks good to me.

Ack? Or want the above recorded in the commit message, I think it'd make sense to put it there.
-Daniel

Quoted 101 lines

Christian.

Quoted 97 lines
Documentation/gpu/drm-usage-stats.rst +2 -1
+
  Driver specific implementations
-===============================
+-------------------------------
  :ref:`i915-usage-stats`
drivers/gpu/drm/drm_file.c +35 -0
index a51ff8cee049..6d5bdd684ae2 100644
@@ -148,6 +148,7 @@ bool drm_dev_needs_global_mutex(struct drm_device *dev)
   */
  struct drm_file *drm_file_alloc(struct drm_minor *minor)
  {
+	static atomic64_t ident = ATOMIC_INIT(0);
  	struct drm_device *dev = minor->dev;
  	struct drm_file *file;
  	int ret;
@@ -156,6 +157,8 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
  	if (!file)
  		return ERR_PTR(-ENOMEM);
+	/* Get a unique identifier for fdinfo: */
+	file->client_id = atomic64_inc_return(&ident);
  	file->pid = get_pid(task_pid(current));
  	file->minor = minor;
@@ -868,6 +871,38 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  }
  EXPORT_SYMBOL(drm_send_event);
+/**
+ * drm_show_fdinfo - helper for drm file fops
+ * @seq_file: output stream
+ * @f: the device file instance
+ *
+ * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
+ * process using the GPU.  See also &drm_driver.show_fdinfo.
+ *
+ * For text output format description please see Documentation/gpu/drm-usage-stats.rst
+ */
+void drm_show_fdinfo(struct seq_file *m, struct file *f)
+{
+	struct drm_file *file = f->private_data;
+	struct drm_device *dev = file->minor->dev;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
+	drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
+
+	if (dev_is_pci(dev->dev)) {
+		struct pci_dev *pdev = to_pci_dev(dev->dev);
+
+		drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
+			   pci_domain_nr(pdev->bus), pdev->bus->number,
+			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+	}
+
+	if (dev->driver->show_fdinfo)
+		dev->driver->show_fdinfo(&p, file);
+}
+EXPORT_SYMBOL(drm_show_fdinfo);
+
  /**
   * mock_drm_getfile - Create a new struct file for the drm device
   * @minor: drm minor to wrap (e.g. #drm_device.primary)
include/drm/drm_drv.h +7 -0
index 5b86bb7603e7..5edf2a13733b 100644
@@ -401,6 +401,13 @@ struct drm_driver {
  			       struct drm_device *dev, uint32_t handle,
  			       uint64_t *offset);
+	/**
+	 * @show_fdinfo:
+	 *
+	 * Print device specific fdinfo.  See Documentation/gpu/drm-usage-stats.rst.
+	 */
+	void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f);
+
  	/** @major: driver major number */
  	int major;
  	/** @minor: driver minor number */
include/drm/drm_file.h +4 -0
index 0d1f853092ab..6de6d0e9c634 100644
@@ -258,6 +258,9 @@ struct drm_file {
  	/** @pid: Process that opened this file. */
  	struct pid *pid;
+	/** @client_id: A unique id for fdinfo */
+	u64 client_id;
+
  	/** @magic: Authentication magic, see @authenticated. */
  	drm_magic_t magic;
@@ -437,6 +440,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
  void drm_send_event_timestamp_locked(struct drm_device *dev,
  				     struct drm_pending_event *e,
  				     ktime_t timestamp);
+void drm_show_fdinfo(struct seq_file *m, struct file *f);
  struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
maintainer
#4 lore raw

Re: [PATCH v4 1/6] drm: Add common fdinfo helper

Am 13.04.23 um 10:46 schrieb Daniel Vetter:

Quoted 35 lines · Christian König
Quoted 26 lines

Am 13.04.23 um 00:42 schrieb Rob Clark:

Quoted 25 lines

From: Rob Clark <[email protected]>

Handle a bit of the boiler-plate in a single case, and make it easier to add some core tracked stats.

v2: Update drm-usage-stats.rst, 64b client-id, rename drm_show_fdinfo

---
   Documentation/gpu/drm-usage-stats.rst | 10 +++++++-
   drivers/gpu/drm/drm_file.c            | 35 +++++++++++++++++++++++++++
   include/drm/drm_drv.h                 |  7 ++++++
   include/drm/drm_file.h                |  4 +++
   4 files changed, 55 insertions(+), 1 deletion(-)
Documentation/gpu/drm-usage-stats.rst +2 -0
index b46327356e80..2ab32c40e93c 100644
@@ -126,7 +126,15 @@ percentage utilization of the engine, whereas drm-engine-<str> only reflects
   time active without considering what frequency the engine is operating as a
   percentage of it's maximum frequency.
+Implementation Details
+======================
Quoted 8 lines

Documentation/gpu/drm-usage-stats.rst percentage utilization of the engine, whereas drm-engine-<str> only reflects · line 135

Quoted 5 lines
Documentation/gpu/drm-usage-stats.rst +5 -0
+
+Drivers should use drm_show_fdinfo() in their `struct file_operations`, and
+implement &drm_driver.show_fdinfo if they wish to provide any stats which
+are not provided by drm_show_fdinfo().  But even driver specific stats should
+be documented above and where possible, aligned with other drivers.

I'm really wondering if it wouldn't be less mid-layering if we let the drivers call the drm function to print the common values instead of the other way around?

Quoted 8 lines

The idea is that we plug this into DRM_GEM_FOPS and then everyone gets it by default. So it's a bit a tradeoff between midlayering and having inconsistent uapi between drivers. And there's generic tools that parse this, so consistency across drivers is good.

My gut feeling was that after a bit of experimenting with lots of different drivers for fdinfo stuff it's time to push for a bit more standardization and less fragmentation.

Yeah, that's indeed a trade of.

Quoted 6 lines

We can of course later on course-correct and shuffle things around again, e.g. by pushing more things into the gem_bo_fops->status hook (ttm and other memory manager libs could implement a decent one by default), or moving more into the drm_driver->show_fdinfo callback again.

Quoted 8 lines

If you look at kms we also shuffle things back&forth between core (for more consistency) and drivers (for more flexibility where needed).

The important part here imo is that we start with some scaffolding to be able to do this. Like another thing that I think we want is some drm_fdinfo_print functions that make sure the formatting is guaranteed consistents and we don't trip up parsers (like some drivers use " \t" as separator instead of just "\t", I guess by accident).

That's indeed a bit ugly and should probably be fixed on a higher level in the fs code.

Something like fdinfo_print(seq, name, format, value);

Quoted 4 lines
Quoted 2 lines

Apart from thatquestion the patch looks good to me.

Ack? Or want the above recorded in the commit message, I think it'd make sense to put it there.

Well if Rob mentions this trade of in the commit message or even better code document feel free to add my rb to the patch.

Christian.

Quoted 101 lines

-Daniel

Quoted 99 lines

Christian.

Quoted 97 lines
Documentation/gpu/drm-usage-stats.rst +2 -1
+
   Driver specific implementations
-===============================
+-------------------------------
   :ref:`i915-usage-stats`
drivers/gpu/drm/drm_file.c +35 -0
index a51ff8cee049..6d5bdd684ae2 100644
@@ -148,6 +148,7 @@ bool drm_dev_needs_global_mutex(struct drm_device *dev)
    */
   struct drm_file *drm_file_alloc(struct drm_minor *minor)
   {
+	static atomic64_t ident = ATOMIC_INIT(0);
   	struct drm_device *dev = minor->dev;
   	struct drm_file *file;
   	int ret;
@@ -156,6 +157,8 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
   	if (!file)
   		return ERR_PTR(-ENOMEM);
+	/* Get a unique identifier for fdinfo: */
+	file->client_id = atomic64_inc_return(&ident);
   	file->pid = get_pid(task_pid(current));
   	file->minor = minor;
@@ -868,6 +871,38 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
   }
   EXPORT_SYMBOL(drm_send_event);
+/**
+ * drm_show_fdinfo - helper for drm file fops
+ * @seq_file: output stream
+ * @f: the device file instance
+ *
+ * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
+ * process using the GPU.  See also &drm_driver.show_fdinfo.
+ *
+ * For text output format description please see Documentation/gpu/drm-usage-stats.rst
+ */
+void drm_show_fdinfo(struct seq_file *m, struct file *f)
+{
+	struct drm_file *file = f->private_data;
+	struct drm_device *dev = file->minor->dev;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
+	drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
+
+	if (dev_is_pci(dev->dev)) {
+		struct pci_dev *pdev = to_pci_dev(dev->dev);
+
+		drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
+			   pci_domain_nr(pdev->bus), pdev->bus->number,
+			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+	}
+
+	if (dev->driver->show_fdinfo)
+		dev->driver->show_fdinfo(&p, file);
+}
+EXPORT_SYMBOL(drm_show_fdinfo);
+
   /**
    * mock_drm_getfile - Create a new struct file for the drm device
    * @minor: drm minor to wrap (e.g. #drm_device.primary)
include/drm/drm_drv.h +7 -0
index 5b86bb7603e7..5edf2a13733b 100644
@@ -401,6 +401,13 @@ struct drm_driver {
   			       struct drm_device *dev, uint32_t handle,
   			       uint64_t *offset);
+	/**
+	 * @show_fdinfo:
+	 *
+	 * Print device specific fdinfo.  See Documentation/gpu/drm-usage-stats.rst.
+	 */
+	void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f);
+
   	/** @major: driver major number */
   	int major;
   	/** @minor: driver minor number */
include/drm/drm_file.h +4 -0
index 0d1f853092ab..6de6d0e9c634 100644
@@ -258,6 +258,9 @@ struct drm_file {
   	/** @pid: Process that opened this file. */
   	struct pid *pid;
+	/** @client_id: A unique id for fdinfo */
+	u64 client_id;
+
   	/** @magic: Authentication magic, see @authenticated. */
   	drm_magic_t magic;
@@ -437,6 +440,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
   void drm_send_event_timestamp_locked(struct drm_device *dev,
   				     struct drm_pending_event *e,
   				     ktime_t timestamp);
+void drm_show_fdinfo(struct seq_file *m, struct file *f);
   struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
#5 lore raw

Re: [PATCH v4 1/6] drm: Add common fdinfo helper

In reply to Daniel Vetter
Quoted 51 lines · Daniel Vetter
Quoted 27 lines · Christian König

Am 13.04.23 um 00:42 schrieb Rob Clark:

Quoted 26 lines

From: Rob Clark <[email protected]>

Handle a bit of the boiler-plate in a single case, and make it easier to add some core tracked stats.

v2: Update drm-usage-stats.rst, 64b client-id, rename drm_show_fdinfo

---
   Documentation/gpu/drm-usage-stats.rst | 10 +++++++-
   drivers/gpu/drm/drm_file.c            | 35 +++++++++++++++++++++++++++
   include/drm/drm_drv.h                 |  7 ++++++
   include/drm/drm_file.h                |  4 +++
   4 files changed, 55 insertions(+), 1 deletion(-)
Documentation/gpu/drm-usage-stats.rst +3 -0
index b46327356e80..2ab32c40e93c 100644
@@ -126,7 +126,15 @@ percentage utilization of the engine, whereas drm-engine-<str> only reflects
   time active without considering what frequency the engine is operating as a
   percentage of it's maximum frequency.
+Implementation Details
+======================
+
Quoted 8 lines

Documentation/gpu/drm-usage-stats.rst percentage utilization of the engine, whereas drm-engine-<str> only reflects · line 135

Quoted 4 lines
Documentation/gpu/drm-usage-stats.rst +4 -0
+Drivers should use drm_show_fdinfo() in their `struct file_operations`, and
+implement &drm_driver.show_fdinfo if they wish to provide any stats which
+are not provided by drm_show_fdinfo().  But even driver specific stats should
+be documented above and where possible, aligned with other drivers.

I'm really wondering if it wouldn't be less mid-layering if we let the drivers call the drm function to print the common values instead of the other way around?

The idea is that we plug this into DRM_GEM_FOPS and then everyone gets it by default. So it's a bit a tradeoff between midlayering and having inconsistent uapi between drivers. And there's generic tools that parse this, so consistency across drivers is good.

My gut feeling was that after a bit of experimenting with lots of different drivers for fdinfo stuff it's time to push for a bit more standardization and less fragmentation.

We can of course later on course-correct and shuffle things around again, e.g. by pushing more things into the gem_bo_fops->status hook (ttm and other memory manager libs could implement a decent one by default), or moving more into the drm_driver->show_fdinfo callback again.

Quoted 8 lines

If you look at kms we also shuffle things back&forth between core (for more consistency) and drivers (for more flexibility where needed).

The important part here imo is that we start with some scaffolding to be able to do this. Like another thing that I think we want is some drm_fdinfo_print functions that make sure the formatting is guaranteed consistents and we don't trip up parsers (like some drivers use " \t" as separator instead of just "\t", I guess by accident).

On this particular part I'd say it's even better to keep userspace on their toes and not let them hardcode assumption about how much and what whitespace characters to expect.

Otherwise I agree that mid-layer or not can be changed later, as long as my opens from elsewhere in the thread are closed before merging.

Regards,

Tvrtko

Quoted 108 lines
Quoted 1 line

Apart from thatquestion the patch looks good to me.

Ack? Or want the above recorded in the commit message, I think it'd make sense to put it there.
-Daniel

Quoted 101 lines

Christian.

Quoted 97 lines
Documentation/gpu/drm-usage-stats.rst +2 -1
+
   Driver specific implementations
-===============================
+-------------------------------
   :ref:`i915-usage-stats`
drivers/gpu/drm/drm_file.c +35 -0
index a51ff8cee049..6d5bdd684ae2 100644
@@ -148,6 +148,7 @@ bool drm_dev_needs_global_mutex(struct drm_device *dev)
    */
   struct drm_file *drm_file_alloc(struct drm_minor *minor)
   {
+	static atomic64_t ident = ATOMIC_INIT(0);
   	struct drm_device *dev = minor->dev;
   	struct drm_file *file;
   	int ret;
@@ -156,6 +157,8 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
   	if (!file)
   		return ERR_PTR(-ENOMEM);
+	/* Get a unique identifier for fdinfo: */
+	file->client_id = atomic64_inc_return(&ident);
   	file->pid = get_pid(task_pid(current));
   	file->minor = minor;
@@ -868,6 +871,38 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
   }
   EXPORT_SYMBOL(drm_send_event);
+/**
+ * drm_show_fdinfo - helper for drm file fops
+ * @seq_file: output stream
+ * @f: the device file instance
+ *
+ * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
+ * process using the GPU.  See also &drm_driver.show_fdinfo.
+ *
+ * For text output format description please see Documentation/gpu/drm-usage-stats.rst
+ */
+void drm_show_fdinfo(struct seq_file *m, struct file *f)
+{
+	struct drm_file *file = f->private_data;
+	struct drm_device *dev = file->minor->dev;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
+	drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
+
+	if (dev_is_pci(dev->dev)) {
+		struct pci_dev *pdev = to_pci_dev(dev->dev);
+
+		drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
+			   pci_domain_nr(pdev->bus), pdev->bus->number,
+			   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
+	}
+
+	if (dev->driver->show_fdinfo)
+		dev->driver->show_fdinfo(&p, file);
+}
+EXPORT_SYMBOL(drm_show_fdinfo);
+
   /**
    * mock_drm_getfile - Create a new struct file for the drm device
    * @minor: drm minor to wrap (e.g. #drm_device.primary)
include/drm/drm_drv.h +7 -0
index 5b86bb7603e7..5edf2a13733b 100644
@@ -401,6 +401,13 @@ struct drm_driver {
   			       struct drm_device *dev, uint32_t handle,
   			       uint64_t *offset);
+	/**
+	 * @show_fdinfo:
+	 *
+	 * Print device specific fdinfo.  See Documentation/gpu/drm-usage-stats.rst.
+	 */
+	void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f);
+
   	/** @major: driver major number */
   	int major;
   	/** @minor: driver minor number */
include/drm/drm_file.h +4 -0
index 0d1f853092ab..6de6d0e9c634 100644
@@ -258,6 +258,9 @@ struct drm_file {
   	/** @pid: Process that opened this file. */
   	struct pid *pid;
+	/** @client_id: A unique id for fdinfo */
+	u64 client_id;
+
   	/** @magic: Authentication magic, see @authenticated. */
   	drm_magic_t magic;
@@ -437,6 +440,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
   void drm_send_event_timestamp_locked(struct drm_device *dev,
   				     struct drm_pending_event *e,
   				     ktime_t timestamp);
+void drm_show_fdinfo(struct seq_file *m, struct file *f);
   struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags);
author
#6 lore raw

PATCH v4 2/6 drm/msm: Switch to fdinfo helper

In reply to Rob Clark

From: Rob Clark <[email protected]>

---
 drivers/gpu/drm/msm/msm_drv.c | 11 +++++------
 drivers/gpu/drm/msm/msm_gpu.c |  2 --
 2 files changed, 5 insertions(+), 8 deletions(-)
drivers/gpu/drm/msm/msm_drv.c +5 -6
index 5a10d28de9dd..3d73b98d6a9c 100644
@@ -1043,23 +1043,21 @@ static const struct drm_ioctl_desc msm_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW),
 };
 
-static void msm_fop_show_fdinfo(struct seq_file *m, struct file *f)
+static void msm_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 {
-	struct drm_file *file = f->private_data;
 	struct drm_device *dev = file->minor->dev;
 	struct msm_drm_private *priv = dev->dev_private;
-	struct drm_printer p = drm_seq_file_printer(m);
 
 	if (!priv->gpu)
 		return;
 
-	msm_gpu_show_fdinfo(priv->gpu, file->driver_priv, &p);
+	msm_gpu_show_fdinfo(priv->gpu, file->driver_priv, p);
 }
 
 static const struct file_operations fops = {
 	.owner = THIS_MODULE,
 	DRM_GEM_FOPS,
-	.show_fdinfo = msm_fop_show_fdinfo,
+	.show_fdinfo = drm_show_fdinfo,
 };
 
 static const struct drm_driver msm_driver = {
@@ -1070,7 +1068,7 @@ static const struct drm_driver msm_driver = {
 				DRIVER_SYNCOBJ_TIMELINE |
 				DRIVER_SYNCOBJ,
 	.open               = msm_open,
-	.postclose           = msm_postclose,
+	.postclose          = msm_postclose,
 	.lastclose          = drm_fb_helper_lastclose,
 	.dumb_create        = msm_gem_dumb_create,
 	.dumb_map_offset    = msm_gem_dumb_map_offset,
@@ -1081,6 +1079,7 @@ static const struct drm_driver msm_driver = {
 #ifdef CONFIG_DEBUG_FS
 	.debugfs_init       = msm_debugfs_init,
 #endif
+	.show_fdinfo        = msm_show_fdinfo,
 	.ioctls             = msm_ioctls,
 	.num_ioctls         = ARRAY_SIZE(msm_ioctls),
 	.fops               = &fops,
drivers/gpu/drm/msm/msm_gpu.c +0 -2
index 26ebda40be4f..c403912d13ab 100644
@@ -151,8 +151,6 @@ int msm_gpu_pm_suspend(struct msm_gpu *gpu)
 void msm_gpu_show_fdinfo(struct msm_gpu *gpu, struct msm_file_private *ctx,
 			 struct drm_printer *p)
 {
-	drm_printf(p, "drm-driver:\t%s\n", gpu->dev->driver->name);
-	drm_printf(p, "drm-client-id:\t%u\n", ctx->seqno);
 	drm_printf(p, "drm-engine-gpu:\t%llu ns\n", ctx->elapsed_ns);
 	drm_printf(p, "drm-cycles-gpu:\t%llu\n", ctx->cycles);
 	drm_printf(p, "drm-maxfreq-gpu:\t%u Hz\n", gpu->fast_rate);
Signature · 1 line
2.39.2
author
#8 lore raw

PATCH v4 3/6 drm/amdgpu: Switch to fdinfo helper

In reply to Rob Clark

From: Rob Clark <[email protected]>

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 16 ++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h |  2 +-
 3 files changed, 9 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
index f5ffca24def4..6c0e0c614b94 100644
@@ -2752,7 +2752,7 @@ static const struct file_operations amdgpu_driver_kms_fops = {
 	.compat_ioctl = amdgpu_kms_compat_ioctl,
 #endif
 #ifdef CONFIG_PROC_FS
-	.show_fdinfo = amdgpu_show_fdinfo
+	.show_fdinfo = drm_show_fdinfo,
 #endif
 };
 
@@ -2807,6 +2807,7 @@ static const struct drm_driver amdgpu_kms_driver = {
 	.dumb_map_offset = amdgpu_mode_dumb_mmap,
 	.fops = &amdgpu_driver_kms_fops,
 	.release = &amdgpu_driver_release_kms,
+	.show_fdinfo = amdgpu_show_fdinfo,
 
 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +6 -10
index 99a7855ab1bc..c2fdd5e448d1 100644
@@ -53,9 +53,8 @@ static const char *amdgpu_ip_name[AMDGPU_HW_IP_NUM] = {
 	[AMDGPU_HW_IP_VCN_JPEG]	=	"jpeg",
 };
 
-void amdgpu_show_fdinfo(struct seq_file *m, struct file *f)
+void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 {
-	struct drm_file *file = f->private_data;
 	struct amdgpu_device *adev = drm_to_adev(file->minor->dev);
 	struct amdgpu_fpriv *fpriv = file->driver_priv;
 	struct amdgpu_vm *vm = &fpriv->vm;
@@ -86,18 +85,15 @@ void amdgpu_show_fdinfo(struct seq_file *m, struct file *f)
 	 * ******************************************************************
 	 */
 
-	seq_printf(m, "pasid:\t%u\n", fpriv->vm.pasid);
-	seq_printf(m, "drm-driver:\t%s\n", file->minor->dev->driver->name);
-	seq_printf(m, "drm-pdev:\t%04x:%02x:%02x.%d\n", domain, bus, dev, fn);
-	seq_printf(m, "drm-client-id:\t%Lu\n", vm->immediate.fence_context);
-	seq_printf(m, "drm-memory-vram:\t%llu KiB\n", vram_mem/1024UL);
-	seq_printf(m, "drm-memory-gtt: \t%llu KiB\n", gtt_mem/1024UL);
-	seq_printf(m, "drm-memory-cpu: \t%llu KiB\n", cpu_mem/1024UL);
+	drm_printf(p, "pasid:\t%u\n", fpriv->vm.pasid);
+	drm_printf(p, "drm-memory-vram:\t%llu KiB\n", vram_mem/1024UL);
+	drm_printf(p, "drm-memory-gtt: \t%llu KiB\n", gtt_mem/1024UL);
+	drm_printf(p, "drm-memory-cpu: \t%llu KiB\n", cpu_mem/1024UL);
 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 		if (!usage[hw_ip])
 			continue;
 
-		seq_printf(m, "drm-engine-%s:\t%Ld ns\n", amdgpu_ip_name[hw_ip],
+		drm_printf(p, "drm-engine-%s:\t%Ld ns\n", amdgpu_ip_name[hw_ip],
 			   ktime_to_ns(usage[hw_ip]));
 	}
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h +1 -1
index e86834bfea1d..0398f5a159ef 100644
@@ -37,6 +37,6 @@
 #include "amdgpu_ids.h"
 
 uint32_t amdgpu_get_ip_count(struct amdgpu_device *adev, int id);
-void amdgpu_show_fdinfo(struct seq_file *m, struct file *f);
+void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file);
 
 #endif
Signature · 1 line
2.39.2
Reviewed-by on v4 3/6
#9 lore raw

Re: [PATCH v4 3/6] drm/amdgpu: Switch to fdinfo helper

Am 13.04.23 um 00:42 schrieb Rob Clark:

Quoted 3 lines

From: Rob Clark <[email protected]>

Quoted 78 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    |  3 ++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 16 ++++++----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h |  2 +-
  3 files changed, 9 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
index f5ffca24def4..6c0e0c614b94 100644
@@ -2752,7 +2752,7 @@ static const struct file_operations amdgpu_driver_kms_fops = {
  	.compat_ioctl = amdgpu_kms_compat_ioctl,
  #endif
  #ifdef CONFIG_PROC_FS
-	.show_fdinfo = amdgpu_show_fdinfo
+	.show_fdinfo = drm_show_fdinfo,
  #endif
  };
 
@@ -2807,6 +2807,7 @@ static const struct drm_driver amdgpu_kms_driver = {
  	.dumb_map_offset = amdgpu_mode_dumb_mmap,
  	.fops = &amdgpu_driver_kms_fops,
  	.release = &amdgpu_driver_release_kms,
+	.show_fdinfo = amdgpu_show_fdinfo,
 
  	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +6 -10
index 99a7855ab1bc..c2fdd5e448d1 100644
@@ -53,9 +53,8 @@ static const char *amdgpu_ip_name[AMDGPU_HW_IP_NUM] = {
  	[AMDGPU_HW_IP_VCN_JPEG]	=	"jpeg",
  };
 
-void amdgpu_show_fdinfo(struct seq_file *m, struct file *f)
+void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  {
-	struct drm_file *file = f->private_data;
  	struct amdgpu_device *adev = drm_to_adev(file->minor->dev);
  	struct amdgpu_fpriv *fpriv = file->driver_priv;
  	struct amdgpu_vm *vm = &fpriv->vm;
@@ -86,18 +85,15 @@ void amdgpu_show_fdinfo(struct seq_file *m, struct file *f)
  	 * ******************************************************************
  	 */
 
-	seq_printf(m, "pasid:\t%u\n", fpriv->vm.pasid);
-	seq_printf(m, "drm-driver:\t%s\n", file->minor->dev->driver->name);
-	seq_printf(m, "drm-pdev:\t%04x:%02x:%02x.%d\n", domain, bus, dev, fn);
-	seq_printf(m, "drm-client-id:\t%Lu\n", vm->immediate.fence_context);
-	seq_printf(m, "drm-memory-vram:\t%llu KiB\n", vram_mem/1024UL);
-	seq_printf(m, "drm-memory-gtt: \t%llu KiB\n", gtt_mem/1024UL);
-	seq_printf(m, "drm-memory-cpu: \t%llu KiB\n", cpu_mem/1024UL);
+	drm_printf(p, "pasid:\t%u\n", fpriv->vm.pasid);
+	drm_printf(p, "drm-memory-vram:\t%llu KiB\n", vram_mem/1024UL);
+	drm_printf(p, "drm-memory-gtt: \t%llu KiB\n", gtt_mem/1024UL);
+	drm_printf(p, "drm-memory-cpu: \t%llu KiB\n", cpu_mem/1024UL);
  	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
  		if (!usage[hw_ip])
  			continue;
 
-		seq_printf(m, "drm-engine-%s:\t%Ld ns\n", amdgpu_ip_name[hw_ip],
+		drm_printf(p, "drm-engine-%s:\t%Ld ns\n", amdgpu_ip_name[hw_ip],
  			   ktime_to_ns(usage[hw_ip]));
  	}
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h +1 -1
index e86834bfea1d..0398f5a159ef 100644
@@ -37,6 +37,6 @@
  #include "amdgpu_ids.h"
 
  uint32_t amdgpu_get_ip_count(struct amdgpu_device *adev, int id);
-void amdgpu_show_fdinfo(struct seq_file *m, struct file *f);
+void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file);
 
  #endif
author
#10 lore raw

PATCH v4 4/6 drm/i915: Switch to fdinfo helper

In reply to Rob Clark

From: Rob Clark <[email protected]>

---
 drivers/gpu/drm/i915/i915_driver.c     |  3 ++-
 drivers/gpu/drm/i915/i915_drm_client.c | 18 +++++-------------
 drivers/gpu/drm/i915/i915_drm_client.h |  2 +-
 3 files changed, 8 insertions(+), 15 deletions(-)
drivers/gpu/drm/i915/i915_driver.c +2 -1
index db7a86def7e2..0d91f85f8b97 100644
@@ -1696,7 +1696,7 @@ static const struct file_operations i915_driver_fops = {
 	.compat_ioctl = i915_ioc32_compat_ioctl,
 	.llseek = noop_llseek,
 #ifdef CONFIG_PROC_FS
-	.show_fdinfo = i915_drm_client_fdinfo,
+	.show_fdinfo = drm_show_fdinfo,
 #endif
 };
 
@@ -1796,6 +1796,7 @@ static const struct drm_driver i915_drm_driver = {
 	.open = i915_driver_open,
 	.lastclose = i915_driver_lastclose,
 	.postclose = i915_driver_postclose,
+	.show_fdinfo = i915_drm_client_fdinfo,
 
 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/i915/i915_drm_client.c +5 -13
index b09d1d386574..4a77e5e47f79 100644
@@ -101,7 +101,7 @@ static u64 busy_add(struct i915_gem_context *ctx, unsigned int class)
 }
 
 static void
-show_client_class(struct seq_file *m,
+show_client_class(struct drm_printer *p,
 		  struct i915_drm_client *client,
 		  unsigned int class)
 {
@@ -117,22 +117,20 @@ show_client_class(struct seq_file *m,
 	rcu_read_unlock();
 
 	if (capacity)
-		seq_printf(m, "drm-engine-%s:\t%llu ns\n",
+		drm_printf(p, "drm-engine-%s:\t%llu ns\n",
 			   uabi_class_names[class], total);
 
 	if (capacity > 1)
-		seq_printf(m, "drm-engine-capacity-%s:\t%u\n",
+		drm_printf(p, "drm-engine-capacity-%s:\t%u\n",
 			   uabi_class_names[class],
 			   capacity);
 }
 
-void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
+void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
 {
-	struct drm_file *file = f->private_data;
 	struct drm_i915_file_private *file_priv = file->driver_priv;
 	struct drm_i915_private *i915 = file_priv->dev_priv;
 	struct i915_drm_client *client = file_priv->client;
-	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
 	unsigned int i;
 
 	/*
@@ -141,12 +139,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
 	 * ******************************************************************
 	 */
 
-	seq_printf(m, "drm-driver:\t%s\n", i915->drm.driver->name);
-	seq_printf(m, "drm-pdev:\t%04x:%02x:%02x.%d\n",
-		   pci_domain_nr(pdev->bus), pdev->bus->number,
-		   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
-	seq_printf(m, "drm-client-id:\t%u\n", client->id);
-
 	/*
 	 * Temporarily skip showing client engine information with GuC submission till
 	 * fetching engine busyness is implemented in the GuC submission backend
@@ -155,6 +147,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
 		return;
 
 	for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
-		show_client_class(m, client, i);
+		show_client_class(p, client, i);
 }
 #endif
drivers/gpu/drm/i915/i915_drm_client.h +1 -1
index 69496af996d9..ef85fef45de5 100644
@@ -60,7 +60,7 @@ static inline void i915_drm_client_put(struct i915_drm_client *client)
 struct i915_drm_client *i915_drm_client_add(struct i915_drm_clients *clients);
 
 #ifdef CONFIG_PROC_FS
-void i915_drm_client_fdinfo(struct seq_file *m, struct file *f);
+void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file);
 #endif
 
 void i915_drm_clients_fini(struct i915_drm_clients *clients);
Signature · 1 line
2.39.2
#11 lore raw

Re: [PATCH v4 4/6] drm/i915: Switch to fdinfo helper

Quoted 1 line · Rob Clark

From: Rob Clark <[email protected]>

There is more do to here to remove my client->id fully (would now be dead code) so maybe easiest if you drop this patch and I do it after you land this and it propagates to our branches? I'd like to avoid pain with conflicts if possible..

Regards,

Tvrtko

Quoted 102 lines
---
  drivers/gpu/drm/i915/i915_driver.c     |  3 ++-
  drivers/gpu/drm/i915/i915_drm_client.c | 18 +++++-------------
  drivers/gpu/drm/i915/i915_drm_client.h |  2 +-
  3 files changed, 8 insertions(+), 15 deletions(-)
drivers/gpu/drm/i915/i915_driver.c +2 -1
index db7a86def7e2..0d91f85f8b97 100644
@@ -1696,7 +1696,7 @@ static const struct file_operations i915_driver_fops = {
  	.compat_ioctl = i915_ioc32_compat_ioctl,
  	.llseek = noop_llseek,
  #ifdef CONFIG_PROC_FS
-	.show_fdinfo = i915_drm_client_fdinfo,
+	.show_fdinfo = drm_show_fdinfo,
  #endif
  };
 
@@ -1796,6 +1796,7 @@ static const struct drm_driver i915_drm_driver = {
  	.open = i915_driver_open,
  	.lastclose = i915_driver_lastclose,
  	.postclose = i915_driver_postclose,
+	.show_fdinfo = i915_drm_client_fdinfo,
 
  	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/i915/i915_drm_client.c +5 -13
index b09d1d386574..4a77e5e47f79 100644
@@ -101,7 +101,7 @@ static u64 busy_add(struct i915_gem_context *ctx, unsigned int class)
  }
 
  static void
-show_client_class(struct seq_file *m,
+show_client_class(struct drm_printer *p,
  		  struct i915_drm_client *client,
  		  unsigned int class)
  {
@@ -117,22 +117,20 @@ show_client_class(struct seq_file *m,
  	rcu_read_unlock();
 
  	if (capacity)
-		seq_printf(m, "drm-engine-%s:\t%llu ns\n",
+		drm_printf(p, "drm-engine-%s:\t%llu ns\n",
  			   uabi_class_names[class], total);
 
  	if (capacity > 1)
-		seq_printf(m, "drm-engine-capacity-%s:\t%u\n",
+		drm_printf(p, "drm-engine-capacity-%s:\t%u\n",
  			   uabi_class_names[class],
  			   capacity);
  }
 
-void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
+void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
  {
-	struct drm_file *file = f->private_data;
  	struct drm_i915_file_private *file_priv = file->driver_priv;
  	struct drm_i915_private *i915 = file_priv->dev_priv;
  	struct i915_drm_client *client = file_priv->client;
-	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
  	unsigned int i;
 
  	/*
@@ -141,12 +139,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
  	 * ******************************************************************
  	 */
 
-	seq_printf(m, "drm-driver:\t%s\n", i915->drm.driver->name);
-	seq_printf(m, "drm-pdev:\t%04x:%02x:%02x.%d\n",
-		   pci_domain_nr(pdev->bus), pdev->bus->number,
-		   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
-	seq_printf(m, "drm-client-id:\t%u\n", client->id);
-
  	/*
  	 * Temporarily skip showing client engine information with GuC submission till
  	 * fetching engine busyness is implemented in the GuC submission backend
@@ -155,6 +147,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
  		return;
 
  	for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
-		show_client_class(m, client, i);
+		show_client_class(p, client, i);
  }
  #endif
drivers/gpu/drm/i915/i915_drm_client.h +1 -1
index 69496af996d9..ef85fef45de5 100644
@@ -60,7 +60,7 @@ static inline void i915_drm_client_put(struct i915_drm_client *client)
  struct i915_drm_client *i915_drm_client_add(struct i915_drm_clients *clients);
 
  #ifdef CONFIG_PROC_FS
-void i915_drm_client_fdinfo(struct seq_file *m, struct file *f);
+void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file);
  #endif
 
  void i915_drm_clients_fini(struct i915_drm_clients *clients);
author
#12 lore raw

Re: [PATCH v4 4/6] drm/i915: Switch to fdinfo helper

Quoted 9 lines · Tvrtko Ursulin
Quoted 1 line · Rob Clark

From: Rob Clark <[email protected]>

There is more do to here to remove my client->id fully (would now be dead code) so maybe easiest if you drop this patch and I do it after you land this and it propagates to our branches? I'd like to avoid pain with conflicts if possible..

That is fine by me

BR,
-R

Quoted 106 lines

Regards,

Tvrtko

Quoted 102 lines
---
  drivers/gpu/drm/i915/i915_driver.c     |  3 ++-
  drivers/gpu/drm/i915/i915_drm_client.c | 18 +++++-------------
  drivers/gpu/drm/i915/i915_drm_client.h |  2 +-
  3 files changed, 8 insertions(+), 15 deletions(-)
drivers/gpu/drm/i915/i915_driver.c +2 -1
index db7a86def7e2..0d91f85f8b97 100644
@@ -1696,7 +1696,7 @@ static const struct file_operations i915_driver_fops = {
      .compat_ioctl = i915_ioc32_compat_ioctl,
      .llseek = noop_llseek,
  #ifdef CONFIG_PROC_FS
-     .show_fdinfo = i915_drm_client_fdinfo,
+     .show_fdinfo = drm_show_fdinfo,
  #endif
  };
 
@@ -1796,6 +1796,7 @@ static const struct drm_driver i915_drm_driver = {
      .open = i915_driver_open,
      .lastclose = i915_driver_lastclose,
      .postclose = i915_driver_postclose,
+     .show_fdinfo = i915_drm_client_fdinfo,
 
      .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
      .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/i915/i915_drm_client.c +5 -13
index b09d1d386574..4a77e5e47f79 100644
@@ -101,7 +101,7 @@ static u64 busy_add(struct i915_gem_context *ctx, unsigned int class)
  }
 
  static void
-show_client_class(struct seq_file *m,
+show_client_class(struct drm_printer *p,
                struct i915_drm_client *client,
                unsigned int class)
  {
@@ -117,22 +117,20 @@ show_client_class(struct seq_file *m,
      rcu_read_unlock();
 
      if (capacity)
-             seq_printf(m, "drm-engine-%s:\t%llu ns\n",
+             drm_printf(p, "drm-engine-%s:\t%llu ns\n",
                         uabi_class_names[class], total);
 
      if (capacity > 1)
-             seq_printf(m, "drm-engine-capacity-%s:\t%u\n",
+             drm_printf(p, "drm-engine-capacity-%s:\t%u\n",
                         uabi_class_names[class],
                         capacity);
  }
 
-void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
+void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
  {
-     struct drm_file *file = f->private_data;
      struct drm_i915_file_private *file_priv = file->driver_priv;
      struct drm_i915_private *i915 = file_priv->dev_priv;
      struct i915_drm_client *client = file_priv->client;
-     struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
      unsigned int i;
 
      /*
@@ -141,12 +139,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
       * ******************************************************************
       */
 
-     seq_printf(m, "drm-driver:\t%s\n", i915->drm.driver->name);
-     seq_printf(m, "drm-pdev:\t%04x:%02x:%02x.%d\n",
-                pci_domain_nr(pdev->bus), pdev->bus->number,
-                PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
-     seq_printf(m, "drm-client-id:\t%u\n", client->id);
-
      /*
       * Temporarily skip showing client engine information with GuC submission till
       * fetching engine busyness is implemented in the GuC submission backend
@@ -155,6 +147,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
              return;
 
      for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
-             show_client_class(m, client, i);
+             show_client_class(p, client, i);
  }
  #endif
drivers/gpu/drm/i915/i915_drm_client.h +1 -1
index 69496af996d9..ef85fef45de5 100644
@@ -60,7 +60,7 @@ static inline void i915_drm_client_put(struct i915_drm_client *client)
  struct i915_drm_client *i915_drm_client_add(struct i915_drm_clients *clients);
 
  #ifdef CONFIG_PROC_FS
-void i915_drm_client_fdinfo(struct seq_file *m, struct file *f);
+void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file);
  #endif
 
  void i915_drm_clients_fini(struct i915_drm_clients *clients);
author
#13 lore raw

PATCH v4 5/6 drm: Add fdinfo memory stats

In reply to Rob Clark

From: Rob Clark <[email protected]>

Add support to dump GEM stats to fdinfo.

v2: Fix typos, change size units to match docs, use div_u64
v3: Do it in core
v4: more kerneldoc

---
 Documentation/gpu/drm-usage-stats.rst | 21 ++++++++
 drivers/gpu/drm/drm_file.c            | 76 +++++++++++++++++++++++++++
 include/drm/drm_file.h                |  1 +
 include/drm/drm_gem.h                 | 30 +++++++++++
 4 files changed, 128 insertions(+)
Documentation/gpu/drm-usage-stats.rst +21 -0
index 2ab32c40e93c..80003e27e28e 100644
@@ -105,6 +105,27 @@ object belong to this client, in the respective memory region.
 Default unit shall be bytes with optional unit specifiers of 'KiB' or 'MiB'
 indicating kibi- or mebi-bytes.
 
+- drm-shared-memory: <uint> [KiB|MiB]
+
+The total size of buffers that are shared with another file (ie. have more
+than a single handle).
+
+- drm-private-memory: <uint> [KiB|MiB]
+
+The total size of buffers that are not shared with another file.
+
+- drm-resident-memory: <uint> [KiB|MiB]
+
+The total size of buffers that are resident in system memory.
+
+- drm-purgeable-memory: <uint> [KiB|MiB]
+
+The total size of buffers that are purgeable.
+
+- drm-active-memory: <uint> [KiB|MiB]
+
+The total size of buffers that are active on one or more rings.
+
 - drm-cycles-<str> <uint>
 
 Engine identifier string must be the same as the one specified in the
drivers/gpu/drm/drm_file.c +76 -0

94 lines not on this page · raw message

include/drm/drm_file.h +1 -0
index 6de6d0e9c634..919284bb4f1d 100644
@@ -41,6 +41,7 @@
 struct dma_fence;
 struct drm_file;
 struct drm_device;
+struct drm_printer;
 struct device;
 struct file;
 
include/drm/drm_gem.h +30 -0
index 189fd618ca65..9ebd2820ad1f 100644
@@ -42,6 +42,25 @@
 struct iosys_map;
 struct drm_gem_object;
 
+/**
+ * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
+ * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
+ * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
+ *
+ * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
+ * and drm_show_fdinfo().  Note that an object can DRM_GEM_OBJECT_PURGEABLE if
+ * it still active or not resident, in which case drm_show_fdinfo() will not
+ * account for it as purgeable.  So drivers do not need to check if the buffer
+ * is idle and resident to return this bit.  (Ie. userspace can mark a buffer
+ * as purgeable even while it is still busy on the GPU.. it does not _actually_
+ * become puregeable until it becomes idle.  The status gem object func does
+ * not need to consider this.)
+ */
+enum drm_gem_object_status {
+	DRM_GEM_OBJECT_RESIDENT  = BIT(0),
+	DRM_GEM_OBJECT_PURGEABLE = BIT(1),
+};
+
 /**
  * struct drm_gem_object_funcs - GEM object functions
  */
@@ -174,6 +193,17 @@ struct drm_gem_object_funcs {
 	 */
 	int (*evict)(struct drm_gem_object *obj);
 
+	/**
+	 * @status:
+	 *
+	 * The optional status callback can return additional object state
+	 * which determines which stats the object is counted against.  The
+	 * callback is called under table_lock.  Racing against object status
+	 * change is "harmless", and the callback can expect to not race
+	 * against object destruction.
+	 */
+	enum drm_gem_object_status (*status)(struct drm_gem_object *obj);
+
 	/**
 	 * @vm_ops:
 	 *
Signature · 1 line
2.39.2
Reviewed-by on v4 5/6
#14 lore raw

Re: [PATCH v4 5/6] drm: Add fdinfo memory stats

Quoted 8 lines · Rob Clark

+/**
+ * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
+ * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
+ * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
+ *
+ * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
+ * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if
+ * it still active or not resident, in which case drm_show_fdinfo() will not

nit: s/can/can be/;s/if it still/if it is still/

Quoted 4 lines

+ * account for it as purgeable. So drivers do not need to check if the buffer
+ * is idle and resident to return this bit. (Ie. userspace can mark a buffer
+ * as purgeable even while it is still busy on the GPU.. it does not _actually_
+ * become puregeable until it becomes idle. The status gem object func does

nit: s/puregeable/purgeable/

I think we want a similar note in the drm-usage-stats.rst file.

With the above the whole series is:

Fwiw: Keeping the i915 patch as part of this series would be great. Sure i915_drm_client->id becomes dead code, but it's a piece one can live with for a release or two. Then again it's not my call to make.

HTH
Emil

#15 lore raw

Re: [PATCH v4 5/6] drm: Add fdinfo memory stats

Quoted 18 lines · Emil Velikov
Quoted 8 lines · Rob Clark

+/**
+ * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
+ * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
+ * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
+ *
+ * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
+ * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if
+ * it still active or not resident, in which case drm_show_fdinfo() will not

nit: s/can/can be/;s/if it still/if it is still/

Quoted 4 lines

+ * account for it as purgeable. So drivers do not need to check if the buffer
+ * is idle and resident to return this bit. (Ie. userspace can mark a buffer
+ * as purgeable even while it is still busy on the GPU.. it does not _actually_
+ * become puregeable until it becomes idle. The status gem object func does

Quoted 7 lines

nit: s/puregeable/purgeable/

I think we want a similar note in the drm-usage-stats.rst file.

With the above the whole series is:

Have you maybe noticed my slightly alternative proposal? (*) I am not a fan of putting this flavour of accounting into the core with no way to opt out. I think it leaves no option but to add more keys in the future for any driver which will not be happy with the core accounting.

*) https://patchwork.freedesktop.org/series/116581/

Quoted 3 lines

Fwiw: Keeping the i915 patch as part of this series would be great. Sure i915_drm_client->id becomes dead code, but it's a piece one can live with for a release or two. Then again it's not my call to make.

Rob can take the i915 patch from my RFC too.

Regards,

Tvrtko

#16 lore raw

Re: [PATCH v4 5/6] drm: Add fdinfo memory stats

Quoted 27 lines · Tvrtko Ursulin
Quoted 18 lines · Emil Velikov
Quoted 8 lines · Rob Clark

+/**
+ * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
+ * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
+ * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
+ *
+ * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
+ * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if
+ * it still active or not resident, in which case drm_show_fdinfo() will not

nit: s/can/can be/;s/if it still/if it is still/

Quoted 4 lines

+ * account for it as purgeable. So drivers do not need to check if the buffer
+ * is idle and resident to return this bit. (Ie. userspace can mark a buffer
+ * as purgeable even while it is still busy on the GPU.. it does not _actually_
+ * become puregeable until it becomes idle. The status gem object func does

Quoted 7 lines

nit: s/puregeable/purgeable/

I think we want a similar note in the drm-usage-stats.rst file.

With the above the whole series is:

Quoted 7 lines

Have you maybe noticed my slightly alternative proposal? (*) I am not a fan of putting this flavour of accounting into the core with no way to opt out. I think it leaves no option but to add more keys in the future for any driver which will not be happy with the core accounting.

*) https://patchwork.freedesktop.org/series/116581/

Indeed I saw it. Not a fan of it, I'm afraid.

Quoted 6 lines
Quoted 3 lines

Fwiw: Keeping the i915 patch as part of this series would be great. Sure i915_drm_client->id becomes dead code, but it's a piece one can live with for a release or two. Then again it's not my call to make.

Rob can take the i915 patch from my RFC too.

Indeed.

-Emil

#17 lore raw

Re: [PATCH v4 5/6] drm: Add fdinfo memory stats

Quoted 35 lines · Emil Velikov
Quoted 32 lines · Tvrtko Ursulin
Quoted 18 lines · Emil Velikov
Quoted 8 lines · Rob Clark

+/**
+ * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
+ * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
+ * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
+ *
+ * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
+ * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if
+ * it still active or not resident, in which case drm_show_fdinfo() will not

nit: s/can/can be/;s/if it still/if it is still/

Quoted 4 lines

+ * account for it as purgeable. So drivers do not need to check if the buffer
+ * is idle and resident to return this bit. (Ie. userspace can mark a buffer
+ * as purgeable even while it is still busy on the GPU.. it does not _actually_
+ * become puregeable until it becomes idle. The status gem object func does

Quoted 7 lines

nit: s/puregeable/purgeable/

I think we want a similar note in the drm-usage-stats.rst file.

With the above the whole series is:

Have you maybe noticed my slightly alternative proposal? (*) I am not a fan of putting this flavour of accounting into the core with no way to opt out. I think it leaves no option but to add more keys in the future for any driver which will not be happy with the core accounting.

Quoted 4 lines
Quoted 2 lines

*) https://patchwork.freedesktop.org/series/116581/

Indeed I saw it. Not a fan of it, I'm afraid.

Hard to guess the reasons. :)

Anyway, at a minimum I suggest that if the no opt out version has to go in, it is clearly documented drm-*-memory-* is *not* about the full memory use of the client, but about memory belonging to user visible buffer objects *only*. Possibly going as far as naming the keys as drm-user-bo-memory-... That way there is a way to implement proper drm-*-memory- in the future.

Regards,

Tvrtko

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

Fwiw: Keeping the i915 patch as part of this series would be great. Sure i915_drm_client->id becomes dead code, but it's a piece one can live with for a release or two. Then again it's not my call to make.

Rob can take the i915 patch from my RFC too.

Indeed.

-Emil

author
#18 lore raw

Re: [PATCH v4 5/6] drm: Add fdinfo memory stats

Quoted 43 lines · Tvrtko Ursulin
Quoted 35 lines · Emil Velikov
Quoted 32 lines · Tvrtko Ursulin
Quoted 18 lines · Emil Velikov
Quoted 8 lines · Rob Clark

+/**
+ * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
+ * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
+ * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
+ *
+ * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
+ * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if
+ * it still active or not resident, in which case drm_show_fdinfo() will not

nit: s/can/can be/;s/if it still/if it is still/

Quoted 4 lines

+ * account for it as purgeable. So drivers do not need to check if the buffer
+ * is idle and resident to return this bit. (Ie. userspace can mark a buffer
+ * as purgeable even while it is still busy on the GPU.. it does not _actually_
+ * become puregeable until it becomes idle. The status gem object func does

Quoted 7 lines

nit: s/puregeable/purgeable/

I think we want a similar note in the drm-usage-stats.rst file.

With the above the whole series is:

Have you maybe noticed my slightly alternative proposal? (*) I am not a fan of putting this flavour of accounting into the core with no way to opt out. I think it leaves no option but to add more keys in the future for any driver which will not be happy with the core accounting.

Quoted 4 lines
Quoted 2 lines

*) https://patchwork.freedesktop.org/series/116581/

Indeed I saw it. Not a fan of it, I'm afraid.

Quoted 8 lines

Hard to guess the reasons. :)

Anyway, at a minimum I suggest that if the no opt out version has to go in, it is clearly documented drm-*-memory-* is *not* about the full memory use of the client, but about memory belonging to user visible buffer objects *only*. Possibly going as far as naming the keys as drm-user-bo-memory-... That way there is a way to implement proper drm-*-memory- in the future.

I'll go back to the helper approach, just been distracted by a few other balls in the air.. should hopefully get to it in the next couple days

BR,
-R

Quoted 14 lines

Regards,

Tvrtko

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

Fwiw: Keeping the i915 patch as part of this series would be great. Sure i915_drm_client->id becomes dead code, but it's a piece one can live with for a release or two. Then again it's not my call to make.

Rob can take the i915 patch from my RFC too.

Indeed.

-Emil

author
#19 lore raw

PATCH v4 6/6 drm/msm: Add memory stats to fdinfo

In reply to Rob Clark

From: Rob Clark <[email protected]>

Use the new helper to export stats about memory usage.

v2: Drop unintended hunk
v3: Rebase

---
 drivers/gpu/drm/msm/msm_gem.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/gpu/drm/msm/msm_gem.c +15 -0
index db6c4e281d75..c32264234ea1 100644
@@ -1096,6 +1096,20 @@ int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
 	return ret;
 }
 
+static enum drm_gem_object_status msm_gem_status(struct drm_gem_object *obj)
+{
+	struct msm_gem_object *msm_obj = to_msm_bo(obj);
+	enum drm_gem_object_status status = 0;
+
+	if (msm_obj->pages)
+		status |= DRM_GEM_OBJECT_RESIDENT;
+
+	if (msm_obj->madv == MSM_MADV_DONTNEED)
+		status |= DRM_GEM_OBJECT_PURGEABLE;
+
+	return status;
+}
+
 static const struct vm_operations_struct vm_ops = {
 	.fault = msm_gem_fault,
 	.open = drm_gem_vm_open,
@@ -1110,6 +1124,7 @@ static const struct drm_gem_object_funcs msm_gem_object_funcs = {
 	.vmap = msm_gem_prime_vmap,
 	.vunmap = msm_gem_prime_vunmap,
 	.mmap = msm_gem_object_mmap,
+	.status = msm_gem_status,
 	.vm_ops = &vm_ops,
 };
 
Signature · 1 line
2.39.2