The IB contents are read from inside the formatting function, which
runs twice: once to size the output and once to fill it. The sizing
pass does not lock the VM, so it prints a placeholder for every dword,
and only the second pass reads the BOs and finds out whether an IB is
readable. Nothing records why one could not be read.

Copy the IB contents once from the coredump worker before formatting,
into a buffer per IB along with an error code explaining why the
contents are missing. Both formatting passes then print the same data,
and the dump says why an IB is absent. This replaces the ttm_bo_kmap()
based read from "drm/amdgpu: fix IB content read in the devcoredump".

The copy only needs a GPU address and a size within one mapping, so
describe it with a struct amdgpu_coredump_blob, which later changes use
to copy other ranges of the VM.

Factor the BO read into a helper that does not allocate and does not
map the whole BO. VRAM is read through amdgpu_device_vram_access() 4 KiB
at a time, so that the MM_INDEX/MM_DATA fallback for CPU invisible VRAM
never keeps interrupts disabled for long. System memory is read one
page at a time with kmap_local_page() when the kernel's linear mapping
of the pages can be used: TTM only gives it the caching of the BO on
x86, and maps decrypted BOs with pgprot_decrypted(), so other BOs are
read through ttm_bo_kmap() of just the range being copied. BOs whose
pages are not resident are refused.

The copies are freed once the dump is formatted, since reading the
dump only uses the formatted text.

Suggested-by: Gregoire Pean <[email protected]>
Signed-off-by: Matt Turner <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c | 319 +++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h |  11 +
 2 files changed, 224 insertions(+), 106 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
index 4d1b89527545..c3f01acc0e8c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
@@ -25,6 +25,7 @@
 #include <linux/devcoredump.h>
 #include <linux/utsname.h>
 #include <drm/drm_exec.h>
+#include <drm/ttm/ttm_tt.h>
 #include "amdgpu_dev_coredump.h"
 #include "atom.h"
 
@@ -222,38 +223,157 @@ amdgpu_devcoredump_mapping_bo(struct 
amdgpu_bo_va_mapping *mapping)
        return mapping->bo_va->base.bo;
 }
 
-static void
-amdgpu_devcoredump_print_ibs(struct drm_printer *p,
-                            struct amdgpu_coredump_info *coredump,
-                            bool sizing_pass)
+/*
+ * Whether the pages of @ttm can be read through the kernel's linear mapping.
+ * TTM only gives that mapping the caching of the BO on x86, and maps the pages
+ * of a decrypted BO with pgprot_decrypted(), so read anything else through
+ * ttm_bo_kmap() as TTM does.
+ */
+static bool amdgpu_devcoredump_tt_linear_ok(struct ttm_tt *ttm)
+{
+       if (ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
+               return false;
+
+       return ttm->caching == ttm_cached || IS_ENABLED(CONFIG_X86);
+}
+
+/*
+ * Copy @size bytes at @offset in @abo to @dst. The caller holds the BO's
+ * reservation. VRAM is read 4 KiB at a time, which bounds how long the
+ * MM_INDEX/MM_DATA fallback for CPU invisible VRAM keeps interrupts disabled
+ * whatever the page size.
+ */
+static int amdgpu_devcoredump_read_bo(struct amdgpu_bo *abo, u64 offset,
+                                     void *dst, u64 size)
+{
+       struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+       struct ttm_resource *res = abo->tbo.resource;
+       struct ttm_tt *ttm = abo->tbo.ttm;
+       struct ttm_bo_kmap_obj map;
+       bool is_iomem;
+       int r;
+
+       if (!res)
+               return -ENODATA;
+
+       if (!IS_ALIGNED(offset, 4) || !IS_ALIGNED(size, 4))
+               return -EINVAL;
+
+       switch (res->mem_type) {
+       case TTM_PL_VRAM: {
+               struct amdgpu_res_cursor cursor;
+
+               amdgpu_res_first(res, offset, size, &cursor);
+               while (cursor.remaining) {
+                       u64 len = min_t(u64, cursor.size, SZ_4K);
+
+                       amdgpu_device_vram_access(adev, cursor.start, dst, len,
+                                                 false);
+                       dst += len;
+                       amdgpu_res_next(&cursor, len);
+                       cond_resched();
+               }
+               return 0;
+       }
+       case TTM_PL_SYSTEM:
+       case TTM_PL_TT:
+       case AMDGPU_PL_PREEMPT:
+               if (!ttm || !ttm->pages)
+                       return -EOPNOTSUPP;
+
+               if (!ttm_tt_is_populated(ttm) ||
+                   ttm->page_flags & (TTM_TT_FLAG_SWAPPED | 
TTM_TT_FLAG_BACKED_UP))
+                       return -ENODATA;
+
+               if (!amdgpu_devcoredump_tt_linear_ok(ttm)) {
+                       pgoff_t first = offset >> PAGE_SHIFT;
+
+                       r = ttm_bo_kmap(&abo->tbo, first,
+                                       PFN_UP(offset + size) - first, &map);
+                       if (r)
+                               return r;
+                       memcpy(dst, ttm_kmap_obj_virtual(&map, &is_iomem) +
+                              offset_in_page(offset), size);
+                       ttm_bo_kunmap(&map);
+                       return 0;
+               }
+
+               while (size) {
+                       pgoff_t idx = offset >> PAGE_SHIFT;
+                       size_t pg_off = offset_in_page(offset);
+                       size_t len = min_t(u64, size, PAGE_SIZE - pg_off);
+                       void *src;
+
+                       if (idx >= ttm->num_pages || !ttm->pages[idx])
+                               return -ENODATA;
+
+                       src = kmap_local_page(ttm->pages[idx]);
+                       memcpy(dst, src + pg_off, len);
+                       kunmap_local(src);
+
+                       dst += len;
+                       offset += len;
+                       size -= len;
+                       cond_resched();
+               }
+               return 0;
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
+/*
+ * Copy a range of GPU memory, which must lie within a single mapping, into a
+ * newly allocated buffer. The VM root PD and the BO behind the mapping must be
+ * reserved by the caller.
+ */
+static int amdgpu_devcoredump_copy_blob(struct amdgpu_vm *vm,
+                                       struct amdgpu_coredump_blob *blob)
+{
+       struct amdgpu_bo_va_mapping *mapping;
+       struct amdgpu_bo *abo;
+       u64 offset;
+       void *data;
+       int r;
+
+       mapping = amdgpu_vm_bo_lookup_mapping(vm, blob->va);
+       abo = amdgpu_devcoredump_mapping_bo(mapping);
+       if (!abo)
+               return -ENOENT;
+
+       if (blob->va + blob->size > (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE)
+               return -ERANGE;
+
+       offset = blob->va - mapping->start * AMDGPU_GPU_PAGE_SIZE +
+                mapping->offset;
+
+       /* amdgpu_device_vram_access() can fail without telling us. */
+       data = kvzalloc(blob->size, GFP_KERNEL | __GFP_NOWARN);
+       if (!data)
+               return -ENOMEM;
+
+       r = amdgpu_devcoredump_read_bo(abo, offset, data, blob->size);
+       if (r) {
+               kvfree(data);
+               return r;
+       }
+
+       blob->data = data;
+       return 0;
+}
+
+/*
+ * Copy the contents of the IBs of the hung job, blocking on the reservation
+ * locks as needed. This runs from the coredump worker, before formatting, so
+ * that both formatting passes see the same data.
+ */
+static void amdgpu_devcoredump_capture_vm(struct amdgpu_coredump_info 
*coredump)
 {
-       struct amdgpu_device *adev = coredump->adev;
        struct amdgpu_bo_va_mapping *mapping;
        struct amdgpu_bo *abo;
        struct drm_exec exec;
        struct amdgpu_vm *vm;
-       u32 *ib_content;
-       u64 va_start, offset;
-       u8 *kptr;
-       u32 off;
-       int r;
-
-       /*
-        * On the sizing pass there is no VM to look up and no BO to lock; the
-        * size estimate doesn't depend on whether the IB BOs are reachable.
-        * Just emit the per-IB headers (the content is not written anywhere).
-        */
-       if (sizing_pass) {
-               for (int i = 0; i < coredump->num_ibs; i++) {
-                       drm_printf(p, "\nIB #%d 0x%llx %d dw\n", i,
-                                  coredump->ibs[i].gpu_addr,
-                                  coredump->ibs[i].ib_size_dw);
-
-                       for (int j = 0; j < coredump->ibs[i].ib_size_dw; j++)
-                               drm_printf(p, "0xffffffff\n");
-               }
-               return;
-       }
+       int r = 0;
 
        /*
         * Lock the VM root PD and every IB BO together in a single drm_exec
@@ -264,14 +384,18 @@ amdgpu_devcoredump_print_ibs(struct drm_printer *p,
         */
        drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 1 + coredump->num_ibs);
        drm_exec_until_all_locked(&exec) {
-               vm = amdgpu_vm_lock_by_pasid(adev, coredump->pasid, &exec);
-               if (!vm)
+               /* Don't keep an -EDEADLK from a previous pass. */
+               r = 0;
+               vm = amdgpu_vm_lock_by_pasid(coredump->adev, coredump->pasid,
+                                            &exec);
+               if (!vm) {
+                       r = -ESRCH;
                        goto unlock;
+               }
 
                for (int i = 0; i < coredump->num_ibs; i++) {
-                       u64 addr = coredump->ibs[i].gpu_addr & 
AMDGPU_GMC_HOLE_MASK;
-
-                       mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
+                       mapping = amdgpu_vm_bo_lookup_mapping(vm,
+                                                             
coredump->ibs[i].blob.va);
                        abo = amdgpu_devcoredump_mapping_bo(mapping);
                        if (!abo)
                                continue;
@@ -284,80 +408,47 @@ 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;
+               struct amdgpu_coredump_blob *blob = &coredump->ibs[i].blob;
 
-               ib_content = kvmalloc_array(coredump->ibs[i].ib_size_dw, 4,
-                                           GFP_KERNEL);
-               if (!ib_content)
-                       continue;
-
-               va_start = coredump->ibs[i].gpu_addr & AMDGPU_GMC_HOLE_MASK;
-               mapping = amdgpu_vm_bo_lookup_mapping(vm, va_start);
-               abo = amdgpu_devcoredump_mapping_bo(mapping);
-               if (!abo || !abo->tbo.resource)
-                       goto output_ib_content;
-
-               if (va_start + size >
-                   (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE)
-                       goto output_ib_content;
-
-               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;
-                       amdgpu_res_first(abo->tbo.resource, offset, size,
-                                        &cursor);
-                       while (cursor.remaining) {
-                               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 {
-                       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 = 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);
-
-                       ttm_bo_kunmap(&map);
-                       emit_content = true;
-               }
-
-output_ib_content:
-               drm_printf(p, "\nIB #%d 0x%llx %d dw\n", i,
-                          coredump->ibs[i].gpu_addr, 
coredump->ibs[i].ib_size_dw);
-               if (emit_content) {
-                       for (int j = 0; j < coredump->ibs[i].ib_size_dw; j++)
-                               drm_printf(p, "0x%08x\n", ib_content[j]);
-               }
-               kvfree(ib_content);
+               blob->status = amdgpu_devcoredump_copy_blob(vm, blob);
        }
 
 unlock:
        drm_exec_fini(&exec);
+
+       if (r) {
+               for (int i = 0; i < coredump->num_ibs; i++)
+                       coredump->ibs[i].blob.status = r;
+       }
+}
+
+/* Print the contents of a blob, or why they are missing. */
+static void amdgpu_devcoredump_print_blob(struct drm_printer *p,
+                                         struct amdgpu_coredump_blob *blob)
+{
+       const u32 *dw = blob->data;
+
+       if (!blob->data) {
+               drm_printf(p, "contents unavailable: %pe\n",
+                          ERR_PTR(blob->status));
+               return;
+       }
+
+       for (u64 i = 0; i < blob->size / 4; i++)
+               drm_printf(p, "0x%08x\n", dw[i]);
+}
+
+static void
+amdgpu_devcoredump_print_ibs(struct drm_printer *p,
+                            struct amdgpu_coredump_info *coredump)
+{
+       for (int i = 0; i < coredump->num_ibs; i++) {
+               struct amdgpu_coredump_ib_info *ib = &coredump->ibs[i];
+
+               drm_printf(p, "\nIB #%d 0x%llx %d dw\n", i, ib->gpu_addr,
+                          ib->ib_size_dw);
+               amdgpu_devcoredump_print_blob(p, &ib->blob);
+       }
 }
 
 static ssize_t
@@ -370,9 +461,7 @@ amdgpu_devcoredump_format(char *buffer, size_t count, 
struct amdgpu_coredump_inf
        struct amdgpu_ring *ring;
        int ver, i, j;
        u32 ring_idx;
-       bool sizing_pass;
 
-       sizing_pass = buffer == NULL;
        iter.data = buffer;
        iter.start = 0;
        iter.offset = 0;
@@ -503,7 +592,7 @@ amdgpu_devcoredump_format(char *buffer, size_t count, 
struct amdgpu_coredump_inf
                drm_printf(&p, "VRAM is lost due to GPU reset!\n");
 
        if (coredump->num_ibs)
-               amdgpu_devcoredump_print_ibs(&p, coredump, sizing_pass);
+               amdgpu_devcoredump_print_ibs(&p, coredump);
 
        return count - iter.remain;
 }
@@ -531,12 +620,22 @@ amdgpu_devcoredump_read(char *buffer, loff_t offset, 
size_t count,
        return byte_copied;
 }
 
+/* Free the copied memory, which is only needed to format the dump. */
+static void amdgpu_devcoredump_free_blobs(struct amdgpu_coredump_info 
*coredump)
+{
+       for (int i = 0; i < coredump->num_ibs; i++) {
+               kvfree(coredump->ibs[i].blob.data);
+               coredump->ibs[i].blob.data = NULL;
+       }
+}
+
 static void amdgpu_devcoredump_free(void *data)
 {
        struct amdgpu_coredump_info *coredump = data;
        u32 i;
 
        kvfree(coredump->formatted);
+       amdgpu_devcoredump_free_blobs(coredump);
        for (i = 0; i < coredump->num_rings; i++)
                kvfree(coredump->rings[i].ring_dw);
        kvfree(coredump->rings);
@@ -551,6 +650,9 @@ static void amdgpu_devcoredump_deferred_work(struct 
work_struct *work)
        if (!coredump)
                goto end;
 
+       if (coredump->num_ibs)
+               amdgpu_devcoredump_capture_vm(coredump);
+
        /* Do a one-time preparation of the coredump output because
         * repeatingly calling drm_coredump_printer is very slow.
         */
@@ -563,6 +665,7 @@ static void amdgpu_devcoredump_deferred_work(struct 
work_struct *work)
        }
 
        amdgpu_devcoredump_format(coredump->formatted, 
coredump->formatted_size, coredump);
+       amdgpu_devcoredump_free_blobs(coredump);
 
        /* If there's an existing coredump for this device, the free function 
will be
         * called immediately so coredump might be invalid after the call to 
dev_coredumpm.
@@ -656,8 +759,12 @@ void amdgpu_coredump(struct amdgpu_device *adev, bool 
skip_vram_check,
                coredump->vmid = job->vmid;
                coredump->num_ibs = job->num_ibs;
                for (i = 0; i < job->num_ibs; ++i) {
-                       coredump->ibs[i].gpu_addr = job->ibs[i].gpu_addr;
-                       coredump->ibs[i].ib_size_dw = job->ibs[i].length_dw;
+                       struct amdgpu_coredump_ib_info *ib = &coredump->ibs[i];
+
+                       ib->gpu_addr = job->ibs[i].gpu_addr;
+                       ib->ib_size_dw = job->ibs[i].length_dw;
+                       ib->blob.va = ib->gpu_addr & AMDGPU_GMC_HOLE_MASK;
+                       ib->blob.size = (u64)ib->ib_size_dw * 4;
                }
        }
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
index 6836f312b3f8..ffcb08428631 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
@@ -38,9 +38,20 @@ struct amdgpu_coredump_ring {
        u32                             ring_index;
 };
 
+/* A range of GPU memory copied into the coredump */
+struct amdgpu_coredump_blob {
+       /* GPU VA with the hole bits masked off, and size in bytes */
+       u64                             va;
+       u64                             size;
+       /* contents, or NULL with the reason in status */
+       void                            *data;
+       int                             status;
+};
+
 struct amdgpu_coredump_ib_info {
        uint64_t                        gpu_addr;
        u32                             ib_size_dw;
+       struct amdgpu_coredump_blob     blob;
 };
 
 struct amdgpu_coredump_info {

-- 
2.56.1

Reply via email to