Skip to contentSkip to conversation

x86/cpu: RDPID+MSR_TSC_AUX fix and a cleanup

PATCH Applied Sean Christopherson 2 participants 5 messages May 4, 2021 – May 6, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86/cpu: RDPID+MSR_TSC_AUX fix and a cleanup

Fix a bug where MSR_TSC_AUX is left uninitialized in the theoretically possible scenario where RDPID is supported by RDTSCP is not. AFAIK, no such CPUs exits, but both Intel and AMD architecturally allow RDPID and MSR_TSC_AUX to exist without RDTSCP.

Found by inspection when cleaning up a similar mess in KVM. Confirmed the bug and verified the fix by running the problematic CPU model under KVM (once KVM's even bigger trainwreck was fixed).

Sean Christopherson (2):
x86/cpu: Initialize MSR_TSC_AUX if RDTSCP *or* RDPID is supported x86/cpu: Remove write_tsc() and write_rdtscp_aux() wrappers

 arch/x86/include/asm/msr.h   | 4 ----
 arch/x86/kernel/cpu/common.c | 4 ++--
 2 files changed, 2 insertions(+), 6 deletions(-)
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#1 lore raw

PATCH 1/2 x86/cpu: Initialize MSR_TSC_AUX if RDTSCP *or* RDPID is supported

Initialize MSR_TSC_AUX with CPU node information if RDTSCP or RDPID is supported. This fixes a bug where vdso_read_cpunode() will read garbage via RDPID if RDPID is supported but RDTSCP is not. While no known CPU supports RDPID but not RDTSCP, both Intel's SDM and AMD's APM allow for RDPID to exist without RDTSCP, e.g. it's technically a legal CPU model for a virtual machine.

Note, technically MSR_TSC_AUX could be initialized if and only if RDPID is supported since RDTSCP is currently not used to retrieve the CPU node. But, the cost of the superfluous WRMSR is negigible, whereas leaving MSR_TSC_AUX uninitialized is just asking for future breakage if someone decides to utilize RDTSCP.

Fixes a582c540ac1b ("x86/vdso: Use RDPID in preference to LSL when available")
Cc Reiji Watanabe <[email protected]>
Cc Jim Mattson <[email protected]>
Cc Andy Lutomirski <[email protected]>
---
 arch/x86/kernel/cpu/common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/common.c +1 -1
index ab640abe26b6..1e576cc831c1 100644
@@ -1850,7 +1850,7 @@ static inline void setup_getcpu(int cpu)
 	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
 	struct desc_struct d = { };
 
-	if (boot_cpu_has(X86_FEATURE_RDTSCP))
+	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
 		write_rdtscp_aux(cpudata);
 
 	/* Store CPU and node number in limit. */
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#2 lore raw

[tip: x86/urgent] x86/cpu: Initialize MSR_TSC_AUX if RDTSCP *or* RDPID is supported

The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     b6b4fbd90b155a0025223df2c137af8a701d53b3
Gitweb:        https://git.kernel.org/tip/b6b4fbd90b155a0025223df2c137af8a701d53b3
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Tue, 04 May 2021 15:56:31 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 05 May 2021 21:50:14 +02:00

x86/cpu: Initialize MSR_TSC_AUX if RDTSCP *or* RDPID is supported

Initialize MSR_TSC_AUX with CPU node information if RDTSCP or RDPID is supported. This fixes a bug where vdso_read_cpunode() will read garbage via RDPID if RDPID is supported but RDTSCP is not. While no known CPU supports RDPID but not RDTSCP, both Intel's SDM and AMD's APM allow for RDPID to exist without RDTSCP, e.g. it's technically a legal CPU model for a virtual machine.

Note, technically MSR_TSC_AUX could be initialized if and only if RDPID is supported since RDTSCP is currently not used to retrieve the CPU node. But, the cost of the superfluous WRMSR is negigible, whereas leaving MSR_TSC_AUX uninitialized is just asking for future breakage if someone decides to utilize RDTSCP.

Fixes a582c540ac1b ("x86/vdso: Use RDPID in preference to LSL when available")
---
 arch/x86/kernel/cpu/common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/common.c +1 -1
index 6bdb69a..490bed0 100644
@@ -1851,7 +1851,7 @@ static inline void setup_getcpu(int cpu)
 	unsigned long cpudata = vdso_encode_cpunode(cpu, early_cpu_to_node(cpu));
 	struct desc_struct d = { };
 
-	if (boot_cpu_has(X86_FEATURE_RDTSCP))
+	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
 		write_rdtscp_aux(cpudata);
 
 	/* Store CPU and node number in limit. */
#3 lore raw

PATCH 2/2 x86/cpu: Remove write_tsc() and write_rdtscp_aux() wrappers

In reply to Sean Christopherson

Drop write_tsc() and write_rdtscp_aux(); the former has no users, and the latter has only a single user and is slightly misleading since the only in-kernel consumer of MSR_TSC_AUX is RDPID, not RDTSCP.

No functional change intended.

---
 arch/x86/include/asm/msr.h   | 4 ----
 arch/x86/kernel/cpu/common.c | 2 +-
 2 files changed, 1 insertion(+), 5 deletions(-)
arch/x86/include/asm/msr.h +0 -4
index e16cccdd0420..a3f87f1015d3 100644
@@ -324,10 +324,6 @@ static inline int wrmsrl_safe(u32 msr, u64 val)
 	return wrmsr_safe(msr, (u32)val,  (u32)(val >> 32));
 }
 
