Skip to contentSkip to conversation

drm/ttm: add a weak BO reference to the resource v2

PATCH In review Christian König 2 participants 8 messages Jul 19, 2021 – Aug 23, 2021

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

PATCH 1/5 drm/ttm: add a weak BO reference to the resource v2

Keep track for which BO a resource was allocated. This is necessary to move the LRU handling into the resources.

A bit problematic is i915 since it tries to use the resource interface without a BO which is illegal from the conceptional point of view.

v2: Document that this is a weak reference and add a workaround for i915

---
 drivers/gpu/drm/i915/intel_region_ttm.c | 5 +++++
 drivers/gpu/drm/ttm/ttm_bo_util.c       | 7 +++++--
 drivers/gpu/drm/ttm/ttm_resource.c      | 1 +
 include/drm/ttm/ttm_resource.h          | 2 ++
 4 files changed, 13 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/intel_region_ttm.c +5 -0
index 27fe0668d094..980b10a7debf 100644
@@ -88,6 +88,7 @@ intel_region_ttm_node_reserve(struct intel_memory_region *mem,
 	place.fpfn = offset >> PAGE_SHIFT;
 	place.lpfn = place.fpfn + (size >> PAGE_SHIFT);
 	mock_bo.base.size = size;
+	mock_bo.bdev = &mem->i915->bdev;
 	ret = man->func->alloc(man, &mock_bo, &place, &res);
 	if (ret == -ENOSPC)
 		ret = -ENXIO;
@@ -104,7 +105,11 @@ void intel_region_ttm_node_free(struct intel_memory_region *mem,
 				struct ttm_resource *res)
 {
 	struct ttm_resource_manager *man = mem->region_private;
+	struct ttm_buffer_object mock_bo = {};
 
+	mock_bo.base.size = res->num_pages * PAGE_SIZE;
+	mock_bo.bdev = &mem->i915->bdev;
+	res->bo = &mock_bo;
 	man->func->free(man, res);
 }
 
drivers/gpu/drm/ttm/ttm_bo_util.c +5 -2
index 2f57f824e6db..a1570aa8ff56 100644
@@ -239,6 +239,11 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
 	if (bo->type != ttm_bo_type_sg)
 		fbo->base.base.resv = &fbo->base.base._resv;
 
+	if (fbo->base.resource) {
+		fbo->base.resource->bo = &fbo->base;
+		bo->resource = NULL;
+	}
+
 	dma_resv_init(&fbo->base.base._resv);
 	fbo->base.base.dev = NULL;
 	ret = dma_resv_trylock(&fbo->base.base._resv);
@@ -507,7 +512,6 @@ static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
 		ghost_obj->ttm = NULL;
 	else
 		bo->ttm = NULL;
-	bo->resource = NULL;
 
 	dma_resv_unlock(&ghost_obj->base._resv);
 	ttm_bo_put(ghost_obj);
@@ -635,7 +639,6 @@ int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
 	dma_resv_unlock(&ghost->base._resv);
 	ttm_bo_put(ghost);
 	bo->ttm = ttm;
-	bo->resource = NULL;
 	ttm_bo_assign_mem(bo, sys_res);
 	return 0;
 
drivers/gpu/drm/ttm/ttm_resource.c +1 -0
index 2431717376e7..7ff6194154fe 100644
@@ -41,6 +41,7 @@ void ttm_resource_init(struct ttm_buffer_object *bo,
 	res->bus.offset = 0;
 	res->bus.is_iomem = false;
 	res->bus.caching = ttm_cached;
+	res->bo = bo;
 }
 EXPORT_SYMBOL(ttm_resource_init);
 
include/drm/ttm/ttm_resource.h +2 -0
index 140b6b9a8bbe..c73b35fb7d42 100644
@@ -161,6 +161,7 @@ struct ttm_bus_placement {
  * @mem_type: Resource type of the allocation.
  * @placement: Placement flags.
  * @bus: Placement on io bus accessible to the CPU
+ * @bo: weak reference to the BO using this resource
  *
  * Structure indicating the placement and space resources used by a
  * buffer object.
@@ -171,6 +172,7 @@ struct ttm_resource {
 	uint32_t mem_type;
 	uint32_t placement;
 	struct ttm_bus_placement bus;
+	struct ttm_buffer_object *bo;
 };
 
 /**
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/5 drm/ttm: add ttm_resource_fini

Make sure we call the common cleanup function in all implementations of the resource manager.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c     | 2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_preempt_mgr.c | 1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c    | 2 ++
 drivers/gpu/drm/nouveau/nouveau_mem.c           | 3 ++-
 drivers/gpu/drm/nouveau/nouveau_mem.h           | 3 ++-
 drivers/gpu/drm/nouveau/nouveau_ttm.c           | 9 +++++----
 drivers/gpu/drm/ttm/ttm_range_manager.c         | 2 ++
 drivers/gpu/drm/ttm/ttm_resource.c              | 6 ++++++
 drivers/gpu/drm/ttm/ttm_sys_manager.c           | 1 +
 drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c   | 2 ++
 drivers/gpu/drm/vmwgfx/vmwgfx_thp.c             | 3 ++-
 include/drm/ttm/ttm_resource.h                  | 3 +++
 12 files changed, 30 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +2 -0
index 543000304a1c..77cfb64dd312 100644
@@ -169,6 +169,7 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
 	return 0;
 
 err_free:
+	ttm_resource_fini(man, &node->base.base);
 	kfree(node);
 
 err_out:
@@ -200,6 +201,7 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
 	if (!(res->placement & TTM_PL_FLAG_TEMPORARY))
 		atomic64_sub(res->num_pages, &mgr->used);
 
+	ttm_resource_fini(man, res);
 	kfree(node);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_preempt_mgr.c +1 -0
index d02c8637f909..ffddec08e931 100644
@@ -95,6 +95,7 @@ static void amdgpu_preempt_mgr_del(struct ttm_resource_manager *man,
 	struct amdgpu_preempt_mgr *mgr = to_preempt_mgr(man);
 
 	atomic64_sub(res->num_pages, &mgr->used);
+	ttm_resource_fini(man, res);
 	kfree(res);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +2 -0
index 436ec246a7da..7e68a07c0fb2 100644
@@ -471,6 +471,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 	while (i--)
 		drm_mm_remove_node(&node->mm_nodes[i]);
 	spin_unlock(&mgr->lock);
+	ttm_resource_fini(man, &node->base);
 	kvfree(node);
 
 error_sub:
@@ -510,6 +511,7 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
 	atomic64_sub(usage, &mgr->usage);
 	atomic64_sub(vis_usage, &mgr->vis_usage);
 
+	ttm_resource_fini(man, res);
 	kvfree(node);
 }
 
drivers/gpu/drm/nouveau/nouveau_mem.c +2 -1
index 0de6549fb875..5b610906bec8 100644
@@ -175,11 +175,12 @@ nouveau_mem_vram(struct ttm_resource *reg, bool contig, u8 page)
 }
 
 void
-nouveau_mem_del(struct ttm_resource *reg)
+nouveau_mem_del(struct ttm_resource_manager *man, struct ttm_resource *reg)
 {
 	struct nouveau_mem *mem = nouveau_mem(reg);
 
 	nouveau_mem_fini(mem);
+	ttm_resource_fini(man, reg);
 	kfree(mem);
 }
 
drivers/gpu/drm/nouveau/nouveau_mem.h +2 -1
index 2c01166a90f2..325551eba5cd 100644
@@ -23,7 +23,8 @@ nouveau_mem(struct ttm_resource *reg)
 
 int nouveau_mem_new(struct nouveau_cli *, u8 kind, u8 comp,
 		    struct ttm_resource **);
-void nouveau_mem_del(struct ttm_resource *);
+void nouveau_mem_del(struct ttm_resource_manager *man,
+		     struct ttm_resource *);
 int nouveau_mem_vram(struct ttm_resource *, bool contig, u8 page);
 int nouveau_mem_host(struct ttm_resource *, struct ttm_tt *);
 void nouveau_mem_fini(struct nouveau_mem *);
drivers/gpu/drm/nouveau/nouveau_ttm.c +5 -4
index f4c2e46b6fe1..34421174ed59 100644
@@ -36,9 +36,10 @@
 #include <core/tegra.h>
 
 static void
-nouveau_manager_del(struct ttm_resource_manager *man, struct ttm_resource *reg)
+nouveau_manager_del(struct ttm_resource_manager *man,
+		    struct ttm_resource *reg)
 {
-	nouveau_mem_del(reg);
+	nouveau_mem_del(man, reg);
 }
 
 static int
@@ -62,7 +63,7 @@ nouveau_vram_manager_new(struct ttm_resource_manager *man,
 
 	ret = nouveau_mem_vram(*res, nvbo->contig, nvbo->page);
 	if (ret) {
-		nouveau_mem_del(*res);
+		nouveau_mem_del(man, *res);
 		return ret;
 	}
 
@@ -118,7 +119,7 @@ nv04_gart_manager_new(struct ttm_resource_manager *man,
 	ret = nvif_vmm_get(&mem->cli->vmm.vmm, PTES, false, 12, 0,
 			   (long)(*res)->num_pages << PAGE_SHIFT, &mem->vma[0]);
 	if (ret) {
-		nouveau_mem_del(*res);
+		nouveau_mem_del(man, *res);
 		return ret;
 	}
 
drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
index 03395386e8a7..9e8c245c320c 100644
@@ -89,6 +89,7 @@ static int ttm_range_man_alloc(struct ttm_resource_manager *man,
 	spin_unlock(&rman->lock);
 
 	if (unlikely(ret)) {
+		ttm_resource_fini(man, *res);
 		kfree(node);
 		return ret;
 	}
@@ -108,6 +109,7 @@ static void ttm_range_man_free(struct ttm_resource_manager *man,
 	drm_mm_remove_node(&node->mm_nodes[0]);
 	spin_unlock(&rman->lock);
 
+	ttm_resource_fini(man, res);
 	kfree(node);
 }
 
drivers/gpu/drm/ttm/ttm_resource.c +6 -0
index 7ff6194154fe..8e890de9af7e 100644
@@ -45,6 +45,12 @@ void ttm_resource_init(struct ttm_buffer_object *bo,
 }
 EXPORT_SYMBOL(ttm_resource_init);
 
+void ttm_resource_fini(struct ttm_resource_manager *man,
+		       struct ttm_resource *res)
+{
+}
+EXPORT_SYMBOL(ttm_resource_fini);
+
 int ttm_resource_alloc(struct ttm_buffer_object *bo,
 		       const struct ttm_place *place,
 		       struct ttm_resource **res_ptr)
drivers/gpu/drm/ttm/ttm_sys_manager.c +1 -0
index 63aca52f75e1..135394dcca95 100644
@@ -23,6 +23,7 @@ static int ttm_sys_man_alloc(struct ttm_resource_manager *man,
 static void ttm_sys_man_free(struct ttm_resource_manager *man,
 			     struct ttm_resource *res)
 {
+	ttm_resource_fini(man, res);
 	kfree(res);
 }
 
drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c +2 -0
index 28ceb749a733..ea89b2f74957 100644
@@ -84,6 +84,7 @@ static int vmw_gmrid_man_get_node(struct ttm_resource_manager *man,
 	gman->used_gmr_pages -= (*res)->num_pages;
 	spin_unlock(&gman->lock);
 	ida_free(&gman->gmr_ida, id);
+	ttm_resource_fini(man, *res);
 	kfree(*res);
 	return -ENOSPC;
 }
@@ -97,6 +98,7 @@ static void vmw_gmrid_man_put_node(struct ttm_resource_manager *man,
 	spin_lock(&gman->lock);
 	gman->used_gmr_pages -= res->num_pages;
 	spin_unlock(&gman->lock);
+	ttm_resource_fini(man, res);
 	kfree(res);
 }
 
drivers/gpu/drm/vmwgfx/vmwgfx_thp.c +2 -1
index 2a3d3468e4e0..4fcbd94ccc11 100644
@@ -104,6 +104,7 @@ static int vmw_thp_get_node(struct ttm_resource_manager *man,
 	spin_unlock(&rman->lock);
 
 	if (unlikely(ret)) {
+		ttm_resource_fini(man, &node->base);
 		kfree(node);
 	} else {
 		node->base.start = node->mm_nodes[0].start;
@@ -122,7 +123,7 @@ static void vmw_thp_put_node(struct ttm_resource_manager *man,
 	spin_lock(&rman->lock);
 	drm_mm_remove_node(&node->mm_nodes[0]);
 	spin_unlock(&rman->lock);
-
+	ttm_resource_fini(man, res);
 	kfree(node);
 }
 
include/drm/ttm/ttm_resource.h +3 -0
index c73b35fb7d42..8e116c0a276d 100644
@@ -264,6 +264,9 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
 void ttm_resource_init(struct ttm_buffer_object *bo,
                        const struct ttm_place *place,
                        struct ttm_resource *res);
+void ttm_resource_fini(struct ttm_resource_manager *man,
+		       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

PATCH 3/5 drm/ttm: add common accounting to the resource mgr

In reply to Christian König

It makes sense to have this in the common manager for debugging and accounting of how much resources are used.

---
 drivers/gpu/drm/ttm/ttm_resource.c |  8 ++++++++
 include/drm/ttm/ttm_resource.h     | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+)
drivers/gpu/drm/ttm/ttm_resource.c +8 -0
index 8e890de9af7e..5f1ff1354fba 100644
@@ -33,6 +33,8 @@ void ttm_resource_init(struct ttm_buffer_object *bo,
                        const struct ttm_place *place,
                        struct ttm_resource *res)
 {
+	struct ttm_resource_manager *man;
+
 	res->start = 0;
 	res->num_pages = PFN_UP(bo->base.size);
 	res->mem_type = place->mem_type;
@@ -42,12 +44,16 @@ void ttm_resource_init(struct ttm_buffer_object *bo,
 	res->bus.is_iomem = false;
 	res->bus.caching = ttm_cached;
 	res->bo = bo;
+
+	man = ttm_manager_type(bo->bdev, place->mem_type);
+	atomic64_add(bo->base.size, &man->usage);
 }
 EXPORT_SYMBOL(ttm_resource_init);
 
 void ttm_resource_fini(struct ttm_resource_manager *man,
 		       struct ttm_resource *res)
 {
+	atomic64_sub(res->bo->base.size, &man->usage);
 }
 EXPORT_SYMBOL(ttm_resource_fini);
 
@@ -89,6 +95,7 @@ void ttm_resource_manager_init(struct ttm_resource_manager *man,
 
 	spin_lock_init(&man->move_lock);
 	man->size = p_size;
+	atomic64_set(&man->usage, 0);
 
 	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
 		INIT_LIST_HEAD(&man->lru[i]);
@@ -161,6 +168,7 @@ void ttm_resource_manager_debug(struct ttm_resource_manager *man,
 	drm_printf(p, "  use_type: %d\n", man->use_type);
 	drm_printf(p, "  use_tt: %d\n", man->use_tt);
 	drm_printf(p, "  size: %llu\n", man->size);
+	drm_printf(p, "  usage: %llu\n", atomic64_read(&man->usage));
 	if (man->func->debug)
 		man->func->debug(man, p);
 }
include/drm/ttm/ttm_resource.h +18 -0
index 8e116c0a276d..b133997f55ce 100644
@@ -27,6 +27,7 @@
 
 #include <linux/types.h>
 #include <linux/mutex.h>
+#include <linux/atomic.h>
 #include <linux/dma-buf-map.h>
 #include <linux/dma-fence.h>
 #include <drm/drm_print.h>
@@ -112,6 +113,7 @@ struct ttm_resource_manager_func {
  * static information. bdev::driver::io_mem_free is never used.
  * @lru: The lru list for this memory type.
  * @move: The fence of the last pipelined move operation.
+ * @usage: How much of the region is used.
  *
  * This structure is used to identify and manage memory types for a device.
  */
@@ -135,6 +137,9 @@ struct ttm_resource_manager {
 	 * Protected by @move_lock.
 	 */
 	struct dma_fence *move;
+
+	/* Own protection */
+	atomic64_t usage;
 };
 
 /**
@@ -261,6 +266,19 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
 	man->move = NULL;
 }
 
+/**
+ * ttm_resource_manager_usage
+ *
+ * @man: A memory manager object.
+ *
+ * Return how many resources are currently used.
+ */
+static inline uint64_t
+ttm_resource_manager_usage(struct ttm_resource_manager *man)
+{
+	return atomic64_read(&man->usage);
+}
+
 void ttm_resource_init(struct ttm_buffer_object *bo,
                        const struct ttm_place *place,
                        struct ttm_resource *res);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 4/5 drm/ttm: move the LRU into resource handling

In reply to Christian König

This way we finally fix the problem that new resource are not immediately evict-able after allocation.

That has caused numerous problems including OOM on GDS handling and not being able to use TTM as general resource manager.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |   8 +-
 drivers/gpu/drm/i915/gem/i915_gem_ttm.c |   2 +-
 drivers/gpu/drm/ttm/ttm_bo.c            | 101 ++-----------------
 drivers/gpu/drm/ttm/ttm_bo_util.c       |   1 -
 drivers/gpu/drm/ttm/ttm_device.c        |   4 +-
 drivers/gpu/drm/ttm/ttm_resource.c      | 127 ++++++++++++++++++++++++
 include/drm/ttm/ttm_bo_api.h            |  16 ---
 include/drm/ttm/ttm_bo_driver.h         |  29 +-----
 include/drm/ttm/ttm_resource.h          |  35 +++++++
 9 files changed, 177 insertions(+), 146 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -5
index 18246b5b6ee3..4b178a74b4e0 100644
@@ -643,12 +643,12 @@ void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
 
 	if (vm->bulk_moveable) {
 		spin_lock(&adev->mman.bdev.lru_lock);
-		ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
+		ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
 		spin_unlock(&adev->mman.bdev.lru_lock);
 		return;
 	}
 
-	memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
+	ttm_lru_bulk_move_init(&vm->lru_bulk_move);
 
 	spin_lock(&adev->mman.bdev.lru_lock);
 	list_for_each_entry(bo_base, &vm->idle, vm_status) {
@@ -658,11 +658,9 @@ void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
 		if (!bo->parent)
 			continue;
 
-		ttm_bo_move_to_lru_tail(&bo->tbo, bo->tbo.resource,
-					&vm->lru_bulk_move);
+		ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
 		if (shadow)
 			ttm_bo_move_to_lru_tail(&shadow->tbo,
-						shadow->tbo.resource,
 						&vm->lru_bulk_move);
 	}
 	spin_unlock(&adev->mman.bdev.lru_lock);
drivers/gpu/drm/i915/gem/i915_gem_ttm.c +1 -1
index bf33724bed5c..b38eef37f1c8 100644
@@ -472,7 +472,7 @@ static void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj)
 			bo->priority = I915_TTM_PRIO_NO_PAGES;
 	}
 
