Re: [PATCH v6 04/44] perf: Add APIs to create/release mediated guest vPMUs

Peter Zijlstra
2025-12-08T11:52:04Z

On Fri, Dec 05, 2025 at 04:16:40PM -0800, Sean Christopherson wrote:

> +static atomic_t nr_include_guest_events __read_mostly;
> +
> +static atomic_t nr_mediated_pmu_vms __read_mostly;
> +static DEFINE_MUTEX(perf_mediated_pmu_mutex);

> +static int mediated_pmu_account_event(struct perf_event *event)
> +{
> + if (!is_include_guest_event(event))
> + return 0;
> +
> + guard(mutex)(&perf_mediated_pmu_mutex);
> +
> + if (atomic_read(&nr_mediated_pmu_vms))
> + return -EOPNOTSUPP;
> +
> + atomic_inc(&nr_include_guest_events);
> + return 0;
> +}
> +
> +static void mediated_pmu_unaccount_event(struct perf_event *event)
> +{
> + if (!is_include_guest_event(event))
> + return;
> +
> + atomic_dec(&nr_include_guest_events);
> +}

> +int perf_create_mediated_pmu(void)
> +{
> + guard(mutex)(&perf_mediated_pmu_mutex);
> + if (atomic_inc_not_zero(&nr_mediated_pmu_vms))
> + return 0;
> +
> + if (atomic_read(&nr_include_guest_events))
> + return -EBUSY;
> +
> + atomic_inc(&nr_mediated_pmu_vms);
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(perf_create_mediated_pmu);
> +
> +void perf_release_mediated_pmu(void)
> +{
> + if (WARN_ON_ONCE(!atomic_read(&nr_mediated_pmu_vms)))
> + return;
> +
> + atomic_dec(&nr_mediated_pmu_vms);
> +}
> +EXPORT_SYMBOL_GPL(perf_release_mediated_pmu);

These two things are supposed to be symmetric, but are implemented
differently; what gives?

That is, should not both have the general shape:

if (atomic_inc_not_zero(&A))
return 0;

guard(mutex)(&lock);

if (atomic_read(&B))
return -EBUSY;

atomic_inc(&A);
return 0;

Similarly, I would imagine both release variants to have the underflow
warn on like:

if (WARN_ON_ONCE(!atomic_read(&A)))
return;

atomic_dec(&A);

Hmm?

Also, EXPORT_SYMBOL_FOR_KVM() ?

I can make these edits when applying, if/when we get to applying. Let me