Some refactoring and maybe a memory accounting fixlet
Subsystems (2)
cover Some refactoring and maybe a memory accounting fixlet
From: Tvrtko Ursulin <[email protected]>
As I was reading through the driver I spotted one thing which could perhaps make it more readable (1/3), one thing which reduces some double conversions (in principle) from TTM placement back to domain (2/3), and also enables the last patch in the series which maybe fixes a small memory accounting issue. But only relevant when interacting with KFD IIUC.
Assuming the fix is real and not my confusion, please let me know if it is all acceptable, or you would like me to re-structure the series to only contain the fix and break off the rest for later (or never).
Tvrtko Ursulin (3):
drm/amdgpu: Add amdgpu_bo_is_vm_bo helper
drm/amdgpu: Reduce mem_type to domain double indirection drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 31 ++++++++++----------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 14 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 31 +++++++++------------ 5 files changed, 44 insertions(+), 38 deletions(-)
Signature · 1 line
2.44.0
PATCH 1/3 drm/amdgpu: Add amdgpu_bo_is_vm_bo helper
From: Tvrtko Ursulin <[email protected]>
Help code readability by replacing a bunch of:
bo->tbo.base.resv == vm->root.bo->tbo.base.resv
With:
amdgpu_bo_is_vm_bo(bo, vm)
No functional changes.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 14 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 31 +++++++++------------- 3 files changed, 28 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
@@ -174,7 +174,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,return -EPERM ;if (abo->flags &AMDGPU_GEM_CREATE_VM_ALWAYS_VALID && - abo->tbo.base.resv != vm->root.bo->tbo.base.resv) + !amdgpu_bo_is_vm_bo (abo, vm))return -EPERM ; r =amdgpu_bo_reserve (abo, false);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +14 -0
@@ -307,6 +307,20 @@ static inline struct amdgpu_bo *amdgpu_bo_shadowed(struct amdgpu_bo *bo)return NULL ; } +/** +* amdgpu_bo_is_vm_bo - check if the BO is VM always valid +* +* @abo: BO to be tested. +* @vm: VM to test against. +* +* Returns true if the BO is VM always valid. +*/ +static inline bool amdgpu_bo_is_vm_bo (struct amdgpu_bo *bo, +struct amdgpu_vm *vm) +{ +return bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv; +} +bool amdgpu_bo_is_amdgpu_bo (struct ttm_buffer_object *bo);void amdgpu_bo_placement_from_domain (struct amdgpu_bo *abo,u32 domain);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +13 -18
@@ -333,7 +333,7 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base, base->next = bo->vm_bo; bo->vm_bo = base; -if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) +if (!amdgpu_bo_is_vm_bo (bo, vm))return ;dma_resv_assert_held (vm->root.bo->tbo.base.resv);
@@ -1101,13 +1101,12 @@ static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,* For now ignore BOs which are currently locked and potentially * changing their location. */ -if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv && - !dma_resv_trylock (bo->tbo.base.resv)) +if (!amdgpu_bo_is_vm_bo (bo, vm) && !dma_resv_trylock (bo->tbo.base.resv))return ;amdgpu_bo_get_memory (bo, stats); -if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) -dma_resv_unlock (bo->tbo.base.resv); +if (amdgpu_bo_is_vm_bo (bo, vm)) +dma_resv_unlock (bo->tbo.base.resv); } void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
@@ -1203,8 +1202,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, uncached = false; } -if (clear || (bo && bo->tbo.base.resv == - vm->root.bo->tbo.base.resv)) +if (clear ||amdgpu_bo_is_vm_bo (bo, vm)) last_update = &vm->last_update;else last_update = &bo_va->last_pt_update;
@@ -1246,7 +1244,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,* the evicted list so that it gets validated again on the * next command submission. */ -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { +if (amdgpu_bo_is_vm_bo (bo, vm)) {uint32_t mem_type = bo->tbo.resource->mem_type; if (!(bo->preferred_domains &
@@ -1640,10 +1638,9 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,if (mapping->flags &AMDGPU_PTE_PRT )amdgpu_vm_prt_get (adev); -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !bo_va->base.moved) { +if (amdgpu_bo_is_vm_bo (bo, vm) && !bo_va->base.moved)amdgpu_vm_bo_moved (&bo_va->base); - } +trace_amdgpu_vm_bo_map (bo_va, mapping); }
@@ -1922,8 +1919,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,if (before->flags &AMDGPU_PTE_PRT )amdgpu_vm_prt_get (adev); -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !before->bo_va->base.moved) +if (amdgpu_bo_is_vm_bo (bo, vm) && !before->bo_va->base.moved)amdgpu_vm_bo_moved (&before->bo_va->base); }else {kfree (before);
@@ -1937,8 +1933,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,if (after->flags &AMDGPU_PTE_PRT )amdgpu_vm_prt_get (adev); -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !after->bo_va->base.moved) +if (amdgpu_bo_is_vm_bo (bo, vm) && !after->bo_va->base.moved)amdgpu_vm_bo_moved (&after->bo_va->base); }else {kfree (after);
@@ -2017,7 +2012,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,if (bo) {dma_resv_assert_held (bo->tbo.base.resv); -if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) +if (amdgpu_bo_is_vm_bo (bo, vm))ttm_bo_set_bulk_move (&bo->tbo,NULL ); for (base = &bo_va->base.bo->vm_bo; *base;
@@ -2111,7 +2106,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {struct amdgpu_vm *vm = bo_base->vm; -if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { +if (evicted &&amdgpu_bo_is_vm_bo (bo, vm)) {amdgpu_vm_bo_evicted (bo_base);continue ; }
@@ -2122,7 +2117,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,if (bo->tbo.type == ttm_bo_type_kernel)amdgpu_vm_bo_relocated (bo_base); -else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) +else if (amdgpu_bo_is_vm_bo (bo, vm))amdgpu_vm_bo_moved (bo_base);else amdgpu_vm_bo_invalidated (bo_base);
Signature · 1 line
2.44.0
Re: [PATCH 1/3] drm/amdgpu: Add amdgpu_bo_is_vm_bo helper
Am 26.04.24 um 18:43 schrieb Tvrtko Ursulin:
Quoted 11 lines
From: Tvrtko Ursulin <[email protected]>
Help code readability by replacing a bunch of:
bo->tbo.base.resv==vm->root.bo->tbo.base.resvWith:
amdgpu_bo_is_vm_bo(bo, vm)No functional changes.
Ah,yes that was on my TODO list as well.
But I would have rather added this to the VM instead. In other words move it to amdgpu_vm.h and call it amdgpu_vm_is_bo_always_valid() or something like that.
Regards,
Christian.
Quoted 155 lines
Signed-off-by Tvrtko Ursulin <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 14 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 31 +++++++++------------- 3 files changed, 28 insertions(+), 19 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-1@@ -174,7 +174,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,return -EPERM ;if (abo->flags &AMDGPU_GEM_CREATE_VM_ALWAYS_VALID && - abo->tbo.base.resv != vm->root.bo->tbo.base.resv) + !amdgpu_bo_is_vm_bo (abo, vm))return -EPERM ; r =amdgpu_bo_reserve (abo, false);drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +14
-0@@ -307,6 +307,20 @@ static inline struct amdgpu_bo *amdgpu_bo_shadowed(struct amdgpu_bo *bo)return NULL ; } +/** +* amdgpu_bo_is_vm_bo - check if the BO is VM always valid +* +* @abo: BO to be tested. +* @vm: VM to test against. +* +* Returns true if the BO is VM always valid. +*/ +static inline bool amdgpu_bo_is_vm_bo (struct amdgpu_bo *bo, +struct amdgpu_vm *vm) +{ +return bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv; +} +bool amdgpu_bo_is_amdgpu_bo (struct ttm_buffer_object *bo);void amdgpu_bo_placement_from_domain (struct amdgpu_bo *abo,u32 domain);drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +13
-18@@ -333,7 +333,7 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base, base->next = bo->vm_bo; bo->vm_bo = base; -if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) +if (!amdgpu_bo_is_vm_bo (bo, vm))return ;dma_resv_assert_held (vm->root.bo->tbo.base.resv);@@ -1101,13 +1101,12 @@ static void amdgpu_vm_bo_get_memory(struct amdgpu_bo_va *bo_va,* For now ignore BOs which are currently locked and potentially * changing their location. */ -if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv && - !dma_resv_trylock (bo->tbo.base.resv)) +if (!amdgpu_bo_is_vm_bo (bo, vm) && !dma_resv_trylock (bo->tbo.base.resv))return ;amdgpu_bo_get_memory (bo, stats); -if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) -dma_resv_unlock (bo->tbo.base.resv); +if (amdgpu_bo_is_vm_bo (bo, vm)) +dma_resv_unlock (bo->tbo.base.resv); } void amdgpu_vm_get_memory(struct amdgpu_vm *vm,@@ -1203,8 +1202,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, uncached = false; } -if (clear || (bo && bo->tbo.base.resv == - vm->root.bo->tbo.base.resv)) +if (clear ||amdgpu_bo_is_vm_bo (bo, vm)) last_update = &vm->last_update;else last_update = &bo_va->last_pt_update;@@ -1246,7 +1244,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,* the evicted list so that it gets validated again on the * next command submission. */ -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { +if (amdgpu_bo_is_vm_bo (bo, vm)) {uint32_t mem_type = bo->tbo.resource->mem_type; if (!(bo->preferred_domains &@@ -1640,10 +1638,9 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,if (mapping->flags &AMDGPU_PTE_PRT )amdgpu_vm_prt_get (adev); -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !bo_va->base.moved) { +if (amdgpu_bo_is_vm_bo (bo, vm) && !bo_va->base.moved)amdgpu_vm_bo_moved (&bo_va->base); - } +trace_amdgpu_vm_bo_map (bo_va, mapping); }@@ -1922,8 +1919,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,if (before->flags &AMDGPU_PTE_PRT )amdgpu_vm_prt_get (adev); -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !before->bo_va->base.moved) +if (amdgpu_bo_is_vm_bo (bo, vm) && !before->bo_va->base.moved)amdgpu_vm_bo_moved (&before->bo_va->base); }else {kfree (before);@@ -1937,8 +1933,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,if (after->flags &AMDGPU_PTE_PRT )amdgpu_vm_prt_get (adev); -if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !after->bo_va->base.moved) +if (amdgpu_bo_is_vm_bo (bo, vm) && !after->bo_va->base.moved)amdgpu_vm_bo_moved (&after->bo_va->base); }else {kfree (after);@@ -2017,7 +2012,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,if (bo) {dma_resv_assert_held (bo->tbo.base.resv); -if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) +if (amdgpu_bo_is_vm_bo (bo, vm))ttm_bo_set_bulk_move (&bo->tbo,NULL ); for (base = &bo_va->base.bo->vm_bo; *base;@@ -2111,7 +2106,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {struct amdgpu_vm *vm = bo_base->vm; -if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { +if (evicted &&amdgpu_bo_is_vm_bo (bo, vm)) {amdgpu_vm_bo_evicted (bo_base);continue ; }@@ -2122,7 +2117,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,if (bo->tbo.type == ttm_bo_type_kernel)amdgpu_vm_bo_relocated (bo_base); -else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) +else if (amdgpu_bo_is_vm_bo (bo, vm))amdgpu_vm_bo_moved (bo_base);else amdgpu_vm_bo_invalidated (bo_base);
Re: [PATCH 1/3] drm/amdgpu: Add amdgpu_bo_is_vm_bo helper
Quoted 9 lines · Christian König
Am 26.04.24 um 18:43 schrieb Tvrtko Ursulin:
Quoted 8 lines
From: Tvrtko Ursulin <[email protected]>
Help code readability by replacing a bunch of:
bo->tbo.base.resv==vm->root.bo->tbo.base.resvWith:
Quoted 7 lines
Quoted 3 lines
amdgpu_bo_is_vm_bo(bo, vm)No functional changes.
Ah,yes that was on my TODO list as well.
But I would have rather added this to the VM instead. In other words move it to
amdgpu_vm.hand call itamdgpu_vm_is_bo_always_valid()or something like that.
I am happy to move it around as long as you are sure amdgpu_vm.h is the location.
For instance main API there it seems to be amdgpu_vm_bo's. At least all the amdgpu_bo usages do not needing anything more that the struct forward declared.
So if I move the helper there I either need to make it include another header, or move the helper out of line to amdgpu_vm.c.
Thoughts?
Regards,
Tvrtko
Quoted 151 lines
Quoted 150 lines
Signed-off-by Tvrtko Ursulin <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +-drivers/gpu/drm/amd/amdgpu/amdgpu_object.h| 14 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 31 +++++++++-------------3 files changed, 28 insertions(+), 19 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0
-0@@ -174,7 +174,7 @@ static int amdgpu_gem_object_open(structdrm_gem_object *obj, return -EPERM;if (
abo->flags&AMDGPU_GEM_CREATE_VM_ALWAYS_VALID&& -abo->tbo.base.resv!=vm->root.bo->tbo.base.resv) + !amdgpu_bo_is_vm_bo(abo, vm))
return-EPERM;
r =amdgpu_bo_reserve(abo, false);drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +14
-0@@ -307,6 +307,20 @@ static inline struct amdgpu_bo *amdgpu_bo_shadowed(struct amdgpu_bo *bo) return NULL; } +/** +* amdgpu_bo_is_vm_bo - check if the BO is VM always valid +* +* @abo: BO to be tested. +* @vm: VM to test against. +* +* Returns true if the BO is VM always valid. +*/ +static inline bool amdgpu_bo_is_vm_bo (struct amdgpu_bo *bo, + struct amdgpu_vm *vm) +{ + return bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv; +} + bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo); void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain);drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5
-7@@ -333,7 +333,7 @@ void amdgpu_vm_bo_base_init(structamdgpu_vm_bo_base *base, base->next = bo->vm_bo; bo->vm_bo = base; - if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) + if (!amdgpu_bo_is_vm_bo(bo, vm)) return; dma_resv_assert_held(vm->root.bo->tbo.base.resv);@@ -1101,13 +1101,12 @@ static void amdgpu_vm_bo_get_memory(structamdgpu_bo_va *bo_va, * For now ignore BOs which are currently locked and potentially * changing their location. */ - if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv && - !dma_resv_trylock(bo->tbo.base.resv)) + if (!amdgpu_bo_is_vm_bo(bo, vm) && !dma_resv_trylock (bo->tbo.base.resv)) return; amdgpu_bo_get_memory(bo, stats); - if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) - dma_resv_unlock(bo->tbo.base.resv); + if (amdgpu_bo_is_vm_bo(bo, vm)) + dma_resv_unlock(bo->tbo.base.resv); } void amdgpu_vm_get_memory(struct amdgpu_vm *vm,@@ -1203,8 +1202,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, uncached = false; } - if (clear || (bo && bo->tbo.base.resv == - vm->root.bo->tbo.base.resv)) + if (clear || amdgpu_bo_is_vm_bo(bo, vm)) last_update = &vm->last_update;else
last_update=&bo_va->last_pt_update;drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1@@ -1246,7 +1244,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device*adev, struct amdgpu_bo_va *bo_va, * the evicted list so that it gets validated again on the * next command submission. */ - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { + if (amdgpu_bo_is_vm_bo(bo, vm)) { uint32_t mem_type = bo->tbo.resource->mem_type;if (!(bo->preferred_domains &
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6
-9@@ -1640,10 +1638,9 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, if (mapping->flags & AMDGPU_PTE_PRT) amdgpu_vm_prt_get(adev); - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !bo_va->base.moved) { + if (amdgpu_bo_is_vm_bo(bo, vm) && !bo_va->base.moved) amdgpu_vm_bo_moved(&bo_va->base); - } + trace_amdgpu_vm_bo_map(bo_va, mapping); }@@ -1922,8 +1919,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, if (before->flags & AMDGPU_PTE_PRT) amdgpu_vm_prt_get(adev); - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !before->bo_va->base.moved) + if (amdgpu_bo_is_vm_bo(bo, vm) && !before->bo_va->base.moved) amdgpu_vm_bo_moved(&before->bo_va->base); } else { kfree(before);@@ -1937,8 +1933,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, if (after->flags & AMDGPU_PTE_PRT) amdgpu_vm_prt_get(adev); - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !after->bo_va->base.moved) + if (amdgpu_bo_is_vm_bo(bo, vm) && !after->bo_va->base.moved) amdgpu_vm_bo_moved(&after->bo_va->base); } else { kfree(after);@@ -2017,7 +2012,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev, if (bo) { dma_resv_assert_held(bo->tbo.base.resv); - if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) + if (amdgpu_bo_is_vm_bo(bo, vm)) ttm_bo_set_bulk_move(&bo->tbo, NULL); for (base = &bo_va->base.bo->vm_bo; *base;@@ -2111,7 +2106,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { struct amdgpu_vm *vm = bo_base->vm; - if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { + if (evicted && amdgpu_bo_is_vm_bo(bo, vm)) { amdgpu_vm_bo_evicted(bo_base); continue;}
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1@@ -2122,7 +2117,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, if (bo->tbo.type == ttm_bo_type_kernel) amdgpu_vm_bo_relocated(bo_base); - else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) + else if (amdgpu_bo_is_vm_bo(bo, vm)) amdgpu_vm_bo_moved(bo_base);else
amdgpu_vm_bo_invalidated(bo_base);
Christian König
maintainerRe: [PATCH 1/3] drm/amdgpu: Add amdgpu_bo_is_vm_bo helper
Am 29.04.24 um 15:34 schrieb Tvrtko Ursulin:
Quoted 19 lines · Christian König
Quoted 9 lines
Am 26.04.24 um 18:43 schrieb Tvrtko Ursulin:
Quoted 8 lines
From: Tvrtko Ursulin <[email protected]>
Help code readability by replacing a bunch of:
bo->tbo.base.resv==vm->root.bo->tbo.base.resvWith:
Quoted 7 lines
Quoted 3 lines
amdgpu_bo_is_vm_bo(bo, vm)No functional changes.
Ah,yes that was on my TODO list as well.
But I would have rather added this to the VM instead. In other words move it to
amdgpu_vm.hand call itamdgpu_vm_is_bo_always_valid()or something like that.
Quoted 7 lines
I am happy to move it around as long as you are sure
amdgpu_vm.his the location.For instance main API there it seems to be amdgpu_vm_bo's. At least all the
amdgpu_bousages do not needing anything more that the struct forward declared.So if I move the helper there I either need to make it include another header, or move the helper out of line to
amdgpu_vm.c.Thoughts?
amdgpu_vm.c is fine as well. I just though that something so simply could be an inline function in the header as well.
Regards,
Christian.
Quoted 157 lines
Regards,
Tvrtko
Quoted 152 lines
Quoted 151 lines
Signed-off-by Tvrtko Ursulin <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +-drivers/gpu/drm/amd/amdgpu/amdgpu_object.h| 14 ++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 31
+++++++++-------------3 files changed, 28 insertions(+), 19 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0
-0@@ -174,7 +174,7 @@ static int amdgpu_gem_object_open(structdrm_gem_object *obj, return -EPERM;if (
abo->flags&AMDGPU_GEM_CREATE_VM_ALWAYS_VALID&& -abo->tbo.base.resv!=vm->root.bo->tbo.base.resv) + !amdgpu_bo_is_vm_bo(abo, vm))
return-EPERM;
r =amdgpu_bo_reserve(abo, false);drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +14
-0@@ -307,6 +307,20 @@ static inline struct amdgpu_bo *amdgpu_bo_shadowed(struct amdgpu_bo *bo) return NULL; } +/** +* amdgpu_bo_is_vm_bo - check if the BO is VM always valid +* +* @abo: BO to be tested. +* @vm: VM to test against. +* +* Returns true if the BO is VM always valid. +*/ +static inline bool amdgpu_bo_is_vm_bo (struct amdgpu_bo *bo, + struct amdgpu_vm *vm) +{ + return bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv; +} + bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo); void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain);drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5
-7@@ -333,7 +333,7 @@ void amdgpu_vm_bo_base_init(structamdgpu_vm_bo_base *base, base->next = bo->vm_bo; bo->vm_bo = base; - if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) + if (!amdgpu_bo_is_vm_bo(bo, vm)) return; dma_resv_assert_held(vm->root.bo->tbo.base.resv);@@ -1101,13 +1101,12 @@ static void amdgpu_vm_bo_get_memory(structamdgpu_bo_va *bo_va, * For now ignore BOs which are currently locked and potentially * changing their location. */ - if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv && - !dma_resv_trylock(bo->tbo.base.resv)) + if (!amdgpu_bo_is_vm_bo(bo, vm) && !dma_resv_trylock (bo->tbo.base.resv)) return; amdgpu_bo_get_memory(bo, stats); - if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv) - dma_resv_unlock(bo->tbo.base.resv); + if (amdgpu_bo_is_vm_bo(bo, vm)) + dma_resv_unlock(bo->tbo.base.resv); } void amdgpu_vm_get_memory(struct amdgpu_vm *vm,@@ -1203,8 +1202,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, uncached = false; } - if (clear || (bo && bo->tbo.base.resv == - vm->root.bo->tbo.base.resv)) + if (clear || amdgpu_bo_is_vm_bo(bo, vm)) last_update = &vm->last_update;else
last_update=&bo_va->last_pt_update;drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1@@ -1246,7 +1244,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device*adev, struct amdgpu_bo_va *bo_va, * the evicted list so that it gets validated again on the * next command submission. */ - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { + if (amdgpu_bo_is_vm_bo(bo, vm)) { uint32_t mem_type = bo->tbo.resource->mem_type;if (!(bo->preferred_domains &
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6
-9@@ -1640,10 +1638,9 @@ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, if (mapping->flags & AMDGPU_PTE_PRT) amdgpu_vm_prt_get(adev); - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !bo_va->base.moved) { + if (amdgpu_bo_is_vm_bo(bo, vm) && !bo_va->base.moved) amdgpu_vm_bo_moved(&bo_va->base); - } + trace_amdgpu_vm_bo_map(bo_va, mapping); }@@ -1922,8 +1919,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, if (before->flags & AMDGPU_PTE_PRT) amdgpu_vm_prt_get(adev); - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !before->bo_va->base.moved) + if (amdgpu_bo_is_vm_bo(bo, vm) && !before->bo_va->base.moved)amdgpu_vm_bo_moved (&before->bo_va->base); } else { kfree(before);@@ -1937,8 +1933,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, if (after->flags & AMDGPU_PTE_PRT) amdgpu_vm_prt_get(adev); - if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && - !after->bo_va->base.moved) + if (amdgpu_bo_is_vm_bo(bo, vm) && !after->bo_va->base.moved)amdgpu_vm_bo_moved (&after->bo_va->base); } else { kfree(after);@@ -2017,7 +2012,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev, if (bo) { dma_resv_assert_held(bo->tbo.base.resv); - if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) + if (amdgpu_bo_is_vm_bo(bo, vm)) ttm_bo_set_bulk_move(&bo->tbo, NULL); for (base = &bo_va->base.bo->vm_bo; *base;@@ -2111,7 +2106,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { struct amdgpu_vm *vm = bo_base->vm; - if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { + if (evicted && amdgpu_bo_is_vm_bo(bo, vm)) { amdgpu_vm_bo_evicted(bo_base); continue;}
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1@@ -2122,7 +2117,7 @@ void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, if (bo->tbo.type == ttm_bo_type_kernel) amdgpu_vm_bo_relocated(bo_base); - else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) + else if (amdgpu_bo_is_vm_bo(bo, vm)) amdgpu_vm_bo_moved(bo_base);else
amdgpu_vm_bo_invalidated(bo_base);
PATCH 2/3 drm/amdgpu: Reduce mem_type to domain double indirection
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
All apart from AMDGPU_GEM_DOMAIN_GTT memory domains map 1:1 to TTM placements. And the former be either AMDGPU_PL_PREEMPT or TTM_PL_TT, depending on AMDGPU_GEM_CREATE_PREEMPTIBLE.
Simplify a few places in the code which convert the TTM placement into a domain by checking against the current placement directly.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 30 ++++++++++----------- 2 files changed, 16 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +2 -2
@@ -165,8 +165,8 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,if (r)return ERR_PTR (r); - }else if (!(amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type) & -AMDGPU_GEM_DOMAIN_GTT )) { + }else if (bo->tbo.resource->mem_type !=TTM_PL_TT && + bo->tbo.resource->mem_type !=AMDGPU_PL_PREEMPT ) {return ERR_PTR (-EBUSY ); }
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +14 -16
@@ -976,12 +976,12 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,ttm_bo_pin (&bo->tbo); - domain =amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type); -if (domain ==AMDGPU_GEM_DOMAIN_VRAM ) { +if (bo->tbo.resource->mem_type ==TTM_PL_VRAM ) {atomic64_add (amdgpu_bo_size (bo), &adev->vram_pin_size);atomic64_add (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); - }else if (domain ==AMDGPU_GEM_DOMAIN_GTT ) { + }else if (bo->tbo.resource->mem_type ==TTM_PL_TT || + bo->tbo.resource->mem_type ==AMDGPU_PL_PREEMPT ) {atomic64_add (amdgpu_bo_size (bo), &adev->gart_pin_size); }
@@ -1280,7 +1280,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, {uint64_t size =amdgpu_bo_size (bo);struct drm_gem_object *obj; -unsigned int domain;bool shared;/* Abort if the BO doesn't currently have a backing store */
@@ -1290,21 +1289,21 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, obj = &bo->tbo.base; shared =drm_gem_object_is_shared_for_memory_stats (obj); - domain =amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type); -switch (domain) { -case AMDGPU_GEM_DOMAIN_VRAM : +switch (bo->tbo.resource->mem_type) { +case TTM_PL_VRAM : stats->vram += size;if (amdgpu_bo_in_cpu_visible_vram (bo)) stats->visible_vram += size;if (shared) stats->vram_shared += size;break ; -case AMDGPU_GEM_DOMAIN_GTT : +case TTM_PL_TT : +case AMDGPU_PL_PREEMPT : stats->gtt += size;if (shared) stats->gtt_shared += size;break ; -case AMDGPU_GEM_DOMAIN_CPU : +case TTM_PL_SYSTEM :default : stats->cpu += size;if (shared)
@@ -1317,7 +1316,7 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,if (bo->flags &AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED ) stats->requested_visible_vram += size; -if (domain !=AMDGPU_GEM_DOMAIN_VRAM ) { +if (bo->tbo.resource->mem_type !=TTM_PL_VRAM ) { stats->evicted_vram += size;if (bo->flags &AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED ) stats->evicted_visible_vram += size;
@@ -1592,19 +1591,18 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m)u64 size;if (dma_resv_trylock (bo->tbo.base.resv)) { -unsigned int domain; - domain =amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type); -switch (domain) { -case AMDGPU_GEM_DOMAIN_VRAM : +switch (bo->tbo.resource->mem_type) { +case TTM_PL_VRAM :if (amdgpu_bo_in_cpu_visible_vram (bo)) placement ="VRAM VISIBLE" ;else placement ="VRAM" ;break ; -case AMDGPU_GEM_DOMAIN_GTT : +case TTM_PL_TT : +case AMDGPU_PL_PREEMPT : placement ="GTT" ;break ; -case AMDGPU_GEM_DOMAIN_CPU : +case TTM_PL_SYSTEM :default : placement ="CPU" ;break ;
Signature · 1 line
2.44.0
Re: [PATCH 2/3] drm/amdgpu: Reduce mem_type to domain double indirection
Am 26.04.24 um 18:43 schrieb Tvrtko Ursulin:
Quoted 10 lines
From: Tvrtko Ursulin <[email protected]>
All apart from
AMDGPU_GEM_DOMAIN_GTTmemory domains map 1:1 to TTM placements. And the former be eitherAMDGPU_PL_PREEMPTorTTM_PL_TT, depending onAMDGPU_GEM_CREATE_PREEMPTIBLE.Simplify a few places in the code which convert the TTM placement into a domain by checking against the current placement directly.
Signed-off-by Tvrtko Ursulin <[email protected]>
Quoted 109 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 30 ++++++++++----------- 2 files changed, 16 insertions(+), 18 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +2
-2@@ -165,8 +165,8 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,if (r)return ERR_PTR (r); - }else if (!(amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type) & -AMDGPU_GEM_DOMAIN_GTT )) { + }else if (bo->tbo.resource->mem_type !=TTM_PL_TT && + bo->tbo.resource->mem_type !=AMDGPU_PL_PREEMPT ) {return ERR_PTR (-EBUSY ); }drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +14
-16@@ -976,12 +976,12 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,ttm_bo_pin (&bo->tbo); - domain =amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type); -if (domain ==AMDGPU_GEM_DOMAIN_VRAM ) { +if (bo->tbo.resource->mem_type ==TTM_PL_VRAM ) {atomic64_add (amdgpu_bo_size (bo), &adev->vram_pin_size);atomic64_add (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); - }else if (domain ==AMDGPU_GEM_DOMAIN_GTT ) { + }else if (bo->tbo.resource->mem_type ==TTM_PL_TT || + bo->tbo.resource->mem_type ==AMDGPU_PL_PREEMPT ) {atomic64_add (amdgpu_bo_size (bo), &adev->gart_pin_size); }@@ -1280,7 +1280,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, {uint64_t size =amdgpu_bo_size (bo);struct drm_gem_object *obj; -unsigned int domain;bool shared;/* Abort if the BO doesn't currently have a backing store */ @@ -1290,21 +1289,21 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, obj = &bo->tbo.base; shared =drm_gem_object_is_shared_for_memory_stats (obj); - domain =amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type); -switch (domain) { -case AMDGPU_GEM_DOMAIN_VRAM : +switch (bo->tbo.resource->mem_type) { +case TTM_PL_VRAM : stats->vram += size;if (amdgpu_bo_in_cpu_visible_vram (bo)) stats->visible_vram += size;if (shared) stats->vram_shared += size;break ; -case AMDGPU_GEM_DOMAIN_GTT : +case TTM_PL_TT : +case AMDGPU_PL_PREEMPT : stats->gtt += size;if (shared) stats->gtt_shared += size;break ; -case AMDGPU_GEM_DOMAIN_CPU : +case TTM_PL_SYSTEM :default : stats->cpu += size;if (shared)@@ -1317,7 +1316,7 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,if (bo->flags &AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED ) stats->requested_visible_vram += size; -if (domain !=AMDGPU_GEM_DOMAIN_VRAM ) { +if (bo->tbo.resource->mem_type !=TTM_PL_VRAM ) { stats->evicted_vram += size;if (bo->flags &AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED ) stats->evicted_visible_vram += size;@@ -1592,19 +1591,18 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m)u64 size;if (dma_resv_trylock (bo->tbo.base.resv)) { -unsigned int domain; - domain =amdgpu_mem_type_to_domain (bo->tbo.resource->mem_type); -switch (domain) { -case AMDGPU_GEM_DOMAIN_VRAM : +switch (bo->tbo.resource->mem_type) { +case TTM_PL_VRAM :if (amdgpu_bo_in_cpu_visible_vram (bo)) placement ="VRAM VISIBLE" ;else placement ="VRAM" ;break ; -case AMDGPU_GEM_DOMAIN_GTT : +case TTM_PL_TT : +case AMDGPU_PL_PREEMPT : placement ="GTT" ;break ; -case AMDGPU_GEM_DOMAIN_CPU : +case TTM_PL_SYSTEM :default : placement ="CPU" ;break ;
Re: [PATCH 2/3] drm/amdgpu: Reduce mem_type to domain double indirection
Hi Christian,
Quoted 7 lines · Christian König
Am 26.04.24 um 18:43 schrieb Tvrtko Ursulin:
Quoted 6 lines
From: Tvrtko Ursulin <[email protected]>
All apart from
AMDGPU_GEM_DOMAIN_GTTmemory domains map 1:1 to TTM placements. And the former be eitherAMDGPU_PL_PREEMPTorTTM_PL_TT, depending onAMDGPU_GEM_CREATE_PREEMPTIBLE.
Quoted 6 lines
Quoted 4 lines
Simplify a few places in the code which convert the TTM placement into a domain by checking against the current placement directly.
Signed-off-by Tvrtko Ursulin <[email protected]>Reviewed-by Christian König <[email protected]>
Thanks! I am however a bit unsure now, read below..
Quoted 5 lines
Quoted 5 lines
---
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c| 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 30 ++++++++++-----------2 files changed, 16 insertions(+), 18 deletions(-)
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c static struct sg_table *amdgpu_dma_buf_map(struct · line 170
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +2
-2@@ -165,8 +165,8 @@ static struct sg_table *amdgpu_dma_buf_map(structdma_buf_attachment *attach, if (r) return ERR_PTR(r); - } else if (!(amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type) & - AMDGPU_GEM_DOMAIN_GTT)) { + } else if (bo->tbo.resource->mem_type != TTM_PL_TT && + bo->tbo.resource->mem_type != AMDGPU_PL_PREEMPT) { return ERR_PTR(-EBUSY);
... this for instance (one hunk below too).
Because what we are discussing in 3/3, amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type) does not return a valid domain for AMDGPU_PL_PREEMPT. Which could make mentioning it here misleading. So I am not really sure what the design is supposed to be.
Is this a weakness in the current code? Maybe best to leave the discussion for 3/3.
Regards,
Tvrtko
Quoted 86 lines
Quoted 85 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +0
-0}drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +8
-9@@ -976,12 +976,12 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain, ttm_bo_pin(&bo->tbo); - domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type); - if (domain == AMDGPU_GEM_DOMAIN_VRAM) { + if (bo->tbo.resource->mem_type == TTM_PL_VRAM) { atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size); atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size); - } else if (domain == AMDGPU_GEM_DOMAIN_GTT) { + } else if (bo->tbo.resource->mem_type == TTM_PL_TT || + bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) { atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size); }@@ -1280,7 +1280,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, { uint64_t size = amdgpu_bo_size(bo); struct drm_gem_object *obj; - unsigned int domain; bool shared; /* Abort if the BO doesn't currently have a backing store */@@ -1290,21 +1289,21 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, obj = &bo->tbo.base; shared = drm_gem_object_is_shared_for_memory_stats(obj); - domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type); - switch (domain) { - case AMDGPU_GEM_DOMAIN_VRAM: + switch (bo->tbo.resource->mem_type) { + case TTM_PL_VRAM: stats->vram += size; if (amdgpu_bo_in_cpu_visible_vram(bo)) stats->visible_vram += size; if (shared) stats->vram_shared += size; break; - case AMDGPU_GEM_DOMAIN_GTT: + case TTM_PL_TT: + case AMDGPU_PL_PREEMPT: stats->gtt += size; if (shared) stats->gtt_shared += size; break; - case AMDGPU_GEM_DOMAIN_CPU: + case TTM_PL_SYSTEM:default:
stats->cpu+= size;
if (shared)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3
-5@@ -1317,7 +1316,7 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, if (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) stats->requested_visible_vram += size; - if (domain != AMDGPU_GEM_DOMAIN_VRAM) { + if (bo->tbo.resource->mem_type != TTM_PL_VRAM) { stats->evicted_vram += size; if (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) stats->evicted_visible_vram += size;@@ -1592,19 +1591,18 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) u64 size; if (dma_resv_trylock(bo->tbo.base.resv)) { - unsigned int domain; - domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type); - switch (domain) { - case AMDGPU_GEM_DOMAIN_VRAM: + switch (bo->tbo.resource->mem_type) { + case TTM_PL_VRAM: if (amdgpu_bo_in_cpu_visible_vram(bo)) placement = "VRAM VISIBLE";else
placement = "VRAM";
break;
- caseAMDGPU_GEM_DOMAIN_GTT:
+ caseTTM_PL_TT:
+ caseAMDGPU_PL_PREEMPT:
placement = "GTT";
break;
- caseAMDGPU_GEM_DOMAIN_CPU:
+ caseTTM_PL_SYSTEM:
default:
placement = "CPU";
break;
PATCH 3/3 drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
When commit b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.
That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
Fix by extending the accounting criteria in amdgpu_bo_unpin.
What also aappears needs fixing is not reporting their size from the amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.
And also amdgpu_bo_print_info for debugfs reporting.
Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -3
@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo)atomic64_sub (amdgpu_bo_size (bo), &adev->vram_pin_size);atomic64_sub (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); - }else if (bo->tbo.resource->mem_type ==TTM_PL_TT ) { + }else if (bo->tbo.resource->mem_type ==TTM_PL_TT || + bo->tbo.resource->mem_type ==AMDGPU_PL_PREEMPT ) {atomic64_sub (amdgpu_bo_size (bo), &adev->gart_pin_size); }
@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size;break ;case TTM_PL_TT : -case AMDGPU_PL_PREEMPT : stats->gtt += size;if (shared) stats->gtt_shared += size;
@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement ="VRAM" ;break ;case TTM_PL_TT : -case AMDGPU_PL_PREEMPT : placement ="GTT" ;break ;case TTM_PL_SYSTEM :
Signature · 1 line
2.44.0
Re: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
Quoted 8 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
AMDGPU_PL_PREEMPT is mostly used for userptr BOs, which cannot be pinned. In any case you should make sure that the accounting is consistent between amdgpu_bo_pin_restricted and amdgpu_bo_unpin. This patch breaks that consistency.
Quoted 6 lines
Fix by extending the accounting criteria in
amdgpu_bo_unpin.What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.
I think that's true. It's a very fine distinction. AMDGPU_PL_PREEMPT does use system memory and it is GPU accessible, just like GTT. The only difference is, that it's not subject to the GTT limits because their eviction is handled by callbacks other than TTM evictions and doesn't need to wait for fences.
Regards,
Felix
Quoted 44 lines
And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-3@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo)atomic64_sub (amdgpu_bo_size (bo), &adev->vram_pin_size);atomic64_sub (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); - }else if (bo->tbo.resource->mem_type ==TTM_PL_TT ) { + }else if (bo->tbo.resource->mem_type ==TTM_PL_TT || + bo->tbo.resource->mem_type ==AMDGPU_PL_PREEMPT ) {atomic64_sub (amdgpu_bo_size (bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size;break ;case TTM_PL_TT : -case AMDGPU_PL_PREEMPT : stats->gtt += size;if (shared) stats->gtt_shared += size;@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement ="VRAM" ;break ;case TTM_PL_TT : -case AMDGPU_PL_PREEMPT : placement ="GTT" ;break ;case TTM_PL_SYSTEM :
Re: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
Quoted 4 lines · Felix Kuehling
Quoted 2 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Quoted 8 lines
Quoted 6 lines
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
AMDGPU_PL_PREEMPTis mostly used for userptr BOs, which cannot be pinned. In any case you should make sure that the accounting is consistent betweenamdgpu_bo_pin_restrictedandamdgpu_bo_unpin. This patch breaks that consistency.
You mean amdgpu_bo_pin(_restricted) and amdgpu_bo_unpin do not run for such objects, or something else?
If they run, then at the end of pin there is:
domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
...
} else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);And unpin has no handling for AMDGPU_PL_PREEMPT.
Ah I see.. does it rely on amdgpu_mem_type_to_domain returning 0 for AMDGPU_PL_PREEMPT? My confusion was I misread the pinning check as checking the domain as stored in the bo at creation time.
Although I am still confused by the statement userptr BOs are not pinned. It is not needed to map them via GART on AMD hardware for GPU to be able to access them?
Quoted 7 lines
Quoted 5 lines
Fix by extending the accounting criteria in
amdgpu_bo_unpin.What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.I think that's true. It's a very fine distinction.
AMDGPU_PL_PREEMPTdoes use system memory and it is GPU accessible, just like GTT. The only difference is, that it's not subject to the GTT limits because their eviction is handled by callbacks other than TTM evictions and doesn't need to wait for fences.
As in you think those two hunks of the patch are correct?
Regards,
Tvrtko
Quoted 48 lines
Regards,
FelixQuoted 44 lines
And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-1@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo) atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size); - } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { + } else if (bo->tbo.resource->mem_type == TTM_PL_TT || + bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) { atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
stats->gtt+= size;
if (shared)
stats->gtt_shared+= size;drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement = "VRAM"; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
placement = "GTT";
break;
caseTTM_PL_SYSTEM:
Christian König
maintainerRe: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
Am 29.04.24 um 11:43 schrieb Tvrtko Ursulin:
Quoted 24 lines · Felix Kuehling
Quoted 12 lines · Tvrtko Ursulin
Quoted 8 lines
From: Tvrtko Ursulin <[email protected]>
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
AMDGPU_PL_PREEMPTis mostly used for userptr BOs, which cannot be pinned. In any case you should make sure that the accounting is consistent betweenamdgpu_bo_pin_restrictedandamdgpu_bo_unpin. This patch breaks that consistency.You mean
amdgpu_bo_pin(_restricted)andamdgpu_bo_unpindo not run for such objects, or something else?If they run, then at the end of pin there is:
domain =
amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
...
} else if (domain ==AMDGPU_GEM_DOMAIN_GTT) {atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
Quoted 5 lines
And unpin has no handling for
AMDGPU_PL_PREEMPT.Ah I see.. does it rely on
amdgpu_mem_type_to_domainreturning 0 forAMDGPU_PL_PREEMPT? My confusion was I misread the pinning check as checking the domain as stored in the bo at creation time.Although I am still confused by the statement userptr BOs are not pinned. It is not needed to map them via GART on AMD hardware for GPU to be able to access them?
No, a GART mapping is only needed if you want to scanout from them or otherwise use them from the kernel on the GPU.
Background is that the kernel doesn't has VM with page tables..
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Fix by extending the accounting criteria in
amdgpu_bo_unpin.What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.I think that's true. It's a very fine distinction.
AMDGPU_PL_PREEMPTdoes use system memory and it is GPU accessible, just like GTT. The only difference is, that it's not subject to the GTT limits because their eviction is handled by callbacks other than TTM evictions and doesn't need to wait for fences.As in you think those two hunks of the patch are correct?
I think so as well, yes. But we still need a name for preemptible BOs while printing them in debugfs.
Regards,
Christian.
Quoted 54 lines
Regards,
Tvrtko
Quoted 48 lines
Regards,
FelixQuoted 44 lines
And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-1@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo) atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size); - } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { + } else if (bo->tbo.resource->mem_type == TTM_PL_TT || + bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) { atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
stats->gtt+= size;
if (shared)
stats->gtt_shared+= size;drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement = "VRAM"; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
placement = "GTT";
break;
caseTTM_PL_SYSTEM:
Re: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
Quoted 27 lines · Christian König
Am 29.04.24 um 11:43 schrieb Tvrtko Ursulin:
Quoted 26 lines · Felix Kuehling
Quoted 12 lines · Tvrtko Ursulin
Quoted 8 lines
From: Tvrtko Ursulin <[email protected]>
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
AMDGPU_PL_PREEMPTis mostly used for userptr BOs, which cannot be pinned. In any case you should make sure that the accounting is consistent betweenamdgpu_bo_pin_restrictedandamdgpu_bo_unpin. This patch breaks that consistency.You mean
amdgpu_bo_pin(_restricted)andamdgpu_bo_unpindo not run for such objects, or something else?If they run, then at the end of pin there is:
domain =
amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
...
} else if (domain ==AMDGPU_GEM_DOMAIN_GTT) {atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);And unpin has no handling for
AMDGPU_PL_PREEMPT.
Quoted 7 lines
Quoted 3 lines
Ah I see.. does it rely on
amdgpu_mem_type_to_domainreturning 0 forAMDGPU_PL_PREEMPT? My confusion was I misread the pinning check as checking the domain as stored in the bo at creation time.Although I am still confused by the statement userptr BOs are not pinned. It is not needed to map them via GART on AMD hardware for GPU to be able to access them?
No, a GART mapping is only needed if you want to scanout from them or otherwise use them from the kernel on the GPU.
Background is that the kernel doesn't has VM with page tables..
Got it, thanks!
Presumably somewhere else in the code then it is prevented to call pin/unpin on those?
What to do, if anything, with the attempt to address the asymmetry in the accounting criteria between the pin and unpin?
I mean domain based on pin:
domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size);
atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo),
&adev->visible_pin_size);
} else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
}Versus placement based on unpin:
if (bo->tbo.resource->mem_type == TTM_PL_VRAM) {
atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size);
atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo),
&adev->visible_pin_size);
} else if (bo->tbo.resource->mem_type == TTM_PL_TT) {
atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size);
}The fact amdgpu_mem_type_to_domain never translates back to AMDGPU_PL_PREEMPT means there is indeed currently no bug.
Is 2/3 still desirable to convert the check in pin to me mem_type based?
Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Fix by extending the accounting criteria in
amdgpu_bo_unpin.What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.I think that's true. It's a very fine distinction.
AMDGPU_PL_PREEMPTdoes use system memory and it is GPU accessible, just like GTT. The only difference is, that it's not subject to the GTT limits because their eviction is handled by callbacks other than TTM evictions and doesn't need to wait for fences.As in you think those two hunks of the patch are correct?
I think so as well, yes. But we still need a name for preemptible BOs while printing them in debugfs.
Currently it looks the name is 'CPU':
amdgpu_bo_print_info()
...
case AMDGPU_GEM_DOMAIN_CPU: default: placement = "CPU"; break;
Also, where to account them in struct amdgpu_mem_stats?
Regards,
Tvrtko
Quoted 59 lines
Regards,
Christian.Quoted 54 lines
Regards,
Tvrtko
Quoted 48 lines
Regards,
FelixQuoted 44 lines
And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-1@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo) atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size); - } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { + } else if (bo->tbo.resource->mem_type == TTM_PL_TT || + bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) { atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
stats->gtt+= size;
if (shared)
stats->gtt_shared+= size;drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement = "VRAM"; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
placement = "GTT";
break;
caseTTM_PL_SYSTEM:
Re: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
Quoted 33 lines · Tvrtko Ursulin
Quoted 31 lines · Christian König
Am 29.04.24 um 11:43 schrieb Tvrtko Ursulin:
Quoted 29 lines · Felix Kuehling
Quoted 12 lines · Tvrtko Ursulin
Quoted 8 lines
From: Tvrtko Ursulin <[email protected]>
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
AMDGPU_PL_PREEMPTis mostly used for userptr BOs, which cannot be pinned. In any case you should make sure that the accounting is consistent betweenamdgpu_bo_pin_restrictedandamdgpu_bo_unpin. This patch breaks that consistency.You mean
amdgpu_bo_pin(_restricted)andamdgpu_bo_unpindo not run for such objects, or something else?If they run, then at the end of pin there is:
domain =
amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
...
} else if (domain ==AMDGPU_GEM_DOMAIN_GTT) {atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);And unpin has no handling for
AMDGPU_PL_PREEMPT.Ah I see.. does it rely on
amdgpu_mem_type_to_domainreturning 0 forAMDGPU_PL_PREEMPT? My confusion was I misread the pinning check as checking the domain as stored in the bo at creation time.Although I am still confused by the statement userptr BOs are not pinned. It is not needed to map them via GART on AMD hardware for GPU to be able to access them?
Quoted 7 lines
Quoted 3 lines
No, a GART mapping is only needed if you want to scanout from them or otherwise use them from the kernel on the GPU.
Background is that the kernel doesn't has VM with page tables..
Got it, thanks!
Presumably somewhere else in the code then it is prevented to call pin/unpin on those?
I was referring to this condition in amdgpu_bo_pin_restricted:
if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
return -EPERM;However, when I look into it more, I see that AMDGPU_PL_PREEMPT is used for other SG BOs that actually are pinned, specifically BOs created by KFD with KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL or KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP. These are very small BOs (one or two pages), and only one per process, per GPU, so I'm not sure it's worth adding special handling for them in the BO pin accounting.
Regards,
Felix
Quoted 115 lines
What to do, if anything, with the attempt to address the asymmetry in the accounting criteria between the pin and unpin?
I mean domain based on pin:
domain =
amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type); if (domain ==AMDGPU_GEM_DOMAIN_VRAM) {atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size);atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size);
} else if (domain ==AMDGPU_GEM_DOMAIN_GTT) {atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size); }Versus placement based on unpin:
if (
bo->tbo.resource->mem_type==TTM_PL_VRAM) {atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size);atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size);
} else if (bo->tbo.resource->mem_type==TTM_PL_TT) {atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); }The fact
amdgpu_mem_type_to_domainnever translates back toAMDGPU_PL_PREEMPTmeans there is indeed currently no bug.Is 2/3 still desirable to convert the check in pin to me
mem_typebased?Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Fix by extending the accounting criteria in
amdgpu_bo_unpin.What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.I think that's true. It's a very fine distinction.
AMDGPU_PL_PREEMPTdoes use system memory and it is GPU accessible, just like GTT. The only difference is, that it's not subject to the GTT limits because their eviction is handled by callbacks other than TTM evictions and doesn't need to wait for fences.As in you think those two hunks of the patch are correct?
I think so as well, yes. But we still need a name for preemptible BOs while printing them in debugfs.
Currently it looks the name is 'CPU':
amdgpu_bo_print_info()
...
caseAMDGPU_GEM_DOMAIN_CPU:
default:
placement = "CPU";
break;Also, where to account them in struct
amdgpu_mem_stats?Regards,
Tvrtko
Quoted 59 lines
Regards,
Christian.Quoted 54 lines
Regards,
Tvrtko
Quoted 48 lines
Regards,
FelixQuoted 44 lines
And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-1@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo) atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size); - } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { + } else if (bo->tbo.resource->mem_type == TTM_PL_TT || + bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) { atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
stats->gtt+= size;
if (shared)
stats->gtt_shared+= size;drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement = "VRAM"; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
placement = "GTT";
break;
caseTTM_PL_SYSTEM:
Re: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
In reply to Tvrtko UrsulinQuoted 10 lines · Tvrtko Ursulin
Quoted 8 lines · Felix Kuehling
Quoted 6 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
AMDGPU_PL_PREEMPTis mostly used for userptr BOs, which cannot be pinned. In any case you should make sure that the accounting is consistent betweenamdgpu_bo_pin_restrictedandamdgpu_bo_unpin. This patch breaks that consistency.You mean
amdgpu_bo_pin(_restricted)andamdgpu_bo_unpindo not run for such objects, or something else?
Right. amdgpu_bo_pin_restricted will return an error for userptr BOs:
if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
return -EPERM;Quoted 7 lines
If they run, then at the end of pin there is:
domain =
amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
...
} else if (domain ==AMDGPU_GEM_DOMAIN_GTT) {atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
You changed that in your patch 2:
- } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
+ } else if (bo->tbo.resource->mem_type == TTM_PL_TT ||
+ bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) {
atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
}
I was suggesting you just change this in patch 2 like this, so it matches what's done on unpin:
- } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
+ } else if (bo->tbo.resource->mem_type == TTM_PL_TT) {
atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
}
Quoted 8 lines
And unpin has no handling for
AMDGPU_PL_PREEMPT.Ah I see.. does it rely on
amdgpu_mem_type_to_domainreturning 0 forAMDGPU_PL_PREEMPT? My confusion was I misread the pinning check as checking the domain as stored in the bo at creation time.Although I am still confused by the statement userptr BOs are not pinned. It is not needed to map them via GART on AMD hardware for GPU to be able to access them?
Quoted 2 lines
Quoted 2 lines
Fix by extending the accounting criteria in
amdgpu_bo_unpin.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.I think that's true. It's a very fine distinction.
AMDGPU_PL_PREEMPTdoes use system memory and it is GPU accessible, just like GTT. The only difference is, that it's not subject to the GTT limits because their eviction is handled by callbacks other than TTM evictions and doesn't need to wait for fences.As in you think those two hunks of the patch are correct?
Yes. It seems, Christian agrees but wants to show preemptible memory separately in debugfs instead of not showing it at all.
Regards,
Felix
Quoted 54 lines
Regards,
Tvrtko
Quoted 48 lines
Regards,
FelixQuoted 44 lines
And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-1@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo) atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size); - } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { + } else if (bo->tbo.resource->mem_type == TTM_PL_TT || + bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) { atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
stats->gtt+= size;
if (shared)
stats->gtt_shared+= size;drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement = "VRAM"; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
placement = "GTT";
break;
caseTTM_PL_SYSTEM:
Re: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
Quoted 14 lines · Felix Kuehling
Quoted 13 lines · Tvrtko Ursulin
Quoted 11 lines · Felix Kuehling
Quoted 8 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
AMDGPU_PL_PREEMPTis mostly used for userptr BOs, which cannot be pinned. In any case you should make sure that the accounting is consistent betweenamdgpu_bo_pin_restrictedandamdgpu_bo_unpin. This patch breaks that consistency.You mean
amdgpu_bo_pin(_restricted)andamdgpu_bo_unpindo not run for such objects, or something else?Right.
amdgpu_bo_pin_restrictedwill return an error for userptr BOs:if (
amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) return-EPERM;
Ah I missed that, thank you!
Quoted 36 lines
Quoted 7 lines
If they run, then at the end of pin there is:
domain =
amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
...
} else if (domain ==AMDGPU_GEM_DOMAIN_GTT) {atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);You changed that in your patch 2:
- } else if (domain ==
AMDGPU_GEM_DOMAIN_GTT) {
+ } else if (bo->tbo.resource->mem_type==TTM_PL_TT|| +bo->tbo.resource->mem_type==AMDGPU_PL_PREEMPT) {
atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size); }I was suggesting you just change this in patch 2 like this, so it matches what's done on unpin:
- } else if (domain ==
AMDGPU_GEM_DOMAIN_GTT) {
+ } else if (bo->tbo.resource->mem_type==TTM_PL_TT) {
atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size); }Quoted 12 lines
And unpin has no handling for
AMDGPU_PL_PREEMPT.Ah I see.. does it rely on
amdgpu_mem_type_to_domainreturning 0 forAMDGPU_PL_PREEMPT? My confusion was I misread the pinning check as checking the domain as stored in the bo at creation time.Although I am still confused by the statement userptr BOs are not pinned. It is not needed to map them via GART on AMD hardware for GPU to be able to access them?
Quoted 6 lines
Quoted 5 lines
Fix by extending the accounting criteria in
amdgpu_bo_unpin.What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I think that's true. It's a very fine distinction.
AMDGPU_PL_PREEMPTdoes use system memory and it is GPU accessible, just like GTT. The only difference is, that it's not subject to the GTT limits because their eviction is handled by callbacks other than TTM evictions and doesn't need to wait for fences.As in you think those two hunks of the patch are correct?
Yes. It seems, Christian agrees but wants to show preemptible memory separately in debugfs instead of not showing it at all.
Okay, I've posted a respin with a name 'PREEMPTIBLE' to start the naming discussion. :)
If I did not get confused again, it is only the last patch in the series moves the reporting for those objects from 'CPU' to this new label.
This is because both the current code:
domain =
amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
switch (domain) {
...
default:
placement = "CPU";
break;And with my 2/3:
switch (bo->tbo.resource->mem_type) {
...
case TTM_PL_SYSTEM:
default:
placement = "CPU";
break;They end up in the CPU bucket. Things only change with 3/3:
Regards,
Tvrtko
Quoted 59 lines
Regards,
FelixQuoted 54 lines
Regards,
Tvrtko
Quoted 48 lines
Regards,
FelixQuoted 44 lines
And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-1@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo) atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), &adev->visible_pin_size); - } else if (bo->tbo.resource->mem_type == TTM_PL_TT) { + } else if (bo->tbo.resource->mem_type == TTM_PL_TT || + bo->tbo.resource->mem_type == AMDGPU_PL_PREEMPT) { atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
stats->gtt+= size;
if (shared)
stats->gtt_shared+= size;drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement = "VRAM"; break;case
TTM_PL_TT:
- caseAMDGPU_PL_PREEMPT:
placement = "GTT";
break;
caseTTM_PL_SYSTEM:
Re: [PATCH 3/3] drm/amdgpu: Fix pinned GART area accounting and fdinfo reporting
In reply to Tvrtko UrsulinAm 26.04.24 um 18:43 schrieb Tvrtko Ursulin:
Quoted 33 lines
From: Tvrtko Ursulin <[email protected]>
When commit
b453e42a6e8b("drm/amdgpu: Add new placement for preemptible SG BOs") added a new TTM region it missed to notice the conceptual imbalance in GART pin size accounting as done in amdgpu_bo_pin/unpin.That imbalance leads to such objects getting accounted against the resource, but are not un-accounted when unpinned.
Fix by extending the accounting criteria in
amdgpu_bo_unpin.What also aappears needs fixing is not reporting their size from the
amdgpu_bo_get_memory, which is used to implement fdinfo stats, so they are not mixed with the regular userspace created and driver owned objects.And also
amdgpu_bo_print_infofor debugfs reporting.Note that the patch depends on the previous one which broke down the relevant checks from the domain based to placement based.
Signed-off-by Tvrtko Ursulin <[email protected]>Fixes b453e42a6e8b ("drm/amdgpu: Add new placement for preemptible SG BOs")Cc Felix Kuehling <[email protected]>Cc Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_unpin(struct amdgpu_bo *bo) · line 1036
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2
-1@@ -1032,7 +1032,8 @@ void amdgpu_bo_unpin(struct amdgpu_bo *bo)atomic64_sub (amdgpu_bo_size (bo), &adev->vram_pin_size);atomic64_sub (amdgpu_vram_mgr_bo_visible_size (bo), &adev->visible_pin_size); - }else if (bo->tbo.resource->mem_type ==TTM_PL_TT ) { + }else if (bo->tbo.resource->mem_type ==TTM_PL_TT || + bo->tbo.resource->mem_type ==AMDGPU_PL_PREEMPT ) {
Good catch, but please separate that one from the other changes since we probably want to backport it.
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-1atomic64_sub (amdgpu_bo_size (bo), &adev->gart_pin_size); }@@ -1298,7 +1299,6 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo, stats->vram_shared += size;break ;case TTM_PL_TT : -case AMDGPU_PL_PREEMPT : stats->gtt += size;if (shared) stats->gtt_shared += size;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) · line 1602
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-1@@ -1599,7 +1599,6 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) placement ="VRAM" ;break ;case TTM_PL_TT : -case AMDGPU_PL_PREEMPT :
Yeah, that makes sense as well. But we need a case for AMDGPU_PL_PREEMPT here as well then.
Regards,
Christian.
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-0placement ="GTT" ;break ;case TTM_PL_SYSTEM :