Skip to contentSkip to conversation

rework bo mem stats tracking

PATCH Superseded Yunxiang Li 2 participants 12 messages Nov 28, 2024 – Dec 4, 2024

Subsystems (6)

v9 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v9 cover rework bo mem stats tracking

Right now every time the fdinfo is read, we go through the vm lists and lock all the BOs to calcuate the statistics. This causes a lot of lock contention when the VM is actively used. It gets worse if there is a lot of shared BOs or if there's a lot of submissions. We have seen submissions lock-up for seconds due to fdinfo for some workload. Therefore, rework the implementation to track the BOs as they get moved around.

The amd-only visible memory stat is removed to simplify implementation, it's unclear how useful this stat is since kernel map/unmap BOs whenever it wants to and on a modern system all of VRAM can be mapped if needed.

v5: rebase on top of the drm_print_memory_stats refactor
v6: split the drm changes into a seperate patch for drm-devel review, fix handling of drm-total- vs drm-resident- and handle drm-purgable-.
v7: make drm-active- optional
v8: clearify documentation, minor tweaks, and some bug fixes found during testing
v9: documentation fix as suggested, no functional change

Yunxiang Li (5):
drm: add drm_memory_stats_is_zero
drm: make drm-active- stats optional Documentation/gpu: Clarify drm memory stats definition drm/amdgpu: remove unused function parameter drm/amdgpu: track bo memory stats at runtime

 Documentation/gpu/drm-usage-stats.rst       |  54 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  17 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c  |  17 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c     |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  | 111 +++++-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h  |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 179 ++++++++++++++------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |  20 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c   |   1 +
 drivers/gpu/drm/drm_file.c                  |  23 ++-
 drivers/gpu/drm/i915/i915_drm_client.c      |   1 +
 drivers/gpu/drm/xe/xe_drm_client.c          |   1 +
 include/drm/drm_file.h                      |   1 +
 include/drm/drm_gem.h                       |  14 +-
 16 files changed, 265 insertions(+), 189 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v9 1/5 drm: add drm_memory_stats_is_zero

Add a helper to check if the memory stats is zero, this will be used to check for memory accounting errors.

---
 drivers/gpu/drm/drm_file.c | 10 ++++++++++
 include/drm/drm_file.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_file.c +10 -0
index 714e42b051080..e285fcc28c59c 100644
@@ -859,6 +859,16 @@ static void print_size(struct drm_printer *p, const char *stat,
 	drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]);
 }
 
+int drm_memory_stats_is_zero(const struct drm_memory_stats *stats)
+{
+	return (stats->shared == 0 &&
+		stats->private == 0 &&
+		stats->resident == 0 &&
+		stats->purgeable == 0 &&
+		stats->active == 0);
+}
+EXPORT_SYMBOL(drm_memory_stats_is_zero);
+
 /**
  * drm_print_memory_stats - A helper to print memory stats
  * @p: The printer to print output to
include/drm/drm_file.h +1 -0
index ab230d3af138d..7f91e35d027d9 100644
@@ -477,6 +477,7 @@ struct drm_memory_stats {
 
 enum drm_gem_object_status;
 
+int drm_memory_stats_is_zero(const struct drm_memory_stats *stats);
 void drm_print_memory_stats(struct drm_printer *p,
 			    const struct drm_memory_stats *stats,
 			    enum drm_gem_object_status supported_status,
Signature · 1 line
2.34.1
#2 lore raw

PATCH v9 2/5 drm: make drm-active- stats optional

In reply to Yunxiang Li

When memory stats is generated fresh everytime by going though all the BOs, their active information is quite easy to get. But if the stats are tracked with BO's state this becomes harder since the job scheduling part doesn't really deal with individual buffers.

Make drm-active- optional to enable amdgpu to switch to the second method.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  1 +
 drivers/gpu/drm/drm_file.c                 | 13 +++++++------
 drivers/gpu/drm/i915/i915_drm_client.c     |  1 +
 drivers/gpu/drm/xe/xe_drm_client.c         |  1 +
 include/drm/drm_gem.h                      | 14 ++++++++------
 5 files changed, 18 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -0
index df2cf5c339255..7717e3e4f05b5 100644
@@ -97,6 +97,7 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 
 		drm_print_memory_stats(p,
 				       &stats[i].drm,
+				       DRM_GEM_OBJECT_ACTIVE |
 				       DRM_GEM_OBJECT_RESIDENT |
 				       DRM_GEM_OBJECT_PURGEABLE,
 				       pl_name[i]);
drivers/gpu/drm/drm_file.c +7 -6
index e285fcc28c59c..fd06671054723 100644
@@ -884,7 +884,9 @@ void drm_print_memory_stats(struct drm_printer *p,
 {
 	print_size(p, "total", region, stats->private + stats->shared);
 	print_size(p, "shared", region, stats->shared);
-	print_size(p, "active", region, stats->active);
+
+	if (supported_status & DRM_GEM_OBJECT_ACTIVE)
+		print_size(p, "active", region, stats->active);
 
 	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
 		print_size(p, "resident", region, stats->resident);
@@ -917,15 +919,13 @@ void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
 
 		if (obj->funcs && obj->funcs->status) {
 			s = obj->funcs->status(obj);
-			supported_status = DRM_GEM_OBJECT_RESIDENT |
-					DRM_GEM_OBJECT_PURGEABLE;
+			supported_status |= s;
 		}
 
-		if (drm_gem_object_is_shared_for_memory_stats(obj)) {
+		if (drm_gem_object_is_shared_for_memory_stats(obj))
 			status.shared += obj->size;
-		} else {
+		else
 			status.private += obj->size;
-		}
 
 		if (s & DRM_GEM_OBJECT_RESIDENT) {
 			status.resident += add_size;
@@ -938,6 +938,7 @@ void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
 
 		if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
 			status.active += add_size;
+			supported_status |= DRM_GEM_OBJECT_ACTIVE;
 
 			/* If still active, don't count as purgeable: */
 			s &= ~DRM_GEM_OBJECT_PURGEABLE;
drivers/gpu/drm/i915/i915_drm_client.c +1 -0
index f586825054918..168d7375304bc 100644
@@ -102,6 +102,7 @@ static void show_meminfo(struct drm_printer *p, struct drm_file *file)
 	for_each_memory_region(mr, i915, id)
 		drm_print_memory_stats(p,
 				       &stats[id],
+				       DRM_GEM_OBJECT_ACTIVE |
 				       DRM_GEM_OBJECT_RESIDENT |
 				       DRM_GEM_OBJECT_PURGEABLE,
 				       mr->uabi_name);
drivers/gpu/drm/xe/xe_drm_client.c +1 -0
index 6a26923fa10e0..54941b4e850c4 100644
@@ -229,6 +229,7 @@ static void show_meminfo(struct drm_printer *p, struct drm_file *file)
 		if (man) {
 			drm_print_memory_stats(p,
 					       &stats[mem_type],
+					       DRM_GEM_OBJECT_ACTIVE |
 					       DRM_GEM_OBJECT_RESIDENT |
 					       (mem_type != XE_PL_SYSTEM ? 0 :
 					       DRM_GEM_OBJECT_PURGEABLE),
include/drm/drm_gem.h +8 -6
index bae4865b2101a..da11c16e212aa 100644
@@ -48,19 +48,21 @@ 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
+ * @DRM_GEM_OBJECT_ACTIVE: object is currently used by an active submission
  *
  * 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
+ * and drm_show_fdinfo().  Note that an object can report DRM_GEM_OBJECT_PURGEABLE
+ * and be 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.)
+ * is idle and resident to return this bit, i.e. userspace can mark a buffer as
+ * purgeable even while it is still busy on the GPU. It will not get reported in
+ * the puregeable stats 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),
+	DRM_GEM_OBJECT_ACTIVE    = BIT(2),
 };
 
 /**
Signature · 1 line
2.34.1
Reviewed-by on v9 2/5
#3 lore raw

Re: [PATCH v9 2/5] drm: make drm-active- stats optional

Quoted 120 lines · Yunxiang Li

When memory stats is generated fresh everytime by going though all the BOs, their active information is quite easy to get. But if the stats are tracked with BO's state this becomes harder since the job scheduling part doesn't really deal with individual buffers.

Make drm-active- optional to enable amdgpu to switch to the second method.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  1 +
  drivers/gpu/drm/drm_file.c                 | 13 +++++++------
  drivers/gpu/drm/i915/i915_drm_client.c     |  1 +
  drivers/gpu/drm/xe/xe_drm_client.c         |  1 +
  include/drm/drm_gem.h                      | 14 ++++++++------
  5 files changed, 18 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -0
index df2cf5c339255..7717e3e4f05b5 100644
@@ -97,6 +97,7 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 
  		drm_print_memory_stats(p,
  				       &stats[i].drm,
+				       DRM_GEM_OBJECT_ACTIVE |
  				       DRM_GEM_OBJECT_RESIDENT |
  				       DRM_GEM_OBJECT_PURGEABLE,
  				       pl_name[i]);
drivers/gpu/drm/drm_file.c +7 -6
index e285fcc28c59c..fd06671054723 100644
@@ -884,7 +884,9 @@ void drm_print_memory_stats(struct drm_printer *p,
  {
  	print_size(p, "total", region, stats->private + stats->shared);
  	print_size(p, "shared", region, stats->shared);
-	print_size(p, "active", region, stats->active);
+
+	if (supported_status & DRM_GEM_OBJECT_ACTIVE)
+		print_size(p, "active", region, stats->active);
 
  	if (supported_status & DRM_GEM_OBJECT_RESIDENT)
  		print_size(p, "resident", region, stats->resident);
@@ -917,15 +919,13 @@ void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
 
  		if (obj->funcs && obj->funcs->status) {
  			s = obj->funcs->status(obj);
-			supported_status = DRM_GEM_OBJECT_RESIDENT |
-					DRM_GEM_OBJECT_PURGEABLE;
+			supported_status |= s;
  		}
 
-		if (drm_gem_object_is_shared_for_memory_stats(obj)) {
+		if (drm_gem_object_is_shared_for_memory_stats(obj))
  			status.shared += obj->size;
-		} else {
+		else
  			status.private += obj->size;
-		}
 
  		if (s & DRM_GEM_OBJECT_RESIDENT) {
  			status.resident += add_size;
@@ -938,6 +938,7 @@ void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
 
  		if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
  			status.active += add_size;
+			supported_status |= DRM_GEM_OBJECT_ACTIVE;
 
  			/* If still active, don't count as purgeable: */
  			s &= ~DRM_GEM_OBJECT_PURGEABLE;
drivers/gpu/drm/i915/i915_drm_client.c +1 -0
index f586825054918..168d7375304bc 100644
@@ -102,6 +102,7 @@ static void show_meminfo(struct drm_printer *p, struct drm_file *file)
  	for_each_memory_region(mr, i915, id)
  		drm_print_memory_stats(p,
  				       &stats[id],
+				       DRM_GEM_OBJECT_ACTIVE |
  				       DRM_GEM_OBJECT_RESIDENT |
  				       DRM_GEM_OBJECT_PURGEABLE,
  				       mr->uabi_name);
drivers/gpu/drm/xe/xe_drm_client.c +1 -0
index 6a26923fa10e0..54941b4e850c4 100644
@@ -229,6 +229,7 @@ static void show_meminfo(struct drm_printer *p, struct drm_file *file)
  		if (man) {
  			drm_print_memory_stats(p,
  					       &stats[mem_type],
+					       DRM_GEM_OBJECT_ACTIVE |
  					       DRM_GEM_OBJECT_RESIDENT |
  					       (mem_type != XE_PL_SYSTEM ? 0 :
  					       DRM_GEM_OBJECT_PURGEABLE),
include/drm/drm_gem.h +5 -6
index bae4865b2101a..da11c16e212aa 100644
@@ -48,19 +48,21 @@ 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
+ * @DRM_GEM_OBJECT_ACTIVE: object is currently used by an active submission
   *
   * 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
+ * and drm_show_fdinfo().  Note that an object can report DRM_GEM_OBJECT_PURGEABLE
+ * and be 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.)
+ * is idle and resident to return this bit, i.e. userspace can mark a buffer as
+ * purgeable even while it is still busy on the GPU. It will not get reported in

include/drm/drm_gem.h struct drm_gem_object; · line 68

Quoted 10 lines
include/drm/drm_gem.h +3 -0
+ * the puregeable stats 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),
+	DRM_GEM_OBJECT_ACTIVE    = BIT(2),
  };
 
  /**

Regards,

Tvrtko

#4 lore raw

PATCH v9 3/5 Documentation/gpu: Clarify drm memory stats definition

In reply to Yunxiang Li

Define how to handle buffers with multiple possible placement so we don't get incompatible implementations. Callout the resident requirement for drm-purgeable- explicitly. Remove the confusing requirement for there to be only drm-memory- or only drm-resident-, and clarify that drm-memory- is legacy, amdgpu only behavior. Re-order the paragraphs to flow better as well.

---
 Documentation/gpu/drm-usage-stats.rst | 54 +++++++++++++--------------
 1 file changed, 27 insertions(+), 27 deletions(-)
Documentation/gpu/drm-usage-stats.rst +27 -27
index ff964c707754a..19a5323d0e682 100644
@@ -140,57 +140,57 @@ both.
 Memory
 ^^^^^^
 
-- drm-memory-<region>: <uint> [KiB|MiB]
-
-Each possible memory type which can be used to store buffer objects by the
-GPU in question shall be given a stable and unique name to be returned as the
-string here.
+Each possible memory type which can be used to store buffer objects by the GPU
+in question shall be given a stable and unique name to be used as the "<region>"
+string.
 
 The region name "memory" is reserved to refer to normal system memory.
 
-Value shall reflect the amount of storage currently consumed by the buffer
+The value shall reflect the amount of storage currently consumed by the buffer
 objects 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.
 
-This key is deprecated and is an alias for drm-resident-<region>. Only one of
-the two should be present in the output.
-
-- drm-shared-<region>: <uint> [KiB|MiB]
+- drm-total-<region>: <uint> [KiB|MiB]
 
-The total size of buffers that are shared with another file (e.g., have more
-than a single handle).
+The total size of all requested buffers, including both shared and private
+memory. The backing store for the buffers does not need to be currently
+instantiated to count under this category. To avoid double-counting, if a buffer
+has multiple regions where it can be allocated to, the implementation should
+consistently select a single region for accounting purposes.
 
-- drm-total-<region>: <uint> [KiB|MiB]
+- drm-shared-<region>: <uint> [KiB|MiB]
 
-The total size of all created buffers including shared and private memory. The
-backing store for the buffers does not have to be currently instantiated to be
-counted under this category.
+The total size of buffers that are shared with another file (i.e., have more
+than one handle). The same requirement to avoid double-counting that applies to
+drm-total-<region> also applies here.
 
 - drm-resident-<region>: <uint> [KiB|MiB]
 
-The total size of buffers that are resident (have their backing store present or
-instantiated) in the specified region.
+The total size of buffers that are resident (i.e., have their backing store
+present or instantiated) in the specified region.
+
+- drm-memory-<region>: <uint> [KiB|MiB]
 
-This is an alias for drm-memory-<region> and only one of the two should be
-present in the output.
+This key is deprecated and is only printed by amdgpu; it is an alias for
+drm-resident-<region>.
 
 - drm-purgeable-<region>: <uint> [KiB|MiB]
 
-The total size of buffers that are purgeable.
+The total size of buffers that are resident and purgeable.
 
-For example drivers which implement a form of 'madvise' like functionality can
-here count buffers which have instantiated backing store, but have been marked
-with an equivalent of MADV_DONTNEED.
+For example, drivers that implement functionality similar to 'madvise' can count
+buffers that have instantiated backing stores but have been marked with an
+equivalent of MADV_DONTNEED.
 
 - drm-active-<region>: <uint> [KiB|MiB]
 
 The total size of buffers that are active on one or more engines.
 
-One practical example of this can be presence of unsignaled fences in an GEM
-buffer reservation object. Therefore the active category is a subset of
-resident.
+One practical example of this could be the presence of unsignaled fences in a
+GEM buffer reservation object. Therefore, the active category is a subset of the
+resident category.
 
 Implementation Details
 ======================
Signature · 1 line
2.34.1
Reviewed-by on v9 3/5
#5 lore raw

Re: [PATCH v9 3/5] Documentation/gpu: Clarify drm memory stats definition

Quoted 92 lines · Yunxiang Li

Define how to handle buffers with multiple possible placement so we don't get incompatible implementations. Callout the resident requirement for drm-purgeable- explicitly. Remove the confusing requirement for there to be only drm-memory- or only drm-resident-, and clarify that drm-memory- is legacy, amdgpu only behavior. Re-order the paragraphs to flow better as well.

---
  Documentation/gpu/drm-usage-stats.rst | 54 +++++++++++++--------------
  1 file changed, 27 insertions(+), 27 deletions(-)
Documentation/gpu/drm-usage-stats.rst +24 -24
index ff964c707754a..19a5323d0e682 100644
@@ -140,57 +140,57 @@ both.
  Memory
  ^^^^^^
 
-- drm-memory-<region>: <uint> [KiB|MiB]
-
-Each possible memory type which can be used to store buffer objects by the
-GPU in question shall be given a stable and unique name to be returned as the
-string here.
+Each possible memory type which can be used to store buffer objects by the GPU
+in question shall be given a stable and unique name to be used as the "<region>"
+string.
 
  The region name "memory" is reserved to refer to normal system memory.
 
-Value shall reflect the amount of storage currently consumed by the buffer
+The value shall reflect the amount of storage currently consumed by the buffer
  objects 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.
 
-This key is deprecated and is an alias for drm-resident-<region>. Only one of
-the two should be present in the output.
-
-- drm-shared-<region>: <uint> [KiB|MiB]
+- drm-total-<region>: <uint> [KiB|MiB]
 
-The total size of buffers that are shared with another file (e.g., have more
-than a single handle).
+The total size of all requested buffers, including both shared and private
+memory. The backing store for the buffers does not need to be currently
+instantiated to count under this category. To avoid double-counting, if a buffer
+has multiple regions where it can be allocated to, the implementation should
+consistently select a single region for accounting purposes.
 
-- drm-total-<region>: <uint> [KiB|MiB]
+- drm-shared-<region>: <uint> [KiB|MiB]
 
-The total size of all created buffers including shared and private memory. The
-backing store for the buffers does not have to be currently instantiated to be
-counted under this category.
+The total size of buffers that are shared with another file (i.e., have more
+than one handle). The same requirement to avoid double-counting that applies to
+drm-total-<region> also applies here.
 
  - drm-resident-<region>: <uint> [KiB|MiB]
 
-The total size of buffers that are resident (have their backing store present or
-instantiated) in the specified region.
+The total size of buffers that are resident (i.e., have their backing store
+present or instantiated) in the specified region.
+
+- drm-memory-<region>: <uint> [KiB|MiB]
 
-This is an alias for drm-memory-<region> and only one of the two should be
-present in the output.
+This key is deprecated and is only printed by amdgpu; it is an alias for
+drm-resident-<region>.
 
  - drm-purgeable-<region>: <uint> [KiB|MiB]
 
-The total size of buffers that are purgeable.
+The total size of buffers that are resident and purgeable.
 
-For example drivers which implement a form of 'madvise' like functionality can
-here count buffers which have instantiated backing store, but have been marked
-with an equivalent of MADV_DONTNEED.
+For example, drivers that implement functionality similar to 'madvise' can count
+buffers that have instantiated backing stores but have been marked with an
+equivalent of MADV_DONTNEED.
 
  - drm-active-<region>: <uint> [KiB|MiB]
 
  The total size of buffers that are active on one or more engines.

Documentation/gpu/drm-usage-stats.rst both. · line 196

Quoted 10 lines
Documentation/gpu/drm-usage-stats.rst +3 -3
 
-One practical example of this can be presence of unsignaled fences in an GEM
-buffer reservation object. Therefore the active category is a subset of
-resident.
+One practical example of this could be the presence of unsignaled fences in a
+GEM buffer reservation object. Therefore, the active category is a subset of the
+resident category.
 
  Implementation Details
  ======================

Regards,

Tvrtko

#6 lore raw

PATCH v9 4/5 drm/amdgpu: remove unused function parameter

In reply to Yunxiang Li

amdgpu_vm_bo_invalidate doesn't use the adev parameter and not all callers have a reference to adev handy, so remove it for cleanliness.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 4 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c     | 3 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  | 3 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 4 +---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      | 3 +--
 6 files changed, 7 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -2
index d891ab779ca7f..471f3dc81e8db 100644
@@ -1105,7 +1105,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	 * We can't use gang submit on with reserved VMIDs when the VM changes
 	 * can't be invalidated by more than one engine at the same time.
 	 */
