Commit 5130d450 authored by Fuad Tabba's avatar Fuad Tabba Committed by Marc Zyngier
Browse files

KVM: arm64: Seed pkvm_ownership_selftest vcpu memcache



The hypercall handlers call pkvm_refill_memcache() to top up the
hyp_vcpu memcache before invoking __pkvm_host_{share,donate}_guest().
pkvm_ownership_selftest invokes those functions directly with a
static selftest_vcpu that has an empty memcache.

Seed selftest_vcpu's memcache from the prepopulated selftest
pages, leaving the remainder for selftest_vm.pool. Required by
the memcache-sufficiency pre-check added in the following
patches.

Assisted-by: Gemini:gemini-3.1-pro review-prompts
Signed-off-by: default avatarFuad Tabba <tabba@google.com>
Link: https://patch.msgid.link/20260501112149.2824881-5-tabba@google.com


Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
parent d4d215e5
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -752,17 +752,31 @@ static struct pkvm_hyp_vcpu selftest_vcpu = {
struct pkvm_hyp_vcpu *init_selftest_vm(void *virt)
{
	struct hyp_page *p = hyp_virt_to_page(virt);
	unsigned long min_pages, seeded = 0;
	int i;

	selftest_vm.kvm.arch.mmu.vtcr = host_mmu.arch.mmu.vtcr;
	WARN_ON(kvm_guest_prepare_stage2(&selftest_vm, virt));

	/*
	 * Mirror pkvm_refill_memcache() for the share/donate pre-checks;
	 * the selftest invokes those functions directly and would
	 * otherwise see an empty memcache.
	 */
	min_pages = kvm_mmu_cache_min_pages(&selftest_vm.kvm.arch.mmu);

	for (i = 0; i < pkvm_selftest_pages(); i++) {
		if (p[i].refcount)
			continue;
		p[i].refcount = 1;
		if (seeded < min_pages) {
			push_hyp_memcache(&selftest_vcpu.vcpu.arch.pkvm_memcache,
					  hyp_page_to_virt(&p[i]), hyp_virt_to_phys);
			seeded++;
		} else {
			hyp_put_page(&selftest_vm.pool, hyp_page_to_virt(&p[i]));
		}
	}

	selftest_vm.kvm.arch.pkvm.handle = __pkvm_reserve_vm();
	insert_vm_table_entry(selftest_vm.kvm.arch.pkvm.handle, &selftest_vm);