Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1260224ybl; Fri, 30 Aug 2019 14:50:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqyNZn6j+NJ6X2BkezV1okfMZVeP7Rd+mihDURGmYe+NucuojiiNfhxq8WW0T+5NOJLycbOO X-Received: by 2002:a65:68d9:: with SMTP id k25mr14734919pgt.337.1567201837407; Fri, 30 Aug 2019 14:50:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567201837; cv=none; d=google.com; s=arc-20160816; b=v4Fb1hSDYVdjLpBNLTqgHoox6oblwXPePrhPd92MwfbmYSKXGdFISJFcNhG2xzvMGW jOulu5cRue2Gvqlzl4zA32qmBgwsWkEpgiuQUeb1k0teudBOf2Z3LUK11gVJ4yHGR59y Xty1j70msQowfeWu4/USDUnW2u5q+gU120cLF5HCBySEf36LqF6ha7KxN3zA2aIpGVsS l+Z1Y7l4PGdDIdVmnoRQHV/TGEQSZ1lc8M2xtu0xlevIOR/A65iSvTV3H3MOEproBFMa XmZPzQYdezWbktFphts870yERRsFLh+8IziWQtVC5ozSXaK2DapgLAXFMmIdexdB+uM4 0ZAA== 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:dkim-signature; bh=4w/5foUOWsGiIALhc9FkX1i52x/QwOcQXv5vExOoBOQ=; b=ijtdpAbtqPWqSlgxJQJviAnjpV097qCvLUFHCMKBhp3sbJZbfKBpcUXGTqkCJ8zyX3 ecztPLGqNGnU7ulzWvETmbu6KJCoESY1qoqsQJrEoEmIXJkxEavkdzMZiVZrkWJQKsi1 QNxTVK/pbjDoijbIqigww7KMt7bC7BXvOR2hj66EicJcqr0KEfXKMapfZEEpjuOlavMU P+nyuLWrB7YQcGbNROxk1tMnbqKRzfYhwRTnEGrwG2/vhL5xFQm8D4+dkd5iuEFWREIU lgzBIl9zkhLdJlHCLCoLBUUez0ZpuiQ59SFInwiW6jh/BVta95tGMIqXjN9ao09BQpR4 iRwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uHXoLyNP; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si5275654pga.260.2019.08.30.14.50.22; Fri, 30 Aug 2019 14:50:37 -0700 (PDT) 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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uHXoLyNP; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728465AbfH3VsG (ORCPT + 99 others); Fri, 30 Aug 2019 17:48:06 -0400 Received: from mail-qt1-f193.google.com ([209.85.160.193]:45960 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728455AbfH3VsF (ORCPT ); Fri, 30 Aug 2019 17:48:05 -0400 Received: by mail-qt1-f193.google.com with SMTP id r15so3594564qtn.12 for ; Fri, 30 Aug 2019 14:48:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4w/5foUOWsGiIALhc9FkX1i52x/QwOcQXv5vExOoBOQ=; b=uHXoLyNPT7g+1xR6937SaYsBmR7HeBZDJ89JU2W4k2H/qnX8OkvNHYZgN9lq8JNRIb MoNfjR4nzlVMgyCqp2ClBfhU16UUhKZKFNQEW1yGnpA++xAWBZrBiwtX1w2HNfcM+rgM xKu6O4AP2AV0rzPzWOJfTwzYVBcVdiJ4UfpXeGw3RqjBFegxYjql8Swlr4fRUG486CcI wRq/v59BYWvutChVlIr1wjptEOAQkAp6da6UVV3ne7yw2FfLwMkCifDL4Rx6mApBaK68 df/oDRTW7F2ALlUq55UiIDKMSfF6Cjr91mst4WkKcgDHgl+sHp1VaxAwrMmmyJYDpmBA Rgeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4w/5foUOWsGiIALhc9FkX1i52x/QwOcQXv5vExOoBOQ=; b=bFHUQOTYiTqPY2uJhTqiiJP5szzj00fKU/wVlWBIRT9xZF+kEPIPAckfmCH4GhpKw+ l387KgTqx3+vB5abyD7rN0Fd31Nz8zXqXPsr3OLwq3BRbUEYoUaLD3PdBKG+aLYVqUQn Vr8/QCrQ3Mgfg5HnIQW1fkE1Z7/oPpS4KZ+6eaAkWpQaqFIUVPjn8av4mTQA6Y5Hy1aa dtNnzFRGEWX+mozYpBgTTNYLyNBd4EKgC4u0Lo+bdoSe+FUAHPMrEJL5W3/u2MO8qbKE BD7Y5DnaZBotSty24IR/je8dVzJ62Jhcz0rk187QTNnHzWmaldliqN3Tn7vfi6vM5OEO 7fjw== X-Gm-Message-State: APjAAAWocVzoJJgL2P9PqwMWMxyaaVygWnoFOgBIgP6EdqRTFdx8dUth xYca24hGAcbCSgWM6MlzNw== X-Received: by 2002:a0c:fec4:: with SMTP id z4mr11930335qvs.101.1567201684588; Fri, 30 Aug 2019 14:48:04 -0700 (PDT) Received: from gabell.bos.redhat.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id a4sm4857834qtb.17.2019.08.30.14.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2019 14:48:04 -0700 (PDT) From: Masayoshi Mizuma To: Borislav Petkov , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Baoquan He Cc: Masayoshi Mizuma , Masayoshi Mizuma , linux-kernel@vger.kernel.org Subject: [PATCH v3 4/5] x86/mm/KASLR: Cleanup calculation for direct mapping size Date: Fri, 30 Aug 2019 17:47:06 -0400 Message-Id: <20190830214707.1201-5-msys.mizuma@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190830214707.1201-1-msys.mizuma@gmail.com> References: <20190830214707.1201-1-msys.mizuma@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masayoshi Mizuma Cleanup calculation for direct mapping size. Signed-off-by: Baoquan He Signed-off-by: Masayoshi Mizuma --- arch/x86/mm/kaslr.c | 50 +++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c index dc6182eec..8e5f3642e 100644 --- a/arch/x86/mm/kaslr.c +++ b/arch/x86/mm/kaslr.c @@ -70,15 +70,45 @@ static inline bool kaslr_memory_enabled(void) return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN); } +/* + * Even though a huge virtual address space is reserved for the direct + * mapping of physical memory, e.g in 4-level paging mode, it's 64TB, + * rare system can own enough physical memory to use it up, most are + * even less than 1TB. So with KASLR enabled, we adapt the size of + * direct mapping area to the size of actual physical memory plus the + * configured padding CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING. + * The left part will be taken out to join memory randomization. + */ +static inline unsigned long calc_direct_mapping_size(void) +{ + unsigned long size_tb, memory_tb; + + /* + * Update Physical memory mapping to available and + * add padding if needed (especially for memory hotplug support). + */ + memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) + + CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING; + + size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT); + + /* + * Adapt physical memory region size based on available memory + */ + if (memory_tb < size_tb) + size_tb = memory_tb; + + return size_tb; +} + /* Initialize base and padding for each memory region randomized with KASLR */ void __init kernel_randomize_memory(void) { - size_t i; - unsigned long vaddr_start, vaddr; - unsigned long rand, memory_tb; - struct rnd_state rand_state; + unsigned long vaddr_start, vaddr, rand; unsigned long remain_entropy; unsigned long vmemmap_size; + struct rnd_state rand_state; + size_t i; vaddr_start = pgtable_l5_enabled() ? __PAGE_OFFSET_BASE_L5 : __PAGE_OFFSET_BASE_L4; vaddr = vaddr_start; @@ -95,20 +125,10 @@ void __init kernel_randomize_memory(void) if (!kaslr_memory_enabled()) return; - kaslr_regions[0].size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT); + kaslr_regions[0].size_tb = calc_direct_mapping_size(); kaslr_regions[1].size_tb = VMALLOC_SIZE_TB; - /* - * Update Physical memory mapping to available and - * add padding if needed (especially for memory hotplug support). - */ BUG_ON(kaslr_regions[0].base != &page_offset_base); - memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) + - CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING; - - /* Adapt phyiscal memory region size based on available memory */ - if (memory_tb < kaslr_regions[0].size_tb) - kaslr_regions[0].size_tb = memory_tb; /* * Calculate the vmemmap region size in TBs, aligned to a TB -- 2.18.1