From patchwork Sat Oct 22 07:20:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7422 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1097702wrr; Sat, 22 Oct 2022 01:08:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7UGnuBPnWXFQCUd19vdZSJmrud6q++jA2RezgPETGFxMruj9aCmFgKiOnOyG0V94OVwShP X-Received: by 2002:a17:90a:2fc7:b0:212:e22a:5ba9 with SMTP id n7-20020a17090a2fc700b00212e22a5ba9mr2644022pjm.70.1666426110121; Sat, 22 Oct 2022 01:08:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666426110; cv=none; d=google.com; s=arc-20160816; b=H50drzufqWj3IwjPaRstXXbEX9IuOd/8FagwSWkqWsGC8O+K9f38HSpnCiD+vZK6Ap 1dOIir4/iVWfcYU+mpP7fH88pzbFIOxFqwCCVH/e6ffkF/t9IYHvg8y/IJ5iBdH1H3pg dEXZ1cAU84tHkoq8LMp4zaeY9P+bDfhp4Mjp/y+C/WrG3VB/ma3wtnWEC5HYU8htDUkE O4DKSJA6R4i82i7wP0UvWxig0G44QjKQTxvoiyVZ4y9VCa3ynjYNUg+RIvjG0Ao1uLlS 1XEEcBXOPZNZmBDwRly7E5cqOwFsQS+Xda4F6aVNDkVMrGtyGOmI8HDCRW6JbeFtvlzA ln4A== 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=dqrjGNoiwbfuYnKGCG7SEaWhL6f65uXbyh6QMdoMhcM=; b=VdFmX+yUpU5YUPiAOmsjn+5SzrjkpGJyd+5tv2uyKUcsTB79Sc0D0WZAty1WDfEBjq k2PQ16uLTBifWDwkoNqrD95InUCjwMu0duDT70CZUdZbQU7C0SxLb2oQUMV+h2lVNPs5 jzLbdIZvIm5bfZwFzt0YZKXot4cZVxy38N4bk4VgIlxAt6QBBG3iX5QkPToktQRB8mtA rUAb5JqkilUfbdZ2JFPphCVEJSFyy+BwxDvbiPA3pY4Ugrlt9rvZUIwfaEk77pZUFS00 bLdSXfCLnY8ToOcK06sotGrlJOAhNF/XH5QJPETSVcGTHK5fmkFSTU0blHsbqrHYp4tK d2oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="DQPlK/dA"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y11-20020a056a001c8b00b00562c5e2a99bsi22010904pfw.163.2022.10.22.01.08.17; Sat, 22 Oct 2022 01:08:30 -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="DQPlK/dA"; 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 S233009AbiJVIH5 (ORCPT + 99 others); Sat, 22 Oct 2022 04:07:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232789AbiJVIEM (ORCPT ); Sat, 22 Oct 2022 04:04:12 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A901B2C172D; Sat, 22 Oct 2022 00:51:41 -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 ams.source.kernel.org (Postfix) with ESMTPS id D26C2B82E10; Sat, 22 Oct 2022 07:41:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D52AC433C1; Sat, 22 Oct 2022 07:41:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424482; bh=R13yed14VpFlurbv9KTJhuYBznnXpn9hlLiQROjkOXo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DQPlK/dAOYnk/wNXbNp3LCJd+dnr1qzQL938GR6GSV8WZpFH+yRFELnhSzRMzK9Ae 6YI+EycKnkAkBHyHwW3QWCAkXUUDv5Zs9uwUAjixEQUt6qZiJjTncQo6HcjX1uQ+gw dRVJQ6pMEjj+wkIPWSTar8C0/MxDy0ShJQlq/vag= 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 5.19 161/717] KVM: nVMX: Unconditionally purge queued/injected events on nested "exit" Date: Sat, 22 Oct 2022 09:20:40 +0200 Message-Id: <20221022072443.957543312@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747374425093900129?= X-GMAIL-MSGID: =?utf-8?q?1747374425093900129?= 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 @@ -4264,14 +4264,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); } /* @@ -4611,6 +4603,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 */