x86/mm/5lvl: Optimize boot-time switching, allow more Xen modes
Subsystems (7)
cover x86/mm/5lvl: Optimize boot-time switching, allow more Xen modes
This is the last batch of patches that enable boot-time switching between paging modes.
The first patch allows two more Xen modes to be enabled with CONFIG_X86_5LEVEL=y. These modes don't support 5-level paging, but we can use them when boot into 4-level paging mode.
The last two patches optimize switching between paging modes by using code pathching in all hot paths.
Please review and cosider applying.
Kirill A. Shutemov (3):
x86/xen: Allow XEN_PV and XEN_PVH to be enabled with X86_5LEVEL x86/mm: Redefine some of page table helpers as macros x86/mm: Offset boot-time paging mode switching cost
arch/x86/boot/compressed/misc.h | 5 +++++ arch/x86/entry/entry_64.S | 11 ++--------- arch/x86/include/asm/paravirt.h | 23 +++++++++++++---------- arch/x86/include/asm/pgtable_64_types.h | 5 ++++- arch/x86/kernel/head64.c | 9 +++++++-- arch/x86/kernel/head_64.S | 14 +++++++------- arch/x86/mm/kasan_init_64.c | 6 ++++++ arch/x86/xen/Kconfig | 5 ----- arch/x86/xen/mmu_pv.c | 21 +++++++++++++++++++++ 9 files changed, 65 insertions(+), 34 deletions(-)
Signature · 7 lines
2.15.1 -- 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 1/3 x86/xen: Allow XEN_PV and XEN_PVH to be enabled with X86_5LEVEL
With boot-time switching between paging modes, XEN_PV and XEN_PVH can be boot into 4-level paging mode.
--- arch/x86/kernel/head_64.S | 12 ++++++------ arch/x86/xen/Kconfig | 5 ----- arch/x86/xen/mmu_pv.c | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-)
arch/x86/kernel/head_64.S +6 -6
@@ -38,12 +38,12 @@ * */ +#define l4_index(x) (((x) >> 39) & 511) #define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1)) -#if defined(CONFIG_XEN_PV) || defined(CONFIG_XEN_PVH) -PGD_PAGE_OFFSET = pgd_index(__PAGE_OFFSET_BASE_L4) -PGD_START_KERNEL = pgd_index(__START_KERNEL_map) -#endif +L4_PAGE_OFFSET = l4_index(__PAGE_OFFSET_BASE_L4) +L4_START_KERNEL = l4_index(__START_KERNEL_map) + L3_START_KERNEL = pud_index(__START_KERNEL_map) .text
@@ -386,9 +386,9 @@ NEXT_PAGE(early_dynamic_pgts) #if defined(CONFIG_XEN_PV) || defined(CONFIG_XEN_PVH) NEXT_PGD_PAGE(init_top_pgt) .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC - .org init_top_pgt + PGD_PAGE_OFFSET*8, 0 + .org init_top_pgt + L4_PAGE_OFFSET*8, 0 .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC - .org init_top_pgt + PGD_START_KERNEL*8, 0 + .org init_top_pgt + L4_START_KERNEL*8, 0 /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */ .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC .fill PTI_USER_PGD_FILL,8,0
arch/x86/xen/Kconfig +0 -5
@@ -18,9 +18,6 @@ config XEN_PV bool "Xen PV guest support" default y depends on XEN - # XEN_PV is not ready to work with 5-level paging. - # Changes to hypervisor are also required. - depends on !X86_5LEVEL select XEN_HAVE_PVMMU select XEN_HAVE_VPMU help
@@ -79,6 +76,4 @@ config XEN_DEBUG_FS config XEN_PVH bool "Support for running as a PVH guest" depends on XEN && XEN_PVHVM && ACPI - # Pre-built page tables are not ready to handle 5-level paging. - depends on !X86_5LEVEL def_bool n
arch/x86/xen/mmu_pv.c +21 -0
@@ -538,6 +538,22 @@ static void xen_set_p4d(p4d_t *ptr, p4d_t val)xen_mc_issue (PARAVIRT_LAZY_MMU ); } + +#if CONFIG_PGTABLE_LEVELS >=5 +__visible p4dval_t xen_p4d_val(p4d_t p4d) +{ +return pte_mfn_to_pfn (p4d.p4d); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_p4d_val); + +__visible p4d_t xen_make_p4d(p4dval_t p4d) +{ + p4d =pte_pfn_to_mfn (p4d); + +return native_make_p4d (p4d); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_make_p4d); +#endif /* CONFIG_PGTABLE_LEVELS >= 5 */ #endif /* CONFIG_X86_64 */ static int xen_pmd_walk(struct mm_struct *mm, pmd_t *pmd,
@@ -2411,6 +2427,11 @@ static const struct pv_mmu_ops xen_mmu_ops __initconst = { .alloc_pud = xen_alloc_pmd_init, .release_pud = xen_release_pmd_init, + +#if CONFIG_PGTABLE_LEVELS >=5 + .p4d_val = PV_CALLEE_SAVE(xen_p4d_val), + .make_p4d = PV_CALLEE_SAVE(xen_make_p4d), +#endif #endif /* CONFIG_X86_64 */ .activate_mm = xen_activate_mm,
Signature · 7 lines
2.15.1 -- 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 2/3 x86/mm: Redefine some of page table helpers as macros
In reply to Kirill A. ShutemovThis is preparation for the next patch, which would change pgtable_l5_enabled to be cpu_feature_enabled(X86_FEATURE_LA57).
The change makes few helpers in paravirt.h dependent on cpu_feature_enabled() definition from cpufeature.h. And cpufeature.h is dependent on paravirt.h.
Let's re-define some of helpers as macros to break this dependency loop.
--- arch/x86/include/asm/paravirt.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
arch/x86/include/asm/paravirt.h +13 -10
@@ -567,19 +567,22 @@ static inline p4dval_t p4d_val(p4d_t p4d) return PVOP_CALLEE1(p4dval_t, pv_mmu_ops.p4d_val, p4d.p4d); } -static inline void set_pgd (pgd_t *pgdp,pgd_t pgd) +static inline void __set_pgd (pgd_t *pgdp,pgd_t pgd) { -if (pgtable_l5_enabled) -PVOP_VCALL2 (pv_mmu_ops.set_pgd, pgdp,native_pgd_val (pgd)); -else -set_p4d ((p4d_t *)(pgdp), (p4d_t ) { pgd.pgd }); +PVOP_VCALL2 (pv_mmu_ops.set_pgd, pgdp,native_pgd_val (pgd)); } -static inline void pgd_clear (pgd_t *pgdp) -{ -if (pgtable_l5_enabled) -set_pgd (pgdp,__pgd (0 )); -} +#define set_pgd (pgdp, pgdval) do { \ + if (pgtable_l5_enabled) \ + __set_pgd(pgdp, pgdval); \ + else \ + set_p4d((p4d_t *)(pgdp), (p4d_t) { (pgdval).pgd }); \ +} while (0) + +#define pgd_clear (pgdp) do { \ + if (pgtable_l5_enabled) \ + set_pgd(pgdp, __pgd(0)); \ +} while (0)#endif /* CONFIG_PGTABLE_LEVELS == 5 */
Signature · 7 lines
2.15.1 -- 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/3 x86/mm: Offset boot-time paging mode switching cost
In reply to Kirill A. ShutemovBy this point we have functioning boot-time switching between 4- and 5-level paging mode. But naive approach comes with cost.
Numbers below are for kernel build, allmodconfig, 5 times.
CONFIG_X86_5LEVEL=n:
Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):
17308719.892691 task-clock:u (msec) # 26.772 CPUs utilized ( +- 0.11% )
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
331,993,164 page-faults:u # 0.019 M/sec ( +- 0.01% )
43,614,978,867,455 cycles:u # 2.520 GHz ( +- 0.01% )
39,371,534,575,126 stalled-cycles-frontend:u # 90.27% frontend cycles idle ( +- 0.09% )
28,363,350,152,428 instructions:u # 0.65 insn per cycle
# 1.39 stalled cycles per insn ( +- 0.00% )
6,316,784,066,413 branches:u # 364.948 M/sec ( +- 0.00% )
250,808,144,781 branch-misses:u # 3.97% of all branches ( +- 0.01% )646.531974142 seconds time elapsed ( +- 1.15% )
CONFIG_X86_5LEVEL=y:
Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):
17411536.780625 task-clock:u (msec) # 26.426 CPUs utilized ( +- 0.10% )
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
331,868,663 page-faults:u # 0.019 M/sec ( +- 0.01% )
43,865,909,056,301 cycles:u # 2.519 GHz ( +- 0.01% )
39,740,130,365,581 stalled-cycles-frontend:u # 90.59% frontend cycles idle ( +- 0.05% )
28,363,358,997,959 instructions:u # 0.65 insn per cycle
# 1.40 stalled cycles per insn ( +- 0.00% )
6,316,784,937,460 branches:u # 362.793 M/sec ( +- 0.00% )
251,531,919,485 branch-misses:u # 3.98% of all branches ( +- 0.00% )658.886307752 seconds time elapsed ( +- 0.92% )
The patch tries to fix the performance regression by using cpu_feature_enabled(X86_FEATURE_LA57) instead of pgtable_l5_enabled in all hot code paths. These will statically patch the target code for additional performance.
CONFIG_X86_5LEVEL=y + the patch:
Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):
17381990.268506 task-clock:u (msec) # 26.907 CPUs utilized ( +- 0.19% )
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
331,862,625 page-faults:u # 0.019 M/sec ( +- 0.01% )
43,697,726,320,051 cycles:u # 2.514 GHz ( +- 0.03% )
39,480,408,690,401 stalled-cycles-frontend:u # 90.35% frontend cycles idle ( +- 0.05% )
28,363,394,221,388 instructions:u # 0.65 insn per cycle
# 1.39 stalled cycles per insn ( +- 0.00% )
6,316,794,985,573 branches:u # 363.410 M/sec ( +- 0.00% )
251,013,232,547 branch-misses:u # 3.97% of all branches ( +- 0.01% )645.991174661 seconds time elapsed ( +- 1.19% )
Unfortunately, this approach doesn't help with text size:
vmlinux.before .text size: 8190319
vmlinux.after .text size: 8200623
The .text section is increased by about 4k. Not sure if we can do anything about this.
--- arch/x86/boot/compressed/misc.h | 5 +++++ arch/x86/entry/entry_64.S | 11 ++--------- arch/x86/include/asm/pgtable_64_types.h | 5 ++++- arch/x86/kernel/head64.c | 9 +++++++-- arch/x86/kernel/head_64.S | 2 +- arch/x86/mm/kasan_init_64.c | 6 ++++++ 6 files changed, 25 insertions(+), 13 deletions(-)
arch/x86/boot/compressed/misc.h +5 -0
@@ -12,6 +12,11 @@#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN +#ifdef CONFIG_X86_5LEVEL +/* cpu_feature_enabled() cannot be used that early */ +#define pgtable_l5_enabled __pgtable_l5_enabled +#endif +#include <linux/linkage.h> #include <linux/screen_info.h> #include <linux/elf.h>
arch/x86/entry/entry_64.S +2 -9
@@ -261,15 +261,8 @@ GLOBAL(entry_SYSCALL_64_after_hwframe) * depending on paging mode) in the address. */ #ifdef CONFIG_X86_5LEVEL - testl $1, pgtable_l5_enabled(%rip) - jz 1f - shl $(64 - 57), %rcx - sar $(64 - 57), %rcx - jmp 2f -1: - shl $(64 - 48), %rcx - sar $(64 - 48), %rcx -2: + ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \ + "shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57 #else shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
arch/x86/include/asm/pgtable_64_types.h +4 -1
@@ -21,7 +21,10 @@ typedef unsigned long pgprotval_t;typedef struct {pteval_t pte; }pte_t ;#ifdef CONFIG_X86_5LEVEL -extern unsigned int pgtable_l5_enabled; +extern unsigned int __pgtable_l5_enabled; +#ifndef pgtable_l5_enabled +#define pgtable_l5_enabled cpu_feature_enabled(X86_FEATURE_LA57) +#endif #else #define pgtable_l5_enabled 0#endif
arch/x86/kernel/head64.c +7 -2
@@ -32,6 +32,11 @@#include <asm/microcode.h> #include <asm/kasan.h> +#ifdef CONFIG_X86_5LEVEL +#undef pgtable_l5_enabled +#define pgtable_l5_enabled __pgtable_l5_enabled +#endif +/* * Manage page tables very early on. */
@@ -40,8 +45,8 @@ static unsigned int __initdata next_early_pgt;pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);#ifdef CONFIG_X86_5LEVEL -unsigned int pgtable_l5_enabled__ro_after_init ; -EXPORT_SYMBOL (pgtable_l5_enabled); +unsigned int __pgtable_l5_enabled__ro_after_init ; +EXPORT_SYMBOL (__pgtable_l5_enabled);unsigned int pgdir_shift__ro_after_init =39 ;EXPORT_SYMBOL (pgdir_shift);unsigned int ptrs_per_p4d__ro_after_init =1 ;
arch/x86/kernel/head_64.S +1 -1
@@ -124,7 +124,7 @@ ENTRY(secondary_startup_64) /* Enable PAE mode, PGE and LA57 */ movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL - testl $1, pgtable_l5_enabled(%rip) + testl $1, __pgtable_l5_enabled(%rip) jz 1f orl $X86_CR4_LA57, %ecx 1:
arch/x86/mm/kasan_init_64.c +6 -0
@@ -1,6 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #define DISABLE_BRANCH_PROFILING #define pr_fmt (fmt) "kasan: " fmt + +#ifdef CONFIG_X86_5LEVEL +/* Too early to use cpu_feature_enabled() */ +#define pgtable_l5_enabled __pgtable_l5_enabled +#endif +#include <linux/bootmem.h> #include <linux/kasan.h> #include <linux/kdebug.h>
Signature · 7 lines
2.15.1 -- 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/3] x86/xen: Allow XEN_PV and XEN_PVH to be enabled with X86_5LEVEL
In reply to Kirill A. Shutemovarch/x86/kernel/head_64.S line 42
Quoted 6 lines · Kirill A. Shutemov
arch/x86/kernel/head_64.S +1
-0@@ -38,12 +38,12 @@ * */ +#define l4_index(x) (((x) >> 39) & 511) #define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
Shouldn't that be
+#define p4d_index(x) (((x) >> P4D_SHIFT) & (PTRS_PER_P4D-1))
--
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/3] x86/xen: Allow XEN_PV and XEN_PVH to be enabled with X86_5LEVEL
Quoted 10 lines · Matthew Wilcox
arch/x86/kernel/head_64.S line 42
Quoted 6 lines · Kirill A. Shutemov
arch/x86/kernel/head_64.S +1
-0@@ -38,12 +38,12 @@ * */ +#define l4_index(x) (((x) >> 39) & 511) #define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1))Shouldn't that be
+#definep4d_index(x)(((x) >>P4D_SHIFT) & (PTRS_PER_P4D-1))
With CONFIG_X86_5LEVEL=y, PTRS_PER_P4D is a varaible, so it won't compile. With CONFIG_X86_5LEVEL=n, PTRS_PER_P4D is 1, so it's broken.
And I didn't want to mixin p4d here: it's actually top-level page table in 4-level paging mode.
I guess we can do something like:
#define l4_index(x) (((x) >> P4D_SIFT) & (PTRS_PER_PGD-1))
But to me it's more confusing than couple numbers.
Signature · 1 line
Kirill A. Shutemov
tip-bot for Kirill A. Shutemov
merge bot[tip:x86/mm] x86/xen: Allow XEN_PV and XEN_PVH to be enabled with X86_5LEVEL
In reply to Kirill A. Shutemovx86/xen: Allow XEN_PV and XEN_PVH to be enabled with X86_5LEVEL
Commit-ID: b9952ec78778aa7ae5b8df672668aece6fc93d2a Gitweb: https://git.kernel.org/tip/b9952ec78778aa7ae5b8df672668aece6fc93d2a Author: Kirill A. Shutemov <[email protected]> AuthorDate: Fri, 16 Feb 2018 14:49:46 +0300 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 21 Feb 2018 10:19:18 +0100
x86/xen: Allow XEN_PV and XEN_PVH to be enabled with X86_5LEVEL
With boot-time switching between paging modes, XEN_PV and XEN_PVH can be boot into 4-level paging mode.
--- arch/x86/kernel/head_64.S | 12 ++++++------ arch/x86/xen/Kconfig | 5 ----- arch/x86/xen/mmu_pv.c | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-)
arch/x86/kernel/head_64.S +6 -6
@@ -38,12 +38,12 @@ * */ +#define l4_index(x) (((x) >> 39) & 511) #define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1)) -#if defined(CONFIG_XEN_PV) || defined(CONFIG_XEN_PVH) -PGD_PAGE_OFFSET = pgd_index(__PAGE_OFFSET_BASE_L4) -PGD_START_KERNEL = pgd_index(__START_KERNEL_map) -#endif +L4_PAGE_OFFSET = l4_index(__PAGE_OFFSET_BASE_L4) +L4_START_KERNEL = l4_index(__START_KERNEL_map) + L3_START_KERNEL = pud_index(__START_KERNEL_map) .text
@@ -386,9 +386,9 @@ NEXT_PAGE(early_dynamic_pgts) #if defined(CONFIG_XEN_PV) || defined(CONFIG_XEN_PVH) NEXT_PGD_PAGE(init_top_pgt) .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC - .org init_top_pgt + PGD_PAGE_OFFSET*8, 0 + .org init_top_pgt + L4_PAGE_OFFSET*8, 0 .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC - .org init_top_pgt + PGD_START_KERNEL*8, 0 + .org init_top_pgt + L4_START_KERNEL*8, 0 /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */ .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC .fill PTI_USER_PGD_FILL,8,0
arch/x86/xen/Kconfig +0 -5
@@ -18,9 +18,6 @@ config XEN_PV bool "Xen PV guest support" default y depends on XEN - # XEN_PV is not ready to work with 5-level paging. - # Changes to hypervisor are also required. - depends on !X86_5LEVEL select XEN_HAVE_PVMMU select XEN_HAVE_VPMU help
@@ -79,6 +76,4 @@ config XEN_DEBUG_FS config XEN_PVH bool "Support for running as a PVH guest" depends on XEN && XEN_PVHVM && ACPI - # Pre-built page tables are not ready to handle 5-level paging. - depends on !X86_5LEVEL def_bool n
arch/x86/xen/mmu_pv.c +21 -0
@@ -538,6 +538,22 @@ static void xen_set_p4d(p4d_t *ptr, p4d_t val)xen_mc_issue (PARAVIRT_LAZY_MMU ); } + +#if CONFIG_PGTABLE_LEVELS >=5 +__visible p4dval_t xen_p4d_val(p4d_t p4d) +{ +return pte_mfn_to_pfn (p4d.p4d); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_p4d_val); + +__visible p4d_t xen_make_p4d(p4dval_t p4d) +{ + p4d =pte_pfn_to_mfn (p4d); + +return native_make_p4d (p4d); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_make_p4d); +#endif /* CONFIG_PGTABLE_LEVELS >= 5 */ #endif /* CONFIG_X86_64 */ static int xen_pmd_walk(struct mm_struct *mm, pmd_t *pmd,
@@ -2411,6 +2427,11 @@ static const struct pv_mmu_ops xen_mmu_ops __initconst = { .alloc_pud = xen_alloc_pmd_init, .release_pud = xen_release_pmd_init, + +#if CONFIG_PGTABLE_LEVELS >=5 + .p4d_val = PV_CALLEE_SAVE(xen_p4d_val), + .make_p4d = PV_CALLEE_SAVE(xen_make_p4d), +#endif #endif /* CONFIG_X86_64 */ .activate_mm = xen_activate_mm,
tip-bot for Kirill A. Shutemov
merge bot[tip:x86/mm] x86/mm: Redefine some of page table helpers as macros
In reply to Kirill A. Shutemovx86/mm: Redefine some of page table helpers as macros
Commit-ID: 92e1c5b3f7bf5407cfdbf13613e7101831216dc5 Gitweb: https://git.kernel.org/tip/92e1c5b3f7bf5407cfdbf13613e7101831216dc5 Author: Kirill A. Shutemov <[email protected]> AuthorDate: Fri, 16 Feb 2018 14:49:47 +0300 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 21 Feb 2018 10:19:18 +0100
x86/mm: Redefine some of page table helpers as macros
This is preparation for the next patch, which would change pgtable_l5_enabled to be cpu_feature_enabled(X86_FEATURE_LA57).
The change makes few helpers in paravirt.h dependent on cpu_feature_enabled() definition from cpufeature.h. And cpufeature.h is dependent on paravirt.h.
Let's re-define some of helpers as macros to break this dependency loop.
--- arch/x86/include/asm/paravirt.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
arch/x86/include/asm/paravirt.h +13 -10
@@ -567,19 +567,22 @@ static inline p4dval_t p4d_val(p4d_t p4d) return PVOP_CALLEE1(p4dval_t, pv_mmu_ops.p4d_val, p4d.p4d); } -static inline void set_pgd (pgd_t *pgdp,pgd_t pgd) +static inline void __set_pgd (pgd_t *pgdp,pgd_t pgd) { -if (pgtable_l5_enabled) -PVOP_VCALL2 (pv_mmu_ops.set_pgd, pgdp,native_pgd_val (pgd)); -else -set_p4d ((p4d_t *)(pgdp), (p4d_t ) { pgd.pgd }); +PVOP_VCALL2 (pv_mmu_ops.set_pgd, pgdp,native_pgd_val (pgd)); } -static inline void pgd_clear (pgd_t *pgdp) -{ -if (pgtable_l5_enabled) -set_pgd (pgdp,__pgd (0 )); -} +#define set_pgd (pgdp, pgdval) do { \ + if (pgtable_l5_enabled) \ + __set_pgd(pgdp, pgdval); \ + else \ + set_p4d((p4d_t *)(pgdp), (p4d_t) { (pgdval).pgd }); \ +} while (0) + +#define pgd_clear (pgdp) do { \ + if (pgtable_l5_enabled) \ + set_pgd(pgdp, __pgd(0)); \ +} while (0)#endif /* CONFIG_PGTABLE_LEVELS == 5 */
tip-bot for Kirill A. Shutemov
merge bot[tip:x86/mm] x86/mm: Optimize boot-time paging mode switching cost
In reply to Kirill A. Shutemovx86/mm: Optimize boot-time paging mode switching cost
Commit-ID: 39b9552281abfcdfc54162897018890dafe7ffef Gitweb: https://git.kernel.org/tip/39b9552281abfcdfc54162897018890dafe7ffef Author: Kirill A. Shutemov <[email protected]> AuthorDate: Fri, 16 Feb 2018 14:49:48 +0300 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 21 Feb 2018 10:19:18 +0100
x86/mm: Optimize boot-time paging mode switching cost
By this point we have functioning boot-time switching between 4- and 5-level paging mode. But naive approach comes with cost.
Numbers below are for kernel build, allmodconfig, 5 times.
CONFIG_X86_5LEVEL=n:
Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):
17308719.892691 task-clock:u (msec) # 26.772 CPUs utilized ( +- 0.11% )
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
331,993,164 page-faults:u # 0.019 M/sec ( +- 0.01% )
43,614,978,867,455 cycles:u # 2.520 GHz ( +- 0.01% )
39,371,534,575,126 stalled-cycles-frontend:u # 90.27% frontend cycles idle ( +- 0.09% )
28,363,350,152,428 instructions:u # 0.65 insn per cycle
# 1.39 stalled cycles per insn ( +- 0.00% )
6,316,784,066,413 branches:u # 364.948 M/sec ( +- 0.00% )
250,808,144,781 branch-misses:u # 3.97% of all branches ( +- 0.01% )646.531974142 seconds time elapsed ( +- 1.15% )
CONFIG_X86_5LEVEL=y:
Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):
17411536.780625 task-clock:u (msec) # 26.426 CPUs utilized ( +- 0.10% )
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
331,868,663 page-faults:u # 0.019 M/sec ( +- 0.01% )
43,865,909,056,301 cycles:u # 2.519 GHz ( +- 0.01% )
39,740,130,365,581 stalled-cycles-frontend:u # 90.59% frontend cycles idle ( +- 0.05% )
28,363,358,997,959 instructions:u # 0.65 insn per cycle
# 1.40 stalled cycles per insn ( +- 0.00% )
6,316,784,937,460 branches:u # 362.793 M/sec ( +- 0.00% )
251,531,919,485 branch-misses:u # 3.98% of all branches ( +- 0.00% )658.886307752 seconds time elapsed ( +- 0.92% )
The patch tries to fix the performance regression by using cpu_feature_enabled(X86_FEATURE_LA57) instead of pgtable_l5_enabled in all hot code paths. These will statically patch the target code for additional performance.
CONFIG_X86_5LEVEL=y + the patch:
Performance counter stats for 'sh -c make -j100 -B -k >/dev/null' (5 runs):
17381990.268506 task-clock:u (msec) # 26.907 CPUs utilized ( +- 0.19% )
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
331,862,625 page-faults:u # 0.019 M/sec ( +- 0.01% )
43,697,726,320,051 cycles:u # 2.514 GHz ( +- 0.03% )
39,480,408,690,401 stalled-cycles-frontend:u # 90.35% frontend cycles idle ( +- 0.05% )
28,363,394,221,388 instructions:u # 0.65 insn per cycle
# 1.39 stalled cycles per insn ( +- 0.00% )
6,316,794,985,573 branches:u # 363.410 M/sec ( +- 0.00% )
251,013,232,547 branch-misses:u # 3.97% of all branches ( +- 0.01% )645.991174661 seconds time elapsed ( +- 1.19% )
Unfortunately, this approach doesn't help with text size:
vmlinux.before .text size: 8190319
vmlinux.after .text size: 8200623
The .text section is increased by about 4k. Not sure if we can do anything about this.
--- arch/x86/boot/compressed/misc.h | 5 +++++ arch/x86/entry/entry_64.S | 11 ++--------- arch/x86/include/asm/pgtable_64_types.h | 5 ++++- arch/x86/kernel/head64.c | 9 +++++++-- arch/x86/kernel/head_64.S | 2 +- arch/x86/mm/kasan_init_64.c | 6 ++++++ 6 files changed, 25 insertions(+), 13 deletions(-)
arch/x86/boot/compressed/misc.h +5 -0
@@ -12,6 +12,11 @@#undef CONFIG_PARAVIRT_SPINLOCKS#undef CONFIG_KASAN +#ifdef CONFIG_X86_5LEVEL +/* cpu_feature_enabled() cannot be used that early */ +#define pgtable_l5_enabled __pgtable_l5_enabled +#endif +#include <linux/linkage.h> #include <linux/screen_info.h> #include <linux/elf.h>
arch/x86/entry/entry_64.S +2 -9
@@ -275,15 +275,8 @@ GLOBAL(entry_SYSCALL_64_after_hwframe) * depending on paging mode) in the address. */ #ifdef CONFIG_X86_5LEVEL - testl $1, pgtable_l5_enabled(%rip) - jz 1f - shl $(64 - 57), %rcx - sar $(64 - 57), %rcx - jmp 2f -1: - shl $(64 - 48), %rcx - sar $(64 - 48), %rcx -2: + ALTERNATIVE "shl $(64 - 48), %rcx; sar $(64 - 48), %rcx", \ + "shl $(64 - 57), %rcx; sar $(64 - 57), %rcx", X86_FEATURE_LA57 #else shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
arch/x86/include/asm/pgtable_64_types.h +4 -1
@@ -21,7 +21,10 @@ typedef unsigned long pgprotval_t;typedef struct {pteval_t pte; }pte_t ;#ifdef CONFIG_X86_5LEVEL -extern unsigned int pgtable_l5_enabled; +extern unsigned int __pgtable_l5_enabled; +#ifndef pgtable_l5_enabled +#define pgtable_l5_enabled cpu_feature_enabled(X86_FEATURE_LA57) +#endif #else #define pgtable_l5_enabled 0#endif
arch/x86/kernel/head64.c +7 -2
@@ -32,6 +32,11 @@#include <asm/microcode.h> #include <asm/kasan.h> +#ifdef CONFIG_X86_5LEVEL +#undef pgtable_l5_enabled +#define pgtable_l5_enabled __pgtable_l5_enabled +#endif +/* * Manage page tables very early on. */
@@ -40,8 +45,8 @@ static unsigned int __initdata next_early_pgt;pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);#ifdef CONFIG_X86_5LEVEL -unsigned int pgtable_l5_enabled__ro_after_init ; -EXPORT_SYMBOL (pgtable_l5_enabled); +unsigned int __pgtable_l5_enabled__ro_after_init ; +EXPORT_SYMBOL (__pgtable_l5_enabled);unsigned int pgdir_shift__ro_after_init =39 ;EXPORT_SYMBOL (pgdir_shift);unsigned int ptrs_per_p4d__ro_after_init =1 ;
arch/x86/kernel/head_64.S +1 -1
@@ -124,7 +124,7 @@ ENTRY(secondary_startup_64) /* Enable PAE mode, PGE and LA57 */ movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx #ifdef CONFIG_X86_5LEVEL - testl $1, pgtable_l5_enabled(%rip) + testl $1, __pgtable_l5_enabled(%rip) jz 1f orl $X86_CR4_LA57, %ecx 1:
arch/x86/mm/kasan_init_64.c +6 -0
@@ -1,6 +1,12 @@// SPDX-License-Identifier: GPL-2.0 #define DISABLE_BRANCH_PROFILING #define pr_fmt (fmt) "kasan: " fmt + +#ifdef CONFIG_X86_5LEVEL +/* Too early to use cpu_feature_enabled() */ +#define pgtable_l5_enabled __pgtable_l5_enabled +#endif +#include <linux/bootmem.h> #include <linux/kasan.h> #include <linux/kdebug.h>