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

From: Sean Christopherson
Date: Thu Apr 11 2024 - 15:26:52 EST


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.