Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4050714pxv; Tue, 13 Jul 2021 09:36:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwW91U8JFFxOLf16kO3DIPyTGetkH6cJanijQ71EQ2LLS9aTom3x9h3OPim4zY3Yk3S73rp X-Received: by 2002:aa7:c412:: with SMTP id j18mr6660045edq.119.1626194165110; Tue, 13 Jul 2021 09:36:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626194165; cv=none; d=google.com; s=arc-20160816; b=Q94B38LxyUEDXHzZBDMk8LFoIGR2Ok7smE17xr/7+BQ13a0jMERLLBZ2MibAttnWKo IyGDG2whcR0QwjNic08vN+yPC43Pt4bT9cjPi0con0UMrrzyCJVjYKY2vMJ3OygCXrue YSEAT/YwMeEnGfAqL1xbw/Wb2O1l07qrAu9EMqUcUWmynb855iU5AGnJ7BasnnKa6I6t S27+/fuwdwTfpCPtEgITkgS4S0hM99vxXsmv5ndfTfJ/LBwRRcP7qryfX3LmYi+kfa5X nJmLOE6fk0FhCPl6/Fu9yfW4FrCuGp9og/bosrg8U4Q84hn59ofts/MB83QyPB5WmIwg 4+rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=LCuijp/LgWy7Zf7nKMsAJ/SqN4uwlKk3jFpZ0B2dGyo=; b=GSw3GPFRNknbbzqK4Zr2HJmmWsyYIZmH+4ZlTRV/UXUTS6+BcgWrCdGYMCDfv3jG+T KLQ92lQOjWHUT7GhkHj298T74MoO70G9f3KNvy8sh7qwubKW38SMZYh0y7zUXLzdutTr tVu/eSrk7zmqsWikfoF7o9DvxZ9u9B/iEr+nTFUGaIl99Tfa07KUFUOcvF7e1NEIjE0a /bdNzPIk6DB7sjkVQPckHOHcsmMyv7TZKn+QfnrGreXcgwdY9C65iJNCP3OlmPKz5cY4 4Gr9oKwkRzihHAx8fbCu0SfNyUZ2b6LWcQzMcqSh7/VjLjQeBqYjkGkjggeXbNTx7YK4 T6Fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=chy0oHxy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si12080043edi.310.2021.07.13.09.35.36; Tue, 13 Jul 2021 09:36:05 -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=@google.com header.s=20161025 header.b=chy0oHxy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234594AbhGMQgy (ORCPT + 99 others); Tue, 13 Jul 2021 12:36:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232813AbhGMQgp (ORCPT ); Tue, 13 Jul 2021 12:36:45 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FD14C0613EE for ; Tue, 13 Jul 2021 09:33:54 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id x12-20020a05620a448cb02903b3dabbe857so17377201qkp.15 for ; Tue, 13 Jul 2021 09:33:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=LCuijp/LgWy7Zf7nKMsAJ/SqN4uwlKk3jFpZ0B2dGyo=; b=chy0oHxym/ue2ksnIVnqz8+fw9GXKUgLXtGVmUl+sFwwgboS/wOTM2nzRO9ESgMBtL hclJywDEgQ+8V5ox2uhi+cqCLKkD4DWHuroZCF0UCKNGOLGY3CVxTkF14aicTr67ytWv aoIhtkRkyyisUMhMDCs5kWQs1n8Qh1FDRiF5ehw2NJTajag0Ym25ERziA6Dvfb+NeEjE jnDVlf8tJp82W2lHaqw8EJe8iH59tzh9jhdlYmicGFuoAkbKNUSlyNbQ68Bukqvy2Udi oNwJDB1LYERljoSAHrUv4SIxbFuTBjpxH5lDnKiGJo2YKG9OZPWRrrlhY5GFgH3Gql0C 1juQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=LCuijp/LgWy7Zf7nKMsAJ/SqN4uwlKk3jFpZ0B2dGyo=; b=eSQc8jPrY6fAOWvbWuhcoHlP394h+wlGY0stdbF6GtpIMoYnDfMIrT5mPCER1eucMV 5VOW54LbWp1QDviEu2pGhyXlCqSUfgwb0DLkosa2/JoKe0efgsIZcpOHOySuqJDFIUCo lCGksDkeAJjsUwr6REP2UVuBJ1Cp/KnWZXH7F8jJz4dnQwa8l5b3ENv44d9QDTIbdkIL bPE9nQvUBajIXTj4r19j6d+qxKh9qpmNqSnGzsdRl35D3a9FQ0YpO5sJC7G9yW53i1JQ UOiwlyputnR6bdv51VqEghRKPC7ysQTzQI9zcbdq//AbrmDegVu7c/B+W4sILaGXsU8o 5e5Q== X-Gm-Message-State: AOAM531nyI2MCB7iIjxq5m6y9WU/j42igGa+x6sVbP6VZntVZIdyhMaJ f6GEBV0UTZw1EdvHvT5/Jab0wN25KgI= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:14e2:: with SMTP id k2mr5662267qvw.21.1626194033616; Tue, 13 Jul 2021 09:33:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:48 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-11-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 10/46] KVM: VMX: Move init_vmcs() invocation to vmx_vcpu_reset() 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, Reiji Watanabe Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Initialize constant VMCS state in vcpu_vcpu_reset() instead of in vmx_vcpu_create(), which allows for the removal of the open coded "vCPU load" sequence since ->vcpu_reset() is invoked while the vCPU is properly loaded (which is the entire point of vCPU reset...). Deferring initialization is effectively a nop as it's impossible to safely access the VMCS between the current call site and its new home, as both the vCPU and the pCPU are put immediately after init_vmcs(), i.e. the VMCS isn't guaranteed to be loaded. Note, task preemption is not a problem as vmx_sched_in() _can't_ touch the VMCS as ->sched_in() is invoked before the vCPU, and thus VMCS, is reloaded. I.e. the preemption path also can't consume VMCS state. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 0f5e97a904e5..26c0e776827c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4293,10 +4293,6 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) #define VMX_XSS_EXIT_BITMAP 0 -/* - * Noting that the initialization of Guest-state Area of VMCS is in - * vmx_vcpu_reset(). - */ static void init_vmcs(struct vcpu_vmx *vmx) { if (nested) @@ -4395,6 +4391,9 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) u32 eax, dummy; u64 cr0; + if (!init_event) + init_vmcs(vmx); + vmx->rmode.vm86_active = 0; vmx->spec_ctrl = 0; @@ -6782,7 +6781,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) { struct vmx_uret_msr *tsx_ctrl; struct vcpu_vmx *vmx; - int i, cpu, err; + int i, err; BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0); vmx = to_vmx(vcpu); @@ -6844,12 +6843,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) vmx->msr_bitmap_mode = 0; vmx->loaded_vmcs = &vmx->vmcs01; - cpu = get_cpu(); - vmx_vcpu_load(vcpu, cpu); - vcpu->cpu = cpu; - init_vmcs(vmx); - vmx_vcpu_put(vcpu); - put_cpu(); + if (cpu_need_virtualize_apic_accesses(vcpu)) { err = alloc_apic_access_page(vcpu->kvm); if (err) -- 2.32.0.93.g670b81a890-goog