perf_clear_dirty_counters() zeroes PMCs that aren't assigned to host events before enabling RDPMC for a task, so that it can't read stale values. It doesn't go through remote_function(), so it doesn't reclaim the PMU, and if a guest's mediated PMU context is loaded, the "dirty" counters hold guest state that clearing would clobber. In preparation for KVM leaving the guest context loaded with IRQs enabled, reclaim the PMU before clearing dirty counters. Do so before filtering out the counters assigned to host events, as host events are scheduled out while the guest context is loaded, and putting the guest context schedules them back in, assigning counters to them. No functional change intended, as the guest context is currently only loaded with IRQs disabled, where perf_clear_dirty_counters() never runs Signed-off-by: Yosry Ahmed --- arch/x86/events/core.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 4e344fb1ae3ed..95ebdec4d5f5d 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -3026,6 +3026,14 @@ void perf_clear_dirty_counters(void) struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); int i; + /* + * Reclaim the mediated vPMU from any loaded guest context before + * accessing counters to avoid clobbering guest state. Do so before + * filtering out assigned counters, as putting the guest context can + * schedule in host events. + */ + perf_reclaim_mediated_pmu(); + /* Don't need to clear the assigned counter. */ for (i = 0; i < cpuc->n_events; i++) __clear_bit(cpuc->assign[i], cpuc->dirty); -- 2.56.1.809.ga0b61035b5-goog