Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4582121pxj; Wed, 12 May 2021 08:38:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxY3icbXtiKdMkbynrWbdLgja7PUb4pXc2TTpMm/c1+2nfc475ctcmsPLyRZg3bLxFokPvo X-Received: by 2002:a05:6402:310a:: with SMTP id dc10mr44107288edb.38.1620833905945; Wed, 12 May 2021 08:38:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620833905; cv=none; d=google.com; s=arc-20160816; b=C7MAKXKm1U6ApYav179wujwc7NPKhghKHS+2In+IkBcaV2F+WqWrj8bfZBC/i/Xn4S PFTnXAnqIHzDf21HfsTPmpHbjKs2hlRq8Z04hO9+Y/HGMF7Te9+Z9BH6wuqazOWL/mjt G6JaZVrzZeVB/H28uaJ6eBXK7uMb3q8+Ab4e1QOxgbaMoF5s1Ei2oT0NO6I1Ny0aXIHS hnZ+ilrpYg6BACFCCxqiqtAqXPOm1yDjwIlP9398dWpIDN2mXg+ifFSvORqYErgAHAJj xQ/jX7hNavC954j7fCdl7fZ4Et+yD4JxkwQyGiHTzLg3t86NwSQPv6gRFYkDsgk3amn8 f0Tw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wfGmP7ZklHHFaTDvQTkS+UW+tG/xffIo/nrRRQBwMn4=; b=pyJpFVKNxL/dkUnd0S6COcKx8AD2NC/bkxPD8P8Da/p7Wd6rXwDDQVCn8HDXt/l65T xtmiH5vci8sOlA+amzVqZBeCd9MGlj3/t5taoSo56M7buTquHmBQCqJHddwlTS5IauDw QspGFgbaGAHNHr8ZUBT8ths+GoVm3XsaXyfyP9imqtewuLqoIr/ljYdzKeWcXaVYQxOk xJLtwZdL4BfZ7Y18CqyeBqASQinZVwqmucrD2hPkBiVfNQlDLnMNktTfDfLSFQ6LfIQF Fucm36wFWQGWftsf7yd5nbk86x8KSoaZhjXNXGRHXD/vkWlzA/w4dugpw2bmx8i2HJac mcpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=I5nmRZzT; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jg39si240714ejc.63.2021.05.12.08.38.02; Wed, 12 May 2021 08:38:25 -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=@linuxfoundation.org header.s=korg header.b=I5nmRZzT; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236066AbhELPg4 (ORCPT + 99 others); Wed, 12 May 2021 11:36:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:47650 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234296AbhELPQY (ORCPT ); Wed, 12 May 2021 11:16:24 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2A32F61956; Wed, 12 May 2021 15:06:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620831977; bh=UAK33Sh/HLW9bz9idD2jMhCNxKDgLJ1x0r5wsk6b1Uk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I5nmRZzTIOTeptz9rBBVjrsmwV1ck3W3W8An+zSGQo/hUdF7P7fNpBQPzYKf7tm4N AuhvO7EM9gOLij7R++QSDWc6AJaIETBrJh4Gsleg8PpxjlIx0IJcEnBbWID4otVD8s 7+VZNxHA+fH12arQHNmCC/xsAS6kDQ7zUkwBTNYo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ben Gardon , Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.10 091/530] KVM: x86/mmu: Alloc page for PDPTEs when shadowing 32-bit NPT with 64-bit Date: Wed, 12 May 2021 16:43:21 +0200 Message-Id: <20210512144822.780532373@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 04d45551a1eefbea42655da52f56e846c0af721a upstream. Allocate the so called pae_root page on-demand, along with the lm_root page, when shadowing 32-bit NPT with 64-bit NPT, i.e. when running a 32-bit L1. KVM currently only allocates the page when NPT is disabled, or when L0 is 32-bit (using PAE paging). Note, there is an existing memory leak involving the MMU roots, as KVM fails to free the PAE roots on failure. This will be addressed in a future commit. Fixes: ee6268ba3a68 ("KVM: x86: Skip pae_root shadow allocation if tdp enabled") Fixes: b6b80c78af83 ("KVM: x86/mmu: Allocate PAE root array when using SVM's 32-bit NPT") Cc: stable@vger.kernel.org Reviewed-by: Ben Gardon Signed-off-by: Sean Christopherson Message-Id: <20210305011101.3597423-3-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/mmu/mmu.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3195,14 +3195,14 @@ void kvm_mmu_free_roots(struct kvm_vcpu if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL && (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) { mmu_free_root_page(kvm, &mmu->root_hpa, &invalid_list); - } else { + } else if (mmu->pae_root) { for (i = 0; i < 4; ++i) if (mmu->pae_root[i] != 0) mmu_free_root_page(kvm, &mmu->pae_root[i], &invalid_list); - mmu->root_hpa = INVALID_PAGE; } + mmu->root_hpa = INVALID_PAGE; mmu->root_pgd = 0; } @@ -3314,9 +3314,23 @@ static int mmu_alloc_shadow_roots(struct * the shadow page table may be a PAE or a long mode page table. */ pm_mask = PT_PRESENT_MASK; - if (vcpu->arch.mmu->shadow_root_level == PT64_ROOT_4LEVEL) + if (vcpu->arch.mmu->shadow_root_level == PT64_ROOT_4LEVEL) { pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK; + /* + * Allocate the page for the PDPTEs when shadowing 32-bit NPT + * with 64-bit only when needed. Unlike 32-bit NPT, it doesn't + * need to be in low mem. See also lm_root below. + */ + if (!vcpu->arch.mmu->pae_root) { + WARN_ON_ONCE(!tdp_enabled); + + vcpu->arch.mmu->pae_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); + if (!vcpu->arch.mmu->pae_root) + return -ENOMEM; + } + } + for (i = 0; i < 4; ++i) { MMU_WARN_ON(VALID_PAGE(vcpu->arch.mmu->pae_root[i])); if (vcpu->arch.mmu->root_level == PT32E_ROOT_LEVEL) { @@ -3339,21 +3353,19 @@ static int mmu_alloc_shadow_roots(struct vcpu->arch.mmu->root_hpa = __pa(vcpu->arch.mmu->pae_root); /* - * If we shadow a 32 bit page table with a long mode page - * table we enter this path. + * When shadowing 32-bit or PAE NPT with 64-bit NPT, the PML4 and PDP + * tables are allocated and initialized at MMU creation as there is no + * equivalent level in the guest's NPT to shadow. Allocate the tables + * on demand, as running a 32-bit L1 VMM is very rare. The PDP is + * handled above (to share logic with PAE), deal with the PML4 here. */ if (vcpu->arch.mmu->shadow_root_level == PT64_ROOT_4LEVEL) { if (vcpu->arch.mmu->lm_root == NULL) { - /* - * The additional page necessary for this is only - * allocated on demand. - */ - u64 *lm_root; lm_root = (void*)get_zeroed_page(GFP_KERNEL_ACCOUNT); - if (lm_root == NULL) - return 1; + if (!lm_root) + return -ENOMEM; lm_root[0] = __pa(vcpu->arch.mmu->pae_root) | pm_mask; @@ -5297,9 +5309,11 @@ static int __kvm_mmu_create(struct kvm_v * while the PDP table is a per-vCPU construct that's allocated at MMU * creation. When emulating 32-bit mode, cr3 is only 32 bits even on * x86_64. Therefore we need to allocate the PDP table in the first - * 4GB of memory, which happens to fit the DMA32 zone. Except for - * SVM's 32-bit NPT support, TDP paging doesn't use PAE paging and can - * skip allocating the PDP table. + * 4GB of memory, which happens to fit the DMA32 zone. TDP paging + * generally doesn't use PAE paging and can skip allocating the PDP + * table. The main exception, handled here, is SVM's 32-bit NPT. The + * other exception is for shadowing L1's 32-bit or PAE NPT on 64-bit + * KVM; that horror is handled on-demand by mmu_alloc_shadow_roots(). */ if (tdp_enabled && kvm_mmu_get_tdp_level(vcpu) > PT32E_ROOT_LEVEL) return 0;