Received: by 2002:a05:622a:251a:b0:39a:b4a2:e86 with SMTP id cm26csp543284qtb; Wed, 19 Oct 2022 08:30:09 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6BSFx+2uq9EEK3bp510fg4FhJ1Cb0OtLOv7YDpJhT4ZPZCOc20IGboriN3MNRRmH4DYN+u X-Received: by 2002:a17:903:200a:b0:184:1881:bfd1 with SMTP id s10-20020a170903200a00b001841881bfd1mr9207123pla.95.1666193409343; Wed, 19 Oct 2022 08:30:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666193409; cv=none; d=google.com; s=arc-20160816; b=OEB7Mru7LL6wEjvoEy8KhP0InMA+5SqV5rR3ceXnHMRP3B+5KA8iC0MNfwH+nwH0Cx ubbUppKcebBPdX5JyVOR3mznO46RQJ0FLXniWnx7m2PCIWwH+xnl99JMOXAfqz/78cJI 4pL0koyb6SoBQ5lHR1k052ur9KxcOE/Ie/kVtT4tHZ01CjRA/fyINU2hMskOUg7qFQGA 5hxqrkPdZRm8jtCbF0dosk1EQATAI4fShnlLYwLmGEEXWt84z4BzebXmYJEio2noanZV 9Ms49rpCS3EzaJ8oZBas9WzHK6zfOwLu7/RELyOFgiNA4CXiK8k7TwXxoKdqYrn20lNH 5eAw== 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=jAVMqIwSurztpBK4Fx97mGee+g/EVnS1UjIBw0KUuKE=; b=G9lIDr/ggKNk5ivpYYGyqK2KWzht8HUY7lYYEt0g1g8pqxbwjM+O1J8aJqxFaUi8GL dkS3J1Ofvp9iD0I4l4vXNbKxUxEPzfz2Pc1KuAc3rBynrVwjkMWMOHd5cOdJtBuNqG9w 3QtKg3HsBfEBvclIKTKnjePzcDPoM7pe2erG0VstJYh+XziQQo8MT41l9r1C2TtKQL+f oHm4Qx7aZ89LKNaRk7GF3Rbcait9gMH9f5TKsu7DZD9TZRB5cBWDxNKQaCZPGGpDq583 Se5J7T/dU8fawzD4H9CT2P2q+ZnOGeg52Vtbz9qpWheNXWH3PgbQEoCMwQhhcOSFTDXr ZSKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Tu1v46gI; 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 gn2-20020a17090ac78200b002006ace0cc4si186466pjb.138.2022.10.19.08.29.54; Wed, 19 Oct 2022 08:30:09 -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=Tu1v46gI; 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 S229635AbiJSOuM (ORCPT + 99 others); Wed, 19 Oct 2022 10:50:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231357AbiJSOtm (ORCPT ); Wed, 19 Oct 2022 10:49:42 -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 25E57AE6B; Wed, 19 Oct 2022 07:37:36 -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 E1352617FF; Wed, 19 Oct 2022 08:45:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E511DC433D7; Wed, 19 Oct 2022 08:45:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169151; bh=QDgl6VLYGMI6vRVHpkwYOT7zODods6LRMMu+wpec8zM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Tu1v46gINz1hQcV3xbqG2V+JDI8iYOfO4AGNfyeA+NCPZIAlKJgDE4CpjjHiTAL/C JR+/elk8b5sqTZtIk5dBv+9iiqWfAWvv26tRjYulOXPehFuLJphxVGmZmkMfZMWzck 9UEdolfRoLMnyhLL+ZzIm+Hg+N1G51eVZYloaN5s= 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 6.0 175/862] KVM: nVMX: Unconditionally purge queued/injected events on nested "exit" Date: Wed, 19 Oct 2022 10:24:22 +0200 Message-Id: <20221019083257.683809042@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@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.4 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/nested.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4255,14 +4255,6 @@ static void prepare_vmcs12(struct kvm_vc 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 * 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 */