Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1113689imi; Fri, 22 Jul 2022 17:53:39 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vtDvekp8t0HQgcDfRDw0XOvkMkHl8H4CpCNj324SHgHo9g6GJgnxYRWxFkR21c4WBegYVj X-Received: by 2002:a17:907:6e0f:b0:72e:d066:dfe5 with SMTP id sd15-20020a1709076e0f00b0072ed066dfe5mr1893224ejc.558.1658537618384; Fri, 22 Jul 2022 17:53:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658537618; cv=none; d=google.com; s=arc-20160816; b=q8T10+7uyN2oG1fyUmvJs3VMH7I78LNzxCpmtsdt4TIZh8/DTPe9BzQDI7WFZuV+Fn QABpIH2eGb5C2kthnn5axBwmzPUI37n+t1AewQi0QtzbATNRmhyIV5GwUPe2Xoqba8cG uV/akwhmBrcwcBL91cq1vkEB/ZvcNBxA0KJXjo5Lek6d7Nzw/K7a2CvAAHqNKJXbC805 PSu0gG6T9OELVKdg4/zjC8eYyqaIbjNYuJuLUtLEO2SsBUNyUaItaOpoLBQkqGZgmOYm 7skrLuYeTvl5eGhtmfBGv7RpvXYIsfIhbTiTy3zAv949i4bE0Sesr0KXrsJGRIvgUHrT s67A== 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:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=dDbBsniYzHp/DkWFlMs8orp7Z4WhIFDdPFs5eLnWS08=; b=ZLRLDMe4PhAUPghuvzzIOljor+kN+40809Z5z7d1meBIImhzjNn0xeFHWPH/qCF5he z7VdYtEKMjISquIWWITJ2IX+l4BuNjxP8JeeI05F+hm0QgUHYbefug9Sw/m7xYj3pps+ GI/rGKQSuyeYER7Zp5GP6sUkRJ0bAN6c1XENIF4du7gU7MakNMjzH0EFdHpCxBghwQUS rj3FYkfpcJl93Axwqe3bJfJOpZxb5A+/OqWZhjzmg2AQBI0DTX61bQ8QqOtnyKXsnHQH AH6HCsLXlB3O1D2yxAbim9/LFjC7S7dCtv9lW6tW9WVjABGHjbfNmbUAab25nnQla2bK V2cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=WyABck1L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w18-20020a05640234d200b0043bc007de84si8646213edc.46.2022.07.22.17.53.12; Fri, 22 Jul 2022 17:53:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=WyABck1L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235966AbiGWAvr (ORCPT + 99 others); Fri, 22 Jul 2022 20:51:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235386AbiGWAvp (ORCPT ); Fri, 22 Jul 2022 20:51:45 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB22A13D7A for ; Fri, 22 Jul 2022 17:51:44 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d185-20020a6236c2000000b0052af7994d56so2441528pfa.16 for ; Fri, 22 Jul 2022 17:51:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=dDbBsniYzHp/DkWFlMs8orp7Z4WhIFDdPFs5eLnWS08=; b=WyABck1LPPvjC6je72o/2E18L+yh/Fefg4gzXmU5uIknt5PWWm5Juk50jGnGUtiQnX hL+pyIBGmbbkOSj3PqffyytipWE5zL2OikSniX+VkGUBtgsE/QHMIORPqtS+ULO3Zsaa ip8JF1lShaoow/UOoWUm2xuTcdwdRR7wAlhiAa4RLE45hJ8wce19sHdZdHdllfSkLK6V LOqw13njtxrvL89XeQ7yv0bKFbXteI/wBieQBJ+L+BYTKD4Dbm0ODZNY2jIuJxf9MG75 61waSCk9Pa1sLCf7zN07XU0d1YcML5jhU8F//+FbyNdZvfCA6QT4vDmCl3tA8S74YScj PyoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=dDbBsniYzHp/DkWFlMs8orp7Z4WhIFDdPFs5eLnWS08=; b=gem+JWuL8Ut2fC//8rf81yT94lFBwNNlDqjtMzOenoICidEXXMUMX9/QXHh3POqrEQ qnxeWfr8nkqtP4WypRP7cQG5mji/SahwZC+OnLFiKfRrsmtNhuZQv4dUGhyvtHGw95eP tpsE4nYTc1BLX0Eg41f5Nss/Eg0EDDVJ3lUUntLrvcUrUBCmtpt3ronoK8VsgRABSns2 gYjacXNujEDiZ00JDJoupYVCArPxfqGbCJAKHgAnjbHrjfFLjCFWzDLc5ZGrE0TmIM+1 QPWKeyOWRxk+CmHyN3ScviOfrqA+O4vw9xsZCqi80vZky1wpCcDYqXfMLT5ystMSZoSD 9RtQ== X-Gm-Message-State: AJIora+TMRACeYb4A/wOfEInaHEuJ8E9Zh769hBeWrwfnIm8WLglyqKU CMvnJ2fyJs9JmG5WPNzfOHjzqCww1wg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:7784:b0:1ef:c0fe:968c with SMTP id v4-20020a17090a778400b001efc0fe968cmr19848862pjk.26.1658537504313; Fri, 22 Jul 2022 17:51:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Sat, 23 Jul 2022 00:51:14 +0000 In-Reply-To: <20220723005137.1649592-1-seanjc@google.com> Message-Id: <20220723005137.1649592-2-seanjc@google.com> Mime-Version: 1.0 References: <20220723005137.1649592-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v4 01/24] KVM: nVMX: Unconditionally purge queued/injected events on nested "exit" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Maxim Levitsky , Oliver Upton , Peter Shier Content-Type: text/plain; charset="UTF-8" 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=unavailable 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 Drop pending exceptions and events queued for re-injection when leaving nested guest mode, even if the "exit" is due to VM-Fail, SMI, or forced by host userspace. Failure to purge events could result in an event belonging to L2 being injected into L1. This _should_ never happen for VM-Fail as all events should be blocked by nested_run_pending, but it's possible if KVM, not the L1 hypervisor, is the source of VM-Fail when running vmcs02. SMI is a nop (barring unknown bugs) as recognition of SMI and thus entry to SMM is blocked by pending exceptions and re-injected events. Forced exit is definitely buggy, but has likely gone unnoticed because userspace probably follows the forced exit with KVM_SET_VCPU_EVENTS (or some other ioctl() that purges the queue). Fixes: 4f350c6dbcb9 ("kvm: nVMX: Handle deferred early VMLAUNCH/VMRESUME failure properly") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Reviewed-by: Jim Mattson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/vmx/nested.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index b9425b142028..a980d9cbee60 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4255,14 +4255,6 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); } - - /* - * Drop what we picked up for L2 via vmx_complete_interrupts. It is - * preserved above and would only end up incorrectly in L1. - */ - vcpu->arch.nmi_injected = false; - kvm_clear_exception_queue(vcpu); - kvm_clear_interrupt_queue(vcpu); } /* @@ -4602,6 +4594,17 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, WARN_ON_ONCE(nested_early_check); } + /* + * Drop events/exceptions that were queued for re-injection to L2 + * (picked up via vmx_complete_interrupts()), as well as exceptions + * that were pending for L2. Note, this must NOT be hoisted above + * prepare_vmcs12(), events/exceptions queued for re-injection need to + * be captured in vmcs12 (see vmcs12_save_pending_event()). + */ + vcpu->arch.nmi_injected = false; + kvm_clear_exception_queue(vcpu); + kvm_clear_interrupt_queue(vcpu); + vmx_switch_vmcs(vcpu, &vmx->vmcs01); /* Update any VMCS fields that might have changed while L2 ran */ -- 2.37.1.359.gd136c6c3e2-goog