drm/amdgpu: don't wait for the SPM VMID's jobs under the VMID manager lock
Subsystems (2)
PATCH v2 1/3 drm/amdgpu: don't wait for the SPM VMID's jobs under the VMID manager lock
amdgpu_vmid_free_spm() waits for the jobs of the reserved VMID while holding the VMID manager lock of the hub, which every VMID grab on the hub takes as well. Freeing the reservation, by UNRESERVE_VMID or when the VM is destroyed, so stalls the kernel queue submissions of every process on the GFXHUB until those jobs completed, or until the job timeout if one of them hangs. A full GPU reset even deadlocks, since amdgpu_vmid_reset_all() needs the lock before the scheduler is started again to signal the jobs.
On navi31, with a GFX job of the reserving process that waits for 1s, an unrelated process' SDMA NOP takes 0.8s instead of no time when the VMID gets unreserved meanwhile.
Wait for the submissions of the VM in the callers instead, through the fences of the root PD, before taking the lock. Use DMA_RESV_USAGE_READ, which covers the command submissions but not the KFD eviction fence. UNRESERVE_VMID keeps the root PD reserved while waiting and freeing the VMID, so that no new submission can grab the VMID in between, and only waits when the VM actually has the VMID reserved.
v2: wait for the root PD's reservation object in the callers instead of
accessing id->active without the lock (Christian)
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 8 +++----- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +3 -5
@@ -634,8 +634,9 @@ int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,* @vm: the VM with the reserved ID * @vmhub: the VMHUB which should be used * -* Waits for all work that used the reserved VMID to finish before unbinding -* its page table, so submissions still in flight don't suddenly fault. +* The caller must wait for all work that used the reserved VMID to finish +* before, so submissions still in flight don't suddenly fault when its page +* table gets unbound. */ void amdgpu_vmid_free_spm (struct amdgpu_device *adev,struct amdgpu_vm *vm,unsigned vmhub)
@@ -649,9 +650,6 @@ void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, vm->reserved_vmid[vmhub] =NULL ; id_mgr->reserved_vmid = false; -/* Only for kernel queues and kfd/userqueues by pass that */ -amdgpu_sync_wait (&id->active, false); -if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs)amdgpu_gmc_bind_vmid (adev,AMDGPU_GFXHUB (0 ), adev->vm_manager.spm_vmid,0 );
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +15 -1
@@ -2852,6 +2852,10 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) root =amdgpu_bo_ref (vm->root.bo);amdgpu_bo_reserve (root, true); +/* Wait for the jobs which might still use the reserved VMID */ +if (vm->reserved_vmid[AMDGPU_GFXHUB (0 )]) +dma_resv_wait_timeout (root->tbo.base.resv,DMA_RESV_USAGE_READ , + false,MAX_SCHEDULE_TIMEOUT );dma_fence_wait (vm->last_unlocked, false);dma_fence_put (vm->last_unlocked);dma_fence_wait (vm->last_tlb_flush, false);
@@ -3002,9 +3006,19 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) }return r; } -case AMDGPU_VM_OP_UNRESERVE_VMID : +case AMDGPU_VM_OP_UNRESERVE_VMID : { +struct amdgpu_bo *root = vm->root.bo; + +amdgpu_bo_reserve (root, true); +/* Wait for the jobs which might still use the reserved VMID */ +if (vm->reserved_vmid[AMDGPU_GFXHUB (0 )]) +dma_resv_wait_timeout (root->tbo.base.resv, +DMA_RESV_USAGE_READ , false, +MAX_SCHEDULE_TIMEOUT );amdgpu_vmid_free_spm (adev, vm,AMDGPU_GFXHUB (0 )); +amdgpu_bo_unreserve (root);break ; + }default :return -EINVAL ; }
Signature · 1 line
2.50.1
PATCH v2 2/3 drm/amdgpu: keep the CS user fence BO in GTT
The CS ioctl hands the GPU the GART address of the user fence BO, but since the BO keeps its own preferred and allowed domains, it no longer makes sure the BO is in GTT. A user fence in a VRAM BO, which only needs to be a page in size, gets its address from amdgpu_bo_gpu_offset(), which warns about non-contiguous VRAM. On navi31, a CS with a user fence in a 4K VRAM BO, or in one created with AMDGPU_GEM_CREATE_VM_ALWAYS_VALID:
WARNING: amdgpu_object.c:1531 at amdgpu_bo_gpu_offset+0x86/0xb0 [amdgpu], CPU#11: cs_gem_checks/18393 RIP: 0010:amdgpu_bo_gpu_offset+0x86/0xb0 [amdgpu] Call Trace: <TASK> amdgpu_cs_parser_bos.isra.0+0x4eb/0x850 [amdgpu] amdgpu_cs_ioctl+0x97e/0x2510 [amdgpu] drm_ioctl_kernel+0xac/0x110 [drm] drm_ioctl+0x2c6/0x5b0 [drm] __x64_sys_ioctl+0xa0/0xf0 do_syscall_64+0xa3/0x4d0 entry_SYSCALL_64_after_hwframe+0x76/0x7e
and BOs which can't be in GTT at all, like GWS or OA ones of a page in size, pass the checks as well.
Only take BOs which can be in GTT as user fence and validate them only there, like before, along with the other BOs of the CS. Validate the user fence BO explicitly as well, a per VM BO shares the reservation of the root PD and so isn't one of the locked objects of the CS.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +17 -1
@@ -153,6 +153,9 @@ static int amdgpu_cs_p1_user_fence(struct amdgpu_cs_parser *p,if (amdgpu_ttm_tt_get_usermm (p->uf_bo->tbo.ttm))return -EINVAL ; +if (!(p->uf_bo->allowed_domains &AMDGPU_GEM_DOMAIN_GTT )) +return -EINVAL ; + *offset = data->offset;return 0 ; }
@@ -823,6 +826,14 @@ static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo)uint32_t domain;int r; +/* The GPU writes the user fence through the GART */ +if (bo == p->uf_bo) { +if (bo->tbo.pin_count) +return bo->tbo.resource->mem_type ==TTM_PL_TT ?0 : -EINVAL ; + domain =AMDGPU_GEM_DOMAIN_GTT ; + goto retry; + } +if (bo->tbo.pin_count)return 0 ;
@@ -858,7 +869,8 @@ static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo) amdgpu_res_cpu_visible(adev, bo->tbo.resource)) p->bytes_moved_vis += ctx.bytes_moved; -if (unlikely (r == -ENOMEM ) && domain != bo->allowed_domains) { +if (unlikely (r == -ENOMEM ) && domain != bo->allowed_domains && + bo != p->uf_bo) { domain = bo->allowed_domains; goto retry; }
@@ -992,6 +1004,10 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, }if (p->uf_bo) { + r =amdgpu_cs_bo_validate (p, p->uf_bo); +if (unlikely (r)) + goto out_free_user_pages; + r =amdgpu_ttm_alloc_gart (&p->uf_bo->tbo);if (unlikely (r)) goto out_free_user_pages;
Signature · 1 line
2.50.1
Christian König
maintainerRe: [PATCH V2 2/3] drm/amdgpu: keep the CS user fence BO in GTT
Quoted 38 lines · Jesse Zhang
The CS ioctl hands the GPU the GART address of the user fence BO, but since the BO keeps its own preferred and allowed domains, it no longer makes sure the BO is in GTT. A user fence in a VRAM BO, which only needs to be a page in size, gets its address from
amdgpu_bo_gpu_offset(), which warns about non-contiguous VRAM. On navi31, a CS with a user fence in a 4K VRAM BO, or in one created withAMDGPU_GEM_CREATE_VM_ALWAYS_VALID:WARNING: amdgpu_object.c:1531 at amdgpu_bo_gpu_offset+0x86/0xb0 [amdgpu], CPU#11: cs_gem_checks/18393 RIP: 0010:amdgpu_bo_gpu_offset+0x86/0xb0 [amdgpu] Call Trace: <TASK> amdgpu_cs_parser_bos.isra.0+0x4eb/0x850 [amdgpu] amdgpu_cs_ioctl+0x97e/0x2510 [amdgpu] drm_ioctl_kernel+0xac/0x110 [drm] drm_ioctl+0x2c6/0x5b0 [drm] __x64_sys_ioctl+0xa0/0xf0 do_syscall_64+0xa3/0x4d0 entry_SYSCALL_64_after_hwframe+0x76/0x7eand BOs which can't be in GTT at all, like GWS or OA ones of a page in size, pass the checks as well.
Only take BOs which can be in GTT as user fence and validate them only there, like before, along with the other BOs of the CS. Validate the user fence BO explicitly as well, a per VM BO shares the reservation of the root PD and so isn't one of the locked objects of the CS.
Fixes 1ea863fd736e ("drm/amdgpu: keep the prefered/allowed domains in the BO")Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_p1_user_fence(struct amdgpu_cs_parser *p, · line 153
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0
-0@@ -153,6 +153,9 @@ static int amdgpu_cs_p1_user_fence(struct amdgpu_cs_parser *p,if (amdgpu_ttm_tt_get_usermm (p->uf_bo->tbo.ttm))
Oh! While at it that check here is incorrect as well.
That should probably call amdgpu_ttm_tt_is_userptr() instead.
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_p1_user_fence(struct amdgpu_cs_parser *p, · line 156
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1
-0return -EINVAL ; +if (!(p->uf_bo->allowed_domains &AMDGPU_GEM_DOMAIN_GTT ))
I would harden this check here, e.g. test for:
(p->uf_bo->preferred_domains != AMDGPU_GEM_DOMAIN_GTT) && (p->uf_bo->allowed_domains != AMDGPU_GEM_DOMAIN_GTT)
This way you can also remove all the other checks because the buffer can only be in GTT and nowhere else.
Background is that I'm pretty sure some of the other checks are not sufficient to have the buffer not pinned elsewhere.
Regards,
Christian.
Quoted 41 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16
-1+return -EINVAL ; + *offset = data->offset;return 0 ; }@@ -823,6 +826,14 @@ static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo)uint32_t domain;int r; +/* The GPU writes the user fence through the GART */ +if (bo == p->uf_bo) { +if (bo->tbo.pin_count) +return bo->tbo.resource->mem_type ==TTM_PL_TT ?0 : -EINVAL ; + domain =AMDGPU_GEM_DOMAIN_GTT ; + goto retry; + } +if (bo->tbo.pin_count)return 0 ;@@ -858,7 +869,8 @@ static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo) amdgpu_res_cpu_visible(adev, bo->tbo.resource)) p->bytes_moved_vis += ctx.bytes_moved; -if (unlikely (r == -ENOMEM ) && domain != bo->allowed_domains) { +if (unlikely (r == -ENOMEM ) && domain != bo->allowed_domains && + bo != p->uf_bo) { domain = bo->allowed_domains; goto retry; }@@ -992,6 +1004,10 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, }if (p->uf_bo) { + r =amdgpu_cs_bo_validate (p, p->uf_bo); +if (unlikely (r)) + goto out_free_user_pages; + r =amdgpu_ttm_alloc_gart (&p->uf_bo->tbo);if (unlikely (r)) goto out_free_user_pages;
PATCH v2 3/3 drm/amdgpu: reject CS chunks too large for kvmalloc()
In reply to Jesse ZhangThe CS ioctl copies every chunk with vmemdup_array_user() before it looks at it, for whatever length_dw userspace gives. kvmalloc() warns about allocations above INT_MAX when called without __GFP_NOWARN, so any process with the render node can trigger a warning. On navi31, a CS with a chunk of length_dw 0x20000000 returns -ENOMEM after:
WARNING: mm/slub.c:6841 at __kvmalloc_node_noprof+0x5a5/0x7d0
Reject such chunks with -EINVAL before trying to copy them.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -0
@@ -221,6 +221,12 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p, size = p->chunks[i].length_dw; +if (size >INT_MAX /sizeof (uint32_t )) { + ret = -EINVAL ; + i--; + goto free_partial_kdata; + } + p->chunks[i].kdata =vmemdup_array_user (u64_to_user_ptr (user_chunk.chunk_data), size,sizeof (uint32_t ));
Signature · 1 line
2.50.1
Quoted 20 lines · Jesse Zhang
amdgpu_vmid_free_spm()waits for the jobs of the reserved VMID while holding the VMID manager lock of the hub, which every VMID grab on the hub takes as well. Freeing the reservation, byUNRESERVE_VMIDor when the VM is destroyed, so stalls the kernel queue submissions of every process on the GFXHUB until those jobs completed, or until the job timeout if one of them hangs. A full GPU reset even deadlocks, sinceamdgpu_vmid_reset_all()needs the lock before the scheduler is started again to signal the jobs.On navi31, with a GFX job of the reserving process that waits for 1s, an unrelated process' SDMA NOP takes 0.8s instead of no time when the VMID gets unreserved meanwhile.
Wait for the submissions of the VM in the callers instead, through the fences of the root PD, before taking the lock. Use
DMA_RESV_USAGE_READ, which covers the command submissions but not the KFD eviction fence.UNRESERVE_VMIDkeeps the root PD reserved while waiting and freeing the VMID, so that no new submission can grab the VMID in between, and only waits when the VM actually has the VMID reserved.
Quoted 5 lines
v2: wait for the root PD's reservation object in the callers instead of
accessing id->active without the lock (Christian)Fixes 9b47649cabf4 ("drm/amdgpu: bind the SPM VMID's page table to the reserving VM")Signed-off-by Jesse Zhang <[email protected]>
Quoted 67 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 8 +++----- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +3
-5@@ -634,8 +634,9 @@ int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,* @vm: the VM with the reserved ID * @vmhub: the VMHUB which should be used * -* Waits for all work that used the reserved VMID to finish before unbinding -* its page table, so submissions still in flight don't suddenly fault. +* The caller must wait for all work that used the reserved VMID to finish +* before, so submissions still in flight don't suddenly fault when its page +* table gets unbound. */ void amdgpu_vmid_free_spm (struct amdgpu_device *adev,struct amdgpu_vm *vm,unsigned vmhub)@@ -649,9 +650,6 @@ void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm, vm->reserved_vmid[vmhub] =NULL ; id_mgr->reserved_vmid = false; -/* Only for kernel queues and kfd/userqueues by pass that */ -amdgpu_sync_wait (&id->active, false); -if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs)amdgpu_gmc_bind_vmid (adev,AMDGPU_GFXHUB (0 ), adev->vm_manager.spm_vmid,0 );drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +15
-1@@ -2852,6 +2852,10 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) root =amdgpu_bo_ref (vm->root.bo);amdgpu_bo_reserve (root, true); +/* Wait for the jobs which might still use the reserved VMID */ +if (vm->reserved_vmid[AMDGPU_GFXHUB (0 )]) +dma_resv_wait_timeout (root->tbo.base.resv,DMA_RESV_USAGE_READ , + false,MAX_SCHEDULE_TIMEOUT );dma_fence_wait (vm->last_unlocked, false);dma_fence_put (vm->last_unlocked);dma_fence_wait (vm->last_tlb_flush, false);@@ -3002,9 +3006,19 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) }return r; } -case AMDGPU_VM_OP_UNRESERVE_VMID : +case AMDGPU_VM_OP_UNRESERVE_VMID : { +struct amdgpu_bo *root = vm->root.bo; + +amdgpu_bo_reserve (root, true); +/* Wait for the jobs which might still use the reserved VMID */ +if (vm->reserved_vmid[AMDGPU_GFXHUB (0 )]) +dma_resv_wait_timeout (root->tbo.base.resv, +DMA_RESV_USAGE_READ , false, +MAX_SCHEDULE_TIMEOUT );amdgpu_vmid_free_spm (adev, vm,AMDGPU_GFXHUB (0 )); +amdgpu_bo_unreserve (root);break ; + }default :return -EINVAL ; }