[PATCH 15/16] KVM: arm64: selftests: Add pkvm_lifecycle teardown test
From: Fuad Tabba
Date: Wed Nov 04 2026 - 03:55:23 EST
Each protected VM holds an entry in EL2's table of KVM_MAX_PVMS (255)
VMs until teardown has reclaimed its pages, so if teardown leaks
entries, KVM_CREATE_VM eventually fails.
Create, run and free twice that many VMs, four alive at once and freed
in random order. Check that each VM's memory survives the others'
teardown, and that freeing a VM drops the process's locked memory by
what its runs added.
Signed-off-by: Fuad Tabba <fuad.tabba@xxxxxxxxx>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/arm64/pkvm_lifecycle.c | 110 ++++++++++++++++++
2 files changed, 111 insertions(+)
create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 68bdd01f62270..214f38e3f67a7 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -194,6 +194,7 @@ TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
TEST_GEN_PROGS_arm64 += arm64/pkvm_id_regs
TEST_GEN_PROGS_arm64 += arm64/pkvm_irq
TEST_GEN_PROGS_arm64 += arm64/pkvm_isolation
+TEST_GEN_PROGS_arm64 += arm64/pkvm_lifecycle
TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
TEST_GEN_PROGS_arm64 += arm64/pkvm_smccc
diff --git a/tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c b/tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c
new file mode 100644
index 0000000000000..0ee475799abc5
--- /dev/null
+++ b/tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * pkvm_lifecycle - more protected VMs than EL2's VM table holds, freed out of order.
+ *
+ * Copyright (C) 2026 Google LLC
+ * Author: Fuad Tabba <fuad.tabba@xxxxxxxxx>
+ */
+
+#include <stdlib.h>
+
+#include "kvm_util.h"
+#include "pkvm.h"
+#include "processor.h"
+#include "test_util.h"
+
+/* EL2's VM table has KVM_MAX_PVMS entries, and a VM holds one until it's freed. */
+#define VM_TABLE_SIZE 255
+/* Enough that leaking the entry of one VM in two fills the table. */
+#define NR_VMS (2 * VM_TABLE_SIZE)
+#define NR_ALIVE 4
+
+struct pvm {
+ struct kvm_vm *vm;
+ struct kvm_vcpu *vcpu;
+ u64 tag;
+ size_t locked;
+};
+
+/* Set in the guest's copy by the host before the first run. */
+static u64 tag;
+
+static void guest_code(void)
+{
+ pkvm_guest_init();
+ for (;;)
+ GUEST_SYNC(READ_ONCE(tag));
+}
+
+/* The tag comes from the guest's own memory, so the VM must survive the others' teardown. */
+static void run_vm(struct pvm *p)
+{
+ size_t locked = pkvm_host_locked_vm();
+ struct ucall uc;
+ u64 cmd;
+
+ vcpu_run(p->vcpu);
+
+ switch (cmd = get_ucall(p->vcpu, &uc)) {
+ case UCALL_SYNC:
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ default:
+ TEST_FAIL("Unexpected guest exit: ucall %lu", cmd);
+ }
+ TEST_ASSERT_EQ(uc.args[1], p->tag);
+
+ p->locked += pkvm_host_locked_vm() - locked;
+}
+
+static void create_vm(struct pvm *p, u64 tag_val)
+{
+ p->vm = pkvm_vm_create_with_one_vcpu(&p->vcpu, guest_code);
+ p->tag = tag_val;
+ p->locked = 0;
+ write_guest_global(p->vm, tag, tag_val);
+
+ run_vm(p);
+ TEST_ASSERT(p->locked, "VM %lu pinned no memory", tag_val);
+}
+
+static void free_vm(struct pvm *p)
+{
+ size_t locked = pkvm_host_locked_vm();
+
+ kvm_vm_free(p->vm);
+ TEST_ASSERT_EQ(locked - pkvm_host_locked_vm(), p->locked);
+}
+
+int main(void)
+{
+ struct pvm vms[NR_ALIVE];
+ int i, j;
+
+ TEST_REQUIRE(pkvm_supported());
+
+ ksft_print_header();
+ ksft_set_plan(1);
+
+ for (i = 0; i < NR_ALIVE; i++)
+ create_vm(&vms[i], i + 1);
+
+ for (; i < NR_VMS; i++) {
+ struct pvm *victim = &vms[random() % NR_ALIVE];
+
+ for (j = 0; j < NR_ALIVE; j++)
+ run_vm(&vms[j]);
+
+ free_vm(victim);
+ create_vm(victim, i + 1);
+ }
+
+ for (j = 0; j < NR_ALIVE; j++)
+ free_vm(&vms[j]);
+
+ ksft_test_result_pass("%d VMs, %d alive at once, freed in random order\n",
+ NR_VMS, NR_ALIVE);
+ ksft_finished();
+}
--
2.47.3