mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Vitaly Kuznetsov <vkuznets@redhat.com>,
	David Woodhouse <dwmw2@infradead.org>,
	Paul Durrant <paul@xen.org>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Yosry Ahmed <yosry@kernel.org>,
	Binbin Wu <binbin.wu@linux.intel.com>,
	 David Woodhouse <dwmw@amazon.co.uk>,
	Kai Huang <kai.huang@intel.com>
Subject: [PATCH v3 24/40] KVM: x86: Extract get/set MSR (list) ioctl logic to helpers
Date: Fri, 29 May 2026 15:22:07 -0700	[thread overview]
Message-ID: <20260529222223.870923-25-seanjc@google.com> (raw)
In-Reply-To: <20260529222223.870923-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.

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 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)))
+		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.823.g6e5bcc1fc9-goog


  parent reply	other threads:[~2026-05-29 22:23 UTC|newest]

Thread overview: 119+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-29 22:21 [PATCH v3 00/40] KVM: x86: GPR accessors and x86.{c,h} spring cleaning Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 01/40] KVM: SVM: Truncate INVLPGA address in compatibility mode Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 02/40] KVM: x86/xen: Bug the VM if 32-bit KVM observes a 64-bit mode hypercall Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 03/40] KVM: x86/xen: Don't truncate RAX when handling hypercall from protected guest Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 04/40] KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of 64-bit mode Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 05/40] KVM: x86: Trace hypercall register *after* truncating values for 32-bit Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 06/40] KVM: x86: Rename kvm_cache_regs.h => regs.h Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 07/40] KVM: x86: Move inlined GPR, CR, and DR helpers from x86.h to regs.h Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 08/40] KVM: x86: Add mode-aware versions of kvm_<reg>_{read,write}() helpers Sean Christopherson
2026-06-03 11:15   ` Huang, Kai
2026-05-29 22:21 ` [PATCH v3 09/40] KVM: x86: Drop non-raw kvm_<reg>_write() helpers Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 10/40] KVM: nSVM: Use kvm_rax_read() now that it's mode-aware Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 11/40] Revert "KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of 64-bit mode" Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 12/40] KVM: x86: Harden is_64_bit_hypercall() against bugs on 32-bit kernels Sean Christopherson
2026-05-29 22:21 ` [PATCH v3 13/40] KVM: x86: Move update_cr8_intercept() to lapic.c Sean Christopherson
2026-05-30  0:35   ` Yosry Ahmed
2026-06-03 11:16   ` Huang, Kai
2026-05-29 22:21 ` [PATCH v3 14/40] KVM: x86: Move async #PF helpers to x86.h (as inlines) Sean Christopherson
2026-05-30  0:36   ` Yosry Ahmed
2026-05-30  0:39     ` Sean Christopherson
2026-05-30  0:45       ` Yosry Ahmed
2026-06-03 11:18   ` Huang, Kai
2026-05-29 22:21 ` [PATCH v3 15/40] KVM: x86: Move the bulk of register specific code from x86.c to regs.c Sean Christopherson
2026-05-30  0:43   ` Yosry Ahmed
2026-06-01 14:15     ` Sean Christopherson
2026-06-01 23:35       ` Yosry Ahmed
2026-06-03 11:33   ` Huang, Kai
2026-05-29 22:21 ` [PATCH v3 16/40] KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h Sean Christopherson
2026-05-30  0:37   ` Yosry Ahmed
2026-06-03 11:40   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 17/40] KVM: x86: Drop defunct vcpu_tsc_khz() declaration Sean Christopherson
2026-05-30  0:45   ` Yosry Ahmed
2026-06-03 11:41   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 18/40] KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h Sean Christopherson
2026-05-30  0:46   ` Yosry Ahmed
2026-06-03 11:51   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 19/40] KVM: x86: Swap the include order between x86.h and mmu.h Sean Christopherson
2026-05-30  0:48   ` Yosry Ahmed
2026-06-01 14:55     ` Sean Christopherson
2026-06-01 20:27       ` Yosry Ahmed
2026-06-01 21:19         ` Sean Christopherson
2026-06-03 11:53   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 20/40] KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h Sean Christopherson
2026-05-30  0:51   ` Yosry Ahmed
2026-06-03 11:56   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 21/40] KVM: x86: Move eager_page_split to mmu.{c,h} Sean Christopherson
2026-05-30  0:51   ` Yosry Ahmed
2026-06-03 11:59   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 22/40] KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h Sean Christopherson
2026-06-04 11:00   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 23/40] KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h Sean Christopherson
2026-05-30  0:52   ` Yosry Ahmed
2026-06-04 11:08   ` Huang, Kai
2026-06-05 19:14     ` Sean Christopherson
2026-05-29 22:22 ` Sean Christopherson [this message]
2026-05-30  0:55   ` [PATCH v3 24/40] KVM: x86: Extract get/set MSR (list) ioctl logic to helpers Yosry Ahmed
2026-06-04 11:18   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 25/40] KVM: x86: Expose several TSC helpers via x86.h for use by MSR code Sean Christopherson
2026-06-04 11:50   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 26/40] KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h} Sean Christopherson
2026-06-04 12:03   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 27/40] KVM: x86: Move register helper declarations from kvm_host.h => regs.h Sean Christopherson
2026-05-30  0:56   ` Yosry Ahmed
2026-06-01 14:24     ` Sean Christopherson
2026-06-01 23:36       ` Yosry Ahmed
2026-06-04 12:38   ` Huang, Kai
2026-06-05 19:13     ` Sean Christopherson
2026-05-29 22:22 ` [PATCH v3 28/40] KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h Sean Christopherson
2026-05-30  0:57   ` Yosry Ahmed
2026-06-04 12:39   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 29/40] KVM: x86: Remove defunct kvm_load_segment_descriptor() declaration Sean Christopherson
2026-05-30  0:57   ` Yosry Ahmed
2026-06-04 12:39   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 30/40] KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h Sean Christopherson
2026-05-30  0:59   ` Yosry Ahmed
2026-06-01 14:50     ` Sean Christopherson
2026-06-01 23:38       ` Yosry Ahmed
2026-06-05  6:53   ` Huang, Kai
2026-06-05 20:36     ` Sean Christopherson
2026-05-29 22:22 ` [PATCH v3 31/40] KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h Sean Christopherson
2026-05-30  0:59   ` Yosry Ahmed
2026-06-05  7:02   ` Huang, Kai
2026-06-05  7:50   ` Huang, Kai
2026-06-05 20:03     ` Sean Christopherson
2026-06-05 20:55       ` Sean Christopherson
2026-06-08 11:15         ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 32/40] KVM: x86: Move LLDT assembly wrappers into VMX Sean Christopherson
2026-05-30  1:02   ` Yosry Ahmed
2026-06-01 15:17     ` Sean Christopherson
2026-06-01 23:41       ` Yosry Ahmed
2026-05-29 22:22 ` [PATCH v3 33/40] KVM: x86: Move kvm_cpu_get_apicid() from kvm_host.h => avic.c Sean Christopherson
2026-05-30  1:03   ` Yosry Ahmed
2026-06-05  7:13   ` Huang, Kai
2026-06-05 20:29     ` Sean Christopherson
2026-05-29 22:22 ` [PATCH v3 34/40] KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c Sean Christopherson
2026-05-30  1:05   ` Yosry Ahmed
2026-06-05  7:45   ` Huang, Kai
2026-06-05 20:32     ` Sean Christopherson
2026-06-08 11:17       ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 35/40] KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h Sean Christopherson
2026-05-30  1:06   ` Yosry Ahmed
2026-06-05  7:53   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 36/40] KVM: x86: Move IRQ-related helper declarations from kvm_host.h => irq.h Sean Christopherson
2026-05-30  1:10   ` Yosry Ahmed
2026-06-01 15:22     ` Sean Christopherson
2026-06-01 23:44       ` Yosry Ahmed
2026-05-29 22:22 ` [PATCH v3 37/40] KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h => lapic.h Sean Christopherson
2026-05-30  1:11   ` Yosry Ahmed
2026-06-05  7:58   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 38/40] KVM: x86/mmu: Move kvm_arch_async_page_ready() below kvm_tdp_page_fault() Sean Christopherson
2026-05-30  1:12   ` Yosry Ahmed
2026-06-05  7:59   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 39/40] KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h => mmu.c Sean Christopherson
2026-05-30  1:13   ` Yosry Ahmed
2026-06-05  8:03   ` Huang, Kai
2026-05-29 22:22 ` [PATCH v3 40/40] KVM: x86: Move a pile of stuff from kvm_host.h => x86.h Sean Christopherson
2026-05-30 16:59 ` [PATCH v3 00/40] KVM: x86: GPR accessors and x86.{c,h} spring cleaning Paolo Bonzini
2026-06-03 12:58   ` Sean Christopherson
2026-06-05 18:31 ` 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=20260529222223.870923-25-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=binbin.wu@linux.intel.com \
    --cc=dwmw2@infradead.org \
    --cc=dwmw@amazon.co.uk \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paul@xen.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

all inboxes | Powered by JetHome®