Skip to content

Commit 84168ae

Browse files
kimphillamdbp3tk0v
authored andcommitted
x86/cpu, kvm: Move X86_FEATURE_LFENCE_RDTSC to its native leaf
The LFENCE always serializing feature bit was defined as scattered LFENCE_RDTSC and its native leaf bit position open-coded for KVM. Add it to its newly added CPUID leaf 0x80000021 EAX proper. With LFENCE_RDTSC in its proper place, the kernel's set_cpu_cap() will effectively synthesize the feature for KVM going forward. Also, DE_CFG[1] doesn't need to be set on such CPUs anymore. [ bp: Massage and merge diff from Sean. ] Signed-off-by: Kim Phillips <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Acked-by: Sean Christopherson <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent a9dc9ec commit 84168ae

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
#define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */
9898
#define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */
9999
#define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */
100-
#define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) /* "" LFENCE synchronizes RDTSC */
100+
/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */
101101
#define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */
102102
#define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */
103103
#define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */
@@ -429,6 +429,7 @@
429429

430430
/* AMD-defined Extended Feature 2 EAX, CPUID level 0x80000021 (EAX), word 20 */
431431
#define X86_FEATURE_NO_NESTED_DATA_BP (20*32+ 0) /* "" No Nested Data Breakpoints */
432+
#define X86_FEATURE_LFENCE_RDTSC (20*32+ 2) /* "" LFENCE always serializing / synchronizes RDTSC */
432433

433434
/*
434435
* BUG word(s)

arch/x86/kernel/cpu/amd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ static void init_amd(struct cpuinfo_x86 *c)
956956

957957
init_amd_cacheinfo(c);
958958

959-
if (cpu_has(c, X86_FEATURE_XMM2)) {
959+
if (!cpu_has(c, X86_FEATURE_LFENCE_RDTSC) && cpu_has(c, X86_FEATURE_XMM2)) {
960960
/*
961961
* Use LFENCE for execution serialization. On families which
962962
* don't have that MSR, LFENCE is already serializing.

arch/x86/kvm/cpuid.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,22 @@ void kvm_set_cpu_caps(void)
742742
F(SME_COHERENT));
743743

744744
kvm_cpu_cap_mask(CPUID_8000_0021_EAX,
745-
F(NO_NESTED_DATA_BP) |
746-
BIT(2) /* LFENCE Always serializing */ | 0 /* SmmPgCfgLock */ |
745+
F(NO_NESTED_DATA_BP) | F(LFENCE_RDTSC) | 0 /* SmmPgCfgLock */ |
747746
BIT(6) /* NULL_SEL_CLR_BASE */ | 0 /* PrefetchCtlMsr */
748747
);
748+
749+
/*
750+
* Synthesize "LFENCE is serializing" into the AMD-defined entry in
751+
* KVM's supported CPUID if the feature is reported as supported by the
752+
* kernel. LFENCE_RDTSC was a Linux-defined synthetic feature long
753+
* before AMD joined the bandwagon, e.g. LFENCE is serializing on most
754+
* CPUs that support SSE2. On CPUs that don't support AMD's leaf,
755+
* kvm_cpu_cap_mask() will unfortunately drop the flag due to ANDing
756+
* the mask with the raw host CPUID, and reporting support in AMD's
757+
* leaf can make it easier for userspace to detect the feature.
758+
*/
749759
if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC))
750-
kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(2) /* LFENCE Always serializing */;
760+
kvm_cpu_cap_set(X86_FEATURE_LFENCE_RDTSC);
751761
if (!static_cpu_has_bug(X86_BUG_NULL_SEG))
752762
kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(6) /* NULL_SEL_CLR_BASE */;
753763
kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(9) /* NO_SMM_CTL_MSR */;

0 commit comments

Comments
 (0)