Skip to contentSkip to conversation

drm/ttm: allocate resource object instead of embedding it v2

PATCH In review Christian König 5 participants 43 messages Jun 2, 2021 – Jun 8, 2021

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

PATCH 1/10 drm/ttm: allocate resource object instead of embedding it v2

To improve the handling we want the establish the resource object as base class for the backend allocations.

v2: add missing error handling

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 54 +++++++-------
 drivers/gpu/drm/nouveau/nouveau_bo.c       |  2 +-
 drivers/gpu/drm/radeon/radeon_ttm.c        |  2 +-
 drivers/gpu/drm/ttm/ttm_bo.c               | 83 ++++++++--------------
 drivers/gpu/drm/ttm/ttm_bo_util.c          | 43 ++++++-----
 drivers/gpu/drm/ttm/ttm_resource.c         | 31 +++++---
 drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c |  2 +-
 include/drm/ttm/ttm_bo_api.h               |  1 -
 include/drm/ttm/ttm_bo_driver.h            | 10 ++-
 include/drm/ttm/ttm_resource.h             |  4 +-
 11 files changed, 110 insertions(+), 126 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -2
index 03c6b63d1d54..59723c3d5826 100644
@@ -362,14 +362,14 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
 	if (cpu_addr)
 		amdgpu_bo_kunmap(*bo_ptr);
 
-	ttm_resource_free(&(*bo_ptr)->tbo, (*bo_ptr)->tbo.resource);
+	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
 
 	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
 		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
 		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
 	}
 	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
-			     (*bo_ptr)->tbo.resource, &ctx);
+			     &(*bo_ptr)->tbo.resource, &ctx);
 	if (r)
 		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -27

80 lines not on this page · raw message

drivers/gpu/drm/nouveau/nouveau_bo.c +1 -1
index e688ca77483d..3a0d9b3bf991 100644
@@ -1009,7 +1009,7 @@ nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
 	if (old_reg->mem_type == TTM_PL_TT &&
 	    new_reg->mem_type == TTM_PL_SYSTEM) {
 		nouveau_ttm_tt_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
 		ttm_bo_assign_mem(bo, new_reg);
 		goto out;
 	}
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
index 2507c1741681..cdffa9b65108 100644
@@ -229,7 +229,7 @@ static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
 	if (old_mem->mem_type == TTM_PL_TT &&
 	    new_mem->mem_type == TTM_PL_SYSTEM) {
 		radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
 		ttm_bo_assign_mem(bo, new_mem);
 		goto out;
 	}
drivers/gpu/drm/ttm/ttm_bo.c +28 -55

207 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_bo_util.c +21 -22

103 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_resource.c +23 -8
index 59e2b7157e41..65451e1bc303 100644
@@ -27,10 +27,16 @@
 
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
 		       const struct ttm_place *place,
-		       struct ttm_resource *res)
+		       struct ttm_resource **res_ptr)
 {
 	struct ttm_resource_manager *man =
 		ttm_manager_type(bo->bdev, place->mem_type);
+	struct ttm_resource *res;
+	int r;
+
+	res = kmalloc(sizeof(*res), GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
 
 	res->mm_node = NULL;
 	res->start = 0;
@@ -41,18 +47,27 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
 	res->bus.offset = 0;
 	res->bus.is_iomem = false;
 	res->bus.caching = ttm_cached;
+	r = man->func->alloc(man, bo, place, res);
+	if (r) {
+		kfree(res);
+		return r;
+	}
 
-	return man->func->alloc(man, bo, place, res);
+	*res_ptr = res;
+	return 0;
 }
 
-void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource *res)
+void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
 {
-	struct ttm_resource_manager *man =
-		ttm_manager_type(bo->bdev, res->mem_type);
+	struct ttm_resource_manager *man;
 
-	man->func->free(man, res);
-	res->mm_node = NULL;
-	res->mem_type = TTM_PL_SYSTEM;
+	if (!*res)
+		return;
+
+	man = ttm_manager_type(bo->bdev, (*res)->mem_type);
+	man->func->free(man, *res);
+	kfree(*res);
+	*res = NULL;
 }
 EXPORT_SYMBOL(ttm_resource_free);
 
drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c +1 -1
index ed8563ef9a3b..bfcf31bf7e37 100644
@@ -741,7 +741,7 @@ static int vmw_move(struct ttm_buffer_object *bo,
 			goto fail;
 
 		vmw_ttm_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
 		ttm_bo_assign_mem(bo, new_mem);
 		return 0;
 	} else {
include/drm/ttm/ttm_bo_api.h +0 -1
index 291a339a7e08..f681bbdbc698 100644
@@ -137,7 +137,6 @@ struct ttm_buffer_object {
 	 */
 
 	struct ttm_resource *resource;
-	struct ttm_resource _mem;
 	struct ttm_tt *ttm;
 	bool deleted;
 
include/drm/ttm/ttm_bo_driver.h +4 -6
index 1a9ba0b13622..ead0ef7136c8 100644
@@ -96,7 +96,7 @@ struct ttm_lru_bulk_move {
  */
 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 		     struct ttm_placement *placement,
-		     struct ttm_resource *mem,
+		     struct ttm_resource **mem,
 		     struct ttm_operation_ctx *ctx);
 
 /**
@@ -188,8 +188,8 @@ ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
 				     struct ttm_resource *new_mem)
 {
-	bo->_mem = *new_mem;
-	new_mem->mm_node = NULL;
+	WARN_ON(bo->resource);
+	bo->resource = new_mem;
 }
 
 /**
@@ -202,9 +202,7 @@ static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
 				    struct ttm_resource *new_mem)
 {
-	struct ttm_resource *old_mem = bo->resource;
-
-	WARN_ON(old_mem->mm_node != NULL);
+	ttm_resource_free(bo, &bo->resource);
 	ttm_bo_assign_mem(bo, new_mem);
 }
 
include/drm/ttm/ttm_resource.h +2 -2
index 890b9d369519..c17c1a52070d 100644
@@ -225,8 +225,8 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
 
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
 		       const struct ttm_place *place,
-		       struct ttm_resource *res);
-void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource *res);
+		       struct ttm_resource **res);
+void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
 
 void ttm_resource_manager_init(struct ttm_resource_manager *man,
 			       unsigned long p_size);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/10 drm/ttm: flip over the range manager to self allocated nodes

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 +
 drivers/gpu/drm/drm_gem_vram_helper.c   |  2 +
 drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 +
 drivers/gpu/drm/qxl/qxl_ttm.c           |  1 +
 drivers/gpu/drm/radeon/radeon_ttm.c     |  1 +
 drivers/gpu/drm/ttm/ttm_range_manager.c | 56 ++++++++++++++++++-------
 drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++----
 include/drm/ttm/ttm_bo_driver.h         | 26 ------------
 include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++
 include/drm/ttm/ttm_resource.h          |  3 ++
 10 files changed, 111 insertions(+), 50 deletions(-)
 create mode 100644 include/drm/ttm/ttm_range_manager.h
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 #include <drm/amdgpu_drm.h>
 
drivers/gpu/drm/drm_gem_vram_helper.c +2 -0
index 83e7258c7f90..17a4c5d47b6a 100644
@@ -17,6 +17,8 @@
 #include <drm/drm_prime.h>
 #include <drm/drm_simple_kms_helper.h>
 
+#include <drm/ttm/ttm_range_manager.h>
+
 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
 
 /**
drivers/gpu/drm/nouveau/nouveau_ttm.c +2 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
 #include <linux/limits.h>
 #include <linux/swiotlb.h>
 
+#include <drm/ttm/ttm_range_manager.h>
+
 #include "nouveau_drv.h"
 #include "nouveau_gem.h"
 #include "nouveau_mem.h"
drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 #include "qxl_drv.h"
 #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 #include "radeon_reg.h"
 #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +41 -15

112 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_resource.c +17 -9
index 65451e1bc303..2a51ace17614 100644
@@ -25,6 +25,22 @@
 #include <drm/ttm/ttm_resource.h>
 #include <drm/ttm/ttm_bo_driver.h>
 
+void ttm_resource_init(struct ttm_buffer_object *bo,
+                       const struct ttm_place *place,
+                       struct ttm_resource *res)
+{
+	res->mm_node = NULL;
+	res->start = 0;
+	res->num_pages = PFN_UP(bo->base.size);
+	res->mem_type = place->mem_type;
+	res->placement = place->flags;
+	res->bus.addr = NULL;
+	res->bus.offset = 0;
+	res->bus.is_iomem = false;
+	res->bus.caching = ttm_cached;
+}
+EXPORT_SYMBOL(ttm_resource_init);
+
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
 		       const struct ttm_place *place,
 		       struct ttm_resource **res_ptr)
@@ -38,15 +54,7 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
 	if (!res)
 		return -ENOMEM;
 
-	res->mm_node = NULL;
-	res->start = 0;
-	res->num_pages = PFN_UP(bo->base.size);
-	res->mem_type = place->mem_type;
-	res->placement = place->flags;
-	res->bus.addr = NULL;
-	res->bus.offset = 0;
-	res->bus.is_iomem = false;
-	res->bus.caching = ttm_cached;
+	ttm_resource_init(bo, place, res);
 	r = man->func->alloc(man, bo, place, res);
 	if (r) {
 		kfree(res);
include/drm/ttm/ttm_bo_driver.h +0 -26
index ead0ef7136c8..b266971c1974 100644
@@ -304,30 +304,4 @@ int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
  */
 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
 
-/**
- * ttm_range_man_init
- *
- * @bdev: ttm device
- * @type: memory manager type
- * @use_tt: if the memory manager uses tt
- * @p_size: size of area to be managed in pages.
- *
- * Initialise a generic range manager for the selected memory type.
- * The range manager is installed for this device in the type slot.
- */
-int ttm_range_man_init(struct ttm_device *bdev,
-		       unsigned type, bool use_tt,
-		       unsigned long p_size);
-
-/**
- * ttm_range_man_fini
- *
- * @bdev: ttm device
- * @type: memory manager type
- *
- * Remove the generic range manager from a slot and tear it down.
- */
-int ttm_range_man_fini(struct ttm_device *bdev,
-		       unsigned type);
-
 #endif
include/drm/ttm/ttm_range_manager.h new file +43 -0

43 lines not on this page · raw message

include/drm/ttm/ttm_resource.h +3 -0
index c17c1a52070d..803e4875d779 100644
@@ -223,6 +223,9 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
 	man->move = NULL;
 }
 
+void ttm_resource_init(struct ttm_buffer_object *bo,
+                       const struct ttm_place *place,
+                       struct ttm_resource *res);
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
 		       const struct ttm_place *place,
 		       struct ttm_resource **res);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Quoted 114 lines · Christian König

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 +
  drivers/gpu/drm/drm_gem_vram_helper.c   |  2 +
  drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 +
  drivers/gpu/drm/qxl/qxl_ttm.c           |  1 +
  drivers/gpu/drm/radeon/radeon_ttm.c     |  1 +
  drivers/gpu/drm/ttm/ttm_range_manager.c | 56 ++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++----
  include/drm/ttm/ttm_bo_driver.h         | 26 ------------
  include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++
  include/drm/ttm/ttm_resource.h          |  3 ++
  10 files changed, 111 insertions(+), 50 deletions(-)

create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
  #include <drm/ttm/ttm_bo_api.h>
  #include <drm/ttm/ttm_bo_driver.h>
  #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
  #include <drm/amdgpu_drm.h>
 
drivers/gpu/drm/drm_gem_vram_helper.c +2 -0
index 83e7258c7f90..17a4c5d47b6a 100644
@@ -17,6 +17,8 @@
  #include <drm/drm_prime.h>
  #include <drm/drm_simple_kms_helper.h>
 
+#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
 
  /**
drivers/gpu/drm/nouveau/nouveau_ttm.c +2 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
  #include <linux/limits.h>
  #include <linux/swiotlb.h>
 
+#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"
drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
  #include <drm/ttm/ttm_bo_api.h>
  #include <drm/ttm/ttm_bo_driver.h>
  #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
  #include "qxl_drv.h"
  #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
  #include <drm/ttm/ttm_bo_api.h>
  #include <drm/ttm/ttm_bo_driver.h>
  #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
  #include "radeon_reg.h"
  #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +4 -3
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
   * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
   */
 
