diff mbox series

[v7,2/4] drm/file: Add driver-specific memory region key printing helper

Message ID 20250108210259.647030-3-adrian.larumbe@collabora.com (mailing list archive)
State New
Headers show
Series drm/panthor: Display size of internal kernel BOs through fdinfo | expand

Commit Message

Adrián Larumbe Jan. 8, 2025, 9:02 p.m. UTC
This is motivated by the desire of some dirvers (eg. Panthor) to print the
size of internal memory regions with a prefix that reflects the driver
name, as suggested in the previous documentation commit.

That means a minor refactoring of print_size() was needed so as to make it
more generic in the format of key strings it takes as input.

Signed-off-by: Adrián Larumbe <adrian.larumbe@collabora.com>
Cc: Tvrtko Ursulin <tursulin@ursulin.net>
---
 drivers/gpu/drm/drm_file.c | 60 +++++++++++++++++++++++++++++++++-----
 include/drm/drm_file.h     |  2 ++
 2 files changed, 54 insertions(+), 8 deletions(-)

Comments

Tvrtko Ursulin Jan. 9, 2025, 1:05 p.m. UTC | #1
On 08/01/2025 21:02, Adrián Larumbe wrote:
> This is motivated by the desire of some dirvers (eg. Panthor) to print the
> size of internal memory regions with a prefix that reflects the driver
> name, as suggested in the previous documentation commit.
> 
> That means a minor refactoring of print_size() was needed so as to make it
> more generic in the format of key strings it takes as input.
> 
> Signed-off-by: Adrián Larumbe <adrian.larumbe@collabora.com>
> Cc: Tvrtko Ursulin <tursulin@ursulin.net>
> ---
>   drivers/gpu/drm/drm_file.c | 60 +++++++++++++++++++++++++++++++++-----
>   include/drm/drm_file.h     |  2 ++
>   2 files changed, 54 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c
> index cb5f22f5bbb6..5deae4cffa79 100644
> --- a/drivers/gpu/drm/drm_file.c
> +++ b/drivers/gpu/drm/drm_file.c
> @@ -830,8 +830,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
>   }
>   EXPORT_SYMBOL(drm_send_event);
>   
> -static void print_size(struct drm_printer *p, const char *stat,
> -		       const char *region, u64 sz)
> +static void print_size(struct drm_printer *p, const char *key, u64 sz)
>   {
>   	const char *units[] = {"", " KiB", " MiB"};
>   	unsigned u;
> @@ -842,9 +841,54 @@ static void print_size(struct drm_printer *p, const char *stat,
>   		sz = div_u64(sz, SZ_1K);
>   	}
>   
> -	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
> +	drm_printf(p, "%s:\t%llu%s\n", key, sz, units[u]);
>   }
>   
> +#define KEY_LEN 1024
> +#define DRM_PREFIX "drm"
> +
> +static void
> +print_size_region(struct drm_printer *p, const char *stat,
> +		  const char *region, u64 sz)
> +{
> +	char key[KEY_LEN+1] = {0};

A kilobyte of stack makes me a bit uneasy.

Would it not work to make print_size take a prefix and also avoid string 
operations? Something like, not compile tested:

diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c
index 2289e71e2fa2..efa4593babbc 100644
--- a/drivers/gpu/drm/drm_file.c
+++ b/drivers/gpu/drm/drm_file.c
@@ -830,8 +830,12 @@ void drm_send_event(struct drm_device *dev, struct 
drm_pending_event *e)
  }
  EXPORT_SYMBOL(drm_send_event);

-static void print_size(struct drm_printer *p, const char *stat,
-		       const char *region, u64 sz)
+static void
+drm_fdinfo_print_size(struct drm_printer *p,
+		      const char *prefix,
+		      const char *stat,
+		      const char *region,
+		      u64 sz)
  {
  	const char *units[] = {"", " KiB", " MiB"};
  	unsigned u;
@@ -842,8 +846,10 @@ static void print_size(struct drm_printer *p, const 
char *stat,
  		sz = div_u64(sz, SZ_1K);
  	}

-	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
+	drm_printf(p, "%s-%s-%s:\t%llu%s\n",
+		   prefix, stat, region, sz, units[u]);
  }
