From patchwork Mon Oct 24 11:28:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9683 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp535657wru; Mon, 24 Oct 2022 09:13:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7wSWZ9Dltfx9wHit/DVXtPctcT+kmoWXYCKj9hiMb1r3T0JFLuGIUfLUHM7FcPh3O5JBbn X-Received: by 2002:a17:90b:4c4f:b0:20d:4f5d:6b7c with SMTP id np15-20020a17090b4c4f00b0020d4f5d6b7cmr75803455pjb.77.1666628000348; Mon, 24 Oct 2022 09:13:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666628000; cv=none; d=google.com; s=arc-20160816; b=WCKW5WJTHlFnpXHt1u/ORHn+I3KgQCieQiI3Uwe9r9ztPlfEb3BVkuJrVqSFjGbAsL sCAZZUMGLBCULkaI71geacbaXBysy6EAfiiV3mf/JIxsfotnv32MFaPiHn01XOgCq+mX oMEITdGBpR1v4sZcjr2+zXS8mJFCr11KV+6PFRBvOrZqshjOogaEod9sGb66H+Tkucst QI4iIdwYAPcHvDby1w0FkYZhC7sCMQVb9v7TNHarlfr8EadyqwwxYslkSjIxTC74Qj9m 47D1rz4Mz5bMjEQWiySsLsKYiQOIzKBZsrHvPmJhQvp3ZUjtAeiVIn3xGVbtDSO4YPcW U8Jw== 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=XquGB7ztmj1GrtE7HZsYGBL70p6hz5x3qyHAJLyOtK0=; b=J7HCjqZK6ccMEIqPJ+wLAV8l1SItoIIO6fHO4Ynkpc5cKOTYhwT4Ry1f4yP4ZKbs/s FIOpiBTIeqN6AglZCLLUjFHKMqzS2whQJMiKHqEEaHHWcguz6XGRBs+uaJC4ppOMVpdt FSmQlOJEygarwbtzLQMJms8b9+SbCLJgpZdx6xaiwwpePuxOuZt8LorrYZVkru62FeFY 3UR2uHD9z9oC1+yqsfQFfzHGcoXQR0rba41W1gMIqBP42/l2DsNQa0vik1E6IAGpqVDs Kb8vcArdbPlVmBga0Pbxf1vc5Jfy74IoUkPz3qzGsG9hrzd3kDGjKq+ui7qg7JpMGjZi hidw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ENj87vOC; 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 186-20020a6301c3000000b0043c474c8942si35355229pgb.673.2022.10.24.09.13.06; Mon, 24 Oct 2022 09:13:20 -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=ENj87vOC; 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 S233791AbiJXQMl (ORCPT + 99 others); Mon, 24 Oct 2022 12:12:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232233AbiJXQGn (ORCPT ); Mon, 24 Oct 2022 12:06:43 -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 F37AC9411C; Mon, 24 Oct 2022 07:58:57 -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 06EC3B8164C; Mon, 24 Oct 2022 12:20:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AA08C433D6; Mon, 24 Oct 2022 12:20:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666614042; bh=oQk4boIzQr0mKRH6Myy62e23A9F+E219r+zhxeCBOcM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ENj87vOChg0wLIQUf8t8qM9lr/LvNOuqmoWmmu0b4YLOLDjF6DcyF8jCMdMcFEYz8 lXXNNgnNhQDJk5RK2u64kvhYMHYoPePH56bFCOrD2vqPA3P5LtVAzOdH1AaPuaRbVF a2hhKq4BAUR9301ZgnGAmYD+BBtl3Wl64XpMs7SY= 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.10 083/390] KVM: nVMX: Unconditionally purge queued/injected events on nested "exit" Date: Mon, 24 Oct 2022 13:28:00 +0200 Message-Id: <20221024113026.150551408@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113022.510008560@linuxfoundation.org> References: <20221024113022.510008560@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747586122299340157?= X-GMAIL-MSGID: =?utf-8?q?1747586122299340157?= 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 @@ -4183,14 +4183,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); } /* @@ -4530,6 +4522,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 */