Skip to contentSkip to conversation

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

PATCH New Jesse Zhang 2 participants 2 messages Nov 27

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

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 into its allowed domains before allocating GART space, as the restore path does. This removes the warning and address-zero faults in the same test.

Fixes 7264bc10c7c6 ("drm/amdgpu: dont pin wptr bo instead use eviction fence")
---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7 -0
index 7abb9d95ef59..105c9866b2a3 100644
@@ -37,6 +37,7 @@ 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;
@@ -84,6 +85,12 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 		goto fail_map;
 	}
 
+	/* 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;
+
 	ret = amdgpu_ttm_alloc_gart(&wptr_obj->obj->tbo);
 	if (ret) {
 		DRM_ERROR("Failed to bind wptr bo to GART. ret %d\n", ret);
Signature · 1 line
2.50.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: missing userptr and domain checks in mes_userq_create_wptr_mapping() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: missing userptr and domain checks in mes_userq_create_wptr_mapping()
--

commit c8808f07dff4b49d4c3e4db4a9b93588681aeec7
Author: Jesse Zhang <[email protected]>

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

This commit validates wptr_obj->obj into its allowed domains before calling amdgpu_ttm_alloc_gart() in mes_userq_create_wptr_mapping().

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
index 7abb9d95ef59a..105c9866b2a3f 100644
@@ -37,6 +37,7 @@ 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;

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 92

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5 -0
@@ -84,6 +85,12 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 		goto fail_map;
 	}
 
+	/* 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]
Can calling ttm_bo_validate() with wptr_obj->obj->allowed_domains cause a NULL pointer dereference in amdgpu_ttm_tt_pin_userptr() or leave wptr_obj->obj outside GTT?

Before 7264bc10c7c6 ("drm/amdgpu: dont pin wptr bo instead use eviction fence"), amdgpu_bo_pin(wptr_obj->obj, AMDGPU_GEM_DOMAIN_GTT) rejected userptr BOs via amdgpu_ttm_tt_get_usermm() and validated the BO into AMDGPU_GEM_DOMAIN_GTT.

If userspace registers a userptr BO without AMDGPU_GEM_USERPTR_VALIDATE while the eviction fence is in its initial signaled stub state, the BO stays in TTM_PL_SYSTEM with allowed_domains = AMDGPU_GEM_DOMAIN_GTT and ttm->pages[0] == NULL. Validating it directly here moves it to TTM_PL_TT without fetching user pages via amdgpu_ttm_tt_get_user_pages(), so amdgpu_bo_move() -> amdgpu_ttm_backend_bind() -> amdgpu_ttm_tt_pin_userptr() passes the unpopulated ttm->pages array to sg_alloc_table_from_pages() and dma_map_sgtable().

In addition, if wptr_obj->obj was created with AMDGPU_GEM_DOMAIN_CPU or non-contiguous AMDGPU_GEM_DOMAIN_VRAM, validating into allowed_domains leaves it in TTM_PL_SYSTEM or TTM_PL_VRAM, causing amdgpu_ttm_alloc_gart() to return 0 without binding GART space and amdgpu_bo_gpu_offset() to warn and pass an invalid GPU address to MES.

Should mes_userq_create_wptr_mapping() reject userptr BOs and require AMDGPU_GEM_DOMAIN_GTT placement?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1