From: Vincent Donnefort <vdonnefort@google.com>
To: Haseeb Ashraf <haseebashraf091@gmail.com>
Cc: maz@kernel.org, Oliver Upton <oupton@kernel.org>,
Haseeb Ashraf <haseeb.ashraf@siemens.com>,
Fuad Tabba <fuad.tabba@linux.dev>,
Wei-Lin Chang <weilin.chang@arm.com>,
Joey Gouly <joey.gouly@arm.com>,
fahad.arslan@siemens.com, Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Quentin Perret <qperret@google.com>,
Raghavendra Rao Ananta <rananta@google.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] KVM: arm64: Resume stage-2 range walks where they stopped
Date: Tue, 17 Nov 2026 16:18:32 +0000 [thread overview]
Message-ID: <avx-WAgBpyjJlwhg@google.com> (raw)
In-Reply-To: <20261113080233.640539-2-haseebashraf091@gmail.com>
On Fri, Nov 13, 2026 at 01:02:33PM +0500, Haseeb Ashraf wrote:
> From: Haseeb Ashraf <haseeb.ashraf@siemens.com>
>
> stage2_apply_range() walks 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.84-0.94 s (mean 0.91 s
> over 226 unmaps) on 6.18.29. With this change and the mainline commits
> it builds on backported, it took 2.1-45 ms (mean 4.1 ms over 672
> unmaps), with 512 to 1023 walks instead of 262144. The slowest TLBI
> VMALLS12E1IS trap took 0.94 s before and 46 ms after.
>
> Have the walk report the address it reached, and have
> stage2_apply_range() resume from there. Unmap, write-protect and flush
> all go through it, so all three get this. kvm_pgtable_stage2_unmap(),
> kvm_pgtable_stage2_wrprotect() and kvm_pgtable_stage2_flush() now
> return the address, or a negative error code, and so do their pKVM
> counterparts, which do not walk page tables and return the end of the
> range. The other callers, in nested.c and at EL2, only looked for an
> error: they now check for a negative value, and those that pass the
> result on turn it back into 0. 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@kernel.org>
> Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>
> ---
> arch/arm64/include/asm/kvm_pgtable.h | 25 ++++++++++-----
> arch/arm64/include/asm/kvm_pkvm.h | 6 +--
> arch/arm64/kvm/hyp/nvhe/mem_protect.c | 24 ++++++++----
> arch/arm64/kvm/hyp/pgtable.c | 55 +++++++++++++++++----------
> arch/arm64/kvm/mmu.c | 32 ++++++++++------
> arch/arm64/kvm/nested.c | 2 +-
> arch/arm64/kvm/pkvm.c | 22 +++++++----
> 7 files changed, 108 insertions(+), 58 deletions(-)
>
[...]
> --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> @@ -383,16 +383,20 @@ static int host_stage2_unmap_dev_all(void)
> struct kvm_pgtable *pgt = &host_mmu.pgt;
> struct memblock_region *reg;
> u64 addr = 0;
> - int i, ret;
> + int i;
> + s64 ret;
>
> /* Unmap all non-memory regions to recycle the pages */
> for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) {
> reg = &hyp_memory[i];
> ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr);
> - if (ret)
> + if (ret < 0)
> return ret;
> }
> - return kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr);
> +
> + ret = kvm_pgtable_stage2_unmap(pgt, addr, BIT(pgt->ia_bits) - addr);
> +
> + return ret < 0 ? ret : 0;
> }
>
> /*
> @@ -1418,7 +1422,7 @@ int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm)
> goto unlock;
> }
>
> - WARN_ON(kvm_pgtable_stage2_unmap(&vm->pgt, ipa, PAGE_SIZE));
> + WARN_ON(kvm_pgtable_stage2_unmap(&vm->pgt, ipa, PAGE_SIZE) < 0);
> WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST));
>
> unlock:
> @@ -1601,6 +1605,7 @@ int __pkvm_host_unshare_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
> u64 ipa = hyp_pfn_to_phys(gfn);
> u64 size, phys;
> int ret;
> + s64 reached;
>
> ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
> if (ret)
> @@ -1613,9 +1618,11 @@ int __pkvm_host_unshare_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
> if (ret)
> goto unlock;
>
> - ret = kvm_pgtable_stage2_unmap(&vm->pgt, ipa, size);
> - if (ret)
> + reached = kvm_pgtable_stage2_unmap(&vm->pgt, ipa, size);
> + if (reached < 0) {
> + ret = reached;
> goto unlock;
> + }
Could we just make ret s64 to avoid adding another variable?
ret = kvm_pgtable_stage2_unmap();
if (ret < 0)
goto unlock;
ret = 0;
>
> for_each_hyp_page(page, phys, size) {
> /* __check_host_shared_guest() protects against underflow */
> @@ -1674,6 +1681,7 @@ int __pkvm_host_wrprotect_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
> {
> u64 size, ipa = hyp_pfn_to_phys(gfn);
> int ret;
> + s64 reached;
>
> if (pkvm_hyp_vm_is_protected(vm))
> return -EPERM;
> @@ -1684,10 +1692,10 @@ int __pkvm_host_wrprotect_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
>
> assert_host_shared_guest(vm, ipa, size);
> guest_lock_component(vm);
> - ret = kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, size);
> + reached = kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, size);
> guest_unlock_component(vm);
>
> - return ret;
> + return reached < 0 ? reached : 0;
> }
>
> int __pkvm_host_test_clear_young_guest(u64 gfn, u64 nr_pages, bool mkold, struct pkvm_hyp_vm *vm)
[...]
> diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
> index 0169972..73aad80 100644
> --- a/arch/arm64/kvm/pkvm.c
> +++ b/arch/arm64/kvm/pkvm.c
> @@ -602,19 +602,24 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
> return ret;
> }
>
> -int pkvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
> +s64 pkvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size)
> {
> struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
> + int ret;
>
> if (WARN_ON(kvm_vm_is_protected(kvm)))
> return -EPERM;
>
> lockdep_assert_held_write(&kvm->mmu_lock);
>
> - return __pkvm_pgtable_stage2_unshare(pgt, addr, addr + size);
> + ret = __pkvm_pgtable_stage2_unshare(pgt, addr, addr + size);
> + if (ret)
> + return ret;
> +
> + return addr + size;
Could we make this shorter?
return __pkvm_pgtable_stage2_unshare(...) ?: addr + size;
ditto pkvm_pgtable_stage2_wrprotect().
--
Vincent
> }
>
> -int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
> +s64 pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
> {
> struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
> pkvm_handle_t handle = kvm->arch.pkvm.handle;
> @@ -632,10 +637,13 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
> break;
> }
>
> - return ret;
> + if (ret)
> + return ret;
> +
> + return addr + size;
> }
>
> -int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
> +s64 pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
> {
> struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
> struct pkvm_mapping *mapping;
> @@ -643,7 +651,7 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
> lockdep_assert_held(&kvm->mmu_lock);
>
> if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
> - return 0;
> + return addr + size;
>
> for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
> if (!mapping->nc)
> @@ -651,7 +659,7 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
> PAGE_SIZE * mapping->nr_pages);
> }
>
> - return 0;
> + return addr + size;
> }
>
> bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64 size, bool mkold)
> --
> 2.43.0
>
prev parent reply other threads:[~2026-11-17 16:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 8:02 [PATCH v3 0/1] KVM: arm64: Resume stage-2 range walks where they stopped Haseeb Ashraf
2026-11-13 8:02 ` [PATCH v3] " Haseeb Ashraf
2026-11-17 16:18 ` Vincent Donnefort [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=avx-WAgBpyjJlwhg@google.com \
--to=vdonnefort@google.com \
--cc=catalin.marinas@arm.com \
--cc=fahad.arslan@siemens.com \
--cc=fuad.tabba@linux.dev \
--cc=haseeb.ashraf@siemens.com \
--cc=haseebashraf091@gmail.com \
--cc=joey.gouly@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=qperret@google.com \
--cc=rananta@google.com \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=weilin.chang@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.