-	if (p->gang_size > 1 && !p->adev->vm_manager.concurrent_flush) {
+	if (p->gang_size > 1 && !adev->vm_manager.concurrent_flush) {
 		for (i = 0; i < p->gang_size; ++i) {
 			struct drm_sched_entity *entity = p->entities[i];
 			struct drm_gpu_scheduler *sched = entity->rq->sched;
@@ -1189,7 +1189,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 			if (!bo)
 				continue;
 
-			amdgpu_vm_bo_invalidate(adev, bo, false);
+			amdgpu_vm_bo_invalidate(bo, false);
 		}
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index 8e81a83d37d84..b144404902255 100644
@@ -345,7 +345,7 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)
 	/* FIXME: This should be after the "if", but needs a fix to make sure
 	 * DMABuf imports are initialized in the right VM list.
 	 */
-	amdgpu_vm_bo_invalidate(adev, bo, false);
+	amdgpu_vm_bo_invalidate(bo, false);
 	if (!bo->tbo.resource || bo->tbo.resource->mem_type == TTM_PL_SYSTEM)
 		return;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -2
index 63e2cb1ab56ea..fe7ae45500639 100644
@@ -977,7 +977,6 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *filp)
 {
-	struct amdgpu_device *adev = drm_to_adev(dev);
 	struct drm_amdgpu_gem_op *args = data;
 	struct drm_gem_object *gobj;
 	struct amdgpu_vm_bo_base *base;
@@ -1037,7 +1036,7 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 			robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
 
 		if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
-			amdgpu_vm_bo_invalidate(adev, robj, true);
+			amdgpu_vm_bo_invalidate(robj, true);
 
 		amdgpu_bo_unreserve(robj);
 		break;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -2
index 4f057996ef35b..951b20e40fd35 100644
@@ -1251,7 +1251,6 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
 			   bool evict,
 			   struct ttm_resource *new_mem)
 {
-	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
 	struct ttm_resource *old_mem = bo->resource;
 	struct amdgpu_bo *abo;
 
@@ -1259,7 +1258,7 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
 		return;
 
 	abo = ttm_to_amdgpu_bo(bo);
-	amdgpu_vm_bo_invalidate(adev, abo, evict);
+	amdgpu_vm_bo_invalidate(abo, evict);
 
 	amdgpu_bo_kunmap(abo);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -3
index 68dd4130b5ad9..d0db155a9ab7c 100644
@@ -2143,14 +2143,12 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
 /**
  * amdgpu_vm_bo_invalidate - mark the bo as invalid
  *
- * @adev: amdgpu_device pointer
  * @bo: amdgpu buffer object
  * @evicted: is the BO evicted
  *
  * Mark @bo as invalid.
  */
-void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
-			     struct amdgpu_bo *bo, bool evicted)
+void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted)
 {
 	struct amdgpu_vm_bo_base *bo_base;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -2
index 5d119ac26c4fe..6a1b344e15e1b 100644
@@ -524,8 +524,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 			struct amdgpu_bo_va *bo_va,
 			bool clear);
 bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
-void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
-			     struct amdgpu_bo *bo, bool evicted);
+void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted);
 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
 				       struct amdgpu_bo *bo);
Signature · 1 line
2.34.1
#7 lore raw

PATCH v9 5/5 drm/amdgpu: track bo memory stats at runtime

In reply to Yunxiang Li

Before, every time fdinfo is queried we try to lock all the BOs in the VM and calculate memory usage from scratch. This works okay if the fdinfo is rarely read and the VMs don't have a ton of BOs. If either of these conditions is not true, we get a massive performance hit.

In this new revision, we track the BOs as they change states. This way when the fdinfo is queried we only need to take the status lock and copy out the usage stats with minimal impact to the runtime performance. With this new approach however, we would no longer be able to track active buffers.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  15 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c  |  18 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  | 110 +++++-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h  |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 175 +++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |  17 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c   |   1 +
 8 files changed, 204 insertions(+), 140 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +13 -2
index b144404902255..423cea4e571ee 100644
@@ -36,6 +36,7 @@
 #include "amdgpu_gem.h"
 #include "amdgpu_dma_buf.h"
 #include "amdgpu_xgmi.h"
+#include "amdgpu_vm.h"
 #include <drm/amdgpu_drm.h>
 #include <drm/ttm/ttm_tt.h>
 #include <linux/dma-buf.h>
@@ -190,6 +191,14 @@ static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach,
 	}
 }
 
+static void amdgpu_dma_buf_release(struct dma_buf *buf)
+{
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(buf->priv);
+
+	amdgpu_vm_bo_update_shared(bo, -1);
+	drm_gem_dmabuf_release(buf);
+}
+
 /**
  * amdgpu_dma_buf_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
  * @dma_buf: Shared DMA buffer
@@ -237,7 +246,7 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = {
 	.unpin = amdgpu_dma_buf_unpin,
 	.map_dma_buf = amdgpu_dma_buf_map,
 	.unmap_dma_buf = amdgpu_dma_buf_unmap,
-	.release = drm_gem_dmabuf_release,
+	.release = amdgpu_dma_buf_release,
 	.begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
 	.mmap = drm_gem_dmabuf_mmap,
 	.vmap = drm_gem_dmabuf_vmap,
@@ -265,8 +274,10 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
 		return ERR_PTR(-EPERM);
 
 	buf = drm_gem_prime_export(gobj, flags);
-	if (!IS_ERR(buf))
+	if (!IS_ERR(buf)) {
 		buf->ops = &amdgpu_dmabuf_ops;
+		amdgpu_vm_bo_update_shared(bo, +1);
+	}
 
 	return buf;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +6 -12
index 7717e3e4f05b5..e625e2481747e 100644
@@ -60,7 +60,7 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 	struct amdgpu_fpriv *fpriv = file->driver_priv;
 	struct amdgpu_vm *vm = &fpriv->vm;
 
-	struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST + 1] = { };
+	struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
 	ktime_t usage[AMDGPU_HW_IP_NUM];
 	const char *pl_name[] = {
 		[TTM_PL_VRAM] = "vram",
@@ -72,15 +72,8 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 		[AMDGPU_PL_DOORBELL] = "doorbell",
 	};
 	unsigned int hw_ip, i;
-	int ret;
-
-	ret = amdgpu_bo_reserve(vm->root.bo, false);
-	if (ret)
-		return;
-
-	amdgpu_vm_get_memory(vm, stats, ARRAY_SIZE(stats));
-	amdgpu_bo_unreserve(vm->root.bo);
 
+	amdgpu_vm_get_memory(vm, stats);
 	amdgpu_ctx_mgr_usage(&fpriv->ctx_mgr, usage);
 
 	/*
@@ -97,7 +90,6 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 
 		drm_print_memory_stats(p,
 				       &stats[i].drm,
-				       DRM_GEM_OBJECT_ACTIVE |
 				       DRM_GEM_OBJECT_RESIDENT |
 				       DRM_GEM_OBJECT_PURGEABLE,
 				       pl_name[i]);
@@ -115,9 +107,11 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 	drm_printf(p, "amd-evicted-vram:\t%llu KiB\n",
 		   stats[TTM_PL_VRAM].evicted/1024UL);
 	drm_printf(p, "amd-requested-vram:\t%llu KiB\n",
-		   stats[TTM_PL_VRAM].requested/1024UL);
+		   (stats[TTM_PL_VRAM].drm.shared +
+		    stats[TTM_PL_VRAM].drm.private) / 1024UL);
 	drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
-		   stats[TTM_PL_TT].requested/1024UL);
+		   (stats[TTM_PL_TT].drm.shared +
+		    stats[TTM_PL_TT].drm.private) / 1024UL);
 
 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 		if (!usage[hw_ip])
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +40 -70

128 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -3
index ce3314152d20f..bdc9a5bc4da46 100644
@@ -305,9 +305,7 @@ int amdgpu_bo_sync_wait_resv(struct amdgpu_device *adev, struct dma_resv *resv,
 int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr);
 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo);
 u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo);
-void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-			  struct amdgpu_mem_stats *stats,
-			  unsigned int size);
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo);
 uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev,
 					    uint32_t domain);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +2 -2
index 2852a6064c9ac..86cb83fb2283c 100644
@@ -26,15 +26,15 @@
 
 #include <linux/dma-direction.h>
 #include <drm/gpu_scheduler.h>
+#include <drm/ttm/ttm_placement.h>
 #include "amdgpu_vram_mgr.h"
-#include "amdgpu.h"
 
 #define AMDGPU_PL_GDS		(TTM_PL_PRIV + 0)
 #define AMDGPU_PL_GWS		(TTM_PL_PRIV + 1)
 #define AMDGPU_PL_OA		(TTM_PL_PRIV + 2)
 #define AMDGPU_PL_PREEMPT	(TTM_PL_PRIV + 3)
 #define AMDGPU_PL_DOORBELL	(TTM_PL_PRIV + 4)
-#define __AMDGPU_PL_LAST	(TTM_PL_PRIV + 4)
+#define __AMDGPU_PL_LAST	(TTM_PL_PRIV + 5)
 
 #define AMDGPU_GTT_MAX_TRANSFER_SIZE	512
 #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS	2
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +130 -45

238 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +11 -6
index 6a1b344e15e1b..257cea4e4f23e 100644
@@ -35,6 +35,7 @@
 #include "amdgpu_sync.h"
 #include "amdgpu_ring.h"
 #include "amdgpu_ids.h"
+#include "amdgpu_ttm.h"
 
 struct drm_exec;
 
@@ -324,10 +325,7 @@ struct amdgpu_vm_fault_info {
 struct amdgpu_mem_stats {
 	struct drm_memory_stats drm;
 
-	/* buffers that requested this placement */
-	uint64_t requested;
-	/* buffers that requested this placement
-	 * but are currently evicted */
+	/* buffers that requested this placement but are currently evicted */
 	uint64_t evicted;
 };
 
@@ -345,6 +343,9 @@ struct amdgpu_vm {
 	/* Lock to protect vm_bo add/del/move on all lists of vm */
 	spinlock_t		status_lock;
 
+	/* Memory statistics for this vm, protected by the status_lock */
+	struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
+
 	/* Per-VM and PT BOs who needs a validation */
 	struct list_head	evicted;
 
@@ -525,6 +526,11 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
 			bool clear);
 bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
 void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted);
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+			    struct ttm_resource *new_res, int sign);
+void amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign);
+void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource *new_mem,
+		       bool evicted);
 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
 				       struct amdgpu_bo *bo);
@@ -575,8 +581,7 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
 				struct amdgpu_vm *vm);
 void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-			  struct amdgpu_mem_stats *stats,
-			  unsigned int size);
+			  struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST]);
 
 int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		       struct amdgpu_bo_vm *vmbo, bool immediate);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1 -0
index f78a0434a48fa..b0bf216821152 100644
@@ -537,6 +537,7 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
 	if (!entry->bo)
 		return;
 
+	amdgpu_vm_update_stats(entry, entry->bo->tbo.resource, -1);
 	entry->bo->vm_bo = NULL;
 	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
 
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v9 5/5] drm/amdgpu: track bo memory stats at runtime

