Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1310464rdb; Mon, 2 Oct 2023 06:03:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFOxR2GAmDtuhx+e/Yv0FDcpIneyoeTFoZYI+D0ondwoxNZOT3Rr05ArF94za0BwzKA3hN3 X-Received: by 2002:a17:90a:950e:b0:277:1355:f9e9 with SMTP id t14-20020a17090a950e00b002771355f9e9mr8366571pjo.22.1696251812702; Mon, 02 Oct 2023 06:03:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696251812; cv=none; d=google.com; s=arc-20160816; b=OgZOB0WwDGn3qIJ7oUoS8TWiWunefsXvesb5omti+qAUF3IiaI6TOhG7RxFmB1Y68F KsiJLxOQ1GZVpzGb8bY4dOj/nbvNBadWxxfE+q7e4VD3odChu7TaUCKUX8lrUDoQUFFJ Wxdr0Tmzy9qcYc0CB2Vnl+/qSUXno68WVUZT6nTNOgw9CXp5qo6HKeF3tGwBdS74DHAK eAHFz0Dd5VAW8p0x5QH3Y5mCNVx9SplhFae5AeDSy7d1xmLXSxkKunzbDvkSAZl0PxZe eZX56r6HBSz1+IjW6dEluZP/eSULNwxvQ/3/B4XywT3gvC4jHN/Hnjnhc9rjt5JET+LK w5Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :reply-to:dkim-signature; bh=d9RyKi2tLeQpV4Sj/8aOYjchp6vn0YLRcfFMMaJdDOc=; fh=+CFzKGmtXkCLY+Wq/3tTul82xJZK+jVX5+b8+nbwMIE=; b=MdYg7DG1WdIKKpugbkbtAYt+5g/VEnI7j7Vanv8s+dDgEa6NaGL7ipygoK2Z1axRO5 Ve5deJ/pWZk3gFpesK9XvrhIUaEMsf/esu/D7uwSc++36ngs6B3XFsf4ZK07kH3nnkYY ttVj2CUrqtvoPhPNSLqUp4bLzRtzEq6FMD8Cac5plyKiBkDeDBJCbZ15hp4VSzpSp/my 0SYeRZMJZV9JrlJZH9Y3h4Jk4INjmo0I42UpeYTtjW0OOV6mMUNgZIl6ASgC9OeDqUcg 9+pCuLkQufIS85V2kZVAuYSbRZbhkLAUzQPVD7QgSUZ/rt0OqYbmbV9XwLXkud/PVvfS FBvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nuhkbkB6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id c3-20020a17090a8d0300b0027686905e79si7627263pjo.146.2023.10.02.06.03.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 06:03:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nuhkbkB6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 9FF198065E09; Sun, 1 Oct 2023 21:09:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229990AbjJBEI7 (ORCPT + 99 others); Mon, 2 Oct 2023 00:08:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbjJBEI6 (ORCPT ); Mon, 2 Oct 2023 00:08:58 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ECF8D3 for ; Sun, 1 Oct 2023 21:08:55 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d86dac81f8fso17259775276.1 for ; Sun, 01 Oct 2023 21:08:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696219734; x=1696824534; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=d9RyKi2tLeQpV4Sj/8aOYjchp6vn0YLRcfFMMaJdDOc=; b=nuhkbkB61/9L0+ARVQso1isocI+VwKg/Dkn+iHemmNXL6SFoB8XqE1HNZLdKmUe7b+ LOnjL9SEyL7+3aZMWI/Aq3tFiB1+0UYl7zqDOKMaZcpaJCVdmxTEWmFpB8vMFgH28lsf 8aJK+zQlpYFsXBgcHZxZjxmszV4/0hTQUCxZDgxhAipNfzIKgEQHjRGJRTe8ZY8uZfvo tpXBIjTdlWVj9ceHFTy1GnmAb/i/9nhm9a2/08iqzWPxmjhzcfph2iiyvm2ZJzcDHJ9Z kRnWhy4HlP1NtVkBg0nxwZkvk1TrtRRpU2eB4+XWtrh7rXiNrrV4YV1WVf6ZCVKsWfDc l7TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696219734; x=1696824534; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=d9RyKi2tLeQpV4Sj/8aOYjchp6vn0YLRcfFMMaJdDOc=; b=WpjvCZEowE5L8tyq1WnFEaIDSc7EB9Yt/P+3LvqHtHDFYQQnxz0Av0cyCzQLBXIbau e15rcDZvNjQskGGgDe1hMLd+I/e85PlrNm8NezLT21Zh7Vc7z3AW8J5x4W6XAskZ+H/X KRIw/VlFu3xwLxg3Idged9RgIksGPoFSKgAiFMfP8OKXxbPYAQQ6u5jLD410n3Cieumb GEG8a5ah65w2x8zp6+sO8U/KYySnVt3bgWL+H0rpXHj+c6J6FtIRT09LsuRsAT6xadgv /8ml9WhiyG/Vg6jFHDeySxdyautIPJHtMIFyb9DzwDGthSOb1J5kRlglc8agpA41uz6O VhiA== X-Gm-Message-State: AOJu0YwiBMJ5U0dsEWLVIwgDqnDoOT61lnBg/xF0D7gGmiy6KJ6aJMRD 5ykSMgg6UyorZAhLEtsTg+CeJC35LfBa X-Received: from mizhang-super.c.googlers.com ([34.105.13.176]) (user=mizhang job=sendgmr) by 2002:a25:ae84:0:b0:d0c:c83b:94ed with SMTP id b4-20020a25ae84000000b00d0cc83b94edmr151069ybj.10.1696219734572; Sun, 01 Oct 2023 21:08:54 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 2 Oct 2023 04:08:39 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <20231002040839.2630027-1-mizhang@google.com> Subject: [PATCH v2] KVM: x86: Service NMI requests after PMI requests in VM-Enter path From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini Cc: "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , Jim Mattson , Like Xu , Kan Liang , Dapeng1 Mi , Xin Li Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email 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 (pete.vger.email [0.0.0.0]); Sun, 01 Oct 2023 21:09:17 -0700 (PDT) Service NMI requests after PMI requests in vcpu_enter_guest() so that KVM does not need to cancel and redo the VM-Enter. Because APIC emulation "injects" NMIs via KVM_REQ_NMI, handling PMI requests after NMI requests means KVM won't detect the pending NMI request until the final check for outstanding requests. Detecting requests at the final stage is costly as KVM has already loaded guest state, potentially queued events for injection, disabled IRQs, dropped SRCU, etc., most of which needs to be unwound. Move the code snippet processing KVM_REQ_NMI to the one after processing KVM_REQ_PMI to make KVM vPMU more efficient. Note that changing the order of the code snippets does make any functional change to KVM. KVM allows two pending NMIs while x86 architecture allows only one pending. This is because KVM may not be able to process NMI quickly. Therefore, if a PMI generated NMI is coincident two other NMIs pending (or one other pending NMI and KVM blocks NMI) in KVM, then it will be dropped and it is architecturely ok, since x86 CPU does not guarantee the delivery of each NMI. Using SPEC2017 benchmark programs running along with Intel vtune in a VM demonstrates that the following code change reduces 800~1500 canceled VM-Enters per second. Some glory details: Probe the invocation to vmx_cancel_injection(): $ perf probe -a vmx_cancel_injection $ perf stat -a -e probe:vmx_cancel_injection -I 10000 # per 10 seconds Partial results when SPEC2017 with Intel vtune are running in the VM: On kernel without the change: 10.010018010 14254 probe:vmx_cancel_injection 20.037646388 15207 probe:vmx_cancel_injection 30.078739816 15261 probe:vmx_cancel_injection 40.114033258 15085 probe:vmx_cancel_injection 50.149297460 15112 probe:vmx_cancel_injection 60.185103088 15104 probe:vmx_cancel_injection On kernel with the change: 10.003595390 40 probe:vmx_cancel_injection 20.017855682 31 probe:vmx_cancel_injection 30.028355883 34 probe:vmx_cancel_injection 40.038686298 31 probe:vmx_cancel_injection 50.048795162 20 probe:vmx_cancel_injection 60.069057747 19 probe:vmx_cancel_injection v1: https://lore.kernel.org/all/20230927040939.342643-1-mizhang@google.com/ Suggested-by: Sean Christopherson Signed-off-by: Mingwei Zhang --- arch/x86/kvm/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 42a4e8f5e89a..302b6f8ddfb1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10580,12 +10580,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_SMI, vcpu)) process_smi(vcpu); #endif - if (kvm_check_request(KVM_REQ_NMI, vcpu)) - process_nmi(vcpu); if (kvm_check_request(KVM_REQ_PMU, vcpu)) kvm_pmu_handle_event(vcpu); if (kvm_check_request(KVM_REQ_PMI, vcpu)) kvm_pmu_deliver_pmi(vcpu); + if (kvm_check_request(KVM_REQ_NMI, vcpu)) + process_nmi(vcpu); if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) { BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); if (test_bit(vcpu->arch.pending_ioapic_eoi, base-commit: 73554b29bd70546c1a9efc9c160641ef1b849358 -- 2.42.0.582.g8ccd20d70d-goog