drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path
Subsystems (3)
PATCH v2 1/2 drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path
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.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +15 -3
@@ -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);
Signature · 1 line
2.34.1
PATCH v2 2/2 drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize
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.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +5 -6
@@ -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);
Signature · 1 line
2.34.1
Re: [PATCH v2 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize
Quoted 46 lines · Xiaogang.Chen
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_devandmigrate_vma_finalizeat 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(-)drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +1
-2@@ -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);
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange, · line 842
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +4
-4-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:
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-0out_free:kvfree (buf);
[email protected]
AI reviewThank 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 --
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 <[email protected]>
Quoted 10 lines
drm/amdkfd: Fix a race issue at
svm_migrate_copy_to_ram()error pathThe 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.
Quoted 7 lines
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 <[email protected]>
[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?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1