-#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>
  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
 
  /*
   * Currently we use a spinlock for the lock, but a mutex *may* be
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
  			       struct ttm_resource *mem)
  {
  	struct ttm_range_manager *rman = to_range_manager(man);
+	struct ttm_range_mgr_node *node;
  	struct drm_mm *mm = &rman->mm;
-	struct drm_mm_node *node;
  	enum drm_mm_insert_mode mode;
  	unsigned long lpfn;
  	int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct ttm_resource_manager *man, · line 74

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
  	if (!lpfn)
  		lpfn = man->size;
 
-	node = kzalloc(sizeof(*node), GFP_KERNEL);
+	node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

Thanks,

Thomas

#3 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 116 lines · Christian König
Quoted 106 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56 ++++++++++++++++++------- drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++ include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Regards,
Christian.

Quoted 6 lines

Thanks,

Thomas

#4 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Quoted 133 lines · Christian König

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 117 lines · Christian König
Quoted 107 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++ include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

Quoted 4 lines

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

/Thomas

Quoted 9 lines

Regards,
Christian.

Quoted 6 lines

Thanks,

Thomas

#5 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Am 02.06.21 um 14:33 schrieb Thomas Hellström (Intel):

Quoted 135 lines · Christian König
Quoted 133 lines

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 117 lines · Christian König
Quoted 107 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++ include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

Quoted 5 lines
Quoted 4 lines

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

Yes, that's the general idea I'm targeting for. I'm just not fully there yet.

The ttm range manager then provides functions to implement debugging and/or the iterator for example while the driver specific parts only implement stuff like special placement handling.

Christian.

Quoted 14 lines

/Thomas

Quoted 9 lines

Regards,
Christian.

Quoted 6 lines

Thanks,

Thomas

#6 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Quoted 138 lines · Christian König

Am 02.06.21 um 14:33 schrieb Thomas Hellström (Intel):

Quoted 135 lines · Christian König
Quoted 133 lines

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 117 lines · Christian König
Quoted 107 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++ include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

Yes, that's the general idea I'm targeting for. I'm just not fully there yet.

Hmm, I don't fully understand the reply, I described a buggy scenario and you replied that's what we're targeting for?

I assume you mean we're going to get an init() method for the range manager, and a destroy method for the struct ttm_resource?

Thanks,

Thomas

#7 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Am 02.06.21 um 16:13 schrieb Thomas Hellström (Intel):

Quoted 142 lines · Christian König
Quoted 140 lines

Am 02.06.21 um 14:33 schrieb Thomas Hellström (Intel):

Quoted 137 lines · Christian König
Quoted 135 lines

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 117 lines · Christian König
Quoted 107 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++ include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

Yes, that's the general idea I'm targeting for. I'm just not fully there yet.

Hmm, I don't fully understand the reply, I described a buggy scenario and you replied that's what we're targeting for?

Ok, I don't seem to understand what you mean here. What is buggy on that?

Quoted 1 line

I assume you mean we're going to get an init() method for the range manager, and a destroy method for the struct ttm_resource?

Well the ttm_range_manager is just another component implementing some functionality by extending the ttm_resource object.

We currently don't have a destroy function for ttm_resource object because that isn't necessary at the moment. But I'm probably going to add one at some point.

Regards,
Christian.

Quoted 6 lines

Thanks,

Thomas

#8 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Hi!

Quoted 145 lines · Christian König

Am 02.06.21 um 16:13 schrieb Thomas Hellström (Intel):

Quoted 144 lines · Christian König
Quoted 142 lines

Am 02.06.21 um 14:33 schrieb Thomas Hellström (Intel):

Quoted 139 lines · Christian König
Quoted 137 lines

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 117 lines · Christian König
Quoted 107 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++ include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

Yes, that's the general idea I'm targeting for. I'm just not fully there yet.

Hmm, I don't fully understand the reply, I described a buggy scenario and you replied that's what we're targeting for?

Ok, I don't seem to understand what you mean here. What is buggy on that?

The buggy thing I'm trying to describe is a scenario where I want to have a struct i915_ttm_resource which embeds a struct ttm_range_mgr_node, but there is no way I can tell the generic ttm range manager to allocate a struct i915_ttm_resource instead of a struct ttm_range_mgr_node.

So what I want to be able to do: I have

struct i915_ttm_resource {
        struct i915_gpu_bindings gpu_bindings;
        struct ttm_range_mgr_node range_node;
};

Now I want to be able to share common code as much as possible and use the generic ttm_range_manager here. How would I go about doing that with the proposed changes?

Thanks,

Thomas

Quoted 16 lines
Quoted 2 lines

I assume you mean we're going to get an init() method for the range manager, and a destroy method for the struct ttm_resource?

Well the ttm_range_manager is just another component implementing some functionality by extending the ttm_resource object.

We currently don't have a destroy function for ttm_resource object because that isn't necessary at the moment. But I'm probably going to add one at some point.

Regards,
Christian.

Quoted 6 lines

Thanks,

Thomas

#9 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Am 02.06.21 um 17:28 schrieb Thomas Hellström (Intel):

Quoted 158 lines

Hi!

Quoted 145 lines · Christian König

Am 02.06.21 um 16:13 schrieb Thomas Hellström (Intel):

Quoted 144 lines · Christian König
Quoted 142 lines

Am 02.06.21 um 14:33 schrieb Thomas Hellström (Intel):

Quoted 139 lines · Christian König
Quoted 137 lines

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 117 lines · Christian König
Quoted 107 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43 +++++++++++++++++++ include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

Yes, that's the general idea I'm targeting for. I'm just not fully there yet.

Hmm, I don't fully understand the reply, I described a buggy scenario and you replied that's what we're targeting for?

Ok, I don't seem to understand what you mean here. What is buggy on that?

The buggy thing I'm trying to describe is a scenario where I want to have a struct i915_ttm_resource which embeds a struct ttm_range_mgr_node, but there is no way I can tell the generic ttm range manager to allocate a struct i915_ttm_resource instead of a struct ttm_range_mgr_node.

Quoted 8 lines

So what I want to be able to do: I have

struct i915_ttm_resource {
        struct i915_gpu_bindings gpu_bindings;
        struct ttm_range_mgr_node range_node;
};

Now I want to be able to share common code as much as possible and use the generic ttm_range_manager here. How would I go about doing that with the proposed changes?

Ah, yes that is the part I haven't moved over yet. In other words that is not possible yet.

Christian.

Quoted 30 lines

Thanks,

Thomas

Quoted 16 lines
Quoted 2 lines

I assume you mean we're going to get an init() method for the range manager, and a destroy method for the struct ttm_resource?

Well the ttm_range_manager is just another component implementing some functionality by extending the ttm_resource object.

We currently don't have a destroy function for ttm_resource object because that isn't necessary at the moment. But I'm probably going to add one at some point.

Regards,
Christian.

Quoted 6 lines

Thanks,

Thomas

#10 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Quoted 162 lines · Christian König

Am 02.06.21 um 17:28 schrieb Thomas Hellström (Intel):

Quoted 161 lines

Hi!

Quoted 146 lines · Christian König

Am 02.06.21 um 16:13 schrieb Thomas Hellström (Intel):

Quoted 145 lines · Christian König
Quoted 143 lines

Am 02.06.21 um 14:33 schrieb Thomas Hellström (Intel):

Quoted 140 lines · Christian König
Quoted 138 lines

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 118 lines · Christian König
Quoted 108 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43
+++++++++++++++++++
  include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

Yes, that's the general idea I'm targeting for. I'm just not fully there yet.

Hmm, I don't fully understand the reply, I described a buggy scenario and you replied that's what we're targeting for?

Ok, I don't seem to understand what you mean here. What is buggy on that?

The buggy thing I'm trying to describe is a scenario where I want to have a struct i915_ttm_resource which embeds a struct ttm_range_mgr_node, but there is no way I can tell the generic ttm range manager to allocate a struct i915_ttm_resource instead of a struct ttm_range_mgr_node.

So what I want to be able to do: I have

Quoted 8 lines
Quoted 6 lines

struct i915_ttm_resource {
        struct i915_gpu_bindings gpu_bindings;
        struct ttm_range_mgr_node range_node;
};

Now I want to be able to share common code as much as possible and use the generic ttm_range_manager here. How would I go about doing that with the proposed changes?

Ah, yes that is the part I haven't moved over yet. In other words that is not possible yet.

OK, that "yet" sounds good. So this will be possible moving forward? (Basically it's the overall design that's not completely clear to me yet, not really the code itself)

Thanks,

Thomas

#11 lore raw

Re: [PATCH 02/10] drm/ttm: flip over the range manager to self allocated nodes

Am 02.06.21 um 20:52 schrieb Thomas Hellström (Intel):

Quoted 169 lines · Christian König
Quoted 167 lines

Am 02.06.21 um 17:28 schrieb Thomas Hellström (Intel):

Quoted 166 lines

Hi!

Quoted 146 lines · Christian König

Am 02.06.21 um 16:13 schrieb Thomas Hellström (Intel):

Quoted 145 lines · Christian König
Quoted 143 lines

Am 02.06.21 um 14:33 schrieb Thomas Hellström (Intel):

Quoted 140 lines · Christian König
Quoted 138 lines

Am 02.06.21 um 13:44 schrieb Thomas Hellström (Intel):

Quoted 118 lines · Christian König
Quoted 108 lines

Start with the range manager to make the resource object the base class for the allocated nodes.

While at it cleanup a lot of the code around that.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  1 + drivers/gpu/drm/drm_gem_vram_helper.c   |  2 + drivers/gpu/drm/nouveau/nouveau_ttm.c   |  2 + drivers/gpu/drm/qxl/qxl_ttm.c           |  1 + drivers/gpu/drm/radeon/radeon_ttm.c     |  1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 56
++++++++++++++++++-------
  drivers/gpu/drm/ttm/ttm_resource.c      | 26 ++++++++---- include/drm/ttm/ttm_bo_driver.h         | 26 ------------ include/drm/ttm/ttm_range_manager.h     | 43
+++++++++++++++++++
  include/drm/ttm/ttm_resource.h          |  3 ++

  10 files changed, 111 insertions(+), 50 deletions(-)

  create mode 100644 include/drm/ttm/ttm_range_manager.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 69db89261650..df1f185faae9 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include <drm/amdgpu_drm.h>

  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c index 83e7258c7f90..17a4c5d47b6a 100644

drivers/gpu/drm/drm_gem_vram_helper.c +0 -0
@@ -17,6 +17,8 @@
   #include <drm/drm_prime.h>
   #include <drm/drm_simple_kms_helper.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  static const struct drm_gem_object_funcs drm_gem_vram_object_funcs; /**

drivers/gpu/drm/nouveau/nouveau_ttm.c +0 -0
index 65430912ff72..b08b8efeefba 100644
@@ -26,6 +26,8 @@
   #include <linux/limits.h>
   #include <linux/swiotlb.h>

  +#include <drm/ttm/ttm_range_manager.h>
+
  #include "nouveau_drv.h"
  #include "nouveau_gem.h"
  #include "nouveau_mem.h"

drivers/gpu/drm/qxl/qxl_ttm.c +1 -0
index 8aa87b8edb9c..19fd39d9a00c 100644
@@ -32,6 +32,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "qxl_drv.h"
   #include "qxl_object.h"
drivers/gpu/drm/radeon/radeon_ttm.c +1 -0
index cdffa9b65108..ad2a5a791bba 100644
@@ -45,6 +45,7 @@
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
     #include "radeon_reg.h"
   #include "radeon.h"
drivers/gpu/drm/ttm/ttm_range_manager.c +0 -0
index b9d5da6e6a81..ce5d07ca384c 100644
@@ -29,12 +29,13 @@
    * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
    */

  -#include <drm/ttm/ttm_bo_driver.h>
+#include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_placement.h>

drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
+#include <drm/ttm/ttm_range_manager.h>
+#include <drm/ttm/ttm_bo_api.h>

  #include <drm/drm_mm.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
-#include <linux/module.h>
    /*
   * Currently we use a spinlock for the lock, but a mutex *may* be

drivers/gpu/drm/ttm/ttm_range_manager.c +1 -1
@@ -60,8 +61,8 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
                      struct ttm_resource *mem)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
+    struct ttm_range_mgr_node *node;
       struct drm_mm *mm = &rman->mm;
-    struct drm_mm_node *node;
       enum drm_mm_insert_mode mode;
       unsigned long lpfn;

      int ret;

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct · line 75

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -70,7 +71,7 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
       if (!lpfn)
           lpfn = man->size;
   -    node = kzalloc(sizeof(*node), GFP_KERNEL);
+    node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);

I'm still a bit confused  about the situation where a driver wants to attach private data to a struct ttm_resource without having to re-implement its own range manager?

Quoted 1 line

Could be cached sg-tables, list of GPU bindings etc. Wouldn't work with the above unless we have a void *driver_private member on the struct ttm_resource. Is that the plan going forward here? Or that the driver actually does the re-implementation?

I don't really understand your concern here. The basic idea is that drivers use ttm_resource as a base class for their own implementation.

See for example how nouveau does that:

struct nouveau_mem {
        struct ttm_resource base;
        struct nouveau_cli *cli;
        u8 kind;
        u8 comp;
        struct nvif_mem mem;
        struct nvif_vma vma[2];
};

The range manager is helping driver specific resource managers which want to implement something drm_mm_nodes based. E.g. amdgpu_gtt_mgr and amdgpu_vram_mgr, but it can also be used stand alone.

The ttm_range_mgr_node can then be used as base class for this functionality. I already want to move some more code from amdgpu_vram_mgr.c into the range manager, but that is just minor cleanup work.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Sure but if you embed a ttm_range_mgr_node in your struct i915_resource, and wanted to use the ttm range manager for it, it would allocate a struct ttm_range_mgr_node rather than a struct i915_resource? Or am I missing something?

Yes, that's the general idea I'm targeting for. I'm just not fully there yet.

Hmm, I don't fully understand the reply, I described a buggy scenario and you replied that's what we're targeting for?

Ok, I don't seem to understand what you mean here. What is buggy on that?

The buggy thing I'm trying to describe is a scenario where I want to have a struct i915_ttm_resource which embeds a struct ttm_range_mgr_node, but there is no way I can tell the generic ttm range manager to allocate a struct i915_ttm_resource instead of a struct ttm_range_mgr_node.

So what I want to be able to do: I have

struct i915_ttm_resource {
        struct i915_gpu_bindings gpu_bindings;
        struct ttm_range_mgr_node range_node;
};

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Now I want to be able to share common code as much as possible and use the generic ttm_range_manager here. How would I go about doing that with the proposed changes?

Ah, yes that is the part I haven't moved over yet. In other words that is not possible yet.

OK, that "yet" sounds good. So this will be possible moving forward? (Basically it's the overall design that's not completely clear to me yet, not really the code itself)

Yes, absolutely.

Christian.

Quoted 6 lines

Thanks,

Thomas

#12 lore raw

PATCH 3/10 drm/ttm: flip over the sys manager to self allocated nodes

In reply to Christian König

Make sure to allocate a resource object here.

---
 drivers/gpu/drm/ttm/ttm_sys_manager.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/ttm/ttm_sys_manager.c +7 -0
index 474221e863d0..2b75f493c3c9 100644
@@ -3,6 +3,7 @@
 #include <drm/ttm/ttm_resource.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
+#include <linux/slab.h>
 
 #include "ttm_module.h"
 
@@ -11,12 +12,18 @@ static int ttm_sys_man_alloc(struct ttm_resource_manager *man,
 			     const struct ttm_place *place,
 			     struct ttm_resource *mem)
 {
+	mem->mm_node = kzalloc(sizeof(*mem), GFP_KERNEL);
+	if (!mem->mm_node)
+		return -ENOMEM;
+
+	ttm_resource_init(bo, place, mem->mm_node);
 	return 0;
 }
 
 static void ttm_sys_man_free(struct ttm_resource_manager *man,
 			     struct ttm_resource *mem)
 {
+	kfree(mem->mm_node);
 }
 
 static const struct ttm_resource_manager_func ttm_sys_manager_func = {
Signature · 1 line
2.25.1
#14 lore raw

PATCH 4/10 drm/amdgpu: revert "drm/amdgpu: stop allocating dummy GTT nodes"

In reply to Christian König

TTM is going to need this again since we are moving the resource allocation into the backend.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 68 ++++++++++++---------
 1 file changed, 39 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +39 -29
index fac563fb6f0c..cd5c55cb38d1 100644
@@ -24,16 +24,22 @@
 
 #include "amdgpu.h"
 
+struct amdgpu_gtt_node {
+	struct drm_mm_node node;
+	struct ttm_buffer_object *tbo;
+};
+
 static inline struct amdgpu_gtt_mgr *
 to_gtt_mgr(struct ttm_resource_manager *man)
 {
 	return container_of(man, struct amdgpu_gtt_mgr, manager);
 }
 
-struct amdgpu_gtt_node {
-	struct drm_mm_node node;
-	struct ttm_buffer_object *tbo;
-};
+static inline struct amdgpu_gtt_node *
+to_amdgpu_gtt_node(struct ttm_resource *res)
+{
+	return container_of(res->mm_node, struct amdgpu_gtt_node, node);
+}
 
 /**
  * DOC: mem_info_gtt_total
@@ -99,7 +105,9 @@ const struct attribute_group amdgpu_gtt_mgr_attr_group = {
  */
 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
 {
-	return mem->mm_node != NULL;
+	struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(mem);
+
+	return drm_mm_node_allocated(&node->node);
 }
 
 /**
@@ -130,12 +138,6 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 	atomic64_sub(mem->num_pages, &mgr->available);
 	spin_unlock(&mgr->lock);
 
-	if (!place->lpfn) {
-		mem->mm_node = NULL;
-		mem->start = AMDGPU_BO_INVALID_OFFSET;
-		return 0;
-	}
-
 	node = kzalloc(sizeof(*node), GFP_KERNEL);
 	if (!node) {
 		r = -ENOMEM;
@@ -143,19 +145,25 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 	}
 
 	node->tbo = tbo;
+	if (place->lpfn) {
+		spin_lock(&mgr->lock);
+		r = drm_mm_insert_node_in_range(&mgr->mm, &node->node,
+						mem->num_pages,
+						tbo->page_alignment, 0,
+						place->fpfn, place->lpfn,
+						DRM_MM_INSERT_BEST);
+		spin_unlock(&mgr->lock);
+		if (unlikely(r))
+			goto err_free;
 
-	spin_lock(&mgr->lock);
-	r = drm_mm_insert_node_in_range(&mgr->mm, &node->node, mem->num_pages,
-					tbo->page_alignment, 0, place->fpfn,
-					place->lpfn, DRM_MM_INSERT_BEST);
-	spin_unlock(&mgr->lock);
-
-	if (unlikely(r))
-		goto err_free;
-
-	mem->mm_node = node;
-	mem->start = node->node.start;
+		mem->start = node->node.start;
+	} else {
+		node->node.start = 0;
+		node->node.size = mem->num_pages;
+		mem->start = AMDGPU_BO_INVALID_OFFSET;
+	}
 
+	mem->mm_node = &node->node;
 	return 0;
 
 err_free:
@@ -178,17 +186,19 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
 			       struct ttm_resource *mem)
 {
+	struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(mem);
 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
-	struct amdgpu_gtt_node *node = mem->mm_node;
 
-	if (node) {
-		spin_lock(&mgr->lock);
-		drm_mm_remove_node(&node->node);
-		spin_unlock(&mgr->lock);
-		kfree(node);
-	}
+	if (!node)
+		return;
 
+	spin_lock(&mgr->lock);
+	if (drm_mm_node_allocated(&node->node))
+		drm_mm_remove_node(&node->node);
+	spin_unlock(&mgr->lock);
 	atomic64_add(mem->num_pages, &mgr->available);
+
+	kfree(node);
 }
 
 /**
Signature · 1 line
2.25.1
#15 lore raw

PATCH 5/10 drm/amdkfd: use resource cursor in svm_migrate_copy_to_vram v2

In reply to Christian König

Access to the mm_node is now forbidden. So instead of hand wiring that use the cursor functionality.

v2: fix handling as pointed out by Philip.

---
 drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 68 ++++--------------------
 1 file changed, 10 insertions(+), 58 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +10 -58
index fd8f544f0de2..5ce8fa2ddab0 100644
@@ -29,6 +29,7 @@
 #include "amdgpu_object.h"
 #include "amdgpu_vm.h"
 #include "amdgpu_mn.h"
+#include "amdgpu_res_cursor.h"
 #include "kfd_priv.h"
 #include "kfd_svm.h"
 #include "kfd_migrate.h"
@@ -205,34 +206,6 @@ svm_migrate_copy_done(struct amdgpu_device *adev, struct dma_fence *mfence)
 	return r;
 }
 
-static uint64_t
-svm_migrate_node_physical_addr(struct amdgpu_device *adev,
-			       struct drm_mm_node **mm_node, uint64_t *offset)
-{
-	struct drm_mm_node *node = *mm_node;
-	uint64_t pos = *offset;
-
-	if (node->start == AMDGPU_BO_INVALID_OFFSET) {
-		pr_debug("drm node is not validated\n");
-		return 0;
-	}
-
-	pr_debug("vram node start 0x%llx npages 0x%llx\n", node->start,
-		 node->size);
-
-	if (pos >= node->size) {
-		do  {
-			pos -= node->size;
-			node++;
-		} while (pos >= node->size);
-
-		*mm_node = node;
-		*offset = pos;
-	}
-
-	return (node->start + pos) << PAGE_SHIFT;
-}
-
 unsigned long
 svm_migrate_addr_to_pfn(struct amdgpu_device *adev, unsigned long addr)
 {
@@ -297,11 +270,9 @@ svm_migrate_copy_to_vram(struct amdgpu_device *adev, struct svm_range *prange,
 {
 	uint64_t npages = migrate->cpages;
 	struct device *dev = adev->dev;
-	struct drm_mm_node *node;
+	struct amdgpu_res_cursor cursor;
 	dma_addr_t *src;
 	uint64_t *dst;
-	uint64_t vram_addr;
-	uint64_t offset;
 	uint64_t i, j;
 	int r;
 
@@ -317,19 +288,12 @@ svm_migrate_copy_to_vram(struct amdgpu_device *adev, struct svm_range *prange,
 		goto out;
 	}
 
-	node = prange->ttm_res->mm_node;
-	offset = prange->offset;
-	vram_addr = svm_migrate_node_physical_addr(adev, &node, &offset);
-	if (!vram_addr) {
-		WARN_ONCE(1, "vram node address is 0\n");
-		r = -ENOMEM;
-		goto out;
-	}
-
+	amdgpu_res_first(prange->ttm_res, prange->offset << PAGE_SHIFT,
+			 npages << PAGE_SHIFT, &cursor);
 	for (i = j = 0; i < npages; i++) {
 		struct page *spage;
 
-		dst[i] = vram_addr + (j << PAGE_SHIFT);
+		dst[i] = cursor.start + (j << PAGE_SHIFT);
 		migrate->dst[i] = svm_migrate_addr_to_pfn(adev, dst[i]);
 		svm_migrate_get_vram_page(prange, migrate->dst[i]);
 
@@ -354,18 +318,10 @@ svm_migrate_copy_to_vram(struct amdgpu_device *adev, struct svm_range *prange,
 						mfence);
 				if (r)
 					goto out_free_vram_pages;
-				offset += j;
-				vram_addr = (node->start + offset) << PAGE_SHIFT;
+				amdgpu_res_next(&cursor, j << PAGE_SHIFT);
 				j = 0;
 			} else {
-				offset++;
-				vram_addr += PAGE_SIZE;
-			}
-			if (offset >= node->size) {
-				node++;
-				pr_debug("next node size 0x%llx\n", node->size);
-				vram_addr = node->start << PAGE_SHIFT;
-				offset = 0;
+				amdgpu_res_next(&cursor, PAGE_SIZE);
 			}
 			continue;
 		}
@@ -373,19 +329,15 @@ svm_migrate_copy_to_vram(struct amdgpu_device *adev, struct svm_range *prange,
 		pr_debug("dma mapping src to 0x%llx, page_to_pfn 0x%lx\n",
 			 src[i] >> PAGE_SHIFT, page_to_pfn(spage));
 
-		if (j + offset >= node->size - 1 && i < npages - 1) {
+		if (j << PAGE_SHIFT >= cursor.size - 1 && i < npages - 1) {
 			r = svm_migrate_copy_memory_gart(adev, src + i - j,
 							 dst + i - j, j + 1,
 							 FROM_RAM_TO_VRAM,
 							 mfence);
 			if (r)
 				goto out_free_vram_pages;
-
-			node++;
-			pr_debug("next node size 0x%llx\n", node->size);
-			vram_addr = node->start << PAGE_SHIFT;
-			offset = 0;
-			j = 0;
+			amdgpu_res_next(&cursor, (j + 1) * PAGE_SIZE);
+			j= 0;
 		} else {
 			j++;
 		}
Signature · 1 line
2.25.1
#17 lore raw

PATCH 6/10 drm/amdgpu: switch the GTT backend to self alloc

In reply to Christian König

Similar to the TTM range manager.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 36 +++++++++++++--------
 1 file changed, 22 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +22 -14
index cd5c55cb38d1..29113f72bc39 100644
@@ -22,11 +22,13 @@
  * Authors: Christian König
  */
 
