Re: [PATCH] KVM: X86: Dynamically allocating MSR number lists(msrs_to_save[], emulated_msrs[], msr_based_features[])

From: Paolo Bonzini
Date: Tue Nov 05 2019 - 06:30:20 EST


On 05/11/19 10:20, Chenyi Qiang wrote:
> The three msr number lists(msrs_to_save[], emulated_msrs[] and
> msr_based_features[]) are global arrays of kvm.ko, which are
> initialized/adjusted (copy supported MSRs forward to override the
> unsupported MSRs) when installing kvm-{intel,amd}.ko, but it doesn't
> reset these three arrays to their initial value when uninstalling
> kvm-{intel,amd}.ko. Thus, at the next installation, kvm-{intel,amd}.ko
> will initialize the modified arrays with some MSRs lost and some MSRs
> duplicated.
>
> So allocate and initialize these three MSR number lists dynamically when
> installing kvm-{intel,amd}.ko and free them when uninstalling.
>
> Cc: stable@xxxxxxxxxxxxxxx
> Reviewed-by: Xiaoyao Li <xiaoyao.li@xxxxxxxxx>
> Signed-off-by: Chenyi Qiang <chenyi.qiang@xxxxxxxxx>
> ---
> arch/x86/kvm/x86.c | 86 ++++++++++++++++++++++++++++++----------------
> 1 file changed, 57 insertions(+), 29 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index ff395f812719..08efcf6351cc 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1132,13 +1132,15 @@ EXPORT_SYMBOL_GPL(kvm_rdpmc);
> * List of msr numbers which we expose to userspace through KVM_GET_MSRS
> * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
> *
> - * This list is modified at module load time to reflect the
> + * The three msr number lists(msrs_to_save, emulated_msrs, msr_based_features)
> + * are allocated and initialized at module load time and freed at unload time.
> + * msrs_to_save is selected from the msrs_to_save_all to reflect the
> * capabilities of the host cpu. This capabilities test skips MSRs that are
> - * kvm-specific. Those are put in emulated_msrs; filtering of emulated_msrs
> + * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
> * may depend on host virtualization features rather than host cpu features.
> */
>
> -static u32 msrs_to_save[] = {
> +const u32 msrs_to_save_all[] = {

This can remain static.

> MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
> MSR_STAR,
> #ifdef CONFIG_X86_64
> @@ -1179,9 +1181,10 @@ static u32 msrs_to_save[] = {
> MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
> };
>
> +static u32 *msrs_to_save;

You can use ARRAY_SIZE to allocate the destination arrays statically.

Paolo