Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2111578pxb; Wed, 9 Feb 2022 11:05:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJxcL6ndwPzoeM1wJYKSOg8ak7Ph82zrwNXnlyLSLbgnVn/mPnnhfbY+WhYbqoMd4wtpASbj X-Received: by 2002:a63:ff0a:: with SMTP id k10mr3014987pgi.179.1644433502887; Wed, 09 Feb 2022 11:05:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644433502; cv=none; d=google.com; s=arc-20160816; b=HYaFJz0F/Z3sWkb7Fl+ZVNdzQ0N4otWXrSMuXA/GXOODy2LB6dpMRYVjE2zRmtUEz1 FR1xZb8V8WzN+W+d8QLjDHoU2hK6Fih+K23wq2J82JmX4zhHoeFW6SziKwZj1X+YaQux Gh1+g+IsV88bzdL1mQ2Qk3vScG9i1FZreBfOuzOuV4OabaX6BArNFPk3GLB3JTsqAUjx A3aTosstXFDuUjB77WY1kz/1pLmJv6MscV1+wAloWZNM4ax38Sf7Znk3PceuGYiS2KTZ OfOxcJbBQGEo7YaRJJlwO465bpKspVczQFezpVbJY+ctg7BYVYj/dC6+k1h0uOaqR7V4 /XZQ== 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=UmiLwad0yNHqYcNsluzF3JRoNRWaXkEyVI1t62KrVok=; b=U2DoZ/36Yjw7Q2/S+U2+6o0dXBPxUKxqy2ULeqfvIOD18yxtpMce3jLuAt0h/DF76Y gIudsmqjequ+lhJyHzQCdvzY3Xpd5bChQLoh/66UD+R+/gNvHlw+WCPVvFor5I4wbZm7 cap6SW8JRhXv3laCn3buef3umCWkkMTtJ6fQ2+gPTvMMGFclJA8zeSNZUM8Kf6sKeL3t ZMv9N/njjtKp8h8eymKNGJJHcD9jLBh8F1f900+OMMbZ5MtR+GVM43rgjChirkqYEBp7 xgbThtI+BJ75Ofcm75Q5sBYB5iU+80XA71rzCIVTHtoaFOCd16ja5W1yysm2TkKPkuhk cS+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OiD7WnRI; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id nl10si6814780pjb.62.2022.02.09.11.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 11:05:02 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OiD7WnRI; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 039E2C094C95; Wed, 9 Feb 2022 11:04:40 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237732AbiBIRBE (ORCPT + 99 others); Wed, 9 Feb 2022 12:01:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237624AbiBIRAy (ORCPT ); Wed, 9 Feb 2022 12:00:54 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 92A3CC05CB87 for ; Wed, 9 Feb 2022 09:00:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644426056; 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=UmiLwad0yNHqYcNsluzF3JRoNRWaXkEyVI1t62KrVok=; b=OiD7WnRIDmh4DeDoLG2p0w/9q7nJuo6lApc2+3E3e1OF4wDPcGF9PFoIMufUPfrt7xqOAD PJ8ZxuLYnPyFx5IxjHOaMPB+IQCQ/u45NO/QeOSVeWlaaMPbCLj5az6wJyw5CW0peCH/K6 feuP+b1eQU8e8AnDfm2RrpQQidCgObY= 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-462-U5WUf8sOO3O9cGvcDwstzQ-1; Wed, 09 Feb 2022 12:00:55 -0500 X-MC-Unique: U5WUf8sOO3O9cGvcDwstzQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F0EBB101F7A1; Wed, 9 Feb 2022 17:00:52 +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 754F47CD66; Wed, 9 Feb 2022 17:00:52 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: vkuznets@redhat.com, mlevitsk@redhat.com, dmatlack@google.com, seanjc@google.com Subject: [PATCH 08/12] KVM: MMU: do not consult levels when freeing roots Date: Wed, 9 Feb 2022 12:00:16 -0500 Message-Id: <20220209170020.1775368-9-pbonzini@redhat.com> In-Reply-To: <20220209170020.1775368-1-pbonzini@redhat.com> References: <20220209170020.1775368-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Right now, PGD caching requires a complicated dance of first computing the MMU role and passing it to __kvm_mmu_new_pgd, and then separately calling kvm_init_mmu. Part of this is due to kvm_mmu_free_roots using mmu->root_level and mmu->shadow_root_level to distinguish whether the page table uses a single root or 4 PAE roots. Because kvm_init_mmu can overwrite mmu->root_level, kvm_mmu_free_roots must be called before kvm_init_mmu. However, even after kvm_init_mmu there is a way to detect whether the page table has a single root or four, because the pae_root does not have an associated struct kvm_mmu_page. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 3c3f597ea00d..95d0fa0bb876 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3219,12 +3219,15 @@ void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, struct kvm *kvm = vcpu->kvm; int i; LIST_HEAD(invalid_list); - bool free_active_root = roots_to_free & KVM_MMU_ROOT_CURRENT; + bool free_active_root; BUILD_BUG_ON(KVM_MMU_NUM_PREV_ROOTS >= BITS_PER_LONG); /* Before acquiring the MMU lock, see if we need to do any real work. */ - if (!(free_active_root && VALID_PAGE(mmu->root.hpa))) { + free_active_root = (roots_to_free & KVM_MMU_ROOT_CURRENT) + && VALID_PAGE(mmu->root.hpa); + + if (!free_active_root) { for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) if ((roots_to_free & KVM_MMU_ROOT_PREVIOUS(i)) && VALID_PAGE(mmu->prev_roots[i].hpa)) @@ -3242,8 +3245,7 @@ void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, &invalid_list); if (free_active_root) { - if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL && - (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) { + if (to_shadow_page(mmu->root.hpa)) { mmu_free_root_page(kvm, &mmu->root.hpa, &invalid_list); } else if (mmu->pae_root) { for (i = 0; i < 4; ++i) { -- 2.31.1