All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jesse Zhang" <Jesse.Zhang@amd.com>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH v2] drm/amdgpu: unlink userptr bo_vas under the notifier_lock
Date: Wed, 25 Nov 2026 09:22:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-233002@kernel.org> (raw)
In-Reply-To: <20261125090415.3345401-1-Jesse.Zhang@amd.com>

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

  reply	other threads:[~2026-11-25  9:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-25 13:49 ` Christian König
2026-11-26  2:08   ` Zhang, Jesse(Jie)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233002@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Jesse.Zhang@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.