Skip to contentSkip to conversation

drm/panthor: Fix handling of partial GPU mapping of BOs

PATCH Superseded Akash Goel 4 participants 4 messages Nov 11, 2024 – Nov 12, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Akash Goel

author
#0 lore raw

PATCH 1/1 drm/panthor: Fix handling of partial GPU mapping of BOs

This commit fixes the handling of partial GPU mapping of buffer objects in Panthor.
VM_BIND ioctl allows Userspace to partially map the BOs to GPU. To map a BO, Panthor walks through the sg_table to retrieve the physical address of pages. If the mapping is created at an offset into the BO, then the scatterlist(s) at the beginning have to be skipped to reach the one corresponding to the offset. But the case where the offset didn't point to the first page of desired scatterlist wasn't handled correctly. The bug caused the partial GPU mapping of BO to go wrong for the said case, as the pages didn't get map at the expected virtual address and consequently there were kernel warnings on unmap.

---
 drivers/gpu/drm/panthor/panthor_mmu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/panthor/panthor_mmu.c +1 -0
index d8cc9e7d064e..6bc188d9a9ad 100644
@@ -957,6 +957,7 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,
 
 		paddr += offset;
 		len -= offset;
+		offset = 0;
 		len = min_t(size_t, len, size);
 		size -= len;
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Akash Goel

This commit fixes the handling of partial GPU mapping of buffer objects in Panthor.
VM_BIND ioctl allows Userspace to partially map the BOs to GPU. To map a BO, Panthor walks through the sg_table to retrieve the physical address of pages. If the mapping is created at an offset into the BO, then the scatterlist(s) at the beginning have to be skipped to reach the one corresponding to the offset. But the case where the offset didn't point to the first page of desired scatterlist wasn't handled correctly. The bug caused the partial GPU mapping of BO to go wrong for the said case, as the pages didn't get map at the expected virtual address and consequently there were kernel warnings on unmap.

Quoted 1 line
Fixes 647810ec2476 ("drm/panthor: Add the MMU/VM logical block")

Thanks,
Steve

Quoted 16 lines
---
 drivers/gpu/drm/panthor/panthor_mmu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/panthor/panthor_mmu.c +1 -0
index d8cc9e7d064e..6bc188d9a9ad 100644
@@ -957,6 +957,7 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,
 
 		paddr += offset;
 		len -= offset;
+		offset = 0;
 		len = min_t(size_t, len, size);
 		size -= len;
 
maintainer Reviewed-by
#2 lore raw
Quoted 13 lines · On Mon, 11 Nov 2024 11:31:12 +0000 Steven Price <[email protected]> wrote:
Quoted 12 lines · Akash Goel

This commit fixes the handling of partial GPU mapping of buffer objects in Panthor.
VM_BIND ioctl allows Userspace to partially map the BOs to GPU. To map a BO, Panthor walks through the sg_table to retrieve the physical address of pages. If the mapping is created at an offset into the BO, then the scatterlist(s) at the beginning have to be skipped to reach the one corresponding to the offset. But the case where the offset didn't point to the first page of desired scatterlist wasn't handled correctly. The bug caused the partial GPU mapping of BO to go wrong for the said case, as the pages didn't get map at the expected virtual address and consequently there were kernel warnings on unmap.

Quoted 5 lines
Quoted 1 line
Fixes 647810ec2476 ("drm/panthor: Add the MMU/VM logical block")
Quoted 21 lines

Thanks,
Steve

Quoted 16 lines
---
 drivers/gpu/drm/panthor/panthor_mmu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/panthor/panthor_mmu.c +1 -0
index d8cc9e7d064e..6bc188d9a9ad 100644
@@ -957,6 +957,7 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,
 
 		paddr += offset;
 		len -= offset;
+		offset = 0;
 		len = min_t(size_t, len, size);
 		size -= len;
 
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Akash Goel
Quoted 11 lines · Akash Goel

This commit fixes the handling of partial GPU mapping of buffer objects in Panthor.
VM_BIND ioctl allows Userspace to partially map the BOs to GPU. To map a BO, Panthor walks through the sg_table to retrieve the physical address of pages. If the mapping is created at an offset into the BO, then the scatterlist(s) at the beginning have to be skipped to reach the one corresponding to the offset. But the case where the offset didn't point to the first page of desired scatterlist wasn't handled correctly. The bug caused the partial GPU mapping of BO to go wrong for the said case, as the pages didn't get map at the expected virtual address and consequently there were kernel warnings on unmap.

Maybe it's just me, but I would find it easier to figure out what's being fixed here if commit message said something like:

When the BO being mapped spans multiple scatterlists, offset is not cleared after the starting scatterlist, leading to holes in the mapping.

If I understand it correctly you found this based on some WARN() being triggered, so maybe adding the dump here would've helped too.

Quoted 11 lines
---
 drivers/gpu/drm/panthor/panthor_mmu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/panthor/panthor_mmu.c +0 -0
index d8cc9e7d064e..6bc188d9a9ad 100644

drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot, · line 962

Quoted 6 lines
drivers/gpu/drm/panthor/panthor_mmu.c +1 -0
@@ -957,6 +957,7 @@ panthor_vm_map_pages(struct panthor_vm *vm, u64 iova, int prot,
 
 		paddr += offset;
 		len -= offset;
+		offset = 0;
 		len = min_t(size_t, len, size);
 		size -= len;

Again, my preference so feel free to ignore, but I would put the resetting of offset at the end of for_each_sgtable_dma_sg() loop, after the if (!size) break lines. That way it is clear that it applies to the next iteration of the loop.

Regardsless of the changes you're going to make,

Best regards,
Liviu

Quoted 4 lines
Signature · 2 lines
-- 
2.25.1
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