[RFC PATCH 09/41] perf: core/x86: Forbid PMI handler when guest own PMU

From: Xiong Zhang
Date: Fri Jan 26 2024 - 04:33:06 EST


From: Mingwei Zhang <mizhang@xxxxxxxxxx>

If a guest PMI is delivered after VM-exit, the KVM maskable interrupt will
be held pending until EFLAGS.IF is set. In the meantime, if the logical
processor receives an NMI for any reason at all, perf_event_nmi_handler()
will be invoked. If there is any active perf event anywhere on the system,
x86_pmu_handle_irq() will be invoked, and it will clear
IA32_PERF_GLOBAL_STATUS. By the time KVM's PMI handler is invoked, it will
be a mystery which counter(s) overflowed.

When LVTPC is using KVM PMI vecotr, PMU is owned by guest, Host NMI let
x86_pmu_handle_irq() run, x86_pmu_handle_irq() restore PMU vector to NMI
and clear IA32_PERF_GLOBAL_STATUS, this breaks guest vPMU passthrough
environment.

So modify perf_event_nmi_handler() to check perf_is_in_guest_pasthrough(),
and if so, to simply return without calling x86_pmu_handle_irq().

Suggested-by: Jim Mattson <jmattson@xxxxxxxxxx>
Signed-off-by: Mingwei Zhang <mizhang@xxxxxxxxxx>
---
arch/x86/events/core.c | 17 +++++++++++++++++
include/linux/perf_event.h | 1 +
kernel/events/core.c | 5 +++++
3 files changed, 23 insertions(+)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index ece042cfb470..20a5ccc641b9 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -1752,6 +1752,23 @@ perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs)
u64 finish_clock;
int ret;

+ /*
+ * When PMU is pass-through into guest, this handler should be forbidden from
+ * running, the reasons are:
+ * 1. After perf_guest_switch_to_kvm_pmi_vector() is called, and before cpu
+ * enter into non-root mode, NMI could happen, but x86_pmu_handle_irq()
+ * restore PMU to use NMI vector, which destroy KVM PMI vector setting.
+ * 2. When VM is running, host NMI other than PMI causes VM exit, KVM will
+ * call host NMI handler (vmx_vcpu_enter_exit()) first before KVM save
+ * guest PMU context (kvm_pmu_save_pmu_context()), as x86_pmu_handle_irq()
+ * clear global_status MSR which has guest status now, then this destroy
+ * guest PMU status.
+ * 3. After VM exit, but before KVM save guest PMU context, host NMI other
+ * than PMI could happen, x86_pmu_handle_irq() clear global_status MSR
+ * which has guest status now, then this destroy guest PMU status.
+ */
+ if (perf_is_in_guest_passthrough())
+ return 0;
/*
* All PMUs/events that share this PMI handler should make sure to
* increment active_events for their events.
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 9912d1112371..6cfa0f5ac120 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1716,6 +1716,7 @@ extern int perf_event_period(struct perf_event *event, u64 value);
extern u64 perf_event_pause(struct perf_event *event, bool reset);
extern void perf_guest_enter(void);
extern void perf_guest_exit(void);
+extern bool perf_is_in_guest_passthrough(void);
#else /* !CONFIG_PERF_EVENTS: */
static inline void *
perf_aux_output_begin(struct perf_output_handle *handle,
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 59471eeec7e4..00ea2705444e 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5848,6 +5848,11 @@ void perf_guest_exit(void)
}
EXPORT_SYMBOL_GPL(perf_guest_exit);

+bool perf_is_in_guest_passthrough(void)
+{
+ return __this_cpu_read(__perf_force_exclude_guest);
+}
+
static inline int perf_force_exclude_guest_check(struct perf_event *event,
int cpu, struct task_struct *task)
{
--
2.34.1