public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Tom Lendacky <thomas.lendacky@amd.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	x86@kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Ingo Molnar <mingo@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Babu Moger <babu.moger@amd.com>
Subject: Re: [PATCH 1/2] KVM: SVM: Fix TSC_AUX virtualization setup
Date: Thu, 14 Sep 2023 14:13:01 -0700	[thread overview]
Message-ID: <ZQN3Xbi5bEqlSkY3@google.com> (raw)
In-Reply-To: <f2c0907c-9e30-e01b-7d65-a20e6be4bf49@amd.com>

On Thu, Sep 14, 2023, Tom Lendacky wrote:
> On 9/14/23 15:28, Sean Christopherson wrote:
> > On Thu, Sep 14, 2023, Tom Lendacky wrote:
> > > The checks for virtualizing TSC_AUX occur during the vCPU reset processing
> > > path. However, at the time of initial vCPU reset processing, when the vCPU
> > > is first created, not all of the guest CPUID information has been set. In
> > > this case the RDTSCP and RDPID feature support for the guest is not in
> > > place and so TSC_AUX virtualization is not established.
> > > 
> > > This continues for each vCPU created for the guest. On the first boot of
> > > an AP, vCPU reset processing is executed as a result of an APIC INIT
> > > event, this time with all of the guest CPUID information set, resulting
> > > in TSC_AUX virtualization being enabled, but only for the APs. The BSP
> > > always sees a TSC_AUX value of 0 which probably went unnoticed because,
> > > at least for Linux, the BSP TSC_AUX value is 0.
> > > 
> > > Move the TSC_AUX virtualization enablement into the vcpu_after_set_cpuid()
> > > path to allow for proper initialization of the support after the guest
> > > CPUID information has been set.
> > > 
> > > Fixes: 296d5a17e793 ("KVM: SEV-ES: Use V_TSC_AUX if available instead of RDTSC/MSR_TSC_AUX intercepts")
> > > Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
> > > ---
> > >   arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++--------
> > >   arch/x86/kvm/svm/svm.c |  3 +++
> > >   arch/x86/kvm/svm/svm.h |  1 +
> > >   3 files changed, 23 insertions(+), 8 deletions(-)
> > > 
> > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> > > index b9a0a939d59f..565c9de87c6d 100644
> > > --- a/arch/x86/kvm/svm/sev.c
> > > +++ b/arch/x86/kvm/svm/sev.c
> > > @@ -2962,6 +2962,25 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in)
> > >   				    count, in);
> > >   }
> > > +static void sev_es_init_vmcb_after_set_cpuid(struct vcpu_svm *svm)
> > 
> > I would rather name this sev_es_after_set_cpuid() and call it directly from
> > svm_vcpu_after_set_cpuid().  Or I suppose bounce through sev_after_set_cpuid(),
> > but that seems gratuitous.
> 
> There is a sev_guest() check in svm_vcpu_after_set_cpuid(), so I can move
> that into sev_vcpu_after_set_cpuid() and keep the separate
> sev_es_vcpu_after_set_cpuid().

Works for me.

> And it looks like you would prefer to not have "vcpu" in the function name?
> Might be better search-wise if vcpu remains part of the name?

Oh, that was just a typo/oversight, not intentional.

> > AFAICT, there's no point in calling this from init_vmcb(); guest_cpuid_has() is
> > guaranteed to be false when called during vCPU creation and so the intercept
> > behavior will be correct, and even if SEV-ES called init_vmcb() from
> > shutdown_interception(), which it doesn't, guest_cpuid_has() wouldn't change,
> > i.e. the intercepts wouldn't need to be changed.
> 
> Ok, I thought that's how it worked, but wasn't 100% sure. I'll move it out
> of the init_vmcb() path.
> 
> > 
> > init_vmcb_after_set_cpuid() is a special snowflake because it handles both SVM's
> > true defaults *and* guest CPUID updates.
> > 
> > > +{
> > > +	struct kvm_vcpu *vcpu = &svm->vcpu;
> > > +
> > > +	if (boot_cpu_has(X86_FEATURE_V_TSC_AUX) &&
> > > +	    (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) ||
> > > +	     guest_cpuid_has(vcpu, X86_FEATURE_RDPID))) {
> > > +		set_msr_interception(vcpu, svm->msrpm, MSR_TSC_AUX, 1, 1);
> > 
> > This needs to toggled interception back on if RDTSCP and RDPID are hidden from
> > the guest.  KVM's wonderful ABI doesn't disallow multiple calls to KVM_SET_CPUID2
> > before KVM_RUN.
> 
> Do you want that as a separate patch with the first patch purely addressing
> the current issue? Or combine them?

Hmm, now that you mention it, probably a seperate patch on top.

  reply	other threads:[~2023-09-14 21:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-14 19:50 [PATCH 0/2] SEV-ES TSC_AUX virtualization fix and optimization Tom Lendacky
2023-09-14 19:50 ` [PATCH 1/2] KVM: SVM: Fix TSC_AUX virtualization setup Tom Lendacky
2023-09-14 20:28   ` Sean Christopherson
2023-09-14 20:48     ` Tom Lendacky
2023-09-14 21:13       ` Sean Christopherson [this message]
2023-09-15 14:26         ` Tom Lendacky
2023-09-15 14:32           ` Sean Christopherson
2023-09-15 16:52       ` Tom Lendacky
2023-09-15 17:32         ` Sean Christopherson
2023-09-15 20:54           ` Tom Lendacky
2023-09-14 19:50 ` [PATCH 2/2] KVM: SVM: Do not use user return MSR support for virtualized TSC_AUX Tom Lendacky
2023-09-15 14:43   ` Sean Christopherson
2023-09-15 14:51     ` Sean Christopherson
2023-09-15 15:08       ` Tom Lendacky

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=ZQN3Xbi5bEqlSkY3@google.com \
    --to=seanjc@google.com \
    --cc=babu.moger@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@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