[PATCH v2 00/11] KVM: arm64: Accelerate lookup of vcpus by MPIDR values (and other fixes)

Marc Zyngier maz at kernel.org
Wed Sep 20 11:17:20 PDT 2023


This is a follow-up on [1], which contains the original patches, only
augmented with a bunch of fixes after Zenghui pointed out that I was
inadvertently fixing bugs (yay!), but that there were plenty more.

The core of the issue is that we tend to confuse vcpu_id with
vcpu_idx. The first is a userspace-provided value, from which we
derive the default MPIDR_EL1 value, while the second is something that
used to represent the position in the vcpu array, and is now more of
an internal identifier.

To convince oneself that things are terminally broken, it is easy
enough to run a kvmtool guest with the following patchlet:

diff --git a/kvm-cpu.c b/kvm-cpu.c
index 1c566b3..520d759 100644
--- a/kvm-cpu.c
+++ b/kvm-cpu.c
@@ -287,7 +287,7 @@ int kvm_cpu__init(struct kvm *kvm)
                return -ENOMEM;
        }
 
-       for (i = 0; i < kvm->nrcpus; i++) {
+       for (i = kvm->nrcpus - 1; i >= 0; i--) {
                kvm->cpus[i] = kvm_cpu__arch_init(kvm, i);
                if (!kvm->cpus[i]) {
                        pr_err("unable to initialize KVM VCPU");

and witness that the resulting VM doesn't boot. The only combination
we support is to have vcpu_id == vcpu_idx. B0rken.

So this series, on top of trying to optimise SGI injection, also aims
at disambiguating this mess, and documenting some of the implicit
requirements for userspace.

With that, a VM created with vcpu upside down boots correctly.

* From v1 [1]:
  - Added a bunch of patches fixing the vcpu_id[x] ambiguity
  - Added a documentation update spelling out some extra ordering requirements
  - Collected RBs/TBs, with thanks

[1] https://lore.kernel.org/r/20230907100931.1186690-1-maz@kernel.org

Marc Zyngier (11):
  KVM: arm64: vgic: Make kvm_vgic_inject_irq() take a vcpu pointer
  KVM: arm64: vgic-its: Treat the collection target address as a vcpu_id
  KVM: arm64: vgic-v3: Refactor GICv3 SGI generation
  KVM: arm64: vgic-v2: Use cpuid from userspace as vcpu_id
  KVM: arm64: vgic: Use vcpu_idx for the debug information
  KVM: arm64: Use vcpu_idx for invalidation tracking
  KVM: arm64: Simplify kvm_vcpu_get_mpidr_aff()
  KVM: arm64: Build MPIDR to vcpu index cache at runtime
  KVM: arm64: Fast-track kvm_mpidr_to_vcpu() when mpidr_data is
    available
  KVM: arm64: vgic-v3: Optimize affinity-based SGI injection
  KVM: arm64: Clarify the ordering requirements for vcpu/RD creation

 .../virt/kvm/devices/arm-vgic-v3.rst          |   7 +
 arch/arm64/include/asm/kvm_emulate.h          |   2 +-
 arch/arm64/include/asm/kvm_host.h             |  28 ++++
 arch/arm64/kvm/arch_timer.c                   |   2 +-
 arch/arm64/kvm/arm.c                          |  90 +++++++++--
 arch/arm64/kvm/pmu-emul.c                     |   2 +-
 arch/arm64/kvm/vgic/vgic-debug.c              |   6 +-
 arch/arm64/kvm/vgic/vgic-irqfd.c              |   2 +-
 arch/arm64/kvm/vgic/vgic-its.c                |  21 ++-
 arch/arm64/kvm/vgic/vgic-kvm-device.c         |   2 +-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c            | 142 +++++++-----------
 arch/arm64/kvm/vgic/vgic.c                    |  12 +-
 include/kvm/arm_vgic.h                        |   4 +-
 13 files changed, 195 insertions(+), 125 deletions(-)

-- 
2.34.1




More information about the linux-arm-kernel mailing list