* [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path
@ 2026-11-18 22:58 Xiaogang.Chen
2026-11-18 22:58 ` [PATCH v2 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize Xiaogang.Chen
2026-11-18 23:10 ` [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path sashiko-bot
0 siblings, 2 replies; 4+ messages in thread
From: Xiaogang.Chen @ 2026-11-18 22:58 UTC (permalink / raw)
To: amd-gfx; +Cc: Xiaogang Chen
From: Xiaogang Chen <xiaogang.chen@amd.com>
The destination pages were freed while SDMA copies queued earlier
could still be writing into them, and while they were still DMA
mapped. The mappings were only removed later by the caller's
svm_range_dma_unmap_dev().
With swiotlb, the BIDIRECTIONAL unmap copies the bounce buffer back into
a page that may already be reused.
With an IOMM, the GPU still has a valid bus address that points at a page
now maybe owned by another user. Any SDMA write still in flight, corrupts
that page.
So, wait on the fence first, then dma unmap before release buffer.
Signed-off-by: Xiaogang Chen <xiaogang.chen@amd.com>
---
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
index 8b96e1fe741a..9254d64588f0 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
@@ -716,6 +716,17 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,
put_page(dpage);
}
+ /*
+ * Copies queued before the failure may still be writing into
+ * the destination pages. Wait for the last SDMA fence (copies
+ * on the entity complete in order), then remove the device DMA
+ * mappings, before the pages go back to the allocator.
+ */
+ if (*mfence) {
+ dma_fence_wait(*mfence, false);
+ dma_fence_put(*mfence);
+ }
+
/* release previous allocated sys pages and unmap dma address */
while (i--) {
@@ -824,11 +835,12 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,
mpages = svm_migrate_successful_pages(&migrate);
pr_debug("migrated/collected/requested 0x%lx/0x%lx/0x%lx\n",
mpages, cpages, migrate.npages);
-
- svm_migrate_copy_done(adev, mfence);
+ if (!r)
+ svm_migrate_copy_done(adev, mfence);
migrate_vma_finalize(&migrate);
- svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
+ if (!r)
+ svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
out_free:
kvfree(buf);
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize
2026-11-18 22:58 [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path Xiaogang.Chen
@ 2026-11-18 22:58 ` Xiaogang.Chen
2026-11-18 23:29 ` Philip Yang
2026-11-18 23:10 ` [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Xiaogang.Chen @ 2026-11-18 22:58 UTC (permalink / raw)
To: amd-gfx; +Cc: Xiaogang Chen
From: Xiaogang Chen <xiaogang.chen@amd.com>
svm_migrate_vma_to_vram() on success: migrate_vma_finalize() releases the old
system-RAM source pages. Only after that they are DMA-unmapped.
svm_migrate_vma_to_ram() for slots that failed to migrate: finalize frees the
newly allocated dpages, and the unmap comes afterwards.
Every mapping uses DMA_BIDIRECTIONAL. With swiotlb, that kind of unmap copies
the bounce buffer back into the original page, and by wthen the page may belong
to someone else. With an IOMMU, the IOVA stays valid for a short window after
the page is freed. During the window GPU may write into the pages that may
belong to someone else.
So, swap svm_range_dma_unmap_dev and migrate_vma_finalize at both migration
direction to make dma unmap happen before sys ram pages got released.
Signed-off-by: Xiaogang Chen <xiaogang.chen@amd.com>
---
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
index 9254d64588f0..6f1e2a68b3c1 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
@@ -494,14 +494,13 @@ svm_migrate_vma_to_vram(struct kfd_node *node, struct svm_range *prange,
migrate_vma_pages(&migrate);
svm_migrate_copy_done(adev, mfence);
+ svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
migrate_vma_finalize(&migrate);
mpages = svm_migrate_successful_pages(&migrate);
pr_debug("migrated/collected/requested 0x%lx/0x%lx/0x%lx\n",
mpages, cpages, migrate.npages);
- svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
-
out_free:
kvfree(buf);
kfd_smi_event_migration_end(node, p->lead_thread,
@@ -835,12 +834,12 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,
mpages = svm_migrate_successful_pages(&migrate);
pr_debug("migrated/collected/requested 0x%lx/0x%lx/0x%lx\n",
mpages, cpages, migrate.npages);
- if (!r)
- svm_migrate_copy_done(adev, mfence);
- migrate_vma_finalize(&migrate);
- if (!r)
+ if (!r) {
+ svm_migrate_copy_done(adev, mfence);
svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
+ }
+ migrate_vma_finalize(&migrate);
out_free:
kvfree(buf);
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path
2026-11-18 22:58 [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path Xiaogang.Chen
2026-11-18 22:58 ` [PATCH v2 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize Xiaogang.Chen
@ 2026-11-18 23:10 ` sashiko-bot
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-18 23:10 UTC (permalink / raw)
To: Xiaogang.Chen, amd-gfx
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] drm/amdkfd: missing Fixes trailer for svm_migrate_copy_to_ram()
fix
--
commit 36078b6a47909e79e845fa9d13dc4df2f88836b7
Author: Xiaogang Chen <xiaogang.chen@amd.com>
> drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path
>
> The destination pages were freed while SDMA copies queued earlier
> could still be writing into them, and while they were still DMA
> mapped. The mappings were only removed later by the caller's
> svm_range_dma_unmap_dev().
>
> With swiotlb, the BIDIRECTIONAL unmap copies the bounce buffer back into
> a page that may already be reused.
>
> With an IOMM, the GPU still has a valid bus address that points at a page
> now maybe owned by another user. Any SDMA write still in flight, corrupts
> that page.
>
> So, wait on the fence first, then dma unmap before release buffer.
>
> Signed-off-by: Xiaogang Chen <xiaogang.chen@amd.com>
[Severity: Low]
This fixes a bug on the svm_migrate_copy_to_ram() error path where
destination pages were unmapped and freed before waiting on in-flight SDMA
copies. Should the commit message include Fixes tags for the introducing
commits?
Fixes: 960c4a8069bf ("drm/amdkfd: Fix error path at svm_migrate_copy_to_ram")
Fixes: 48ff079b28d8 ("drm/amdkfd: HMM migrate vram to ram")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118225834.60743-1-xiaogang.chen@amd.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize
2026-11-18 22:58 ` [PATCH v2 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize Xiaogang.Chen
@ 2026-11-18 23:29 ` Philip Yang
0 siblings, 0 replies; 4+ messages in thread
From: Philip Yang @ 2026-11-18 23:29 UTC (permalink / raw)
To: Xiaogang.Chen, amd-gfx
[-- Attachment #1: Type: text/plain, Size: 2602 bytes --]
On 2026-11-18 17:58, Xiaogang.Chen wrote:
> From: Xiaogang Chen<xiaogang.chen@amd.com>
>
> svm_migrate_vma_to_vram() on success: migrate_vma_finalize() releases the old
> system-RAM source pages. Only after that they are DMA-unmapped.
>
> svm_migrate_vma_to_ram() for slots that failed to migrate: finalize frees the
> newly allocated dpages, and the unmap comes afterwards.
>
> Every mapping uses DMA_BIDIRECTIONAL. With swiotlb, that kind of unmap copies
> the bounce buffer back into the original page, and by wthen the page may belong
> to someone else. With an IOMMU, the IOVA stays valid for a short window after
> the page is freed. During the window GPU may write into the pages that may
> belong to someone else.
>
> So, swap svm_range_dma_unmap_dev and migrate_vma_finalize at both migration
> direction to make dma unmap happen before sys ram pages got released.
>
> Signed-off-by: Xiaogang Chen<xiaogang.chen@amd.com>
> ---
> drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> index 9254d64588f0..6f1e2a68b3c1 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
> @@ -494,14 +494,13 @@ svm_migrate_vma_to_vram(struct kfd_node *node, struct svm_range *prange,
> migrate_vma_pages(&migrate);
>
> svm_migrate_copy_done(adev, mfence);
> + svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
> migrate_vma_finalize(&migrate);
>
> mpages = svm_migrate_successful_pages(&migrate);
> pr_debug("migrated/collected/requested 0x%lx/0x%lx/0x%lx\n",
> mpages, cpages, migrate.npages);
>
> - svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
> -
> out_free:
> kvfree(buf);
> kfd_smi_event_migration_end(node, p->lead_thread,
> @@ -835,12 +834,12 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,
> mpages = svm_migrate_successful_pages(&migrate);
> pr_debug("migrated/collected/requested 0x%lx/0x%lx/0x%lx\n",
> mpages, cpages, migrate.npages);
> - if (!r)
> - svm_migrate_copy_done(adev, mfence);
> - migrate_vma_finalize(&migrate);
>
> - if (!r)
> + if (!r) {
> + svm_migrate_copy_done(adev, mfence);
> svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
> + }
> + migrate_vma_finalize(&migrate);
You could swap patches 1 and 2 to avoid modifying the same code twice.
Either way, the patch series is:
Reviewed-by: Philip Yang <philip.yang@amd.com>
>
> out_free:
> kvfree(buf);
[-- Attachment #2: Type: text/html, Size: 3337 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-18 23:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 22:58 [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path Xiaogang.Chen
2026-11-18 22:58 ` [PATCH v2 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize Xiaogang.Chen
2026-11-18 23:29 ` Philip Yang
2026-11-18 23:10 ` [PATCH v2 1/2] drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox