mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: 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>,
	Paolo Bonzini <pbonzini@redhat.com>,
	x86@kernel.org,  Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Mingwei Zhang <mizhang@google.com>,
	 linux-perf-users@vger.kernel.org, kvm@vger.kernel.org,
	 linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC
Date: Tue, 1 Dec 2026 15:56:59 -0800	[thread overview]
Message-ID: <aw9eywBC7BaplLV0@seanjc-mac.roam.internal> (raw)
In-Reply-To: <20261201171413.2289887-2-yosry@kernel.org>

On Tue, Dec 01, 2026, Yosry Ahmed wrote:
> perf_event_nmi_handler() checks guest_lvtpc_loaded to skip handling NMIs
> and unmasking LVTPC on Intel (as it is automasked by hardware on PMI).
> However, when loading the guest LVTPC, perf_load_guest_lvtpc() updates
> APIC_LVTPC before setting guest_lvtpc_loaded. If an NMI arrives in
> between, perf_event_nmi_handler() will observe !guest_lvtpc_loaded and
> proceed.
> 
> On Intel, the NMI handler will unconditionally write APIC_LVTPC to

Unconditionally writing APIC_LVTCP is fine.  The issue is specifically that
it does a straight write, not a read-modify-write operation.  That's kinda
sorta mentioned below, but it really should be the focus of the changelog.

> unmask PMIs, overwriting it to APIC_DM_NMI and clobbering the guest PMI
> vector. The first guest PMI is delivered as an unknown host NMI (which
> results in a panic if unknown_nmi_panic/panic_on_unrecovered_nmi is
> set). Subsequent guest PMIs will be completely lost because
> perf_event_nmi_handler() will observe guest_lvtpc_loaded and bail
> without unmasking LVTPC. Recovery only happens when
> perf_put_guest_lvtpc() restores unmasked host NMIs in APIC_LVTPC.
> 
> Set guest_lvtpc_loaded before writing APIC_LVTPC in
> perf_load_guest_lvtpc() to prevent a racing NMI from clobbering LVTPC.
> The current ordering of perf_put_guest_lvtpc() is correct,
> guest_lvtpc_loaded should be cleared before writing APIC_LVTPC to ensure
> that NMIs immediately start unmasking APIC_LVTPC as needed.  Document it
> as such.
> 
> Fixes: 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> ---
>  arch/x86/events/core.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> index 4e344fb1ae3ed..c6608ada06ebd 100644
> --- a/arch/x86/events/core.c
> +++ b/arch/x86/events/core.c
> @@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc)
>  {
>  	u32 masked = guest_lvtpc & APIC_LVT_MASKED;
>  
> +	/*
> +	 * Set guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> +	 * arriving in between will unconditionally write APIC_DM_NMI to
> +	 * APIC_LVTPC on Intel, clobbering the guest's mediated PMI vector.
> +	 */
> +	this_cpu_write(guest_lvtpc_loaded, true);
> +	barrier();

I doubt the barrier() is necessary, though it doesn't harm anything either.  WRMSR
(for x2APIC) and MMIO accesses (xAPIC) are both volatile asm().

>  	apic_write(APIC_LVTPC,
>  		   APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);
> -	this_cpu_write(guest_lvtpc_loaded, true);
>  }
>  EXPORT_SYMBOL_FOR_KVM(perf_load_guest_lvtpc);
>  
>  void perf_put_guest_lvtpc(void)
>  {
> +	/*
> +	 * Clear guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> +	 * arriving in between will skip unmasking APIC_LVTPC on Intel.
> +	 */
>  	this_cpu_write(guest_lvtpc_loaded, false);
> +	barrier();
>  	apic_write(APIC_LVTPC, APIC_DM_NMI);
>  }
>  EXPORT_SYMBOL_FOR_KVM(perf_put_guest_lvtpc);
> -- 
> 2.56.1.839.g22a8290db9-goog
> 

  reply	other threads:[~2026-12-01 23:57 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:14 [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context Yosry Ahmed
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
2026-12-01 23:56   ` Sean Christopherson [this message]
2026-12-02  0:39     ` Yosry Ahmed
2026-12-02  2:44   ` Mi, Dapeng
2026-12-02  4:18     ` Yosry Ahmed
2026-12-01 17:14 ` [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC Yosry Ahmed
2026-12-01 23:50   ` Sean Christopherson
2026-12-02  0:46     ` Yosry Ahmed
2026-12-02  1:14       ` Sean Christopherson
2026-12-02  3:39         ` Mi, Dapeng
2026-12-02  4:32           ` Yosry Ahmed
2026-12-02  5:26             ` Mi, Dapeng
2026-12-02  5:48               ` Yosry Ahmed

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=aw9eywBC7BaplLV0@seanjc-mac.roam.internal \
    --to=seanjc@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kvm@vger.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=mizhang@google.com \
    --cc=namhyung@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=x86@kernel.org \
    --cc=yosry@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

all inboxes | Powered by JetHome®