Received: by 10.223.176.46 with SMTP id f43csp4432781wra; Tue, 23 Jan 2018 09:20:00 -0800 (PST) X-Google-Smtp-Source: AH8x226tGqGKLgUvEZFHg184OU6/sbBytOCOEvwpUr9SCgahlcyYWsErVLWuOyTKBSY91bSaneFc X-Received: by 10.36.185.18 with SMTP id w18mr4582101ite.140.1516728000040; Tue, 23 Jan 2018 09:20:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516727999; cv=none; d=google.com; s=arc-20160816; b=KFvj9RHCl37wAgIGOIpOEkZFHc9lKr+l/ch0EJWAvcLqsf43at+4Cp/3g+psW2U7ES GnpyxB47/rIlo3JTgAdybWPqXRdKS+RFd2oJ0HZhvY8YpJ9qpXMuf8oI1vMh9/x2bQ4n MRULN2oNaQOZ9qELc/8Dgfoc3+XvO3NuUmah0zVjQcnVT8XVuzGl7YWPwQIOX5VaOemA 86UaQE9g6rwqVtECas5GTP+j/pVJMN+zCFwXJ+spnoR3TcyP8odKPcqWl24YOdCvF2Xw gNTzs1vsIdrlRouiNruL3wwYeKT5BD14ruDNcyGGrnuC0mGrWs8P9BENk/A0phNiKsha YQZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=RP009jFZ3BBaLb2NAZnk9dbPsYzYZ/bJ657MIjXg8co=; b=gAssvTai26oxoHTZctlk+Cc5eIZfFuRBfnx7lveFi6fbOuHBUzQKRQFj4xJunS2kYt 5rWvojWRWpO6G7OsK/y1DzXySxJjBvv+1M4HrzkLsCVajl8wKXO32g1yGOO0PmUpq+em FWdfAUUrbmwd6fnc50sPnO00a9P/U2gKCFTt2fgiiaVo+pxrJig6BdGVEGEeAhiUh/kH A+2M3xJY183mNFoSgYunRqzHTUrn2Lk1z3GiONxjZPNrVzyO3lYILllX7VyC6F6w3qbl S/RkBGQDVWa7z5xTyYo8vsT5kYMGgx/viGLyCJ2PXDuP+T7e9LmF0Vl1ln25TQTjJ7iP pRtw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h83si15733272ioi.78.2018.01.23.09.19.45; Tue, 23 Jan 2018 09:19:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751598AbeAWRTY (ORCPT + 99 others); Tue, 23 Jan 2018 12:19:24 -0500 Received: from mga01.intel.com ([192.55.52.88]:5058 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751325AbeAWRTW (ORCPT ); Tue, 23 Jan 2018 12:19:22 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Jan 2018 09:19:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,402,1511856000"; d="scan'208";a="198161968" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga006.fm.intel.com with ESMTP; 23 Jan 2018 09:19:16 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 7139F314; Tue, 23 Jan 2018 19:19:15 +0200 (EET) From: "Kirill A. Shutemov" To: Ingo Molnar , x86@kernel.org, Thomas Gleixner , "H. Peter Anvin" , Tom Lendacky Cc: Linus Torvalds , Borislav Petkov , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCH 3/3] x86/mm/encrypt: Rewrite sme_pgtable_calc() Date: Tue, 23 Jan 2018 20:19:10 +0300 Message-Id: <20180123171910.55841-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180123171910.55841-1-kirill.shutemov@linux.intel.com> References: <20180123171910.55841-1-kirill.shutemov@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org sme_pgtable_calc() is unnecessary complex. It can be re-written in a more stream-lined way. As a side effect, we would get the code ready to boot-time switching between paging modes. Signed-off-by: Kirill A. Shutemov --- arch/x86/mm/mem_encrypt.c | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 740b8a54f616..88e9d4b1a233 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -556,8 +556,7 @@ static void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd) static unsigned long __init sme_pgtable_calc(unsigned long len) { - unsigned long p4d_size, pud_size, pmd_size, pte_size; - unsigned long total; + unsigned long entries = 0, tables = 0; /* * Perform a relatively simplistic calculation of the pagetable @@ -571,42 +570,25 @@ static unsigned long __init sme_pgtable_calc(unsigned long len) * Incrementing the count for each covers the case where the addresses * cross entries. */ - if (IS_ENABLED(CONFIG_X86_5LEVEL)) { - p4d_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1; - p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D; - pud_size = (ALIGN(len, P4D_SIZE) / P4D_SIZE) + 1; - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; - } else { - p4d_size = 0; - pud_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1; - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; - } - pmd_size = (ALIGN(len, PUD_SIZE) / PUD_SIZE) + 1; - pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD; - pte_size = 2 * sizeof(pte_t) * PTRS_PER_PTE; - total = p4d_size + pud_size + pmd_size + pte_size; + /* PGDIR_SIZE is equal to P4D_SIZE on 4-level machine. */ + if (PTRS_PER_P4D > 1) + entries += (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * sizeof(p4d_t) * PTRS_PER_P4D; + entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * sizeof(pud_t) * PTRS_PER_PUD; + entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * sizeof(pmd_t) * PTRS_PER_PMD; + entries += 2 * sizeof(pte_t) * PTRS_PER_PTE; /* * Now calculate the added pagetable structures needed to populate * the new pagetables. */ - if (IS_ENABLED(CONFIG_X86_5LEVEL)) { - p4d_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE; - p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D; - pud_size = ALIGN(total, P4D_SIZE) / P4D_SIZE; - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; - } else { - p4d_size = 0; - pud_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE; - pud_size *= sizeof(pud_t) * PTRS_PER_PUD; - } - pmd_size = ALIGN(total, PUD_SIZE) / PUD_SIZE; - pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD; - total += p4d_size + pud_size + pmd_size; + if (PTRS_PER_P4D > 1) + tables += DIV_ROUND_UP(entries, PGDIR_SIZE) * sizeof(p4d_t) * PTRS_PER_P4D; + tables += DIV_ROUND_UP(entries, P4D_SIZE) * sizeof(pud_t) * PTRS_PER_PUD; + tables += DIV_ROUND_UP(entries, PUD_SIZE) * sizeof(pmd_t) * PTRS_PER_PMD; - return total; + return entries + tables; } void __init __nostackprotector sme_encrypt_kernel(struct boot_params *bp) -- 2.15.1