-	ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
+	ttm_bo_move_to_lru_tail(bo, NULL);
 	spin_unlock(&bo->bdev->lru_lock);
 }
 
drivers/gpu/drm/ttm/ttm_bo.c +7 -94

176 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_bo_util.c +0 -1
index a1570aa8ff56..e57fc64ffce9 100644
@@ -229,7 +229,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
 
 	atomic_inc(&ttm_glob.bo_count);
 	INIT_LIST_HEAD(&fbo->base.ddestroy);
-	INIT_LIST_HEAD(&fbo->base.lru);
 	fbo->base.moving = NULL;
 	drm_vma_node_reset(&fbo->base.base.vma_node);
 
drivers/gpu/drm/ttm/ttm_device.c +3 -1
index 460953dcad11..a6e533c3ac56 100644
@@ -132,6 +132,7 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
 {
 	struct ttm_resource_manager *man;
 	struct ttm_buffer_object *bo;
+	struct ttm_resource *res;
 	unsigned i, j;
 	int ret;
 
@@ -142,9 +143,10 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
 			continue;
 
 		for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) {
-			list_for_each_entry(bo, &man->lru[j], lru) {
+			list_for_each_entry(res, &man->lru[j], lru) {
 				uint32_t num_pages;
 
+				bo = res->bo;
 				if (!bo->ttm ||
 				    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
 				    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)
drivers/gpu/drm/ttm/ttm_resource.c +127 -0

148 lines not on this page · raw message

include/drm/ttm/ttm_bo_api.h +0 -16
index f681bbdbc698..0928d8cfb45a 100644
@@ -56,8 +56,6 @@ struct ttm_placement;
 
 struct ttm_place;
 
-struct ttm_lru_bulk_move;
-
 /**
  * enum ttm_bo_type
  *
@@ -95,7 +93,6 @@ struct ttm_tt;
  * @ttm: TTM structure holding system pages.
  * @evicted: Whether the object was evicted without user-space knowing.
  * @deleted: True if the object is only a zombie and already deleted.
- * @lru: List head for the lru list.
  * @ddestroy: List head for the delayed destroy list.
  * @swap: List head for swap LRU list.
  * @moving: Fence set when BO is moving
@@ -144,7 +141,6 @@ struct ttm_buffer_object {
 	 * Members protected by the bdev::lru_lock.
 	 */
 
-	struct list_head lru;
 	struct list_head ddestroy;
 
 	/**
@@ -308,7 +304,6 @@ void ttm_bo_put(struct ttm_buffer_object *bo);
  * ttm_bo_move_to_lru_tail
  *
  * @bo: The buffer object.
- * @mem: Resource object.
  * @bulk: optional bulk move structure to remember BO positions
  *
  * Move this BO to the tail of all lru lists used to lookup and reserve an
@@ -316,19 +311,8 @@ void ttm_bo_put(struct ttm_buffer_object *bo);
  * held, and is used to make a BO less likely to be considered for eviction.
  */
 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
-			     struct ttm_resource *mem,
 			     struct ttm_lru_bulk_move *bulk);
 
-/**
- * ttm_bo_bulk_move_lru_tail
- *
- * @bulk: bulk move structure
- *
- * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
- * BO order never changes. Should be called with ttm_global::lru_lock held.
- */
-void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
-
 /**
  * ttm_bo_lock_delayed_workqueue
  *
include/drm/ttm/ttm_bo_driver.h +1 -28
index 68d6069572aa..fba2f7d3d34e 100644
@@ -45,33 +45,6 @@
 #include "ttm_tt.h"
 #include "ttm_pool.h"
 
-/**
- * struct ttm_lru_bulk_move_pos
- *
- * @first: first BO in the bulk move range
- * @last: last BO in the bulk move range
- *
- * Positions for a lru bulk move.
- */
-struct ttm_lru_bulk_move_pos {
-	struct ttm_buffer_object *first;
-	struct ttm_buffer_object *last;
-};
-
-/**
- * struct ttm_lru_bulk_move
- *
- * @tt: first/last lru entry for BOs in the TT domain
- * @vram: first/last lru entry for BOs in the VRAM domain
- * @swap: first/last lru entry for BOs on the swap list
- *
- * Helper structure for bulk moves on the LRU list.
- */
-struct ttm_lru_bulk_move {
-	struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
-	struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
-};
-
 /*
  * ttm_bo.c
  */
@@ -182,7 +155,7 @@ static inline void
 ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
 {
 	spin_lock(&bo->bdev->lru_lock);
-	ttm_bo_move_to_lru_tail(bo, bo->resource, NULL);
+	ttm_bo_move_to_lru_tail(bo, NULL);
 	spin_unlock(&bo->bdev->lru_lock);
 }
 
include/drm/ttm/ttm_resource.h +35 -0
index b133997f55ce..500711415ae9 100644
@@ -26,10 +26,12 @@
 #define _TTM_RESOURCE_H_
 
 #include <linux/types.h>
+#include <linux/list.h>
 #include <linux/mutex.h>
 #include <linux/atomic.h>
 #include <linux/dma-buf-map.h>
 #include <linux/dma-fence.h>
+
 #include <drm/drm_print.h>
 #include <drm/ttm/ttm_caching.h>
 #include <drm/ttm/ttm_kmap_iter.h>
@@ -178,6 +180,33 @@ struct ttm_resource {
 	uint32_t placement;
 	struct ttm_bus_placement bus;
 	struct ttm_buffer_object *bo;
+	struct list_head lru;
+};
+
+/**
+ * struct ttm_lru_bulk_move_pos
+ *
+ * @first: first res in the bulk move range
+ * @last: last res in the bulk move range
+ *
+ * Positions for a lru bulk move.
+ */
+struct ttm_lru_bulk_move_pos {
+	struct ttm_resource *first;
+	struct ttm_resource *last;
+};
+
+/**
+ * struct ttm_lru_bulk_move
+ *
+ * @tt: first/last lru entry for resources in the TT domain
+ * @vram: first/last lru entry for resources in the VRAM domain
+ *
+ * Helper structure for bulk moves on the LRU list.
+ */
+struct ttm_lru_bulk_move {
+	struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
+	struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
 };
 
 /**
@@ -279,6 +308,12 @@ ttm_resource_manager_usage(struct ttm_resource_manager *man)
 	return atomic64_read(&man->usage);
 }
 
+void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk);
+void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk);
+
+void ttm_resource_move_to_lru_tail(struct ttm_resource *res,
+				   struct ttm_lru_bulk_move *bulk);
+
 void ttm_resource_init(struct ttm_buffer_object *bo,
                        const struct ttm_place *place,
                        struct ttm_resource *res);
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 4/5] drm/ttm: move the LRU into resource handling

Quoted 217 lines · Christian König

This way we finally fix the problem that new resource are not immediately evict-able after allocation.

That has caused numerous problems including OOM on GDS handling and not being able to use TTM as general resource manager.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |   8 +- drivers/gpu/drm/i915/gem/i915_gem_ttm.c |   2 +- drivers/gpu/drm/ttm/ttm_bo.c            | 101 ++----------------- drivers/gpu/drm/ttm/ttm_bo_util.c       |   1 - drivers/gpu/drm/ttm/ttm_device.c        |   4 +- drivers/gpu/drm/ttm/ttm_resource.c      | 127
++++++++++++++++++++++++
 include/drm/ttm/ttm_bo_api.h            |  16 --- include/drm/ttm/ttm_bo_driver.h         |  29 +----- include/drm/ttm/ttm_resource.h          |  35 +++++++

 9 files changed, 177 insertions(+), 146 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 18246b5b6ee3..4b178a74b4e0 100644
@@ -643,12 +643,12 @@ void amdgpu_vm_move_to_lru_tail(struct
 amdgpu_device *adev,


        if (vm->bulk_moveable) { spin_lock(&adev->mman.bdev.lru_lock);
-               ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move); +               ttm_lru_bulk_move_tail(&vm->lru_bulk_move); spin_unlock(&adev->mman.bdev.lru_lock); return;
        }

-       memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move)); +       ttm_lru_bulk_move_init(&vm->lru_bulk_move);

        spin_lock(&adev->mman.bdev.lru_lock); list_for_each_entry(bo_base, &vm->idle, vm_status) {

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
@@ -658,11 +658,9 @@ void amdgpu_vm_move_to_lru_tail(struct
 amdgpu_device *adev,
                 if (!bo->parent)
                         continue;


-               ttm_bo_move_to_lru_tail(&bo->tbo, bo->tbo.resource, -                                       &vm->lru_bulk_move); +               ttm_bo_move_to_lru_tail(&bo->tbo, &vm-

Quoted 1 line

lru_bulk_move);

                if (shadow) ttm_bo_move_to_lru_tail(&shadow->tbo, -                                               shadow->tbo.resource, &vm->lru_bulk_move); }
        spin_unlock(&adev->mman.bdev.lru_lock);

drivers/gpu/drm/i915/gem/i915_gem_ttm.c +0 -0
index bf33724bed5c..b38eef37f1c8 100644
@@ -472,7 +472,7 @@ static void i915_ttm_adjust_lru(struct
 drm_i915_gem_object *obj)
                         bo->priority = I915_TTM_PRIO_NO_PAGES;
         }


-       ttm_bo_move_to_lru_tail(bo, bo->resource, NULL); +       ttm_bo_move_to_lru_tail(bo, NULL); spin_unlock(&bo->bdev->lru_lock);
 }

drivers/gpu/drm/ttm/ttm_bo.c +0 -0
index 5a2dc712c632..09a62ad06b9d 100644
@@ -69,95 +69,15 @@ static void ttm_bo_mem_space_debug(struct
 ttm_buffer_object *bo,
         }
  }

drivers/gpu/drm/ttm/ttm_bo.c +0 -11
-static void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
-{
-       struct ttm_device *bdev = bo->bdev;
-
-       list_del_init(&bo->lru);
-
-       if (bdev->funcs->del_from_lru_notify)
-               bdev->funcs->del_from_lru_notify(bo);
-}
-
-static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos

*pos,
-                                    struct ttm_buffer_object *bo)

drivers/gpu/drm/ttm/ttm_bo.c +0 -6
-{
-       if (!pos->first)
-               pos->first = bo;
-       pos->last = bo;
-}
-

 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo, -                            struct ttm_resource *mem, struct ttm_lru_bulk_move *bulk) {
-       struct ttm_device *bdev = bo->bdev;
-       struct ttm_resource_manager *man;

drivers/gpu/drm/ttm/ttm_bo.c +0 -19
-
-       if (!bo->deleted)
-               dma_resv_assert_held(bo->base.resv);
-
-       if (bo->pin_count) {
-               ttm_bo_del_from_lru(bo);
-               return;
-       }
-
-       man = ttm_manager_type(bdev, mem->mem_type);
-       list_move_tail(&bo->lru, &man->lru[bo->priority]);
-
-       if (bdev->funcs->del_from_lru_notify)
-               bdev->funcs->del_from_lru_notify(bo);
-
-       if (bulk && !bo->pin_count) {
-               switch (bo->resource->mem_type) {
-               case TTM_PL_TT:
-                       ttm_bo_bulk_move_set_pos(&bulk->tt[bo-
Quoted 1 line

priority], bo);

-                       break;
+       dma_resv_assert_held(bo->base.resv);

-               case TTM_PL_VRAM:
-                       ttm_bo_bulk_move_set_pos(&bulk->vram[bo-

Quoted 1 line

priority], bo);

-                       break;
-               }
-       }
+       ttm_resource_move_to_lru_tail(bo->resource, bulk); }
 EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);

drivers/gpu/drm/ttm/ttm_bo.c +0 -30
-void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
-{
-       unsigned i;
-
-       for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
-               struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
-               struct ttm_resource_manager *man;
-
-               if (!pos->first)
-                       continue;
-
-               dma_resv_assert_held(pos->first->base.resv);
-               dma_resv_assert_held(pos->last->base.resv);
-
-               man = ttm_manager_type(pos->first->bdev, TTM_PL_TT);
-               list_bulk_move_tail(&man->lru[i], &pos->first->lru,
-                                   &pos->last->lru);
-       }
-
-       for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
-               struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
-               struct ttm_resource_manager *man;
-
-               if (!pos->first)
-                       continue;
-
-               dma_resv_assert_held(pos->first->base.resv);
-               dma_resv_assert_held(pos->last->base.resv);
-
-               man = ttm_manager_type(pos->first->bdev,

TTM_PL_VRAM);
-               list_bulk_move_tail(&man->lru[i], &pos->first->lru, -                                   &pos->last->lru);
-       }

drivers/gpu/drm/ttm/ttm_bo.c +0 -3
-}
-EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
-

 static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, struct ttm_resource *mem, bool evict,
                                  struct ttm_operation_ctx *ctx,

drivers/gpu/drm/ttm/ttm_bo.c +0 -0
@@ -339,7 +259,6 @@ static int ttm_bo_cleanup_refs(struct
 ttm_buffer_object *bo,
                 return ret;
         }


-       ttm_bo_del_from_lru(bo);
        list_del_init(&bo->ddestroy);
        spin_unlock(&bo->bdev->lru_lock);
        ttm_bo_cleanup_memtype_use(bo);

drivers/gpu/drm/ttm/ttm_bo.c +1 -1
@@ -440,7 +359,7 @@ static void ttm_bo_release(struct kref *kref)
                  */
                 if (bo->pin_count) {
                         bo->pin_count = 0;
-                       ttm_bo_move_to_lru_tail(bo, bo->resource,
 NULL);
+                       ttm_resource_move_to_lru_tail(bo->resource,
 NULL);
                 }


                kref_init(&bo->kref);

drivers/gpu/drm/ttm/ttm_bo.c +0 -0
@@ -453,7 +372,6 @@ static void ttm_bo_release(struct kref *kref)
         }


        spin_lock(&bo->bdev->lru_lock);
-       ttm_bo_del_from_lru(bo);
        list_del(&bo->ddestroy);
        spin_unlock(&bo->bdev->lru_lock);

drivers/gpu/drm/ttm/ttm_bo.c +1 -0
@@ -667,15 +585,17 @@ int ttm_mem_evict_first(struct ttm_device
 *bdev,
                         struct ww_acquire_ctx *ticket)
  {
         struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
+       struct ttm_resource *res;
         bool locked = false;
         unsigned i;
         int ret;
Quoted 8 lines


        spin_lock(&bdev->lru_lock);
        for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { -               list_for_each_entry(bo, &man->lru[i], lru) { +               list_for_each_entry(res, &man->lru[i], lru) { bool busy;

+                       bo = res->bo;

Follow up to previous review: What happens here if someone now reassigns @res->bo and then kills @bo. At least it's not immediately clear what's protecting from that. Isn't a kref_get_unless_zero() on the bo needed here, and res->bo being assigned (and properly cleared on bo destruction) under the lru_lock when needed?

Admittedly as you pointed out earlier we can't kref_put() the bo under the lru lock but (if all else fails) one could perhaps defer the put to a worker, or move the bo to lru tail and drop the lru lock iff kref_put() may hit a zero refcount.

/Thomas

#5 lore raw

PATCH 5/5 drm/ttm: add resource iterator

In reply to Christian König

Instead of duplicating that at different places add an iterator over all the resources in a resource manager.

---
 drivers/gpu/drm/ttm/ttm_bo.c       | 41 +++++++++++----------------
 drivers/gpu/drm/ttm/ttm_device.c   | 37 ++++++++++++------------
 drivers/gpu/drm/ttm/ttm_resource.c | 45 ++++++++++++++++++++++++++++++
 include/drm/ttm/ttm_resource.h     | 23 +++++++++++++++
 4 files changed, 102 insertions(+), 44 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +16 -25
index 09a62ad06b9d..98ad0b819deb 100644
@@ -585,38 +585,29 @@ int ttm_mem_evict_first(struct ttm_device *bdev,
 			struct ww_acquire_ctx *ticket)
 {
 	struct ttm_buffer_object *bo = NULL, *busy_bo = NULL;
+	struct ttm_resource_cursor cursor;
 	struct ttm_resource *res;
 	bool locked = false;
-	unsigned i;
 	int ret;
 
 	spin_lock(&bdev->lru_lock);
-	for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
-		list_for_each_entry(res, &man->lru[i], lru) {
-			bool busy;
-
-			bo = res->bo;
-			if (!ttm_bo_evict_swapout_allowable(bo, ctx, place,
-							    &locked, &busy)) {
-				if (busy && !busy_bo && ticket !=
-				    dma_resv_locking_ctx(bo->base.resv))
-					busy_bo = bo;
-				continue;
-			}
-
-			if (!ttm_bo_get_unless_zero(bo)) {
-				if (locked)
-					dma_resv_unlock(bo->base.resv);
-				continue;
-			}
-			break;
+	ttm_resource_manager_for_each_res(man, &cursor, res) {
+		bool busy;
+
+		if (!ttm_bo_evict_swapout_allowable(res->bo, ctx, place,
+						    &locked, &busy)) {
+			if (busy && !busy_bo && ticket !=
+			    dma_resv_locking_ctx(bo->base.resv))
+				busy_bo = res->bo;
+			continue;
 		}
 
-		/* If the inner loop terminated early, we have our candidate */
-		if (&res->lru != &man->lru[i])
-			break;
-
-		bo = NULL;
+		if (!ttm_bo_get_unless_zero(res->bo)) {
+			if (locked)
+				dma_resv_unlock(res->bo->base.resv);
+			continue;
+		}
+		bo = res->bo;
 	}
 
 	if (!bo) {
drivers/gpu/drm/ttm/ttm_device.c +18 -19
index a6e533c3ac56..8225575d2d0f 100644
@@ -130,10 +130,11 @@ EXPORT_SYMBOL(ttm_global_swapout);
 int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
 		       gfp_t gfp_flags)
 {
+	struct ttm_resource_cursor cursor;
 	struct ttm_resource_manager *man;
 	struct ttm_buffer_object *bo;
 	struct ttm_resource *res;
-	unsigned i, j;
+	unsigned i;
 	int ret;
 
 	spin_lock(&bdev->lru_lock);
@@ -142,24 +143,22 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
 		if (!man || !man->use_tt)
 			continue;
 
-		for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) {
-			list_for_each_entry(res, &man->lru[j], lru) {
-				uint32_t num_pages;
-
-				bo = res->bo;
-				if (!bo->ttm ||
-				    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
-				    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)
-					continue;
-
-				num_pages = bo->ttm->num_pages;
-				ret = ttm_bo_swapout(bo, ctx, gfp_flags);
-				/* ttm_bo_swapout has dropped the lru_lock */
-				if (!ret)
-					return num_pages;
-				if (ret != -EBUSY)
-					return ret;
-			}
+		ttm_resource_manager_for_each_res(man, &cursor, res) {
+			uint32_t num_pages;
+
+			bo = res->bo;
+			if (!bo->ttm ||
+			    bo->ttm->page_flags & TTM_PAGE_FLAG_SG ||
+			    bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)
+				continue;
+
+			num_pages = bo->ttm->num_pages;
+			ret = ttm_bo_swapout(bo, ctx, gfp_flags);
+			/* ttm_bo_swapout has dropped the lru_lock */
+			if (!ret)
+				return num_pages;
+			if (ret != -EBUSY)
+				return ret;
 		}
 	}
 	spin_unlock(&bdev->lru_lock);
drivers/gpu/drm/ttm/ttm_resource.c +45 -0

51 lines not on this page · raw message

include/drm/ttm/ttm_resource.h +23 -0
index 500711415ae9..a42cdb94fd9f 100644
@@ -183,6 +183,17 @@ struct ttm_resource {
 	struct list_head lru;
 };
 
+/**
+ * struct ttm_resource_cursor
+ *
+ * @priority: the current priority
+ *
+ * Cursor to iterate over the resources in a manager.
+ */
+struct ttm_resource_cursor {
+	unsigned int priority;
+};
+
 /**
  * struct ttm_lru_bulk_move_pos
  *
@@ -334,6 +345,18 @@ int ttm_resource_manager_evict_all(struct ttm_device *bdev,
 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
 				struct drm_printer *p);
 
+struct ttm_resource *
+ttm_resource_manager_first(struct ttm_resource_manager *man,
+			   struct ttm_resource_cursor *cursor);
+struct ttm_resource *
+ttm_resource_manager_next(struct ttm_resource_manager *man,
+			  struct ttm_resource_cursor *cursor,
+			  struct ttm_resource *res);
+
+#define ttm_resource_manager_for_each_res(man, cursor, res)		\
+	for (res = ttm_resource_manager_first(man, cursor); res;	\
+	     res = ttm_resource_manager_next(man, cursor, res))
+
 struct ttm_kmap_iter *
 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
 			 struct io_mapping *iomap,
Signature · 1 line
2.25.1
#6 lore raw
In reply to Christian König

Hi, Christian,

Still working through some backlog and this series appears to have slipped under the radar.

Still I think a cover letter would benefit reviewers...

Quoted 2 lines · Christian König

Keep track for which BO a resource was allocated. This is necessary to move the LRU handling into the resources.

So is this needed, when looking up a resource from the LRU, to find the bo the resource is currently attached to?

Quoted 3 lines

A bit problematic is i915 since it tries to use the resource interface without a BO which is illegal from the conceptional

s/conceptional/conceptual/ ?

Quoted 4 lines

point of view.

v2: Document that this is a weak reference and add a workaround for i915

Hmm. As pointed out in my previous review the weak pointer should really be cleared under a lookup lock to avoid use-after-free bugs. I don't see that happening here. Doesn't this series aim for a future direction of early destruction of bos and ditching the ghost objects? In that case IMHO you need to allow for a NULL bo pointer and any bo information needed at resource destruction needs to be copied on the resource... (That would also ofc help with the i915 problem).

Quoted 45 lines

---
 drivers/gpu/drm/i915/intel_region_ttm.c | 5 +++++ drivers/gpu/drm/ttm/ttm_bo_util.c       | 7 +++++-- drivers/gpu/drm/ttm/ttm_resource.c      | 1 + include/drm/ttm/ttm_resource.h          | 2 ++

 4 files changed, 13 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/intel_region_ttm.c +1 -0
index 27fe0668d094..980b10a7debf 100644
@@ -88,6 +88,7 @@ intel_region_ttm_node_reserve(struct
 intel_memory_region *mem,
         place.fpfn = offset >> PAGE_SHIFT;
         place.lpfn = place.fpfn + (size >> PAGE_SHIFT);
         mock_bo.base.size = size;
+       mock_bo.bdev = &mem->i915->bdev;
         ret = man->func->alloc(man, &mock_bo, &place, &res);
         if (ret == -ENOSPC)

                ret = -ENXIO;

drivers/gpu/drm/i915/intel_region_ttm.c +1 -0
@@ -104,7 +105,11 @@ void intel_region_ttm_node_free(struct
 intel_memory_region *mem,
                                 struct ttm_resource *res)
  {
         struct ttm_resource_manager *man = mem->region_private;
+       struct ttm_buffer_object mock_bo = {};


+       mock_bo.base.size = res->num_pages * PAGE_SIZE; +       mock_bo.bdev = &mem->i915->bdev;
+       res->bo = &mock_bo; man->func->free(man, res);
 }

drivers/gpu/drm/ttm/ttm_bo_util.c +0 -0
index 2f57f824e6db..a1570aa8ff56 100644
@@ -239,6 +239,11 @@ static int ttm_buffer_object_transfer(struct
 ttm_buffer_object *bo,
         if (bo->type != ttm_bo_type_sg)
                 fbo->base.base.resv = &fbo->base.base._resv;
Quoted 3 lines


+       if (fbo->base.resource) {
+               fbo->base.resource->bo = &fbo->base;

This is scary: What if someone else (LRU lookup) just dereferenced the previous resource->bo pointer? I think this needs proper weak reference locking and lifetime handling, as mentioned above.

Quoted 3 lines

+               bo->resource = NULL;
+       }
+

/Thomas

#7 lore raw

Hi Thomas,

Am 23.08.21 um 09:56 schrieb Thomas Hellström:

Quoted 5 lines

Hi, Christian,

Still working through some backlog and this series appears to have slipped under the radar.

Quoted 7 lines

Still I think a cover letter would benefit reviewers...

Quoted 2 lines · Christian König

Keep track for which BO a resource was allocated. This is necessary to move the LRU handling into the resources.

So is this needed, when looking up a resource from the LRU, to find the bo the resource is currently attached to?

yes, correct. The main motivation is to fix resource handling during allocations and moves.

Essentially we currently have the following steps during resource allocation:
1. Locking the BO.
2. Figuring out we need resources.
2. Allocating the resource.
3. Moving the BO to the correct LRU.
4. Unlocking the BO.

The problem is now that we have a short time where we allocated the resource, but can't evict it again. In other words we don't know to which object a resource belongs.

Buffer moves are even worse since we have an old resource as well as a new one at the same time and so potentially would need the BO on two LRUs at the same time.

This has caused a whole bunch of problems in the past because we ran into out of resource situations and implemented quite a number of workarounds for this.

Quoted 7 lines
Quoted 2 lines

A bit problematic is i915 since it tries to use the resource interface without a BO which is illegal from the conceptional

s/conceptional/conceptual/ ?

Quoted 4 lines

point of view.

v2: Document that this is a weak reference and add a workaround for i915

Quoted 7 lines

Hmm. As pointed out in my previous review the weak pointer should really be cleared under a lookup lock to avoid use-after-free bugs. I don't see that happening here. Doesn't this series aim for a future direction of early destruction of bos and ditching the ghost objects? In that case IMHO you need to allow for a NULL bo pointer and any bo information needed at resource destruction needs to be copied on the resource... (That would also ofc help with the i915 problem).

Yes, I'm going back and force how to do this cleanly as well.

Ok going to give the NULL pointer a try.

Quoted 46 lines
Quoted 46 lines
---
  drivers/gpu/drm/i915/intel_region_ttm.c | 5 +++++
  drivers/gpu/drm/ttm/ttm_bo_util.c       | 7 +++++--
  drivers/gpu/drm/ttm/ttm_resource.c      | 1 +
  include/drm/ttm/ttm_resource.h          | 2 ++
  4 files changed, 13 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/intel_region_ttm.c +1 -0
index 27fe0668d094..980b10a7debf 100644
@@ -88,6 +88,7 @@ intel_region_ttm_node_reserve(struct
 intel_memory_region *mem,
         place.fpfn = offset >> PAGE_SHIFT;
         place.lpfn = place.fpfn + (size >> PAGE_SHIFT);
         mock_bo.base.size = size;
+       mock_bo.bdev = &mem->i915->bdev;
         ret = man->func->alloc(man, &mock_bo, &place, &res);
         if (ret == -ENOSPC)

                ret = -ENXIO;

drivers/gpu/drm/i915/intel_region_ttm.c +4 -0
@@ -104,7 +105,11 @@ void intel_region_ttm_node_free(struct
 intel_memory_region *mem,
                                 struct ttm_resource *res)
  {
         struct ttm_resource_manager *man = mem->region_private;
+       struct ttm_buffer_object mock_bo = {};
 
+       mock_bo.base.size = res->num_pages * PAGE_SIZE;
+       mock_bo.bdev = &mem->i915->bdev;
+       res->bo = &mock_bo;
         man->func->free(man, res);
  }
drivers/gpu/drm/ttm/ttm_bo_util.c +0 -0
index 2f57f824e6db..a1570aa8ff56 100644
@@ -239,6 +239,11 @@ static int ttm_buffer_object_transfer(struct
 ttm_buffer_object *bo,
         if (bo->type != ttm_bo_type_sg)
                 fbo->base.base.resv = &fbo->base.base._resv;
 
Quoted 5 lines

drivers/gpu/drm/ttm/ttm_bo_util.c static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo, · line 243

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_bo_util.c +2 -0
+       if (fbo->base.resource) {
+               fbo->base.resource->bo = &fbo->base;

This is scary: What if someone else (LRU lookup) just dereferenced the previous resource->bo pointer? I think this needs proper weak reference locking and lifetime handling, as mentioned above.

Ah, yes good point. Missed this occasion.

Thanks,
Christian.

Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo_util.c static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo, · line 246

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_bo_util.c +3 -0
+               bo->resource = NULL;
+       }
+

/Thomas