+EXPORT_SYMBOL(drm_fdinfo_print_size);

  int drm_memory_stats_is_zero(const struct drm_memory_stats *stats)
  {
@@ -868,17 +874,23 @@ void drm_print_memory_stats(struct drm_printer *p,
  			    enum drm_gem_object_status supported_status,
  			    const char *region)
  {
-	print_size(p, "total", region, stats->private + stats->shared);
-	print_size(p, "shared", region, stats->shared);
+	const char *prefix = "drm";
+
+	drm_fdinfo_print_size(p, prefix, "total", region,
+			      stats->private + stats->shared);
+	drm_fdinfo_print_size(p, prefix, "shared", region, stats->shared);

  	if (supported_status & DRM_GEM_OBJECT_ACTIVE)
-		print_size(p, "active", region, stats->active);
+		drm_fdinfo_print_size(p, prefix, "active", region,
+				      stats->active);

  	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
-		print_size(p, "resident", region, stats->resident);
+		drm_fdinfo_print_size(p, prefix, "resident", region,
+				      stats->resident);

  	if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
-		print_size(p, "purgeable", region, stats->purgeable);
+		drm_fdinfo_print_size(p, prefix, "purgeable", region,
+				      stats->purgeable);
  }
  EXPORT_SYMBOL(drm_print_memory_stats);

?

Regards,

Tvrtko

> +
> +	if (strnlen(stat, KEY_LEN) + strnlen(region, KEY_LEN) +
> +	    strlen(DRM_PREFIX) + 2 > KEY_LEN)
> +		return;
> +
> +	snprintf(key, sizeof(key), "%s-%s-%s", DRM_PREFIX, stat, region);
> +	print_size(p, key, sz);
> +}
> +
> +/**
> + * drm_driver_print_size - A helper to print driver-specific k:v pairs
> + * @p: The printer to print output to
> + * @file: DRM file private data
> + * @subkey: Name of the key minus the driver name
> + * @sz: Size value expressed in bytes
> + *
> + * Helper to display key:value pairs where the value is a numerical
> + * size expressed in bytes. It's useful for driver-internal memory
> + * whose objects aren't exposed to UM through a DRM handle.
> + */
> +void drm_driver_print_size(struct drm_printer *p, struct drm_file *file,
> +			   const char *subkey, u64 sz)
> +{
> +	char key[KEY_LEN+1] = {0};
> +	char *drv_name = file->minor->dev->driver->name;
> +
> +	if (strnlen(subkey, KEY_LEN) + strlen(drv_name) + 1 > KEY_LEN)
> +		return;
> +
> +	snprintf(key, sizeof(key), "%s-%s", drv_name, subkey);
> +	print_size(p, key, sz);
> +}
> +EXPORT_SYMBOL(drm_driver_print_size);
> +
> +#undef DRM_PREFIX
> +#undef KEY_LEN
> +
>   /**
>    * drm_print_memory_stats - A helper to print memory stats
>    * @p: The printer to print output to
> @@ -858,15 +902,15 @@ void drm_print_memory_stats(struct drm_printer *p,
>   			    enum drm_gem_object_status supported_status,
>   			    const char *region)
>   {
> -	print_size(p, "total", region, stats->private + stats->shared);
> -	print_size(p, "shared", region, stats->shared);
> -	print_size(p, "active", region, stats->active);
> +	print_size_region(p, "total", region, stats->private + stats->shared);
> +	print_size_region(p, "shared", region, stats->shared);
> +	print_size_region(p, "active", region, stats->active);
>   
>   	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
> -		print_size(p, "resident", region, stats->resident);
> +		print_size_region(p, "resident", region, stats->resident);
>   
>   	if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
> -		print_size(p, "purgeable", region, stats->purgeable);
> +		print_size_region(p, "purgeable", region, stats->purgeable);
>   }
>   EXPORT_SYMBOL(drm_print_memory_stats);
>   
> diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h
> index f0ef32e9fa5e..07da14859289 100644
> --- a/include/drm/drm_file.h
> +++ b/include/drm/drm_file.h
> @@ -494,6 +494,8 @@ struct drm_memory_stats {
>   
>   enum drm_gem_object_status;
>   
> +void drm_driver_print_size(struct drm_printer *p, struct drm_file *file,
> +			   const char *subkey, u64 sz);
>   void drm_print_memory_stats(struct drm_printer *p,
>   			    const struct drm_memory_stats *stats,
>   			    enum drm_gem_object_status supported_status,
Adrián Larumbe Jan. 9, 2025, 5:17 p.m. UTC | #2
On 09.01.2025 13:05, Tvrtko Ursulin wrote:
> On 08/01/2025 21:02, Adrián Larumbe wrote:
> > This is motivated by the desire of some dirvers (eg. Panthor) to print the
> > size of internal memory regions with a prefix that reflects the driver
> > name, as suggested in the previous documentation commit.
> > 
> > That means a minor refactoring of print_size() was needed so as to make it
> > more generic in the format of key strings it takes as input.
> > 
> > Signed-off-by: Adrián Larumbe <adrian.larumbe@collabora.com>
> > Cc: Tvrtko Ursulin <tursulin@ursulin.net>
> > ---
> >   drivers/gpu/drm/drm_file.c | 60 +++++++++++++++++++++++++++++++++-----
> >   include/drm/drm_file.h     |  2 ++
> >   2 files changed, 54 insertions(+), 8 deletions(-)
> > 
> > diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c
> > index cb5f22f5bbb6..5deae4cffa79 100644
> > --- a/drivers/gpu/drm/drm_file.c
> > +++ b/drivers/gpu/drm/drm_file.c
> > @@ -830,8 +830,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
> >   }
> >   EXPORT_SYMBOL(drm_send_event);
> > -static void print_size(struct drm_printer *p, const char *stat,
> > -		       const char *region, u64 sz)
> > +static void print_size(struct drm_printer *p, const char *key, u64 sz)
> >   {
> >   	const char *units[] = {"", " KiB", " MiB"};
> >   	unsigned u;
> > @@ -842,9 +841,54 @@ static void print_size(struct drm_printer *p, const char *stat,
> >   		sz = div_u64(sz, SZ_1K);
> >   	}
> > -	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
> > +	drm_printf(p, "%s:\t%llu%s\n", key, sz, units[u]);
> >   }
> > +#define KEY_LEN 1024
> > +#define DRM_PREFIX "drm"
> > +
> > +static void
> > +print_size_region(struct drm_printer *p, const char *stat,
> > +		  const char *region, u64 sz)
> > +{
> > +	char key[KEY_LEN+1] = {0};
> 
> A kilobyte of stack makes me a bit uneasy.
> 
> Would it not work to make print_size take a prefix and also avoid string
> operations? Something like, not compile tested:

This looks good to me, I had insisted we mandate a maximum key length, but
like you said on IRC there's truly no need for this.

> diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c
> index 2289e71e2fa2..efa4593babbc 100644
> --- a/drivers/gpu/drm/drm_file.c
> +++ b/drivers/gpu/drm/drm_file.c
> @@ -830,8 +830,12 @@ void drm_send_event(struct drm_device *dev, struct
> drm_pending_event *e)
>  }
>  EXPORT_SYMBOL(drm_send_event);
> 
> -static void print_size(struct drm_printer *p, const char *stat,
> -		       const char *region, u64 sz)
> +static void
> +drm_fdinfo_print_size(struct drm_printer *p,
> +		      const char *prefix,
> +		      const char *stat,
> +		      const char *region,
> +		      u64 sz)
>  {
>  	const char *units[] = {"", " KiB", " MiB"};
>  	unsigned u;
> @@ -842,8 +846,10 @@ static void print_size(struct drm_printer *p, const char
> *stat,
>  		sz = div_u64(sz, SZ_1K);
>  	}
> 
> -	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
> +	drm_printf(p, "%s-%s-%s:\t%llu%s\n",
> +		   prefix, stat, region, sz, units[u]);
>  }
> +EXPORT_SYMBOL(drm_fdinfo_print_size);
> 
>  int drm_memory_stats_is_zero(const struct drm_memory_stats *stats)
>  {
> @@ -868,17 +874,23 @@ void drm_print_memory_stats(struct drm_printer *p,
>  			    enum drm_gem_object_status supported_status,
>  			    const char *region)
>  {
> -	print_size(p, "total", region, stats->private + stats->shared);
> -	print_size(p, "shared", region, stats->shared);
> +	const char *prefix = "drm";
> +
> +	drm_fdinfo_print_size(p, prefix, "total", region,
> +			      stats->private + stats->shared);
> +	drm_fdinfo_print_size(p, prefix, "shared", region, stats->shared);
> 
>  	if (supported_status & DRM_GEM_OBJECT_ACTIVE)
> -		print_size(p, "active", region, stats->active);
> +		drm_fdinfo_print_size(p, prefix, "active", region,
> +				      stats->active);
> 
>  	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
> -		print_size(p, "resident", region, stats->resident);
> +		drm_fdinfo_print_size(p, prefix, "resident", region,
> +				      stats->resident);
> 
>  	if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
> -		print_size(p, "purgeable", region, stats->purgeable);
> +		drm_fdinfo_print_size(p, prefix, "purgeable", region,
> +				      stats->purgeable);
>  }
>  EXPORT_SYMBOL(drm_print_memory_stats);
> 
> ?
> 
> Regards,
> 
> Tvrtko
> 
> > +
> > +	if (strnlen(stat, KEY_LEN) + strnlen(region, KEY_LEN) +
> > +	    strlen(DRM_PREFIX) + 2 > KEY_LEN)
> > +		return;
> > +
> > +	snprintf(key, sizeof(key), "%s-%s-%s", DRM_PREFIX, stat, region);
> > +	print_size(p, key, sz);
> > +}
> > +
> > +/**
> > + * drm_driver_print_size - A helper to print driver-specific k:v pairs
> > + * @p: The printer to print output to
> > + * @file: DRM file private data
> > + * @subkey: Name of the key minus the driver name
> > + * @sz: Size value expressed in bytes
> > + *
> > + * Helper to display key:value pairs where the value is a numerical
> > + * size expressed in bytes. It's useful for driver-internal memory
> > + * whose objects aren't exposed to UM through a DRM handle.
> > + */
> > +void drm_driver_print_size(struct drm_printer *p, struct drm_file *file,
> > +			   const char *subkey, u64 sz)
> > +{
> > +	char key[KEY_LEN+1] = {0};
> > +	char *drv_name = file->minor->dev->driver->name;
> > +
> > +	if (strnlen(subkey, KEY_LEN) + strlen(drv_name) + 1 > KEY_LEN)
> > +		return;
> > +
> > +	snprintf(key, sizeof(key), "%s-%s", drv_name, subkey);
> > +	print_size(p, key, sz);
> > +}
> > +EXPORT_SYMBOL(drm_driver_print_size);
> > +
> > +#undef DRM_PREFIX
> > +#undef KEY_LEN
> > +
> >   /**
> >    * drm_print_memory_stats - A helper to print memory stats
> >    * @p: The printer to print output to
> > @@ -858,15 +902,15 @@ void drm_print_memory_stats(struct drm_printer *p,
> >   			    enum drm_gem_object_status supported_status,
> >   			    const char *region)
> >   {
> > -	print_size(p, "total", region, stats->private + stats->shared);
> > -	print_size(p, "shared", region, stats->shared);
> > -	print_size(p, "active", region, stats->active);
> > +	print_size_region(p, "total", region, stats->private + stats->shared);
> > +	print_size_region(p, "shared", region, stats->shared);
> > +	print_size_region(p, "active", region, stats->active);
> >   	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
> > -		print_size(p, "resident", region, stats->resident);
> > +		print_size_region(p, "resident", region, stats->resident);
> >   	if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
> > -		print_size(p, "purgeable", region, stats->purgeable);
> > +		print_size_region(p, "purgeable", region, stats->purgeable);
> >   }
> >   EXPORT_SYMBOL(drm_print_memory_stats);
> > diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h
> > index f0ef32e9fa5e..07da14859289 100644
> > --- a/include/drm/drm_file.h
> > +++ b/include/drm/drm_file.h
> > @@ -494,6 +494,8 @@ struct drm_memory_stats {
> >   enum drm_gem_object_status;
> > +void drm_driver_print_size(struct drm_printer *p, struct drm_file *file,
> > +			   const char *subkey, u64 sz);
> >   void drm_print_memory_stats(struct drm_printer *p,
> >   			    const struct drm_memory_stats *stats,
> >   			    enum drm_gem_object_status supported_status,

Adrian Larumbe
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c
index cb5f22f5bbb6..5deae4cffa79 100644
--- a/drivers/gpu/drm/drm_file.c
+++ b/drivers/gpu/drm/drm_file.c
@@ -830,8 +830,7 @@  void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
 }
 EXPORT_SYMBOL(drm_send_event);
 
-static void print_size(struct drm_printer *p, const char *stat,
-		       const char *region, u64 sz)
+static void print_size(struct drm_printer *p, const char *key, u64 sz)
 {
 	const char *units[] = {"", " KiB", " MiB"};
 	unsigned u;
@@ -842,9 +841,54 @@  static void print_size(struct drm_printer *p, const char *stat,
 		sz = div_u64(sz, SZ_1K);
 	}
 
-	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
+	drm_printf(p, "%s:\t%llu%s\n", key, sz, units[u]);
 }
 
+#define KEY_LEN 1024
+#define DRM_PREFIX "drm"
+
+static void
+print_size_region(struct drm_printer *p, const char *stat,
+		  const char *region, u64 sz)
+{
+	char key[KEY_LEN+1] = {0};
+
+	if (strnlen(stat, KEY_LEN) + strnlen(region, KEY_LEN) +
+	    strlen(DRM_PREFIX) + 2 > KEY_LEN)
+		return;
+
+	snprintf(key, sizeof(key), "%s-%s-%s", DRM_PREFIX, stat, region);
+	print_size(p, key, sz);
+}
+
+/**
+ * drm_driver_print_size - A helper to print driver-specific k:v pairs
+ * @p: The printer to print output to
+ * @file: DRM file private data
+ * @subkey: Name of the key minus the driver name
+ * @sz: Size value expressed in bytes
+ *
+ * Helper to display key:value pairs where the value is a numerical
+ * size expressed in bytes. It's useful for driver-internal memory
+ * whose objects aren't exposed to UM through a DRM handle.
+ */
+void drm_driver_print_size(struct drm_printer *p, struct drm_file *file,
+			   const char *subkey, u64 sz)
+{
+	char key[KEY_LEN+1] = {0};
+	char *drv_name = file->minor->dev->driver->name;
+
+	if (strnlen(subkey, KEY_LEN) + strlen(drv_name) + 1 > KEY_LEN)
+		return;
+
+	snprintf(key, sizeof(key), "%s-%s", drv_name, subkey);
+	print_size(p, key, sz);
+}
+EXPORT_SYMBOL(drm_driver_print_size);
+
+#undef DRM_PREFIX
+#undef KEY_LEN
+
 /**
  * drm_print_memory_stats - A helper to print memory stats
  * @p: The printer to print output to
@@ -858,15 +902,15 @@  void drm_print_memory_stats(struct drm_printer *p,
 			    enum drm_gem_object_status supported_status,
 			    const char *region)
 {
-	print_size(p, "total", region, stats->private + stats->shared);
-	print_size(p, "shared", region, stats->shared);
-	print_size(p, "active", region, stats->active);
+	print_size_region(p, "total", region, stats->private + stats->shared);
+	print_size_region(p, "shared", region, stats->shared);
+	print_size_region(p, "active", region, stats->active);
 
 	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
-		print_size(p, "resident", region, stats->resident);
+		print_size_region(p, "resident", region, stats->resident);
 
 	if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
-		print_size(p, "purgeable", region, stats->purgeable);
+		print_size_region(p, "purgeable", region, stats->purgeable);
 }
 EXPORT_SYMBOL(drm_print_memory_stats);
 
diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h
index f0ef32e9fa5e..07da14859289 100644
--- a/include/drm/drm_file.h
+++ b/include/drm/drm_file.h
@@ -494,6 +494,8 @@  struct drm_memory_stats {
 
 enum drm_gem_object_status;
 
+void drm_driver_print_size(struct drm_printer *p, struct drm_file *file,
+			   const char *subkey, u64 sz);
 void drm_print_memory_stats(struct drm_printer *p,
 			    const struct drm_memory_stats *stats,
 			    enum drm_gem_object_status supported_status,