Merge branch 'x86/pti-urgent' into x86/pti
[linux-2.6-microblaze.git] / arch / x86 / mm / pti.c
index e41ee93..ef8db6f 100644 (file)
@@ -176,7 +176,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
 
        if (pgd_none(*pgd)) {
                unsigned long new_p4d_page = __get_free_page(gfp);
-               if (!new_p4d_page)
+               if (WARN_ON_ONCE(!new_p4d_page))
                        return NULL;
 
                set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
@@ -195,13 +195,17 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
 static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
 {
        gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
-       p4d_t *p4d = pti_user_pagetable_walk_p4d(address);
+       p4d_t *p4d;
        pud_t *pud;
 
+       p4d = pti_user_pagetable_walk_p4d(address);
+       if (!p4d)
+               return NULL;
+
        BUILD_BUG_ON(p4d_large(*p4d) != 0);
        if (p4d_none(*p4d)) {
                unsigned long new_pud_page = __get_free_page(gfp);
-               if (!new_pud_page)
+               if (WARN_ON_ONCE(!new_pud_page))
                        return NULL;
 
                set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page)));
@@ -215,7 +219,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
        }
        if (pud_none(*pud)) {
                unsigned long new_pmd_page = __get_free_page(gfp);
-               if (!new_pmd_page)
+               if (WARN_ON_ONCE(!new_pmd_page))
                        return NULL;
 
                set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page)));
@@ -237,9 +241,13 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
 static __init pte_t *pti_user_pagetable_walk_pte(unsigned long address)
 {
        gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
-       pmd_t *pmd = pti_user_pagetable_walk_pmd(address);
+       pmd_t *pmd;
        pte_t *pte;
 
+       pmd = pti_user_pagetable_walk_pmd(address);
+       if (!pmd)
+               return NULL;
+
        /* We can't do anything sensible if we hit a large mapping. */
        if (pmd_large(*pmd)) {
                WARN_ON(1);
@@ -359,6 +367,9 @@ static void __init pti_clone_p4d(unsigned long addr)
        pgd_t *kernel_pgd;
 
        user_p4d = pti_user_pagetable_walk_p4d(addr);
+       if (!user_p4d)
+               return;
+
        kernel_pgd = pgd_offset_k(addr);
        kernel_p4d = p4d_offset(kernel_pgd, addr);
        *user_p4d = *kernel_p4d;
@@ -404,7 +415,7 @@ static void __init pti_setup_espfix64(void)
 /*
  * Clone the populated PMDs of the entry and irqentry text and force it RO.
  */
-static void __init pti_clone_entry_text(void)
+static void pti_clone_entry_text(void)
 {
        pti_clone_pmds((unsigned long) __entry_text_start,
                        (unsigned long) __irqentry_text_end,
@@ -458,11 +469,18 @@ static inline bool pti_kernel_image_global_ok(void)
        return true;
 }
 
+/*
+ * This is the only user for these and it is not arch-generic
+ * like the other set_memory.h functions.  Just extern them.
+ */
+extern int set_memory_nonglobal(unsigned long addr, int numpages);
+extern int set_memory_global(unsigned long addr, int numpages);
+
 /*
  * For some configurations, map all of kernel text into the user page
  * tables.  This reduces TLB misses, especially on non-PCID systems.
  */
-void pti_clone_kernel_text(void)
+static void pti_clone_kernel_text(void)
 {
        /*
         * rodata is part of the kernel image and is normally
@@ -470,7 +488,8 @@ void pti_clone_kernel_text(void)
         * clone the areas past rodata, they might contain secrets.
         */
        unsigned long start = PFN_ALIGN(_text);
-       unsigned long end = (unsigned long)__end_rodata_aligned;
+       unsigned long end_clone  = (unsigned long)__end_rodata_aligned;
+       unsigned long end_global = PFN_ALIGN((unsigned long)__stop___ex_table);
 
        if (!pti_kernel_image_global_ok())
                return;
@@ -482,15 +501,19 @@ void pti_clone_kernel_text(void)
         * pti_set_kernel_image_nonglobal() did to clear the
         * global bit.
         */
-       pti_clone_pmds(start, end, 0);
+       pti_clone_pmds(start, end_clone, _PAGE_RW);
+
+       /*
+        * pti_clone_pmds() will set the global bit in any PMDs
+        * that it clones, but we also need to get any PTEs in
+        * the last level for areas that are not huge-page-aligned.
+        */
+
+       /* Set the global bit for normal non-__init kernel text: */
+       set_memory_global(start, (end_global - start) >> PAGE_SHIFT);
 }
 
-/*
- * This is the only user for it and it is not arch-generic like
- * the other set_memory.h functions.  Just extern it.
- */
-extern int set_memory_nonglobal(unsigned long addr, int numpages);
-static void pti_set_kernel_image_nonglobal(void)
+void pti_set_kernel_image_nonglobal(void)
 {
        /*
         * The identity map is created with PMDs, regardless of the
@@ -501,9 +524,11 @@ static void pti_set_kernel_image_nonglobal(void)
        unsigned long start = PFN_ALIGN(_text);
        unsigned long end = ALIGN((unsigned long)_end, PMD_PAGE_SIZE);
 
-       if (pti_kernel_image_global_ok())
-               return;
-
+       /*
+        * This clears _PAGE_GLOBAL from the entire kernel image.
+        * pti_clone_kernel_text() map put _PAGE_GLOBAL back for
+        * areas that are mapped to userspace.
+        */
        set_memory_nonglobal(start, (end - start) >> PAGE_SHIFT);
 }
 
@@ -517,6 +542,28 @@ void __init pti_init(void)
 
        pr_info("enabled\n");
 
+#ifdef CONFIG_X86_32
+       /*
+        * We check for X86_FEATURE_PCID here. But the init-code will
+        * clear the feature flag on 32 bit because the feature is not
+        * supported on 32 bit anyway. To print the warning we need to
+        * check with cpuid directly again.
+        */
+       if (cpuid_ecx(0x1) && BIT(17)) {
+               /* Use printk to work around pr_fmt() */
+               printk(KERN_WARNING "\n");
+               printk(KERN_WARNING "************************************************************\n");
+               printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!  **\n");
+               printk(KERN_WARNING "**                                                        **\n");
+               printk(KERN_WARNING "** You are using 32-bit PTI on a 64-bit PCID-capable CPU. **\n");
+               printk(KERN_WARNING "** Your performance will increase dramatically if you     **\n");
+               printk(KERN_WARNING "** switch to a 64-bit kernel!                             **\n");
+               printk(KERN_WARNING "**                                                        **\n");
+               printk(KERN_WARNING "** WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!  **\n");
+               printk(KERN_WARNING "************************************************************\n");
+       }
+#endif
+
        pti_clone_user_shared();
 
        /* Undo all global bits from the init pagetables in head_64.S: */
@@ -526,3 +573,20 @@ void __init pti_init(void)
        pti_setup_espfix64();
        pti_setup_vsyscall();
 }
+
+/*
+ * Finalize the kernel mappings in the userspace page-table. Some of the
+ * mappings for the kernel image might have changed since pti_init()
+ * cloned them. This is because parts of the kernel image have been
+ * mapped RO and/or NX.  These changes need to be cloned again to the
+ * userspace page-table.
+ */
+void pti_finalize(void)
+{
+       /*
+        * We need to clone everything (again) that maps parts of the
+        * kernel image.
+        */
+       pti_clone_entry_text();
+       pti_clone_kernel_text();
+}