AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V2 1/2] drm/amdgpu: reject userptr BOs as userqueue write pointers
@ 2026-11-30  6:25 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:51 ` [PATCH V2 1/2] drm/amdgpu: reject userptr BOs as userqueue write pointers sashiko-bot
  0 siblings, 2 replies; 4+ messages in thread
From: Jesse Zhang @ 2026-11-30  6:25 UTC (permalink / raw)
  To: amd-gfx; +Cc: Alexander.Deucher, Christian Koenig, Sunil Khatri, Jesse Zhang

Userptr BOs may not have their pages populated. Reject them before
validating the WPTR BO during queue creation and before CPU mapping it
in USERQ_SIGNAL. The latter also covers remapping the WPTR VA after
queue creation.

Fixes: 7264bc10c7c6 ("drm/amdgpu: dont pin wptr bo instead use eviction fence")
Fixes: a292fdecd728 ("drm/amdgpu: Implement userqueue signal/wait IOCTL")
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c      | 4 +++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c
index dd976a50e053..78d33884a1b3 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c
@@ -402,6 +402,12 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
 	}
 
 	bo = mapping->bo_va->base.bo;
+	/* Userptr pages may not be populated when reading the WPTR. */
+	if (amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) {
+		ret = -EINVAL;
+		goto lock_error;
+	}
+
 	ret = amdgpu_bo_kmap(bo, (void **)&ptr);
 	if (ret) {
 		DRM_ERROR("Failed mapping the userqueue wptr bo");
diff --git a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 7abb9d95ef59..3d4bc3a902a3 100644
--- a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
+++ b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
@@ -73,7 +73,9 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 	}
 
 	wptr_obj->obj = amdgpu_bo_ref(wptr_mapping->bo_va->base.bo);
-	if (wptr_obj->obj->tbo.base.size > PAGE_SIZE) {
+	/* Userptr pages cannot be obtained by ttm_bo_validate(). */
+	if (wptr_obj->obj->tbo.base.size > PAGE_SIZE ||
+	    amdgpu_ttm_tt_is_userptr(wptr_obj->obj->tbo.ttm)) {
 		ret = -EINVAL;
 		goto fail_map;
 	}
-- 
2.50.1


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

* [PATCH V2 2/2] drm/amdgpu: validate the WPTR BO before handing its address to the MES
  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 ` Jesse Zhang
  2026-11-30  6:47   ` sashiko-bot
  2026-11-30  6:51 ` [PATCH V2 1/2] drm/amdgpu: reject userptr BOs as userqueue write pointers sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Jesse Zhang @ 2026-11-30  6:25 UTC (permalink / raw)
  To: amd-gfx; +Cc: Alexander.Deucher, Christian Koenig, Sunil Khatri, Jesse Zhang

The unpinned WPTR BO can be evicted to SYSTEM before queue creation.
amdgpu_evf_mgr_attach_fence() skips validation when the eviction fence
is already signaled, leaving the BO in SYSTEM. Since resource->start
is zero, amdgpu_ttm_alloc_gart() also returns success without allocating
or binding GART space. mes_userq_create_wptr_mapping() then passes an
invalid GPU address to MES.

On Navi31, creating SDMA user queues during GTT eviction triggers:

  WARNING: amdgpu_object.c:1550 at amdgpu_bo_gpu_offset
   mes_userq_create_wptr_mapping
   mes_userq_mqd_create
   amdgpu_userq_ioctl

followed by page faults at address 0 and GPU resets.

Validate the BO explicitly before allocating GART space, then attach
the eviction fence directly if it is unsignaled. This also avoids a
second validation through amdgpu_evf_mgr_attach_fence(). The warning
and address-zero faults are gone in the eviction test with validation.

Fixes: 7264bc10c7c6 ("drm/amdgpu: dont pin wptr bo instead use eviction fence")
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index 3d4bc3a902a3..48f2e66dcb27 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;
-	}
+
+	/* 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) {
-- 
2.50.1


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

* Re: [PATCH V2 2/2] drm/amdgpu: validate the WPTR BO before handing its address to the MES
  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
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-30  6:47 UTC (permalink / raw)
  To: Jesse Zhang, amd-gfx

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

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

* Re: [PATCH V2 1/2] drm/amdgpu: reject userptr BOs as userqueue write pointers
  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:51 ` sashiko-bot
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-30  6:51 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: userptr check bypass via zero addr in
  amdgpu_ttm_tt_is_userptr()
--

commit 49d56b1a7c36fe3478f82185ee8e35956c14e164
Author: Jesse Zhang <Jesse.Zhang@amd.com>

drm/amdgpu: reject userptr BOs as userqueue write pointers

This commit rejects userptr BOs in amdgpu_userq_fence_read_wptr() and
mes_userq_create_wptr_mapping() using amdgpu_ttm_tt_is_userptr().

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c
> index 614599ba5455a..443a00514035f 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c
> @@ -401,6 +401,12 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
>  	}
>  
>  	bo = mapping->bo_va->base.bo;
> +	/* Userptr pages may not be populated when reading the WPTR. */
> +	if (amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) {
> +		ret = -EINVAL;
> +		goto lock_error;
> +	}
> +
>  	ret = amdgpu_bo_kmap(bo, (void **)&ptr);
>  	if (ret) {
>  		DRM_ERROR("Failed mapping the userqueue wptr bo");
> diff --git a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
> index 7abb9d95ef59a..3d4bc3a902a32 100644
> --- a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
> +++ b/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
> @@ -73,7 +73,9 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
>  	}
>  
>  	wptr_obj->obj = amdgpu_bo_ref(wptr_mapping->bo_va->base.bo);
> -	if (wptr_obj->obj->tbo.base.size > PAGE_SIZE) {
> +	/* Userptr pages cannot be obtained by ttm_bo_validate(). */
> +	if (wptr_obj->obj->tbo.base.size > PAGE_SIZE ||
> +	    amdgpu_ttm_tt_is_userptr(wptr_obj->obj->tbo.ttm)) {
>  		ret = -EINVAL;
>  		goto fail_map;
>  	}

[Severity: High]
Can a userptr BO created with addr == 0 bypass both of these
amdgpu_ttm_tt_is_userptr() checks?

amdgpu_gem_userptr_ioctl() permits args->addr == 0 (for example with
AMDGPU_GEM_USERPTR_READONLY and without AMDGPU_GEM_USERPTR_VALIDATE),
which leaves gtt->userptr == 0, sets TTM_TT_FLAG_EXTERNAL, and leaves
ttm->pages unpopulated.  Because amdgpu_ttm_tt_is_userptr() tests
!gtt->userptr rather than gtt->usertask:

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;
}

it returns false when gtt->userptr == 0.  Calling amdgpu_bo_kmap() on that
BO in amdgpu_userq_fence_read_wptr() then reaches ttm_bo_kmap_ttm() with
ttm->pages[0] == NULL, and validating it into GTT in
mes_userq_create_wptr_mapping() reaches amdgpu_ttm_backend_bind() with
gtt->gobj->import_attach == NULL, both causing a NULL pointer
dereference.

Checking gtt->usertask (as amdgpu_ttm_tt_get_usermm() does) in
amdgpu_ttm_tt_is_userptr() would catch userptr BOs regardless of address.

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

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

end of thread, other threads:[~2026-11-30  6:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-11-30  6:51 ` [PATCH V2 1/2] drm/amdgpu: reject userptr BOs as userqueue write pointers sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox