drm/amdkfd: Fix a race issue at svm_migrate_copy_to_ram() error path
Subsystems (3)
PATCH 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.
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.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +12 -0
@@ -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); +svm_range_dma_unmap_dev (dev, dst,0 , npages); +/* release previous allocated sys pages and unmap dma address */ while (i--) {
Signature · 1 line
2.34.1
PATCH 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 then 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 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +2 -4
@@ -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,
@@ -838,9 +837,8 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange, mpages, cpages, migrate.npages);svm_migrate_copy_done (adev, mfence); -migrate_vma_finalize (&migrate); -svm_range_dma_unmap_dev (adev->dev, scratch,0 , npages); +migrate_vma_finalize (&migrate); out_free:kvfree (buf);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/amdkfd: missing Fixes: trailers for DMA unmap ordering bug fix It also noticed 1 related…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/amdkfd: missing Fixes: trailers for DMA unmap ordering bug fix
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] drm/amdkfd: unhandled SDMA fence error in svm_migrate_copy_done() [1]
--
commit ccdfb551849cc8856e631a5865cff36ff2c4ceb7
Author: Xiaogang Chen <[email protected]>
Quoted 14 lines
drm/amdkfd: Swap order of
svm_range_dma_unmap_devandmigrate_vma_finalize
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 then 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.
Quoted 4 lines
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]>
[Severity: Low]
Should this bug fix include Fixes: tags for the commits that introduced the unmap ordering in both migration directions?
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_vma_to_vram(struct kfd_node *node, struct svm_range *prange, · line 508
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +1
-0@@ -504,14 +504,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);
[Severity: Critical]
This problem wasn't introduced by this patch, but can svm_migrate_vma_to_vram() and svm_migrate_vma_to_ram() commit uninitialized destination pages when the SDMA copy job fails [1]?
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c:svm_migrate_copy_done() {
if (mfence) {
r = dma_fence_wait(mfence, false);
dma_fence_put(mfence);
pr_debug("sdma copy memory fence done\n");
}
}dma_fence_wait() returns 0 whenever the fence signals, even if the SDMA job aborted with mfence->error set (for example during a GPU reset), and neither svm_migrate_copy_done() nor its callers inspect the fence error or return value before migrate_vma_finalize() installs the new page table entries, exposing stale or uninitialized memory to userspace.
[1] https://sashiko.dev/#/bug/linux-bdb6e388-a490-42f9-a491-9e9b540c6920
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize
In reply to Xiaogang.ChenQuoted 43 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 then 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 | 6 ++---- 1 file changed, 2 insertions(+), 4 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,
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange, · line 841
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +1
-2@@ -838,9 +837,8 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange, mpages, cpages, migrate.npages);svm_migrate_copy_done (adev, mfence); -migrate_vma_finalize (&migrate); -svm_range_dma_unmap_dev (adev->dev, scratch,0 , npages); +migrate_vma_finalize (&migrate);
With the suggested change in patch 1, this will be
if (!r) {svm_migrate_copy_done(adev, mfence); svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages); }
migrate_vma_finalize(&migrate);
Regards,
Philip
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-0out_free:kvfree (buf);
Re: [PATCH 2/2] drm/amdkfd: Swap order of svm_range_dma_unmap_dev and migrate_vma_finalize
Quoted 53 lines · Philip Yang
Quoted 41 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 then 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| 6 ++----1 file changed, 2 insertions(+), 4 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +1
-1@@ -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,drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_vma_to_ram(struct kfd_node *node, · line 841
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +1
-2@@ -838,9 +837,8 @@ svm_migrate_vma_to_ram(struct kfd_node *node,struct svm_range *prange, mpages, cpages, migrate.npages); svm_migrate_copy_done(adev, mfence); - migrate_vma_finalize(&migrate); - svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages); + migrate_vma_finalize(&migrate);With the suggested change in patch 1, this will be
Quoted 4 lines
if (!r) {
svm_migrate_copy_done(adev, mfence);svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages); }
It is sys->vram path. There is no dma_wait at error path.
Regards
Xiaogang
Quoted 7 lines
migrate_vma_finalize(&migrate);Regards,
PhilipQuoted 2 lines
out_free:
kvfree(buf);
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] drm/amdkfd: redundant DMA unmap in svm_migrate_copy_to_ram() error path - [Low] drm/amdkfd: missing…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] drm/amdkfd: redundant DMA unmap in svm_migrate_copy_to_ram() error path
- [Low] drm/amdkfd: missing Fixes trailer on svm_migrate_copy_to_ram() fix
--
commit 04e6170d8b5365de5f3b17fda44a17daca9153e9
Author: Xiaogang Chen <[email protected]>
Quoted 16 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.
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.
Quoted 7 lines
svm_range_dma_unmap_dev()sets each entry to 0 after unmapping it, so the caller's latersvm_range_dma_unmap_devcall 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]>
[Severity: Low]
Should this bug fix include Fixes tags for the commits that introduced and updated the error cleanup path in svm_migrate_copy_to_ram()?
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +4
-0@@ -726,6 +726,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
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, · line 742
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +8
-0+* 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); +svm_range_dma_unmap_dev (dev, dst,0 , npages); +/* release previous allocated sys pages and unmap dma address */ while (i--) {
[Severity: Low]
This isn't a bug, but commit 960c4a8069bf ("drm/amdkfd: Fix error path at svm_migrate_copy_to_ram") already added dma_unmap_page() and zeroed dst[i] inside the while (i--) loop right below this:
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c:svm_migrate_copy_to_ram() {
...
while (i--) {
if (dst[i]) {
dma_unmap_page(dev, dst[i], PAGE_SIZE,
DMA_BIDIRECTIONAL);
dst[i] = 0;
}
...
}Because dst is zero-initialized and dst[i] is cleared on dma_map_page() failure before jumping to out_oom, only entries below i hold mappings. Calling svm_range_dma_unmap_dev(dev, dst, 0, npages) beforehand unmaps and zeroes all of dst[], leaving the dma_unmap_page() block in the while (i--) loop dead code, and the commit message's statement that mappings were only removed later by the caller's svm_range_dma_unmap_dev() no longer matches the code.
Wait on *mfence before the while (i--) loop and either drop the new svm_range_dma_unmap_dev() call or remove the duplicate dma_unmap_page() block inside the loop.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 35 lines · Xiaogang.Chen
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 latersvm_range_dma_unmap_devcall 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(+)drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-0@@ -716,6 +716,18 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,put_page (dpage); }
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, · line 728
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +10
-0+/* +* 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);
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange, · line 730
Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +2
-0+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
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-0/* release previous allocated sys pages and unmap dma address */ while (i--) {