amdgpu_devcoredump_print_ibs() has several problems when reading the
contents of the IBs of the hung job:

- PRT mappings have a bo_va with no BO behind it, so an IB address that
  falls into a PRT range dereferences a NULL BO in both the lock loop
  and the copy loop.
- A BO without a resource (e.g. purged) is dereferenced via
  tbo.resource->mem_type.
- The offset into the BO ignores mapping->offset, so for a mapping that
  does not start at the beginning of its BO the wrong data is dumped.
- There is no check that the IB lies within the mapping, so a bogus IB
  address or size from a misbehaving process reads outside the mapping,
  possibly past the end of the BO.
- ttm_bo_kmap() maps the entire BO, which needlessly vmaps large BOs,
  and the mapping is stored in abo->kmap, clobbering any kernel mapping
  the driver already holds there.
- BOs with AMDGPU_GEM_CREATE_NO_CPU_ACCESS that have been moved to GTT
  are skipped, while BOs in CPU invisible VRAM without that flag fail
  in ttm_bo_kmap().

Skip mappings with no BO and BOs with no resource, honor
mapping->offset, and check that the IB lies within its mapping, which
amdgpu_vm_verify_parameters() already keeps within the BO. Read VRAM
through amdgpu_device_vram_access(), which uses the CPU visible
aperture where it can and MM_INDEX/MM_DATA for the rest, and kmap only
the pages covering the IB into a local map object for everything else.

Fixes: 7b15fc2d1f1a ("drm/amdgpu: dump job ibs in the devcoredump")
Suggested-by: Gregoire Pean <[email protected]>
Signed-off-by: Matt Turner <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c | 67 ++++++++++++++++--------
 1 file changed, 46 insertions(+), 21 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
index 30a8b4c5591d..0c6533b950f6 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
@@ -209,6 +209,16 @@ static void amdgpu_devcoredump_fw_info(struct 
amdgpu_device *adev,
        }
 }
 
+static struct amdgpu_bo *
+amdgpu_devcoredump_mapping_bo(struct amdgpu_bo_va_mapping *mapping)
+{
+       /* PRT mappings have a bo_va with no BO behind it. */
+       if (!mapping || !mapping->bo_va)
+               return NULL;
+
+       return mapping->bo_va->base.bo;
+}
+
 static void
 amdgpu_devcoredump_print_ibs(struct drm_printer *p,
                             struct amdgpu_coredump_info *coredump,
@@ -259,10 +269,10 @@ amdgpu_devcoredump_print_ibs(struct drm_printer *p,
                        u64 addr = coredump->ibs[i].gpu_addr & 
AMDGPU_GMC_HOLE_MASK;
 
                        mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
-                       if (!mapping)
+                       abo = amdgpu_devcoredump_mapping_bo(mapping);
+                       if (!abo)
                                continue;
 
-                       abo = mapping->bo_va->base.bo;
                        r = drm_exec_lock_obj(&exec, &abo->tbo.base);
                        drm_exec_retry_on_contention(&exec);
                        if (r)
@@ -271,6 +281,7 @@ amdgpu_devcoredump_print_ibs(struct drm_printer *p,
        }
 
        for (int i = 0; i < coredump->num_ibs; i++) {
+               u64 size = (u64)coredump->ibs[i].ib_size_dw * 4;
                bool emit_content = false;
 
                ib_content = kvmalloc_array(coredump->ibs[i].ib_size_dw, 4,
@@ -280,41 +291,55 @@ amdgpu_devcoredump_print_ibs(struct drm_printer *p,
 
                va_start = coredump->ibs[i].gpu_addr & AMDGPU_GMC_HOLE_MASK;
                mapping = amdgpu_vm_bo_lookup_mapping(vm, va_start);
-               if (!mapping)
+               abo = amdgpu_devcoredump_mapping_bo(mapping);
+               if (!abo || !abo->tbo.resource)
                        goto output_ib_content;
 
-               abo = mapping->bo_va->base.bo;
-               offset = va_start - mapping->start * AMDGPU_GPU_PAGE_SIZE;
+               if (va_start + size >
+                   (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE)
+                       goto output_ib_content;
 
-               if (abo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) {
+               offset = va_start - mapping->start * AMDGPU_GPU_PAGE_SIZE +
+                        mapping->offset;
+
+               if (abo->tbo.resource->mem_type == TTM_PL_VRAM) {
                        struct amdgpu_res_cursor cursor;
 
                        off = 0;
-
-                       if (abo->tbo.resource->mem_type != TTM_PL_VRAM)
-                               goto output_ib_content;
-
-                       amdgpu_res_first(abo->tbo.resource, offset,
-                                        coredump->ibs[i].ib_size_dw * 4, 
&cursor);
+                       amdgpu_res_first(abo->tbo.resource, offset, size,
+                                        &cursor);
                        while (cursor.remaining) {
-                               amdgpu_device_mm_access(adev, cursor.start,
-                                                       &ib_content[off], 
cursor.size,
-                                                       false);
+                               amdgpu_device_vram_access(adev, cursor.start,
+                                                         &ib_content[off],
+                                                         cursor.size, false);
                                off += cursor.size / 4;
                                amdgpu_res_next(&cursor, cursor.size);
                        }
                        emit_content = true;
                } else {
-                       r = ttm_bo_kmap(&abo->tbo, 0, 
PFN_UP(abo->tbo.base.size),
-                                       &abo->kmap);
+                       struct ttm_bo_kmap_obj map;
+                       unsigned long first = offset >> PAGE_SHIFT;
+                       bool is_iomem;
+
+                       /*
+                        * Map only the pages backing the IB, into a local map
+                        * object so a kernel mapping held in abo->kmap is left
+                        * untouched.
+                        */
+                       r = ttm_bo_kmap(&abo->tbo, first,
+                                       PFN_UP(offset + size) - first, &map);
                        if (r)
                                goto output_ib_content;
 
-                       kptr = amdgpu_bo_kptr(abo);
-                       kptr += offset;
-                       memcpy(ib_content, kptr, coredump->ibs[i].ib_size_dw * 
4);
+                       kptr = ttm_kmap_obj_virtual(&map, &is_iomem);
+                       kptr += offset_in_page(offset);
+                       if (is_iomem)
+                               memcpy_fromio(ib_content,
+                                             (void __iomem *)kptr, size);
+                       else
+                               memcpy(ib_content, kptr, size);
 
-                       amdgpu_bo_kunmap(abo);
+                       ttm_bo_kunmap(&map);
                        emit_content = true;
                }
 

-- 
2.56.1

Reply via email to