x86/kaiser: Boot time disabling and debug support
Subsystems (5)
PATCH 2/4 x86/dump_pagetables: Check KAISER shadow page table for WX pages
In reply to Thomas Gleixnerptdump_walk_pgd_level_checkwx() checks the kernel page table for WX pages, but does not check the KAISER shadow page table.
Restructure the code so that dmesg output is selected by an explicit argument and not implicit via checking the pgd argument for !NULL. Add the check for the shadow page table.
--- arch/x86/include/asm/pgtable.h | 1 + arch/x86/mm/debug_pagetables.c | 2 +- arch/x86/mm/dump_pagetables.c | 27 ++++++++++++++++++++++----- 3 files changed, 24 insertions(+), 6 deletions(-)
arch/x86/include/asm/pgtable.h +1 -0
@@ -28,6 +28,7 @@ extern pgd_t early_top_pgt[PTRS_PER_PGD]int __init __early_make_pgtable (unsigned long address,pmdval_t pmd);void ptdump_walk_pgd_level (struct seq_file *m,pgd_t *pgd); +void ptdump_walk_pgd_level_debugfs (struct seq_file *m,pgd_t *pgd);void ptdump_walk_pgd_level_checkwx (void );#ifdef CONFIG_DEBUG_WX
arch/x86/mm/debug_pagetables.c +1 -1
@@ -5,7 +5,7 @@static int ptdump_show (struct seq_file *m,void *v) { -ptdump_walk_pgd_level (m,NULL ); +ptdump_walk_pgd_level_debugfs (m,NULL );return 0 ; }
arch/x86/mm/dump_pagetables.c +22 -5
@@ -447,7 +447,7 @@ static inline bool is_hypervisor_range(i }static void ptdump_walk_pgd_level_core (struct seq_file *m,pgd_t *pgd, -bool checkwx) +bool checkwx,bool dmesg) {#ifdef CONFIG_X86_64 pgd_t *start = (pgd_t *) &init_top_pgt;
@@ -460,7 +460,7 @@ static void ptdump_walk_pgd_level_core(sif (pgd) { start = pgd; - st.to_dmesg = true; + st.to_dmesg = dmesg; } st.check_wx = checkwx;
@@ -498,13 +498,30 @@ static void ptdump_walk_pgd_level_core(svoid ptdump_walk_pgd_level (struct seq_file *m,pgd_t *pgd) { -ptdump_walk_pgd_level_core (m, pgd, false); +ptdump_walk_pgd_level_core (m, pgd, false, true); +} + +void ptdump_walk_pgd_level_debugfs (struct seq_file *m,pgd_t *pgd) +{ +ptdump_walk_pgd_level_core (m, pgd, false, false); +} +EXPORT_SYMBOL_GPL (ptdump_walk_pgd_level_debugfs); + +void ptdump_walk_shadow_pgd_level_checkwx (void ) +{ +#ifdef CONFIG_KAISER +pgd_t *pgd = (pgd_t *) &init_top_pgt; + +pr_info ("x86/mm: Checking shadow page tables\n" ); + pgd +=PTRS_PER_PGD ; +ptdump_walk_pgd_level_core (NULL , pgd, true, false); +#endif } -EXPORT_SYMBOL_GPL (ptdump_walk_pgd_level);void ptdump_walk_pgd_level_checkwx (void ) { -ptdump_walk_pgd_level_core (NULL ,NULL , true); +ptdump_walk_pgd_level_core (NULL ,NULL , true, false); +ptdump_walk_shadow_pgd_level_checkwx (); }static int __init pt_dump_init (void )
PATCH 4/4 x86/kaiser: Add boottime disable switch
In reply to Thomas GleixnerKAISER comes with overhead. The most expensive part is the CR3 switching in the entry code.
Add a command line parameter which allows to disable KAISER at boot time.
Most code pathes simply check a variable, but the entry code uses a static branch. The other code pathes cannot use a static branch because they are used before jump label patching is possible. Not an issue as the code pathes are not so performance sensitive as the entry/exit code.
This makes KAISER depend on JUMP_LABEL and on a GCC which supports it, but that's a resonable requirement.
This does not yet take care of the 8k PGD allocations, but that can be done on top.
--- arch/x86/entry/calling.h | 7 +++++++ arch/x86/include/asm/kaiser.h | 8 ++++++++ arch/x86/include/asm/pgtable_64.h | 6 ++++++ arch/x86/mm/init.c | 7 ++++--- arch/x86/mm/kaiser.c | 30 ++++++++++++++++++++++++++++++ security/Kconfig | 2 +- 6 files changed, 56 insertions(+), 4 deletions(-)
arch/x86/entry/calling.h +7 -0
@@ -210,18 +210,23 @@ For 32-bit we have the following convent .endm .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req + STATIC_JUMP_IF_FALSE .Lend_\@, kaiser_enabled_key, def=1 mov %cr3, \scratch_reg ADJUST_KERNEL_CR3 \scratch_reg mov \scratch_reg, %cr3 +.Lend_\@: .endm .macro SWITCH_TO_USER_CR3 scratch_reg:req + STATIC_JUMP_IF_FALSE .Lend_\@, kaiser_enabled_key, def=1 mov %cr3, \scratch_reg ADJUST_USER_CR3 \scratch_reg mov \scratch_reg, %cr3 +.Lend_\@: .endm .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req + STATIC_JUMP_IF_FALSE .Ldone_\@, kaiser_enabled_key, def=1 movq %cr3, %r\scratch_reg movq %r\scratch_reg, \save_reg/*
@@ -244,11 +249,13 @@ For 32-bit we have the following convent .endm .macro RESTORE_CR3 save_reg:req + STATIC_JUMP_IF_FALSE .Lend_\@, kaiser_enabled_key, def=1/* * The CR3 write could be avoided when not changing its value, * but would require a CR3 read *and* a scratch register. */ movq \save_reg, %cr3 +.Lend_\@: .endm #else /* CONFIG_KAISER=n: */
arch/x86/include/asm/kaiser.h +8 -0
@@ -56,6 +56,14 @@ extern void kaiser_remove_mapping(unsign*/ extern void kaiser_init (void ); +/* True if kaiser is enabled at boot time */ +extern struct static_key_true kaiser_enabled_key; +extern bool kaiser_enabled; +extern void kaiser_check_cmdline (void ); + +#else /* CONFIG_KAISER */ +# define kaiser_enabled (false) +static inline void kaiser_check_cmdline (void ) { }#endif #endif /* __ASSEMBLY__ */
arch/x86/include/asm/pgtable_64.h +6 -0
@@ -175,6 +175,9 @@ static inline p4d_t *shadow_to_kernel_p4 {return ptr_clear_bit (p4dp,KAISER_PGTABLE_SWITCH_BIT ); } + +extern bool kaiser_enabled; +#endif /* CONFIG_KAISER */ /*
@@ -208,6 +211,9 @@ static inline bool pgd_userspace_access(static inline pgd_t kaiser_set_shadow_pgd (pgd_t *pgdp,pgd_t pgd) {#ifdef CONFIG_KAISER +if (!kaiser_enabled) +return pgd; +if (pgd_userspace_access (pgd)) {if (pgdp_maps_userspace (pgdp)) {/*
arch/x86/mm/init.c +4 -3
@@ -20,6 +20,7 @@#include <asm/kaslr.h> #include <asm/hypervisor.h> #include <asm/cpufeature.h> +#include <asm/kaiser.h> /* * We need to define the tracepoints somewhere, and tlb.c
@@ -163,9 +164,8 @@ static int page_size_mask;static void enable_global_pages (void ) { -#ifndef CONFIG_KAISER - __supported_pte_mask |= _PAGE_GLOBAL; -#endif +if (!kaiser_enabled) + __supported_pte_mask |= _PAGE_GLOBAL; }static void __init probe_page_size_mask (void )
@@ -656,6 +656,7 @@ void __init init_mem_mapping(void) {unsigned long end; +kaiser_check_cmdline ();probe_page_size_mask ();setup_pcid ();
arch/x86/mm/kaiser.c +30 -0
@@ -34,12 +34,23 @@#include <linux/mm.h> #include <linux/uaccess.h> +#include <asm/cmdline.h> #include <asm/kaiser.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> #include <asm/tlbflush.h> #include <asm/desc.h> +/* Global flag for boot time kaiser enable/disable */ +bool kaiser_enabled__ro_after_init = true; +DEFINE_STATIC_KEY_TRUE (kaiser_enabled_key); + +void __init kaiser_check_cmdline (void ) +{ +if (cmdline_find_option_bool (boot_command_line,"nokaiser" )) + kaiser_enabled = false; +} +#define KAISER_WALK_ATOMIC 0x1/*
@@ -250,6 +261,9 @@ int kaiser_add_user_map(const void *__stunsigned long end_addr =PAGE_ALIGN (start_addr + size);unsigned long target_address; +if (!kaiser_enabled) +return 0 ; +for (; address < end_addr; address +=PAGE_SIZE ) { target_address =get_pa_from_kernel_map (address);if (target_address ==-1 )
@@ -392,6 +406,9 @@ void __init kaiser_init(void) {int cpu; +if (!kaiser_enabled) +return ; +kaiser_init_all_pgds ();for_each_possible_cpu (cpu) {
@@ -426,6 +443,16 @@ void __init kaiser_init(void)kaiser_add_mapping_cpu_entry (0 ); } +static int __init kaiser_boottime_control (void ) +{ +if (!kaiser_enabled) { +static_branch_disable (&kaiser_enabled_key); +pr_info ("kaiser: Disabled on command line\n" ); + } +return 0 ; +} +subsys_initcall (kaiser_boottime_control); +int kaiser_add_mapping (unsigned long addr,unsigned long size,unsigned long flags) {
@@ -436,6 +463,9 @@ void kaiser_remove_mapping(unsigned long {unsigned long addr; +if (!kaiser_enabled) +return ; +/* The shadow page tables always use small pages: */ for (addr = start; addr < start + size; addr +=PAGE_SIZE ) {/*
security/Kconfig +1 -1
@@ -56,7 +56,7 @@ config SECURITY_NETWORK config KAISER bool "Remove the kernel mapping in user mode" - depends on X86_64 && SMP && !PARAVIRT + depends on X86_64 && SMP && !PARAVIRT && JUMP_LABEL help This feature reduces the number of hardware side channels by ensuring that the majority of kernel addresses are not mapped
PATCH 1/4 x86/kaiser: Simplify disabling of global pages
In reply to Thomas GleixnerThe current way of disabling global pages at compile time prevents boot time disabling of kaiser and creates unnecessary indirections.
Global pages can be supressed by __supported_pte_mask as well. The shadow mappings set PAGE_GLOBAL for the minimal kernel mappings which are required for entry/exit. These mappings are setup manually so the filtering does not take place.
--- arch/x86/include/asm/pgtable_types.h | 16 +--------------- arch/x86/mm/init.c | 13 ++++++++++--- arch/x86/mm/pageattr.c | 16 ++++++++-------- 3 files changed, 19 insertions(+), 26 deletions(-)
arch/x86/include/asm/pgtable_types.h +1 -15
@@ -191,23 +191,9 @@ enum page_cache_mode {#define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | \ _PAGE_ACCESSED) -/* -* Disable global pages for anything using the default -* __PAGE_KERNEL* macros. -* -* PGE will still be enabled and _PAGE_GLOBAL may still be used carefully -* for a few selected kernel mappings which must be visible to userspace, -* when KAISER is enabled, like the entry/exit code and data. -*/ -#ifdef CONFIG_KAISER -#define __PAGE_KERNEL_GLOBAL 0 -#else -#define __PAGE_KERNEL_GLOBAL _PAGE_GLOBAL -#endif -#define __PAGE_KERNEL_EXEC \ (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED | \ - __PAGE_KERNEL_GLOBAL) + _PAGE_GLOBAL)#define __PAGE_KERNEL (__PAGE_KERNEL_EXEC | _PAGE_NX)#define __PAGE_KERNEL_RO (__PAGE_KERNEL & ~_PAGE_RW)
arch/x86/mm/init.c +10 -3
@@ -161,6 +161,13 @@ struct map_range {static int page_size_mask; +static void enable_global_pages (void ) +{ +#ifndef CONFIG_KAISER + __supported_pte_mask |= _PAGE_GLOBAL; +#endif +} +static void __init probe_page_size_mask (void ) {/*
@@ -179,11 +186,11 @@ static void __init probe_page_size_mask(cr4_set_bits_and_update_boot (X86_CR4_PSE );/* Enable PGE if available */ + __supported_pte_mask &= ~_PAGE_GLOBAL;if (boot_cpu_has (X86_FEATURE_PGE )) {cr4_set_bits_and_update_boot (X86_CR4_PGE ); - __supported_pte_mask |= _PAGE_GLOBAL; - }else - __supported_pte_mask &= ~_PAGE_GLOBAL; +enable_global_pages (); + }/* Enable 1 GB linear kernel mappings if available: */ if (direct_gbpages &&boot_cpu_has (X86_FEATURE_GBPAGES )) {
arch/x86/mm/pageattr.c +8 -8
@@ -585,9 +585,9 @@ try_preserve_large_page(pte_t *kpte, uns* for the ancient hardware that doesn't support it. */ if (pgprot_val (req_prot) & _PAGE_PRESENT) -pgprot_val (req_prot) |= _PAGE_PSE | __PAGE_KERNEL_GLOBAL; +pgprot_val (req_prot) |= _PAGE_PSE | _PAGE_GLOBAL;else -pgprot_val (req_prot) &= ~(_PAGE_PSE | __PAGE_KERNEL_GLOBAL); +pgprot_val (req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL); req_prot =canon_pgprot (req_prot);
@@ -705,9 +705,9 @@ static int* for the ancient hardware that doesn't support it. */ if (pgprot_val (ref_prot) & _PAGE_PRESENT) -pgprot_val (ref_prot) |= __PAGE_KERNEL_GLOBAL; +pgprot_val (ref_prot) |= _PAGE_GLOBAL;else -pgprot_val (ref_prot) &= ~__PAGE_KERNEL_GLOBAL; +pgprot_val (ref_prot) &= ~_PAGE_GLOBAL;/* * Get the target pfn from the original entry:
@@ -938,9 +938,9 @@ static void populate_pte(struct cpa_data* support it. */ if (pgprot_val (pgprot) & _PAGE_PRESENT) -pgprot_val (pgprot) |= __PAGE_KERNEL_GLOBAL; +pgprot_val (pgprot) |= _PAGE_GLOBAL;else -pgprot_val (pgprot) &= ~__PAGE_KERNEL_GLOBAL; +pgprot_val (pgprot) &= ~_PAGE_GLOBAL; pgprot =canon_pgprot (pgprot);
@@ -1242,9 +1242,9 @@ static int __change_page_attr(struct cpa* support it. */ if (pgprot_val (new_prot) & _PAGE_PRESENT) -pgprot_val (new_prot) |= __PAGE_KERNEL_GLOBAL; +pgprot_val (new_prot) |= _PAGE_GLOBAL;else -pgprot_val (new_prot) &= ~__PAGE_KERNEL_GLOBAL; +pgprot_val (new_prot) &= ~_PAGE_GLOBAL;/* * We need to keep the pfn from the existing PTE,
cover x86/kaiser: Boot time disabling and debug support
This patch series applies on top of
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git WIP.x86/mm
It contains the following updates:
- Get rid of the compile time PAGE_GLOBAL disabling
- Add debug support for WX mappings in the KAISER shadow table
- Provide debug files to dump the kernel and the user page table for the current task.
- Add a boot time switch to disable KAISER. This does not yet take care of the 8k PGD allocations, but that can be done on top.
Thanks,
tglx
--- arch/x86/entry/calling.h | 7 +++ arch/x86/include/asm/kaiser.h | 8 +++ arch/x86/include/asm/pgtable.h | 1 arch/x86/include/asm/pgtable_64.h | 6 ++ arch/x86/include/asm/pgtable_types.h | 16 ------ arch/x86/mm/debug_pagetables.c | 81 ++++++++++++++++++++++++++++++++--- arch/x86/mm/dump_pagetables.c | 27 +++++++++-- arch/x86/mm/init.c | 14 ++++-- arch/x86/mm/kaiser.c | 30 ++++++++++++ arch/x86/mm/pageattr.c | 16 +++--- security/Kconfig | 2 11 files changed, 170 insertions(+), 38 deletions(-)
--
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/mm/debug_pagetables: Allow dumping current pagetables
Add two debugfs files which allow to dump the pagetable of the current task.
current_page_tables_knl dumps the regular page table. This is the page table which is normally shared between kernel and user space. If KAISER is enabled this is the kernel space mapping.
If KAISER is enabled the second file, current_page_tables_usr, dumps the user space page table.
These files allow to verify the resulting page tables for KAISER, but even in the non KAISER case its useful to be able to inspect user space page tables of current for debugging purposes.
--- arch/x86/mm/debug_pagetables.c | 79 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 5 deletions(-)
arch/x86/mm/debug_pagetables.c +74 -5
@@ -22,21 +22,90 @@ static const struct file_operations ptdu .release = single_release, }; -static struct dentry *pe; +static int ptdump_show_curknl (struct seq_file *m,void *v) +{ +if (current->mm->pgd) { +down_read (¤t->mm->mmap_sem); +ptdump_walk_pgd_level_debugfs (m, current->mm->pgd); +up_read (¤t->mm->mmap_sem); + } +return 0 ; +} + +static int ptdump_open_curknl (struct inode *inode,struct file *filp) +{ +return single_open (filp, ptdump_show_curknl,NULL ); +} + +static const struct file_operations ptdump_curknl_fops = { + .owner =THIS_MODULE , + .open = ptdump_open_curknl, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +#ifdef CONFIG_KAISER +static int ptdump_show_curusr (struct seq_file *m,void *v) +{ +if (current->mm->pgd) { +down_read (¤t->mm->mmap_sem); +ptdump_walk_pgd_level_debugfs (m, current->mm->pgd +PTRS_PER_PGD ); +up_read (¤t->mm->mmap_sem); + } +return 0 ; +} + +static int ptdump_open_curusr (struct inode *inode,struct file *filp) +{ +return single_open (filp, ptdump_show_curusr,NULL ); +} + +static const struct file_operations ptdump_curusr_fops = { + .owner =THIS_MODULE , + .open = ptdump_open_curusr, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; +#endif + +static struct dentry *pe_knl, *pe_curknl, *pe_curusr; + +static void pt_dump_debug_remove_files (void ) +{ +debugfs_remove_recursive (pe_knl); +debugfs_remove_recursive (pe_curknl); +debugfs_remove_recursive (pe_curusr); +}static int __init pt_dump_debug_init (void ) { - pe =debugfs_create_file ("kernel_page_tables" ,S_IRUSR ,NULL ,NULL , - &ptdump_fops); -if (!pe) + pe_knl =debugfs_create_file ("kernel_page_tables" ,S_IRUSR ,NULL ,NULL , + &ptdump_fops); +if (!pe_knl)return -ENOMEM ; + pe_curknl =debugfs_create_file ("current_page_tables_knl" ,S_IRUSR , +NULL ,NULL , &ptdump_curknl_fops); +if (!pe_curknl) + goto err; + +#ifdef CONFIG_KAISER + pe_curusr =debugfs_create_file ("current_page_tables_usr" ,S_IRUSR , +NULL ,NULL , &ptdump_curusr_fops); +if (!pe_curusr) + goto err; +#endif return 0 ; +err: +pt_dump_debug_remove_files (); +return -ENOMEM ; } static void __exit pt_dump_debug_exit(void) { -debugfs_remove_recursive (pe); +pt_dump_debug_remove_files (); }module_init (pt_dump_debug_init);
--
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>