From mboxrd@z Thu Jan 1 00:00:00 1970 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 B06E1C433EF for ; Thu, 24 Feb 2022 19:19:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233628AbiBXTUD (ORCPT ); Thu, 24 Feb 2022 14:20:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233639AbiBXTTx (ORCPT ); Thu, 24 Feb 2022 14:19:53 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B0BD1B84C9 for ; Thu, 24 Feb 2022 11:19:23 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id f128-20020a623886000000b004e152a2c149so1762384pfa.5 for ; Thu, 24 Feb 2022 11:19:23 -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=yieelq7XzoMm9U+sAB44XjR1x4+Di8aVqI+Ht06XBSY=; b=BP3ft1zkTKcGkK0f2+HsK7YjABI81wEEkbltSB6pfEqw5GleoGN3Rbmaw4xhmLahBg zFJE25ew/HqvKM3hS1EI5nqz+Tbo9kI7x0uwTu49dyjupTeta0I+LRaEFXPcD5kRcLa+ rf1Plaby0PUZoHqizdWUa7xQJh45NumpLi5vhFOxL86X4waXeMwbzYVznNaiA+5KYLTl /mZ9UEzhi87gfwzjaPWkWLB6WAAMdVUrFZmqHIVcHoyY10+IEQSh24u6wIUtk2Pqrsej GzRs96vkd9G8p/T4JAA+pER+i+hTWykK9JqkHFkN9LXRpr8Z+bGwgA/yS4R92HaasltI EyrA== 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=yieelq7XzoMm9U+sAB44XjR1x4+Di8aVqI+Ht06XBSY=; b=SxJhW9HuPpGcqjOvygKzIZtqGP7iLmNhCtp/J2CABH2jcJDLmFXqkx3RaFj2laZbDu gAtc5HIjP1WJJRLyMiQu/6pFxnvT9MHt4wD/wLhTlMxagu3XWoNfGrWK9r9dSF/kd0tz 9VxBrmSCX96WLtFdqMKijejF8iBB9lJsEsM2oYTSBIsWwqCXTvTu/KzlxNiXutxfLb0s m374028aPl4InJK8NF5pioWQT9akqKTnY2xEvS3GC4hziUTH/xLvDgDMs63/Sedy2ZsA z9BPiAaLYAV9ADGF1zxvBsJyPwFLYm8G9ynncI8adyMHCVtZAiUdVSKbDq97LqcHPgVH aLMg== X-Gm-Message-State: AOAM531donw9iYqIqmwfscnynMjjftJwRTYiJoGKM1jr4RvIuykjDK0e s/mgTEIPjbcDysKtQD2BQ6SyvskgqJ8= X-Google-Smtp-Source: ABdhPJxEOKP7SjXFgqmje7/U4JD92c0LMC57eWxrJRQqbSQt6GELqxMbBC9x4ksWOzaksc5zc6zN7bKMSPg= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:902:654b:b0:14d:964d:7578 with SMTP id d11-20020a170902654b00b0014d964d7578mr4168544pln.166.1645730362575; Thu, 24 Feb 2022 11:19:22 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 24 Feb 2022 19:19:16 +0000 In-Reply-To: <20220224191917.3508476-1-seanjc@google.com> Message-Id: <20220224191917.3508476-2-seanjc@google.com> Mime-Version: 1.0 References: <20220224191917.3508476-1-seanjc@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH 1/2] Revert "KVM: VMX: Save HOST_CR3 in vmx_set_host_fs_gs()" 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, Wanpeng Li , Lai Jiangshan Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Undo a nested VMX fix as a step toward reverting the commit it fixed, 15ad9762d69f ("KVM: VMX: Save HOST_CR3 in vmx_prepare_switch_to_guest()"), as the underlying premise that "host CR3 in the vcpu thread can only be changed when scheduling" is wrong. This reverts commit a9f2705ec84449e3b8d70c804766f8e97e23080d. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 3 +-- arch/x86/kvm/vmx/vmx.c | 20 +++++++++++--------- arch/x86/kvm/vmx/vmx.h | 5 ++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index ba34e94049c7..c12f95004a72 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -246,8 +246,7 @@ static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx, src = &prev->host_state; dest = &vmx->loaded_vmcs->host_state; - vmx_set_vmcs_host_state(dest, src->cr3, src->fs_sel, src->gs_sel, - src->fs_base, src->gs_base); + vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base); dest->ldt_sel = src->ldt_sel; #ifdef CONFIG_X86_64 dest->ds_sel = src->ds_sel; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index efda5e4d6247..beb68cd28aca 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1080,14 +1080,9 @@ static void pt_guest_exit(struct vcpu_vmx *vmx) wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); } -void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3, - u16 fs_sel, u16 gs_sel, - unsigned long fs_base, unsigned long gs_base) +void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel, + unsigned long fs_base, unsigned long gs_base) { - if (unlikely(cr3 != host->cr3)) { - vmcs_writel(HOST_CR3, cr3); - host->cr3 = cr3; - } if (unlikely(fs_sel != host->fs_sel)) { if (!(fs_sel & 7)) vmcs_write16(HOST_FS_SELECTOR, fs_sel); @@ -1119,6 +1114,7 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) #ifdef CONFIG_X86_64 int cpu = raw_smp_processor_id(); #endif + unsigned long cr3; unsigned long fs_base, gs_base; u16 fs_sel, gs_sel; int i; @@ -1182,8 +1178,14 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) gs_base = segment_base(gs_sel); #endif - vmx_set_vmcs_host_state(host_state, __get_current_cr3_fast(), - fs_sel, gs_sel, fs_base, gs_base); + vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base); + + /* Host CR3 including its PCID is stable when guest state is loaded. */ + cr3 = __get_current_cr3_fast(); + if (unlikely(cr3 != host_state->cr3)) { + vmcs_writel(HOST_CR3, cr3); + host_state->cr3 = cr3; + } vmx->guest_state_loaded = true; } diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 7f2c82e7f38f..9c6bfcd84008 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -374,9 +374,8 @@ int allocate_vpid(void); void free_vpid(int vpid); void vmx_set_constant_host_state(struct vcpu_vmx *vmx); void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu); -void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3, - u16 fs_sel, u16 gs_sel, - unsigned long fs_base, unsigned long gs_base); +void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel, + unsigned long fs_base, unsigned long gs_base); int vmx_get_cpl(struct kvm_vcpu *vcpu); bool vmx_emulation_required(struct kvm_vcpu *vcpu); unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu); -- 2.35.1.574.g5d30c73bfb-goog