]> git.itanic.dy.fi Git - linux-stable/commitdiff
KVM: SVM: Do not use user return MSR support for virtualized TSC_AUX
authorTom Lendacky <thomas.lendacky@amd.com>
Fri, 15 Sep 2023 20:54:32 +0000 (15:54 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 23 Sep 2023 09:35:49 +0000 (05:35 -0400)
When the TSC_AUX MSR is virtualized, the TSC_AUX value is swap type "B"
within the VMSA. This means that the guest value is loaded on VMRUN and
the host value is restored from the host save area on #VMEXIT.

Since the value is restored on #VMEXIT, the KVM user return MSR support
for TSC_AUX can be replaced by populating the host save area with the
current host value of TSC_AUX. And, since TSC_AUX is not changed by Linux
post-boot, the host save area can be set once in svm_hardware_enable().
This eliminates the two WRMSR instructions associated with the user return
MSR support.

Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Message-Id: <d381de38eb0ab6c9c93dda8503b72b72546053d7.1694811272.git.thomas.lendacky@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/svm/svm.c

index aef1ddf0b7055b6d3449fe433b2b5373c4790caf..9507df93f410a631b1b06acd9937bea881e7b5c9 100644 (file)
@@ -683,6 +683,21 @@ static int svm_hardware_enable(void)
 
        amd_pmu_enable_virt();
 
+       /*
+        * If TSC_AUX virtualization is supported, TSC_AUX becomes a swap type
+        * "B" field (see sev_es_prepare_switch_to_guest()) for SEV-ES guests.
+        * Since Linux does not change the value of TSC_AUX once set, prime the
+        * TSC_AUX field now to avoid a RDMSR on every vCPU run.
+        */
+       if (boot_cpu_has(X86_FEATURE_V_TSC_AUX)) {
+               struct sev_es_save_area *hostsa;
+               u32 msr_hi;
+
+               hostsa = (struct sev_es_save_area *)(page_address(sd->save_area) + 0x400);
+
+               rdmsr(MSR_TSC_AUX, hostsa->tsc_aux, msr_hi);
+       }
+
        return 0;
 }
 
@@ -1532,7 +1547,14 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
        if (tsc_scaling)
                __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
 
-       if (likely(tsc_aux_uret_slot >= 0))
+       /*
+        * TSC_AUX is always virtualized for SEV-ES guests when the feature is
+        * available. The user return MSR support is not required in this case
+        * because TSC_AUX is restored on #VMEXIT from the host save area
+        * (which has been initialized in svm_hardware_enable()).
+        */
+       if (likely(tsc_aux_uret_slot >= 0) &&
+           (!boot_cpu_has(X86_FEATURE_V_TSC_AUX) || !sev_es_guest(vcpu->kvm)))
                kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
 
        svm->guest_state_loaded = true;
@@ -3086,6 +3108,16 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
                svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
                break;
        case MSR_TSC_AUX:
+               /*
+                * TSC_AUX is always virtualized for SEV-ES guests when the
+                * feature is available. The user return MSR support is not
+                * required in this case because TSC_AUX is restored on #VMEXIT
+                * from the host save area (which has been initialized in
+                * svm_hardware_enable()).
+                */
+               if (boot_cpu_has(X86_FEATURE_V_TSC_AUX) && sev_es_guest(vcpu->kvm))
+                       break;
+
                /*
                 * TSC_AUX is usually changed only during boot and never read
                 * directly.  Intercept TSC_AUX instead of exposing it to the