Skip to contentSkip to conversation

drm/amdgpu: fix KFDMemoryTest.PtraceAccessInvisibleVram fail on SRIOV

PATCH In review Samuel Zhang 3 participants 5 messages Aug 7, 2024 – Aug 12, 2024

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

PATCH 1/1 drm/amdgpu: fix KFDMemoryTest.PtraceAccessInvisibleVram fail on SRIOV

Ptrace access VRAM bo will first try sdma access in amdgpu_ttm_access_memory_sdma(), if fails, it will fallback to mmio access.

Since ptrace only access 8 bytes at a time and amdgpu_ttm_access_memory_sdma() only allow PAGE_SIZE bytes access, it returns fail.
On SRIOV, mmio access will also fail as MM_INDEX/MM_DATA register write is blocked for security reasons.

The fix is just change len check in amdgpu_ttm_access_memory_sdma() so that len in (0, PAGE_SIZE] are allowed. This will not fix the ptrace test case on SRIOV, but also improve the access performance when the access length is < PAGE_SIZE.
len > PAGE_SIZE case support is not needed as larger size will be break into chunks of PAGE_SIZE len max in mem_rw().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -2
index 5daa05e23ddf..a6e90eada367 100644
@@ -1486,7 +1486,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 	unsigned int num_dw;
 	int r, idx;
 
-	if (len != PAGE_SIZE)
+	if (len > PAGE_SIZE)
 		return -EINVAL;
 
 	if (!adev->mman.sdma_access_ptr)
@@ -1514,7 +1514,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
 		swap(src_addr, dst_addr);
 
 	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
-				PAGE_SIZE, 0);
+				len, 0);
 
 	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
 	WARN_ON(job->ibs[0].length_dw > num_dw);
Signature · 1 line
2.25.1
#1 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Hi @Koenig, Christian<mailto:[email protected]> and @Kim, Jonathan<mailto:[email protected]>,

Could you help review this patch?

Thanks
Sam

From: Samuel Zhang <[email protected]>
Date: Wednesday, August 7, 2024 at 16:36
To: [email protected] <[email protected]>
Cc: Zhang, GuoQing (Sam) <[email protected]>
Subject: [PATCH] drm/amdgpu: fix KFDMemoryTest.PtraceAccessInvisibleVram fail on SRIOV Ptrace access VRAM bo will first try sdma access in amdgpu_ttm_access_memory_sdma(), if fails, it will fallback to mmio access.

Since ptrace only access 8 bytes at a time and amdgpu_ttm_access_memory_sdma() only allow PAGE_SIZE bytes access, it returns fail.
On SRIOV, mmio access will also fail as MM_INDEX/MM_DATA register write is blocked for security reasons.

The fix is just change len check in amdgpu_ttm_access_memory_sdma() so that len in (0, PAGE_SIZE] are allowed. This will not fix the ptrace test case on SRIOV, but also improve the access performance when the access length is < PAGE_SIZE.
len > PAGE_SIZE case support is not needed as larger size will be break into chunks of PAGE_SIZE len max in mem_rw().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -2
index 5daa05e23ddf..a6e90eada367 100644
@@ -1486,7 +1486,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
         unsigned int num_dw;
         int r, idx;
 
-       if (len != PAGE_SIZE)
+       if (len > PAGE_SIZE)
                 return -EINVAL;
 
         if (!adev->mman.sdma_access_ptr)
@@ -1514,7 +1514,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
                 swap(src_addr, dst_addr);
 
         amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
-                               PAGE_SIZE, 0);
+                               len, 0);
 
         amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
         WARN_ON(job->ibs[0].length_dw > num_dw);

--
2.25.1

#2 lore raw
In reply to Samuel Zhang
Quoted 10 lines · Samuel Zhang

Ptrace access VRAM bo will first try sdma access in amdgpu_ttm_access_memory_sdma(), if fails, it will fallback to mmio access.

Since ptrace only access 8 bytes at a time and amdgpu_ttm_access_memory_sdma() only allow PAGE_SIZE bytes access, it returns fail.
On SRIOV, mmio access will also fail as MM_INDEX/MM_DATA register write is blocked for security reasons.

Quoted 6 lines

The fix is just change len check in amdgpu_ttm_access_memory_sdma() so that len in (0, PAGE_SIZE] are allowed. This will not fix the ptrace test case on SRIOV, but also improve the access performance when the access length is < PAGE_SIZE.
len > PAGE_SIZE case support is not needed as larger size will be break into chunks of PAGE_SIZE len max in mem_rw().

I'm not convinced that using SDMA for small accesses is the best solution for all cases. For example, on large-BAR GPUs we should fall back to access through the FB BAR before we use indirect register access. That may still perform better than SDMA especially for very small accesses like 4-bytes typical for ptrace accesses. Maybe this needs an SRIOV-VF-specific condition if MMIO register access is not an option there.

@Jonathan Kim, can you chime in as well?

Thanks,
  Felix

Quoted 28 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -2
index 5daa05e23ddf..a6e90eada367 100644
@@ -1486,7 +1486,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
  	unsigned int num_dw;
  	int r, idx;
 
-	if (len != PAGE_SIZE)
+	if (len > PAGE_SIZE)
  		return -EINVAL;
 
  	if (!adev->mman.sdma_access_ptr)
@@ -1514,7 +1514,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
  		swap(src_addr, dst_addr);
 
  	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
