Re: [PATCH v4 5/5] KVM: SVM: Enable Secure TSC for SNP guests

Nikunj A. Dadhania
2025-03-11T09:11:24Z

On 3/10/2025 9:09 PM, Tom Lendacky wrote:
> On 3/10/25 01:45, Nikunj A Dadhania wrote:

>> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
>> index 50263b473f95..b61d6bd75b37 100644
>> --- a/arch/x86/kvm/svm/sev.c
>> +++ b/arch/x86/kvm/svm/sev.c
>> @@ -2205,6 +2205,20 @@ 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)) {
>> + u32 user_tsc_khz = params.desired_tsc_khz;
>> +
>> + /* Use tsc_khz if the VMM has not provided the TSC frequency */
>> + if (!user_tsc_khz)
>> + user_tsc_khz = tsc_khz;
>> +
>> + start.desired_tsc_khz = user_tsc_khz;
>
> Do we need to perform any sanity checking against this value?

On the higher side, sev-snp-guest.stsc-freq is u32, a Secure TSC guest boots fine with
TSC frequency set to the highest value (stsc-freq=0xFFFFFFFF).

On the lower side as MSR_AMD64_GUEST_TSC_FREQ is in MHz, TSC clock should at least be 1Mhz.
Any values below would either triggers a splat or crashes the guest kernel

For stsc-freq=1000 (1Khz), guest crash with the below:

kvm-clock: using sched offset of 4782335885 cycles
CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 6.14.0-rc5-00537-gcecc16fa7fac #254
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS unknown 02/02/2022
RIP: 0010:pvclock_tsc_khz+0x13/0x40

For stsc-freq=500000 (500KHz), boots but with the below warning: basically tsc_khz is
zero as we are reading zero from MSR_AMD64_GUEST_TSC_FREQ.

WARNING: CPU: 0 PID: 0 at arch/x86/kernel/tsc.c:1463 determine_cpu_tsc_frequencies+0x11b/0x120

Regards
Nikunj