Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1878654rdh; Tue, 26 Sep 2023 06:22:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGug/aIiVUkbP+KwgsyWIscAaD0aK+ncfhIQ586h7+aTNX5mEFzZOr8secup3OxWfhF+ib0 X-Received: by 2002:a05:6a21:18a:b0:13a:6bca:7a84 with SMTP id le10-20020a056a21018a00b0013a6bca7a84mr13494290pzb.44.1695734556625; Tue, 26 Sep 2023 06:22:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695734556; cv=none; d=google.com; s=arc-20160816; b=IrwbzeT+0ZqtScKsfvJEDaB0XQhUbBHexeQVUMS9Og/Mb40dWPfRIOlonfjdb/9POo SBp30X9Olyo0gzwwFFscGH7TjdkTG3WWYsOGnPZ3qS9g+o/XwH44PMC//5NJRQi3B+gY gvqyD/gAedi0IYb6sUz+f+BAIzl6cEotw5TfsiUmxOu9uU+q6aVF3V7cMZyug0P7J9CD BWaK2t11yUNKGErruiP6wLALPg4XeWuysRjyyhI8nwQpBhgONNu/55qCgVPHyrDxkrTO ADOlybvZ/q5V2ysamcTzeY5P3E76BW2YIkmtN20MDkhq9HfWSgIbDX7w9xlgLX5kU1qY PT0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=gduy8ABBWRcIZ6Nsk8bBDlRqu/MEicJSudL4Sfih2Ds=; fh=iPyjJpaKqGoQMmq/R72nL5VcH3rHgl6MHiSCLlV8D74=; b=XBWHgadLW0474+0QZwTGKaiL4aYXGaPjtO2MoGtM+0AjL3v3OhrPFvCOgf2/VnYpfk 9RZh0NaJf2Sz8AdRERc86wBAmnd/FdGqN1xMv1Ggf7FgE55eTmCVCCuhJ971n3r39OMG uB5ootnFdGrOd2dvi+zYISvAFSqwalcefTt47guDAio4iNlNIVDzzxypN+PYMVJ3ZHxA O2ZLk7IobUwXeLNoSFRMDwh3flgXxSS20aVH7yaHj45O7myUi72jBtqQVUo4QYa6jeA4 62F5wYjB6lAWz/yWSxiJn7llEqNxEGK3PuY0mS2p9M3BHiTQVuLGW3hpiIe9IXQD8MNJ 5pog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=C7ensMbe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id q26-20020a631f5a000000b0057c9e0c7bb7si10505013pgm.77.2023.09.26.06.22.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 06:22:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=C7ensMbe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 7521F80BB522; Mon, 25 Sep 2023 14:28:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233421AbjIYV2c (ORCPT + 99 others); Mon, 25 Sep 2023 17:28:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233389AbjIYV2a (ORCPT ); Mon, 25 Sep 2023 17:28:30 -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 697AB11B for ; Mon, 25 Sep 2023 14:28:23 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a1d352a86dso20352597b3.0 for ; Mon, 25 Sep 2023 14:28:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695677302; x=1696282102; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=gduy8ABBWRcIZ6Nsk8bBDlRqu/MEicJSudL4Sfih2Ds=; b=C7ensMbe9mQG6kdqSlIuCX6z+QWIjBJt9BCclgLqOF5ibWXFgO6dT9M20Kuz3dWM65 Fl2gGaF39fHJALOuC86Q+vjwZmJOpRbw0v0Ol1CMZt6GbAxezCgZWbfwM/vSOGWP1+ZE hvYYOvy6HcHQhWZah6QupkAnT9+XKHD/Arfy5TTY6HeqZZLp7rx6cQaa1SbMHx/CkkMN bl3DCTr1F59UKUPBc7f62D4zDCsc7/fy4YVNUS5TR76jBb8ZLzEzCRu49hCkBfQvZf2m eNOUAElab//HOB9Iyis4E7dyya20cRM8wNagPmQSD2FMbtwur5ehM3ywGWfayoj/QdUz jcXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695677302; x=1696282102; h=content-transfer-encoding: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=gduy8ABBWRcIZ6Nsk8bBDlRqu/MEicJSudL4Sfih2Ds=; b=UC8u/lXgiG6wpw6y35NTmHM26iv78Ey1s8s4kJQjEvp+M0HkcNQTAO+/P57S7dWrIk fPo5qR45NC5T0zr1m/yUASBkFZW6XRCJyjXJA/RjlJ+aVIs9IPZuyWhNJe5D2FbuFzro Pivca+lR8CIfdQWjvv15HI+dET5KxbXaazdJS2GPGMfFfViOZtc8NSDhuKohrF2XnQEV NghaAJMfPb4/j9h6rFFUbWZ+BOb2mqAqTcjEGk8ci2kDTkl/jrJZhOW9++PT4gry/vEJ 8Qygm6vEfqQY9v39C7zW6TijrE8f8jXdM18jPowjvW4n+qOSaqMHtmaPKXJElnibHMn1 HU7Q== X-Gm-Message-State: AOJu0YyC6kiJLISTJqcZ/dt2ZRmJVoLOo/ci0M7Ezudhx9yb+1e6eqq5 k8hk8L1knH0LPR9nVGGKLcFi96Fu2V0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:ac56:0:b0:59b:ee27:bbe8 with SMTP id z22-20020a81ac56000000b0059bee27bbe8mr91467ywj.7.1695677302634; Mon, 25 Sep 2023 14:28:22 -0700 (PDT) Date: Mon, 25 Sep 2023 14:28:21 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230925173448.3518223-1-mizhang@google.com> <20230925173448.3518223-2-mizhang@google.com> Message-ID: Subject: Re: [PATCH 1/2] KVM: x86: Synthesize at most one PMI per VM-exit From: Sean Christopherson To: Mingwei Zhang Cc: Paolo Bonzini , "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Dapeng Mi , Like Xu , Roman Kagan , Kan Liang , Dapeng1 Mi Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 groat.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 (groat.vger.email [0.0.0.0]); Mon, 25 Sep 2023 14:28:41 -0700 (PDT) On Mon, Sep 25, 2023, Mingwei Zhang wrote: > On Mon, Sep 25, 2023 at 10:59=E2=80=AFAM Sean Christopherson wrote: > > > > On Mon, Sep 25, 2023, Mingwei Zhang wrote: > > > 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 th= e > > > 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 PMI= s > > > 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. > > > > Unless Jim outright objects, I strongly prefer placing this patch secon= d, with > > the above two paragraphs replaced with my suggestion (or something simi= lar): > > > > Calling kvm_pmu_deliver_pmi() twice is unlikely to be problematic now= that > > KVM sets the LVTPC mask bit when delivering a PMI. But using IRQ wor= k to > > trigger the PMI is still broken, albeit very theoretically. > > > > E.g. if the self-IPI to trigger IRQ work is be delayed long enough fo= r the > > vCPU to be migrated to a different pCPU, then it's possible for > > kvm_pmi_trigger_fn() to race with the kvm_pmu_deliver_pmi() from > > KVM_REQ_PMI and still generate two PMIs. > > > > KVM could set the mask bit using an atomic operation, but that'd just= be > > piling on unnecessary code to workaround what is effectively a hack. = The > > *only* reason KVM uses IRQ work is to ensure the PMI is treated as a = wake > > event, e.g. if the vCPU just executed HLT. > > > > I understand Jim's desire for the patch to be more obviously valuable, = but the > > people that need convincing are already convinced that the patch is wor= th taking. > > > > > 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 wit= h > > > a PMI pending, add a check for KVM_REQ_PMI to kvm_vcpu_has_events(). > > > > > > Fixes: 9cd803d496e7 ("KVM: x86: Update vPMCs when retiring instructio= ns") > > > Signed-off-by: Jim Mattson > > > Tested-by: Mingwei Zhang > > > Tested-by: Dapeng Mi > > > > Needs your SoB >=20 > Signed-off-by: Mingwei Zhang Thanks! Jim gave his blessing off-list for swapping the order, I'll do that and mas= sage the changelogs when applying, i.e. no need for a v3.