From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.82 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793782241; b=TceGfIMqrdgwQy0MY7Q41WKChoEvkfj/H6WlJY8jPaBUK9zOcJjJ+Arw71/QY6XiWcux RWH1Fg9nN2y2lg5oj8XenGXK6eA7Rq3JGLMLT61ONeEXFNpb2Dffr7L9aEOMp+B6eIi0z qJhdouiRBVT4+oCgzAekZUUSOiie+fw5cspaxXKZ/4MB6EiiQut3+ImSnXqbxL1S4Juyb ZdWHYgkJ8zv4iOlpDBhYlJ0wQDCHlt1MMHls7lbLuvkCmJ0Hru5pX7+vSCu1j0BwbF7Nx snNDRy3zJ4jGg45JpIej/GXqW0PAbDy8pYg33lPWqV5bjUqBbGIDbHbJBBrOp1nN/fw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793782241; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=U2XBWSBfcfQCqve1wrsykzKjay5QUHR6JzVkYSHce1U=; b=msz3ZMLhsIWYo61szyeV073FUByqgRDc5kG/cOD1S4l6ZFvPeYATrGfJH1w99klDxQhZ cgJmgDiDeViPUqNFSKeWPngYWCT6mSEqD/Kfrdj6IXEU503WuP5tzBwTA/NWl5pcj0Yo8 aLnLn5rHhX4Gu2Ldcdijnk8idxoZorLnTYVmHyOmY+hMLyb/005CAmt24FCy6kajHiCwl RivDRsLfW9dAIO9wYCVhhbTAvI2+B3C7mZ+6IISJxUMqcTVoqOW5YBDtqVWygrSwDBnBz L9oj3V++ctIRB//wiLAVLr6TBPtOBurCqBZ50+d8bOwVoQXMmHqFsTj4w+2nNqV6q9Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=nBZAeV24; arc=none smtp.remote-ip=91.218.175.82 Received: from mta0.migadu.com (out-82.mta0.migadu.com [91.218.175.82]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E079C606D802 for ; Wed, 4 Nov 2026 08:50:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E079C606D802 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=nBZAeV24 X-Envelope-To: kvm@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=p3Len8xL7Q9ENP4Ztv32/RbG11wP2iP1K8oXCC/ZeVA=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793782239; v=1; x=1794387039; b=nBZAeV24BizL3hYLqFVzpAzWVwpnQB4WFabcCIRe7chyqZnK3StyqxSMjPkeruouE9Extzxl ffqHVPU/9Na6LfPt0RrBuq9cytahn1YkUwGzeIqTfs6s82XLrv8PD3c0UegGM1VtimLBkDnMqfP d0rljpawOHfN6bS/NtQBdQnI= X-Envelope-To: kvm@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id ad119996fe61293c; Wed, 04 Nov 2026 08:50:39 +0000 X-Mizu-Trace-ID: ad119996fe61293c X-Migadu-Flow: FLOW_OUT From: Fuad Tabba To: Marc Zyngier , Oliver Upton Cc: Will Deacon , Paolo Bonzini , Sean Christopherson , Shuah Khan , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Vincent Donnefort , Quentin Perret , Fuad Tabba , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 15/16] KVM: arm64: selftests: Add pkvm_lifecycle teardown test Date: Wed, 4 Nov 2026 08:48:41 +0000 Message-ID: <20261104084842.132944-16-fuad.tabba@linux.dev> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261104084842.132944-1-fuad.tabba@linux.dev> References: <20261104084842.132944-1-fuad.tabba@linux.dev> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 + */ + +#include + +#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