kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Huang, Kai" <kai.huang@intel.com>
To: "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"pbonzini@redhat.com" <pbonzini@redhat.com>,
	"seanjc@google.com" <seanjc@google.com>,
	"nikunj@amd.com" <nikunj@amd.com>
Cc: "thomas.lendacky@amd.com" <thomas.lendacky@amd.com>,
	"vaishali.thakkar@suse.com" <vaishali.thakkar@suse.com>,
	"Li, Xiaoyao" <xiaoyao.li@intel.com>,
	"bp@alien8.de" <bp@alien8.de>,
	"Yamahata, Isaku" <isaku.yamahata@intel.com>,
	"santosh.shukla@amd.com" <santosh.shukla@amd.com>
Subject: Re: [PATCH v8 2/2] KVM: SVM: Enable Secure TSC for SNP guests
Date: Tue, 8 Jul 2025 10:48:00 +0000	[thread overview]
Message-ID: <fec4e8dd2d015ec6a37a852f6d7bcf815d538fdc.camel@intel.com> (raw)
In-Reply-To: <57a2933e-34c3-4313-b75a-68659d117b14@amd.com>


> > 
> > > @@ -2146,6 +2158,14 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
> > >  
> > >  	start.gctx_paddr = __psp_pa(sev->snp_context);
> > >  	start.policy = params.policy;
> > > +
> > > +	if (snp_secure_tsc_enabled(kvm)) {
> > > +		if (!kvm->arch.default_tsc_khz)
> > > +			return -EINVAL;
> > 
> > Here snp_context_create() has been called successfully therefore IIUC you
> > need to use
> > 
> > 		goto e_free_context;
> 
> Ack.
> 
> > 
> > instead.
> > 
> > Btw, IIUC it shouldn't be possible for the kvm->arch.default_tsc_khz to be
> > 0.  Perhaps we can just remove the check.
> 
> I will keep this check and correct the goto.
> 
> > 
> > Even some bug results in the default_tsc_khz being 0, will the
> > SNP_LAUNCH_START command catch this and return error?
> 
> No, that is an invalid configuration, desired_tsc_khz is set to 0 when
> SecureTSC is disabled. If SecureTSC is enabled, desired_tsc_khz should
> have correct value.

So it's an invalid configuration that when Secure TSC is enabled and
desired_tsc_khz is 0.  Assuming the SNP_LAUNCH_START will return an error
if such configuration is used, wouldn't it be simpler if you remove the
above check and depend on the SNP_LAUNCH_START command to catch the
invalid configuration?

Anyway, no problem to me if you have the check.  I just thought w/o check
the code will be simpler and you can still get what you want (supposedly).

> 
> > 
> > > +
> > > +		start.desired_tsc_khz = kvm->arch.default_tsc_khz;
> > > +	}
> > > +
> > >  	memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw));
> > >  	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error);
> > >  	if (rc) {
> > > @@ -2386,7 +2406,9 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
> > >  			return ret;
> > >  		}
> > >  
> > > -		svm->vcpu.arch.guest_state_protected = true;
> > > +		vcpu->arch.guest_state_protected = true;
> > > +		vcpu->arch.guest_tsc_protected = snp_secure_tsc_enabled(kvm);
> > > +
> > 
> > + Xiaoyao.
> > 
> > The KVM_SET_TSC_KHZ can also be a vCPU ioctl (in fact, the support of VM
> > ioctl of it was added later).  I am wondering whether we should reject
> > this vCPU ioctl for TSC protected guests, like:
> > 
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 2806f7104295..699ca5e74bba 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -6186,6 +6186,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
> >                 u32 user_tsc_khz;
> >  
> >                 r = -EINVAL;
> > +
> > +               if (vcpu->arch.guest_tsc_protected)
> > +                       goto out;
> > +
> >                 user_tsc_khz = (u32)arg;
> >  
> >                 if (kvm_caps.has_tsc_control &&
> 
> Ack, more below.
> 
> > 
> > TDX doesn't do this either, but TDX has its own version for TSC related
> > kvm_x86_ops callbacks:
> > 
> >         .get_l2_tsc_offset = vt_op(get_l2_tsc_offset),                   
> >         .get_l2_tsc_multiplier = vt_op(get_l2_tsc_multiplier),           
> >         .write_tsc_offset = vt_op(write_tsc_offset),                     
> >         .write_tsc_multiplier = vt_op(write_tsc_multiplier),
> > 
> > which basically ignore the operations for TDX guests, so no harm even
> > KVM_SET_TSC_KHZ ioctl is called for vCPU I suppose.
> > 
> > But IIRC, for AMD side they just use default version of SVM guests thus
> > SEV/SNP guests are not ignored:
> > 
> >         .get_l2_tsc_offset = svm_get_l2_tsc_offset,
> >         .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier,
> >         .write_tsc_offset = svm_write_tsc_offset,
> >         .write_tsc_multiplier = svm_write_tsc_multiplier,
> > 
> > So I am not sure whether there will be problem here.
> 
> For the guest, changing TSC frequency after SNP_LAUNCH_START will not
> matter. As the guest TSC frequency cannot be changed after that.

But will there any side effect if doing so?  E.g., what if
svm_write_tsc_offset()/svm_write_tsc_multiplier() are called for vCPU for
SNP guest?

> 
> > Anyway, conceptually, I think we should just reject the KVM_SET_TSC_KHZ
> > vCPU ioctl for TSC protected guests.
> > 
> > However, it seems for SEV/SNP the setting of guest_state_protected and
> > guest_tsc_protected is done at a rather late time in
> > snp_launch_update_vmsa() as shown in this patch.  This means checking of
> > guest_tsc_protected won't work if KVM_SET_TSC_KHZ is called at earlier
> > time.
> > > TDX sets those two at early time when initializing the VM.  I think the
> > SEV/SNP guests should do the same.
> 
> Setting of guest_state_protected is correct as it is part of
> LAUNCH_UPDATE_VMSA, from this point onward the guest state is protected.

Oh I made a mistake that TDX sets them when initializing the VM.  They are
actually set when vCPU is created at which point you already know the VM
type thus already know the vCPU is for TDX guest.

I thought the same logic could be applied to vCPU of SNP guest?

> 
> For guest_tsc_protected, vCPUs are created after SNP_LAUNCH_START, so setting
> vcpu->arch.guest_tsc_protected there is not possible. We might need to have a
> kvm->arch.guest_tsc_protected which can be set and then percolate it to
> vcpu->arch.guest_tsc_protected when vCPUs are created, comments?

See above.  I think we can set vcpu->arch.guest_tsc_protected when
creating the vCPU, at which point you already know the VM type.

  reply	other threads:[~2025-07-08 10:48 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-07 10:10 [PATCH v8 0/2] Enable Secure TSC for SEV-SNP Nikunj A Dadhania
2025-07-07 10:10 ` [PATCH v8 1/2] x86/cpufeatures: Add SNP Secure TSC Nikunj A Dadhania
2025-07-07 10:10 ` [PATCH v8 2/2] KVM: SVM: Enable Secure TSC for SNP guests Nikunj A Dadhania
2025-07-07 13:34   ` Tom Lendacky
2025-07-08  2:21   ` Huang, Kai
2025-07-08  6:45     ` Nikunj A. Dadhania
2025-07-08 10:48       ` Huang, Kai [this message]
2025-07-08 14:34         ` Sean Christopherson
2025-07-08 22:42           ` Huang, Kai
2025-07-09  4:14           ` Nikunj A. Dadhania
2025-07-08  7:16     ` Xiaoyao Li
2025-07-08 10:53       ` Huang, Kai
2025-07-08 14:42         ` Sean Christopherson
2025-07-08 22:56           ` Huang, Kai
2025-07-08 23:08             ` Sean Christopherson
2025-07-09  5:54               ` Huang, Kai
2025-07-08 14:37   ` Sean Christopherson
2025-07-09  4:12     ` Nikunj A. Dadhania
2025-07-09 13:02       ` Sean Christopherson
2025-07-10 10:59         ` Nikunj A Dadhania
2025-07-10 13:20           ` Sean Christopherson
2025-07-10 15:04             ` Nikunj A. Dadhania
2025-07-10 23:30               ` 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=fec4e8dd2d015ec6a37a852f6d7bcf815d538fdc.camel@intel.com \
    --to=kai.huang@intel.com \
    --cc=bp@alien8.de \
    --cc=isaku.yamahata@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=santosh.shukla@amd.com \
    --cc=seanjc@google.com \
    --cc=thomas.lendacky@amd.com \
    --cc=vaishali.thakkar@suse.com \
    --cc=xiaoyao.li@intel.com \
    /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;
as well as URLs for NNTP newsgroup(s).