Skip to contentSkip to conversation

drm/amdgpu: add shared fdinfo stats

PATCH In review Alex Deucher 4 participants 6 messages Nov 17, 2023 – Nov 30, 2023

Subsystems (2)
lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/1 drm/amdgpu: add shared fdinfo stats

Add shared stats. Useful for seeing shared memory.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
 3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 		   stats.requested_visible_vram/1024UL);
 	drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
 		   stats.requested_gtt/1024UL);
+	drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+	drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+	drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 		if (!usage[hw_ip])
 			continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +11 -0
index d79b4ca1ecfc..c24f7b2c04c1 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
 			  struct amdgpu_mem_stats *stats)
 {
 	uint64_t size = amdgpu_bo_size(bo);
+	struct drm_gem_object *obj;
 	unsigned int domain;
+	bool shared;
 
 	/* Abort if the BO doesn't currently have a backing store */
 	if (!bo->tbo.resource)
 		return;
 
+	obj = &bo->tbo.base;
+	shared = obj->handle_count > 1;
+
 	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
 	switch (domain) {
 	case AMDGPU_GEM_DOMAIN_VRAM:
 		stats->vram += size;
 		if (amdgpu_bo_in_cpu_visible_vram(bo))
 			stats->visible_vram += size;
+		if (shared)
+			stats->vram_shared += size;
 		break;
 	case AMDGPU_GEM_DOMAIN_GTT:
 		stats->gtt += size;
+		if (shared)
+			stats->gtt_shared += size;
 		break;
 	case AMDGPU_GEM_DOMAIN_CPU:
 	default:
 		stats->cpu += size;
+		if (shared)
+			stats->cpu_shared += size;
 		break;
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
 struct amdgpu_mem_stats {
 	/* current VRAM usage, includes visible VRAM */
 	uint64_t vram;
+	/* current shared VRAM usage, includes visible VRAM */
+	uint64_t vram_shared;
 	/* current visible VRAM usage */
 	uint64_t visible_vram;
 	/* current GTT usage */
 	uint64_t gtt;
+	/* current shared GTT usage */
+	uint64_t gtt_shared;
 	/* current system memory usage */
 	uint64_t cpu;
+	/* current shared system memory usage */
+	uint64_t cpu_shared;
 	/* sum of evicted buffers, includes visible VRAM */
 	uint64_t evicted_vram;
 	/* sum of evicted buffers due to CPU access */
Signature · 1 line
2.41.0
#1 lore raw

Am 17.11.23 um 20:56 schrieb Alex Deucher:

Quoted 32 lines

Add shared stats. Useful for seeing shared memory.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
  3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  		   stats.requested_visible_vram/1024UL);
  	drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
  		   stats.requested_gtt/1024UL);
+	drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+	drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+	drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
  	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
  		if (!usage[hw_ip])
  			continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index d79b4ca1ecfc..c24f7b2c04c1 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
  			  struct amdgpu_mem_stats *stats)
  {
  	uint64_t size = amdgpu_bo_size(bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +4 -0
+	struct drm_gem_object *obj;
  	unsigned int domain;
+	bool shared;
 
  	/* Abort if the BO doesn't currently have a backing store */
  	if (!bo->tbo.resource)
  		return;
 
+	obj = &bo->tbo.base;
+	shared = obj->handle_count > 1;

Interesting approach but I don't think that this is correct.

The handle_count is basically how many GEM handles are there for BO, so for example it doesn't catch sharing things with V4L.

What we should probably rather do is to take a look if bo->tbo.base.dma_buf is NULL or not.

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
  	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
  	switch (domain) {
  	case AMDGPU_GEM_DOMAIN_VRAM:
  		stats->vram += size;
  		if (amdgpu_bo_in_cpu_visible_vram(bo))
  			stats->visible_vram += size;
+		if (shared)
+			stats->vram_shared += size;
  		break;
  	case AMDGPU_GEM_DOMAIN_GTT:
  		stats->gtt += size;
+		if (shared)
+			stats->gtt_shared += size;
  		break;
  	case AMDGPU_GEM_DOMAIN_CPU:
  	default:
  		stats->cpu += size;
+		if (shared)
+			stats->cpu_shared += size;
  		break;
  	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
  struct amdgpu_mem_stats {
  	/* current VRAM usage, includes visible VRAM */
  	uint64_t vram;
+	/* current shared VRAM usage, includes visible VRAM */
+	uint64_t vram_shared;
  	/* current visible VRAM usage */
  	uint64_t visible_vram;
  	/* current GTT usage */
  	uint64_t gtt;
+	/* current shared GTT usage */
+	uint64_t gtt_shared;
  	/* current system memory usage */
  	uint64_t cpu;
+	/* current shared system memory usage */
+	uint64_t cpu_shared;
  	/* sum of evicted buffers, includes visible VRAM */
  	uint64_t evicted_vram;
  	/* sum of evicted buffers due to CPU access */
#2 lore raw
Quoted 47 lines · Christian König

Am 17.11.23 um 20:56 schrieb Alex Deucher:

Quoted 32 lines

Add shared stats. Useful for seeing shared memory.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
  3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
                 stats.requested_visible_vram/1024UL);
      drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
                 stats.requested_gtt/1024UL);
+     drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+     drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+     drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
      for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
              if (!usage[hw_ip])
                      continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index d79b4ca1ecfc..c24f7b2c04c1 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                        struct amdgpu_mem_stats *stats)
  {
      uint64_t size = amdgpu_bo_size(bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +4 -0
+     struct drm_gem_object *obj;
      unsigned int domain;
+     bool shared;
 
      /* Abort if the BO doesn't currently have a backing store */
      if (!bo->tbo.resource)
              return;
 
+     obj = &bo->tbo.base;
+     shared = obj->handle_count > 1;

Interesting approach but I don't think that this is correct.

Quoted 5 lines

The handle_count is basically how many GEM handles are there for BO, so for example it doesn't catch sharing things with V4L.

What we should probably rather do is to take a look if bo->tbo.base.dma_buf is NULL or not.

+Rob, dri-devel

This is what the generic drm helper code does. See drm_show_memory_stats(). If that is not correct that code should probably be fixed too.

Alex

Quoted 52 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
      domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
      switch (domain) {
      case AMDGPU_GEM_DOMAIN_VRAM:
              stats->vram += size;
              if (amdgpu_bo_in_cpu_visible_vram(bo))
                      stats->visible_vram += size;
+             if (shared)
+                     stats->vram_shared += size;
              break;
      case AMDGPU_GEM_DOMAIN_GTT:
              stats->gtt += size;
+             if (shared)
+                     stats->gtt_shared += size;
              break;
      case AMDGPU_GEM_DOMAIN_CPU:
      default:
              stats->cpu += size;
+             if (shared)
+                     stats->cpu_shared += size;
              break;
      }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
  struct amdgpu_mem_stats {
      /* current VRAM usage, includes visible VRAM */
      uint64_t vram;
+     /* current shared VRAM usage, includes visible VRAM */
+     uint64_t vram_shared;
      /* current visible VRAM usage */
      uint64_t visible_vram;
      /* current GTT usage */
      uint64_t gtt;
+     /* current shared GTT usage */
+     uint64_t gtt_shared;
      /* current system memory usage */
      uint64_t cpu;
+     /* current shared system memory usage */
+     uint64_t cpu_shared;
      /* sum of evicted buffers, includes visible VRAM */
      uint64_t evicted_vram;
      /* sum of evicted buffers due to CPU access */
#3 lore raw
Quoted 53 lines · Alex Deucher
Quoted 50 lines · Christian König

Am 17.11.23 um 20:56 schrieb Alex Deucher:

Quoted 32 lines

Add shared stats. Useful for seeing shared memory.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
  3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
                 stats.requested_visible_vram/1024UL);
      drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
                 stats.requested_gtt/1024UL);
+     drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+     drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+     drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
      for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
              if (!usage[hw_ip])
                      continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index d79b4ca1ecfc..c24f7b2c04c1 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                        struct amdgpu_mem_stats *stats)
  {
      uint64_t size = amdgpu_bo_size(bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +4 -0
+     struct drm_gem_object *obj;
      unsigned int domain;
+     bool shared;
 
      /* Abort if the BO doesn't currently have a backing store */
      if (!bo->tbo.resource)
              return;
 
+     obj = &bo->tbo.base;
+     shared = obj->handle_count > 1;

Interesting approach but I don't think that this is correct.

The handle_count is basically how many GEM handles are there for BO, so for example it doesn't catch sharing things with V4L.

Quoted 8 lines
Quoted 2 lines

What we should probably rather do is to take a look if bo->tbo.base.dma_buf is NULL or not.

+Rob, dri-devel

This is what the generic drm helper code does. See drm_show_memory_stats(). If that is not correct that code should probably be fixed too.

OTOH, v4l doesn't expose fdinfo. What "shared" is telling you is whether the BO is counted multiple times when you look at all processes fdinfo.

But I guess it would be ok to look for obj->handle_count > 1 || obj->dma_buf

BR,
-R

Quoted 55 lines

Alex

Quoted 52 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
      domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
      switch (domain) {
      case AMDGPU_GEM_DOMAIN_VRAM:
              stats->vram += size;
              if (amdgpu_bo_in_cpu_visible_vram(bo))
                      stats->visible_vram += size;
+             if (shared)
+                     stats->vram_shared += size;
              break;
      case AMDGPU_GEM_DOMAIN_GTT:
              stats->gtt += size;
+             if (shared)
+                     stats->gtt_shared += size;
              break;
      case AMDGPU_GEM_DOMAIN_CPU:
      default:
              stats->cpu += size;
+             if (shared)
+                     stats->cpu_shared += size;
              break;
      }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
  struct amdgpu_mem_stats {
      /* current VRAM usage, includes visible VRAM */
      uint64_t vram;
+     /* current shared VRAM usage, includes visible VRAM */
+     uint64_t vram_shared;
      /* current visible VRAM usage */
      uint64_t visible_vram;
      /* current GTT usage */
      uint64_t gtt;
+     /* current shared GTT usage */
+     uint64_t gtt_shared;
      /* current system memory usage */
      uint64_t cpu;
+     /* current shared system memory usage */
+     uint64_t cpu_shared;
      /* sum of evicted buffers, includes visible VRAM */
      uint64_t evicted_vram;
      /* sum of evicted buffers due to CPU access */
#4 lore raw

Am 28.11.23 um 18:52 schrieb Rob Clark:

Quoted 53 lines · Alex Deucher
Quoted 52 lines
Quoted 50 lines · Christian König

Am 17.11.23 um 20:56 schrieb Alex Deucher:

Quoted 32 lines

Add shared stats. Useful for seeing shared memory.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
   3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
                  stats.requested_visible_vram/1024UL);
       drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
                  stats.requested_gtt/1024UL);
+     drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+     drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+     drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
       for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
               if (!usage[hw_ip])
                       continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index d79b4ca1ecfc..c24f7b2c04c1 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                         struct amdgpu_mem_stats *stats)
   {
       uint64_t size = amdgpu_bo_size(bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +4 -0
+     struct drm_gem_object *obj;
       unsigned int domain;
+     bool shared;
 
       /* Abort if the BO doesn't currently have a backing store */
       if (!bo->tbo.resource)
               return;
 
+     obj = &bo->tbo.base;
+     shared = obj->handle_count > 1;

Interesting approach but I don't think that this is correct.

The handle_count is basically how many GEM handles are there for BO, so for example it doesn't catch sharing things with V4L.

What we should probably rather do is to take a look if bo->tbo.base.dma_buf is NULL or not.

Quoted 8 lines
Quoted 5 lines

+Rob, dri-devel

This is what the generic drm helper code does. See drm_show_memory_stats(). If that is not correct that code should probably be fixed too.

OTOH, v4l doesn't expose fdinfo. What "shared" is telling you is whether the BO is counted multiple times when you look at all processes fdinfo.

Oh, then that's not fully correct either.

You can have multiple handles for the same GEM object in a single client as well.

This for example happens when you interact with KMS to get an handle for a displayed BO.

DRM flink was one of the major other reasons, but I hope we are not using that widely any more.

What exactly is the purpose? To avoid counting a BO multiple times because you go over the handles in the common code?

If yes than I would say use obj->handle_count in the common code and ob->dma_buf in amdgpu because that is certainly unique.

Regards,
Christian.

Quoted 58 lines

But I guess it would be ok to look for obj->handle_count > 1 || obj->dma_buf

BR,
-R

Quoted 52 lines

Alex

Quoted 50 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
       domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
       switch (domain) {
       case AMDGPU_GEM_DOMAIN_VRAM:
               stats->vram += size;
               if (amdgpu_bo_in_cpu_visible_vram(bo))
                       stats->visible_vram += size;
+             if (shared)
+                     stats->vram_shared += size;
               break;
       case AMDGPU_GEM_DOMAIN_GTT:
               stats->gtt += size;
+             if (shared)
+                     stats->gtt_shared += size;
               break;
       case AMDGPU_GEM_DOMAIN_CPU:
       default:
               stats->cpu += size;
+             if (shared)
+                     stats->cpu_shared += size;
               break;
       }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
   struct amdgpu_mem_stats {
       /* current VRAM usage, includes visible VRAM */
       uint64_t vram;
+     /* current shared VRAM usage, includes visible VRAM */
+     uint64_t vram_shared;
       /* current visible VRAM usage */
       uint64_t visible_vram;
       /* current GTT usage */
       uint64_t gtt;
+     /* current shared GTT usage */
+     uint64_t gtt_shared;
       /* current system memory usage */
       uint64_t cpu;
+     /* current shared system memory usage */
+     uint64_t cpu_shared;
       /* sum of evicted buffers, includes visible VRAM */
       uint64_t evicted_vram;
       /* sum of evicted buffers due to CPU access */
#5 lore raw
Quoted 64 lines · Christian König

Am 28.11.23 um 18:52 schrieb Rob Clark:

Quoted 53 lines · Alex Deucher
Quoted 52 lines
Quoted 50 lines · Christian König

Am 17.11.23 um 20:56 schrieb Alex Deucher:

Quoted 32 lines

Add shared stats. Useful for seeing shared memory.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
   3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
                  stats.requested_visible_vram/1024UL);
       drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
                  stats.requested_gtt/1024UL);
+     drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+     drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+     drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
       for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
               if (!usage[hw_ip])
                       continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index d79b4ca1ecfc..c24f7b2c04c1 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                         struct amdgpu_mem_stats *stats)
   {
       uint64_t size = amdgpu_bo_size(bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +4 -0
+     struct drm_gem_object *obj;
       unsigned int domain;
+     bool shared;
 
       /* Abort if the BO doesn't currently have a backing store */
       if (!bo->tbo.resource)
               return;
 
+     obj = &bo->tbo.base;
+     shared = obj->handle_count > 1;

Interesting approach but I don't think that this is correct.

The handle_count is basically how many GEM handles are there for BO, so for example it doesn't catch sharing things with V4L.

What we should probably rather do is to take a look if bo->tbo.base.dma_buf is NULL or not.

Quoted 8 lines
Quoted 5 lines

+Rob, dri-devel

This is what the generic drm helper code does. See drm_show_memory_stats(). If that is not correct that code should probably be fixed too.

OTOH, v4l doesn't expose fdinfo. What "shared" is telling you is whether the BO is counted multiple times when you look at all processes fdinfo.

Quoted 7 lines

Oh, then that's not fully correct either.

You can have multiple handles for the same GEM object in a single client as well.

This for example happens when you interact with KMS to get an handle for a displayed BO.

so, the handle is unique per drm_file which is (at least usually) unique per process. The handle_count is agnostic to _how_ you got the handle (ie. via flink or dma-buf)

Quoted 8 lines

DRM flink was one of the major other reasons, but I hope we are not using that widely any more.

What exactly is the purpose? To avoid counting a BO multiple times because you go over the handles in the common code?

If yes than I would say use obj->handle_count in the common code and ob->dma_buf in amdgpu because that is certainly unique.

Because the drm_file is (usually) unique per process, the purpose was to show the amount of memory that is getting counted against multiple processes. The intention behind using handle_count was just that it didn't care _how_ the buffer was shared, just that it is mapped into more than a single drm_file.

Maybe amd userspace is doing something unique that I'm not aware of?

BR,
-R

Quoted 62 lines

Regards,
Christian.

Quoted 58 lines

But I guess it would be ok to look for obj->handle_count > 1 || obj->dma_buf

BR,
-R

Quoted 52 lines

Alex

Quoted 50 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
       domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
       switch (domain) {
       case AMDGPU_GEM_DOMAIN_VRAM:
               stats->vram += size;
               if (amdgpu_bo_in_cpu_visible_vram(bo))
                       stats->visible_vram += size;
+             if (shared)
+                     stats->vram_shared += size;
               break;
       case AMDGPU_GEM_DOMAIN_GTT:
               stats->gtt += size;
+             if (shared)
+                     stats->gtt_shared += size;
               break;
       case AMDGPU_GEM_DOMAIN_CPU:
       default:
               stats->cpu += size;
+             if (shared)
+                     stats->cpu_shared += size;
               break;
       }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
   struct amdgpu_mem_stats {
       /* current VRAM usage, includes visible VRAM */
       uint64_t vram;
+     /* current shared VRAM usage, includes visible VRAM */
+     uint64_t vram_shared;
       /* current visible VRAM usage */
       uint64_t visible_vram;
       /* current GTT usage */
       uint64_t gtt;
+     /* current shared GTT usage */
+     uint64_t gtt_shared;
       /* current system memory usage */
       uint64_t cpu;
+     /* current shared system memory usage */
+     uint64_t cpu_shared;
       /* sum of evicted buffers, includes visible VRAM */
       uint64_t evicted_vram;
       /* sum of evicted buffers due to CPU access */