The Linux Kernel Mailing List
 help / color / mirror / Atom feed
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 10/14] drm/amdgpu: dump the unsignaled fences of each ring in the coredump
Date: Wed, 02 Dec 2026 17:36:16 -0500	[thread overview]
Message-ID: <20261202-amdgpu-devcoredump-v1-10-3dfa0e033b97@gmail.com> (raw)
In-Reply-To: <20261202-amdgpu-devcoredump-v1-0-3dfa0e033b97@gmail.com>

The coredump has the ring contents and read and write pointers, but
nothing to tell which part of the ring belongs to which submission, or
which submissions were queued behind the hung one.

For each ring in the dump, record the last signaled and last emitted
sequence numbers and, for every unsignaled fence, its fence context,
the ring position and size of its commands as recorded for queue
reset, and how long ago it was emitted. This is captured along with
the ring contents, reading the fences under the fence driver lock into
a GFP_NOWAIT allocation.

Emitting a fence reuses its slot without the fence driver lock, and
polled fences take a seq without a slot, so a slot whose fence has a
different seqno is skipped. amdgpu_ib_schedule() sets the size of a job's
commands after emitting its fence, so mark that store and the read
with WRITE_ONCE() and READ_ONCE().

Suggested-by: Gregoire Pean <gpean@netflix.com>
Signed-off-by: Matt Turner <mattst88@gmail.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c | 84 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h | 15 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c           |  9 ++-
 3 files changed, 104 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
index 62a0ae0aff87..3cb1927b0f47 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.c
@@ -571,6 +571,84 @@ amdgpu_devcoredump_print_ibs(struct drm_printer *p,
 	}
 }
 
+/*
+ * Record the unsignaled fences of @ring: for each, where the job was written
+ * to the ring and how long ago it was emitted. Together with the ring
+ * contents this shows which submissions were queued behind the hung one.
+ */
+static void amdgpu_devcoredump_capture_fences(struct amdgpu_ring *ring,
+					      struct amdgpu_coredump_ring *cdump)
+{
+	struct amdgpu_fence_driver *drv = &ring->fence_drv;
+	unsigned long flags;
+	u32 count, seq;
+
+	cdump->last_seq = atomic_read(&drv->last_seq);
+	cdump->sync_seq = READ_ONCE(drv->sync_seq);
+
+	count = min(cdump->sync_seq - cdump->last_seq, drv->num_fences_mask + 1);
+	if (!count)
+		return;
+
+	cdump->fences = kvzalloc_objs(struct amdgpu_coredump_fence, count,
+				      GFP_NOWAIT | __GFP_NOWARN);
+	if (!cdump->fences)
+		return;
+
+	spin_lock_irqsave(&drv->lock, flags);
+	rcu_read_lock();
+	for (seq = cdump->sync_seq - count + 1; count--; seq++) {
+		struct amdgpu_coredump_fence *cf;
+		struct dma_fence *f;
+		struct amdgpu_fence *af;
+
+		/*
+		 * drv->lock doesn't keep the slots from being reused, and
+		 * polled fences take a seq without a slot, so the slot may
+		 * hold an older or newer fence.
+		 */
+		f = rcu_dereference(drv->fences[seq & drv->num_fences_mask]);
+		if (!f || f->seqno != seq || dma_fence_test_signaled_flag(f))
+			continue;
+
+		af = container_of(f, struct amdgpu_fence, base);
+		cf = &cdump->fences[cdump->num_fences++];
+		cf->seq = seq;
+		cf->context = af->context;
+		cf->ib_wptr = af->ib_wptr;
+		/* 0 while the job is still being written to the ring */
+		cf->ib_dw_size = READ_ONCE(af->ib_dw_size);
+		cf->skip_ib_dw_start = af->skip_ib_dw_start_offset;
+		cf->skip_ib_dw_end = af->skip_ib_dw_end_offset;
+		/* Set before the slot, so it is not after this ktime_get(). */
+		cf->age_us = ktime_us_delta(ktime_get(), af->start_timestamp);
+	}
+	rcu_read_unlock();
+	spin_unlock_irqrestore(&drv->lock, flags);
+}
+
+static void amdgpu_devcoredump_print_fences(struct drm_printer *p,
+					    struct amdgpu_coredump_ring *cdump)
+{
+	drm_printf(p, "Signaled seq: 0x%08x Emitted seq: 0x%08x\n",
+		   cdump->last_seq, cdump->sync_seq);
+
+	if (cdump->last_seq != cdump->sync_seq && !cdump->fences) {
+		drm_printf(p, "Unsignaled fences unavailable\n");
+		return;
+	}
+
+	for (u32 i = 0; i < cdump->num_fences; i++) {
+		struct amdgpu_coredump_fence *cf = &cdump->fences[i];
+
+		drm_printf(p,
+			   "fence seq: 0x%08x context: %llu wptr: 0x%llx dw: %u skip: %u-%u age: %lld us\n",
+			   cf->seq, cf->context, cf->ib_wptr, cf->ib_dw_size,
+			   cf->skip_ib_dw_start, cf->skip_ib_dw_end,
+			   cf->age_us);
+	}
+}
+
 static ssize_t
 amdgpu_devcoredump_format(char *buffer, size_t count, struct amdgpu_coredump_info *coredump)
 {
@@ -701,6 +779,7 @@ amdgpu_devcoredump_format(char *buffer, size_t count, struct amdgpu_coredump_inf
 				   ring->buf_mask);
 			drm_printf(&p, "Ring size in dwords: %d\n",
 				ring->ring_size / 4);
+			amdgpu_devcoredump_print_fences(&p, &coredump->rings[i]);
 
 			if (!coredump->rings[i].ring_dw) {
 				drm_printf(&p, "Ring contents unavailable\n");
@@ -766,8 +845,10 @@ static void amdgpu_devcoredump_free(void *data)
 
 	kvfree(coredump->formatted);
 	amdgpu_devcoredump_free_blobs(coredump);
-	for (i = 0; i < coredump->num_rings; i++)
+	for (i = 0; i < coredump->num_rings; i++) {
 		kvfree(coredump->rings[i].ring_dw);
+		kvfree(coredump->rings[i].fences);
+	}
 	kvfree(coredump->rings);
 	kvfree(data);
 }
@@ -946,6 +1027,7 @@ void amdgpu_coredump(struct amdgpu_device *adev, bool skip_vram_check,
 			cdump_ring->ring_index = ring->idx;
 			cdump_ring->rptr = amdgpu_ring_get_rptr(ring);
 			cdump_ring->wptr = amdgpu_ring_get_wptr(ring);
+			amdgpu_devcoredump_capture_fences(ring, cdump_ring);
 			idx++;
 		}
 		coredump->num_rings = idx;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
index c0192d0c583a..6a51fbe357dc 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dev_coredump.h
@@ -31,11 +31,26 @@
 
 #define AMDGPU_COREDUMP_VERSION "1"
 
+struct amdgpu_coredump_fence {
+	u64				context;
+	u64				ib_wptr;
+	s64				age_us;
+	u32				seq;
+	u32				ib_dw_size;
+	u32				skip_ib_dw_start;
+	u32				skip_ib_dw_end;
+};
+
 struct amdgpu_coredump_ring {
 	u64				rptr;
 	u64				wptr;
 	u32				*ring_dw;
 	u32				ring_index;
+	u32				last_seq;
+	u32				sync_seq;
+	/* unsignaled fences, oldest first */
+	struct amdgpu_coredump_fence	*fences;
+	u32				num_fences;
 };
 
 /* A range of GPU memory copied into the coredump */
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c
index ae028eeacaed..5d16d6f5f9da 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c
@@ -199,8 +199,10 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, struct amdgpu_job *job,
 	/* VM sequence */
 	vm_af->ib_wptr = ring->wptr;
 	amdgpu_vm_flush(ring, job, &need_pipe_sync, &emit_gds_needed);
-	vm_af->ib_dw_size =
-		amdgpu_ring_get_dw_distance(ring, vm_af->ib_wptr, ring->wptr);
+	/* The devcoredump reads it locklessly once the fence is emitted. */
+	WRITE_ONCE(vm_af->ib_dw_size,
+		   amdgpu_ring_get_dw_distance(ring, vm_af->ib_wptr,
+					       ring->wptr));
 
 	/* IB sequence */
 	af->ib_wptr = ring->wptr;
@@ -318,7 +320,8 @@ int amdgpu_ib_schedule(struct amdgpu_ring *ring, struct amdgpu_job *job,
 
 	amdgpu_ring_ib_end(ring);
 
-	af->ib_dw_size = amdgpu_ring_get_dw_distance(ring, af->ib_wptr, ring->wptr);
+	WRITE_ONCE(af->ib_dw_size,
+		   amdgpu_ring_get_dw_distance(ring, af->ib_wptr, ring->wptr));
 
 	amdgpu_ring_commit(ring);
 

-- 
2.56.1


  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 ` [PATCH 08/14] drm/amdgpu: copy the IB contents when the hang is detected Matt Turner
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 ` Matt Turner [this message]
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-10-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox