Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 2 | /* |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 3 | * Copyright IBM Corp. 2006 |
| 4 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com> |
| 5 | */ |
| 6 | |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 7 | #include <linux/memblock.h> |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 8 | #include <linux/pfn.h> |
| 9 | #include <linux/mm.h> |
Paul Gortmaker | ff24b07 | 2017-02-09 15:20:24 -0500 | [diff] [blame] | 10 | #include <linux/init.h> |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 11 | #include <linux/list.h> |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 12 | #include <linux/hugetlb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Heiko Carstens | bab247f | 2016-05-10 16:28:28 +0200 | [diff] [blame] | 14 | #include <asm/cacheflush.h> |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 15 | #include <asm/pgalloc.h> |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 16 | #include <asm/setup.h> |
| 17 | #include <asm/tlbflush.h> |
Gerald Schaefer | 53492b1 | 2008-04-30 13:38:46 +0200 | [diff] [blame] | 18 | #include <asm/sections.h> |
Laura Abbott | e6c7c63 | 2017-05-08 15:58:08 -0700 | [diff] [blame] | 19 | #include <asm/set_memory.h> |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 20 | |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 21 | static DEFINE_MUTEX(vmem_mutex); |
| 22 | |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 23 | static void __ref *vmem_alloc_pages(unsigned int order) |
| 24 | { |
Heiko Carstens | 2e9996f | 2016-05-13 11:10:09 +0200 | [diff] [blame] | 25 | unsigned long size = PAGE_SIZE << order; |
| 26 | |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 27 | if (slab_is_available()) |
| 28 | return (void *)__get_free_pages(GFP_KERNEL, order); |
Mike Rapoport | 9a8dd70 | 2018-10-30 15:07:59 -0700 | [diff] [blame] | 29 | return (void *) memblock_phys_alloc(size, size); |
Heiko Carstens | 67060d9 | 2008-05-30 10:03:27 +0200 | [diff] [blame] | 30 | } |
| 31 | |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 32 | static void vmem_free_pages(unsigned long addr, int order) |
| 33 | { |
| 34 | /* We don't expect boot memory to be removed ever. */ |
| 35 | if (!slab_is_available() || |
| 36 | WARN_ON_ONCE(PageReserved(phys_to_page(addr)))) |
| 37 | return; |
| 38 | free_pages(addr, order); |
| 39 | } |
| 40 | |
Heiko Carstens | a01ef30 | 2017-06-16 17:51:15 +0200 | [diff] [blame] | 41 | void *vmem_crst_alloc(unsigned long val) |
Martin Schwidefsky | 1aea9b3 | 2017-04-24 18:19:10 +0200 | [diff] [blame] | 42 | { |
Heiko Carstens | a01ef30 | 2017-06-16 17:51:15 +0200 | [diff] [blame] | 43 | unsigned long *table; |
Martin Schwidefsky | 1aea9b3 | 2017-04-24 18:19:10 +0200 | [diff] [blame] | 44 | |
Heiko Carstens | a01ef30 | 2017-06-16 17:51:15 +0200 | [diff] [blame] | 45 | table = vmem_alloc_pages(CRST_ALLOC_ORDER); |
| 46 | if (table) |
| 47 | crst_table_init(table, val); |
| 48 | return table; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Heiko Carstens | e8a97e4 | 2016-05-17 10:50:15 +0200 | [diff] [blame] | 51 | pte_t __ref *vmem_pte_alloc(void) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 52 | { |
Heiko Carstens | 9e42736 | 2016-10-18 13:35:32 +0200 | [diff] [blame] | 53 | unsigned long size = PTRS_PER_PTE * sizeof(pte_t); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 54 | pte_t *pte; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 55 | |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 56 | if (slab_is_available()) |
Martin Schwidefsky | 527e30b | 2014-04-30 16:04:25 +0200 | [diff] [blame] | 57 | pte = (pte_t *) page_table_alloc(&init_mm); |
Martin Schwidefsky | 146e4b3 | 2008-02-09 18:24:35 +0100 | [diff] [blame] | 58 | else |
Mike Rapoport | 9a8dd70 | 2018-10-30 15:07:59 -0700 | [diff] [blame] | 59 | pte = (pte_t *) memblock_phys_alloc(size, size); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 60 | if (!pte) |
| 61 | return NULL; |
Heiko Carstens | 41879ff | 2017-10-04 19:27:07 +0200 | [diff] [blame] | 62 | memset64((u64 *)pte, _PAGE_INVALID, PTRS_PER_PTE); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 63 | return pte; |
| 64 | } |
| 65 | |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 66 | static void vmem_pte_free(unsigned long *table) |
| 67 | { |
| 68 | /* We don't expect boot memory to be removed ever. */ |
| 69 | if (!slab_is_available() || |
| 70 | WARN_ON_ONCE(PageReserved(virt_to_page(table)))) |
| 71 | return; |
| 72 | page_table_free(&init_mm, table); |
| 73 | } |
| 74 | |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 75 | #define PAGE_UNUSED 0xFD |
| 76 | |
David Hildenbrand | 2c114df | 2020-07-22 11:45:58 +0200 | [diff] [blame] | 77 | /* |
| 78 | * The unused vmemmap range, which was not yet memset(PAGE_UNUSED) ranges |
| 79 | * from unused_pmd_start to next PMD_SIZE boundary. |
| 80 | */ |
| 81 | static unsigned long unused_pmd_start; |
| 82 | |
| 83 | static void vmemmap_flush_unused_pmd(void) |
| 84 | { |
| 85 | if (!unused_pmd_start) |
| 86 | return; |
| 87 | memset(__va(unused_pmd_start), PAGE_UNUSED, |
| 88 | ALIGN(unused_pmd_start, PMD_SIZE) - unused_pmd_start); |
| 89 | unused_pmd_start = 0; |
| 90 | } |
| 91 | |
| 92 | static void __vmemmap_use_sub_pmd(unsigned long start, unsigned long end) |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 93 | { |
| 94 | /* |
| 95 | * As we expect to add in the same granularity as we remove, it's |
| 96 | * sufficient to mark only some piece used to block the memmap page from |
| 97 | * getting removed (just in case the memmap never gets initialized, |
| 98 | * e.g., because the memory block never gets onlined). |
| 99 | */ |
| 100 | memset(__va(start), 0, sizeof(struct page)); |
| 101 | } |
| 102 | |
David Hildenbrand | 2c114df | 2020-07-22 11:45:58 +0200 | [diff] [blame] | 103 | static void vmemmap_use_sub_pmd(unsigned long start, unsigned long end) |
| 104 | { |
| 105 | /* |
| 106 | * We only optimize if the new used range directly follows the |
| 107 | * previously unused range (esp., when populating consecutive sections). |
| 108 | */ |
| 109 | if (unused_pmd_start == start) { |
| 110 | unused_pmd_start = end; |
| 111 | if (likely(IS_ALIGNED(unused_pmd_start, PMD_SIZE))) |
| 112 | unused_pmd_start = 0; |
| 113 | return; |
| 114 | } |
| 115 | vmemmap_flush_unused_pmd(); |
| 116 | __vmemmap_use_sub_pmd(start, end); |
| 117 | } |
| 118 | |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 119 | static void vmemmap_use_new_sub_pmd(unsigned long start, unsigned long end) |
| 120 | { |
| 121 | void *page = __va(ALIGN_DOWN(start, PMD_SIZE)); |
| 122 | |
David Hildenbrand | 2c114df | 2020-07-22 11:45:58 +0200 | [diff] [blame] | 123 | vmemmap_flush_unused_pmd(); |
| 124 | |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 125 | /* Could be our memmap page is filled with PAGE_UNUSED already ... */ |
David Hildenbrand | 2c114df | 2020-07-22 11:45:58 +0200 | [diff] [blame] | 126 | __vmemmap_use_sub_pmd(start, end); |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 127 | |
| 128 | /* Mark the unused parts of the new memmap page PAGE_UNUSED. */ |
| 129 | if (!IS_ALIGNED(start, PMD_SIZE)) |
| 130 | memset(page, PAGE_UNUSED, start - __pa(page)); |
David Hildenbrand | 2c114df | 2020-07-22 11:45:58 +0200 | [diff] [blame] | 131 | /* |
| 132 | * We want to avoid memset(PAGE_UNUSED) when populating the vmemmap of |
| 133 | * consecutive sections. Remember for the last added PMD the last |
| 134 | * unused range in the populated PMD. |
| 135 | */ |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 136 | if (!IS_ALIGNED(end, PMD_SIZE)) |
David Hildenbrand | 2c114df | 2020-07-22 11:45:58 +0200 | [diff] [blame] | 137 | unused_pmd_start = end; |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* Returns true if the PMD is completely unused and can be freed. */ |
| 141 | static bool vmemmap_unuse_sub_pmd(unsigned long start, unsigned long end) |
| 142 | { |
| 143 | void *page = __va(ALIGN_DOWN(start, PMD_SIZE)); |
| 144 | |
David Hildenbrand | 2c114df | 2020-07-22 11:45:58 +0200 | [diff] [blame] | 145 | vmemmap_flush_unused_pmd(); |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 146 | memset(__va(start), PAGE_UNUSED, end - start); |
| 147 | return !memchr_inv(page, PAGE_UNUSED, PMD_SIZE); |
| 148 | } |
| 149 | |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 150 | /* __ref: we'll only call vmemmap_alloc_block() via vmemmap_populate() */ |
| 151 | static int __ref modify_pte_table(pmd_t *pmd, unsigned long addr, |
| 152 | unsigned long end, bool add, bool direct) |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 153 | { |
| 154 | unsigned long prot, pages = 0; |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 155 | int ret = -ENOMEM; |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 156 | pte_t *pte; |
| 157 | |
| 158 | prot = pgprot_val(PAGE_KERNEL); |
| 159 | if (!MACHINE_HAS_NX) |
| 160 | prot &= ~_PAGE_NOEXEC; |
| 161 | |
| 162 | pte = pte_offset_kernel(pmd, addr); |
| 163 | for (; addr < end; addr += PAGE_SIZE, pte++) { |
| 164 | if (!add) { |
| 165 | if (pte_none(*pte)) |
| 166 | continue; |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 167 | if (!direct) |
| 168 | vmem_free_pages(pfn_to_phys(pte_pfn(*pte)), 0); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 169 | pte_clear(&init_mm, addr, pte); |
| 170 | } else if (pte_none(*pte)) { |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 171 | if (!direct) { |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 172 | void *new_page = vmemmap_alloc_block(PAGE_SIZE, NUMA_NO_NODE); |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 173 | |
| 174 | if (!new_page) |
| 175 | goto out; |
| 176 | pte_val(*pte) = __pa(new_page) | prot; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 177 | } else { |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 178 | pte_val(*pte) = addr | prot; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 179 | } |
| 180 | } else { |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 181 | continue; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 182 | } |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 183 | pages++; |
| 184 | } |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 185 | ret = 0; |
| 186 | out: |
| 187 | if (direct) |
| 188 | update_page_count(PG_DIRECT_MAP_4K, add ? pages : -pages); |
| 189 | return ret; |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 190 | } |
| 191 | |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 192 | static void try_free_pte_table(pmd_t *pmd, unsigned long start) |
| 193 | { |
| 194 | pte_t *pte; |
| 195 | int i; |
| 196 | |
| 197 | /* We can safely assume this is fully in 1:1 mapping & vmemmap area */ |
| 198 | pte = pte_offset_kernel(pmd, start); |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 199 | for (i = 0; i < PTRS_PER_PTE; i++, pte++) { |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 200 | if (!pte_none(*pte)) |
| 201 | return; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 202 | } |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 203 | vmem_pte_free(__va(pmd_deref(*pmd))); |
| 204 | pmd_clear(pmd); |
| 205 | } |
| 206 | |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 207 | /* __ref: we'll only call vmemmap_alloc_block() via vmemmap_populate() */ |
| 208 | static int __ref modify_pmd_table(pud_t *pud, unsigned long addr, |
| 209 | unsigned long end, bool add, bool direct) |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 210 | { |
| 211 | unsigned long next, prot, pages = 0; |
| 212 | int ret = -ENOMEM; |
| 213 | pmd_t *pmd; |
| 214 | pte_t *pte; |
| 215 | |
| 216 | prot = pgprot_val(SEGMENT_KERNEL); |
| 217 | if (!MACHINE_HAS_NX) |
| 218 | prot &= ~_SEGMENT_ENTRY_NOEXEC; |
| 219 | |
| 220 | pmd = pmd_offset(pud, addr); |
| 221 | for (; addr < end; addr = next, pmd++) { |
| 222 | next = pmd_addr_end(addr, end); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 223 | if (!add) { |
| 224 | if (pmd_none(*pmd)) |
| 225 | continue; |
Alexander Gordeev | af71657 | 2020-11-10 10:36:21 +0100 | [diff] [blame^] | 226 | if (pmd_large(*pmd)) { |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 227 | if (IS_ALIGNED(addr, PMD_SIZE) && |
| 228 | IS_ALIGNED(next, PMD_SIZE)) { |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 229 | if (!direct) |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 230 | vmem_free_pages(pmd_deref(*pmd), get_order(PMD_SIZE)); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 231 | pmd_clear(pmd); |
| 232 | pages++; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 233 | } else if (!direct && vmemmap_unuse_sub_pmd(addr, next)) { |
| 234 | vmem_free_pages(pmd_deref(*pmd), get_order(PMD_SIZE)); |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 235 | pmd_clear(pmd); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 236 | } |
| 237 | continue; |
| 238 | } |
| 239 | } else if (pmd_none(*pmd)) { |
| 240 | if (IS_ALIGNED(addr, PMD_SIZE) && |
| 241 | IS_ALIGNED(next, PMD_SIZE) && |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 242 | MACHINE_HAS_EDAT1 && addr && direct && |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 243 | !debug_pagealloc_enabled()) { |
| 244 | pmd_val(*pmd) = addr | prot; |
| 245 | pages++; |
| 246 | continue; |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 247 | } else if (!direct && MACHINE_HAS_EDAT1) { |
| 248 | void *new_page; |
| 249 | |
| 250 | /* |
| 251 | * Use 1MB frames for vmemmap if available. We |
| 252 | * always use large frames even if they are only |
| 253 | * partially used. Otherwise we would have also |
| 254 | * page tables since vmemmap_populate gets |
| 255 | * called for each section separately. |
| 256 | */ |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 257 | new_page = vmemmap_alloc_block(PMD_SIZE, NUMA_NO_NODE); |
David Hildenbrand | f2057b4 | 2020-07-22 11:45:56 +0200 | [diff] [blame] | 258 | if (new_page) { |
| 259 | pmd_val(*pmd) = __pa(new_page) | prot; |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 260 | if (!IS_ALIGNED(addr, PMD_SIZE) || |
| 261 | !IS_ALIGNED(next, PMD_SIZE)) { |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 262 | vmemmap_use_new_sub_pmd(addr, next); |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 263 | } |
David Hildenbrand | f2057b4 | 2020-07-22 11:45:56 +0200 | [diff] [blame] | 264 | continue; |
| 265 | } |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 266 | } |
| 267 | pte = vmem_pte_alloc(); |
| 268 | if (!pte) |
| 269 | goto out; |
| 270 | pmd_populate(&init_mm, pmd, pte); |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 271 | } else if (pmd_large(*pmd)) { |
| 272 | if (!direct) |
| 273 | vmemmap_use_sub_pmd(addr, next); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 274 | continue; |
David Hildenbrand | cd5781d | 2020-07-22 11:45:57 +0200 | [diff] [blame] | 275 | } |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 276 | ret = modify_pte_table(pmd, addr, next, add, direct); |
| 277 | if (ret) |
| 278 | goto out; |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 279 | if (!add) |
| 280 | try_free_pte_table(pmd, addr & PMD_MASK); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 281 | } |
| 282 | ret = 0; |
| 283 | out: |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 284 | if (direct) |
| 285 | update_page_count(PG_DIRECT_MAP_1M, add ? pages : -pages); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 286 | return ret; |
| 287 | } |
| 288 | |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 289 | static void try_free_pmd_table(pud_t *pud, unsigned long start) |
| 290 | { |
| 291 | const unsigned long end = start + PUD_SIZE; |
| 292 | pmd_t *pmd; |
| 293 | int i; |
| 294 | |
| 295 | /* Don't mess with any tables not fully in 1:1 mapping & vmemmap area */ |
| 296 | if (end > VMALLOC_START) |
| 297 | return; |
| 298 | #ifdef CONFIG_KASAN |
| 299 | if (start < KASAN_SHADOW_END && KASAN_SHADOW_START > end) |
| 300 | return; |
| 301 | #endif |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 302 | pmd = pmd_offset(pud, start); |
| 303 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) |
| 304 | if (!pmd_none(*pmd)) |
| 305 | return; |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 306 | vmem_free_pages(pud_deref(*pud), CRST_ALLOC_ORDER); |
| 307 | pud_clear(pud); |
| 308 | } |
| 309 | |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 310 | static int modify_pud_table(p4d_t *p4d, unsigned long addr, unsigned long end, |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 311 | bool add, bool direct) |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 312 | { |
| 313 | unsigned long next, prot, pages = 0; |
| 314 | int ret = -ENOMEM; |
| 315 | pud_t *pud; |
| 316 | pmd_t *pmd; |
| 317 | |
| 318 | prot = pgprot_val(REGION3_KERNEL); |
| 319 | if (!MACHINE_HAS_NX) |
| 320 | prot &= ~_REGION_ENTRY_NOEXEC; |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 321 | pud = pud_offset(p4d, addr); |
| 322 | for (; addr < end; addr = next, pud++) { |
| 323 | next = pud_addr_end(addr, end); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 324 | if (!add) { |
| 325 | if (pud_none(*pud)) |
| 326 | continue; |
| 327 | if (pud_large(*pud)) { |
| 328 | if (IS_ALIGNED(addr, PUD_SIZE) && |
| 329 | IS_ALIGNED(next, PUD_SIZE)) { |
| 330 | pud_clear(pud); |
| 331 | pages++; |
| 332 | } |
| 333 | continue; |
| 334 | } |
| 335 | } else if (pud_none(*pud)) { |
| 336 | if (IS_ALIGNED(addr, PUD_SIZE) && |
| 337 | IS_ALIGNED(next, PUD_SIZE) && |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 338 | MACHINE_HAS_EDAT2 && addr && direct && |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 339 | !debug_pagealloc_enabled()) { |
| 340 | pud_val(*pud) = addr | prot; |
| 341 | pages++; |
| 342 | continue; |
| 343 | } |
| 344 | pmd = vmem_crst_alloc(_SEGMENT_ENTRY_EMPTY); |
| 345 | if (!pmd) |
| 346 | goto out; |
| 347 | pud_populate(&init_mm, pud, pmd); |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 348 | } else if (pud_large(*pud)) { |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 349 | continue; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 350 | } |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 351 | ret = modify_pmd_table(pud, addr, next, add, direct); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 352 | if (ret) |
| 353 | goto out; |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 354 | if (!add) |
| 355 | try_free_pmd_table(pud, addr & PUD_MASK); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 356 | } |
| 357 | ret = 0; |
| 358 | out: |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 359 | if (direct) |
| 360 | update_page_count(PG_DIRECT_MAP_2G, add ? pages : -pages); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 361 | return ret; |
| 362 | } |
| 363 | |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 364 | static void try_free_pud_table(p4d_t *p4d, unsigned long start) |
| 365 | { |
| 366 | const unsigned long end = start + P4D_SIZE; |
| 367 | pud_t *pud; |
| 368 | int i; |
| 369 | |
| 370 | /* Don't mess with any tables not fully in 1:1 mapping & vmemmap area */ |
| 371 | if (end > VMALLOC_START) |
| 372 | return; |
| 373 | #ifdef CONFIG_KASAN |
| 374 | if (start < KASAN_SHADOW_END && KASAN_SHADOW_START > end) |
| 375 | return; |
| 376 | #endif |
| 377 | |
| 378 | pud = pud_offset(p4d, start); |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 379 | for (i = 0; i < PTRS_PER_PUD; i++, pud++) { |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 380 | if (!pud_none(*pud)) |
| 381 | return; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 382 | } |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 383 | vmem_free_pages(p4d_deref(*p4d), CRST_ALLOC_ORDER); |
| 384 | p4d_clear(p4d); |
| 385 | } |
| 386 | |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 387 | static int modify_p4d_table(pgd_t *pgd, unsigned long addr, unsigned long end, |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 388 | bool add, bool direct) |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 389 | { |
| 390 | unsigned long next; |
| 391 | int ret = -ENOMEM; |
| 392 | p4d_t *p4d; |
| 393 | pud_t *pud; |
| 394 | |
| 395 | p4d = p4d_offset(pgd, addr); |
| 396 | for (; addr < end; addr = next, p4d++) { |
| 397 | next = p4d_addr_end(addr, end); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 398 | if (!add) { |
| 399 | if (p4d_none(*p4d)) |
| 400 | continue; |
| 401 | } else if (p4d_none(*p4d)) { |
| 402 | pud = vmem_crst_alloc(_REGION3_ENTRY_EMPTY); |
| 403 | if (!pud) |
| 404 | goto out; |
Vasily Gorbik | bffc2f7 | 2020-08-21 18:27:36 +0200 | [diff] [blame] | 405 | p4d_populate(&init_mm, p4d, pud); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 406 | } |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 407 | ret = modify_pud_table(p4d, addr, next, add, direct); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 408 | if (ret) |
| 409 | goto out; |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 410 | if (!add) |
| 411 | try_free_pud_table(p4d, addr & P4D_MASK); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 412 | } |
| 413 | ret = 0; |
| 414 | out: |
| 415 | return ret; |
| 416 | } |
| 417 | |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 418 | static void try_free_p4d_table(pgd_t *pgd, unsigned long start) |
| 419 | { |
| 420 | const unsigned long end = start + PGDIR_SIZE; |
| 421 | p4d_t *p4d; |
| 422 | int i; |
| 423 | |
| 424 | /* Don't mess with any tables not fully in 1:1 mapping & vmemmap area */ |
| 425 | if (end > VMALLOC_START) |
| 426 | return; |
| 427 | #ifdef CONFIG_KASAN |
| 428 | if (start < KASAN_SHADOW_END && KASAN_SHADOW_START > end) |
| 429 | return; |
| 430 | #endif |
| 431 | |
| 432 | p4d = p4d_offset(pgd, start); |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 433 | for (i = 0; i < PTRS_PER_P4D; i++, p4d++) { |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 434 | if (!p4d_none(*p4d)) |
| 435 | return; |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 436 | } |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 437 | vmem_free_pages(pgd_deref(*pgd), CRST_ALLOC_ORDER); |
| 438 | pgd_clear(pgd); |
| 439 | } |
| 440 | |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 441 | static int modify_pagetable(unsigned long start, unsigned long end, bool add, |
| 442 | bool direct) |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 443 | { |
| 444 | unsigned long addr, next; |
| 445 | int ret = -ENOMEM; |
| 446 | pgd_t *pgd; |
| 447 | p4d_t *p4d; |
| 448 | |
| 449 | if (WARN_ON_ONCE(!PAGE_ALIGNED(start | end))) |
| 450 | return -EINVAL; |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 451 | for (addr = start; addr < end; addr = next) { |
| 452 | next = pgd_addr_end(addr, end); |
| 453 | pgd = pgd_offset_k(addr); |
| 454 | |
| 455 | if (!add) { |
| 456 | if (pgd_none(*pgd)) |
| 457 | continue; |
| 458 | } else if (pgd_none(*pgd)) { |
| 459 | p4d = vmem_crst_alloc(_REGION2_ENTRY_EMPTY); |
| 460 | if (!p4d) |
| 461 | goto out; |
| 462 | pgd_populate(&init_mm, pgd, p4d); |
| 463 | } |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 464 | ret = modify_p4d_table(pgd, addr, next, add, direct); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 465 | if (ret) |
| 466 | goto out; |
David Hildenbrand | b9ff810 | 2020-07-22 11:45:55 +0200 | [diff] [blame] | 467 | if (!add) |
| 468 | try_free_p4d_table(pgd, addr & PGDIR_MASK); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 469 | } |
| 470 | ret = 0; |
| 471 | out: |
| 472 | if (!add) |
| 473 | flush_tlb_kernel_range(start, end); |
| 474 | return ret; |
| 475 | } |
| 476 | |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 477 | static int add_pagetable(unsigned long start, unsigned long end, bool direct) |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 478 | { |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 479 | return modify_pagetable(start, end, true, direct); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 480 | } |
| 481 | |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 482 | static int remove_pagetable(unsigned long start, unsigned long end, bool direct) |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 483 | { |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 484 | return modify_pagetable(start, end, false, direct); |
David Hildenbrand | 3e0d3e4 | 2020-07-22 11:45:51 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 487 | /* |
| 488 | * Add a physical memory range to the 1:1 mapping. |
| 489 | */ |
David Hildenbrand | 8398b22 | 2020-07-22 11:45:50 +0200 | [diff] [blame] | 490 | static int vmem_add_range(unsigned long start, unsigned long size) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 491 | { |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 492 | return add_pagetable(start, start + size, true); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Remove a physical memory range from the 1:1 mapping. |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 497 | */ |
| 498 | static void vmem_remove_range(unsigned long start, unsigned long size) |
| 499 | { |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 500 | remove_pagetable(start, start + size, true); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Add a backed mem_map array to the virtual mem_map array. |
| 505 | */ |
Christoph Hellwig | 7b73d97 | 2017-12-29 08:53:54 +0100 | [diff] [blame] | 506 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 507 | struct vmem_altmap *altmap) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 508 | { |
David Hildenbrand | c00f05a | 2020-07-22 11:45:53 +0200 | [diff] [blame] | 509 | int ret; |
| 510 | |
David Hildenbrand | aa18e0e | 2020-07-22 11:45:54 +0200 | [diff] [blame] | 511 | mutex_lock(&vmem_mutex); |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 512 | /* We don't care about the node, just use NUMA_NO_NODE on allocations */ |
David Hildenbrand | c00f05a | 2020-07-22 11:45:53 +0200 | [diff] [blame] | 513 | ret = add_pagetable(start, end, false); |
| 514 | if (ret) |
| 515 | remove_pagetable(start, end, false); |
David Hildenbrand | aa18e0e | 2020-07-22 11:45:54 +0200 | [diff] [blame] | 516 | mutex_unlock(&vmem_mutex); |
David Hildenbrand | c00f05a | 2020-07-22 11:45:53 +0200 | [diff] [blame] | 517 | return ret; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 520 | void vmemmap_free(unsigned long start, unsigned long end, |
Heiko Carstens | 9a996c6 | 2020-07-23 21:42:36 +0200 | [diff] [blame] | 521 | struct vmem_altmap *altmap) |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 522 | { |
David Hildenbrand | aa18e0e | 2020-07-22 11:45:54 +0200 | [diff] [blame] | 523 | mutex_lock(&vmem_mutex); |
David Hildenbrand | 9ec8fa8 | 2020-07-22 11:45:52 +0200 | [diff] [blame] | 524 | remove_pagetable(start, end, false); |
David Hildenbrand | aa18e0e | 2020-07-22 11:45:54 +0200 | [diff] [blame] | 525 | mutex_unlock(&vmem_mutex); |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 526 | } |
| 527 | |
David Hildenbrand | f05f62d | 2020-06-25 17:00:29 +0200 | [diff] [blame] | 528 | void vmem_remove_mapping(unsigned long start, unsigned long size) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 529 | { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 530 | mutex_lock(&vmem_mutex); |
David Hildenbrand | f05f62d | 2020-06-25 17:00:29 +0200 | [diff] [blame] | 531 | vmem_remove_range(start, size); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 532 | mutex_unlock(&vmem_mutex); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 533 | } |
| 534 | |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 535 | int vmem_add_mapping(unsigned long start, unsigned long size) |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 536 | { |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 537 | int ret; |
| 538 | |
David Hildenbrand | f05f62d | 2020-06-25 17:00:29 +0200 | [diff] [blame] | 539 | if (start + size > VMEM_MAX_PHYS || |
| 540 | start + size < start) |
| 541 | return -ERANGE; |
| 542 | |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 543 | mutex_lock(&vmem_mutex); |
David Hildenbrand | 8398b22 | 2020-07-22 11:45:50 +0200 | [diff] [blame] | 544 | ret = vmem_add_range(start, size); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 545 | if (ret) |
David Hildenbrand | f05f62d | 2020-06-25 17:00:29 +0200 | [diff] [blame] | 546 | vmem_remove_range(start, size); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 547 | mutex_unlock(&vmem_mutex); |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * map whole physical memory to virtual memory (identity mapping) |
Christian Borntraeger | 5fd9c6e | 2008-01-26 14:11:00 +0100 | [diff] [blame] | 553 | * we reserve enough space in the vmalloc area for vmemmap to hotplug |
| 554 | * additional memory segments. |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 555 | */ |
| 556 | void __init vmem_map_init(void) |
| 557 | { |
Mike Rapoport | b10d6bc | 2020-10-13 16:58:08 -0700 | [diff] [blame] | 558 | phys_addr_t base, end; |
| 559 | u64 i; |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 560 | |
Mike Rapoport | b10d6bc | 2020-10-13 16:58:08 -0700 | [diff] [blame] | 561 | for_each_mem_range(i, &base, &end) |
| 562 | vmem_add_range(base, end - base); |
Heiko Carstens | ead7a22 | 2017-11-08 11:18:29 +0100 | [diff] [blame] | 563 | __set_memory((unsigned long)_stext, |
| 564 | (unsigned long)(_etext - _stext) >> PAGE_SHIFT, |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 565 | SET_MEMORY_RO | SET_MEMORY_X); |
Heiko Carstens | ead7a22 | 2017-11-08 11:18:29 +0100 | [diff] [blame] | 566 | __set_memory((unsigned long)_etext, |
| 567 | (unsigned long)(__end_rodata - _etext) >> PAGE_SHIFT, |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 568 | SET_MEMORY_RO); |
Heiko Carstens | ead7a22 | 2017-11-08 11:18:29 +0100 | [diff] [blame] | 569 | __set_memory((unsigned long)_sinittext, |
| 570 | (unsigned long)(_einittext - _sinittext) >> PAGE_SHIFT, |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 571 | SET_MEMORY_RO | SET_MEMORY_X); |
Gerald Schaefer | a80313f | 2019-02-03 21:37:20 +0100 | [diff] [blame] | 572 | __set_memory(__stext_dma, (__etext_dma - __stext_dma) >> PAGE_SHIFT, |
| 573 | SET_MEMORY_RO | SET_MEMORY_X); |
Sven Schnelle | 0b38b5e | 2020-01-22 13:38:22 +0100 | [diff] [blame] | 574 | |
| 575 | /* we need lowcore executable for our LPSWE instructions */ |
| 576 | set_memory_x(0, 1); |
| 577 | |
Martin Schwidefsky | 57d7f93 | 2016-03-22 10:54:24 +0100 | [diff] [blame] | 578 | pr_info("Write protected kernel read-only data: %luk\n", |
Heiko Carstens | ead7a22 | 2017-11-08 11:18:29 +0100 | [diff] [blame] | 579 | (unsigned long)(__end_rodata - _stext) >> 10); |
Heiko Carstens | f4eb07c | 2006-12-08 15:56:07 +0100 | [diff] [blame] | 580 | } |