blob: 5488d21123bd2edb3f71ded119495474f3e8b728 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/mm/init.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
Pavel Macheka2531292010-07-18 14:27:13 +02005 * Copyright (C) 2000 Pavel Machek <pavel@ucw.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2002,2003 Andi Kleen <ak@suse.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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 Gleixner11034d52008-05-12 15:43:36 +020021#include <linux/initrd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
23#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070024#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/proc_fs.h>
Andi Kleen59170892005-11-05 17:25:53 +010026#include <linux/pci.h>
Jan Beulich6fb14752007-05-02 19:27:10 +020027#include <linux/pfn.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070028#include <linux/poison.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010029#include <linux/dma-mapping.h>
Matt Tolentino44df75e2006-01-17 07:03:41 +010030#include <linux/module.h>
Benjamin Herrenschmidta63fdc52011-06-14 10:57:50 +100031#include <linux/memory.h>
Matt Tolentino44df75e2006-01-17 07:03:41 +010032#include <linux/memory_hotplug.h>
Dan Williams4b94ffd2016-01-15 16:56:22 -080033#include <linux/memremap.h>
Konrad Rzeszutekae32b122007-05-02 19:27:11 +020034#include <linux/nmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/gfp.h>
David Howells2f96b8c2013-04-12 00:10:25 +010036#include <linux/kcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/processor.h>
Ingo Molnar46eaa672008-10-12 15:06:29 +020039#include <asm/bios_ebda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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 Kleen2bc04142005-11-05 17:25:53 +010051#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010052#include <asm/kdebug.h>
Thomas Gleixneraaa64e02008-01-30 13:30:17 +010053#include <asm/numa.h>
Harvey Harrison7bfeab92008-02-12 12:12:01 -080054#include <asm/cacheflush.h>
Pekka Enberg4fcb2082009-03-05 14:55:08 +020055#include <asm/init.h>
Yinghai Lue5f15b42011-02-18 11:30:30 +000056#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Yinghai Lu5c51bdb2012-11-16 19:39:01 -080058#include "mm_internal.h"
59
Yinghai Luaece2782013-01-24 12:19:48 -080060static void ident_pmd_init(unsigned long pmd_flag, pmd_t *pmd_page,
61 unsigned long addr, unsigned long end)
62{
63 addr &= PMD_MASK;
64 for (; addr < end; addr += PMD_SIZE) {
65 pmd_t *pmd = pmd_page + pmd_index(addr);
66
67 if (!pmd_present(*pmd))
68 set_pmd(pmd, __pmd(addr | pmd_flag));
69 }
70}
71static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page,
72 unsigned long addr, unsigned long end)
73{
74 unsigned long next;
75
76 for (; addr < end; addr = next) {
77 pud_t *pud = pud_page + pud_index(addr);
78 pmd_t *pmd;
79
80 next = (addr & PUD_MASK) + PUD_SIZE;
81 if (next > end)
82 next = end;
83
84 if (pud_present(*pud)) {
85 pmd = pmd_offset(pud, 0);
86 ident_pmd_init(info->pmd_flag, pmd, addr, next);
87 continue;
88 }
89 pmd = (pmd_t *)info->alloc_pgt_page(info->context);
90 if (!pmd)
91 return -ENOMEM;
92 ident_pmd_init(info->pmd_flag, pmd, addr, next);
93 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
94 }
95
96 return 0;
97}
98
99int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page,
100 unsigned long addr, unsigned long end)
101{
102 unsigned long next;
103 int result;
104 int off = info->kernel_mapping ? pgd_index(__PAGE_OFFSET) : 0;
105
106 for (; addr < end; addr = next) {
107 pgd_t *pgd = pgd_page + pgd_index(addr) + off;
108 pud_t *pud;
109
110 next = (addr & PGDIR_MASK) + PGDIR_SIZE;
111 if (next > end)
112 next = end;
113
114 if (pgd_present(*pgd)) {
115 pud = pud_offset(pgd, 0);
116 result = ident_pud_init(info, pud, addr, next);
117 if (result)
118 return result;
119 continue;
120 }
121
122 pud = (pud_t *)info->alloc_pgt_page(info->context);
123 if (!pud)
124 return -ENOMEM;
125 result = ident_pud_init(info, pud, addr, next);
126 if (result)
127 return result;
128 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
129 }
130
131 return 0;
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
136 * physical space so we can cache the place of the first one and move
137 * around without checking the pgd every time.
138 */
139
David Vrabelf9553712014-01-07 17:03:06 +0000140pteval_t __supported_pte_mask __read_mostly = ~0;
Yinghai Lubd220a22008-09-05 00:58:28 -0700141EXPORT_SYMBOL_GPL(__supported_pte_mask);
142
Yinghai Lubd220a22008-09-05 00:58:28 -0700143int force_personality32;
144
Ingo Molnardeed05b2008-09-05 10:23:26 +0200145/*
146 * noexec32=on|off
147 * Control non executable heap for 32bit processes.
148 * To control the stack too use noexec=off
149 *
150 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
151 * off PROT_READ implies PROT_EXEC
152 */
Yinghai Lubd220a22008-09-05 00:58:28 -0700153static int __init nonx32_setup(char *str)
154{
155 if (!strcmp(str, "on"))
156 force_personality32 &= ~READ_IMPLIES_EXEC;
157 else if (!strcmp(str, "off"))
158 force_personality32 |= READ_IMPLIES_EXEC;
159 return 1;
160}
161__setup("noexec32=", nonx32_setup);
162
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200163/*
Haicheng Li6afb5152010-05-19 17:42:14 +0800164 * When memory was added/removed make sure all the processes MM have
165 * suitable PGD entries in the local PGD level page.
166 */
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700167void sync_global_pgds(unsigned long start, unsigned long end, int removed)
Haicheng Li6afb5152010-05-19 17:42:14 +0800168{
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700169 unsigned long address;
Haicheng Li6afb5152010-05-19 17:42:14 +0800170
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700171 for (address = start; address <= end; address += PGDIR_SIZE) {
172 const pgd_t *pgd_ref = pgd_offset_k(address);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700173 struct page *page;
Haicheng Li6afb5152010-05-19 17:42:14 +0800174
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700175 /*
176 * When it is called after memory hot remove, pgd_none()
177 * returns true. In this case (removed == 1), we must clear
178 * the PGD entries in the local PGD level page.
179 */
180 if (pgd_none(*pgd_ref) && !removed)
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700181 continue;
Haicheng Li6afb5152010-05-19 17:42:14 +0800182
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800183 spin_lock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700184 list_for_each_entry(page, &pgd_list, lru) {
Linus Torvaldsbe354f42012-12-15 12:29:54 -0800185 pgd_t *pgd;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700186 spinlock_t *pgt_lock;
187
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700188 pgd = (pgd_t *)page_address(page) + pgd_index(address);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800189 /* the pgt_lock only for Xen */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700190 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
191 spin_lock(pgt_lock);
192
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700193 if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700194 BUG_ON(pgd_page_vaddr(*pgd)
195 != pgd_page_vaddr(*pgd_ref));
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700196
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700197 if (removed) {
198 if (pgd_none(*pgd_ref) && !pgd_none(*pgd))
199 pgd_clear(pgd);
200 } else {
201 if (pgd_none(*pgd))
202 set_pgd(pgd, *pgd_ref);
203 }
204
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700205 spin_unlock(pgt_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700206 }
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800207 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700208 }
Haicheng Li6afb5152010-05-19 17:42:14 +0800209}
210
211/*
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200212 * NOTE: This function is marked __ref because it calls __init function
213 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
214 */
215static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (after_bootmem)
Vegard Nossum9e730232009-02-22 11:28:25 +0100220 ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 else
222 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100223
224 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
225 panic("set_pte_phys: cannot allocate page data %s\n",
226 after_bootmem ? "after bootmem" : "");
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100229 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100231 return ptr;
232}
233
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800234static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900235{
236 if (pgd_none(*pgd)) {
237 pud_t *pud = (pud_t *)spp_getpage();
238 pgd_populate(&init_mm, pgd, pud);
239 if (pud != pud_offset(pgd, 0))
240 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
241 pud, pud_offset(pgd, 0));
242 }
243 return pud_offset(pgd, vaddr);
244}
245
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800246static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900247{
248 if (pud_none(*pud)) {
249 pmd_t *pmd = (pmd_t *) spp_getpage();
250 pud_populate(&init_mm, pud, pmd);
251 if (pmd != pmd_offset(pud, 0))
252 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
253 pmd, pmd_offset(pud, 0));
254 }
255 return pmd_offset(pud, vaddr);
256}
257
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800258static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900259{
260 if (pmd_none(*pmd)) {
261 pte_t *pte = (pte_t *) spp_getpage();
262 pmd_populate_kernel(&init_mm, pmd, pte);
263 if (pte != pte_offset_kernel(pmd, 0))
264 printk(KERN_ERR "PAGETABLE BUG #02!\n");
265 }
266 return pte_offset_kernel(pmd, vaddr);
267}
268
269void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 pud_t *pud;
272 pmd_t *pmd;
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700273 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400275 pud = pud_page + pud_index(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900276 pmd = fill_pmd(pud, vaddr);
277 pte = fill_pte(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 set_pte(pte, new_pte);
280
281 /*
282 * It's enough to flush this one mapping.
283 * (PGE mappings get flushed as well)
284 */
285 __flush_tlb_one(vaddr);
286}
287
Tejun Heo458a3e62009-02-24 11:57:21 +0900288void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400289{
290 pgd_t *pgd;
291 pud_t *pud_page;
292
293 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
294
295 pgd = pgd_offset_k(vaddr);
296 if (pgd_none(*pgd)) {
297 printk(KERN_ERR
298 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
299 return;
300 }
301 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
302 set_pte_vaddr_pud(pud_page, vaddr, pteval);
303}
304
Tejun Heo458a3e62009-02-24 11:57:21 +0900305pmd_t * __init populate_extra_pmd(unsigned long vaddr)
Tejun Heo11124412009-02-20 16:29:09 +0900306{
307 pgd_t *pgd;
308 pud_t *pud;
309
310 pgd = pgd_offset_k(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900311 pud = fill_pud(pgd, vaddr);
312 return fill_pmd(pud, vaddr);
313}
Tejun Heo11124412009-02-20 16:29:09 +0900314
Tejun Heo458a3e62009-02-24 11:57:21 +0900315pte_t * __init populate_extra_pte(unsigned long vaddr)
316{
317 pmd_t *pmd;
318
319 pmd = populate_extra_pmd(vaddr);
320 return fill_pte(pmd, vaddr);
Tejun Heo11124412009-02-20 16:29:09 +0900321}
322
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100323/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500324 * Create large page table mappings for a range of physical addresses.
325 */
326static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
Juergen Gross2df58b62014-11-03 14:01:52 +0100327 enum page_cache_mode cache)
Jack Steiner3a9e1892008-07-01 14:45:32 -0500328{
329 pgd_t *pgd;
330 pud_t *pud;
331 pmd_t *pmd;
Juergen Gross2df58b62014-11-03 14:01:52 +0100332 pgprot_t prot;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500333
Juergen Gross2df58b62014-11-03 14:01:52 +0100334 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
335 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500336 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
337 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
338 pgd = pgd_offset_k((unsigned long)__va(phys));
339 if (pgd_none(*pgd)) {
340 pud = (pud_t *) spp_getpage();
341 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
342 _PAGE_USER));
343 }
344 pud = pud_offset(pgd, (unsigned long)__va(phys));
345 if (pud_none(*pud)) {
346 pmd = (pmd_t *) spp_getpage();
347 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
348 _PAGE_USER));
349 }
350 pmd = pmd_offset(pud, phys);
351 BUG_ON(!pmd_none(*pmd));
352 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
353 }
354}
355
356void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
357{
Juergen Gross2df58b62014-11-03 14:01:52 +0100358 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500359}
360
361void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
362{
Juergen Gross2df58b62014-11-03 14:01:52 +0100363 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500364}
365
366/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100367 * The head.S code sets up the kernel high mapping:
368 *
369 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100370 *
Zhang Yanfei1e3b3082013-05-15 10:58:07 +0800371 * phys_base holds the negative offset to the kernel, which is added
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100372 * to the compile time generated pmds. This results in invalid pmds up
373 * to the point where we hit the physaddr 0 mapping.
374 *
Yinghai Lue5f15b42011-02-18 11:30:30 +0000375 * We limit the mappings to the region from _text to _brk_end. _brk_end
376 * is rounded up to the 2MB boundary. This catches the invalid pmds as
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100377 * well, as they are located before _text:
378 */
379void __init cleanup_highmap(void)
380{
381 unsigned long vaddr = __START_KERNEL_map;
Yinghai Lu10054232013-01-24 12:19:54 -0800382 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
Yinghai Lue5f15b42011-02-18 11:30:30 +0000383 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100384 pmd_t *pmd = level2_kernel_pgt;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100385
Yinghai Lu10054232013-01-24 12:19:54 -0800386 /*
387 * Native path, max_pfn_mapped is not set yet.
388 * Xen has valid max_pfn_mapped set in
389 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
390 */
391 if (max_pfn_mapped)
392 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
393
Yinghai Lue5f15b42011-02-18 11:30:30 +0000394 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100395 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100396 continue;
397 if (vaddr < (unsigned long) _text || vaddr > end)
398 set_pmd(pmd, __pmd(0));
399 }
400}
401
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700402static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700403phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
404 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400405{
Yinghai Lueceb3632012-11-16 19:38:56 -0800406 unsigned long pages = 0, next;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700407 unsigned long last_map_addr = end;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400408 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700409
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400410 pte_t *pte = pte_page + pte_index(addr);
411
Yinghai Lueceb3632012-11-16 19:38:56 -0800412 for (i = pte_index(addr); i < PTRS_PER_PTE; i++, addr = next, pte++) {
413 next = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400414 if (addr >= end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800415 if (!after_bootmem &&
416 !e820_any_mapped(addr & PAGE_MASK, next, E820_RAM) &&
417 !e820_any_mapped(addr & PAGE_MASK, next, E820_RESERVED_KERN))
418 set_pte(pte, __pte(0));
419 continue;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400420 }
421
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700422 /*
423 * We will re-use the existing mapping.
424 * Xen for example has some special requirements, like mapping
425 * pagetable pages as RO. So assume someone who pre-setup
426 * these mappings are more intelligent.
427 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700428 if (pte_val(*pte)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100429 if (!after_bootmem)
430 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400431 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700432 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400433
434 if (0)
435 printk(" pte=%p addr=%lx pte=%016lx\n",
436 pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400437 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700438 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400439 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400440 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700441
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400442 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700443
444 return last_map_addr;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400445}
446
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700447static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700448phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700449 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100450{
Jan Beulich20167d32012-05-16 14:06:26 +0100451 unsigned long pages = 0, next;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700452 unsigned long last_map_addr = end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200453
Keith Mannthey6ad91652006-09-26 10:52:36 +0200454 int i = pmd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Jan Beulich20167d32012-05-16 14:06:26 +0100456 for (; i < PTRS_PER_PMD; i++, address = next) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200457 pmd_t *pmd = pmd_page + pmd_index(address);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400458 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700459 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100460
Jan Beulich20167d32012-05-16 14:06:26 +0100461 next = (address & PMD_MASK) + PMD_SIZE;
Yinghai Lueceb3632012-11-16 19:38:56 -0800462 if (address >= end) {
463 if (!after_bootmem &&
464 !e820_any_mapped(address & PMD_MASK, next, E820_RAM) &&
465 !e820_any_mapped(address & PMD_MASK, next, E820_RESERVED_KERN))
466 set_pmd(pmd, __pmd(0));
467 continue;
468 }
Jan Beulich20167d32012-05-16 14:06:26 +0100469
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400470 if (pmd_val(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100471 if (!pmd_large(*pmd)) {
472 spin_lock(&init_mm.page_table_lock);
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800473 pte = (pte_t *)pmd_page_vaddr(*pmd);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800474 last_map_addr = phys_pte_init(pte, address,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700475 end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100476 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700477 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100478 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700479 /*
480 * If we are ok with PG_LEVEL_2M mapping, then we will
481 * use the existing mapping,
482 *
483 * Otherwise, we will split the large page mapping but
484 * use the same existing protection bits except for
485 * large page, so that we don't violate Intel's TLB
486 * Application note (317080) which says, while changing
487 * the page sizes, new and old translations should
488 * not differ with respect to page frame and
489 * attributes.
490 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700491 if (page_size_mask & (1 << PG_LEVEL_2M)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100492 if (!after_bootmem)
493 pages++;
Jan Beulich20167d32012-05-16 14:06:26 +0100494 last_map_addr = next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700495 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700496 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700497 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400498 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200499
Yinghai Lub50efd22008-07-08 01:41:05 -0700500 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400501 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100502 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400503 set_pte((pte_t *)pmd,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800504 pfn_pte((address & PMD_MASK) >> PAGE_SHIFT,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700505 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100506 spin_unlock(&init_mm.page_table_lock);
Jan Beulich20167d32012-05-16 14:06:26 +0100507 last_map_addr = next;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400508 continue;
509 }
510
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800511 pte = alloc_low_page();
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700512 last_map_addr = phys_pte_init(pte, address, end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400513
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100514 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800515 pmd_populate_kernel(&init_mm, pmd, pte);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100516 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100517 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200518 update_page_count(PG_LEVEL_2M, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700519 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100520}
521
Andi Kleencc615032008-03-12 03:53:28 +0100522static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700523phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
524 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100525{
Jan Beulich20167d32012-05-16 14:06:26 +0100526 unsigned long pages = 0, next;
Andi Kleencc615032008-03-12 03:53:28 +0100527 unsigned long last_map_addr = end;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200528 int i = pud_index(addr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100529
Jan Beulich20167d32012-05-16 14:06:26 +0100530 for (; i < PTRS_PER_PUD; i++, addr = next) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200531 pud_t *pud = pud_page + pud_index(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700533 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Jan Beulich20167d32012-05-16 14:06:26 +0100535 next = (addr & PUD_MASK) + PUD_SIZE;
Yinghai Lueceb3632012-11-16 19:38:56 -0800536 if (addr >= end) {
537 if (!after_bootmem &&
538 !e820_any_mapped(addr & PUD_MASK, next, E820_RAM) &&
539 !e820_any_mapped(addr & PUD_MASK, next, E820_RESERVED_KERN))
540 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Keith Mannthey6ad91652006-09-26 10:52:36 +0200544 if (pud_val(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700545 if (!pud_large(*pud)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800546 pmd = pmd_offset(pud, 0);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800547 last_map_addr = phys_pmd_init(pmd, addr, end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700548 page_size_mask, prot);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800549 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700550 continue;
551 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700552 /*
553 * If we are ok with PG_LEVEL_1G mapping, then we will
554 * use the existing mapping.
555 *
556 * Otherwise, we will split the gbpage mapping but use
557 * the same existing protection bits except for large
558 * page, so that we don't violate Intel's TLB
559 * Application note (317080) which says, while changing
560 * the page sizes, new and old translations should
561 * not differ with respect to page frame and
562 * attributes.
563 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700564 if (page_size_mask & (1 << PG_LEVEL_1G)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100565 if (!after_bootmem)
566 pages++;
Jan Beulich20167d32012-05-16 14:06:26 +0100567 last_map_addr = next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700568 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700569 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700570 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200571 }
572
Yinghai Lub50efd22008-07-08 01:41:05 -0700573 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200574 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100575 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200576 set_pte((pte_t *)pud,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800577 pfn_pte((addr & PUD_MASK) >> PAGE_SHIFT,
578 PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100579 spin_unlock(&init_mm.page_table_lock);
Jan Beulich20167d32012-05-16 14:06:26 +0100580 last_map_addr = next;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200581 continue;
582 }
583
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800584 pmd = alloc_low_page();
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700585 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
586 prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100587
588 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800589 pud_populate(&init_mm, pud, pmd);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100590 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100592 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700593
Andi Kleence0c0e52008-05-02 11:46:49 +0200594 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100595
Yinghai Lu1a0db382008-06-24 14:56:20 -0700596 return last_map_addr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100597}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Shaohua Li41d840e2009-06-12 12:57:52 +0800599unsigned long __meminit
Pekka Enbergf7650902009-03-05 14:55:05 +0200600kernel_physical_mapping_init(unsigned long start,
601 unsigned long end,
602 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700603{
Haicheng Li9b861522010-08-20 17:50:16 +0800604 bool pgd_changed = false;
Yinghai Lub50efd22008-07-08 01:41:05 -0700605 unsigned long next, last_map_addr = end;
Haicheng Li9b861522010-08-20 17:50:16 +0800606 unsigned long addr;
Yinghai Lub50efd22008-07-08 01:41:05 -0700607
608 start = (unsigned long)__va(start);
609 end = (unsigned long)__va(end);
Wu Fengguang1c5f50e2010-09-03 17:04:07 +0800610 addr = start;
Yinghai Lub50efd22008-07-08 01:41:05 -0700611
612 for (; start < end; start = next) {
613 pgd_t *pgd = pgd_offset_k(start);
Yinghai Lub50efd22008-07-08 01:41:05 -0700614 pud_t *pud;
615
Yinghai Luc2bdee52013-01-24 12:19:46 -0800616 next = (start & PGDIR_MASK) + PGDIR_SIZE;
Yinghai Lub50efd22008-07-08 01:41:05 -0700617
618 if (pgd_val(*pgd)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800619 pud = (pud_t *)pgd_page_vaddr(*pgd);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800620 last_map_addr = phys_pud_init(pud, __pa(start),
Yinghai Lub50efd22008-07-08 01:41:05 -0700621 __pa(end), page_size_mask);
622 continue;
623 }
624
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800625 pud = alloc_low_page();
Yinghai Luc2bdee52013-01-24 12:19:46 -0800626 last_map_addr = phys_pud_init(pud, __pa(start), __pa(end),
Yinghai Lub50efd22008-07-08 01:41:05 -0700627 page_size_mask);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100628
629 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800630 pgd_populate(&init_mm, pgd, pud);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100631 spin_unlock(&init_mm.page_table_lock);
Haicheng Li9b861522010-08-20 17:50:16 +0800632 pgd_changed = true;
Yinghai Lub50efd22008-07-08 01:41:05 -0700633 }
Haicheng Li9b861522010-08-20 17:50:16 +0800634
635 if (pgd_changed)
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700636 sync_global_pgds(addr, end - 1, 0);
Haicheng Li9b861522010-08-20 17:50:16 +0800637
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700638 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700639
640 return last_map_addr;
641}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700642
Matt Tolentino2b976902005-06-23 00:08:06 -0700643#ifndef CONFIG_NUMA
Tejun Heod8fc3af2011-02-16 12:13:06 +0100644void __init initmem_init(void)
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700645{
Tang Chene7e8de52014-01-21 15:49:26 -0800646 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700647}
Pekka Enberg3551f882009-05-07 15:35:41 +0300648#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650void __init paging_init(void)
651{
Pekka Enberg3551f882009-05-07 15:35:41 +0300652 sparse_memory_present_with_active_regions(MAX_NUMNODES);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100653 sparse_init();
Yinghai Lu44b57282009-07-08 09:50:19 -0700654
655 /*
656 * clear the default setting with node 0
657 * note: don't use nodes_clear here, that is really clearing when
658 * numa support is not compiled in, and later node_set_state
659 * will not set it back.
660 */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -0800661 node_clear_state(0, N_MEMORY);
662 if (N_MEMORY != N_NORMAL_MEMORY)
663 node_clear_state(0, N_NORMAL_MEMORY);
Yinghai Lu44b57282009-07-08 09:50:19 -0700664
Pekka Enberg4c0b2e52011-11-01 15:58:17 +0200665 zone_sizes_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100668/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100669 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100670 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700671#ifdef CONFIG_MEMORY_HOTPLUG
672/*
Shaohui Zhengea085412010-02-02 13:44:16 -0800673 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
674 * updating.
675 */
676static void update_end_of_memory_vars(u64 start, u64 size)
677{
678 unsigned long end_pfn = PFN_UP(start + size);
679
680 if (end_pfn > max_pfn) {
681 max_pfn = end_pfn;
682 max_low_pfn = end_pfn;
683 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
684 }
685}
686
687/*
Yasunori Gotobc02af92006-06-27 02:53:30 -0700688 * Memory is added always to NORMAL zone. This means you will never get
689 * additional DMA/DMA32 memory.
690 */
Dan Williams033fbae2015-08-09 15:29:06 -0400691int arch_add_memory(int nid, u64 start, u64 size, bool for_device)
Yasunori Gotobc02af92006-06-27 02:53:30 -0700692{
693 struct pglist_data *pgdat = NODE_DATA(nid);
Wang Nan9bfc4112014-08-06 16:07:38 -0700694 struct zone *zone = pgdat->node_zones +
Dan Williams033fbae2015-08-09 15:29:06 -0400695 zone_for_memory(nid, start, size, ZONE_NORMAL, for_device);
Jacob Shin66520eb2012-11-16 19:38:52 -0800696 unsigned long start_pfn = start >> PAGE_SHIFT;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700697 unsigned long nr_pages = size >> PAGE_SHIFT;
698 int ret;
699
Jacob Shin66520eb2012-11-16 19:38:52 -0800700 init_memory_mapping(start, start + size);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700701
Gary Hadec04fc582009-01-06 14:39:14 -0800702 ret = __add_pages(nid, zone, start_pfn, nr_pages);
Gary Hadefe8b8682008-10-28 16:43:14 -0700703 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700704
Shaohui Zhengea085412010-02-02 13:44:16 -0800705 /* update max_pfn, max_low_pfn and high_memory */
706 update_end_of_memory_vars(start, size);
707
Yasunori Gotobc02af92006-06-27 02:53:30 -0700708 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700709}
710EXPORT_SYMBOL_GPL(arch_add_memory);
711
Wen Congyangae9aae92013-02-22 16:33:04 -0800712#define PAGE_INUSE 0xFD
713
714static void __meminit free_pagetable(struct page *page, int order)
715{
Wen Congyangae9aae92013-02-22 16:33:04 -0800716 unsigned long magic;
717 unsigned int nr_pages = 1 << order;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800718 struct vmem_altmap *altmap = to_vmem_altmap((unsigned long) page);
719
720 if (altmap) {
721 vmem_altmap_free(altmap, nr_pages);
722 return;
723 }
Wen Congyangae9aae92013-02-22 16:33:04 -0800724
725 /* bootmem page has reserved flag */
726 if (PageReserved(page)) {
727 __ClearPageReserved(page);
Wen Congyangae9aae92013-02-22 16:33:04 -0800728
729 magic = (unsigned long)page->lru.next;
730 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
731 while (nr_pages--)
732 put_page_bootmem(page++);
733 } else
Jiang Liu170a5a72013-07-03 15:03:17 -0700734 while (nr_pages--)
735 free_reserved_page(page++);
Wen Congyangae9aae92013-02-22 16:33:04 -0800736 } else
737 free_pages((unsigned long)page_address(page), order);
Wen Congyangae9aae92013-02-22 16:33:04 -0800738}
739
740static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
741{
742 pte_t *pte;
743 int i;
744
745 for (i = 0; i < PTRS_PER_PTE; i++) {
746 pte = pte_start + i;
747 if (pte_val(*pte))
748 return;
749 }
750
751 /* free a pte talbe */
752 free_pagetable(pmd_page(*pmd), 0);
753 spin_lock(&init_mm.page_table_lock);
754 pmd_clear(pmd);
755 spin_unlock(&init_mm.page_table_lock);
756}
757
758static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
759{
760 pmd_t *pmd;
761 int i;
762
763 for (i = 0; i < PTRS_PER_PMD; i++) {
764 pmd = pmd_start + i;
765 if (pmd_val(*pmd))
766 return;
767 }
768
769 /* free a pmd talbe */
770 free_pagetable(pud_page(*pud), 0);
771 spin_lock(&init_mm.page_table_lock);
772 pud_clear(pud);
773 spin_unlock(&init_mm.page_table_lock);
774}
775
776/* Return true if pgd is changed, otherwise return false. */
777static bool __meminit free_pud_table(pud_t *pud_start, pgd_t *pgd)
778{
779 pud_t *pud;
780 int i;
781
782 for (i = 0; i < PTRS_PER_PUD; i++) {
783 pud = pud_start + i;
784 if (pud_val(*pud))
785 return false;
786 }
787
788 /* free a pud table */
789 free_pagetable(pgd_page(*pgd), 0);
790 spin_lock(&init_mm.page_table_lock);
791 pgd_clear(pgd);
792 spin_unlock(&init_mm.page_table_lock);
793
794 return true;
795}
796
797static void __meminit
798remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
799 bool direct)
800{
801 unsigned long next, pages = 0;
802 pte_t *pte;
803 void *page_addr;
804 phys_addr_t phys_addr;
805
806 pte = pte_start + pte_index(addr);
807 for (; addr < end; addr = next, pte++) {
808 next = (addr + PAGE_SIZE) & PAGE_MASK;
809 if (next > end)
810 next = end;
811
812 if (!pte_present(*pte))
813 continue;
814
815 /*
816 * We mapped [0,1G) memory as identity mapping when
817 * initializing, in arch/x86/kernel/head_64.S. These
818 * pagetables cannot be removed.
819 */
820 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
821 if (phys_addr < (phys_addr_t)0x40000000)
822 return;
823
Kefeng Wangb500f772016-01-12 10:19:30 +0800824 if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
Wen Congyangae9aae92013-02-22 16:33:04 -0800825 /*
826 * Do not free direct mapping pages since they were
827 * freed when offlining, or simplely not in use.
828 */
829 if (!direct)
830 free_pagetable(pte_page(*pte), 0);
831
832 spin_lock(&init_mm.page_table_lock);
833 pte_clear(&init_mm, addr, pte);
834 spin_unlock(&init_mm.page_table_lock);
835
836 /* For non-direct mapping, pages means nothing. */
837 pages++;
838 } else {
839 /*
840 * If we are here, we are freeing vmemmap pages since
841 * direct mapped memory ranges to be freed are aligned.
842 *
843 * If we are not removing the whole page, it means
844 * other page structs in this page are being used and
845 * we canot remove them. So fill the unused page_structs
846 * with 0xFD, and remove the page when it is wholly
847 * filled with 0xFD.
848 */
849 memset((void *)addr, PAGE_INUSE, next - addr);
850
851 page_addr = page_address(pte_page(*pte));
852 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
853 free_pagetable(pte_page(*pte), 0);
854
855 spin_lock(&init_mm.page_table_lock);
856 pte_clear(&init_mm, addr, pte);
857 spin_unlock(&init_mm.page_table_lock);
858 }
859 }
860 }
861
862 /* Call free_pte_table() in remove_pmd_table(). */
863 flush_tlb_all();
864 if (direct)
865 update_page_count(PG_LEVEL_4K, -pages);
866}
867
868static void __meminit
869remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
870 bool direct)
871{
872 unsigned long next, pages = 0;
873 pte_t *pte_base;
874 pmd_t *pmd;
875 void *page_addr;
876
877 pmd = pmd_start + pmd_index(addr);
878 for (; addr < end; addr = next, pmd++) {
879 next = pmd_addr_end(addr, end);
880
881 if (!pmd_present(*pmd))
882 continue;
883
884 if (pmd_large(*pmd)) {
885 if (IS_ALIGNED(addr, PMD_SIZE) &&
886 IS_ALIGNED(next, PMD_SIZE)) {
887 if (!direct)
888 free_pagetable(pmd_page(*pmd),
889 get_order(PMD_SIZE));
890
891 spin_lock(&init_mm.page_table_lock);
892 pmd_clear(pmd);
893 spin_unlock(&init_mm.page_table_lock);
894 pages++;
895 } else {
896 /* If here, we are freeing vmemmap pages. */
897 memset((void *)addr, PAGE_INUSE, next - addr);
898
899 page_addr = page_address(pmd_page(*pmd));
900 if (!memchr_inv(page_addr, PAGE_INUSE,
901 PMD_SIZE)) {
902 free_pagetable(pmd_page(*pmd),
903 get_order(PMD_SIZE));
904
905 spin_lock(&init_mm.page_table_lock);
906 pmd_clear(pmd);
907 spin_unlock(&init_mm.page_table_lock);
908 }
909 }
910
911 continue;
912 }
913
914 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
915 remove_pte_table(pte_base, addr, next, direct);
916 free_pte_table(pte_base, pmd);
917 }
918
919 /* Call free_pmd_table() in remove_pud_table(). */
920 if (direct)
921 update_page_count(PG_LEVEL_2M, -pages);
922}
923
924static void __meminit
925remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
926 bool direct)
927{
928 unsigned long next, pages = 0;
929 pmd_t *pmd_base;
930 pud_t *pud;
931 void *page_addr;
932
933 pud = pud_start + pud_index(addr);
934 for (; addr < end; addr = next, pud++) {
935 next = pud_addr_end(addr, end);
936
937 if (!pud_present(*pud))
938 continue;
939
940 if (pud_large(*pud)) {
941 if (IS_ALIGNED(addr, PUD_SIZE) &&
942 IS_ALIGNED(next, PUD_SIZE)) {
943 if (!direct)
944 free_pagetable(pud_page(*pud),
945 get_order(PUD_SIZE));
946
947 spin_lock(&init_mm.page_table_lock);
948 pud_clear(pud);
949 spin_unlock(&init_mm.page_table_lock);
950 pages++;
951 } else {
952 /* If here, we are freeing vmemmap pages. */
953 memset((void *)addr, PAGE_INUSE, next - addr);
954
955 page_addr = page_address(pud_page(*pud));
956 if (!memchr_inv(page_addr, PAGE_INUSE,
957 PUD_SIZE)) {
958 free_pagetable(pud_page(*pud),
959 get_order(PUD_SIZE));
960
961 spin_lock(&init_mm.page_table_lock);
962 pud_clear(pud);
963 spin_unlock(&init_mm.page_table_lock);
964 }
965 }
966
967 continue;
968 }
969
970 pmd_base = (pmd_t *)pud_page_vaddr(*pud);
971 remove_pmd_table(pmd_base, addr, next, direct);
972 free_pmd_table(pmd_base, pud);
973 }
974
975 if (direct)
976 update_page_count(PG_LEVEL_1G, -pages);
977}
978
979/* start and end are both virtual address. */
980static void __meminit
981remove_pagetable(unsigned long start, unsigned long end, bool direct)
982{
983 unsigned long next;
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -0700984 unsigned long addr;
Wen Congyangae9aae92013-02-22 16:33:04 -0800985 pgd_t *pgd;
986 pud_t *pud;
987 bool pgd_changed = false;
988
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -0700989 for (addr = start; addr < end; addr = next) {
990 next = pgd_addr_end(addr, end);
Wen Congyangae9aae92013-02-22 16:33:04 -0800991
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -0700992 pgd = pgd_offset_k(addr);
Wen Congyangae9aae92013-02-22 16:33:04 -0800993 if (!pgd_present(*pgd))
994 continue;
995
996 pud = (pud_t *)pgd_page_vaddr(*pgd);
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -0700997 remove_pud_table(pud, addr, next, direct);
Wen Congyangae9aae92013-02-22 16:33:04 -0800998 if (free_pud_table(pud, pgd))
999 pgd_changed = true;
1000 }
1001
1002 if (pgd_changed)
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -07001003 sync_global_pgds(start, end - 1, 1);
Wen Congyangae9aae92013-02-22 16:33:04 -08001004
1005 flush_tlb_all();
1006}
1007
Johannes Weiner0aad8182013-04-29 15:07:50 -07001008void __ref vmemmap_free(unsigned long start, unsigned long end)
Tang Chen01975182013-02-22 16:33:08 -08001009{
Tang Chen01975182013-02-22 16:33:08 -08001010 remove_pagetable(start, end, false);
1011}
1012
Tang Chen587ff8c2013-04-15 17:46:46 +08001013#ifdef CONFIG_MEMORY_HOTREMOVE
Tang Chenbbcab872013-02-22 16:33:06 -08001014static void __meminit
1015kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1016{
1017 start = (unsigned long)__va(start);
1018 end = (unsigned long)__va(end);
1019
1020 remove_pagetable(start, end, true);
1021}
1022
Wen Congyang24d335c2013-02-22 16:32:58 -08001023int __ref arch_remove_memory(u64 start, u64 size)
1024{
1025 unsigned long start_pfn = start >> PAGE_SHIFT;
1026 unsigned long nr_pages = size >> PAGE_SHIFT;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001027 struct page *page = pfn_to_page(start_pfn);
1028 struct vmem_altmap *altmap;
Wen Congyang24d335c2013-02-22 16:32:58 -08001029 struct zone *zone;
1030 int ret;
1031
Dan Williams4b94ffd2016-01-15 16:56:22 -08001032 /* With altmap the first mapped page is offset from @start */
1033 altmap = to_vmem_altmap((unsigned long) page);
1034 if (altmap)
1035 page += vmem_altmap_offset(altmap);
1036 zone = page_zone(page);
Wen Congyang24d335c2013-02-22 16:32:58 -08001037 ret = __remove_pages(zone, start_pfn, nr_pages);
1038 WARN_ON_ONCE(ret);
Dan Williams4b94ffd2016-01-15 16:56:22 -08001039 kernel_physical_mapping_remove(start, start + size);
Wen Congyang24d335c2013-02-22 16:32:58 -08001040
1041 return ret;
1042}
1043#endif
Keith Mannthey45e0b782006-09-30 23:27:09 -07001044#endif /* CONFIG_MEMORY_HOTPLUG */
1045
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -07001046static struct kcore_list kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Yinghai Lu94b43c32012-11-16 19:39:19 -08001048static void __init register_page_bootmem_info(void)
1049{
1050#ifdef CONFIG_NUMA
1051 int i;
1052
1053 for_each_online_node(i)
1054 register_page_bootmem_info_node(NODE_DATA(i));
1055#endif
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058void __init mem_init(void)
1059{
Jon Mason0dc243a2006-06-26 13:58:11 +02001060 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Yinghai Lu48ddb152008-01-30 13:32:36 +01001062 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Yinghai Lu94b43c32012-11-16 19:39:19 -08001064 register_page_bootmem_info();
Jiang Liubced0e32013-04-29 15:06:53 -07001065
1066 /* this will put all memory onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -07001067 free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 after_bootmem = 1;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* Register memory areas for /proc/kcore */
Andy Lutomirskif40c3302014-05-05 12:19:36 -07001071 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR,
1072 PAGE_SIZE, KCORE_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Jiang Liu46a84132013-07-03 15:04:19 -07001074 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Arjan van de Ven67df1972006-01-06 00:12:04 -08001077#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +01001078const int rodata_test_data = 0xC3;
1079EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001080
Suresh Siddha502f6602009-10-28 18:46:56 -08001081int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -05001082
1083void set_kernel_text_rw(void)
1084{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001085 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001086 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001087
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 Siddhae7d23dd2009-10-28 18:46:58 -08001094 /*
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 Rostedt16239632009-02-17 17:57:30 -05001099 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1100}
1101
1102void set_kernel_text_ro(void)
1103{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001104 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001105 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001106
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 Siddhae7d23dd2009-10-28 18:46:58 -08001113 /*
1114 * Set the kernel identity mapping for text RO.
1115 */
Steven Rostedt16239632009-02-17 17:57:30 -05001116 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1117}
1118
Arjan van de Ven67df1972006-01-06 00:12:04 -08001119void mark_rodata_ro(void)
1120{
Suresh Siddha74e08172009-10-14 14:46:56 -07001121 unsigned long start = PFN_ALIGN(_text);
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001122 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
Suresh Siddha74e08172009-10-14 14:46:56 -07001123 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001124 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1125 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
Kees Cook45e2a9d2014-11-14 11:47:37 -08001126 unsigned long all_end;
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001127
Jan Beulich6fb14752007-05-02 19:27:10 +02001128 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001129 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +01001130 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1131
Steven Rostedt16239632009-02-17 17:57:30 -05001132 kernel_set_to_readonly = 1;
1133
Arjan van de Ven984bb802008-02-06 22:39:45 +01001134 /*
Yinghai Lu72212672013-01-24 12:20:13 -08001135 * The rodata/data/bss/brk section (but not the kernel text!)
1136 * should also be not-executable.
Kees Cook45e2a9d2014-11-14 11:47:37 -08001137 *
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 Ven984bb802008-02-06 22:39:45 +01001145 */
Kees Cook45e2a9d2014-11-14 11:47:37 -08001146 all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
Stephen Smalleyab76f7b2015-10-01 09:04:22 -04001147 set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001148
Arjan van de Ven1a487252008-01-30 13:34:09 +01001149 rodata_test();
1150
Andi Kleen0c42f392008-01-30 13:33:42 +01001151#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001152 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001153 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001154
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001155 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001156 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001157#endif
Suresh Siddha74e08172009-10-14 14:46:56 -07001158
Jiang Liuc88442e2013-07-03 15:02:58 -07001159 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001160 (unsigned long) __va(__pa_symbol(text_end)),
1161 (unsigned long) __va(__pa_symbol(rodata_start)));
Jiang Liuc88442e2013-07-03 15:02:58 -07001162 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001163 (unsigned long) __va(__pa_symbol(rodata_end)),
1164 (unsigned long) __va(__pa_symbol(_sdata)));
Stephen Smalleye1a58322015-10-05 12:55:20 -04001165
1166 debug_checkwx();
Arjan van de Ven67df1972006-01-06 00:12:04 -08001167}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001168
Arjan van de Ven67df1972006-01-06 00:12:04 -08001169#endif
1170
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001171int kern_addr_valid(unsigned long addr)
1172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001174 pgd_t *pgd;
1175 pud_t *pud;
1176 pmd_t *pmd;
1177 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001180 return 0;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 pgd = pgd_offset_k(addr);
1183 if (pgd_none(*pgd))
1184 return 0;
1185
1186 pud = pud_offset(pgd, addr);
1187 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Mel Gorman0ee364e2013-02-11 14:52:36 +00001190 if (pud_large(*pud))
1191 return pfn_valid(pud_pfn(*pud));
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 pmd = pmd_offset(pud, addr);
1194 if (pmd_none(*pmd))
1195 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (pmd_large(*pmd))
1198 return pfn_valid(pmd_pfn(*pmd));
1199
1200 pte = pte_offset_kernel(pmd, addr);
1201 if (pte_none(*pte))
1202 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return pfn_valid(pte_pfn(*pte));
1205}
1206
Yinghai Lu982792c2014-06-04 16:06:32 -07001207static unsigned long probe_memory_block_size(void)
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001208{
Yinghai Lu982792c2014-06-04 16:06:32 -07001209 /* start from 2g */
1210 unsigned long bz = 1UL<<31;
1211
Daniel J Bluemanbdee2372014-11-04 16:29:44 +08001212 if (totalram_pages >= (64ULL << (30 - PAGE_SHIFT))) {
1213 pr_info("Using 2GB memory block size for large-memory system\n");
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001214 return 2UL * 1024 * 1024 * 1024;
1215 }
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001216
Yinghai Lu982792c2014-06-04 16:06:32 -07001217 /* less than 64g installed */
1218 if ((max_pfn << PAGE_SHIFT) < (16UL << 32))
1219 return MIN_MEMORY_BLOCK_SIZE;
1220
1221 /* get the tail size */
1222 while (bz > MIN_MEMORY_BLOCK_SIZE) {
1223 if (!((max_pfn << PAGE_SHIFT) & (bz - 1)))
1224 break;
1225 bz >>= 1;
1226 }
1227
1228 printk(KERN_DEBUG "memory block size : %ldMB\n", bz >> 20);
1229
1230 return bz;
1231}
1232
1233static unsigned long memory_block_size_probed;
1234unsigned long memory_block_size_bytes(void)
1235{
1236 if (!memory_block_size_probed)
1237 memory_block_size_probed = probe_memory_block_size();
1238
1239 return memory_block_size_probed;
1240}
1241
Christoph Lameter0889eba2007-10-16 01:24:15 -07001242#ifdef CONFIG_SPARSEMEM_VMEMMAP
1243/*
1244 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1245 */
Yinghai Luc2b91e22008-04-12 01:19:24 -07001246static long __meminitdata addr_start, addr_end;
1247static void __meminitdata *p_start, *p_end;
1248static int __meminitdata node_start;
1249
Johannes Weinere8216da2013-04-29 15:07:54 -07001250static int __meminit vmemmap_populate_hugepages(unsigned long start,
Dan Williams4b94ffd2016-01-15 16:56:22 -08001251 unsigned long end, int node, struct vmem_altmap *altmap)
Christoph Lameter0889eba2007-10-16 01:24:15 -07001252{
Johannes Weiner0aad8182013-04-29 15:07:50 -07001253 unsigned long addr;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001254 unsigned long next;
1255 pgd_t *pgd;
1256 pud_t *pud;
1257 pmd_t *pmd;
1258
Johannes Weiner0aad8182013-04-29 15:07:50 -07001259 for (addr = start; addr < end; addr = next) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001260 next = pmd_addr_end(addr, end);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001261
1262 pgd = vmemmap_pgd_populate(addr, node);
1263 if (!pgd)
1264 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001265
Christoph Lameter0889eba2007-10-16 01:24:15 -07001266 pud = vmemmap_pud_populate(pgd, addr, node);
1267 if (!pud)
1268 return -ENOMEM;
1269
Johannes Weinere8216da2013-04-29 15:07:54 -07001270 pmd = pmd_offset(pud, addr);
1271 if (pmd_none(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001272 void *p;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001273
Dan Williams4b94ffd2016-01-15 16:56:22 -08001274 p = __vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001275 if (p) {
1276 pte_t entry;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001277
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001278 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1279 PAGE_KERNEL_LARGE);
1280 set_pmd(pmd, __pmd(pte_val(entry)));
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001281
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001282 /* check to see if we have contiguous blocks */
1283 if (p_end != p || node_start != node) {
1284 if (p_start)
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001285 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001286 addr_start, addr_end-1, p_start, p_end-1, node_start);
1287 addr_start = addr;
1288 node_start = node;
1289 p_start = p;
1290 }
1291
1292 addr_end = addr + PMD_SIZE;
1293 p_end = p + PMD_SIZE;
1294 continue;
Dan Williams4b94ffd2016-01-15 16:56:22 -08001295 } else if (altmap)
1296 return -ENOMEM; /* no fallback */
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001297 } else if (pmd_large(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001298 vmemmap_verify((pte_t *)pmd, node, addr, next);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001299 continue;
1300 }
1301 pr_warn_once("vmemmap: falling back to regular page backing\n");
1302 if (vmemmap_populate_basepages(addr, next, node))
1303 return -ENOMEM;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001304 }
Christoph Lameter0889eba2007-10-16 01:24:15 -07001305 return 0;
1306}
Yinghai Luc2b91e22008-04-12 01:19:24 -07001307
Johannes Weinere8216da2013-04-29 15:07:54 -07001308int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
1309{
Dan Williams4b94ffd2016-01-15 16:56:22 -08001310 struct vmem_altmap *altmap = to_vmem_altmap(start);
Johannes Weinere8216da2013-04-29 15:07:54 -07001311 int err;
1312
1313 if (cpu_has_pse)
Dan Williams4b94ffd2016-01-15 16:56:22 -08001314 err = vmemmap_populate_hugepages(start, end, node, altmap);
1315 else if (altmap) {
1316 pr_err_once("%s: no cpu support for altmap allocations\n",
1317 __func__);
1318 err = -ENOMEM;
1319 } else
Johannes Weinere8216da2013-04-29 15:07:54 -07001320 err = vmemmap_populate_basepages(start, end, node);
1321 if (!err)
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -07001322 sync_global_pgds(start, end - 1, 0);
Johannes Weinere8216da2013-04-29 15:07:54 -07001323 return err;
1324}
1325
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001326#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1327void register_page_bootmem_memmap(unsigned long section_nr,
1328 struct page *start_page, unsigned long size)
1329{
1330 unsigned long addr = (unsigned long)start_page;
1331 unsigned long end = (unsigned long)(start_page + size);
1332 unsigned long next;
1333 pgd_t *pgd;
1334 pud_t *pud;
1335 pmd_t *pmd;
1336 unsigned int nr_pages;
1337 struct page *page;
1338
1339 for (; addr < end; addr = next) {
1340 pte_t *pte = NULL;
1341
1342 pgd = pgd_offset_k(addr);
1343 if (pgd_none(*pgd)) {
1344 next = (addr + PAGE_SIZE) & PAGE_MASK;
1345 continue;
1346 }
1347 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1348
1349 pud = pud_offset(pgd, addr);
1350 if (pud_none(*pud)) {
1351 next = (addr + PAGE_SIZE) & PAGE_MASK;
1352 continue;
1353 }
1354 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1355
1356 if (!cpu_has_pse) {
1357 next = (addr + PAGE_SIZE) & PAGE_MASK;
1358 pmd = pmd_offset(pud, addr);
1359 if (pmd_none(*pmd))
1360 continue;
1361 get_page_bootmem(section_nr, pmd_page(*pmd),
1362 MIX_SECTION_INFO);
1363
1364 pte = pte_offset_kernel(pmd, addr);
1365 if (pte_none(*pte))
1366 continue;
1367 get_page_bootmem(section_nr, pte_page(*pte),
1368 SECTION_INFO);
1369 } else {
1370 next = pmd_addr_end(addr, end);
1371
1372 pmd = pmd_offset(pud, addr);
1373 if (pmd_none(*pmd))
1374 continue;
1375
1376 nr_pages = 1 << (get_order(PMD_SIZE));
1377 page = pmd_page(*pmd);
1378 while (nr_pages--)
1379 get_page_bootmem(section_nr, page++,
1380 SECTION_INFO);
1381 }
1382 }
1383}
1384#endif
1385
Yinghai Luc2b91e22008-04-12 01:19:24 -07001386void __meminit vmemmap_populate_print_last(void)
1387{
1388 if (p_start) {
Dan Williamsc9cdaeb2015-09-17 16:27:57 -04001389 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
Yinghai Luc2b91e22008-04-12 01:19:24 -07001390 addr_start, addr_end-1, p_start, p_end-1, node_start);
1391 p_start = NULL;
1392 p_end = NULL;
1393 node_start = 0;
1394 }
1395}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001396#endif