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