Received: by 10.223.176.46 with SMTP id f43csp972184wra; Wed, 24 Jan 2018 08:39:02 -0800 (PST) X-Google-Smtp-Source: AH8x227bDiZGjNdVQwoZuXuT+B77+JER8o+1wvpMExxIo6Jsfpvce+5D6s/6lw+fF5MURiBO9qwO X-Received: by 2002:a17:902:5a1:: with SMTP id f30-v6mr8859510plf.124.1516811942269; Wed, 24 Jan 2018 08:39:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516811942; cv=none; d=google.com; s=arc-20160816; b=XBtHGq4LQbL7mELcHTWFKGD3VjgLrq7k7VT2MiNOWZmWTRbHWVJUz4syVAvRtTuMY6 +HzHO2SmhKXtNno73YyVVypT9IAa3FT5f/dM+AGZs6+AdYMM8GKJ5epb/TOXvrwONDew 6U3j/srd52tQLW0a//RxFfvZcLs5iAd8uaY8vvU9LTy7SRnlrddvmZ1/Q3w8kOXXdcwv /HdGpq75qeqpp6nlZDBHsO/xY5mS2ZucD+Qm4Ilk7NWCKu0sP1VhKPYYxoRtA8Za6iKJ 4CczwHs6ye66neYghts7iOnjEQQnliX3oNuOilUwi6YhPmX6uzblD90ckXov9sdnPpwh lgYA== 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=9Z3dA9/4dzh0it6rUrVf7LzQcowbUz95NvYGhRxJdAI=; b=IFFRs1jDx7ueiKWe90ho56s54oEvCp8/MDn2UZ5xir/SInyF9zNRlVR+zE6jS6vM8G OFAU2Pt9C3OeSpFpqY0PYdtW/+ut4tps1gQy/ZpUzyBgLU9a77Cg9lwXemLhPFSx8J0G lBgPkHLhVjclrYaxMF+sRJ4jcaJjM5tgAcC2//yo9Uod3Wdb4w859PCfRPcTVzMqSM39 p7onXkgVtYTWuGXmLW8yS7NcHYy4GCDogtIrdW+nBqX7MTQhCv3J79JfgGM4RMJwxjQP hR71bZD+f0ChSvqLrsoGsuNyEdYmo1Wh/cqYnaoI0DHZkacySaFZWIi2k13oOFE25WMW 67ow== 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 72si3152083pfj.408.2018.01.24.08.38.48; Wed, 24 Jan 2018 08:39:02 -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 S934365AbeAXQgk (ORCPT + 99 others); Wed, 24 Jan 2018 11:36:40 -0500 Received: from mga14.intel.com ([192.55.52.115]:59475 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934343AbeAXQgj (ORCPT ); Wed, 24 Jan 2018 11:36:39 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Jan 2018 08:36:35 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,408,1511856000"; d="scan'208";a="12730054" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga008.jf.intel.com with ESMTP; 24 Jan 2018 08:36:32 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 91203314; Wed, 24 Jan 2018 18:36:31 +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: [PATCHv3 3/3] x86/mm/encrypt: Rewrite sme_pgtable_calc() Date: Wed, 24 Jan 2018 19:36:23 +0300 Message-Id: <20180124163623.61765-4-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180124163623.61765-1-kirill.shutemov@linux.intel.com> References: <20180124163623.61765-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_identity.c | 42 +++++++++++--------------------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/arch/x86/mm/mem_encrypt_identity.c b/arch/x86/mm/mem_encrypt_identity.c index 69635a02ce9e..613686cc56ae 100644 --- a/arch/x86/mm/mem_encrypt_identity.c +++ b/arch/x86/mm/mem_encrypt_identity.c @@ -230,8 +230,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 @@ -245,42 +244,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