blob: bd42786f078b3679427d8de46709cc4e4494648e [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>
Konrad Rzeszutekae32b122007-05-02 19:27:11 +020033#include <linux/nmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
David Howells2f96b8c2013-04-12 00:10:25 +010035#include <linux/kcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/processor.h>
Ingo Molnar46eaa672008-10-12 15:06:29 +020038#include <asm/bios_ebda.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40#include <asm/pgtable.h>
41#include <asm/pgalloc.h>
42#include <asm/dma.h>
43#include <asm/fixmap.h>
44#include <asm/e820.h>
45#include <asm/apic.h>
46#include <asm/tlb.h>
47#include <asm/mmu_context.h>
48#include <asm/proto.h>
49#include <asm/smp.h>
Andi Kleen2bc04142005-11-05 17:25:53 +010050#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010051#include <asm/kdebug.h>
Thomas Gleixneraaa64e02008-01-30 13:30:17 +010052#include <asm/numa.h>
Harvey Harrison7bfeab92008-02-12 12:12:01 -080053#include <asm/cacheflush.h>
Pekka Enberg4fcb2082009-03-05 14:55:08 +020054#include <asm/init.h>
Yinghai Lue5f15b42011-02-18 11:30:30 +000055#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Yinghai Lu5c51bdb2012-11-16 19:39:01 -080057#include "mm_internal.h"
58
Yinghai Luaece2782013-01-24 12:19:48 -080059static void ident_pmd_init(unsigned long pmd_flag, pmd_t *pmd_page,
60 unsigned long addr, unsigned long end)
61{
62 addr &= PMD_MASK;
63 for (; addr < end; addr += PMD_SIZE) {
64 pmd_t *pmd = pmd_page + pmd_index(addr);
65
66 if (!pmd_present(*pmd))
67 set_pmd(pmd, __pmd(addr | pmd_flag));
68 }
69}
70static int ident_pud_init(struct x86_mapping_info *info, pud_t *pud_page,
71 unsigned long addr, unsigned long end)
72{
73 unsigned long next;
74
75 for (; addr < end; addr = next) {
76 pud_t *pud = pud_page + pud_index(addr);
77 pmd_t *pmd;
78
79 next = (addr & PUD_MASK) + PUD_SIZE;
80 if (next > end)
81 next = end;
82
83 if (pud_present(*pud)) {
84 pmd = pmd_offset(pud, 0);
85 ident_pmd_init(info->pmd_flag, pmd, addr, next);
86 continue;
87 }
88 pmd = (pmd_t *)info->alloc_pgt_page(info->context);
89 if (!pmd)
90 return -ENOMEM;
91 ident_pmd_init(info->pmd_flag, pmd, addr, next);
92 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
93 }
94
95 return 0;
96}
97
98int kernel_ident_mapping_init(struct x86_mapping_info *info, pgd_t *pgd_page,
99 unsigned long addr, unsigned long end)
100{
101 unsigned long next;
102 int result;
103 int off = info->kernel_mapping ? pgd_index(__PAGE_OFFSET) : 0;
104
105 for (; addr < end; addr = next) {
106 pgd_t *pgd = pgd_page + pgd_index(addr) + off;
107 pud_t *pud;
108
109 next = (addr & PGDIR_MASK) + PGDIR_SIZE;
110 if (next > end)
111 next = end;
112
113 if (pgd_present(*pgd)) {
114 pud = pud_offset(pgd, 0);
115 result = ident_pud_init(info, pud, addr, next);
116 if (result)
117 return result;
118 continue;
119 }
120
121 pud = (pud_t *)info->alloc_pgt_page(info->context);
122 if (!pud)
123 return -ENOMEM;
124 result = ident_pud_init(info, pud, addr, next);
125 if (result)
126 return result;
127 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE));
128 }
129
130 return 0;
131}
132
Ingo Molnar00d1c5e2008-04-17 17:40:45 +0200133static int __init parse_direct_gbpages_off(char *arg)
134{
135 direct_gbpages = 0;
136 return 0;
137}
138early_param("nogbpages", parse_direct_gbpages_off);
139
140static int __init parse_direct_gbpages_on(char *arg)
141{
142 direct_gbpages = 1;
143 return 0;
144}
145early_param("gbpages", parse_direct_gbpages_on);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
148 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
149 * physical space so we can cache the place of the first one and move
150 * around without checking the pgd every time.
151 */
152
David Vrabelf9553712014-01-07 17:03:06 +0000153pteval_t __supported_pte_mask __read_mostly = ~0;
Yinghai Lubd220a22008-09-05 00:58:28 -0700154EXPORT_SYMBOL_GPL(__supported_pte_mask);
155
Yinghai Lubd220a22008-09-05 00:58:28 -0700156int force_personality32;
157
Ingo Molnardeed05b2008-09-05 10:23:26 +0200158/*
159 * noexec32=on|off
160 * Control non executable heap for 32bit processes.
161 * To control the stack too use noexec=off
162 *
163 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
164 * off PROT_READ implies PROT_EXEC
165 */
Yinghai Lubd220a22008-09-05 00:58:28 -0700166static int __init nonx32_setup(char *str)
167{
168 if (!strcmp(str, "on"))
169 force_personality32 &= ~READ_IMPLIES_EXEC;
170 else if (!strcmp(str, "off"))
171 force_personality32 |= READ_IMPLIES_EXEC;
172 return 1;
173}
174__setup("noexec32=", nonx32_setup);
175
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200176/*
Haicheng Li6afb5152010-05-19 17:42:14 +0800177 * When memory was added/removed make sure all the processes MM have
178 * suitable PGD entries in the local PGD level page.
179 */
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700180void sync_global_pgds(unsigned long start, unsigned long end, int removed)
Haicheng Li6afb5152010-05-19 17:42:14 +0800181{
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700182 unsigned long address;
Haicheng Li6afb5152010-05-19 17:42:14 +0800183
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700184 for (address = start; address <= end; address += PGDIR_SIZE) {
185 const pgd_t *pgd_ref = pgd_offset_k(address);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700186 struct page *page;
Haicheng Li6afb5152010-05-19 17:42:14 +0800187
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700188 /*
189 * When it is called after memory hot remove, pgd_none()
190 * returns true. In this case (removed == 1), we must clear
191 * the PGD entries in the local PGD level page.
192 */
193 if (pgd_none(*pgd_ref) && !removed)
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700194 continue;
Haicheng Li6afb5152010-05-19 17:42:14 +0800195
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800196 spin_lock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700197 list_for_each_entry(page, &pgd_list, lru) {
Linus Torvaldsbe354f42012-12-15 12:29:54 -0800198 pgd_t *pgd;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700199 spinlock_t *pgt_lock;
200
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700201 pgd = (pgd_t *)page_address(page) + pgd_index(address);
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800202 /* the pgt_lock only for Xen */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700203 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
204 spin_lock(pgt_lock);
205
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700206 if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700207 BUG_ON(pgd_page_vaddr(*pgd)
208 != pgd_page_vaddr(*pgd_ref));
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700209
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700210 if (removed) {
211 if (pgd_none(*pgd_ref) && !pgd_none(*pgd))
212 pgd_clear(pgd);
213 } else {
214 if (pgd_none(*pgd))
215 set_pgd(pgd, *pgd_ref);
216 }
217
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700218 spin_unlock(pgt_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700219 }
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800220 spin_unlock(&pgd_lock);
Jeremy Fitzhardinge44235dc2010-10-14 17:04:59 -0700221 }
Haicheng Li6afb5152010-05-19 17:42:14 +0800222}
223
224/*
Marcin Slusarz8d6ea9672008-08-15 18:32:24 +0200225 * NOTE: This function is marked __ref because it calls __init function
226 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
227 */
228static __ref void *spp_getpage(void)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 void *ptr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (after_bootmem)
Vegard Nossum9e730232009-02-22 11:28:25 +0100233 ptr = (void *) get_zeroed_page(GFP_ATOMIC | __GFP_NOTRACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 else
235 ptr = alloc_bootmem_pages(PAGE_SIZE);
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100236
237 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
238 panic("set_pte_phys: cannot allocate page data %s\n",
239 after_bootmem ? "after bootmem" : "");
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Ingo Molnar10f22dd2008-01-30 13:34:10 +0100242 pr_debug("spp_getpage %p\n", ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100244 return ptr;
245}
246
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800247static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900248{
249 if (pgd_none(*pgd)) {
250 pud_t *pud = (pud_t *)spp_getpage();
251 pgd_populate(&init_mm, pgd, pud);
252 if (pud != pud_offset(pgd, 0))
253 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
254 pud, pud_offset(pgd, 0));
255 }
256 return pud_offset(pgd, vaddr);
257}
258
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800259static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900260{
261 if (pud_none(*pud)) {
262 pmd_t *pmd = (pmd_t *) spp_getpage();
263 pud_populate(&init_mm, pud, pmd);
264 if (pmd != pmd_offset(pud, 0))
265 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
266 pmd, pmd_offset(pud, 0));
267 }
268 return pmd_offset(pud, vaddr);
269}
270
Jeremy Fitzhardingef254f392009-03-03 12:02:57 -0800271static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
Tejun Heo458a3e62009-02-24 11:57:21 +0900272{
273 if (pmd_none(*pmd)) {
274 pte_t *pte = (pte_t *) spp_getpage();
275 pmd_populate_kernel(&init_mm, pmd, pte);
276 if (pte != pte_offset_kernel(pmd, 0))
277 printk(KERN_ERR "PAGETABLE BUG #02!\n");
278 }
279 return pte_offset_kernel(pmd, vaddr);
280}
281
282void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 pud_t *pud;
285 pmd_t *pmd;
Jeremy Fitzhardinged494a962008-06-17 11:41:59 -0700286 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400288 pud = pud_page + pud_index(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900289 pmd = fill_pmd(pud, vaddr);
290 pte = fill_pte(pmd, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 set_pte(pte, new_pte);
293
294 /*
295 * It's enough to flush this one mapping.
296 * (PGE mappings get flushed as well)
297 */
298 __flush_tlb_one(vaddr);
299}
300
Tejun Heo458a3e62009-02-24 11:57:21 +0900301void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
Eduardo Habkost0814e0b2008-06-25 00:19:22 -0400302{
303 pgd_t *pgd;
304 pud_t *pud_page;
305
306 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
307
308 pgd = pgd_offset_k(vaddr);
309 if (pgd_none(*pgd)) {
310 printk(KERN_ERR
311 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
312 return;
313 }
314 pud_page = (pud_t*)pgd_page_vaddr(*pgd);
315 set_pte_vaddr_pud(pud_page, vaddr, pteval);
316}
317
Tejun Heo458a3e62009-02-24 11:57:21 +0900318pmd_t * __init populate_extra_pmd(unsigned long vaddr)
Tejun Heo11124412009-02-20 16:29:09 +0900319{
320 pgd_t *pgd;
321 pud_t *pud;
322
323 pgd = pgd_offset_k(vaddr);
Tejun Heo458a3e62009-02-24 11:57:21 +0900324 pud = fill_pud(pgd, vaddr);
325 return fill_pmd(pud, vaddr);
326}
Tejun Heo11124412009-02-20 16:29:09 +0900327
Tejun Heo458a3e62009-02-24 11:57:21 +0900328pte_t * __init populate_extra_pte(unsigned long vaddr)
329{
330 pmd_t *pmd;
331
332 pmd = populate_extra_pmd(vaddr);
333 return fill_pte(pmd, vaddr);
Tejun Heo11124412009-02-20 16:29:09 +0900334}
335
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100336/*
Jack Steiner3a9e1892008-07-01 14:45:32 -0500337 * Create large page table mappings for a range of physical addresses.
338 */
339static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
Juergen Gross2df58b62014-11-03 14:01:52 +0100340 enum page_cache_mode cache)
Jack Steiner3a9e1892008-07-01 14:45:32 -0500341{
342 pgd_t *pgd;
343 pud_t *pud;
344 pmd_t *pmd;
Juergen Gross2df58b62014-11-03 14:01:52 +0100345 pgprot_t prot;
Jack Steiner3a9e1892008-07-01 14:45:32 -0500346
Juergen Gross2df58b62014-11-03 14:01:52 +0100347 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
348 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
Jack Steiner3a9e1892008-07-01 14:45:32 -0500349 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
350 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
351 pgd = pgd_offset_k((unsigned long)__va(phys));
352 if (pgd_none(*pgd)) {
353 pud = (pud_t *) spp_getpage();
354 set_pgd(pgd, __pgd(__pa(pud) | _KERNPG_TABLE |
355 _PAGE_USER));
356 }
357 pud = pud_offset(pgd, (unsigned long)__va(phys));
358 if (pud_none(*pud)) {
359 pmd = (pmd_t *) spp_getpage();
360 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
361 _PAGE_USER));
362 }
363 pmd = pmd_offset(pud, phys);
364 BUG_ON(!pmd_none(*pmd));
365 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
366 }
367}
368
369void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
370{
Juergen Gross2df58b62014-11-03 14:01:52 +0100371 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500372}
373
374void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
375{
Juergen Gross2df58b62014-11-03 14:01:52 +0100376 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
Jack Steiner3a9e1892008-07-01 14:45:32 -0500377}
378
379/*
Ingo Molnar88f3aec2008-02-21 11:04:11 +0100380 * The head.S code sets up the kernel high mapping:
381 *
382 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100383 *
Zhang Yanfei1e3b3082013-05-15 10:58:07 +0800384 * phys_base holds the negative offset to the kernel, which is added
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100385 * to the compile time generated pmds. This results in invalid pmds up
386 * to the point where we hit the physaddr 0 mapping.
387 *
Yinghai Lue5f15b42011-02-18 11:30:30 +0000388 * We limit the mappings to the region from _text to _brk_end. _brk_end
389 * is rounded up to the 2MB boundary. This catches the invalid pmds as
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100390 * well, as they are located before _text:
391 */
392void __init cleanup_highmap(void)
393{
394 unsigned long vaddr = __START_KERNEL_map;
Yinghai Lu10054232013-01-24 12:19:54 -0800395 unsigned long vaddr_end = __START_KERNEL_map + KERNEL_IMAGE_SIZE;
Yinghai Lue5f15b42011-02-18 11:30:30 +0000396 unsigned long end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100397 pmd_t *pmd = level2_kernel_pgt;
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100398
Yinghai Lu10054232013-01-24 12:19:54 -0800399 /*
400 * Native path, max_pfn_mapped is not set yet.
401 * Xen has valid max_pfn_mapped set in
402 * arch/x86/xen/mmu.c:xen_setup_kernel_pagetable().
403 */
404 if (max_pfn_mapped)
405 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
406
Yinghai Lue5f15b42011-02-18 11:30:30 +0000407 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
Hugh Dickins2884f112008-05-28 19:36:07 +0100408 if (pmd_none(*pmd))
Thomas Gleixner31eedd82008-02-15 17:29:12 +0100409 continue;
410 if (vaddr < (unsigned long) _text || vaddr > end)
411 set_pmd(pmd, __pmd(0));
412 }
413}
414
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700415static unsigned long __meminit
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700416phys_pte_init(pte_t *pte_page, unsigned long addr, unsigned long end,
417 pgprot_t prot)
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400418{
Yinghai Lueceb3632012-11-16 19:38:56 -0800419 unsigned long pages = 0, next;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700420 unsigned long last_map_addr = end;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400421 int i;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700422
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400423 pte_t *pte = pte_page + pte_index(addr);
424
Yinghai Lueceb3632012-11-16 19:38:56 -0800425 for (i = pte_index(addr); i < PTRS_PER_PTE; i++, addr = next, pte++) {
426 next = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400427 if (addr >= end) {
Yinghai Lueceb3632012-11-16 19:38:56 -0800428 if (!after_bootmem &&
429 !e820_any_mapped(addr & PAGE_MASK, next, E820_RAM) &&
430 !e820_any_mapped(addr & PAGE_MASK, next, E820_RESERVED_KERN))
431 set_pte(pte, __pte(0));
432 continue;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400433 }
434
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700435 /*
436 * We will re-use the existing mapping.
437 * Xen for example has some special requirements, like mapping
438 * pagetable pages as RO. So assume someone who pre-setup
439 * these mappings are more intelligent.
440 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700441 if (pte_val(*pte)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100442 if (!after_bootmem)
443 pages++;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400444 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700445 }
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400446
447 if (0)
448 printk(" pte=%p addr=%lx pte=%016lx\n",
449 pte, addr, pfn_pte(addr >> PAGE_SHIFT, PAGE_KERNEL).pte);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400450 pages++;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700451 set_pte(pte, pfn_pte(addr >> PAGE_SHIFT, prot));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400452 last_map_addr = (addr & PAGE_MASK) + PAGE_SIZE;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400453 }
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700454
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400455 update_page_count(PG_LEVEL_4K, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700456
457 return last_map_addr;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400458}
459
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700460static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700461phys_pmd_init(pmd_t *pmd_page, unsigned long address, unsigned long end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700462 unsigned long page_size_mask, pgprot_t prot)
Matt Tolentino44df75e2006-01-17 07:03:41 +0100463{
Jan Beulich20167d32012-05-16 14:06:26 +0100464 unsigned long pages = 0, next;
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700465 unsigned long last_map_addr = end;
Andi Kleence0c0e52008-05-02 11:46:49 +0200466
Keith Mannthey6ad91652006-09-26 10:52:36 +0200467 int i = pmd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jan Beulich20167d32012-05-16 14:06:26 +0100469 for (; i < PTRS_PER_PMD; i++, address = next) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200470 pmd_t *pmd = pmd_page + pmd_index(address);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400471 pte_t *pte;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700472 pgprot_t new_prot = prot;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100473
Jan Beulich20167d32012-05-16 14:06:26 +0100474 next = (address & PMD_MASK) + PMD_SIZE;
Yinghai Lueceb3632012-11-16 19:38:56 -0800475 if (address >= end) {
476 if (!after_bootmem &&
477 !e820_any_mapped(address & PMD_MASK, next, E820_RAM) &&
478 !e820_any_mapped(address & PMD_MASK, next, E820_RESERVED_KERN))
479 set_pmd(pmd, __pmd(0));
480 continue;
481 }
Jan Beulich20167d32012-05-16 14:06:26 +0100482
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400483 if (pmd_val(*pmd)) {
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100484 if (!pmd_large(*pmd)) {
485 spin_lock(&init_mm.page_table_lock);
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800486 pte = (pte_t *)pmd_page_vaddr(*pmd);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800487 last_map_addr = phys_pte_init(pte, address,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700488 end, prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100489 spin_unlock(&init_mm.page_table_lock);
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700490 continue;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100491 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700492 /*
493 * If we are ok with PG_LEVEL_2M mapping, then we will
494 * use the existing mapping,
495 *
496 * Otherwise, we will split the large page mapping but
497 * use the same existing protection bits except for
498 * large page, so that we don't violate Intel's TLB
499 * Application note (317080) which says, while changing
500 * the page sizes, new and old translations should
501 * not differ with respect to page frame and
502 * attributes.
503 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700504 if (page_size_mask & (1 << PG_LEVEL_2M)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100505 if (!after_bootmem)
506 pages++;
Jan Beulich20167d32012-05-16 14:06:26 +0100507 last_map_addr = next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700508 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700509 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700510 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400511 }
Keith Mannthey6ad91652006-09-26 10:52:36 +0200512
Yinghai Lub50efd22008-07-08 01:41:05 -0700513 if (page_size_mask & (1<<PG_LEVEL_2M)) {
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400514 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100515 spin_lock(&init_mm.page_table_lock);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400516 set_pte((pte_t *)pmd,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800517 pfn_pte((address & PMD_MASK) >> PAGE_SHIFT,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700518 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100519 spin_unlock(&init_mm.page_table_lock);
Jan Beulich20167d32012-05-16 14:06:26 +0100520 last_map_addr = next;
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400521 continue;
522 }
523
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800524 pte = alloc_low_page();
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700525 last_map_addr = phys_pte_init(pte, address, end, new_prot);
Jeremy Fitzhardinge4f9c11d2008-06-25 00:19:19 -0400526
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100527 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800528 pmd_populate_kernel(&init_mm, pmd, pte);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100529 spin_unlock(&init_mm.page_table_lock);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100530 }
Andi Kleence0c0e52008-05-02 11:46:49 +0200531 update_page_count(PG_LEVEL_2M, pages);
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700532 return last_map_addr;
Matt Tolentino44df75e2006-01-17 07:03:41 +0100533}
534
Andi Kleencc615032008-03-12 03:53:28 +0100535static unsigned long __meminit
Yinghai Lub50efd22008-07-08 01:41:05 -0700536phys_pud_init(pud_t *pud_page, unsigned long addr, unsigned long end,
537 unsigned long page_size_mask)
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100538{
Jan Beulich20167d32012-05-16 14:06:26 +0100539 unsigned long pages = 0, next;
Andi Kleencc615032008-03-12 03:53:28 +0100540 unsigned long last_map_addr = end;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200541 int i = pud_index(addr);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100542
Jan Beulich20167d32012-05-16 14:06:26 +0100543 for (; i < PTRS_PER_PUD; i++, addr = next) {
Keith Mannthey6ad91652006-09-26 10:52:36 +0200544 pud_t *pud = pud_page + pud_index(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 pmd_t *pmd;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700546 pgprot_t prot = PAGE_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jan Beulich20167d32012-05-16 14:06:26 +0100548 next = (addr & PUD_MASK) + PUD_SIZE;
Yinghai Lueceb3632012-11-16 19:38:56 -0800549 if (addr >= end) {
550 if (!after_bootmem &&
551 !e820_any_mapped(addr & PUD_MASK, next, E820_RAM) &&
552 !e820_any_mapped(addr & PUD_MASK, next, E820_RESERVED_KERN))
553 set_pud(pud, __pud(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 continue;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Keith Mannthey6ad91652006-09-26 10:52:36 +0200557 if (pud_val(*pud)) {
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700558 if (!pud_large(*pud)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800559 pmd = pmd_offset(pud, 0);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800560 last_map_addr = phys_pmd_init(pmd, addr, end,
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700561 page_size_mask, prot);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800562 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700563 continue;
564 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700565 /*
566 * If we are ok with PG_LEVEL_1G mapping, then we will
567 * use the existing mapping.
568 *
569 * Otherwise, we will split the gbpage mapping but use
570 * the same existing protection bits except for large
571 * page, so that we don't violate Intel's TLB
572 * Application note (317080) which says, while changing
573 * the page sizes, new and old translations should
574 * not differ with respect to page frame and
575 * attributes.
576 */
Yinghai Lu3afa3942008-10-25 22:58:21 -0700577 if (page_size_mask & (1 << PG_LEVEL_1G)) {
Jan Beulich876ee612012-10-04 14:48:10 +0100578 if (!after_bootmem)
579 pages++;
Jan Beulich20167d32012-05-16 14:06:26 +0100580 last_map_addr = next;
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700581 continue;
Yinghai Lu3afa3942008-10-25 22:58:21 -0700582 }
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700583 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
Andi Kleenef925762008-04-17 17:40:45 +0200584 }
585
Yinghai Lub50efd22008-07-08 01:41:05 -0700586 if (page_size_mask & (1<<PG_LEVEL_1G)) {
Andi Kleence0c0e52008-05-02 11:46:49 +0200587 pages++;
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100588 spin_lock(&init_mm.page_table_lock);
Andi Kleenef925762008-04-17 17:40:45 +0200589 set_pte((pte_t *)pud,
Yinghai Lu960ddb4f2012-11-16 19:38:54 -0800590 pfn_pte((addr & PUD_MASK) >> PAGE_SHIFT,
591 PAGE_KERNEL_LARGE));
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100592 spin_unlock(&init_mm.page_table_lock);
Jan Beulich20167d32012-05-16 14:06:26 +0100593 last_map_addr = next;
Keith Mannthey6ad91652006-09-26 10:52:36 +0200594 continue;
595 }
596
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800597 pmd = alloc_low_page();
Suresh Siddhab27a43c2008-10-07 13:58:46 -0700598 last_map_addr = phys_pmd_init(pmd, addr, end, page_size_mask,
599 prot);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100600
601 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800602 pud_populate(&init_mm, pud, pmd);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100603 spin_unlock(&init_mm.page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Andi Kleen1a2b4412008-01-30 13:33:54 +0100605 __flush_tlb_all();
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700606
Andi Kleence0c0e52008-05-02 11:46:49 +0200607 update_page_count(PG_LEVEL_1G, pages);
Andi Kleencc615032008-03-12 03:53:28 +0100608
Yinghai Lu1a0db382008-06-24 14:56:20 -0700609 return last_map_addr;
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Shaohua Li41d840e2009-06-12 12:57:52 +0800612unsigned long __meminit
Pekka Enbergf7650902009-03-05 14:55:05 +0200613kernel_physical_mapping_init(unsigned long start,
614 unsigned long end,
615 unsigned long page_size_mask)
Yinghai Lub50efd22008-07-08 01:41:05 -0700616{
Haicheng Li9b861522010-08-20 17:50:16 +0800617 bool pgd_changed = false;
Yinghai Lub50efd22008-07-08 01:41:05 -0700618 unsigned long next, last_map_addr = end;
Haicheng Li9b861522010-08-20 17:50:16 +0800619 unsigned long addr;
Yinghai Lub50efd22008-07-08 01:41:05 -0700620
621 start = (unsigned long)__va(start);
622 end = (unsigned long)__va(end);
Wu Fengguang1c5f50e2010-09-03 17:04:07 +0800623 addr = start;
Yinghai Lub50efd22008-07-08 01:41:05 -0700624
625 for (; start < end; start = next) {
626 pgd_t *pgd = pgd_offset_k(start);
Yinghai Lub50efd22008-07-08 01:41:05 -0700627 pud_t *pud;
628
Yinghai Luc2bdee52013-01-24 12:19:46 -0800629 next = (start & PGDIR_MASK) + PGDIR_SIZE;
Yinghai Lub50efd22008-07-08 01:41:05 -0700630
631 if (pgd_val(*pgd)) {
Yinghai Lu973dc4f2012-11-16 19:38:59 -0800632 pud = (pud_t *)pgd_page_vaddr(*pgd);
Yinghai Lu4b239f42010-12-17 16:58:28 -0800633 last_map_addr = phys_pud_init(pud, __pa(start),
Yinghai Lub50efd22008-07-08 01:41:05 -0700634 __pa(end), page_size_mask);
635 continue;
636 }
637
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800638 pud = alloc_low_page();
Yinghai Luc2bdee52013-01-24 12:19:46 -0800639 last_map_addr = phys_pud_init(pud, __pa(start), __pa(end),
Yinghai Lub50efd22008-07-08 01:41:05 -0700640 page_size_mask);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100641
642 spin_lock(&init_mm.page_table_lock);
Yinghai Lu868bf4d2012-11-16 19:39:00 -0800643 pgd_populate(&init_mm, pgd, pud);
Jan Beulich8ae3a5a2008-08-21 14:27:22 +0100644 spin_unlock(&init_mm.page_table_lock);
Haicheng Li9b861522010-08-20 17:50:16 +0800645 pgd_changed = true;
Yinghai Lub50efd22008-07-08 01:41:05 -0700646 }
Haicheng Li9b861522010-08-20 17:50:16 +0800647
648 if (pgd_changed)
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -0700649 sync_global_pgds(addr, end - 1, 0);
Haicheng Li9b861522010-08-20 17:50:16 +0800650
Suresh Siddhaa2699e42008-09-23 14:00:38 -0700651 __flush_tlb_all();
Yinghai Lub50efd22008-07-08 01:41:05 -0700652
653 return last_map_addr;
654}
Yinghai Lu7b16eb82008-07-09 20:15:02 -0700655
Matt Tolentino2b976902005-06-23 00:08:06 -0700656#ifndef CONFIG_NUMA
Tejun Heod8fc3af2011-02-16 12:13:06 +0100657void __init initmem_init(void)
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700658{
Tang Chene7e8de52014-01-21 15:49:26 -0800659 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700660}
Pekka Enberg3551f882009-05-07 15:35:41 +0300661#endif
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663void __init paging_init(void)
664{
Pekka Enberg3551f882009-05-07 15:35:41 +0300665 sparse_memory_present_with_active_regions(MAX_NUMNODES);
Matt Tolentino44df75e2006-01-17 07:03:41 +0100666 sparse_init();
Yinghai Lu44b57282009-07-08 09:50:19 -0700667
668 /*
669 * clear the default setting with node 0
670 * note: don't use nodes_clear here, that is really clearing when
671 * numa support is not compiled in, and later node_set_state
672 * will not set it back.
673 */
Lai Jiangshan4b0ef1fe2012-12-12 13:51:46 -0800674 node_clear_state(0, N_MEMORY);
675 if (N_MEMORY != N_NORMAL_MEMORY)
676 node_clear_state(0, N_NORMAL_MEMORY);
Yinghai Lu44b57282009-07-08 09:50:19 -0700677
Pekka Enberg4c0b2e52011-11-01 15:58:17 +0200678 zone_sizes_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Thomas Gleixner14a62c32008-01-30 13:34:10 +0100681/*
Matt Tolentino44df75e2006-01-17 07:03:41 +0100682 * Memory hotplug specific functions
Matt Tolentino44df75e2006-01-17 07:03:41 +0100683 */
Yasunori Gotobc02af92006-06-27 02:53:30 -0700684#ifdef CONFIG_MEMORY_HOTPLUG
685/*
Shaohui Zhengea085412010-02-02 13:44:16 -0800686 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
687 * updating.
688 */
689static void update_end_of_memory_vars(u64 start, u64 size)
690{
691 unsigned long end_pfn = PFN_UP(start + size);
692
693 if (end_pfn > max_pfn) {
694 max_pfn = end_pfn;
695 max_low_pfn = end_pfn;
696 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
697 }
698}
699
700/*
Yasunori Gotobc02af92006-06-27 02:53:30 -0700701 * Memory is added always to NORMAL zone. This means you will never get
702 * additional DMA/DMA32 memory.
703 */
704int arch_add_memory(int nid, u64 start, u64 size)
705{
706 struct pglist_data *pgdat = NODE_DATA(nid);
Wang Nan9bfc4112014-08-06 16:07:38 -0700707 struct zone *zone = pgdat->node_zones +
708 zone_for_memory(nid, start, size, ZONE_NORMAL);
Jacob Shin66520eb2012-11-16 19:38:52 -0800709 unsigned long start_pfn = start >> PAGE_SHIFT;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700710 unsigned long nr_pages = size >> PAGE_SHIFT;
711 int ret;
712
Jacob Shin66520eb2012-11-16 19:38:52 -0800713 init_memory_mapping(start, start + size);
Keith Mannthey45e0b782006-09-30 23:27:09 -0700714
Gary Hadec04fc582009-01-06 14:39:14 -0800715 ret = __add_pages(nid, zone, start_pfn, nr_pages);
Gary Hadefe8b8682008-10-28 16:43:14 -0700716 WARN_ON_ONCE(ret);
Yasunori Gotobc02af92006-06-27 02:53:30 -0700717
Shaohui Zhengea085412010-02-02 13:44:16 -0800718 /* update max_pfn, max_low_pfn and high_memory */
719 update_end_of_memory_vars(start, size);
720
Yasunori Gotobc02af92006-06-27 02:53:30 -0700721 return ret;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700722}
723EXPORT_SYMBOL_GPL(arch_add_memory);
724
Wen Congyangae9aae92013-02-22 16:33:04 -0800725#define PAGE_INUSE 0xFD
726
727static void __meminit free_pagetable(struct page *page, int order)
728{
Wen Congyangae9aae92013-02-22 16:33:04 -0800729 unsigned long magic;
730 unsigned int nr_pages = 1 << order;
731
732 /* bootmem page has reserved flag */
733 if (PageReserved(page)) {
734 __ClearPageReserved(page);
Wen Congyangae9aae92013-02-22 16:33:04 -0800735
736 magic = (unsigned long)page->lru.next;
737 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
738 while (nr_pages--)
739 put_page_bootmem(page++);
740 } else
Jiang Liu170a5a72013-07-03 15:03:17 -0700741 while (nr_pages--)
742 free_reserved_page(page++);
Wen Congyangae9aae92013-02-22 16:33:04 -0800743 } else
744 free_pages((unsigned long)page_address(page), order);
Wen Congyangae9aae92013-02-22 16:33:04 -0800745}
746
747static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
748{
749 pte_t *pte;
750 int i;
751
752 for (i = 0; i < PTRS_PER_PTE; i++) {
753 pte = pte_start + i;
754 if (pte_val(*pte))
755 return;
756 }
757
758 /* free a pte talbe */
759 free_pagetable(pmd_page(*pmd), 0);
760 spin_lock(&init_mm.page_table_lock);
761 pmd_clear(pmd);
762 spin_unlock(&init_mm.page_table_lock);
763}
764
765static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
766{
767 pmd_t *pmd;
768 int i;
769
770 for (i = 0; i < PTRS_PER_PMD; i++) {
771 pmd = pmd_start + i;
772 if (pmd_val(*pmd))
773 return;
774 }
775
776 /* free a pmd talbe */
777 free_pagetable(pud_page(*pud), 0);
778 spin_lock(&init_mm.page_table_lock);
779 pud_clear(pud);
780 spin_unlock(&init_mm.page_table_lock);
781}
782
783/* Return true if pgd is changed, otherwise return false. */
784static bool __meminit free_pud_table(pud_t *pud_start, pgd_t *pgd)
785{
786 pud_t *pud;
787 int i;
788
789 for (i = 0; i < PTRS_PER_PUD; i++) {
790 pud = pud_start + i;
791 if (pud_val(*pud))
792 return false;
793 }
794
795 /* free a pud table */
796 free_pagetable(pgd_page(*pgd), 0);
797 spin_lock(&init_mm.page_table_lock);
798 pgd_clear(pgd);
799 spin_unlock(&init_mm.page_table_lock);
800
801 return true;
802}
803
804static void __meminit
805remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
806 bool direct)
807{
808 unsigned long next, pages = 0;
809 pte_t *pte;
810 void *page_addr;
811 phys_addr_t phys_addr;
812
813 pte = pte_start + pte_index(addr);
814 for (; addr < end; addr = next, pte++) {
815 next = (addr + PAGE_SIZE) & PAGE_MASK;
816 if (next > end)
817 next = end;
818
819 if (!pte_present(*pte))
820 continue;
821
822 /*
823 * We mapped [0,1G) memory as identity mapping when
824 * initializing, in arch/x86/kernel/head_64.S. These
825 * pagetables cannot be removed.
826 */
827 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
828 if (phys_addr < (phys_addr_t)0x40000000)
829 return;
830
831 if (IS_ALIGNED(addr, PAGE_SIZE) &&
832 IS_ALIGNED(next, PAGE_SIZE)) {
833 /*
834 * Do not free direct mapping pages since they were
835 * freed when offlining, or simplely not in use.
836 */
837 if (!direct)
838 free_pagetable(pte_page(*pte), 0);
839
840 spin_lock(&init_mm.page_table_lock);
841 pte_clear(&init_mm, addr, pte);
842 spin_unlock(&init_mm.page_table_lock);
843
844 /* For non-direct mapping, pages means nothing. */
845 pages++;
846 } else {
847 /*
848 * If we are here, we are freeing vmemmap pages since
849 * direct mapped memory ranges to be freed are aligned.
850 *
851 * If we are not removing the whole page, it means
852 * other page structs in this page are being used and
853 * we canot remove them. So fill the unused page_structs
854 * with 0xFD, and remove the page when it is wholly
855 * filled with 0xFD.
856 */
857 memset((void *)addr, PAGE_INUSE, next - addr);
858
859 page_addr = page_address(pte_page(*pte));
860 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
861 free_pagetable(pte_page(*pte), 0);
862
863 spin_lock(&init_mm.page_table_lock);
864 pte_clear(&init_mm, addr, pte);
865 spin_unlock(&init_mm.page_table_lock);
866 }
867 }
868 }
869
870 /* Call free_pte_table() in remove_pmd_table(). */
871 flush_tlb_all();
872 if (direct)
873 update_page_count(PG_LEVEL_4K, -pages);
874}
875
876static void __meminit
877remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
878 bool direct)
879{
880 unsigned long next, pages = 0;
881 pte_t *pte_base;
882 pmd_t *pmd;
883 void *page_addr;
884
885 pmd = pmd_start + pmd_index(addr);
886 for (; addr < end; addr = next, pmd++) {
887 next = pmd_addr_end(addr, end);
888
889 if (!pmd_present(*pmd))
890 continue;
891
892 if (pmd_large(*pmd)) {
893 if (IS_ALIGNED(addr, PMD_SIZE) &&
894 IS_ALIGNED(next, PMD_SIZE)) {
895 if (!direct)
896 free_pagetable(pmd_page(*pmd),
897 get_order(PMD_SIZE));
898
899 spin_lock(&init_mm.page_table_lock);
900 pmd_clear(pmd);
901 spin_unlock(&init_mm.page_table_lock);
902 pages++;
903 } else {
904 /* If here, we are freeing vmemmap pages. */
905 memset((void *)addr, PAGE_INUSE, next - addr);
906
907 page_addr = page_address(pmd_page(*pmd));
908 if (!memchr_inv(page_addr, PAGE_INUSE,
909 PMD_SIZE)) {
910 free_pagetable(pmd_page(*pmd),
911 get_order(PMD_SIZE));
912
913 spin_lock(&init_mm.page_table_lock);
914 pmd_clear(pmd);
915 spin_unlock(&init_mm.page_table_lock);
916 }
917 }
918
919 continue;
920 }
921
922 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
923 remove_pte_table(pte_base, addr, next, direct);
924 free_pte_table(pte_base, pmd);
925 }
926
927 /* Call free_pmd_table() in remove_pud_table(). */
928 if (direct)
929 update_page_count(PG_LEVEL_2M, -pages);
930}
931
932static void __meminit
933remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
934 bool direct)
935{
936 unsigned long next, pages = 0;
937 pmd_t *pmd_base;
938 pud_t *pud;
939 void *page_addr;
940
941 pud = pud_start + pud_index(addr);
942 for (; addr < end; addr = next, pud++) {
943 next = pud_addr_end(addr, end);
944
945 if (!pud_present(*pud))
946 continue;
947
948 if (pud_large(*pud)) {
949 if (IS_ALIGNED(addr, PUD_SIZE) &&
950 IS_ALIGNED(next, PUD_SIZE)) {
951 if (!direct)
952 free_pagetable(pud_page(*pud),
953 get_order(PUD_SIZE));
954
955 spin_lock(&init_mm.page_table_lock);
956 pud_clear(pud);
957 spin_unlock(&init_mm.page_table_lock);
958 pages++;
959 } else {
960 /* If here, we are freeing vmemmap pages. */
961 memset((void *)addr, PAGE_INUSE, next - addr);
962
963 page_addr = page_address(pud_page(*pud));
964 if (!memchr_inv(page_addr, PAGE_INUSE,
965 PUD_SIZE)) {
966 free_pagetable(pud_page(*pud),
967 get_order(PUD_SIZE));
968
969 spin_lock(&init_mm.page_table_lock);
970 pud_clear(pud);
971 spin_unlock(&init_mm.page_table_lock);
972 }
973 }
974
975 continue;
976 }
977
978 pmd_base = (pmd_t *)pud_page_vaddr(*pud);
979 remove_pmd_table(pmd_base, addr, next, direct);
980 free_pmd_table(pmd_base, pud);
981 }
982
983 if (direct)
984 update_page_count(PG_LEVEL_1G, -pages);
985}
986
987/* start and end are both virtual address. */
988static void __meminit
989remove_pagetable(unsigned long start, unsigned long end, bool direct)
990{
991 unsigned long next;
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -0700992 unsigned long addr;
Wen Congyangae9aae92013-02-22 16:33:04 -0800993 pgd_t *pgd;
994 pud_t *pud;
995 bool pgd_changed = false;
996
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -0700997 for (addr = start; addr < end; addr = next) {
998 next = pgd_addr_end(addr, end);
Wen Congyangae9aae92013-02-22 16:33:04 -0800999
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001000 pgd = pgd_offset_k(addr);
Wen Congyangae9aae92013-02-22 16:33:04 -08001001 if (!pgd_present(*pgd))
1002 continue;
1003
1004 pud = (pud_t *)pgd_page_vaddr(*pgd);
Yasuaki Ishimatsu5255e0a2014-08-22 13:27:31 -07001005 remove_pud_table(pud, addr, next, direct);
Wen Congyangae9aae92013-02-22 16:33:04 -08001006 if (free_pud_table(pud, pgd))
1007 pgd_changed = true;
1008 }
1009
1010 if (pgd_changed)
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -07001011 sync_global_pgds(start, end - 1, 1);
Wen Congyangae9aae92013-02-22 16:33:04 -08001012
1013 flush_tlb_all();
1014}
1015
Johannes Weiner0aad8182013-04-29 15:07:50 -07001016void __ref vmemmap_free(unsigned long start, unsigned long end)
Tang Chen01975182013-02-22 16:33:08 -08001017{
Tang Chen01975182013-02-22 16:33:08 -08001018 remove_pagetable(start, end, false);
1019}
1020
Tang Chen587ff8c2013-04-15 17:46:46 +08001021#ifdef CONFIG_MEMORY_HOTREMOVE
Tang Chenbbcab872013-02-22 16:33:06 -08001022static void __meminit
1023kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1024{
1025 start = (unsigned long)__va(start);
1026 end = (unsigned long)__va(end);
1027
1028 remove_pagetable(start, end, true);
1029}
1030
Wen Congyang24d335c2013-02-22 16:32:58 -08001031int __ref arch_remove_memory(u64 start, u64 size)
1032{
1033 unsigned long start_pfn = start >> PAGE_SHIFT;
1034 unsigned long nr_pages = size >> PAGE_SHIFT;
1035 struct zone *zone;
1036 int ret;
1037
1038 zone = page_zone(pfn_to_page(start_pfn));
Tang Chenbbcab872013-02-22 16:33:06 -08001039 kernel_physical_mapping_remove(start, start + size);
Wen Congyang24d335c2013-02-22 16:32:58 -08001040 ret = __remove_pages(zone, start_pfn, nr_pages);
1041 WARN_ON_ONCE(ret);
1042
1043 return ret;
1044}
1045#endif
Keith Mannthey45e0b782006-09-30 23:27:09 -07001046#endif /* CONFIG_MEMORY_HOTPLUG */
1047
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -07001048static struct kcore_list kcore_vsyscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Yinghai Lu94b43c32012-11-16 19:39:19 -08001050static void __init register_page_bootmem_info(void)
1051{
1052#ifdef CONFIG_NUMA
1053 int i;
1054
1055 for_each_online_node(i)
1056 register_page_bootmem_info_node(NODE_DATA(i));
1057#endif
1058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060void __init mem_init(void)
1061{
Jon Mason0dc243a2006-06-26 13:58:11 +02001062 pci_iommu_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Yinghai Lu48ddb152008-01-30 13:32:36 +01001064 /* clear_bss() already clear the empty_zero_page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Yinghai Lu94b43c32012-11-16 19:39:19 -08001066 register_page_bootmem_info();
Jiang Liubced0e32013-04-29 15:06:53 -07001067
1068 /* this will put all memory onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -07001069 free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 after_bootmem = 1;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* Register memory areas for /proc/kcore */
Andy Lutomirskif40c3302014-05-05 12:19:36 -07001073 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR,
1074 PAGE_SIZE, KCORE_OTHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jiang Liu46a84132013-07-03 15:04:19 -07001076 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Arjan van de Ven67df1972006-01-06 00:12:04 -08001079#ifdef CONFIG_DEBUG_RODATA
Arjan van de Venedeed302008-01-30 13:34:08 +01001080const int rodata_test_data = 0xC3;
1081EXPORT_SYMBOL_GPL(rodata_test_data);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001082
Suresh Siddha502f6602009-10-28 18:46:56 -08001083int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -05001084
1085void set_kernel_text_rw(void)
1086{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001087 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001088 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001089
1090 if (!kernel_set_to_readonly)
1091 return;
1092
1093 pr_debug("Set kernel text: %lx - %lx for read write\n",
1094 start, end);
1095
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001096 /*
1097 * Make the kernel identity mapping for text RW. Kernel text
1098 * mapping will always be RO. Refer to the comment in
1099 * static_protections() in pageattr.c
1100 */
Steven Rostedt16239632009-02-17 17:57:30 -05001101 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1102}
1103
1104void set_kernel_text_ro(void)
1105{
Suresh Siddhab9af7c02009-10-14 14:46:55 -07001106 unsigned long start = PFN_ALIGN(_text);
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001107 unsigned long end = PFN_ALIGN(__stop___ex_table);
Steven Rostedt16239632009-02-17 17:57:30 -05001108
1109 if (!kernel_set_to_readonly)
1110 return;
1111
1112 pr_debug("Set kernel text: %lx - %lx for read only\n",
1113 start, end);
1114
Suresh Siddhae7d23dd2009-10-28 18:46:58 -08001115 /*
1116 * Set the kernel identity mapping for text RO.
1117 */
Steven Rostedt16239632009-02-17 17:57:30 -05001118 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1119}
1120
Arjan van de Ven67df1972006-01-06 00:12:04 -08001121void mark_rodata_ro(void)
1122{
Suresh Siddha74e08172009-10-14 14:46:56 -07001123 unsigned long start = PFN_ALIGN(_text);
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001124 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
Suresh Siddha74e08172009-10-14 14:46:56 -07001125 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001126 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1127 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
Yinghai Lu72212672013-01-24 12:20:13 -08001128 unsigned long all_end = PFN_ALIGN(&_end);
Steven Rostedt8f0f9962008-05-12 21:20:56 +02001129
Jan Beulich6fb14752007-05-02 19:27:10 +02001130 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
Linus Torvaldse3ebadd2007-05-07 08:44:24 -07001131 (end - start) >> 10);
Arjan van de Ven984bb802008-02-06 22:39:45 +01001132 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1133
Steven Rostedt16239632009-02-17 17:57:30 -05001134 kernel_set_to_readonly = 1;
1135
Arjan van de Ven984bb802008-02-06 22:39:45 +01001136 /*
Yinghai Lu72212672013-01-24 12:20:13 -08001137 * The rodata/data/bss/brk section (but not the kernel text!)
1138 * should also be not-executable.
Arjan van de Ven984bb802008-02-06 22:39:45 +01001139 */
Yinghai Lu72212672013-01-24 12:20:13 -08001140 set_memory_nx(rodata_start, (all_end - rodata_start) >> PAGE_SHIFT);
Arjan van de Ven67df1972006-01-06 00:12:04 -08001141
Arjan van de Ven1a487252008-01-30 13:34:09 +01001142 rodata_test();
1143
Andi Kleen0c42f392008-01-30 13:33:42 +01001144#ifdef CONFIG_CPA_DEBUG
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001145 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001146 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001147
Ingo Molnar10f22dd2008-01-30 13:34:10 +01001148 printk(KERN_INFO "Testing CPA: again\n");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +01001149 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
Andi Kleen0c42f392008-01-30 13:33:42 +01001150#endif
Suresh Siddha74e08172009-10-14 14:46:56 -07001151
Jiang Liuc88442e2013-07-03 15:02:58 -07001152 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001153 (unsigned long) __va(__pa_symbol(text_end)),
1154 (unsigned long) __va(__pa_symbol(rodata_start)));
Jiang Liuc88442e2013-07-03 15:02:58 -07001155 free_init_pages("unused kernel",
Alexander Duyckfc8d7822012-11-16 13:57:13 -08001156 (unsigned long) __va(__pa_symbol(rodata_end)),
1157 (unsigned long) __va(__pa_symbol(_sdata)));
Arjan van de Ven67df1972006-01-06 00:12:04 -08001158}
Mathieu Desnoyers4e4eee02008-02-02 15:42:20 -05001159
Arjan van de Ven67df1972006-01-06 00:12:04 -08001160#endif
1161
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001162int kern_addr_valid(unsigned long addr)
1163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001165 pgd_t *pgd;
1166 pud_t *pud;
1167 pmd_t *pmd;
1168 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 if (above != 0 && above != -1UL)
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001171 return 0;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 pgd = pgd_offset_k(addr);
1174 if (pgd_none(*pgd))
1175 return 0;
1176
1177 pud = pud_offset(pgd, addr);
1178 if (pud_none(*pud))
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Mel Gorman0ee364e2013-02-11 14:52:36 +00001181 if (pud_large(*pud))
1182 return pfn_valid(pud_pfn(*pud));
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 pmd = pmd_offset(pud, addr);
1185 if (pmd_none(*pmd))
1186 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (pmd_large(*pmd))
1189 return pfn_valid(pmd_pfn(*pmd));
1190
1191 pte = pte_offset_kernel(pmd, addr);
1192 if (pte_none(*pte))
1193 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return pfn_valid(pte_pfn(*pte));
1196}
1197
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001198/*
1199 * A pseudo VMA to allow ptrace access for the vsyscall page. This only
1200 * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
1201 * not need special handling anymore:
1202 */
Andy Lutomirskiac49b9a2014-05-19 15:58:34 -07001203static const char *gate_vma_name(struct vm_area_struct *vma)
1204{
1205 return "[vsyscall]";
1206}
1207static struct vm_operations_struct gate_vma_ops = {
1208 .name = gate_vma_name,
1209};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210static struct vm_area_struct gate_vma = {
Andy Lutomirskif40c3302014-05-05 12:19:36 -07001211 .vm_start = VSYSCALL_ADDR,
1212 .vm_end = VSYSCALL_ADDR + PAGE_SIZE,
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001213 .vm_page_prot = PAGE_READONLY_EXEC,
Andy Lutomirskiac49b9a2014-05-19 15:58:34 -07001214 .vm_flags = VM_READ | VM_EXEC,
1215 .vm_ops = &gate_vma_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216};
1217
Stephen Wilson31db58b2011-03-13 15:49:15 -04001218struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220#ifdef CONFIG_IA32_EMULATION
Stephen Wilson31db58b2011-03-13 15:49:15 -04001221 if (!mm || mm->context.ia32_compat)
Andi Kleen1e014412005-04-16 15:24:55 -07001222 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223#endif
1224 return &gate_vma;
1225}
1226
Stephen Wilson83b964b2011-03-13 15:49:16 -04001227int in_gate_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Stephen Wilson83b964b2011-03-13 15:49:16 -04001229 struct vm_area_struct *vma = get_gate_vma(mm);
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001230
Andi Kleen1e014412005-04-16 15:24:55 -07001231 if (!vma)
1232 return 0;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return (addr >= vma->vm_start) && (addr < vma->vm_end);
1235}
1236
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001237/*
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001238 * Use this when you have no reliable mm, typically from interrupt
1239 * context. It is less reliable than using a task's mm and may give
1240 * false positives.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001242int in_gate_area_no_mm(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Andy Lutomirskif40c3302014-05-05 12:19:36 -07001244 return (addr & PAGE_MASK) == VSYSCALL_ADDR;
Andi Kleen2aae9502007-07-21 17:10:01 +02001245}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001246
Yinghai Lu982792c2014-06-04 16:06:32 -07001247static unsigned long probe_memory_block_size(void)
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001248{
Yinghai Lu982792c2014-06-04 16:06:32 -07001249 /* start from 2g */
1250 unsigned long bz = 1UL<<31;
1251
Daniel J Bluemanbdee2372014-11-04 16:29:44 +08001252 if (totalram_pages >= (64ULL << (30 - PAGE_SHIFT))) {
1253 pr_info("Using 2GB memory block size for large-memory system\n");
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001254 return 2UL * 1024 * 1024 * 1024;
1255 }
Nathan Fontenot1dc41aa2011-01-20 10:46:15 -06001256
Yinghai Lu982792c2014-06-04 16:06:32 -07001257 /* less than 64g installed */
1258 if ((max_pfn << PAGE_SHIFT) < (16UL << 32))
1259 return MIN_MEMORY_BLOCK_SIZE;
1260
1261 /* get the tail size */
1262 while (bz > MIN_MEMORY_BLOCK_SIZE) {
1263 if (!((max_pfn << PAGE_SHIFT) & (bz - 1)))
1264 break;
1265 bz >>= 1;
1266 }
1267
1268 printk(KERN_DEBUG "memory block size : %ldMB\n", bz >> 20);
1269
1270 return bz;
1271}
1272
1273static unsigned long memory_block_size_probed;
1274unsigned long memory_block_size_bytes(void)
1275{
1276 if (!memory_block_size_probed)
1277 memory_block_size_probed = probe_memory_block_size();
1278
1279 return memory_block_size_probed;
1280}
1281
Christoph Lameter0889eba2007-10-16 01:24:15 -07001282#ifdef CONFIG_SPARSEMEM_VMEMMAP
1283/*
1284 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1285 */
Yinghai Luc2b91e22008-04-12 01:19:24 -07001286static long __meminitdata addr_start, addr_end;
1287static void __meminitdata *p_start, *p_end;
1288static int __meminitdata node_start;
1289
Johannes Weinere8216da2013-04-29 15:07:54 -07001290static int __meminit vmemmap_populate_hugepages(unsigned long start,
1291 unsigned long end, int node)
Christoph Lameter0889eba2007-10-16 01:24:15 -07001292{
Johannes Weiner0aad8182013-04-29 15:07:50 -07001293 unsigned long addr;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001294 unsigned long next;
1295 pgd_t *pgd;
1296 pud_t *pud;
1297 pmd_t *pmd;
1298
Johannes Weiner0aad8182013-04-29 15:07:50 -07001299 for (addr = start; addr < end; addr = next) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001300 next = pmd_addr_end(addr, end);
Christoph Lameter0889eba2007-10-16 01:24:15 -07001301
1302 pgd = vmemmap_pgd_populate(addr, node);
1303 if (!pgd)
1304 return -ENOMEM;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001305
Christoph Lameter0889eba2007-10-16 01:24:15 -07001306 pud = vmemmap_pud_populate(pgd, addr, node);
1307 if (!pud)
1308 return -ENOMEM;
1309
Johannes Weinere8216da2013-04-29 15:07:54 -07001310 pmd = pmd_offset(pud, addr);
1311 if (pmd_none(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001312 void *p;
Thomas Gleixner14a62c32008-01-30 13:34:10 +01001313
Johannes Weinere8216da2013-04-29 15:07:54 -07001314 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001315 if (p) {
1316 pte_t entry;
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001317
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001318 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1319 PAGE_KERNEL_LARGE);
1320 set_pmd(pmd, __pmd(pte_val(entry)));
Jeremy Fitzhardinge7c934d32008-06-25 00:19:20 -04001321
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001322 /* check to see if we have contiguous blocks */
1323 if (p_end != p || node_start != node) {
1324 if (p_start)
1325 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1326 addr_start, addr_end-1, p_start, p_end-1, node_start);
1327 addr_start = addr;
1328 node_start = node;
1329 p_start = p;
1330 }
1331
1332 addr_end = addr + PMD_SIZE;
1333 p_end = p + PMD_SIZE;
1334 continue;
Johannes Weinere8216da2013-04-29 15:07:54 -07001335 }
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001336 } else if (pmd_large(*pmd)) {
Johannes Weinere8216da2013-04-29 15:07:54 -07001337 vmemmap_verify((pte_t *)pmd, node, addr, next);
Johannes Weiner8e2cdbc2013-04-29 15:07:56 -07001338 continue;
1339 }
1340 pr_warn_once("vmemmap: falling back to regular page backing\n");
1341 if (vmemmap_populate_basepages(addr, next, node))
1342 return -ENOMEM;
Christoph Lameter0889eba2007-10-16 01:24:15 -07001343 }
Christoph Lameter0889eba2007-10-16 01:24:15 -07001344 return 0;
1345}
Yinghai Luc2b91e22008-04-12 01:19:24 -07001346
Johannes Weinere8216da2013-04-29 15:07:54 -07001347int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
1348{
1349 int err;
1350
1351 if (cpu_has_pse)
1352 err = vmemmap_populate_hugepages(start, end, node);
1353 else
1354 err = vmemmap_populate_basepages(start, end, node);
1355 if (!err)
Yasuaki Ishimatsu9661d5b2014-08-22 13:27:34 -07001356 sync_global_pgds(start, end - 1, 0);
Johannes Weinere8216da2013-04-29 15:07:54 -07001357 return err;
1358}
1359
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08001360#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1361void register_page_bootmem_memmap(unsigned long section_nr,
1362 struct page *start_page, unsigned long size)
1363{
1364 unsigned long addr = (unsigned long)start_page;
1365 unsigned long end = (unsigned long)(start_page + size);
1366 unsigned long next;
1367 pgd_t *pgd;
1368 pud_t *pud;
1369 pmd_t *pmd;
1370 unsigned int nr_pages;
1371 struct page *page;
1372
1373 for (; addr < end; addr = next) {
1374 pte_t *pte = NULL;
1375
1376 pgd = pgd_offset_k(addr);
1377 if (pgd_none(*pgd)) {
1378 next = (addr + PAGE_SIZE) & PAGE_MASK;
1379 continue;
1380 }
1381 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1382
1383 pud = pud_offset(pgd, addr);
1384 if (pud_none(*pud)) {
1385 next = (addr + PAGE_SIZE) & PAGE_MASK;
1386 continue;
1387 }
1388 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1389
1390 if (!cpu_has_pse) {
1391 next = (addr + PAGE_SIZE) & PAGE_MASK;
1392 pmd = pmd_offset(pud, addr);
1393 if (pmd_none(*pmd))
1394 continue;
1395 get_page_bootmem(section_nr, pmd_page(*pmd),
1396 MIX_SECTION_INFO);
1397
1398 pte = pte_offset_kernel(pmd, addr);
1399 if (pte_none(*pte))
1400 continue;
1401 get_page_bootmem(section_nr, pte_page(*pte),
1402 SECTION_INFO);
1403 } else {
1404 next = pmd_addr_end(addr, end);
1405
1406 pmd = pmd_offset(pud, addr);
1407 if (pmd_none(*pmd))
1408 continue;
1409
1410 nr_pages = 1 << (get_order(PMD_SIZE));
1411 page = pmd_page(*pmd);
1412 while (nr_pages--)
1413 get_page_bootmem(section_nr, page++,
1414 SECTION_INFO);
1415 }
1416 }
1417}
1418#endif
1419
Yinghai Luc2b91e22008-04-12 01:19:24 -07001420void __meminit vmemmap_populate_print_last(void)
1421{
1422 if (p_start) {
1423 printk(KERN_DEBUG " [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1424 addr_start, addr_end-1, p_start, p_end-1, node_start);
1425 p_start = NULL;
1426 p_end = NULL;
1427 node_start = 0;
1428 }
1429}
Christoph Lameter0889eba2007-10-16 01:24:15 -07001430#endif