Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp2610001ybn; Thu, 26 Sep 2019 14:45:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHDQ0uJmzRkET1MuCdVnqEvq5TFS7JLH0zI9TtDx3WWEkwVkELbDxrByERs/yA/QAYXqL1 X-Received: by 2002:a05:6402:2054:: with SMTP id bc20mr1132994edb.55.1569534329990; Thu, 26 Sep 2019 14:45:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569534329; cv=none; d=google.com; s=arc-20160816; b=KqFViQ2W6TdQLhIRzZwSxNexNvFHbzVEdDujPBCVl5Av/IWd+UStRzKn+VLLFAhVOe JwwVbTH8dAO95KvU8Y3FOooP9DZQmQ7H2/ioJxhixDHhDKkibtonV+DmUeKPdPtyUSS1 X7fvQ+zxqEl6zfBoBabt5GRmU3nqeVHet0v9p129wLZ0DHFiDXPChZp5LAN3wj/Pc0so 2cIjkk8cAE7zYBqm4FWvF/4BL6vWtFqLp5MErDApqUPfHPe5SInNLiCc090npmmBcu9y ImMpPRbfC2tJhr5ZTznPgN6ThCWVieiuFBJb5+X86/jQRajkV6Fi4W78vAgEF9oGRNgc hOfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=FbWD7Q3RxEaSxRLPc2VWsk8DWWdmj+VvMjLzLlZ2zws=; b=ax6Q2w/A3Ml+WFoaum0kup9LzAvxw9yExUa0GI4bvef3V2/k3ALezCiNVL7r9lIkFh 7bq1E09ngorMc+Ho/jQUsvlO0V/ntdr7iBBHOXx5TQSVq68vvHDuQdUj7dWZaWJ5CszR WmJUa8ae3nhWe3Dc+sel2TzuC++ti2C4XjyjxItbCSc10O+sBv4soCUdoHJuddYCxNlN ANiHOypIABZHgeZaylLLXM3pUC5+qZ8bGh9HlKzedz7FwQEGd8nI6vfSUcW93qOCounU 0Crmq9g0Vsiaz8X4KcDaTzHZoFdifI6H8Fdv7PMztsMXD4/sC2JUml0NFhVF/80ltBJN XRkA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 64si380055eda.384.2019.09.26.14.45.06; Thu, 26 Sep 2019 14:45:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726444AbfIZVnJ (ORCPT + 99 others); Thu, 26 Sep 2019 17:43:09 -0400 Received: from mga07.intel.com ([134.134.136.100]:8270 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725943AbfIZVnF (ORCPT ); Thu, 26 Sep 2019 17:43:05 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Sep 2019 14:43:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,553,1559545200"; d="scan'208";a="192958528" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.41]) by orsmga003.jf.intel.com with ESMTP; 26 Sep 2019 14:43:03 -0700 From: Sean Christopherson To: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reto Buerki Subject: [PATCH 1/2] KVM: nVMX: Always write vmcs02.GUEST_CR3 during nested VM-Enter Date: Thu, 26 Sep 2019 14:43:01 -0700 Message-Id: <20190926214302.21990-2-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190926214302.21990-1-sean.j.christopherson@intel.com> References: <20190926214302.21990-1-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Write the desired L2 CR3 into vmcs02.GUEST_CR3 during nested VM-Enter isntead of deferring the VMWRITE until vmx_set_cr3(). If the VMWRITE is deferred, then KVM can consume a stale vmcs02.GUEST_CR3 when it refreshes vmcs12->guest_cr3 during nested_vmx_vmexit() if the emulated VM-Exit occurs without actually entering L2, e.g. if the nested run is squashed because L2 is being put into HLT. In an ideal world where EPT *requires* unrestricted guest (and vice versa), VMX could handle CR3 similar to how it handles RSP and RIP, e.g. mark CR3 dirty and conditionally load it at vmx_vcpu_run(). But the unrestricted guest silliness complicates the dirty tracking logic to the point that explicitly handling vmcs02.GUEST_CR3 during nested VM-Enter is a simpler overall implementation. Cc: stable@vger.kernel.org Reported-by: Reto Buerki Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 8 ++++++++ arch/x86/kvm/vmx/vmx.c | 9 ++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 41abc62c9a8a..971a24134081 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -2418,6 +2418,14 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, entry_failure_code)) return -EINVAL; + /* + * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 + * on nested VM-Exit, which can occur without actually running L2, e.g. + * if L2 is entering HLT state, and thus without hitting vmx_set_cr3(). + */ + if (enable_ept) + vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); + /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d4575ffb3cec..b530950a9c2b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2985,6 +2985,7 @@ void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) { struct kvm *kvm = vcpu->kvm; unsigned long guest_cr3; + bool skip_cr3 = false; u64 eptp; guest_cr3 = cr3; @@ -3000,15 +3001,17 @@ void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); } - if (enable_unrestricted_guest || is_paging(vcpu) || - is_guest_mode(vcpu)) + if (is_guest_mode(vcpu)) + skip_cr3 = true; + else if (enable_unrestricted_guest || is_paging(vcpu)) guest_cr3 = kvm_read_cr3(vcpu); else guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; ept_load_pdptrs(vcpu); } - vmcs_writel(GUEST_CR3, guest_cr3); + if (!skip_cr3) + vmcs_writel(GUEST_CR3, guest_cr3); } int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) -- 2.22.0