Boot-time switching between 4- and 5-level paging for 4.15, Part 2
Subsystems (3)
PATCH 2/4 x86/boot/compressed/64: Detect and handle 5-level paging at boot-time
In reply to Kirill A. ShutemovThis patch prepare decompression code to boot-time switching between 4- and 5-level paging.
--- arch/x86/boot/compressed/head_64.S | 16 ++++++++++++---- arch/x86/boot/compressed/pagetable.c | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/head_64.S +12 -4
@@ -288,10 +288,18 @@ ENTRY(startup_64) leaq boot_stack_end(%rbx), %rsp #ifdef CONFIG_X86_5LEVEL - /* Check if 5-level paging has already enabled */ - movq %cr4, %rax - testl $X86_CR4_LA57, %eax - jnz lvl5 + /* + * Check if we need to enable 5-level paging. + * RSI holds real mode data and need to be preserved across + * a function call. + */ + pushq %rsi + call need_to_enabled_l5 + popq %rsi + + /* If need_to_enabled_l5() returned zero, we're done here. */ + cmpq $0, %rax + je lvl5 /* * At this point we are in long mode with 4-level paging enabled,
arch/x86/boot/compressed/pagetable.c +19 -0
@@ -149,3 +149,22 @@ void finalize_identity_maps(void) {write_cr3 (top_level_pgt); } + +#ifdef CONFIG_X86_5LEVEL +int need_to_enabled_l5 (void ) +{ +/* Check i leaf 7 is supported. */ +if (native_cpuid_eax (0 ) <7 ) +return 0 ; + +/* Check if la57 is supported. */ +if (!(native_cpuid_ecx (7 ) & (1 << (X86_FEATURE_LA57 &31 )))) +return 0 ; + +/* Check if 5-level paging has already been enabled. */ +if (native_read_cr4 () &X86_CR4_LA57 ) +return 0 ; + +return 1 ; +} +#endif
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/4 x86/boot/compressed/64: Introduce place_trampoline()
In reply to Kirill A. ShutemovIf bootloader enables 64-bit mode with 4-level paging, we need to switch over to 5-level paging. The switching requires disabling paging. It works fine if kernel itself is loaded below 4G.
If bootloader put the kernel above 4G (not sure if anybody does this), we would loose control as soon as paging is disabled as code becomes unreachable.
To handle the situation, we need a trampoline in lower memory that would take care about switching on 5-level paging.
Apart from trampoline itself we also need place to store top level page table in lower memory as we don't have a way to load 64-bit value into CR3 from 32-bit mode. We only really need 8-bytes there as we only use the very first entry of the page table. But we allocate whole page anyway. We cannot have the code in the same because, there's hazard that a CPU would read page table speculatively and get confused seeing garbage.
This patch introduces place_trampoline() that finds right spot in lower memory for trampoline, copies trampoline code there and setups new top level page table for 5-level paging.
At this point we do all the preparation, but not yet use trampoline. It will be done in following patch.
--- arch/x86/boot/compressed/head_64.S | 13 +++++++++++ arch/x86/boot/compressed/pagetable.c | 42 ++++++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/pagetable.h | 18 ++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 arch/x86/boot/compressed/pagetable.h
arch/x86/boot/compressed/head_64.S +13 -0
@@ -315,6 +315,18 @@ ENTRY(startup_64) * The first step is go into compatibility mode. */ + /* + * Find suitable place for trampoline and populate it. + * The address will be stored in RCX. + * + * RSI holds real mode data and need to be preserved across + * a function call. + */ + pushq %rsi + call place_trampoline + popq %rsi + movq %rax, %rcx + /* Clear additional page table */ leaq lvl5_pgtable(%rbx), %rdi xorq %rax, %rax
@@ -474,6 +486,7 @@ relocated: .code32 #ifdef CONFIG_X86_5LEVEL +ENTRY(lvl5_trampoline_src) compatible_mode: /* Setup data and stack segments */ movl $__KERNEL_DS, %eax
arch/x86/boot/compressed/pagetable.c +42 -0
52 lines not on this page · raw message
arch/x86/boot/compressed/pagetable.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef BOOT_COMPRESSED_PAGETABLE_H +#define BOOT_COMPRESSED_PAGETABLE_H + +#define LVL5_TRAMPOLINE_SIZE (2 * PAGE_SIZE) + +#define LVL5_TRAMPOLINE_PGTABLE_OFF 0 + +#define LVL5_TRAMPOLINE_CODE_OFF PAGE_SIZE +#define LVL5_TRAMPOLINE_CODE_SIZE 0x40 + +#define LVL5_TRAMPOLINE_STACK_END LVL5_TRAMPOLINE_SIZE + +#ifndef __ASSEMBLER__ + +extern void (*lvl5_trampoline_src)(void *return_ptr); + +#endif /* __ASSEMBLER__ */ +#endif /* BOOT_COMPRESSED_PAGETABLE_H */
Signature · 1 line
2.14.2
PATCH 1/4 mm/zsmalloc: Prepare to variable MAX_PHYSMEM_BITS
In reply to Kirill A. ShutemovWith boot-time switching between paging mode we will have variable MAX_PHYSMEM_BITS.
Let's use the maximum variable possible for CONFIG_X86_5LEVEL=y configuration to define zsmalloc data structures.
The patch introduces MAX_POSSIBLE_PHYSMEM_BITS to cover such case. It also suits well to handle PAE special case.
--- arch/x86/include/asm/pgtable-3level_types.h | 1 + arch/x86/include/asm/pgtable_64_types.h | 2 ++ mm/zsmalloc.c | 13 +++++++------ 3 files changed, 10 insertions(+), 6 deletions(-)
arch/x86/include/asm/pgtable-3level_types.h +1 -0
@@ -43,5 +43,6 @@ typedef union {*/ #define PTRS_PER_PTE 512 +#define MAX_POSSIBLE_PHYSMEM_BITS 36#endif /* _ASM_X86_PGTABLE_3LEVEL_DEFS_H */
arch/x86/include/asm/pgtable_64_types.h +2 -0
@@ -39,6 +39,8 @@ typedef struct { pteval_t pte; } pte_t;#define P4D_SIZE (_AC(1, UL) << P4D_SHIFT)#define P4D_MASK (~(P4D_SIZE - 1)) +#define MAX_POSSIBLE_PHYSMEM_BITS 52 +#else /* CONFIG_X86_5LEVEL */ /*
mm/zsmalloc.c +7 -6
@@ -82,18 +82,19 @@* This is made more complicated by various memory models and PAE. */ -#ifndef MAX_PHYSMEM_BITS -#ifdef CONFIG_HIGHMEM64G -#define MAX_PHYSMEM_BITS 36 -#else /* !CONFIG_HIGHMEM64G */ +#ifndef MAX_POSSIBLE_PHYSMEM_BITS +#ifdef MAX_PHYSMEM_BITS +#define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS +#else /* * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just * be PAGE_SHIFT */ -#define MAX_PHYSMEM_BITS BITS_PER_LONG +#define MAX_POSSIBLE_PHYSMEM_BITS BITS_PER_LONG#endif #endif -#define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT) + +#define _PFN_BITS (MAX_POSSIBLE_PHYSMEM_BITS - PAGE_SHIFT)/* * Memory for allocating for handle keeps object position by
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
cover Boot-time switching between 4- and 5-level paging for 4.15, Part 2
Hi Ingo,
Here's the second bunch of patches that prepare kernel to boot-time switching between paging modes.
It's a small one. I hope we can get it in quick. :)
I include the zsmalloc patch again. We need something to address the issue. If we would find a better solution, we can come back to the topic and rework it.
Apart from zsmalloc patch, the patchset includes changes to decompression code. I reworked these patches. They are split not exactly the way you've described before, but I hope it's sensible anyway.
Please review and consider applying.
Kirill A. Shutemov (4):
mm/zsmalloc: Prepare to variable MAX_PHYSMEM_BITS x86/boot/compressed/64: Detect and handle 5-level paging at boot-time x86/boot/compressed/64: Introduce place_trampoline() x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G
arch/x86/boot/compressed/head_64.S | 99 ++++++++++++++++++++--------- arch/x86/boot/compressed/pagetable.c | 61 ++++++++++++++++++ arch/x86/boot/compressed/pagetable.h | 18 ++++++ arch/x86/include/asm/pgtable-3level_types.h | 1 + arch/x86/include/asm/pgtable_64_types.h | 2 + mm/zsmalloc.c | 13 ++-- 6 files changed, 158 insertions(+), 36 deletions(-) create mode 100644 arch/x86/boot/compressed/pagetable.h
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 4/4 x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G
This patch addresses shortcoming in current boot process on machines that supports 5-level paging.
If bootloader enables 64-bit mode with 4-level paging, we need to switch over to 5-level paging. The switching requires disabling paging. It works fine if kernel itself is loaded below 4G.
If bootloader put the kernel above 4G (not sure if anybody does this), we would loose control as soon as paging is disabled as code becomes unreachable.
This patch implements trampoline in lower memory to handle this situation.
We only need the memory for very short time, until main kernel image setup its own page tables.
--- arch/x86/boot/compressed/head_64.S | 72 ++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 27 deletions(-)
arch/x86/boot/compressed/head_64.S +45 -27
@@ -32,6 +32,7 @@ #include <asm/processor-flags.h> #include <asm/asm-offsets.h> #include <asm/bootparam.h> +#include "pagetable.h" /* * Locally defined symbols should be marked hidden:
@@ -288,6 +289,19 @@ ENTRY(startup_64) leaq boot_stack_end(%rbx), %rsp #ifdef CONFIG_X86_5LEVEL +/* + * We need trampoline in lower memory switch from 4- to 5-level paging for + * cases when bootloader put kernel above 4G, but didn't enable 5-level paging + * for us. + * + * We also have to have top page table in lower memory as we don't have a way + * to load 64-bit value into CR3 from 32-bit mode. We only need 8-bytes there + * as we only use the very first entry of the page table, but we allocate whole + * page anyway. We cannot have the code in the same because, there's hazard + * that a CPU would read page table speculatively and get confused seeing + * garbage. + */ + /* * Check if we need to enable 5-level paging. * RSI holds real mode data and need to be preserved across
@@ -309,8 +323,8 @@ ENTRY(startup_64) * long mode would trigger #GP. So we need to switch off long mode * first. * - * NOTE: This is not going to work if bootloader put us above 4G - * limit. + * We use trampoline in lower memory to handle situation when + * bootloader put the kernel image above 4G. * * The first step is go into compatibility mode. */
@@ -327,26 +341,20 @@ ENTRY(startup_64) popq %rsi movq %rax, %rcx - /* Clear additional page table */ - leaq lvl5_pgtable(%rbx), %rdi - xorq %rax, %rax - movq $(PAGE_SIZE/8), %rcx - rep stosq - /* - * Setup current CR3 as the first and only entry in a new top level - * page table. + * Load address of lvl5 into RDI. + * It will be used to return address from trampoline. */ - movq %cr3, %rdi - leaq 0x7 (%rdi), %rax - movq %rax, lvl5_pgtable(%rbx) + leaq lvl5(%rip), %rdi /* Switch to compatibility mode (CS.L = 0 CS.D = 1) via far return */ pushq $__KERNEL32_CS - leaq compatible_mode(%rip), %rax + leaq LVL5_TRAMPOLINE_CODE_OFF(%rcx), %rax pushq %rax lretq lvl5: + /* Restore stack, 32-bit trampoline uses own stack */ + leaq boot_stack_end(%rbx), %rsp #endif /* Zero EFLAGS */
@@ -484,22 +492,30 @@ relocated: */ jmp *%rax - .code32 #ifdef CONFIG_X86_5LEVEL + .code32 +/* + * This is 32-bit trampoline that will be copied over to low memory. + * + * RDI contains return address (might be above 4G). + * ECX contains the base address of trampoline memory. + */ ENTRY(lvl5_trampoline_src) -compatible_mode: /* Setup data and stack segments */ movl $__KERNEL_DS, %eax movl %eax, %ds movl %eax, %ss + /* Setup new stack at the end of trampoline memory */ + leal LVL5_TRAMPOLINE_STACK_END (%ecx), %esp + /* Disable paging */ movl %cr0, %eax btrl $X86_CR0_PG_BIT, %eax movl %eax, %cr0 /* Point CR3 to 5-level paging */ - leal lvl5_pgtable(%ebx), %eax + leal LVL5_TRAMPOLINE_PGTABLE_OFF (%ecx), %eax movl %eax, %cr3 /* Enable PAE and LA57 mode */
@@ -507,23 +523,29 @@ compatible_mode: orl $(X86_CR4_PAE | X86_CR4_LA57), %eax movl %eax, %cr4 - /* Calculate address we are running at */ - call 1f -1: popl %edi - subl $1b, %edi + /* Calculate address of lvl5_enabled once we are in trampoline */ + leal lvl5_enabled - lvl5_trampoline_src + LVL5_TRAMPOLINE_CODE_OFF (%ecx), %eax /* Prepare stack for far return to Long Mode */ pushl $__KERNEL_CS - leal lvl5(%edi), %eax - push %eax + pushl %eax /* Enable paging back */ movl $(X86_CR0_PG | X86_CR0_PE), %eax movl %eax, %cr0 lret + + .code64 +lvl5_enabled: + /* Return from trampoline */ + jmp *%rdi + + /* Bound size of trampoline code */ + .org lvl5_trampoline_src + LVL5_TRAMPOLINE_CODE_SIZE #endif + .code32 no_longmode: /* This isn't an x86-64 CPU so hang */ 1:
@@ -581,7 +603,3 @@ boot_stack_end: .balign 4096 pgtable: .fill BOOT_PGT_SIZE, 1, 0 -#ifdef CONFIG_X86_5LEVEL -lvl5_pgtable: - .fill PAGE_SIZE, 1, 0 -#endif
Signature · 7 lines
2.14.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/4] mm/zsmalloc: Prepare to variable MAX_PHYSMEM_BITS
In reply to Kirill A. ShutemovQuoted 9 lines · Kirill A. Shutemov
With boot-time switching between paging mode we will have variable
MAX_PHYSMEM_BITS.Let's use the maximum variable possible for
CONFIG_X86_5LEVEL=yconfiguration to define zsmalloc data structures.The patch introduces
MAX_POSSIBLE_PHYSMEM_BITSto cover such case. It also suits well to handle PAE special case.
I see that with your upcoming patch, MAX_PHYSMEM_BITS is turned into a variable for x86_64 case as: (pgtable_l5_enabled ? 52 : 46).
Even with this change, I don't see a need for this new MAX_POSSIBLE_PHYSMEM_BITS constant.
mm/zsmalloc.c line 88
Quoted 8 lines
mm/zsmalloc.c +4
-4-#ifndef MAX_PHYSMEM_BITS -#ifdef CONFIG_HIGHMEM64G -#define MAX_PHYSMEM_BITS 36 -#else /* !CONFIG_HIGHMEM64G */ +#ifndef MAX_POSSIBLE_PHYSMEM_BITS +#ifdef MAX_PHYSMEM_BITS +#define MAX_POSSIBLE_PHYSMEM_BITS MAX_PHYSMEM_BITS +#else
This ifdef on HIGHMEM64G is redundant, as x86 already defines MAX_PHYSMEM_BITS = 36 in PAE case. So, all that zsmalloc should do is:
#ifndef MAX_PHYSMEM_BITS
#define MAX_PHYSMEM_BITS BITS_PER_LONG
#endif
.. and then no change is needed for rest of derived constants like _PFN_BITS.
It is upto every arch to define correct MAX_PHYSMEM_BITS (variable or constant) based on whatever configurations the arch supports. If not defined, zsmalloc picks
a reasonable default of BITS_PER_LONG.
I will send a patch which makes the change to remove ifdef on CONFIG_HIGHMEM64G.
Thanks,
Nitin
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/4] mm/zsmalloc: Prepare to variable MAX_PHYSMEM_BITS
Quoted 13 lines · Nitin Gupta
Quoted 9 lines · Kirill A. Shutemov
With boot-time switching between paging mode we will have variable
MAX_PHYSMEM_BITS.Let's use the maximum variable possible for
CONFIG_X86_5LEVEL=yconfiguration to define zsmalloc data structures.The patch introduces
MAX_POSSIBLE_PHYSMEM_BITSto cover such case. It also suits well to handle PAE special case.
Quoted 5 lines
I see that with your upcoming patch,
MAX_PHYSMEM_BITSis turned into a variable forx86_64case as: (pgtable_l5_enabled? 52 : 46).Even with this change, I don't see a need for this new
MAX_POSSIBLE_PHYSMEM_BITSconstant.
This is the error, I'm talking about:
mm/zsmalloc.c:249:21: error: variably modified a??size_classa?? at file scope
struct size_class *size_class[ZS_SIZE_CLASSES];
ZS_SIZE_CLASSES
ZS_MIN_ALLOC_SIZE
OBJ_INDEX_BITS
_PFN_BITS
MAX_PHYSMEM_BITS
(pgtable_l5_enabled ? 52 : 46)Check without the patch and full patchset applied.
Signature · 7 lines
Kirill A. Shutemov -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/4] mm/zsmalloc: Prepare to variable MAX_PHYSMEM_BITS
In reply to Kirill A. ShutemovQuoted 6 lines · Kirill A. Shutemov
With boot-time switching between paging mode we will have variable
MAX_PHYSMEM_BITS.Let's use the maximum variable possible for
CONFIG_X86_5LEVEL=yconfiguration to define zsmalloc data structures.
Quoted 7 lines
The patch introduces
MAX_POSSIBLE_PHYSMEM_BITSto cover such case. It also suits well to handle PAE special case.Signed-off-by Kirill A. Shutemov <[email protected]>Cc Minchan Kim <[email protected]>Cc Nitin Gupta <[email protected]>Cc Sergey Senozhatsky <[email protected]>
Nitin:
I think this patch works and it would be best for Kirill to be able to do. So if you have better idea to clean it up, let's make it as another patch regardless of this patch series.
Thanks.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Nitin Gupta
Reviewed-byRe: [PATCH 1/4] mm/zsmalloc: Prepare to variable MAX_PHYSMEM_BITS
Quoted 14 lines · Minchan Kim
Quoted 6 lines · Kirill A. Shutemov
With boot-time switching between paging mode we will have variable
MAX_PHYSMEM_BITS.Let's use the maximum variable possible for
CONFIG_X86_5LEVEL=yconfiguration to define zsmalloc data structures.Quoted 7 lines
The patch introduces
MAX_POSSIBLE_PHYSMEM_BITSto cover such case. It also suits well to handle PAE special case.Signed-off-by Kirill A. Shutemov <[email protected]>Cc Minchan Kim <[email protected]>Cc Nitin Gupta <[email protected]>Cc Sergey Senozhatsky <[email protected]>
Quoted 8 lines
Acked-by Minchan Kim <[email protected]>Nitin:
I think this patch works and it would be best for Kirill to be able to do. So if you have better idea to clean it up, let's make it as another patch regardless of this patch series.
I was looking into dynamically allocating size_class array to avoid that compile error, but yes, that can be done in a future patch. So, for this patch:
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>