OSDN Git Service

powerpc/32: Add support of KASAN_VMALLOC
authorChristophe Leroy <christophe.leroy@c-s.fr>
Tue, 14 Jan 2020 17:54:00 +0000 (17:54 +0000)
committerMichael Ellerman <mpe@ellerman.id.au>
Mon, 27 Jan 2020 11:37:33 +0000 (22:37 +1100)
Add support of KASAN_VMALLOC on PPC32.

To allow this, the early shadow covering the VMALLOC space
need to be removed once high_memory var is set and before
freeing memblock.

And the VMALLOC area need to be aligned such that boundaries
are covered by a full shadow page.

Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/031dec5487bde9b2181c8b3c9800e1879cf98c1a.1579024426.git.christophe.leroy@c-s.fr
arch/powerpc/Kconfig
arch/powerpc/include/asm/book3s/32/pgtable.h
arch/powerpc/include/asm/kasan.h
arch/powerpc/include/asm/nohash/32/pgtable.h
arch/powerpc/mm/kasan/kasan_init_32.c
arch/powerpc/mm/mem.c

index 6093c48..a7f10a1 100644 (file)
@@ -173,6 +173,7 @@ config PPC
        select HAVE_ARCH_HUGE_VMAP              if PPC_BOOK3S_64 && PPC_RADIX_MMU
        select HAVE_ARCH_JUMP_LABEL
        select HAVE_ARCH_KASAN                  if PPC32
+       select HAVE_ARCH_KASAN_VMALLOC          if PPC32
        select HAVE_ARCH_KGDB
        select HAVE_ARCH_MMAP_RND_BITS
        select HAVE_ARCH_MMAP_RND_COMPAT_BITS   if COMPAT
index 0796533..5b39c11 100644 (file)
@@ -193,7 +193,12 @@ int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);
 #else
 #define VMALLOC_START ((((long)high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1)))
 #endif
+
+#ifdef CONFIG_KASAN_VMALLOC
+#define VMALLOC_END    _ALIGN_DOWN(ioremap_bot, PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
+#else
 #define VMALLOC_END    ioremap_bot
+#endif
 
 #ifndef __ASSEMBLY__
 #include <linux/sched.h>
index 296e51c..fbff9ff 100644 (file)
 void kasan_early_init(void);
 void kasan_mmu_init(void);
 void kasan_init(void);
+void kasan_late_init(void);
 #else
 static inline void kasan_init(void) { }
 static inline void kasan_mmu_init(void) { }
+static inline void kasan_late_init(void) { }
 #endif
 
 #endif /* __ASSEMBLY */
index 552b96e..60c4d82 100644 (file)
@@ -114,7 +114,12 @@ int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);
 #else
 #define VMALLOC_START ((((long)high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1)))
 #endif
+
+#ifdef CONFIG_KASAN_VMALLOC
+#define VMALLOC_END    _ALIGN_DOWN(ioremap_bot, PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
+#else
 #define VMALLOC_END    ioremap_bot
+#endif
 
 /*
  * Bits in a linux-style PTE.  These match the bits in the
index 0e6ed44..88036fb 100644 (file)
@@ -129,6 +129,31 @@ static void __init kasan_remap_early_shadow_ro(void)
        flush_tlb_kernel_range(KASAN_SHADOW_START, KASAN_SHADOW_END);
 }
 
+static void __init kasan_unmap_early_shadow_vmalloc(void)
+{
+       unsigned long k_start = (unsigned long)kasan_mem_to_shadow((void *)VMALLOC_START);
+       unsigned long k_end = (unsigned long)kasan_mem_to_shadow((void *)VMALLOC_END);
+       unsigned long k_cur;
+       phys_addr_t pa = __pa(kasan_early_shadow_page);
+
+       if (!early_mmu_has_feature(MMU_FTR_HPTE_TABLE)) {
+               int ret = kasan_init_shadow_page_tables(k_start, k_end);
+
+               if (ret)
+                       panic("kasan: kasan_init_shadow_page_tables() failed");
+       }
+       for (k_cur = k_start & PAGE_MASK; k_cur < k_end; k_cur += PAGE_SIZE) {
+               pmd_t *pmd = pmd_offset(pud_offset(pgd_offset_k(k_cur), k_cur), k_cur);
+               pte_t *ptep = pte_offset_kernel(pmd, k_cur);
+
+               if ((pte_val(*ptep) & PTE_RPN_MASK) != pa)
+                       continue;
+
+               __set_pte_at(&init_mm, k_cur, ptep, __pte(0), 0);
+       }
+       flush_tlb_kernel_range(k_start, k_end);
+}
+
 void __init kasan_mmu_init(void)
 {
        int ret;
@@ -165,7 +190,13 @@ void __init kasan_init(void)
        pr_info("KASAN init done\n");
 }
 
-#ifdef CONFIG_MODULES
+void __init kasan_late_init(void)
+{
+       if (IS_ENABLED(CONFIG_KASAN_VMALLOC))
+               kasan_unmap_early_shadow_vmalloc();
+}
+
+#if defined(CONFIG_MODULES) && !defined(CONFIG_KASAN_VMALLOC)
 void *module_alloc(unsigned long size)
 {
        void *base;
index f5535ea..ef7b111 100644 (file)
@@ -49,6 +49,7 @@
 #include <asm/fixmap.h>
 #include <asm/swiotlb.h>
 #include <asm/rtas.h>
+#include <asm/kasan.h>
 
 #include <mm/mmu_decl.h>
 
@@ -301,6 +302,9 @@ void __init mem_init(void)
 
        high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
        set_max_mapnr(max_pfn);
+
+       kasan_late_init();
+
        memblock_free_all();
 
 #ifdef CONFIG_HIGHMEM