Skip to contentSkip to conversation

drm/amdgpu: sign extend the addresses GEM_OP(GET_MAPPING_INFO) returns

PATCH In review Jesse Zhang 3 participants 6 messages Oct 23 – Oct 26

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

PATCH 1/3 drm/amdgpu: sign extend the addresses GEM_OP(GET_MAPPING_INFO) returns

GEM_VA takes the GPU virtual addresses above the VA hole sign extended, like the rest of the UAPI, but GET_MAPPING_INFO reports them without the upper bits, so they land in the hole. Mappings in the upper half of the address space can't be recreated or removed with what it returns. On navi31:

  MAP at 0xffff800100000000                 -> 0
  GET_MAPPING_INFO                          -> addr 0x800100000000
  UNMAP at 0x800100000000                   -> -EINVAL

Sign extend the addresses before handing them out.

Fixes 4d82724f7f2b ("drm/amdgpu: Add mapping info option for GEM_OP ioctl")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +5 -2
index fb60a99f43c1..c982d102614e 100644
@@ -1082,6 +1082,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *filp)
 {
+	struct amdgpu_device *adev = drm_to_adev(dev);
 	struct drm_amdgpu_gem_op *args = data;
 	struct drm_gem_object *gobj;
 	struct amdgpu_vm_bo_base *base;
@@ -1188,7 +1189,8 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 
 		amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) {
 			if (num_mappings < args->num_entries) {
-				vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
+				vm_entries[num_mappings].addr =
+					amdgpu_gmc_sign_extend(mapping->start * AMDGPU_GPU_PAGE_SIZE);
 				vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
 				vm_entries[num_mappings].offset = mapping->offset;
 				vm_entries[num_mappings].flags = mapping->flags;
@@ -1198,7 +1200,8 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 
 		amdgpu_vm_bo_va_for_each_invalid_mapping(bo_va, mapping) {
 			if (num_mappings < args->num_entries) {
-				vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
+				vm_entries[num_mappings].addr =
+					amdgpu_gmc_sign_extend(mapping->start * AMDGPU_GPU_PAGE_SIZE);
 				vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
 				vm_entries[num_mappings].offset = mapping->offset;
 				vm_entries[num_mappings].flags = mapping->flags;
Signature · 1 line
2.50.1
#1 lore raw

PATCH 2/3 drm/amdgpu: parse CS IBs at the BO offset their VA mapping starts at

For rings which parse or patch the IBs on the CPU, like JPEG and VCN, the CS looks the IB up through its VA mapping and maps the BO, but ignores the offset in the BO the mapping starts at. It then parses other data than the GPU executes. On navi31, an IB of JPEG NOPs in the second page of a BO, mapped with offset_in_bo 4096, is parsed from the zeros in the first page and rejected:

amdgpu 0000:03:00.0: Invalid packet [0x00000000]!

while the same IB submitted through a mapping of the whole BO works.

Add the mapping offset when locating the IB in the BO.

Fixes 4802ce117786 ("drm/amdgpu: fix UVD/VCE VM emulation")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index fa301c71b782..4a277f04cde5 100644
@@ -1073,7 +1073,7 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
 		if (r)
 			return r;
 
-		kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE);
+		kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE) + m->offset;
 
 		if (ring->funcs->parse_cs) {
 			memcpy(ib->ptr, kptr, ib->length_dw * 4);
Signature · 1 line
2.50.1
maintainer
#2 lore raw

Re: [PATCH 2/3] drm/amdgpu: parse CS IBs at the BO offset their VA mapping starts at

Quoted 7 lines · Jesse Zhang

For rings which parse or patch the IBs on the CPU, like JPEG and VCN, the CS looks the IB up through its VA mapping and maps the BO, but ignores the offset in the BO the mapping starts at. It then parses other data than the GPU executes. On navi31, an IB of JPEG NOPs in the second page of a BO, mapped with offset_in_bo 4096, is parsed from the zeros in the first page and rejected:

Quoted 8 lines

amdgpu 0000:03:00.0: Invalid packet [0x00000000]!

while the same IB submitted through a mapping of the whole BO works.

Add the mapping offset when locating the IB in the BO.

Fixes 4802ce117786 ("drm/amdgpu: fix UVD/VCE VM emulation")

IIRC that was brought up before, I wonder why that patch wasn't merged.

@Alex do you remember?

Regards,
Christian.

Quoted 17 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index fa301c71b782..4a277f04cde5 100644
@@ -1073,7 +1073,7 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
 		if (r)
 			return r;
 
-		kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE);
+		kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE) + m->offset;
 
 		if (ring->funcs->parse_cs) {
 			memcpy(ib->ptr, kptr, ib->length_dw * 4);
Reviewed-by
#3 lore raw

Re: [PATCH 2/3] drm/amdgpu: parse CS IBs at the BO offset their VA mapping starts at

Quoted 13 lines · Christian König
Quoted 11 lines · Jesse Zhang

For rings which parse or patch the IBs on the CPU, like JPEG and VCN, the CS looks the IB up through its VA mapping and maps the BO, but ignores the offset in the BO the mapping starts at. It then parses other data than the GPU executes. On navi31, an IB of JPEG NOPs in the second page of a BO, mapped with offset_in_bo 4096, is parsed from the zeros in the first page and rejected:

amdgpu 0000:03:00.0: Invalid packet [0x00000000]!

while the same IB submitted through a mapping of the whole BO works.

Quoted 8 lines
Quoted 4 lines

Add the mapping offset when locating the IB in the BO.

Fixes 4802ce117786 ("drm/amdgpu: fix UVD/VCE VM emulation")

IIRC that was brought up before, I wonder why that patch wasn't merged.

@Alex do you remember?

I think the last time this was found was in the IB dumping code, which, IIRC, we fixed? The patch looks good to me:

Alex

Quoted 22 lines

Regards,
Christian.

Quoted 17 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
index fa301c71b782..4a277f04cde5 100644
@@ -1073,7 +1073,7 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
              if (r)
                      return r;
 
-             kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE);
+             kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE) + m->offset;
 
              if (ring->funcs->parse_cs) {
                      memcpy(ib->ptr, kptr, ib->length_dw * 4);
#4 lore raw

PATCH 3/3 drm/amdgpu: wait for the GEM_VA input fences interruptibly

In reply to Jesse Zhang

GEM_VA waits for the fences of its input syncobjs uninterruptibly. Those are provided by userspace and can take arbitrarily long to signal, and while waiting the task can't even be killed.

On navi31, with a sw_sync fence as
input fence, the task stays in D state after SIGKILL until the fence is signaled. Holding the fence for 260s on the latter, the hung task detector complains:

  INFO: task va_infence_kill:17316 blocked for more than 120 seconds.
  task:va_infence_kill state:D stack:0     pid:17316 tgid:17316 ppid:17315
  Call Trace:
   __schedule+0x44f/0x1960
   schedule+0x2e/0x100
   schedule_timeout+0xee/0x100
   dma_fence_default_wait+0xe0/0x260
   dma_fence_wait_timeout+0xaf/0x1b0
   amdgpu_gem_va_ioctl+0x3aa/0x940 [amdgpu]
   drm_ioctl_kernel+0xac/0x110 [drm]
   drm_ioctl+0x2c6/0x5b0 [drm]
   __x64_sys_ioctl+0xa0/0xf0

Wait interruptibly and return the error, nothing has been changed yet at that point, so the ioctl can simply be restarted.

Fixes 38c67ec9aa4b ("drm/amdgpu: Add input fence to sync bo map/unmap")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -2
index c982d102614e..29960d932e75 100644
@@ -77,10 +77,11 @@ amdgpu_gem_add_input_fence(struct drm_file *filp,
 		if (ret)
 			goto free_memdup;
 
-		dma_fence_wait(fence, false);
-
 		/* TODO: optimize async handling */
+		ret = dma_fence_wait(fence, true);
 		dma_fence_put(fence);
+		if (ret)
+			goto free_memdup;
 	}
 
 free_memdup:
Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 1/3
#5 lore raw
In reply to Jesse Zhang
Quoted 6 lines · Jesse Zhang

GEM_VA takes the GPU virtual addresses above the VA hole sign extended, like the rest of the UAPI, but GET_MAPPING_INFO reports them without the upper bits, so they land in the hole. Mappings in the upper half of the address space can't be recreated or removed with what it returns. On navi31:

Quoted 8 lines
  MAP at 0xffff800100000000                 -> 0
  GET_MAPPING_INFO                          -> addr 0x800100000000
  UNMAP at 0x800100000000                   -> -EINVAL

Sign extend the addresses before handing them out.

Fixes 4d82724f7f2b ("drm/amdgpu: Add mapping info option for GEM_OP ioctl")
Quoted 36 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +5 -2
index fb60a99f43c1..c982d102614e 100644
@@ -1082,6 +1082,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *filp)
 {
+	struct amdgpu_device *adev = drm_to_adev(dev);
 	struct drm_amdgpu_gem_op *args = data;
 	struct drm_gem_object *gobj;
 	struct amdgpu_vm_bo_base *base;
@@ -1188,7 +1189,8 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 
 		amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) {
 			if (num_mappings < args->num_entries) {
-				vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
+				vm_entries[num_mappings].addr =
+					amdgpu_gmc_sign_extend(mapping->start * AMDGPU_GPU_PAGE_SIZE);
 				vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
 				vm_entries[num_mappings].offset = mapping->offset;
 				vm_entries[num_mappings].flags = mapping->flags;
@@ -1198,7 +1200,8 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 
 		amdgpu_vm_bo_va_for_each_invalid_mapping(bo_va, mapping) {
 			if (num_mappings < args->num_entries) {
-				vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
+				vm_entries[num_mappings].addr =
+					amdgpu_gmc_sign_extend(mapping->start * AMDGPU_GPU_PAGE_SIZE);
 				vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
 				vm_entries[num_mappings].offset = mapping->offset;
 				vm_entries[num_mappings].flags = mapping->flags;