[PATCH v2] KVM: arm64: Resume stage-2 unmap where the walk stopped

Wei-Lin Chang weilin.chang at arm.com
Wed Nov 11 12:59:27 PST 2026


Hi,

On Tue, Nov 10, 2026 at 09:47:06PM +0500, Haseeb Ashraf wrote:
> From: Haseeb Ashraf <haseeb.ashraf at siemens.com>
> 
> kvm_vm_stage2_unmap_range() unmaps a range one block-sized chunk at a
> time. The walker steps over an invalid entry in one go, but the next
> walk starts at the following chunk, so an invalid entry larger than a
> chunk is visited once for every chunk it covers. Unmapping the whole
> IPA space of a shadow stage-2, as emulating TLBI VMALLS12E1IS and TLBI
> ALLE1IS does, takes 2^18 walks with 4K pages and a 48-bit IPA space,
> however little it maps. Each walk also ends with a range TLBI when
> FEAT_TLBIRANGE and FWB are available.
> 
> On an AmpereOne A192-32X (192 CPUs, Xen on KVM, 4K pages, 48-bit IPA),
> a full-range unmap of one shadow stage-2 took 0.82-0.94 s (mean 0.91 s
> over 192 unmaps) on 6.18.29. With this change and the mainline commits
> it builds on backported, it took 2.1-27.8 ms (mean 3.8 ms over 128
> unmaps), with 512 to 1024 walks instead of 262144. A TLBI VMALLS12E1IS
> trap that unmapped two shadow stage-2s took up to 1.85 s before.
> 
> Add kvm_pgtable_stage2_unmap_range(), which returns the address its
> walk reached, and have the unmap loop resume from there.
> kvm_pgtable_stage2_unmap() still returns 0 or an error, for its
> callers at EL2. The walk handles every entry it steps over, including
> invalid entries that carry state, so nothing has to be looked up
> beforehand.
> 
> The unmap of a shadow stage-2 also drops the guest stage-2 records of
> the whole span the walk covered: nothing is mapped in the part it
> stepped over, so any record there is stale. Each walk that remains
> ends with the same deferred TLB invalidation as before. The spans that
> were skipped have no valid entry, so there is nothing to invalidate.
> 
> Suggested-by: Oliver Upton <oupton at kernel.org>
> Signed-off-by: Haseeb Ashraf <haseeb.ashraf at siemens.com>
> ---

[...]

> +/*
> + * Unmap [start, end) one chunk at a time, resuming from the address that each
> + * walk reached. An invalid entry that is larger than a chunk is then stepped
> + * over once, rather than once for every chunk that it covers.
> + */
> +static int stage2_apply_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
> +				    phys_addr_t end,
> +				    s64 (*fn)(struct kvm_pgtable *, u64, u64),
> +				    bool resched)
> +{
> +	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
> +	bool lock_dropped = false;
> +	phys_addr_t addr = start;
> +	u64 next;
> +	s64 ret;
> +
> +	do {
> +		struct kvm_pgtable *pgt = mmu->pgt;
> +
> +		/*
> +		 * We may be raced on PGT teardown when we release the
> +		 * kvm->mmu_lock. That's fine as the PGT is legitimately no
> +		 * longer present.
> +		 */
> +		if (!pgt)
> +			return lock_dropped ? 0 : -EINVAL;
> +
> +		next = stage2_range_addr_end(addr, end);
> +		ret = fn(pgt, addr, next - addr);
> +		if (ret < 0)
> +			return ret;
> +
> +		next = min_t(u64, ret, end);
> +		if (resched && next != end) {
> +			cond_resched_rwlock_write(&kvm->mmu_lock);
> +			lock_dropped = true;
> +		}
> +	} while (addr = next, addr != end);
> +
> +	return 0;
> +}

In my opinion it is worth giving the same treatment to all users of
stage2_apply_range(), instead of having this new
stage2_apply_unmap_range() that is almost identical.

Thanks,
Wei-Lin Chang

> +
>  static void kvm_vm_stage2_unmap_range(struct kvm_s2_mmu *mmu,
>  				      phys_addr_t start,
>  				      u64 size, bool may_block)
>  {
>  	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
> -	int (*fn)(struct kvm_pgtable *, u64, u64);
> +	s64 (*fn)(struct kvm_pgtable *pgt, u64 addr, u64 size);
>  
>  	if (kvm_is_nested_s2_mmu(kvm, mmu))
>  		fn = kvm_pgtable_stage2_unmap_tracked;
>  	else
> -		fn = kvm_pgtable_stage2_unmap;
> +		fn = kvm_pgtable_stage2_unmap_range;
>  
> -	WARN_ON(stage2_apply_range(mmu, start, start + size, fn, may_block));
> +	WARN_ON(stage2_apply_unmap_range(mmu, start, start + size, fn, may_block));
>  }
>  

[...]



More information about the linux-arm-kernel mailing list