[RFC PATCH 06/26] perf/core: Guard against accessing guest mediated vPMU state
From: Yosry Ahmed
Date: Mon Nov 23 2026 - 19:23:42 EST
While iterating PMU contexts (i.e. using for_each_epc()) for purposes
other than switching to/from guest context (i.e. other than
EVENT_GUEST), skip contexts of mediated PMUs if the guest context is
loaded, to avoid clobbering guest state (e.g. disabling/enabling the
core PMU would overwrite the guest GLOBAL_CTRL on x86). The guest
context should be put before any host events on those PMUs are
activated, so WARN if the skipped PMUs have any active events.
In merge_sched_in(), WARN if the check for events on mediated PMUs is
true, as that shouldn't happen anymore. merge_sched_in() is only
reachable through:
- ctx_sched_in(): Uses for_each_epc(), which now skips such contexts.
- perf_rotate_context(): Rotation is never necessary for such PMUs while
the guest context is loaded. rotate_necessary is cleared when the host
contexts are scheduled out before loading the guest context, and only
set again when the contexts are scheduled back in after the guest
context is put.
Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
---
kernel/events/core.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 8fd1e19cf5ab9..78441df572487 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -802,6 +802,18 @@ static bool perf_skip_pmu_ctx(struct perf_event_pmu_context *pmu_ctx,
if ((event_type & EVENT_GUEST) &&
!(pmu_ctx->pmu->capabilities & PERF_PMU_CAP_MEDIATED_VPMU))
return true;
+
+ /*
+ * Except when loading/putting the guest vPMU context, accessing a
+ * mediated PMU while the guest vPMU context is loaded would clobber
+ * guest state.
+ */
+ if (!(event_type & EVENT_GUEST) && is_guest_mediated_pmu_loaded() &&
+ (pmu_ctx->pmu->capabilities & PERF_PMU_CAP_MEDIATED_VPMU)) {
+ WARN_ON_ONCE(perf_pmu_ctx_is_active(pmu_ctx));
+ return true;
+ }
+
return false;
}
@@ -4139,13 +4151,15 @@ static int merge_sched_in(struct perf_event *event, void *data)
return 0;
/*
- * Don't schedule in any host events from PMU with
- * PERF_PMU_CAP_MEDIATED_VPMU, while a guest is running.
+ * perf_skip_pmu_ctx() skips mediated PMUs while the guest vPMU context
+ * is loaded, except when loading/putting it.
*/
if (is_guest_mediated_pmu_loaded() &&
event->pmu_ctx->pmu->capabilities & PERF_PMU_CAP_MEDIATED_VPMU &&
- !(msd->event_type & EVENT_GUEST))
+ !(msd->event_type & EVENT_GUEST)) {
+ WARN_ON_ONCE(1);
return 0;
+ }
if (group_can_go_on(event, msd->can_add_hw)) {
if (!group_sched_in(event, ctx))
--
2.56.1.809.ga0b61035b5-goog