From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEE9520C47F for ; Thu, 15 May 2025 00:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747267221; cv=none; b=t3iQMSAbuG57X9wIt91azpXohhjuaeiabcxaVhB+wD70QoKK5aCPpWoBOUUGBtn4xVbkl84NZ405jCigfOxQK8I0biIFPVI86NEu1zmF4EsxwK8inIg7b0InIaU9+O1AnsyR7OqVXb8AaQlwNSJ9IbQCCh01PyKt4KUb67Oj4T0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747267221; c=relaxed/simple; bh=XUwDyLw5/4WxSPmLB0tULktlt1YSPQcfNDFK4JJ3UgI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VtNrQZ7MRjmjSHN2piB22395h1tBk/rRyHCsGd4SspGnYwD1ldvv60IBIdfHpHeucstqQlZ+ZLvoZLBbQr1UBaRWGC7uXLvNslnc+wW7rClqtQ31cEfD1o63TuWvUSytCwZWEWCQnCrRoiJXzESx/ixxzZnKOFwvLQc2nvZZyd8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4PgPaJEs; arc=none smtp.client-ip=209.85.210.201 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.b="4PgPaJEs" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7394792f83cso306509b3a.3 for ; Wed, 14 May 2025 17:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747267219; x=1747872019; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TlJJVY9lFXBl5aa6vrGZ3X+o2Y5gO/1RKuzRIMBedTA=; b=4PgPaJEsMv2EiQpQrpehalu77nU95qUo+MY5mjpFxwEmNW7JS/LVVWaI1mv70jpJEQ pic5urODW8wECi11VfN59nDobOLqwrKDSd5kUydmUfZidnM1oDu4qdvqnKACWrsxbmLu Tg8PdHNS2K6ywPIEKr+uU2IYmy2NBQQ20CyvmQrKBbwJRy4nJqtp8oahM5hK8t5iWFm0 WWttAjhiDcQ3h/Vt1wVuwzTn3vRJm2KZT8wMCswD/mTwvAD7coddYc5BPmWfvdzFN+UQ wOGcYMGDJJU1RhWD4fmvgxfi/mgpA5//omluwLQACy9Kzu5IUaVZi8a6zlj6EUzLJOe0 manw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747267219; x=1747872019; h=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; bh=TlJJVY9lFXBl5aa6vrGZ3X+o2Y5gO/1RKuzRIMBedTA=; b=prlKPQzdDD+miCAfXQnezfj6FKN/jHULr9ZNIvLTRaiiSH5TB6pLwN2haM84YouHvF Swsewq+IPDmtB3Snpb2rFye/m1rn5N5VBjfOYjnYkOu0un0k3KazilnoRYhu1SvnlMh1 jgqThW3L/eNlHBUc1l5tvxCqzGgyB/zTimFTUbc0Rh1H5Ak1d6RKSFEC2V5Nm9zMBArq 7m28OfklNq4UQTXqSeaVe17QA/bEtm6N6HasDyL33/0OsQCM3AoAbRr4fHhu2ZDVnAIh pbqa05KgpZEbJ4CoB7THCQtgfvz4WWz8yk+61IL8cuSI0pyQj1XWLLTjnPr8Apx+y+Y+ v/4A== X-Forwarded-Encrypted: i=1; AJvYcCWlXqELC/A2AokbK9Zl3qst+AFpabcVmIhR/nXDk+AcbzDFcqyhwM0HVFuyu5gg/SykTf2UPuH5fb2S4rDwqvmL@vger.kernel.org X-Gm-Message-State: AOJu0YwW9aXgoTJz8+/LckeY2QYdl7k1FxRK1Zvw+Tj/94El5eUZinGX YpGrkrQSaC7mJGfGiPetvAxXWWg3JMeZvmve4Z6pf9amj8c510PeP3c5DEvzhFoBsGSydj+EaMG ZXQ== X-Google-Smtp-Source: AGHT+IEHBbPUeQde2g3FySUvI+0JlrKcjlKZyfOpIZMlkRvVqTwOi/0XchtIZWlB6QuVKlDRiXI5fZZvczI= X-Received: from pgam17.prod.google.com ([2002:a05:6a02:2b51:b0:af9:5717:cfbb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6d88:b0:1f5:5ca4:2744 with SMTP id adf61e73a8af0-215ff0d4647mr6806295637.17.1747267218995; Wed, 14 May 2025 17:00:18 -0700 (PDT) Date: Wed, 14 May 2025 17:00:17 -0700 In-Reply-To: <20250324173121.1275209-12-mizhang@google.com> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250324173121.1275209-1-mizhang@google.com> <20250324173121.1275209-12-mizhang@google.com> Message-ID: Subject: Re: [PATCH v4 11/38] perf/x86: Forbid PMI handler when guest own PMU From: Sean Christopherson To: Mingwei Zhang Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Paolo Bonzini , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Liang@google.com, Kan , "H. Peter Anvin" , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Yongwei Ma , Xiong Zhang , Dapeng Mi , Jim Mattson , Sandipan Das , Zide Chen , Eranian Stephane , Shukla Manali , Nikunj Dadhania Content-Type: text/plain; charset="us-ascii" On Mon, Mar 24, 2025, Mingwei Zhang wrote: > If a guest PMI is delivered after VM-exit, the KVM maskable interrupt will > be held pending until EFLAGS.IF is set. In the meantime, if the logical > processor receives an NMI for any reason at all, perf_event_nmi_handler() > will be invoked. If there is any active perf event anywhere on the system, > x86_pmu_handle_irq() will be invoked, and it will clear > IA32_PERF_GLOBAL_STATUS. By the time KVM's PMI handler is invoked, it will > be a mystery which counter(s) overflowed. > > When LVTPC is using KVM PMI vecotr, PMU is owned by guest, Host NMI let > x86_pmu_handle_irq() run, x86_pmu_handle_irq() restore PMU vector to NMI > and clear IA32_PERF_GLOBAL_STATUS, this breaks guest vPMU passthrough > environment. > > So modify perf_event_nmi_handler() to check perf_in_guest per cpu variable, > and if so, to simply return without calling x86_pmu_handle_irq(). > > Suggested-by: Jim Mattson > Signed-off-by: Mingwei Zhang > Signed-off-by: Dapeng Mi > --- > arch/x86/events/core.c | 27 +++++++++++++++++++++++++-- > 1 file changed, 25 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c > index 28161d6ff26d..96a173bbbec2 100644 > --- a/arch/x86/events/core.c > +++ b/arch/x86/events/core.c > @@ -54,6 +54,8 @@ DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { > .pmu = &pmu, > }; > > +static DEFINE_PER_CPU(bool, pmi_vector_is_nmi) = true; I strongly prefer guest_ctx_loaded. pmi_vector_is_nmi very inflexible and doesn't communicate *why* perf's NMI handler needs to ignore NMIs > DEFINE_STATIC_KEY_FALSE(rdpmc_never_available_key); > DEFINE_STATIC_KEY_FALSE(rdpmc_always_available_key); > DEFINE_STATIC_KEY_FALSE(perf_is_hybrid); > @@ -1737,6 +1739,24 @@ perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs) > u64 finish_clock; > int ret; > > + /* > + * When guest pmu context is loaded this handler should be forbidden from > + * running, the reasons are: > + * 1. After perf_guest_enter() is called, and before cpu enter into > + * non-root mode, host non-PMI NMI could happen, but x86_pmu_handle_irq() > + * restore PMU to use NMI vector, which destroy KVM PMI vector setting. > + * 2. When VM is running, host non-PMI NMI causes VM exit, KVM will > + * call host NMI handler (vmx_vcpu_enter_exit()) first before KVM save > + * guest PMU context (kvm_pmu_put_guest_context()), as x86_pmu_handle_irq() > + * clear global_status MSR which has guest status now, then this destroy > + * guest PMU status. > + * 3. After VM exit, but before KVM save guest PMU context, host non-PMI NMI > + * could happen, x86_pmu_handle_irq() clear global_status MSR which has > + * guest status now, then this destroy guest PMU status. > + */ This *might* be useful for a changelog, but even then it's probably overkill. NMIs can happen at any time, that's the full the story. Enumerating the exact edge cases adds a lot of noise and not much value.