Skip to contentSkip to conversation

drm/amdgpu: unlink userptr bo_vas under the notifier_lock

PATCH Superseded Jesse Zhang 2 participants 2 messages Nov 25

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

Fixes 1c824497d8ac ("drm/amdgpu: fix calling VM invalidation in amdgpu_hmm_invalidate_gfx")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -0
index ea8672451d98..87b06daff892 100644
@@ -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
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: notifier_lock bypass for addr 0 userptr in amdgpu_vm_bo_del() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: notifier_lock bypass for addr 0 userptr in amdgpu_vm_bo_del()
--

commit 22400668ad8179b001864d3570debe97ad11a006
Author: Jesse Zhang <[email protected]>

drm/amdgpu: unlink userptr bo_vas under the notifier_lock

This commit acquires adev->notifier_lock in amdgpu_vm_bo_del() when unlinking userptr bo_vas from bo->vm_bo to serialize against concurrent MMU notifier invalidations.

Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8 -0
index 94b1cfe05e3e0..d33dcfd7059cf 100644
@@ -2252,10 +2252,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)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_del(struct amdgpu_device *adev, · line 2278

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
@@ -2265,6 +2273,8 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,
 			*base = bo_va->base.next;
 			break;
 		}
+		if (userptr)
+			mutex_unlock(&adev->notifier_lock);
 	}

[Severity: High]
Can a userptr BO created at address 0 bypass adev->notifier_lock here and still hit the use-after-free on bo_va?

amdgpu_gem_userptr_ioctl() accepts args->addr == 0 with a non-zero page-aligned size and AMDGPU_GEM_USERPTR_REGISTER, setting gtt->userptr = 0 and registering the MMU interval notifier for [0, args->size). However, amdgpu_ttm_tt_is_userptr() tests !gtt->userptr:

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:amdgpu_ttm_tt_is_userptr() {
	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);

	if (gtt == NULL || !gtt->userptr)
		return false;

	return true;
}

When gtt->userptr is 0, userptr evaluates to false, so amdgpu_vm_bo_del() skips adev->notifier_lock while an MMU invalidation on a VMA overlapping [0, args->size) can concurrently walk bo->vm_bo in amdgpu_hmm_invalidate_gfx() -> amdgpu_vm_bo_invalidate().

Checking amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) (or gtt->usertask in amdgpu_ttm_tt_is_userptr()) would cover userptr BOs at address 0 as well.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1