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