Skip to contentSkip to conversation

perf/x86/amd: Don't touch the Host-only bit inside the guest hypervisor

PATCH Superseded Dongli Si 2 participants 4 messages Mar 14, 2022 – Mar 18, 2022

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Dongli Si

author
#0 lore raw

PATCH v3 1/1 perf/x86/amd: Don't touch the Host-only bit inside the guest hypervisor

From: Dongli Si <[email protected]>

With nested virtualization on AMD Milan, if "perf record" is run in an L1 hypervisor with an L2 guest, the following warning is emitted in the L1 guest.

[] unchecked MSR access error: WRMSR to 0xc0010200 (tried to write 0x0000020000510076) at rIP: 0xffffffff81003a50 (x86_pmu_enable_all+0x60/0x100)
[] Call Trace:

[]  <IRQ>
[]  ? x86_pmu_enable+0x146/0x300
[]  __perf_install_in_context+0x150/0x170

The AMD64_EVENTSEL_HOSTONLY bit is defined and used on the host (L0), while the L1 hypervisor Performance Monitor Unit should avoid such use.

Fixes 1018faa6cf23 ("perf/x86/kvm: Fix Host-Only/Guest-Only counting with SVM disabled")

---
v3: Add Tested-by, Reviewed-by from Liam and improve description.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 arch/x86/events/amd/core.c        |  4 +++-
 arch/x86/include/asm/hypervisor.h | 10 ++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
arch/x86/events/amd/core.c +3 -1
index 9687a8aef01c..14cd079243a4 100644
@@ -8,6 +8,7 @@
 #include <linux/jiffies.h>
 #include <asm/apicdef.h>
 #include <asm/nmi.h>
+#include <asm/hypervisor.h>
 
 #include "../perf_event.h"
 
@@ -1027,7 +1028,8 @@ void amd_pmu_enable_virt(void)
 {
 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
 
-	cpuc->perf_ctr_virt_mask = 0;
+	if (run_as_host())
+		cpuc->perf_ctr_virt_mask = 0;
 
 	/* Reload all events */
 	amd_pmu_disable_all();
arch/x86/include/asm/hypervisor.h +10 -0
index e41cbf2ec41d..fcc66c23cc72 100644
@@ -73,11 +73,21 @@ static inline bool hypervisor_is_type(enum x86_hypervisor_type type)
 {
 	return x86_hyper_type == type;
 }
+
+static inline bool run_as_host(void)
+{
+	return hypervisor_is_type(X86_HYPER_NATIVE);
+}
 #else
 static inline void init_hypervisor_platform(void) { }
 static inline bool hypervisor_is_type(enum x86_hypervisor_type type)
 {
 	return type == X86_HYPER_NATIVE;
 }
+
+static inline bool run_as_host(void)
+{
+	return true;
+}
 #endif /* CONFIG_HYPERVISOR_GUEST */
 #endif /* _ASM_X86_HYPERVISOR_H */
Signature · 1 line
2.32.0
maintainer
#1 lore raw

arch/x86/include/asm/hypervisor.h static inline bool hypervisor_is_type(enum x86_hypervisor_type type) · line 80

Quoted 13 lines · Dongli Si
arch/x86/include/asm/hypervisor.h +5 -0
index e41cbf2ec41d..fcc66c23cc72 100644
@@ -73,11 +73,21 @@ static inline bool hypervisor_is_type(enum x86_hypervisor_type type)
 {
 	return x86_hyper_type == type;
 }
+
+static inline bool run_as_host(void)
+{
+	return hypervisor_is_type(X86_HYPER_NATIVE);
+}

What's the point of this "wrapper"?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#2 lore raw
Quoted 1 line · Borislav Petkov

What's the point of this "wrapper"?

Hi Borislav, this is just to more clearly test if the current kernel is running directly on the host.

If this is redundant, I can remove this "wrapper" and resend the patch, the v4 patch code will be the same as the v1 patch.

Thanks,
Dongli

maintainer
#3 lore raw
Quoted 5 lines · Dongli Si

Hi Borislav, this is just to more clearly test if the current kernel is running directly on the host.

If this is redundant, I can remove this "wrapper" and resend the patch, the v4 patch code will be the same as the v1 patch.

Yes please. Using

hypervisor_is_type(X86_HYPER_NATIVE)

directly is a lot more descriptive than "run_as_host".

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette