Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2833429pxv; Mon, 12 Jul 2021 03:05:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1qWVQ9ITlgEpM382UJB/atVM7gXNV+L+PUy9M7h02ZjJl1gDfY0VNeNZpTSOc43Vxye+L X-Received: by 2002:a05:6638:348c:: with SMTP id t12mr39048998jal.1.1626084304385; Mon, 12 Jul 2021 03:05:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626084304; cv=none; d=google.com; s=arc-20160816; b=ja5ShQ+uDutpP03NvFj0LNDKM8UnO71T7q67KEH8piJf8T0o/E66NV6x5Ijof7+bAu hHaT4aU5PSnz/0U3f8w76vj96l1oJt9yGZO/W45lF5usW+hi9Y7rBi0LL28zN2QmFKvw G5WPpWVbic3ekFugFLJkDIySTmsJkrODsSpbpmwANcpd1NiiArTvOKzKSS+P39C6nGtI tn64tTTUBZSnnab/cfGDtaiTXEwDU9facS5iOi4EfoivtlZTR+Dw4cvhWLpvEVnwzy8e xPosoAW4XLYxKS3CiuEPhp841uRMkvNSV84z/xJM4TjLFkRc+GXA+yGhBQsLcliOYXbw NuCg== 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=TkhPoTcoTDv3BZhnTdP0ACqFhwjuSD4QyOtdoEUYfz4=; b=o5Qp8zGLNu6a7ZUBfMvQvYVZ4Akm63S6wHgD0BXpbPPbNPH1tAUf1i2zrT/Ap9v8MS ESCRdCrwRVYqw7GcQxvf6xSxS3rJzh8up4I7hisMNlszNP13S3XbrpheobFI6KnOOLE7 OLI4Ux48pircysI9rW6U6qgF5lN9NStDj8esoP70Xt8zUlch+zIAEKOptT61nOphBPAl hOvYYeKo9wh3YPGMZsIqJ7Q6ts6NqYkAyhjARUXOw/W3cbCNGwQwPCR/4WC4m9KURnju ouZzVB0+A6tIvLKRo55TFgWaq5zj4JfHJjZhvyxNjtsvz8cDCpiqRQxW5IiH2dL0fjdW KgTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wRmEc5rO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h21si3376467ila.73.2021.07.12.03.04.52; Mon, 12 Jul 2021 03:05:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wRmEc5rO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1343550AbhGLH2d (ORCPT + 99 others); Mon, 12 Jul 2021 03:28:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:36162 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240485AbhGLHBJ (ORCPT ); Mon, 12 Jul 2021 03:01:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 01D366124B; Mon, 12 Jul 2021 06:58:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626073101; bh=QUgga7brYU40lC3MbubC2EZH/yb5HkkfDUa9r27aY6Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wRmEc5rOOkgvFSDIPyM94X7qaG8yK6vyVXK87jMVEk9CcrYVPH4uecmJ6f+BDS9Hi qZW3WhlX7tiSmPhaiJ2lJKD4YTy/U4Utsaj4Cx8Qs8cV7oG0S9AcQvP4RZNoRDt+VC UT2mP5fuAf90xZKU66lWroxIpRutiifJKsS+zZA0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.12 074/700] KVM: x86: Properly reset MMU context at vCPU RESET/INIT Date: Mon, 12 Jul 2021 08:02:37 +0200 Message-Id: <20210712060935.157740716@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060924.797321836@linuxfoundation.org> References: <20210712060924.797321836@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 0aa1837533e5f4be8cc21bbc06314c23ba2c5447 upstream. Reset the MMU context at vCPU INIT (and RESET for good measure) if CR0.PG was set prior to INIT. Simply re-initializing the current MMU is not sufficient as the current root HPA may not be usable in the new context. E.g. if TDP is disabled and INIT arrives while the vCPU is in long mode, KVM will fail to switch to the 32-bit pae_root and bomb on the next VM-Enter due to running with a 64-bit CR3 in 32-bit mode. This bug was papered over in both VMX and SVM, but still managed to rear its head in the MMU role on VMX. Because EFER.LMA=1 requires CR0.PG=1, kvm_calc_shadow_mmu_root_page_role() checks for EFER.LMA without first checking CR0.PG. VMX's RESET/INIT flow writes CR0 before EFER, and so an INIT with the vCPU in 64-bit mode will cause the hack-a-fix to generate the wrong MMU role. In VMX, the INIT issue is specific to running without unrestricted guest since unrestricted guest is available if and only if EPT is enabled. Commit 8668a3c468ed ("KVM: VMX: Reset mmu context when entering real mode") resolved the issue by forcing a reset when entering emulated real mode. In SVM, commit ebae871a509d ("kvm: svm: reset mmu on VCPU reset") forced a MMU reset on every INIT to workaround the flaw in common x86. Note, at the time the bug was fixed, the SVM problem was exacerbated by a complete lack of a CR4 update. The vendor resets will be reverted in future patches, primarily to aid bisection in case there are non-INIT flows that rely on the existing VMX logic. Because CR0.PG is unconditionally cleared on INIT, and because CR0.WP and all CR4/EFER paging bits are ignored if CR0.PG=0, simply checking that CR0.PG was '1' prior to INIT/RESET is sufficient to detect a required MMU context reset. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Message-Id: <20210622175739.3610207-4-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/x86.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10302,6 +10302,8 @@ void kvm_arch_vcpu_destroy(struct kvm_vc void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { + unsigned long old_cr0 = kvm_read_cr0(vcpu); + kvm_lapic_reset(vcpu, init_event); vcpu->arch.hflags = 0; @@ -10370,6 +10372,17 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcp vcpu->arch.ia32_xss = 0; static_call(kvm_x86_vcpu_reset)(vcpu, init_event); + + /* + * Reset the MMU context if paging was enabled prior to INIT (which is + * implied if CR0.PG=1 as CR0 will be '0' prior to RESET). Unlike the + * standard CR0/CR4/EFER modification paths, only CR0.PG needs to be + * checked because it is unconditionally cleared on INIT and all other + * paging related bits are ignored if paging is disabled, i.e. CR0.WP, + * CR4, and EFER changes are all irrelevant if CR0.PG was '0'. + */ + if (old_cr0 & X86_CR0_PG) + kvm_mmu_reset_context(vcpu); } void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)