From patchwork Fri Oct 8 09:57:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12544851 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCD24C433F5 for ; Fri, 8 Oct 2021 09:58:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B0BCC6101A for ; Fri, 8 Oct 2021 09:58:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239036AbhJHKAX (ORCPT ); Fri, 8 Oct 2021 06:00:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236118AbhJHKAU (ORCPT ); Fri, 8 Oct 2021 06:00:20 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78DC6C061570; Fri, 8 Oct 2021 02:58:25 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id kk10so7172516pjb.1; Fri, 08 Oct 2021 02:58:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id; bh=r9j8e1y3uJ+lIEwImYYJrXBxHGgDtZ3NYM0TMNrxFbA=; b=cyry4K/KEs9uirUsUkL7wug+KwTpCxYMrGecNeCt0QxlVLui5NkKDInhO0dmkCpDfB Zwb0ngiWguJBxDaCDaWY/rtKykXbbx+gLQuHRaA/g6HG0POEvdqZ7hDP2T0mMeq6Ewhb eZehx1ZpKnkJXLrZBbNEGWmuN/cCFbALCgtvp9dDW63JO1oVOjb81poC1BRM2L9jSgtl 6eT3TJo5PX8njvXWlNAUjx2ob91Ogt/TgUWmexzyVd+D8R1gdSiz59Ues3X8y2kiN0BS ZRMRCNaNCLzFimg6jADZwK1hQpDnX64jXHPMJ6/6TwcEig8U7eDEjrkWzYXBsE5Fwl00 mAcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=r9j8e1y3uJ+lIEwImYYJrXBxHGgDtZ3NYM0TMNrxFbA=; b=QIXxljvnACf79+VfreT5bIE2M+FQQEBpAfQ8M+EHzDPPqBNbE35Xx3EbLACTT7tNfZ bNOw0xSEEJ+7imBPxJwtQrP8DF28p48LlCy50usg6ifwhO3O44zvY08AZWnR4SVDQeC8 BS/dhxv4KiAWvQnk+odLZuUzO9IIMXWym6pcxYNHDgkAJmr/2p8R1bvV04YzqEKyhzR2 y7HuTbSkVhm8DgssM61lzFM8CgVUrOlud6IpB/zxKUaHvA+3pNFS8O8vryt7R2buovmG zXu5AcBGj7VMOzG22eBVn3yE6QfnyRMqxx3NGU6E/wOVnzibGe3hXPLxRjgWpcFFkoV2 8E7g== X-Gm-Message-State: AOAM5303shKk8voTi0AeqEWo9g9rfQIpYqGwR0+hkukD9TFWduJmjdRd zwA2AkN2lDq1Xft1YuLpHEApdvjT+c5Epw== X-Google-Smtp-Source: ABdhPJx5ahU8H2UL3OF3xIRqooBLoO1q2olQTEE6gsxXVx7kLAac3xJ5LyoD4zr5xFbXT7tyFH6LPg== X-Received: by 2002:a17:902:bd45:b0:13d:b4d1:eb39 with SMTP id b5-20020a170902bd4500b0013db4d1eb39mr8450908plx.53.1633687104832; Fri, 08 Oct 2021 02:58:24 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.117]) by smtp.googlemail.com with ESMTPSA id mu7sm2121148pjb.12.2021.10.08.02.58.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Oct 2021 02:58:24 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH 1/3] KVM: emulate: #GP when emulating rdpmc if CR0.PE is 1 Date: Fri, 8 Oct 2021 02:57:32 -0700 Message-Id: <1633687054-18865-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li SDM mentioned that, RDPMC: IF (((CR4.PCE = 1) or (CPL = 0) or (CR0.PE = 0)) and (ECX indicates a supported counter)) THEN EAX := counter[31:0]; EDX := ZeroExtend(counter[MSCB:32]); ELSE (* ECX is not valid or CR4.PCE is 0 and CPL is 1, 2, or 3 and CR0.PE is 1 *) #GP(0); FI; Let's add the CR0.PE is 1 checking to rdpmc emulate. Signed-off-by: Wanpeng Li --- arch/x86/kvm/emulate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 9a144ca8e146..ab7ec569e8c9 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -4213,6 +4213,7 @@ static int check_rdtsc(struct x86_emulate_ctxt *ctxt) static int check_rdpmc(struct x86_emulate_ctxt *ctxt) { u64 cr4 = ctxt->ops->get_cr(ctxt, 4); + u64 cr0 = ctxt->ops->get_cr(ctxt, 0); u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); /* @@ -4222,7 +4223,7 @@ static int check_rdpmc(struct x86_emulate_ctxt *ctxt) if (enable_vmware_backdoor && is_vmware_backdoor_pmc(rcx)) return X86EMUL_CONTINUE; - if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || + if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt) && (cr0 & X86_CR0_PE)) || ctxt->ops->check_pmc(ctxt, rcx)) return emulate_gp(ctxt, 0); From patchwork Fri Oct 8 09:57:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12544853 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B1B1C433EF for ; Fri, 8 Oct 2021 09:58:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5AF1361027 for ; Fri, 8 Oct 2021 09:58:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239274AbhJHKAY (ORCPT ); Fri, 8 Oct 2021 06:00:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238917AbhJHKAX (ORCPT ); Fri, 8 Oct 2021 06:00:23 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21251C061570; Fri, 8 Oct 2021 02:58:28 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id ls14-20020a17090b350e00b001a00e2251c8so7382919pjb.4; Fri, 08 Oct 2021 02:58:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Dl7BY6WNXJJR9fsH+L/LsbUXC1kBJ8G31nGkyKnWPnk=; b=i0zW1RkpVkhPeM+XnTF/oFumSHN7BZbp6ADLaHTvAp7OsWmj0XarVWungxSR9+81Hd A52R2UyvwoRFtydGAjdpnGTUXurGPjk6BlibAteLzsoCP5AkUqirZIcSyKSOIrhIqAQY w1Mpg5i26GxaRQX6tPY5DIrOXdlmlSnHUvbHIPpV8KWoUbd/aorJsm5VFcQdOXH5t1TZ o7AQDkLcvU4u7Ia6Ihe7kL113Qzowc1wQAlGnfY2O2RRehyzR39SbPARJgytJ7n2U9qE T9wb0OQwvazGEU7bBIHUSTXrntttzfw+7/FzyK8VXPtgWfHbnc5v6oOu3zJu1YsHsnry FZ/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Dl7BY6WNXJJR9fsH+L/LsbUXC1kBJ8G31nGkyKnWPnk=; b=nqIPEkK1GgC04zsmi+W3wIUHPIi9cPMpnaA25DV8qc1BDdxaL8pU72mtCDvJAQwicj 7eZwozKnAql3EA+3Q9jho7/eXZUH1ME78s1n6rvAUwSSd8Gf7tjGX1tag5U107zjJhAK Mu+hbkv/nue0HL2KIaeNC+bzifGVYwzworvcKMz56gwWjxQyY8RMcT710IK4MtUBMG06 OTkoXrKMMiz06krrXAVyHvPM/Rh8nYbAa8pjSDMDtLKAEuWwWlveu3uRlRW2GglicjWB tqQXzwY+w3+M9VFhexZ/mkVDlc2NSv3b8jnjnudgEh1NY3zj1/AKoMf8TIdbB3/ITbQY Jtxg== X-Gm-Message-State: AOAM531757uW371u1VtG59PNnO8jJwPD1XQu/AO/8mAqIbSSj0lF4dny 0vbjEuFkz3VJawjKbCQYfIYe8Il+YGR44Q== X-Google-Smtp-Source: ABdhPJw/3xVjyJfbZbok1gzR3CkBi17qPMK9zFyU9VyOAl1ZVtaq4oOujGym6UVG4AGG4YTGaBdXjw== X-Received: by 2002:a17:902:6849:b0:13e:97cf:416a with SMTP id f9-20020a170902684900b0013e97cf416amr8394395pln.79.1633687107468; Fri, 08 Oct 2021 02:58:27 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.117]) by smtp.googlemail.com with ESMTPSA id mu7sm2121148pjb.12.2021.10.08.02.58.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Oct 2021 02:58:27 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH 2/3] KVM: vPMU: Fill get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL w/ 0 Date: Fri, 8 Oct 2021 02:57:33 -0700 Message-Id: <1633687054-18865-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1633687054-18865-1-git-send-email-wanpengli@tencent.com> References: <1633687054-18865-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li SDM section 18.2.3 mentioned that: "IA32_PERF_GLOBAL_OVF_CTL MSR allows software to clear overflow indicator(s) of any general-purpose or fixed-function counters via a single WRMSR." It is R/W mentioned by SDM, we read this msr on bare-metal during perf testing, the value is always 0 for CLX/SKX boxes on hands. Let's fill get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL w/ 0 as hardware behavior. Signed-off-by: Wanpeng Li Tested-by: Like Xu --- Btw, xen also fills get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL 0. arch/x86/kvm/vmx/pmu_intel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 10cc4f65c4ef..47260a8563f9 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -365,7 +365,7 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = pmu->global_ctrl; return 0; case MSR_CORE_PERF_GLOBAL_OVF_CTRL: - msr_info->data = pmu->global_ovf_ctrl; + msr_info->data = 0; return 0; default: if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || From patchwork Fri Oct 8 09:57:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12544855 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B21AC433F5 for ; Fri, 8 Oct 2021 09:58:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4564760EE3 for ; Fri, 8 Oct 2021 09:58:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239485AbhJHKA1 (ORCPT ); Fri, 8 Oct 2021 06:00:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238917AbhJHKAZ (ORCPT ); Fri, 8 Oct 2021 06:00:25 -0400 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 DB339C061570; Fri, 8 Oct 2021 02:58:30 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id g5so2785323plg.1; Fri, 08 Oct 2021 02:58:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=j2q5akxrmJ1uW+0AGmi44yh3ldXa7zUiHWRb/q4x2xg=; b=CUp0kbyb2DbyBRFmtl7ELnbt9XV1afFor5Db3z7mz42DwseAUsjls2ZQnKAgmDqOpX iAlmoptFfSBYTeFYl5n3aBUy/EHAV43DiR4W8xxflMJ2OdAYHWIV7Tx7Fc5mTGtf4wG4 29JvTILaDTjM8anolQHloTyjdYNeTRZjdUekx1HtHxV682eslYu0kMVF1xR9iNthFECA uYE1jtHBFodVrKG4uG/vkSsKyqiacLwWFgrxI6w1xbwBr6y07tKnRQXIVeOv2d1+Eq1k haqZ2a6SfvQ9RGMDxIJ9XID156ZCPRhe/liz4vyUZ78hTh/NyT4TX1aMhhJuJjR+QPDs kN8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=j2q5akxrmJ1uW+0AGmi44yh3ldXa7zUiHWRb/q4x2xg=; b=UeRd0iIvEtMmxwwO5GG6pyy4q+j36eys6X/OmOGm4hPClzsHCn/drD2rzF/IqeHK2s PUBQ3SFuTi2X14iXd6DJNut2btUZuXl977RAxq83O8qD2CInYOOibj9n9N4/3IkzH1Vl yeW/rvrKCYXt68O0M9ltbjhuSLwQgGcjAUreGKI6UFY/nEqWOOurKKo8zfsUIOOu38tg ystZSDysMgMGWfFCupWCqxTPnIVoovKzNWJKMVo+YJKtz05Ak7t70ruIm+lZrcKLd+CG +iWjyI7CalzQEItvreoZf/O2KvZPcYWFvuJPsLdItMk+N74A8fPSe8SVBqvw1DhVPRly +WkA== X-Gm-Message-State: AOAM531ykjoQvDvaiuVkFpbSC75Kfdasm7dlEVEfgM2+DGGdglPP2uhT v5zk23ctnrUDlkk21iWebHMg0Gcj3r6GdA== X-Google-Smtp-Source: ABdhPJwhQQWgKsMw2vJaY3bOAzQPsJaoztggPFtM32i4WbyVXZeAeGaxa6uLCny4XD+JtTebXTrYZQ== X-Received: by 2002:a17:902:7b98:b0:138:c171:c1af with SMTP id w24-20020a1709027b9800b00138c171c1afmr8589936pll.70.1633687110221; Fri, 08 Oct 2021 02:58:30 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.117]) by smtp.googlemail.com with ESMTPSA id mu7sm2121148pjb.12.2021.10.08.02.58.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Oct 2021 02:58:29 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH 3/3] KVM: LAPIC: Optimize PMI delivering overhead Date: Fri, 8 Oct 2021 02:57:34 -0700 Message-Id: <1633687054-18865-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1633687054-18865-1-git-send-email-wanpengli@tencent.com> References: <1633687054-18865-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li The overhead of kvm_vcpu_kick() is huge since expensive rcu/memory barrier etc operations in rcuwait_wake_up(). It is worse when local delivery since the vCPU is scheduled and we still suffer from this. We can observe 12us+ for kvm_vcpu_kick() in kvm_pmu_deliver_pmi() path by ftrace before the patch and 6us+ after the optimization. Signed-off-by: Wanpeng Li --- arch/x86/kvm/lapic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 76fb00921203..ec6997187c6d 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1120,7 +1120,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, case APIC_DM_NMI: result = 1; kvm_inject_nmi(vcpu); - kvm_vcpu_kick(vcpu); + if (vcpu != kvm_get_running_vcpu()) + kvm_vcpu_kick(vcpu); break; case APIC_DM_INIT: