[RFC PATCH 3/6] KVM: arm64: Add kvm_arm_vmid_nr_usable()

From: Nathan Chen

Date: Tue Nov 24 2026 - 16:12:40 EST


Growing the shadow stage-2 MMU pool of nested VMs needs a host-wide
limit derived from how many VMIDs the allocator can hand out within a
generation, before a rollover.

That is fewer than NUM_USER_VMIDS: VMID #0 is never allocated, and a
rollover can keep one VMID reserved per CPU for the VMID active on it.
Add kvm_arm_vmid_nr_usable() to return that number, so that the limit
is computed next to the allocator that sets it.

Signed-off-by: Nathan Chen <nathanc@xxxxxxxxxx>
---
arch/arm64/include/asm/kvm_host.h | 1 +
arch/arm64/kvm/vmid.c | 12 ++++++++++++
2 files changed, 13 insertions(+)

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index c7133999296f..6ae86bc96600 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -1431,6 +1431,7 @@ int __init kvm_arm_vmid_alloc_init(void);
void __init kvm_arm_vmid_alloc_free(void);
void kvm_arm_vmid_update(struct kvm_vmid *kvm_vmid);
void kvm_arm_vmid_clear_active(void);
+unsigned int kvm_arm_vmid_nr_usable(void);

static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch)
{
diff --git a/arch/arm64/kvm/vmid.c b/arch/arm64/kvm/vmid.c
index 7fe8ba1a2851..cf044f434531 100644
--- a/arch/arm64/kvm/vmid.c
+++ b/arch/arm64/kvm/vmid.c
@@ -129,6 +129,18 @@ static u64 new_vmid(struct kvm_vmid *kvm_vmid)
return vmid;
}

+/*
+ * Number of VMIDs that can be handed out within a generation: VMID #0
+ * is never allocated, and a rollover can keep one VMID reserved per CPU.
+ */
+unsigned int kvm_arm_vmid_nr_usable(void)
+{
+ if (NUM_USER_VMIDS - 1 <= num_possible_cpus())
+ return 0;
+
+ return NUM_USER_VMIDS - 1 - num_possible_cpus();
+}
+
/* Called from vCPU sched out with preemption disabled */
void kvm_arm_vmid_clear_active(void)
{
--
2.43.0