Re: [PATCH][v6] KVM: X86: support APERF/MPERF registers

From: Paolo Bonzini
Date: Fri Jun 05 2020 - 02:35:46 EST


On 05/06/20 07:00, Xiaoyao Li wrote:
> you could do
>
> ÂÂÂÂbool guest_cpuid_aperfmperf = false;
> ÂÂÂÂif (best)
> ÂÂÂÂÂÂÂ guest_cpuid_aperfmperf = !!(best->ecx & BIT(0));
>
> ÂÂÂÂif (guest_cpuid_aperfmerf != guest_has_aperfmperf(vcpu->kvm))
> ÂÂÂÂÂÂÂ return -EINVAL;
>
>
> In fact, I think we can do nothing here. Leave it as what usersapce
> wants just like how KVM treats other CPUID bits.

The reason to do it like Rongqing did is that it's suggested to take the
output of KVM_GET_SUPPORTED_CPUID and pass it down to KVM_SET_CPUID2.
Unfortunately we have KVM_GET_SUPPORTED_CPUID as a /dev/kvm (not VM)
ioctl, otherwise you could have used guest_has_aperfmperf to affect the
output of KVM_GET_SUPPORTED_CPUID.

I think it's okay however to keep it simple as you suggest. In this
case however __do_cpuid_func must not return the X86_FEATURE_APERFMPERF bit.

The guest can instead check for the availability of KVM_CAP_APERFMPERF,
which is already done in Rongqing's patch.

>> @@ -4930,6 +4939,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
>> ÂÂÂÂÂÂÂÂÂ kvm->arch.exception_payload_enabled = cap->args[0];
>> ÂÂÂÂÂÂÂÂÂ r = 0;
>> ÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂ case KVM_CAP_APERFMPERF:
>> +ÂÂÂÂÂÂÂ kvm->arch.aperfmperf_mode =
>> +ÂÂÂÂÂÂÂÂÂÂÂ boot_cpu_has(X86_FEATURE_APERFMPERF) ? cap->args[0] : 0;
>
> Shouldn't check whether cap->args[0] is a valid value?

Yes, only valid values should be allowed.

Also, it should fail with -EINVAL if the host does not have
X86_FEATURE_APERFMPERF.

Thanks,

Paolo