svm_range_split_pages() splits the DMA address array of one GPU after
the other, and each split replaces the old range's array by a shorter
one. If a later GPU's array can't be allocated, the old range is left
with the arrays of the earlier GPUs already shortened, but with its old
size. Unmapping it then walks past their end.

On navi31 with its iGPU, an SVM range accessible by both GPUs, and
unmapping a page in its middle with allocations failing in turn through
failslab:

  BUG: KASAN: slab-out-of-bounds in svm_range_dma_unmap_dev+0x13f/0x170 [amdgpu]
  Read of size 8 at addr ffff88819ac32800 by task kworker/8:1/171
   svm_range_dma_unmap_dev+0x13f/0x170 [amdgpu]
   svm_range_dma_unmap+0x119/0x2c0 [amdgpu]
   svm_range_free+0x312/0x520 [amdgpu]
   svm_range_handle_list_op+0x326/0x500 [amdgpu]
   svm_range_deferred_list_work+0x338/0x790 [amdgpu]
  Allocated by task 2749:
   svm_range_copy_array.constprop.0+0x55/0x190 [amdgpu]
   svm_range_split+0x388/0x17b0 [amdgpu]
   svm_range_unmap_split+0x254/0x370 [amdgpu]
   svm_range_cpu_invalidate_pagetables+0xb29/0x11d0 [amdgpu]
  The buggy address is located 0 bytes to the right of
   allocated 2048-byte region [ffff88819ac32000, ffff88819ac32800)

Split the arrays of all GPUs first and only replace the old ones once
all succeeded. With that the 2759 unmaps of the same sweep give no
report.

Fixes: f80fe9d3c114 ("drm/amdkfd: map svm range to GPUs")
Signed-off-by: Jesse Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 0879e37faa71..3dbf8002da76 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -1008,7 +1008,7 @@ svm_range_split_array(void *ppnew, void *ppold, size_t 
size,
                kvfree(new);
                return -ENOMEM;
        }
-       kvfree(pold);
+       /* The caller frees pold once all arrays of the range are split */
        *(void **)ppold = old;
        *(void **)ppnew = new;
 
@@ -1019,19 +1019,36 @@ static int
 svm_range_split_pages(struct svm_range *new, struct svm_range *old,
                      uint64_t start, uint64_t last)
 {
+       dma_addr_t *old_dma[MAX_GPU_INSTANCE];
        uint64_t npages = last - start + 1;
        bool has_dma_addr = false;
        int i, r;
 
+       /*
+        * Split the arrays of all GPUs before replacing any of them, the old
+        * range must stay intact if one fails.
+        */
        for (i = 0; i < MAX_GPU_INSTANCE; i++) {
+               old_dma[i] = old->dma_addr[i];
                if (old->dma_addr[i])
                        has_dma_addr = true;
-               r = svm_range_split_array(&new->dma_addr[i], &old->dma_addr[i],
+               r = svm_range_split_array(&new->dma_addr[i], &old_dma[i],
                                          sizeof(*old->dma_addr[i]), old->start,
                                          npages, new->start, new->npages,
                                          old->actual_loc ? &new->vram_pages : 
NULL);
-               if (r)
+               if (r) {
+                       /* new->dma_addr[] goes with the new range */
+                       while (i--)
+                               if (old_dma[i] != old->dma_addr[i])
+                                       kvfree(old_dma[i]);
                        return r;
+               }
+       }
+       for (i = 0; i < MAX_GPU_INSTANCE; i++) {
+               if (old_dma[i] == old->dma_addr[i])
+                       continue;
+               kvfree(old->dma_addr[i]);
+               old->dma_addr[i] = old_dma[i];
        }
        if (old->actual_loc) {
                /*
-- 
2.50.1

Reply via email to