Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp3539510pxb; Fri, 4 Feb 2022 10:37:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJw0dxdM8hupIjBIfUfmR6Se68OZEVZozZhzkc57t2GlXpmA9vQDz24FIF1FJ8jv+2oSHO5R X-Received: by 2002:a17:90b:4a06:: with SMTP id kk6mr314861pjb.140.1643999819976; Fri, 04 Feb 2022 10:36:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643999819; cv=none; d=google.com; s=arc-20160816; b=mvHg8g6+Rc+GsaxjjxBddBs9hZyLULkqHtKKzcwo7wPaR6MNH/qTtgx6uU5K7mMA35 Xw2RxFrguWMZbopjKIgcSbpi4/3L3Hq+WAdpRPhCjS93aycpvvT7LVK9kUUDG/pogELN 5j4IX75OVzlA8JI0s7gK+cc4NKIHJ4pOztgrgpCJUuqO9gZvMckaK0bk9U1hLQjT19fT P+9p1rAUp8KeuRuTlsMPhxakHgz/31V3NY2O157TU7ktcBgTr3uQ+ODTy1ufzsQui5nR LqI1m4cVgZhxkMURP6SGgEozmKs/6lvQqwbJ49KNjbkHHV26ySXT2IosaQ5/HH6Q1fBW CQtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OzIDUAXsJoa+BiSWuGe751GcZgmjbrKEWZp98e47DYA=; b=G+rQchPCB5XMzu7X4DD/PPsb5/zjXGJvpyxvwxS+ieeNOG2JaMaHEH4L/0f81g3Wvg zv9jQcywKj2PQ5ihoJDE4Q3LkkvdUUu8VpmVuKLrdOXkOY3tyks1ulVAgPnjxqhuKH14 2VF0GW0vJeKHtER5NKOw5uMqTJfclE+buNTZGwmP43R6zY8P2b39FEYtenZC0M/TBYDM 13KYpq3IClY7WdIMwJ4hJGm23t6/Lu6qRQI0hdltGpa/Wbyvlm1nApoHT/BJTMfhvl96 XHmwAOKusLNHvo6ZRZCjyWI/vTREe2x2eBN0j9dFQBym9C/gjLTHizXy1tUQ+4jNbYhC TbiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RV2fs8Fi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bm10si3217139pgb.28.2022.02.04.10.36.47; Fri, 04 Feb 2022 10:36:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RV2fs8Fi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358745AbiBDL5p (ORCPT + 99 others); Fri, 4 Feb 2022 06:57:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:37000 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358540AbiBDL51 (ORCPT ); Fri, 4 Feb 2022 06:57:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643975846; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OzIDUAXsJoa+BiSWuGe751GcZgmjbrKEWZp98e47DYA=; b=RV2fs8FiEN5OBPCsZkZxqwvyHUT2SbYBvyltOdy16lB9wZ9VtXi2PTDFY9O0mz6RLjHjNp f1qlcMUgc4tRnQlhiYP0BTCeiP7ajWc7gGfdELHWrW7CraMbQ9aFv/4Yse4xFaZg/fzE08 cIaUjoNKorT0tacwdpbA3U+GLlEB3po= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-657-ziQl9z8bOUeB2IVJ1VGaRQ-1; Fri, 04 Feb 2022 06:57:23 -0500 X-MC-Unique: ziQl9z8bOUeB2IVJ1VGaRQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B05268710F8; Fri, 4 Feb 2022 11:57:22 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 429281084184; Fri, 4 Feb 2022 11:57:22 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: dmatlack@google.com, seanjc@google.com, vkuznets@redhat.com Subject: [PATCH 06/23] KVM: MMU: load new PGD once nested two-dimensional paging is initialized Date: Fri, 4 Feb 2022 06:57:01 -0500 Message-Id: <20220204115718.14934-7-pbonzini@redhat.com> In-Reply-To: <20220204115718.14934-1-pbonzini@redhat.com> References: <20220204115718.14934-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org __kvm_mmu_new_pgd looks at the MMU's root_level and shadow_root_level via fast_pgd_switch. It makes no sense to call it before updating these fields, even though it was done like that ever since nested VMX grew the ability to use fast CR3 switch (commit 50c28f21d045, "kvm: x86: Use fast CR3 switch for nested VMX"). Pull it to the end of the initialization of the shadow nested MMUs. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 577e70509510..b8ab16323629 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4869,10 +4869,9 @@ void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0, new_role = kvm_calc_shadow_npt_root_page_role(vcpu, ®s); - __kvm_mmu_new_pgd(vcpu, nested_cr3, new_role.base); - shadow_mmu_init_context(vcpu, context, ®s, new_role); reset_shadow_zero_bits_mask(vcpu, context, is_efer_nx(context)); + __kvm_mmu_new_pgd(vcpu, nested_cr3, new_role.base); } EXPORT_SYMBOL_GPL(kvm_init_shadow_npt_mmu); @@ -4906,27 +4905,25 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, kvm_calc_shadow_ept_root_page_role(vcpu, accessed_dirty, execonly, level); - __kvm_mmu_new_pgd(vcpu, new_eptp, new_role.base); - - if (new_role.as_u64 == context->mmu_role.as_u64) - return; - - context->mmu_role.as_u64 = new_role.as_u64; - - context->shadow_root_level = level; - - context->ept_ad = accessed_dirty; - context->page_fault = ept_page_fault; - context->gva_to_gpa = ept_gva_to_gpa; - context->sync_page = ept_sync_page; - context->invlpg = ept_invlpg; - context->root_level = level; - context->direct_map = false; + if (new_role.as_u64 != context->mmu_role.as_u64) { + context->mmu_role.as_u64 = new_role.as_u64; + + context->shadow_root_level = level; + + context->ept_ad = accessed_dirty; + context->page_fault = ept_page_fault; + context->gva_to_gpa = ept_gva_to_gpa; + context->sync_page = ept_sync_page; + context->invlpg = ept_invlpg; + context->root_level = level; + context->direct_map = false; + update_permission_bitmask(context, true); + context->pkru_mask = 0; + reset_rsvds_bits_mask_ept(vcpu, context, execonly, huge_page_level); + reset_ept_shadow_zero_bits_mask(context, execonly); + } - update_permission_bitmask(context, true); - context->pkru_mask = 0; - reset_rsvds_bits_mask_ept(vcpu, context, execonly, huge_page_level); - reset_ept_shadow_zero_bits_mask(context, execonly); + __kvm_mmu_new_pgd(vcpu, new_eptp, new_role.base); } EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu); -- 2.31.1