drm/amdgpu: unlink userptr bo_vas under the notifier_lock
Subsystems (2)
PATCH v2 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. Test for the task of the userptr rather than its address, the GEM ioctl accepts a userptr at address 0 and registers the notifier for it as well.
v2: check amdgpu_ttm_tt_get_usermm() instead of
amdgpu_ttm_tt_is_userptr(), which is false for a userptr at address 0 (Sashiko)
--- 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_get_usermm (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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu:bo_va UAF in amdgpu_vm_bo_del() when group_leader->mm is NULL --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu:bo_va UAF in amdgpu_vm_bo_del() when group_leader->mm is NULL
--
commit cfb11e728a1b3b102cad849d9bad6a5262a6b72c
Author: Jesse Zhang <[email protected]>
drm/amdgpu: unlink userptr bo_vas under the notifier_lock
This commit takes adev->notifier_lock in amdgpu_vm_bo_del() when unlinking a userptr bo_va from bo->vm_bo to serialize against concurrent MMU notifier invalidations.
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8
-0@@ -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_get_usermm (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 2280
Quoted 8 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); }spin_lock (&vm->individual_lock);
[Severity: High]
Can amdgpu_vm_bo_del() still skip adev->notifier_lock and free bo_va while amdgpu_hmm_invalidate_gfx() walks bo->vm_bo if the thread group leader has already exited?
amdgpu_ttm_tt_set_userptr() binds gtt->usertask to current->group_leader, and amdgpu_ttm_tt_get_usermm() returns gtt->usertask->mm:
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c:amdgpu_ttm_tt_get_usermm() {
...
if (gtt->usertask == NULL)
return NULL;
return gtt->usertask->mm;
}Meanwhile, amdgpu_hmm_register() inserts bo->notifier into current->mm, where it stays active until amdgpu_gem_object_free() calls amdgpu_hmm_unregister(). In a multi-threaded process, if the thread group leader exits via pthread_exit() while other threads remain alive, exit_mm() clears gtt->usertask->mm to NULL while the shared mm_struct and bo->notifier are still live:
CPU0 (group leader) CPU1 CPU2do_exit()
exit_mm()
tsk->mm = NULL
drm_gem_close_ioctl()
...
amdgpu_vm_bo_del()
/* get_usermm() == NULL */
madvise(MADV_DONTNEED)
...
amdgpu_hmm_invalidate_gfx()
mutex_lock(&adev->notifier_lock)
amdgpu_vm_bo_invalidate()
*base = bo_va->base.next
kfree(bo_va)
/* walks bo->vm_bo */
<-- use-after-freeThe same happens during process exit when do_exit() runs exit_mm() before exit_files() closes the DRM file descriptor while another task sharing the mm or file triggers an invalidation.
Checking amdgpu_ttm_tt_get_usertask() (or bo->notifier.mm) instead of amdgpu_ttm_tt_get_usermm() would keep adev->notifier_lock serialized for as long as the userptr BO exists.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Christian König
maintainerQuoted 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 8 lines
Take
notifier_lockwhen unlinking userptrbo_vasto serialize with the notifier. Test for the task of the userptr rather than its address, the GEM ioctl accepts a userptr at address 0 and registers the notifier for it as well.v2: check
amdgpu_ttm_tt_get_usermm()instead of
amdgpu_ttm_tt_is_userptr(), which is false for a userptr at address 0 (Sashiko)
That is nonsense and very dangerous. Please use amdgpu_ttm_tt_is_userptr() instead.
Apart from that good catch.
Regards,
Christian.
Quoted 39 lines
Fixes 1c824497d8ac ("drm/amdgpu: fix calling VM invalidation in amdgpu_hmm_invalidate_gfx")Signed-off-by Jesse Zhang <[email protected]>--- 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_get_usermm (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);
AMD General
Quoted 34 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, November 25, 2026 9:49 PM
To: Zhang, Jesse(Jie) <[email protected]>; amd- [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v2] drm/amdgpu: unlink userptrbo_vasunder thenotifier_lockQuoted 24 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_ioctlTake
notifier_lockwhen unlinking userptrbo_vasto serialize with the notifier. Test for the task of the userptr rather than its address, the GEM ioctl accepts a userptr at address 0 and registers the notifier for it as well.
Quoted 6 lines
Quoted 3 lines
v2: check
amdgpu_ttm_tt_get_usermm()instead of
amdgpu_ttm_tt_is_userptr(), which is false for a userptr at address 0 (Sashiko)That is nonsense and very dangerous. Please use
amdgpu_ttm_tt_is_userptr()instead.
Thanks Christian, I'll switch back to amdgpu_ttm_tt_is_userptr().
I'll handle the address-zero case separately. GEM_USERPTR accepts address 0 and registers a notifier, while the TTM backend uses zero to mean no userptr. With v1 applied, I reproduced the same KASAN use-after-free.
Rejecting address 0 in amdgpu_gem_userptr_ioctl() resolves that case. I'll send it as a separate patch.
Regards,
Jesse
Quoted 49 lines
Apart from that good catch.
Regards,
Christian.drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_del(struct
Quoted 14 lines
Fixes 1c824497d8ac ("drm/amdgpu: fix calling VM invalidation in
amdgpu_hmm_invalidate_gfx")Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0@@ -2279,10 +2279,18 @@ void amdgpu_vm_bo_del(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_bo_del(struct amdgpu_device
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8
-0dma_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_get_usermm (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,
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-0*base = bo_va->base.next;break ; } +if (userptr) +mutex_unlock (&adev->notifier_lock); }spin_lock (&vm->individual_lock);