Skip to contentSkip to conversation

Add vm_bind param check to ensure no overlap with kbo AS carveout

PATCH In review Adrián Larumbe 2 participants 4 messages Jul 20 – Jul 29

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

v6 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 v6:
- Filled in 'Fixes' commit tag for first patch in the series.
- Link to v5: https://patch.msgid.link/[email protected]

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: 35fff4830cb999b5496ae892a85516fef2912438
change-id: 20260614-vm_bind_checks-46075ba069a0

Best regards,

Signature · 1 line
Adrián Larumbe <[email protected]>
#1 lore raw

PATCH v6 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.

Fixes 12cf826bf1dd ("drm/panthor: Support sparse mappings")
Fixes 647810ec2476 ("drm/panthor: Add the MMU/VM logical block")
---
 drivers/gpu/drm/panthor/panthor_mmu.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/gpu/drm/panthor/panthor_mmu.c +9 -0
index f45ef5824ff2..80a1dac40a6a 100644
@@ -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 {
 		/**
@@ -2892,6 +2895,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;
@@ -2980,6 +2985,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
maintainer Reviewed-by on v6 1/2
#2 lore raw

Re: [PATCH v6 1/2] drm/panthor: Add vm_bind region with kbo range overlap check

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_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.

Quoted 4 lines
Fixes 12cf826bf1dd ("drm/panthor: Support sparse mappings")
Fixes 647810ec2476 ("drm/panthor: Add the MMU/VM logical block")

I'll push both patches to drm-misc-next.

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
index f45ef5824ff2..80a1dac40a6a 100644
@@ -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 {
 		/**
@@ -2892,6 +2895,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;
@@ -2980,6 +2985,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)) {
#3 lore raw

PATCH v6 2/2 drm/panthor: Fix comment to reflect actual struct field name

In reply to Adrián Larumbe

The 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
index 80a1dac40a6a..0182b72f1932 100644
@@ -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