KVM: arm64: pKVM is_created cleanup
cover KVM: arm64: pKVM is_created cleanup
This small series tidies up the host-side kvm->arch.pkvm.is_created flag, which tracks whether the hypervisor-side (EL2) VM has been instantiated.
It comes out of the ongoing pKVM (protected KVM) upstreaming work and runs in parallel with it. The changes only remove dead or redundant code around the flag, not any of the functional paths that work touches, so there is no dependency in either direction and the two can be applied in any order.
is_created stays: the pKVM handle is reserved early (so host MMU-notifier TLB invalidations have a valid handle before the first vCPU run), so a non-zero handle no longer implies the EL2 VM exists. is_created is what distinguishes "reserved" from "created", and the teardown path relies on it. Only the cruft around it goes.
Cheers,
/fuad
Fuad Tabba (3):
KVM: arm64: Drop the unused EL2-side is_created write
KVM: arm64: Remove unreachable early checks in pkvm_init_host_vm()
KVM: arm64: Drop redundant READ_ONCE() in pkvm_hyp_vm_is_created()
arch/arm64/kvm/hyp/nvhe/pkvm.c | 1 - arch/arm64/kvm/pkvm.c | 13 +++++-------- 2 files changed, 5 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0.1189.g8c84645362-goog
PATCH 1/3 KVM: arm64: Drop the unused EL2-side is_created write
init_pkvm_hyp_vm() sets is_created on the EL2-private VM struct, but the hypervisor never reads it: pkvm_hyp_vm_is_created() and every other consumer operate on the host's struct kvm, a distinct allocation from the EL2-private copy. The field is write-only at EL2.
Remove the store; host-side is_created tracking is unaffected.
--- arch/arm64/kvm/hyp/nvhe/pkvm.c | 1 - 1 file changed, 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -1
@@ -433,7 +433,6 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm, hyp_vm->host_kvm = host_kvm; hyp_vm->kvm.created_vcpus = nr_vcpus; hyp_vm->kvm.arch.pkvm.is_protected =READ_ONCE (host_kvm->arch.pkvm.is_protected); - hyp_vm->kvm.arch.pkvm.is_created = true; hyp_vm->kvm.arch.flags =0 ;pkvm_init_features_from_host (hyp_vm, host_kvm);
Signature · 1 line
2.54.0.1189.g8c84645362-goog
PATCH 2/3 KVM: arm64: Remove unreachable early checks in pkvm_init_host_vm()
In reply to Fuad Tabbapkvm_init_host_vm() runs once from kvm_arch_init_vm(), while the VM is still being allocated and is not yet reachable by another thread. Both early checks therefore test impossible state: is_created is still false (it is only set on first vCPU run) and the handle is still zero (this function is what reserves it). Neither branch can be taken.
Remove them.
--- arch/arm64/kvm/pkvm.c | 7 ------- 1 file changed, 7 deletions(-)
arch/arm64/kvm/pkvm.c +0 -7
@@ -230,13 +230,6 @@ int pkvm_init_host_vm(struct kvm *kvm, unsigned long type)int ret;bool protected = type &KVM_VM_TYPE_ARM_PROTECTED ; -if (pkvm_hyp_vm_is_created (kvm)) -return -EINVAL ; - -/* VM is already reserved, no need to proceed. */ -if (kvm->arch.pkvm.handle) -return 0 ; -/* Reserve the VM in hyp and obtain a hyp handle for the VM. */ ret =kvm_call_hyp_nvhe (__pkvm_reserve_vm);if (ret <0 )
Signature · 1 line
2.54.0.1189.g8c84645362-goog
PATCH 3/3 KVM: arm64: Drop redundant READ_ONCE() in pkvm_hyp_vm_is_created()
In reply to Fuad Tabbais_created is written under config_lock. Every concurrent reader is serialised against that write: pkvm_create_hyp_vm() under config_lock, and the memslot path (kvm_arch_prepare_memory_region) via slots_lock, which the creation writer also holds. The teardown-path accesses have no concurrent writer. The read is therefore serialised, and the READ_ONCE() is unnecessary.
--- arch/arm64/kvm/pkvm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/arm64/kvm/pkvm.c +5 -1
@@ -185,7 +185,11 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)bool pkvm_hyp_vm_is_created (struct kvm *kvm) { -return READ_ONCE (kvm->arch.pkvm.is_created); +/* +* Serialised by config_lock/slots_lock, or by VM lifecycle at +* teardown, so a plain read suffices. +*/ +return kvm->arch.pkvm.is_created; }int pkvm_create_hyp_vm (struct kvm *kvm)
Signature · 1 line
2.54.0.1189.g8c84645362-goog
[email protected]
AI reviewRe: [PATCH 3/3] KVM: arm64: Drop redundant READ_ONCE() in pkvm_hyp_vm_is_created()
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] Teardown sequence inversion in kvm_arch_destroy_vm() causes memory leak of protected guest pages. --
commit 3c4352fb45fd315836f7f7aa2ad63af580a902d7
Author: Fuad Tabba <[email protected]>
KVM: arm64: Drop redundant READ_ONCE() in pkvm_hyp_vm_is_created()
This patch removes an unnecessary READ_ONCE() around the read of kvm->arch.pkvm.is_created. The read is already serialised against concurrent writers by either config_lock, slots_lock, or the teardown VM lifecycle.
arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vm(struct kvm *kvm) · line 191
Quoted 13 lines
arch/arm64/kvm/pkvm.c +4
-1@@ -185,7 +185,11 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)bool pkvm_hyp_vm_is_created (struct kvm *kvm) { -return READ_ONCE (kvm->arch.pkvm.is_created); +/* +* Serialised by config_lock/slots_lock, or by VM lifecycle at +* teardown, so a plain read suffices. +*/
[Severity: High]
This isn't a bug introduced by this patch, but since the comment mentions the teardown lifecycle, is there a teardown sequence inversion in kvm_arch_destroy_vm() that leaks protected guest pages?
Looking at kvm_arch_destroy_vm() in arch/arm64/kvm/arm.c, it calls pkvm_destroy_hyp_vm() before kvm_uninit_stage2_mmu().
Because kvm_uninit_stage2_mmu() hasn't run yet, __pkvm_start_teardown_vm() is not invoked. This causes the __pkvm_finalize_teardown_vm hypercall inside pkvm_destroy_hyp_vm() to be rejected by the hypervisor, and the host clears the handle via __pkvm_destroy_hyp_vm():
arch/arm64/kvm/pkvm.c:__pkvm_destroy_hyp_vm() {
...
kvm->arch.pkvm.handle = 0;
}Subsequently, when kvm_uninit_stage2_mmu() does run, it eventually calls pkvm_pgtable_stage2_destroy_range(), which returns early due to the cleared handle:
arch/arm64/kvm/pkvm.c:pkvm_pgtable_stage2_destroy_range() {
if (!handle)
return;
...
}Does this sequence skip page reclamation and unsharing, permanently leaking all memory donated to the protected guest?
Quoted 4 lines
arch/arm64/kvm/pkvm.c +1
-0+return kvm->arch.pkvm.is_created; }int pkvm_create_hyp_vm (struct kvm *kvm)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/3] KVM: arm64: Drop redundant READ_ONCE() in pkvm_hyp_vm_is_created()
Quoted 52 lines · On Thu, 18 Jun 2026 at 10:22, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Teardown sequence inversion inkvm_arch_destroy_vm()causes memory leak of protected guest pages. --commit
3c4352fb45fd315836f7f7aa2ad63af580a902d7
Author: Fuad Tabba <[email protected]>KVM: arm64: Drop redundant
READ_ONCE()inpkvm_hyp_vm_is_created()This patch removes an unnecessary
READ_ONCE()around the read ofkvm->arch.pkvm.is_created. The read is already serialised against concurrent writers by eitherconfig_lock,slots_lock, or the teardown VM lifecycle.arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vm(struct kvm *kvm) · line 191
Quoted 13 lines
arch/arm64/kvm/pkvm.c +4
-1@@ -185,7 +185,11 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)bool pkvm_hyp_vm_is_created (struct kvm *kvm) { -return READ_ONCE (kvm->arch.pkvm.is_created); +/* +* Serialised by config_lock/slots_lock, or by VM lifecycle at +* teardown, so a plain read suffices. +*/ [Severity: High]
This isn't a bug introduced by this patch, but since the comment mentions the teardown lifecycle, is there a teardown sequence inversion inkvm_arch_destroy_vm()that leaks protected guest pages?Looking at
kvm_arch_destroy_vm()inarch/arm64/kvm/arm.c, it callspkvm_destroy_hyp_vm()beforekvm_uninit_stage2_mmu().Because
kvm_uninit_stage2_mmu()hasn't run yet,__pkvm_start_teardown_vm()is not invoked. This causes the__pkvm_finalize_teardown_vmhypercall insidepkvm_destroy_hyp_vm()to be rejected by the hypervisor, and the host clears the handle via__pkvm_destroy_hyp_vm():arch/arm64/kvm/pkvm.c:__pkvm_destroy_hyp_vm() { ... kvm->arch.pkvm.handle = 0; }Subsequently, when
kvm_uninit_stage2_mmu()does run, it eventually callspkvm_pgtable_stage2_destroy_range(), which returns early due to the cleared handle:
Quoted 8 lines
arch/arm64/kvm/pkvm.c:pkvm_pgtable_stage2_destroy_range() { if (!handle) return; ... }Does this sequence skip page reclamation and unsharing, permanently leaking all memory donated to the protected guest?
Not a leak. The reclaim runs before kvm_arch_destroy_vm(), not after it.
kvm_destroy_vm() unregisters the mmu-notifier before calling kvm_arch_destroy_vm(). That release path (kvm_flush_shadow_all -> kvm_arch_flush_shadow_all(), nested.c) calls kvm_uninit_stage2_mmu() -> pkvm_pgtable_stage2_destroy_range() while the handle is still valid, so __pkvm_start_teardown_vm() sets is_dying and the guest pages are reclaimed there.
By the time kvm_arch_destroy_vm() runs pkvm_destroy_hyp_vm(), __pkvm_finalize_teardown_vm() therefore succeeds (is_dying already set, no WARN), and the kvm_uninit_stage2_mmu() that follows is a second pass that returns early on the now-cleared handle. A pVM teardown is WARN-free in practice, which wouldn't hold if finalize were being rejected as described.
Cheers,
/fuad
Quoted 8 lines
arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vm(struct kvm *kvm) · line 195
Quoted 5 lines
arch/arm64/kvm/pkvm.c +1
-0+return kvm->arch.pkvm.is_created; }int pkvm_create_hyp_vm (struct kvm *kvm)--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Quoted 8 lines · Fuad Tabba
This small series tidies up the host-side
kvm->arch.pkvm.is_createdflag, which tracks whether the hypervisor-side (EL2) VM has been instantiated.It comes out of the ongoing pKVM (protected KVM) upstreaming work and runs in parallel with it. The changes only remove dead or redundant code around the flag, not any of the functional paths that work touches, so there is no dependency in either direction and the two can be applied in any order.
Quoted 5 lines
is_createdstays: the pKVM handle is reserved early (so host MMU-notifier TLB invalidations have a valid handle before the first vCPU run), so a non-zero handle no longer implies the EL2 VM exists.is_createdis what distinguishes "reserved" from "created", and the teardown path relies on it. Only the cruft around it goes.
For the whole series: Reviewed-by: Keir Fraser <[email protected]>
Quoted 15 lines
Cheers,
/fuadFuad Tabba (3):
KVM: arm64: Drop the unused EL2-sideis_createdwrite
KVM: arm64: Remove unreachable early checks inpkvm_init_host_vm()
KVM: arm64: Drop redundantREAD_ONCE()inpkvm_hyp_vm_is_created()arch/arm64/kvm/hyp/nvhe/pkvm.c | 1 - arch/arm64/kvm/pkvm.c | 13 +++++-------- 2 files changed, 5 insertions(+), 9 deletions(-)Signature · 1 line
2.54.0.1189.g8c84645362-goog