Quoted 282 lines · Yunxiang Li

Before, every time fdinfo is queried we try to lock all the BOs in the VM and calculate memory usage from scratch. This works okay if the fdinfo is rarely read and the VMs don't have a ton of BOs. If either of these conditions is not true, we get a massive performance hit.

In this new revision, we track the BOs as they change states. This way when the fdinfo is queried we only need to take the status lock and copy out the usage stats with minimal impact to the runtime performance. With this new approach however, we would no longer be able to track active buffers.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  15 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c  |  18 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  | 110 +++++-------
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h  |   4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |   4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 175 +++++++++++++++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |  17 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c   |   1 +
  8 files changed, 204 insertions(+), 140 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +13 -2
index b144404902255..423cea4e571ee 100644
@@ -36,6 +36,7 @@
  #include "amdgpu_gem.h"
  #include "amdgpu_dma_buf.h"
  #include "amdgpu_xgmi.h"
+#include "amdgpu_vm.h"
  #include <drm/amdgpu_drm.h>
  #include <drm/ttm/ttm_tt.h>
  #include <linux/dma-buf.h>
@@ -190,6 +191,14 @@ static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach,
  	}
  }
 
+static void amdgpu_dma_buf_release(struct dma_buf *buf)
+{
+	struct amdgpu_bo *bo = gem_to_amdgpu_bo(buf->priv);
+
+	amdgpu_vm_bo_update_shared(bo, -1);
+	drm_gem_dmabuf_release(buf);
+}
+
  /**
   * amdgpu_dma_buf_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
   * @dma_buf: Shared DMA buffer
@@ -237,7 +246,7 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = {
  	.unpin = amdgpu_dma_buf_unpin,
  	.map_dma_buf = amdgpu_dma_buf_map,
  	.unmap_dma_buf = amdgpu_dma_buf_unmap,
-	.release = drm_gem_dmabuf_release,
+	.release = amdgpu_dma_buf_release,
  	.begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
  	.mmap = drm_gem_dmabuf_mmap,
  	.vmap = drm_gem_dmabuf_vmap,
@@ -265,8 +274,10 @@ struct dma_buf *amdgpu_gem_prime_export(struct drm_gem_object *gobj,
  		return ERR_PTR(-EPERM);
 
  	buf = drm_gem_prime_export(gobj, flags);
-	if (!IS_ERR(buf))
+	if (!IS_ERR(buf)) {
  		buf->ops = &amdgpu_dmabuf_ops;
+		amdgpu_vm_bo_update_shared(bo, +1);
+	}
 
  	return buf;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +6 -12
index 7717e3e4f05b5..e625e2481747e 100644
@@ -60,7 +60,7 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  	struct amdgpu_fpriv *fpriv = file->driver_priv;
  	struct amdgpu_vm *vm = &fpriv->vm;
 
-	struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST + 1] = { };
+	struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
  	ktime_t usage[AMDGPU_HW_IP_NUM];
  	const char *pl_name[] = {
  		[TTM_PL_VRAM] = "vram",
@@ -72,15 +72,8 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  		[AMDGPU_PL_DOORBELL] = "doorbell",
  	};
  	unsigned int hw_ip, i;
-	int ret;
-
-	ret = amdgpu_bo_reserve(vm->root.bo, false);
-	if (ret)
-		return;
-
-	amdgpu_vm_get_memory(vm, stats, ARRAY_SIZE(stats));
-	amdgpu_bo_unreserve(vm->root.bo);
 
+	amdgpu_vm_get_memory(vm, stats);
  	amdgpu_ctx_mgr_usage(&fpriv->ctx_mgr, usage);
 
  	/*
@@ -97,7 +90,6 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 
  		drm_print_memory_stats(p,
  				       &stats[i].drm,
-				       DRM_GEM_OBJECT_ACTIVE |
  				       DRM_GEM_OBJECT_RESIDENT |
  				       DRM_GEM_OBJECT_PURGEABLE,
  				       pl_name[i]);
@@ -115,9 +107,11 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  	drm_printf(p, "amd-evicted-vram:\t%llu KiB\n",
  		   stats[TTM_PL_VRAM].evicted/1024UL);
  	drm_printf(p, "amd-requested-vram:\t%llu KiB\n",
-		   stats[TTM_PL_VRAM].requested/1024UL);
+		   (stats[TTM_PL_VRAM].drm.shared +
+		    stats[TTM_PL_VRAM].drm.private) / 1024UL);
  	drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
-		   stats[TTM_PL_TT].requested/1024UL);
+		   (stats[TTM_PL_TT].drm.shared +
+		    stats[TTM_PL_TT].drm.private) / 1024UL);
 
  	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
  		if (!usage[hw_ip])
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +40 -70
index 951b20e40fd35..96f4b8904e9a6 100644
@@ -1258,7 +1258,7 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  		return;
 
  	abo = ttm_to_amdgpu_bo(bo);
-	amdgpu_vm_bo_invalidate(abo, evict);
+	amdgpu_vm_bo_move(abo, new_mem, evict);
 
  	amdgpu_bo_kunmap(abo);
 
@@ -1271,75 +1271,6 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
  			     old_mem ? old_mem->mem_type : -1);
  }
 
-void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-			  struct amdgpu_mem_stats *stats,
-			  unsigned int sz)
-{
-	const unsigned int domain_to_pl[] = {
-		[ilog2(AMDGPU_GEM_DOMAIN_CPU)]	    = TTM_PL_SYSTEM,
-		[ilog2(AMDGPU_GEM_DOMAIN_GTT)]	    = TTM_PL_TT,
-		[ilog2(AMDGPU_GEM_DOMAIN_VRAM)]	    = TTM_PL_VRAM,
-		[ilog2(AMDGPU_GEM_DOMAIN_GDS)]	    = AMDGPU_PL_GDS,
-		[ilog2(AMDGPU_GEM_DOMAIN_GWS)]	    = AMDGPU_PL_GWS,
-		[ilog2(AMDGPU_GEM_DOMAIN_OA)]	    = AMDGPU_PL_OA,
-		[ilog2(AMDGPU_GEM_DOMAIN_DOORBELL)] = AMDGPU_PL_DOORBELL,
-	};
-	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
-	struct ttm_resource *res = bo->tbo.resource;
-	struct drm_gem_object *obj = &bo->tbo.base;
-	uint64_t size = amdgpu_bo_size(bo);
-	unsigned int type;
-
-	if (!res) {
-		/*
-		 * If no backing store use one of the preferred domain for basic
-		 * stats. We take the MSB since that should give a reasonable
-		 * view.
-		 */
-		BUILD_BUG_ON(TTM_PL_VRAM < TTM_PL_TT ||
-			     TTM_PL_VRAM < TTM_PL_SYSTEM);
-		type = fls(bo->preferred_domains & AMDGPU_GEM_DOMAIN_MASK);
-		if (!type)
-			return;
-		type--;
-		if (drm_WARN_ON_ONCE(&adev->ddev,
-				     type >= ARRAY_SIZE(domain_to_pl)))
-			return;
-		type = domain_to_pl[type];
-	} else {
-		type = res->mem_type;
-	}
-
-	if (drm_WARN_ON_ONCE(&adev->ddev, type >= sz))
-		return;
-
-	/* DRM stats common fields: */
-
-	if (drm_gem_object_is_shared_for_memory_stats(obj))
-		stats[type].drm.shared += size;
-	else
-		stats[type].drm.private += size;
-
-	if (res) {
-		stats[type].drm.resident += size;
-
-		if (!dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_BOOKKEEP))
-			stats[type].drm.active += size;
-		else if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
-			stats[type].drm.purgeable += size;
-	}
-
-	/* amdgpu specific stats: */
-
-	if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) {
-		stats[TTM_PL_VRAM].requested += size;
-		if (type != TTM_PL_VRAM)
-			stats[TTM_PL_VRAM].evicted += size;
-	} else if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_GTT) {
-		stats[TTM_PL_TT].requested += size;
-	}
-}
-
  /**
   * amdgpu_bo_release_notify - notification about a BO being released
   * @bo: pointer to a buffer object
@@ -1554,6 +1485,45 @@ u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo)
  	return amdgpu_gmc_sign_extend(offset);
  }
 
+/**
+ * amdgpu_bo_mem_stats_placement - bo placement for memory accounting
+ * @bo:	the buffer object we should look at
+ *
+ * BO can have multiple preferred placements, to avoid double counting we want
+ * to file it under a single placement for memory stats.
+ * Luckily, if we take the highest set bit in preferred_domains the result is
+ * quite sensible.
+ *
+ * Returns:
+ * Which of the placements should the BO be accounted under.
+ */
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo)
+{
+	uint32_t domain = bo->preferred_domains & AMDGPU_GEM_DOMAIN_MASK;
+
+	if (!domain)
+		return TTM_PL_SYSTEM;
+
+	switch (rounddown_pow_of_two(domain)) {
+	case AMDGPU_GEM_DOMAIN_CPU:
+		return TTM_PL_SYSTEM;
+	case AMDGPU_GEM_DOMAIN_GTT:
+		return TTM_PL_TT;
+	case AMDGPU_GEM_DOMAIN_VRAM:
+		return TTM_PL_VRAM;
+	case AMDGPU_GEM_DOMAIN_GDS:
+		return AMDGPU_PL_GDS;
+	case AMDGPU_GEM_DOMAIN_GWS:
+		return AMDGPU_PL_GWS;
+	case AMDGPU_GEM_DOMAIN_OA:
+		return AMDGPU_PL_OA;
+	case AMDGPU_GEM_DOMAIN_DOORBELL:
+		return AMDGPU_PL_DOORBELL;
+	default:
+		return TTM_PL_SYSTEM;
+	}
+}
+
  /**
   * amdgpu_bo_get_preferred_domain - get preferred domain
   * @adev: amdgpu device object
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -3
index ce3314152d20f..bdc9a5bc4da46 100644
@@ -305,9 +305,7 @@ int amdgpu_bo_sync_wait_resv(struct amdgpu_device *adev, struct dma_resv *resv,
  int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr);
  u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo);
  u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo);
-void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-			  struct amdgpu_mem_stats *stats,
-			  unsigned int size);
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo);
  uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev,
  					    uint32_t domain);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -0
index 2852a6064c9ac..86cb83fb2283c 100644
@@ -26,15 +26,15 @@
 
  #include <linux/dma-direction.h>
  #include <drm/gpu_scheduler.h>
+#include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h line 37

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -2
  #include "amdgpu_vram_mgr.h"
-#include "amdgpu.h"
 
  #define AMDGPU_PL_GDS		(TTM_PL_PRIV + 0)
  #define AMDGPU_PL_GWS		(TTM_PL_PRIV + 1)
  #define AMDGPU_PL_OA		(TTM_PL_PRIV + 2)
  #define AMDGPU_PL_PREEMPT	(TTM_PL_PRIV + 3)
  #define AMDGPU_PL_DOORBELL	(TTM_PL_PRIV + 4)
-#define __AMDGPU_PL_LAST	(TTM_PL_PRIV + 4)
+#define __AMDGPU_PL_LAST	(TTM_PL_PRIV + 5)

I like the the convention where "last" is equal to the last used index but maybe it is just me. I guess you strongly wanted to avoid the "+ 1" in code albeit for a price in making it less obvious.

Quoted 71 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
  #define AMDGPU_GTT_MAX_TRANSFER_SIZE	512
  #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS	2
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +54 -0
index d0db155a9ab7c..26effcf703d47 100644
@@ -36,6 +36,7 @@
  #include <drm/ttm/ttm_tt.h>
  #include <drm/drm_exec.h>
  #include "amdgpu.h"
+#include "amdgpu_vm.h"
  #include "amdgpu_trace.h"
  #include "amdgpu_amdkfd.h"
  #include "amdgpu_gmc.h"
@@ -310,6 +311,74 @@ static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)
  	spin_unlock(&vm->status_lock);
  }
 
+/**
+ * amdgpu_vm_update_shared - helper to update shared memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @sign: if we should add (+1) or subtract (-1) from the shared stat
+ *
+ * Takes the vm status_lock and updates the shared memory stat. If the basic
+ * stat changed (e.g. buffer was moved) amdgpu_vm_update_stats need to be called
+ * as well.
+ */
+static void amdgpu_vm_update_shared(struct amdgpu_vm_bo_base *base, int sign)
+{
+	struct amdgpu_vm *vm = base->vm;
+	struct amdgpu_bo *bo = base->bo;
+	int64_t size = sign * amdgpu_bo_size(bo);
+	uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+
+	spin_lock(&vm->status_lock);
+	vm->stats[bo_memtype].drm.shared += size;
+	vm->stats[bo_memtype].drm.private -= size;
+	spin_unlock(&vm->status_lock);
+}
+
+/**
+ * amdgpu_vm_update_stats - helper to update normal memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @res:  the ttm_resource to use for the purpose of accounting, may or may not
+ *        be bo->tbo.resource
+ * @sign: if we should add (+1) or subtract (-1) from the stat
+ *
+ * Takes the vm status_lock and updates the basic memory stat. If the shared
+ * stat changed (e.g. buffer was exported) amdgpu_vm_update_shared need to be
+ * called as well.
+ */
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+			    struct ttm_resource *res, int sign)
+{
+	struct amdgpu_vm *vm = base->vm;
+	struct amdgpu_bo *bo = base->bo;
+	int64_t size = sign * amdgpu_bo_size(bo);
+	uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+	bool shared = drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base);
+
+	spin_lock(&vm->status_lock);
+
+	/* For drm-total- and drm-shared-, BO are accounted by their preferred
+	 * placement, see also amdgpu_bo_mem_stats_placement.
+	 */
+	if (shared)
+		vm->stats[bo_memtype].drm.shared += size;
+	else
+		vm->stats[bo_memtype].drm.private += size;
+
+	if (res && res->mem_type < __AMDGPU_PL_LAST) {

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm) · line 376

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
+		uint32_t res_memtype = res->mem_type;
+
+		vm->stats[res_memtype].drm.resident += size;
+		/* BO only count as purgeable if it is resident,
+		 * since otherwise there's nothing to purge.
+		 */
+		if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
+			vm->stats[res_memtype].drm.purgeable += size;
+		if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(res_memtype)))
+			vm->stats[bo_memtype].evicted += size;

So for VRAM+CPU Mesa common case, when backing store is in system memory, it not count as evicted? Intentionally?

Quoted 114 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +32 -44
+	}
+
+	spin_unlock(&vm->status_lock);
+}
+
  /**
   * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
   *
@@ -332,6 +401,7 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
  		return;
  	base->next = bo->vm_bo;
  	bo->vm_bo = base;
+	amdgpu_vm_update_stats(base, bo->tbo.resource, +1);
 
  	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
  		return;
@@ -1082,53 +1152,11 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  	return r;
  }
 
-static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,
-				    struct amdgpu_mem_stats *stats,
-				    unsigned int size)
-{
-	struct amdgpu_vm *vm = bo_va->base.vm;
-	struct amdgpu_bo *bo = bo_va->base.bo;
-
-	if (!bo)
-		return;
-
-	/*
-	 * For now ignore BOs which are currently locked and potentially
-	 * changing their location.
-	 */
-	if (!amdgpu_vm_is_bo_always_valid(vm, bo) &&
-	    !dma_resv_trylock(bo->tbo.base.resv))
-		return;
-
-	amdgpu_bo_get_memory(bo, stats, size);
-	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
-		dma_resv_unlock(bo->tbo.base.resv);
-}
-
  void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-			  struct amdgpu_mem_stats *stats,
-			  unsigned int size)
+			  struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST])
  {
-	struct amdgpu_bo_va *bo_va, *tmp;
-
  	spin_lock(&vm->status_lock);
-	list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status)
-		amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-	list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
-		amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-	list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status)
-		amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-	list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
-		amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-	list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status)
-		amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-	list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status)
-		amdgpu_vm_bo_get_memory(bo_va, stats, size);
+	memcpy(stats, vm->stats, sizeof(*stats) * __AMDGPU_PL_LAST);
  	spin_unlock(&vm->status_lock);
  }
 
