Skip to content

Commit 5107000

Browse files
author
Marc Zyngier
committed
KVM: arm64: Make KVM_CAP_MAX_VCPUS compatible with the selected GIC version
KVM_CAP_MAX_VCPUS always return the maximum possible number of VCPUs, irrespective of the selected interrupt controller. This is pretty misleading for userspace that selects a GICv2 on a GICv3 system that supports v2 compat: It always gets a maximum of 512 VCPUs, even if the effective limit is 8. The 9th VCPU will fail to be created, which is unexpected as far as userspace is concerned. Fortunately, we already have the right information stashed in the kvm structure, and we can return it as requested. Reported-by: Ard Biesheuvel <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Tested-by: Alexandru Elisei <[email protected]> Reviewed-by: Alexandru Elisei <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent c862626 commit 5107000

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

arch/arm64/kvm/arm.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
9595
return r;
9696
}
9797

98+
static int kvm_arm_default_max_vcpus(void)
99+
{
100+
return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
101+
}
102+
98103
/**
99104
* kvm_arch_init_vm - initializes a VM data structure
100105
* @kvm: pointer to the KVM struct
@@ -128,8 +133,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
128133
kvm->arch.vmid.vmid_gen = 0;
129134

130135
/* The maximum number of VCPUs is limited by the host's GIC model */
131-
kvm->arch.max_vcpus = vgic_present ?
132-
kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
136+
kvm->arch.max_vcpus = kvm_arm_default_max_vcpus();
133137

134138
return ret;
135139
out_free_stage2_pgd:
@@ -204,10 +208,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
204208
r = num_online_cpus();
205209
break;
206210
case KVM_CAP_MAX_VCPUS:
207-
r = KVM_MAX_VCPUS;
208-
break;
209211
case KVM_CAP_MAX_VCPU_ID:
210-
r = KVM_MAX_VCPU_ID;
212+
if (kvm)
213+
r = kvm->arch.max_vcpus;
214+
else
215+
r = kvm_arm_default_max_vcpus();
211216
break;
212217
case KVM_CAP_MSI_DEVID:
213218
if (!kvm)

0 commit comments

Comments
 (0)