On 2026-11-18 17:58, Xiaogang.Chen wrote:
From: Xiaogang Chen<[email protected]>

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<[email protected]>
---
  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 <[email protected]>
out_free:
        kvfree(buf);

Reply via email to