@@ -2075,6 +2103,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,
  			if (*base != &bo_va->base)
  				continue;
 
+			amdgpu_vm_update_stats(*base, bo->tbo.resource, -1);
  			*base = bo_va->base.next;
  			break;
  		}
@@ -2140,6 +2169,22 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
  	return true;
  }
 
+/**
+ * amdgpu_vm_bo_update_shared - called when bo gets shared/unshared
+ *
+ * @bo: amdgpu buffer object
+ * @sign: if we should add (+1) or subtract (-1) the memory stat
+ *
+ * Update the per VM stats for all the vm
+ */
+void amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign)
+{
+	struct amdgpu_vm_bo_base *bo_base;
+
+	for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next)
+		amdgpu_vm_update_shared(bo_base, sign);
+}
+
  /**
   * amdgpu_vm_bo_invalidate - mark the bo as invalid
   *
@@ -2173,6 +2218,28 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted)
  	}
  }
 
+/**
+ * amdgpu_vm_bo_move - handle BO move
+ *
+ * @bo: amdgpu buffer object
+ * @new_mem: the new placement of the BO move
+ * @evicted: is the BO evicted
+ *

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted) · line 2237

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
+ * Update the memory stats for the new placement and mark @bo as invalid.
+ */
+void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource *new_mem,
+		       bool evicted)
+{
+	struct amdgpu_vm_bo_base *bo_base;
+
+	for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
+		amdgpu_vm_update_stats(bo_base, bo->tbo.resource, -1);
+		amdgpu_vm_update_stats(bo_base, new_mem, +1);

FWIW dropping and re-acquiring the lock makes the readout from amdgpu_vm_get_memory() non-atomic in terms of view consistency. Should be worth adding a helper which does the move in one go, with one lock acquire section.

Regards,

Tvrtko

Quoted 117 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +24 -1
+	}
+
+	amdgpu_vm_bo_invalidate(bo, evicted);
+}
+
  /**
   * amdgpu_vm_get_block_size - calculate VM page table size as power of two
   *
@@ -2589,6 +2656,16 @@ void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  	vm->is_compute_context = false;
  }
 
+static int amdgpu_vm_stats_is_zero(struct amdgpu_vm *vm)
+{
+	for (int i = 0; i < __AMDGPU_PL_LAST; ++i) {
+		if (!(drm_memory_stats_is_zero(&vm->stats[i].drm) &&
+		      vm->stats[i].evicted == 0))
+			return false;
+	}
+	return true;
+}
+
  /**
   * amdgpu_vm_fini - tear down a vm instance
   *
@@ -2612,7 +2689,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 
  	root = amdgpu_bo_ref(vm->root.bo);
  	amdgpu_bo_reserve(root, true);
-	amdgpu_vm_put_task_info(vm->task_info);
  	amdgpu_vm_set_pasid(adev, vm, 0);
  	dma_fence_wait(vm->last_unlocked, false);
  	dma_fence_put(vm->last_unlocked);
@@ -2660,6 +2736,15 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  		}
  	}
 
+	if (!amdgpu_vm_stats_is_zero(vm)) {
+		struct amdgpu_task_info *ti = vm->task_info;
+
+		dev_warn(adev->dev,
+			 "VM memory stats for proc %s(%d) task %s(%d) is non-zero when fini\n",
+			 ti->process_name, ti->pid, ti->task_name, ti->tgid);
+	}
+
+	amdgpu_vm_put_task_info(vm->task_info);
  }
 
  /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +11 -6
index 6a1b344e15e1b..257cea4e4f23e 100644
@@ -35,6 +35,7 @@
  #include "amdgpu_sync.h"
  #include "amdgpu_ring.h"
  #include "amdgpu_ids.h"
+#include "amdgpu_ttm.h"
 
  struct drm_exec;
 
@@ -324,10 +325,7 @@ struct amdgpu_vm_fault_info {
  struct amdgpu_mem_stats {
  	struct drm_memory_stats drm;
 
-	/* buffers that requested this placement */
-	uint64_t requested;
-	/* buffers that requested this placement
-	 * but are currently evicted */
+	/* buffers that requested this placement but are currently evicted */
  	uint64_t evicted;
  };
 
@@ -345,6 +343,9 @@ struct amdgpu_vm {
  	/* Lock to protect vm_bo add/del/move on all lists of vm */
  	spinlock_t		status_lock;
 
+	/* Memory statistics for this vm, protected by the status_lock */
+	struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
+
  	/* Per-VM and PT BOs who needs a validation */
  	struct list_head	evicted;
 
@@ -525,6 +526,11 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  			bool clear);
  bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
  void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted);
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+			    struct ttm_resource *new_res, int sign);
+void amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign);
+void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource *new_mem,
+		       bool evicted);
  uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
  struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  				       struct amdgpu_bo *bo);
@@ -575,8 +581,7 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
  void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
  				struct amdgpu_vm *vm);
  void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-			  struct amdgpu_mem_stats *stats,
-			  unsigned int size);
+			  struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST]);
 
  int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  		       struct amdgpu_bo_vm *vmbo, bool immediate);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1 -0
index f78a0434a48fa..b0bf216821152 100644
@@ -537,6 +537,7 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
  	if (!entry->bo)
  		return;
 
+	amdgpu_vm_update_stats(entry, entry->bo->tbo.resource, -1);
  	entry->bo->vm_bo = NULL;
  	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
 
#9 lore raw

RE: [PATCH v9 5/5] drm/amdgpu: track bo memory stats at runtime

[Public]

Quoted 310 lines

From: Tvrtko Ursulin <[email protected]>
Sent: Tuesday, December 3, 2024 9:42

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static void amdgpu_dma_buf_unmap(struct

Quoted 37 lines · Yunxiang Li

Before, every time fdinfo is queried we try to lock all the BOs in the VM and calculate memory usage from scratch. This works okay if the fdinfo is rarely read and the VMs don't have a ton of BOs. If either of these conditions is not true, we get a massive performance hit.

In this new revision, we track the BOs as they change states. This way when the fdinfo is queried we only need to take the status lock and copy out the usage stats with minimal impact to the runtime performance. With this new approach however, we would no longer be able to track active buffers.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  15 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c  |  18 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  | 110 +++++-------
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h  |   4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |   4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 175 +++++++++++++++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |  17 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c   |   1 +
  8 files changed, 204 insertions(+), 140 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -0
index b144404902255..423cea4e571ee 100644
@@ -36,6 +36,7 @@
  #include "amdgpu_gem.h"
  #include "amdgpu_dma_buf.h"
  #include "amdgpu_xgmi.h"
+#include "amdgpu_vm.h"
  #include <drm/amdgpu_drm.h>
  #include <drm/ttm/ttm_tt.h>
  #include <linux/dma-buf.h>
@@ -190,6 +191,14 @@ static void amdgpu_dma_buf_unmap(struct

dma_buf_attachment *attach,

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +7 -0
    }
  }
 
+static void amdgpu_dma_buf_release(struct dma_buf *buf) {
+   struct amdgpu_bo *bo = gem_to_amdgpu_bo(buf->priv);
+
+   amdgpu_vm_bo_update_shared(bo, -1);
+   drm_gem_dmabuf_release(buf);
+}
+
  /**
   * amdgpu_dma_buf_begin_cpu_access - &dma_buf_ops.begin_cpu_access

implementation

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c struct dma_buf *amdgpu_gem_prime_export(struct

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
   * @dma_buf: Shared DMA buffer
@@ -237,7 +246,7 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = {
    .unpin = amdgpu_dma_buf_unpin,
    .map_dma_buf = amdgpu_dma_buf_map,
    .unmap_dma_buf = amdgpu_dma_buf_unmap,
-   .release = drm_gem_dmabuf_release,
+   .release = amdgpu_dma_buf_release,
    .begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
    .mmap = drm_gem_dmabuf_mmap,
    .vmap = drm_gem_dmabuf_vmap,
@@ -265,8 +274,10 @@ struct dma_buf *amdgpu_gem_prime_export(struct

drm_gem_object *gobj,

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c void amdgpu_show_fdinfo(struct drm_printer *p, struct

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +3 -1
            return ERR_PTR(-EPERM);
 
    buf = drm_gem_prime_export(gobj, flags);
-   if (!IS_ERR(buf))
+   if (!IS_ERR(buf)) {
            buf->ops = &amdgpu_dmabuf_ops;
+           amdgpu_vm_bo_update_shared(bo, +1);
+   }
 
    return buf;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +0 -0
index 7717e3e4f05b5..e625e2481747e 100644
@@ -60,7 +60,7 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c void amdgpu_show_fdinfo(struct drm_printer *p, struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -1
    struct amdgpu_fpriv *fpriv = file->driver_priv;
    struct amdgpu_vm *vm = &fpriv->vm;
 
-   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST + 1] = { };
+   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
    ktime_t usage[AMDGPU_HW_IP_NUM];
    const char *pl_name[] = {
            [TTM_PL_VRAM] = "vram",
@@ -72,15 +72,8 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c void amdgpu_show_fdinfo(struct drm_printer *p, struct

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -9
            [AMDGPU_PL_DOORBELL] = "doorbell",
    };
    unsigned int hw_ip, i;
-   int ret;
-
-   ret = amdgpu_bo_reserve(vm->root.bo, false);
-   if (ret)
-           return;
-
-   amdgpu_vm_get_memory(vm, stats, ARRAY_SIZE(stats));
-   amdgpu_bo_unreserve(vm->root.bo);
 
+   amdgpu_vm_get_memory(vm, stats);
    amdgpu_ctx_mgr_usage(&fpriv->ctx_mgr, usage);
 
    /*
@@ -97,7 +90,6 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
 
            drm_print_memory_stats(p,
                                   &stats[i].drm,
-                                  DRM_GEM_OBJECT_ACTIVE |
                                   DRM_GEM_OBJECT_RESIDENT |
                                   DRM_GEM_OBJECT_PURGEABLE,
                                   pl_name[i]);
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +0 -0
@@ -115,9 +107,11 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -2
    drm_printf(p, "amd-evicted-vram:\t%llu KiB\n",
               stats[TTM_PL_VRAM].evicted/1024UL);
    drm_printf(p, "amd-requested-vram:\t%llu KiB\n",
-              stats[TTM_PL_VRAM].requested/1024UL);
+              (stats[TTM_PL_VRAM].drm.shared +
+               stats[TTM_PL_VRAM].drm.private) / 1024UL);
    drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
-              stats[TTM_PL_TT].requested/1024UL);
+              (stats[TTM_PL_TT].drm.shared +
+               stats[TTM_PL_TT].drm.private) / 1024UL);
 
    for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
            if (!usage[hw_ip])
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index 951b20e40fd35..96f4b8904e9a6 100644
@@ -1258,7 +1258,7 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object

*bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -1
            return;
 
    abo = ttm_to_amdgpu_bo(bo);
-   amdgpu_vm_bo_invalidate(abo, evict);
+   amdgpu_vm_bo_move(abo, new_mem, evict);
 
    amdgpu_bo_kunmap(abo);
 
@@ -1271,75 +1271,6 @@ void amdgpu_bo_move_notify(struct

ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -9
                         old_mem ? old_mem->mem_type : -1);
  }
 
-void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int sz)
-{
-   const unsigned int domain_to_pl[] = {
-           [ilog2(AMDGPU_GEM_DOMAIN_CPU)]      = TTM_PL_SYSTEM,
-           [ilog2(AMDGPU_GEM_DOMAIN_GTT)]      = TTM_PL_TT,
-           [ilog2(AMDGPU_GEM_DOMAIN_VRAM)]     = TTM_PL_VRAM,
-           [ilog2(AMDGPU_GEM_DOMAIN_GDS)]      =

AMDGPU_PL_GDS,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct · line 1283

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -1
-           [ilog2(AMDGPU_GEM_DOMAIN_GWS)]      =

AMDGPU_PL_GWS,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct · line 1285

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -2
-           [ilog2(AMDGPU_GEM_DOMAIN_OA)]       = AMDGPU_PL_OA,
-           [ilog2(AMDGPU_GEM_DOMAIN_DOORBELL)] =

AMDGPU_PL_DOORBELL,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -6
-   };
-   struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
-   struct ttm_resource *res = bo->tbo.resource;
-   struct drm_gem_object *obj = &bo->tbo.base;
-   uint64_t size = amdgpu_bo_size(bo);
-   unsigned int type;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -10
-
-   if (!res) {
-           /*
-            * If no backing store use one of the preferred domain for basic
-            * stats. We take the MSB since that should give a reasonable
-            * view.
-            */
-           BUILD_BUG_ON(TTM_PL_VRAM < TTM_PL_TT ||
-                        TTM_PL_VRAM < TTM_PL_SYSTEM);
-           type = fls(bo->preferred_domains &

AMDGPU_GEM_DOMAIN_MASK);

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -15
-           if (!type)
-                   return;
-           type--;
-           if (drm_WARN_ON_ONCE(&adev->ddev,
-                                type >= ARRAY_SIZE(domain_to_pl)))
-                   return;
-           type = domain_to_pl[type];
-   } else {
-           type = res->mem_type;
-   }
-
-   if (drm_WARN_ON_ONCE(&adev->ddev, type >= sz))
-           return;
-
-   /* DRM stats common fields: */

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -10
-
-   if (drm_gem_object_is_shared_for_memory_stats(obj))
-           stats[type].drm.shared += size;
-   else
-           stats[type].drm.private += size;
-
-   if (res) {
-           stats[type].drm.resident += size;
-
-           if (!dma_resv_test_signaled(obj->resv,

DMA_RESV_USAGE_BOOKKEEP))

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c u64 amdgpu_bo_gpu_offset_no_check(struct

Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -16
-                   stats[type].drm.active += size;
-           else if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
-                   stats[type].drm.purgeable += size;
-   }
-
-   /* amdgpu specific stats: */
-
-   if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) {
-           stats[TTM_PL_VRAM].requested += size;
-           if (type != TTM_PL_VRAM)
-                   stats[TTM_PL_VRAM].evicted += size;
-   } else if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_GTT) {
-           stats[TTM_PL_TT].requested += size;
-   }
-}
-
  /**
   * amdgpu_bo_release_notify - notification about a BO being released
   * @bo: pointer to a buffer object
@@ -1554,6 +1485,45 @@ u64 amdgpu_bo_gpu_offset_no_check(struct

amdgpu_bo *bo)

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +6 -0
    return amdgpu_gmc_sign_extend(offset);
  }
 
+/**
+ * amdgpu_bo_mem_stats_placement - bo placement for memory accounting
+ * @bo:    the buffer object we should look at
+ *
+ * BO can have multiple preferred placements, to avoid double
+counting we want

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
+ * to file it under a single placement for memory stats.
+ * Luckily, if we take the highest set bit in preferred_domains the
+result is
+ * quite sensible.
+ *
+ * Returns:
+ * Which of the placements should the BO be accounted under.
+ */
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo) {
+   uint32_t domain = bo->preferred_domains &

AMDGPU_GEM_DOMAIN_MASK;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h int amdgpu_bo_sync_wait_resv(struct amdgpu_device

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +24 -0
+
+   if (!domain)
+           return TTM_PL_SYSTEM;
+
+   switch (rounddown_pow_of_two(domain)) {
+   case AMDGPU_GEM_DOMAIN_CPU:
+           return TTM_PL_SYSTEM;
+   case AMDGPU_GEM_DOMAIN_GTT:
+           return TTM_PL_TT;
+   case AMDGPU_GEM_DOMAIN_VRAM:
+           return TTM_PL_VRAM;
+   case AMDGPU_GEM_DOMAIN_GDS:
+           return AMDGPU_PL_GDS;
+   case AMDGPU_GEM_DOMAIN_GWS:
+           return AMDGPU_PL_GWS;
+   case AMDGPU_GEM_DOMAIN_OA:
+           return AMDGPU_PL_OA;
+   case AMDGPU_GEM_DOMAIN_DOORBELL:
+           return AMDGPU_PL_DOORBELL;
+   default:
+           return TTM_PL_SYSTEM;
+   }
+}
+
  /**
   * amdgpu_bo_get_preferred_domain - get preferred domain
   * @adev: amdgpu device object
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index ce3314152d20f..bdc9a5bc4da46 100644
@@ -305,9 +305,7 @@ int amdgpu_bo_sync_wait_resv(struct amdgpu_device

*adev, struct dma_resv *resv,

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -2
  int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr);
  u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo);
  u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo); -void
 amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size);
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo);
  uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev,
                                        uint32_t domain);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -1
index 2852a6064c9ac..86cb83fb2283c 100644
@@ -26,15 +26,15 @@
 
  #include <linux/dma-direction.h>
  #include <drm/gpu_scheduler.h>
+#include <drm/ttm/ttm_placement.h>
  #include "amdgpu_vram_mgr.h"
-#include "amdgpu.h"
 
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h line 37

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -1
  #define AMDGPU_PL_GDS             (TTM_PL_PRIV + 0)
  #define AMDGPU_PL_GWS             (TTM_PL_PRIV + 1)
  #define AMDGPU_PL_OA              (TTM_PL_PRIV + 2)
  #define AMDGPU_PL_PREEMPT (TTM_PL_PRIV + 3)
  #define AMDGPU_PL_DOORBELL        (TTM_PL_PRIV + 4)
-#define __AMDGPU_PL_LAST   (TTM_PL_PRIV + 4)
+#define __AMDGPU_PL_LAST   (TTM_PL_PRIV + 5)

I like the the convention where "last" is equal to the last used index but maybe it is just me. I guess you strongly wanted to avoid the "+ 1"
in code albeit for a price in making it less obvious.

Perhaps "_END" is a more common name for what I'm doing here, and yeah, I found that in earlier patches I had missed the +1 was required here and got an off by one bug.

Quoted 87 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
  #define AMDGPU_GTT_MAX_TRANSFER_SIZE      512
  #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS   2
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index d0db155a9ab7c..26effcf703d47 100644
@@ -36,6 +36,7 @@
  #include <drm/ttm/ttm_tt.h>
  #include <drm/drm_exec.h>
  #include "amdgpu.h"
+#include "amdgpu_vm.h"
  #include "amdgpu_trace.h"
  #include "amdgpu_amdkfd.h"
  #include "amdgpu_gmc.h"
@@ -310,6 +311,74 @@ static void amdgpu_vm_bo_reset_state_machine(struct

amdgpu_vm *vm)

Quoted 59 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +56 -0
    spin_unlock(&vm->status_lock);
  }
 
+/**
+ * amdgpu_vm_update_shared - helper to update shared memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @sign: if we should add (+1) or subtract (-1) from the shared stat
+ *
+ * Takes the vm status_lock and updates the shared memory stat. If
+the basic
+ * stat changed (e.g. buffer was moved) amdgpu_vm_update_stats need
+to be called
+ * as well.
+ */
+static void amdgpu_vm_update_shared(struct amdgpu_vm_bo_base *base,
+int sign) {
+   struct amdgpu_vm *vm = base->vm;
+   struct amdgpu_bo *bo = base->bo;
+   int64_t size = sign * amdgpu_bo_size(bo);
+   uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+
+   spin_lock(&vm->status_lock);
+   vm->stats[bo_memtype].drm.shared += size;
+   vm->stats[bo_memtype].drm.private -= size;
+   spin_unlock(&vm->status_lock);
+}
+
+/**
+ * amdgpu_vm_update_stats - helper to update normal memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @res:  the ttm_resource to use for the purpose of accounting, may or may not
+ *        be bo->tbo.resource
+ * @sign: if we should add (+1) or subtract (-1) from the stat
+ *
+ * Takes the vm status_lock and updates the basic memory stat. If the
+shared
+ * stat changed (e.g. buffer was exported) amdgpu_vm_update_shared
+need to be
+ * called as well.
+ */
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+                       struct ttm_resource *res, int sign) {
+   struct amdgpu_vm *vm = base->vm;
+   struct amdgpu_bo *bo = base->bo;
+   int64_t size = sign * amdgpu_bo_size(bo);
+   uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+   bool shared =
+drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base);
+
+   spin_lock(&vm->status_lock);
+
+   /* For drm-total- and drm-shared-, BO are accounted by their preferred
+    * placement, see also amdgpu_bo_mem_stats_placement.
+    */
+   if (shared)
+           vm->stats[bo_memtype].drm.shared += size;
+   else
+           vm->stats[bo_memtype].drm.private += size;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
+   if (res && res->mem_type < __AMDGPU_PL_LAST) {
+           uint32_t res_memtype = res->mem_type;
+
+           vm->stats[res_memtype].drm.resident += size;
+           /* BO only count as purgeable if it is resident,
+            * since otherwise there's nothing to purge.
+            */
+           if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
+                   vm->stats[res_memtype].drm.purgeable += size;
+           if (!(bo->preferred_domains &

amdgpu_mem_type_to_domain(res_memtype)))

Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm) · line 376

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
+                   vm->stats[bo_memtype].evicted += size;

