From: Adrian Hunter <adrian.hunter@intel.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: 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>,
James Clark <james.clark@linaro.org>,
Thomas Gleixner <tglx@kernel.org>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Pavel Machek <pavel@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Danilo Krummrich <dakr@kernel.org>,
Johan Hovold <johan@kernel.org>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Jiayuan Chen <jiayuan.chen@linux.dev>,
"Steven Rostedt (Google)" <rostedt@goodmis.org>,
"Vineeth Pillai (Google)" <vineeth@bitbyteword.org>,
Frederic Weisbecker <frederic@kernel.org>,
Andi Kleen <ak@linux.intel.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-pm@vger.kernel.org, driver-core@lists.linux.dev
Subject: [PATCH 3/6] perf/x86/intel/pt: Stop tracing at syscore shutdown
Date: Mon, 2 Nov 2026 09:40:36 +0200 [thread overview]
Message-ID: <20261102074039.26983-4-adrian.hunter@intel.com> (raw)
In-Reply-To: <20261102074039.26983-1-adrian.hunter@intel.com>
Intel PT tracing can survive a shutdown sequence and continue into a
kexec'ed kernel.
The perf reboot notifier removes only per-CPU events, so per-task
events can remain active, for example:
perf record -e intel_pt// -- kexec -e
Tracing can also remain enabled on CPUs running traced tasks when
stop_other_cpus() halts them. On systems where Intel PT is unavailable
in VMX operation (IA32_VMX_MISC[14] = 0), VMXOFF during KVM shutdown can
re-enable TraceEn via intel_pt_handle_vmx(0) if a tracing event is
still active.
As a result, the new kernel may inherit TraceEn set and Intel PT can
continue writing into buffers allocated by the old kernel. Those pages
may subsequently be reused for unrelated purposes. In addition,
pt_init() detects TraceEn already enabled and refuses to register the
PMU.
Stopping events from a shutdown callback is not sufficient because they
may be scheduled back in until interrupts are finally disabled.
Instead, add a syscore shutdown handler that:
- prevents TraceEn from being enabled again, and
- clears TraceEn on every CPU.
syscore_shutdown() is executed for kexec, reboot, halt and poweroff,
and still runs while CPUs can service IPIs.
To block tracing from restarting, introduce a shutdown flag checked by
both pt_config_start() and intel_pt_handle_vmx(), the only places that
can enable TraceEn. After shutdown begins, events remain in a
self-consistent perf state but no longer generate trace data, matching
the existing VMX-on handling.
Clear TraceEn by reading IA32_RTIT_CTL on each CPU and updating the
hardware state directly rather than relying on aux_config, ensuring
tracing is disabled regardless of perf's software state. Once the
shutdown flag is set, no racing path can re-enable TraceEn.
kexec jump (KEXEC_PRESERVE_CONTEXT) is not covered because it does not
execute syscore shutdown. kdump continues to rely on
cpu_emergency_stop_pt().
Example:
On first kernel:
perf record -e intel_pt// -- kexec -e
On second kernel:
# Without the fix, PT is enabled at boot
dmesg | grep -i 'PT is enabled at boot time'
[ 2.065889] pt: PT is enabled at boot time, doing nothing
Fixes: 52ca9ced3f70 ("perf/x86/intel/pt: Add Intel PT PMU driver")
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: stable@vger.kernel.org
---
arch/x86/events/intel/pt.c | 46 +++++++++++++++++++++++++++++++++++---
1 file changed, 43 insertions(+), 3 deletions(-)
diff --git a/arch/x86/events/intel/pt.c b/arch/x86/events/intel/pt.c
index 36bf039668d7..7453e9880b25 100644
--- a/arch/x86/events/intel/pt.c
+++ b/arch/x86/events/intel/pt.c
@@ -18,6 +18,7 @@
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/kvm_types.h>
+#include <linux/syscore_ops.h>
#include <asm/cpuid/api.h>
#include <asm/perf_event.h>
@@ -34,6 +35,9 @@ static DEFINE_PER_CPU(struct pt, pt_ctx);
static struct pt_pmu pt_pmu;
+/* Set at shutdown, after which TraceEn must not be set */
+static bool pt_shutdown;
+
/*
* Capabilities of Intel PT hardware, such as number of address bits or
* supported output schemes, are cached and exported to userspace as "caps"
@@ -425,7 +429,7 @@ static void pt_config_start(struct perf_event *event)
return;
ctl |= RTIT_CTL_TRACEEN;
- if (READ_ONCE(pt->vmx_on))
+ if (READ_ONCE(pt->vmx_on) || READ_ONCE(pt_shutdown))
perf_aux_output_flag(&pt->handle, PERF_AUX_FLAG_PARTIAL);
else
wrmsrq(MSR_IA32_RTIT_CTL, ctl);
@@ -1609,7 +1613,7 @@ void intel_pt_handle_vmx(int on)
PERF_AUX_FLAG_PARTIAL);
/* Turn PTs back on */
- if (!on && event)
+ if (!on && event && !READ_ONCE(pt_shutdown))
wrmsrq(MSR_IA32_RTIT_CTL, event->hw.aux_config);
local_irq_restore(flags);
@@ -1860,6 +1864,38 @@ static int pt_event_init(struct perf_event *event)
return 0;
}
+static void pt_shutdown_cpu(void *info)
+{
+ u64 ctl;
+
+ /*
+ * After VMXON on CPUs without PT support in VMX operation,
+ * TraceEn is already clear and RTIT_CTL must not be written.
+ */
+ rdmsrq(MSR_IA32_RTIT_CTL, ctl);
+ if (ctl & RTIT_CTL_TRACEEN)
+ wrmsrq(MSR_IA32_RTIT_CTL, ctl & ~RTIT_CTL_TRACEEN);
+}
+
+/*
+ * Tracing must not continue into a kexec'ed kernel, which may reuse the
+ * trace buffer memory, so stop tracing on all CPUs and prevent it from
+ * being started again. Events are left in place, but record nothing more.
+ */
+static void pt_syscore_shutdown(void *data)
+{
+ WRITE_ONCE(pt_shutdown, true);
+ on_each_cpu(pt_shutdown_cpu, NULL, 1);
+}
+
+static const struct syscore_ops pt_syscore_ops = {
+ .shutdown = pt_syscore_shutdown,
+};
+
+static struct syscore pt_syscore = {
+ .ops = &pt_syscore_ops,
+};
+
void cpu_emergency_stop_pt(void)
{
struct pt *pt = this_cpu_ptr(&pt_ctx);
@@ -1933,7 +1969,11 @@ static __init int pt_init(void)
intel_pt_validate_hw_cap(PT_CAP_num_address_ranges);
ret = perf_pmu_register(&pt_pmu.pmu, "intel_pt", -1);
+ if (ret)
+ return ret;
- return ret;
+ register_syscore(&pt_syscore);
+
+ return 0;
}
arch_initcall(pt_init);
--
2.53.0
next prev parent reply other threads:[~2026-11-02 7:41 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 7:40 [PATCH 0/6] perf/x86/intel/pt: Fix tracing across kexec, kdump and suspend Adrian Hunter
2026-11-02 7:40 ` [PATCH 1/6] irq_work: Re-raise pending irq_work after system resume Adrian Hunter
2026-11-02 7:51 ` sashiko-bot
2026-11-02 9:06 ` Peter Zijlstra
2026-11-02 10:54 ` Adrian Hunter
2026-11-02 16:39 ` Sebastian Andrzej Siewior
2026-11-03 15:46 ` Adrian Hunter
2026-11-02 7:40 ` [PATCH 2/6] perf/x86/intel/pt: Stop tracing if VMXON has not yet happened Adrian Hunter
2026-11-02 7:59 ` sashiko-bot
2026-11-02 9:57 ` Peter Zijlstra
2026-11-02 11:04 ` Adrian Hunter
2026-11-02 11:07 ` Peter Zijlstra
2026-11-02 7:40 ` Adrian Hunter [this message]
2026-11-02 8:05 ` [PATCH 3/6] perf/x86/intel/pt: Stop tracing at syscore shutdown sashiko-bot
2026-11-02 10:04 ` Peter Zijlstra
2026-11-02 10:23 ` Adrian Hunter
2026-11-02 11:01 ` Peter Zijlstra
2026-11-02 16:15 ` Andi Kleen
2026-11-02 16:27 ` Adrian Hunter
2026-11-02 17:45 ` Andi Kleen
2026-11-02 18:13 ` Adrian Hunter
2026-11-02 7:40 ` [PATCH 4/6] perf/x86/intel/pt: Clear TraceEn in hardware in cpu_emergency_stop_pt() Adrian Hunter
2026-11-02 8:21 ` sashiko-bot
2026-11-02 10:07 ` Peter Zijlstra
2026-11-02 18:41 ` Adrian Hunter
2026-11-02 7:40 ` [PATCH 5/6] perf/x86/intel/pt: Reset stale MSR state on CPU start and resume Adrian Hunter
2026-11-02 8:31 ` sashiko-bot
2026-11-02 7:40 ` [PATCH 6/6] perf/x86/intel/pt: Stop tracing across system suspend Adrian Hunter
2026-11-02 8:59 ` sashiko-bot
2026-11-03 15:57 ` Adrian Hunter
2026-11-02 10:10 ` [PATCH 0/6] perf/x86/intel/pt: Fix tracing across kexec, kdump and suspend Peter Zijlstra
2026-11-02 12:22 ` Adrian Hunter
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=20261102074039.26983-4-adrian.hunter@intel.com \
--to=adrian.hunter@intel.com \
--cc=acme@kernel.org \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bigeasy@linutronix.de \
--cc=bp@alien8.de \
--cc=dakr@kernel.org \
--cc=dave.hansen@linux.intel.com \
--cc=driver-core@lists.linux.dev \
--cc=frederic@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=hpa@zytor.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jiayuan.chen@linux.dev \
--cc=johan@kernel.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=pavel@kernel.org \
--cc=peterz@infradead.org \
--cc=rafael@kernel.org \
--cc=rostedt@goodmis.org \
--cc=tglx@kernel.org \
--cc=vineeth@bitbyteword.org \
--cc=x86@kernel.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