Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp6741346rwi; Mon, 24 Oct 2022 05:39:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Iz/Ck3pY5hCaMLU6nFnOp4kBD+Zm75/Duc6z2pFp+ZVid7k1ZR/KVFj6XYiweyoUPC2Vo X-Received: by 2002:a05:6402:249b:b0:460:cb11:27d4 with SMTP id q27-20020a056402249b00b00460cb1127d4mr20266586eda.410.1666615161874; Mon, 24 Oct 2022 05:39:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666615161; cv=none; d=google.com; s=arc-20160816; b=JuaZjlxy5TYLOuRHNXwwq3VCzspeMlImnB83iek+/bQnWfHnYZiExBNQ1zeqJ1qVo+ OIE76apL4182L53GIUmYeJbveixq78vu7whZp+q2YrvAyT45e/5c+dKUwqQmfvklEz1/ V5MNZnL95MVxovIvFAdL5KXMg/2iTqEmKldJZZAYSrn9JOESpDpOA8GAoSF4WuTQyH6T oqls3hCGEu5GYMB1p+8o70PWCLvKaBXib/KntdSl3dKsPiBTygrRXQFCFxMBkvs/OIdq gQsFW3tYMy87RQzvdrgM/1nFnxJh5nvBwJk/vvjkV5YvfOOrPRCg2pz3CCp8dCDgU4jH SHVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KzY4nP3djJe6RIC2upnuKsaqW+WbkrFhlSUE3BL9+To=; b=tnXtv+lgvuIQ2LxbkbQpTL274t3ctYBYKPfrJOnbgVfwqegorroHcmzH4Mtg9ZmYCY 1s5xpgLK0oG1ZNjoloq2a9YKDYZGZacjdEinDAx9DyJGJQ0JDz6INHuFUkwWRc1uOA/l SEWohmYopXQeJe0HVX6Kgg75P/clTp12Z3j+OQNrcY0ZXYj5Q7iAlET4tCA4BKm+IGdN 1kDRvsXp+btzzcjjdyYBpBzIxiM7QYLEOdb0/Psd4+sD8Pa0KSNv/AF2oGw0gbrPQGah 5sjok5eWRJ7mZYCUGp18EUSDBXPYIy5pN51kVRm3IVG9cbkixZP6wr74NDzcbZFpFrJc f/Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=diz7BDPJ; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 10-20020a508e0a000000b0045d0660a039si25246195edw.316.2022.10.24.05.38.49; Mon, 24 Oct 2022 05:39:21 -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=@linuxfoundation.org header.s=korg header.b=diz7BDPJ; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229956AbiJXMGs (ORCPT + 99 others); Mon, 24 Oct 2022 08:06:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232902AbiJXMEh (ORCPT ); Mon, 24 Oct 2022 08:04:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85BCA7B596; Mon, 24 Oct 2022 04:50:46 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B2076612A1; Mon, 24 Oct 2022 11:48:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C33C5C433C1; Mon, 24 Oct 2022 11:48:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612120; bh=sZ/yz13J10ldzxTjOIIhRXD7fl+g9Se2enYqUrLTuGw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=diz7BDPJx8A1FscNKssBGqtZYJFT92mUTzTvWnBoL50VCJ5cJizqNvr+koWixml6p SmTknUzEjBUbaxrwE2YM5oHmsMqcWsGJrZrlvjITudRZtXvBTGEVdA/0iUV9EXFAXy qWcIHApxmT+/jdHjC7zU2q5iYEPRgqHgztlIDqxo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Jim Mattson , Maxim Levitsky , Paolo Bonzini Subject: [PATCH 4.14 078/210] KVM: nVMX: Unconditionally purge queued/injected events on nested "exit" Date: Mon, 24 Oct 2022 13:29:55 +0200 Message-Id: <20221024112959.596807202@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112956.797777597@linuxfoundation.org> References: <20221024112956.797777597@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 From: Sean Christopherson commit d953540430c5af57f5de97ea9e36253908204027 upstream. 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 Link: https://lore.kernel.org/r/20220830231614.3580124-2-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/vmx.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -11917,14 +11917,6 @@ static void prepare_vmcs12(struct kvm_vc */ vmcs12_save_pending_event(vcpu, vmcs12); } - - /* - * 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); } /* @@ -12236,6 +12228,17 @@ static void nested_vmx_vmexit(struct kvm nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); } + /* + * 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); vm_entry_controls_reset_shadow(vmx); vm_exit_controls_reset_shadow(vmx);