Add vm_bind param check to ensure no overlap with kbo AS carveout
Subsystems (3)
v5 cover Add vm_bind param check to ensure no overlap with kbo AS carveout
Just a quick check to make sure user-supplied vm_bind regions aren't clashing with the region reserved for kernel bo's.
I tried to introduce a similar check for panthor_vm_alloc_va(), to throw back an error when mappings of kernel bo's against specific addresses fall within the auto_va region. However that is not possible, since there's one FW region that must be mapped right at CSF_MCU_SHARED_REGION_START. That is usually not a problem, since drm_mm_insert_node_in_range() will pick the next one available.
v3 also comes with an early bind range overflow check for sparse mappings.
---
Changes in v5:
- Coalesced uint64 and kbo ranges overflow checks into a single patch.
- Link to v4: https://patch.msgid.link/[email protected]
Changes in v4:
- Deleted commit that passes single operation to panthor_vm_prepare_unmap_op_ctx.
- Moved overflow check one step above in the call stack.
- Rearranged the commits in the series.
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- Fixed off-by-one error for user va range calculations.
- Added new commit for panthor_vm_prepare_unmap_op_ctx to take a whole operation.
- Added commit with bind range early overflow check.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Simplified user VA range with kernel BO range overlap to a single statement.
- Link to v1: https://patch.msgid.link/[email protected]
To: Boris Brezillon <[email protected]>
To: Steven Price <[email protected]>
To: Liviu Dudau <[email protected]>
To: Maarten Lankhorst <[email protected]>
To: Maxime Ripard <[email protected]>
To: Thomas Zimmermann <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
To: Heiko Stuebner <[email protected]>
To: Grant Likely <[email protected]>
To: Adrián Larumbe <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
Adrián Larumbe (2):
drm/panthor: Add vm_bind region with kbo range overlap check drm/panthor: Fix comment to reflect actual struct field name
drivers/gpu/drm/panthor/panthor_mmu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
---
base-commit: 8fca3d8dbebf8d960dad7b10db3cb4a61139454b
change-id: 20260614-vm_bind_checks-46075ba069a0
Best regards,
Signature · 1 line
Adrián Larumbe <[email protected]>
PATCH v5 1/2 drm/panthor: Add vm_bind region with kbo range overlap check
When a VM is created, caller has to specify the range of the address space carve-out set aside for mapping kernel BO's. That means vm_bind mappings of UM-exposed BO's should not intersect with that region, but at the moment we're not checking this.
At first, I thought of giving these values to drm_gpuvm_init() through its reserve_{offset, range} arguments, but it turns out that is meant for VM address spans that are not managed through the usual drm_gpuvm split/merge circuit, so storing the end of the user VA range at VM creation time and doing a quick check in the vm_bind ioctl path was the simplest workaround.
The new check also makes sure vm_bind range doesn't overflow the size of a 64-bit unsigned integer. That was already being done further down the call stack inside drm_gpuvm_sm_map -> drm_gpuvm_range_valid, but it's best to fail early in the driver before GPUVM functions are invoked so that we won't waste time allocating vm_bind context resources.
--- drivers/gpu/drm/panthor/panthor_mmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/gpu/drm/panthor/panthor_mmu.c +9 -0
@@ -310,6 +310,9 @@ struct panthor_vm {u64 end; } kernel_auto_va; +/** @user_va_range: Upper boundary of VAs VM users can map objects against. */ +u64 user_va_range; +/** @as: Address space related fields. */ struct {/**
@@ -2893,6 +2896,8 @@ panthor_vm_create(struct panthor_device *ptdev, bool for_mcu, va_range = full_va_range; } + vm->user_va_range = kernel_va_start; +mutex_init (&vm->mm_lock);drm_mm_init (&vm->mm, kernel_va_start, kernel_va_size); vm->kernel_auto_va.start = auto_kernel_va_start;
@@ -2981,6 +2986,10 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,if (!IS_ALIGNED (op->va | op->size | op->bo_offset, vm_pgsz))return -EINVAL ; +/* We don't allow mappings that overlap with kbo's reserved range */ +if (range_overflows (op->va, op->size, vm->user_va_range)) +return -EINVAL ; +switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP :if (!(op->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) {
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 1/2] drm/panthor: Add vm_bind region with kbo range overlap check
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Zero-size asynchronous VM_BIND operations can corrupt the drm_gpuvm interval tree. --
--- Patch [1]: [PATCH v5 1/2] drm/panthor: Add vm_bind region with kbo range overlap check --- commit 0c0faa58a385567244a3f75b05f6667907422807
Author: Adrián Larumbe <[email protected]>
drm/panthor: Add vm_bind region with kbo range overlap check
When a VM is created, the caller has to specify the range of the address space carve-out set aside for mapping kernel buffer objects. That means vm_bind mappings of userspace-exposed objects should not intersect with that region, which this patch checks during the vm_bind ioctl.
Quoted 4 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-0
[ ... ]
drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_bind_prepare_op_ctx(struct drm_file *file, · line 2992
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-0@@ -2981,6 +2986,10 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,if (!IS_ALIGNED (op->va | op->size | op->bo_offset, vm_pgsz))return -EINVAL ; +/* We don't allow mappings that overlap with kbo's reserved range */ +if (range_overflows (op->va, op->size, vm->user_va_range)) +return -EINVAL ; +
[Severity: High]
This is a pre-existing issue, but does this function need to reject op->size == 0 before proceeding?
A zero-size map operation is currently treated as aligned, and range_overflows() will return false, allowing it to pass this validation.
If userspace passes a zero size, inserting a zero-size interval into the interval tree will compute an end address of addr - 1. This violates the red-black augmented tree invariants which require start to be less than or equal to last.
Could this cause structural corruption of the interval subtree values, leading to subsequent tree traversals silently failing to find mapped regions? Unlike the sync path, panthor_vm_bind_job_create() appears to lack a size check for asynchronous jobs, which might allow unprivileged userspace to trigger this directly.
Quoted 3 lines
drivers/gpu/drm/panthor/panthor_mmu.c +0
-0switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP :if (!(op->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v5 1/2] drm/panthor: Add vm_bind region with kbo range overlap check
In reply to Adrián LarumbeQuoted 11 lines · Adrián Larumbe
When a VM is created, caller has to specify the range of the address space carve-out set aside for mapping kernel BO's. That means
vm_bindmappings of UM-exposed BO's should not intersect with that region, but at the moment we're not checking this.At first, I thought of giving these values to
drm_gpuvm_init()through its reserve_{offset, range} arguments, but it turns out that is meant for VM address spans that are not managed through the usualdrm_gpuvmsplit/merge circuit, so storing the end of the user VA range at VM creation time and doing a quick check in thevm_bindioctl path was the simplest workaround.
Quoted 7 lines
The new check also makes sure
vm_bindrange doesn't overflow the size of a 64-bit unsigned integer. That was already being done further down the call stack insidedrm_gpuvm_sm_map->drm_gpuvm_range_valid, but it's best to fail early in the driver before GPUVM functions are invoked so that we won't waste time allocatingvm_bindcontext resources.Fixes tag and reference the relevant sparse binding support commit.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Can you dig out the relevant commit ;)
Quoted 3 lines
Fixes 647810ec2476 ("drm/panthor: Add the MMU/VM logical block")Reviewed-by Boris Brezillon <[email protected]>Signed-off-by Adrián Larumbe <[email protected]>
Thanks,
Steve
Quoted 39 lines
--- drivers/gpu/drm/panthor/panthor_mmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/gpu/drm/panthor/panthor_mmu.c +9
-0@@ -310,6 +310,9 @@ struct panthor_vm {u64 end; } kernel_auto_va; +/** @user_va_range: Upper boundary of VAs VM users can map objects against. */ +u64 user_va_range; +/** @as: Address space related fields. */ struct {/** @@ -2893,6 +2896,8 @@ panthor_vm_create(struct panthor_device *ptdev, bool for_mcu, va_range = full_va_range; } + vm->user_va_range = kernel_va_start; +mutex_init (&vm->mm_lock);drm_mm_init (&vm->mm, kernel_va_start, kernel_va_size); vm->kernel_auto_va.start = auto_kernel_va_start;@@ -2981,6 +2986,10 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,if (!IS_ALIGNED (op->va | op->size | op->bo_offset, vm_pgsz))return -EINVAL ; +/* We don't allow mappings that overlap with kbo's reserved range */ +if (range_overflows (op->va, op->size, vm->user_va_range)) +return -EINVAL ; +switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP :if (!(op->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) {
Re: [PATCH v5 1/2] drm/panthor: Add vm_bind region with kbo range overlap check
Quoted 18 lines · Steven Price
Quoted 17 lines · Adrián Larumbe
When a VM is created, caller has to specify the range of the address space carve-out set aside for mapping kernel BO's. That means
vm_bindmappings of UM-exposed BO's should not intersect with that region, but at the moment we're not checking this.At first, I thought of giving these values to
drm_gpuvm_init()through its reserve_{offset, range} arguments, but it turns out that is meant for VM address spans that are not managed through the usualdrm_gpuvmsplit/merge circuit, so storing the end of the user VA range at VM creation time and doing a quick check in thevm_bindioctl path was the simplest workaround.The new check also makes sure
vm_bindrange doesn't overflow the size of a 64-bit unsigned integer. That was already being done further down the call stack insidedrm_gpuvm_sm_map->drm_gpuvm_range_valid, but it's best to fail early in the driver before GPUVM functions are invoked so that we won't waste time allocatingvm_bindcontext resources.
Quoted 3 lines
Quoted 1 line
Fixes tag and reference the relevant sparse binding support commit.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Can you dig out the relevant commit ;)
Oops. Will do.
Quoted 40 lines
Quoted 3 lines
Fixes 647810ec2476 ("drm/panthor: Add the MMU/VM logical block")Reviewed-by Boris Brezillon <[email protected]>Signed-off-by Adrián Larumbe <[email protected]>Reviewed-by Steven Price <[email protected]>Thanks,
SteveQuoted 31 lines
--- drivers/gpu/drm/panthor/panthor_mmu.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/gpu/drm/panthor/panthor_mmu.c +5
-0@@ -310,6 +310,9 @@ struct panthor_vm {u64 end; } kernel_auto_va; +/** @user_va_range: Upper boundary of VAs VM users can map objects against. */ +u64 user_va_range; +/** @as: Address space related fields. */ struct {/** @@ -2893,6 +2896,8 @@ panthor_vm_create(struct panthor_device *ptdev, bool for_mcu, va_range = full_va_range; } + vm->user_va_range = kernel_va_start; +mutex_init (&vm->mm_lock);drm_mm_init (&vm->mm, kernel_va_start, kernel_va_size); vm->kernel_auto_va.start = auto_kernel_va_start;@@ -2981,6 +2986,10 @@ panthor_vm_bind_prepare_op_ctx(struct drm_file *file,if (!IS_ALIGNED (op->va | op->size | op->bo_offset, vm_pgsz))return -EINVAL ;
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c panthor_vm_bind_prepare_op_ctx(struct drm_file *file, · line 2995
Quoted 8 lines
drivers/gpu/drm/panthor/panthor_mmu.c +4
-0+/* We don't allow mappings that overlap with kbo's reserved range */ +if (range_overflows (op->va, op->size, vm->user_va_range)) +return -EINVAL ; +switch (op->flags &DRM_PANTHOR_VM_BIND_OP_TYPE_MASK ) {case DRM_PANTHOR_VM_BIND_OP_TYPE_MAP :if (!(op->flags &DRM_PANTHOR_VM_BIND_OP_MAP_SPARSE )) {
Adrian Larumbe
PATCH v5 2/2 drm/panthor: Fix comment to reflect actual struct field name
In reply to Adrián LarumbeThe mismatch would pop up when building the kernel with W=1.
--- drivers/gpu/drm/panthor/panthor_mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/panthor/panthor_mmu.c +1 -1
@@ -306,7 +306,7 @@ struct panthor_vm {/** @kernel_auto_va.start: Start of the automatic VA-range for kernel BOs. */ u64 start; -/** @kernel_auto_va.size: Size of the automatic VA-range for kernel BOs. */ +/** @kernel_auto_va.end: End of the automatic VA-range for kernel BOs. */ u64 end; } kernel_auto_va;
Signature · 1 line
2.55.0