+#include <drm/ttm/ttm_range_manager.h>
+
 #include "amdgpu.h"
 
 struct amdgpu_gtt_node {
-	struct drm_mm_node node;
 	struct ttm_buffer_object *tbo;
+	struct ttm_range_mgr_node base;
 };
 
 static inline struct amdgpu_gtt_mgr *
@@ -38,7 +40,8 @@ to_gtt_mgr(struct ttm_resource_manager *man)
 static inline struct amdgpu_gtt_node *
 to_amdgpu_gtt_node(struct ttm_resource *res)
 {
-	return container_of(res->mm_node, struct amdgpu_gtt_node, node);
+	return container_of(res->mm_node, struct amdgpu_gtt_node,
+			    base.mm_nodes[0]);
 }
 
 /**
@@ -107,7 +110,7 @@ bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
 {
 	struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(mem);
 
-	return drm_mm_node_allocated(&node->node);
+	return drm_mm_node_allocated(&node->base.mm_nodes[0]);
 }
 
 /**
@@ -138,16 +141,19 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 	atomic64_sub(mem->num_pages, &mgr->available);
 	spin_unlock(&mgr->lock);
 
-	node = kzalloc(sizeof(*node), GFP_KERNEL);
+	node = kzalloc(struct_size(node, base.mm_nodes, 1), GFP_KERNEL);
 	if (!node) {
 		r = -ENOMEM;
 		goto err_out;
 	}
 
 	node->tbo = tbo;
+	ttm_resource_init(tbo, place, &node->base.base);
+
 	if (place->lpfn) {
 		spin_lock(&mgr->lock);
-		r = drm_mm_insert_node_in_range(&mgr->mm, &node->node,
+		r = drm_mm_insert_node_in_range(&mgr->mm,
+						&node->base.mm_nodes[0],
 						mem->num_pages,
 						tbo->page_alignment, 0,
 						place->fpfn, place->lpfn,
@@ -156,14 +162,14 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 		if (unlikely(r))
 			goto err_free;
 
-		mem->start = node->node.start;
+		mem->start = node->base.mm_nodes[0].start;
 	} else {
-		node->node.start = 0;
-		node->node.size = mem->num_pages;
+		node->base.mm_nodes[0].start = 0;
+		node->base.mm_nodes[0].size = mem->num_pages;
 		mem->start = AMDGPU_BO_INVALID_OFFSET;
 	}
 
-	mem->mm_node = &node->node;
+	mem->mm_node = &node->base.mm_nodes[0];
 	return 0;
 
 err_free:
@@ -186,15 +192,17 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
 			       struct ttm_resource *mem)
 {
-	struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(mem);
 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
+	struct amdgpu_gtt_node *node;
 
-	if (!node)
+	if (!mem->mm_node)
 		return;
 
+	node = to_amdgpu_gtt_node(mem);
+
 	spin_lock(&mgr->lock);
-	if (drm_mm_node_allocated(&node->node))
-		drm_mm_remove_node(&node->node);
+	if (drm_mm_node_allocated(&node->base.mm_nodes[0]))
+		drm_mm_remove_node(&node->base.mm_nodes[0]);
 	spin_unlock(&mgr->lock);
 	atomic64_add(mem->num_pages, &mgr->available);
 
@@ -232,7 +240,7 @@ int amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man)
 
 	spin_lock(&mgr->lock);
 	drm_mm_for_each_node(mm_node, &mgr->mm) {
-		node = container_of(mm_node, struct amdgpu_gtt_node, node);
+		node = container_of(mm_node, typeof(*node), base.mm_nodes[0]);
 		r = amdgpu_ttm_recover_gart(node->tbo);
 		if (r)
 			break;
Signature · 1 line
2.25.1
#18 lore raw

PATCH 7/10 drm/amdgpu: switch the VRAM backend to self alloc

In reply to Christian König

Similar to the TTM range manager.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 51 ++++++++++++--------
 1 file changed, 30 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +30 -21
index 525cabe6c47a..5ebfaed37e47 100644
@@ -23,6 +23,8 @@
  */
 
 #include <linux/dma-mapping.h>
+#include <drm/ttm/ttm_range_manager.h>
+
 #include "amdgpu.h"
 #include "amdgpu_vm.h"
 #include "amdgpu_res_cursor.h"
@@ -371,9 +373,9 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
 	struct amdgpu_device *adev = to_amdgpu_device(mgr);
 	uint64_t vis_usage = 0, mem_bytes, max_bytes;
+	struct ttm_range_mgr_node *node;
 	struct drm_mm *mm = &mgr->mm;
 	enum drm_mm_insert_mode mode;
-	struct drm_mm_node *nodes;
 	unsigned i;
 	int r;
 
@@ -388,8 +390,8 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 	/* bail out quickly if there's likely not enough VRAM for this BO */
 	mem_bytes = (u64)mem->num_pages << PAGE_SHIFT;
 	if (atomic64_add_return(mem_bytes, &mgr->usage) > max_bytes) {
-		atomic64_sub(mem_bytes, &mgr->usage);
-		return -ENOSPC;
+		r = -ENOSPC;
+		goto error_sub;
 	}
 
 	if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
@@ -407,13 +409,15 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 		num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
 	}
 
-	nodes = kvmalloc_array((uint32_t)num_nodes, sizeof(*nodes),
-			       GFP_KERNEL | __GFP_ZERO);
-	if (!nodes) {
-		atomic64_sub(mem_bytes, &mgr->usage);
-		return -ENOMEM;
+	node = kvmalloc(struct_size(node, mm_nodes, num_nodes),
+			GFP_KERNEL | __GFP_ZERO);
+	if (!node) {
+		r = -ENOMEM;
+		goto error_sub;
 	}
 
+	ttm_resource_init(tbo, place, &node->base);
+
 	mode = DRM_MM_INSERT_BEST;
 	if (place->flags & TTM_PL_FLAG_TOPDOWN)
 		mode = DRM_MM_INSERT_HIGH;
@@ -432,8 +436,9 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 		if (pages >= pages_per_node)
 			alignment = pages_per_node;
 
-		r = drm_mm_insert_node_in_range(mm, &nodes[i], pages, alignment,
-						0, place->fpfn, lpfn, mode);
+		r = drm_mm_insert_node_in_range(mm, &node->mm_nodes[i], pages,
+						alignment, 0, place->fpfn,
+						lpfn, mode);
 		if (unlikely(r)) {
 			if (pages > pages_per_node) {
 				if (is_power_of_2(pages))
@@ -442,11 +447,11 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 					pages = rounddown_pow_of_two(pages);
 				continue;
 			}
-			goto error;
+			goto error_free;
 		}
 
-		vis_usage += amdgpu_vram_mgr_vis_size(adev, &nodes[i]);
-		amdgpu_vram_mgr_virt_start(mem, &nodes[i]);
+		vis_usage += amdgpu_vram_mgr_vis_size(adev, &node->mm_nodes[i]);
+		amdgpu_vram_mgr_virt_start(mem, &node->mm_nodes[i]);
 		pages_left -= pages;
 		++i;
 
@@ -459,16 +464,17 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 		mem->placement |= TTM_PL_FLAG_CONTIGUOUS;
 
 	atomic64_add(vis_usage, &mgr->vis_usage);
-	mem->mm_node = nodes;
+	mem->mm_node = &node->mm_nodes[0];
 	return 0;
 
-error:
+error_free:
 	while (i--)
-		drm_mm_remove_node(&nodes[i]);
+		drm_mm_remove_node(&node->mm_nodes[i]);
 	spin_unlock(&mgr->lock);
-	atomic64_sub(mem->num_pages << PAGE_SHIFT, &mgr->usage);
+	kvfree(node);
 
-	kvfree(nodes);
+error_sub:
+	atomic64_sub(mem_bytes, &mgr->usage);
 	return r;
 }
 
@@ -485,13 +491,17 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
 {
 	struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
 	struct amdgpu_device *adev = to_amdgpu_device(mgr);
-	struct drm_mm_node *nodes = mem->mm_node;
+	struct ttm_range_mgr_node *node;
 	uint64_t usage = 0, vis_usage = 0;
 	unsigned pages = mem->num_pages;
+	struct drm_mm_node *nodes;
 
 	if (!mem->mm_node)
 		return;
 
+	node = to_ttm_range_mgr_node(mem);
+	nodes = &node->mm_nodes[0];
+
 	spin_lock(&mgr->lock);
 	while (pages) {
 		pages -= nodes->size;
@@ -506,8 +516,7 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
 	atomic64_sub(usage, &mgr->usage);
 	atomic64_sub(vis_usage, &mgr->vis_usage);
 
-	kvfree(mem->mm_node);
-	mem->mm_node = NULL;
+	kvfree(node);
 }
 
 /**
Signature · 1 line
2.25.1
#19 lore raw

PATCH 8/10 drm/nouveau: switch the TTM backends to self alloc

In reply to Christian König

Similar to the TTM range manager.

---
 drivers/gpu/drm/nouveau/nouveau_mem.h | 1 +
 drivers/gpu/drm/nouveau/nouveau_ttm.c | 4 ++++
 2 files changed, 5 insertions(+)
drivers/gpu/drm/nouveau/nouveau_mem.h +1 -0
index 7df3848e85aa..3a6a1be2ed52 100644
@@ -13,6 +13,7 @@ nouveau_mem(struct ttm_resource *reg)
 }
 
 struct nouveau_mem {
+	struct ttm_resource base;
 	struct nouveau_cli *cli;
 	u8 kind;
 	u8 comp;
drivers/gpu/drm/nouveau/nouveau_ttm.c +4 -0
index b08b8efeefba..1ac2417effc0 100644
@@ -58,6 +58,8 @@ nouveau_vram_manager_new(struct ttm_resource_manager *man,
 	if (ret)
 		return ret;
 
+	ttm_resource_init(bo, place, reg->mm_node);
+
 	ret = nouveau_mem_vram(reg, nvbo->contig, nvbo->page);
 	if (ret) {
 		nouveau_mem_del(reg);
@@ -86,6 +88,7 @@ nouveau_gart_manager_new(struct ttm_resource_manager *man,
 	if (ret)
 		return ret;
 
+	ttm_resource_init(bo, place, reg->mm_node);
 	reg->start = 0;
 	return 0;
 }
@@ -111,6 +114,7 @@ nv04_gart_manager_new(struct ttm_resource_manager *man,
 	if (ret)
 		return ret;
 
+	ttm_resource_init(bo, place, reg->mm_node);
 	ret = nvif_vmm_get(&mem->cli->vmm.vmm, PTES, false, 12, 0,
 			   (long)reg->num_pages << PAGE_SHIFT, &mem->vma[0]);
 	if (ret) {
Signature · 1 line
2.25.1
#20 lore raw

PATCH 9/10 drm/vmwgfx: switch the TTM backends to self alloc

In reply to Christian König

Similar to the TTM range manager.

---
 drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c | 18 +++++----
 drivers/gpu/drm/vmwgfx/vmwgfx_thp.c           | 37 ++++++++++---------
 2 files changed, 31 insertions(+), 24 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c +11 -7
index 1774960d1b89..82a5e6489810 100644
@@ -57,6 +57,12 @@ static int vmw_gmrid_man_get_node(struct ttm_resource_manager *man,
 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
 	int id;
 
+	mem->mm_node = kmalloc(sizeof(*mem), GFP_KERNEL);
+	if (!mem->mm_node)
+		return -ENOMEM;
+
+	ttm_resource_init(bo, place, mem->mm_node);
+
 	id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL);
 	if (id < 0)
 		return id;
@@ -87,13 +93,11 @@ static void vmw_gmrid_man_put_node(struct ttm_resource_manager *man,
 {
 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
 
-	if (mem->mm_node) {
-		ida_free(&gman->gmr_ida, mem->start);
-		spin_lock(&gman->lock);
-		gman->used_gmr_pages -= mem->num_pages;
-		spin_unlock(&gman->lock);
-		mem->mm_node = NULL;
-	}
+	ida_free(&gman->gmr_ida, mem->start);
+	spin_lock(&gman->lock);
+	gman->used_gmr_pages -= mem->num_pages;
+	spin_unlock(&gman->lock);
+	kfree(mem->mm_node);
 }
 
 static const struct ttm_resource_manager_func vmw_gmrid_manager_func;
drivers/gpu/drm/vmwgfx/vmwgfx_thp.c +20 -17
index 5ccc35b3194c..8765835696ac 100644
@@ -7,6 +7,7 @@
 #include "vmwgfx_drv.h"
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 /**
  * struct vmw_thp_manager - Range manager implementing huge page alignment
@@ -54,16 +55,18 @@ static int vmw_thp_get_node(struct ttm_resource_manager *man,
 {
 	struct vmw_thp_manager *rman = to_thp_manager(man);
 	struct drm_mm *mm = &rman->mm;
-	struct drm_mm_node *node;
+	struct ttm_range_mgr_node *node;
 	unsigned long align_pages;
 	unsigned long lpfn;
 	enum drm_mm_insert_mode mode = DRM_MM_INSERT_BEST;
 	int ret;
 
-	node = kzalloc(sizeof(*node), GFP_KERNEL);
+	node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);
 	if (!node)
 		return -ENOMEM;
 
+	ttm_resource_init(bo, place, &node->base);
+
 	lpfn = place->lpfn;
 	if (!lpfn)
 		lpfn = man->size;
@@ -76,8 +79,9 @@ static int vmw_thp_get_node(struct ttm_resource_manager *man,
 	if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)) {
 		align_pages = (HPAGE_PUD_SIZE >> PAGE_SHIFT);
 		if (mem->num_pages >= align_pages) {
-			ret = vmw_thp_insert_aligned(bo, mm, node, align_pages,
-						     place, mem, lpfn, mode);
+			ret = vmw_thp_insert_aligned(bo, mm, &node->mm_nodes[0],
+						     align_pages, place, mem,
+						     lpfn, mode);
 			if (!ret)
 				goto found_unlock;
 		}
@@ -85,14 +89,15 @@ static int vmw_thp_get_node(struct ttm_resource_manager *man,
 
 	align_pages = (HPAGE_PMD_SIZE >> PAGE_SHIFT);
 	if (mem->num_pages >= align_pages) {
-		ret = vmw_thp_insert_aligned(bo, mm, node, align_pages, place,
-					     mem, lpfn, mode);
+		ret = vmw_thp_insert_aligned(bo, mm, &node->mm_nodes[0],
+					     align_pages, place, mem, lpfn,
+					     mode);
 		if (!ret)
 			goto found_unlock;
 	}
 
-	ret = drm_mm_insert_node_in_range(mm, node, mem->num_pages,
-					  bo->page_alignment, 0,
+	ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
+					  mem->num_pages, bo->page_alignment, 0,
 					  place->fpfn, lpfn, mode);
 found_unlock:
 	spin_unlock(&rman->lock);
@@ -100,8 +105,8 @@ static int vmw_thp_get_node(struct ttm_resource_manager *man,
 	if (unlikely(ret)) {
 		kfree(node);
 	} else {
-		mem->mm_node = node;
-		mem->start = node->start;
+		mem->mm_node = &node->mm_nodes[0];
+		mem->start = node->mm_nodes[0].start;
 	}
 
 	return ret;
@@ -113,15 +118,13 @@ static void vmw_thp_put_node(struct ttm_resource_manager *man,
 			     struct ttm_resource *mem)
 {
 	struct vmw_thp_manager *rman = to_thp_manager(man);
+	struct ttm_range_mgr_node * node = mem->mm_node;
 
-	if (mem->mm_node) {
-		spin_lock(&rman->lock);
-		drm_mm_remove_node(mem->mm_node);
-		spin_unlock(&rman->lock);
+	spin_lock(&rman->lock);
+	drm_mm_remove_node(&node->mm_nodes[0]);
+	spin_unlock(&rman->lock);
 
-		kfree(mem->mm_node);
-		mem->mm_node = NULL;
-	}
+	kfree(node);
 }
 
 int vmw_thp_init(struct vmw_private *dev_priv)
Signature · 1 line
2.25.1
#21 lore raw

PATCH 10/10 drm/ttm: flip the switch for driver allocated resources v2

In reply to Christian König

Instead of both driver and TTM allocating memory finalize embedding the ttm_resource object as base into the driver backends.

v2: fix typo in vmwgfx grid mgr and double init in amdgpu_vram_mgr.c

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c   | 44 ++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  | 60 +++++++++----------
 drivers/gpu/drm/drm_gem_vram_helper.c         |  3 +-
 drivers/gpu/drm/nouveau/nouveau_bo.c          |  8 +--
 drivers/gpu/drm/nouveau/nouveau_mem.c         | 11 ++--
 drivers/gpu/drm/nouveau/nouveau_mem.h         | 14 ++---
 drivers/gpu/drm/nouveau/nouveau_ttm.c         | 32 +++++-----
 drivers/gpu/drm/ttm/ttm_range_manager.c       | 23 +++----
 drivers/gpu/drm/ttm/ttm_resource.c            | 18 +-----
 drivers/gpu/drm/ttm/ttm_sys_manager.c         | 12 ++--
 drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c | 24 ++++----
 drivers/gpu/drm/vmwgfx/vmwgfx_thp.c           | 27 ++++-----
 include/drm/ttm/ttm_range_manager.h           |  3 +-
 include/drm/ttm/ttm_resource.h                | 43 ++++++-------
 16 files changed, 140 insertions(+), 189 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +19 -25
index 29113f72bc39..194f9eecf89c 100644
@@ -40,8 +40,7 @@ to_gtt_mgr(struct ttm_resource_manager *man)
 static inline struct amdgpu_gtt_node *
 to_amdgpu_gtt_node(struct ttm_resource *res)
 {
-	return container_of(res->mm_node, struct amdgpu_gtt_node,
-			    base.mm_nodes[0]);
+	return container_of(res, struct amdgpu_gtt_node, base.base);
 }
 
 /**
@@ -102,13 +101,13 @@ const struct attribute_group amdgpu_gtt_mgr_attr_group = {
 /**
  * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
  *
- * @mem: the mem object to check
+ * @res: the mem object to check
  *
  * Check if a mem object has already address space allocated.
  */
-bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
+bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *res)
 {
-	struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(mem);
+	struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(res);
 
 	return drm_mm_node_allocated(&node->base.mm_nodes[0]);
 }
@@ -126,19 +125,20 @@ bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 			      struct ttm_buffer_object *tbo,
 			      const struct ttm_place *place,
-			      struct ttm_resource *mem)
+			      struct ttm_resource **res)
 {
 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
+	uint32_t num_pages = PFN_UP(tbo->base.size);
 	struct amdgpu_gtt_node *node;
 	int r;
 
 	spin_lock(&mgr->lock);
-	if ((tbo->resource == mem || tbo->resource->mem_type != TTM_PL_TT) &&
-	    atomic64_read(&mgr->available) < mem->num_pages) {
+	if (tbo->resource && tbo->resource->mem_type != TTM_PL_TT &&
+	    atomic64_read(&mgr->available) < num_pages) {
 		spin_unlock(&mgr->lock);
 		return -ENOSPC;
 	}
-	atomic64_sub(mem->num_pages, &mgr->available);
+	atomic64_sub(num_pages, &mgr->available);
 	spin_unlock(&mgr->lock);
 
 	node = kzalloc(struct_size(node, base.mm_nodes, 1), GFP_KERNEL);
@@ -154,29 +154,28 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 		spin_lock(&mgr->lock);
 		r = drm_mm_insert_node_in_range(&mgr->mm,
 						&node->base.mm_nodes[0],
-						mem->num_pages,
-						tbo->page_alignment, 0,
-						place->fpfn, place->lpfn,
+						num_pages, tbo->page_alignment,
+						0, place->fpfn, place->lpfn,
 						DRM_MM_INSERT_BEST);
 		spin_unlock(&mgr->lock);
 		if (unlikely(r))
 			goto err_free;
 
-		mem->start = node->base.mm_nodes[0].start;
+		node->base.base.start = node->base.mm_nodes[0].start;
 	} else {
 		node->base.mm_nodes[0].start = 0;
-		node->base.mm_nodes[0].size = mem->num_pages;
-		mem->start = AMDGPU_BO_INVALID_OFFSET;
+		node->base.mm_nodes[0].size = node->base.base.num_pages;
+		node->base.base.start = AMDGPU_BO_INVALID_OFFSET;
 	}
 
-	mem->mm_node = &node->base.mm_nodes[0];
+	*res = &node->base.base;
 	return 0;
 
 err_free:
 	kfree(node);
 
 err_out:
-	atomic64_add(mem->num_pages, &mgr->available);
+	atomic64_add(num_pages, &mgr->available);
 
 	return r;
 }
@@ -190,21 +189,16 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
  * Free the allocated GTT again.
  */
 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
-			       struct ttm_resource *mem)
+			       struct ttm_resource *res)
 {
+	struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(res);
 	struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
-	struct amdgpu_gtt_node *node;
-
-	if (!mem->mm_node)
-		return;
-
-	node = to_amdgpu_gtt_node(mem);
 
 	spin_lock(&mgr->lock);
 	if (drm_mm_node_allocated(&node->base.mm_nodes[0]))
 		drm_mm_remove_node(&node->base.mm_nodes[0]);
 	spin_unlock(&mgr->lock);
-	atomic64_add(mem->num_pages, &mgr->available);
+	atomic64_add(res->num_pages, &mgr->available);
 
 	kfree(node);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -1
index 59723c3d5826..19c1384a133f 100644
@@ -1296,7 +1296,7 @@ void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
 	if (bo->base.resv == &bo->base._resv)
 		amdgpu_amdkfd_remove_fence_on_pt_pd_bos(abo);
 
-	if (bo->resource->mem_type != TTM_PL_VRAM || !bo->resource->mm_node ||
+	if (bo->resource->mem_type != TTM_PL_VRAM ||
 	    !(abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE))
 		return;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_res_cursor.h +3 -2
index 40f2adf305bc..59e0fefb15aa 100644
@@ -28,6 +28,7 @@
 
 #include <drm/drm_mm.h>
 #include <drm/ttm/ttm_resource.h>
+#include <drm/ttm/ttm_range_manager.h>
 
 /* state back for walking over vram_mgr and gtt_mgr allocations */
 struct amdgpu_res_cursor {
@@ -53,7 +54,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 {
 	struct drm_mm_node *node;
 
-	if (!res || !res->mm_node) {
+	if (!res) {
 		cur->start = start;
 		cur->size = size;
 		cur->remaining = size;
@@ -63,7 +64,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
 
 	BUG_ON(start + size > res->num_pages << PAGE_SHIFT);
 
-	node = res->mm_node;
+	node = to_ttm_range_mgr_node(res)->mm_nodes;
 	while (start >= node->size << PAGE_SHIFT)
 		start -= node++->size << PAGE_SHIFT;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +27 -33

141 lines not on this page · raw message

drivers/gpu/drm/drm_gem_vram_helper.c +2 -1
index 17a4c5d47b6a..2a1229b8364e 100644
@@ -250,7 +250,8 @@ EXPORT_SYMBOL(drm_gem_vram_put);
 static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
 {
 	/* Keep TTM behavior for now, remove when drivers are audited */
-	if (WARN_ON_ONCE(!gbo->bo.resource->mm_node))
+	if (WARN_ON_ONCE(!gbo->bo.resource ||
+			 gbo->bo.resource->mem_type == TTM_PL_SYSTEM))
 		return 0;
 
 	return gbo->bo.resource->start;
drivers/gpu/drm/nouveau/nouveau_bo.c +2 -6
index 3a0d9b3bf991..c3d20bc80022 100644
@@ -918,12 +918,8 @@ static void nouveau_bo_move_ntfy(struct ttm_buffer_object *bo,
 		}
 	}
 
-	if (new_reg) {
-		if (new_reg->mm_node)
-			nvbo->offset = (new_reg->start << PAGE_SHIFT);
-		else
-			nvbo->offset = 0;
-	}
+	if (new_reg)
+		nvbo->offset = (new_reg->start << PAGE_SHIFT);
 
 }
 
drivers/gpu/drm/nouveau/nouveau_mem.c +5 -6
index a1049e9feee1..0de6549fb875 100644
@@ -178,25 +178,24 @@ void
 nouveau_mem_del(struct ttm_resource *reg)
 {
 	struct nouveau_mem *mem = nouveau_mem(reg);
-	if (!mem)
-		return;
+
 	nouveau_mem_fini(mem);
-	kfree(reg->mm_node);
-	reg->mm_node = NULL;
+	kfree(mem);
 }
 
 int
 nouveau_mem_new(struct nouveau_cli *cli, u8 kind, u8 comp,
-		struct ttm_resource *reg)
+		struct ttm_resource **res)
 {
 	struct nouveau_mem *mem;
 
 	if (!(mem = kzalloc(sizeof(*mem), GFP_KERNEL)))
 		return -ENOMEM;
+
 	mem->cli = cli;
 	mem->kind = kind;
 	mem->comp = comp;
 
-	reg->mm_node = mem;
+	*res = &mem->base;
 	return 0;
 }
drivers/gpu/drm/nouveau/nouveau_mem.h +7 -7
index 3a6a1be2ed52..2c01166a90f2 100644
@@ -6,12 +6,6 @@ struct ttm_tt;
 #include <nvif/mem.h>
 #include <nvif/vmm.h>
 
-static inline struct nouveau_mem *
-nouveau_mem(struct ttm_resource *reg)
-{
-	return reg->mm_node;
-}
-
 struct nouveau_mem {
 	struct ttm_resource base;
 	struct nouveau_cli *cli;
@@ -21,8 +15,14 @@ struct nouveau_mem {
 	struct nvif_vma vma[2];
 };
 
+static inline struct nouveau_mem *
+nouveau_mem(struct ttm_resource *reg)
+{
+	return container_of(reg, struct nouveau_mem, base);
+}
+
 int nouveau_mem_new(struct nouveau_cli *, u8 kind, u8 comp,
-		    struct ttm_resource *);
+		    struct ttm_resource **);
 void nouveau_mem_del(struct ttm_resource *);
 int nouveau_mem_vram(struct ttm_resource *, bool contig, u8 page);
 int nouveau_mem_host(struct ttm_resource *, struct ttm_tt *);
drivers/gpu/drm/nouveau/nouveau_ttm.c +16 -16
index 1ac2417effc0..f4c2e46b6fe1 100644
@@ -45,7 +45,7 @@ static int
 nouveau_vram_manager_new(struct ttm_resource_manager *man,
 			 struct ttm_buffer_object *bo,
 			 const struct ttm_place *place,
-			 struct ttm_resource *reg)
+			 struct ttm_resource **res)
 {
 	struct nouveau_bo *nvbo = nouveau_bo(bo);
 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
@@ -54,15 +54,15 @@ nouveau_vram_manager_new(struct ttm_resource_manager *man,
 	if (drm->client.device.info.ram_size == 0)
 		return -ENOMEM;
 
-	ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
+	ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
 	if (ret)
 		return ret;
 
-	ttm_resource_init(bo, place, reg->mm_node);
+	ttm_resource_init(bo, place, *res);
 
-	ret = nouveau_mem_vram(reg, nvbo->contig, nvbo->page);
+	ret = nouveau_mem_vram(*res, nvbo->contig, nvbo->page);
 	if (ret) {
-		nouveau_mem_del(reg);
+		nouveau_mem_del(*res);
 		return ret;
 	}
 
@@ -78,18 +78,18 @@ static int
 nouveau_gart_manager_new(struct ttm_resource_manager *man,
 			 struct ttm_buffer_object *bo,
 			 const struct ttm_place *place,
-			 struct ttm_resource *reg)
+			 struct ttm_resource **res)
 {
 	struct nouveau_bo *nvbo = nouveau_bo(bo);
 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
 	int ret;
 
-	ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
+	ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
 	if (ret)
 		return ret;
 
-	ttm_resource_init(bo, place, reg->mm_node);
-	reg->start = 0;
+	ttm_resource_init(bo, place, *res);
+	(*res)->start = 0;
 	return 0;
 }
 
@@ -102,27 +102,27 @@ static int
 nv04_gart_manager_new(struct ttm_resource_manager *man,
 		      struct ttm_buffer_object *bo,
 		      const struct ttm_place *place,
-		      struct ttm_resource *reg)
+		      struct ttm_resource **res)
 {
 	struct nouveau_bo *nvbo = nouveau_bo(bo);
 	struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
 	struct nouveau_mem *mem;
 	int ret;
 
-	ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
-	mem = nouveau_mem(reg);
+	ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, res);
 	if (ret)
 		return ret;
 
-	ttm_resource_init(bo, place, reg->mm_node);
+	mem = nouveau_mem(*res);
+	ttm_resource_init(bo, place, *res);
 	ret = nvif_vmm_get(&mem->cli->vmm.vmm, PTES, false, 12, 0,
-			   (long)reg->num_pages << PAGE_SHIFT, &mem->vma[0]);
+			   (long)(*res)->num_pages << PAGE_SHIFT, &mem->vma[0]);
 	if (ret) {
-		nouveau_mem_del(reg);
+		nouveau_mem_del(*res);
 		return ret;
 	}
 
-	reg->start = mem->vma[0].addr >> PAGE_SHIFT;
+	(*res)->start = mem->vma[0].addr >> PAGE_SHIFT;
 	return 0;
 }
 
drivers/gpu/drm/ttm/ttm_range_manager.c +8 -15
index ce5d07ca384c..c32e1aee2481 100644
@@ -58,7 +58,7 @@ to_range_manager(struct ttm_resource_manager *man)
 static int ttm_range_man_alloc(struct ttm_resource_manager *man,
 			       struct ttm_buffer_object *bo,
 			       const struct ttm_place *place,
-			       struct ttm_resource *mem)
+			       struct ttm_resource **res)
 {
 	struct ttm_range_manager *rman = to_range_manager(man);
 	struct ttm_range_mgr_node *node;
@@ -83,37 +83,30 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
 
 	spin_lock(&rman->lock);
 	ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
-					  mem->num_pages, bo->page_alignment, 0,
+					  node->base.num_pages,
+					  bo->page_alignment, 0,
 					  place->fpfn, lpfn, mode);
 	spin_unlock(&rman->lock);
 
