From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796169422; b=w492uIQt9M89+0SOs/p6k0e0sbY5j8QVBjFBKJWlSP8VB2w0l93USoFn5J8hlThgpOX5 DCgpNagdOrgSQsPNGo8VhGUGAr/DcDuCKU6nlQC2C5t093Y74YZaQoSXEVArB9h8C2qiZ fQTEguTXlP/Gct7CfEEsW7dY0i1YBOe+wt7ggNCYx9VyNPSXf+ocKt8AcqBFnrhcEPyYj Z+A1BOPqaQsatDnVqtXUtFN+IiWoQ4+aOVDDlmJA7XWeKm8V9lAHX26EGbSls4jpPOi7X GDz1frh6mys1VSErQ5pvsWU/BSJAJ1KqAvPoRN/QZ1UNswSl3rkANMpFEq1Vpwdj9cQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796169422; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:Message-ID:Subject:From:To:Cc:Content-Type; bh=Qx9ee94hIJBWw9r0dn2ZiHBjbZFri6LgBy4n2Y9vOGc=; b=iVpAKoEyB/zA0xLCFMEAQksZHyZZ1waYPUiGqL1Y7G5/S3lHaEf64myJp0R3ERL15IEt pC1hQr9xFgnteQtJ3tyeeH6dhytTlW+P3Nt51qUZwKR/nPi6gZ2Q7iSohbHVAJJegMcXW b5LLStbX6xjE5s4fbBh8VgPsbS1//mopa7W/WyIYxFz6msrv7OUnZADuDDfPZGjJf5HX9 eyuOeOl4Q56qj3LvqcjT3MEDg6JEpSFhqEQ2xtSif3JM2A+9IJhNjb1+HvkpYikOdWS+s rSzWgFo+nvAN4WueAJS8Fa/p/5S8ZLfdC7kHR6V/xkalZxZtcmbxSfwTZMoAqcaPnEQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Z08b1xGU; arc=none smtp.remote-ip=209.85.215.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35F85606A322 for ; Tue, 1 Dec 2026 23:57:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 35F85606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Z08b1xGU Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cdea3a64d1bso6493446a12.1 for ; Tue, 01 Dec 2026 15:57:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796169420; x=1796774220; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Qx9ee94hIJBWw9r0dn2ZiHBjbZFri6LgBy4n2Y9vOGc=; b=Z08b1xGUW9jnLX7I2wSSNG5BL13YLb2wQRTvJwaultVykHr9qNkwvqgr4j9wyCdUiL oKXlqJMqXByuwDQMDd/kYN3J7slxau0qCRqEtuE7ix8rxXYN09jUMGlZTE7lAEGUJyKq DMMvFV/1saOLwD4ZG9ZZMKSY014ca3aMI4cPP2spV6t4tqaTqaw5g4s7YNLyUvGpNSdl yh6W4h3OhI91+6qGsaozoMb9gXKWiF6YNeQXkgMVi4KLiP5OkUCn6xC6FalWB8AQtaJv fPSMto5MxLhBscYKWPomomqfhG8CelMWkU0p7Oa+KrFY4msS6X2hMmgL52wLsDohQX1M EUmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796169420; x=1796774220; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Qx9ee94hIJBWw9r0dn2ZiHBjbZFri6LgBy4n2Y9vOGc=; b=RInW7M/KovdH239PryBexebFkp1B1efK2UGVVkuNj7xZF/D0uAyMTaCeFz48ywpBm9 ZkMYtD7vTz7m9XOKOTy0pMLxHTlA2n1zRGoJ3sYWTC52dDw7kPMpS8NTw4LmoHnzpTdE /FYW5yMUUUUTNXpQjDiZFMwsE4qAN8zjRSKNQfOKR/ti4eQUknB/UqFAnTlUZ7lVuZH/ 3iDzsNl8jzsek2NI/8sKOk9v8Ou+7shOX9icdhP4SlS+O9UQYftsdfRN3LwdHDaHSwOj 0EjC4wVW6tgaX0wvkP+/LjGaKaD0Gf0Qg7HqTFhzIaU6WB/EqHcSeTuthrTMb/emahFV 7AAw== X-Forwarded-Encrypted: i=1; AL+hErHLaRf7GmeIt2s8qx/nk3sGARZ6qZhcouh9eIbYabSLIOP1r3ckRYqQ10EIpeXbltnpgBOkeHWOuC1S8DSV6KWJ@vger.kernel.org X-Gm-Message-State: APuU4Z+KaxpyD1xttzUD5CG+f/Zy7oXSOo0I9otmezgXkFKzsKYHGjRA wbtwmRps9zlWJorYcCy0ix8jV9jV98N7MshRP+hnZV2ikMxEZR05V2R4qGR2G1aSm52wQ/4S420 YCeJePg== X-Received: from pgmb28-n1.prod.google.com ([2002:a05:6a02:6adc:10b0:cdb:8bfd:2cb2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2587:b0:3ed:bdfd:23a8 with SMTP id adf61e73a8af0-4014ce5d1femr44963428637.61.1796169420143; Tue, 01 Dec 2026 15:57:00 -0800 (PST) Date: Tue, 1 Dec 2026 15:56:59 -0800 In-Reply-To: <20261201171413.2289887-2-yosry@kernel.org> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201171413.2289887-1-yosry@kernel.org> <20261201171413.2289887-2-yosry@kernel.org> Message-ID: Subject: Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC From: Sean Christopherson To: Yosry Ahmed Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Paolo Bonzini , x86@kernel.org, Dapeng Mi , Mingwei Zhang , linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="us-ascii" 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 > --- > 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 >