Linux Perf Users
 help / color / mirror / Atom feed
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 2/6] perf/x86/intel/pt: Stop tracing if VMXON has not yet happened
Date: Mon,  2 Nov 2026 09:40:35 +0200	[thread overview]
Message-ID: <20261102074039.26983-3-adrian.hunter@intel.com> (raw)
In-Reply-To: <20261102074039.26983-1-adrian.hunter@intel.com>

On Broadwell, which does not support Intel PT in VMX operation
(IA32_VMX_MISC[14] = 0), VMXON clears IA32_RTIT_CTL.TraceEn and any
subsequent write to IA32_RTIT_CTL causes #GP.

To avoid that, intel_pt_handle_vmx() sets pt->vmx_on so that
pt_config_start() and pt_config_stop() skip writes to
IA32_RTIT_CTL while VMX is active.

However, pt->vmx_on is set before VMXON executes. An NMI in the window
between setting pt->vmx_on and executing VMXON can then observe
vmx_on == true while tracing is still enabled.

In that case pt_config_stop() skips the write that would clear TraceEn,
leaving tracing active. This can subsequently cause:

  - intel_pt_interrupt() to reprogram PT output registers while
    TraceEn is set, resulting in #GP, or
  - cpu_emergency_stop_pt() to leave tracing active during kdump,
    allowing tracing to continue into the capture kernel.

If pt->vmx_on is set, read IA32_RTIT_CTL and still clear TraceEn when
it remains enabled.  Writing IA32_RTIT_CTL is valid until VMXON
executes, while after VMXON TraceEn is guaranteed clear and cannot be
re-enabled.  Consequently no write is attempted while VMX is active.

Example (on a Broadwell):

    Instrument x86_vmx_enable_virtualization_cpu() to enlarge the
    window e.g.

	 	intel_pt_handle_vmx(1);

	+	for (int j = 0; j < 6; j ++) {
	+		int cpu = smp_processor_id();
	+
	+		pr_info("VMX: cpu %d: spinning %d\n", cpu, j);
	+		for (int i = 0; i < 10000; i++)
	+			cpu_relax();
	+	}

	 	r = x86_virt_cpu_vmxon();

    Start Intel PT tracing on a CPU:

	perf record -e intel_pt//k -C 1 &

    Load KVM:

	modprobe kvm_intel

    Observe kernel messages.  Without the fix, there is an unchecked
    MSR access:

	VMX: cpu 1: spinning 0
	VMX: cpu 1: spinning 1
	VMX: cpu 1: spinning 2
	VMX: cpu 1: spinning 3
	unchecked MSR access error: WRMSR to 0x571 ...
	Call Trace:
	 <NMI>
	 intel_pt_interrupt+0x190/0x490
	 handle_pmi_common+0x6fc/0xc10
	 intel_pmu_handle_irq+0x1c9/0x5e0
	 perf_event_nmi_handler+0x62/0x90
	 nmi_handle.part.0+0xbd/0x270
	 default_do_nmi+0x3c/0x120
	 exc_nmi+0xe3/0x110
	 <TASK>
	 x86_vmx_enable_virtualization_cpu+0x88/0xd0
	 x86_virt_get_ref+0x3f/0x80
	 kvm_arch_enable_virtualization_cpu+0x1c4/0x820 [kvm]
	 kvm_online_cpu+0x31/0x50 [kvm]
	 cpuhp_invoke_callback+0x25c/0xf90
	 cpuhp_thread_fun+0x298/0x7e0
	 smpboot_thread_fn+0x28c/0x6d0
	 kthread+0x2cb/0x3a0
	 ret_from_fork+0x5ff/0x7f0

Fixes: 1c5ac21a0e9b ("perf/x86/intel/pt: Don't die on VMXON")
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: stable@vger.kernel.org
---
 arch/x86/events/intel/pt.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/x86/events/intel/pt.c b/arch/x86/events/intel/pt.c
index 49349afee611..36bf039668d7 100644
--- a/arch/x86/events/intel/pt.c
+++ b/arch/x86/events/intel/pt.c
@@ -567,6 +567,15 @@ static void pt_config(struct perf_event *event)
 	pt_config_enable(event);
 }
 
+static bool pt_trace_enabled(void)
+{
+	u64 ctl;
+
+	rdmsrq(MSR_IA32_RTIT_CTL, ctl);
+
+	return ctl & RTIT_CTL_TRACEEN;
+}
+
 static void pt_config_stop(struct perf_event *event)
 {
 	struct pt *pt = this_cpu_ptr(&pt_ctx);
@@ -577,7 +586,12 @@ static void pt_config_stop(struct perf_event *event)
 		return;
 
 	ctl &= ~RTIT_CTL_TRACEEN;
-	if (!READ_ONCE(pt->vmx_on))
+	/*
+	 * vmx_on is set before VMXON, so tracing may not have stopped yet.
+	 * RTIT_CTL can be written until VMXON, and after VMXON TraceEn is
+	 * clear, so still write RTIT_CTL if TraceEn is set.
+	 */
+	if (!READ_ONCE(pt->vmx_on) || pt_trace_enabled())
 		wrmsrq(MSR_IA32_RTIT_CTL, ctl);
 
 	WRITE_ONCE(event->hw.aux_config, ctl);
-- 
2.53.0


  parent reply	other threads:[~2026-11-02  7:41 UTC|newest]

Thread overview: 30+ 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-02  7:40 ` Adrian Hunter [this message]
2026-11-02  7:59   ` [PATCH 2/6] perf/x86/intel/pt: Stop tracing if VMXON has not yet happened 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 ` [PATCH 3/6] perf/x86/intel/pt: Stop tracing at syscore shutdown Adrian Hunter
2026-11-02  8:05   ` 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-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-3-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