-	if (unlikely(ret)) {
+	if (unlikely(ret))
 		kfree(node);
-	} else {
-		mem->mm_node = &node->mm_nodes[0];
-		mem->start = node->mm_nodes[0].start;
-	}
+	else
+		node->base.start = node->mm_nodes[0].start;
 
 	return ret;
 }
 
 static void ttm_range_man_free(struct ttm_resource_manager *man,
-			       struct ttm_resource *mem)
+			       struct ttm_resource *res)
 {
+	struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res);
 	struct ttm_range_manager *rman = to_range_manager(man);
-	struct ttm_range_mgr_node *node;
-
-	if (!mem->mm_node)
-		return;
-
-	node = to_ttm_range_mgr_node(mem);
 
 	spin_lock(&rman->lock);
 	drm_mm_remove_node(&node->mm_nodes[0]);
 	spin_unlock(&rman->lock);
 
 	kfree(node);
-	mem->mm_node = NULL;
 }
 
 static void ttm_range_man_debug(struct ttm_resource_manager *man,
drivers/gpu/drm/ttm/ttm_resource.c +1 -17
index 2a51ace17614..2a68145572cc 100644
@@ -29,7 +29,6 @@ void ttm_resource_init(struct ttm_buffer_object *bo,
                        const struct ttm_place *place,
                        struct ttm_resource *res)
 {
-	res->mm_node = NULL;
 	res->start = 0;
 	res->num_pages = PFN_UP(bo->base.size);
 	res->mem_type = place->mem_type;
@@ -47,22 +46,8 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
 {
 	struct ttm_resource_manager *man =
 		ttm_manager_type(bo->bdev, place->mem_type);
-	struct ttm_resource *res;
-	int r;
-
-	res = kmalloc(sizeof(*res), GFP_KERNEL);
-	if (!res)
-		return -ENOMEM;
-
-	ttm_resource_init(bo, place, res);
-	r = man->func->alloc(man, bo, place, res);
-	if (r) {
-		kfree(res);
-		return r;
-	}
 
-	*res_ptr = res;
-	return 0;
+	return man->func->alloc(man, bo, place, res_ptr);
 }
 
 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
@@ -74,7 +59,6 @@ void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
 
 	man = ttm_manager_type(bo->bdev, (*res)->mem_type);
 	man->func->free(man, *res);
-	kfree(*res);
 	*res = NULL;
 }
 EXPORT_SYMBOL(ttm_resource_free);
drivers/gpu/drm/ttm/ttm_sys_manager.c +6 -6
index 2b75f493c3c9..63aca52f75e1 100644
@@ -10,20 +10,20 @@
 static int ttm_sys_man_alloc(struct ttm_resource_manager *man,
 			     struct ttm_buffer_object *bo,
 			     const struct ttm_place *place,
-			     struct ttm_resource *mem)
+			     struct ttm_resource **res)
 {
-	mem->mm_node = kzalloc(sizeof(*mem), GFP_KERNEL);
-	if (!mem->mm_node)
+	*res = kzalloc(sizeof(**res), GFP_KERNEL);
+	if (!*res)
 		return -ENOMEM;
 
-	ttm_resource_init(bo, place, mem->mm_node);
+	ttm_resource_init(bo, place, *res);
 	return 0;
 }
 
 static void ttm_sys_man_free(struct ttm_resource_manager *man,
-			     struct ttm_resource *mem)
+			     struct ttm_resource *res)
 {
-	kfree(mem->mm_node);
+	kfree(res);
 }
 
 static const struct ttm_resource_manager_func ttm_sys_manager_func = {
drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c +12 -12
index 82a5e6489810..28ceb749a733 100644
@@ -52,16 +52,16 @@ static struct vmwgfx_gmrid_man *to_gmrid_manager(struct ttm_resource_manager *ma
 static int vmw_gmrid_man_get_node(struct ttm_resource_manager *man,
 				  struct ttm_buffer_object *bo,
 				  const struct ttm_place *place,
-				  struct ttm_resource *mem)
+				  struct ttm_resource **res)
 {
 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
 	int id;
 
-	mem->mm_node = kmalloc(sizeof(*mem), GFP_KERNEL);
-	if (!mem->mm_node)
+	*res = kmalloc(sizeof(**res), GFP_KERNEL);
+	if (!*res)
 		return -ENOMEM;
 
-	ttm_resource_init(bo, place, mem->mm_node);
+	ttm_resource_init(bo, place, *res);
 
 	id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL);
 	if (id < 0)
@@ -70,34 +70,34 @@ static int vmw_gmrid_man_get_node(struct ttm_resource_manager *man,
 	spin_lock(&gman->lock);
 
 	if (gman->max_gmr_pages > 0) {
-		gman->used_gmr_pages += mem->num_pages;
+		gman->used_gmr_pages += (*res)->num_pages;
 		if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages))
 			goto nospace;
 	}
 
-	mem->mm_node = gman;
-	mem->start = id;
+	(*res)->start = id;
 
 	spin_unlock(&gman->lock);
 	return 0;
 
 nospace:
-	gman->used_gmr_pages -= mem->num_pages;
+	gman->used_gmr_pages -= (*res)->num_pages;
 	spin_unlock(&gman->lock);
 	ida_free(&gman->gmr_ida, id);
+	kfree(*res);
 	return -ENOSPC;
 }
 
 static void vmw_gmrid_man_put_node(struct ttm_resource_manager *man,
-				   struct ttm_resource *mem)
+				   struct ttm_resource *res)
 {
 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
 
-	ida_free(&gman->gmr_ida, mem->start);
+	ida_free(&gman->gmr_ida, res->start);
 	spin_lock(&gman->lock);
-	gman->used_gmr_pages -= mem->num_pages;
+	gman->used_gmr_pages -= res->num_pages;
 	spin_unlock(&gman->lock);
-	kfree(mem->mm_node);
+	kfree(res);
 }
 
 static const struct ttm_resource_manager_func vmw_gmrid_manager_func;
drivers/gpu/drm/vmwgfx/vmwgfx_thp.c +13 -14
index 8765835696ac..2a3d3468e4e0 100644
@@ -51,7 +51,7 @@ static int vmw_thp_insert_aligned(struct ttm_buffer_object *bo,
 static int vmw_thp_get_node(struct ttm_resource_manager *man,
 			    struct ttm_buffer_object *bo,
 			    const struct ttm_place *place,
-			    struct ttm_resource *mem)
+			    struct ttm_resource **res)
 {
 	struct vmw_thp_manager *rman = to_thp_manager(man);
 	struct drm_mm *mm = &rman->mm;
@@ -78,26 +78,27 @@ static int vmw_thp_get_node(struct ttm_resource_manager *man,
 	spin_lock(&rman->lock);
 	if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)) {
 		align_pages = (HPAGE_PUD_SIZE >> PAGE_SHIFT);
-		if (mem->num_pages >= align_pages) {
+		if (node->base.num_pages >= align_pages) {
 			ret = vmw_thp_insert_aligned(bo, mm, &node->mm_nodes[0],
-						     align_pages, place, mem,
-						     lpfn, mode);
+						     align_pages, place,
+						     &node->base, lpfn, mode);
 			if (!ret)
 				goto found_unlock;
 		}
 	}
 
 	align_pages = (HPAGE_PMD_SIZE >> PAGE_SHIFT);
-	if (mem->num_pages >= align_pages) {
+	if (node->base.num_pages >= align_pages) {
 		ret = vmw_thp_insert_aligned(bo, mm, &node->mm_nodes[0],
-					     align_pages, place, mem, lpfn,
-					     mode);
+					     align_pages, place, &node->base,
+					     lpfn, mode);
 		if (!ret)
 			goto found_unlock;
 	}
 
 	ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
-					  mem->num_pages, bo->page_alignment, 0,
+					  node->base.num_pages,
+					  bo->page_alignment, 0,
 					  place->fpfn, lpfn, mode);
 found_unlock:
 	spin_unlock(&rman->lock);
@@ -105,20 +106,18 @@ static int vmw_thp_get_node(struct ttm_resource_manager *man,
 	if (unlikely(ret)) {
 		kfree(node);
 	} else {
-		mem->mm_node = &node->mm_nodes[0];
-		mem->start = node->mm_nodes[0].start;
+		node->base.start = node->mm_nodes[0].start;
+		*res = &node->base;
 	}
 
 	return ret;
 }
 
-
-
 static void vmw_thp_put_node(struct ttm_resource_manager *man,
-			     struct ttm_resource *mem)
+			     struct ttm_resource *res)
 {
+	struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res);
 	struct vmw_thp_manager *rman = to_thp_manager(man);
-	struct ttm_range_mgr_node * node = mem->mm_node;
 
 	spin_lock(&rman->lock);
 	drm_mm_remove_node(&node->mm_nodes[0]);
include/drm/ttm/ttm_range_manager.h +1 -2
index 983f452ce54b..22b6fa42ac20 100644
@@ -30,8 +30,7 @@ struct ttm_range_mgr_node {
 static inline struct ttm_range_mgr_node *
 to_ttm_range_mgr_node(struct ttm_resource *res)
 {
-	return container_of(res->mm_node, struct ttm_range_mgr_node,
-			    mm_nodes[0]);
+	return container_of(res, struct ttm_range_mgr_node, base);
 }
 
 int ttm_range_man_init(struct ttm_device *bdev,
include/drm/ttm/ttm_resource.h +17 -26

79 lines not on this page · raw message

Signature · 1 line
2.25.1
#22 lore raw

Re: [PATCH 10/10] drm/ttm: flip the switch for driver allocated resources v2

Quoted 25 lines · Christian König

Instead of both driver and TTM allocating memory finalize embedding the ttm_resource object as base into the driver backends.

v2: fix typo in vmwgfx grid mgr and double init in amdgpu_vram_mgr.c

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c   | 44 ++++++--------
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  2 +-
  .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  | 60 +++++++++----------
  drivers/gpu/drm/drm_gem_vram_helper.c         |  3 +-
  drivers/gpu/drm/nouveau/nouveau_bo.c          |  8 +--
  drivers/gpu/drm/nouveau/nouveau_mem.c         | 11 ++--
  drivers/gpu/drm/nouveau/nouveau_mem.h         | 14 ++---
  drivers/gpu/drm/nouveau/nouveau_ttm.c         | 32 +++++-----
  drivers/gpu/drm/ttm/ttm_range_manager.c       | 23 +++----
  drivers/gpu/drm/ttm/ttm_resource.c            | 18 +-----
  drivers/gpu/drm/ttm/ttm_sys_manager.c         | 12 ++--
  drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c | 24 ++++----
  drivers/gpu/drm/vmwgfx/vmwgfx_thp.c           | 27 ++++-----
  include/drm/ttm/ttm_range_manager.h           |  3 +-
  include/drm/ttm/ttm_resource.h                | 43 ++++++-------
  16 files changed, 140 insertions(+), 189 deletions(-)

...

Quoted 26 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +4 -3
index ce5d07ca384c..c32e1aee2481 100644
@@ -58,7 +58,7 @@ to_range_manager(struct ttm_resource_manager *man)
  static int ttm_range_man_alloc(struct ttm_resource_manager *man,
  			       struct ttm_buffer_object *bo,
  			       const struct ttm_place *place,
-			       struct ttm_resource *mem)
+			       struct ttm_resource **res)
  {
  	struct ttm_range_manager *rman = to_range_manager(man);
  	struct ttm_range_mgr_node *node;
@@ -83,37 +83,30 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
 
  	spin_lock(&rman->lock);
  	ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
-					  mem->num_pages, bo->page_alignment, 0,
+					  node->base.num_pages,
+					  bo->page_alignment, 0,
  					  place->fpfn, lpfn, mode);
  	spin_unlock(&rman->lock);
 
-	if (unlikely(ret)) {
+	if (unlikely(ret))

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct ttm_resource_manager *man, · line 97

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +2 -4
  		kfree(node);
-	} else {
-		mem->mm_node = &node->mm_nodes[0];
-		mem->start = node->mm_nodes[0].start;
-	}
+	else
+		node->base.start = node->mm_nodes[0].start;
 
  	return ret;
  }

Looks like this patch forgets to assign *@res. Null pointer derefs when testing i915.

BTW shouldn't we return the struct ttm_resource ptr here rather than passing it as an argument?

/Thomas

#23 lore raw

Re: [PATCH 10/10] drm/ttm: flip the switch for driver allocated resources v2

Am 07.06.21 um 12:15 schrieb Thomas Hellström (Intel):

Quoted 55 lines · Christian König
Quoted 25 lines

Instead of both driver and TTM allocating memory finalize embedding the ttm_resource object as base into the driver backends.

v2: fix typo in vmwgfx grid mgr and double init in amdgpu_vram_mgr.c

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c   | 44 ++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  2 +- .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |  5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  | 60 +++++++++---------- drivers/gpu/drm/drm_gem_vram_helper.c         |  3 +- drivers/gpu/drm/nouveau/nouveau_bo.c          |  8 +--

  drivers/gpu/drm/nouveau/nouveau_mem.c         | 11 ++--
  drivers/gpu/drm/nouveau/nouveau_mem.h         | 14 ++---
  drivers/gpu/drm/nouveau/nouveau_ttm.c         | 32 +++++-----

  drivers/gpu/drm/ttm/ttm_range_manager.c       | 23 +++---- drivers/gpu/drm/ttm/ttm_resource.c            | 18 +----- drivers/gpu/drm/ttm/ttm_sys_manager.c         | 12 ++-- drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c | 24 ++++---- drivers/gpu/drm/vmwgfx/vmwgfx_thp.c           | 27 ++++----- include/drm/ttm/ttm_range_manager.h           |  3 +- include/drm/ttm/ttm_resource.h                | 43 ++++++-------

  16 files changed, 140 insertions(+), 189 deletions(-)

...

Quoted 27 lines

  diff --git a/drivers/gpu/drm/ttm/ttm_range_manager.c b/drivers/gpu/drm/ttm/ttm_range_manager.c index ce5d07ca384c..c32e1aee2481 100644

drivers/gpu/drm/ttm/ttm_range_manager.c +4 -4
@@ -58,7 +58,7 @@ to_range_manager(struct ttm_resource_manager *man)
   static int ttm_range_man_alloc(struct ttm_resource_manager *man,
                      struct ttm_buffer_object *bo,
                      const struct ttm_place *place,
-                   struct ttm_resource *mem)
+                   struct ttm_resource **res)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
       struct ttm_range_mgr_node *node;
@@ -83,37 +83,30 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
         spin_lock(&rman->lock);
       ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
-                      mem->num_pages, bo->page_alignment, 0,
+                      node->base.num_pages,
+                      bo->page_alignment, 0,
                         place->fpfn, lpfn, mode);
       spin_unlock(&rman->lock);
   -    if (unlikely(ret)) {
+    if (unlikely(ret))
           kfree(node);
-    } else {
-        mem->mm_node = &node->mm_nodes[0];
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct ttm_resource_manager *man, · line 97

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +2 -2
-        mem->start = node->mm_nodes[0].start;
-    }
+    else
+        node->base.start = node->mm_nodes[0].start;
         return ret;
   }

Looks like this patch forgets to assign *@res. Null pointer derefs when testing i915.

I should really CC the Intel list for TTM patches as well. The CI system should have spotted that.

Quoted 2 lines

BTW shouldn't we return the struct ttm_resource ptr here rather than passing it as an argument?

Yeah, good idea.

Thanks for the report,
Christian.

Quoted 4 lines

/Thomas

#24 lore raw

Re: [PATCH 10/10] drm/ttm: flip the switch for driver allocated resources v2

Quoted 66 lines · Christian König

Am 07.06.21 um 12:15 schrieb Thomas Hellström (Intel):

Quoted 56 lines · Christian König
Quoted 26 lines

Instead of both driver and TTM allocating memory finalize embedding the ttm_resource object as base into the driver backends.

v2: fix typo in vmwgfx grid mgr and double init in amdgpu_vram_mgr.c

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c   | 44 ++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  2 +- .../gpu/drm/amd/amdgpu/amdgpu_res_cursor.h    |  5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c  | 60
+++++++++----------
  drivers/gpu/drm/drm_gem_vram_helper.c         |  3 +- drivers/gpu/drm/nouveau/nouveau_bo.c          |  8 +--

  drivers/gpu/drm/nouveau/nouveau_mem.c         | 11 ++--
  drivers/gpu/drm/nouveau/nouveau_mem.h         | 14 ++---
  drivers/gpu/drm/nouveau/nouveau_ttm.c         | 32 +++++-----

  drivers/gpu/drm/ttm/ttm_range_manager.c       | 23 +++---- drivers/gpu/drm/ttm/ttm_resource.c            | 18 +----- drivers/gpu/drm/ttm/ttm_sys_manager.c         | 12 ++-- drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c | 24 ++++---- drivers/gpu/drm/vmwgfx/vmwgfx_thp.c           | 27 ++++----- include/drm/ttm/ttm_range_manager.h           |  3 +- include/drm/ttm/ttm_resource.h                | 43 ++++++-------

  16 files changed, 140 insertions(+), 189 deletions(-)

...

Quoted 27 lines

  diff --git a/drivers/gpu/drm/ttm/ttm_range_manager.c b/drivers/gpu/drm/ttm/ttm_range_manager.c index ce5d07ca384c..c32e1aee2481 100644

drivers/gpu/drm/ttm/ttm_range_manager.c +4 -4
@@ -58,7 +58,7 @@ to_range_manager(struct ttm_resource_manager *man)
   static int ttm_range_man_alloc(struct ttm_resource_manager *man,
                      struct ttm_buffer_object *bo,
                      const struct ttm_place *place,
-                   struct ttm_resource *mem)
+                   struct ttm_resource **res)
   {
       struct ttm_range_manager *rman = to_range_manager(man);
       struct ttm_range_mgr_node *node;
@@ -83,37 +83,30 @@ static int ttm_range_man_alloc(struct 
 ttm_resource_manager *man,
         spin_lock(&rman->lock);
       ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
-                      mem->num_pages, bo->page_alignment, 0,
+                      node->base.num_pages,
+                      bo->page_alignment, 0,
                         place->fpfn, lpfn, mode);
       spin_unlock(&rman->lock);
   -    if (unlikely(ret)) {
+    if (unlikely(ret))
           kfree(node);
-    } else {
-        mem->mm_node = &node->mm_nodes[0];
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_range_manager.c static int ttm_range_man_alloc(struct ttm_resource_manager *man, · line 97

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_range_manager.c +2 -2
-        mem->start = node->mm_nodes[0].start;
-    }
+    else
+        node->base.start = node->mm_nodes[0].start;
         return ret;
   }

Looks like this patch forgets to assign *@res. Null pointer derefs when testing i915.

Quoted 1 line

I should really CC the Intel list for TTM patches as well. The CI system should have spotted that.

Unfortunately, the dg1 system is not participating in CI yet AFAICT, but moving forward I think it's a good idea.

/Thomas

reviewer
#25 lore raw
In reply to Christian König
Quoted 227 lines · Christian König

To improve the handling we want the establish the resource object as base class for the backend allocations.

v2: add missing error handling

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c |  4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 54 +++++++-------
  drivers/gpu/drm/nouveau/nouveau_bo.c       |  2 +-
  drivers/gpu/drm/radeon/radeon_ttm.c        |  2 +-
  drivers/gpu/drm/ttm/ttm_bo.c               | 83 ++++++++--------------
  drivers/gpu/drm/ttm/ttm_bo_util.c          | 43 ++++++-----
  drivers/gpu/drm/ttm/ttm_resource.c         | 31 +++++---
  drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c |  2 +-
  include/drm/ttm/ttm_bo_api.h               |  1 -
  include/drm/ttm/ttm_bo_driver.h            | 10 ++-
  include/drm/ttm/ttm_resource.h             |  4 +-
  11 files changed, 110 insertions(+), 126 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -2
index 03c6b63d1d54..59723c3d5826 100644
@@ -362,14 +362,14 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
  	if (cpu_addr)
  		amdgpu_bo_kunmap(*bo_ptr);
 
-	ttm_resource_free(&(*bo_ptr)->tbo, (*bo_ptr)->tbo.resource);
+	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
 
  	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
  		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
  		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
  	}
  	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
-			     (*bo_ptr)->tbo.resource, &ctx);
+			     &(*bo_ptr)->tbo.resource, &ctx);
  	if (r)
  		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -27
index 663aa7d2e2ea..69db89261650 100644
@@ -491,7 +491,7 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
  			return r;
 
  		amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
  		ttm_bo_assign_mem(bo, new_mem);
  		goto out;
  	}
@@ -950,9 +950,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  	struct ttm_operation_ctx ctx = { false, false };
  	struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
-	struct ttm_resource tmp;
  	struct ttm_placement placement;
  	struct ttm_place placements;
+	struct ttm_resource *tmp;
  	uint64_t addr, flags;
  	int r;
 
@@ -962,37 +962,37 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
  	addr = amdgpu_gmc_agp_addr(bo);
  	if (addr != AMDGPU_BO_INVALID_OFFSET) {
  		bo->resource->start = addr >> PAGE_SHIFT;
-	} else {
+		return 0;
+	}
 
-		/* allocate GART space */
-		placement.num_placement = 1;
-		placement.placement = &placements;
-		placement.num_busy_placement = 1;
-		placement.busy_placement = &placements;
-		placements.fpfn = 0;
-		placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
-		placements.mem_type = TTM_PL_TT;
-		placements.flags = bo->resource->placement;
-
-		r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
-		if (unlikely(r))
-			return r;
+	/* allocate GART space */
+	placement.num_placement = 1;
+	placement.placement = &placements;
+	placement.num_busy_placement = 1;
+	placement.busy_placement = &placements;
+	placements.fpfn = 0;
+	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
+	placements.mem_type = TTM_PL_TT;
+	placements.flags = bo->resource->placement;
 
-		/* compute PTE flags for this buffer object */
-		flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
+	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
+	if (unlikely(r))
+		return r;
 
-		/* Bind pages */
-		gtt->offset = (u64)tmp.start << PAGE_SHIFT;
-		r = amdgpu_ttm_gart_bind(adev, bo, flags);
-		if (unlikely(r)) {
-			ttm_resource_free(bo, &tmp);
-			return r;
-		}
+	/* compute PTE flags for this buffer object */
+	flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp);
 
-		ttm_resource_free(bo, bo->resource);
-		ttm_bo_assign_mem(bo, &tmp);
+	/* Bind pages */
+	gtt->offset = (u64)tmp->start << PAGE_SHIFT;
+	r = amdgpu_ttm_gart_bind(adev, bo, flags);
+	if (unlikely(r)) {
+		ttm_resource_free(bo, &tmp);
+		return r;
  	}
 
+	ttm_resource_free(bo, &bo->resource);
+	ttm_bo_assign_mem(bo, tmp);
+
  	return 0;
  }
 
drivers/gpu/drm/nouveau/nouveau_bo.c +1 -1
index e688ca77483d..3a0d9b3bf991 100644
@@ -1009,7 +1009,7 @@ nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
  	if (old_reg->mem_type == TTM_PL_TT &&
  	    new_reg->mem_type == TTM_PL_SYSTEM) {
  		nouveau_ttm_tt_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
  		ttm_bo_assign_mem(bo, new_reg);
  		goto out;
  	}
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
index 2507c1741681..cdffa9b65108 100644
@@ -229,7 +229,7 @@ static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
  	if (old_mem->mem_type == TTM_PL_TT &&
  	    new_mem->mem_type == TTM_PL_SYSTEM) {
  		radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
  		ttm_bo_assign_mem(bo, new_mem);
  		goto out;
  	}
drivers/gpu/drm/ttm/ttm_bo.c +6 -25
index 5a7ab4b35b2d..4ed56520b81d 100644
@@ -223,7 +223,7 @@ static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  		bo->bdev->funcs->delete_mem_notify(bo);
 
  	ttm_bo_tt_destroy(bo);
-	ttm_resource_free(bo, bo->resource);
+	ttm_resource_free(bo, &bo->resource);
  }
 
  static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
@@ -489,7 +489,7 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
  			struct ttm_operation_ctx *ctx)
  {
  	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource evict_mem;
+	struct ttm_resource *evict_mem;
  	struct ttm_placement placement;
  	struct ttm_place hop;
  	int ret = 0;
@@ -519,7 +519,7 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
  		goto out;
  	}
 
-	ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
+	ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
  	if (unlikely(ret)) {
  		WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
  		if (ret != -ERESTARTSYS)
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
   */
  static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  				  const struct ttm_place *place,
-				  struct ttm_resource *mem,
+				  struct ttm_resource **mem,
  				  struct ttm_operation_ctx *ctx)
  {
  	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
+	struct ttm_resource_manager *man;
  	struct ww_acquire_ctx *ticket;
  	int ret;
 
+	man = ttm_manager_type(bdev, (*mem)->mem_type);
  	ticket = dma_resv_locking_ctx(bo->base.resv);
  	do {
  		ret = ttm_resource_alloc(bo, place, mem);
@@ -749,37 +750,7 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  			return ret;
  	} while (1);
 
-	return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
-}
-
-/**
- * ttm_bo_mem_placement - check if placement is compatible
- * @bo: BO to find memory for
- * @place: where to search
- * @mem: the memory object to fill in
- *
- * Check if placement is compatible and fill in mem structure.
- * Returns -EBUSY if placement won't work or negative error code.
- * 0 when placement can be used.
- */
-static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
-				const struct ttm_place *place,
-				struct ttm_resource *mem)
-{
-	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource_manager *man;
-

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, · line 781

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -10
-	man = ttm_manager_type(bdev, place->mem_type);
-	if (!man || !ttm_resource_manager_used(man))
-		return -EBUSY;
-
-	mem->mem_type = place->mem_type;
-	mem->placement = place->flags;
-
-	spin_lock(&bo->bdev->lru_lock);
-	ttm_bo_move_to_lru_tail(bo, mem, NULL);
-	spin_unlock(&bo->bdev->lru_lock);

Why do we drop the move_to_lru_tail here?

#26 lore raw

Am 03.06.21 um 10:45 schrieb Matthew Auld:

Quoted 23 lines · Christian König
Quoted 22 lines

[SNIP]

drivers/gpu/drm/ttm/ttm_bo.c +0 -21
-/**
- * ttm_bo_mem_placement - check if placement is compatible
- * @bo: BO to find memory for
- * @place: where to search
- * @mem: the memory object to fill in
- *
- * Check if placement is compatible and fill in mem structure.
- * Returns -EBUSY if placement won't work or negative error code.
- * 0 when placement can be used.
- */
-static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
-                const struct ttm_place *place,
-                struct ttm_resource *mem)
-{
-    struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man;
-
-    man = ttm_manager_type(bdev, place->mem_type);
-    if (!man || !ttm_resource_manager_used(man))
-        return -EBUSY;
-
Quoted 8 lines
Quoted 6 lines

-    mem->mem_type = place->mem_type;
-    mem->placement = place->flags;
-
-    spin_lock(&bo->bdev->lru_lock);
-    ttm_bo_move_to_lru_tail(bo, mem, NULL);
-    spin_unlock(&bo->bdev->lru_lock);

Why do we drop the move_to_lru_tail here?

Ah, good point.

The move_to_lru_tail() was here to make sure we see the BO in the new LRU instead of the old one before actually doing the move.

Since we haven't allocated the mem structure at this point that is no longer possible, but I think it is ok to do this for now.

One motivation of doing this is to move the LRU handling into the resource backend, so that tricks like those are not needed any more.

Regards,
Christian.

Acked-by Thomas Hellström on v1 1/10
#27 lore raw
In reply to Christian König

Hi, Christian,

It looks like all patches in the series have been reviewed or acked by Matthew, and while still a little worried about the final outcome of embedding a struct ttm_mem_resource, FWIW,

/Thomas

Quoted 629 lines · Christian König

To improve the handling we want the establish the resource object as base class for the backend allocations.

v2: add missing error handling

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c |  4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 54 +++++++-------
  drivers/gpu/drm/nouveau/nouveau_bo.c       |  2 +-
  drivers/gpu/drm/radeon/radeon_ttm.c        |  2 +-
  drivers/gpu/drm/ttm/ttm_bo.c               | 83 ++++++++--------------
  drivers/gpu/drm/ttm/ttm_bo_util.c          | 43 ++++++-----
  drivers/gpu/drm/ttm/ttm_resource.c         | 31 +++++---
  drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c |  2 +-
  include/drm/ttm/ttm_bo_api.h               |  1 -
  include/drm/ttm/ttm_bo_driver.h            | 10 ++-
  include/drm/ttm/ttm_resource.h             |  4 +-
  11 files changed, 110 insertions(+), 126 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -2
index 03c6b63d1d54..59723c3d5826 100644
@@ -362,14 +362,14 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
  	if (cpu_addr)
  		amdgpu_bo_kunmap(*bo_ptr);
 
-	ttm_resource_free(&(*bo_ptr)->tbo, (*bo_ptr)->tbo.resource);
+	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
 
  	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
  		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
  		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
  	}
  	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
-			     (*bo_ptr)->tbo.resource, &ctx);
+			     &(*bo_ptr)->tbo.resource, &ctx);
  	if (r)
  		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -27
index 663aa7d2e2ea..69db89261650 100644
@@ -491,7 +491,7 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
  			return r;
 
  		amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
  		ttm_bo_assign_mem(bo, new_mem);
  		goto out;
  	}
@@ -950,9 +950,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  	struct ttm_operation_ctx ctx = { false, false };
  	struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
-	struct ttm_resource tmp;
  	struct ttm_placement placement;
  	struct ttm_place placements;
+	struct ttm_resource *tmp;
  	uint64_t addr, flags;
  	int r;
 
@@ -962,37 +962,37 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
  	addr = amdgpu_gmc_agp_addr(bo);
  	if (addr != AMDGPU_BO_INVALID_OFFSET) {
  		bo->resource->start = addr >> PAGE_SHIFT;
-	} else {
+		return 0;
+	}
 
-		/* allocate GART space */
-		placement.num_placement = 1;
-		placement.placement = &placements;
-		placement.num_busy_placement = 1;
-		placement.busy_placement = &placements;
-		placements.fpfn = 0;
-		placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
-		placements.mem_type = TTM_PL_TT;
-		placements.flags = bo->resource->placement;
-
-		r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
-		if (unlikely(r))
-			return r;
+	/* allocate GART space */
+	placement.num_placement = 1;
+	placement.placement = &placements;
+	placement.num_busy_placement = 1;
+	placement.busy_placement = &placements;
+	placements.fpfn = 0;
+	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
+	placements.mem_type = TTM_PL_TT;
+	placements.flags = bo->resource->placement;
 
-		/* compute PTE flags for this buffer object */
-		flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
+	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
+	if (unlikely(r))
+		return r;
 
-		/* Bind pages */
-		gtt->offset = (u64)tmp.start << PAGE_SHIFT;
-		r = amdgpu_ttm_gart_bind(adev, bo, flags);
-		if (unlikely(r)) {
-			ttm_resource_free(bo, &tmp);
-			return r;
-		}
+	/* compute PTE flags for this buffer object */
+	flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp);
 
-		ttm_resource_free(bo, bo->resource);
-		ttm_bo_assign_mem(bo, &tmp);
+	/* Bind pages */
+	gtt->offset = (u64)tmp->start << PAGE_SHIFT;
+	r = amdgpu_ttm_gart_bind(adev, bo, flags);
+	if (unlikely(r)) {
+		ttm_resource_free(bo, &tmp);
+		return r;
  	}
 
+	ttm_resource_free(bo, &bo->resource);
+	ttm_bo_assign_mem(bo, tmp);
+
  	return 0;
  }
 
drivers/gpu/drm/nouveau/nouveau_bo.c +1 -1
index e688ca77483d..3a0d9b3bf991 100644
@@ -1009,7 +1009,7 @@ nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
  	if (old_reg->mem_type == TTM_PL_TT &&
  	    new_reg->mem_type == TTM_PL_SYSTEM) {
  		nouveau_ttm_tt_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
  		ttm_bo_assign_mem(bo, new_reg);
  		goto out;
  	}
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
index 2507c1741681..cdffa9b65108 100644
@@ -229,7 +229,7 @@ static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
  	if (old_mem->mem_type == TTM_PL_TT &&
  	    new_mem->mem_type == TTM_PL_SYSTEM) {
  		radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
  		ttm_bo_assign_mem(bo, new_mem);
  		goto out;
  	}
drivers/gpu/drm/ttm/ttm_bo.c +28 -55
index 5a7ab4b35b2d..4ed56520b81d 100644
@@ -223,7 +223,7 @@ static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  		bo->bdev->funcs->delete_mem_notify(bo);
 
  	ttm_bo_tt_destroy(bo);
-	ttm_resource_free(bo, bo->resource);
+	ttm_resource_free(bo, &bo->resource);
  }
 
  static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
@@ -489,7 +489,7 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
  			struct ttm_operation_ctx *ctx)
  {
  	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource evict_mem;
+	struct ttm_resource *evict_mem;
  	struct ttm_placement placement;
  	struct ttm_place hop;
  	int ret = 0;
@@ -519,7 +519,7 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
  		goto out;
  	}
 
-	ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
+	ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
  	if (unlikely(ret)) {
  		WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
  		if (ret != -ERESTARTSYS)
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
   */
  static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  				  const struct ttm_place *place,
-				  struct ttm_resource *mem,
+				  struct ttm_resource **mem,
  				  struct ttm_operation_ctx *ctx)
  {
  	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
+	struct ttm_resource_manager *man;
  	struct ww_acquire_ctx *ticket;
  	int ret;
 
+	man = ttm_manager_type(bdev, (*mem)->mem_type);
  	ticket = dma_resv_locking_ctx(bo->base.resv);
  	do {
  		ret = ttm_resource_alloc(bo, place, mem);
@@ -749,37 +750,7 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  			return ret;
  	} while (1);
 
-	return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
-}
-
-/**
- * ttm_bo_mem_placement - check if placement is compatible
- * @bo: BO to find memory for
- * @place: where to search
- * @mem: the memory object to fill in
- *
- * Check if placement is compatible and fill in mem structure.
- * Returns -EBUSY if placement won't work or negative error code.
- * 0 when placement can be used.
- */
-static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
-				const struct ttm_place *place,
-				struct ttm_resource *mem)
-{
-	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource_manager *man;
-
-	man = ttm_manager_type(bdev, place->mem_type);
-	if (!man || !ttm_resource_manager_used(man))
-		return -EBUSY;
-
-	mem->mem_type = place->mem_type;
-	mem->placement = place->flags;
-
-	spin_lock(&bo->bdev->lru_lock);
-	ttm_bo_move_to_lru_tail(bo, mem, NULL);
-	spin_unlock(&bo->bdev->lru_lock);
-	return 0;
+	return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
  }
 
  /*
@@ -792,7 +763,7 @@ static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
   */
  int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  			struct ttm_placement *placement,
