Andrey Konovalov | e886bf9 | 2018-12-28 00:31:14 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 2 | /* |
Andrey Konovalov | b266e8f | 2020-12-22 12:00:28 -0800 | [diff] [blame] | 3 | * This file contains KASAN shadow initialization code. |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2015 Samsung Electronics Co., Ltd. |
| 6 | * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 7 | */ |
| 8 | |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 9 | #include <linux/memblock.h> |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/kasan.h> |
| 12 | #include <linux/kernel.h> |
Laura Abbott | 5c6a84a | 2017-01-10 13:35:44 -0800 | [diff] [blame] | 13 | #include <linux/mm.h> |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 14 | #include <linux/pfn.h> |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 16 | |
| 17 | #include <asm/page.h> |
| 18 | #include <asm/pgalloc.h> |
| 19 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 20 | #include "kasan.h" |
| 21 | |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 22 | /* |
| 23 | * This page serves two purposes: |
| 24 | * - It used as early shadow memory. The entire shadow region populated |
| 25 | * with this page, before we will be able to setup normal shadow memory. |
| 26 | * - Latter it reused it as zero shadow to cover large ranges of memory |
| 27 | * that allowed to access, but not handled by kasan (vmalloc/vmemmap ...). |
| 28 | */ |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 29 | unsigned char kasan_early_shadow_page[PAGE_SIZE] __page_aligned_bss; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 30 | |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 31 | #if CONFIG_PGTABLE_LEVELS > 4 |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 32 | p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D] __page_aligned_bss; |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 33 | static inline bool kasan_p4d_table(pgd_t pgd) |
| 34 | { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 35 | return pgd_page(pgd) == virt_to_page(lm_alias(kasan_early_shadow_p4d)); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 36 | } |
| 37 | #else |
| 38 | static inline bool kasan_p4d_table(pgd_t pgd) |
| 39 | { |
Andrey Konovalov | 5c0198b | 2019-03-05 15:41:31 -0800 | [diff] [blame] | 40 | return false; |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 41 | } |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 42 | #endif |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 43 | #if CONFIG_PGTABLE_LEVELS > 3 |
Daniel Axtens | cb32c9c | 2021-06-28 19:40:49 -0700 | [diff] [blame] | 44 | pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss; |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 45 | static inline bool kasan_pud_table(p4d_t p4d) |
| 46 | { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 47 | return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud)); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 48 | } |
| 49 | #else |
| 50 | static inline bool kasan_pud_table(p4d_t p4d) |
| 51 | { |
Andrey Konovalov | 5c0198b | 2019-03-05 15:41:31 -0800 | [diff] [blame] | 52 | return false; |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 53 | } |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 54 | #endif |
| 55 | #if CONFIG_PGTABLE_LEVELS > 2 |
Daniel Axtens | cb32c9c | 2021-06-28 19:40:49 -0700 | [diff] [blame] | 56 | pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss; |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 57 | static inline bool kasan_pmd_table(pud_t pud) |
| 58 | { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 59 | return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd)); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 60 | } |
| 61 | #else |
| 62 | static inline bool kasan_pmd_table(pud_t pud) |
| 63 | { |
Andrey Konovalov | 5c0198b | 2019-03-05 15:41:31 -0800 | [diff] [blame] | 64 | return false; |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 65 | } |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 66 | #endif |
Daniel Axtens | cb32c9c | 2021-06-28 19:40:49 -0700 | [diff] [blame] | 67 | pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS] |
Hailong Liu | 29970dc | 2021-01-12 15:49:14 -0800 | [diff] [blame] | 68 | __page_aligned_bss; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 69 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 70 | static inline bool kasan_pte_table(pmd_t pmd) |
| 71 | { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 72 | return pmd_page(pmd) == virt_to_page(lm_alias(kasan_early_shadow_pte)); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 75 | static inline bool kasan_early_shadow_page_entry(pte_t pte) |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 76 | { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 77 | return pte_page(pte) == virt_to_page(lm_alias(kasan_early_shadow_page)); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 80 | static __init void *early_alloc(size_t size, int node) |
| 81 | { |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 82 | void *ptr = memblock_alloc_try_nid(size, size, __pa(MAX_DMA_ADDRESS), |
| 83 | MEMBLOCK_ALLOC_ACCESSIBLE, node); |
| 84 | |
| 85 | if (!ptr) |
| 86 | panic("%s: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n", |
| 87 | __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS)); |
| 88 | |
| 89 | return ptr; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 90 | } |
| 91 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 92 | static void __ref zero_pte_populate(pmd_t *pmd, unsigned long addr, |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 93 | unsigned long end) |
| 94 | { |
| 95 | pte_t *pte = pte_offset_kernel(pmd, addr); |
| 96 | pte_t zero_pte; |
| 97 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 98 | zero_pte = pfn_pte(PFN_DOWN(__pa_symbol(kasan_early_shadow_page)), |
| 99 | PAGE_KERNEL); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 100 | zero_pte = pte_wrprotect(zero_pte); |
| 101 | |
| 102 | while (addr + PAGE_SIZE <= end) { |
| 103 | set_pte_at(&init_mm, addr, pte, zero_pte); |
| 104 | addr += PAGE_SIZE; |
| 105 | pte = pte_offset_kernel(pmd, addr); |
| 106 | } |
| 107 | } |
| 108 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 109 | static int __ref zero_pmd_populate(pud_t *pud, unsigned long addr, |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 110 | unsigned long end) |
| 111 | { |
| 112 | pmd_t *pmd = pmd_offset(pud, addr); |
| 113 | unsigned long next; |
| 114 | |
| 115 | do { |
| 116 | next = pmd_addr_end(addr, end); |
| 117 | |
| 118 | if (IS_ALIGNED(addr, PMD_SIZE) && end - addr >= PMD_SIZE) { |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 119 | pmd_populate_kernel(&init_mm, pmd, |
| 120 | lm_alias(kasan_early_shadow_pte)); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | |
| 124 | if (pmd_none(*pmd)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 125 | pte_t *p; |
| 126 | |
| 127 | if (slab_is_available()) |
Joel Fernandes (Google) | 4cf5892 | 2019-01-03 15:28:34 -0800 | [diff] [blame] | 128 | p = pte_alloc_one_kernel(&init_mm); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 129 | else |
| 130 | p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); |
| 131 | if (!p) |
| 132 | return -ENOMEM; |
| 133 | |
| 134 | pmd_populate_kernel(&init_mm, pmd, p); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 135 | } |
| 136 | zero_pte_populate(pmd, addr, next); |
| 137 | } while (pmd++, addr = next, addr != end); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 138 | |
| 139 | return 0; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 140 | } |
| 141 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 142 | static int __ref zero_pud_populate(p4d_t *p4d, unsigned long addr, |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 143 | unsigned long end) |
| 144 | { |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 145 | pud_t *pud = pud_offset(p4d, addr); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 146 | unsigned long next; |
| 147 | |
| 148 | do { |
| 149 | next = pud_addr_end(addr, end); |
| 150 | if (IS_ALIGNED(addr, PUD_SIZE) && end - addr >= PUD_SIZE) { |
| 151 | pmd_t *pmd; |
| 152 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 153 | pud_populate(&init_mm, pud, |
| 154 | lm_alias(kasan_early_shadow_pmd)); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 155 | pmd = pmd_offset(pud, addr); |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 156 | pmd_populate_kernel(&init_mm, pmd, |
| 157 | lm_alias(kasan_early_shadow_pte)); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 158 | continue; |
| 159 | } |
| 160 | |
| 161 | if (pud_none(*pud)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 162 | pmd_t *p; |
| 163 | |
| 164 | if (slab_is_available()) { |
| 165 | p = pmd_alloc(&init_mm, pud, addr); |
| 166 | if (!p) |
| 167 | return -ENOMEM; |
| 168 | } else { |
| 169 | pud_populate(&init_mm, pud, |
| 170 | early_alloc(PAGE_SIZE, NUMA_NO_NODE)); |
| 171 | } |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 172 | } |
| 173 | zero_pmd_populate(pud, addr, next); |
| 174 | } while (pud++, addr = next, addr != end); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 175 | |
| 176 | return 0; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 177 | } |
| 178 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 179 | static int __ref zero_p4d_populate(pgd_t *pgd, unsigned long addr, |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 180 | unsigned long end) |
| 181 | { |
| 182 | p4d_t *p4d = p4d_offset(pgd, addr); |
| 183 | unsigned long next; |
| 184 | |
| 185 | do { |
| 186 | next = p4d_addr_end(addr, end); |
Joonsoo Kim | 458f792 | 2017-07-10 15:50:21 -0700 | [diff] [blame] | 187 | if (IS_ALIGNED(addr, P4D_SIZE) && end - addr >= P4D_SIZE) { |
| 188 | pud_t *pud; |
| 189 | pmd_t *pmd; |
| 190 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 191 | p4d_populate(&init_mm, p4d, |
| 192 | lm_alias(kasan_early_shadow_pud)); |
Joonsoo Kim | 458f792 | 2017-07-10 15:50:21 -0700 | [diff] [blame] | 193 | pud = pud_offset(p4d, addr); |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 194 | pud_populate(&init_mm, pud, |
| 195 | lm_alias(kasan_early_shadow_pmd)); |
Joonsoo Kim | 458f792 | 2017-07-10 15:50:21 -0700 | [diff] [blame] | 196 | pmd = pmd_offset(pud, addr); |
| 197 | pmd_populate_kernel(&init_mm, pmd, |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 198 | lm_alias(kasan_early_shadow_pte)); |
Joonsoo Kim | 458f792 | 2017-07-10 15:50:21 -0700 | [diff] [blame] | 199 | continue; |
| 200 | } |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 201 | |
| 202 | if (p4d_none(*p4d)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 203 | pud_t *p; |
| 204 | |
| 205 | if (slab_is_available()) { |
| 206 | p = pud_alloc(&init_mm, p4d, addr); |
| 207 | if (!p) |
| 208 | return -ENOMEM; |
| 209 | } else { |
| 210 | p4d_populate(&init_mm, p4d, |
| 211 | early_alloc(PAGE_SIZE, NUMA_NO_NODE)); |
| 212 | } |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 213 | } |
| 214 | zero_pud_populate(p4d, addr, next); |
| 215 | } while (p4d++, addr = next, addr != end); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 216 | |
| 217 | return 0; |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 218 | } |
| 219 | |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 220 | /** |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 221 | * kasan_populate_early_shadow - populate shadow memory region with |
| 222 | * kasan_early_shadow_page |
Yu Kuai | 7b6889f | 2021-06-04 20:01:33 -0700 | [diff] [blame] | 223 | * @shadow_start: start of the memory range to populate |
| 224 | * @shadow_end: end of the memory range to populate |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 225 | */ |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 226 | int __ref kasan_populate_early_shadow(const void *shadow_start, |
| 227 | const void *shadow_end) |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 228 | { |
| 229 | unsigned long addr = (unsigned long)shadow_start; |
| 230 | unsigned long end = (unsigned long)shadow_end; |
| 231 | pgd_t *pgd = pgd_offset_k(addr); |
| 232 | unsigned long next; |
| 233 | |
| 234 | do { |
| 235 | next = pgd_addr_end(addr, end); |
| 236 | |
| 237 | if (IS_ALIGNED(addr, PGDIR_SIZE) && end - addr >= PGDIR_SIZE) { |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 238 | p4d_t *p4d; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 239 | pud_t *pud; |
| 240 | pmd_t *pmd; |
| 241 | |
| 242 | /* |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 243 | * kasan_early_shadow_pud should be populated with pmds |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 244 | * at this moment. |
| 245 | * [pud,pmd]_populate*() below needed only for |
| 246 | * 3,2 - level page tables where we don't have |
| 247 | * puds,pmds, so pgd_populate(), pud_populate() |
| 248 | * is noops. |
| 249 | */ |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 250 | pgd_populate(&init_mm, pgd, |
| 251 | lm_alias(kasan_early_shadow_p4d)); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 252 | p4d = p4d_offset(pgd, addr); |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 253 | p4d_populate(&init_mm, p4d, |
| 254 | lm_alias(kasan_early_shadow_pud)); |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 255 | pud = pud_offset(p4d, addr); |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 256 | pud_populate(&init_mm, pud, |
| 257 | lm_alias(kasan_early_shadow_pmd)); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 258 | pmd = pmd_offset(pud, addr); |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 259 | pmd_populate_kernel(&init_mm, pmd, |
| 260 | lm_alias(kasan_early_shadow_pte)); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 261 | continue; |
| 262 | } |
| 263 | |
| 264 | if (pgd_none(*pgd)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 265 | p4d_t *p; |
| 266 | |
| 267 | if (slab_is_available()) { |
| 268 | p = p4d_alloc(&init_mm, pgd, addr); |
| 269 | if (!p) |
| 270 | return -ENOMEM; |
| 271 | } else { |
| 272 | pgd_populate(&init_mm, pgd, |
| 273 | early_alloc(PAGE_SIZE, NUMA_NO_NODE)); |
| 274 | } |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 275 | } |
Kirill A. Shutemov | c2febaf | 2017-03-09 17:24:07 +0300 | [diff] [blame] | 276 | zero_p4d_populate(pgd, addr, next); |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 277 | } while (pgd++, addr = next, addr != end); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static void kasan_free_pte(pte_t *pte_start, pmd_t *pmd) |
| 283 | { |
| 284 | pte_t *pte; |
| 285 | int i; |
| 286 | |
| 287 | for (i = 0; i < PTRS_PER_PTE; i++) { |
| 288 | pte = pte_start + i; |
| 289 | if (!pte_none(*pte)) |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | pte_free_kernel(&init_mm, (pte_t *)page_to_virt(pmd_page(*pmd))); |
| 294 | pmd_clear(pmd); |
| 295 | } |
| 296 | |
| 297 | static void kasan_free_pmd(pmd_t *pmd_start, pud_t *pud) |
| 298 | { |
| 299 | pmd_t *pmd; |
| 300 | int i; |
| 301 | |
| 302 | for (i = 0; i < PTRS_PER_PMD; i++) { |
| 303 | pmd = pmd_start + i; |
| 304 | if (!pmd_none(*pmd)) |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | pmd_free(&init_mm, (pmd_t *)page_to_virt(pud_page(*pud))); |
| 309 | pud_clear(pud); |
| 310 | } |
| 311 | |
| 312 | static void kasan_free_pud(pud_t *pud_start, p4d_t *p4d) |
| 313 | { |
| 314 | pud_t *pud; |
| 315 | int i; |
| 316 | |
| 317 | for (i = 0; i < PTRS_PER_PUD; i++) { |
| 318 | pud = pud_start + i; |
| 319 | if (!pud_none(*pud)) |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | pud_free(&init_mm, (pud_t *)page_to_virt(p4d_page(*p4d))); |
| 324 | p4d_clear(p4d); |
| 325 | } |
| 326 | |
| 327 | static void kasan_free_p4d(p4d_t *p4d_start, pgd_t *pgd) |
| 328 | { |
| 329 | p4d_t *p4d; |
| 330 | int i; |
| 331 | |
| 332 | for (i = 0; i < PTRS_PER_P4D; i++) { |
| 333 | p4d = p4d_start + i; |
| 334 | if (!p4d_none(*p4d)) |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | p4d_free(&init_mm, (p4d_t *)page_to_virt(pgd_page(*pgd))); |
| 339 | pgd_clear(pgd); |
| 340 | } |
| 341 | |
| 342 | static void kasan_remove_pte_table(pte_t *pte, unsigned long addr, |
| 343 | unsigned long end) |
| 344 | { |
| 345 | unsigned long next; |
| 346 | |
| 347 | for (; addr < end; addr = next, pte++) { |
| 348 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 349 | if (next > end) |
| 350 | next = end; |
| 351 | |
| 352 | if (!pte_present(*pte)) |
| 353 | continue; |
| 354 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 355 | if (WARN_ON(!kasan_early_shadow_page_entry(*pte))) |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 356 | continue; |
| 357 | pte_clear(&init_mm, addr, pte); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void kasan_remove_pmd_table(pmd_t *pmd, unsigned long addr, |
| 362 | unsigned long end) |
| 363 | { |
| 364 | unsigned long next; |
| 365 | |
| 366 | for (; addr < end; addr = next, pmd++) { |
| 367 | pte_t *pte; |
| 368 | |
| 369 | next = pmd_addr_end(addr, end); |
| 370 | |
| 371 | if (!pmd_present(*pmd)) |
| 372 | continue; |
| 373 | |
| 374 | if (kasan_pte_table(*pmd)) { |
| 375 | if (IS_ALIGNED(addr, PMD_SIZE) && |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 376 | IS_ALIGNED(next, PMD_SIZE)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 377 | pmd_clear(pmd); |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 378 | continue; |
| 379 | } |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 380 | } |
| 381 | pte = pte_offset_kernel(pmd, addr); |
| 382 | kasan_remove_pte_table(pte, addr, next); |
| 383 | kasan_free_pte(pte_offset_kernel(pmd, 0), pmd); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void kasan_remove_pud_table(pud_t *pud, unsigned long addr, |
| 388 | unsigned long end) |
| 389 | { |
| 390 | unsigned long next; |
| 391 | |
| 392 | for (; addr < end; addr = next, pud++) { |
| 393 | pmd_t *pmd, *pmd_base; |
| 394 | |
| 395 | next = pud_addr_end(addr, end); |
| 396 | |
| 397 | if (!pud_present(*pud)) |
| 398 | continue; |
| 399 | |
| 400 | if (kasan_pmd_table(*pud)) { |
| 401 | if (IS_ALIGNED(addr, PUD_SIZE) && |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 402 | IS_ALIGNED(next, PUD_SIZE)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 403 | pud_clear(pud); |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 404 | continue; |
| 405 | } |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 406 | } |
| 407 | pmd = pmd_offset(pud, addr); |
| 408 | pmd_base = pmd_offset(pud, 0); |
| 409 | kasan_remove_pmd_table(pmd, addr, next); |
| 410 | kasan_free_pmd(pmd_base, pud); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | static void kasan_remove_p4d_table(p4d_t *p4d, unsigned long addr, |
| 415 | unsigned long end) |
| 416 | { |
| 417 | unsigned long next; |
| 418 | |
| 419 | for (; addr < end; addr = next, p4d++) { |
| 420 | pud_t *pud; |
| 421 | |
| 422 | next = p4d_addr_end(addr, end); |
| 423 | |
| 424 | if (!p4d_present(*p4d)) |
| 425 | continue; |
| 426 | |
| 427 | if (kasan_pud_table(*p4d)) { |
| 428 | if (IS_ALIGNED(addr, P4D_SIZE) && |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 429 | IS_ALIGNED(next, P4D_SIZE)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 430 | p4d_clear(p4d); |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 431 | continue; |
| 432 | } |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 433 | } |
| 434 | pud = pud_offset(p4d, addr); |
| 435 | kasan_remove_pud_table(pud, addr, next); |
| 436 | kasan_free_pud(pud_offset(p4d, 0), p4d); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void kasan_remove_zero_shadow(void *start, unsigned long size) |
| 441 | { |
| 442 | unsigned long addr, end, next; |
| 443 | pgd_t *pgd; |
| 444 | |
| 445 | addr = (unsigned long)kasan_mem_to_shadow(start); |
| 446 | end = addr + (size >> KASAN_SHADOW_SCALE_SHIFT); |
| 447 | |
Andrey Konovalov | affc3f0 | 2020-12-22 12:00:35 -0800 | [diff] [blame] | 448 | if (WARN_ON((unsigned long)start % KASAN_MEMORY_PER_SHADOW_PAGE) || |
| 449 | WARN_ON(size % KASAN_MEMORY_PER_SHADOW_PAGE)) |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 450 | return; |
| 451 | |
| 452 | for (; addr < end; addr = next) { |
| 453 | p4d_t *p4d; |
| 454 | |
| 455 | next = pgd_addr_end(addr, end); |
| 456 | |
| 457 | pgd = pgd_offset_k(addr); |
| 458 | if (!pgd_present(*pgd)) |
| 459 | continue; |
| 460 | |
| 461 | if (kasan_p4d_table(*pgd)) { |
| 462 | if (IS_ALIGNED(addr, PGDIR_SIZE) && |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 463 | IS_ALIGNED(next, PGDIR_SIZE)) { |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 464 | pgd_clear(pgd); |
Lecopzer Chen | a11a496 | 2021-01-23 21:01:25 -0800 | [diff] [blame] | 465 | continue; |
| 466 | } |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | p4d = p4d_offset(pgd, addr); |
| 470 | kasan_remove_p4d_table(p4d, addr, next); |
| 471 | kasan_free_p4d(p4d_offset(pgd, 0), pgd); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | int kasan_add_zero_shadow(void *start, unsigned long size) |
| 476 | { |
| 477 | int ret; |
| 478 | void *shadow_start, *shadow_end; |
| 479 | |
| 480 | shadow_start = kasan_mem_to_shadow(start); |
| 481 | shadow_end = shadow_start + (size >> KASAN_SHADOW_SCALE_SHIFT); |
| 482 | |
Andrey Konovalov | affc3f0 | 2020-12-22 12:00:35 -0800 | [diff] [blame] | 483 | if (WARN_ON((unsigned long)start % KASAN_MEMORY_PER_SHADOW_PAGE) || |
| 484 | WARN_ON(size % KASAN_MEMORY_PER_SHADOW_PAGE)) |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 485 | return -EINVAL; |
| 486 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 487 | ret = kasan_populate_early_shadow(shadow_start, shadow_end); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 488 | if (ret) |
Lecopzer Chen | 5dabd17 | 2021-01-23 21:01:29 -0800 | [diff] [blame] | 489 | kasan_remove_zero_shadow(start, size); |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 490 | return ret; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 491 | } |