Process-local memory allocations for hiding KVM secrets
Subsystems (10)
cover Process-local memory allocations for hiding KVM secrets
The Linux kernel has a global address space that is the same for any kernel code. This address space becomes a liability in a world with processor information leak vulnerabilities, such as L1TF. With the right cache load gadget, an attacker-controlled hyperthread pair can leak arbitrary data via L1TF. Disabling hyperthreading is one recommended mitigation, but it comes with a large performance hit for a wide range of workloads.
An alternative mitigation is to not make certain data in the kernel globally visible, but only when the kernel executes in the context of the process where this data belongs to.
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space. Page tables and mappings in that region will be exclusive to one address space, instead of implicitly shared between all kernel address spaces. Any data placed in that region will be out of reach of cache load gadgets that execute in different address spaces. To implement process-local memory, we introduce a new interface kmalloc_proclocal() / kfree_proclocal() that allocates and maps pages exclusively into the current kernel address space. As a first use case, we move architectural state of guest CPUs in KVM out of reach of other kernel address spaces.
The patch set is a prototype for x86-64 that we have developed on top of kernel 4.20.17 (with cherry-picked commit d253ca0c3865 "x86/mm/cpa: Add set_direct_map_*() functions"). I am aware that the integration with KVM will see some changes while rebasing to 5.x. Patches 7 and 8, in particular, help make patch 9 more readable, but will be dropped in rebasing. We have tested the code on both Intel and AMDs, launching VMs in a loop. So far, we have not done in-depth performance evaluation. Impact on starting VMs was within measurement noise.
---
Julian Stecklina (2):
kvm, vmx: move CR2 context switch out of assembly path kvm, vmx: move register clearing out of assembly path
Marius Hillenbrand (8):
x86/mm/kaslr: refactor to use enum indices for regions x86/speculation, mm: add process local virtual memory region x86/mm, mm,kernel: add teardown for process-local memory to mm cleanup
mm: allocate virtual space for process-local memory
mm: allocate/release physical pages for process-local memory kvm/x86: add support for storing vCPU state in process-local memory kvm, vmx: move gprs to process local memory
kvm, x86: move guest FPU state into process local memory
Documentation/x86/x86_64/mm.txt | 11 +- arch/x86/Kconfig | 1 + arch/x86/include/asm/kvm_host.h | 40 ++- arch/x86/include/asm/page_64.h | 4 + arch/x86/include/asm/pgtable_64_types.h | 12 + arch/x86/include/asm/proclocal.h | 11 + arch/x86/kernel/head64.c | 8 + arch/x86/kvm/Kconfig | 10 + arch/x86/kvm/kvm_cache_regs.h | 4 +- arch/x86/kvm/svm.c | 104 +++++-- arch/x86/kvm/vmx.c | 213 ++++++++++----- arch/x86/kvm/x86.c | 31 ++- arch/x86/mm/Makefile | 1 + arch/x86/mm/dump_pagetables.c | 9 + arch/x86/mm/fault.c | 19 ++ arch/x86/mm/kaslr.c | 63 ++++- arch/x86/mm/proclocal.c | 136 +++++++++ include/linux/mm_types.h | 13 + include/linux/proclocal.h | 35 +++ kernel/fork.c | 6 + mm/Makefile | 1 + mm/proclocal.c | 348 ++++++++++++++++++++++++ security/Kconfig | 18 ++ 23 files changed, 978 insertions(+), 120 deletions(-) create mode 100644 arch/x86/include/asm/proclocal.h create mode 100644 arch/x86/mm/proclocal.c create mode 100644 include/linux/proclocal.h create mode 100644 mm/proclocal.c
Signature · 1 line
2.21.0
RFC 1/10 x86/mm/kaslr: refactor to use enum indices for regions
The KASLR randomization code currently refers to specific regions, such as the vmalloc area, by literal indices into an array. When adding new regions, we have to be careful to also change all indices that may potentially change. Avoid that risk by introducing an enum used as indices.
--- arch/x86/mm/kaslr.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
arch/x86/mm/kaslr.c +14 -8
@@ -41,6 +41,12 @@*/ static const unsigned long vaddr_end =CPU_ENTRY_AREA_BASE ; +enum { +PHYSMAP , +VMALLOC , +VMMEMMAP , +}; +/* * Memory regions randomized by KASLR (except modules that use a separate logic * earlier during boot). The list is ordered based on virtual addresses. This
@@ -50,9 +56,9 @@ static __initdata struct kaslr_memory_region {unsigned long *base;unsigned long size_tb; } kaslr_regions[] = { - { &page_offset_base, 0 }, - { &vmalloc_base, 0 }, - { &vmemmap_base, 1 }, + [PHYSMAP] = { &page_offset_base, 0 }, + [VMALLOC] = { &vmalloc_base, 0 }, + [VMMEMMAP] = { &vmemmap_base, 1 }, };/* Get size in bytes used by the memory region */
@@ -94,20 +100,20 @@ void __init kernel_randomize_memory(void)if (!kaslr_memory_enabled ())return ; - kaslr_regions[0 ].size_tb =1 << (__PHYSICAL_MASK_SHIFT -TB_SHIFT ); - kaslr_regions[1 ].size_tb =VMALLOC_SIZE_TB ; + kaslr_regions[PHYSMAP ].size_tb =1 << (__PHYSICAL_MASK_SHIFT -TB_SHIFT ); + kaslr_regions[VMALLOC ].size_tb =VMALLOC_SIZE_TB ;/* * Update Physical memory mapping to available and * add padding if needed (especially for memory hotplug support). */ -BUG_ON (kaslr_regions[0 ].base != &page_offset_base); +BUG_ON (kaslr_regions[PHYSMAP ].base != &page_offset_base); 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 */ -if (memory_tb < kaslr_regions[0 ].size_tb) - kaslr_regions[0 ].size_tb = memory_tb; +if (memory_tb < kaslr_regions[PHYSMAP ].size_tb) + kaslr_regions[PHYSMAP ].size_tb = memory_tb;/* Calculate entropy available between regions */ remain_entropy = vaddr_end - vaddr_start;
Signature · 1 line
2.21.0
RFC 2/10 x86/speculation, mm: add process local virtual memory region
In reply to Marius HillenbrandThe Linux kernel has a global address space that is the same for any kernel code. This address space becomes a liability in a world with processor information leak vulnerabilities, such as L1TF. With the right cache load gadget, an attacker-controlled hyperthread pair can leak arbitrary data via L1TF. Disabling hyperthreading is one recommended mitigation, but it comes with a large performance hit for a wide range of workloads.
An alternative mitigation is to not make certain data in the kernel globally visible, but only when the kernel executes in the context of the process where this data belongs to.
This patch introduces a region for process-local memory into the kernel's virtual address space. It has a length of 64 GiB (to give more than enough space while leaving enough room for KASLR) and will always occupy a pgd entry that is exclusive for process-local mappings (other pgds may point to shared page tables for the kernel space).
--- Documentation/x86/x86_64/mm.txt | 11 +++++-- arch/x86/Kconfig | 1 + arch/x86/include/asm/page_64.h | 4 +++ arch/x86/include/asm/pgtable_64_types.h | 12 ++++++++ arch/x86/kernel/head64.c | 8 +++++ arch/x86/mm/dump_pagetables.c | 9 ++++++ arch/x86/mm/fault.c | 19 ++++++++++++ arch/x86/mm/kaslr.c | 41 +++++++++++++++++++++++++ security/Kconfig | 18 +++++++++++ 9 files changed, 121 insertions(+), 2 deletions(-)
Documentation/x86/x86_64/mm.txt +9 -2
@@ -40,7 +40,10 @@ ____________________________________________________________|___________________ ffffc90000000000 | -55 TB | ffffe8ffffffffff | 32 TB | vmalloc/ioremap space (vmalloc_base) ffffe90000000000 | -23 TB | ffffe9ffffffffff | 1 TB | ... unused hole ffffea0000000000 | -22 TB | ffffeaffffffffff | 1 TB | virtual memory map (vmemmap_base) - ffffeb0000000000 | -21 TB | ffffebffffffffff | 1 TB | ... unused hole + ffffeb0000000000 | -21 TB | ffffeb7fffffffff | 512 GB | ... unused hole + ffffeb8000000000 | -20.5 TB | ffffebffffffffff | 512 GB | process-local kernel memory (layout shared but mappings + | | | | exclusive to processes, needs an exclusive entry in the + | | | | top-level page table) ffffec0000000000 | -20 TB | fffffbffffffffff | 16 TB | KASAN shadow memory __________________|____________|__________________|_________|____________________________________________________________ |
@@ -98,7 +101,11 @@ ____________________________________________________________|___________________ ffa0000000000000 | -24 PB | ffd1ffffffffffff | 12.5 PB | vmalloc/ioremap space (vmalloc_base) ffd2000000000000 | -11.5 PB | ffd3ffffffffffff | 0.5 PB | ... unused hole ffd4000000000000 | -11 PB | ffd5ffffffffffff | 0.5 PB | virtual memory map (vmemmap_base) - ffd6000000000000 | -10.5 PB | ffdeffffffffffff | 2.25 PB | ... unused hole + ffd6000000000000 | -10.5 PB | ffd7ffffffffffff | 0.5 PB | ... unused hole + ffd8000000000000 | -10 PB | ffd8ffffffffffff | 256 TB | process-local kernel memory (layout shared but mappings + | | | | exclusive to processes, needs an exclusive entry in the + | | | | top-level page table) + ffd9000000000000 | -9.75 PB | ffdeffffffffffff | 1.5 PB | ... unused hole ffdf000000000000 | -8.25 PB | fffffdffffffffff | ~8 PB | KASAN shadow memory __________________|____________|__________________|_________|____________________________________________________________ |
arch/x86/Kconfig +1 -0
@@ -32,6 +32,7 @@ config X86_64 select SWIOTLB select X86_DEV_DMA_OPS select ARCH_HAS_SYSCALL_WRAPPER + select ARCH_SUPPORTS_PROCLOCAL # # Arch settings
arch/x86/include/asm/page_64.h +4 -0
@@ -15,6 +15,10 @@ extern unsigned long page_offset_base;extern unsigned long vmalloc_base;extern unsigned long vmemmap_base; +#ifdef CONFIG_PROCLOCAL +extern unsigned long proclocal_base; +#endif +static inline unsigned long __phys_addr_nodebug (unsigned long x) {unsigned long y = x - __START_KERNEL_map;
arch/x86/include/asm/pgtable_64_types.h +12 -0
@@ -141,6 +141,18 @@ extern unsigned int ptrs_per_p4d;#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1) +#ifdef CONFIG_PROCLOCAL +# define __PROCLOCAL_BASE_L4 0xffffeb8000000000UL +# define __PROCLOCAL_BASE_L5 0xffd8000000000000UL +# define PROCLOCAL_SIZE (64UL * 1024 * 1024 * 1024) + +# ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT +# define PROCLOCAL_START proclocal_base +# else /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ +# define PROCLOCAL_START __PROCLOCAL_BASE_L4 +# endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ +#endif /* CONFIG_PROCLOCAL */ +#define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE)/* The module sections ends with the start of the fixmap */ #define MODULES_END _AC(0xfffffffff4000000, UL)
arch/x86/kernel/head64.c +8 -0
@@ -59,6 +59,10 @@ unsigned long vmalloc_base __ro_after_init = __VMALLOC_BASE_L4;EXPORT_SYMBOL (vmalloc_base);unsigned long vmemmap_base__ro_after_init = __VMEMMAP_BASE_L4;EXPORT_SYMBOL (vmemmap_base); +#ifdef CONFIG_PROCLOCAL +unsigned long proclocal_base__ro_after_init = __PROCLOCAL_BASE_L4; +EXPORT_SYMBOL (proclocal_base); +#endif #endif #define __head __section(.head.text)
@@ -94,6 +98,10 @@ static bool __head check_la57_support(unsigned long physaddr) *fixup_long (&page_offset_base, physaddr) = __PAGE_OFFSET_BASE_L5; *fixup_long (&vmalloc_base, physaddr) = __VMALLOC_BASE_L5; *fixup_long (&vmemmap_base, physaddr) = __VMEMMAP_BASE_L5; +#ifdef CONFIG_PROCLOCAL +#warning "Process-local memory with 5-level page tables is compile-tested only." + *fixup_long (&proclocal_base, physaddr) = __PROCLOCAL_BASE_L5; +#endif return true; }
arch/x86/mm/dump_pagetables.c +9 -0
@@ -61,6 +61,9 @@ enum address_markers_idx {LOW_KERNEL_NR ,VMALLOC_START_NR ,VMEMMAP_START_NR , +#ifdef CONFIG_PROCLOCAL +PROCLOCAL_START_NR , +#endif #ifdef CONFIG_KASANKASAN_SHADOW_START_NR , KASAN_SHADOW_END_NR,
@@ -85,6 +88,9 @@ static struct addr_marker address_markers[] = { [LOW_KERNEL_NR] = { 0UL, "Low Kernel Mapping" }, [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" }, [VMEMMAP_START_NR ] = {0UL ,"Vmemmap" }, +#ifdef CONFIG_PROCLOCAL + [PROCLOCAL_START_NR ] = {0UL ,"Process local" }, +#endif #ifdef CONFIG_KASAN/* * These fields get initialized with the (dynamic)
@@ -622,6 +628,9 @@ static int __init pt_dump_init(void) address_markers[KASAN_SHADOW_START_NR ].start_address =KASAN_SHADOW_START ; address_markers[KASAN_SHADOW_END_NR ].start_address =KASAN_SHADOW_END ;#endif +#ifdef CONFIG_PROCLOCAL + address_markers[PROCLOCAL_START_NR ].start_address =PROCLOCAL_START ; +#endif #endif #ifdef CONFIG_X86_32 address_markers[VMALLOC_START_NR ].start_address =VMALLOC_START ;
arch/x86/mm/fault.c +19 -0
@@ -1171,6 +1171,15 @@ static inline bool smap_violation(int error_code, struct pt_regs *regs)return true; } +static int fault_in_process_local (unsigned long address) +{ +#ifdef CONFIG_PROCLOCAL +return address >=PROCLOCAL_START && address < (PROCLOCAL_START +PROCLOCAL_SIZE ); +#else +return false; +#endif +} +/* * Called for all faults where 'address' is part of the kernel address * space. Might get called for faults that originate from *code* that
@@ -1214,6 +1223,16 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,if (spurious_kernel_fault (hw_error_code, address))return ; +/* +* Faults in process-local memory may be caused by process-local +* addresses leaking into other contexts. +* tbd: warn and handle gracefully. +*/ +if (unlikely (fault_in_process_local (address))) { +pr_err ("page fault in PROCLOCAL at %lx" , address); +force_sig_fault (SIGSEGV ,SEGV_MAPERR , (void __user *)address, current); + } +/* kprobes don't want to hook the spurious faults: */ if (kprobes_fault (regs))return ;
arch/x86/mm/kaslr.c +41 -0
71 lines not on this page · raw message
security/Kconfig +18 -0
@@ -35,6 +35,24 @@ config XPFO_DEBUG If in doubt, say "N". +config ARCH_SUPPORTS_PROCLOCAL + bool + default n + +config PROCLOCAL + bool "Support process-local allocations in the kernel" + depends on ARCH_SUPPORTS_PROCLOCAL + select GENERIC_ALLOCATOR + default n + help + This feature allows subsystems in the kernel to allocate memory that + is only visible in the context of a specific process. This hardens the + kernel against information leak vulnerabilities. + + There is a slight performance impact when this option is enabled. + + If you are unsure how to answer this question, answer N. + config SECURITY_DMESG_RESTRICT bool "Restrict unprivileged access to the kernel syslog" default n
Signature · 1 line
2.21.0
RFC 3/10 x86/mm, mm,kernel: add teardown for process-local memory to mm cleanup
In reply to Marius HillenbrandProcess-local memory uses a dedicated pgd entry in kernel space and its own page table structure. Hook mm exit functions to cleanup those dedicated page tables. As a preparation, release any left-over process-local allocations in the address space.
--- arch/x86/include/asm/proclocal.h | 11 +++ arch/x86/mm/Makefile | 1 + arch/x86/mm/proclocal.c | 131 +++++++++++++++++++++++++++++++ include/linux/mm_types.h | 9 +++ include/linux/proclocal.h | 16 ++++ kernel/fork.c | 6 ++ mm/Makefile | 1 + mm/proclocal.c | 35 +++++++++ 8 files changed, 210 insertions(+) create mode 100644 arch/x86/include/asm/proclocal.h create mode 100644 arch/x86/mm/proclocal.c create mode 100644 include/linux/proclocal.h create mode 100644 mm/proclocal.c
arch/x86/include/asm/proclocal.h new file +11 -0
@@ -0,0 +1,11 @@ +/* +* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +*/ +#ifndef _ASM_X86_PROCLOCAL_H +#define _ASM_X86_PROCLOCAL_H + +struct mm_struct ; + +void arch_proclocal_teardown_pages_and_pt (struct mm_struct *mm); + +#endif /* _ASM_X86_PROCLOCAL_H */
arch/x86/mm/Makefile +1 -0
@@ -56,3 +56,4 @@ obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o obj-$(CONFIG_XPFO) += xpfo.o +obj-$(CONFIG_PROCLOCAL) += proclocal.o
arch/x86/mm/proclocal.c new file +131 -0
131 lines not on this page · raw message
include/linux/mm_types.h +9 -0
@@ -158,6 +158,15 @@ struct page {/** @rcu_head: You can use this to free a page by RCU. */ struct rcu_head rcu_head; + +#ifdef CONFIG_PROCLOCAL +struct {/* PROCLOCAL pages */ +struct list_head proclocal_next;/* track pages in one allocation */ +unsigned long _proclocal_pad_1;/* mapping */ +/* head page of an allocation stores its length */ +size_t proclocal_nr_pages; + }; +#endif };union {/* This union is 4 bytes in size. */
include/linux/proclocal.h new file +16 -0
@@ -0,0 +1,16 @@ +/* +* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +*/ +#ifndef _PROCLOCAL_H +#define _PROCLOCAL_H + +#ifdef CONFIG_PROCLOCAL + +struct mm_struct ; + +void proclocal_mm_exit (struct mm_struct *mm); +#else /* !CONFIG_PROCLOCAL */ +static inline void proclocal_mm_exit (struct mm_struct *mm) { } +#endif + +#endif /* _PROCLOCAL_H */
kernel/fork.c +6 -0
@@ -92,6 +92,7 @@#include <linux/livepatch.h> #include <linux/thread_info.h> #include <linux/stackleak.h> +#include <linux/proclocal.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -1051,6 +1052,11 @@ static inline void __mmput(struct mm_struct *mm)exit_mmap (mm);mm_put_huge_zero_page (mm);set_mm_exe_file (mm,NULL ); +/* +* No real users of this address space left, dropping process-local +* mappings. +*/ +proclocal_mm_exit (mm);if (!list_empty (&mm->mmlist)) {spin_lock (&mmlist_lock);list_del (&mm->mmlist);
mm/Makefile +1 -0
@@ -100,3 +100,4 @@ obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_HMM) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o obj-$(CONFIG_XPFO) += xpfo.o +obj-$(CONFIG_PROCLOCAL) += proclocal.o
mm/proclocal.c new file +35 -0
@@ -0,0 +1,35 @@ +/* +* mm/proclocal.c +* +* The code in this file implements process-local mappings in the Linux kernel +* address space. This memory is only usable in the process context. With memory +* not globally visible in the kernel, it cannot easily be prefetched and leaked +* via L1TF. +* +* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +*/ +#include <linux/mm.h> +#include <linux/proclocal.h> +#include <linux/set_memory.h> +#include <linux/slab.h> +#include <linux/spinlock.h> + +#include <asm/proclocal.h> +#include <asm/pgalloc.h> +#include <asm/tlb.h> + +void proclocal_release_pages (struct list_head *pages) +{ +struct page *pos, *n; +list_for_each_entry_safe (pos, n, pages, proclocal_next) { +list_del (&pos->proclocal_next); +__free_page (pos); + } +} + +void proclocal_mm_exit (struct mm_struct *mm) +{ +pr_debug ("proclocal_mm_exit for mm %p pgd %p (current is %p)\n" , mm, mm->pgd, current); + +arch_proclocal_teardown_pages_and_pt (mm); +}
Signature · 1 line
2.21.0
RFC 4/10 mm: allocate virtual space for process-local memory
In reply to Marius HillenbrandImplement first half of kmalloc and kfree for process-local memory, which deals with allocating virtual address ranges in the process-local memory area.
While the process-local mappings will be visible only in a single address space, the address of each allocation is still unique to aid in debugging (e.g., to see page faults instead of silent invalid accesses). For that purpose, use a global allocator for virtual address ranges of process-local mappings. Note that the single centralized lock is good enough for our use case.
--- arch/x86/mm/proclocal.c | 7 +- include/linux/mm_types.h | 4 + include/linux/proclocal.h | 19 +++++ mm/proclocal.c | 150 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 1 deletion(-)
arch/x86/mm/proclocal.c +6 -1
@@ -10,6 +10,8 @@#include <linux/set_memory.h> #include <asm/tlb.h> +extern void handle_proclocal_page (struct mm_struct *mm,struct page *page, +unsigned long addr); static void unmap_leftover_pages_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr, unsigned long end,
@@ -27,6 +29,8 @@ static void unmap_leftover_pages_pte(struct mm_struct *mm, pmd_t *pmd,pte_clear (mm, addr, pte);set_direct_map_default_noflush (page); +/* callback to non-arch allocator */ +handle_proclocal_page (mm, page, addr);/* * scrub page contents. since mm teardown happens from a * different mm, we cannot just use the process-local virtual
@@ -126,6 +130,7 @@ static void arch_proclocal_teardown_pt(struct mm_struct *mm)void arch_proclocal_teardown_pages_and_pt (struct mm_struct *mm) { -unmap_free_leftover_proclocal_pages (mm); +if (mm->proclocal_nr_pages) +unmap_free_leftover_proclocal_pages (mm);arch_proclocal_teardown_pt (mm); }
include/linux/mm_types.h +4 -0
@@ -510,6 +510,10 @@ struct mm_struct {/* HMM needs to track a few things per mm */ struct hmm *hmm;#endif + +#ifdef CONFIG_PROCLOCAL +size_t proclocal_nr_pages; +#endif } __randomize_layout;/*
include/linux/proclocal.h +19 -0
@@ -8,8 +8,27 @@struct mm_struct ; +void *kmalloc_proclocal (size_t size); +void *kzalloc_proclocal (size_t size); +void kfree_proclocal (void *vaddr); +void proclocal_mm_exit (struct mm_struct *mm);#else /* !CONFIG_PROCLOCAL */ +static inline void *kmalloc_proclocal (size_t size) +{ +return kmalloc (size,GFP_KERNEL ); +} + +static inline void *kzalloc_proclocal (size_t size) +{ +return kzalloc (size,GFP_KERNEL ); +} + +static inline void kfree_proclocal (void *vaddr) +{ +kfree (vaddr); +} +static inline void proclocal_mm_exit (struct mm_struct *mm) { }#endif
mm/proclocal.c +150 -0
171 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC 5/10 mm: allocate/release physical pages for process-local memory
In reply to Marius HillenbrandImplement second half of kmalloc and kfree for process-local memory, which allocates physical pages, maps them into the kernel virtual address space of the current process, and removes them from the kernel's shared direct physical mapping. On kfree, the code performs that sequence in reverse, after scrubbing the pages' contents.
Note that both the allocation and free path require TLB flushes, to flush remaining mappings in the direct physical mappings (on allocation) and in the process-local memory (on release). Aim to keep the impact of these flushes minimal by flushing only necessary address ranges.
The allocation only handles the smallest page size (i.e., 4 KiB), no huge pages, because in our use case, the size of individual allocations is in the order of 4 KiB.
--- mm/proclocal.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 165 insertions(+), 2 deletions(-)
mm/proclocal.c +165 -2
@@ -56,6 +56,70 @@ static struct page *proclocal_find_first_page(struct mm_struct *mm, const void *return pfn_to_page (pte_pfn (*ptep)); } +/* +* Lookup PTE for a given virtual address. Allocate page table structures, if +* they are not present yet. +*/ +static pte_t *pte_lookup_alloc_map (struct mm_struct *mm,unsigned long kvaddr) +{ +pgd_t *pgd =pgd_offset (mm, kvaddr); +p4d_t *p4d; +pud_t *pud; +pmd_t *pmd; + + p4d =p4d_alloc (mm, pgd, kvaddr); +if (IS_ERR_OR_NULL (p4d)) +return (pte_t *)p4d; + + pud =pud_alloc (mm, p4d, kvaddr); +if (IS_ERR_OR_NULL (pud)) +return (pte_t *)pud; + + pmd =pmd_alloc (mm, pud, kvaddr); +if (IS_ERR_OR_NULL (pmd)) +return (pte_t *)pmd; + +return pte_alloc_map (mm, pmd, kvaddr); +} + +static int proclocal_map_notlbflush (struct mm_struct *mm,struct page *page,void *kvaddr) +{ +int rc; +pte_t *ptep =pte_lookup_alloc_map (mm, (unsigned long )kvaddr); + +if (IS_ERR_OR_NULL (ptep)) { +pr_err ("failed to pte_lookup_alloc_map, ptep=0x%lx\n" , + (unsigned long )ptep); +return ptep ?PTR_ERR (ptep) : -ENOMEM ; + } + +set_pte (ptep,mk_pte (page, kmap_prot)); + rc =set_direct_map_invalid_noflush (page); +if (rc) +pte_clear (mm, (unsigned long )kvaddr, ptep); +else +pr_debug ("map pfn %lx at %p for mm %p pgd %p\n" ,page_to_pfn (page), kvaddr, mm, mm->pgd); +return rc; +} + +static void proclocal_unmap_page_notlbflush (struct mm_struct *mm,void *vaddr) +{ +pte_t *ptep =pte_lookup_map (mm, (unsigned long )vaddr); +pte_t pte; +struct page *page; + +BUG_ON (IS_ERR_OR_NULL (ptep)); +BUG_ON (!pte_present (*ptep));// already cleared?! + +/* scrub page contents */ +memset (vaddr,0 ,PAGE_SIZE ); + + pte =ptep_get_and_clear (mm, (unsigned long )vaddr, ptep); + page =pfn_to_page (pte_pfn (pte)); + +BUG_ON (set_direct_map_default_noflush (page));/* should never fail for mapped 4K-pages */ +} +void proclocal_release_pages (struct list_head *pages) {struct page *pos, *n;
@@ -65,6 +129,76 @@ void proclocal_release_pages(struct list_head *pages) } } +static void proclocal_release_pages_incl_head (struct list_head *pages) +{ +proclocal_release_pages (pages); +/* the list_head itself is embedded in a struct page we want to release. */ +__free_page (list_entry(pages ,struct page ,proclocal_next )); +} + +struct physmap_tlb_flush { +unsigned long start; +unsigned long end; +}; + +static inline void track_page_to_flush (struct physmap_tlb_flush *flush,struct page *page) +{ +const unsigned long page_start = (unsigned long )page_to_virt (page); +const unsigned long page_end = page_start +PAGE_SIZE ; + +if (page_start < flush->start) + flush->start = page_start; +if (page_end > flush->end) + flush->end = page_end; +} + +static int alloc_and_map_proclocal_pages (struct mm_struct *mm,void *kvaddr,size_t nr_pages) +{ +int rc; +size_t i, j; +struct page *page; +struct list_head *pages_list =NULL ; +struct physmap_tlb_flush flush = {-1 ,0 }; + +for (i =0 ; i < nr_pages; i++) { + page =alloc_page (GFP_KERNEL ); + +if (!page) { + rc = -ENOMEM ; + goto unmap_release; + } + + rc =proclocal_map_notlbflush (mm, page, kvaddr + i *PAGE_SIZE ); +if (rc) { +__free_page (page); + goto unmap_release; + } + +track_page_to_flush (&flush, page); +INIT_LIST_HEAD (&page->proclocal_next); +/* track allocation in first struct page */ +if (!pages_list) { + pages_list = &page->proclocal_next; + page->proclocal_nr_pages = nr_pages; + }else { +list_add_tail (&page->proclocal_next, pages_list); + page->proclocal_nr_pages =0 ; + } + } + +/* flush direct mappings of allocated pages from TLBs. */ +flush_tlb_kernel_range (flush.start, flush.end); +return 0 ; + +unmap_release: +for (j =0 ; j < i; j++) +proclocal_unmap_page_notlbflush (mm, kvaddr + j *PAGE_SIZE ); + +if (pages_list) +proclocal_release_pages_incl_head (pages_list); +return rc; +} +static DEFINE_SPINLOCK (proclocal_lock);static struct gen_pool *allocator;
@@ -106,9 +240,11 @@ static void free_virtual(const void *kvaddr, size_t nr_pages)void *kmalloc_proclocal (size_t size) { +int rc;void *kvaddr =NULL ;size_t nr_pages =round_up (size,PAGE_SIZE ) /PAGE_SIZE ;size_t nr_pages_virtual = nr_pages +1 ;/* + guard page */ +struct mm_struct *mm;BUG_ON (!current);if (!size)
@@ -120,7 +256,18 @@ void *kmalloc_proclocal(size_t size)if (IS_ERR_OR_NULL (kvaddr))return kvaddr; -/* tbd: subsequent patch will allocate and map physical pages */ + mm = current->mm; +down_write (&mm->mmap_sem); + rc =alloc_and_map_proclocal_pages (mm, kvaddr, nr_pages); +if (!rc) + mm->proclocal_nr_pages += nr_pages; +up_write (&mm->mmap_sem); + +if (unlikely (rc)) + kvaddr =ERR_PTR (rc); + +pr_debug ("allocated %zd bytes at %p (current %p mm %p)\n" , size, kvaddr, + current, current ? current->mm :0 );return kvaddr; }
@@ -138,6 +285,7 @@ EXPORT_SYMBOL(kzalloc_proclocal);void kfree_proclocal (void *kvaddr) { +int i;struct page *first_page;int nr_pages;struct mm_struct *mm;
@@ -152,8 +300,10 @@ void kfree_proclocal(void *kvaddr)BUG_ON ((unsigned long )kvaddr >= (PROCLOCAL_START +PROCLOCAL_SIZE ));BUG_ON (!current); +might_sleep (); mm = current->mm;down_write (&mm->mmap_sem); + first_page =proclocal_find_first_page (mm, kvaddr);if (IS_ERR_OR_NULL (first_page)) {pr_err ("double-free?!\n" );
@@ -162,9 +312,22 @@ void kfree_proclocal(void *kvaddr) nr_pages = first_page->proclocal_nr_pages;BUG_ON (!nr_pages); mm->proclocal_nr_pages -= nr_pages; -/* subsequent patch will unmap and release physical pages */ + +for (i =0 ; i < nr_pages; i++) +proclocal_unmap_page_notlbflush (mm, kvaddr + i *PAGE_SIZE ); +up_write (&mm->mmap_sem); +/* +* Flush process-local mappings from TLBs so that we can release the +* pages afterwards. +*/ +flush_tlb_mm_range (mm, (unsigned long )kvaddr, + (unsigned long )kvaddr + nr_pages *PAGE_SIZE , +PAGE_SHIFT , false); + +proclocal_release_pages_incl_head (&first_page->proclocal_next); +free_virtual (kvaddr, nr_pages +1 ); }EXPORT_SYMBOL (kfree_proclocal);
Signature · 1 line
2.21.0
RFC 6/10 kvm/x86: add support for storing vCPU state in process-local memory
In reply to Marius HillenbrandThe hidden KVM state will both contain guest state that is specific to x86-64 as well as state specific to SVM or VMX, respectively. Thus, allocate the hidden state in the code paths specific to SVM and VMX. For the code that is shared between SVM and VMX, introduce a common struct for hidden guest state.
--- arch/x86/include/asm/kvm_host.h | 9 ++++++++ arch/x86/kvm/Kconfig | 10 +++++++++ arch/x86/kvm/svm.c | 37 +++++++++++++++++++++++++++++++- arch/x86/kvm/vmx.c | 38 ++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 5 +++++ 5 files changed, 97 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +9 -0
@@ -534,7 +534,16 @@ struct kvm_vcpu_hv {cpumask_t tlb_flush; }; +#ifdef CONFIG_KVM_PROCLOCAL +struct kvm_vcpu_arch_hidden { +u64 placeholder; +}; +#endif +struct kvm_vcpu_arch { +#ifdef CONFIG_KVM_PROCLOCAL +struct kvm_vcpu_arch_hidden *hidden; +#endif /* * rip and regs accesses must go through * kvm_{register,rip}_{read,write} functions.
arch/x86/kvm/Kconfig +10 -0
@@ -97,6 +97,16 @@ config KVM_MMU_AUDIT This option adds a R/W kVM module parameter 'mmu_audit', which allows auditing of KVM MMU events at runtime. +config KVM_PROCLOCAL + bool "Use process-local allocation for KVM" + depends on KVM && PROCLOCAL + ---help--- + Use process-local memory for storing vCPU state in KVM. This + option removes assets from the kernel's global direct mapping + of physical memory and stores them only in the address space + of the process hosting a VM. + + # OK, it's a little counter-intuitive to do this, but it puts it neatly under # the virtualization menu. source drivers/vhost/Kconfig
arch/x86/kvm/svm.c +36 -1
@@ -41,6 +41,7 @@#include <linux/file.h> #include <linux/pagemap.h> #include <linux/swap.h> +#include <linux/proclocal.h> #include <asm/apic.h> #include <asm/perf_event.h>
@@ -190,9 +191,20 @@ static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;*/ static uint64_t osvw_len =4 , osvw_status; +#ifdef CONFIG_KVM_PROCLOCAL +struct vcpu_svm_hidden { +struct {/* mimic topology in vcpu_svm: */ +struct kvm_vcpu_arch_hidden arch; + } vcpu; +}; +#endif +struct vcpu_svm {struct kvm_vcpu vcpu;struct vmcb *vmcb; +#ifdef CONFIG_KVM_PROCLOCAL +struct vcpu_svm_hidden *hidden; +#endif unsigned long vmcb_pa;struct svm_cpu_data *svm_data;uint64_t asid_generation;
@@ -2129,9 +2141,18 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) goto out; } +#ifdef CONFIG_KVM_PROCLOCAL + svm->hidden =kzalloc_proclocal (sizeof (struct vcpu_svm_hidden )); +if (!svm->hidden) { + err = -ENOMEM ; + goto free_svm; + } + svm->vcpu.arch.hidden = &svm->hidden->vcpu.arch; +#endif + err =kvm_vcpu_init (&svm->vcpu, kvm, id);if (err) - goto free_svm; + goto free_hidden; err = -ENOMEM ; page =alloc_page (GFP_KERNEL );
@@ -2187,7 +2208,11 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)__free_page (page); uninit:kvm_vcpu_uninit (&svm->vcpu); +free_hidden: +#ifdef CONFIG_KVM_PROCLOCAL +kfree_proclocal (svm->hidden); free_svm: +#endif kmem_cache_free (kvm_vcpu_cache, svm); out:return ERR_PTR (err);
@@ -2205,6 +2230,16 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +#ifdef CONFIG_KVM_PROCLOCAL +/* +* note that the hidden vCPU state in a process-local allocation is +* already cleaned up, because a process's mm is torn down before files +* are closed. make any access in the cleanup code very visible. +*/ + svm->hidden = (struct vcpu_svm_hidden *)POISON_POINTER_DELTA ; + svm->vcpu.arch.hidden = (struct kvm_vcpu_arch_hidden *)POISON_POINTER_DELTA ; +#endif +/* * The vmcb page can be recycled, causing a false negative in * svm_vcpu_load(). So, ensure that no logical CPU has this
arch/x86/kvm/vmx.c +37 -1
@@ -37,6 +37,8 @@#include <linux/hrtimer.h> #include <linux/frame.h> #include <linux/nospec.h> +#include <linux/proclocal.h> +#include "kvm_cache_regs.h" #include "x86.h"
@@ -975,8 +977,19 @@ struct vmx_msrs {struct vmx_msr_entry val[NR_AUTOLOAD_MSRS ]; }; +#ifdef CONFIG_KVM_PROCLOCAL +struct vcpu_vmx_hidden { +struct {/* mimic topology in vcpu_svm: */ +struct kvm_vcpu_arch_hidden arch; + } vcpu; +}; +#endif +struct vcpu_vmx {struct kvm_vcpu vcpu; +#ifdef CONFIG_KVM_PROCLOCAL +struct vcpu_vmx_hidden *hidden; +#endif unsigned long host_rsp;u8 fail;u8 msr_bitmap_mode;
@@ -11756,6 +11769,16 @@ static void vmx_free_vcpu(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +#ifdef CONFIG_KVM_PROCLOCAL +/* +* note that the hidden vCPU state in a process-local allocation is +* already cleaned up, because a process's mm is torn down before files +* are closed. make any access in the cleanup code very visible. +*/ + vmx->hidden = (struct vcpu_vmx_hidden *)POISON_POINTER_DELTA ; + vmx->vcpu.arch.hidden = (struct kvm_vcpu_arch_hidden *)POISON_POINTER_DELTA ; +#endif +if (enable_pml)vmx_destroy_pml_buffer (vmx);free_vpid (vmx->vpid);
@@ -11777,11 +11800,20 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)if (!vmx)return ERR_PTR (-ENOMEM ); +#ifdef CONFIG_KVM_PROCLOCAL + vmx->hidden =kzalloc_proclocal (sizeof (struct vcpu_vmx_hidden )); +if (!vmx->hidden) { + err = -ENOMEM ; + goto free_vcpu; + } + vmx->vcpu.arch.hidden = &vmx->hidden->vcpu.arch; +#endif + vmx->vpid =allocate_vpid (); err =kvm_vcpu_init (&vmx->vcpu, kvm, id);if (err) - goto free_vcpu; + goto free_hidden; err = -ENOMEM ;
@@ -11868,7 +11900,11 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)vmx_destroy_pml_buffer (vmx); uninit_vcpu:kvm_vcpu_uninit (&vmx->vcpu); +free_hidden: +#ifdef CONFIG_KVM_PROCLOCAL +kfree_proclocal (vmx->hidden); free_vcpu: +#endif free_vpid (vmx->vpid);kmem_cache_free (kvm_vcpu_cache, vmx);return ERR_PTR (err);
arch/x86/kvm/x86.c +5 -0
@@ -9309,6 +9309,11 @@ void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)free_page ((unsigned long )vcpu->arch.pio_data);if (!lapic_in_kernel (vcpu))static_key_slow_dec (&kvm_no_apic_vcpu); +/* +* note that the hidden vCPU state in a process-local allocation is +* already cleaned up at this point, because a process's mm is torn down +* before files are closed. +*/ }void kvm_arch_sched_in (struct kvm_vcpu *vcpu,int cpu)
Signature · 1 line
2.21.0
RFC 7/10 kvm, vmx: move CR2 context switch out of assembly path
In reply to Marius HillenbrandFrom: Julian Stecklina <[email protected]>
The VM entry/exit path is a giant inline assembly statement. Simplify it by doing CR2 context switching in plain C. Move CR2 restore behind IBRS clearing, so we reduce the amount of code we execute with IBRS on.
Using {read,write}_cr2() means KVM will use pv_mmu_ops instead of open coding native_{read,write}_cr2(). The CR2 code has been done in assembly since KVM's genesis[1], which predates the addition of the paravirt ops[2], i.e. KVM isn't deliberately avoiding the paravirt ops.
[1] Commit 6aa8b732ca01 ("[PATCH] kvm: userspace interface")
[2] Commit d3561b7fa0fb ("[PATCH] paravirt: header and stubs for paravirtualisation")
[rebased; note that this patch mainly improves the readability of subsequent patches; we will drop it when rebasing to 5.x, since major refactoring of KVM makes this patch redundant.]
--- arch/x86/kvm/vmx.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
arch/x86/kvm/vmx.c +5 -10
@@ -11513,6 +11513,9 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) evmcs_rsp =static_branch_unlikely (&enable_evmcs) ? (unsigned long )¤t_evmcs->host_rsp :0 ; +if (read_cr2 () != vcpu->arch.cr2) +write_cr2 (vcpu->arch.cr2); +if (static_branch_unlikely (&vmx_l1d_should_flush))vmx_l1d_flush (vcpu);
@@ -11532,13 +11535,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)"2: \n\t" __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t""1: \n\t" -/* Reload cr2 if changed */ -"mov %c[cr2](%0), %%" _ASM_AX" \n\t" -"mov %%cr2, %%" _ASM_DX" \n\t" -"cmp %%" _ASM_AX", %%" _ASM_DX" \n\t" -"je 3f \n\t" -"mov %%" _ASM_AX", %%cr2 \n\t" -"3: \n\t" /* Check if vmlaunch of vmresume is needed */ "cmpl $0, %c[launched](%0) \n\t" /* Load guest registers. Don't clobber flags. */
@@ -11599,8 +11595,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)"xor %%r14d, %%r14d \n\t" "xor %%r15d, %%r15d \n\t"#endif -"mov %%cr2, %%" _ASM_AX" \n\t" -"mov %%" _ASM_AX", %c[cr2](%0) \n\t" "xor %%eax, %%eax \n\t" "xor %%ebx, %%ebx \n\t"
@@ -11632,7 +11626,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), #endif - [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), [wordsize]"i"(sizeof(ulong)) : "cc", "memory"#ifdef CONFIG_X86_64
@@ -11666,6 +11659,8 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)/* Eliminate branch target predictions from guest mode */ vmexit_fill_RSB (); + vcpu->arch.cr2 =read_cr2 (); +/* All fields are clean at this point */ if (static_branch_unlikely (&enable_evmcs)) current_evmcs->hv_clean_fields |=
Signature · 1 line
2.21.0
RFC 8/10 kvm, vmx: move register clearing out of assembly path
In reply to Marius HillenbrandFrom: Julian Stecklina <[email protected]>
Split the security related register clearing out of the large inline assembly VM entry path. This results in two slightly less complicated inline assembly statements, where it is clearer what each one does.
[rebased to 4.20; note that the purpose of this patch is to make the changes in the next commit more readable. we will drop this patch when rebasing to 5.x, since major refactoring of KVM makes it redundant.]
--- arch/x86/kvm/vmx.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-)
arch/x86/kvm/vmx.c +29 -17
@@ -11582,24 +11582,7 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)"mov %%r13, %c[r13](%0) \n\t" "mov %%r14, %c[r14](%0) \n\t" "mov %%r15, %c[r15](%0) \n\t" -/* -* Clear host registers marked as clobbered to prevent -* speculative use. -*/ -"xor %%r8d, %%r8d \n\t" -"xor %%r9d, %%r9d \n\t" -"xor %%r10d, %%r10d \n\t" -"xor %%r11d, %%r11d \n\t" -"xor %%r12d, %%r12d \n\t" -"xor %%r13d, %%r13d \n\t" -"xor %%r14d, %%r14d \n\t" - "xor %%r15d, %%r15d \n\t"#endif - -"xor %%eax, %%eax \n\t" -"xor %%ebx, %%ebx \n\t" -"xor %%esi, %%esi \n\t" -"xor %%edi, %%edi \n\t" "pop %%" _ASM_BP"; pop %%" _ASM_DX" \n\t" ".pushsection .rodata \n\t" ".global vmx_return \n\t"
@@ -11636,6 +11619,35 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)#endif ); +/* +* Explicitly clear (in addition to marking them as clobbered) all GPRs +* that have not been loaded with host state to prevent speculatively +* using the guest's values. +*/ + asmvolatile ( +"xor %%eax, %%eax \n\t" +"xor %%ebx, %%ebx \n\t" +"xor %%esi, %%esi \n\t" +"xor %%edi, %%edi \n\t" +#ifdef CONFIG_X86_64 +"xor %%r8d, %%r8d \n\t" +"xor %%r9d, %%r9d \n\t" +"xor %%r10d, %%r10d \n\t" +"xor %%r11d, %%r11d \n\t" +"xor %%r12d, %%r12d \n\t" +"xor %%r13d, %%r13d \n\t" +"xor %%r14d, %%r14d \n\t" +"xor %%r15d, %%r15d \n\t" +#endif + :::"cc" +#ifdef CONFIG_X86_64 + ,"rax" ,"rbx" ,"rsi" ,"rdi" + ,"r8" ,"r9" ,"r10" ,"r11" ,"r12" ,"r13" ,"r14" ,"r15" +#else + ,"eax" ,"ebx" ,"esi" ,"edi" +#endif + ); +/* * We do not use IBRS in the kernel. If this vCPU has used the * SPEC_CTRL MSR it may have left it on; save the value and
Signature · 1 line
2.21.0
RFC 9/10 kvm, vmx: move gprs to process local memory
In reply to Marius HillenbrandGeneral-purpose registers (GPRs) contain guest data and must be protected from information leak vulnerabilities in the kernel.
Move GPRs into process local memory and change the VMX and SVM world switch and related code accordingly.
The VMX and SVM world switch are giant inline assembly code blocks. To keep the changes minimal, we pass all required state as a pointer to a single struct in process-local memory, which is hidden from other processes.
Note that this feature is strictly opt-in. When disabled, the world switch code remains unchanged.
--- arch/x86/include/asm/kvm_host.h | 27 +++++++- arch/x86/kvm/kvm_cache_regs.h | 4 +- arch/x86/kvm/svm.c | 67 +++++++++++-------- arch/x86/kvm/vmx.c | 114 +++++++++++++++++++++----------- arch/x86/kvm/x86.c | 2 +- 5 files changed, 143 insertions(+), 71 deletions(-)
arch/x86/include/asm/kvm_host.h +24 -3
@@ -534,21 +534,30 @@ struct kvm_vcpu_hv {cpumask_t tlb_flush; }; +typedef unsigned long kvm_arch_regs_t [NR_VCPU_REGS ];#ifdef CONFIG_KVM_PROCLOCAL +/* +* access to vcpu guest state must go through kvm_vcpu_arch_state(vcpu). +*/ struct kvm_vcpu_arch_hidden { -u64 placeholder; +/* +* rip and regs accesses must go through +* kvm_{register,rip}_{read,write} functions. +*/ +kvm_arch_regs_t regs; };#endif struct kvm_vcpu_arch {#ifdef CONFIG_KVM_PROCLOCAL struct kvm_vcpu_arch_hidden *hidden; -#endif +#else /* * rip and regs accesses must go through * kvm_{register,rip}_{read,write} functions. */ -unsigned long regs[NR_VCPU_REGS ]; +kvm_arch_regs_t regs; +#endif u32 regs_avail;u32 regs_dirty;
@@ -791,6 +800,18 @@ struct kvm_vcpu_arch {bool l1tf_flush_l1d; }; +#ifdef CONFIG_KVM_PROCLOCAL +static inline struct kvm_vcpu_arch_hidden *kvm_vcpu_arch_state (struct kvm_vcpu_arch *arch) +{ +return arch->hidden; +} +#else +static inline struct kvm_vcpu_arch *kvm_vcpu_arch_state (struct kvm_vcpu_arch *vcpu_arch) +{ +return vcpu_arch; +} +#endif +struct kvm_lpage_info {int disallow_lpage; };
arch/x86/kvm/kvm_cache_regs.h +2 -2
@@ -13,14 +13,14 @@ static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu,if (!test_bit (reg, (unsigned long *)&vcpu->arch.regs_avail)) kvm_x86_ops->cache_reg (vcpu, reg); -return vcpu->arch.regs[reg]; +return kvm_vcpu_arch_state (&vcpu->arch)->regs[reg]; }static inline void kvm_register_write (struct kvm_vcpu *vcpu,enum kvm_reg reg,unsigned long val) { - vcpu->arch.regs[reg] = val; +kvm_vcpu_arch_state (&vcpu->arch)->regs[reg] = val;__set_bit (reg, (unsigned long *)&vcpu->arch.regs_dirty);__set_bit (reg, (unsigned long *)&vcpu->arch.regs_avail); }
arch/x86/kvm/svm.c +41 -26
124 lines not on this page · raw message
arch/x86/kvm/vmx.c +75 -39
225 lines not on this page · raw message
arch/x86/kvm/x86.c +1 -1
@@ -9048,7 +9048,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->arch.xcr0 =XFEATURE_MASK_FP ; } -memset (vcpu->arch.regs,0 ,sizeof (vcpu->arch.regs)); +memset (kvm_vcpu_arch_state (&vcpu->arch)->regs,0 ,sizeof (kvm_arch_regs_t)); vcpu->arch.regs_avail = ~0 ; vcpu->arch.regs_dirty = ~0 ;
Signature · 1 line
2.21.0
RFC 10/10 kvm, x86: move guest FPU state into process local memory
In reply to Marius HillenbrandFPU registers contain guest data and must be protected from information leak vulnerabilities in the kernel.
FPU register state for vCPUs are allocated from the globally-visible kernel heap. Change this to use process-local memory instead and thus prevent access (or prefetching) in any other context in the kernel.
--- arch/x86/include/asm/kvm_host.h | 8 ++++++++ arch/x86/kvm/x86.c | 24 ++++++++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +8 -0
@@ -36,6 +36,7 @@#include <asm/asm.h> #include <asm/kvm_page_track.h> #include <asm/hyperv-tlfs.h> +#include <asm/proclocal.h> #define KVM_MAX_VCPUS 288#define KVM_SOFT_MAX_VCPUS 240
@@ -545,6 +546,7 @@ struct kvm_vcpu_arch_hidden {* kvm_{register,rip}_{read,write} functions. */ kvm_arch_regs_t regs; + struct fpu guest_fpu; };#endif
@@ -631,9 +633,15 @@ struct kvm_vcpu_arch {* it is switched out separately at VMENTER and VMEXIT time. The * "guest_fpu" state here contains the guest FPU context, with the * host PRKU bits. +* +* With process-local memory, the guest FPU state will be hidden in +* kvm_vcpu_arch_hidden. Thus, access to this struct must go through +* kvm_vcpu_arch_state(vcpu). */ struct fpu user_fpu; +#ifndef CONFIG_KVM_PROCLOCAL struct fpu guest_fpu; +#endif u64 xcr0;u64 guest_supported_xcr0;
arch/x86/kvm/x86.c +12 -12
@@ -3792,7 +3792,7 @@ static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,static void fill_xsave (u8 *dest,struct kvm_vcpu *vcpu) { -struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave; +struct xregs_state *xsave = &kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.xsave;u64 xstate_bv = xsave->header.xfeatures;u64 valid;
@@ -3834,7 +3834,7 @@ static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)static void load_xsave (struct kvm_vcpu *vcpu,u8 *src) { -struct xregs_state *xsave = &vcpu->arch.guest_fpu.state.xsave; +struct xregs_state *xsave = &kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.xsave;u64 xstate_bv = *(u64 *)(src +XSAVE_HDR_OFFSET );u64 valid;
@@ -3882,7 +3882,7 @@ static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,fill_xsave ((u8 *) guest_xsave->region, vcpu); }else {memcpy (guest_xsave->region, - &vcpu->arch.guest_fpu.state.fxsave, + &kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.fxsave,sizeof (struct fxregs_state )); *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET /sizeof (u32)] =XFEATURE_MASK_FPSSE ;
@@ -3912,7 +3912,7 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,if (xstate_bv & ~XFEATURE_MASK_FPSSE || mxcsr & ~mxcsr_feature_mask)return -EINVAL ; -memcpy (&vcpu->arch.guest_fpu.state.fxsave, +memcpy (&kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.fxsave, guest_xsave->region,sizeof (struct fxregs_state )); }return 0 ;
@@ -8302,7 +8302,7 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)preempt_disable ();copy_fpregs_to_fpstate (&vcpu->arch.user_fpu);/* PKRU is separately restored in kvm_x86_ops->run. */ -__copy_kernel_to_fpregs (&vcpu->arch.guest_fpu.state, +__copy_kernel_to_fpregs (&kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state, ~XFEATURE_MASK_PKRU );preempt_enable ();trace_kvm_fpu (1 );
@@ -8312,7 +8312,7 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)static void kvm_put_guest_fpu (struct kvm_vcpu *vcpu) {preempt_disable (); -copy_fpregs_to_fpstate (&vcpu->arch.guest_fpu); +copy_fpregs_to_fpstate (&kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu);copy_kernel_to_fpregs (&vcpu->arch.user_fpu.state);preempt_enable (); ++vcpu->stat.fpu_reload;
@@ -8807,7 +8807,7 @@ int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)vcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu.state.fxsave; + fxsave = &kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.fxsave;memcpy (fpu->fpr, fxsave->st_space,128 ); fpu->fcw = fxsave->cwd; fpu->fsw = fxsave->swd;
@@ -8827,7 +8827,7 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)vcpu_load (vcpu); - fxsave = &vcpu->arch.guest_fpu.state.fxsave; + fxsave = &kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.fxsave;memcpy (fxsave->st_space, fpu->fpr,128 ); fxsave->cwd = fpu->fcw;
@@ -8883,9 +8883,9 @@ static int sync_regs(struct kvm_vcpu *vcpu)static void fx_init (struct kvm_vcpu *vcpu) { -fpstate_init (&vcpu->arch.guest_fpu.state); +fpstate_init (&kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state);if (boot_cpu_has (X86_FEATURE_XSAVES )) - vcpu->arch.guest_fpu.state.xsave.header.xcomp_bv = +kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.xsave.header.xcomp_bv = host_xcr0 |XSTATE_COMPACTION_ENABLED ;/*
@@ -9009,11 +9009,11 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)*/ if (init_event)kvm_put_guest_fpu (vcpu); - mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu.state.xsave, + mpx_state_buffer =get_xsave_addr (&kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.xsave,XFEATURE_MASK_BNDREGS );if (mpx_state_buffer)memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndreg_state )); - mpx_state_buffer =get_xsave_addr (&vcpu->arch.guest_fpu.state.xsave, + mpx_state_buffer =get_xsave_addr (&kvm_vcpu_arch_state (&vcpu->arch)->guest_fpu.state.xsave,XFEATURE_MASK_BNDCSR );if (mpx_state_buffer)memset (mpx_state_buffer,0 ,sizeof (struct mpx_bndcsr ));
Signature · 1 line
2.21.0
Quoted 12 lines · Marius Hillenbrand
The Linux kernel has a global address space that is the same for any kernel code. This address space becomes a liability in a world with processor information leak vulnerabilities, such as L1TF. With the right cache load gadget, an attacker-controlled hyperthread pair can leak arbitrary data via L1TF. Disabling hyperthreading is one recommended mitigation, but it comes with a large performance hit for a wide range of workloads.
An alternative mitigation is to not make certain data in the kernel globally visible, but only when the kernel executes in the context of the process where this data belongs to.
Quoted 10 lines
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space. Page tables and mappings in that region will be exclusive to one address space, instead of implicitly shared between all kernel address spaces. Any data placed in that region will be out of reach of cache load gadgets that execute in different address spaces. To implement process-local memory, we introduce a new interface
kmalloc_proclocal()/kfree_proclocal()that allocates and maps pages exclusively into the current kernel address space. As a first use case, we move architectural state of guest CPUs in KVM out of reach of other kernel address spaces.
Can you briefly describe what types of attacks this is intended to mitigate? E.g. guest-guest, userspace-guest, etc... I don't want to make comments based on my potentially bad assumptions.
Quoted 4 lines
The patch set is a prototype for x86-64 that we have developed on top of kernel 4.20.17 (with cherry-picked commit
d253ca0c3865"x86/mm/cpa: Add set_direct_map_*() functions"). I am aware that the integration with KVM will see some changes while rebasing to 5.x. Patches 7 and 8, in
Ha, "some" :-)
Quoted 4 lines
particular, help make patch 9 more readable, but will be dropped in rebasing. We have tested the code on both Intel and AMDs, launching VMs in a loop. So far, we have not done in-depth performance evaluation. Impact on starting VMs was within measurement noise.
Quoted 23 lines · Sean Christopherson
Quoted 12 lines · Marius Hillenbrand
The Linux kernel has a global address space that is the same for any kernel code. This address space becomes a liability in a world with processor information leak vulnerabilities, such as L1TF. With the right cache load gadget, an attacker-controlled hyperthread pair can leak arbitrary data via L1TF. Disabling hyperthreading is one recommended mitigation, but it comes with a large performance hit for a wide range of workloads.
An alternative mitigation is to not make certain data in the kernel globally visible, but only when the kernel executes in the context of the process where this data belongs to.
Quoted 10 lines
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space. Page tables and mappings in that region will be exclusive to one address space, instead of implicitly shared between all kernel address spaces. Any data placed in that region will be out of reach of cache load gadgets that execute in different address spaces. To implement process-local memory, we introduce a new interface
kmalloc_proclocal()/kfree_proclocal()that allocates and maps pages exclusively into the current kernel address space. As a first use case, we move architectural state of guest CPUs in KVM out of reach of other kernel address spaces.
Quoted 3 lines
Can you briefly describe what types of attacks this is intended to mitigate? E.g. guest-guest, userspace-guest, etc... I don't want to make comments based on my potentially bad assumptions.
(quickly jumping in for Marius, he's offline today)
The main purpose of this is to protect from leakage of data from one guest into another guest using speculation gadgets on the host.
The same mechanism can be used to prevent leakage of secrets from one host process into another host process though, as host processes potentially have access to gadgets via the syscall interface.
Alex
Quoted 28 lines · Sean Christopherson
Quoted 12 lines · Marius Hillenbrand
The Linux kernel has a global address space that is the same for any kernel code. This address space becomes a liability in a world with processor information leak vulnerabilities, such as L1TF. With the right cache load gadget, an attacker-controlled hyperthread pair can leak arbitrary data via L1TF. Disabling hyperthreading is one recommended mitigation, but it comes with a large performance hit for a wide range of workloads.
An alternative mitigation is to not make certain data in the kernel globally visible, but only when the kernel executes in the context of the process where this data belongs to.
Quoted 10 lines
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space. Page tables and mappings in that region will be exclusive to one address space, instead of implicitly shared between all kernel address spaces. Any data placed in that region will be out of reach of cache load gadgets that execute in different address spaces. To implement process-local memory, we introduce a new interface
kmalloc_proclocal()/kfree_proclocal()that allocates and maps pages exclusively into the current kernel address space. As a first use case, we move architectural state of guest CPUs in KVM out of reach of other kernel address spaces.
Quoted 3 lines
Can you briefly describe what types of attacks this is intended to mitigate? E.g. guest-guest, userspace-guest, etc... I don't want to make comments based on my potentially bad assumptions.
I think I can assist in the explanation.
Consider the following scenario:
1) Hyperthread A in CPU core runs in guest and triggers a VMExit which is handled by host kernel. While hyperthread A runs VMExit handler, it populates CPU core cache / internal-resources (e.g. MDS buffers) with some sensitive data it have speculatively/architecturally access.
2) During hyperthread A running on host kernel, hyperthread B on same CPU core runs in guest and use some CPU speculative execution vulnerability to leak the sensitive host data populated by hyperthread A in CPU core cache / internal-resources.
Current CPU microcode mitigations (L1D/MDS flush) only handle the case of a single hyperthread and don’t provide a mechanism to mitigate this hyperthreading attack scenario.
Assuming there is some guest triggerable speculative load gadget in some VMExit path,
it can be used to force any data that is mapped into kernel address space to be loaded into CPU resource that is subject to leak. Therefore, there were multiple attempts to reduce sensitive information from being mapped into the kernel address space that is accessible by this VMExit path.
One attempt was XPFO which attempts to remove from kernel direct-map any page that is currently used only by userspace. Unfortunately, XPFO currently exhibits multiple performance issues that *currently* makes it impractical as far as I know.
Another attempt is this patch-series which attempts to remove from one vCPU thread host kernel address space,
the state of vCPUs of other guests. Which is very specific but I personally have additional ideas on how this patch series can be further used. For example, vhost-net needs to kmap entire guest memory into kernel-space to write ingress packets data into guest memory. Thus, vCPU thread kernel address space now maps entire other guest memory which can be leaked using the technique described above. Therefore, it should be useful to also move this kmap() to happen on process-local kernel virtual address region.
One could argue however that there is still a much bigger issue because of kernel direct-map that maps all physical pages that kernel manage (i.e. have struct page) in kernel virtual address space. And all of those pages can theoretically be leaked. However, this could be handled by complementary techniques such as booting host kernel with “mem=X” and mapping guest memory by directly mmap relevant portion of /dev/mem.
Which is probably what AWS does given these upstream KVM patches they have contributed:bd53cb35a3e9 X86/KVM: Handle PFNs outside of kernel reach when touching GPTEse45adf665a53 KVM: Introduce a new guest mapping API0c55671f84ff kvm, x86: Properly check whether a pfn is an MMIO or not
Also note that when using such “mem=X” technique, you can also avoid performance penalties introduced by CPU microcode mitigations. E.g. You can avoid doing L1D flush on VMEntry if VMExit handler run only in kernel and didn’t context-switch as you assume kernel address space don’t map any host sensitive data.
It’s also worth mentioning that another alternative that I have attempted to this “mem=X” technique was to create an isolated address space that is only used when running KVM VMExit handlers. For more information, refer to:
https://lkml.org/lkml/2019/5/13/515
(See some of my comments on that thread)
This is my 2cents on this at least.
-Liran
Quoted 2 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Quoted 13 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Quoted 8 lines
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
Just to be clear, are you referring to the per-cpu PGDs, or to this patch set with a per-mm kernel area?
Quoted 9 lines · Dave Hansen
Quoted 4 lines · Andy Lutomirski
Quoted 4 lines
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Quoted 5 lines
Quoted 2 lines
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
Just to be clear, are you referring to the per-cpu PGDs, or to this patch set with a per-mm kernel area?
per-CPU PGDs
Andy Lutomirski
maintainerQuoted 17 lines · Andy Lutomirski
Quoted 17 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Quoted 7 lines
Quoted 5 lines
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
I looked at the patch, and it (sensibly) has nothing to do with per-cpu PGDs. So it's in great shape!
Seriously, though, here are some very high-level review comments:
Please don't call it "process local", since "process" is meaningless. Call it "mm local" or something like that.
We already have a per-mm kernel mapping: the LDT. So please nix all the code that adds a new VA region, etc, except to the extent that some of it consists of valid cleanups in and of itself. Instead, please refactor the LDT code (arch/x86/kernel/ldt.c, mainly) to make it use a more general "mm local" address range, and then reuse the same infrastructure for other fancy things. The code that makes it KASLR-able should be in its very own patch that applies *after* the code that makes it all work so that, when the KASLR part causes a crash, we can bisect it.
+ /*
+ * Faults in process-local memory may be caused by process-local
+ * addresses leaking into other contexts.
+ * tbd: warn and handle gracefully.
+ */
+ if (unlikely(fault_in_process_local(address))) {
+ pr_err("page fault in PROCLOCAL at %lx", address);
+ force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)address, current);
+ }
+
Huh? Either it's an OOPS or you shouldn't print any special debugging. As it is, you're just blatantly leaking the address of the mm-local range to malicious user programs.
Also, you should IMO consider using this mechanism for kmap_atomic(). Hi, Nadav!
Quoted 54 lines · Andy Lutomirski
Quoted 14 lines · Andy Lutomirski
Quoted 14 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Quoted 7 lines
Quoted 5 lines
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
I looked at the patch, and it (sensibly) has nothing to do with per-cpu PGDs. So it's in great shape!
Seriously, though, here are some very high-level review comments:
Please don't call it "process local", since "process" is meaningless. Call it "mm local" or something like that.
We already have a per-mm kernel mapping: the LDT. So please nix all the code that adds a new VA region, etc, except to the extent that some of it consists of valid cleanups in and of itself. Instead, please refactor the LDT code (
arch/x86/kernel/ldt.c, mainly) to make it use a more general "mm local" address range, and then reuse the same infrastructure for other fancy things. The code that makes it KASLR-able should be in its very own patch that applies *after* the code that makes it all work so that, when the KASLR part causes a crash, we can bisect it.+ /*
+ * Faults in process-local memory may be caused by process-local
+ * addresses leaking into other contexts.
+ * tbd: warn and handle gracefully.
+ */
+ if (unlikely(fault_in_process_local(address))) {
+pr_err("page fault in PROCLOCAL at %lx", address);
+force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)address, current);
+ }
+
Quoted 6 lines
Huh? Either it's an OOPS or you shouldn't print any special debugging. As it is, you're just blatantly leaking the address of the mm-local range to malicious user programs.
Also, you should IMO consider using this mechanism for
kmap_atomic(). Hi, Nadav!
Well, some context for the “hi” would have been helpful. (Do I have a bug and I still don’t understand it?)
Perhaps you regard some use-case for a similar mechanism that I mentioned before. I did implement something similar (but not the way that you wanted) to improve the performance of seccomp and system-calls when retpolines are used. I set per-mm code area that held code that used direct calls to invoke seccomp filters and frequently used system-calls.
My mechanism, I think, is more not suitable for this use-case. I needed my code-page to be at the same 2GB range as the kernel text/modules, which does complicate things. Due to the same reason, it is also limited in the size of the data/code that it can hold.
Andy Lutomirski
maintainerQuoted 59 lines · Nadav Amit
Quoted 59 lines · Andy Lutomirski
Quoted 14 lines · Andy Lutomirski
Quoted 14 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Quoted 7 lines
Quoted 5 lines
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
I looked at the patch, and it (sensibly) has nothing to do with per-cpu PGDs. So it's in great shape!
Seriously, though, here are some very high-level review comments:
Please don't call it "process local", since "process" is meaningless. Call it "mm local" or something like that.
We already have a per-mm kernel mapping: the LDT. So please nix all the code that adds a new VA region, etc, except to the extent that some of it consists of valid cleanups in and of itself. Instead, please refactor the LDT code (
arch/x86/kernel/ldt.c, mainly) to make it use a more general "mm local" address range, and then reuse the same infrastructure for other fancy things. The code that makes it KASLR-able should be in its very own patch that applies *after* the code that makes it all work so that, when the KASLR part causes a crash, we can bisect it.+ /*
+ * Faults in process-local memory may be caused by process-local
+ * addresses leaking into other contexts.
+ * tbd: warn and handle gracefully.
+ */
+ if (unlikely(fault_in_process_local(address))) {
+pr_err("page fault in PROCLOCAL at %lx", address);
+force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)address, current);
+ }
+Huh? Either it's an OOPS or you shouldn't print any special debugging. As it is, you're just blatantly leaking the address of the mm-local range to malicious user programs.
Quoted 5 lines
Quoted 2 lines
Also, you should IMO consider using this mechanism for
kmap_atomic(). Hi, Nadav!Well, some context for the “hi” would have been helpful. (Do I have a bug and I still don’t understand it?)
Fair enough :)
Quoted 7 lines
Perhaps you regard some use-case for a similar mechanism that I mentioned before. I did implement something similar (but not the way that you wanted) to improve the performance of seccomp and system-calls when retpolines are used. I set per-mm code area that held code that used direct calls to invoke seccomp filters and frequently used system-calls.
Quoted 5 lines
My mechanism, I think, is more not suitable for this use-case. I needed my code-page to be at the same 2GB range as the kernel text/modules, which does complicate things. Due to the same reason, it is also limited in the size of the data/code that it can hold.
I actually meant the opposite. If we had a general-purpose per-mm kernel address range, could it be used to optimize kmap_atomic() by limiting the scope of any shootdowns? As a rough sketch, we'd have some kmap_atomic slots for each cpu *in the mm-local region*. I'm not entirely sure this is a win.
--Andy
Quoted 16 lines · Andy Lutomirski
Quoted 15 lines · Andy Lutomirski
Quoted 15 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
I looked at the patch, and it (sensibly) has nothing to do with per-cpu PGDs. So it's in great shape!
Thanks a lot for the very timely review!
Quoted 5 lines
Seriously, though, here are some very high-level review comments:
Please don't call it "process local", since "process" is meaningless. Call it "mm local" or something like that.
Naming is hard, yes :). Is "mmlocal" obvious enough to most readers? I'm not fully convinced, but I don't find it better or worse than proclocal. So whatever flies with the majority works for me :).
Quoted 6 lines
We already have a per-mm kernel mapping: the LDT. So please nix all the code that adds a new VA region, etc, except to the extent that some of it consists of valid cleanups in and of itself. Instead, please refactor the LDT code (
arch/x86/kernel/ldt.c, mainly) to make it use a more general "mm local" address range, and then reuse the same infrastructure for other fancy things. The code that makes it
I don't fully understand how those two are related. Are you referring to the KPTI enabling code in there? That just maps the LDT at the same address in both kernel and user mappings, no?
So you're suggesting we use the new mm local address as LDT address instead and have that mapped in both kernel and user space? This patch set today maps "mm local" data only in kernel space, not in user space, as it's meant for kernel data structures.
So I'm not really seeing the path to adapt any of the LDT logic to this. Could you please elaborate?
Quoted 3 lines
KASLR-able should be in its very own patch that applies *after* the code that makes it all work so that, when the KASLR part causes a crash, we can bisect it.
That sounds very reasonable, yes.
Quoted 12 lines
+ /*
+ * Faults in process-local memory may be caused by process-local
+ * addresses leaking into other contexts.
+ * tbd: warn and handle gracefully.
+ */
+ if (unlikely(fault_in_process_local(address))) {
+pr_err("page fault in PROCLOCAL at %lx", address);
+force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)address, current);
+ }
+
Quoted 3 lines
Huh? Either it's an OOPS or you shouldn't print any special debugging. As it is, you're just blatantly leaking the address of the mm-local range to malicious user programs.
Yes, this is a left over bit from an idea that we discussed and rejected yesterday. The idea was to have a DEBUG config option that allows proclocal memory to leak into other processes, but print debug output so that it's easier to catch bugs. After discussion, I think we managed to convince everyone that an OOPS is the better tool to find bugs :).
Any trace of this will disappear in the next version.
Quoted 2 lines
Also, you should IMO consider using this mechanism for
kmap_atomic().
It might make sense to use it for kmap_atomic() for debug purposes, as it ensures that other users can no longer access the same mapping through the linear map. However, it does come at quite a big cost, as we need to shoot down the TLB of all other threads in the system. So I'm not sure it's of general value?
Alex
Quoted 1 line
Hi, Nadav!
Andy Lutomirski
maintainerQuoted 35 lines · Alexander Graf
Quoted 16 lines · Andy Lutomirski
Quoted 15 lines · Andy Lutomirski
Quoted 15 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
I looked at the patch, and it (sensibly) has nothing to do with per-cpu PGDs. So it's in great shape!
Thanks a lot for the very timely review!
Quoted 3 lines
Seriously, though, here are some very high-level review comments:
Quoted 7 lines
Quoted 2 lines
Please don't call it "process local", since "process" is meaningless. Call it "mm local" or something like that.
Naming is hard, yes :). Is "mmlocal" obvious enough to most readers? I'm not fully convinced, but I don't find it better or worse than proclocal. So whatever flies with the majority works for me :).
My objection to "proc" is that we have many concepts of "process" in the kernel: task, mm, signal handling context, etc. These memory ranges are specifically local to the mm. Admittedly, it would be very surprising to have memory that is local to a signal handling context, but still.
Quoted 10 lines
Quoted 8 lines
We already have a per-mm kernel mapping: the LDT. So please nix all the code that adds a new VA region, etc, except to the extent that some of it consists of valid cleanups in and of itself. Instead, please refactor the LDT code (
arch/x86/kernel/ldt.c, mainly) to make it use a more general "mm local" address range, and then reuse the same infrastructure for other fancy things. The code that makes it
Quoted 3 lines
I don't fully understand how those two are related. Are you referring to the KPTI enabling code in there? That just maps the LDT at the same address in both kernel and user mappings, no?
The relevance here is that, when KPTI is on, the exact same address refers to a different LDT in different mms, so it's genuinely an mm-local mapping. It works just like yours: a whole top-level paging entry is reserved for it. What I'm suggesting is that, when you're all done, the LDT should be more or less just one more mm-local mapping, with two caveats. First, the LDT needs special KPTI handling, but that's fine. Second, the LDT address is visible to user code on non-UMIP systems, so you'll have to decide if that's okay. My suggestion is to have the LDT be the very first address in the mm-local range and then to randomize everything else in the mm-local range.
Quoted 5 lines
So you're suggesting we use the new mm local address as LDT address instead and have that mapped in both kernel and user space? This patch set today maps "mm local" data only in kernel space, not in user space, as it's meant for kernel data structures.
Yes, exactly.
Quoted 40 lines
So I'm not really seeing the path to adapt any of the LDT logic to this. Could you please elaborate?
Quoted 3 lines
KASLR-able should be in its very own patch that applies *after* the code that makes it all work so that, when the KASLR part causes a crash, we can bisect it.
That sounds very reasonable, yes.
Quoted 12 lines
+ /*
+ * Faults in process-local memory may be caused by process-local
+ * addresses leaking into other contexts.
+ * tbd: warn and handle gracefully.
+ */
+ if (unlikely(fault_in_process_local(address))) {
+pr_err("page fault in PROCLOCAL at %lx", address);
+force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)address, current);
+ }
+Quoted 3 lines
Huh? Either it's an OOPS or you shouldn't print any special debugging. As it is, you're just blatantly leaking the address of the mm-local range to malicious user programs.
Yes, this is a left over bit from an idea that we discussed and rejected yesterday. The idea was to have a DEBUG config option that allows proclocal memory to leak into other processes, but print debug output so that it's easier to catch bugs. After discussion, I think we managed to convince everyone that an OOPS is the better tool to find bugs :).
Any trace of this will disappear in the next version.
Quoted 1 line
Quoted 8 lines
Quoted 1 line
Also, you should IMO consider using this mechanism for
kmap_atomic().It might make sense to use it for
kmap_atomic()for debug purposes, as it ensures that other users can no longer access the same mapping through the linear map. However, it does come at quite a big cost, as we need to shoot down the TLB of all other threads in the system. So I'm not sure it's of general value?
What I meant was that kmap_atomic() could use mm-local memory so that it doesn't need to do a global shootdown. But I guess it's not actually used for real on 64-bit, so this is mostly moot. Are you planning to support mm-local on 32-bit?
--Andy
Quoted 6 lines
Alex
Quoted 1 line
Hi, Nadav!
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines
It might make sense to use it for
kmap_atomic()for debug purposes, as it ensures that other users can no longer access the same mapping through the linear map. However, it does come at quite a big cost, as we need to shoot down the TLB of all other threads in the system. So I'm not sure it's of general value?What I meant was that
kmap_atomic()could use mm-local memory so that it doesn't need to do a global shootdown. But I guess it's not actually used for real on 64-bit, so this is mostly moot. Are you planning to support mm-local on 32-bit?
Do we *do* global shootdowns on kmap_atomic()s on 32-bit? I thought we used entirely per-cpu addresses, so a stale entry from another CPU can get loaded in the TLB speculatively but it won't ever actually get used.
I think it goes:
kunmap_atomic() ->__kunmap_atomic() ->kpte_clear_flush() ->__flush_tlb_one_kernel() ->__flush_tlb_one_user() ->__native_flush_tlb_one_user() ->
invlpg
The per-cpu address calculation is visible in kmap_atomic_prot():
idx = type + KM_TYPE_NR*smp_processor_id();
Quoted 26 lines · Dave Hansen
Quoted 9 lines · Andy Lutomirski
Quoted 5 lines
It might make sense to use it for
kmap_atomic()for debug purposes, as it ensures that other users can no longer access the same mapping through the linear map. However, it does come at quite a big cost, as we need to shoot down the TLB of all other threads in the system. So I'm not sure it's of general value?What I meant was that
kmap_atomic()could use mm-local memory so that it doesn't need to do a global shootdown. But I guess it's not actually used for real on 64-bit, so this is mostly moot. Are you planning to support mm-local on 32-bit?Do we *do* global shootdowns on kmap_atomic()s on 32-bit? I thought we used entirely per-cpu addresses, so a stale entry from another CPU can get loaded in the TLB speculatively but it won't ever actually get used. I think it goes:
kunmap_atomic()->__kunmap_atomic()->kpte_clear_flush()->__flush_tlb_one_kernel()->__flush_tlb_one_user()->__native_flush_tlb_one_user()->
invlpg
Quoted 5 lines
The per-cpu address calculation is visible in
kmap_atomic_prot():idx = type + KM_TYPE_NR*smp_processor_id();From a security point-of-view, having such an entry is still not too good,
since the mapping protection might override the default protection. This might lead to potential W+X cases, for example, that might stay for a long time if they are speculatively cached in the TLB and not invalidated upon kunmap_atomic().
Having said that, I am not too excited to deal with this issue. Do people still care about x86/32-bit? In addition, if kunmap_atomic() is used when IRQs are disabled, sending a TLB shootdown during kunmap_atomic() can cause a deadlock.
Quoted 2 lines · Nadav Amit
Having said that, I am not too excited to deal with this issue. Do people still care about x86/32-bit?
No, not really. It's just fun to try to give history lessons about the good old days. ;)
Quoted 4 lines · Dave Hansen
Quoted 2 lines · Nadav Amit
Having said that, I am not too excited to deal with this issue. Do people still care about x86/32-bit?
No, not really.
Especially not for KVM, given the number of times 32-bit KVM has been broken recently without anyone noticing for several kernel releases.
Quoted 14 lines · Andy Lutomirski
Quoted 14 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Quoted 8 lines
Quoted 5 lines
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
Yes I know, but as a benefit we could get rid of all the GSBASE horrors in the entry code as we could just put the percpu space into the local PGD.
Thanks,
tglx
Andy Lutomirski
maintainerQuoted 22 lines · Thomas Gleixner
Quoted 20 lines · Andy Lutomirski
Quoted 19 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Where's the context-switching code? Did I just miss it?
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Quoted 6 lines
Quoted 2 lines
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
Yes I know, but as a benefit we could get rid of all the GSBASE horrors in the entry code as we could just put the percpu space into the local PGD.
I have personally suggested this to Linus on a couple of occasions, and he seemed quite skeptical.
Quoted 6 lines · Andy Lutomirski
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines · Andy Lutomirski
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
Quoted 6 lines
Quoted 3 lines
Yes I know, but as a benefit we could get rid of all the GSBASE horrors in the entry code as we could just put the percpu space into the local PGD.
I have personally suggested this to Linus on a couple of occasions, and he seemed quite skeptical.
The only way to find out is the good old: numbers talk ....
So someone has to bite the bullet, implement it and figure out whether it's bollocks or not. :)
Thanks,
tglx
Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Andy Lutomirski
Quoted 15 lines · Dave Hansen
Quoted 4 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
Where's the context-switching code? Did I just miss it?
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
We've discussed having per-cpu page tables where a given PGD is only in use from one CPU at a time. I *think* this scheme still works in such a case, it just adds one more PGD entry that would have to context-switched.
Fair warning: Linus is on record as absolutely hating this idea. He might change his mind, but it’s an uphill battle.
Yes I know, but as a benefit we could get rid of all the GSBASE horrors in the entry code as we could just put the percpu space into the local PGD.
Would that mean that with Meltdown affected CPUs we open speculation attacks against the mmlocal memory from KVM user space?
Alex
Quoted 6 lines · Alexander Graf
Quoted 3 lines
Yes I know, but as a benefit we could get rid of all the GSBASE horrors in
the entry code as we could just put the percpu space into the local PGD.Would that mean that with Meltdown affected CPUs we open speculation attacks against the mmlocal memory from KVM user space?
Not necessarily. There would likely be a _set_ of local PGDs. We could still have pair of PTI PGDs just like we do know, they'd just be a local PGD pair.
Andy Lutomirski
maintainerQuoted 6 lines · Dave Hansen
Quoted 4 lines · Alexander Graf
Quoted 3 lines
Yes I know, but as a benefit we could get rid of all the GSBASE horrors in
the entry code as we could just put the percpu space into the local PGD.
Quoted 7 lines
Quoted 2 lines
Would that mean that with Meltdown affected CPUs we open speculation attacks against the mmlocal memory from KVM user space?
Not necessarily. There would likely be a _set_ of local PGDs. We could still have pair of PTI PGDs just like we do know, they'd just be a local PGD pair.
Unfortunately, this would mean that we need to sync twice as many top-level entries when we context switch.
Quoted 8 lines · Andy Lutomirski
Quoted 6 lines
Quoted 2 lines
Would that mean that with Meltdown affected CPUs we open speculation attacks against the mmlocal memory from KVM user space?
Not necessarily. There would likely be a _set_ of local PGDs. We could still have pair of PTI PGDs just like we do know, they'd just be a local PGD pair.
Unfortunately, this would mean that we need to sync twice as many top-level entries when we context switch.
Yeah, PTI sucks. :)
For anyone following along at home, I'm going to go off into crazy per-cpu-pgds speculation mode now... Feel free to stop reading now. :)
But, I was thinking we could get away with not doing this on _every_ context switch at least. For instance, couldn't 'struct tlb_context' have PGD pointer (or two with PTI) in addition to the TLB info? That way we only do the copying when we change the context. Or does that tie the implementation up too much with PCIDs?
Andy Lutomirski
maintainerQuoted 13 lines · Dave Hansen
Quoted 8 lines · Andy Lutomirski
Quoted 6 lines
Quoted 2 lines
Would that mean that with Meltdown affected CPUs we open speculation attacks against the mmlocal memory from KVM user space?
Not necessarily. There would likely be a _set_ of local PGDs. We could still have pair of PTI PGDs just like we do know, they'd just be a local PGD pair.
Unfortunately, this would mean that we need to sync twice as many top-level entries when we context switch.
Yeah, PTI sucks. :)
Quoted 8 lines
For anyone following along at home, I'm going to go off into crazy per-cpu-pgds speculation mode now... Feel free to stop reading now. :)
But, I was thinking we could get away with not doing this on _every_ context switch at least. For instance, couldn't 'struct
tlb_context' have PGD pointer (or two with PTI) in addition to the TLB info? That way we only do the copying when we change the context. Or does that tie the implementation up too much with PCIDs?
Hmm, that seems entirely reasonable. I think the nasty bit would be figuring out all the interactions with PV TLB flushing. PV TLB flushes already don't play so well with PCID tracking, and this will make it worse. We probably need to rewrite all that code regardless.
Quoted 23 lines · Andy Lutomirski
Quoted 11 lines · Dave Hansen
Quoted 7 lines · Andy Lutomirski
Quoted 5 lines
Quoted 2 lines
Would that mean that with Meltdown affected CPUs we open speculation attacks against the mmlocal memory from KVM user space?
Not necessarily. There would likely be a _set_ of local PGDs. We could still have pair of PTI PGDs just like we do know, they'd just be a local PGD pair.
Unfortunately, this would mean that we need to sync twice as many top-level entries when we context switch.
Yeah, PTI sucks. :)
Quoted 8 lines
For anyone following along at home, I'm going to go off into crazy per-cpu-pgds speculation mode now... Feel free to stop reading now. :)
But, I was thinking we could get away with not doing this on _every_ context switch at least. For instance, couldn't 'struct
tlb_context' have PGD pointer (or two with PTI) in addition to the TLB info? That way we only do the copying when we change the context. Or does that tie the implementation up too much with PCIDs?
Quoted 4 lines
Hmm, that seems entirely reasonable. I think the nasty bit would be figuring out all the interactions with PV TLB flushing. PV TLB flushes already don't play so well with PCID tracking, and this will make it worse. We probably need to rewrite all that code regardless.
How is PCID (as you implemented) related to TLB flushing of kernel (not user) PTEs? These kernel PTEs would be global, so they would be invalidated from all the address-spaces using INVLPG, I presume. No?
Having said that, the fact that every hypervisor implements PV-TLB completely differently might be unwarranted.
Quoted 8 lines · Nadav Amit
Quoted 8 lines
Quoted 8 lines
For anyone following along at home, I'm going to go off into crazy per-cpu-pgds speculation mode now... Feel free to stop reading now. :)
But, I was thinking we could get away with not doing this on _every_ context switch at least. For instance, couldn't 'struct
tlb_context' have PGD pointer (or two with PTI) in addition to the TLB info? That way we only do the copying when we change the context. Or does that tie the implementation up too much with PCIDs?
Quoted 7 lines
Quoted 4 lines
Hmm, that seems entirely reasonable. I think the nasty bit would be figuring out all the interactions with PV TLB flushing. PV TLB flushes already don't play so well with PCID tracking, and this will make it worse. We probably need to rewrite all that code regardless.
How is PCID (as you implemented) related to TLB flushing of kernel (not user) PTEs? These kernel PTEs would be global, so they would be invalidated from all the address-spaces using INVLPG, I presume. No?
The idea is that you have a per-cpu address space. Certain kernel virtual addresses would map to different physical address based on where you are running. Each of the physical addresses would be "owned" by a single CPU and would, by convention, never use a PGD that mapped an address unless that CPU that "owned" it.
In that case, you never really invalidate those addresses.
Quoted 17 lines · Dave Hansen
Quoted 8 lines · Nadav Amit
Quoted 8 lines
Quoted 8 lines
For anyone following along at home, I'm going to go off into crazy per-cpu-pgds speculation mode now... Feel free to stop reading now. :)
But, I was thinking we could get away with not doing this on _every_ context switch at least. For instance, couldn't 'struct
tlb_context' have PGD pointer (or two with PTI) in addition to the TLB info? That way we only do the copying when we change the context. Or does that tie the implementation up too much with PCIDs?Quoted 7 lines
Quoted 4 lines
Hmm, that seems entirely reasonable. I think the nasty bit would be figuring out all the interactions with PV TLB flushing. PV TLB flushes already don't play so well with PCID tracking, and this will make it worse. We probably need to rewrite all that code regardless.
How is PCID (as you implemented) related to TLB flushing of kernel (not user) PTEs? These kernel PTEs would be global, so they would be invalidated from all the address-spaces using INVLPG, I presume. No?
Quoted 7 lines
The idea is that you have a per-cpu address space. Certain kernel virtual addresses would map to different physical address based on where you are running. Each of the physical addresses would be "owned" by a single CPU and would, by convention, never use a PGD that mapped an address unless that CPU that "owned" it.
In that case, you never really invalidate those addresses.
But you would need to invalidate if the process moved to another CPU, correct?
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 7 lines
The idea is that you have a per-cpu address space. Certain kernel virtual addresses would map to different physical address based on where you are running. Each of the physical addresses would be "owned" by a single CPU and would, by convention, never use a PGD that mapped an address unless that CPU that "owned" it.
In that case, you never really invalidate those addresses.
But you would need to invalidate if the process moved to another CPU, correct?
If you have a per-cpu PGD, the rule is that you never use the PGD on more than one CPU. In that model, processes "take over" an existing PGD to run on the CPU rather than having the CPU use an existing PGD that came with the process.
But we've really hijacked the original thread at this point, which is probably causing a ton of confusion.
Andy Lutomirski
maintainerQuoted 25 lines · Konrad Rzeszutek Wilk
Quoted 23 lines · Dave Hansen
Quoted 8 lines · Nadav Amit
Quoted 8 lines
Quoted 8 lines
For anyone following along at home, I'm going to go off into crazy per-cpu-pgds speculation mode now... Feel free to stop reading now. :)
But, I was thinking we could get away with not doing this on _every_ context switch at least. For instance, couldn't 'struct
tlb_context' have PGD pointer (or two with PTI) in addition to the TLB info? That way we only do the copying when we change the context. Or does that tie the implementation up too much with PCIDs?Quoted 7 lines
Quoted 4 lines
Hmm, that seems entirely reasonable. I think the nasty bit would be figuring out all the interactions with PV TLB flushing. PV TLB flushes already don't play so well with PCID tracking, and this will make it worse. We probably need to rewrite all that code regardless.
How is PCID (as you implemented) related to TLB flushing of kernel (not user) PTEs? These kernel PTEs would be global, so they would be invalidated from all the address-spaces using INVLPG, I presume. No?
The idea is that you have a per-cpu address space. Certain kernel virtual addresses would map to different physical address based on where you are running. Each of the physical addresses would be "owned" by a single CPU and would, by convention, never use a PGD that mapped an address unless that CPU that "owned" it.
Quoted 4 lines
Quoted 1 line
In that case, you never really invalidate those addresses.
But you would need to invalidate if the process moved to another CPU, correct?
There's nothing to invalidate. It's a different CPU with a different TLB.
The big problem is that you have a choice. Either you can have one PGD per (mm, cpu) or you just have one or a few PGDs per CPU and you change them every time you change processes. Dave's idea to have one or two per (cpu, asid) is right, though. It means we have a decent chance of context switching without rewriting the whole thing, and it also means we don't need to write to the one that's currently loaded when we switch CR3. The latter could plausibly be important enough that we'd want to pretend we're using PCID even if we're not.
Quoted 19 lines · Dave Hansen
Quoted 8 lines · Nadav Amit
Quoted 8 lines
Quoted 8 lines
For anyone following along at home, I'm going to go off into crazy per-cpu-pgds speculation mode now... Feel free to stop reading now. :)
But, I was thinking we could get away with not doing this on _every_ context switch at least. For instance, couldn't 'struct
tlb_context' have PGD pointer (or two with PTI) in addition to the TLB info? That way we only do the copying when we change the context. Or does that tie the implementation up too much with PCIDs?Quoted 7 lines
Quoted 4 lines
Hmm, that seems entirely reasonable. I think the nasty bit would be figuring out all the interactions with PV TLB flushing. PV TLB flushes already don't play so well with PCID tracking, and this will make it worse. We probably need to rewrite all that code regardless.
How is PCID (as you implemented) related to TLB flushing of kernel (not user) PTEs? These kernel PTEs would be global, so they would be invalidated from all the address-spaces using INVLPG, I presume. No?
Quoted 7 lines
The idea is that you have a per-cpu address space. Certain kernel virtual addresses would map to different physical address based on where you are running. Each of the physical addresses would be "owned" by a single CPU and would, by convention, never use a PGD that mapped an address unless that CPU that "owned" it.
In that case, you never really invalidate those addresses.
I understand, but as I see it, this is not related directly to PCIDs.
Quoted 8 lines · Nadav Amit
Quoted 7 lines
The idea is that you have a per-cpu address space. Certain kernel virtual addresses would map to different physical address based on where you are running. Each of the physical addresses would be "owned" by a single CPU and would, by convention, never use a PGD that mapped an address unless that CPU that "owned" it.
In that case, you never really invalidate those addresses.
I understand, but as I see it, this is not related directly to PCIDs.
Yeah, the only link I was thinking of is that we can manage per-CPU PGDs in the same way that we manage PCIDs. Basically we can reuse a chunk of the software concept.
Quoted 10 lines · Dave Hansen
Quoted 2 lines · Marius Hillenbrand
This patch series proposes to introduce a region for what we call process-local memory into the kernel's virtual address space.
It might be fun to cc some x86 folks on this series. They might have some relevant opinions. ;)
A few high-level questions:
Why go to all this trouble to hide guest state like registers if all the guest data itself is still mapped?
(jumping in for Marius, he's offline today)
Glad you asked :). I hope this cover letter explains well how to achieve guest data not being mapped:
https://lkml.org/lkml/2019/1/31/933
Quoted 1 line
Where's the context-switching code? Did I just miss it?
I'm not sure I understand the question. With this mechanism, the global linear map pages are just not present anymore, so there is no context switching needed. For the process local memory, the page table is already mm local, so we don't need to do anything special during context switch, no?
Alex
Quoted 7 lines · Alexander Graf
Quoted 1 line
Where's the context-switching code? Did I just miss it?
I'm not sure I understand the question. With this mechanism, the global linear map pages are just not present anymore, so there is no context switching needed. For the process local memory, the page table is already mm local, so we don't need to do anything special during context switch, no?
Thanks for explaining, I was just confused.
Andy reminded me when comparing it to the LDT area: since this area is per-mm/pgd and we context switch that *obviously* there's no extra work to do at context switch time, as long as the area is marked non-Global.