The kernel rejects KVM_CREATE_IRQCHIP with -EINVAL if any vcpus have already been created, but the API documentation does not mention this requirement. Add a note. Signed-off-by: Tim Wiederhake --- Documentation/virt/kvm/api.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 52bbbb553ce1..ec5bf99ff8b8 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -856,7 +856,8 @@ Writes the floating point state to the vcpu. Creates an interrupt controller model in the kernel. On x86, creates a virtual ioapic, a virtual PIC (two PICs, nested), and sets up future vcpus to have a local APIC. IRQ routing for GSIs 0-15 is set to both -PIC and IOAPIC; GSI 16-23 only go to the IOAPIC. +PIC and IOAPIC; GSI 16-23 only go to the IOAPIC. This ioctl must be called +before creating any vcpus. On arm64, a GICv2 is created. Any other GIC versions require the usage of KVM_CREATE_DEVICE, which also supports creating a GICv2. Using KVM_CREATE_DEVICE is preferred over KVM_CREATE_IRQCHIP for GICv2. -- 2.52.0 When an in-kernel irqchip is enabled, vcpu creation installs a private 4 KiB memory slot at the default APIC base address (0xfee00000). If a user memory region overlaps this slot, vcpu creation fails with EEXIST. The same error occurs when installing an overlapping user memory region after vcpu creation. This constraint is not documented anywhere. Add a note to the KVM_CREATE_IRQCHIP documentation. Signed-off-by: Tim Wiederhake --- Documentation/virt/kvm/api.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index ec5bf99ff8b8..da8beb80699a 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -863,6 +863,11 @@ KVM_CREATE_DEVICE, which also supports creating a GICv2. Using KVM_CREATE_DEVICE is preferred over KVM_CREATE_IRQCHIP for GICv2. On s390, a dummy irq routing table is created. +On x86, when an in-kernel irqchip is enabled, KVM reserves a private memory +slot for the local APIC at the default APIC base address (0xfee00000). User +space must not map guest memory that covers this address and must leave a 4 KiB +hole in the guest physical memory map at this address. + Note that on s390 the KVM_CAP_S390_IRQCHIP vm capability needs to be enabled before KVM_CREATE_IRQCHIP can be used. -- 2.52.0 User space must not map guest memory that covers the default APIC base address (0xfee00000) when using an in-kernel irqchip. Currently, KVM_CREATE_IRQCHIP does not check for this. The violation goes unnoticed until vcpu creation, which fails with the misleading error -EEXIST. Add an explicit check to KVM_CREATE_IRQCHIP so the conflict is reported at the point where the constraint takes effect. Signed-off-by: Tim Wiederhake --- arch/x86/kvm/x86.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0550359ed798..07f348232a72 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7336,6 +7336,10 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) if (kvm->created_vcpus) goto create_irqchip_unlock; + r = -EEXIST; + if (gfn_to_memslot(kvm, gpa_to_gfn(APIC_DEFAULT_PHYS_BASE))) + goto create_irqchip_unlock; + r = kvm_pic_init(kvm); if (r) goto create_irqchip_unlock; -- 2.52.0 User space must not map guest memory that covers the default APIC base address (0xfee00000) when using an in-kernel irqchip. Currently, KVM_SET_USER_MEMORY_REGION does not check for this. The violation goes unnoticed until vcpu creation, which fails with the misleading error -EEXIST. Add an explicit check to kvm_arch_prepare_memory_region() so the conflict is reported at the point where it is introduced. Signed-off-by: Tim Wiederhake --- arch/x86/kvm/x86.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 07f348232a72..b38d4355137a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13684,12 +13684,19 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, return -EINVAL; if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) { + const gfn_t apic_gfn = gpa_to_gfn(APIC_DEFAULT_PHYS_BASE); + if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn()) return -EINVAL; if (kvm_is_gfn_alias(kvm, new->base_gfn + new->npages - 1)) return -EINVAL; + if (irqchip_in_kernel(kvm) && + new->base_gfn <= apic_gfn && + apic_gfn < new->base_gfn + new->npages) + return -EEXIST; + return kvm_alloc_memslot_metadata(kvm, new); } -- 2.52.0