perf: fix racy update of cpc->hrtimer_interval
PATCH 1/1 perf: fix racy update of cpc->hrtimer_interval
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.
--- kernel/events/core.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
kernel/events/core.c +12 -12
@@ -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; }
Signature · 1 line
2.56.0.385.gac671c819a-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 10 lines
Updates to
cpc->hrtimer_intervalcan race with reads either in the hrtimer itself, or other calls toperf_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 <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[tip: perf/urgent] perf: Fix racy update of cpc->hrtimer_interval
In reply to Benjamin SegallThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 3bbd85b2f67aff58f2cbec9a7fb705677b94473b Gitweb: https://git.kernel.org/tip/3bbd85b2f67aff58f2cbec9a7fb705677b94473b Author: Benjamin Segall <[email protected]> AuthorDate: Mon, 02 Nov 2026 15:11:56 -08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 06 Nov 2026 11:45:15 +01:00
perf: Fix racy update of cpc->hrtimer_interval
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.
--- kernel/events/core.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
kernel/events/core.c +11 -11
@@ -1332,14 +1332,19 @@ static int perf_mux_hrtimer_restart(struct perf_cpu_pmu_context *cpc)return 0 ; } -static int perf_mux_hrtimer_restart_ipi (void *arg) +static __always_inline struct perf_cpu_pmu_context *this_cpc(struct pmu *pmu) { -return perf_mux_hrtimer_restart (arg); +return *this_cpu_ptr (pmu->cpu_pmu_context); } -static __always_inline struct perf_cpu_pmu_context *this_cpc(struct pmu *pmu) +static int perf_mux_hrtimer_interval_ipi (void *arg) { -return *this_cpu_ptr (pmu->cpu_pmu_context); +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)
@@ -12780,13 +12785,8 @@ perf_event_mux_interval_ms_store(struct device *dev,/* 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);