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