All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock
@ 2026-11-25  9:04 Jesse Zhang
  2026-11-25  9:22 ` sashiko-bot
  2026-11-25 13:49 ` Christian König
  0 siblings, 2 replies; 4+ messages in thread
From: Jesse Zhang @ 2026-11-25  9:04 UTC (permalink / raw)
  To: amd-gfx; +Cc: Alexander.Deucher, Christian Koenig, Jesse Zhang

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)

Fixes: 1c824497d8ac ("drm/amdgpu: fix calling VM invalidation in amdgpu_hmm_invalidate_gfx")
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index ea8672451d98..5f9ed9cb18b5 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -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);
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock
  2026-11-25  9:04 [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock Jesse Zhang
@ 2026-11-25  9:22 ` sashiko-bot
  2026-11-25 13:49 ` Christian König
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-25  9:22 UTC (permalink / raw)
  To: Jesse Zhang, amd-gfx

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 <Jesse.Zhang@amd.com>

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.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index 94b1cfe05e3e0..7a6de95e902e4 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -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)
> @@ -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                          CPU2
do_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-free

The 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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125090415.3345401-1-Jesse.Zhang@amd.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock
  2026-11-25  9:04 [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock Jesse Zhang
  2026-11-25  9:22 ` sashiko-bot
@ 2026-11-25 13:49 ` Christian König
  2026-11-26  2:08   ` Zhang, Jesse(Jie)
  1 sibling, 1 reply; 4+ messages in thread
From: Christian König @ 2026-11-25 13:49 UTC (permalink / raw)
  To: Jesse Zhang, amd-gfx; +Cc: Alexander.Deucher

On 11/25/26 10:04, Jesse Zhang wrote:
> 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)

That is nonsense and very dangerous. Please use amdgpu_ttm_tt_is_userptr() instead.

Apart from that good catch.

Regards,
Christian.

> 
> Fixes: 1c824497d8ac ("drm/amdgpu: fix calling VM invalidation in amdgpu_hmm_invalidate_gfx")
> Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index ea8672451d98..5f9ed9cb18b5 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -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);


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock
  2026-11-25 13:49 ` Christian König
@ 2026-11-26  2:08   ` Zhang, Jesse(Jie)
  0 siblings, 0 replies; 4+ messages in thread
From: Zhang, Jesse(Jie) @ 2026-11-26  2:08 UTC (permalink / raw)
  To: Koenig, Christian, amd-gfx@lists.freedesktop.org; +Cc: Deucher, Alexander

AMD General

> -----Original Message-----
> From: Koenig, Christian <Christian.Koenig@amd.com>
> Sent: Wednesday, November 25, 2026 9:49 PM
> To: Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>; amd-
> gfx@lists.freedesktop.org
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>
> Subject: Re: [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the
> notifier_lock
>
> On 11/25/26 10:04, Jesse Zhang wrote:
> > 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)
>
> 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
>
> Apart from that good catch.
>
> Regards,
> Christian.
>
> >
> > Fixes: 1c824497d8ac ("drm/amdgpu: fix calling VM invalidation in
> > amdgpu_hmm_invalidate_gfx")
> > Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> > ---
> >  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 10 ++++++++++
> >  1 file changed, 10 insertions(+)
> >
> > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > index ea8672451d98..5f9ed9cb18b5 100644
> > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> > @@ -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);


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-26  2:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  9:04 [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock Jesse Zhang
2026-11-25  9:22 ` sashiko-bot
2026-11-25 13:49 ` Christian König
2026-11-26  2:08   ` Zhang, Jesse(Jie)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.