From patchwork Mon Sep 25 17:34:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingwei Zhang X-Patchwork-Id: 144709 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp1661164vqu; Mon, 25 Sep 2023 21:16:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGvbN70sxJy6dCzOzWHvvaJ1oMfm8JXweCDbklboniDSyy8PBYMbsF+qGERKctzcg4vxl9g X-Received: by 2002:a05:6a20:d41d:b0:13d:ac08:6b79 with SMTP id il29-20020a056a20d41d00b0013dac086b79mr6115792pzb.60.1695701808371; Mon, 25 Sep 2023 21:16:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695701808; cv=none; d=google.com; s=arc-20160816; b=cJznV78DcSoA3ujqcD1IoEETj+/NkCf4XpS7aUizJyYVjiYfVSVtLxy5m2R/YpxR9f /Zn6e2KThtcNWkYSL8x9QK0CuzY2XkWKInv8L9RG2MWgginOPel3GH3WiU+cmM+D9bvv wuLvB8cVCq7lmvKd4IwcTQNceb4C8qHjCyI6QIg9RBwf+5pyDFAh5IJJDzHtdtUcREHt w5TqYDhl+79GjB2pjncoBSbZzW2spfRbSC9YvVj/kd86h+qNBErjkyuz+663Jgx9RGW/ VReOS0KKqVXpyc+j6lXFXpHWnNOMN39xZ5h55Z9e7OEkV/Z5X+y+RMzM3DwKHfMjggW6 lPhw== 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:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=N3mybIXWXmxE94oHFn/stJfySL7s2PMrxYppKn/7o5E=; fh=K+J2qYD/91bJS7zqTA6goMUf/yDX26DDWcmH+Ph084I=; b=E/ts+k9Oy62x3z5RYGGTiYacsD5seZG1b0Huq3WVQkyh2jeUECCL0cjMfBRt/ZXewT flE73pUaxerlAA3PrjXMBzuf2JaH59E3fTZ2oAo0YDwQG/iYqUk1LUuTcN1xPRd9pGB8 AHvVvQj5l4wsW6OsjCugwaIf+QV5ogTFLG5CIUYJUI5m7a+piaso5YRcNis29n2+Ce5W UETQxahUheLRFVwXobbnsXqAuC2BFJtQgE4wF+8ABxhgBYIwD4/DJ8xzL1ONXQ0ziKgR vMHGnQFUf0rS+hwRZRCTupI2TzMh7MgJFk8WLc9cEgzCyXMsL6nig4Rr7DlPmEpb8P6O 6o2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="l0fgHK/t"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id j18-20020a170902f25200b001b674055d72si10825833plc.621.2023.09.25.21.16.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 21:16:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="l0fgHK/t"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id BAE7F833488C; Mon, 25 Sep 2023 10:35:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233060AbjIYRfG (ORCPT + 29 others); Mon, 25 Sep 2023 13:35:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233065AbjIYRfE (ORCPT ); Mon, 25 Sep 2023 13:35:04 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE7AB10D for ; Mon, 25 Sep 2023 10:34:57 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-59bdb9fe821so130672627b3.0 for ; Mon, 25 Sep 2023 10:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695663296; x=1696268096; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=N3mybIXWXmxE94oHFn/stJfySL7s2PMrxYppKn/7o5E=; b=l0fgHK/tYNmZCoIFnp1u4GvzMn7MgmKgs5nOMB3MmccHqjSaqHWNLifvBUHB2HkP5G Cwg1jYsQ0A6KaBUrNEljJFvPilIDVizpCoxwsOs1pr/jCqQy/Y2ed9BE7Qo18KkFK1q6 1yc16MNM0qrsVAcwERqCi31kgrzr1sJW/o50OfMtO4H31yHnCfzx3Bf9BZg2m+HJUBix 8iepH2LOryPebM4GT/Ncx5s285FB5aECVxjqhDg2aSmVmYYi6T37XzHl8zlotEq5jaNR WR1e07AGxzoRzshTPoF8yOewo8YPbtadgqlVveMSntpz5LpIuf4tCg964F7u9IPzmCIh vqbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695663296; x=1696268096; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=N3mybIXWXmxE94oHFn/stJfySL7s2PMrxYppKn/7o5E=; b=wlSZvtTaXABm6jcSjaQRw5B1jM0OgLvN36IAB5zMQ7UHkG8unKhHFcQ5gduIzCJKV4 HAbq/cuhGQg/8IpzR0GeVwTWiL4aNktIBE3fX25oTOUawAotP3qQTffgyvLt28oSTIbE uSNH2Zb17AEx023i05GhyCaC/TCbNfkdNNFpUFi/4w40iRnQO0Cmcfnvy+7JNWY+Dylg Zu9SJQUi7slOzboM+q9yt1ii077s7mAE7+SXqNOrhyvYs/+wpnQvtjuNoN9W+/hJRfAl YOm+n5OtbsuB8QL8SBovpf3ftYvrKwtoaCwa+F7dSyGv/2W03hOfNSJS7ljdwA82VOiD YwFQ== X-Gm-Message-State: AOJu0YyEk01DBkiPrR2LjINEL/nhvb3vAzXgzipCerealzaMXVsiFXqi Sli6Os349c5k+YKwBmG2vgSrOpFiEJhb X-Received: from mizhang-super.c.googlers.com ([35.247.89.60]) (user=mizhang job=sendgmr) by 2002:a81:ac20:0:b0:59e:ee51:52a1 with SMTP id k32-20020a81ac20000000b0059eee5152a1mr93922ywh.10.1695663296760; Mon, 25 Sep 2023 10:34:56 -0700 (PDT) Reply-To: Mingwei Zhang Date: Mon, 25 Sep 2023 17:34:46 +0000 In-Reply-To: <20230925173448.3518223-1-mizhang@google.com> Mime-Version: 1.0 References: <20230925173448.3518223-1-mizhang@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230925173448.3518223-2-mizhang@google.com> Subject: [PATCH 1/2] KVM: x86: Synthesize at most one PMI per VM-exit From: Mingwei Zhang To: Sean Christopherson , Paolo Bonzini Cc: "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Mingwei Zhang , Dapeng Mi , Like Xu , Roman Kagan , Kan Liang , Dapeng1 Mi X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 (howler.vger.email [0.0.0.0]); Mon, 25 Sep 2023 10:35:12 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778037983301741875 X-GMAIL-MSGID: 1778072219398267704 From: Jim Mattson When the irq_work callback, kvm_pmi_trigger_fn(), is invoked during a VM-exit that also invokes __kvm_perf_overflow() as a result of instruction emulation, kvm_pmu_deliver_pmi() will be called twice before the next VM-entry. That shouldn't be a problem. The local APIC is supposed to automatically set the mask flag in LVTPC when it handles a PMI, so the second PMI should be inhibited. However, KVM's local APIC emulation fails to set the mask flag in LVTPC when it handles a PMI, so two PMIs are delivered via the local APIC. In the common case, where LVTPC is configured to deliver an NMI, the first NMI is vectored through the guest IDT, and the second one is held pending. When the NMI handler returns, the second NMI is vectored through the IDT. For Linux guests, this results in the "dazed and confused" spurious NMI message. Though the obvious fix is to set the mask flag in LVTPC when handling a PMI, KVM's logic around synthesizing a PMI is unnecessarily convoluted. Remove the irq_work callback for synthesizing a PMI, and all of the logic for invoking it. Instead, to prevent a vcpu from leaving C0 with a PMI pending, add a check for KVM_REQ_PMI to kvm_vcpu_has_events(). Fixes: 9cd803d496e7 ("KVM: x86: Update vPMCs when retiring instructions") Signed-off-by: Jim Mattson Tested-by: Mingwei Zhang Tested-by: Dapeng Mi --- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/pmu.c | 27 +-------------------------- arch/x86/kvm/x86.c | 3 +++ 3 files changed, 4 insertions(+), 27 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 28bd38303d70..de951d6aa9a8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -528,7 +528,6 @@ struct kvm_pmu { u64 raw_event_mask; struct kvm_pmc gp_counters[KVM_INTEL_PMC_MAX_GENERIC]; struct kvm_pmc fixed_counters[KVM_PMC_MAX_FIXED]; - struct irq_work irq_work; /* * Overlay the bitmap with a 64-bit atomic so that all bits can be diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index edb89b51b383..9ae07db6f0f6 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -93,14 +93,6 @@ void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops) #undef __KVM_X86_PMU_OP } -static void kvm_pmi_trigger_fn(struct irq_work *irq_work) -{ - struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work); - struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); - - kvm_pmu_deliver_pmi(vcpu); -} - static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi) { struct kvm_pmu *pmu = pmc_to_pmu(pmc); @@ -124,20 +116,7 @@ static inline void __kvm_perf_overflow(struct kvm_pmc *pmc, bool in_pmi) __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); } - if (!pmc->intr || skip_pmi) - return; - - /* - * Inject PMI. If vcpu was in a guest mode during NMI PMI - * can be ejected on a guest mode re-entry. Otherwise we can't - * be sure that vcpu wasn't executing hlt instruction at the - * time of vmexit and is not going to re-enter guest mode until - * woken up. So we should wake it, but this is impossible from - * NMI context. Do it from irq work instead. - */ - if (in_pmi && !kvm_handling_nmi_from_guest(pmc->vcpu)) - irq_work_queue(&pmc_to_pmu(pmc)->irq_work); - else + if (pmc->intr && !skip_pmi) kvm_make_request(KVM_REQ_PMI, pmc->vcpu); } @@ -675,9 +654,6 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) void kvm_pmu_reset(struct kvm_vcpu *vcpu) { - struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); - - irq_work_sync(&pmu->irq_work); static_call(kvm_x86_pmu_reset)(vcpu); } @@ -687,7 +663,6 @@ void kvm_pmu_init(struct kvm_vcpu *vcpu) memset(pmu, 0, sizeof(*pmu)); static_call(kvm_x86_pmu_init)(vcpu); - init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn); pmu->event_count = 0; pmu->need_cleanup = false; kvm_pmu_refresh(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a6b9bea62fb8..6f24a8c1e136 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12820,6 +12820,9 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) return true; #endif + if (kvm_test_request(KVM_REQ_PMI, vcpu)) + return true; + if (kvm_arch_interrupt_allowed(vcpu) && (kvm_cpu_has_interrupt(vcpu) || kvm_guest_apic_has_interrupt(vcpu)))