Re: [PATCH] perf: fix racy update of cpc->hrtimer_interval
From: Ian Rogers
Date: Tue Dec 01 2026 - 18:32:11 EST
On Mon, Nov 2, 2026 at 3:12 PM Benjamin Segall <bsegall@xxxxxxxxxx> wrote:
>
> Updates to cpc->hrtimer_interval can race with reads either in the
> hrtimer itself, or other calls to perf_mux_hrtimer_restart. This is not
> exactly expected to be a common config change, but it's easy enough to
> fix.
>
> Since there's already an IPI to each cpu to restart the timer, and all
> reads are with irqs disabled, just move the update onto the target cpu.
>
> Fixes: 62b856397927 ("perf: Add sysfs entry to adjust multiplexing interval per PMU")
> Signed-off-by: Ben Segall <bsegall@xxxxxxxxxx>
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Link: https://sashiko.dev/#/patchset/xm265wykhyrv.fsf%40google.com
Reviewed-by: Ian Rogers <irogers@xxxxxxxxxx>
Thanks,
Ian
> ---
> kernel/events/core.c | 24 ++++++++++++------------
> 1 file changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 457d75ea71420..3415e4e3c26db 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -1330,20 +1330,25 @@ static int perf_mux_hrtimer_restart(struct perf_cpu_pmu_context *cpc)
> raw_spin_unlock_irqrestore(&cpc->hrtimer_lock, flags);
>
> return 0;
> }
>
> -static int perf_mux_hrtimer_restart_ipi(void *arg)
> -{
> - return perf_mux_hrtimer_restart(arg);
> -}
> -
> static __always_inline struct perf_cpu_pmu_context *this_cpc(struct pmu *pmu)
> {
> return *this_cpu_ptr(pmu->cpu_pmu_context);
> }
>
> +static int perf_mux_hrtimer_interval_ipi(void *arg)
> +{
> + struct pmu *pmu = arg;
> + struct perf_cpu_pmu_context *cpc = this_cpc(pmu);
> + int ms = pmu->hrtimer_interval_ms;
> +
> + cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * ms);
> + return perf_mux_hrtimer_restart(cpc);
> +}
> +
> void perf_pmu_disable(struct pmu *pmu)
> {
> int *count = &this_cpc(pmu)->pmu_disable_count;
> if (!(*count)++)
> pmu->pmu_disable(pmu);
> @@ -12763,17 +12768,12 @@ perf_event_mux_interval_ms_store(struct device *dev,
> mutex_lock(&mux_interval_mutex);
> pmu->hrtimer_interval_ms = timer;
>
> /* update all cpuctx for this PMU */
> cpus_read_lock();
> - for_each_online_cpu(cpu) {
> - struct perf_cpu_pmu_context *cpc;
> - cpc = *per_cpu_ptr(pmu->cpu_pmu_context, cpu);
> - cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
> -
> - cpu_function_call(cpu, perf_mux_hrtimer_restart_ipi, cpc);
> - }
> + for_each_online_cpu(cpu)
> + cpu_function_call(cpu, perf_mux_hrtimer_interval_ipi, pmu);
> cpus_read_unlock();
> mutex_unlock(&mux_interval_mutex);
>
> return count;
> }
> --
> 2.56.0.385.gac671c819a-goog
>