From: Matt Turner <mattst88@gmail.com>
To: "Alex Deucher" <alexander.deucher@amd.com>,
"Christian König" <christian.koenig@amd.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"Sunil Khatri" <sunil.khatri@amd.com>,
"Trigger Huang" <Trigger.Huang@amd.com>
Cc: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org, Matt Turner <mattst88@gmail.com>,
Gregoire Pean <gpean@netflix.com>
Subject: [PATCH 08/14] drm/amdgpu: copy the IB contents when the hang is detected
Date: Wed, 02 Dec 2026 17:36:14 -0500 [thread overview]
Message-ID: <20261202-amdgpu-devcoredump-v1-8-3dfa0e033b97@gmail.com> (raw)
In-Reply-To: <20261202-amdgpu-devcoredump-v1-0-3dfa0e033b97@gmail.com>
amdgpu_coredump() is called from the job timeout handler before soft
recovery and queue reset, but the IB contents are only read later from
the coredump worker, which runs concurrently with recovery. By then the
process may have been killed and its BOs freed or evicted, or a reset
may have lost VRAM, so the dump either has no IB contents or contents
that no longer match what the GPU was executing.
Copy the IBs from amdgpu_coredump() itself, after the ring contents.
The timeout and reset paths are dma_fence signaling critical sections,
so look up the VM with a new amdgpu_vm_trylock_by_pasid() that trylocks
the root PD under the PASID lock, trylock IB BOs that do not share its
reservation object, and allocate with GFP_NOWAIT. VRAM is only read
through the CPU visible aperture there, since reading CPU invisible
VRAM through MM_INDEX/MM_DATA would hold up recovery, and system pages
that would need ttm_bo_kmap() are not read either. IBs that cannot
be copied for any of these reasons are retried from the worker as
before, and are marked in the dump as captured after recovery started.
Skip IBs larger than 4 MiB, since a GFP_NOWAIT allocation that large
is unlikely to succeed anyway. The copy runs in the timeout handler and
delays recovery, and a job can have up to 192 IBs, so copy at most
8 MiB in total there and leave the rest to the worker.
Suggested-by: Gregoire Pean <gpean@netflix.com>
Signed-off-by: Matt Turner <mattst88@gmail.com>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c | 168 ++++++++++++++++++++---
drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h | 2 +
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 35 +++++
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 2 +
4 files changed, 186 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 c3f01acc0e8c..033e1b94fcd7 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
@@ -47,6 +47,15 @@ void amdgpu_coredump_fini(struct amdgpu_device *adev)
#define AMDGPU_CORE_DUMP_SIZE_MAX (256 * 1024 * 1024)
+/* Larger IBs are not copied into the coredump. */
+#define AMDGPU_COREDUMP_IB_MAX_BYTES SZ_4M
+
+/*
+ * Copying at hang time delays recovery, so only this much is copied then. The
+ * rest is left for the worker.
+ */
+#define AMDGPU_COREDUMP_NOWAIT_MAX_BYTES SZ_8M
+
const char *hw_ip_names[MAX_HWIP] = {
[GC_HWIP] = "GC",
[HDP_HWIP] = "HDP",
@@ -242,9 +251,14 @@ static bool amdgpu_devcoredump_tt_linear_ok(struct ttm_tt *ttm)
* 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.
+ *
+ * With @nowait, which the timeout and reset paths use, nothing sleeps, and
+ * VRAM is only read through the CPU visible aperture: reading CPU invisible
+ * VRAM a dword at a time would hold up recovery, so it fails with -EBUSY and
+ * is left for the worker, as do system pages that need ttm_bo_kmap().
*/
static int amdgpu_devcoredump_read_bo(struct amdgpu_bo *abo, u64 offset,
- void *dst, u64 size)
+ void *dst, u64 size, bool nowait)
{
struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
struct ttm_resource *res = abo->tbo.resource;
@@ -267,11 +281,17 @@ static int amdgpu_devcoredump_read_bo(struct amdgpu_bo *abo, u64 offset,
while (cursor.remaining) {
u64 len = min_t(u64, cursor.size, SZ_4K);
- amdgpu_device_vram_access(adev, cursor.start, dst, len,
- false);
+ if (!nowait)
+ amdgpu_device_vram_access(adev, cursor.start, dst,
+ len, false);
+ else if (!adev->mman.aper_base_kaddr ||
+ amdgpu_device_aper_access(adev, cursor.start, dst,
+ len, false) != len)
+ return -EBUSY;
dst += len;
amdgpu_res_next(&cursor, len);
- cond_resched();
+ if (!nowait)
+ cond_resched();
}
return 0;
}
@@ -288,6 +308,9 @@ static int amdgpu_devcoredump_read_bo(struct amdgpu_bo *abo, u64 offset,
if (!amdgpu_devcoredump_tt_linear_ok(ttm)) {
pgoff_t first = offset >> PAGE_SHIFT;
+ if (nowait)
+ return -EBUSY;
+
r = ttm_bo_kmap(&abo->tbo, first,
PFN_UP(offset + size) - first, &map);
if (r)
@@ -314,7 +337,8 @@ static int amdgpu_devcoredump_read_bo(struct amdgpu_bo *abo, u64 offset,
dst += len;
offset += len;
size -= len;
- cond_resched();
+ if (!nowait)
+ cond_resched();
}
return 0;
default:
@@ -324,13 +348,16 @@ static int amdgpu_devcoredump_read_bo(struct amdgpu_bo *abo, u64 offset,
/*
* 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.
+ * newly allocated buffer. The VM root PD must be reserved by the caller. With
+ * @nowait the BO behind the mapping is trylocked here unless it shares the
+ * root PD's reservation object; otherwise the caller must have reserved it.
*/
static int amdgpu_devcoredump_copy_blob(struct amdgpu_vm *vm,
- struct amdgpu_coredump_blob *blob)
+ struct amdgpu_coredump_blob *blob,
+ bool nowait)
{
struct amdgpu_bo_va_mapping *mapping;
+ struct dma_resv *resv = NULL;
struct amdgpu_bo *abo;
u64 offset;
void *data;
@@ -348,11 +375,24 @@ static int amdgpu_devcoredump_copy_blob(struct amdgpu_vm *vm,
mapping->offset;
/* amdgpu_device_vram_access() can fail without telling us. */
- data = kvzalloc(blob->size, GFP_KERNEL | __GFP_NOWARN);
+ data = kvzalloc(blob->size,
+ (nowait ? GFP_NOWAIT : GFP_KERNEL) | __GFP_NOWARN);
if (!data)
return -ENOMEM;
- r = amdgpu_devcoredump_read_bo(abo, offset, data, blob->size);
+ if (nowait && abo->tbo.base.resv != vm->root.bo->tbo.base.resv) {
+ if (!dma_resv_trylock(abo->tbo.base.resv)) {
+ kvfree(data);
+ return -EBUSY;
+ }
+ resv = abo->tbo.base.resv;
+ }
+
+ r = amdgpu_devcoredump_read_bo(abo, offset, data, blob->size, nowait);
+
+ if (resv)
+ dma_resv_unlock(resv);
+
if (r) {
kvfree(data);
return r;
@@ -363,18 +403,86 @@ static int amdgpu_devcoredump_copy_blob(struct amdgpu_vm *vm,
}
/*
- * 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.
+ * Whether a capture that failed with @status, or with -EAGAIN was not tried
+ * yet, could succeed in the worker.
*/
-static void amdgpu_devcoredump_capture_vm(struct amdgpu_coredump_info *coredump)
+static bool amdgpu_devcoredump_status_retry(int status)
+{
+ return status == -EAGAIN || status == -EBUSY || status == -ENOMEM;
+}
+
+static bool amdgpu_devcoredump_blob_retry(struct amdgpu_coredump_blob *blob)
+{
+ return !blob->data && amdgpu_devcoredump_status_retry(blob->status);
+}
+
+/* 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,
+ u64 *budget)
+{
+ if (blob->status != -EAGAIN || blob->size > *budget)
+ return;
+
+ *budget -= blob->size;
+ blob->status = amdgpu_devcoredump_copy_blob(vm, blob, true);
+}
+
+/*
+ * Capture the state of the hung job's VM at the time the hang is detected,
+ * before soft recovery, queue reset or a full GPU reset change it. This runs
+ * in the job timeout or GPU reset path, which is a dma_fence signaling
+ * critical section, so it must not block on reservation locks or allocate
+ * memory that may wait on reclaim: everything is trylocked, and what cannot
+ * be copied here is retried from the worker.
+ */
+static void
+amdgpu_devcoredump_capture_vm_nowait(struct amdgpu_device *adev,
+ struct amdgpu_coredump_info *coredump)
+{
+ u64 budget = AMDGPU_COREDUMP_NOWAIT_MAX_BYTES;
+ struct amdgpu_vm *vm;
+
+ vm = amdgpu_vm_trylock_by_pasid(adev, coredump->pasid);
+ if (IS_ERR(vm)) {
+ for (int i = 0; i < coredump->num_ibs; i++) {
+ struct amdgpu_coredump_blob *blob = &coredump->ibs[i].blob;
+
+ if (blob->status == -EAGAIN)
+ blob->status = PTR_ERR(vm);
+ }
+ return;
+ }
+
+ for (int i = 0; i < coredump->num_ibs; i++)
+ amdgpu_devcoredump_copy_blob_nowait(vm, &coredump->ibs[i].blob,
+ &budget);
+
+ dma_resv_unlock(vm->root.bo->tbo.base.resv);
+}
+
+/*
+ * 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.
+ */
+static void
+amdgpu_devcoredump_capture_vm_late(struct amdgpu_coredump_info *coredump)
{
struct amdgpu_bo_va_mapping *mapping;
+ struct amdgpu_coredump_blob *blob;
struct amdgpu_bo *abo;
struct drm_exec exec;
struct amdgpu_vm *vm;
+ bool retry = false;
int r = 0;
+ for (int i = 0; i < coredump->num_ibs; i++)
+ retry |= amdgpu_devcoredump_blob_retry(&coredump->ibs[i].blob);
+ if (!retry)
+ return;
+
/*
* Lock the VM root PD and every IB BO together in a single drm_exec
* ticket. Reserving the IB BOs one by one while the root PD is held
@@ -394,8 +502,11 @@ static void amdgpu_devcoredump_capture_vm(struct amdgpu_coredump_info *coredump)
}
for (int i = 0; i < coredump->num_ibs; i++) {
- mapping = amdgpu_vm_bo_lookup_mapping(vm,
- coredump->ibs[i].blob.va);
+ blob = &coredump->ibs[i].blob;
+ if (!amdgpu_devcoredump_blob_retry(blob))
+ continue;
+
+ mapping = amdgpu_vm_bo_lookup_mapping(vm, blob->va);
abo = amdgpu_devcoredump_mapping_bo(mapping);
if (!abo)
continue;
@@ -408,17 +519,23 @@ static void amdgpu_devcoredump_capture_vm(struct amdgpu_coredump_info *coredump)
}
for (int i = 0; i < coredump->num_ibs; i++) {
- struct amdgpu_coredump_blob *blob = &coredump->ibs[i].blob;
+ blob = &coredump->ibs[i].blob;
+ if (!amdgpu_devcoredump_blob_retry(blob))
+ continue;
- blob->status = amdgpu_devcoredump_copy_blob(vm, blob);
+ blob->status = amdgpu_devcoredump_copy_blob(vm, blob, false);
+ blob->late = !blob->status;
}
unlock:
drm_exec_fini(&exec);
if (r) {
- for (int i = 0; i < coredump->num_ibs; i++)
- coredump->ibs[i].blob.status = r;
+ for (int i = 0; i < coredump->num_ibs; i++) {
+ blob = &coredump->ibs[i].blob;
+ if (amdgpu_devcoredump_blob_retry(blob))
+ blob->status = r;
+ }
}
}
@@ -434,6 +551,9 @@ static void amdgpu_devcoredump_print_blob(struct drm_printer *p,
return;
}
+ if (blob->late)
+ drm_printf(p, "contents captured after recovery started\n");
+
for (u64 i = 0; i < blob->size / 4; i++)
drm_printf(p, "0x%08x\n", dw[i]);
}
@@ -651,7 +771,7 @@ static void amdgpu_devcoredump_deferred_work(struct work_struct *work)
goto end;
if (coredump->num_ibs)
- amdgpu_devcoredump_capture_vm(coredump);
+ amdgpu_devcoredump_capture_vm_late(coredump);
/* Do a one-time preparation of the coredump output because
* repeatingly calling drm_coredump_printer is very slow.
@@ -765,6 +885,9 @@ void amdgpu_coredump(struct amdgpu_device *adev, bool skip_vram_check,
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;
+ ib->blob.status =
+ ib->blob.size > AMDGPU_COREDUMP_IB_MAX_BYTES ?
+ -EFBIG : -EAGAIN;
}
}
@@ -812,6 +935,9 @@ void amdgpu_coredump(struct amdgpu_device *adev, bool skip_vram_check,
coredump->num_rings = idx;
}
+ if (coredump->num_ibs)
+ amdgpu_devcoredump_capture_vm_nowait(adev, coredump);
+
coredump->adev = adev;
ktime_get_ts64(&coredump->reset_time);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
index ffcb08428631..d62031369e56 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
@@ -46,6 +46,8 @@ struct amdgpu_coredump_blob {
/* contents, or NULL with the reason in status */
void *data;
int status;
+ /* copied by the worker rather than when the hang was detected */
+ bool late;
};
struct amdgpu_coredump_ib_info {
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index a627a3092990..b03f3ee4d78c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -3106,6 +3106,41 @@ struct amdgpu_vm *amdgpu_vm_lock_by_pasid(struct amdgpu_device *adev,
return vm;
}
+/**
+ * amdgpu_vm_trylock_by_pasid - look up a VM by PASID and trylock its root PD
+ * @adev: amdgpu device pointer
+ * @pasid: PASID of the VM
+ *
+ * Like amdgpu_vm_lock_by_pasid(), but never sleeps, so it can be used from
+ * the job timeout and GPU reset paths. The root PD is trylocked under the
+ * PASID lock, and holding its reservation keeps the VM alive without a
+ * reference: amdgpu_driver_postclose_kms() clears the PASID's owner before
+ * amdgpu_vm_fini() reserves the root PD to free it. On success the caller
+ * must unlock vm->root.bo with dma_resv_unlock().
+ *
+ * Return: the VM on success, ERR_PTR(-ESRCH) if the PASID has no VM, or
+ * ERR_PTR(-EBUSY) if the root PD is contended.
+ */
+struct amdgpu_vm *amdgpu_vm_trylock_by_pasid(struct amdgpu_device *adev,
+ u32 pasid)
+{
+ struct amdgpu_vm *vm = ERR_PTR(-ESRCH);
+ struct amdgpu_fpriv *fpriv;
+ unsigned long irqflags;
+
+ amdgpu_pasid_lock(&irqflags);
+ fpriv = amdgpu_pasid_get_fpriv_locked(pasid);
+ if (fpriv && fpriv->vm.root.bo) {
+ if (dma_resv_trylock(fpriv->vm.root.bo->tbo.base.resv))
+ vm = &fpriv->vm;
+ else
+ vm = ERR_PTR(-EBUSY);
+ }
+ amdgpu_pasid_unlock(irqflags);
+
+ return vm;
+}
+
/**
* amdgpu_vm_handle_fault - graceful handling of VM faults.
* @adev: amdgpu device pointer
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index f6285a9d9e98..10fd477f7765 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -529,6 +529,8 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
struct amdgpu_vm *amdgpu_vm_lock_by_pasid(struct amdgpu_device *adev,
u32 pasid, struct drm_exec *exec);
+struct amdgpu_vm *amdgpu_vm_trylock_by_pasid(struct amdgpu_device *adev,
+ u32 pasid);
void amdgpu_vm_set_task_info(struct amdgpu_vm *vm);
--
2.56.1
next prev parent reply other threads:[~2026-12-02 22:36 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 22:36 [PATCH 00/14] drm/amdgpu: capture more of the hang state in the devcoredump Matt Turner
2026-12-02 22:36 ` [PATCH 01/14] drm/amdgpu: dump the IP state of each device in an XGMI hive Matt Turner
2026-12-02 22:36 ` [PATCH 02/14] drm/amdgpu: only give the hung job to its own device's coredump Matt Turner
2026-12-02 22:36 ` [PATCH 03/14] drm/amdgpu: don't overwrite the IP state while a coredump is formatted Matt Turner
2026-12-02 22:36 ` [PATCH 04/14] drm/amdgpu: take a coredump before resets done by a reset handler Matt Turner
2026-12-02 22:36 ` [PATCH 05/14] drm/amdgpu: copy the VM fault info when the hang is detected Matt Turner
2026-12-02 22:36 ` [PATCH 06/14] drm/amdgpu/gfx10-12: dump CP_STAT and the IB2 state of each gfx queue Matt Turner
2026-12-02 22:36 ` [PATCH 07/14] drm/amdgpu: copy the IB contents before formatting the devcoredump Matt Turner
2026-12-02 22:36 ` Matt Turner [this message]
2026-12-02 22:36 ` [PATCH 09/14] drm/amdgpu: record the submitting thread of the hung job in the coredump Matt Turner
2026-12-02 22:36 ` [PATCH 10/14] drm/amdgpu: dump the unsignaled fences of each ring " Matt Turner
2026-12-02 22:36 ` [PATCH 11/14] drm/amdgpu: report dropped devcoredumps Matt Turner
2026-12-02 22:36 ` [PATCH 12/14] drm/amdgpu: list the VM mappings of the hung job in the coredump Matt Turner
2026-12-02 22:36 ` [PATCH 13/14] drm/amdgpu: add AMDGPU_VM_PAGE_DUMP to include BOs " Matt Turner
2026-12-02 22:36 ` [PATCH 14/14] drm/amdgpu: bump the devcoredump version Matt Turner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202-amdgpu-devcoredump-v1-8-3dfa0e033b97@gmail.com \
--to=mattst88@gmail.com \
--cc=Trigger.Huang@amd.com \
--cc=airlied@gmail.com \
--cc=alexander.deucher@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gpean@netflix.com \
--cc=linux-kernel@vger.kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=simona@ffwll.ch \
--cc=sunil.khatri@amd.com \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.