drm/amdgpu/userq: fix access to stale wptr mapping
Subsystems (2)
PATCH 1/1 drm/amdgpu/userq: fix access to stale wptr mapping
Use drm_exec to take both locks i.e vm root bo and wptr_obj bo to access the mapping data properly.
This fixes the security issue of unmap the wptr_obj while a queue creation is in progress and passing other bo at same address.
--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 122 ++++++++++----------- 1 file changed, 57 insertions(+), 65 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +57 -65
@@ -30,34 +30,6 @@#define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE#define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE -static int -mes_userq_map_gtt_bo_to_gart (struct amdgpu_bo *bo) -{ -int ret; - - ret =amdgpu_bo_reserve (bo, true); -if (ret) { -DRM_ERROR ("Failed to reserve bo. ret %d\n" , ret); - goto err_reserve_bo_failed; - } - - ret =amdgpu_ttm_alloc_gart (&bo->tbo); -if (ret) { -DRM_ERROR ("Failed to bind bo to GART. ret %d\n" , ret); - goto err_map_bo_gart_failed; - } - -amdgpu_bo_unreserve (bo); - bo =amdgpu_bo_ref (bo); - -return 0 ; - -err_map_bo_gart_failed: -amdgpu_bo_unreserve (bo); -err_reserve_bo_failed: -return ret; -} - static int mes_userq_create_wptr_mapping(struct amdgpu_device *adev, struct amdgpu_userq_mgr *uq_mgr,
@@ -65,55 +37,75 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev, uint64_t wptr) {struct amdgpu_bo_va_mapping *wptr_mapping; -struct amdgpu_vm *wptr_vm;struct amdgpu_userq_obj *wptr_obj = &queue->wptr_obj; +struct amdgpu_bo *obj; +struct amdgpu_vm *vm = queue->vm; +struct drm_exec exec;int ret; - wptr_vm = queue->vm; - ret =amdgpu_bo_reserve (wptr_vm->root.bo, false); -if (ret) -return ret; - wptr &=AMDGPU_GMC_HOLE_MASK ; - wptr_mapping =amdgpu_vm_bo_lookup_mapping (wptr_vm, wptr >>PAGE_SHIFT ); -amdgpu_bo_unreserve (wptr_vm->root.bo); -if (!wptr_mapping) { -DRM_ERROR ("Failed to lookup wptr bo\n" ); -return -EINVAL ; - } - wptr_obj->obj = wptr_mapping->bo_va->base.bo; -if (wptr_obj->obj->tbo.base.size >PAGE_SIZE ) { -DRM_ERROR ("Requested GART mapping for wptr bo larger than one page\n" ); -return -EINVAL ; - } +drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); + drm_exec_until_all_locked(&exec) { + ret =amdgpu_vm_lock_pd (vm, &exec,1 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) + goto fail_lock; + + wptr_mapping =amdgpu_vm_bo_lookup_mapping (vm, wptr >>PAGE_SHIFT ); +if (!wptr_mapping) { +DRM_ERROR ("Failed to lock up wptr bo\n" ); + ret = -EINVAL ; + goto fail_lock; + } - ret =mes_userq_map_gtt_bo_to_gart (wptr_obj->obj); -if (ret) { -DRM_ERROR ("Failed to map wptr bo to GART\n" ); -return ret; - } + obj = wptr_mapping->bo_va->base.bo; + ret =drm_exec_prepare_obj (&exec, &obj->tbo.base,1 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) { +DRM_ERROR ("Failed to prepare wptr bo\n" ); + goto fail_lock; + } - ret =amdgpu_bo_reserve (wptr_obj->obj, true); -if (ret) { -DRM_ERROR ("Failed to reserve wptr bo\n" ); -return ret; - } +/* mapping now should be stable since both the locks are held */ + wptr_mapping =amdgpu_vm_bo_lookup_mapping (vm, wptr >>PAGE_SHIFT ); +if (!wptr_mapping) { +DRM_ERROR ("Failed to lock up wptr bo\n" ); + ret = -EINVAL ; + goto fail_lock; + } -/* TODO use eviction fence instead of pinning. */ - ret =amdgpu_bo_pin (wptr_obj->obj,AMDGPU_GEM_DOMAIN_GTT ); -if (ret) { -drm_file_err (uq_mgr->file,"[Usermode queues] Failed to pin wptr bo\n" ); - goto unresv_bo; - } + wptr_obj->obj =amdgpu_bo_ref (wptr_mapping->bo_va->base.bo); + +if (wptr_obj->obj->tbo.base.size >PAGE_SIZE ) { +DRM_ERROR ("Requested wptr bo size is larger than one page\n" ); + ret = -EINVAL ; + goto fail_map; + } + + ret =amdgpu_ttm_alloc_gart (&wptr_obj->obj->tbo); +if (ret) { +DRM_ERROR ("Failed to bind bo to GART. ret %d\n" , ret); + goto fail_map; + } + +/* TODO use eviction fence instead of pinning. */ + ret =amdgpu_bo_pin (wptr_obj->obj,AMDGPU_GEM_DOMAIN_GTT ); +if (ret) { +DRM_ERROR ("Failed to pin wptr bo. ret %d\n" , ret); + goto fail_map; + } - queue->wptr_obj.gpu_addr =amdgpu_bo_gpu_offset (wptr_obj->obj); -amdgpu_bo_unreserve (wptr_obj->obj); + queue->wptr_obj.gpu_addr =amdgpu_bo_gpu_offset (wptr_obj->obj); + } +drm_exec_fini (&exec);return 0 ; -unresv_bo: -amdgpu_bo_unreserve (wptr_obj->obj); +fail_map: +amdgpu_bo_unref (&wptr_obj->obj); +fail_lock: +drm_exec_fini (&exec);return ret; }
Signature · 1 line
2.34.1
Christian König
maintainerQuoted 71 lines · Sunil Khatri
Use
drm_execto take both locks i.e vm root bo andwptr_objbo to access the mapping data properly.This fixes the security issue of unmap the
wptr_objwhile a queue creation is in progress and passing other bo at same address.Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 122 ++++++++++----------- 1 file changed, 57 insertions(+), 65 deletions(-)drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-37@@ -30,34 +30,6 @@#define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE#define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE -static int -mes_userq_map_gtt_bo_to_gart (struct amdgpu_bo *bo) -{ -int ret; - - ret =amdgpu_bo_reserve (bo, true); -if (ret) { -DRM_ERROR ("Failed to reserve bo. ret %d\n" , ret); - goto err_reserve_bo_failed; - } - - ret =amdgpu_ttm_alloc_gart (&bo->tbo); -if (ret) { -DRM_ERROR ("Failed to bind bo to GART. ret %d\n" , ret); - goto err_map_bo_gart_failed; - } - -amdgpu_bo_unreserve (bo); - bo =amdgpu_bo_ref (bo); - -return 0 ; - -err_map_bo_gart_failed: -amdgpu_bo_unreserve (bo); -err_reserve_bo_failed: -return ret; -} - static int mes_userq_create_wptr_mapping(struct amdgpu_device *adev, struct amdgpu_userq_mgr *uq_mgr,@@ -65,55 +37,75 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev, uint64_t wptr) {struct amdgpu_bo_va_mapping *wptr_mapping; -struct amdgpu_vm *wptr_vm;struct amdgpu_userq_obj *wptr_obj = &queue->wptr_obj; +struct amdgpu_bo *obj; +struct amdgpu_vm *vm = queue->vm; +struct drm_exec exec;int ret; - wptr_vm = queue->vm; - ret =amdgpu_bo_reserve (wptr_vm->root.bo, false); -if (ret) -return ret; - wptr &=AMDGPU_GMC_HOLE_MASK ; - wptr_mapping =amdgpu_vm_bo_lookup_mapping (wptr_vm, wptr >>PAGE_SHIFT ); -amdgpu_bo_unreserve (wptr_vm->root.bo); -if (!wptr_mapping) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 48
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-8-DRM_ERROR ("Failed to lookup wptr bo\n" ); -return -EINVAL ; - } - wptr_obj->obj = wptr_mapping->bo_va->base.bo; -if (wptr_obj->obj->tbo.base.size >PAGE_SIZE ) { -DRM_ERROR ("Requested GART mapping for wptr bo larger than one page\n" ); -return -EINVAL ; - } +drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 );
This should probably be 2 instead of 0.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 57
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +9
-0+ drm_exec_until_all_locked(&exec) { + ret =amdgpu_vm_lock_pd (vm, &exec,1 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) + goto fail_lock; + + wptr_mapping =amdgpu_vm_bo_lookup_mapping (vm, wptr >>PAGE_SHIFT ); +if (!wptr_mapping) { +DRM_ERROR ("Failed to lock up wptr bo\n" );
Please drop that error message. It can spam the logs when userspace intentionally gives incorrect values.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 63
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-5+ ret = -EINVAL ; + goto fail_lock; + } - ret =mes_userq_map_gtt_bo_to_gart (wptr_obj->obj); -if (ret) { -DRM_ERROR ("Failed to map wptr bo to GART\n" ); -return ret; - } + obj = wptr_mapping->bo_va->base.bo; + ret =drm_exec_prepare_obj (&exec, &obj->tbo.base,1 );
Using drm_exec_lock_obj() should be sufficient.
We don't need a fence slot for this use case.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 66
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-0+drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) { +DRM_ERROR ("Failed to prepare wptr bo\n" );
Same here, that this can fail is normal handling. The worst case is OOM and that is already printed in the logs.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 68
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+ goto fail_lock; + }
We got all the locks now, so the drm_exec_until_all_locked() loop can be closed here.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-4- ret =amdgpu_bo_reserve (wptr_obj->obj, true); -if (ret) { -DRM_ERROR ("Failed to reserve wptr bo\n" ); -return ret;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 76
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7
-1- } +/* mapping now should be stable since both the locks are held */ + wptr_mapping =amdgpu_vm_bo_lookup_mapping (vm, wptr >>PAGE_SHIFT ); +if (!wptr_mapping) { +DRM_ERROR ("Failed to lock up wptr bo\n" ); + ret = -EINVAL ; + goto fail_lock; + }
Doing that again is unecessary. We are holding the VM lock above while doing the lockup.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 78
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-6-/* TODO use eviction fence instead of pinning. */ - ret =amdgpu_bo_pin (wptr_obj->obj,AMDGPU_GEM_DOMAIN_GTT ); -if (ret) { -drm_file_err (uq_mgr->file,"[Usermode queues] Failed to pin wptr bo\n" ); - goto unresv_bo; - } + wptr_obj->obj =amdgpu_bo_ref (wptr_mapping->bo_va->base.bo);
That is now unecessary as well.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 81
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-0+ +if (wptr_obj->obj->tbo.base.size >PAGE_SIZE ) { +DRM_ERROR ("Requested wptr bo size is larger than one page\n" );
Same, please drop that error message.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 88
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7
-0+ ret = -EINVAL ; + goto fail_map; + } + + ret =amdgpu_ttm_alloc_gart (&wptr_obj->obj->tbo); +if (ret) { +DRM_ERROR ("Failed to bind bo to GART. ret %d\n" , ret);
That error message is useful.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 93
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-0+ goto fail_map; + } + +/* TODO use eviction fence instead of pinning. */ + ret =amdgpu_bo_pin (wptr_obj->obj,AMDGPU_GEM_DOMAIN_GTT );
Oh! The piun needs to come before the alloc_gart! That's wrong in the existing code as well.
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 95
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+if (ret) { +DRM_ERROR ("Failed to pin wptr bo. ret %d\n" , ret);
That error message makes sense.
Regards,
Christian.
Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +9
-4+ goto fail_map; + } - queue->wptr_obj.gpu_addr =amdgpu_bo_gpu_offset (wptr_obj->obj); -amdgpu_bo_unreserve (wptr_obj->obj); + queue->wptr_obj.gpu_addr =amdgpu_bo_gpu_offset (wptr_obj->obj); + } +drm_exec_fini (&exec);return 0 ; -unresv_bo: -amdgpu_bo_unreserve (wptr_obj->obj); +fail_map: +amdgpu_bo_unref (&wptr_obj->obj); +fail_lock: +drm_exec_fini (&exec);return ret; }
Quoted 76 lines · Christian König
Quoted 75 lines · Sunil Khatri
Use
drm_execto take both locks i.e vm root bo andwptr_objbo to access the mapping data properly.This fixes the security issue of unmap the
wptr_objwhile a queue creation is in progress and passing other bo at same address.Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 122 ++++++++++----------- 1 file changed, 57 insertions(+), 65 deletions(-)drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-40@@ -30,34 +30,6 @@#define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE#define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE -static int -mes_userq_map_gtt_bo_to_gart (struct amdgpu_bo *bo) -{ -int ret; - - ret =amdgpu_bo_reserve (bo, true); -if (ret) { -DRM_ERROR ("Failed to reserve bo. ret %d\n" , ret); - goto err_reserve_bo_failed; - } - - ret =amdgpu_ttm_alloc_gart (&bo->tbo); -if (ret) { -DRM_ERROR ("Failed to bind bo to GART. ret %d\n" , ret); - goto err_map_bo_gart_failed; - } - -amdgpu_bo_unreserve (bo); - bo =amdgpu_bo_ref (bo); - -return 0 ; - -err_map_bo_gart_failed: -amdgpu_bo_unreserve (bo); -err_reserve_bo_failed: -return ret; -} - static int mes_userq_create_wptr_mapping(struct amdgpu_device *adev, struct amdgpu_userq_mgr *uq_mgr,@@ -65,55 +37,75 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev, uint64_t wptr) {struct amdgpu_bo_va_mapping *wptr_mapping; -struct amdgpu_vm *wptr_vm;struct amdgpu_userq_obj *wptr_obj = &queue->wptr_obj; +struct amdgpu_bo *obj; +struct amdgpu_vm *vm = queue->vm; +struct drm_exec exec;int ret; - wptr_vm = queue->vm; - ret =amdgpu_bo_reserve (wptr_vm->root.bo, false); -if (ret) -return ret; - wptr &=AMDGPU_GMC_HOLE_MASK ; - wptr_mapping =amdgpu_vm_bo_lookup_mapping (wptr_vm, wptr >>PAGE_SHIFT ); -amdgpu_bo_unreserve (wptr_vm->root.bo); -if (!wptr_mapping) { -DRM_ERROR ("Failed to lookup wptr bo\n" ); -return -EINVAL ; - }
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 48
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-5- wptr_obj->obj = wptr_mapping->bo_va->base.bo; -if (wptr_obj->obj->tbo.base.size >PAGE_SIZE ) { -DRM_ERROR ("Requested GART mapping for wptr bo larger than one page\n" ); -return -EINVAL ; - } +drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 );This should probably be 2 instead of 0.
Noted
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 57
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +9
-0+ drm_exec_until_all_locked(&exec) { + ret =amdgpu_vm_lock_pd (vm, &exec,1 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) + goto fail_lock; + + wptr_mapping =amdgpu_vm_bo_lookup_mapping (vm, wptr >>PAGE_SHIFT ); +if (!wptr_mapping) { +DRM_ERROR ("Failed to lock up wptr bo\n" );Please drop that error message. It can spam the logs when userspace intentionally gives incorrect values.
Noted
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 63
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-5+ ret = -EINVAL ; + goto fail_lock; + } - ret =mes_userq_map_gtt_bo_to_gart (wptr_obj->obj); -if (ret) { -DRM_ERROR ("Failed to map wptr bo to GART\n" ); -return ret; - } + obj = wptr_mapping->bo_va->base.bo; + ret =drm_exec_prepare_obj (&exec, &obj->tbo.base,1 );Using
drm_exec_lock_obj()should be sufficient.
Quoted 1 line
We don't need a fence slot for this use case.
Sure
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 66
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-0+drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) { +DRM_ERROR ("Failed to prepare wptr bo\n" );Same here, that this can fail is normal handling. The worst case is OOM and that is already printed in the logs.
Noted
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 68
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+ goto fail_lock; + }We got all the locks now, so the
drm_exec_until_all_locked()loop can be closed here.
True, Noted
Quoted 8 lines
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-5- ret =amdgpu_bo_reserve (wptr_obj->obj, true); -if (ret) { -DRM_ERROR ("Failed to reserve wptr bo\n" ); -return ret; - }
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 76
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7
-0+/* mapping now should be stable since both the locks are held */ + wptr_mapping =amdgpu_vm_bo_lookup_mapping (vm, wptr >>PAGE_SHIFT ); +if (!wptr_mapping) { +DRM_ERROR ("Failed to lock up wptr bo\n" ); + ret = -EINVAL ; + goto fail_lock; + }Doing that again is unecessary. We are holding the VM lock above while doing the lockup.
First time we had mapping at time we did not had wptr object lock. Isnt it possible that either mapping is changed/updated or the bo is freed. At this moment we have both the locks and we
should be having correct mapping.
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 78
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-6-/* TODO use eviction fence instead of pinning. */ - ret =amdgpu_bo_pin (wptr_obj->obj,AMDGPU_GEM_DOMAIN_GTT ); -if (ret) { -drm_file_err (uq_mgr->file,"[Usermode queues] Failed to pin wptr bo\n" ); - goto unresv_bo; - } + wptr_obj->obj =amdgpu_bo_ref (wptr_mapping->bo_va->base.bo);That is now unecessary as well.
We are holding the reference in original code and freeing it in destroy. Dont we want to hold any reference now, how do we make sure that its not being freed in between?
If we dont need a reference then we need to remove from the destroy path too. In amdgpu_userq_destroy we are doing unpin and unref both db_obj and wptr_obj.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 81
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-0+ +if (wptr_obj->obj->tbo.base.size >PAGE_SIZE ) { +DRM_ERROR ("Requested wptr bo size is larger than one page\n" );Same, please drop that error message.
Noted
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 88
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7
-0+ ret = -EINVAL ; + goto fail_map; + } + + ret =amdgpu_ttm_alloc_gart (&wptr_obj->obj->tbo); +if (ret) { +DRM_ERROR ("Failed to bind bo to GART. ret %d\n" , ret);That error message is useful.
Got it.
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 93
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-0+ goto fail_map; + } + +/* TODO use eviction fence instead of pinning. */ + ret =amdgpu_bo_pin (wptr_obj->obj,AMDGPU_GEM_DOMAIN_GTT );Oh! The piun needs to come before the
alloc_gart! That's wrong in the existing code as well.
Got it.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 93
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+if (ret) { +DRM_ERROR ("Failed to pin wptr bo. ret %d\n" , ret);That error message makes sense.
regards
Sunil Khatri
Quoted 24 lines
Regards,
Christian.Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +9
-4+ goto fail_map; + } - queue->wptr_obj.gpu_addr =amdgpu_bo_gpu_offset (wptr_obj->obj); -amdgpu_bo_unreserve (wptr_obj->obj); + queue->wptr_obj.gpu_addr =amdgpu_bo_gpu_offset (wptr_obj->obj); + } +drm_exec_fini (&exec);return 0 ; -unresv_bo: -amdgpu_bo_unreserve (wptr_obj->obj); +fail_map: +amdgpu_bo_unref (&wptr_obj->obj); +fail_lock: +drm_exec_fini (&exec);return ret; }
Christian König
maintainerQuoted 135 lines · Sunil
Quoted 72 lines · Christian König
Quoted 71 lines · Sunil Khatri
Use
drm_execto take both locks i.e vm root bo andwptr_objbo to access the mapping data properly.This fixes the security issue of unmap the
wptr_objwhile a queue creation is in progress and passing other bo at same address.Signed-off-by Sunil Khatri <[email protected]>---
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c| 122 ++++++++++-----------1 file changed, 57 insertions(+), 65 deletions(-)drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -30,34 +30,6 @@ #define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE #define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE-static int
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-27-mes_userq_map_gtt_bo_to_gart(struct amdgpu_bo *bo) -{ - int ret; - - ret = amdgpu_bo_reserve(bo, true); - if (ret) { - DRM_ERROR("Failed to reserve bo. ret %d\n", ret); - goto err_reserve_bo_failed; - } - - ret = amdgpu_ttm_alloc_gart(&bo->tbo); - if (ret) { - DRM_ERROR("Failed to bind bo to GART. ret %d\n", ret); - goto err_map_bo_gart_failed; - } - - amdgpu_bo_unreserve(bo); - bo = amdgpu_bo_ref(bo); - - return 0; - -err_map_bo_gart_failed: - amdgpu_bo_unreserve(bo); -err_reserve_bo_failed: - return ret; -} -static int
mes_userq_create_wptr_mapping(structamdgpu_device*adev, structamdgpu_userq_mgr*uq_mgr,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-10@@ -65,55 +37,75 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev, uint64_t wptr) { struct amdgpu_bo_va_mapping *wptr_mapping; - struct amdgpu_vm *wptr_vm; struct amdgpu_userq_obj *wptr_obj = &queue->wptr_obj; + struct amdgpu_bo *obj; + struct amdgpu_vm *vm = queue->vm; + struct drm_exec exec; int ret; - wptr_vm = queue->vm; - ret = amdgpu_bo_reserve(wptr_vm->root.bo, false); - if (ret) - return ret; - wptr &= AMDGPU_GMC_HOLE_MASK; - wptr_mapping = amdgpu_vm_bo_lookup_mapping(wptr_vm, wptr >> PAGE_SHIFT); - amdgpu_bo_unreserve(wptr_vm->root.bo); - if (!wptr_mapping) { - DRM_ERROR("Failed to lookup wptr bo\n"); - return -EINVAL;Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 48
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-5- } - wptr_obj->obj = wptr_mapping->bo_va->base.bo; - if (wptr_obj->obj->tbo.base.size > PAGE_SIZE) { - DRM_ERROR("Requested GART mapping for wptr bo larger than one page\n"); - return -EINVAL; - } + drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);This should probably be 2 instead of 0.
Noted
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 57
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +9
-0+ drm_exec_until_all_locked(&exec) { + ret = amdgpu_vm_lock_pd(vm, &exec, 1); + drm_exec_retry_on_contention(&exec); + if (unlikely(ret)) + goto fail_lock; + + wptr_mapping = amdgpu_vm_bo_lookup_mapping(vm, wptr >> PAGE_SHIFT); + if (!wptr_mapping) { + DRM_ERROR("Failed to lock up wptr bo\n");Please drop that error message. It can spam the logs when userspace intentionally gives incorrect values.
Noted
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 63
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-4+ ret = -EINVAL; + goto fail_lock; + } - ret = mes_userq_map_gtt_bo_to_gart(wptr_obj->obj); - if (ret) { - DRM_ERROR("Failed to map wptr bo to GART\n"); - return ret; - } + obj = wptr_mapping->bo_va->base.bo; + ret = drm_exec_prepare_obj(&exec, &obj->tbo.base, 1);Quoted 3 lines
Using
drm_exec_lock_obj()should be sufficient.We don't need a fence slot for this use case.
Sure
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 66
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3
-0+ drm_exec_retry_on_contention(&exec); + if (unlikely(ret)) { + DRM_ERROR("Failed to prepare wptr bo\n");Same here, that this can fail is normal handling. The worst case is OOM and that is already printed in the logs.
Noted
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 68
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+ goto fail_lock; + }We got all the locks now, so the
drm_exec_until_all_locked()loop can be closed here.True, Noted
Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7
-4- ret = amdgpu_bo_reserve(wptr_obj->obj, true); - if (ret) { - DRM_ERROR("Failed to reserve wptr bo\n"); - return ret; - } + /* mapping now should be stable since both the locks are held */ + wptr_mapping = amdgpu_vm_bo_lookup_mapping(vm, wptr >> PAGE_SHIFT); + if (!wptr_mapping) { + DRM_ERROR("Failed to lock up wptr bo\n"); + ret = -EINVAL; + goto fail_lock; + }
Quoted 5 lines
Quoted 1 line
Doing that again is unecessary. We are holding the VM lock above while doing the lockup.
First time we had mapping at time we did not had wptr object lock. Isnt it possible that either mapping is changed/updated or the bo is freed. At this moment we have both the locks and we
should be having correct mapping.
The VM lock is sufficient for that. If somebody tries to unmap or free the BO the code will block on the VM lock to remove all the mappings.
So as soon as you grab the VM lock you know that the mapping wouldn't go away.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
- /* TODO use eviction fence instead of pinning. */
- ret =amdgpu_bo_pin(wptr_obj->obj, AMDGPU_GEM_DOMAIN_GTT);
- if (ret) {
-drm_file_err(uq_mgr->file, "[Usermode queues] Failed to pin wptr bo\n"); - gotounresv_bo;
- }
+wptr_obj->obj=amdgpu_bo_ref(wptr_mapping->bo_va->base.bo);
Quoted 5 lines
Quoted 1 line
That is now unecessary as well.
We are holding the reference in original code and freeing it in destroy. Dont we want to hold any reference now, how do we make sure that its not being freed in between?
If we dont need a reference then we need to remove from the destroy path too. In
amdgpu_userq_destroywe are doing unpin and unref bothdb_objandwptr_obj.
Ah! So we keep wptr_obj around and drop that in the destroy path! Yeah then it makes sense to grab the reference.
We can potentially drop that when we stop pinning the wptr BO and use the eviction fence instead.
Thanks for the explanation.
Regards,
Christian.
Quoted 51 lines
Quoted 5 lines
Quoted 4 lines
+
+ if (wptr_obj->obj->tbo.base.size>PAGE_SIZE) {
+DRM_ERROR("Requested wptr bo size is larger than one page\n");Same, please drop that error message.
Noted
Quoted 9 lines
Quoted 8 lines
+ ret =
-EINVAL;
+ gotofail_map;
+ }
+
+ ret =amdgpu_ttm_alloc_gart(&wptr_obj->obj->tbo); + if (ret) {
+ DRM_ERROR("Failed to bind bo to GART. ret %d\n", ret);That error message is useful.
Got it.
Quoted 7 lines
Quoted 6 lines
+ goto
fail_map;
+ }
+
+ /* TODO use eviction fence instead of pinning. */ + ret =amdgpu_bo_pin(wptr_obj->obj, AMDGPU_GEM_DOMAIN_GTT);Oh! The piun needs to come before the
alloc_gart! That's wrong in the existing code as well.Got it.
Quoted 4 lines
Quoted 3 lines
+ if (ret) {
+ DRM_ERROR("Failed to pin wptr bo. ret %d\n", ret);That error message makes sense.
regards
Sunil KhatriQuoted 20 lines
Regards,
Christian.Quoted 16 lines
+ goto
fail_map;
+ }
-queue->wptr_obj.gpu_addr=amdgpu_bo_gpu_offset(wptr_obj->obj); -amdgpu_bo_unreserve(wptr_obj->obj);
+queue->wptr_obj.gpu_addr=amdgpu_bo_gpu_offset(wptr_obj->obj); + }
+drm_exec_fini(&exec);
return 0;
-unresv_bo:
-amdgpu_bo_unreserve(wptr_obj->obj);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4
-0+fail_map: + amdgpu_bo_unref(&wptr_obj->obj); +fail_lock: + drm_exec_fini(&exec);return ret;
}