All of lore.kernel.org
 help / color / mirror / Atom feed
From: Xiaoyao Li <xiaoyao.li@intel.com>
To: "Huang, Kai" <kai.huang@intel.com>,
	"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>,
	"Yamahata, Isaku" <isaku.yamahata@intel.com>,
	"vaishali.thakkar@suse.com" <vaishali.thakkar@suse.com>,
	"santosh.shukla@amd.com" <santosh.shukla@amd.com>,
	"bp@alien8.de" <bp@alien8.de>
Subject: Re: [PATCH v8 2/2] KVM: SVM: Enable Secure TSC for SNP guests
Date: Tue, 8 Jul 2025 15:16:48 +0800	[thread overview]
Message-ID: <cf03633c-63ba-40b7-abd1-8cbeb4daadd9@intel.com> (raw)
In-Reply-To: <26a5d7dcc54ec434615e0cfb340ad93a429b3f90.camel@intel.com>

On 7/8/2025 10:21 AM, Huang, Kai wrote:
> On Mon, 2025-07-07 at 15:40 +0530, Nikunj A Dadhania wrote:
>> Add support for Secure TSC, allowing userspace to configure the Secure TSC
>> feature for SNP guests. Use the SNP specification's desired TSC frequency
>> parameter during the SNP_LAUNCH_START command to set the mean TSC
>> frequency in KHz for Secure TSC enabled guests.
>>
>> Always use kvm->arch.arch.default_tsc_khz as the TSC frequency that is
>> passed to SNP guests in the SNP_LAUNCH_START command.  The default value
>> is the host TSC frequency.  The userspace can optionally change the TSC
>> frequency via the KVM_SET_TSC_KHZ ioctl before calling the
>> SNP_LAUNCH_START ioctl.
>>
>> Introduce the read-only MSR GUEST_TSC_FREQ (0xc0010134) that returns
>> guest's effective frequency in MHZ when Secure TSC is enabled for SNP
>> guests. Disable interception of this MSR when Secure TSC is enabled. Note
>> that GUEST_TSC_FREQ MSR is accessible only to the guest and not from the
>> hypervisor context.
>>
>> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
> 
> This SoB isn't needed.
> 
>> Co-developed-by: Ketan Chaturvedi <Ketan.Chaturvedi@amd.com>
>> Signed-off-by: Ketan Chaturvedi <Ketan.Chaturvedi@amd.com>
>> Co-developed-by: Sean Christopherson <seanjc@google.com>
>> Signed-off-by: Nikunj A Dadhania <nikunj@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;
> 
> 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.
> 
> Even some bug results in the default_tsc_khz being 0, will the
> SNP_LAUNCH_START command catch this and return error?
> 
>> +
>> +		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 &&

It seems to need to be opt-in since it changes the ABI somehow. E.g., it 
at least works before when the VMM calls KVM_SET_TSC_KHZ at vcpu with 
the same value passed to KVM_SET_TSC_KHZ at vm. But with the above 
change, it would fail.

Well, in reality, it's OK for QEMU since QEMU explicitly doesn't call 
KVM_SET_TSC_KHZ at vcpu for TDX VMs. But I'm not sure about the impact 
on other VMMs. Considering KVM TDX support just gets in from v6.16-rc1, 
maybe it doesn't have real impact for other VMMs as well?


  parent reply	other threads:[~2025-07-08  7:17 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
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 [this message]
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=cf03633c-63ba-40b7-abd1-8cbeb4daadd9@intel.com \
    --to=xiaoyao.li@intel.com \
    --cc=bp@alien8.de \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@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 \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.