drm/amdkfd: don't check the HMM range when getting its pages failed
Subsystems (3)
PATCH 1/2 drm/amdkfd: don't check the HMM range when getting its pages failed
When allocating the PFN array fails, amdgpu_hmm_range_get_pages() returns -ENOMEM before setting the notifier of the range, but svm_range_validate_and_map() still checks the range, as it is not NULL, before looking at the error. On navi48, setting SVM attributes with failslab:
KASAN: null-ptr-deref in range [0x0000000000000050-0x0000000000000057] RIP: 0010:amdgpu_hmm_range_valid+0x66/0xa0 [amdgpu] svm_range_validate_and_map+0xaef/0x4e40 [amdgpu] svm_range_set_attr+0x193e/0x4610 [amdgpu] kfd_ioctl_svm+0x137/0x320 [amdgpu]
Check the error first; it is only 0 when the pages were taken.
--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +5 -4
@@ -1895,11 +1895,12 @@ static int svm_range_validate_and_map(struct mm_struct *mm,svm_range_lock (prange); -/* Free backing memory of hmm_range if it was initialized -* Override return value to TRY AGAIN only if prior returns -* were successful +/* +* Retry if the pages were invalidated after they were taken. +* Only check that when taking them succeeded: a range can exist +* without a notifier, e.g. when allocating its PFN array failed. */ -if (range && !amdgpu_hmm_range_valid (range) && !r) { +if (!r && !amdgpu_hmm_range_valid (range)) {pr_debug ("hmm update the range, need validate again\n" ); r = -EAGAIN ; }
Signature · 1 line
2.50.1
PATCH 2/2 drm/amdkfd: keep the DMA addresses of a range intact if splitting fails
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.
--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +20 -3
@@ -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) {/*
Signature · 1 line
2.50.1
Re: [PATCH 2/2] drm/amdkfd: keep the DMA addresses of a range intact if splitting fails
Quoted 25 lines · Jesse Zhang
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)
Quoted 6 lines
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]>
The series is
Quoted 56 lines
--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_svm.c +20
-3@@ -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) {/*