public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Chao Gao <chao.gao@intel.com>
Cc: isaku.yamahata@intel.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com,
	Paolo Bonzini <pbonzini@redhat.com>,
	Kai Huang <kai.huang@intel.com>, Will Deacon <will@kernel.org>
Subject: Re: [PATCH v2 01/19] KVM: x86: Drop kvm_user_return_msr_cpu_online()
Date: Thu, 1 Sep 2022 14:12:56 +0000	[thread overview]
Message-ID: <YxC96HujrBAwlgK0@google.com> (raw)
In-Reply-To: <YxBDRaAyRpyz/5Q+@gao-cwp>

On Thu, Sep 01, 2022, Chao Gao wrote:
> On Tue, Aug 30, 2022 at 05:01:16AM -0700, isaku.yamahata@intel.com wrote:
> >From: Isaku Yamahata <isaku.yamahata@intel.com>
> >
> >KVM/X86 uses user return notifier to switch MSR for guest or user space.
> >Snapshot host values on CPU online, change MSR values for guest, and
> >restore them on returning to user space.  The current code abuses
> >kvm_arch_hardware_enable() which is called on kvm module initialization or
> >CPU online.
> >
> >Remove such the abuse of kvm_arch_hardware_enable by capturing the host
> >value on the first change of the MSR value to guest VM instead of CPU
> >online.
> >
> >Suggested-by: Sean Christopherson <seanjc@google.com>
> >Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> >---
> > arch/x86/kvm/x86.c | 43 ++++++++++++++++++++++++-------------------
> > 1 file changed, 24 insertions(+), 19 deletions(-)
> >
> >diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> >index 205ebdc2b11b..16104a2f7d8e 100644
> >--- a/arch/x86/kvm/x86.c
> >+++ b/arch/x86/kvm/x86.c
> >@@ -200,6 +200,7 @@ struct kvm_user_return_msrs {
> > 	struct kvm_user_return_msr_values {
> > 		u64 host;
> > 		u64 curr;
> >+		bool initialized;
> > 	} values[KVM_MAX_NR_USER_RETURN_MSRS];
> 
> The benefit of having an "initialized" state for each user return MSR on
> each CPU is small. A per-cpu state looks suffice. With it, you can keep
> kvm_user_return_msr_cpu_online() and simply call the function from
> kvm_set_user_return_msr() if initialized is false on current CPU.

Yep, a per-CPU flag is I intended.  This is the completely untested patch that's
sitting in a development branch of mine.

---
 arch/x86/kvm/x86.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index eca76f187e4b..1328326acfae 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -194,6 +194,7 @@ module_param(eager_page_split, bool, 0644);
 
 struct kvm_user_return_msrs {
 	struct user_return_notifier urn;
+	bool initialized;
 	bool registered;
 	struct kvm_user_return_msr_values {
 		u64 host;
@@ -400,18 +401,20 @@ int kvm_find_user_return_msr(u32 msr)
 	return -1;
 }
 
-static void kvm_user_return_msr_cpu_online(void)
+static void kvm_user_return_msr_init_cpu(struct kvm_user_return_msrs *msrs)
 {
-	unsigned int cpu = smp_processor_id();
-	struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
 	u64 value;
 	int i;
 
+	if (msrs->initialized)
+		return;
+
 	for (i = 0; i < kvm_nr_uret_msrs; ++i) {
 		rdmsrl_safe(kvm_uret_msrs_list[i], &value);
 		msrs->values[i].host = value;
 		msrs->values[i].curr = value;
 	}
+	msrs->initialized = true;
 }
 
 int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)
@@ -420,6 +423,8 @@ int kvm_set_user_return_msr(unsigned slot, u64 value, u64 mask)
 	struct kvm_user_return_msrs *msrs = per_cpu_ptr(user_return_msrs, cpu);
 	int err;
 
+	kvm_user_return_msr_init_cpu(msrs);
+
 	value = (value & mask) | (msrs->values[slot].host & ~mask);
 	if (value == msrs->values[slot].curr)
 		return 0;
@@ -11740,7 +11745,6 @@ int kvm_arch_hardware_enable(void)
 	u64 max_tsc = 0;
 	bool stable, backwards_tsc = false;
 
-	kvm_user_return_msr_cpu_online();
 	ret = static_call(kvm_x86_hardware_enable)();
 	if (ret != 0)
 		return ret;

base-commit: a8f21d1980fbd7e877ed174142f7f572d547e611
-- 


  reply	other threads:[~2022-09-01 14:13 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-30 12:01 [PATCH v2 00/19] KVM hardware enable/disable reorganize isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 01/19] KVM: x86: Drop kvm_user_return_msr_cpu_online() isaku.yamahata
2022-09-01  5:29   ` Chao Gao
2022-09-01 14:12     ` Sean Christopherson [this message]
2022-09-01 17:49       ` Isaku Yamahata
2022-08-30 12:01 ` [PATCH v2 02/19] KVM: x86: Use this_cpu_ptr() instead of per_cpu_ptr(smp_processor_id()) isaku.yamahata
2022-09-01  5:56   ` Chao Gao
2022-08-30 12:01 ` [PATCH v2 03/19] KVM: x86: Move check_processor_compatibility from init ops to runtime ops isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 04/19] Partially revert "KVM: Pass kvm_init()'s opaque param to additional arch funcs" isaku.yamahata
2022-08-30 22:39   ` Huang, Kai
2022-09-01 18:01     ` Isaku Yamahata
2022-08-30 12:01 ` [PATCH v2 05/19] KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section isaku.yamahata
2022-09-01  5:59   ` Chao Gao
2022-09-01  6:18   ` Chao Gao
2022-09-01 10:58     ` Huang, Kai
2022-09-01 16:52       ` Isaku Yamahata
2022-08-30 12:01 ` [PATCH v2 06/19] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 07/19] KVM: Add arch hooks for PM events with empty stub isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 08/19] KVM: x86: Move TSC fixup logic to KVM arch resume callback isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 09/19] KVM: Add arch hook when VM is added/deleted isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 10/19] KVM: Move out KVM arch PM hooks and hardware enable/disable logic isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 11/19] KVM: kvm_arch.c: Remove _nolock post fix isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 12/19] KVM: kvm_arch.c: Remove a global variable, hardware_enable_failed isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 13/19] KVM: Do processor compatibility check on cpu online and resume isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 14/19] KVM: x86: Duplicate arch callbacks related to pm events isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 15/19] KVM: Eliminate kvm_arch_post_init_vm() isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 16/19] KVM: x86: Delete kvm_arch_hardware_enable/disable() isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 17/19] KVM: Add config to not compile kvm_arch.c isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 18/19] RFC: KVM: x86: Remove cpus_hardware_enabled and related sanity check isaku.yamahata
2022-08-30 12:01 ` [PATCH v2 19/19] RFC: KVM: " isaku.yamahata

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=YxC96HujrBAwlgK0@google.com \
    --to=seanjc@google.com \
    --cc=chao.gao@intel.com \
    --cc=isaku.yamahata@gmail.com \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=will@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