From: Benjamin Segall <bsegall@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] perf: fix racy update of cpc->hrtimer_interval
Date: Mon, 02 Nov 2026 15:11:56 -0800 [thread overview]
Message-ID: <xm26se1ihlqb.fsf@google.com> (raw)
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@google.com>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/patchset/xm265wykhyrv.fsf%40google.com
---
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
reply other threads:[~2026-11-02 23:12 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xm26se1ihlqb.fsf@google.com \
--to=bsegall@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®