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