Re: [PATCH v3 24/40] KVM: x86: Extract get/set MSR (list) ioctl logic to helpers

From: Huang, Kai

Date: Thu Jun 04 2026 - 07:25:48 EST


On Fri, 2026-05-29 at 15:22 -0700, Sean Christopherson wrote:
> Extract the code for getting/setting MSRs and MSR lists to dedicated
> helpers in anticipation of moving the MSR code to a new msrs.c.
>
> No functional change intended.
>
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>

Reviewed-by: Kai Huang <kai.huang@xxxxxxxxx>

> ---
> arch/x86/kvm/x86.c | 135 ++++++++++++++++++++++++++-------------------
> 1 file changed, 78 insertions(+), 57 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 56ccb6b77abb..64c3680d889b 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -4602,6 +4602,61 @@ static int kvm_x86_dev_has_attr(struct kvm_device_attr *attr)
> return __kvm_x86_dev_get_attr(attr, &val);
> }
>
> +static int kvm_get_msr_index_list(struct kvm_msr_list __user *user_msr_list)
> +{
> + struct kvm_msr_list msr_list;
> + unsigned int n;
> +
> + if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
> + return -EFAULT;
> +
> + n = msr_list.nmsrs;
> + msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
> + if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
> + return -EFAULT;
> +
> + if (n < msr_list.nmsrs)
> + return -E2BIG;
> +
> + if (copy_to_user(user_msr_list->indices, &msrs_to_save,
> + num_msrs_to_save * sizeof(u32)))

Nit: apparent misaligned function call argument.

> + return -EFAULT;
> +
> + if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
> + &emulated_msrs, num_emulated_msrs * sizeof(u32)))
> + return -EFAULT;
> +
> + return 0;
> +}
> +
> +static int kvm_get_feature_msr_index_list(struct kvm_msr_list __user *user_msr_list)
> +{
> + struct kvm_msr_list msr_list;
> + unsigned int n;
> +
> + if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
> + return -EFAULT;
> +
> + n = msr_list.nmsrs;
> + msr_list.nmsrs = num_msr_based_features;
> + if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
> + return -EFAULT;
> +
> + if (n < msr_list.nmsrs)
> + return -E2BIG;
> +
> + if (copy_to_user(user_msr_list->indices, &msr_based_features,
> + num_msr_based_features * sizeof(u32)))

Ditto.