AMD-GFX Archive on 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 2/2] drm/amdgpu: validate the WPTR BO before handing its address to the MES
Date: Mon, 30 Nov 2026 06:47:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-240526@kernel.org> (raw)
In-Reply-To: <20261130062604.1921479-2-Jesse.Zhang@amd.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/amdgpu: non-GTT WPTR BO validation in
  mes_userq_create_wptr_mapping()
- [Medium] drm/amdgpu: WPTR BO eviction window in
  mes_userq_create_wptr_mapping()
--

commit 119f0cb948c33ef03d377ba9eecfb939c5ae5cb0
Author: Jesse Zhang <Jesse.Zhang@amd.com>

drm/amdgpu: validate the WPTR BO before handing its address to the MES

This commit validates the userqueue WPTR buffer object before allocating
GART space and attaches the eviction fence directly if it has not yet
signaled.

> diff --git a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
> index 3d4bc3a902a32..48f2e66dcb27e 100644
> --- a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
> +++ b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
> @@ -37,10 +37,12 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
>  			      struct amdgpu_usermode_queue *queue,
>  			      uint64_t wptr)
>  {
> +	struct ttm_operation_ctx ctx = { false, false };
>  	struct amdgpu_bo_va_mapping *wptr_mapping;
>  	struct amdgpu_userq_obj *wptr_obj = &queue->wptr_obj;
>  	struct amdgpu_bo *obj;
>  	struct amdgpu_vm *vm = queue->vm;
> +	struct dma_fence *ev_fence;
>  	struct drm_exec exec;
>  	int ret;
>  
> @@ -79,12 +81,19 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
>  		ret = -EINVAL;
>  		goto fail_map;
>  	}
> -	/* Keep WPTR BO under eviction-fence control instead of pinning. */
> -	ret = amdgpu_evf_mgr_attach_fence(&uq_mgr_to_fpriv(uq_mgr)->evf_mgr, wptr_obj->obj);
> -	if (ret) {
> -		DRM_ERROR("Failed to attach eviction fence to wptr bo. ret %d\n", ret);
> +
> +	/* The BO may have been evicted, it needs a GPU address for the MES */
> +	amdgpu_bo_placement_from_domain(wptr_obj->obj, wptr_obj->obj->allowed_domains);
> +	ret = ttm_bo_validate(&wptr_obj->obj->tbo, &wptr_obj->obj->placement, &ctx);
> +	if (ret)
>  		goto fail_map;
> -	}

[Severity: High]
Does validating wptr_obj->obj against wptr_obj->obj->allowed_domains
instead of AMDGPU_GEM_DOMAIN_GTT allow a non-GTT WPTR BO to bypass GART
binding?

Before commit 7264bc10c7c6 ("drm/amdgpu: dont pin wptr bo instead use
eviction fence"), amdgpu_bo_pin(wptr_obj->obj, AMDGPU_GEM_DOMAIN_GTT)
forced the BO into TTM_PL_TT.  If userspace supplies a 1-page WPTR BO
created with AMDGPU_GEM_DOMAIN_CPU (or AMDGPU_GEM_DOMAIN_VRAM),
ttm_bo_validate() now leaves it in TTM_PL_SYSTEM (resource->start == 0)
or places it in non-contiguous TTM_PL_VRAM.  amdgpu_ttm_alloc_gart() then
returns 0 without binding GART because bo->resource->start is not
AMDGPU_BO_INVALID_OFFSET, and amdgpu_bo_gpu_offset() triggers
WARN_ON_ONCE() and hands an invalid GPU address to MES, causing page
faults and GPU resets.

> +
> +	/* The BO is already validated; only attach the eviction fence. */
> +	ev_fence = amdgpu_evf_mgr_get_fence(&uq_mgr_to_fpriv(uq_mgr)->evf_mgr);
> +	if (!dma_fence_is_signaled(ev_fence))
> +		dma_resv_add_fence(wptr_obj->obj->tbo.base.resv, ev_fence,
> +				   DMA_RESV_USAGE_BOOKKEEP);
> +	dma_fence_put(ev_fence);
>  
>  	ret = amdgpu_ttm_alloc_gart(&wptr_obj->obj->tbo);
>  	if (ret) {

[Severity: Medium]
When ev_fence is already signaled, can wptr_obj->obj be evicted again
after drm_exec_fini() unlocks it and before amdgpu_userq_create() runs
amdgpu_userq_ensure_ev_fence()?

CPU0                                     CPU1 (GTT eviction)
amdgpu_userq_create()
  mes_userq_mqd_create()
    mes_userq_create_wptr_mapping()
      ttm_bo_validate()
      /* ev_fence is signaled, not added */
      amdgpu_ttm_alloc_gart()
      wptr_obj.gpu_addr =
        amdgpu_bo_gpu_offset()
      drm_exec_fini(&exec)
                                         ttm_bo_evict()
                                           /* evicts wptr_obj->obj to SYSTEM,
                                            * freeing its GART range
                                            */
  amdgpu_userq_ensure_ev_fence()
    amdgpu_userq_vm_validate_and_restore_queue()
      /* validates BO back to TTM_PL_TT with
       * start = AMDGPU_BO_INVALID_OFFSET;
       * queue is not in uq_mgr->userq_xa yet,
       * so GART re-bind is skipped
       */
  amdgpu_userq_map_helper()
    mes_userq_map()                      <-- passes stale wptr_obj.gpu_addr
                                             to MES

In amdgpu_userq_create(), xa_alloc(&uq_mgr->userq_xa, ...) runs after
amdgpu_userq_ensure_ev_fence() and amdgpu_userq_map_helper().  Because
amdgpu_userq_vm_validate_and_restore_queue() only re-runs
amdgpu_ttm_alloc_gart() and refreshes wptr_obj.gpu_addr for queues already
in uq_mgr->userq_xa:

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c:amdgpu_userq_vm_validate_and_restore_queue() {
    ...
	xa_for_each(&uq_mgr->userq_xa, tmp_key, queue) {
		bo = queue->wptr_obj.obj;
		if (!bo) {
			ret = -EINVAL;
			goto unlock_all;
		}

		ret = amdgpu_ttm_alloc_gart(&bo->tbo);
    ...
		queue->wptr_obj.gpu_addr = amdgpu_bo_gpu_offset(bo);
	}
    ...
}

the newly created queue keeps a stale, freed GART address in
queue->wptr_obj.gpu_addr and hands it to MES in mes_userq_map().

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

  reply	other threads:[~2026-11-30  6:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  6:25 [PATCH V2 1/2] drm/amdgpu: reject userptr BOs as userqueue write pointers Jesse Zhang
2026-11-30  6:25 ` [PATCH V2 2/2] drm/amdgpu: validate the WPTR BO before handing its address to the MES Jesse Zhang
2026-11-30  6:47   ` sashiko-bot [this message]
2026-11-30  6:51 ` [PATCH V2 1/2] drm/amdgpu: reject userptr BOs as userqueue write pointers sashiko-bot

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-240526@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox