The coredump has the addresses of the IBs of the hung job, and the IP
dump has addresses the hardware was accessing, but nothing to tell
which BO those addresses belong to, or whether they were mapped at all.

At hang time, under the same root PD trylock used to copy the IBs,
record every mapping of the job's VM: its address range, offset into
the BO, AMDGPU_VM_PAGE_* flags, and the size and current placement of
the BO behind it. A BO with its own reservation object can move under
us, so its placement is only read if the BO can be trylocked. Userspace
decides how many mappings there are, so the list is capped at 1024
mappings, which bounds the GFP_NOWAIT allocation and how long the root
PD is held, and the dump says when mappings were left out. If the root
PD is contended or the allocation fails, the list is made by the
coredump worker instead and marked as made after recovery started.

Add amdgpu_vm_bo_next_mapping() to iterate over a VM's mappings from
outside amdgpu_vm.c.

Suggested-by: Gregoire Pean <[email protected]>
Signed-off-by: Matt Turner <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c | 179 ++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h |  19 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c           |  22 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h           |   3 +
 4 files changed, 218 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
index 19e3a47ded79..8395e9d35f7e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
@@ -56,6 +56,14 @@ void amdgpu_coredump_fini(struct amdgpu_device *adev)
  */
 #define AMDGPU_COREDUMP_NOWAIT_MAX_BYTES SZ_8M
 
+/* Only this many VM mappings are listed in the coredump. */
+#define AMDGPU_COREDUMP_MAX_MAPPINGS 1024
+
+/* Placement of a mapping without a BO or whose BO has no backing store */
+#define AMDGPU_COREDUMP_MEM_TYPE_NONE          -1
+/* Placement of a mapping whose BO could not be locked to read it */
+#define AMDGPU_COREDUMP_MEM_TYPE_UNKNOWN       -2
+
 const char *hw_ip_names[MAX_HWIP] = {
        [GC_HWIP]               = "GC",
        [HDP_HWIP]              = "HDP",
@@ -416,6 +424,79 @@ static bool amdgpu_devcoredump_blob_retry(struct 
amdgpu_coredump_blob *blob)
        return !blob->data && amdgpu_devcoredump_status_retry(blob->status);
 }
 
+/*
+ * Read the placement of @abo. A BO that does not share the root PD's
+ * reservation object can be moved concurrently, so its placement is only read
+ * if it can be trylocked.
+ */
+static int amdgpu_devcoredump_bo_mem_type(struct amdgpu_vm *vm,
+                                         struct amdgpu_bo *abo)
+{
+       struct dma_resv *resv = abo->tbo.base.resv;
+       bool shared = resv == vm->root.bo->tbo.base.resv;
+       int mem_type;
+
+       if (!shared && !dma_resv_trylock(resv))
+               return AMDGPU_COREDUMP_MEM_TYPE_UNKNOWN;
+
+       mem_type = abo->tbo.resource ? abo->tbo.resource->mem_type :
+                  AMDGPU_COREDUMP_MEM_TYPE_NONE;
+
+       if (!shared)
+               dma_resv_unlock(resv);
+
+       return mem_type;
+}
+
+/*
+ * Record the address, BO and flags of the mappings of @vm. The VM root PD
+ * must be reserved by the caller.
+ */
+static int amdgpu_devcoredump_capture_mappings(struct amdgpu_vm *vm,
+                                              struct amdgpu_coredump_info 
*coredump,
+                                              bool nowait)
+{
+       struct amdgpu_bo_va_mapping *mapping = NULL;
+       u32 count = 0, n = 0;
+
+       /*
+        * Userspace decides how many mappings the VM has, so don't walk more
+        * of them than are listed while the root PD is held.
+        */
+       while (count <= AMDGPU_COREDUMP_MAX_MAPPINGS &&
+              (mapping = amdgpu_vm_bo_next_mapping(vm, mapping)))
+               count++;
+
+       coredump->more_mappings = count > AMDGPU_COREDUMP_MAX_MAPPINGS;
+       count = min(count, AMDGPU_COREDUMP_MAX_MAPPINGS);
+       if (!count)
+               return 0;
+
+       coredump->mappings = kvzalloc_objs(struct amdgpu_coredump_mapping, 
count,
+                                          (nowait ? GFP_NOWAIT : GFP_KERNEL) |
+                                          __GFP_NOWARN);
+       if (!coredump->mappings)
+               return -ENOMEM;
+
+       mapping = NULL;
+       while (n < count && (mapping = amdgpu_vm_bo_next_mapping(vm, mapping))) 
{
+               struct amdgpu_coredump_mapping *m = &coredump->mappings[n++];
+               struct amdgpu_bo *abo = amdgpu_devcoredump_mapping_bo(mapping);
+
+               m->va = mapping->start * AMDGPU_GPU_PAGE_SIZE;
+               m->size = (mapping->last - mapping->start + 1) *
+                         AMDGPU_GPU_PAGE_SIZE;
+               m->offset = mapping->offset;
+               m->flags = mapping->flags;
+               m->bo_size = abo ? amdgpu_bo_size(abo) : 0;
+               m->mem_type = abo ? amdgpu_devcoredump_bo_mem_type(vm, abo) :
+                             AMDGPU_COREDUMP_MEM_TYPE_NONE;
+       }
+       coredump->num_mappings = n;
+
+       return 0;
+}
+
 /* Copy @blob at hang time if it fits in what is left of @budget. */
 static void amdgpu_devcoredump_copy_blob_nowait(struct amdgpu_vm *vm,
                                                struct amdgpu_coredump_blob 
*blob,
@@ -451,6 +532,7 @@ amdgpu_devcoredump_capture_vm_nowait(struct amdgpu_device 
*adev,
                        if (blob->status == -EAGAIN)
                                blob->status = PTR_ERR(vm);
                }
+               coredump->mappings_status = PTR_ERR(vm);
                return;
        }
 
@@ -458,14 +540,92 @@ amdgpu_devcoredump_capture_vm_nowait(struct amdgpu_device 
*adev,
                amdgpu_devcoredump_copy_blob_nowait(vm, &coredump->ibs[i].blob,
                                                    &budget);
 
+       coredump->mappings_status =
+               amdgpu_devcoredump_capture_mappings(vm, coredump, true);
+
        dma_resv_unlock(vm->root.bo->tbo.base.resv);
 }
 
+static const char *amdgpu_devcoredump_mem_type_name(int mem_type)
+{
+       switch (mem_type) {
+       case AMDGPU_COREDUMP_MEM_TYPE_NONE:
+               return "none";
+       case AMDGPU_COREDUMP_MEM_TYPE_UNKNOWN:
+               return "unknown";
+       case TTM_PL_SYSTEM:
+               return "CPU";
+       case TTM_PL_TT:
+               return "GTT";
+       case TTM_PL_VRAM:
+               return "VRAM";
+       case AMDGPU_PL_GDS:
+               return "GDS";
+       case AMDGPU_PL_GWS:
+               return "GWS";
+       case AMDGPU_PL_OA:
+               return "OA";
+       case AMDGPU_PL_PREEMPT:
+               return "PREEMPT";
+       case AMDGPU_PL_DOORBELL:
+               return "DOORBELL";
+       case AMDGPU_PL_MMIO_REMAP:
+               return "MMIO_REMAP";
+       default:
+               return "other";
+       }
+}
+
+static void
+amdgpu_devcoredump_print_mappings(struct drm_printer *p,
+                                 struct amdgpu_coredump_info *coredump)
+{
+       struct amdgpu_device *adev = coredump->adev;
+
+       if (coredump->mappings_status) {
+               drm_printf(p, "\nVM mappings unavailable: %pe\n",
+                          ERR_PTR(coredump->mappings_status));
+               return;
+       }
+
+       drm_printf(p, "\nVM mappings: %u\n", coredump->num_mappings);
+       if (coredump->mappings_late)
+               drm_printf(p, "mappings listed after recovery started\n");
+
+       for (u32 i = 0; i < coredump->num_mappings; i++) {
+               struct amdgpu_coredump_mapping *m = &coredump->mappings[i];
+
+               drm_printf(p,
+                          "0x%016llx-0x%016llx offset: 0x%llx flags: 0x%x 
bo_size: 0x%llx domain: %s\n",
+                          amdgpu_gmc_sign_extend(m->va),
+                          amdgpu_gmc_sign_extend(m->va + m->size - 1),
+                          m->offset, m->flags, m->bo_size,
+                          amdgpu_devcoredump_mem_type_name(m->mem_type));
+       }
+
+       if (coredump->more_mappings)
+               drm_printf(p, "more VM mappings not listed\n");
+}
+
+/*
+ * List the VM mappings if that failed at hang time. The VM root PD must be
+ * reserved by the caller.
+ */
+static void amdgpu_devcoredump_list_vm_late(struct amdgpu_vm *vm,
+                                           struct amdgpu_coredump_info 
*coredump)
+{
+       if (amdgpu_devcoredump_status_retry(coredump->mappings_status)) {
+               coredump->mappings_status =
+                       amdgpu_devcoredump_capture_mappings(vm, coredump, 
false);
+               coredump->mappings_late = !coredump->mappings_status;
+       }
+}
+
 /*
  * Retry what could not be captured at hang time, blocking on the reservation
  * locks as needed. This runs from the coredump worker, by which point recovery
- * may have modified or freed the memory, so the result is marked as captured
- * late.
+ * may have modified or freed the memory or changed the mappings, so the result
+ * is marked as captured late.
  */
 static void
 amdgpu_devcoredump_capture_vm_late(struct amdgpu_coredump_info *coredump)
@@ -475,9 +635,10 @@ amdgpu_devcoredump_capture_vm_late(struct 
amdgpu_coredump_info *coredump)
        struct amdgpu_bo *abo;
        struct drm_exec exec;
        struct amdgpu_vm *vm;
-       bool retry = false;
+       bool retry;
        int r = 0;
 
+       retry = amdgpu_devcoredump_status_retry(coredump->mappings_status);
        for (int i = 0; i < coredump->num_ibs; i++)
                retry |= amdgpu_devcoredump_blob_retry(&coredump->ibs[i].blob);
        if (!retry)
@@ -501,6 +662,8 @@ amdgpu_devcoredump_capture_vm_late(struct 
amdgpu_coredump_info *coredump)
                        goto unlock;
                }
 
+               amdgpu_devcoredump_list_vm_late(vm, coredump);
+
                for (int i = 0; i < coredump->num_ibs; i++) {
                        blob = &coredump->ibs[i].blob;
                        if (!amdgpu_devcoredump_blob_retry(blob))
@@ -531,6 +694,8 @@ amdgpu_devcoredump_capture_vm_late(struct 
amdgpu_coredump_info *coredump)
        drm_exec_fini(&exec);
 
        if (r) {
+               if (amdgpu_devcoredump_status_retry(coredump->mappings_status))
+                       coredump->mappings_status = r;
                for (int i = 0; i < coredump->num_ibs; i++) {
                        blob = &coredump->ibs[i].blob;
                        if (amdgpu_devcoredump_blob_retry(blob))
@@ -802,6 +967,9 @@ amdgpu_devcoredump_format(char *buffer, size_t count, 
struct amdgpu_coredump_inf
        else if (coredump->reset_vram_lost)
                drm_printf(&p, "VRAM is lost due to GPU reset!\n");
 
+       if (coredump->pasid)
+               amdgpu_devcoredump_print_mappings(&p, coredump);
+
        if (coredump->num_ibs)
                amdgpu_devcoredump_print_ibs(&p, coredump);
 
@@ -846,6 +1014,7 @@ static void amdgpu_devcoredump_free(void *data)
        u32 i;
 
        kvfree(coredump->formatted);
+       kvfree(coredump->mappings);
        amdgpu_devcoredump_free_blobs(coredump);
        for (i = 0; i < coredump->num_rings; i++) {
                kvfree(coredump->rings[i].ring_dw);
@@ -863,7 +1032,7 @@ static void amdgpu_devcoredump_deferred_work(struct 
work_struct *work)
        if (!coredump)
                goto end;
 
-       if (coredump->num_ibs)
+       if (coredump->pasid)
                amdgpu_devcoredump_capture_vm_late(coredump);
 
        /* Do a one-time preparation of the coredump output because
@@ -1053,7 +1222,7 @@ void amdgpu_coredump(struct amdgpu_device *adev, bool 
skip_vram_check,
                coredump->num_rings = idx;
        }
 
-       if (coredump->num_ibs)
+       if (coredump->pasid)
                amdgpu_devcoredump_capture_vm_nowait(adev, coredump);
 
        coredump->adev = adev;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
index 97d5fbe8320e..844fc87f386e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
@@ -71,6 +71,16 @@ struct amdgpu_coredump_ib_info {
        struct amdgpu_coredump_blob     blob;
 };
 
+struct amdgpu_coredump_mapping {
+       u64                             va;
+       u64                             size;
+       u64                             offset;
+       u64                             bo_size;
+       u32                             flags;
+       /* TTM_PL_* of the BO, or AMDGPU_COREDUMP_MEM_TYPE_* */
+       int                             mem_type;
+};
+
 struct amdgpu_coredump_info {
        struct amdgpu_device            *adev;
        struct amdgpu_task_info         reset_task_info;
@@ -97,6 +107,15 @@ struct amdgpu_coredump_info {
        unsigned int                    dropped;
        unsigned int                    pasid;
        unsigned int                    vmid;
+       /* VM mappings of the hung job, or NULL with the reason in status */
+       struct amdgpu_coredump_mapping  *mappings;
+       u32                             num_mappings;
+       /* the VM has more than AMDGPU_COREDUMP_MAX_MAPPINGS mappings */
+       bool                            more_mappings;
+       int                             mappings_status;
+       /* listed by the worker rather than when the hang was detected */
+       bool                            mappings_late;
+
        pid_t                           submit_tgid;
        pid_t                           submit_pid;
        char                            submit_process_name[TASK_COMM_LEN];
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index b03f3ee4d78c..159a22406db7 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -2201,6 +2201,28 @@ struct amdgpu_bo_va_mapping 
*amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
        return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
 }
 
+/**
+ * amdgpu_vm_bo_next_mapping - iterate over all mappings of a VM
+ *
+ * @vm: the requested VM
+ * @mapping: the previous mapping, or NULL to start
+ *
+ * The VM root PD must be reserved by the caller.
+ *
+ * Returns:
+ * The mapping following @mapping in address order, the first mapping if
+ * @mapping is NULL, or NULL at the end.
+ */
+struct amdgpu_bo_va_mapping *
+amdgpu_vm_bo_next_mapping(struct amdgpu_vm *vm,
+                         struct amdgpu_bo_va_mapping *mapping)
+{
+       if (!mapping)
+               return amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX);
+
+       return amdgpu_vm_it_iter_next(mapping, 0, U64_MAX);
+}
+
 /**
  * amdgpu_vm_bo_trace_cs - trace all reserved mappings
  *
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index 10fd477f7765..e74077d25029 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -504,6 +504,9 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
                                uint64_t saddr, uint64_t size);
 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
                                                         uint64_t addr);
+struct amdgpu_bo_va_mapping *
+amdgpu_vm_bo_next_mapping(struct amdgpu_vm *vm,
+                         struct amdgpu_bo_va_mapping *mapping);
 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx 
*ticket);
 void amdgpu_vm_bo_del(struct amdgpu_device *adev,
                      struct amdgpu_bo_va *bo_va);

-- 
2.56.1

Reply via email to