Skip to contentSkip to conversation

drm/amdkfd: rebuild CWSR trap buffers after hibernation

PATCH New Heng Zhou 2 participants 2 messages Nov 23

Subsystems (3)
lore
Share & tools
lore

Heng Zhou

author
#0 lore raw

PATCH 1/1 drm/amdkfd: rebuild CWSR trap buffers after hibernation

The CWSR trap buffer can reside in pinned VRAM, which is skipped by TTM eviction during hibernation. Restoring the process GPUVM mappings does not restore the buffer contents. This can lead to SQ errors and failed queue preemption after resume.

Keep the second-level TBA/TMA and trap flags in system memory. Rebuild the buffer from the trap ISA and saved TMA values and flush HDP before restoring process queues.

---
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h    |  7 ++++++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 31 ++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +7 -0
index d8631847f0eb..a719c3be0db1 100644
@@ -723,6 +723,13 @@ struct qcm_process_device {
 	uint64_t cwsr_base;
 	uint64_t tba_addr;
 	uint64_t tma_addr;
+	/*
+	 * CPU copies of the second-level TBA/TMA and trap flags.
+	 * Keep them in sync with the first-level TMA for hibernation restore.
+	 */
+	uint64_t cwsr_second_level_tba;
+	uint64_t cwsr_second_level_tma;
+	uint64_t cwsr_tma_flags;
 
 	/* IB memory */
 	struct kgd_mem *ib_mem;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +31 -0
index dd938788bdd9..307851d3d882 100644
@@ -1590,6 +1590,8 @@ void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
 		/* KFD trap handler is bound, record as second-level TBA/TMA
 		 * in first-level TMA. First-level trap will jump to second.
 		 */
+		qpd->cwsr_second_level_tba = tba_addr;
+		qpd->cwsr_second_level_tma = tma_addr;
 		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET,
 			     uint64_t, tba_addr);
 		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET + sizeof(uint64_t),
@@ -1660,12 +1662,40 @@ void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
 				     bool enabled)
 {
 	if (!iosys_map_is_null(&qpd->cwsr_map)) {
+		qpd->cwsr_tma_flags = enabled;
 		iosys_map_wr(&qpd->cwsr_map,
 			     KFD_CWSR_TMA_OFFSET + 2 * sizeof(uint64_t),
 			     uint64_t, enabled);
 	}
 }
 
+static void kfd_process_rebuild_cwsr(struct kfd_process *p)
+{
+	struct kfd_process_device *pdd;
+	struct qcm_process_device *qpd;
+	int i;
+
+	for (i = 0; i < p->n_pdds; i++) {
+		pdd = p->pdds[i];
+		qpd = &pdd->qpd;
+
+		if (!pdd->dev->adev->in_s4 || !qpd->cwsr_base ||
+		    iosys_map_is_null(&qpd->cwsr_map))
+			continue;
+
+		iosys_map_memset(&qpd->cwsr_map, 0, 0, KFD_CWSR_TBA_TMA_SIZE);
+		iosys_map_memcpy_to(&qpd->cwsr_map, 0, pdd->dev->kfd->cwsr_isa,
+				    pdd->dev->kfd->cwsr_isa_size);
+		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET,
+			     uint64_t, qpd->cwsr_second_level_tba);
+		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET + sizeof(uint64_t),
+			     uint64_t, qpd->cwsr_second_level_tma);
+		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET + 2 * sizeof(uint64_t),
+			     uint64_t, qpd->cwsr_tma_flags);
+		amdgpu_device_flush_hdp(pdd->dev->adev, NULL);
+	}
+}
+
 /*
  * On return the kfd_process is fully operational and will be freed when the
  * mm is released
@@ -2301,6 +2331,7 @@ int kfd_resume_all_processes(void)
 	int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
 
 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+		kfd_process_rebuild_cwsr(p);
 		if (restore_process_helper(p)) {
 			pr_err("Restore process pid %d failed during resume\n",
 			      p->lead_thread->pid);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/amdkfd: kfd_process_rebuild_cwsr() skips S3 resume due to in_s4 check - [Low] drm/amdkfd…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/amdkfd: kfd_process_rebuild_cwsr() skips S3 resume due to in_s4 check
- [Low] drm/amdkfd: missing Fixes trailer for commit 5088a1ba6d6d --

commit cee0c9930133433e39014ce7869f89cb97cc145b
Author: Heng Zhou <[email protected]>

Quoted 7 lines

drm/amdkfd: rebuild CWSR trap buffers after hibernation

The CWSR trap buffer can reside in pinned VRAM, which is skipped by TTM eviction during hibernation. Restoring the process GPUVM mappings does not restore the buffer contents. This can lead to SQ errors and failed queue preemption after resume.

Quoted 5 lines

Keep the second-level TBA/TMA and trap flags in system memory. Rebuild the buffer from the trap ISA and saved TMA values and flush HDP before restoring process queues.

[Severity: Low]
This fixes the regression introduced when CWSR trap buffers were moved to pinned VRAM in 5088a1ba6d6d ("drm/amdkfd: move TBA/TMA from system to device memory"). Should the commit message include:

Fixes 5088a1ba6d6d ("drm/amdkfd: move TBA/TMA from system to device memory")
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
index 546721b4d59ac..342ad7178ed69 100644

[ ... ]

Quoted 14 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -0
@@ -1681,12 +1683,40 @@ void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
 				     bool enabled)
 {
 	if (!iosys_map_is_null(&qpd->cwsr_map)) {
+		qpd->cwsr_tma_flags = enabled;
 		iosys_map_wr(&qpd->cwsr_map,
 			     KFD_CWSR_TMA_OFFSET + 2 * sizeof(uint64_t),
 			     uint64_t, enabled);
 	}
 }
 
+static void kfd_process_rebuild_cwsr(struct kfd_process *p)
+{
+	struct kfd_process_device *pdd;

drivers/gpu/drm/amd/amdkfd/kfd_process.c void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd, · line 1705

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+	struct qcm_process_device *qpd;
+	int i;
+
+	for (i = 0; i < p->n_pdds; i++) {
+		pdd = p->pdds[i];
+		qpd = &pdd->qpd;
+
+		if (!pdd->dev->adev->in_s4 || !qpd->cwsr_base ||
+		    iosys_map_is_null(&qpd->cwsr_map))
+			continue;

[Severity: Medium]
Does checking !pdd->dev->adev->in_s4 leave the VRAM CWSR trap buffer uninitialized after an S3 suspend/resume cycle?

All discrete GPUs with GC >= 9.4.2 (including GFX10, GFX11 and GFX12 dGPUs) allocate and pin qpd->cwsr_mem in VRAM. During S3 suspend, amdgpu_device_evict_resources() evicts unpinned VRAM BOs while skipping the pinned qpd->cwsr_mem BO, and VRAM loses its contents while the dGPU is powered down. Because adev->in_s3 is set instead of adev->in_s4, kfd_process_rebuild_cwsr() skips the rebuild on S3 resume before restore_process_helper() restores the queues, leading to the same SQ errors and failed queue preemption when CWSR executes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1