Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/x86_64/mm/init.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 5 | * Copyright (C) 2000 Pavel Machek <pavel@ucw.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de> |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/signal.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/ptrace.h> |
| 16 | #include <linux/mman.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/swap.h> |
| 19 | #include <linux/smp.h> |
| 20 | #include <linux/init.h> |
Thomas Gleixner | 11034d5 | 2008-05-12 15:43:36 +0200 | [diff] [blame] | 21 | #include <linux/initrd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/bootmem.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 24 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/proc_fs.h> |
Andi Kleen | 5917089 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 26 | #include <linux/pci.h> |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 27 | #include <linux/pfn.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 28 | #include <linux/poison.h> |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 29 | #include <linux/dma-mapping.h> |
Benjamin Herrenschmidt | a63fdc5 | 2011-06-14 10:57:50 +1000 | [diff] [blame] | 30 | #include <linux/memory.h> |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 31 | #include <linux/memory_hotplug.h> |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 32 | #include <linux/memremap.h> |
Konrad Rzeszutek | ae32b12 | 2007-05-02 19:27:11 +0200 | [diff] [blame] | 33 | #include <linux/nmi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/gfp.h> |
David Howells | 2f96b8c | 2013-04-12 00:10:25 +0100 | [diff] [blame] | 35 | #include <linux/kcore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <asm/processor.h> |
Ingo Molnar | 46eaa67 | 2008-10-12 15:06:29 +0200 | [diff] [blame] | 38 | #include <asm/bios_ebda.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 39 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/pgtable.h> |
| 41 | #include <asm/pgalloc.h> |
| 42 | #include <asm/dma.h> |
| 43 | #include <asm/fixmap.h> |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 44 | #include <asm/e820/api.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <asm/apic.h> |
| 46 | #include <asm/tlb.h> |
| 47 | #include <asm/mmu_context.h> |
| 48 | #include <asm/proto.h> |
| 49 | #include <asm/smp.h> |
Andi Kleen | 2bc0414 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 50 | #include <asm/sections.h> |
Thomas Gleixner | 718fc13 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 51 | #include <asm/kdebug.h> |
Thomas Gleixner | aaa64e0 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 52 | #include <asm/numa.h> |
Laura Abbott | d116365 | 2017-05-08 15:58:11 -0700 | [diff] [blame] | 53 | #include <asm/set_memory.h> |
Pekka Enberg | 4fcb208 | 2009-03-05 14:55:08 +0200 | [diff] [blame] | 54 | #include <asm/init.h> |
Seth Jennings | 43c75f9 | 2015-11-30 10:47:43 -0600 | [diff] [blame] | 55 | #include <asm/uv/uv.h> |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 56 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Yinghai Lu | 5c51bdb | 2012-11-16 19:39:01 -0800 | [diff] [blame] | 58 | #include "mm_internal.h" |
| 59 | |
Yinghai Lu | cf4fb15 | 2016-05-06 15:01:34 -0700 | [diff] [blame] | 60 | #include "ident_map.c" |
Yinghai Lu | aece278 | 2013-01-24 12:19:48 -0800 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
| 63 | * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the |
| 64 | * physical space so we can cache the place of the first one and move |
| 65 | * around without checking the pgd every time. |
| 66 | */ |
| 67 | |
David Vrabel | f955371 | 2014-01-07 17:03:06 +0000 | [diff] [blame] | 68 | pteval_t __supported_pte_mask __read_mostly = ~0; |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(__supported_pte_mask); |
| 70 | |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 71 | int force_personality32; |
| 72 | |
Ingo Molnar | deed05b | 2008-09-05 10:23:26 +0200 | [diff] [blame] | 73 | /* |
| 74 | * noexec32=on|off |
| 75 | * Control non executable heap for 32bit processes. |
| 76 | * To control the stack too use noexec=off |
| 77 | * |
| 78 | * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default) |
| 79 | * off PROT_READ implies PROT_EXEC |
| 80 | */ |
Yinghai Lu | bd220a2 | 2008-09-05 00:58:28 -0700 | [diff] [blame] | 81 | static int __init nonx32_setup(char *str) |
| 82 | { |
| 83 | if (!strcmp(str, "on")) |
| 84 | force_personality32 &= ~READ_IMPLIES_EXEC; |
| 85 | else if (!strcmp(str, "off")) |
| 86 | force_personality32 |= READ_IMPLIES_EXEC; |
| 87 | return 1; |
| 88 | } |
| 89 | __setup("noexec32=", nonx32_setup); |
| 90 | |
Kirill A. Shutemov | 91f606a | 2018-02-14 21:25:41 +0300 | [diff] [blame^] | 91 | static void sync_global_pgds_l5(unsigned long start, unsigned long end) |
Kirill A. Shutemov | 141efad | 2017-06-06 14:31:29 +0300 | [diff] [blame] | 92 | { |
| 93 | unsigned long addr; |
| 94 | |
| 95 | for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) { |
| 96 | const pgd_t *pgd_ref = pgd_offset_k(addr); |
| 97 | struct page *page; |
| 98 | |
| 99 | /* Check for overflow */ |
| 100 | if (addr < start) |
| 101 | break; |
| 102 | |
| 103 | if (pgd_none(*pgd_ref)) |
| 104 | continue; |
| 105 | |
| 106 | spin_lock(&pgd_lock); |
| 107 | list_for_each_entry(page, &pgd_list, lru) { |
| 108 | pgd_t *pgd; |
| 109 | spinlock_t *pgt_lock; |
| 110 | |
| 111 | pgd = (pgd_t *)page_address(page) + pgd_index(addr); |
| 112 | /* the pgt_lock only for Xen */ |
| 113 | pgt_lock = &pgd_page_get_mm(page)->page_table_lock; |
| 114 | spin_lock(pgt_lock); |
| 115 | |
| 116 | if (!pgd_none(*pgd_ref) && !pgd_none(*pgd)) |
| 117 | BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref)); |
| 118 | |
| 119 | if (pgd_none(*pgd)) |
| 120 | set_pgd(pgd, *pgd_ref); |
| 121 | |
| 122 | spin_unlock(pgt_lock); |
| 123 | } |
| 124 | spin_unlock(&pgd_lock); |
| 125 | } |
| 126 | } |
Kirill A. Shutemov | 91f606a | 2018-02-14 21:25:41 +0300 | [diff] [blame^] | 127 | |
| 128 | static void sync_global_pgds_l4(unsigned long start, unsigned long end) |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 129 | { |
Baoquan He | fc5f9d5 | 2017-05-04 10:25:47 +0800 | [diff] [blame] | 130 | unsigned long addr; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 131 | |
Baoquan He | fc5f9d5 | 2017-05-04 10:25:47 +0800 | [diff] [blame] | 132 | for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) { |
| 133 | pgd_t *pgd_ref = pgd_offset_k(addr); |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 134 | const p4d_t *p4d_ref; |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 135 | struct page *page; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 136 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 137 | /* |
| 138 | * With folded p4d, pgd_none() is always false, we need to |
| 139 | * handle synchonization on p4d level. |
| 140 | */ |
Kirill A. Shutemov | c65e774 | 2018-02-14 14:16:53 +0300 | [diff] [blame] | 141 | MAYBE_BUILD_BUG_ON(pgd_none(*pgd_ref)); |
Baoquan He | fc5f9d5 | 2017-05-04 10:25:47 +0800 | [diff] [blame] | 142 | p4d_ref = p4d_offset(pgd_ref, addr); |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 143 | |
| 144 | if (p4d_none(*p4d_ref)) |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 145 | continue; |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 146 | |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 147 | spin_lock(&pgd_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 148 | list_for_each_entry(page, &pgd_list, lru) { |
Linus Torvalds | be354f4 | 2012-12-15 12:29:54 -0800 | [diff] [blame] | 149 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 150 | p4d_t *p4d; |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 151 | spinlock_t *pgt_lock; |
| 152 | |
Baoquan He | fc5f9d5 | 2017-05-04 10:25:47 +0800 | [diff] [blame] | 153 | pgd = (pgd_t *)page_address(page) + pgd_index(addr); |
| 154 | p4d = p4d_offset(pgd, addr); |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 155 | /* the pgt_lock only for Xen */ |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 156 | pgt_lock = &pgd_page_get_mm(page)->page_table_lock; |
| 157 | spin_lock(pgt_lock); |
| 158 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 159 | if (!p4d_none(*p4d_ref) && !p4d_none(*p4d)) |
| 160 | BUG_ON(p4d_page_vaddr(*p4d) |
| 161 | != p4d_page_vaddr(*p4d_ref)); |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 162 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 163 | if (p4d_none(*p4d)) |
| 164 | set_p4d(p4d, *p4d_ref); |
Yasuaki Ishimatsu | 9661d5b | 2014-08-22 13:27:34 -0700 | [diff] [blame] | 165 | |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 166 | spin_unlock(pgt_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 167 | } |
Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 168 | spin_unlock(&pgd_lock); |
Jeremy Fitzhardinge | 44235dc | 2010-10-14 17:04:59 -0700 | [diff] [blame] | 169 | } |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 170 | } |
Kirill A. Shutemov | 91f606a | 2018-02-14 21:25:41 +0300 | [diff] [blame^] | 171 | |
| 172 | /* |
| 173 | * When memory was added make sure all the processes MM have |
| 174 | * suitable PGD entries in the local PGD level page. |
| 175 | */ |
| 176 | void sync_global_pgds(unsigned long start, unsigned long end) |
| 177 | { |
| 178 | if (pgtable_l5_enabled) |
| 179 | sync_global_pgds_l5(start, end); |
| 180 | else |
| 181 | sync_global_pgds_l4(start, end); |
| 182 | } |
Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 183 | |
| 184 | /* |
Marcin Slusarz | 8d6ea967 | 2008-08-15 18:32:24 +0200 | [diff] [blame] | 185 | * NOTE: This function is marked __ref because it calls __init function |
| 186 | * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0. |
| 187 | */ |
| 188 | static __ref void *spp_getpage(void) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 189 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | void *ptr; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (after_bootmem) |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 193 | ptr = (void *) get_zeroed_page(GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | else |
| 195 | ptr = alloc_bootmem_pages(PAGE_SIZE); |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 196 | |
| 197 | if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) { |
| 198 | panic("set_pte_phys: cannot allocate page data %s\n", |
| 199 | after_bootmem ? "after bootmem" : ""); |
| 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 202 | pr_debug("spp_getpage %p\n", ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 204 | return ptr; |
| 205 | } |
| 206 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 207 | static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 208 | { |
| 209 | if (pgd_none(*pgd)) { |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 210 | p4d_t *p4d = (p4d_t *)spp_getpage(); |
| 211 | pgd_populate(&init_mm, pgd, p4d); |
| 212 | if (p4d != p4d_offset(pgd, 0)) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 213 | printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n", |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 214 | p4d, p4d_offset(pgd, 0)); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 215 | } |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 216 | return p4d_offset(pgd, vaddr); |
| 217 | } |
| 218 | |
| 219 | static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr) |
| 220 | { |
| 221 | if (p4d_none(*p4d)) { |
| 222 | pud_t *pud = (pud_t *)spp_getpage(); |
| 223 | p4d_populate(&init_mm, p4d, pud); |
| 224 | if (pud != pud_offset(p4d, 0)) |
| 225 | printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n", |
| 226 | pud, pud_offset(p4d, 0)); |
| 227 | } |
| 228 | return pud_offset(p4d, vaddr); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 229 | } |
| 230 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 231 | static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 232 | { |
| 233 | if (pud_none(*pud)) { |
| 234 | pmd_t *pmd = (pmd_t *) spp_getpage(); |
| 235 | pud_populate(&init_mm, pud, pmd); |
| 236 | if (pmd != pmd_offset(pud, 0)) |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 237 | printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n", |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 238 | pmd, pmd_offset(pud, 0)); |
| 239 | } |
| 240 | return pmd_offset(pud, vaddr); |
| 241 | } |
| 242 | |
Jeremy Fitzhardinge | f254f39 | 2009-03-03 12:02:57 -0800 | [diff] [blame] | 243 | static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr) |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 244 | { |
| 245 | if (pmd_none(*pmd)) { |
| 246 | pte_t *pte = (pte_t *) spp_getpage(); |
| 247 | pmd_populate_kernel(&init_mm, pmd, pte); |
| 248 | if (pte != pte_offset_kernel(pmd, 0)) |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 249 | printk(KERN_ERR "PAGETABLE BUG #03!\n"); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 250 | } |
| 251 | return pte_offset_kernel(pmd, vaddr); |
| 252 | } |
| 253 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 254 | static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 256 | pmd_t *pmd = fill_pmd(pud, vaddr); |
| 257 | pte_t *pte = fill_pte(pmd, vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | set_pte(pte, new_pte); |
| 260 | |
| 261 | /* |
| 262 | * It's enough to flush this one mapping. |
| 263 | * (PGE mappings get flushed as well) |
| 264 | */ |
| 265 | __flush_tlb_one(vaddr); |
| 266 | } |
| 267 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 268 | void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte) |
| 269 | { |
| 270 | p4d_t *p4d = p4d_page + p4d_index(vaddr); |
| 271 | pud_t *pud = fill_pud(p4d, vaddr); |
| 272 | |
| 273 | __set_pte_vaddr(pud, vaddr, new_pte); |
| 274 | } |
| 275 | |
| 276 | void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte) |
| 277 | { |
| 278 | pud_t *pud = pud_page + pud_index(vaddr); |
| 279 | |
| 280 | __set_pte_vaddr(pud, vaddr, new_pte); |
| 281 | } |
| 282 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 283 | void set_pte_vaddr(unsigned long vaddr, pte_t pteval) |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 284 | { |
| 285 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 286 | p4d_t *p4d_page; |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 287 | |
| 288 | pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval)); |
| 289 | |
| 290 | pgd = pgd_offset_k(vaddr); |
| 291 | if (pgd_none(*pgd)) { |
| 292 | printk(KERN_ERR |
| 293 | "PGD FIXMAP MISSING, it should be setup in head.S!\n"); |
| 294 | return; |
| 295 | } |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 296 | |
| 297 | p4d_page = p4d_offset(pgd, 0); |
| 298 | set_pte_vaddr_p4d(p4d_page, vaddr, pteval); |
Eduardo Habkost | 0814e0b | 2008-06-25 00:19:22 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 301 | pmd_t * __init populate_extra_pmd(unsigned long vaddr) |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 302 | { |
| 303 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 304 | p4d_t *p4d; |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 305 | pud_t *pud; |
| 306 | |
| 307 | pgd = pgd_offset_k(vaddr); |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 308 | p4d = fill_p4d(pgd, vaddr); |
| 309 | pud = fill_pud(p4d, vaddr); |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 310 | return fill_pmd(pud, vaddr); |
| 311 | } |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 312 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 313 | pte_t * __init populate_extra_pte(unsigned long vaddr) |
| 314 | { |
| 315 | pmd_t *pmd; |
| 316 | |
| 317 | pmd = populate_extra_pmd(vaddr); |
| 318 | return fill_pte(pmd, vaddr); |
Tejun Heo | 1112441 | 2009-02-20 16:29:09 +0900 | [diff] [blame] | 319 | } |
| 320 | |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 321 | /* |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 322 | * Create large page table mappings for a range of physical addresses. |
| 323 | */ |
| 324 | static void __init __init_extra_mapping(unsigned long phys, unsigned long size, |
Juergen Gross | 2df58b6 | 2014-11-03 14:01:52 +0100 | [diff] [blame] | 325 | enum page_cache_mode cache) |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 326 | { |
| 327 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 328 | p4d_t *p4d; |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 329 | pud_t *pud; |
| 330 | pmd_t *pmd; |
Juergen Gross | 2df58b6 | 2014-11-03 14:01:52 +0100 | [diff] [blame] | 331 | pgprot_t prot; |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 332 | |
Juergen Gross | 2df58b6 | 2014-11-03 14:01:52 +0100 | [diff] [blame] | 333 | pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) | |
| 334 | pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache))); |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 335 | BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK)); |
| 336 | for (; size; phys += PMD_SIZE, size -= PMD_SIZE) { |
| 337 | pgd = pgd_offset_k((unsigned long)__va(phys)); |
| 338 | if (pgd_none(*pgd)) { |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 339 | p4d = (p4d_t *) spp_getpage(); |
| 340 | set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE | |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 341 | _PAGE_USER)); |
| 342 | } |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 343 | p4d = p4d_offset(pgd, (unsigned long)__va(phys)); |
| 344 | if (p4d_none(*p4d)) { |
| 345 | pud = (pud_t *) spp_getpage(); |
| 346 | set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE | |
| 347 | _PAGE_USER)); |
| 348 | } |
| 349 | pud = pud_offset(p4d, (unsigned long)__va(phys)); |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 350 | if (pud_none(*pud)) { |
| 351 | pmd = (pmd_t *) spp_getpage(); |
| 352 | set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | |
| 353 | _PAGE_USER)); |
| 354 | } |
| 355 | pmd = pmd_offset(pud, phys); |
| 356 | BUG_ON(!pmd_none(*pmd)); |
| 357 | set_pmd(pmd, __pmd(phys | pgprot_val(prot))); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void __init init_extra_mapping_wb(unsigned long phys, unsigned long size) |
| 362 | { |
Juergen Gross | 2df58b6 | 2014-11-03 14:01:52 +0100 | [diff] [blame] | 363 | __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB); |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void __init init_extra_mapping_uc(unsigned long phys, unsigned long size) |
| 367 | { |
Juergen Gross | 2df58b6 | 2014-11-03 14:01:52 +0100 | [diff] [blame] | 368 | __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC); |
Jack Steiner | 3a9e189 | 2008-07-01 14:45:32 -0500 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
Ingo Molnar | 88f3aec | 2008-02-21 11:04:11 +0100 | [diff] [blame] | 372 | * The head.S code sets up the kernel high mapping: |
| 373 | * |
| 374 | * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 375 | * |
Zhang Yanfei | 1e3b308 | 2013-05-15 10:58:07 +0800 | [diff] [blame] | 376 | * phys_base holds the negative offset to the kernel, which is added |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 377 | * to the compile time generated pmds. This results in invalid pmds up |
| 378 | * to the point where we hit the physaddr 0 mapping. |
| 379 | * |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 380 | * We limit the mappings to the region from _text to _brk_end. _brk_end |
| 381 | * is rounded up to the 2MB boundary. This catches the invalid pmds as |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 382 | * well, as they are located before _text: |
| 383 | */ |
| 384 | void __init cleanup_highmap(void) |
| 385 | { |
| 386 | unsigned long vaddr = __START_KERNEL_map; |
Yinghai Lu | 1005423 | 2013-01-24 12:19:54 -0800 | [diff] [blame] | 387 | unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE; |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 388 | unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1; |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 389 | pmd_t *pmd = level2_kernel_pgt; |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 390 | |
Yinghai Lu | 1005423 | 2013-01-24 12:19:54 -0800 | [diff] [blame] | 391 | /* |
| 392 | * Native path, max_pfn_mapped is not set yet. |
| 393 | * Xen has valid max_pfn_mapped set in |
| 394 | * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable(). |
| 395 | */ |
| 396 | if (max_pfn_mapped) |
| 397 | vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT); |
| 398 | |
Yinghai Lu | e5f15b4 | 2011-02-18 11:30:30 +0000 | [diff] [blame] | 399 | for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) { |
Hugh Dickins | 2884f11 | 2008-05-28 19:36:07 +0100 | [diff] [blame] | 400 | if (pmd_none(*pmd)) |
Thomas Gleixner | 31eedd8 | 2008-02-15 17:29:12 +0100 | [diff] [blame] | 401 | continue; |
| 402 | if (vaddr < (unsigned long) _text || vaddr > end) |
| 403 | set_pmd(pmd, __pmd(0)); |
| 404 | } |
| 405 | } |
| 406 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 407 | /* |
| 408 | * Create PTE level page table mapping for physical addresses. |
| 409 | * It returns the last physical address mapped. |
| 410 | */ |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 411 | static unsigned long __meminit |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 412 | phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 413 | pgprot_t prot) |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 414 | { |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 415 | unsigned long pages = 0, paddr_next; |
| 416 | unsigned long paddr_last = paddr_end; |
| 417 | pte_t *pte; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 418 | int i; |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 419 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 420 | pte = pte_page + pte_index(paddr); |
| 421 | i = pte_index(paddr); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 422 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 423 | for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) { |
| 424 | paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE; |
| 425 | if (paddr >= paddr_end) { |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 426 | if (!after_bootmem && |
Ingo Molnar | 3bce64f | 2017-01-28 14:14:25 +0100 | [diff] [blame] | 427 | !e820__mapped_any(paddr & PAGE_MASK, paddr_next, |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 428 | E820_TYPE_RAM) && |
Ingo Molnar | 3bce64f | 2017-01-28 14:14:25 +0100 | [diff] [blame] | 429 | !e820__mapped_any(paddr & PAGE_MASK, paddr_next, |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 430 | E820_TYPE_RESERVED_KERN)) |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 431 | set_pte(pte, __pte(0)); |
| 432 | continue; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 433 | } |
| 434 | |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 435 | /* |
| 436 | * We will re-use the existing mapping. |
| 437 | * Xen for example has some special requirements, like mapping |
| 438 | * pagetable pages as RO. So assume someone who pre-setup |
| 439 | * these mappings are more intelligent. |
| 440 | */ |
Dave Hansen | dcb32d9 | 2016-07-07 17:19:15 -0700 | [diff] [blame] | 441 | if (!pte_none(*pte)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 442 | if (!after_bootmem) |
| 443 | pages++; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 444 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 445 | } |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 446 | |
| 447 | if (0) |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 448 | pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr, |
| 449 | pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 450 | pages++; |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 451 | set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot)); |
| 452 | paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 453 | } |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 454 | |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 455 | update_page_count(PG_LEVEL_4K, pages); |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 456 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 457 | return paddr_last; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 458 | } |
| 459 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 460 | /* |
| 461 | * Create PMD level page table mapping for physical addresses. The virtual |
| 462 | * and physical address have to be aligned at this level. |
| 463 | * It returns the last physical address mapped. |
| 464 | */ |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 465 | static unsigned long __meminit |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 466 | phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 467 | unsigned long page_size_mask, pgprot_t prot) |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 468 | { |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 469 | unsigned long pages = 0, paddr_next; |
| 470 | unsigned long paddr_last = paddr_end; |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 471 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 472 | int i = pmd_index(paddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 474 | for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) { |
| 475 | pmd_t *pmd = pmd_page + pmd_index(paddr); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 476 | pte_t *pte; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 477 | pgprot_t new_prot = prot; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 478 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 479 | paddr_next = (paddr & PMD_MASK) + PMD_SIZE; |
| 480 | if (paddr >= paddr_end) { |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 481 | if (!after_bootmem && |
Ingo Molnar | 3bce64f | 2017-01-28 14:14:25 +0100 | [diff] [blame] | 482 | !e820__mapped_any(paddr & PMD_MASK, paddr_next, |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 483 | E820_TYPE_RAM) && |
Ingo Molnar | 3bce64f | 2017-01-28 14:14:25 +0100 | [diff] [blame] | 484 | !e820__mapped_any(paddr & PMD_MASK, paddr_next, |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 485 | E820_TYPE_RESERVED_KERN)) |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 486 | set_pmd(pmd, __pmd(0)); |
| 487 | continue; |
| 488 | } |
Jan Beulich | 20167d3 | 2012-05-16 14:06:26 +0100 | [diff] [blame] | 489 | |
Dave Hansen | dcb32d9 | 2016-07-07 17:19:15 -0700 | [diff] [blame] | 490 | if (!pmd_none(*pmd)) { |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 491 | if (!pmd_large(*pmd)) { |
| 492 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 493 | pte = (pte_t *)pmd_page_vaddr(*pmd); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 494 | paddr_last = phys_pte_init(pte, paddr, |
| 495 | paddr_end, prot); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 496 | spin_unlock(&init_mm.page_table_lock); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 497 | continue; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 498 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 499 | /* |
| 500 | * If we are ok with PG_LEVEL_2M mapping, then we will |
| 501 | * use the existing mapping, |
| 502 | * |
| 503 | * Otherwise, we will split the large page mapping but |
| 504 | * use the same existing protection bits except for |
| 505 | * large page, so that we don't violate Intel's TLB |
| 506 | * Application note (317080) which says, while changing |
| 507 | * the page sizes, new and old translations should |
| 508 | * not differ with respect to page frame and |
| 509 | * attributes. |
| 510 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 511 | if (page_size_mask & (1 << PG_LEVEL_2M)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 512 | if (!after_bootmem) |
| 513 | pages++; |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 514 | paddr_last = paddr_next; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 515 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 516 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 517 | new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd)); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 518 | } |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 519 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 520 | if (page_size_mask & (1<<PG_LEVEL_2M)) { |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 521 | pages++; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 522 | spin_lock(&init_mm.page_table_lock); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 523 | set_pte((pte_t *)pmd, |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 524 | pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT, |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 525 | __pgprot(pgprot_val(prot) | _PAGE_PSE))); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 526 | spin_unlock(&init_mm.page_table_lock); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 527 | paddr_last = paddr_next; |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 528 | continue; |
| 529 | } |
| 530 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 531 | pte = alloc_low_page(); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 532 | paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot); |
Jeremy Fitzhardinge | 4f9c11d | 2008-06-25 00:19:19 -0400 | [diff] [blame] | 533 | |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 534 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 535 | pmd_populate_kernel(&init_mm, pmd, pte); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 536 | spin_unlock(&init_mm.page_table_lock); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 537 | } |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 538 | update_page_count(PG_LEVEL_2M, pages); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 539 | return paddr_last; |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 542 | /* |
| 543 | * Create PUD level page table mapping for physical addresses. The virtual |
Thomas Garnier | faa3793 | 2016-06-21 17:47:00 -0700 | [diff] [blame] | 544 | * and physical address do not have to be aligned at this level. KASLR can |
| 545 | * randomize virtual addresses up to this level. |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 546 | * It returns the last physical address mapped. |
| 547 | */ |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 548 | static unsigned long __meminit |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 549 | phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end, |
| 550 | unsigned long page_size_mask) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 551 | { |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 552 | unsigned long pages = 0, paddr_next; |
| 553 | unsigned long paddr_last = paddr_end; |
Thomas Garnier | faa3793 | 2016-06-21 17:47:00 -0700 | [diff] [blame] | 554 | unsigned long vaddr = (unsigned long)__va(paddr); |
| 555 | int i = pud_index(vaddr); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 556 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 557 | for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) { |
Thomas Garnier | faa3793 | 2016-06-21 17:47:00 -0700 | [diff] [blame] | 558 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | pmd_t *pmd; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 560 | pgprot_t prot = PAGE_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Thomas Garnier | faa3793 | 2016-06-21 17:47:00 -0700 | [diff] [blame] | 562 | vaddr = (unsigned long)__va(paddr); |
| 563 | pud = pud_page + pud_index(vaddr); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 564 | paddr_next = (paddr & PUD_MASK) + PUD_SIZE; |
Thomas Garnier | faa3793 | 2016-06-21 17:47:00 -0700 | [diff] [blame] | 565 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 566 | if (paddr >= paddr_end) { |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 567 | if (!after_bootmem && |
Ingo Molnar | 3bce64f | 2017-01-28 14:14:25 +0100 | [diff] [blame] | 568 | !e820__mapped_any(paddr & PUD_MASK, paddr_next, |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 569 | E820_TYPE_RAM) && |
Ingo Molnar | 3bce64f | 2017-01-28 14:14:25 +0100 | [diff] [blame] | 570 | !e820__mapped_any(paddr & PUD_MASK, paddr_next, |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 571 | E820_TYPE_RESERVED_KERN)) |
Yinghai Lu | eceb363 | 2012-11-16 19:38:56 -0800 | [diff] [blame] | 572 | set_pud(pud, __pud(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | continue; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Dave Hansen | dcb32d9 | 2016-07-07 17:19:15 -0700 | [diff] [blame] | 576 | if (!pud_none(*pud)) { |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 577 | if (!pud_large(*pud)) { |
Yinghai Lu | 973dc4f | 2012-11-16 19:38:59 -0800 | [diff] [blame] | 578 | pmd = pmd_offset(pud, 0); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 579 | paddr_last = phys_pmd_init(pmd, paddr, |
| 580 | paddr_end, |
| 581 | page_size_mask, |
| 582 | prot); |
Yinghai Lu | 4b239f4 | 2010-12-17 16:58:28 -0800 | [diff] [blame] | 583 | __flush_tlb_all(); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 584 | continue; |
| 585 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 586 | /* |
| 587 | * If we are ok with PG_LEVEL_1G mapping, then we will |
| 588 | * use the existing mapping. |
| 589 | * |
| 590 | * Otherwise, we will split the gbpage mapping but use |
| 591 | * the same existing protection bits except for large |
| 592 | * page, so that we don't violate Intel's TLB |
| 593 | * Application note (317080) which says, while changing |
| 594 | * the page sizes, new and old translations should |
| 595 | * not differ with respect to page frame and |
| 596 | * attributes. |
| 597 | */ |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 598 | if (page_size_mask & (1 << PG_LEVEL_1G)) { |
Jan Beulich | 876ee61 | 2012-10-04 14:48:10 +0100 | [diff] [blame] | 599 | if (!after_bootmem) |
| 600 | pages++; |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 601 | paddr_last = paddr_next; |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 602 | continue; |
Yinghai Lu | 3afa394 | 2008-10-25 22:58:21 -0700 | [diff] [blame] | 603 | } |
Suresh Siddha | b27a43c | 2008-10-07 13:58:46 -0700 | [diff] [blame] | 604 | prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud)); |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 607 | if (page_size_mask & (1<<PG_LEVEL_1G)) { |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 608 | pages++; |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 609 | spin_lock(&init_mm.page_table_lock); |
Andi Kleen | ef92576 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 610 | set_pte((pte_t *)pud, |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 611 | pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT, |
Yinghai Lu | 960ddb4f | 2012-11-16 19:38:54 -0800 | [diff] [blame] | 612 | PAGE_KERNEL_LARGE)); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 613 | spin_unlock(&init_mm.page_table_lock); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 614 | paddr_last = paddr_next; |
Keith Mannthey | 6ad9165 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 615 | continue; |
| 616 | } |
| 617 | |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 618 | pmd = alloc_low_page(); |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 619 | paddr_last = phys_pmd_init(pmd, paddr, paddr_end, |
| 620 | page_size_mask, prot); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 621 | |
| 622 | spin_lock(&init_mm.page_table_lock); |
Yinghai Lu | 868bf4d | 2012-11-16 19:39:00 -0800 | [diff] [blame] | 623 | pud_populate(&init_mm, pud, pmd); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 624 | spin_unlock(&init_mm.page_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
Andi Kleen | 1a2b441 | 2008-01-30 13:33:54 +0100 | [diff] [blame] | 626 | __flush_tlb_all(); |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 627 | |
Andi Kleen | ce0c0e5 | 2008-05-02 11:46:49 +0200 | [diff] [blame] | 628 | update_page_count(PG_LEVEL_1G, pages); |
Andi Kleen | cc61503 | 2008-03-12 03:53:28 +0100 | [diff] [blame] | 629 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 630 | return paddr_last; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Kirill A. Shutemov | 7e82ea9 | 2017-06-06 14:31:30 +0300 | [diff] [blame] | 633 | static unsigned long __meminit |
| 634 | phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end, |
| 635 | unsigned long page_size_mask) |
| 636 | { |
| 637 | unsigned long paddr_next, paddr_last = paddr_end; |
| 638 | unsigned long vaddr = (unsigned long)__va(paddr); |
| 639 | int i = p4d_index(vaddr); |
| 640 | |
Kirill A. Shutemov | 91f606a | 2018-02-14 21:25:41 +0300 | [diff] [blame^] | 641 | if (!pgtable_l5_enabled) |
Kirill A. Shutemov | 7e82ea9 | 2017-06-06 14:31:30 +0300 | [diff] [blame] | 642 | return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, page_size_mask); |
| 643 | |
| 644 | for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) { |
| 645 | p4d_t *p4d; |
| 646 | pud_t *pud; |
| 647 | |
| 648 | vaddr = (unsigned long)__va(paddr); |
| 649 | p4d = p4d_page + p4d_index(vaddr); |
| 650 | paddr_next = (paddr & P4D_MASK) + P4D_SIZE; |
| 651 | |
| 652 | if (paddr >= paddr_end) { |
| 653 | if (!after_bootmem && |
| 654 | !e820__mapped_any(paddr & P4D_MASK, paddr_next, |
| 655 | E820_TYPE_RAM) && |
| 656 | !e820__mapped_any(paddr & P4D_MASK, paddr_next, |
| 657 | E820_TYPE_RESERVED_KERN)) |
| 658 | set_p4d(p4d, __p4d(0)); |
| 659 | continue; |
| 660 | } |
| 661 | |
| 662 | if (!p4d_none(*p4d)) { |
| 663 | pud = pud_offset(p4d, 0); |
| 664 | paddr_last = phys_pud_init(pud, paddr, |
| 665 | paddr_end, |
| 666 | page_size_mask); |
| 667 | __flush_tlb_all(); |
| 668 | continue; |
| 669 | } |
| 670 | |
| 671 | pud = alloc_low_page(); |
| 672 | paddr_last = phys_pud_init(pud, paddr, paddr_end, |
| 673 | page_size_mask); |
| 674 | |
| 675 | spin_lock(&init_mm.page_table_lock); |
| 676 | p4d_populate(&init_mm, p4d, pud); |
| 677 | spin_unlock(&init_mm.page_table_lock); |
| 678 | } |
| 679 | __flush_tlb_all(); |
| 680 | |
| 681 | return paddr_last; |
| 682 | } |
| 683 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 684 | /* |
| 685 | * Create page table mapping for the physical memory for specific physical |
Thomas Garnier | faa3793 | 2016-06-21 17:47:00 -0700 | [diff] [blame] | 686 | * addresses. The virtual and physical addresses have to be aligned on PMD level |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 687 | * down. It returns the last physical address mapped. |
| 688 | */ |
Shaohua Li | 41d840e | 2009-06-12 12:57:52 +0800 | [diff] [blame] | 689 | unsigned long __meminit |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 690 | kernel_physical_mapping_init(unsigned long paddr_start, |
| 691 | unsigned long paddr_end, |
Pekka Enberg | f765090 | 2009-03-05 14:55:05 +0200 | [diff] [blame] | 692 | unsigned long page_size_mask) |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 693 | { |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 694 | bool pgd_changed = false; |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 695 | unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 696 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 697 | paddr_last = paddr_end; |
| 698 | vaddr = (unsigned long)__va(paddr_start); |
| 699 | vaddr_end = (unsigned long)__va(paddr_end); |
| 700 | vaddr_start = vaddr; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 701 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 702 | for (; vaddr < vaddr_end; vaddr = vaddr_next) { |
| 703 | pgd_t *pgd = pgd_offset_k(vaddr); |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 704 | p4d_t *p4d; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 705 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 706 | vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 707 | |
Kirill A. Shutemov | 7e82ea9 | 2017-06-06 14:31:30 +0300 | [diff] [blame] | 708 | if (pgd_val(*pgd)) { |
| 709 | p4d = (p4d_t *)pgd_page_vaddr(*pgd); |
| 710 | paddr_last = phys_p4d_init(p4d, __pa(vaddr), |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 711 | __pa(vaddr_end), |
| 712 | page_size_mask); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 713 | continue; |
| 714 | } |
| 715 | |
Kirill A. Shutemov | 7e82ea9 | 2017-06-06 14:31:30 +0300 | [diff] [blame] | 716 | p4d = alloc_low_page(); |
| 717 | paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end), |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 718 | page_size_mask); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 719 | |
| 720 | spin_lock(&init_mm.page_table_lock); |
Kirill A. Shutemov | 91f606a | 2018-02-14 21:25:41 +0300 | [diff] [blame^] | 721 | if (pgtable_l5_enabled) |
Kirill A. Shutemov | 7e82ea9 | 2017-06-06 14:31:30 +0300 | [diff] [blame] | 722 | pgd_populate(&init_mm, pgd, p4d); |
| 723 | else |
| 724 | p4d_populate(&init_mm, p4d_offset(pgd, vaddr), (pud_t *) p4d); |
Jan Beulich | 8ae3a5a | 2008-08-21 14:27:22 +0100 | [diff] [blame] | 725 | spin_unlock(&init_mm.page_table_lock); |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 726 | pgd_changed = true; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 727 | } |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 728 | |
| 729 | if (pgd_changed) |
Kirill A. Shutemov | 5372e15 | 2016-12-15 02:44:03 +0300 | [diff] [blame] | 730 | sync_global_pgds(vaddr_start, vaddr_end - 1); |
Haicheng Li | 9b86152 | 2010-08-20 17:50:16 +0800 | [diff] [blame] | 731 | |
Suresh Siddha | a2699e4 | 2008-09-23 14:00:38 -0700 | [diff] [blame] | 732 | __flush_tlb_all(); |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 733 | |
Thomas Garnier | 59b3d02 | 2016-06-21 17:46:59 -0700 | [diff] [blame] | 734 | return paddr_last; |
Yinghai Lu | b50efd2 | 2008-07-08 01:41:05 -0700 | [diff] [blame] | 735 | } |
Yinghai Lu | 7b16eb8 | 2008-07-09 20:15:02 -0700 | [diff] [blame] | 736 | |
Matt Tolentino | 2b97690 | 2005-06-23 00:08:06 -0700 | [diff] [blame] | 737 | #ifndef CONFIG_NUMA |
Tejun Heo | d8fc3af | 2011-02-16 12:13:06 +0100 | [diff] [blame] | 738 | void __init initmem_init(void) |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 739 | { |
Tang Chen | e7e8de5 | 2014-01-21 15:49:26 -0800 | [diff] [blame] | 740 | memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0); |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 741 | } |
Pekka Enberg | 3551f88 | 2009-05-07 15:35:41 +0300 | [diff] [blame] | 742 | #endif |
Yinghai Lu | 1f75d7e | 2008-06-22 02:44:49 -0700 | [diff] [blame] | 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | void __init paging_init(void) |
| 745 | { |
Pekka Enberg | 3551f88 | 2009-05-07 15:35:41 +0300 | [diff] [blame] | 746 | sparse_memory_present_with_active_regions(MAX_NUMNODES); |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 747 | sparse_init(); |
Yinghai Lu | 44b5728 | 2009-07-08 09:50:19 -0700 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * clear the default setting with node 0 |
| 751 | * note: don't use nodes_clear here, that is really clearing when |
| 752 | * numa support is not compiled in, and later node_set_state |
| 753 | * will not set it back. |
| 754 | */ |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 755 | node_clear_state(0, N_MEMORY); |
| 756 | if (N_MEMORY != N_NORMAL_MEMORY) |
| 757 | node_clear_state(0, N_NORMAL_MEMORY); |
Yinghai Lu | 44b5728 | 2009-07-08 09:50:19 -0700 | [diff] [blame] | 758 | |
Pekka Enberg | 4c0b2e5 | 2011-11-01 15:58:17 +0200 | [diff] [blame] | 759 | zone_sizes_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 762 | /* |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 763 | * Memory hotplug specific functions |
Matt Tolentino | 44df75e | 2006-01-17 07:03:41 +0100 | [diff] [blame] | 764 | */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 765 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 766 | /* |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 767 | * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need |
| 768 | * updating. |
| 769 | */ |
Michal Hocko | 3072e41 | 2017-09-08 16:11:39 -0700 | [diff] [blame] | 770 | static void update_end_of_memory_vars(u64 start, u64 size) |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 771 | { |
| 772 | unsigned long end_pfn = PFN_UP(start + size); |
| 773 | |
| 774 | if (end_pfn > max_pfn) { |
| 775 | max_pfn = end_pfn; |
| 776 | max_low_pfn = end_pfn; |
| 777 | high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1; |
| 778 | } |
| 779 | } |
| 780 | |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 781 | int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages, |
| 782 | struct vmem_altmap *altmap, bool want_memblock) |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 783 | { |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 784 | int ret; |
| 785 | |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 786 | ret = __add_pages(nid, start_pfn, nr_pages, altmap, want_memblock); |
Gary Hade | fe8b868 | 2008-10-28 16:43:14 -0700 | [diff] [blame] | 787 | WARN_ON_ONCE(ret); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 788 | |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 789 | /* update max_pfn, max_low_pfn and high_memory */ |
Michal Hocko | 3072e41 | 2017-09-08 16:11:39 -0700 | [diff] [blame] | 790 | update_end_of_memory_vars(start_pfn << PAGE_SHIFT, |
| 791 | nr_pages << PAGE_SHIFT); |
Shaohui Zheng | ea08541 | 2010-02-02 13:44:16 -0800 | [diff] [blame] | 792 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 793 | return ret; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 794 | } |
Michal Hocko | 3072e41 | 2017-09-08 16:11:39 -0700 | [diff] [blame] | 795 | |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 796 | int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap, |
| 797 | bool want_memblock) |
Michal Hocko | 3072e41 | 2017-09-08 16:11:39 -0700 | [diff] [blame] | 798 | { |
| 799 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 800 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 801 | |
| 802 | init_memory_mapping(start, start + size); |
| 803 | |
Christoph Hellwig | 24e6d5a | 2017-12-29 08:53:53 +0100 | [diff] [blame] | 804 | return add_pages(nid, start_pfn, nr_pages, altmap, want_memblock); |
Michal Hocko | 3072e41 | 2017-09-08 16:11:39 -0700 | [diff] [blame] | 805 | } |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 806 | |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 807 | #define PAGE_INUSE 0xFD |
| 808 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 809 | static void __meminit free_pagetable(struct page *page, int order, |
| 810 | struct vmem_altmap *altmap) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 811 | { |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 812 | unsigned long magic; |
| 813 | unsigned int nr_pages = 1 << order; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 814 | |
| 815 | if (altmap) { |
| 816 | vmem_altmap_free(altmap, nr_pages); |
| 817 | return; |
| 818 | } |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 819 | |
| 820 | /* bootmem page has reserved flag */ |
| 821 | if (PageReserved(page)) { |
| 822 | __ClearPageReserved(page); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 823 | |
Yasuaki Ishimatsu | ddffe98 | 2017-02-22 15:45:13 -0800 | [diff] [blame] | 824 | magic = (unsigned long)page->freelist; |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 825 | if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) { |
| 826 | while (nr_pages--) |
| 827 | put_page_bootmem(page++); |
| 828 | } else |
Jiang Liu | 170a5a7 | 2013-07-03 15:03:17 -0700 | [diff] [blame] | 829 | while (nr_pages--) |
| 830 | free_reserved_page(page++); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 831 | } else |
| 832 | free_pages((unsigned long)page_address(page), order); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 835 | static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd, |
| 836 | struct vmem_altmap *altmap) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 837 | { |
| 838 | pte_t *pte; |
| 839 | int i; |
| 840 | |
| 841 | for (i = 0; i < PTRS_PER_PTE; i++) { |
| 842 | pte = pte_start + i; |
Dave Hansen | dcb32d9 | 2016-07-07 17:19:15 -0700 | [diff] [blame] | 843 | if (!pte_none(*pte)) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | |
| 847 | /* free a pte talbe */ |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 848 | free_pagetable(pmd_page(*pmd), 0, altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 849 | spin_lock(&init_mm.page_table_lock); |
| 850 | pmd_clear(pmd); |
| 851 | spin_unlock(&init_mm.page_table_lock); |
| 852 | } |
| 853 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 854 | static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud, |
| 855 | struct vmem_altmap *altmap) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 856 | { |
| 857 | pmd_t *pmd; |
| 858 | int i; |
| 859 | |
| 860 | for (i = 0; i < PTRS_PER_PMD; i++) { |
| 861 | pmd = pmd_start + i; |
Dave Hansen | dcb32d9 | 2016-07-07 17:19:15 -0700 | [diff] [blame] | 862 | if (!pmd_none(*pmd)) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 863 | return; |
| 864 | } |
| 865 | |
| 866 | /* free a pmd talbe */ |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 867 | free_pagetable(pud_page(*pud), 0, altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 868 | spin_lock(&init_mm.page_table_lock); |
| 869 | pud_clear(pud); |
| 870 | spin_unlock(&init_mm.page_table_lock); |
| 871 | } |
| 872 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 873 | static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d, |
| 874 | struct vmem_altmap *altmap) |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 875 | { |
| 876 | pud_t *pud; |
| 877 | int i; |
| 878 | |
| 879 | for (i = 0; i < PTRS_PER_PUD; i++) { |
| 880 | pud = pud_start + i; |
| 881 | if (!pud_none(*pud)) |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | /* free a pud talbe */ |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 886 | free_pagetable(p4d_page(*p4d), 0, altmap); |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 887 | spin_lock(&init_mm.page_table_lock); |
| 888 | p4d_clear(p4d); |
| 889 | spin_unlock(&init_mm.page_table_lock); |
| 890 | } |
| 891 | |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 892 | static void __meminit |
| 893 | remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end, |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 894 | struct vmem_altmap *altmap, bool direct) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 895 | { |
| 896 | unsigned long next, pages = 0; |
| 897 | pte_t *pte; |
| 898 | void *page_addr; |
| 899 | phys_addr_t phys_addr; |
| 900 | |
| 901 | pte = pte_start + pte_index(addr); |
| 902 | for (; addr < end; addr = next, pte++) { |
| 903 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 904 | if (next > end) |
| 905 | next = end; |
| 906 | |
| 907 | if (!pte_present(*pte)) |
| 908 | continue; |
| 909 | |
| 910 | /* |
| 911 | * We mapped [0,1G) memory as identity mapping when |
| 912 | * initializing, in arch/x86/kernel/head_64.S. These |
| 913 | * pagetables cannot be removed. |
| 914 | */ |
| 915 | phys_addr = pte_val(*pte) + (addr & PAGE_MASK); |
| 916 | if (phys_addr < (phys_addr_t)0x40000000) |
| 917 | return; |
| 918 | |
Kefeng Wang | b500f77 | 2016-01-12 10:19:30 +0800 | [diff] [blame] | 919 | if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) { |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 920 | /* |
| 921 | * Do not free direct mapping pages since they were |
| 922 | * freed when offlining, or simplely not in use. |
| 923 | */ |
| 924 | if (!direct) |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 925 | free_pagetable(pte_page(*pte), 0, altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 926 | |
| 927 | spin_lock(&init_mm.page_table_lock); |
| 928 | pte_clear(&init_mm, addr, pte); |
| 929 | spin_unlock(&init_mm.page_table_lock); |
| 930 | |
| 931 | /* For non-direct mapping, pages means nothing. */ |
| 932 | pages++; |
| 933 | } else { |
| 934 | /* |
| 935 | * If we are here, we are freeing vmemmap pages since |
| 936 | * direct mapped memory ranges to be freed are aligned. |
| 937 | * |
| 938 | * If we are not removing the whole page, it means |
| 939 | * other page structs in this page are being used and |
| 940 | * we canot remove them. So fill the unused page_structs |
| 941 | * with 0xFD, and remove the page when it is wholly |
| 942 | * filled with 0xFD. |
| 943 | */ |
| 944 | memset((void *)addr, PAGE_INUSE, next - addr); |
| 945 | |
| 946 | page_addr = page_address(pte_page(*pte)); |
| 947 | if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) { |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 948 | free_pagetable(pte_page(*pte), 0, altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 949 | |
| 950 | spin_lock(&init_mm.page_table_lock); |
| 951 | pte_clear(&init_mm, addr, pte); |
| 952 | spin_unlock(&init_mm.page_table_lock); |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /* Call free_pte_table() in remove_pmd_table(). */ |
| 958 | flush_tlb_all(); |
| 959 | if (direct) |
| 960 | update_page_count(PG_LEVEL_4K, -pages); |
| 961 | } |
| 962 | |
| 963 | static void __meminit |
| 964 | remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end, |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 965 | bool direct, struct vmem_altmap *altmap) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 966 | { |
| 967 | unsigned long next, pages = 0; |
| 968 | pte_t *pte_base; |
| 969 | pmd_t *pmd; |
| 970 | void *page_addr; |
| 971 | |
| 972 | pmd = pmd_start + pmd_index(addr); |
| 973 | for (; addr < end; addr = next, pmd++) { |
| 974 | next = pmd_addr_end(addr, end); |
| 975 | |
| 976 | if (!pmd_present(*pmd)) |
| 977 | continue; |
| 978 | |
| 979 | if (pmd_large(*pmd)) { |
| 980 | if (IS_ALIGNED(addr, PMD_SIZE) && |
| 981 | IS_ALIGNED(next, PMD_SIZE)) { |
| 982 | if (!direct) |
| 983 | free_pagetable(pmd_page(*pmd), |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 984 | get_order(PMD_SIZE), |
| 985 | altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 986 | |
| 987 | spin_lock(&init_mm.page_table_lock); |
| 988 | pmd_clear(pmd); |
| 989 | spin_unlock(&init_mm.page_table_lock); |
| 990 | pages++; |
| 991 | } else { |
| 992 | /* If here, we are freeing vmemmap pages. */ |
| 993 | memset((void *)addr, PAGE_INUSE, next - addr); |
| 994 | |
| 995 | page_addr = page_address(pmd_page(*pmd)); |
| 996 | if (!memchr_inv(page_addr, PAGE_INUSE, |
| 997 | PMD_SIZE)) { |
| 998 | free_pagetable(pmd_page(*pmd), |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 999 | get_order(PMD_SIZE), |
| 1000 | altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1001 | |
| 1002 | spin_lock(&init_mm.page_table_lock); |
| 1003 | pmd_clear(pmd); |
| 1004 | spin_unlock(&init_mm.page_table_lock); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
| 1011 | pte_base = (pte_t *)pmd_page_vaddr(*pmd); |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1012 | remove_pte_table(pte_base, addr, next, altmap, direct); |
| 1013 | free_pte_table(pte_base, pmd, altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* Call free_pmd_table() in remove_pud_table(). */ |
| 1017 | if (direct) |
| 1018 | update_page_count(PG_LEVEL_2M, -pages); |
| 1019 | } |
| 1020 | |
| 1021 | static void __meminit |
| 1022 | remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end, |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1023 | struct vmem_altmap *altmap, bool direct) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1024 | { |
| 1025 | unsigned long next, pages = 0; |
| 1026 | pmd_t *pmd_base; |
| 1027 | pud_t *pud; |
| 1028 | void *page_addr; |
| 1029 | |
| 1030 | pud = pud_start + pud_index(addr); |
| 1031 | for (; addr < end; addr = next, pud++) { |
| 1032 | next = pud_addr_end(addr, end); |
| 1033 | |
| 1034 | if (!pud_present(*pud)) |
| 1035 | continue; |
| 1036 | |
| 1037 | if (pud_large(*pud)) { |
| 1038 | if (IS_ALIGNED(addr, PUD_SIZE) && |
| 1039 | IS_ALIGNED(next, PUD_SIZE)) { |
| 1040 | if (!direct) |
| 1041 | free_pagetable(pud_page(*pud), |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1042 | get_order(PUD_SIZE), |
| 1043 | altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1044 | |
| 1045 | spin_lock(&init_mm.page_table_lock); |
| 1046 | pud_clear(pud); |
| 1047 | spin_unlock(&init_mm.page_table_lock); |
| 1048 | pages++; |
| 1049 | } else { |
| 1050 | /* If here, we are freeing vmemmap pages. */ |
| 1051 | memset((void *)addr, PAGE_INUSE, next - addr); |
| 1052 | |
| 1053 | page_addr = page_address(pud_page(*pud)); |
| 1054 | if (!memchr_inv(page_addr, PAGE_INUSE, |
| 1055 | PUD_SIZE)) { |
| 1056 | free_pagetable(pud_page(*pud), |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1057 | get_order(PUD_SIZE), |
| 1058 | altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1059 | |
| 1060 | spin_lock(&init_mm.page_table_lock); |
| 1061 | pud_clear(pud); |
| 1062 | spin_unlock(&init_mm.page_table_lock); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | continue; |
| 1067 | } |
| 1068 | |
Kirill A. Shutemov | e6ab9c4 | 2017-04-25 12:25:57 +0300 | [diff] [blame] | 1069 | pmd_base = pmd_offset(pud, 0); |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1070 | remove_pmd_table(pmd_base, addr, next, direct, altmap); |
| 1071 | free_pmd_table(pmd_base, pud, altmap); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | if (direct) |
| 1075 | update_page_count(PG_LEVEL_1G, -pages); |
| 1076 | } |
| 1077 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1078 | static void __meminit |
| 1079 | remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end, |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1080 | struct vmem_altmap *altmap, bool direct) |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1081 | { |
| 1082 | unsigned long next, pages = 0; |
| 1083 | pud_t *pud_base; |
| 1084 | p4d_t *p4d; |
| 1085 | |
| 1086 | p4d = p4d_start + p4d_index(addr); |
| 1087 | for (; addr < end; addr = next, p4d++) { |
| 1088 | next = p4d_addr_end(addr, end); |
| 1089 | |
| 1090 | if (!p4d_present(*p4d)) |
| 1091 | continue; |
| 1092 | |
| 1093 | BUILD_BUG_ON(p4d_large(*p4d)); |
| 1094 | |
Kirill A. Shutemov | e6ab9c4 | 2017-04-25 12:25:57 +0300 | [diff] [blame] | 1095 | pud_base = pud_offset(p4d, 0); |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1096 | remove_pud_table(pud_base, addr, next, altmap, direct); |
Jérôme Glisse | 98fe363 | 2017-06-24 14:05:14 -0400 | [diff] [blame] | 1097 | /* |
| 1098 | * For 4-level page tables we do not want to free PUDs, but in the |
| 1099 | * 5-level case we should free them. This code will have to change |
| 1100 | * to adapt for boot-time switching between 4 and 5 level page tables. |
| 1101 | */ |
Kirill A. Shutemov | 91f606a | 2018-02-14 21:25:41 +0300 | [diff] [blame^] | 1102 | if (pgtable_l5_enabled) |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1103 | free_pud_table(pud_base, p4d, altmap); |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | if (direct) |
| 1107 | update_page_count(PG_LEVEL_512G, -pages); |
| 1108 | } |
| 1109 | |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1110 | /* start and end are both virtual address. */ |
| 1111 | static void __meminit |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1112 | remove_pagetable(unsigned long start, unsigned long end, bool direct, |
| 1113 | struct vmem_altmap *altmap) |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1114 | { |
| 1115 | unsigned long next; |
Yasuaki Ishimatsu | 5255e0a | 2014-08-22 13:27:31 -0700 | [diff] [blame] | 1116 | unsigned long addr; |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1117 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1118 | p4d_t *p4d; |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1119 | |
Yasuaki Ishimatsu | 5255e0a | 2014-08-22 13:27:31 -0700 | [diff] [blame] | 1120 | for (addr = start; addr < end; addr = next) { |
| 1121 | next = pgd_addr_end(addr, end); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1122 | |
Yasuaki Ishimatsu | 5255e0a | 2014-08-22 13:27:31 -0700 | [diff] [blame] | 1123 | pgd = pgd_offset_k(addr); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1124 | if (!pgd_present(*pgd)) |
| 1125 | continue; |
| 1126 | |
Kirill A. Shutemov | e6ab9c4 | 2017-04-25 12:25:57 +0300 | [diff] [blame] | 1127 | p4d = p4d_offset(pgd, 0); |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1128 | remove_p4d_table(p4d, addr, next, altmap, direct); |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
Wen Congyang | ae9aae9 | 2013-02-22 16:33:04 -0800 | [diff] [blame] | 1131 | flush_tlb_all(); |
| 1132 | } |
| 1133 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1134 | void __ref vmemmap_free(unsigned long start, unsigned long end, |
| 1135 | struct vmem_altmap *altmap) |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 1136 | { |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1137 | remove_pagetable(start, end, false, altmap); |
Tang Chen | 0197518 | 2013-02-22 16:33:08 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Tang Chen | 587ff8c | 2013-04-15 17:46:46 +0800 | [diff] [blame] | 1140 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Tang Chen | bbcab87 | 2013-02-22 16:33:06 -0800 | [diff] [blame] | 1141 | static void __meminit |
| 1142 | kernel_physical_mapping_remove(unsigned long start, unsigned long end) |
| 1143 | { |
| 1144 | start = (unsigned long)__va(start); |
| 1145 | end = (unsigned long)__va(end); |
| 1146 | |
Christoph Hellwig | 24b6d41 | 2017-12-29 08:53:56 +0100 | [diff] [blame] | 1147 | remove_pagetable(start, end, true, NULL); |
Tang Chen | bbcab87 | 2013-02-22 16:33:06 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Christoph Hellwig | da02451 | 2017-12-29 08:53:55 +0100 | [diff] [blame] | 1150 | int __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 1151 | { |
| 1152 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 1153 | unsigned long nr_pages = size >> PAGE_SHIFT; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 1154 | struct page *page = pfn_to_page(start_pfn); |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 1155 | struct zone *zone; |
| 1156 | int ret; |
| 1157 | |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 1158 | /* With altmap the first mapped page is offset from @start */ |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 1159 | if (altmap) |
| 1160 | page += vmem_altmap_offset(altmap); |
| 1161 | zone = page_zone(page); |
Christoph Hellwig | da02451 | 2017-12-29 08:53:55 +0100 | [diff] [blame] | 1162 | ret = __remove_pages(zone, start_pfn, nr_pages, altmap); |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 1163 | WARN_ON_ONCE(ret); |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 1164 | kernel_physical_mapping_remove(start, start + size); |
Wen Congyang | 24d335c | 2013-02-22 16:32:58 -0800 | [diff] [blame] | 1165 | |
| 1166 | return ret; |
| 1167 | } |
| 1168 | #endif |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 1169 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
| 1170 | |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 1171 | static struct kcore_list kcore_vsyscall; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
Yinghai Lu | 94b43c3 | 2012-11-16 19:39:19 -0800 | [diff] [blame] | 1173 | static void __init register_page_bootmem_info(void) |
| 1174 | { |
| 1175 | #ifdef CONFIG_NUMA |
| 1176 | int i; |
| 1177 | |
| 1178 | for_each_online_node(i) |
| 1179 | register_page_bootmem_info_node(NODE_DATA(i)); |
| 1180 | #endif |
| 1181 | } |
| 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | void __init mem_init(void) |
| 1184 | { |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 1185 | pci_iommu_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Yinghai Lu | 48ddb15 | 2008-01-30 13:32:36 +0100 | [diff] [blame] | 1187 | /* clear_bss() already clear the empty_zero_page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
Jiang Liu | bced0e3 | 2013-04-29 15:06:53 -0700 | [diff] [blame] | 1189 | /* this will put all memory onto the freelists */ |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 1190 | free_all_bootmem(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | after_bootmem = 1; |
| 1192 | |
Pavel Tatashin | 353b1e7 | 2017-11-15 17:36:14 -0800 | [diff] [blame] | 1193 | /* |
| 1194 | * Must be done after boot memory is put on freelist, because here we |
| 1195 | * might set fields in deferred struct pages that have not yet been |
| 1196 | * initialized, and free_all_bootmem() initializes all the reserved |
| 1197 | * deferred pages for us. |
| 1198 | */ |
| 1199 | register_page_bootmem_info(); |
| 1200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | /* Register memory areas for /proc/kcore */ |
Andy Lutomirski | f40c330 | 2014-05-05 12:19:36 -0700 | [diff] [blame] | 1202 | kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR, |
| 1203 | PAGE_SIZE, KCORE_OTHER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
Jiang Liu | 46a8413 | 2013-07-03 15:04:19 -0700 | [diff] [blame] | 1205 | mem_init_print_info(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Suresh Siddha | 502f660 | 2009-10-28 18:46:56 -0800 | [diff] [blame] | 1208 | int kernel_set_to_readonly; |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1209 | |
| 1210 | void set_kernel_text_rw(void) |
| 1211 | { |
Suresh Siddha | b9af7c0 | 2009-10-14 14:46:55 -0700 | [diff] [blame] | 1212 | unsigned long start = PFN_ALIGN(_text); |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1213 | unsigned long end = PFN_ALIGN(__stop___ex_table); |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1214 | |
| 1215 | if (!kernel_set_to_readonly) |
| 1216 | return; |
| 1217 | |
| 1218 | pr_debug("Set kernel text: %lx - %lx for read write\n", |
| 1219 | start, end); |
| 1220 | |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1221 | /* |
| 1222 | * Make the kernel identity mapping for text RW. Kernel text |
| 1223 | * mapping will always be RO. Refer to the comment in |
| 1224 | * static_protections() in pageattr.c |
| 1225 | */ |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1226 | set_memory_rw(start, (end - start) >> PAGE_SHIFT); |
| 1227 | } |
| 1228 | |
| 1229 | void set_kernel_text_ro(void) |
| 1230 | { |
Suresh Siddha | b9af7c0 | 2009-10-14 14:46:55 -0700 | [diff] [blame] | 1231 | unsigned long start = PFN_ALIGN(_text); |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1232 | unsigned long end = PFN_ALIGN(__stop___ex_table); |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1233 | |
| 1234 | if (!kernel_set_to_readonly) |
| 1235 | return; |
| 1236 | |
| 1237 | pr_debug("Set kernel text: %lx - %lx for read only\n", |
| 1238 | start, end); |
| 1239 | |
Suresh Siddha | e7d23dd | 2009-10-28 18:46:58 -0800 | [diff] [blame] | 1240 | /* |
| 1241 | * Set the kernel identity mapping for text RO. |
| 1242 | */ |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1243 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
| 1244 | } |
| 1245 | |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1246 | void mark_rodata_ro(void) |
| 1247 | { |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 1248 | unsigned long start = PFN_ALIGN(_text); |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1249 | unsigned long rodata_start = PFN_ALIGN(__start_rodata); |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 1250 | unsigned long end = (unsigned long) &__end_rodata_hpage_align; |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1251 | unsigned long text_end = PFN_ALIGN(&__stop___ex_table); |
| 1252 | unsigned long rodata_end = PFN_ALIGN(&__end_rodata); |
Kees Cook | 45e2a9d | 2014-11-14 11:47:37 -0800 | [diff] [blame] | 1253 | unsigned long all_end; |
Steven Rostedt | 8f0f996 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 1254 | |
Jan Beulich | 6fb1475 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 1255 | printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n", |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 1256 | (end - start) >> 10); |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 1257 | set_memory_ro(start, (end - start) >> PAGE_SHIFT); |
| 1258 | |
Steven Rostedt | 1623963 | 2009-02-17 17:57:30 -0500 | [diff] [blame] | 1259 | kernel_set_to_readonly = 1; |
| 1260 | |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 1261 | /* |
Yinghai Lu | 7221267 | 2013-01-24 12:20:13 -0800 | [diff] [blame] | 1262 | * The rodata/data/bss/brk section (but not the kernel text!) |
| 1263 | * should also be not-executable. |
Kees Cook | 45e2a9d | 2014-11-14 11:47:37 -0800 | [diff] [blame] | 1264 | * |
| 1265 | * We align all_end to PMD_SIZE because the existing mapping |
| 1266 | * is a full PMD. If we would align _brk_end to PAGE_SIZE we |
| 1267 | * split the PMD and the reminder between _brk_end and the end |
| 1268 | * of the PMD will remain mapped executable. |
| 1269 | * |
| 1270 | * Any PMD which was setup after the one which covers _brk_end |
| 1271 | * has been zapped already via cleanup_highmem(). |
Arjan van de Ven | 984bb80 | 2008-02-06 22:39:45 +0100 | [diff] [blame] | 1272 | */ |
Kees Cook | 45e2a9d | 2014-11-14 11:47:37 -0800 | [diff] [blame] | 1273 | all_end = roundup((unsigned long)_brk_end, PMD_SIZE); |
Stephen Smalley | ab76f7b | 2015-10-01 09:04:22 -0400 | [diff] [blame] | 1274 | set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1275 | |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1276 | #ifdef CONFIG_CPA_DEBUG |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1277 | printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1278 | set_memory_rw(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1279 | |
Ingo Molnar | 10f22dd | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1280 | printk(KERN_INFO "Testing CPA: again\n"); |
Arjan van de Ven | 6d238cc | 2008-01-30 13:34:06 +0100 | [diff] [blame] | 1281 | set_memory_ro(start, (end-start) >> PAGE_SHIFT); |
Andi Kleen | 0c42f39 | 2008-01-30 13:33:42 +0100 | [diff] [blame] | 1282 | #endif |
Suresh Siddha | 74e0817 | 2009-10-14 14:46:56 -0700 | [diff] [blame] | 1283 | |
Jiang Liu | c88442e | 2013-07-03 15:02:58 -0700 | [diff] [blame] | 1284 | free_init_pages("unused kernel", |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1285 | (unsigned long) __va(__pa_symbol(text_end)), |
| 1286 | (unsigned long) __va(__pa_symbol(rodata_start))); |
Jiang Liu | c88442e | 2013-07-03 15:02:58 -0700 | [diff] [blame] | 1287 | free_init_pages("unused kernel", |
Alexander Duyck | fc8d782 | 2012-11-16 13:57:13 -0800 | [diff] [blame] | 1288 | (unsigned long) __va(__pa_symbol(rodata_end)), |
| 1289 | (unsigned long) __va(__pa_symbol(_sdata))); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 1290 | |
| 1291 | debug_checkwx(); |
Arjan van de Ven | 67df197 | 2006-01-06 00:12:04 -0800 | [diff] [blame] | 1292 | } |
Mathieu Desnoyers | 4e4eee0 | 2008-02-02 15:42:20 -0500 | [diff] [blame] | 1293 | |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1294 | int kern_addr_valid(unsigned long addr) |
| 1295 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1297 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1298 | p4d_t *p4d; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1299 | pud_t *pud; |
| 1300 | pmd_t *pmd; |
| 1301 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
| 1303 | if (above != 0 && above != -1UL) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1304 | return 0; |
| 1305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | pgd = pgd_offset_k(addr); |
| 1307 | if (pgd_none(*pgd)) |
| 1308 | return 0; |
| 1309 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1310 | p4d = p4d_offset(pgd, addr); |
| 1311 | if (p4d_none(*p4d)) |
| 1312 | return 0; |
| 1313 | |
| 1314 | pud = pud_offset(p4d, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (pud_none(*pud)) |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1316 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
Mel Gorman | 0ee364e | 2013-02-11 14:52:36 +0000 | [diff] [blame] | 1318 | if (pud_large(*pud)) |
| 1319 | return pfn_valid(pud_pfn(*pud)); |
| 1320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | pmd = pmd_offset(pud, addr); |
| 1322 | if (pmd_none(*pmd)) |
| 1323 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | if (pmd_large(*pmd)) |
| 1326 | return pfn_valid(pmd_pfn(*pmd)); |
| 1327 | |
| 1328 | pte = pte_offset_kernel(pmd, addr); |
| 1329 | if (pte_none(*pte)) |
| 1330 | return 0; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return pfn_valid(pte_pfn(*pte)); |
| 1333 | } |
| 1334 | |
Yinghai Lu | 982792c | 2014-06-04 16:06:32 -0700 | [diff] [blame] | 1335 | static unsigned long probe_memory_block_size(void) |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 1336 | { |
Seth Jennings | 43c75f9 | 2015-11-30 10:47:43 -0600 | [diff] [blame] | 1337 | unsigned long bz = MIN_MEMORY_BLOCK_SIZE; |
Yinghai Lu | 982792c | 2014-06-04 16:06:32 -0700 | [diff] [blame] | 1338 | |
Seth Jennings | 43c75f9 | 2015-11-30 10:47:43 -0600 | [diff] [blame] | 1339 | /* if system is UV or has 64GB of RAM or more, use large blocks */ |
| 1340 | if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30))) |
| 1341 | bz = 2UL << 30; /* 2GB */ |
Nathan Fontenot | 1dc41aa | 2011-01-20 10:46:15 -0600 | [diff] [blame] | 1342 | |
Seth Jennings | 43c75f9 | 2015-11-30 10:47:43 -0600 | [diff] [blame] | 1343 | pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20); |
Yinghai Lu | 982792c | 2014-06-04 16:06:32 -0700 | [diff] [blame] | 1344 | |
| 1345 | return bz; |
| 1346 | } |
| 1347 | |
| 1348 | static unsigned long memory_block_size_probed; |
| 1349 | unsigned long memory_block_size_bytes(void) |
| 1350 | { |
| 1351 | if (!memory_block_size_probed) |
| 1352 | memory_block_size_probed = probe_memory_block_size(); |
| 1353 | |
| 1354 | return memory_block_size_probed; |
| 1355 | } |
| 1356 | |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1357 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 1358 | /* |
| 1359 | * Initialise the sparsemem vmemmap using huge-pages at the PMD level. |
| 1360 | */ |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1361 | static long __meminitdata addr_start, addr_end; |
| 1362 | static void __meminitdata *p_start, *p_end; |
| 1363 | static int __meminitdata node_start; |
| 1364 | |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1365 | static int __meminit vmemmap_populate_hugepages(unsigned long start, |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 1366 | unsigned long end, int node, struct vmem_altmap *altmap) |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1367 | { |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 1368 | unsigned long addr; |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1369 | unsigned long next; |
| 1370 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1371 | p4d_t *p4d; |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1372 | pud_t *pud; |
| 1373 | pmd_t *pmd; |
| 1374 | |
Johannes Weiner | 0aad818 | 2013-04-29 15:07:50 -0700 | [diff] [blame] | 1375 | for (addr = start; addr < end; addr = next) { |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1376 | next = pmd_addr_end(addr, end); |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1377 | |
| 1378 | pgd = vmemmap_pgd_populate(addr, node); |
| 1379 | if (!pgd) |
| 1380 | return -ENOMEM; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1381 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1382 | p4d = vmemmap_p4d_populate(pgd, addr, node); |
| 1383 | if (!p4d) |
| 1384 | return -ENOMEM; |
| 1385 | |
| 1386 | pud = vmemmap_pud_populate(p4d, addr, node); |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1387 | if (!pud) |
| 1388 | return -ENOMEM; |
| 1389 | |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1390 | pmd = pmd_offset(pud, addr); |
| 1391 | if (pmd_none(*pmd)) { |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1392 | void *p; |
Thomas Gleixner | 14a62c3 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1393 | |
Christoph Hellwig | a8fc357 | 2017-12-29 08:53:58 +0100 | [diff] [blame] | 1394 | if (altmap) |
| 1395 | p = altmap_alloc_block_buf(PMD_SIZE, altmap); |
| 1396 | else |
| 1397 | p = vmemmap_alloc_block_buf(PMD_SIZE, node); |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1398 | if (p) { |
| 1399 | pte_t entry; |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1400 | |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1401 | entry = pfn_pte(__pa(p) >> PAGE_SHIFT, |
| 1402 | PAGE_KERNEL_LARGE); |
| 1403 | set_pmd(pmd, __pmd(pte_val(entry))); |
Jeremy Fitzhardinge | 7c934d3 | 2008-06-25 00:19:20 -0400 | [diff] [blame] | 1404 | |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1405 | /* check to see if we have contiguous blocks */ |
| 1406 | if (p_end != p || node_start != node) { |
| 1407 | if (p_start) |
Dan Williams | c9cdaeb | 2015-09-17 16:27:57 -0400 | [diff] [blame] | 1408 | pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1409 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1410 | addr_start = addr; |
| 1411 | node_start = node; |
| 1412 | p_start = p; |
| 1413 | } |
| 1414 | |
| 1415 | addr_end = addr + PMD_SIZE; |
| 1416 | p_end = p + PMD_SIZE; |
| 1417 | continue; |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 1418 | } else if (altmap) |
| 1419 | return -ENOMEM; /* no fallback */ |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1420 | } else if (pmd_large(*pmd)) { |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1421 | vmemmap_verify((pte_t *)pmd, node, addr, next); |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1422 | continue; |
| 1423 | } |
Johannes Weiner | 8e2cdbc | 2013-04-29 15:07:56 -0700 | [diff] [blame] | 1424 | if (vmemmap_populate_basepages(addr, next, node)) |
| 1425 | return -ENOMEM; |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1426 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1427 | return 0; |
| 1428 | } |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1429 | |
Christoph Hellwig | 7b73d97 | 2017-12-29 08:53:54 +0100 | [diff] [blame] | 1430 | int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, |
| 1431 | struct vmem_altmap *altmap) |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1432 | { |
| 1433 | int err; |
| 1434 | |
Borislav Petkov | 16bf922 | 2016-03-29 17:42:03 +0200 | [diff] [blame] | 1435 | if (boot_cpu_has(X86_FEATURE_PSE)) |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 1436 | err = vmemmap_populate_hugepages(start, end, node, altmap); |
| 1437 | else if (altmap) { |
| 1438 | pr_err_once("%s: no cpu support for altmap allocations\n", |
| 1439 | __func__); |
| 1440 | err = -ENOMEM; |
| 1441 | } else |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1442 | err = vmemmap_populate_basepages(start, end, node); |
| 1443 | if (!err) |
Kirill A. Shutemov | 5372e15 | 2016-12-15 02:44:03 +0300 | [diff] [blame] | 1444 | sync_global_pgds(start, end - 1); |
Johannes Weiner | e8216da | 2013-04-29 15:07:54 -0700 | [diff] [blame] | 1445 | return err; |
| 1446 | } |
| 1447 | |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1448 | #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE) |
| 1449 | void register_page_bootmem_memmap(unsigned long section_nr, |
Baoquan He | 15670bf | 2017-10-28 09:30:38 +0800 | [diff] [blame] | 1450 | struct page *start_page, unsigned long nr_pages) |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1451 | { |
| 1452 | unsigned long addr = (unsigned long)start_page; |
Baoquan He | 15670bf | 2017-10-28 09:30:38 +0800 | [diff] [blame] | 1453 | unsigned long end = (unsigned long)(start_page + nr_pages); |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1454 | unsigned long next; |
| 1455 | pgd_t *pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1456 | p4d_t *p4d; |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1457 | pud_t *pud; |
| 1458 | pmd_t *pmd; |
Baoquan He | 15670bf | 2017-10-28 09:30:38 +0800 | [diff] [blame] | 1459 | unsigned int nr_pmd_pages; |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1460 | struct page *page; |
| 1461 | |
| 1462 | for (; addr < end; addr = next) { |
| 1463 | pte_t *pte = NULL; |
| 1464 | |
| 1465 | pgd = pgd_offset_k(addr); |
| 1466 | if (pgd_none(*pgd)) { |
| 1467 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1468 | continue; |
| 1469 | } |
| 1470 | get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO); |
| 1471 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 1472 | p4d = p4d_offset(pgd, addr); |
| 1473 | if (p4d_none(*p4d)) { |
| 1474 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1475 | continue; |
| 1476 | } |
| 1477 | get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO); |
| 1478 | |
| 1479 | pud = pud_offset(p4d, addr); |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1480 | if (pud_none(*pud)) { |
| 1481 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1482 | continue; |
| 1483 | } |
| 1484 | get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO); |
| 1485 | |
Borislav Petkov | 16bf922 | 2016-03-29 17:42:03 +0200 | [diff] [blame] | 1486 | if (!boot_cpu_has(X86_FEATURE_PSE)) { |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1487 | next = (addr + PAGE_SIZE) & PAGE_MASK; |
| 1488 | pmd = pmd_offset(pud, addr); |
| 1489 | if (pmd_none(*pmd)) |
| 1490 | continue; |
| 1491 | get_page_bootmem(section_nr, pmd_page(*pmd), |
| 1492 | MIX_SECTION_INFO); |
| 1493 | |
| 1494 | pte = pte_offset_kernel(pmd, addr); |
| 1495 | if (pte_none(*pte)) |
| 1496 | continue; |
| 1497 | get_page_bootmem(section_nr, pte_page(*pte), |
| 1498 | SECTION_INFO); |
| 1499 | } else { |
| 1500 | next = pmd_addr_end(addr, end); |
| 1501 | |
| 1502 | pmd = pmd_offset(pud, addr); |
| 1503 | if (pmd_none(*pmd)) |
| 1504 | continue; |
| 1505 | |
Baoquan He | 15670bf | 2017-10-28 09:30:38 +0800 | [diff] [blame] | 1506 | nr_pmd_pages = 1 << get_order(PMD_SIZE); |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1507 | page = pmd_page(*pmd); |
Baoquan He | 15670bf | 2017-10-28 09:30:38 +0800 | [diff] [blame] | 1508 | while (nr_pmd_pages--) |
Yasuaki Ishimatsu | 46723bf | 2013-02-22 16:33:00 -0800 | [diff] [blame] | 1509 | get_page_bootmem(section_nr, page++, |
| 1510 | SECTION_INFO); |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | #endif |
| 1515 | |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1516 | void __meminit vmemmap_populate_print_last(void) |
| 1517 | { |
| 1518 | if (p_start) { |
Dan Williams | c9cdaeb | 2015-09-17 16:27:57 -0400 | [diff] [blame] | 1519 | pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n", |
Yinghai Lu | c2b91e2 | 2008-04-12 01:19:24 -0700 | [diff] [blame] | 1520 | addr_start, addr_end-1, p_start, p_end-1, node_start); |
| 1521 | p_start = NULL; |
| 1522 | p_end = NULL; |
| 1523 | node_start = 0; |
| 1524 | } |
| 1525 | } |
Christoph Lameter | 0889eba | 2007-10-16 01:24:15 -0700 | [diff] [blame] | 1526 | #endif |