[PATCH 2/2] x86/mm/KASLR: Adjust the vmemmap size according to paging mode

Baoquan He
2018-08-29T02:18:08Z

Vmemmap area has different base and size depending on paging mode.
Now we just hardcode its size as 1TB in memory KASLR, it's not
right for 5-level paging mode.

Adjust it according to paging mode and use it during memory KASLR.

Signed-off-by: Baoquan He <bhe@redhat.com>
---
arch/x86/include/asm/pgtable_64_types.h | 5 +++++
arch/x86/mm/kaslr.c | 3 ++-
2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/pgtable_64_types.h b/arch/x86/include/asm/pgtable_64_types.h
index 04edd2d58211..fa759d2d3186 100644
--- a/arch/x86/include/asm/pgtable_64_types.h
+++ b/arch/x86/include/asm/pgtable_64_types.h
@@ -126,14 +126,19 @@ extern unsigned int ptrs_per_p4d;
#define __VMEMMAP_BASE_L4 0xffffea0000000000UL
#define __VMEMMAP_BASE_L5 0xffd4000000000000UL

+#define VMEMMAP_SIZE_TB_L4 1UL
+#define VMEMMAP_SIZE_TB_L5 512UL
+
#ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT
# define VMALLOC_START vmalloc_base
# define VMALLOC_SIZE_TB (pgtable_l5_enabled() ? VMALLOC_SIZE_TB_L5 : VMALLOC_SIZE_TB_L4)
# define VMEMMAP_START vmemmap_base
+# define VMEMMAP_SIZE_TB (pgtable_l5_enabled() ? VMEMMAP_SIZE_TB_L5 : VMEMMAP_SIZE_TB_L4)
#else
# define VMALLOC_START __VMALLOC_BASE_L4
# define VMALLOC_SIZE_TB VMALLOC_SIZE_TB_L4
# define VMEMMAP_START __VMEMMAP_BASE_L4
+# define VMEMMAP_SIZE_TB VMEMMAP_SIZE_TB_L4
#endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */

#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1)
diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 0988971069c9..69228af4c7d7 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -51,7 +51,7 @@ static __initdata struct kaslr_memory_region {
} kaslr_regions[] = {
{ &page_offset_base, 0 },
{ &vmalloc_base, 0 },
- { &vmemmap_base, 1 },
+ { &vmemmap_base, 0 },
};

/* Get size in bytes used by the memory region */
@@ -95,6 +95,7 @@ void __init kernel_randomize_memory(void)

kaslr_regions[0].size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT);
kaslr_regions[1].size_tb = VMALLOC_SIZE_TB;
+ kaslr_regions[2].size_tb = VMEMMAP_SIZE_TB;

/*
* Update Physical memory mapping to available and
--
2.13.6