So for VRAM+CPU Mesa common case, when backing store is in system memory, it not count as evicted? Intentionally?

Only when buffer prefers both placements. With this change the reported evicted stats matches the state machine for managing the buffers, the same comparison is done for adding the BO to the evicted list later in the code.

Quoted 119 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_base_init(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
+   }
+
+   spin_unlock(&vm->status_lock);
+}
+
  /**
   * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
   *
@@ -332,6 +401,7 @@ void amdgpu_vm_bo_base_init(struct

amdgpu_vm_bo_base *base,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
            return;
    base->next = bo->vm_bo;
    bo->vm_bo = base;
+   amdgpu_vm_update_stats(base, bo->tbo.resource, +1);
 
    if (!amdgpu_vm_is_bo_always_valid(vm, bo))
            return;
@@ -1082,53 +1152,11 @@ int amdgpu_vm_update_range(struct

amdgpu_device *adev, struct amdgpu_vm *vm,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_del(struct amdgpu_device

Quoted 56 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -44
    return r;
  }
 
-static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,
-                               struct amdgpu_mem_stats *stats,
-                               unsigned int size)
-{
-   struct amdgpu_vm *vm = bo_va->base.vm;
-   struct amdgpu_bo *bo = bo_va->base.bo;
-
-   if (!bo)
-           return;
-
-   /*
-    * For now ignore BOs which are currently locked and potentially
-    * changing their location.
-    */
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo) &&
-       !dma_resv_trylock(bo->tbo.base.resv))
-           return;
-
-   amdgpu_bo_get_memory(bo, stats, size);
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo))
-           dma_resv_unlock(bo->tbo.base.resv);
-}
-
  void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size)
+                     struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST])
  {
-   struct amdgpu_bo_va *bo_va, *tmp;
-
    spin_lock(&vm->status_lock);
-   list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
+   memcpy(stats, vm->stats, sizeof(*stats) * __AMDGPU_PL_LAST);
    spin_unlock(&vm->status_lock);
  }
 
@@ -2075,6 +2103,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device

*adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo

Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +14 -0
                    if (*base != &bo_va->base)
                            continue;
 
+                   amdgpu_vm_update_stats(*base, bo->tbo.resource, -1);
                    *base = bo_va->base.next;
                    break;
            }
@@ -2140,6 +2169,22 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
    return true;
  }
 
+/**
+ * amdgpu_vm_bo_update_shared - called when bo gets shared/unshared
+ *
+ * @bo: amdgpu buffer object
+ * @sign: if we should add (+1) or subtract (-1) the memory stat
+ *
+ * Update the per VM stats for all the vm  */ void
+amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign) {
+   struct amdgpu_vm_bo_base *bo_base;
+
+   for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next)
+           amdgpu_vm_update_shared(bo_base, sign); }
+
  /**
   * amdgpu_vm_bo_invalidate - mark the bo as invalid
   *
@@ -2173,6 +2218,28 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_bo

*bo, bool evicted)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted)

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
    }
  }
 
+/**
+ * amdgpu_vm_bo_move - handle BO move
+ *
+ * @bo: amdgpu buffer object
+ * @new_mem: the new placement of the BO move
+ * @evicted: is the BO evicted
+ *
+ * Update the memory stats for the new placement and mark @bo as invalid.
+ */
+void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource

*new_mem,

Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted) · line 2237

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -0
+                  bool evicted)
+{
+   struct amdgpu_vm_bo_base *bo_base;
+
+   for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
+           amdgpu_vm_update_stats(bo_base, bo->tbo.resource, -1);
+           amdgpu_vm_update_stats(bo_base, new_mem, +1);

FWIW dropping and re-acquiring the lock makes the readout from amdgpu_vm_get_memory() non-atomic in terms of view consistency. Should be worth adding a helper which does the move in one go, with one lock acquire section.

Good point, thanks.

Quoted 129 lines

Regards,

Tvrtko

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_release_compute(struct

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
+   }
+
+   amdgpu_vm_bo_invalidate(bo, evicted); }
+
  /**
   * amdgpu_vm_get_block_size - calculate VM page table size as power of two
   *
@@ -2589,6 +2656,16 @@ void amdgpu_vm_release_compute(struct

amdgpu_device *adev, struct amdgpu_vm *vm)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_fini(struct amdgpu_device *adev,

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -1
    vm->is_compute_context = false;
  }
 
+static int amdgpu_vm_stats_is_zero(struct amdgpu_vm *vm) {
+   for (int i = 0; i < __AMDGPU_PL_LAST; ++i) {
+           if (!(drm_memory_stats_is_zero(&vm->stats[i].drm) &&
+                 vm->stats[i].evicted == 0))
+                   return false;
+   }
+   return true;
+}
+
  /**
   * amdgpu_vm_fini - tear down a vm instance
   *
@@ -2612,7 +2689,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev,
 struct amdgpu_vm *vm)
 
    root = amdgpu_bo_ref(vm->root.bo);
    amdgpu_bo_reserve(root, true);
-   amdgpu_vm_put_task_info(vm->task_info);
    amdgpu_vm_set_pasid(adev, vm, 0);
    dma_fence_wait(vm->last_unlocked, false);
    dma_fence_put(vm->last_unlocked);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -2660,6 +2736,15 @@ void amdgpu_vm_fini(struct amdgpu_device *adev,

struct amdgpu_vm *vm)

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
            }
    }
 
+   if (!amdgpu_vm_stats_is_zero(vm)) {
+           struct amdgpu_task_info *ti = vm->task_info;
+
+           dev_warn(adev->dev,
+                    "VM memory stats for proc %s(%d) task %s(%d) is non-zero

when fini\n",

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h int amdgpu_vm_bo_update(struct amdgpu_device

Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
+                    ti->process_name, ti->pid, ti->task_name, ti->tgid);
+   }
+
+   amdgpu_vm_put_task_info(vm->task_info);
  }
 
  /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -4
index 6a1b344e15e1b..257cea4e4f23e 100644
@@ -35,6 +35,7 @@
  #include "amdgpu_sync.h"
  #include "amdgpu_ring.h"
  #include "amdgpu_ids.h"
+#include "amdgpu_ttm.h"
 
  struct drm_exec;
 
@@ -324,10 +325,7 @@ struct amdgpu_vm_fault_info {
  struct amdgpu_mem_stats {
    struct drm_memory_stats drm;
 
-   /* buffers that requested this placement */
-   uint64_t requested;
-   /* buffers that requested this placement
-    * but are currently evicted */
+   /* buffers that requested this placement but are currently evicted
+*/
    uint64_t evicted;
  };
@@ -345,6 +343,9 @@ struct amdgpu_vm {
    /* Lock to protect vm_bo add/del/move on all lists of vm */
    spinlock_t              status_lock;
 
+   /* Memory statistics for this vm, protected by the status_lock */
+   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
+
    /* Per-VM and PT BOs who needs a validation */
    struct list_head        evicted;
 
@@ -525,6 +526,11 @@ int amdgpu_vm_bo_update(struct amdgpu_device

*adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h void amdgpu_vm_set_task_info(struct amdgpu_vm

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5 -0
                    bool clear);
  bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
  void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted);
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+                       struct ttm_resource *new_res, int sign); void
+amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign); void
+amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource *new_mem,
+                  bool evicted);
  uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
  struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
                                   struct amdgpu_bo *bo);
@@ -575,8 +581,7 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm

*vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free(struct

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -2
  void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
                            struct amdgpu_vm *vm);
  void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size);
+                     struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST]);
 
  int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                   struct amdgpu_bo_vm *vmbo, bool immediate); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
index f78a0434a48fa..b0bf216821152 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -0
@@ -537,6 +537,7 @@ static void amdgpu_vm_pt_free(struct

amdgpu_vm_bo_base *entry)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1 -0
    if (!entry->bo)
            return;
 
+   amdgpu_vm_update_stats(entry, entry->bo->tbo.resource, -1);
    entry->bo->vm_bo = NULL;
    ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
 
#10 lore raw

Re: [PATCH v9 5/5] drm/amdgpu: track bo memory stats at runtime

Quoted 324 lines · Yunxiang (Teddy)

[Public]

Quoted 310 lines

From: Tvrtko Ursulin <[email protected]>
Sent: Tuesday, December 3, 2024 9:42

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static void amdgpu_dma_buf_unmap(struct

Quoted 37 lines · Yunxiang Li

Before, every time fdinfo is queried we try to lock all the BOs in the VM and calculate memory usage from scratch. This works okay if the fdinfo is rarely read and the VMs don't have a ton of BOs. If either of these conditions is not true, we get a massive performance hit.

In this new revision, we track the BOs as they change states. This way when the fdinfo is queried we only need to take the status lock and copy out the usage stats with minimal impact to the runtime performance. With this new approach however, we would no longer be able to track active buffers.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  15 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c  |  18 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  | 110 +++++-------
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h  |   4 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |   4 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 175 +++++++++++++++-----
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |  17 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c   |   1 +
   8 files changed, 204 insertions(+), 140 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -0
index b144404902255..423cea4e571ee 100644
@@ -36,6 +36,7 @@
   #include "amdgpu_gem.h"
   #include "amdgpu_dma_buf.h"
   #include "amdgpu_xgmi.h"
+#include "amdgpu_vm.h"
   #include <drm/amdgpu_drm.h>
   #include <drm/ttm/ttm_tt.h>
   #include <linux/dma-buf.h>
@@ -190,6 +191,14 @@ static void amdgpu_dma_buf_unmap(struct

dma_buf_attachment *attach,

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +7 -0
     }
   }
 
