Skip to contentSkip to conversation

drm/amdgpu/userq: fix reading the WPTR at a non-zero BO offset

PATCH In review Jesse Zhang 2 participants 2 messages Sep 23

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

PATCH 1/1 drm/amdgpu/userq: fix reading the WPTR at a non-zero BO offset

The user queue UAPI permits the WPTR at any 8-byte aligned offset within a BO. amdgpu_userq_fence_read_wptr() locates the mapping containing the WPTR address, but amdgpu_bo_kmap() returns the start of the BO, so the code read the first u64 regardless of the WPTR offset. USERQ_SIGNAL uses this value as the exported fence seqno, so a wrong read makes the fence signal early (or never), breaking dependency synchronization.

Translate the WPTR GPU address to a byte offset within the BO, accounting for the VA mapping start and the mapping's BO offset, before reading it.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +5 -2
index c270635c907b..b885e90ec5f5 100644
@@ -372,7 +372,7 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
 	struct amdgpu_bo_va_mapping *mapping;
 	struct amdgpu_bo *bo;
 	struct drm_exec exec;
-	u64 addr, *ptr;
+	u64 addr, offset, *ptr;
 	int ret;
 
 	addr = queue->userq_prop->wptr_gpu_addr;
@@ -404,7 +404,10 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
 		goto lock_error;
 	}
 
-	*wptr = le64_to_cpu(*ptr);
+	/* The WPTR can be anywhere within the BO's VA mapping. */
+	offset = addr - (mapping->start << AMDGPU_GPU_PAGE_SHIFT);
+	offset += mapping->offset;
+	*wptr = le64_to_cpu(ptr[offset / sizeof(*ptr)]);
 
 	amdgpu_bo_kunmap(bo);
 	drm_exec_fini(&exec);
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jesse Zhang

The user queue UAPI permits the WPTR at any 8-byte aligned offset within a BO. amdgpu_userq_fence_read_wptr() locates the mapping containing the WPTR address, but amdgpu_bo_kmap() returns the start of the BO, so the code read the first u64 regardless of the WPTR offset. USERQ_SIGNAL uses this value as the exported fence seqno, so a wrong read makes the fence signal early (or never), breaking dependency synchronization.

Quoted 4 lines

Translate the WPTR GPU address to a byte offset within the BO, accounting for the VA mapping start and the mapping's BO offset, before reading it.

Fixes a292fdecd728 ("drm/amdgpu: Implement userqueue signal/wait IOCTL")
Quoted 32 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +5 -3
index c270635c907b..b885e90ec5f5 100644
@@ -372,7 +372,7 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
        struct amdgpu_bo_va_mapping *mapping;
        struct amdgpu_bo *bo;
        struct drm_exec exec;
-       u64 addr, *ptr;
+       u64 addr, offset, *ptr;
        int ret;
 
        addr = queue->userq_prop->wptr_gpu_addr;
@@ -404,7 +404,10 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
                goto lock_error;
        }
 
-       *wptr = le64_to_cpu(*ptr);
+       /* The WPTR can be anywhere within the BO's VA mapping. */
+       offset = addr - (mapping->start << AMDGPU_GPU_PAGE_SHIFT);
+       offset += mapping->offset;
+       *wptr = le64_to_cpu(ptr[offset / sizeof(*ptr)]);
 
        amdgpu_bo_kunmap(bo);
        drm_exec_fini(&exec);
--

2.49.0