[RFC PATCH 08/26] perf/core: Reclaim the mediated PMU before executing remote calls
From: Yosry Ahmed
Date: Mon Nov 23 2026 - 19:24:17 EST
Remote function calls (e.g. installing, enabling or disabling events) may
alter the PMU state, which would clobber the guest's state if a guest's
mediated PMU context is loaded on the target CPU. In preparation for KVM
leaving the guest context loaded with IRQs enabled (where host events
may get scheduled), reclaim the PMU before executing any remote call.
Hooking remote_function() is conservative, as only installing events
through __perf_install_in_context() strictly needs to reclaim the PMU.
However, hooking only event installation would be less future-proof, and
would require perf_install_in_context() to not skip the IPI when
installing a disabled event on a mediated-capable PMU, as the event
could otherwise be enabled later through perf_event_enable() while the
guest context is still loaded.
The PMU reclaim callback must be called with IRQs disabled and without
perf context locks held (and hence isn't NMI-safe), as putting the guest
context takes them. WARN if the guest context is still loaded after the
callback.
No functional change intended, as the guest context is currently only
loaded with IRQs disabled, so remote_function() never runs while it's
loaded.
Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
include/linux/perf_event.h | 3 +++
kernel/events/core.c | 28 ++++++++++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index a83b9f2785301..50b625f1aff20 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1938,6 +1938,7 @@ extern u64 perf_event_pause(struct perf_event *event, bool reset);
#ifdef CONFIG_PERF_GUEST_MEDIATED_PMU
int perf_create_mediated_pmu(void);
void perf_release_mediated_pmu(void);
+void perf_reclaim_mediated_pmu(void);
void perf_load_guest_context(void);
void perf_put_guest_context(void);
@@ -1953,6 +1954,8 @@ static __always_inline bool is_guest_mediated_pmu_loaded(void)
{
return false;
}
+
+static __always_inline void perf_reclaim_mediated_pmu(void) { }
#endif
#else /* !CONFIG_PERF_EVENTS: */
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 2f301ef0bd26a..99aa56ced8253 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -93,6 +93,13 @@ static void remote_function(void *data)
return;
}
+ /*
+ * Reclaim the mediated vPMU from any potentially loaded guest context
+ * before executing the remote function call, as it may alter the PMU
+ * state (e.g. by installing new events).
+ */
+ perf_reclaim_mediated_pmu();
+
tfc->ret = tfc->func(tfc->info);
}
@@ -6425,6 +6432,27 @@ static void mediated_pmu_unaccount_event(struct perf_event *event)
atomic_dec(&nr_include_guest_events);
}
+/*
+ * Reclaim the mediated vPMU from any loaded guest context on this CPU. This
+ * must be called with IRQs disabled and without any perf ctx locks held, and is
+ * not NMI-safe.
+ */
+void perf_reclaim_mediated_pmu(void)
+{
+ struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context);
+
+ WARN_ON_ONCE(in_nmi());
+ lockdep_assert_irqs_disabled();
+ lockdep_assert_not_held(&cpuctx->ctx.lock);
+ if (cpuctx->task_ctx)
+ lockdep_assert_not_held(&cpuctx->task_ctx->lock);
+
+ if (is_guest_mediated_pmu_loaded()) {
+ perf_guest_reclaim_mediated_pmu();
+ WARN_ON_ONCE(is_guest_mediated_pmu_loaded());
+ }
+}
+
/*
* Currently invoked at VM creation to
* - Check whether there are existing !exclude_guest events of PMU with
--
2.56.1.809.ga0b61035b5-goog