+static void amdgpu_dma_buf_release(struct dma_buf *buf) {
+   struct amdgpu_bo *bo = gem_to_amdgpu_bo(buf->priv);
+
+   amdgpu_vm_bo_update_shared(bo, -1);
+   drm_gem_dmabuf_release(buf);
+}
+
   /**
    * amdgpu_dma_buf_begin_cpu_access - &dma_buf_ops.begin_cpu_access

implementation

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c struct dma_buf *amdgpu_gem_prime_export(struct

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
    * @dma_buf: Shared DMA buffer
@@ -237,7 +246,7 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = {
     .unpin = amdgpu_dma_buf_unpin,
     .map_dma_buf = amdgpu_dma_buf_map,
     .unmap_dma_buf = amdgpu_dma_buf_unmap,
-   .release = drm_gem_dmabuf_release,
+   .release = amdgpu_dma_buf_release,
     .begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
     .mmap = drm_gem_dmabuf_mmap,
     .vmap = drm_gem_dmabuf_vmap,
@@ -265,8 +274,10 @@ struct dma_buf *amdgpu_gem_prime_export(struct

drm_gem_object *gobj,

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c void amdgpu_show_fdinfo(struct drm_printer *p, struct

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +3 -1
             return ERR_PTR(-EPERM);
 
     buf = drm_gem_prime_export(gobj, flags);
-   if (!IS_ERR(buf))
+   if (!IS_ERR(buf)) {
             buf->ops = &amdgpu_dmabuf_ops;
+           amdgpu_vm_bo_update_shared(bo, +1);
+   }
 
     return buf;
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +0 -0
index 7717e3e4f05b5..e625e2481747e 100644
@@ -60,7 +60,7 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c void amdgpu_show_fdinfo(struct drm_printer *p, struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -1
     struct amdgpu_fpriv *fpriv = file->driver_priv;
     struct amdgpu_vm *vm = &fpriv->vm;
 
-   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST + 1] = { };
+   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
     ktime_t usage[AMDGPU_HW_IP_NUM];
     const char *pl_name[] = {
             [TTM_PL_VRAM] = "vram",
@@ -72,15 +72,8 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c void amdgpu_show_fdinfo(struct drm_printer *p, struct

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -9
             [AMDGPU_PL_DOORBELL] = "doorbell",
     };
     unsigned int hw_ip, i;
-   int ret;
-
-   ret = amdgpu_bo_reserve(vm->root.bo, false);
-   if (ret)
-           return;
-
-   amdgpu_vm_get_memory(vm, stats, ARRAY_SIZE(stats));
-   amdgpu_bo_unreserve(vm->root.bo);
 
+   amdgpu_vm_get_memory(vm, stats);
     amdgpu_ctx_mgr_usage(&fpriv->ctx_mgr, usage);
 
     /*
@@ -97,7 +90,6 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
 
             drm_print_memory_stats(p,
                                    &stats[i].drm,
-                                  DRM_GEM_OBJECT_ACTIVE |
                                    DRM_GEM_OBJECT_RESIDENT |
                                    DRM_GEM_OBJECT_PURGEABLE,
                                    pl_name[i]);
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +0 -0
@@ -115,9 +107,11 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -2
     drm_printf(p, "amd-evicted-vram:\t%llu KiB\n",
                stats[TTM_PL_VRAM].evicted/1024UL);
     drm_printf(p, "amd-requested-vram:\t%llu KiB\n",
-              stats[TTM_PL_VRAM].requested/1024UL);
+              (stats[TTM_PL_VRAM].drm.shared +
+               stats[TTM_PL_VRAM].drm.private) / 1024UL);
     drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
-              stats[TTM_PL_TT].requested/1024UL);
+              (stats[TTM_PL_TT].drm.shared +
+               stats[TTM_PL_TT].drm.private) / 1024UL);
 
     for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
             if (!usage[hw_ip])
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index 951b20e40fd35..96f4b8904e9a6 100644
@@ -1258,7 +1258,7 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object

*bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -1
             return;
 
     abo = ttm_to_amdgpu_bo(bo);
-   amdgpu_vm_bo_invalidate(abo, evict);
+   amdgpu_vm_bo_move(abo, new_mem, evict);
 
     amdgpu_bo_kunmap(abo);
 
@@ -1271,75 +1271,6 @@ void amdgpu_bo_move_notify(struct

ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -9
                          old_mem ? old_mem->mem_type : -1);
   }
 
-void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int sz)
-{
-   const unsigned int domain_to_pl[] = {
-           [ilog2(AMDGPU_GEM_DOMAIN_CPU)]      = TTM_PL_SYSTEM,
-           [ilog2(AMDGPU_GEM_DOMAIN_GTT)]      = TTM_PL_TT,
-           [ilog2(AMDGPU_GEM_DOMAIN_VRAM)]     = TTM_PL_VRAM,
-           [ilog2(AMDGPU_GEM_DOMAIN_GDS)]      =

AMDGPU_PL_GDS,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct · line 1283

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -1
-           [ilog2(AMDGPU_GEM_DOMAIN_GWS)]      =

AMDGPU_PL_GWS,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct · line 1285

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -2
-           [ilog2(AMDGPU_GEM_DOMAIN_OA)]       = AMDGPU_PL_OA,
-           [ilog2(AMDGPU_GEM_DOMAIN_DOORBELL)] =

AMDGPU_PL_DOORBELL,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -6
-   };
-   struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
-   struct ttm_resource *res = bo->tbo.resource;
-   struct drm_gem_object *obj = &bo->tbo.base;
-   uint64_t size = amdgpu_bo_size(bo);
-   unsigned int type;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -10
-
-   if (!res) {
-           /*
-            * If no backing store use one of the preferred domain for basic
-            * stats. We take the MSB since that should give a reasonable
-            * view.
-            */
-           BUILD_BUG_ON(TTM_PL_VRAM < TTM_PL_TT ||
-                        TTM_PL_VRAM < TTM_PL_SYSTEM);
-           type = fls(bo->preferred_domains &

AMDGPU_GEM_DOMAIN_MASK);

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -15
-           if (!type)
-                   return;
-           type--;
-           if (drm_WARN_ON_ONCE(&adev->ddev,
-                                type >= ARRAY_SIZE(domain_to_pl)))
-                   return;
-           type = domain_to_pl[type];
-   } else {
-           type = res->mem_type;
-   }
-
-   if (drm_WARN_ON_ONCE(&adev->ddev, type >= sz))
-           return;
-
-   /* DRM stats common fields: */

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -10
-
-   if (drm_gem_object_is_shared_for_memory_stats(obj))
-           stats[type].drm.shared += size;
-   else
-           stats[type].drm.private += size;
-
-   if (res) {
-           stats[type].drm.resident += size;
-
-           if (!dma_resv_test_signaled(obj->resv,

DMA_RESV_USAGE_BOOKKEEP))

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c u64 amdgpu_bo_gpu_offset_no_check(struct

Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -16
-                   stats[type].drm.active += size;
-           else if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
-                   stats[type].drm.purgeable += size;
-   }
-
-   /* amdgpu specific stats: */
-
-   if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) {
-           stats[TTM_PL_VRAM].requested += size;
-           if (type != TTM_PL_VRAM)
-                   stats[TTM_PL_VRAM].evicted += size;
-   } else if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_GTT) {
-           stats[TTM_PL_TT].requested += size;
-   }
-}
-
   /**
    * amdgpu_bo_release_notify - notification about a BO being released
    * @bo: pointer to a buffer object
@@ -1554,6 +1485,45 @@ u64 amdgpu_bo_gpu_offset_no_check(struct

amdgpu_bo *bo)

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +6 -0
     return amdgpu_gmc_sign_extend(offset);
   }
 
+/**
+ * amdgpu_bo_mem_stats_placement - bo placement for memory accounting
+ * @bo:    the buffer object we should look at
+ *
+ * BO can have multiple preferred placements, to avoid double
+counting we want

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
+ * to file it under a single placement for memory stats.
+ * Luckily, if we take the highest set bit in preferred_domains the
+result is
+ * quite sensible.
+ *
+ * Returns:
+ * Which of the placements should the BO be accounted under.
+ */
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo) {
+   uint32_t domain = bo->preferred_domains &

AMDGPU_GEM_DOMAIN_MASK;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h int amdgpu_bo_sync_wait_resv(struct amdgpu_device

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +24 -0
+
+   if (!domain)
+           return TTM_PL_SYSTEM;
+
+   switch (rounddown_pow_of_two(domain)) {
+   case AMDGPU_GEM_DOMAIN_CPU:
+           return TTM_PL_SYSTEM;
+   case AMDGPU_GEM_DOMAIN_GTT:
+           return TTM_PL_TT;
+   case AMDGPU_GEM_DOMAIN_VRAM:
+           return TTM_PL_VRAM;
+   case AMDGPU_GEM_DOMAIN_GDS:
+           return AMDGPU_PL_GDS;
+   case AMDGPU_GEM_DOMAIN_GWS:
+           return AMDGPU_PL_GWS;
+   case AMDGPU_GEM_DOMAIN_OA:
+           return AMDGPU_PL_OA;
+   case AMDGPU_GEM_DOMAIN_DOORBELL:
+           return AMDGPU_PL_DOORBELL;
+   default:
+           return TTM_PL_SYSTEM;
+   }
+}
+
   /**
    * amdgpu_bo_get_preferred_domain - get preferred domain
    * @adev: amdgpu device object
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index ce3314152d20f..bdc9a5bc4da46 100644
@@ -305,9 +305,7 @@ int amdgpu_bo_sync_wait_resv(struct amdgpu_device

*adev, struct dma_resv *resv,

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -2
   int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr);
   u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo);
   u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo); -void
 amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size);
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo);
   uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev,
                                         uint32_t domain);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -1
index 2852a6064c9ac..86cb83fb2283c 100644
@@ -26,15 +26,15 @@
 
   #include <linux/dma-direction.h>
   #include <drm/gpu_scheduler.h>
+#include <drm/ttm/ttm_placement.h>
   #include "amdgpu_vram_mgr.h"
-#include "amdgpu.h"
 
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h line 37

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -1
   #define AMDGPU_PL_GDS             (TTM_PL_PRIV + 0)
   #define AMDGPU_PL_GWS             (TTM_PL_PRIV + 1)
   #define AMDGPU_PL_OA              (TTM_PL_PRIV + 2)
   #define AMDGPU_PL_PREEMPT (TTM_PL_PRIV + 3)
   #define AMDGPU_PL_DOORBELL        (TTM_PL_PRIV + 4)
-#define __AMDGPU_PL_LAST   (TTM_PL_PRIV + 4)
+#define __AMDGPU_PL_LAST   (TTM_PL_PRIV + 5)

I like the the convention where "last" is equal to the last used index but maybe it is just me. I guess you strongly wanted to avoid the "+ 1"
in code albeit for a price in making it less obvious.

Quoted 1 line

Perhaps "_END" is a more common name for what I'm doing here, and yeah, I found that in earlier patches I had missed the +1 was required here and got an off by one bug.

For me "NUM" is how many elements, "LAST" and "MAX" and "END" are all the index of the last element. :) But maybe point me to some examples of _END and if it is an established pattern to mean what you want it to then fine.

Quoted 88 lines
Quoted 88 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
   #define AMDGPU_GTT_MAX_TRANSFER_SIZE      512
   #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS   2
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index d0db155a9ab7c..26effcf703d47 100644
@@ -36,6 +36,7 @@
   #include <drm/ttm/ttm_tt.h>
   #include <drm/drm_exec.h>
   #include "amdgpu.h"
+#include "amdgpu_vm.h"
   #include "amdgpu_trace.h"
   #include "amdgpu_amdkfd.h"
   #include "amdgpu_gmc.h"
@@ -310,6 +311,74 @@ static void amdgpu_vm_bo_reset_state_machine(struct

amdgpu_vm *vm)

Quoted 59 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +56 -0
     spin_unlock(&vm->status_lock);
   }
 
+/**
+ * amdgpu_vm_update_shared - helper to update shared memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @sign: if we should add (+1) or subtract (-1) from the shared stat
+ *
+ * Takes the vm status_lock and updates the shared memory stat. If
+the basic
+ * stat changed (e.g. buffer was moved) amdgpu_vm_update_stats need
+to be called
+ * as well.
+ */
+static void amdgpu_vm_update_shared(struct amdgpu_vm_bo_base *base,
+int sign) {
+   struct amdgpu_vm *vm = base->vm;
+   struct amdgpu_bo *bo = base->bo;
+   int64_t size = sign * amdgpu_bo_size(bo);
+   uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+
+   spin_lock(&vm->status_lock);
+   vm->stats[bo_memtype].drm.shared += size;
+   vm->stats[bo_memtype].drm.private -= size;
+   spin_unlock(&vm->status_lock);
+}
+
+/**
+ * amdgpu_vm_update_stats - helper to update normal memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @res:  the ttm_resource to use for the purpose of accounting, may or may not
+ *        be bo->tbo.resource
+ * @sign: if we should add (+1) or subtract (-1) from the stat
+ *
+ * Takes the vm status_lock and updates the basic memory stat. If the
+shared
+ * stat changed (e.g. buffer was exported) amdgpu_vm_update_shared
+need to be
+ * called as well.
+ */
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+                       struct ttm_resource *res, int sign) {
+   struct amdgpu_vm *vm = base->vm;
+   struct amdgpu_bo *bo = base->bo;
+   int64_t size = sign * amdgpu_bo_size(bo);
+   uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+   bool shared =
+drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base);
+
+   spin_lock(&vm->status_lock);
+
+   /* For drm-total- and drm-shared-, BO are accounted by their preferred
+    * placement, see also amdgpu_bo_mem_stats_placement.
+    */
+   if (shared)
+           vm->stats[bo_memtype].drm.shared += size;
+   else
+           vm->stats[bo_memtype].drm.private += size;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
+   if (res && res->mem_type < __AMDGPU_PL_LAST) {
+           uint32_t res_memtype = res->mem_type;
+
+           vm->stats[res_memtype].drm.resident += size;
+           /* BO only count as purgeable if it is resident,
+            * since otherwise there's nothing to purge.
+            */
+           if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
+                   vm->stats[res_memtype].drm.purgeable += size;
+           if (!(bo->preferred_domains &

amdgpu_mem_type_to_domain(res_memtype)))

Quoted 6 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm) · line 376

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
+                   vm->stats[bo_memtype].evicted += size;

So for VRAM+CPU Mesa common case, when backing store is in system memory, it not count as evicted? Intentionally?

Only when buffer prefers both placements. With this change the reported evicted stats matches the state machine for managing the buffers, the same comparison is done for adding the BO to the evicted list later in the code.

But it neither matches the current semantics nor offers anything interesting (in fact it would be misleading) to the userspace observing these stats, no?

Regards,

Tvrtko

Quoted 263 lines
Quoted 119 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_base_init(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
+   }
+
+   spin_unlock(&vm->status_lock);
+}
+
   /**
    * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
    *
@@ -332,6 +401,7 @@ void amdgpu_vm_bo_base_init(struct

amdgpu_vm_bo_base *base,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
             return;
     base->next = bo->vm_bo;
     bo->vm_bo = base;
+   amdgpu_vm_update_stats(base, bo->tbo.resource, +1);
 
     if (!amdgpu_vm_is_bo_always_valid(vm, bo))
             return;
@@ -1082,53 +1152,11 @@ int amdgpu_vm_update_range(struct

amdgpu_device *adev, struct amdgpu_vm *vm,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_del(struct amdgpu_device

Quoted 56 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -44
     return r;
   }
 
-static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,
-                               struct amdgpu_mem_stats *stats,
-                               unsigned int size)
-{
-   struct amdgpu_vm *vm = bo_va->base.vm;
-   struct amdgpu_bo *bo = bo_va->base.bo;
-
-   if (!bo)
-           return;
-
-   /*
-    * For now ignore BOs which are currently locked and potentially
-    * changing their location.
-    */
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo) &&
-       !dma_resv_trylock(bo->tbo.base.resv))
-           return;
-
-   amdgpu_bo_get_memory(bo, stats, size);
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo))
-           dma_resv_unlock(bo->tbo.base.resv);
-}
-
   void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size)
+                     struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST])
   {
-   struct amdgpu_bo_va *bo_va, *tmp;
-
     spin_lock(&vm->status_lock);
-   list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
+   memcpy(stats, vm->stats, sizeof(*stats) * __AMDGPU_PL_LAST);
     spin_unlock(&vm->status_lock);
   }
 
@@ -2075,6 +2103,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device

*adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo

Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +14 -0
                     if (*base != &bo_va->base)
                             continue;
 
+                   amdgpu_vm_update_stats(*base, bo->tbo.resource, -1);
                     *base = bo_va->base.next;
                     break;
             }
@@ -2140,6 +2169,22 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
     return true;
   }
 
