Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp786221pxp; Fri, 11 Mar 2022 15:06:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJz/ljiK+B216uEEoNxYp21ZGGIaSDi4J/bKj7Ys1PeAbOUfgXa34Nb4Iv+aP1zCElmZBgoD X-Received: by 2002:a17:90a:5794:b0:1b9:8932:d475 with SMTP id g20-20020a17090a579400b001b98932d475mr13216846pji.24.1647039978807; Fri, 11 Mar 2022 15:06:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647039978; cv=none; d=google.com; s=arc-20160816; b=TEfKO45T7m9mY6uw++JuQ6WsuARi7BQIABb+dsT5NHiNL/Lp05hCla7ZkltaAA9ZHF fccgMrocVzX5dxPn+Syq99yFxckox8taspF7o5VofNyl0+APZwBZAgA/rmHa2T7e+m0v 1df3c3VdpLNfiR0I5coDAWLi0+mqggqi2kWoyS/A7w1Wzqfs2R59QZ4oJxI/jZOfOZk+ eoalBniwnQ1Oiw9Ii8I64rHCODz984NblLYKeFWHswchyVehtEhWXqdm11tc6171F2nr mqyAaRrZZ8eJNfvtiVb3BGDCS12z4wdn0dkIsXbdS6h6OlmMmmMmorLZzrs9gYgfLV4Q ZFmw== 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=I+0PCK08cjf4M6d/EijWO6MxFUvmgvYBy0yuwDtEnpk=; b=GWWOlGhfv7gzSR2Pbew3gWrbNkIrQrPkCJOOJuKNpvHk8mJVooS6W+78HOUd6gF1mt Tz1UiFlzUhFochSr0q7okAKnQb3xuCYiRQnfWOSBpZ0FfMQ9K0au7cE99IIqk7FWdBPx +8xIUnPFC1Yc31IIts/Pvr+DviEQajo7G6StNf6sPKbx4ehNsudtFRMqe8qwEPhcN/kf mK7xdCGYOOjxdzovzZ/dsNBBTZrRZjmS6ppmQc+HMiz663DC+2z2rR0EVLsLwBRqTB90 aKRSd9RDJgBwqjpPN48bcqHpplg992vSaWAEvTi2pOWDPCxRblNTw7Sn15kfrO1837IM 8bGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=gSWPnigY; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id w22-20020a63f516000000b003743d5f74a2si9209614pgh.349.2022.03.11.15.06.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 15:06:18 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=gSWPnigY; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 966751986D1; Fri, 11 Mar 2022 14:02:24 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346044AbiCKD3Q (ORCPT + 99 others); Thu, 10 Mar 2022 22:29:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242089AbiCKD3K (ORCPT ); Thu, 10 Mar 2022 22:29:10 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CFD4EB305 for ; Thu, 10 Mar 2022 19:28:08 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id b9-20020a17090aa58900b001b8b14b4aabso4413877pjq.9 for ; Thu, 10 Mar 2022 19:28:08 -0800 (PST) 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=I+0PCK08cjf4M6d/EijWO6MxFUvmgvYBy0yuwDtEnpk=; b=gSWPnigYbX/RC9U7qKUlH9roio4rflbomJk7Ap7DYKERzce0Iionv/jdFxt70QdlHi 1wnOabBbi4hvTonv1Inf9a7CfmQHGagoM0DdCPUHi6X9lHD+DqFYxyVWh+dkk2V7flUx 6YkCA0sAONc025Unx9uYQagsk49dsH4TBmpXGcT7PF87bxMIgigQbLBVn9I5AhpWGT01 De7ZCA0sQOaN6UjiioD3taDE+i2ciZe8/QxNJIs1FGzwjvR46avRUNveVp4hHEbonFd4 JTl3+oHXtzjGUiM4ULQe9m85BYR+9lMbndDabaxrq8ISO9RMHBPkerPiLNCiCbUBO65i CYwA== 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=I+0PCK08cjf4M6d/EijWO6MxFUvmgvYBy0yuwDtEnpk=; b=HAJFqF9MA6TqVWuErV62DvA5XkFtFzFLHDgktp1llGTjDaOypY7gYHKY5BYM1qHvdd tvp4b0geAf866M13MH7FdLZ/fNeA18ZdztdmP9eNuburj0AWD1SrCBC50kE0VnYkLJP3 yYZ3cCnqU7qR0M0DSk+fC+Bik2VclgHvry2hD5uDwyThaDs5IaVzy5Kn/0ayVEd6SsfV nc0pG9h2ki1dYRzoogGD/RmgP/16rBWqAUAnK8oNhw5KceLofp0UgJlZ6aysATINOsr3 2Lp+6/m/mmU4ores/F1eSDGmhur3OoYLCyO/qLac1Osa73LgqZLLMpVpVFbjSZKMUpa9 R1rg== X-Gm-Message-State: AOAM533uRYFmCAd4dhbptTp4IsvaVFxefXXy2k3bv0w8xHeJAemCXFHK 0sxOP8R/nHAxuZl3rh4UtHc4WQVbHjg= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:903:124f:b0:151:76bc:6e0b with SMTP id u15-20020a170903124f00b0015176bc6e0bmr8632554plh.81.1646969287475; Thu, 10 Mar 2022 19:28:07 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 11 Mar 2022 03:27:42 +0000 In-Reply-To: <20220311032801.3467418-1-seanjc@google.com> Message-Id: <20220311032801.3467418-3-seanjc@google.com> Mime-Version: 1.0 References: <20220311032801.3467418-1-seanjc@google.com> X-Mailer: git-send-email 2.35.1.723.g4982287a31-goog Subject: [PATCH 02/21] KVM: nVMX: Unconditionally purge queued/injected events on nested "exit" From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Oliver Upton , Peter Shier Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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 --- 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 f18744f7ff82..f09c6eff7af9 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4233,14 +4233,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); } /* @@ -4582,6 +4574,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.35.1.723.g4982287a31-goog