From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry@kernel.org>, Kai Huang <kai.huang@intel.com>
Subject: [PATCH v4 12/30] KVM: x86: Extract get/set MSR (list) ioctl logic to helpers
Date: Fri, 12 Jun 2026 17:03:11 -0700 [thread overview]
Message-ID: <20260613000329.732085-13-seanjc@google.com> (raw)
In-Reply-To: <20260613000329.732085-1-seanjc@google.com>
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.
Reviewed-by: Yosry Ahmed <yosry@kernel.org>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
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 8f9a1572a39f..ece8d53fb7fe 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)))
+ 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)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int kvm_get_feature_msrs(struct kvm_msrs __user *user_msrs)
+{
+ return msr_io(NULL, user_msrs, do_get_feature_msr, 1);
+}
+
long kvm_arch_dev_ioctl(struct file *filp,
unsigned int ioctl, unsigned long arg)
{
@@ -4609,32 +4664,9 @@ long kvm_arch_dev_ioctl(struct file *filp,
long r;
switch (ioctl) {
- case KVM_GET_MSR_INDEX_LIST: {
- struct kvm_msr_list __user *user_msr_list = argp;
- struct kvm_msr_list msr_list;
- unsigned n;
-
- r = -EFAULT;
- if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
- goto out;
- 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)))
- goto out;
- r = -E2BIG;
- if (n < msr_list.nmsrs)
- goto out;
- r = -EFAULT;
- if (copy_to_user(user_msr_list->indices, &msrs_to_save,
- num_msrs_to_save * sizeof(u32)))
- goto out;
- if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
- &emulated_msrs,
- num_emulated_msrs * sizeof(u32)))
- goto out;
- r = 0;
+ case KVM_GET_MSR_INDEX_LIST:
+ r = kvm_get_msr_index_list(argp);
break;
- }
case KVM_GET_SUPPORTED_CPUID:
case KVM_GET_EMULATED_CPUID: {
struct kvm_cpuid2 __user *cpuid_arg = argp;
@@ -4662,30 +4694,11 @@ long kvm_arch_dev_ioctl(struct file *filp,
goto out;
r = 0;
break;
- case KVM_GET_MSR_FEATURE_INDEX_LIST: {
- struct kvm_msr_list __user *user_msr_list = argp;
- struct kvm_msr_list msr_list;
- unsigned int n;
-
- r = -EFAULT;
- if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
- goto out;
- n = msr_list.nmsrs;
- msr_list.nmsrs = num_msr_based_features;
- if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
- goto out;
- r = -E2BIG;
- if (n < msr_list.nmsrs)
- goto out;
- r = -EFAULT;
- if (copy_to_user(user_msr_list->indices, &msr_based_features,
- num_msr_based_features * sizeof(u32)))
- goto out;
- r = 0;
+ case KVM_GET_MSR_FEATURE_INDEX_LIST:
+ r = kvm_get_feature_msr_index_list(argp);
break;
- }
case KVM_GET_MSRS:
- r = msr_io(NULL, argp, do_get_feature_msr, 1);
+ r = kvm_get_feature_msrs(argp);
break;
#ifdef CONFIG_KVM_HYPERV
case KVM_GET_SUPPORTED_HV_CPUID:
@@ -5719,6 +5732,20 @@ static int kvm_get_reg_list(struct kvm_vcpu *vcpu,
return 0;
}
+static int kvm_get_msrs(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs)
+{
+ guard(srcu)(&vcpu->kvm->srcu);
+
+ return msr_io(vcpu, user_msrs, do_get_msr, 1);
+}
+
+static int kvm_set_msrs(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs)
+{
+ guard(srcu)(&vcpu->kvm->srcu);
+
+ return msr_io(vcpu, user_msrs, do_set_msr, 0);
+}
+
long kvm_arch_vcpu_ioctl(struct file *filp,
unsigned int ioctl, unsigned long arg)
{
@@ -5823,18 +5850,12 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
r = 0;
break;
}
- case KVM_GET_MSRS: {
- int idx = srcu_read_lock(&vcpu->kvm->srcu);
- r = msr_io(vcpu, argp, do_get_msr, 1);
- srcu_read_unlock(&vcpu->kvm->srcu, idx);
+ case KVM_GET_MSRS:
+ r = kvm_get_msrs(vcpu, argp);
break;
- }
- case KVM_SET_MSRS: {
- int idx = srcu_read_lock(&vcpu->kvm->srcu);
- r = msr_io(vcpu, argp, do_set_msr, 0);
- srcu_read_unlock(&vcpu->kvm->srcu, idx);
+ case KVM_SET_MSRS:
+ r = kvm_set_msrs(vcpu, argp);
break;
- }
case KVM_GET_ONE_REG:
case KVM_SET_ONE_REG:
r = kvm_get_set_one_reg(vcpu, ioctl, argp);
--
2.54.0.1136.gdb2ca164c4-goog
next prev parent reply other threads:[~2026-06-13 0:03 UTC|newest]
Thread overview: 92+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-13 0:02 [PATCH v4 00/30] KVM: x86: x86.{c,h} spring cleaning Sean Christopherson
2026-06-13 0:03 ` [PATCH v4 01/30] KVM: x86: Extract REGS and SREGS runtime sync code to helpers Sean Christopherson
2026-06-15 2:16 ` Huang, Kai
2026-06-15 5:02 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 02/30] KVM: x86: Move get_segment_base() to regs.h, as kvm_get_segment_base() Sean Christopherson
2026-06-15 2:43 ` Huang, Kai
2026-06-15 5:03 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 03/30] KVM: x86: Rename __{g,s}et_sregs2() => kvm_x86_vcpu_ioctl_{g,s}et_sregs2() Sean Christopherson
2026-06-15 2:46 ` Huang, Kai
2026-06-15 5:13 ` Binbin Wu
2026-06-15 15:58 ` Sean Christopherson
2026-06-13 0:03 ` [PATCH v4 04/30] KVM: x86: Move the bulk of register specific code from x86.c to regs.c Sean Christopherson
2026-06-15 5:25 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 05/30] KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h Sean Christopherson
2026-06-15 5:47 ` Binbin Wu
2026-06-15 16:06 ` Sean Christopherson
2026-06-13 0:03 ` [PATCH v4 06/30] KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h Sean Christopherson
2026-06-13 9:01 ` Xiaoyao Li
2026-06-15 6:49 ` Binbin Wu
2026-06-15 16:24 ` Sean Christopherson
2026-06-16 8:18 ` Xiaoyao Li
2026-06-13 0:03 ` [PATCH v4 07/30] KVM: x86: Swap the include order between x86.h and mmu.h Sean Christopherson
2026-06-15 7:26 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 08/30] KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h Sean Christopherson
2026-06-15 7:33 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 09/30] KVM: x86: Move eager_page_split to mmu.{c,h} Sean Christopherson
2026-06-15 7:49 ` Binbin Wu
2026-06-16 17:19 ` Sean Christopherson
2026-06-13 0:03 ` [PATCH v4 10/30] KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h Sean Christopherson
2026-06-15 7:52 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 11/30] KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h Sean Christopherson
2026-06-15 8:13 ` Binbin Wu
2026-06-15 16:31 ` Sean Christopherson
2026-06-13 0:03 ` Sean Christopherson [this message]
2026-06-15 8:30 ` [PATCH v4 12/30] KVM: x86: Extract get/set MSR (list) ioctl logic to helpers Binbin Wu
2026-06-13 0:03 ` [PATCH v4 13/30] KVM: x86: Expose several TSC helpers via x86.h for use by MSR code Sean Christopherson
2026-06-13 0:03 ` [PATCH v4 14/30] KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h} Sean Christopherson
2026-06-15 9:30 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 15/30] KVM: x86: Move register helper declarations from kvm_host.h => regs.h Sean Christopherson
2026-06-16 2:15 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 16/30] KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h Sean Christopherson
2026-06-16 2:19 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 17/30] KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h Sean Christopherson
2026-06-16 2:25 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 18/30] KVM: x86: Move "struct kvm_x86_msr_filter" definition to msrs.c Sean Christopherson
2026-06-15 2:47 ` Huang, Kai
2026-06-16 3:12 ` Binbin Wu
2026-06-16 6:29 ` Binbin Wu
2026-06-16 7:29 ` Huang, Kai
2026-06-16 7:43 ` Binbin Wu
2026-06-16 7:46 ` Huang, Kai
2026-06-16 16:19 ` Sean Christopherson
2026-06-16 23:30 ` Huang, Kai
2026-06-16 23:45 ` Sean Christopherson
2026-06-13 0:03 ` [PATCH v4 19/30] KVM: x86/pmu: Move "struct kvm_x86_pmu_event_filter" definition to pmu.c Sean Christopherson
2026-06-15 2:48 ` Huang, Kai
2026-06-16 3:18 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 20/30] KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h Sean Christopherson
2026-06-16 5:04 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 21/30] KVM: x86: Move LLDT assembly wrappers into VMX Sean Christopherson
2026-06-16 6:40 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 22/30] KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c Sean Christopherson
2026-06-16 6:46 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 23/30] KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h Sean Christopherson
2026-06-16 6:50 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 24/30] KVM: x86: Move IRQ-related helper declarations from kvm_host.h => irq.h Sean Christopherson
2026-06-15 11:55 ` Huang, Kai
2026-06-16 6:55 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 25/30] KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h => lapic.h Sean Christopherson
2026-06-16 6:58 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 26/30] KVM: x86: Don't treat interrupts as allowed just because a nested run is pending Sean Christopherson
2026-06-15 16:40 ` Yosry Ahmed
2026-06-15 16:43 ` Yosry Ahmed
2026-06-15 17:03 ` Sean Christopherson
2026-06-15 19:37 ` Yosry Ahmed
2026-06-15 17:26 ` Sean Christopherson
2026-06-15 19:48 ` Yosry Ahmed
2026-06-16 17:46 ` Sean Christopherson
2026-06-16 18:08 ` Yosry Ahmed
2026-06-13 0:03 ` [PATCH v4 27/30] KVM: x86: Rework kvm_arch_interrupt_allowed() into kvm_is_interrupt_allowed() Sean Christopherson
2026-06-13 0:03 ` [PATCH v4 28/30] KVM: x86/mmu: Move kvm_arch_async_page_ready() below kvm_tdp_page_fault() Sean Christopherson
2026-06-16 8:40 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 29/30] KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h => mmu.c Sean Christopherson
2026-06-16 8:48 ` Binbin Wu
2026-06-13 0:03 ` [PATCH v4 30/30] KVM: x86: Move a pile of stuff from kvm_host.h => x86.h Sean Christopherson
2026-06-15 13:01 ` Huang, Kai
2026-06-15 14:23 ` Sean Christopherson
2026-06-24 12:30 ` [PATCH v4 00/30] KVM: x86: x86.{c,h} spring cleaning Paolo Bonzini
2026-06-24 12:55 ` Sean Christopherson
2026-06-24 13:00 ` Paolo Bonzini
2026-06-24 13:02 ` Sean Christopherson
2026-06-24 14:37 ` Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260613000329.732085-13-seanjc@google.com \
--to=seanjc@google.com \
--cc=kai.huang@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=vkuznets@redhat.com \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome