]> Git Repo - linux.git/commitdiff
Merge branch 'x86/pti-urgent' into x86/pti
authorThomas Gleixner <[email protected]>
Mon, 6 Aug 2018 18:56:34 +0000 (20:56 +0200)
committerThomas Gleixner <[email protected]>
Mon, 6 Aug 2018 18:56:34 +0000 (20:56 +0200)
Integrate the PTI Global bit fixes which conflict with the 32bit PTI
support.

Signed-off-by: Thomas Gleixner <[email protected]>
1  2 
arch/x86/mm/init_64.c
arch/x86/mm/pti.c

diff --combined arch/x86/mm/init_64.c
index 9b19f9a8948ecef8601af7c036361a8757385c3f,68c292cb1ebfb2ab5b596ede52fe90926b872945..dd519f3721692180b3aac7f5e8eeda52a1d68226
@@@ -1283,14 -1283,16 +1283,10 @@@ void mark_rodata_ro(void
        set_memory_ro(start, (end-start) >> PAGE_SHIFT);
  #endif
  
-       free_init_pages("unused kernel",
-                       (unsigned long) __va(__pa_symbol(text_end)),
-                       (unsigned long) __va(__pa_symbol(rodata_start)));
-       free_init_pages("unused kernel",
-                       (unsigned long) __va(__pa_symbol(rodata_end)),
-                       (unsigned long) __va(__pa_symbol(_sdata)));
+       free_kernel_image_pages((void *)text_end, (void *)rodata_start);
+       free_kernel_image_pages((void *)rodata_end, (void *)_sdata);
  
        debug_checkwx();
 -
 -      /*
 -       * Do this after all of the manipulation of the
 -       * kernel text page tables are complete.
 -       */
 -      pti_clone_kernel_text();
  }
  
  int kern_addr_valid(unsigned long addr)
diff --combined arch/x86/mm/pti.c
index bcf35dac19203d1df13160a040a1a9bc48fa11db,8d88d067b3d75a6782a6614a47c772e5dbbb6382..ef8db6ffc836e6cf26d26c6e60be00604e76b90e
@@@ -117,7 -117,7 +117,7 @@@ enable
        setup_force_cpu_cap(X86_FEATURE_PTI);
  }
  
 -pgd_t __pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)
 +pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
  {
        /*
         * Changes to the high (kernel) portion of the kernelmode page
@@@ -176,7 -176,7 +176,7 @@@ static p4d_t *pti_user_pagetable_walk_p
  
        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)));
  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)));
        }
        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)));
  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);
@@@ -305,10 -297,6 +305,10 @@@ pti_clone_pmds(unsigned long start, uns
                p4d_t *p4d;
                pud_t *pud;
  
 +              /* Overflow check */
 +              if (addr < start)
 +                      break;
 +
                pgd = pgd_offset_k(addr);
                if (WARN_ON(pgd_none(*pgd)))
                        return;
        }
  }
  
 +#ifdef CONFIG_X86_64
  /*
   * Clone a single p4d (i.e. a top-level entry on 4-level systems and a
   * next-level entry on 5-level systems.
@@@ -367,9 -354,6 +367,9 @@@ static void __init pti_clone_p4d(unsign
        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;
@@@ -383,25 -367,6 +383,25 @@@ static void __init pti_clone_user_share
        pti_clone_p4d(CPU_ENTRY_AREA_BASE);
  }
  
 +#else /* CONFIG_X86_64 */
 +
 +/*
 + * On 32 bit PAE systems with 1GB of Kernel address space there is only
 + * one pgd/p4d for the whole kernel. Cloning that would map the whole
 + * address space into the user page-tables, making PTI useless. So clone
 + * the page-table on the PMD level to prevent that.
 + */
 +static void __init pti_clone_user_shared(void)
 +{
 +      unsigned long start, end;
 +
 +      start = CPU_ENTRY_AREA_BASE;
 +      end   = start + (PAGE_SIZE * CPU_ENTRY_AREA_PAGES);
 +
 +      pti_clone_pmds(start, end, 0);
 +}
 +#endif /* CONFIG_X86_64 */
 +
  /*
   * Clone the ESPFIX P4D into the user space visible page table
   */
@@@ -415,7 -380,7 +415,7 @@@ static void __init pti_setup_espfix64(v
  /*
   * 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,
@@@ -469,11 -434,18 +469,18 @@@ static inline bool pti_kernel_image_glo
        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
         * 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_hpage_align;
++      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;
         * 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
        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);
  }
  
@@@ -528,28 -507,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: */
        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();
 +}
This page took 0.070581 seconds and 4 git commands to generate.