drm/amdgpu: unlink userptr bo_vas under the notifier_lock
Subsystems (2)
PATCH v3 1/2 drm/amdgpu: unlink userptr bo_vas under the notifier_lock
The userptr MMU notifier walks bo->vm_bo under notifier_lock, but amdgpu_vm_bo_del() removes and frees entries with only the BO and VM reservations held. Concurrent invalidation can access a freed bo_va.
On Navi48, racing userptr BO close with MADV_DONTNEED triggers:
BUG: KASAN: slab-use-after-free in amdgpu_vm_bo_needs_update amdgpu_vm_bo_invalidate amdgpu_hmm_invalidate_gfx __mmu_notifier_invalidate_range_start zap_vma_range_batched madvise_vma_behavior
Freed by:
kfree
amdgpu_vm_bo_del
amdgpu_gem_object_close
drm_gem_close_ioctl
Take notifier_lock when unlinking userptr bo_vas to serialize with the notifier.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
@@ -2279,10 +2279,18 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,dma_resv_assert_held (vm->root.bo->tbo.base.resv);if (bo) { +/* +* The MMU notifier of a userptr walks bo->vm_bo under the +* notifier_lock, it can't take the BO reservation. +*/ +bool userptr =amdgpu_ttm_tt_is_userptr (bo->tbo.ttm); +dma_resv_assert_held (bo->tbo.base.resv);if (amdgpu_vm_is_bo_always_valid (vm, bo))ttm_bo_set_bulk_move (&bo->tbo,NULL ); +if (userptr) +mutex_lock (&adev->notifier_lock);for (base = &bo_va->base.bo->vm_bo; *base; base = &(*base)->next) {if (*base != &bo_va->base)
@@ -2292,6 +2300,8 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev, *base = bo_va->base.next;break ; } +if (userptr) +mutex_unlock (&adev->notifier_lock); }spin_lock (&vm->individual_lock);
Signature · 1 line
2.50.1
PATCH v3 2/2 drm/amdgpu: reject userptr BOs at address 0
amdgpu_gem_userptr_ioctl() accepts a userptr at address 0 and registers the MMU notifier for it, but the rest of the driver treats gtt->userptr == 0 as no userptr. amdgpu_vm_bo_del() then frees its bo_vas without the notifier_lock:
BUG: KASAN: slab-use-after-free in amdgpu_vm_bo_needs_update+0x5d9/0x6a0 [amdgpu] amdgpu_vm_bo_invalidate amdgpu_hmm_invalidate_gfx __mmu_notifier_invalidate_range_start madvise_vma_behavior Freed by: kfree amdgpu_vm_bo_del amdgpu_gem_object_close drm_gem_close_ioctl
Reject address 0, as KFD already does.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
@@ -571,7 +571,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, args->addr =untagged_addr (args->addr); -if (offset_in_page (args->addr | args->size)) +if (!args->addr ||offset_in_page (args->addr | args->size))return -EINVAL ;if (!access_ok ((void __user *)(uintptr_t )args->addr, args->size))
Signature · 1 line
2.50.1
Quoted 19 lines · Jesse Zhang
The userptr MMU notifier walks
bo->vm_boundernotifier_lock, butamdgpu_vm_bo_del()removes and frees entries with only the BO and VM reservations held. Concurrent invalidation can access a freedbo_va.On Navi48, racing userptr BO close with
MADV_DONTNEEDtriggers:BUG: KASAN: slab-use-after-free in amdgpu_vm_bo_needs_update amdgpu_vm_bo_invalidate amdgpu_hmm_invalidate_gfx __mmu_notifier_invalidate_range_start zap_vma_range_batched madvise_vma_behaviorFreed by:
kfree
amdgpu_vm_bo_del
amdgpu_gem_object_close
drm_gem_close_ioctl
Quoted 5 lines
Take
notifier_lockwhen unlinking userptrbo_vasto serialize with the notifier.Fixes 1c824497d8ac ("drm/amdgpu: fix calling VM invalidation in amdgpu_hmm_invalidate_gfx")Signed-off-by Jesse Zhang <[email protected]>
Quoted 36 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10
-0@@ -2279,10 +2279,18 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,dma_resv_assert_held (vm->root.bo->tbo.base.resv);if (bo) { +/* +* The MMU notifier of a userptr walks bo->vm_bo under the +* notifier_lock, it can't take the BO reservation. +*/ +bool userptr =amdgpu_ttm_tt_is_userptr (bo->tbo.ttm); +dma_resv_assert_held (bo->tbo.base.resv);if (amdgpu_vm_is_bo_always_valid (vm, bo))ttm_bo_set_bulk_move (&bo->tbo,NULL ); +if (userptr) +mutex_lock (&adev->notifier_lock);for (base = &bo_va->base.bo->vm_bo; *base; base = &(*base)->next) {if (*base != &bo_va->base)@@ -2292,6 +2300,8 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev, *base = bo_va->base.next;break ; } +if (userptr) +mutex_unlock (&adev->notifier_lock); }spin_lock (&vm->individual_lock);