Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp735456imm; Thu, 13 Sep 2018 07:02:06 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZKGpszfz1hNUqs77dTGuNevH1eZ9VVqj4ZWV0cf24A8ZRwPt1IrBt4QIxnA2JYyuM6uGW3 X-Received: by 2002:a63:334c:: with SMTP id z73-v6mr7295687pgz.220.1536847326560; Thu, 13 Sep 2018 07:02:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536847326; cv=none; d=google.com; s=arc-20160816; b=jEhUtvfNUvunGdqC8NUMSmQK9uitiZfq1umfpNwXZP7rx4MVTEfxjE7yQbb0zBxXPu VBMs81aCHir4QxkoBD/Rbe3M6QFLCTiGlV0tOKPIgEMvzFE2XgCqZNIpZz/dJjYQtWOt +VqYhPESqZqbHbL2EdDibIx8WOfpAsTZkhbzm0Sl5jEf+bXK0C6lZ4L+TH4DwNTExizY FiFhU63bil/rHAIrrkUCehKOkCXqiFWQwNZctSxLREUYROOInKjeArnxIcgQLrWL8GEN LVkT3QKRrZZJhPHrdGutx/v5KQSUa3AevonGA/PR/rR2j94ArDmv1x0zTBLBXiu63wEo AFFA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=xdQ/d9ER1emoPeuEmSzcSnD6J4hPLZ7Z6M1FAqToQ7E=; b=Fv7tV+firdtivE5GFDOgFzSW3zY5GdQpFd+YWMGX0s0/ejo3LBbdaFwrAhmCwRSBnp gJL7IdFbpIRfkRiGpSYuLRBqQGAhe3HVYqM22kzTzQpuonHGvVzE0hVW49rR/t0omb12 dbsSCI728ASuAdK1RJLPQMvLd1AHh6hNBk8H4TsJeeLQz5XK503R2ImRwAEGLTo6fhLn vmqagqGFaJ2RzvarY1RE6xSyRFwUVQnA4fnxQXlLiCDDzG0gg8aonRJd0VpD0f5Vzq3l X1G+zZsysafy2ML784acUfnBy3dfE4le8Wv8pDPRZ9qAh1kIzSXXiGSPsPtJbICfYu8k u8Bw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 22-v6si4290454pfb.215.2018.09.13.07.01.47; Thu, 13 Sep 2018 07:02:06 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731779AbeIMTJv (ORCPT + 99 others); Thu, 13 Sep 2018 15:09:51 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:34914 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729047AbeIMTJu (ORCPT ); Thu, 13 Sep 2018 15:09:50 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 0E521D15; Thu, 13 Sep 2018 14:00:11 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Paolo Bonzini , Sasha Levin Subject: [PATCH 4.18 130/197] KVM: vmx: track host_state.loaded using a loaded_vmcs pointer Date: Thu, 13 Sep 2018 15:31:19 +0200 Message-Id: <20180913131846.757756409@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180913131841.568116777@linuxfoundation.org> References: <20180913131841.568116777@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sean Christopherson [ Upstream commit bd9966de4e14fb559e89a06f7f5c9aab2cc028b9 ] Using 'struct loaded_vmcs*' to track whether the CPU registers contain host or guest state kills two birds with one stone. 1. The (effective) boolean host_state.loaded is poorly named. It does not track whether or not host state is loaded into the CPU registers (which most readers would expect), but rather tracks if host state has been saved AND guest state is loaded. 2. Using a loaded_vmcs pointer provides a more robust framework for the optimized guest/host state switching, especially when consideration per-VMCS enhancements. To that end, WARN_ONCE if we try to switch to host state with a different VMCS than was last used to save host state. Resolve an occurrence of the new WARN by setting loaded_vmcs after the call to vmx_vcpu_put() in vmx_switch_vmcs(). Signed-off-by: Sean Christopherson Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/vmx.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -939,17 +939,21 @@ struct vcpu_vmx { /* * loaded_vmcs points to the VMCS currently used in this vcpu. For a * non-nested (L1) guest, it always points to vmcs01. For a nested - * guest (L2), it points to a different VMCS. + * guest (L2), it points to a different VMCS. loaded_cpu_state points + * to the VMCS whose state is loaded into the CPU registers that only + * need to be switched when transitioning to/from the kernel; a NULL + * value indicates that host state is loaded. */ struct loaded_vmcs vmcs01; struct loaded_vmcs *loaded_vmcs; + struct loaded_vmcs *loaded_cpu_state; bool __launched; /* temporary, used in vmx_vcpu_run */ struct msr_autoload { struct vmx_msrs guest; struct vmx_msrs host; } msr_autoload; + struct { - int loaded; u16 fs_sel, gs_sel, ldt_sel; #ifdef CONFIG_X86_64 u16 ds_sel, es_sel; @@ -2750,10 +2754,11 @@ static void vmx_save_host_state(struct k #endif int i; - if (vmx->host_state.loaded) + if (vmx->loaded_cpu_state) return; - vmx->host_state.loaded = 1; + vmx->loaded_cpu_state = vmx->loaded_vmcs; + /* * Set host fs and gs selectors. Unfortunately, 22.2.3 does not * allow segment selectors with cpl > 0 or ti == 1. @@ -2815,11 +2820,14 @@ static void vmx_save_host_state(struct k static void __vmx_load_host_state(struct vcpu_vmx *vmx) { - if (!vmx->host_state.loaded) + if (!vmx->loaded_cpu_state) return; + WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs); + ++vmx->vcpu.stat.host_state_reload; - vmx->host_state.loaded = 0; + vmx->loaded_cpu_state = NULL; + #ifdef CONFIG_X86_64 if (is_long_mode(&vmx->vcpu)) rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); @@ -10517,8 +10525,8 @@ static void vmx_switch_vmcs(struct kvm_v return; cpu = get_cpu(); - vmx->loaded_vmcs = vmcs; vmx_vcpu_put(vcpu); + vmx->loaded_vmcs = vmcs; vmx_vcpu_load(vcpu, cpu); put_cpu(); }