drm/ttm: add a pointer to the allocating BO into ttm_resource
Subsystems (6)
PATCH 1/4 drm/ttm: add a pointer to the allocating BO into ttm_resource
We are going to need this for the next patch and it allows us to clean up amdgpu as well.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 47 ++++++++------------- drivers/gpu/drm/ttm/ttm_resource.c | 1 + include/drm/ttm/ttm_resource.h | 1 + 3 files changed, 19 insertions(+), 30 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +17 -30
@@ -26,23 +26,12 @@#include "amdgpu.h" -struct amdgpu_gtt_node { -struct ttm_buffer_object *tbo; -struct ttm_range_mgr_node base; -}; -static inline struct amdgpu_gtt_mgr *to_gtt_mgr (struct ttm_resource_manager *man) { return container_of(man, struct amdgpu_gtt_mgr, manager); } -static inline struct amdgpu_gtt_node * -to_amdgpu_gtt_node (struct ttm_resource *res) -{ - return container_of(res, struct amdgpu_gtt_node, base.base); -} -/** * DOC: mem_info_gtt_total *
@@ -107,9 +96,9 @@ const struct attribute_group amdgpu_gtt_mgr_attr_group = {*/ bool amdgpu_gtt_mgr_has_gart_addr (struct ttm_resource *res) { -struct amdgpu_gtt_node *node =to_amdgpu_gtt_node (res); +struct ttm_range_mgr_node *node =to_ttm_range_mgr_node (res); -return drm_mm_node_allocated (&node->base.mm_nodes[0 ]); +return drm_mm_node_allocated (&node->mm_nodes[0 ]); }/**
@@ -129,7 +118,7 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man, {struct amdgpu_gtt_mgr *mgr =to_gtt_mgr (man);uint32_t num_pages =PFN_UP (tbo->base.size); -struct amdgpu_gtt_node *node; +struct ttm_range_mgr_node *node;int r;spin_lock (&mgr->lock);
@@ -141,19 +130,17 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,atomic64_sub (num_pages, &mgr->available);spin_unlock (&mgr->lock); - node =kzalloc (struct_size (node, base.mm_nodes,1 ),GFP_KERNEL ); + node =kzalloc (struct_size (node, mm_nodes,1 ),GFP_KERNEL );if (!node) { r = -ENOMEM ; goto err_out; } - node->tbo = tbo; -ttm_resource_init (tbo, place, &node->base.base); - +ttm_resource_init (tbo, place, &node->base);if (place->lpfn) {spin_lock (&mgr->lock); r =drm_mm_insert_node_in_range (&mgr->mm, - &node->base.mm_nodes[0 ], + &node->mm_nodes[0 ], num_pages, tbo->page_alignment,0 , place->fpfn, place->lpfn,DRM_MM_INSERT_BEST );
@@ -161,14 +148,14 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,if (unlikely (r)) goto err_free; - node->base.base.start = node->base.mm_nodes[0 ].start; + node->base.start = node->mm_nodes[0 ].start; }else { - node->base.mm_nodes[0 ].start =0 ; - node->base.mm_nodes[0 ].size = node->base.base.num_pages; - node->base.base.start =AMDGPU_BO_INVALID_OFFSET ; + node->mm_nodes[0 ].start =0 ; + node->mm_nodes[0 ].size = node->base.num_pages; + node->base.start =AMDGPU_BO_INVALID_OFFSET ; } - *res = &node->base.base; + *res = &node->base;return 0 ; err_free:
@@ -191,12 +178,12 @@ 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 *res) { -struct amdgpu_gtt_node *node =to_amdgpu_gtt_node (res); +struct ttm_range_mgr_node *node =to_ttm_range_mgr_node (res);struct amdgpu_gtt_mgr *mgr =to_gtt_mgr (man);spin_lock (&mgr->lock); -if (drm_mm_node_allocated (&node->base.mm_nodes[0 ])) -drm_mm_remove_node (&node->base.mm_nodes[0 ]); +if (drm_mm_node_allocated (&node->mm_nodes[0 ])) +drm_mm_remove_node (&node->mm_nodes[0 ]);spin_unlock (&mgr->lock);atomic64_add (res->num_pages, &mgr->available);
@@ -228,14 +215,14 @@ uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)int amdgpu_gtt_mgr_recover (struct ttm_resource_manager *man) {struct amdgpu_gtt_mgr *mgr =to_gtt_mgr (man); -struct amdgpu_gtt_node *node; +struct ttm_range_mgr_node *node;struct drm_mm_node *mm_node;int r =0 ;spin_lock (&mgr->lock);drm_mm_for_each_node (mm_node, &mgr->mm) { - node =container_of (mm_node,typeof (*node), base.mm_nodes[0 ]); - r =amdgpu_ttm_recover_gart (node->tbo); + node =container_of (mm_node,typeof (*node), mm_nodes[0 ]); + r =amdgpu_ttm_recover_gart (node->base.bo);if (r)break ; }
drivers/gpu/drm/ttm/ttm_resource.c +1 -0
@@ -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 +1 -0
@@ -171,6 +171,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
PATCH 2/4 drm/ttm: add ttm_resource_fini
For now that function is just a stub.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 1 + drivers/gpu/drm/nouveau/nouveau_ttm.c | 1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 1 + drivers/gpu/drm/ttm/ttm_resource.c | 5 +++++ drivers/gpu/drm/ttm/ttm_sys_manager.c | 1 + drivers/gpu/drm/vmwgfx/vmwgfx_thp.c | 1 + include/drm/ttm/ttm_resource.h | 2 ++ 8 files changed, 13 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +1 -0
@@ -187,6 +187,7 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,spin_unlock (&mgr->lock);atomic64_add (res->num_pages, &mgr->available); +ttm_resource_fini (&node->base);kfree (node); }
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -0
@@ -510,6 +510,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 (&node->base);kvfree (node); }
drivers/gpu/drm/nouveau/nouveau_ttm.c +1 -0
@@ -38,6 +38,7 @@static void nouveau_manager_del (struct ttm_resource_manager *man,struct ttm_resource *reg) { +ttm_resource_fini (reg);nouveau_mem_del (reg); }
drivers/gpu/drm/ttm/ttm_range_manager.c +1 -0
@@ -108,6 +108,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 (&node->base);kfree (node); }
drivers/gpu/drm/ttm/ttm_resource.c +5 -0
@@ -45,6 +45,11 @@ void ttm_resource_init(struct ttm_buffer_object *bo, }EXPORT_SYMBOL (ttm_resource_init); +void ttm_resource_fini (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
@@ -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 (res);kfree (res); }
drivers/gpu/drm/vmwgfx/vmwgfx_thp.c +1 -0
@@ -123,6 +123,7 @@ static void vmw_thp_put_node(struct ttm_resource_manager *man,drm_mm_remove_node (&node->mm_nodes[0 ]);spin_unlock (&rman->lock); +ttm_resource_fini (&node->base);kfree (node); }
include/drm/ttm/ttm_resource.h +2 -0
@@ -263,6 +263,8 @@ 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 *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
Re: [PATCH 2/4] drm/ttm: add ttm_resource_fini
Ah, crap forget this patch. I wanted to squash it into the next one.
Am 10.06.21 um 13:05 schrieb Christian König:
Quoted 115 lines
For now that function is just a stub.
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 1 + drivers/gpu/drm/nouveau/nouveau_ttm.c | 1 + drivers/gpu/drm/ttm/ttm_range_manager.c | 1 + drivers/gpu/drm/ttm/ttm_resource.c | 5 +++++ drivers/gpu/drm/ttm/ttm_sys_manager.c | 1 + drivers/gpu/drm/vmwgfx/vmwgfx_thp.c | 1 + include/drm/ttm/ttm_resource.h | 2 ++ 8 files changed, 13 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +1
-0@@ -187,6 +187,7 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,spin_unlock (&mgr->lock);atomic64_add (res->num_pages, &mgr->available); +ttm_resource_fini (&node->base);kfree (node); }drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1
-0@@ -510,6 +510,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 (&node->base);kvfree (node); }drivers/gpu/drm/nouveau/nouveau_ttm.c +1
-0@@ -38,6 +38,7 @@static void nouveau_manager_del (struct ttm_resource_manager *man,struct ttm_resource *reg) { +ttm_resource_fini (reg);nouveau_mem_del (reg); }drivers/gpu/drm/ttm/ttm_range_manager.c +1
-0@@ -108,6 +108,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 (&node->base);kfree (node); }drivers/gpu/drm/ttm/ttm_resource.c +5
-0@@ -45,6 +45,11 @@ void ttm_resource_init(struct ttm_buffer_object *bo, }EXPORT_SYMBOL (ttm_resource_init); +void ttm_resource_fini (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@@ -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 (res);kfree (res); }drivers/gpu/drm/vmwgfx/vmwgfx_thp.c +1
-0@@ -123,6 +123,7 @@ static void vmw_thp_put_node(struct ttm_resource_manager *man,drm_mm_remove_node (&node->mm_nodes[0 ]);spin_unlock (&rman->lock); +ttm_resource_fini (&node->base);kfree (node); }include/drm/ttm/ttm_resource.h +2
-0@@ -263,6 +263,8 @@ 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 *res); +int ttm_resource_alloc (struct ttm_buffer_object *bo,const struct ttm_place *place,struct ttm_resource **res);
PATCH 3/4 drm/ttm: move the LRU into resource handling
In reply to Christian KönigThis 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/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 | 119 +++++++++++++++++++++++++ include/drm/ttm/ttm_bo_api.h | 16 ---- include/drm/ttm/ttm_bo_driver.h | 29 +----- include/drm/ttm/ttm_resource.h | 35 ++++++++ 8 files changed, 168 insertions(+), 145 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -5
@@ -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_bulk_move_lru_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_bulk_move_init (&vm->lru_bulk_move);spin_lock (&adev->mman.bdev.lru_lock);list_for_each_entry (bo_base, &vm->idle, vm_status) {
@@ -657,11 +657,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 (bo->shadow)ttm_bo_move_to_lru_tail (&bo->shadow->tbo, - bo->shadow->tbo.resource, &vm->lru_bulk_move); }spin_unlock (&adev->mman.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
@@ -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
@@ -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 +119 -0
136 lines not on this page · raw message
include/drm/ttm/ttm_bo_api.h +0 -16
@@ -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
@@ -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
@@ -26,9 +26,11 @@#define _TTM_RESOURCE_H_#include <linux/types.h> +#include <linux/list.h> #include <linux/mutex.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>
@@ -172,6 +174,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 ]; };/**
@@ -260,6 +289,12 @@ ttm_resource_manager_cleanup(struct ttm_resource_manager *man) man->move =NULL ; } +void ttm_bulk_move_init (struct ttm_lru_bulk_move *bulk); +void ttm_bulk_move_lru_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
PATCH 4/4 drm/ttm: add resource iterator
In reply to Christian KönigInstead 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 | 29 +++++++++++++++++++++ include/drm/ttm/ttm_resource.h | 23 +++++++++++++++++ 4 files changed, 86 insertions(+), 44 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +16 -25
@@ -555,38 +555,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
@@ -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 +29 -0
@@ -284,6 +284,35 @@ void ttm_resource_manager_debug(struct ttm_resource_manager *man, }EXPORT_SYMBOL (ttm_resource_manager_debug); +struct ttm_resource * +ttm_resource_manager_first (struct ttm_resource_manager *man, +struct ttm_resource_cursor *cursor) +{ +struct ttm_resource *res; + +for (cursor->priority =0 ; cursor->priority <TTM_MAX_BO_PRIORITY ; + ++cursor->priority) +list_for_each_entry (res, &man->lru[cursor->priority], lru) +return res; + +return NULL ; +} + +struct ttm_resource * +ttm_resource_manager_next (struct ttm_resource_manager *man, +struct ttm_resource_cursor *cursor, +struct ttm_resource *res) +{ +list_for_each_entry_continue (res, &man->lru[cursor->priority], lru) +return res; + +for (; cursor->priority <TTM_MAX_BO_PRIORITY ; ++cursor->priority) +list_for_each_entry (res, &man->lru[cursor->priority], lru) +return res; + +return NULL ; +} +static void ttm_kmap_iter_iomap_map_local (struct ttm_kmap_iter *iter,struct dma_buf_map *dmap,pgoff_t i)
include/drm/ttm/ttm_resource.h +23 -0
@@ -177,6 +177,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 *
@@ -314,6 +325,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
Hi, Christian,
I know you have a lot on your plate, and that the drm community is a bit lax about following the kernel patch submitting guidelines, but now that we're also spinning up a number of Intel developers on TTM could we please make a better effort with cover letters and commit messages so that they understand what the purpose and end goal of the series is. A reviewer shouldn't have to look at the last patch to try to get an understanding what the series is doing and why.
Quoted 5 lines · Christian König
We are going to need this for the next patch
and it allows us to clean
up amdgpu as well.
The amdgpu changes are not reflected in the commit title.
Quoted 151 lines
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 47 ++++++++------------- drivers/gpu/drm/ttm/ttm_resource.c | 1 + include/drm/ttm/ttm_resource.h | 1 + 3 files changed, 19 insertions(+), 30 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +17
-30@@ -26,23 +26,12 @@#include "amdgpu.h" -struct amdgpu_gtt_node { -struct ttm_buffer_object *tbo; -struct ttm_range_mgr_node base; -}; -static inline struct amdgpu_gtt_mgr *to_gtt_mgr (struct ttm_resource_manager *man) { return container_of(man, struct amdgpu_gtt_mgr, manager); } -static inline struct amdgpu_gtt_node * -to_amdgpu_gtt_node (struct ttm_resource *res) -{ - return container_of(res, struct amdgpu_gtt_node, base.base); -} -/** * DOC: mem_info_gtt_total * @@ -107,9 +96,9 @@ const struct attribute_group amdgpu_gtt_mgr_attr_group = {*/ bool amdgpu_gtt_mgr_has_gart_addr (struct ttm_resource *res) { -struct amdgpu_gtt_node *node =to_amdgpu_gtt_node (res); +struct ttm_range_mgr_node *node =to_ttm_range_mgr_node (res); -return drm_mm_node_allocated (&node->base.mm_nodes[0 ]); +return drm_mm_node_allocated (&node->mm_nodes[0 ]); }/** @@ -129,7 +118,7 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man, {struct amdgpu_gtt_mgr *mgr =to_gtt_mgr (man);uint32_t num_pages =PFN_UP (tbo->base.size); -struct amdgpu_gtt_node *node; +struct ttm_range_mgr_node *node;int r;spin_lock (&mgr->lock);@@ -141,19 +130,17 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,atomic64_sub (num_pages, &mgr->available);spin_unlock (&mgr->lock); - node =kzalloc (struct_size (node, base.mm_nodes,1 ),GFP_KERNEL ); + node =kzalloc (struct_size (node, mm_nodes,1 ),GFP_KERNEL );if (!node) { r = -ENOMEM ; goto err_out; } - node->tbo = tbo; -ttm_resource_init (tbo, place, &node->base.base); - +ttm_resource_init (tbo, place, &node->base);if (place->lpfn) {spin_lock (&mgr->lock); r =drm_mm_insert_node_in_range (&mgr->mm, - &node->base.mm_nodes[0 ], + &node->mm_nodes[0 ], num_pages, tbo->page_alignment,0 , place->fpfn, place->lpfn,DRM_MM_INSERT_BEST );@@ -161,14 +148,14 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,if (unlikely (r)) goto err_free; - node->base.base.start = node->base.mm_nodes[0 ].start; + node->base.start = node->mm_nodes[0 ].start; }else { - node->base.mm_nodes[0 ].start =0 ; - node->base.mm_nodes[0 ].size = node->base.base.num_pages; - node->base.base.start =AMDGPU_BO_INVALID_OFFSET ; + node->mm_nodes[0 ].start =0 ; + node->mm_nodes[0 ].size = node->base.num_pages; + node->base.start =AMDGPU_BO_INVALID_OFFSET ; } - *res = &node->base.base; + *res = &node->base;return 0 ; err_free:@@ -191,12 +178,12 @@ 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 *res) { -struct amdgpu_gtt_node *node =to_amdgpu_gtt_node (res); +struct ttm_range_mgr_node *node =to_ttm_range_mgr_node (res);struct amdgpu_gtt_mgr *mgr =to_gtt_mgr (man);spin_lock (&mgr->lock); -if (drm_mm_node_allocated (&node->base.mm_nodes[0 ])) -drm_mm_remove_node (&node->base.mm_nodes[0 ]); +if (drm_mm_node_allocated (&node->mm_nodes[0 ])) +drm_mm_remove_node (&node->mm_nodes[0 ]);spin_unlock (&mgr->lock);atomic64_add (res->num_pages, &mgr->available);@@ -228,14 +215,14 @@ uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)int amdgpu_gtt_mgr_recover (struct ttm_resource_manager *man) {struct amdgpu_gtt_mgr *mgr =to_gtt_mgr (man); -struct amdgpu_gtt_node *node; +struct ttm_range_mgr_node *node;struct drm_mm_node *mm_node;int r =0 ;spin_lock (&mgr->lock);drm_mm_for_each_node (mm_node, &mgr->mm) { - node =container_of (mm_node,typeof (*node), base.mm_nodes[0 ]); - r =amdgpu_ttm_recover_gart (node->tbo); + node =container_of (mm_node,typeof (*node), mm_nodes[0 ]); + r =amdgpu_ttm_recover_gart (node->base.bo);if (r)break ; }drivers/gpu/drm/ttm/ttm_resource.c +1
-0@@ -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 +0
-0
include/drm/ttm/ttm_resource.h struct ttm_resource { · line 174
Quoted 4 lines
include/drm/ttm/ttm_resource.h +1
-0@@ -171,6 +171,7 @@ struct ttm_resource {uint32_t mem_type;uint32_t placement;struct ttm_bus_placement bus; +struct ttm_buffer_object *bo;
Not that I'm against this change by itself, but this bo pointer is not refcounted, and therefore needs a description when it's needed and why. What happens, for example when the resource is moved to a ghost object, or the bo is killed while the resource is remaining on a lru list (which I understand was one of the main purposes with free-standing resources). Weak references need a guarantee that the object they pointed to is alive. What is that guarantee?
Also could we introduce new TTM structure members where they are first used /referenced by TTM and not where they are used by amdgpu? Without finding out in patch 3 that this member is needed to look up the bo from a lru list the correct response to this patch would have been: That bo is amdgpu-specific and needs to be in a driver private struct...
Thanks,
/Thomas
Quoted 3 lines
include/drm/ttm/ttm_resource.h +0
-0};/**
Am 11.06.21 um 07:34 schrieb Thomas Hellström (Intel):
Quoted 3 lines
Hi, Christian,
I know you have a lot on your plate, and that the drm community is a bit lax about following the kernel patch submitting guidelines, but now that we're also spinning up a number of Intel developers on TTM could we please make a better effort with cover letters and commit messages so that they understand what the purpose and end goal of the series is. A reviewer shouldn't have to look at the last patch to try to get an understanding what the series is doing and why.
Sorry, that was send out this early unintentionally. See it more like an RFC.
Quoted 150 lines · Christian König
Quoted 5 lines
We are going to need this for the next patch
and it allows us to clean
up amdgpu as well.The amdgpu changes are not reflected in the commit title.
Quoted 139 lines
Signed-off-by Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c| 47 ++++++++------------- drivers/gpu/drm/ttm/ttm_resource.c | 1 + include/drm/ttm/ttm_resource.h | 1 +3 files changed, 19 insertions(+), 30 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +0
-4@@ -26,23 +26,12 @@ #include "amdgpu.h" -struct amdgpu_gtt_node { - struct ttm_buffer_object *tbo; - struct ttm_range_mgr_node base; -}; -static inline struct
amdgpu_gtt_mgr*to_gtt_mgr(struct ttm_resource_manager *man)
{
returncontainer_of(man, struct amdgpu_gtt_mgr, manager); }
-static inline structamdgpu_gtt_node*drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +0
-5-to_amdgpu_gtt_node(struct ttm_resource *res) -{ - return container_of(res, struct amdgpu_gtt_node, base.base); -} -/**
* DOC:mem_info_gtt_total
*drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +6
-5@@ -107,9 +96,9 @@ const struct attribute_groupamdgpu_gtt_mgr_attr_group = { */ bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *res) { - struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(res); + struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); - return drm_mm_node_allocated(&node->base.mm_nodes[0]); + return drm_mm_node_allocated(&node->mm_nodes[0]); } /**@@ -129,7 +118,7 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man, { struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); uint32_t num_pages = PFN_UP(tbo->base.size); - struct amdgpu_gtt_node *node; + struct ttm_range_mgr_node *node; int r; spin_lock(&mgr->lock);@@ -141,19 +130,17 @@ static int amdgpu_gtt_mgr_new(structttm_resource_manager *man, atomic64_sub(num_pages, &mgr->available); spin_unlock(&mgr->lock); - node = kzalloc(struct_size(node, base.mm_nodes, 1), GFP_KERNEL); + node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL); if (!node) { r = -ENOMEM; goto err_out; } - node->tbo = tbo; - ttm_resource_init(tbo, place, &node->base.base); - + ttm_resource_init(tbo, place, &node->base); if (place->lpfn) { spin_lock(&mgr->lock); r = drm_mm_insert_node_in_range(&mgr->mm, - &node->base.mm_nodes[0], + &node->mm_nodes[0], num_pages, tbo->page_alignment, 0, place->fpfn, place->lpfn,
DRM_MM_INSERT_BEST);drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +5
-3@@ -161,14 +148,14 @@ static int amdgpu_gtt_mgr_new(structttm_resource_manager *man, if (unlikely(r)) goto err_free; - node->base.base.start = node->base.mm_nodes[0].start; + node->base.start = node->mm_nodes[0].start; } else { - node->base.mm_nodes[0].start = 0; - node->base.mm_nodes[0].size = node->base.base.num_pages; - node->base.base.start = AMDGPU_BO_INVALID_OFFSET; + node->mm_nodes[0].start = 0; + node->mm_nodes[0].size = node->base.num_pages; + node->base.start = AMDGPU_BO_INVALID_OFFSET; } - *res = &node->base.base; + *res = &node->base; return 0;
err_free:drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +3
-3@@ -191,12 +178,12 @@ static int amdgpu_gtt_mgr_new(structttm_resource_manager *man, static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man, struct ttm_resource *res) { - struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(res); + struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); spin_lock(&mgr->lock); - if (drm_mm_node_allocated(&node->base.mm_nodes[0])) - drm_mm_remove_node(&node->base.mm_nodes[0]); + if (drm_mm_node_allocated(&node->mm_nodes[0])) + drm_mm_remove_node(&node->mm_nodes[0]); spin_unlock(&mgr->lock); atomic64_add(res->num_pages, &mgr->available); @@ -228,14 +215,14 @@ uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man)int
amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man){
structamdgpu_gtt_mgr*mgr =to_gtt_mgr(man);
- structamdgpu_gtt_node*node;
+ structttm_range_mgr_node*node;
structdrm_mm_node*mm_node;
int r = 0;
spin_lock(&mgr->lock);
drm_mm_for_each_node(mm_node, &mgr->mm){
- node =container_of(mm_node, typeof(*node), base.mm_nodes[0]); - r =amdgpu_ttm_recover_gart(node->tbo);
+ node =container_of(mm_node, typeof(*node), mm_nodes[0]); + r =amdgpu_ttm_recover_gart(node->base.bo);
if (r)
break;
}drivers/gpu/drm/ttm/ttm_resource.c +1
-0@@ -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);diff --git
a/include/drm/ttm/ttm_resource.hb/include/drm/ttm/ttm_resource.hindex140b6b9a8bbe..6d0b7a6d2169100644
---a/include/drm/ttm/ttm_resource.h
Quoted 8 lines
drivers/gpu/drm/ttm/ttm_resource.c struct ttm_resource { · line 174
Quoted 6 lines
+++
b/include/drm/ttm/ttm_resource.hdrivers/gpu/drm/ttm/ttm_resource.c +1
-0@@ -171,6 +171,7 @@ struct ttm_resource { uint32_t mem_type; uint32_t placement; struct ttm_bus_placement bus; + struct ttm_buffer_object *bo;Not that I'm against this change by itself, but this bo pointer is not refcounted, and therefore needs a description when it's needed and why. What happens, for example when the resource is moved to a ghost object, or the bo is killed while the resource is remaining on a lru list (which I understand was one of the main purposes with free-standing resources). Weak references need a guarantee that the object they pointed to is alive. What is that guarantee?
The basic idea is that we want to get rid of ghost objects and all the related workarounds in the mid term. But yes for the interim we probably need more logic here to make sure the BO is not destroyed.
Quoted 2 lines
Also could we introduce new TTM structure members where they are first used /referenced by TTM and not where they are used by amdgpu? Without finding out in patch 3 that this member is needed to look up the bo from a lru list the correct response to this patch would have been: That bo is amdgpu-specific and needs to be in a driver private struct...
That was indeed not supposed like this.
The question I rather wanted to raise if it's ok to make the resource object fully depend on the BO for allocation/destruction?
I've seen some code in the DG1 branch where a mock BO is used to allocate some resource, but that approach won't work with this here.
On the other hand this fixes a long outstanding and very annoying problem we had.
Christian.
Quoted 9 lines
Thanks,
/Thomas
Quoted 2 lines
drivers/gpu/drm/ttm/ttm_resource.c +0
-0}; /**