Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1793474pxy; Fri, 23 Apr 2021 17:50:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzB8po/imd7IjsUpnZbXPOjKWJEkgDuqxCm8dX1UXXk2an2am6P1ZVk2HXLB0xtCdocd+gS X-Received: by 2002:a05:6402:3ce:: with SMTP id t14mr7545285edw.142.1619225450298; Fri, 23 Apr 2021 17:50:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619225450; cv=none; d=google.com; s=arc-20160816; b=wvURB/LmS37n1tMuNw0g8gjw7y5nHAjaz5RrgOSRjLQURraZe8mr1gQxAacNoTmdVc GOXmx7pWf9WdBrmh98KMfwKggOyfxXyEDeN2chuhZn7LRjdNy+E8kVQc9f5kJBLt87vj cULPGjwFjwB9lO264xXHj0I4pYqq7NZu3Ov08CH7sFeoPgQyqC49b7rfU55BIvy481cq bGTImkwsBvbhTntOfrnONKZg2Jz/IQBNDnHIvmoHQfRRC5imq2WgIsz5ycpdA2iezc3V //0UI0M8Mu+rULDCVMgnRfT+PpMc61oewDifzXtzjE3g+7JAIxJP6UribR6VDXysQ6RZ 2bIA== 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=8lM2E0jJiBC1VTmkEjEGfcPNw32U+DRkCC8iMe7vR7g=; b=Fu/+t3nUTOkXlB/35xmckVUAGzND+ZQ5cLSm1A6+azxJfxYWOsECCv+XZOSZ3ZrrUg Cs9MUh7rQc1uqUwgA6EMKCb66A/BmAlq7oh6843rwrbbXajhOXGMwrvQcM8yiIuLetoI 0GGQjp5v6oWMTmnPgHnk5JCOm3sNYLM23NJ6Fm3IKbwCx/DAaqJ8zNa9j5a1Soit6/Yx KH5clEId7IM6Cq9E2f3zHykU1skjcQdIGTZIoRIY+jMp4QtP4kNQdaw6Cvt/WO4qRTO8 9GBLgqdRE/bGFtyo86UZIs2cTF5KUV+KI9PSYOT7HpB36iksmq3L5xya6+XuM80jk1ij Wl8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fRnpEjwE; 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 dn6si6411333edb.367.2021.04.23.17.50.27; Fri, 23 Apr 2021 17:50:50 -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=fRnpEjwE; 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 S244409AbhDXAtD (ORCPT + 99 others); Fri, 23 Apr 2021 20:49:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244201AbhDXAsT (ORCPT ); Fri, 23 Apr 2021 20:48:19 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5355C06138B for ; Fri, 23 Apr 2021 17:47:22 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id u73-20020a25ab4f0000b0290410f38a2f81so26212668ybi.22 for ; Fri, 23 Apr 2021 17:47:22 -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=8lM2E0jJiBC1VTmkEjEGfcPNw32U+DRkCC8iMe7vR7g=; b=fRnpEjwEWFFWgQyiODGaPJYvV9lGJtlFOFWquX9veLOfyDjX4Ju1bx95sXoo3aW+H8 XUNnnwv+t993ylG6HdqfPFSpp6UekjvlRSb7nu/xuJr9D8y52SYLKUW6UnvPV2jzsEm+ 49SUHn3vdm1B1shrMRA+Ezvn6rnlysIEisI7/e9B6Rx/USc2+h9wO/ni3pN5QVeJ4Lzj 9JFDU94eJF9lXKUH4+7bOohj73mCMkNSlk7vZSZdwtBbvK7eazc1MAZzBY+XLpXCL89D Bi8tgZu/dAjP3bF3plexU0WYT1zvAlFOw8nwDR0Bh83qiAM8jqG0QzuX9t3ergoWlSzu nw5A== 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=8lM2E0jJiBC1VTmkEjEGfcPNw32U+DRkCC8iMe7vR7g=; b=KlzFf5yqvzgPzw711uSKa8adDVrWy7yF7mUs5/BMYASHTrFUN8mZKxG/4k9gkrRBwr yNVI1l83aoMf9WLShaOYjmJ01JYXODq3UogjT1B775FKBrQaUcw2WEQqOtUlAU45bL/q xvvdAh0289TbImWnaOBnKPerMrRLogpN1T3bEGJfTHxqOlVYQ+Ou/8hUqWTy+iPn0sJV G/dHGz42YXLhos359VlTLSqAtQRZLzLY1h18TpyiMTyubQ0NR6oD9BuQcehR/DCE3lPR kXPfT3pby+gLfw9FPSjZ10YKyRgXqoHOSOqJeOR5xSUgDfNFOjq4tKl7I0jtT71f588y BWAw== X-Gm-Message-State: AOAM5305FfsjmoYsQooERXK6pNR+fNrk2zjJRNG1dOkNCrPyzEvWe107 pKYlCGaoMfg9fdQvp6+Gh6ZuPTBqzNU= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:f:10:ad52:3246:e190:f070]) (user=seanjc job=sendgmr) by 2002:a25:8884:: with SMTP id d4mr9544198ybl.410.1619225242226; Fri, 23 Apr 2021 17:47:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 23 Apr 2021 17:46:12 -0700 In-Reply-To: <20210424004645.3950558-1-seanjc@google.com> Message-Id: <20210424004645.3950558-11-seanjc@google.com> Mime-Version: 1.0 References: <20210424004645.3950558-1-seanjc@google.com> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH 10/43] 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 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. 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 e90952ca6087..856aa44b17d5 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4396,10 +4396,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) @@ -4498,6 +4494,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; @@ -6905,7 +6904,7 @@ static void vmx_free_vcpu(struct kvm_vcpu *vcpu) static int vmx_create_vcpu(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx; - int i, cpu, err; + int i, err; BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0); vmx = to_vmx(vcpu); @@ -6991,12 +6990,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.31.1.498.g6c1eba8ee3d-goog