+/**
+ * amdgpu_vm_bo_update_shared - called when bo gets shared/unshared
+ *
+ * @bo: amdgpu buffer object
+ * @sign: if we should add (+1) or subtract (-1) the memory stat
+ *
+ * Update the per VM stats for all the vm  */ void
+amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign) {
+   struct amdgpu_vm_bo_base *bo_base;
+
+   for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next)
+           amdgpu_vm_update_shared(bo_base, sign); }
+
   /**
    * amdgpu_vm_bo_invalidate - mark the bo as invalid
    *
@@ -2173,6 +2218,28 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_bo

*bo, bool evicted)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted)

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
     }
   }
 
+/**
+ * amdgpu_vm_bo_move - handle BO move
+ *
+ * @bo: amdgpu buffer object
+ * @new_mem: the new placement of the BO move
+ * @evicted: is the BO evicted
+ *
+ * Update the memory stats for the new placement and mark @bo as invalid.
+ */
+void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource

*new_mem,

Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted) · line 2237

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -0
+                  bool evicted)
+{
+   struct amdgpu_vm_bo_base *bo_base;
+
+   for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
+           amdgpu_vm_update_stats(bo_base, bo->tbo.resource, -1);
+           amdgpu_vm_update_stats(bo_base, new_mem, +1);

FWIW dropping and re-acquiring the lock makes the readout from amdgpu_vm_get_memory() non-atomic in terms of view consistency. Should be worth adding a helper which does the move in one go, with one lock acquire section.

Good point, thanks.

Quoted 129 lines

Regards,

Tvrtko

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_release_compute(struct

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
+   }
+
+   amdgpu_vm_bo_invalidate(bo, evicted); }
+
   /**
    * amdgpu_vm_get_block_size - calculate VM page table size as power of two
    *
@@ -2589,6 +2656,16 @@ void amdgpu_vm_release_compute(struct

amdgpu_device *adev, struct amdgpu_vm *vm)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_fini(struct amdgpu_device *adev,

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -1
     vm->is_compute_context = false;
   }
 
+static int amdgpu_vm_stats_is_zero(struct amdgpu_vm *vm) {
+   for (int i = 0; i < __AMDGPU_PL_LAST; ++i) {
+           if (!(drm_memory_stats_is_zero(&vm->stats[i].drm) &&
+                 vm->stats[i].evicted == 0))
+                   return false;
+   }
+   return true;
+}
+
   /**
    * amdgpu_vm_fini - tear down a vm instance
    *
@@ -2612,7 +2689,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev,
 struct amdgpu_vm *vm)
 
     root = amdgpu_bo_ref(vm->root.bo);
     amdgpu_bo_reserve(root, true);
-   amdgpu_vm_put_task_info(vm->task_info);
     amdgpu_vm_set_pasid(adev, vm, 0);
     dma_fence_wait(vm->last_unlocked, false);
     dma_fence_put(vm->last_unlocked);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -2660,6 +2736,15 @@ void amdgpu_vm_fini(struct amdgpu_device *adev,

struct amdgpu_vm *vm)

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
             }
     }
 
+   if (!amdgpu_vm_stats_is_zero(vm)) {
+           struct amdgpu_task_info *ti = vm->task_info;
+
+           dev_warn(adev->dev,
+                    "VM memory stats for proc %s(%d) task %s(%d) is non-zero

when fini\n",

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h int amdgpu_vm_bo_update(struct amdgpu_device

Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
+                    ti->process_name, ti->pid, ti->task_name, ti->tgid);
+   }
+
+   amdgpu_vm_put_task_info(vm->task_info);
   }
 
   /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -4
index 6a1b344e15e1b..257cea4e4f23e 100644
@@ -35,6 +35,7 @@
   #include "amdgpu_sync.h"
   #include "amdgpu_ring.h"
   #include "amdgpu_ids.h"
+#include "amdgpu_ttm.h"
 
   struct drm_exec;
 
@@ -324,10 +325,7 @@ struct amdgpu_vm_fault_info {
   struct amdgpu_mem_stats {
     struct drm_memory_stats drm;
 
-   /* buffers that requested this placement */
-   uint64_t requested;
-   /* buffers that requested this placement
-    * but are currently evicted */
+   /* buffers that requested this placement but are currently evicted
+*/
     uint64_t evicted;
   };
@@ -345,6 +343,9 @@ struct amdgpu_vm {
     /* Lock to protect vm_bo add/del/move on all lists of vm */
     spinlock_t              status_lock;
 
+   /* Memory statistics for this vm, protected by the status_lock */
+   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
+
     /* Per-VM and PT BOs who needs a validation */
     struct list_head        evicted;
 
@@ -525,6 +526,11 @@ int amdgpu_vm_bo_update(struct amdgpu_device

*adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h void amdgpu_vm_set_task_info(struct amdgpu_vm

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5 -0
                     bool clear);
   bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
   void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted);
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+                       struct ttm_resource *new_res, int sign); void
+amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign); void
+amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource *new_mem,
+                  bool evicted);
   uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr);
   struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
                                    struct amdgpu_bo *bo);
@@ -575,8 +581,7 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm

*vm);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free(struct

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -2
   void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
                             struct amdgpu_vm *vm);
   void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size);
+                     struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST]);
 
   int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                    struct amdgpu_bo_vm *vmbo, bool immediate); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
index f78a0434a48fa..b0bf216821152 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -0
@@ -537,6 +537,7 @@ static void amdgpu_vm_pt_free(struct

amdgpu_vm_bo_base *entry)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1 -0
     if (!entry->bo)
             return;
 
+   amdgpu_vm_update_stats(entry, entry->bo->tbo.resource, -1);
     entry->bo->vm_bo = NULL;
     ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
 
#11 lore raw

RE: [PATCH v9 5/5] drm/amdgpu: track bo memory stats at runtime

[Public]

Quoted 332 lines

From: Tvrtko Ursulin <[email protected]>
Sent: Tuesday, December 3, 2024 10:16

Quoted 24 lines · Yunxiang (Teddy)

[Public]

Quoted 22 lines

From: Tvrtko Ursulin <[email protected]>
Sent: Tuesday, December 3, 2024 9:42

Quoted 19 lines · Yunxiang Li

Before, every time fdinfo is queried we try to lock all the BOs in the VM and calculate memory usage from scratch. This works okay if the fdinfo is rarely read and the VMs don't have a ton of BOs. If either of these conditions is not true, we get a massive performance hit.

In this new revision, we track the BOs as they change states. This way when the fdinfo is queried we only need to take the status lock and copy out the usage stats with minimal impact to the runtime performance. With this new approach however, we would no longer be able to track active buffers.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c |  15 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c  |  18 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c  | 110 +++++-------
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h  |   4 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |   4 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      | 175 +++++++++++++++---

--

Quoted 304 lines
Quoted 303 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static void amdgpu_dma_buf_unmap(struct

Quoted 18 lines
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |  17 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c   |   1 +
   8 files changed, 204 insertions(+), 140 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -0
index b144404902255..423cea4e571ee 100644
@@ -36,6 +36,7 @@
   #include "amdgpu_gem.h"
   #include "amdgpu_dma_buf.h"
   #include "amdgpu_xgmi.h"
+#include "amdgpu_vm.h"
   #include <drm/amdgpu_drm.h>
   #include <drm/ttm/ttm_tt.h>
   #include <linux/dma-buf.h>
@@ -190,6 +191,14 @@ static void amdgpu_dma_buf_unmap(struct

dma_buf_attachment *attach,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +7 -0
     }
   }
 
+static void amdgpu_dma_buf_release(struct dma_buf *buf) {
+   struct amdgpu_bo *bo = gem_to_amdgpu_bo(buf->priv);
+
+   amdgpu_vm_bo_update_shared(bo, -1);
+   drm_gem_dmabuf_release(buf);
+}
+
   /**
    * amdgpu_dma_buf_begin_cpu_access -
Quoted 1 line

&dma_buf_ops.begin_cpu_access

implementation

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c struct dma_buf *amdgpu_gem_prime_export(struct

Quoted 11 lines

* @dma_buf: Shared DMA buffer

drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
@@ -237,7 +246,7 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = {
     .unpin = amdgpu_dma_buf_unpin,
     .map_dma_buf = amdgpu_dma_buf_map,
     .unmap_dma_buf = amdgpu_dma_buf_unmap,
-   .release = drm_gem_dmabuf_release,
+   .release = amdgpu_dma_buf_release,
     .begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
     .mmap = drm_gem_dmabuf_mmap,
     .vmap = drm_gem_dmabuf_vmap,
@@ -265,8 +274,10 @@ struct dma_buf *amdgpu_gem_prime_export(struct

drm_gem_object *gobj,

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +3 -1
             return ERR_PTR(-EPERM);
 
     buf = drm_gem_prime_export(gobj, flags);
-   if (!IS_ERR(buf))
+   if (!IS_ERR(buf)) {
             buf->ops = &amdgpu_dmabuf_ops;
+           amdgpu_vm_bo_update_shared(bo, +1);
+   }
 
     return buf;
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +0 -0
index 7717e3e4f05b5..e625e2481747e 100644
@@ -60,7 +60,7 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
Quoted 1 line

struct

drm_file *file)

Quoted 9 lines
     struct amdgpu_fpriv *fpriv = file->driver_priv;
     struct amdgpu_vm *vm = &fpriv->vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -1
-   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST + 1] = { };
+   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
     ktime_t usage[AMDGPU_HW_IP_NUM];
     const char *pl_name[] = {
             [TTM_PL_VRAM] = "vram", @@ -72,15 +72,8 @@ void

amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

Quoted 25 lines
             [AMDGPU_PL_DOORBELL] = "doorbell",
     };
     unsigned int hw_ip, i;
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -9
-   int ret;
-
-   ret = amdgpu_bo_reserve(vm->root.bo, false);
-   if (ret)
-           return;
-
-   amdgpu_vm_get_memory(vm, stats, ARRAY_SIZE(stats));
-   amdgpu_bo_unreserve(vm->root.bo);
 
+   amdgpu_vm_get_memory(vm, stats);
     amdgpu_ctx_mgr_usage(&fpriv->ctx_mgr, usage);
 
     /*
@@ -97,7 +90,6 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
 
             drm_print_memory_stats(p,
                                    &stats[i].drm,
-                                  DRM_GEM_OBJECT_ACTIVE |
                                    DRM_GEM_OBJECT_RESIDENT |
                                    DRM_GEM_OBJECT_PURGEABLE,
                                    pl_name[i]); @@ -115,9 +107,11
Quoted 1 line

@@ void amdgpu_show_fdinfo(struct drm_printer *p, struct

drm_file *file)

Quoted 19 lines
     drm_printf(p, "amd-evicted-vram:\t%llu KiB\n",
                stats[TTM_PL_VRAM].evicted/1024UL);
     drm_printf(p, "amd-requested-vram:\t%llu KiB\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -2
-              stats[TTM_PL_VRAM].requested/1024UL);
+              (stats[TTM_PL_VRAM].drm.shared +
+               stats[TTM_PL_VRAM].drm.private) / 1024UL);
     drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
-              stats[TTM_PL_TT].requested/1024UL);
+              (stats[TTM_PL_TT].drm.shared +
+               stats[TTM_PL_TT].drm.private) / 1024UL);
 
     for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
             if (!usage[hw_ip])
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index 951b20e40fd35..96f4b8904e9a6 100644
@@ -1258,7 +1258,7 @@ void amdgpu_bo_move_notify(struct
Quoted 1 line

ttm_buffer_object

*bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct

Quoted 9 lines
             return;

     abo = ttm_to_amdgpu_bo(bo);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -1
-   amdgpu_vm_bo_invalidate(abo, evict);
+   amdgpu_vm_bo_move(abo, new_mem, evict);
 
     amdgpu_bo_kunmap(abo);
@@ -1271,75 +1271,6 @@ void amdgpu_bo_move_notify(struct

ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -9
                          old_mem ? old_mem->mem_type : -1);
   }
 
-void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int sz)
-{
-   const unsigned int domain_to_pl[] = {
-           [ilog2(AMDGPU_GEM_DOMAIN_CPU)]      = TTM_PL_SYSTEM,
-           [ilog2(AMDGPU_GEM_DOMAIN_GTT)]      = TTM_PL_TT,
-           [ilog2(AMDGPU_GEM_DOMAIN_VRAM)]     = TTM_PL_VRAM,
-           [ilog2(AMDGPU_GEM_DOMAIN_GDS)]      =

AMDGPU_PL_GDS,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct · line 1283

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -1
-           [ilog2(AMDGPU_GEM_DOMAIN_GWS)]      =

AMDGPU_PL_GWS,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct · line 1285

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -2
-           [ilog2(AMDGPU_GEM_DOMAIN_OA)]       = AMDGPU_PL_OA,
-           [ilog2(AMDGPU_GEM_DOMAIN_DOORBELL)] =

AMDGPU_PL_DOORBELL,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -6
-   };
-   struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
-   struct ttm_resource *res = bo->tbo.resource;
-   struct drm_gem_object *obj = &bo->tbo.base;
-   uint64_t size = amdgpu_bo_size(bo);
-   unsigned int type;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -10
-
-   if (!res) {
-           /*
-            * If no backing store use one of the preferred domain for basic
-            * stats. We take the MSB since that should give a reasonable
-            * view.
-            */
-           BUILD_BUG_ON(TTM_PL_VRAM < TTM_PL_TT ||
-                        TTM_PL_VRAM < TTM_PL_SYSTEM);
-           type = fls(bo->preferred_domains &

AMDGPU_GEM_DOMAIN_MASK);

Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -15
-           if (!type)
-                   return;
-           type--;
-           if (drm_WARN_ON_ONCE(&adev->ddev,
-                                type >= ARRAY_SIZE(domain_to_pl)))
-                   return;
-           type = domain_to_pl[type];
-   } else {
-           type = res->mem_type;
-   }
-
-   if (drm_WARN_ON_ONCE(&adev->ddev, type >= sz))
-           return;
-
-   /* DRM stats common fields: */

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -10
-
-   if (drm_gem_object_is_shared_for_memory_stats(obj))
-           stats[type].drm.shared += size;
-   else
-           stats[type].drm.private += size;
-
-   if (res) {
-           stats[type].drm.resident += size;
-
-           if (!dma_resv_test_signaled(obj->resv,

DMA_RESV_USAGE_BOOKKEEP))

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -16
-                   stats[type].drm.active += size;
-           else if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
-                   stats[type].drm.purgeable += size;
-   }
-
-   /* amdgpu specific stats: */
-
-   if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM) {
-           stats[TTM_PL_VRAM].requested += size;
-           if (type != TTM_PL_VRAM)
-                   stats[TTM_PL_VRAM].evicted += size;
-   } else if (bo->preferred_domains & AMDGPU_GEM_DOMAIN_GTT) {
-           stats[TTM_PL_TT].requested += size;
-   }
-}
-
   /**
    * amdgpu_bo_release_notify - notification about a BO being released
    * @bo: pointer to a buffer object @@ -1554,6 +1485,45 @@ u64
Quoted 1 line

amdgpu_bo_gpu_offset_no_check(struct

amdgpu_bo *bo)

Quoted 9 lines
     return amdgpu_gmc_sign_extend(offset);

}

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +6 -0
+/**
+ * amdgpu_bo_mem_stats_placement - bo placement for memory accounting
+ * @bo:    the buffer object we should look at
+ *
+ * BO can have multiple preferred placements, to avoid double
+counting we want

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
+ * to file it under a single placement for memory stats.
+ * Luckily, if we take the highest set bit in preferred_domains the
+result is
+ * quite sensible.
+ *
+ * Returns:
+ * Which of the placements should the BO be accounted under.
+ */
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo) {
+   uint32_t domain = bo->preferred_domains &

AMDGPU_GEM_DOMAIN_MASK;

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +24 -0
+
+   if (!domain)
+           return TTM_PL_SYSTEM;
+
+   switch (rounddown_pow_of_two(domain)) {
+   case AMDGPU_GEM_DOMAIN_CPU:
+           return TTM_PL_SYSTEM;
+   case AMDGPU_GEM_DOMAIN_GTT:
+           return TTM_PL_TT;
+   case AMDGPU_GEM_DOMAIN_VRAM:
+           return TTM_PL_VRAM;
+   case AMDGPU_GEM_DOMAIN_GDS:
+           return AMDGPU_PL_GDS;
+   case AMDGPU_GEM_DOMAIN_GWS:
+           return AMDGPU_PL_GWS;
+   case AMDGPU_GEM_DOMAIN_OA:
+           return AMDGPU_PL_OA;
+   case AMDGPU_GEM_DOMAIN_DOORBELL:
+           return AMDGPU_PL_DOORBELL;
+   default:
+           return TTM_PL_SYSTEM;
+   }
+}
+
   /**
    * amdgpu_bo_get_preferred_domain - get preferred domain
    * @adev: amdgpu device object
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index ce3314152d20f..bdc9a5bc4da46 100644
@@ -305,9 +305,7 @@ int amdgpu_bo_sync_wait_resv(struct
Quoted 1 line

amdgpu_device

*adev, struct dma_resv *resv,

Quoted 20 lines
   int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr);
   u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo);
   u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo); -void

amdgpu_bo_get_memory(struct amdgpu_bo *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -2
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size);
+uint32_t amdgpu_bo_mem_stats_placement(struct amdgpu_bo *bo);
   uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev,
                                         uint32_t domain);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -0
index 2852a6064c9ac..86cb83fb2283c 100644
@@ -26,15 +26,15 @@
 
   #include <linux/dma-direction.h>
   #include <drm/gpu_scheduler.h>
+#include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h line 37

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -2
   #include "amdgpu_vram_mgr.h"
-#include "amdgpu.h"
 
   #define AMDGPU_PL_GDS             (TTM_PL_PRIV + 0)
   #define AMDGPU_PL_GWS             (TTM_PL_PRIV + 1)
   #define AMDGPU_PL_OA              (TTM_PL_PRIV + 2)
   #define AMDGPU_PL_PREEMPT (TTM_PL_PRIV + 3)
   #define AMDGPU_PL_DOORBELL        (TTM_PL_PRIV + 4)
-#define __AMDGPU_PL_LAST   (TTM_PL_PRIV + 4)
+#define __AMDGPU_PL_LAST   (TTM_PL_PRIV + 5)

I like the the convention where "last" is equal to the last used index but maybe it is just me. I guess you strongly wanted to avoid the "+ 1" in code albeit for a price in making it less obvious.

Quoted 7 lines
Quoted 1 line

Perhaps "_END" is a more common name for what I'm doing here, and yeah, I

found that in earlier patches I had missed the +1 was required here and got an off by one bug.

For me "NUM" is how many elements, "LAST" and "MAX" and "END" are all the index of the last element. :) But maybe point me to some examples of _END and if it is an established pattern to mean what you want it to then fine.

Quite interesting, I wasn't expecting this, but I did some grepping and found that there's no consistency at all in how to use _{NUM,LAST,MAX,END} in the kernel code base. For each of these suffixes there seems to be just as may cases for "equaling the last" as there are "one past the last". Guess we have just found another holy war.

Quoted 95 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
   #define AMDGPU_GTT_MAX_TRANSFER_SIZE      512
   #define AMDGPU_GTT_NUM_TRANSFER_WINDOWS   2
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index d0db155a9ab7c..26effcf703d47 100644
@@ -36,6 +36,7 @@
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
   #include <drm/ttm/ttm_tt.h>
   #include <drm/drm_exec.h>
   #include "amdgpu.h"
+#include "amdgpu_vm.h"
   #include "amdgpu_trace.h"
   #include "amdgpu_amdkfd.h"
   #include "amdgpu_gmc.h"
@@ -310,6 +311,74 @@ static void

amdgpu_vm_bo_reset_state_machine(struct

Quoted 24 lines
Quoted 24 lines

amdgpu_vm *vm)

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +20 -0
     spin_unlock(&vm->status_lock);
   }
 
+/**
+ * amdgpu_vm_update_shared - helper to update shared memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @sign: if we should add (+1) or subtract (-1) from the shared stat
+ *
+ * Takes the vm status_lock and updates the shared memory stat. If
+the basic
+ * stat changed (e.g. buffer was moved) amdgpu_vm_update_stats need
+to be called
+ * as well.
+ */
+static void amdgpu_vm_update_shared(struct amdgpu_vm_bo_base *base,
+int sign) {
+   struct amdgpu_vm *vm = base->vm;
+   struct amdgpu_bo *bo = base->bo;
+   int64_t size = sign * amdgpu_bo_size(bo);
+   uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+
+   spin_lock(&vm->status_lock);
+   vm->stats[bo_memtype].drm.shared += size;
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8 -0
+   vm->stats[bo_memtype].drm.private -= size;
+   spin_unlock(&vm->status_lock);
+}
+
+/**
+ * amdgpu_vm_update_stats - helper to update normal memory stat
+ * @base: base structure for tracking BO usage in a VM
+ * @res:  the ttm_resource to use for the purpose of accounting, may or may

not

Quoted 39 lines
Quoted 39 lines
Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +28 -0
+ *        be bo->tbo.resource
+ * @sign: if we should add (+1) or subtract (-1) from the stat
+ *
+ * Takes the vm status_lock and updates the basic memory stat. If the
+shared
+ * stat changed (e.g. buffer was exported) amdgpu_vm_update_shared
+need to be
+ * called as well.
+ */
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+                       struct ttm_resource *res, int sign) {
+   struct amdgpu_vm *vm = base->vm;
+   struct amdgpu_bo *bo = base->bo;
+   int64_t size = sign * amdgpu_bo_size(bo);
+   uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo);
+   bool shared =
+drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base);
+
+   spin_lock(&vm->status_lock);
+
+   /* For drm-total- and drm-shared-, BO are accounted by their preferred
+    * placement, see also amdgpu_bo_mem_stats_placement.
+    */
+   if (shared)
+           vm->stats[bo_memtype].drm.shared += size;
+   else
+           vm->stats[bo_memtype].drm.private += size;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
+   if (res && res->mem_type < __AMDGPU_PL_LAST) {
+           uint32_t res_memtype = res->mem_type;
+
+           vm->stats[res_memtype].drm.resident += size;
+           /* BO only count as purgeable if it is resident,
+            * since otherwise there's nothing to purge.
+            */
+           if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE)
+                   vm->stats[res_memtype].drm.purgeable += size;
+           if (!(bo->preferred_domains &

amdgpu_mem_type_to_domain(res_memtype)))

Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm) · line 376

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
+                   vm->stats[bo_memtype].evicted += size;

So for VRAM+CPU Mesa common case, when backing store is in system

memory,

Quoted 2 lines
Quoted 1 line

it not count as evicted? Intentionally?

Quoted 7 lines
Quoted 1 line

Only when buffer prefers both placements. With this change the reported evicted

stats matches the state machine for managing the buffers, the same comparison is done for adding the BO to the evicted list later in the code.

But it neither matches the current semantics nor offers anything interesting (in fact it would be misleading) to the userspace observing these stats, no?

If the code that manages the buffer placement doesn't treat the buffer is evicted, wouldn't it be more confusing to report them as such? The evicted stats will be positive even when VRAM is quite empty, and these "evicted" buffers won't be moved at all.

Quoted 274 lines

Regards,

Tvrtko

Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
+   }
+
+   spin_unlock(&vm->status_lock);
+}
+
   /**
    * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the

vm

Quoted 69 lines
Quoted 69 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_base_init(struct

Quoted 2 lines
    *
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -332,6 +401,7 @@ void amdgpu_vm_bo_base_init(struct

amdgpu_vm_bo_base *base,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
             return;
     base->next = bo->vm_bo;
     bo->vm_bo = base;
+   amdgpu_vm_update_stats(base, bo->tbo.resource, +1);
 
     if (!amdgpu_vm_is_bo_always_valid(vm, bo))
             return;
@@ -1082,53 +1152,11 @@ int amdgpu_vm_update_range(struct

amdgpu_device *adev, struct amdgpu_vm *vm,

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_del(struct amdgpu_device

Quoted 56 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -44
     return r;
   }
 
-static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,
-                               struct amdgpu_mem_stats *stats,
-                               unsigned int size)
-{
-   struct amdgpu_vm *vm = bo_va->base.vm;
-   struct amdgpu_bo *bo = bo_va->base.bo;
-
-   if (!bo)
-           return;
-
-   /*
-    * For now ignore BOs which are currently locked and potentially
-    * changing their location.
-    */
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo) &&
-       !dma_resv_trylock(bo->tbo.base.resv))
-           return;
-
-   amdgpu_bo_get_memory(bo, stats, size);
-   if (!amdgpu_vm_is_bo_always_valid(vm, bo))
-           dma_resv_unlock(bo->tbo.base.resv);
-}
-
   void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size)
+                     struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST])
   {
-   struct amdgpu_bo_va *bo_va, *tmp;
-
     spin_lock(&vm->status_lock);
-   list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
-
-   list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status)
-           amdgpu_vm_bo_get_memory(bo_va, stats, size);
+   memcpy(stats, vm->stats, sizeof(*stats) * __AMDGPU_PL_LAST);
     spin_unlock(&vm->status_lock);
   }
 
@@ -2075,6 +2103,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device

*adev,

Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c bool amdgpu_vm_evictable(struct amdgpu_bo

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
                     if (*base != &bo_va->base)
                             continue;
 
+                   amdgpu_vm_update_stats(*base, bo->tbo.resource, -1);
                     *base = bo_va->base.next;
                     break;
             }
@@ -2140,6 +2169,22 @@ bool amdgpu_vm_evictable(struct amdgpu_bo

*bo)

Quoted 52 lines
Quoted 35 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo

Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +13 -0
     return true;
   }
 
+/**
+ * amdgpu_vm_bo_update_shared - called when bo gets shared/unshared
+ *
+ * @bo: amdgpu buffer object
+ * @sign: if we should add (+1) or subtract (-1) the memory stat
+ *
+ * Update the per VM stats for all the vm  */ void
+amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign) {
+   struct amdgpu_vm_bo_base *bo_base;
+
+   for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next)
+           amdgpu_vm_update_shared(bo_base, sign); }
+
   /**
    * amdgpu_vm_bo_invalidate - mark the bo as invalid
    *
@@ -2173,6 +2218,28 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_bo

*bo, bool evicted)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted)

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
     }
   }
 
+/**
+ * amdgpu_vm_bo_move - handle BO move
+ *
+ * @bo: amdgpu buffer object
+ * @new_mem: the new placement of the BO move
+ * @evicted: is the BO evicted
+ *
+ * Update the memory stats for the new placement and mark @bo as invalid.
+ */
+void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource

*new_mem,

Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted) · line 2237

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -0
+                  bool evicted)
+{
+   struct amdgpu_vm_bo_base *bo_base;
+
+   for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
+           amdgpu_vm_update_stats(bo_base, bo->tbo.resource, -1);
+           amdgpu_vm_update_stats(bo_base, new_mem, +1);

FWIW dropping and re-acquiring the lock makes the readout from amdgpu_vm_get_memory() non-atomic in terms of view consistency. Should be worth adding a helper which does the move in one go, with one lock acquire section.

Good point, thanks.

Quoted 2 lines

Regards,

Quoted 8 lines
Quoted 8 lines

Tvrtko

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm)

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
+   }
+
+   amdgpu_vm_bo_invalidate(bo, evicted); }
+
   /**
    * amdgpu_vm_get_block_size - calculate VM page table size as power of

two

Quoted 21 lines
Quoted 21 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_release_compute(struct

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
    *
@@ -2589,6 +2656,16 @@ void amdgpu_vm_release_compute(struct

amdgpu_device *adev, struct amdgpu_vm *vm)

Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -0
     vm->is_compute_context = false;
   }
 
+static int amdgpu_vm_stats_is_zero(struct amdgpu_vm *vm) {
+   for (int i = 0; i < __AMDGPU_PL_LAST; ++i) {
+           if (!(drm_memory_stats_is_zero(&vm->stats[i].drm) &&
+                 vm->stats[i].evicted == 0))
+                   return false;
+   }
+   return true;
+}
+
   /**
    * amdgpu_vm_fini - tear down a vm instance
    *
@@ -2612,7 +2689,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev,
 struct amdgpu_vm *vm)
 
Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_fini(struct amdgpu_device

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
     root = amdgpu_bo_ref(vm->root.bo);
     amdgpu_bo_reserve(root, true);
-   amdgpu_vm_put_task_info(vm->task_info);
     amdgpu_vm_set_pasid(adev, vm, 0);
     dma_fence_wait(vm->last_unlocked, false);
     dma_fence_put(vm->last_unlocked);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -2660,6 +2736,15 @@ void amdgpu_vm_fini(struct amdgpu_device

*adev,

Quoted 55 lines
Quoted 55 lines

struct amdgpu_vm *vm)

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -0
             }
     }
 
+   if (!amdgpu_vm_stats_is_zero(vm)) {
+           struct amdgpu_task_info *ti = vm->task_info;
+
+           dev_warn(adev->dev,
+                    "VM memory stats for proc %s(%d) task %s(%d) is non-zero

when fini\n",

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h int amdgpu_vm_bo_update(struct amdgpu_device

Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
+                    ti->process_name, ti->pid, ti->task_name, ti->tgid);
+   }
+
+   amdgpu_vm_put_task_info(vm->task_info);
   }
 
   /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -4
index 6a1b344e15e1b..257cea4e4f23e 100644
@@ -35,6 +35,7 @@
   #include "amdgpu_sync.h"
   #include "amdgpu_ring.h"
   #include "amdgpu_ids.h"
+#include "amdgpu_ttm.h"
 
   struct drm_exec;
 
@@ -324,10 +325,7 @@ struct amdgpu_vm_fault_info {
   struct amdgpu_mem_stats {
     struct drm_memory_stats drm;
 
-   /* buffers that requested this placement */
-   uint64_t requested;
-   /* buffers that requested this placement
-    * but are currently evicted */
+   /* buffers that requested this placement but are currently evicted
+*/
     uint64_t evicted;
   };
@@ -345,6 +343,9 @@ struct amdgpu_vm {
     /* Lock to protect vm_bo add/del/move on all lists of vm */
     spinlock_t              status_lock;
 
+   /* Memory statistics for this vm, protected by the status_lock */
+   struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST];
+
     /* Per-VM and PT BOs who needs a validation */
     struct list_head        evicted;
 
@@ -525,6 +526,11 @@ int amdgpu_vm_bo_update(struct amdgpu_device

*adev,

Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h int amdgpu_vm_bo_update(struct amdgpu_device *adev,

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4 -0
                     bool clear);
   bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
   void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted);
+void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base,
+                       struct ttm_resource *new_res, int sign); void
+amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo, int sign); void
+amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource

*new_mem,

Quoted 2 lines
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h int amdgpu_vm_bo_update(struct amdgpu_device · line 534

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
+                  bool evicted);
   uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t

addr);

Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h void amdgpu_vm_set_task_info(struct amdgpu_vm

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
   struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
                                    struct amdgpu_bo *bo);
@@ -575,8 +581,7 @@ void amdgpu_vm_set_task_info(struct amdgpu_vm

*vm);

Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -2
   void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
                             struct amdgpu_vm *vm);
   void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
-                     struct amdgpu_mem_stats *stats,
-                     unsigned int size);
+                     struct amdgpu_mem_stats stats[__AMDGPU_PL_LAST]);
 
   int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm

*vm,

Quoted 15 lines
Quoted 15 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c static void amdgpu_vm_pt_free(struct

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
                    struct amdgpu_bo_vm *vmbo, bool immediate); diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
index f78a0434a48fa..b0bf216821152 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -0
@@ -537,6 +537,7 @@ static void amdgpu_vm_pt_free(struct

amdgpu_vm_bo_base *entry)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +1 -0
     if (!entry->bo)
             return;
 
+   amdgpu_vm_update_stats(entry, entry->bo->tbo.resource, -1);
     entry->bo->vm_bo = NULL;
     ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);