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

Robin Murphy robin.murphy at arm.com
Wed Oct 14 07:26:10 PDT 2026


On 2026-10-14 11:27 am, Peter Zijlstra wrote:
> 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?

Not much, other than that hotplug is at least already serialised against 
perf_event_open on the perf core side via perf_online_mask. This is also 
where I was heading with my generic hotplug proposal[1] - once perf core 
can be in charge of that, it should then be viable to also migrate PMUs 
for reasons other than hotplug, and it would all look the same to the 
drivers.

(Yes, as-is my patch still preserves the status quo of drivers picking 
their own target CPU to keep things simple, but that was one of the 
points I was hoping to generate some discussion on...)

Thanks,
Robin.

[1] 
https://lore.kernel.org/linux-arm-kernel/cover.1784911757.git.robin.murphy@arm.com/



More information about the linux-arm-kernel mailing list