-			struct ttm_resource *mem,
+			struct ttm_resource **mem,
  			struct ttm_operation_ctx *ctx)
  {
  	struct ttm_device *bdev = bo->bdev;
@@ -807,8 +778,8 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  		const struct ttm_place *place = &placement->placement[i];
  		struct ttm_resource_manager *man;
 
-		ret = ttm_bo_mem_placement(bo, place, mem);
-		if (ret)
+		man = ttm_manager_type(bdev, place->mem_type);
+		if (!man || !ttm_resource_manager_used(man))
  			continue;
 
  		type_found = true;
@@ -818,8 +789,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  		if (unlikely(ret))
  			goto error;
 
-		man = ttm_manager_type(bdev, mem->mem_type);
-		ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
+		ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
  		if (unlikely(ret)) {
  			ttm_resource_free(bo, mem);
  			if (ret == -EBUSY)
@@ -832,9 +802,10 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 
  	for (i = 0; i < placement->num_busy_placement; ++i) {
  		const struct ttm_place *place = &placement->busy_placement[i];
+		struct ttm_resource_manager *man;
 
-		ret = ttm_bo_mem_placement(bo, place, mem);
-		if (ret)
+		man = ttm_manager_type(bdev, place->mem_type);
+		if (!man || !ttm_resource_manager_used(man))
  			continue;
 
  		type_found = true;
@@ -861,12 +832,12 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  EXPORT_SYMBOL(ttm_bo_mem_space);
 
  static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
-				     struct ttm_resource *mem,
+				     struct ttm_resource **mem,
  				     struct ttm_operation_ctx *ctx,
  				     struct ttm_place *hop)
  {
  	struct ttm_placement hop_placement;
-	struct ttm_resource hop_mem;
+	struct ttm_resource *hop_mem;
  	int ret;
 
  	hop_placement.num_placement = hop_placement.num_busy_placement = 1;
@@ -877,7 +848,7 @@ static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
  	if (ret)
  		return ret;
  	/* move to the bounce domain */
-	ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
+	ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
  	if (ret) {
  		ttm_resource_free(bo, &hop_mem);
  		return ret;
@@ -889,14 +860,12 @@ static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  			      struct ttm_placement *placement,
  			      struct ttm_operation_ctx *ctx)
  {
+	struct ttm_resource *mem;
  	struct ttm_place hop;
-	struct ttm_resource mem;
  	int ret;
 
  	dma_resv_assert_held(bo->base.resv);
 
-	memset(&hop, 0, sizeof(hop));
-
  	/*
  	 * Determine where to move the buffer.
  	 *
@@ -910,7 +879,7 @@ static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  	if (ret)
  		return ret;
  bounce:
-	ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
+	ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
  	if (ret == -EMULTIHOP) {
  		ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
  		if (ret)
@@ -1019,7 +988,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
  {
  	static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
  	bool locked;
-	int ret = 0;
+	int ret;
 
  	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
 
@@ -1029,8 +998,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
  	bo->bdev = bdev;
  	bo->type = type;
  	bo->page_alignment = page_alignment;
-	bo->resource = &bo->_mem;
-	ttm_resource_alloc(bo, &sys_mem, bo->resource);
  	bo->moving = NULL;
  	bo->pin_count = 0;
  	bo->sg = sg;
@@ -1042,6 +1009,12 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
  	}
  	atomic_inc(&ttm_glob.bo_count);
 
+	ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
+	if (unlikely(ret)) {
+		ttm_bo_put(bo);
+		return ret;
+	}
+
  	/*
  	 * For ttm_bo_type_device buffers, allocate
  	 * address space from the device.
@@ -1170,7 +1143,7 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
  	 */
  	if (bo->resource->mem_type != TTM_PL_SYSTEM) {
  		struct ttm_operation_ctx ctx = { false, false };
-		struct ttm_resource evict_mem;
+		struct ttm_resource *evict_mem;
  		struct ttm_place place, hop;
 
  		memset(&place, 0, sizeof(place));
@@ -1182,7 +1155,7 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
  		if (unlikely(ret))
  			goto out;
 
-		ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
+		ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
  		if (unlikely(ret != 0)) {
  			WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
  			goto out;
drivers/gpu/drm/ttm/ttm_bo_util.c +21 -22
index aedf02a31c70..1b326e70cb02 100644
@@ -176,16 +176,17 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  		       struct ttm_operation_ctx *ctx,
  		       struct ttm_resource *new_mem)
  {
+	struct ttm_resource *old_mem = bo->resource;
  	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
+	struct ttm_resource_manager *man;
  	struct ttm_tt *ttm = bo->ttm;
-	struct ttm_resource *old_mem = bo->resource;
-	struct ttm_resource old_copy = *old_mem;
  	void *old_iomap;
  	void *new_iomap;
  	int ret;
  	unsigned long i;
 
+	man = ttm_manager_type(bdev, new_mem->mem_type);
+
  	ret = ttm_bo_wait_ctx(bo, ctx);
  	if (ret)
  		return ret;
@@ -201,7 +202,7 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  	 * Single TTM move. NOP.
  	 */
  	if (old_iomap == NULL && new_iomap == NULL)
-		goto out2;
+		goto out1;
 
  	/*
  	 * Don't move nonexistent data. Clear destination instead.
@@ -210,7 +211,7 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  	    (ttm == NULL || (!ttm_tt_is_populated(ttm) &&
  			     !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
  		memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
-		goto out2;
+		goto out1;
  	}
 
  	/*
@@ -235,27 +236,25 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  			ret = ttm_copy_io_page(new_iomap, old_iomap, i);
  		}
  		if (ret)
-			goto out1;
+			break;
  	}
  	mb();
-out2:
-	old_copy = *old_mem;
+out1:
+	ttm_resource_iounmap(bdev, new_mem, new_iomap);
+out:
+	ttm_resource_iounmap(bdev, old_mem, old_iomap);
+
+	if (ret) {
+		ttm_resource_free(bo, &new_mem);
+		return ret;
+	}
 
+	ttm_resource_free(bo, &bo->resource);
  	ttm_bo_assign_mem(bo, new_mem);
 
  	if (!man->use_tt)
  		ttm_bo_tt_destroy(bo);
 
-out1:
-	ttm_resource_iounmap(bdev, old_mem, new_iomap);
-out:
-	ttm_resource_iounmap(bdev, &old_copy, old_iomap);
-
-	/*
-	 * On error, keep the mm node!
-	 */
-	if (!ret)
-		ttm_resource_free(bo, &old_copy);
  	return ret;
  }
  EXPORT_SYMBOL(ttm_bo_move_memcpy);
@@ -566,7 +565,7 @@ static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
 
  	if (!dst_use_tt)
  		ttm_bo_tt_destroy(bo);
-	ttm_resource_free(bo, bo->resource);
+	ttm_resource_free(bo, &bo->resource);
  	return 0;
  }
 
@@ -629,7 +628,7 @@ static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
  	}
  	spin_unlock(&from->move_lock);
 
-	ttm_resource_free(bo, bo->resource);
+	ttm_resource_free(bo, &bo->resource);
 
  	dma_fence_put(bo->moving);
  	bo->moving = dma_fence_get(fence);
@@ -678,11 +677,11 @@ int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
  	if (ret)
  		ttm_bo_wait(bo, false, false);
 
-	ttm_resource_alloc(bo, &sys_mem, bo->resource);
+	ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
  	bo->ttm = NULL;
 
  	dma_resv_unlock(&ghost->base._resv);
  	ttm_bo_put(ghost);
 
-	return 0;
+	return ret;
  }
drivers/gpu/drm/ttm/ttm_resource.c +23 -8
index 59e2b7157e41..65451e1bc303 100644
@@ -27,10 +27,16 @@
 
  int ttm_resource_alloc(struct ttm_buffer_object *bo,
  		       const struct ttm_place *place,
-		       struct ttm_resource *res)
+		       struct ttm_resource **res_ptr)
  {
  	struct ttm_resource_manager *man =
  		ttm_manager_type(bo->bdev, place->mem_type);
+	struct ttm_resource *res;
+	int r;
+
+	res = kmalloc(sizeof(*res), GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
 
  	res->mm_node = NULL;
  	res->start = 0;
@@ -41,18 +47,27 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
  	res->bus.offset = 0;
  	res->bus.is_iomem = false;
  	res->bus.caching = ttm_cached;
+	r = man->func->alloc(man, bo, place, res);
+	if (r) {
+		kfree(res);
+		return r;
+	}
 
-	return man->func->alloc(man, bo, place, res);
+	*res_ptr = res;
+	return 0;
  }
 
-void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource *res)
+void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
  {
-	struct ttm_resource_manager *man =
-		ttm_manager_type(bo->bdev, res->mem_type);
+	struct ttm_resource_manager *man;
 
-	man->func->free(man, res);
-	res->mm_node = NULL;
-	res->mem_type = TTM_PL_SYSTEM;
+	if (!*res)
+		return;
+
+	man = ttm_manager_type(bo->bdev, (*res)->mem_type);
+	man->func->free(man, *res);
+	kfree(*res);
+	*res = NULL;
  }
  EXPORT_SYMBOL(ttm_resource_free);
 
drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c +1 -1
index ed8563ef9a3b..bfcf31bf7e37 100644
@@ -741,7 +741,7 @@ static int vmw_move(struct ttm_buffer_object *bo,
  			goto fail;
 
  		vmw_ttm_unbind(bo->bdev, bo->ttm);
-		ttm_resource_free(bo, bo->resource);
+		ttm_resource_free(bo, &bo->resource);
  		ttm_bo_assign_mem(bo, new_mem);
  		return 0;
  	} else {
include/drm/ttm/ttm_bo_api.h +0 -1
index 291a339a7e08..f681bbdbc698 100644
@@ -137,7 +137,6 @@ struct ttm_buffer_object {
  	 */
 
  	struct ttm_resource *resource;
-	struct ttm_resource _mem;
  	struct ttm_tt *ttm;
  	bool deleted;
 
include/drm/ttm/ttm_bo_driver.h +4 -6
index 1a9ba0b13622..ead0ef7136c8 100644
@@ -96,7 +96,7 @@ struct ttm_lru_bulk_move {
   */
  int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  		     struct ttm_placement *placement,
-		     struct ttm_resource *mem,
+		     struct ttm_resource **mem,
  		     struct ttm_operation_ctx *ctx);
 
  /**
@@ -188,8 +188,8 @@ ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
  static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
  				     struct ttm_resource *new_mem)
  {
-	bo->_mem = *new_mem;
-	new_mem->mm_node = NULL;
+	WARN_ON(bo->resource);
+	bo->resource = new_mem;
  }
 
  /**
@@ -202,9 +202,7 @@ static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
  static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
  				    struct ttm_resource *new_mem)
  {
-	struct ttm_resource *old_mem = bo->resource;
-
-	WARN_ON(old_mem->mm_node != NULL);
+	ttm_resource_free(bo, &bo->resource);
  	ttm_bo_assign_mem(bo, new_mem);
  }
 
include/drm/ttm/ttm_resource.h +2 -2
index 890b9d369519..c17c1a52070d 100644
@@ -225,8 +225,8 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
 
  int ttm_resource_alloc(struct ttm_buffer_object *bo,
  		       const struct ttm_place *place,
-		       struct ttm_resource *res);
-void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource *res);
+		       struct ttm_resource **res);
+void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
 
  void ttm_resource_manager_init(struct ttm_resource_manager *man,
  			       unsigned long p_size);
#28 lore raw
In reply to Christian König

On 6/2/21 12:09 PM, Christian König wrote:
...

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
   */
  static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  				  const struct ttm_place *place,
-				  struct ttm_resource *mem,

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo, · line 739

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +3 -1
+				  struct ttm_resource **mem,
  				  struct ttm_operation_ctx *ctx)
  {
  	struct ttm_device *bdev = bo->bdev;
-	struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
+	struct ttm_resource_manager *man;
  	struct ww_acquire_ctx *ticket;
  	int ret;
 
+	man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

/Thomas

#29 lore raw
Quoted 22 lines · Thomas Hellström (Intel)

On 6/2/21 12:09 PM, Christian König wrote:
...

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_add_move_fence(struct · line 740

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Quoted 1 line

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

Actually this last one was probably due to a bad temporary fix of the above one.

/Thomas

Quoted 2 lines
#30 lore raw

Am 07.06.21 um 19:06 schrieb Thomas Hellström (Intel):

Quoted 24 lines · Thomas Hellström (Intel)
Quoted 22 lines · Christian König

...

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_add_move_fence(struct · line 740

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Quoted 3 lines
Quoted 1 line

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

Actually this last one was probably due to a bad temporary fix of the above one.

I've found one more warning during my testing now. But that is just a false positive.

Apart from that I haven't seen any other fallout, but fingers crossed.

Christian.

Quoted 5 lines

/Thomas

Quoted 2 lines
#31 lore raw
Quoted 27 lines · Christian König

Am 07.06.21 um 19:06 schrieb Thomas Hellström (Intel):

Quoted 24 lines · Thomas Hellström (Intel)
Quoted 22 lines · Christian König

...

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_add_move_fence(struct · line 740

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

Actually this last one was probably due to a bad temporary fix of the above one.

I've found one more warning during my testing now. But that is just a false positive.

Apart from that I haven't seen any other fallout, but fingers crossed.

vmwgfx doesn't seem to happy. It works AFAICT., but warns in vmw_move() about ttm_bo_assign_mem() replacing an existing resource.

/Thomas

Quoted 8 lines

Christian.

Quoted 5 lines

/Thomas

Quoted 2 lines
#32 lore raw

Am 07.06.21 um 19:58 schrieb Thomas Hellström (Intel):

Quoted 31 lines · Christian König
Quoted 29 lines

Am 07.06.21 um 19:06 schrieb Thomas Hellström (Intel):

Quoted 26 lines · Thomas Hellström (Intel)
Quoted 23 lines · Christian König

...

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_add_move_fence(struct · line 740

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Actually this last one was probably due to a bad temporary fix of the above one.

I've found one more warning during my testing now. But that is just a false positive.

Apart from that I haven't seen any other fallout, but fingers crossed.

vmwgfx doesn't seem to happy. It works AFAICT., but warns in vmw_move() about ttm_bo_assign_mem() replacing an existing resource.

Yeah, that's the one I've just fixed. Patch is underway.

Christian.

Quoted 13 lines

/Thomas

Quoted 8 lines

Christian.

Quoted 5 lines

/Thomas

Quoted 2 lines
#33 lore raw

Hi,

Quoted 36 lines · Christian König

Am 07.06.21 um 19:58 schrieb Thomas Hellström (Intel):

Quoted 33 lines · Christian König
Quoted 31 lines

Am 07.06.21 um 19:06 schrieb Thomas Hellström (Intel):

Quoted 27 lines · Thomas Hellström (Intel)
Quoted 22 lines · Christian König

...

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_add_move_fence(struct · line 740

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +2 -1
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Quoted 1 line

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

Actually this last one was probably due to a bad temporary fix of the above one.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I've found one more warning during my testing now. But that is just a false positive.

Apart from that I haven't seen any other fallout, but fingers crossed.

vmwgfx doesn't seem to happy. It works AFAICT., but warns in vmw_move() about ttm_bo_assign_mem() replacing an existing resource.

Yeah, that's the one I've just fixed. Patch is underway.

If that's the move_to_ghost patch, I don't think that would fix the vmwgfx issue, as IIRC vmwgfx ever uses ghost objects.

/Thomas

#34 lore raw

Am 08.06.21 um 07:29 schrieb Thomas Hellström (Intel):

Quoted 41 lines

Hi,

Quoted 38 lines · Christian König

Am 07.06.21 um 19:58 schrieb Thomas Hellström (Intel):

Quoted 35 lines · Christian König
Quoted 33 lines

Am 07.06.21 um 19:06 schrieb Thomas Hellström (Intel):

Quoted 24 lines · Thomas Hellström (Intel)
Quoted 22 lines · Christian König

...

Quoted 6 lines
(quoted diff) +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,

Review static int ttm_bo_add_move_fence(struct · line 740

Quoted 10 lines
(quoted diff) +2 -1
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Quoted 3 lines
Quoted 1 line

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

Actually this last one was probably due to a bad temporary fix of the above one.

I've found one more warning during my testing now. But that is just a false positive.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Apart from that I haven't seen any other fallout, but fingers crossed.

vmwgfx doesn't seem to happy. It works AFAICT., but warns in vmw_move() about ttm_bo_assign_mem() replacing an existing resource.

Yeah, that's the one I've just fixed. Patch is underway.

If that's the move_to_ghost patch, I don't think that would fix the vmwgfx issue, as IIRC vmwgfx ever uses ghost objects.

Mhm, could be that vmwgfx is hitting the same warning with a different backtrace.

Do you have the log to double check?

Thanks,
Christian.

Quoted 4 lines

/Thomas

#35 lore raw

Hi,

Quoted 46 lines · Christian König

Am 08.06.21 um 07:29 schrieb Thomas Hellström (Intel):

Quoted 45 lines

Hi,

Quoted 42 lines · Christian König

Am 07.06.21 um 19:58 schrieb Thomas Hellström (Intel):

Quoted 38 lines · Christian König
Quoted 27 lines

Am 07.06.21 um 19:06 schrieb Thomas Hellström (Intel):

Quoted 24 lines · Thomas Hellström (Intel)
Quoted 22 lines · Christian König

...

Quoted 6 lines
(quoted diff) +0 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,

Review static int ttm_bo_add_move_fence(struct · line 740

Quoted 10 lines
(quoted diff) +2 -1
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

Actually this last one was probably due to a bad temporary fix of the above one.

I've found one more warning during my testing now. But that is just a false positive.

Apart from that I haven't seen any other fallout, but fingers crossed.

vmwgfx doesn't seem to happy. It works AFAICT., but warns in vmw_move() about ttm_bo_assign_mem() replacing an existing resource.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Yeah, that's the one I've just fixed. Patch is underway.

If that's the move_to_ghost patch, I don't think that would fix the vmwgfx issue, as IIRC vmwgfx ever uses ghost objects.

Mhm, could be that vmwgfx is hitting the same warning with a different backtrace.

Do you have the log to double check?

Unfortunately not, but IIRC it was directly from vmw_move().

/Thomas

#36 lore raw

Am 08.06.21 um 09:17 schrieb Thomas Hellström (Intel):

Quoted 4 lines

[SNIP]

Quoted 1 line

Do you have the log to double check?

Unfortunately not, but IIRC it was directly from vmw_move().

Nirmoy do you still have your vmwgfx test environment?

Thanks,
Christian.

Quoted 4 lines

/Thomas

#37 lore raw
Quoted 9 lines · Christian König

Am 08.06.21 um 09:17 schrieb Thomas Hellström (Intel):

Quoted 4 lines

[SNIP]

Quoted 1 line

Do you have the log to double check?

Unfortunately not, but IIRC it was directly from vmw_move().

Nirmoy do you still have your vmwgfx test environment?

Yes!

Quoted 9 lines

Thanks,
Christian.

Quoted 4 lines

/Thomas

#38 lore raw
Quoted 6 lines · Nirmoy
Quoted 4 lines · Christian König

Am 08.06.21 um 09:17 schrieb Thomas Hellström (Intel):

Quoted 1 line

[SNIP]

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Do you have the log to double check?

Unfortunately not, but IIRC it was directly from vmw_move().

Nirmoy do you still have your vmwgfx test environment?

Yes!

I will test this series on my vmwgfx setup.

Quoted 11 lines
Quoted 11 lines

Thanks,
Christian.

Quoted 4 lines

/Thomas

#39 lore raw

Am 08.06.21 um 11:40 schrieb Das, Nirmoy:

Quoted 12 lines · Nirmoy
Quoted 10 lines · Christian König
Quoted 8 lines

Am 08.06.21 um 09:17 schrieb Thomas Hellström (Intel):

Quoted 4 lines

[SNIP]

Quoted 1 line

Do you have the log to double check?

Unfortunately not, but IIRC it was directly from vmw_move().

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Nirmoy do you still have your vmwgfx test environment?

Yes!

I will test this series on my vmwgfx setup.

Since it is already pushed (and we fixed a bunch of its fallout) can you please test drm-misc-next instead?

Thanks,
Christian.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thanks,
Christian.

Quoted 4 lines

/Thomas

#40 lore raw
Quoted 18 lines · Christian König

Am 08.06.21 um 11:40 schrieb Das, Nirmoy:

Quoted 15 lines · Nirmoy
Quoted 13 lines · Christian König
Quoted 9 lines

Am 08.06.21 um 09:17 schrieb Thomas Hellström (Intel):

Quoted 4 lines

[SNIP]

Quoted 1 line

Do you have the log to double check?

Unfortunately not, but IIRC it was directly from vmw_move().

Nirmoy do you still have your vmwgfx test environment?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Yes!

I will test this series on my vmwgfx setup.

Since it is already pushed (and we fixed a bunch of its fallout) can you please test drm-misc-next instead?

Sure!

Nirmoy

Quoted 17 lines

Thanks,
Christian.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thanks,
Christian.

Quoted 4 lines

/Thomas

#41 lore raw
In reply to Thomas Hellström (Intel)

Am 07.06.21 um 18:40 schrieb Thomas Hellström (Intel):

Quoted 13 lines · Christian König

...

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct 
 ttm_buffer_object *bo,
    */
   static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
                     const struct ttm_place *place,
-                  struct ttm_resource *mem,
+                  struct ttm_resource **mem,
                     struct ttm_operation_ctx *ctx)
   {
       struct ttm_device *bdev = bo->bdev;
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo.c static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
-    struct ttm_resource_manager *man = ttm_manager_type(bdev, 
 mem->mem_type);
+    struct ttm_resource_manager *man;
       struct ww_acquire_ctx *ticket;
       int ret;
   +    man = ttm_manager_type(bdev, (*mem)->mem_type);

Isn't (*mem) uninitialized here? Should be place->mem_type? Eviction is immediately sent to the bushes.

Indeed and I'm like 99% sure that Nirmoy has pointed that out to me as well.

Looks like I missed to fix that one. We need more CI testing.

Thanks,
Christian.

Quoted 6 lines

Got at least one additional NULL pointer deref to track down in the eviction code, but could be a merge error of mine as well.

/Thomas

#42 lore raw
In reply to Christian König

Hi,

Quoted 19 lines · Christian König

To improve the handling we want the establish the resource object as base class for the backend allocations.

v2: add missing error handling

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c |  4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 54 +++++++-------
  drivers/gpu/drm/nouveau/nouveau_bo.c       |  2 +-
  drivers/gpu/drm/radeon/radeon_ttm.c        |  2 +-
  drivers/gpu/drm/ttm/ttm_bo.c               | 83 ++++++++--------------
  drivers/gpu/drm/ttm/ttm_bo_util.c          | 43 ++++++-----
  drivers/gpu/drm/ttm/ttm_resource.c         | 31 +++++---
  drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c |  2 +-
  include/drm/ttm/ttm_bo_api.h               |  1 -
  include/drm/ttm/ttm_bo_driver.h            | 10 ++-
  include/drm/ttm/ttm_resource.h             |  4 +-
  11 files changed, 110 insertions(+), 126 deletions(-)

...

Quoted 13 lines
drivers/gpu/drm/ttm/ttm_bo_util.c +1 -1
@@ -629,7 +628,7 @@ static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
  	}
  	spin_unlock(&from->move_lock);
 
-	ttm_resource_free(bo, bo->resource);
+	ttm_resource_free(bo, &bo->resource);
 
  	dma_fence_put(bo->moving);
  	bo->moving = dma_fence_get(fence);
@@ -678,11 +677,11 @@ int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
  	if (ret)
  		ttm_bo_wait(bo, false, false);

drivers/gpu/drm/ttm/ttm_bo_util.c int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo) · line 686

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_util.c +2 -2
 
-	ttm_resource_alloc(bo, &sys_mem, bo->resource);
+	ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
  	bo->ttm = NULL;
 
  	dma_resv_unlock(&ghost->base._resv);
  	ttm_bo_put(ghost);
 
-	return 0;
+	return ret;

Here we re-introduce a late point of failure, which I guess leaves the bo in an undefined state? Same thing with my optimization for the idle case. Needs fixing as soon as possible.

/Thomas