Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp718200rdb; Tue, 5 Dec 2023 19:21:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwQwqORZqvD5ul8L7XFU+7Fu7noPcQjmVj52D2w26mSD1TaHRNFyjVBdnJTEFnYvKpwyQj X-Received: by 2002:a05:6808:1708:b0:3b6:da98:437c with SMTP id bc8-20020a056808170800b003b6da98437cmr452357oib.0.1701832876344; Tue, 05 Dec 2023 19:21:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701832876; cv=none; d=google.com; s=arc-20160816; b=ntLmJMxWO4CO5xDt9PdARrjmesEKRCZagZKMEQYcy1/qKQoFjbEdXLQpDcSbGmOmqK GS4pDRF6UHTS/DnVw/gvNJBpYvfxIavjmZmlfjT0xrYkYiRpRTfUAssbPsCnEzxwOZAO UVI3WrjCB3j5jKxacu/XVNBsYB1XIS60f9yZDdbdPA3RRw2YJoQqgTymjtv4L32nOeqk rC74/myVo2mrfqfJBUGOjyJ0YIaw842Ct0jApPZ1IqJbcr6COvPvG1oz75xGxjoV/5bh H/gM/SeIlmcNDkhfG4OmKT4Dp1x4tEGAMOtCE4KXcVCOTVRXF0dMbAc2/I0fppv+BFvq DjiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=KG4vDN3r5Jq53Fpae/2yr0v5k4jdcc0jM7Mwk4eiNY0=; fh=Cp2r2uPQAXhsC6PM68DNAH1ENPD8Nern3oGtMacA4pk=; b=d7ThpDHhysdse7Woa0mGhKBNbtER88gAhnvpI4OufPzu+gut++LztLGqHROnzH+Q0x 8AthcdI6XPhqMKQBqu8EwRfIAyusYV2Sm2N5uuryU4q0UrHB9B34Oqs6J8/AOINxzdUZ uLU7UtgDi5oC7XJ2Cx3xeXPwiBxIK/DbxCEsIwAlHMw3gYLtXdqIcWuTb286Hl34qsXc ndiwKJhA576g5E70KS7b/jHb1F0S0MiHaxZCHHcwjlNNCj1GwavNYdP0wEnl0GcVppSC 2VYbxDSky5OsKPQcNVskuoStkauaLYxHYvlKv4zmEIBwb3NLbzB1183th0dis+oDKZ0F uZvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IaGpJxPC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id de12-20020a056a00468c00b006cddb04ab75si10308379pfb.53.2023.12.05.19.21.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 19:21:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IaGpJxPC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E32B6802FA6B; Tue, 5 Dec 2023 19:21:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229577AbjLFDVE (ORCPT + 99 others); Tue, 5 Dec 2023 22:21:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229493AbjLFDVD (ORCPT ); Tue, 5 Dec 2023 22:21:03 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD5571B6; Tue, 5 Dec 2023 19:21:09 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1d03bcf27e9so33405395ad.0; Tue, 05 Dec 2023 19:21:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701832869; x=1702437669; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=KG4vDN3r5Jq53Fpae/2yr0v5k4jdcc0jM7Mwk4eiNY0=; b=IaGpJxPCP+FRWCuYDWyv7ps7ims5f47Om4ITlXjaOJ6CYd4gWuyGGH5/LtO0339H3T u31lDIks+z2KO2rimpZlhDAKCnsoIhy9iCc32SKcVLJxrtX4sRp35ThKipp9EYGVhPbj IViql8zefsCyMFU4+gXTANF3C/a/h2KK65lPtJtbrA0Rh3BZzoCwuzjE4BTOxKLzhsSP AvOItkcq5f728XNL5+5/yqJEasuCnstqM1ngCYmPQ/dgVbi4kR1r9wUy2aZ67cn84zVg JqHfXMNHZvMb7K9k/7KLg6mNfv0TDVeKdyhM1jd3ALcmnqRGgxjk2HwiyDz13+vsYVTA kzXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701832869; x=1702437669; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KG4vDN3r5Jq53Fpae/2yr0v5k4jdcc0jM7Mwk4eiNY0=; b=bVIktyWftqtZuCcRXUVEmp77imQYlGfopXOUuS6eHqXAbegtpEHE05TXkR3s5CkVP+ MizXMosSl3Fr3dmEltaf82IXqdtPX/OUQfJcaPMj2tGhPpxRpwaihRqtnNvF01YnM93C /ZU/1+Bf0j6V1CiAsehNsFZn+0iZj4jjH/8i1/wiXa0gUkLNe/M7MyE7KJK+vYPSpC2L +9rEPUv80LMl7BvRmXBQwV2CnJKQaj08gZLqtpHaMDqnfC+VzWthgufa9CvzS3vW1AXF oRxUtL6VZnfZ/PWs+qfw+0B/ZON+Zfek7a5clkEqALzY1aPH/ljPtvBTZad3pn+f/DK4 JUsw== X-Gm-Message-State: AOJu0YyzjtbNy9BkYreEtlyfT2VLTivhinp0xo2HJc4wPRHz0PkvpCSE kV3SsN04cbE2R4W7vuSxtFw= X-Received: by 2002:a17:903:185:b0:1d0:a084:a1e8 with SMTP id z5-20020a170903018500b001d0a084a1e8mr150807plg.106.1701832869038; Tue, 05 Dec 2023 19:21:09 -0800 (PST) Received: from localhost.localdomain ([203.205.141.83]) by smtp.gmail.com with ESMTPSA id f3-20020a170902e98300b001c5b8087fe5sm8046131plb.94.2023.12.05.19.21.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 19:21:08 -0800 (PST) From: Like Xu X-Google-Original-From: Like Xu To: Sean Christopherson Cc: Paolo Bonzini , Andi Kleen , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] KVM: x86/intr: Explicitly check NMI from guest to eliminate false positives Date: Wed, 6 Dec 2023 11:20:54 +0800 Message-ID: <20231206032054.55070-1-likexu@tencent.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 05 Dec 2023 19:21:15 -0800 (PST) From: Like Xu Explicitly checking the source of external interrupt is indeed NMI and not other types in the kvm_arch_pmi_in_guest(), which prevents perf-kvm false positive samples generated in perf/core NMI mode after vm-exit but before kvm_before_interrupt() from being incorrectly labelled as guest samples: # test: perf-record + cpu-cycles:HP (which collects host-only precise samples) # Symbol Overhead sys usr guest sys guest usr # ....................................... ........ ........ ........ ......... ......... # # Before: [g] entry_SYSCALL_64 24.63% 0.00% 0.00% 24.63% 0.00% [g] syscall_return_via_sysret 23.23% 0.00% 0.00% 23.23% 0.00% [g] files_lookup_fd_raw 6.35% 0.00% 0.00% 6.35% 0.00% # After: [k] perf_adjust_freq_unthr_context 57.23% 57.23% 0.00% 0.00% 0.00% [k] __vmx_vcpu_run 4.09% 4.09% 0.00% 0.00% 0.00% [k] vmx_update_host_rsp 3.17% 3.17% 0.00% 0.00% 0.00% In the above case, perf records the samples labelled '[g]', the RIPs behind the weird samples are actually being queried by perf_instruction_pointer() after determining whether it's in GUEST state or not, and here's the issue: If vm-exit is caused by a non-NMI interrupt (such as hrtimer_interrupt) and at least one PMU counter is enabled on host, the kvm_arch_pmi_in_guest() will remain true (KVM_HANDLING_IRQ is set) until kvm_before_interrupt(). During this window, if a PMI occurs on host (since the KVM instructions on host are being executed), the control flow, with the help of the host NMI context, will be transferred to perf/core to generate performance samples, thus perf_instruction_pointer() and perf_guest_get_ip() is called. Since kvm_arch_pmi_in_guest() only checks if there is an interrupt, it may cause perf/core to mistakenly assume that the source RIP of the host NMI belongs to the guest world and use perf_guest_get_ip() to get the RIP of a vCPU that has already exited by a non-NMI interrupt. Error samples are recorded and presented to the end-user via perf-report. Such false positive samples could be eliminated by explicitly determining if the exit reason is KVM_HANDLING_NMI. Note that when vm-exit is indeed triggered by PMI and before HANDLING_NMI is cleared, it's also still possible that another PMI is generated on host. Also for perf/core timer mode, the false positives are still possible since that non-NMI sources of interrupts are not always being used by perf/core. In both cases above, perf/core should correctly distinguish between real RIP sources or even need to generate two samples, belonging to host and guest separately, but that's perf/core's story for interested warriors. Fixes: dd60d217062f ("KVM: x86: Fix perf timer mode IP reporting") Signed-off-by: Like Xu --- V1 -> V2 Changelog: - Refine commit message to cover both perf/core timer and NMI modes; - Use in_nmi() to distinguish whether it's NMI mode or not; (Sean) V1: https://lore.kernel.org/kvm/20231204074535.9567-1-likexu@tencent.com/ arch/x86/include/asm/kvm_host.h | 10 +++++++++- arch/x86/kvm/x86.h | 6 ------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c8c7e2475a18..167d592e08d0 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1868,8 +1868,16 @@ static inline int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, } #endif /* CONFIG_HYPERV */ +enum kvm_intr_type { + /* Values are arbitrary, but must be non-zero. */ + KVM_HANDLING_IRQ = 1, + KVM_HANDLING_NMI, +}; + +/* Enable perf NMI and timer modes to work, and minimise false positives. */ #define kvm_arch_pmi_in_guest(vcpu) \ - ((vcpu) && (vcpu)->arch.handling_intr_from_guest) + ((vcpu) && (vcpu)->arch.handling_intr_from_guest && \ + (in_nmi() == ((vcpu)->arch.handling_intr_from_guest == KVM_HANDLING_NMI))) void __init kvm_mmu_x86_module_init(void); int kvm_mmu_vendor_module_init(void); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 2f7e19166658..4dc38092d599 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -431,12 +431,6 @@ static inline bool kvm_notify_vmexit_enabled(struct kvm *kvm) return kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_ENABLED; } -enum kvm_intr_type { - /* Values are arbitrary, but must be non-zero. */ - KVM_HANDLING_IRQ = 1, - KVM_HANDLING_NMI, -}; - static __always_inline void kvm_before_interrupt(struct kvm_vcpu *vcpu, enum kvm_intr_type intr) { base-commit: 1ab097653e4dd8d23272d028a61352c23486fd4a -- 2.43.0