Commit-ID: c7d2361f7524f365c1ae42f47880e3fa9efb2c2a Gitweb: http://git.kernel.org/tip/c7d2361f7524f365c1ae42f47880e3fa9efb2c2a Author: Thomas Garnier <thgar...@google.com> AuthorDate: Tue, 9 Aug 2016 10:11:04 -0700 Committer: Ingo Molnar <mi...@kernel.org> CommitDate: Wed, 10 Aug 2016 14:45:19 +0200
x86/mm/KASLR: Fix physical memory calculation on KASLR memory randomization Initialize KASLR memory randomization after max_pfn is initialized. Also ensure the size is rounded up. It could create problems on machines with more than 1Tb of memory on certain random addresses. Signed-off-by: Thomas Garnier <thgar...@google.com> Cc: Aleksey Makarov <aleksey.maka...@linaro.org> Cc: Andrew Morton <a...@linux-foundation.org> Cc: Andy Lutomirski <l...@kernel.org> Cc: Baoquan He <b...@redhat.com> Cc: Borislav Petkov <b...@alien8.de> Cc: Borislav Petkov <b...@suse.de> Cc: Brian Gerst <brge...@gmail.com> Cc: Christian Borntraeger <borntrae...@de.ibm.com> Cc: Dan Williams <dan.j.willi...@intel.com> Cc: Dave Hansen <dave.han...@linux.intel.com> Cc: Dave Young <dyo...@redhat.com> Cc: Denys Vlasenko <dvlas...@redhat.com> Cc: Fabian Frederick <f...@skynet.be> Cc: H. Peter Anvin <h...@zytor.com> Cc: Joerg Roedel <jroe...@suse.de> Cc: Josh Poimboeuf <jpoim...@redhat.com> Cc: Kees Cook <keesc...@chromium.org> Cc: Linus Torvalds <torva...@linux-foundation.org> Cc: Lv Zheng <lv.zh...@intel.com> Cc: Mark Salter <msal...@redhat.com> Cc: Peter Zijlstra <pet...@infradead.org> Cc: Rafael J . Wysocki <rafael.j.wyso...@intel.com> Cc: Thomas Gleixner <t...@linutronix.de> Cc: Toshi Kani <toshi.k...@hp.com> Cc: kernel-harden...@lists.openwall.com Fixes: 021182e52fe0 ("Enable KASLR for physical mapping memory regions") Link: http://lkml.kernel.org/r/1470762665-88032-1-git-send-email-thgar...@google.com Signed-off-by: Ingo Molnar <mi...@kernel.org> --- arch/x86/kernel/setup.c | 8 ++++++-- arch/x86/mm/kaslr.c | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 991b779..95cf31c 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -936,8 +936,6 @@ void __init setup_arch(char **cmdline_p) x86_init.oem.arch_setup(); - kernel_randomize_memory(); - iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1; setup_memory_map(); parse_setup_data(); @@ -1055,6 +1053,12 @@ void __init setup_arch(char **cmdline_p) max_possible_pfn = max_pfn; + /* + * Define random base addresses for memory sections after max_pfn is + * defined and before each memory section base is used. + */ + kernel_randomize_memory(); + #ifdef CONFIG_X86_32 /* max_low_pfn get updated here */ find_low_pfn_range(); diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c index 26dccd6..ec8654f 100644 --- a/arch/x86/mm/kaslr.c +++ b/arch/x86/mm/kaslr.c @@ -97,7 +97,7 @@ void __init kernel_randomize_memory(void) * add padding if needed (especially for memory hotplug support). */ BUG_ON(kaslr_regions[0].base != &page_offset_base); - memory_tb = ((max_pfn << PAGE_SHIFT) >> TB_SHIFT) + + 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 */