From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796169421; b=Z/xdoTTs6W0eQ53y36V8kFEeT1S1BDKWNRRWU7x+ZZpgJ8Qgj2BkdHT48QlQ0Imni1AU dW7951y0gHDMxIhv4S/q60Ec9IZ7v8Xzy247sWos23tMNDvaz3ZEj5DDKXIUbS8m5B2JN OrlzrGFoMfWElYsgEXohW4UCbnw2jr1jqAI4hrucdu6T3bQwcehzZXX55SCTxjowmKQi7 dBiRBlddCO9HrJFtKUIAZR1MbHuw86uPmeoLcYYaCdKLP3ZhE0yykxKzYjlbvqSANeTTF kk+kh9fsL753MVo9baGCnnL0aCoYiDpWz5EsKb6a3HetcXYkS+yCM5vWs7WI1NGQoWA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796169421; 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=r4sJbt8Ltu9vj5JxM4bSxYc1ibneMt86nU3nbYpbQozL43gL+JsyaiKGjKxEw4eCCtFZ XZVqHqP8bhRqEKzdugV7hoUEk12UZXiKhbBVhHTb8N+y9okvng7qcKb/uqxjlXcvUTOK3 QP7sbUiLChi6AZIM5H1O4U0JWRZ35hOpLvW+a6yOvDjTNBif2TZUfN0jcgh9V69U0xgwL hQTto2QnOsmsutJJ5zN43VSzb+xRKVT/kLVDmPVy5pkZgihL1XVqxEJHyj+s7/Hltguv9 h71uLcCdWajGHnQPYdeflaWmxIl5l5+xrhOzcL5UZllxfmfyzqFHD9o1Ey0RBKeL3HQ== 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.210.198 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.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 3315C606A320 for ; Tue, 1 Dec 2026 23:57:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3315C606A320 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-pf1-f198.google.com with SMTP id d2e1a72fcca58-8b8cef34b43so13713589b3a.2 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=QJww1EbVd45YjFI8pkIoow7tb6uotfhvjT8y1wC/PDNSAySgvIjb7wiHL8/uX4CSIF lbdNvEVNbNhC9sgcU7WQv1BvnD08R/OllzzEQ2TsA9xO77KjBmAloEFfynIUd/ElEz51 GwgQcKhtbMDUVanPpmYWFlDYSzUKdM+0pCHpbx/BCr0yVnG4uJYirg/30uzrxTUm7+4V gkUI5rz3YdS42f5zL0fIgAuYOayYfx341Pk7ktTngiCBarXTH3KZcrWqiKPWIp1OU+CH lJMfjEwZdwyDFfLF2xYKDgFsFPMrjW/pevX52nx2trEfD2R/2DR0TYtTcGoe1hbRdZjn Ardg== X-Forwarded-Encrypted: i=1; AL+hErGUjNPMB9n3uHJm9oAJ4ji0Jm6Y/msqw3LRabmwsOdbpyWjfs24Bga4pEV7ozFDYTHexvqb+8BmUXwCwjU=@vger.kernel.org X-Gm-Message-State: APuU4Z8DMNTI+Ask5B0LzvLOfhKO8sWqRgIbIOA1l+PqLg5MUEFt42SG a1pvTcLjJR1wreM8x/cglZjfHoewsHOdjg/s65h3HipGu855QGdW0Ac/e5t2dxkFQ/21TK2+uAt pMd1hdQ== 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-kernel@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 >