drm/amdgpu: Mark amdgpu_bo as invalid after moved
Subsystems (2)
PATCH 1/1 drm/amdgpu: Mark amdgpu_bo as invalid after moved
It leads to race condition if amdgpu_bo is marked as invalid before it is really moved.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -5
@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,if (!old_mem || (old_mem->mem_type ==TTM_PL_SYSTEM && bo->ttm ==NULL )) { -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA ||
@@ -530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,if (old_mem->mem_type ==TTM_PL_SYSTEM && (new_mem->mem_type ==TTM_PL_TT || new_mem->mem_type ==AMDGPU_PL_PREEMPT )) { -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT ||
@@ -542,9 +542,9 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,return r;amdgpu_ttm_backend_unbind (bo->bdev, bo->ttm); -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }
@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }
@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,return -EMULTIHOP ; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) {/* Check that all memory is CPU accessible */
Signature · 1 line
2.25.1
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done in amdgpu_bo_move_notify
For example, amdgpu_bo_kunmap() will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.
Thanks,
Trigger
Quoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA || @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (old_mem->mem_type == TTM_PL_SYSTEM && (new_mem->mem_type == TTM_PL_TT || new_mem->mem_type == AMDGPU_PL_PREEMPT)) {drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT || @@ -542,9 +542,9 @@ staticint amdgpu_bo_move(struct
ttm_buffer_object*bo, bool evict,return r; amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) {/* Check that all memory is CPU accessible */--
2.25.1
Yeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.
Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 95 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA || @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (old_mem->mem_type == TTM_PL_SYSTEM && (new_mem->mem_type == TTM_PL_TT || new_mem->mem_type == AMDGPU_PL_PREEMPT)) {drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT || @@ -542,9 +542,9 @@ staticint amdgpu_bo_move(struct
ttm_buffer_object*bo, bool evict,return r; amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) {/* Check that all memory is CPU accessible */--
2.25.1
[AMD Official Use Only - AMD Internal Distribution Only]
We need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction. If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.
Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River
-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Mark amdgpu_bo as invalid after moved
Yeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.
Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 95 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA || @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (old_mem->mem_type == TTM_PL_SYSTEM && (new_mem->mem_type == TTM_PL_TT || new_mem->mem_type == AMDGPU_PL_PREEMPT)) {drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT || @@ -542,9 +542,9 @@static int amdgpu_bo_move(struct
ttm_buffer_object*bo, bool evict,return r; amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) {/* Check that all memory is CPU accessible */--
2.25.1
[AMD Official Use Only - AMD Internal Distribution Only]
Add more info and CC @Kuehling, Felix @cao, lin
In amdgpu_amdkfd_fence.c, there is a design description:
/* Eviction Fence
* Fence helper functions to deal with KFD memory eviction.
* Big Idea - Since KFD submissions are done by user queues, a BO cannot be
* evicted unless all the user queues for that process are evicted. *
* All the BOs in a process share an eviction fence. When process X wants
* to map VRAM memory but TTM can't find enough space, TTM will attempt to
* evict BOs from its LRU list. TTM checks if the BO is valuable to evict
* by calling ttm_device_funcs->eviction_valuable().
*
* ttm_device_funcs->eviction_valuable() - will return false if the BO belongs
* to process X. Otherwise, it will return true to indicate BO can be
* evicted by TTM.
*
* If ttm_device_funcs->eviction_valuable returns true, then TTM will continue
* the evcition process for that BO by calling ttm_bo_evict --> amdgpu_bo_move
* --> amdgpu_copy_buffer(). This sets up job in GPU scheduler.
*
* GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to
* nofity when the BO is free to move. fence_add_callback --> enable_signaling
* --> amdgpu_amdkfd_fence.enable_signaling
*
* amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
* user queues and signal fence. The work item will also start another delayed
* work item to restore BOs
*/
If mark BOs as invalidated before submitting job to move the buffer, user queue is still active.
During the time before user queue is evicted, if a drm job achieve, amdgpu_cs_vm_handling will call amdgpu_vm_handle_moved to clear the ptes of Invalidated BOs. Then page fault happens because compute shader is still accessing the "invalidated" BO.
I am not familiar with amdgpu_vm_bo state machine, so I don’t know if it is an code error or an design error.
Thanks
River
-----Original Message-----
From: YuanShang Mao (River)
Sent: Friday, July 12, 2024 10:55 AM
To: Christian König <[email protected]>
Cc: Huang, Trigger <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: Mark amdgpu_bo as invalid after moved
We need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction. If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.
Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River
-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Mark amdgpu_bo as invalid after moved
Yeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.
Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 95 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA || @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (old_mem->mem_type == TTM_PL_SYSTEM && (new_mem->mem_type == TTM_PL_TT || new_mem->mem_type == AMDGPU_PL_PREEMPT)) {drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT || @@ -542,9 +542,9 @@static int amdgpu_bo_move(struct
ttm_buffer_object*bo, bool evict,return r; amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) {/* Check that all memory is CPU accessible */--
2.25.1
Hi River,
well that isn't an error at all, this is perfectly expected behavior.
The VMs used by the KFD process are currently not meant to be used by classic CS at the same time.
This is one of the reasons for that.
Regards,
Christian.
Am 12.07.24 um 09:35 schrieb YuanShang Mao (River):
Quoted 167 lines
[AMD Official Use Only - AMD Internal Distribution Only]
Add more info and CC @Kuehling, Felix @cao, lin
In
amdgpu_amdkfd_fence.c, there is a design description:/* Eviction Fence
* Fence helper functions to deal with KFD memory eviction.
* Big Idea - Since KFD submissions are done by user queues, a BO cannot be
* evicted unless all the user queues for that process are evicted. *
* All the BOs in a process share an eviction fence. When process X wants
* to map VRAM memory but TTM can't find enough space, TTM will attempt to
* evict BOs from its LRU list. TTM checks if the BO is valuable to evict
* by callingttm_device_funcs->eviction_valuable().
*
*ttm_device_funcs->eviction_valuable()- will return false if the BO belongs
* to process X. Otherwise, it will return true to indicate BO can be
* evicted by TTM.
*
* Ifttm_device_funcs->eviction_valuablereturns true, then TTM will continue
* the evcition process for that BO by callingttm_bo_evict-->amdgpu_bo_move
* -->amdgpu_copy_buffer(). This sets up job in GPU scheduler.
*
* GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to
* nofity when the BO is free to move.fence_add_callback-->enable_signaling
* --> amdgpu_amdkfd_fence.enable_signaling
*
* amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
* user queues and signal fence. The work item will also start another delayed
* work item to restore BOs
*/If mark BOs as invalidated before submitting job to move the buffer, user queue is still active.
During the time before user queue is evicted, if a drm job achieve,amdgpu_cs_vm_handlingwill callamdgpu_vm_handle_movedto clear the ptes of Invalidated BOs. Then page fault happens because compute shader is still accessing the "invalidated" BO.I am not familiar with
amdgpu_vm_bostate machine, so I don’t know if it is an code error or an design error.Thanks
River-----Original Message-----
From: YuanShang Mao (River)
Sent: Friday, July 12, 2024 10:55 AM
To: Christian König <[email protected]>
Cc: Huang, Trigger <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedWe need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction. If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.
Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedYeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 95 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA || @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (old_mem->mem_type == TTM_PL_SYSTEM && (new_mem->mem_type == TTM_PL_TT || new_mem->mem_type == AMDGPU_PL_PREEMPT)) {drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT || @@ -542,9 +542,9 @@static int amdgpu_bo_move(struct
ttm_buffer_object*bo, bool evict,return r; amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) {/* Check that all memory is CPU accessible */--
2.25.1
KFD eviction fences are triggered by the enable_signaling callback on the eviction fence. Any move operations scheduled by amdgpu_bo_move are held up by the GPU scheduler until the eviction fence is signaled by the KFD eviction handler, which only happens after the user mode queues are stopped.
As I understand it, VM BO invalidation does not unmap anything from the page table itself. So the KFD queues are OK continue running until the eviction handler stops them and signals the fence.
However, if amdgpu_vm_handle_moved gets called before the eviction fence is signaled, then there could be a problem. In applications that do compute-graphics interop, the VM is shared between compute and graphics. So graphics and compute submissions at the same time are possible. @Christian, this is a concequence of using libdrm and insisting that each process uses only a single VM per GPU.
Regards,
Felix
Quoted 180 lines · Christian König
Hi River,
well that isn't an error at all, this is perfectly expected behavior.
The VMs used by the KFD process are currently not meant to be used by classic CS at the same time.
This is one of the reasons for that.
Regards,
Christian.Am 12.07.24 um 09:35 schrieb YuanShang Mao (River):
Quoted 167 lines
[AMD Official Use Only - AMD Internal Distribution Only]
Add more info and CC @Kuehling, Felix @cao, lin
In
amdgpu_amdkfd_fence.c, there is a design description:/* Eviction Fence
* Fence helper functions to deal with KFD memory eviction.
* Big Idea - Since KFD submissions are done by user queues, a BO cannot be * evicted unless all the user queues for that process are evicted. *
* All the BOs in a process share an eviction fence. When process X wants
* to map VRAM memory but TTM can't find enough space, TTM will attempt to
* evict BOs from its LRU list. TTM checks if the BO is valuable to evict
* by callingttm_device_funcs->eviction_valuable().
*
*ttm_device_funcs->eviction_valuable()- will return false if the BO belongs * to process X. Otherwise, it will return true to indicate BO can be * evicted by TTM.
*
* Ifttm_device_funcs->eviction_valuablereturns true, then TTM will continue
* the evcition process for that BO by callingttm_bo_evict-->amdgpu_bo_move
* -->amdgpu_copy_buffer(). This sets up job in GPU scheduler.
*
* GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to * nofity when the BO is free to move.fence_add_callback-->enable_signaling* --> amdgpu_amdkfd_fence.enable_signaling
*
* amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
* user queues and signal fence. The work item will also start another delayed
* work item to restore BOs
*/If mark BOs as invalidated before submitting job to move the buffer, user queue is still active.
During the time before user queue is evicted, if a drm job achieve,amdgpu_cs_vm_handlingwill callamdgpu_vm_handle_movedto clear the ptes of Invalidated BOs. Then page fault happens because compute shader is still accessing the "invalidated" BO.I am not familiar with
amdgpu_vm_bostate machine, so I don’t know if it is an code error or an design error.Thanks
River-----Original Message-----
From: YuanShang Mao (River)
Sent: Friday, July 12, 2024 10:55 AM
To: Christian König <[email protected]>
Cc: Huang, Trigger <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedWe need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction. If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.
Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedYeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 95 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c| 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-0@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,if (!old_mem || (
old_mem->mem_type==TTM_PL_SYSTEM&&bo->ttm== NULL)) {
-amdgpu_bo_move_notify(bo, evict, new_mem);ttm_bo_move_null(bo, new_mem);
+amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}
if (old_mem->mem_type==AMDGPU_GEM_DOMAIN_DGMA|| @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(structttm_buffer_object*bo, bool evict,
if (old_mem->mem_type==TTM_PL_SYSTEM&& (new_mem->mem_type==TTM_PL_TT||new_mem->mem_type==AMDGPU_PL_PREEMPT)) { -amdgpu_bo_move_notify(bo, evict, new_mem);ttm_bo_move_null(bo, new_mem);
+amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}
if ((old_mem->mem_type==TTM_PL_TT|| @@ -542,9 +542,9 @@ static int amdgpu_bo_move(structttm_buffer_object*bo, bool evict, return r;
amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm); -amdgpu_bo_move_notify(bo, evict, new_mem);ttm_resource_free(bo, &bo->resource);ttm_bo_assign_mem(bo, new_mem);
+amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-0@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
new_mem->mem_type==AMDGPU_PL_OA||new_mem->mem_type==AMDGPU_PL_DOORBELL) { /* Nothing to save here */
-amdgpu_bo_move_notify(bo, evict, new_mem);ttm_bo_move_null(bo, new_mem); +amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-1@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } - amdgpu_bo_move_notify(bo, evict, new_mem); if (adev->mman.buffer_funcs_enabled) r = amdgpu_move_blit(bo, evict, new_mem, old_mem);else
r =-ENODEV;
+amdgpu_bo_move_notify(bo, evict, new_mem);if (r) {
/* Check that all memory is CPU accessible */Signature · 1 line
2.25.1
Hi Felix,
yes that is a perfectly expected consequence.
The last time we talked about it the problem to solve this was that amdgpu_vm_sdma_prepare() couldn't read the fences from a resv object which wasn't locked.
That happens both during amdgpu_vm_handle_moved() as well as unlocked in validations of the page tables.
IIRC we postponed looking into the issue until it really becomes a problem which is probably now :)
Regards,
Christian.
Am 12.07.24 um 16:56 schrieb Felix Kuehling:
Quoted 189 lines
KFD eviction fences are triggered by the
enable_signalingcallback on the eviction fence. Any move operations scheduled byamdgpu_bo_moveare held up by the GPU scheduler until the eviction fence is signaled by the KFD eviction handler, which only happens after the user mode queues are stopped.As I understand it, VM BO invalidation does not unmap anything from the page table itself. So the KFD queues are OK continue running until the eviction handler stops them and signals the fence.
However, if
amdgpu_vm_handle_movedgets called before the eviction fence is signaled, then there could be a problem. In applications that do compute-graphics interop, the VM is shared between compute and graphics. So graphics and compute submissions at the same time are possible. @Christian, this is a concequence of using libdrm and insisting that each process uses only a single VM per GPU.Regards,
FelixQuoted 179 lines · Christian König
Hi River,
well that isn't an error at all, this is perfectly expected behavior.
The VMs used by the KFD process are currently not meant to be used by classic CS at the same time.
This is one of the reasons for that.
Regards,
Christian.Am 12.07.24 um 09:35 schrieb YuanShang Mao (River):
Quoted 167 lines
[AMD Official Use Only - AMD Internal Distribution Only]
Add more info and CC @Kuehling, Felix @cao, lin
In
amdgpu_amdkfd_fence.c, there is a design description:/* Eviction Fence
* Fence helper functions to deal with KFD memory eviction.
* Big Idea - Since KFD submissions are done by user queues, a BO cannot be * evicted unless all the user queues for that process are evicted. *
* All the BOs in a process share an eviction fence. When process X wants
* to map VRAM memory but TTM can't find enough space, TTM will attempt to
* evict BOs from its LRU list. TTM checks if the BO is valuable to evict
* by callingttm_device_funcs->eviction_valuable().
*
*ttm_device_funcs->eviction_valuable()- will return false if the BO belongs * to process X. Otherwise, it will return true to indicate BO can be * evicted by TTM.
*
* Ifttm_device_funcs->eviction_valuablereturns true, then TTM will continue
* the evcition process for that BO by callingttm_bo_evict-->amdgpu_bo_move
* -->amdgpu_copy_buffer(). This sets up job in GPU scheduler.
*
* GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to * nofity when the BO is free to move.fence_add_callback-->enable_signaling* --> amdgpu_amdkfd_fence.enable_signaling
*
* amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
* user queues and signal fence. The work item will also start another delayed
* work item to restore BOs
*/If mark BOs as invalidated before submitting job to move the buffer, user queue is still active.
During the time before user queue is evicted, if a drm job achieve,amdgpu_cs_vm_handlingwill callamdgpu_vm_handle_movedto clear the ptes of Invalidated BOs. Then page fault happens because compute shader is still accessing the "invalidated" BO.I am not familiar with
amdgpu_vm_bostate machine, so I don’t know if it is an code error or an design error.Thanks
River-----Original Message-----
From: YuanShang Mao (River)
Sent: Friday, July 12, 2024 10:55 AM
To: Christian König <[email protected]>
Cc: Huang, Trigger <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedWe need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction. If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.
Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedYeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 95 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { - amdgpu_bo_move_notify(bo, evict, new_mem); ttm_bo_move_null(bo, new_mem); + amdgpu_bo_move_notify(bo, evict, new_mem); return 0; }if (
old_mem->mem_type==AMDGPU_GEM_DOMAIN_DGMA|| @@ -
530,8 +530,8 @@ static int amdgpu_bo_move(structttm_buffer_object*bo, bool evict,
if (old_mem->mem_type==TTM_PL_SYSTEM&& (new_mem->mem_type==TTM_PL_TT||new_mem->mem_type==AMDGPU_PL_PREEMPT)) {
-amdgpu_bo_move_notify(bo, evict, new_mem);
ttm_bo_move_null(bo, new_mem);
+amdgpu_bo_move_notify(bo, evict, new_mem);
return 0;
}
if ((old_mem->mem_type==TTM_PL_TT|| @@ -542,9 +542,9 @@
static int amdgpu_bo_move(structttm_buffer_object*bo, bool evict,
return r;
amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
-amdgpu_bo_move_notify(bo, evict, new_mem);
ttm_resource_free(bo, &bo->resource);ttm_bo_assign_mem(bo, new_mem);
+amdgpu_bo_move_notify(bo, evict, new_mem);
return 0;
}drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2
-2@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) { /* Nothing to save here */ - amdgpu_bo_move_notify(bo, evict, new_mem); ttm_bo_move_null(bo, new_mem); + amdgpu_bo_move_notify(bo, evict, new_mem); return 0; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } - amdgpu_bo_move_notify(bo, evict, new_mem); if (adev->mman.buffer_funcs_enabled) r = amdgpu_move_blit(bo, evict, new_mem, old_mem); else r = -ENODEV; + amdgpu_bo_move_notify(bo, evict, new_mem); if (r) {/* Check that all memory is CPU accessible */
Signature · 1 line
2.25.1
Quoted 5 lines · Christian König
Hi Felix,
yes that is a perfectly expected consequence.
The last time we talked about it the problem to solve this was that
amdgpu_vm_sdma_prepare()couldn't read the fences from a resv object which wasn't locked.
Why only amdgpu_vm_sdma_prepare? Doesn't CPU page table update have the same problem?
Quoted 2 lines
That happens both during
amdgpu_vm_handle_moved()as well as unlocked in validations of the page tables.
By "unlocked validations of page table entries" do you mean the "unlocked" flag in amdgpu_vm_update_range? That should only be used for invalidating page table entries in MMU notifiers for SVM ranges. It should not affect normal BOs.
amdgpu_vm_handle_moved tries to lock the reservations. But if it fails, it clears page table entries. So this is another case of "unlocked invalidations". This one does affect normal BOs. I think amdgpu_vm_handle_moved makes an assumption that the other user of the BO is in a different VM but not the same VM. Clearing the PTEs in this VM even though the BO move is still waiting for some other VM to finish accessing it, is safe.
I think here we have a case where the BO is used by something else in the same VM. In this case we cannot safely clear the PTEs before the BO fence signals.
We want to clear the PTEs before the move happens. Otherwise we risk memory corruption. Maybe the same job that does the move blit should also invalidate the PTEs?
Regards,
Felix
Quoted 198 lines
IIRC we postponed looking into the issue until it really becomes a problem which is probably now :)
Regards,
Christian.Am 12.07.24 um 16:56 schrieb Felix Kuehling:
Quoted 190 lines
KFD eviction fences are triggered by the
enable_signalingcallback on the eviction fence. Any move operations scheduled byamdgpu_bo_moveare held up by the GPU scheduler until the eviction fence is signaled by the KFD eviction handler, which only happens after the user mode queues are stopped.As I understand it, VM BO invalidation does not unmap anything from the page table itself. So the KFD queues are OK continue running until the eviction handler stops them and signals the fence.
However, if
amdgpu_vm_handle_movedgets called before the eviction fence is signaled, then there could be a problem. In applications that do compute-graphics interop, the VM is shared between compute and graphics. So graphics and compute submissions at the same time are possible. @Christian, this is a concequence of using libdrm and insisting that each process uses only a single VM per GPU.Regards,
FelixQuoted 180 lines · Christian König
Hi River,
well that isn't an error at all, this is perfectly expected behavior.
The VMs used by the KFD process are currently not meant to be used by classic CS at the same time.
This is one of the reasons for that.
Regards,
Christian.Am 12.07.24 um 09:35 schrieb YuanShang Mao (River):
Quoted 168 lines
[AMD Official Use Only - AMD Internal Distribution Only]
Add more info and CC @Kuehling, Felix @cao, lin
In
amdgpu_amdkfd_fence.c, there is a design description:/* Eviction Fence
* Fence helper functions to deal with KFD memory eviction.
* Big Idea - Since KFD submissions are done by user queues, a BO cannot be * evicted unless all the user queues for that process are evicted. *
* All the BOs in a process share an eviction fence. When process X wants
* to map VRAM memory but TTM can't find enough space, TTM will attempt to
* evict BOs from its LRU list. TTM checks if the BO is valuable to evict
* by callingttm_device_funcs->eviction_valuable().
*
*ttm_device_funcs->eviction_valuable()- will return false if the BO belongs * to process X. Otherwise, it will return true to indicate BO can be * evicted by TTM.
*
* Ifttm_device_funcs->eviction_valuablereturns true, then TTM will continue
* the evcition process for that BO by callingttm_bo_evict-->amdgpu_bo_move
* -->amdgpu_copy_buffer(). This sets up job in GPU scheduler.
*
* GPU Scheduler (amd_sched_main) - sets up a cb (fence_add_callback) to * nofity when the BO is free to move.fence_add_callback-->enable_signaling* --> amdgpu_amdkfd_fence.enable_signaling
*
* amdgpu_amdkfd_fence.enable_signaling - Start a work item that will quiesce
* user queues and signal fence. The work item will also start another delayed
* work item to restore BOs
*/If mark BOs as invalidated before submitting job to move the buffer, user queue is still active.
During the time before user queue is evicted, if a drm job achieve,amdgpu_cs_vm_handlingwill callamdgpu_vm_handle_movedto clear the ptes of Invalidated BOs. Then page fault happens because compute shader is still accessing the "invalidated" BO.I am not familiar with
amdgpu_vm_bostate machine, so I don’t know if it is an code error or an design error.Thanks
River-----Original Message-----
From: YuanShang Mao (River)
Sent: Friday, July 12, 2024 10:55 AM
To: Christian König <[email protected]>
Cc: Huang, Trigger <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedWe need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction. If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.
Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedYeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 96 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 87 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c| 10 +++++-----1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-0@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,if (!old_mem || (
old_mem->mem_type==TTM_PL_SYSTEM&&bo->ttm== NULL)) {
-amdgpu_bo_move_notify(bo, evict, new_mem);ttm_bo_move_null(bo, new_mem);
+amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}
if (old_mem->mem_type==AMDGPU_GEM_DOMAIN_DGMA|| @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(structttm_buffer_object*bo, bool evict,
if (old_mem->mem_type==TTM_PL_SYSTEM&& (new_mem->mem_type==TTM_PL_TT||new_mem->mem_type==AMDGPU_PL_PREEMPT)) { -amdgpu_bo_move_notify(bo, evict, new_mem);ttm_bo_move_null(bo, new_mem);
+amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}
if ((old_mem->mem_type==TTM_PL_TT|| @@ -542,9 +542,9 @@
static int amdgpu_bo_move(structttm_buffer_object*bo, bool evict, return r;
amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
-amdgpu_bo_move_notify(bo, evict, new_mem);ttm_resource_free(bo, &bo->resource);ttm_bo_assign_mem(bo, new_mem); +amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-0@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
new_mem->mem_type==AMDGPU_PL_OA||new_mem->mem_type==AMDGPU_PL_DOORBELL) { /* Nothing to save here */
-amdgpu_bo_move_notify(bo, evict, new_mem);ttm_bo_move_null(bo, new_mem); +amdgpu_bo_move_notify(bo, evict, new_mem); return 0;
}drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0
-1@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } - amdgpu_bo_move_notify(bo, evict, new_mem); if (adev->mman.buffer_funcs_enabled) r = amdgpu_move_blit(bo, evict, new_mem, old_mem);else
r =-ENODEV;
+amdgpu_bo_move_notify(bo, evict, new_mem);if (r) {
/* Check that all memory is CPU accessible */Signature · 1 line
2.25.1
[AMD Official Use Only - AMD Internal Distribution Only]
Same issue on CPU page table update.
-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Thursday, July 18, 2024 12:28 AM
To: Christian König <[email protected]>; YuanShang Mao (River) <[email protected]>; Huang, Trigger <[email protected]>; [email protected]; cao, lin <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: Mark amdgpu_bo as invalid after moved
Quoted 7 lines · Christian König
Hi Felix,
yes that is a perfectly expected consequence.
The last time we talked about it the problem to solve this was that
amdgpu_vm_sdma_prepare()couldn't read the fences from a resv object which wasn't locked.
Why only amdgpu_vm_sdma_prepare? Doesn't CPU page table update have the same problem?
Quoted 3 lines
That happens both during
amdgpu_vm_handle_moved()as well as unlocked in validations of the page tables.
By "unlocked validations of page table entries" do you mean the "unlocked" flag in amdgpu_vm_update_range? That should only be used for invalidating page table entries in MMU notifiers for SVM ranges. It should not affect normal BOs.
amdgpu_vm_handle_moved tries to lock the reservations. But if it fails, it clears page table entries. So this is another case of "unlocked invalidations". This one does affect normal BOs. I think amdgpu_vm_handle_moved makes an assumption that the other user of the BO is in a different VM but not the same VM. Clearing the PTEs in this VM even though the BO move is still waiting for some other VM to finish accessing it, is safe.
I think here we have a case where the BO is used by something else in the same VM. In this case we cannot safely clear the PTEs before the BO fence signals.
We want to clear the PTEs before the move happens. Otherwise we risk memory corruption. Maybe the same job that does the move blit should also invalidate the PTEs?
Regards,
Felix
Quoted 236 lines
IIRC we postponed looking into the issue until it really becomes a problem which is probably now :)
Regards,
Christian.Am 12.07.24 um 16:56 schrieb Felix Kuehling:
Quoted 227 lines
KFD eviction fences are triggered by the
enable_signalingcallback on the eviction fence. Any move operations scheduled byamdgpu_bo_moveare held up by the GPU scheduler until the eviction fence is signaled by the KFD eviction handler, which only happens after the user mode queues are stopped.As I understand it, VM BO invalidation does not unmap anything from the page table itself. So the KFD queues are OK continue running until the eviction handler stops them and signals the fence.
However, if
amdgpu_vm_handle_movedgets called before the eviction fence is signaled, then there could be a problem. In applications that do compute-graphics interop, the VM is shared between compute and graphics. So graphics and compute submissions at the same time are possible. @Christian, this is a concequence of using libdrm and insisting that each process uses only a single VM per GPU.Regards,
FelixQuoted 206 lines · Christian König
Hi River,
well that isn't an error at all, this is perfectly expected behavior.
The VMs used by the KFD process are currently not meant to be used by classic CS at the same time.
This is one of the reasons for that.
Regards,
Christian.Am 12.07.24 um 09:35 schrieb YuanShang Mao (River):
Quoted 193 lines
[AMD Official Use Only - AMD Internal Distribution Only]
Add more info and CC @Kuehling, Felix @cao, lin
In
amdgpu_amdkfd_fence.c, there is a design description:/* Eviction Fence
* Fence helper functions to deal with KFD memory eviction.
* Big Idea - Since KFD submissions are done by user queues, a BO
cannot be
* evicted unless all the user queues for that process are evicted. *
* All the BOs in a process share an eviction fence. When process
X wants
* to map VRAM memory but TTM can't find enough space, TTM will
attempt to
* evict BOs from its LRU list. TTM checks if the BO is valuable
to evict
* by callingttm_device_funcs->eviction_valuable().
*
*ttm_device_funcs->eviction_valuable()- will return false if
the BO belongs
* to process X. Otherwise, it will return true to indicate BO
can be
* evicted by TTM.
*
* Ifttm_device_funcs->eviction_valuablereturns true, then TTM
will continue
* the evcition process for that BO by callingttm_bo_evict-->amdgpu_bo_move
* -->amdgpu_copy_buffer(). This sets up job in GPU scheduler. *
* GPU Scheduler (amd_sched_main) - sets up a cb
(fence_add_callback) to
* nofity when the BO is free to move.fence_add_callback-->enable_signaling
* --> amdgpu_amdkfd_fence.enable_signaling
*
* amdgpu_amdkfd_fence.enable_signaling - Start a work item that
will quiesce
* user queues and signal fence. The work item will also start
another delayed
* work item to restore BOs
*/If mark BOs as invalidated before submitting job to move the buffer, user queue is still active.
During the time before user queue is evicted, if a drm job achieve,amdgpu_cs_vm_handlingwill callamdgpu_vm_handle_movedto clear the ptes of
Invalidated BOs. Then page fault happens because compute shader is still accessing the "invalidated" BO.I am not familiar with
amdgpu_vm_bostate machine, so I don’t know if it is an code error or an design error.Thanks
River-----Original Message-----
From: YuanShang Mao (River)
Sent: Friday, July 12, 2024 10:55 AM
To: Christian König <[email protected]>
Cc: Huang, Trigger <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedWe need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction.
If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedYeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 99 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notifyFor example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 88 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA || @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (old_mem->mem_type == TTM_PL_SYSTEM && (new_mem->mem_type == TTM_PL_TT || new_mem->mem_type == AMDGPU_PL_PREEMPT)) {drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2
-1-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT || @@ -542,9 +542,9 @@static int amdgpu_bo_move(struct
ttm_buffer_object*bo, bool evict,return r;
amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) { /* Check that all memory is CPU accessible */--
2.25.1
Hi River,
well that is complete nonsense.
The BOs are not meant to be used with on demand faulting, so their VM state absolutely shouldn't matter to the KFD.
Regards,
Christian.
Am 12.07.24 um 04:54 schrieb YuanShang Mao (River):
Quoted 119 lines
[AMD Official Use Only - AMD Internal Distribution Only]
We need to make sure that all BOs of an active kfd process validated. Moving buffer will trigger process eviction. If mark it as invalided before process eviction, related kfd process is still active and may attempt to access this invalidated BO.
Agree with Trigger. Seems kfd eviction should been synced to move notify, not the move action.
Thanks
River-----Original Message-----
From: Christian König <[email protected]>
Sent: Thursday, July 11, 2024 8:39 PM
To: Huang, Trigger <[email protected]>; YuanShang Mao (River) <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedYeah, completely agree. This patch doesn't really make sense.
Please explain why you would want to do this?
Regards,
Christian.Am 11.07.24 um 13:56 schrieb Huang, Trigger:
Quoted 95 lines
[AMD Official Use Only - AMD Internal Distribution Only]
This patch seems to be wrong.
Quite a lot of preparations have been done inamdgpu_bo_move_notify
For example,amdgpu_bo_kunmap()will be called to prevent the BO from being accessed by CPU. If not called, the CPU may attempt to access the BO while it is being moved.Thanks,
TriggerQuoted 86 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of YuanShang
Sent: Thursday, July 11, 2024 5:10 PM
To: [email protected]
Cc: YuanShang Mao (River) <[email protected]>; YuanShang Mao (River) <[email protected]>
Subject: [PATCH] drm/amdgpu: Markamdgpu_boas invalid after movedCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
It leads to race condition if
amdgpu_bois marked as invalid before it is really moved.Signed-off-by YuanShang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1@@ -519,8 +519,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL)) { -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }if (old_mem->mem_type == AMDGPU_GEM_DOMAIN_DGMA || @@ - 530,8 +530,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, if (old_mem->mem_type == TTM_PL_SYSTEM && (new_mem->mem_type == TTM_PL_TT || new_mem->mem_type == AMDGPU_PL_PREEMPT)) {drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1
-1-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_bo_move_null (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; } if ((old_mem->mem_type == TTM_PL_TT || @@ -542,9 +542,9 @@static int amdgpu_bo_move(struct
ttm_buffer_object*bo, bool evict,return r; amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3-amdgpu_bo_move_notify (bo, evict, new_mem);ttm_resource_free (bo, &bo->resource);ttm_bo_assign_mem (bo, new_mem); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -557,8 +557,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, new_mem->mem_type == AMDGPU_PL_OA || new_mem->mem_type == AMDGPU_PL_DOORBELL) {/* Nothing to save here */ -amdgpu_bo_move_notify (bo ,evict ,new_mem );ttm_bo_move_null (bo ,new_mem ); +amdgpu_bo_move_notify (bo, evict, new_mem);return 0 ; }@@ -583,11 +583,11 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, return -EMULTIHOP; } -amdgpu_bo_move_notify (bo, evict, new_mem);if (adev->mman.buffer_funcs_enabled) r =amdgpu_move_blit (bo, evict, new_mem, old_mem);else r = -ENODEV ; +amdgpu_bo_move_notify (bo, evict, new_mem);if (r) {/* Check that all memory is CPU accessible */--
2.25.1