Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3E33C433EF for ; Tue, 14 Jun 2022 20:47:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236064AbiFNUrn (ORCPT ); Tue, 14 Jun 2022 16:47:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234111AbiFNUrj (ORCPT ); Tue, 14 Jun 2022 16:47:39 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B66B1E3DB for ; Tue, 14 Jun 2022 13:47:38 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id d125-20020a636883000000b003db5e24db27so5491101pgc.13 for ; Tue, 14 Jun 2022 13:47:38 -0700 (PDT) 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=B7koh/xeMRbNx7ZveeQAqwn9fkODM62nIce4GzBlAJw=; b=U/gshE5IW1C8sXvFdPCkoSY6Aw1mJ3k5t/1jzxkWSTDNDiI/80meqCPAOI3vV0NCPm 6TSUPULwBdHGkK7derN6KCOqAdhph6LaS/MYpjDn1z+DXkD1u8lR0OHbrQUpeLmLDgL1 eeuNlcHglsQCU4lnTRJHTu4JWsGT9t96OiFN+1/dZAbcdSoo+cO/FpHylpzo/ZQfl7Iq 4TkoF11P0SxGABPuI6PCExKT9LWGizxX/kuDzQ6VdV6XECf46rfWU20W4UuR7gOf2+sR FoM/3f64+jsDmLU/IASLKXmRdTMVhPHFxk2V3SpGF8/5wBVgMhuxZLWx0gyNtUhmIcaQ ZWZQ== 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=B7koh/xeMRbNx7ZveeQAqwn9fkODM62nIce4GzBlAJw=; b=MlEWT7JGhovRxWlDDaGMP8Lvchl6zMeIj3jbzQHjoRmDzqkIgxaf7u4wzAc1EC3W14 7XaF4FglDH+83XEI3QOSfnTtw/8CgqyWrO8glMQfhM5qvcIaSkeTGj/O+6sZlHCfJhze px3Xd0y3wWHahbuKIdz5LD2ra/yhxZ/6eIqbGLrkjum35o9iQNLjDLUDsK1A6TGTM+5D ipD5Dh8d70DgafAsIeGtkru1u4BiDby5AcZJehUJ4AuZ7yivgNiESKcGdLUMTv8cUGal FgDWDpEIvoV804layS0br3ZzdTmipwdD0h1mw7X9uFmmzdB+fwc5SPWdIK9TpSo/TEvQ jLmQ== X-Gm-Message-State: AJIora/leAqjrxZskudLZdaQfn9xzoe3V26X6bJR1WPRvgR4duVB6+A0 3u67PzwESo+mjhK7MEIPxflMXcaBGAM= X-Google-Smtp-Source: AGRyM1vFRUUeN03pDeoJwyH3bcpEVVB5Pt0xRaeOBTezfDqV/a2WY419Ys0lZwI/Jfd0k+ALn0NwIQWePwY= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:903:130d:b0:164:17f5:9de5 with SMTP id iy13-20020a170903130d00b0016417f59de5mr6025190plb.132.1655239657887; Tue, 14 Jun 2022 13:47:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Jun 2022 20:47:10 +0000 In-Reply-To: <20220614204730.3359543-1-seanjc@google.com> Message-Id: <20220614204730.3359543-2-seanjc@google.com> Mime-Version: 1.0 References: <20220614204730.3359543-1-seanjc@google.com> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH v2 01/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" Precedence: bulk List-ID: X-Mailing-List: kvm@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 7d8cd0ebcc75..ee6f27dffdba 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4263,14 +4263,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); } /* @@ -4609,6 +4601,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.36.1.476.g0c4daa206d-goog