Re: [PATCH v3 3/4] x86/kvm: add max number of vcpus for hyperv emulation

From: Juergen Gross
Date: Thu Nov 18 2021 - 10:24:39 EST

On 18.11.21 15:49, Sean Christopherson wrote:
On Thu, Nov 18, 2021, Juergen Gross wrote:
On 17.11.21 21:50, Sean Christopherson wrote:
@@ -166,7 +166,7 @@ static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
struct kvm_vcpu *vcpu = NULL;
int i;
- if (vpidx >= KVM_MAX_VCPUS)
+ if (vpidx >= min(KVM_MAX_VCPUS, KVM_MAX_HYPERV_VCPUS))

IMO, this is conceptually wrong. KVM should refuse to allow Hyper-V to be enabled
if the max number of vCPUs exceeds what can be supported, or should refuse to create

TBH, I wasn't sure where to put this test. Is there a guaranteed
sequence of ioctl()s regarding vcpu creation (or setting the max
number of vcpus) and the Hyper-V enabling?

For better or worse (mostly worse), like all other things CPUID, Hyper-V is a per-vCPU
knob. If KVM can't detect the impossible condition at compile time, kvm_check_cpuid()
is probably the right place to prevent enabling Hyper-V on an unreachable vCPU.

With HYPERV_CPUID_IMPLEMENT_LIMITS already returning the
supported number of vcpus for the Hyper-V case I'm not sure
there is really more needed.

The problem I'm seeing is that the only thing I can do is to
let kvm_get_hv_cpuid() not adding the Hyper-V cpuid leaves for
vcpus > 64. I can't return a failure, because that would
probably let vcpu creation fail. And this is something we don't
want, as kvm_get_hv_cpuid() is called even in the case the guest
doesn't plan to use Hyper-V extensions.

the vCPUs. I agree it makes sense to add a Hyper-V specific limit, since there are
Hyper-V structures that have a hard limit, but detection of violations should be a
BUILD_BUG_ON, not a silent failure at runtime.

A BUILD_BUG_ON won't be possible with KVM_MAX_VCPUS being selecteble via
boot parameter.

I was thinking that there would still be a KVM-defined max that would cap whatever
comes in from userspace.

See my answers to you your other responses.


Attachment: OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature