On 2026-11-18 10:10, Xiaogang.Chen wrote:
From: Xiaogang Chen<[email protected]>
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.
svm_range_dma_unmap_dev() sets each entry to 0 after unmapping it,
so the caller's later svm_range_dma_unmap_dev call skips them.
Not drop the fence there. *mfence still belongs to the caller. Later
svm_migrate_copy_done() puts it and second wait there returns immediately.
Signed-off-by: Xiaogang Chen<[email protected]>
---
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
index 8b96e1fe741a..78a2aab81c17 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c
@@ -716,6 +716,18 @@ 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. The
+ * caller's svm_migrate_copy_done() still puts the fence, and
+ * its svm_range_dma_unmap_dev() skips the entries zeroed here.
+ */
+ if (*mfence)
+ dma_fence_wait(*mfence, false);
wait fence is good, but I perfer to add dma_fence_put() here to free the
fence, then in caller, only wait for fence if copy return 0.
if (!r)
svm_migrate_copy_done(adev, mfence);
+ svm_range_dma_unmap_dev(dev, dst, 0, npages);
+
This is not needed, as the below loop unmap dma address and free sys pages.
And add this in caller
if (!r)
svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
Regards,
Philip
/* release previous allocated sys pages and unmap dma address */
while (i--) {