* [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation
@ 2026-11-23 9:56 Heng Zhou
2026-11-23 10:21 ` sashiko-bot
2026-12-01 7:44 ` Heng Zhou
0 siblings, 2 replies; 4+ messages in thread
From: Heng Zhou @ 2026-11-23 9:56 UTC (permalink / raw)
To: amd-gfx
Cc: Lijo.Lazar, Christian.Koenig, Emily.Deng, Victor.Zhao,
Felix.Kuehling, phasta, HaiJun.Chang, Heng.Zhou, James.Zhu
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.
Signed-off-by: Heng Zhou <Heng.Zhou@amd.com>
---
drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 7 ++++++
drivers/gpu/drm/amd/amdkfd/kfd_process.c | 31 ++++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index d8631847f0eb..a719c3be0db1 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -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;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index dd938788bdd9..307851d3d882 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -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);
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation
2026-11-23 9:56 [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation Heng Zhou
@ 2026-11-23 10:21 ` sashiko-bot
2026-12-01 7:44 ` Heng Zhou
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-23 10:21 UTC (permalink / raw)
To: Heng Zhou, amd-gfx
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 <Heng.Zhou@amd.com>
> 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.
>
> Signed-off-by: Heng Zhou <Heng.Zhou@amd.com>
[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")
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
> index 546721b4d59ac..342ad7178ed69 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
[ ... ]
> @@ -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;
> + 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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123095605.162656-1-Heng.Zhou@amd.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation
2026-11-23 9:56 [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation Heng Zhou
2026-11-23 10:21 ` sashiko-bot
@ 2026-12-01 7:44 ` Heng Zhou
2026-12-01 18:56 ` Felix Kuehling
1 sibling, 1 reply; 4+ messages in thread
From: Heng Zhou @ 2026-12-01 7:44 UTC (permalink / raw)
To: amd-gfx
Cc: Lijo.Lazar, Christian.Koenig, Emily.Deng, Victor.Zhao,
Felix.Kuehling, phasta, HaiJun.Chang, Heng.Zhou, James.Zhu,
Tiantian.Zhang
Hi Felix and all,
A quick follow-up on this patch. Could you take a look when you have a
chance? I'd especially appreciate feedback on whether rebuilding the
CWSR trap buffer before restoring the KFD process queues is the right
point in the S4 resume path.
In the S4 test with the patch, all 32 trap buffers matched their
pre-hibernation CRCs and TMA values after the rebuild. The HIP workload
continued without the SQ errors or queue preemption failures seen in
the baseline run.
Thanks,
Heng
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation
2026-12-01 7:44 ` Heng Zhou
@ 2026-12-01 18:56 ` Felix Kuehling
0 siblings, 0 replies; 4+ messages in thread
From: Felix Kuehling @ 2026-12-01 18:56 UTC (permalink / raw)
To: Heng Zhou, amd-gfx
Cc: Lijo.Lazar, Christian.Koenig, Emily.Deng, Victor.Zhao, phasta,
HaiJun.Chang, James.Zhu, Tiantian.Zhang
On 2026-12-01 02:44, Heng Zhou wrote:
> Hi Felix and all,
>
> A quick follow-up on this patch. Could you take a look when you have a
> chance? I'd especially appreciate feedback on whether rebuilding the
> CWSR trap buffer before restoring the KFD process queues is the right
> point in the S4 resume path.
Before resuming the queues is definitely correct.
The patch looks fine to me. I'm not sure if there are other pinned
buffers that may need similar fixes. We just moved MQDs to VRAM on some
MI GPUs. But probably S4 doesn't matter on those GPUs. Also, some DMABuf
exports get pinned when they are imported by a peer device that doesn't
support DMABuf move notifiers. If we want that to work with S4 we
probably need a more general solution for saving and restoring pinned
VRAM BOs.
Regards,
Felix
>
> In the S4 test with the patch, all 32 trap buffers matched their
> pre-hibernation CRCs and TMA values after the rebuild. The HIP workload
> continued without the SQ errors or queue preemption failures seen in
> the baseline run.
>
> Thanks,
> Heng
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 18:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 9:56 [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation Heng Zhou
2026-11-23 10:21 ` sashiko-bot
2026-12-01 7:44 ` Heng Zhou
2026-12-01 18:56 ` Felix Kuehling
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.