AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [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

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