-				PAGE_SIZE, 0);
+				len, 0);
 
  	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
  	WARN_ON(job->ibs[0].length_dw > num_dw);
#3 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Hi @Kuehling, Felix<mailto:[email protected]>,
Thank you for review. I agree with you that we need to adjust the priority of each access method here.

Hi @Kim, Jonathan<mailto:[email protected]>,
I also find a new issue in amdgpu_ttm_access_memory_sdma() after my first patch. I sent a second patch to fix the new issue. Please help review. “[PATCH 2/2] drm/amdgpu: fix incomplete access issue in amdgpu_ttm_access_memory_sdma()”

Regards
Sam

From: Kuehling, Felix <[email protected]>
Date: Saturday, August 10, 2024 at 07:49
To: [email protected] <[email protected]>, Zhang, GuoQing (Sam) <[email protected]>, Kim, Jonathan <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: fix KFDMemoryTest.PtraceAccessInvisibleVram fail on SRIOV

Quoted 10 lines · Samuel Zhang

Ptrace access VRAM bo will first try sdma access in amdgpu_ttm_access_memory_sdma(), if fails, it will fallback to mmio access.

Since ptrace only access 8 bytes at a time and amdgpu_ttm_access_memory_sdma() only allow PAGE_SIZE bytes access, it returns fail.
On SRIOV, mmio access will also fail as MM_INDEX/MM_DATA register write is blocked for security reasons.

Quoted 6 lines

The fix is just change len check in amdgpu_ttm_access_memory_sdma() so that len in (0, PAGE_SIZE] are allowed. This will not fix the ptrace test case on SRIOV, but also improve the access performance when the access length is < PAGE_SIZE.
len > PAGE_SIZE case support is not needed as larger size will be break into chunks of PAGE_SIZE len max in mem_rw().

I'm not convinced that using SDMA for small accesses is the best solution for all cases. For example, on large-BAR GPUs we should fall back to access through the FB BAR before we use indirect register access. That may still perform better than SDMA especially for very small accesses like 4-bytes typical for ptrace accesses. Maybe this needs an SRIOV-VF-specific condition if MMIO register access is not an option there.

@Jonathan Kim, can you chime in as well?

Thanks,
Felix

Quoted 28 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -2
index 5daa05e23ddf..a6e90eada367 100644
@@ -1486,7 +1486,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
       unsigned int num_dw;
       int r, idx;
 
-     if (len != PAGE_SIZE)
+     if (len > PAGE_SIZE)
               return -EINVAL;
 
       if (!adev->mman.sdma_access_ptr)
@@ -1514,7 +1514,7 @@ static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
               swap(src_addr, dst_addr);
 
       amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
-                             PAGE_SIZE, 0);
+                             len, 0);
 
       amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
       WARN_ON(job->ibs[0].length_dw > num_dw);
#4 lore raw
In reply to Felix Kuehling

[Public]

Quoted 35 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Friday, August 9, 2024 7:49 PM
To: [email protected]; Zhang, GuoQing (Sam) <[email protected]>; Kim, Jonathan <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: fix KFDMemoryTest.PtraceAccessInvisibleVram fail on SRIOV

Quoted 10 lines · Samuel Zhang

Ptrace access VRAM bo will first try sdma access in amdgpu_ttm_access_memory_sdma(), if fails, it will fallback to mmio access.

Since ptrace only access 8 bytes at a time and amdgpu_ttm_access_memory_sdma() only allow PAGE_SIZE bytes access, it returns fail.
On SRIOV, mmio access will also fail as MM_INDEX/MM_DATA register write is blocked for security reasons.

Quoted 6 lines

The fix is just change len check in amdgpu_ttm_access_memory_sdma() so that len in (0, PAGE_SIZE] are allowed. This will not fix the ptrace test case on SRIOV, but also improve the access performance when the access length is < PAGE_SIZE.
len > PAGE_SIZE case support is not needed as larger size will be break into chunks of PAGE_SIZE len max in mem_rw().

I'm not convinced that using SDMA for small accesses is the best solution for all cases. For example, on large-BAR GPUs we should fall back to access through the FB BAR before we use indirect register access. That may still perform better than SDMA especially for very small accesses like 4-bytes typical for ptrace accesses. Maybe this needs an SRIOV-VF-specific condition if MMIO register access is not an option there.

Quoted 1 line

@Jonathan Kim, can you chime in as well?

Relaxing length checks only under SRIOV mode is probably a good idea.
SDMA enqueue latency impacting performance for sub-page copy sizes has been observed in the past.
Plus MMIO is stable even if SDMA is dead.
I know we have fallbacks anyways in the general case, but it's probably better not to prod a defunct SDMA if we don't have to.

Thanks,

Jon

Quoted 36 lines

Thanks,
Felix

Review

Quoted 7 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int

Quoted 4 lines

index 5daa05e23ddf..a6e90eada367 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1486,7 +1486,7 @@ static int

amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
    unsigned int num_dw;
    int r, idx;
 
-   if (len != PAGE_SIZE)
+   if (len > PAGE_SIZE)
            return -EINVAL;
 
    if (!adev->mman.sdma_access_ptr)
@@ -1514,7 +1514,7 @@ static int

amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
            swap(src_addr, dst_addr);
 
    amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
-                           PAGE_SIZE, 0);
+                           len, 0);
 
    amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
    WARN_ON(job->ibs[0].length_dw > num_dw);