Re: [PATCH 2/4] perf/core: Add PERF_PMU_SCOPE_DEVICE

From: Peter Zijlstra

Date: Wed Oct 14 2026 - 06:27:22 EST


On Tue, Oct 13, 2026 at 01:56:01PM -0500, Haris Okanovic wrote:
> Add a "device" pmu::scope for PMUs counted on a single CPU drawn
> from a driver-supplied set instead of a fixed topology.
>
> This mechanism is implemented by new callbacks: filter_allowed_cpus()
> is invoked to constrain the running CPU set to an allowed set for a
> particular device; migrate_cpu() is used to perform any device specific
> migration steps. Both are optional, e.g. for devices that can run on
> any CPU or have no additional migration steps.
>
> Placement initially happens in perf_pmu_register() before the PMU
> becomes visible, and is subsequently re-evaluated on hotplug events.
> A new CPU selection is made by cpumask_any_distribute() to spread out
> load from multiple devices, but only when the previous CPU selection is
> offline or no longer allowed by filter_allowed_cpus().
>
> PMU events of these devices follow the chosen CPU. They are initially
> bound to a CPU in perf_try_init_event() then reassigned by
> perf_pmu_migrate_context() when a new CPU is selected.

>
> The initial CPU assignment can race with migration, leaving a stranded
> event. A sequence counter is added to close this gap without additional
> locks. A count in struct pmu increments on each migration and is copied
> to struct perf_event on init. The copied count is checked again before
> the event is installed, under the same context mutex, which causes
> perf_event_open() and perf_event_create_kernel_counter() to return -EBUSY
> during migration periods.

I'm not entirely sure on this. How is this race different from changing
CPU on hotplug?