On Sun, 12 Jul 2026 15:54:35 +0200 Ketil Johnsen <[email protected]> wrote:
> Currently the panthor_vm_lock_region() function will implicitly expand > an already locked VM region. This can be problematic because the caller > do not reliably know if it needs to call panthor_vm_unlock_region() > or not. > > Worth noting, there is currently no known issues with this as the code > is written today. > > This change introduces panthor_vm_expand_region() which will only work > if there is already a locked VM region. This again means that the > original lock and unlock functions can work as a pair. This pairing is > needed for subsequent protected memory changes. > > Signed-off-by: Ketil Johnsen <[email protected]> > --- > drivers/gpu/drm/panthor/panthor_mmu.c | 87 +++++++++++++++++++-------- > 1 file changed, 62 insertions(+), 25 deletions(-) > > diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c > b/drivers/gpu/drm/panthor/panthor_mmu.c > index 1f6b9242279c2..b82b01013611c 100644 > --- a/drivers/gpu/drm/panthor/panthor_mmu.c > +++ b/drivers/gpu/drm/panthor/panthor_mmu.c > @@ -1774,20 +1774,49 @@ static const char *access_type_name(struct > panthor_device *ptdev, > } > } > > +static int panthor_vm_apply_as_lock(struct panthor_vm *vm, u64 region) > +{ > + struct panthor_device *ptdev = vm->ptdev; > + > + lockdep_assert_held(&ptdev->mmu->as.slots_lock); > + > + gpu_write64(ptdev->mmu->iomem, AS_LOCKADDR(vm->as.id), region); > + return as_send_cmd_and_wait(ptdev, vm->as.id, AS_COMMAND_LOCK); > +} > + > static int panthor_vm_lock_region(struct panthor_vm *vm, u64 start, u64 size) > { > struct panthor_device *ptdev = vm->ptdev; > int ret = 0; > > - /* sm_step_remap() can call panthor_vm_lock_region() to account for > - * the wider unmap needed when doing a partial huge page unamp. We > - * need to ignore the lock if it's already part of the locked region. > - */ > - if (start >= vm->locked_region.start && > - start + size <= vm->locked_region.start + vm->locked_region.size) > - return 0; > + if (drm_WARN_ON(&ptdev->base, vm->locked_region.size)) > + return -EINVAL; > + > + mutex_lock(&ptdev->mmu->as.slots_lock); > + if (vm->as.id >= 0 && size) { > + u64 region = pack_region_range(ptdev, &start, &size); > + > + ret = panthor_vm_apply_as_lock(vm, region); > + } > > - /* sm_step_remap() may need a locked region that isn't a strict superset > + if (!ret) { > + vm->locked_region.start = start; > + vm->locked_region.size = size; > + } > + mutex_unlock(&ptdev->mmu->as.slots_lock); > + > + return ret; > +} > + > +static int panthor_vm_expand_locked_region(struct panthor_vm *vm, > + u64 start, u64 size) > +{ > + struct panthor_device *ptdev = vm->ptdev; > + u64 end; > + int ret = 0; > + > + /* This function is here to handle the following case: > + * sm_step_remap() may need a locked region that isn't a strict superset > * of the original one because of having to extend unmap boundaries > beyond > * it to deal with partial unmaps of transparent huge pages. What we > want > * in those cases is to lock the union of both regions. The new region > must > @@ -1795,25 +1824,30 @@ static int panthor_vm_lock_region(struct panthor_vm > *vm, u64 start, u64 size) > * boundaries in a remap operation can only shift up or down > respectively, > * but never otherwise. > */ > - if (vm->locked_region.size) { > - u64 end = max(vm->locked_region.start + vm->locked_region.size, > - start + size); > > - drm_WARN_ON_ONCE(&vm->ptdev->base, (start + size <= > vm->locked_region.start) || > - (start >= vm->locked_region.start + > vm->locked_region.size)); > + /* This function can only expand an already locked region */ > + if (drm_WARN_ON(&ptdev->base, !vm->locked_region.size)) > + return -EINVAL; > + > + /* Early out if requested range is already locked */ > + if (start >= vm->locked_region.start && > + start + size <= vm->locked_region.start + vm->locked_region.size) > + return 0; > > - start = min(start, vm->locked_region.start); > - size = end - start; > - } > + end = max(vm->locked_region.start + vm->locked_region.size, > + start + size); > + > + drm_WARN_ON_ONCE(&ptdev->base, (start + size <= > vm->locked_region.start) || > + (start >= vm->locked_region.start + > vm->locked_region.size)); > + > + start = min(start, vm->locked_region.start); > + size = end - start; > > mutex_lock(&ptdev->mmu->as.slots_lock); > if (vm->as.id >= 0 && size) { > - /* Lock the region that needs to be updated */ > - gpu_write64(ptdev->mmu->iomem, AS_LOCKADDR(vm->as.id), > - pack_region_range(ptdev, &start, &size)); > + u64 region = pack_region_range(ptdev, &start, &size); > > - /* If the lock succeeded, update the locked_region info. */ > - ret = as_send_cmd_and_wait(ptdev, vm->as.id, AS_COMMAND_LOCK); > + ret = panthor_vm_apply_as_lock(vm, region); > } > > if (!ret) { > @@ -2367,11 +2401,14 @@ static int panthor_gpuva_sm_step_remap(struct > drm_gpuva_op *op, > */ > unmap_hugepage_align(&op->remap, &unmap_start, &unmap_range); > > - /* If the range changed, we might have to lock a wider region > to guarantee > - * atomicity. panthor_vm_lock_region() bails out early if the > new region > - * is already part of the locked region, so no need to do this > check here. > + /* If the range changed, we might have to lock a wider region to > + * guarantee atomicity. > */ > - panthor_vm_lock_region(vm, unmap_start, unmap_range); > + ret = panthor_vm_expand_locked_region(vm, unmap_start, > + unmap_range); Could we add the panthor_vm_lock_region() error check in a commit coming before this one, and maybe flag it for backport, with a Fixes tag? > + if (ret) > + return ret; > + > panthor_vm_unmap_pages(vm, unmap_start, unmap_range); > } >
