Message ID | 20240126085444.324918-10-xiong.y.zhang@linux.intel.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | KVM: x86/pmu: Introduce passthrough vPM | expand |
On Fri, Jan 26, 2024, Xiong Zhang wrote: > + /* > + * 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()) Maybe a name more along the lines of: if (perf_is_guest_context_loaded()) because that makes it more obvious that the NMI _can't_ belong to the host PMU. For that matter, I would also rename __perf_force_exclude_guest to perf_guest_context_loaded (or "active" if that's better). The boolean tracks the state (guest vs. host context loaded/active), where as forcing perf events to exclude_guest is an action based on that state.
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) {