-#define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
-
-#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
-
 struct msr *msrs_alloc(void);
 void msrs_free(struct msr *msrs);
 int msr_set_bit(u32 msr, u8 bit);
arch/x86/kernel/cpu/common.c +1 -1
index 1e576cc831c1..0fc80c5ba5ae 100644
@@ -1851,7 +1851,7 @@ static inline void setup_getcpu(int cpu)
 	struct desc_struct d = { };
 
 	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
-		write_rdtscp_aux(cpudata);
+		wrmsr(MSR_TSC_AUX, cpudata, 0);
 
 	/* Store CPU and node number in limit. */
 	d.limit0 = cpudata;
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#4 lore raw

[tip: x86/urgent] x86/cpu: Remove write_tsc() and write_rdtscp_aux() wrappers

The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     fc48a6d1faadbf08b7a840d58a5a6eb85bd1a79a
Gitweb:        https://git.kernel.org/tip/fc48a6d1faadbf08b7a840d58a5a6eb85bd1a79a
Author:        Sean Christopherson <[email protected]>
AuthorDate:    Tue, 04 May 2021 15:56:32 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 05 May 2021 21:50:14 +02:00

x86/cpu: Remove write_tsc() and write_rdtscp_aux() wrappers

Drop write_tsc() and write_rdtscp_aux(); the former has no users, and the latter has only a single user and is slightly misleading since the only in-kernel consumer of MSR_TSC_AUX is RDPID, not RDTSCP.

No functional change intended.

---
 arch/x86/include/asm/msr.h   | 4 ----
 arch/x86/kernel/cpu/common.c | 2 +-
 2 files changed, 1 insertion(+), 5 deletions(-)
arch/x86/include/asm/msr.h +0 -4
index e16cccd..a3f87f1 100644
@@ -324,10 +324,6 @@ static inline int wrmsrl_safe(u32 msr, u64 val)
 	return wrmsr_safe(msr, (u32)val,  (u32)(val >> 32));
 }
 
-#define write_tsc(low, high) wrmsr(MSR_IA32_TSC, (low), (high))
-
-#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0)
-
 struct msr *msrs_alloc(void);
 void msrs_free(struct msr *msrs);
 int msr_set_bit(u32 msr, u8 bit);
arch/x86/kernel/cpu/common.c +1 -1
index 490bed0..a1b756c 100644
@@ -1852,7 +1852,7 @@ static inline void setup_getcpu(int cpu)
 	struct desc_struct d = { };
 
 	if (boot_cpu_has(X86_FEATURE_RDTSCP) || boot_cpu_has(X86_FEATURE_RDPID))
-		write_rdtscp_aux(cpudata);
+		wrmsr(MSR_TSC_AUX, cpudata, 0);
 
 	/* Store CPU and node number in limit. */
 	d.limit0 = cpudata;