Skip to contentSkip to conversation

drm/amdgpu: fix debug sdma vram access checks

PATCH In review Jonathan Kim 3 participants 3 messages Jan 17, 2022 – Jan 18, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 drm/amdgpu: fix debug sdma vram access checks

drm_dev_enter returns true when accessiable so correct the error check.

Source VRAM buffer is reserved by TTM but not pinned so the gpu offset fetch throws a warning. Get the gpu offset without a check and then double check to make sure the source buffer hasn't fallen into a hole. Otherwise use MMIO access to deal with non-contiguous VRAM cases as usual.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +15 -4
index 6178ae7ba624..0acfd872bfef 100644
@@ -1440,6 +1440,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 	struct dma_fence *fence;
 	uint64_t src_addr, dst_addr;
 	unsigned int num_dw;
+	bool vram_is_contiguous;
 	int r, idx;
 
 	if (len != PAGE_SIZE)
@@ -1448,9 +1449,8 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 	if (!adev->mman.sdma_access_ptr)
 		return -EACCES;
 
-	r = drm_dev_enter(adev_to_drm(adev), &idx);
-	if (r)
-		return r;
+	if (!drm_dev_enter(adev_to_drm(adev), &idx))
+		return -ENODEV;
 
 	if (write)
 		memcpy(adev->mman.sdma_access_ptr, buf, len);
@@ -1460,7 +1460,18 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 	if (r)
 		goto out;
 
-	src_addr = amdgpu_bo_gpu_offset(abo);
+	src_addr = amdgpu_bo_gpu_offset_no_check(abo);
+	vram_is_contiguous = (adev->gmc.real_vram_size - 1 ==
+				adev->gmc.vram_end - adev->gmc.vram_start) &&
+				src_addr >= adev->gmc.vram_start &&
+				src_addr + len <= adev->gmc.vram_end;
+
+	if (!vram_is_contiguous) {
+		amdgpu_job_free(job);
+		r = -EACCES;
+		goto out;
+	}
+
 	dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
 	if (write)
 		swap(src_addr, dst_addr);
Signature · 1 line
2.25.1
#1 lore raw

Am 2022-01-17 um 6:43 p.m. schrieb Jonathan Kim:

Quoted 7 lines

drm_dev_enter returns true when accessiable so correct the error check.

Source VRAM buffer is reserved by TTM but not pinned so the gpu offset fetch throws a warning. Get the gpu offset without a check and then double check to make sure the source buffer hasn't fallen into a hole. Otherwise use MMIO access to deal with non-contiguous VRAM cases as usual.

There is a way to get the correct offset for non-contiguous memory the same way amdgpu_ttm_access_memory does, using amdgpu_res_first. Since you only need a single page, you should never need amdgpu_res_next. Just make sure that the cursor.size >= len.

Regards,
  Felix

Quoted 50 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +15 -4
index 6178ae7ba624..0acfd872bfef 100644
@@ -1440,6 +1440,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 	struct dma_fence *fence;
 	uint64_t src_addr, dst_addr;
 	unsigned int num_dw;
+	bool vram_is_contiguous;
 	int r, idx;
 
 	if (len != PAGE_SIZE)
@@ -1448,9 +1449,8 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 	if (!adev->mman.sdma_access_ptr)
 		return -EACCES;
 
-	r = drm_dev_enter(adev_to_drm(adev), &idx);
-	if (r)
-		return r;
+	if (!drm_dev_enter(adev_to_drm(adev), &idx))
+		return -ENODEV;
 
 	if (write)
 		memcpy(adev->mman.sdma_access_ptr, buf, len);
@@ -1460,7 +1460,18 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 	if (r)
 		goto out;
 
-	src_addr = amdgpu_bo_gpu_offset(abo);
+	src_addr = amdgpu_bo_gpu_offset_no_check(abo);
+	vram_is_contiguous = (adev->gmc.real_vram_size - 1 ==
+				adev->gmc.vram_end - adev->gmc.vram_start) &&
+				src_addr >= adev->gmc.vram_start &&
+				src_addr + len <= adev->gmc.vram_end;
+
+	if (!vram_is_contiguous) {
+		amdgpu_job_free(job);
+		r = -EACCES;
+		goto out;
+	}
+
 	dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
 	if (write)
 		swap(src_addr, dst_addr);
maintainer
#2 lore raw
In reply to Jonathan Kim

Am 18.01.22 um 00:43 schrieb Jonathan Kim:

Quoted 4 lines

drm_dev_enter returns true when accessiable so correct the error check.

Source VRAM buffer is reserved by TTM but not pinned so the gpu offset fetch throws a warning.

Well it throws a warning because what you try to do here won't work.

Quoted 35 lines

Get the gpu offset without a check and then
double check to make sure the source buffer hasn't fallen into a hole. Otherwise use MMIO access to deal with non-contiguous VRAM cases as usual.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 19 +++++++++++++++----
  1 file changed, 15 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -3
index 6178ae7ba624..0acfd872bfef 100644
@@ -1440,6 +1440,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
  	struct dma_fence *fence;
  	uint64_t src_addr, dst_addr;
  	unsigned int num_dw;
+	bool vram_is_contiguous;
  	int r, idx;
 
  	if (len != PAGE_SIZE)
@@ -1448,9 +1449,8 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
  	if (!adev->mman.sdma_access_ptr)
  		return -EACCES;
 
-	r = drm_dev_enter(adev_to_drm(adev), &idx);
-	if (r)
-		return r;
+	if (!drm_dev_enter(adev_to_drm(adev), &idx))
+		return -ENODEV;
 
  	if (write)
  		memcpy(adev->mman.sdma_access_ptr, buf, len);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo, · line 1467

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -1
@@ -1460,7 +1460,18 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
  	if (r)
  		goto out;
 
-	src_addr = amdgpu_bo_gpu_offset(abo);
+	src_addr = amdgpu_bo_gpu_offset_no_check(abo);
+	vram_is_contiguous = (adev->gmc.real_vram_size - 1 ==
+				adev->gmc.vram_end - adev->gmc.vram_start) &&
+				src_addr >= adev->gmc.vram_start &&
+				src_addr + len <= adev->gmc.vram_end;

That code is utterly nonsense. What you need to do is to use the iterator to get the real VRAM address.

The return value of amdgpu_bo_gpu_offset() and amdgpu_bo_gpu_offset_no_check() is only valid for pinned contiguous buffers.

Regards,
Christian.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -0
+
+	if (!vram_is_contiguous) {
+		amdgpu_job_free(job);
+		r = -EACCES;
+		goto out;
+	}
+
  	dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
  	if (write)
  		swap(src_addr, dst_addr);