2 * linux/arch/x86_64/mm/init.c
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>
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>
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/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>
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>
43 #include <asm/fixmap.h>
44 #include <asm/e820/api.h>
47 #include <asm/mmu_context.h>
48 #include <asm/proto.h>
50 #include <asm/sections.h>
51 #include <asm/kdebug.h>
53 #include <asm/set_memory.h>
55 #include <asm/uv/uv.h>
56 #include <asm/setup.h>
58 #include "mm_internal.h"
60 #include "ident_map.c"
63 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
64 * physical space so we can cache the place of the first one and move
65 * around without checking the pgd every time.
68 pteval_t __supported_pte_mask __read_mostly = ~0;
69 EXPORT_SYMBOL_GPL(__supported_pte_mask);
71 int force_personality32;
75 * Control non executable heap for 32bit processes.
76 * To control the stack too use noexec=off
78 * on PROT_READ does not imply PROT_EXEC for 32-bit processes (default)
79 * off PROT_READ implies PROT_EXEC
81 static int __init nonx32_setup(char *str)
83 if (!strcmp(str, "on"))
84 force_personality32 &= ~READ_IMPLIES_EXEC;
85 else if (!strcmp(str, "off"))
86 force_personality32 |= READ_IMPLIES_EXEC;
89 __setup("noexec32=", nonx32_setup);
92 * When memory was added make sure all the processes MM have
93 * suitable PGD entries in the local PGD level page.
95 #ifdef CONFIG_X86_5LEVEL
96 void sync_global_pgds(unsigned long start, unsigned long end)
100 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
101 const pgd_t *pgd_ref = pgd_offset_k(addr);
104 /* Check for overflow */
108 if (pgd_none(*pgd_ref))
111 spin_lock(&pgd_lock);
112 list_for_each_entry(page, &pgd_list, lru) {
114 spinlock_t *pgt_lock;
116 pgd = (pgd_t *)page_address(page) + pgd_index(addr);
117 /* the pgt_lock only for Xen */
118 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
121 if (!pgd_none(*pgd_ref) && !pgd_none(*pgd))
122 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
125 set_pgd(pgd, *pgd_ref);
127 spin_unlock(pgt_lock);
129 spin_unlock(&pgd_lock);
133 void sync_global_pgds(unsigned long start, unsigned long end)
137 for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
138 pgd_t *pgd_ref = pgd_offset_k(addr);
139 const p4d_t *p4d_ref;
143 * With folded p4d, pgd_none() is always false, we need to
144 * handle synchonization on p4d level.
146 BUILD_BUG_ON(pgd_none(*pgd_ref));
147 p4d_ref = p4d_offset(pgd_ref, addr);
149 if (p4d_none(*p4d_ref))
152 spin_lock(&pgd_lock);
153 list_for_each_entry(page, &pgd_list, lru) {
156 spinlock_t *pgt_lock;
158 pgd = (pgd_t *)page_address(page) + pgd_index(addr);
159 p4d = p4d_offset(pgd, addr);
160 /* the pgt_lock only for Xen */
161 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
164 if (!p4d_none(*p4d_ref) && !p4d_none(*p4d))
165 BUG_ON(p4d_page_vaddr(*p4d)
166 != p4d_page_vaddr(*p4d_ref));
169 set_p4d(p4d, *p4d_ref);
171 spin_unlock(pgt_lock);
173 spin_unlock(&pgd_lock);
179 * NOTE: This function is marked __ref because it calls __init function
180 * (alloc_bootmem_pages). It's safe to do it ONLY when after_bootmem == 0.
182 static __ref void *spp_getpage(void)
187 ptr = (void *) get_zeroed_page(GFP_ATOMIC);
189 ptr = alloc_bootmem_pages(PAGE_SIZE);
191 if (!ptr || ((unsigned long)ptr & ~PAGE_MASK)) {
192 panic("set_pte_phys: cannot allocate page data %s\n",
193 after_bootmem ? "after bootmem" : "");
196 pr_debug("spp_getpage %p\n", ptr);
201 static p4d_t *fill_p4d(pgd_t *pgd, unsigned long vaddr)
203 if (pgd_none(*pgd)) {
204 p4d_t *p4d = (p4d_t *)spp_getpage();
205 pgd_populate(&init_mm, pgd, p4d);
206 if (p4d != p4d_offset(pgd, 0))
207 printk(KERN_ERR "PAGETABLE BUG #00! %p <-> %p\n",
208 p4d, p4d_offset(pgd, 0));
210 return p4d_offset(pgd, vaddr);
213 static pud_t *fill_pud(p4d_t *p4d, unsigned long vaddr)
215 if (p4d_none(*p4d)) {
216 pud_t *pud = (pud_t *)spp_getpage();
217 p4d_populate(&init_mm, p4d, pud);
218 if (pud != pud_offset(p4d, 0))
219 printk(KERN_ERR "PAGETABLE BUG #01! %p <-> %p\n",
220 pud, pud_offset(p4d, 0));
222 return pud_offset(p4d, vaddr);
225 static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr)
227 if (pud_none(*pud)) {
228 pmd_t *pmd = (pmd_t *) spp_getpage();
229 pud_populate(&init_mm, pud, pmd);
230 if (pmd != pmd_offset(pud, 0))
231 printk(KERN_ERR "PAGETABLE BUG #02! %p <-> %p\n",
232 pmd, pmd_offset(pud, 0));
234 return pmd_offset(pud, vaddr);
237 static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr)
239 if (pmd_none(*pmd)) {
240 pte_t *pte = (pte_t *) spp_getpage();
241 pmd_populate_kernel(&init_mm, pmd, pte);
242 if (pte != pte_offset_kernel(pmd, 0))
243 printk(KERN_ERR "PAGETABLE BUG #03!\n");
245 return pte_offset_kernel(pmd, vaddr);
248 static void __set_pte_vaddr(pud_t *pud, unsigned long vaddr, pte_t new_pte)
250 pmd_t *pmd = fill_pmd(pud, vaddr);
251 pte_t *pte = fill_pte(pmd, vaddr);
253 set_pte(pte, new_pte);
256 * It's enough to flush this one mapping.
257 * (PGE mappings get flushed as well)
259 __flush_tlb_one_kernel(vaddr);
262 void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte)
264 p4d_t *p4d = p4d_page + p4d_index(vaddr);
265 pud_t *pud = fill_pud(p4d, vaddr);
267 __set_pte_vaddr(pud, vaddr, new_pte);
270 void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte)
272 pud_t *pud = pud_page + pud_index(vaddr);
274 __set_pte_vaddr(pud, vaddr, new_pte);
277 void set_pte_vaddr(unsigned long vaddr, pte_t pteval)
282 pr_debug("set_pte_vaddr %lx to %lx\n", vaddr, native_pte_val(pteval));
284 pgd = pgd_offset_k(vaddr);
285 if (pgd_none(*pgd)) {
287 "PGD FIXMAP MISSING, it should be setup in head.S!\n");
291 p4d_page = p4d_offset(pgd, 0);
292 set_pte_vaddr_p4d(p4d_page, vaddr, pteval);
295 pmd_t * __init populate_extra_pmd(unsigned long vaddr)
301 pgd = pgd_offset_k(vaddr);
302 p4d = fill_p4d(pgd, vaddr);
303 pud = fill_pud(p4d, vaddr);
304 return fill_pmd(pud, vaddr);
307 pte_t * __init populate_extra_pte(unsigned long vaddr)
311 pmd = populate_extra_pmd(vaddr);
312 return fill_pte(pmd, vaddr);
316 * Create large page table mappings for a range of physical addresses.
318 static void __init __init_extra_mapping(unsigned long phys, unsigned long size,
319 enum page_cache_mode cache)
327 pgprot_val(prot) = pgprot_val(PAGE_KERNEL_LARGE) |
328 pgprot_val(pgprot_4k_2_large(cachemode2pgprot(cache)));
329 BUG_ON((phys & ~PMD_MASK) || (size & ~PMD_MASK));
330 for (; size; phys += PMD_SIZE, size -= PMD_SIZE) {
331 pgd = pgd_offset_k((unsigned long)__va(phys));
332 if (pgd_none(*pgd)) {
333 p4d = (p4d_t *) spp_getpage();
334 set_pgd(pgd, __pgd(__pa(p4d) | _KERNPG_TABLE |
337 p4d = p4d_offset(pgd, (unsigned long)__va(phys));
338 if (p4d_none(*p4d)) {
339 pud = (pud_t *) spp_getpage();
340 set_p4d(p4d, __p4d(__pa(pud) | _KERNPG_TABLE |
343 pud = pud_offset(p4d, (unsigned long)__va(phys));
344 if (pud_none(*pud)) {
345 pmd = (pmd_t *) spp_getpage();
346 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE |
349 pmd = pmd_offset(pud, phys);
350 BUG_ON(!pmd_none(*pmd));
351 set_pmd(pmd, __pmd(phys | pgprot_val(prot)));
355 void __init init_extra_mapping_wb(unsigned long phys, unsigned long size)
357 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_WB);
360 void __init init_extra_mapping_uc(unsigned long phys, unsigned long size)
362 __init_extra_mapping(phys, size, _PAGE_CACHE_MODE_UC);
366 * The head.S code sets up the kernel high mapping:
368 * from __START_KERNEL_map to __START_KERNEL_map + size (== _end-_text)
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.
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:
378 void __init cleanup_highmap(void)
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;
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().
391 vaddr_end = __START_KERNEL_map + (max_pfn_mapped << PAGE_SHIFT);
393 for (; vaddr + PMD_SIZE - 1 < vaddr_end; pmd++, vaddr += PMD_SIZE) {
396 if (vaddr < (unsigned long) _text || vaddr > end)
397 set_pmd(pmd, __pmd(0));
402 * Create PTE level page table mapping for physical addresses.
403 * It returns the last physical address mapped.
405 static unsigned long __meminit
406 phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
409 unsigned long pages = 0, paddr_next;
410 unsigned long paddr_last = paddr_end;
414 pte = pte_page + pte_index(paddr);
415 i = pte_index(paddr);
417 for (; i < PTRS_PER_PTE; i++, paddr = paddr_next, pte++) {
418 paddr_next = (paddr & PAGE_MASK) + PAGE_SIZE;
419 if (paddr >= paddr_end) {
420 if (!after_bootmem &&
421 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
423 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
424 E820_TYPE_RESERVED_KERN))
425 set_pte(pte, __pte(0));
430 * We will re-use the existing mapping.
431 * Xen for example has some special requirements, like mapping
432 * pagetable pages as RO. So assume someone who pre-setup
433 * these mappings are more intelligent.
435 if (!pte_none(*pte)) {
442 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr,
443 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
445 set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, prot));
446 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
449 update_page_count(PG_LEVEL_4K, pages);
455 * Create PMD level page table mapping for physical addresses. The virtual
456 * and physical address have to be aligned at this level.
457 * It returns the last physical address mapped.
459 static unsigned long __meminit
460 phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
461 unsigned long page_size_mask, pgprot_t prot)
463 unsigned long pages = 0, paddr_next;
464 unsigned long paddr_last = paddr_end;
466 int i = pmd_index(paddr);
468 for (; i < PTRS_PER_PMD; i++, paddr = paddr_next) {
469 pmd_t *pmd = pmd_page + pmd_index(paddr);
471 pgprot_t new_prot = prot;
473 paddr_next = (paddr & PMD_MASK) + PMD_SIZE;
474 if (paddr >= paddr_end) {
475 if (!after_bootmem &&
476 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
478 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
479 E820_TYPE_RESERVED_KERN))
480 set_pmd(pmd, __pmd(0));
484 if (!pmd_none(*pmd)) {
485 if (!pmd_large(*pmd)) {
486 spin_lock(&init_mm.page_table_lock);
487 pte = (pte_t *)pmd_page_vaddr(*pmd);
488 paddr_last = phys_pte_init(pte, paddr,
490 spin_unlock(&init_mm.page_table_lock);
494 * If we are ok with PG_LEVEL_2M mapping, then we will
495 * use the existing mapping,
497 * Otherwise, we will split the large page mapping but
498 * use the same existing protection bits except for
499 * large page, so that we don't violate Intel's TLB
500 * Application note (317080) which says, while changing
501 * the page sizes, new and old translations should
502 * not differ with respect to page frame and
505 if (page_size_mask & (1 << PG_LEVEL_2M)) {
508 paddr_last = paddr_next;
511 new_prot = pte_pgprot(pte_clrhuge(*(pte_t *)pmd));
514 if (page_size_mask & (1<<PG_LEVEL_2M)) {
516 spin_lock(&init_mm.page_table_lock);
517 set_pte((pte_t *)pmd,
518 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
519 __pgprot(pgprot_val(prot) | _PAGE_PSE)));
520 spin_unlock(&init_mm.page_table_lock);
521 paddr_last = paddr_next;
525 pte = alloc_low_page();
526 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot);
528 spin_lock(&init_mm.page_table_lock);
529 pmd_populate_kernel(&init_mm, pmd, pte);
530 spin_unlock(&init_mm.page_table_lock);
532 update_page_count(PG_LEVEL_2M, pages);
537 * Create PUD level page table mapping for physical addresses. The virtual
538 * and physical address do not have to be aligned at this level. KASLR can
539 * randomize virtual addresses up to this level.
540 * It returns the last physical address mapped.
542 static unsigned long __meminit
543 phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
544 unsigned long page_size_mask)
546 unsigned long pages = 0, paddr_next;
547 unsigned long paddr_last = paddr_end;
548 unsigned long vaddr = (unsigned long)__va(paddr);
549 int i = pud_index(vaddr);
551 for (; i < PTRS_PER_PUD; i++, paddr = paddr_next) {
554 pgprot_t prot = PAGE_KERNEL;
556 vaddr = (unsigned long)__va(paddr);
557 pud = pud_page + pud_index(vaddr);
558 paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
560 if (paddr >= paddr_end) {
561 if (!after_bootmem &&
562 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
564 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
565 E820_TYPE_RESERVED_KERN))
566 set_pud(pud, __pud(0));
570 if (!pud_none(*pud)) {
571 if (!pud_large(*pud)) {
572 pmd = pmd_offset(pud, 0);
573 paddr_last = phys_pmd_init(pmd, paddr,
580 * If we are ok with PG_LEVEL_1G mapping, then we will
581 * use the existing mapping.
583 * Otherwise, we will split the gbpage mapping but use
584 * the same existing protection bits except for large
585 * page, so that we don't violate Intel's TLB
586 * Application note (317080) which says, while changing
587 * the page sizes, new and old translations should
588 * not differ with respect to page frame and
591 if (page_size_mask & (1 << PG_LEVEL_1G)) {
594 paddr_last = paddr_next;
597 prot = pte_pgprot(pte_clrhuge(*(pte_t *)pud));
600 if (page_size_mask & (1<<PG_LEVEL_1G)) {
602 spin_lock(&init_mm.page_table_lock);
603 set_pte((pte_t *)pud,
604 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
606 spin_unlock(&init_mm.page_table_lock);
607 paddr_last = paddr_next;
611 pmd = alloc_low_page();
612 paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
613 page_size_mask, prot);
615 spin_lock(&init_mm.page_table_lock);
616 pud_populate(&init_mm, pud, pmd);
617 spin_unlock(&init_mm.page_table_lock);
620 update_page_count(PG_LEVEL_1G, pages);
625 static unsigned long __meminit
626 phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
627 unsigned long page_size_mask)
629 unsigned long paddr_next, paddr_last = paddr_end;
630 unsigned long vaddr = (unsigned long)__va(paddr);
631 int i = p4d_index(vaddr);
633 if (!IS_ENABLED(CONFIG_X86_5LEVEL))
634 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, page_size_mask);
636 for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) {
640 vaddr = (unsigned long)__va(paddr);
641 p4d = p4d_page + p4d_index(vaddr);
642 paddr_next = (paddr & P4D_MASK) + P4D_SIZE;
644 if (paddr >= paddr_end) {
645 if (!after_bootmem &&
646 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
648 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
649 E820_TYPE_RESERVED_KERN))
650 set_p4d(p4d, __p4d(0));
654 if (!p4d_none(*p4d)) {
655 pud = pud_offset(p4d, 0);
656 paddr_last = phys_pud_init(pud, paddr,
662 pud = alloc_low_page();
663 paddr_last = phys_pud_init(pud, paddr, paddr_end,
666 spin_lock(&init_mm.page_table_lock);
667 p4d_populate(&init_mm, p4d, pud);
668 spin_unlock(&init_mm.page_table_lock);
675 * Create page table mapping for the physical memory for specific physical
676 * addresses. The virtual and physical addresses have to be aligned on PMD level
677 * down. It returns the last physical address mapped.
679 unsigned long __meminit
680 kernel_physical_mapping_init(unsigned long paddr_start,
681 unsigned long paddr_end,
682 unsigned long page_size_mask)
684 bool pgd_changed = false;
685 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
687 paddr_last = paddr_end;
688 vaddr = (unsigned long)__va(paddr_start);
689 vaddr_end = (unsigned long)__va(paddr_end);
692 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
693 pgd_t *pgd = pgd_offset_k(vaddr);
696 vaddr_next = (vaddr & PGDIR_MASK) + PGDIR_SIZE;
699 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
700 paddr_last = phys_p4d_init(p4d, __pa(vaddr),
706 p4d = alloc_low_page();
707 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
710 spin_lock(&init_mm.page_table_lock);
711 if (IS_ENABLED(CONFIG_X86_5LEVEL))
712 pgd_populate(&init_mm, pgd, p4d);
714 p4d_populate(&init_mm, p4d_offset(pgd, vaddr), (pud_t *) p4d);
715 spin_unlock(&init_mm.page_table_lock);
720 sync_global_pgds(vaddr_start, vaddr_end - 1);
726 void __init initmem_init(void)
728 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
732 void __init paging_init(void)
734 sparse_memory_present_with_active_regions(MAX_NUMNODES);
738 * clear the default setting with node 0
739 * note: don't use nodes_clear here, that is really clearing when
740 * numa support is not compiled in, and later node_set_state
741 * will not set it back.
743 node_clear_state(0, N_MEMORY);
744 if (N_MEMORY != N_NORMAL_MEMORY)
745 node_clear_state(0, N_NORMAL_MEMORY);
751 * Memory hotplug specific functions
753 #ifdef CONFIG_MEMORY_HOTPLUG
755 * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
758 static void update_end_of_memory_vars(u64 start, u64 size)
760 unsigned long end_pfn = PFN_UP(start + size);
762 if (end_pfn > max_pfn) {
764 max_low_pfn = end_pfn;
765 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
769 int add_pages(int nid, unsigned long start_pfn,
770 unsigned long nr_pages, bool want_memblock)
774 ret = __add_pages(nid, start_pfn, nr_pages, want_memblock);
777 /* update max_pfn, max_low_pfn and high_memory */
778 update_end_of_memory_vars(start_pfn << PAGE_SHIFT,
779 nr_pages << PAGE_SHIFT);
784 int arch_add_memory(int nid, u64 start, u64 size, bool want_memblock)
786 unsigned long start_pfn = start >> PAGE_SHIFT;
787 unsigned long nr_pages = size >> PAGE_SHIFT;
789 init_memory_mapping(start, start + size);
791 return add_pages(nid, start_pfn, nr_pages, want_memblock);
793 EXPORT_SYMBOL_GPL(arch_add_memory);
795 #define PAGE_INUSE 0xFD
797 static void __meminit free_pagetable(struct page *page, int order)
800 unsigned int nr_pages = 1 << order;
801 struct vmem_altmap *altmap = to_vmem_altmap((unsigned long) page);
804 vmem_altmap_free(altmap, nr_pages);
808 /* bootmem page has reserved flag */
809 if (PageReserved(page)) {
810 __ClearPageReserved(page);
812 magic = (unsigned long)page->freelist;
813 if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) {
815 put_page_bootmem(page++);
818 free_reserved_page(page++);
820 free_pages((unsigned long)page_address(page), order);
823 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
828 for (i = 0; i < PTRS_PER_PTE; i++) {
834 /* free a pte talbe */
835 free_pagetable(pmd_page(*pmd), 0);
836 spin_lock(&init_mm.page_table_lock);
838 spin_unlock(&init_mm.page_table_lock);
841 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
846 for (i = 0; i < PTRS_PER_PMD; i++) {
852 /* free a pmd talbe */
853 free_pagetable(pud_page(*pud), 0);
854 spin_lock(&init_mm.page_table_lock);
856 spin_unlock(&init_mm.page_table_lock);
859 static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
864 for (i = 0; i < PTRS_PER_PUD; i++) {
870 /* free a pud talbe */
871 free_pagetable(p4d_page(*p4d), 0);
872 spin_lock(&init_mm.page_table_lock);
874 spin_unlock(&init_mm.page_table_lock);
877 static void __meminit
878 remove_pte_table(pte_t *pte_start, unsigned long addr, unsigned long end,
881 unsigned long next, pages = 0;
884 phys_addr_t phys_addr;
886 pte = pte_start + pte_index(addr);
887 for (; addr < end; addr = next, pte++) {
888 next = (addr + PAGE_SIZE) & PAGE_MASK;
892 if (!pte_present(*pte))
896 * We mapped [0,1G) memory as identity mapping when
897 * initializing, in arch/x86/kernel/head_64.S. These
898 * pagetables cannot be removed.
900 phys_addr = pte_val(*pte) + (addr & PAGE_MASK);
901 if (phys_addr < (phys_addr_t)0x40000000)
904 if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
906 * Do not free direct mapping pages since they were
907 * freed when offlining, or simplely not in use.
910 free_pagetable(pte_page(*pte), 0);
912 spin_lock(&init_mm.page_table_lock);
913 pte_clear(&init_mm, addr, pte);
914 spin_unlock(&init_mm.page_table_lock);
916 /* For non-direct mapping, pages means nothing. */
920 * If we are here, we are freeing vmemmap pages since
921 * direct mapped memory ranges to be freed are aligned.
923 * If we are not removing the whole page, it means
924 * other page structs in this page are being used and
925 * we canot remove them. So fill the unused page_structs
926 * with 0xFD, and remove the page when it is wholly
929 memset((void *)addr, PAGE_INUSE, next - addr);
931 page_addr = page_address(pte_page(*pte));
932 if (!memchr_inv(page_addr, PAGE_INUSE, PAGE_SIZE)) {
933 free_pagetable(pte_page(*pte), 0);
935 spin_lock(&init_mm.page_table_lock);
936 pte_clear(&init_mm, addr, pte);
937 spin_unlock(&init_mm.page_table_lock);
942 /* Call free_pte_table() in remove_pmd_table(). */
945 update_page_count(PG_LEVEL_4K, -pages);
948 static void __meminit
949 remove_pmd_table(pmd_t *pmd_start, unsigned long addr, unsigned long end,
952 unsigned long next, pages = 0;
957 pmd = pmd_start + pmd_index(addr);
958 for (; addr < end; addr = next, pmd++) {
959 next = pmd_addr_end(addr, end);
961 if (!pmd_present(*pmd))
964 if (pmd_large(*pmd)) {
965 if (IS_ALIGNED(addr, PMD_SIZE) &&
966 IS_ALIGNED(next, PMD_SIZE)) {
968 free_pagetable(pmd_page(*pmd),
969 get_order(PMD_SIZE));
971 spin_lock(&init_mm.page_table_lock);
973 spin_unlock(&init_mm.page_table_lock);
976 /* If here, we are freeing vmemmap pages. */
977 memset((void *)addr, PAGE_INUSE, next - addr);
979 page_addr = page_address(pmd_page(*pmd));
980 if (!memchr_inv(page_addr, PAGE_INUSE,
982 free_pagetable(pmd_page(*pmd),
983 get_order(PMD_SIZE));
985 spin_lock(&init_mm.page_table_lock);
987 spin_unlock(&init_mm.page_table_lock);
994 pte_base = (pte_t *)pmd_page_vaddr(*pmd);
995 remove_pte_table(pte_base, addr, next, direct);
996 free_pte_table(pte_base, pmd);
999 /* Call free_pmd_table() in remove_pud_table(). */
1001 update_page_count(PG_LEVEL_2M, -pages);
1004 static void __meminit
1005 remove_pud_table(pud_t *pud_start, unsigned long addr, unsigned long end,
1008 unsigned long next, pages = 0;
1013 pud = pud_start + pud_index(addr);
1014 for (; addr < end; addr = next, pud++) {
1015 next = pud_addr_end(addr, end);
1017 if (!pud_present(*pud))
1020 if (pud_large(*pud)) {
1021 if (IS_ALIGNED(addr, PUD_SIZE) &&
1022 IS_ALIGNED(next, PUD_SIZE)) {
1024 free_pagetable(pud_page(*pud),
1025 get_order(PUD_SIZE));
1027 spin_lock(&init_mm.page_table_lock);
1029 spin_unlock(&init_mm.page_table_lock);
1032 /* If here, we are freeing vmemmap pages. */
1033 memset((void *)addr, PAGE_INUSE, next - addr);
1035 page_addr = page_address(pud_page(*pud));
1036 if (!memchr_inv(page_addr, PAGE_INUSE,
1038 free_pagetable(pud_page(*pud),
1039 get_order(PUD_SIZE));
1041 spin_lock(&init_mm.page_table_lock);
1043 spin_unlock(&init_mm.page_table_lock);
1050 pmd_base = pmd_offset(pud, 0);
1051 remove_pmd_table(pmd_base, addr, next, direct);
1052 free_pmd_table(pmd_base, pud);
1056 update_page_count(PG_LEVEL_1G, -pages);
1059 static void __meminit
1060 remove_p4d_table(p4d_t *p4d_start, unsigned long addr, unsigned long end,
1063 unsigned long next, pages = 0;
1067 p4d = p4d_start + p4d_index(addr);
1068 for (; addr < end; addr = next, p4d++) {
1069 next = p4d_addr_end(addr, end);
1071 if (!p4d_present(*p4d))
1074 BUILD_BUG_ON(p4d_large(*p4d));
1076 pud_base = pud_offset(p4d, 0);
1077 remove_pud_table(pud_base, addr, next, direct);
1079 * For 4-level page tables we do not want to free PUDs, but in the
1080 * 5-level case we should free them. This code will have to change
1081 * to adapt for boot-time switching between 4 and 5 level page tables.
1083 if (CONFIG_PGTABLE_LEVELS == 5)
1084 free_pud_table(pud_base, p4d);
1088 update_page_count(PG_LEVEL_512G, -pages);
1091 /* start and end are both virtual address. */
1092 static void __meminit
1093 remove_pagetable(unsigned long start, unsigned long end, bool direct)
1100 for (addr = start; addr < end; addr = next) {
1101 next = pgd_addr_end(addr, end);
1103 pgd = pgd_offset_k(addr);
1104 if (!pgd_present(*pgd))
1107 p4d = p4d_offset(pgd, 0);
1108 remove_p4d_table(p4d, addr, next, direct);
1114 void __ref vmemmap_free(unsigned long start, unsigned long end)
1116 remove_pagetable(start, end, false);
1119 #ifdef CONFIG_MEMORY_HOTREMOVE
1120 static void __meminit
1121 kernel_physical_mapping_remove(unsigned long start, unsigned long end)
1123 start = (unsigned long)__va(start);
1124 end = (unsigned long)__va(end);
1126 remove_pagetable(start, end, true);
1129 int __ref arch_remove_memory(u64 start, u64 size)
1131 unsigned long start_pfn = start >> PAGE_SHIFT;
1132 unsigned long nr_pages = size >> PAGE_SHIFT;
1133 struct page *page = pfn_to_page(start_pfn);
1134 struct vmem_altmap *altmap;
1138 /* With altmap the first mapped page is offset from @start */
1139 altmap = to_vmem_altmap((unsigned long) page);
1141 page += vmem_altmap_offset(altmap);
1142 zone = page_zone(page);
1143 ret = __remove_pages(zone, start_pfn, nr_pages);
1145 kernel_physical_mapping_remove(start, start + size);
1150 #endif /* CONFIG_MEMORY_HOTPLUG */
1152 static struct kcore_list kcore_vsyscall;
1154 static void __init register_page_bootmem_info(void)
1159 for_each_online_node(i)
1160 register_page_bootmem_info_node(NODE_DATA(i));
1164 void __init mem_init(void)
1168 /* clear_bss() already clear the empty_zero_page */
1170 register_page_bootmem_info();
1172 /* this will put all memory onto the freelists */
1176 /* Register memory areas for /proc/kcore */
1177 kclist_add(&kcore_vsyscall, (void *)VSYSCALL_ADDR, PAGE_SIZE, KCORE_USER);
1179 mem_init_print_info(NULL);
1182 int kernel_set_to_readonly;
1184 void set_kernel_text_rw(void)
1186 unsigned long start = PFN_ALIGN(_text);
1187 unsigned long end = PFN_ALIGN(__stop___ex_table);
1189 if (!kernel_set_to_readonly)
1192 pr_debug("Set kernel text: %lx - %lx for read write\n",
1196 * Make the kernel identity mapping for text RW. Kernel text
1197 * mapping will always be RO. Refer to the comment in
1198 * static_protections() in pageattr.c
1200 set_memory_rw(start, (end - start) >> PAGE_SHIFT);
1203 void set_kernel_text_ro(void)
1205 unsigned long start = PFN_ALIGN(_text);
1206 unsigned long end = PFN_ALIGN(__stop___ex_table);
1208 if (!kernel_set_to_readonly)
1211 pr_debug("Set kernel text: %lx - %lx for read only\n",
1215 * Set the kernel identity mapping for text RO.
1217 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1220 void mark_rodata_ro(void)
1222 unsigned long start = PFN_ALIGN(_text);
1223 unsigned long rodata_start = PFN_ALIGN(__start_rodata);
1224 unsigned long end = (unsigned long) &__end_rodata_hpage_align;
1225 unsigned long text_end = PFN_ALIGN(&__stop___ex_table);
1226 unsigned long rodata_end = PFN_ALIGN(&__end_rodata);
1227 unsigned long all_end;
1229 printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
1230 (end - start) >> 10);
1231 set_memory_ro(start, (end - start) >> PAGE_SHIFT);
1233 kernel_set_to_readonly = 1;
1236 * The rodata/data/bss/brk section (but not the kernel text!)
1237 * should also be not-executable.
1239 * We align all_end to PMD_SIZE because the existing mapping
1240 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
1241 * split the PMD and the reminder between _brk_end and the end
1242 * of the PMD will remain mapped executable.
1244 * Any PMD which was setup after the one which covers _brk_end
1245 * has been zapped already via cleanup_highmem().
1247 all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
1248 set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
1250 #ifdef CONFIG_CPA_DEBUG
1251 printk(KERN_INFO "Testing CPA: undo %lx-%lx\n", start, end);
1252 set_memory_rw(start, (end-start) >> PAGE_SHIFT);
1254 printk(KERN_INFO "Testing CPA: again\n");
1255 set_memory_ro(start, (end-start) >> PAGE_SHIFT);
1258 free_init_pages("unused kernel",
1259 (unsigned long) __va(__pa_symbol(text_end)),
1260 (unsigned long) __va(__pa_symbol(rodata_start)));
1261 free_init_pages("unused kernel",
1262 (unsigned long) __va(__pa_symbol(rodata_end)),
1263 (unsigned long) __va(__pa_symbol(_sdata)));
1268 int kern_addr_valid(unsigned long addr)
1270 unsigned long above = ((long)addr) >> __VIRTUAL_MASK_SHIFT;
1277 if (above != 0 && above != -1UL)
1280 pgd = pgd_offset_k(addr);
1284 p4d = p4d_offset(pgd, addr);
1285 if (!p4d_present(*p4d))
1288 pud = pud_offset(p4d, addr);
1289 if (!pud_present(*pud))
1292 if (pud_large(*pud))
1293 return pfn_valid(pud_pfn(*pud));
1295 pmd = pmd_offset(pud, addr);
1296 if (!pmd_present(*pmd))
1299 if (pmd_large(*pmd))
1300 return pfn_valid(pmd_pfn(*pmd));
1302 pte = pte_offset_kernel(pmd, addr);
1306 return pfn_valid(pte_pfn(*pte));
1309 static unsigned long probe_memory_block_size(void)
1311 unsigned long bz = MIN_MEMORY_BLOCK_SIZE;
1313 /* if system is UV or has 64GB of RAM or more, use large blocks */
1314 if (is_uv_system() || ((max_pfn << PAGE_SHIFT) >= (64UL << 30)))
1315 bz = 2UL << 30; /* 2GB */
1317 pr_info("x86/mm: Memory block size: %ldMB\n", bz >> 20);
1322 static unsigned long memory_block_size_probed;
1323 unsigned long memory_block_size_bytes(void)
1325 if (!memory_block_size_probed)
1326 memory_block_size_probed = probe_memory_block_size();
1328 return memory_block_size_probed;
1331 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1333 * Initialise the sparsemem vmemmap using huge-pages at the PMD level.
1335 static long __meminitdata addr_start, addr_end;
1336 static void __meminitdata *p_start, *p_end;
1337 static int __meminitdata node_start;
1339 static int __meminit vmemmap_populate_hugepages(unsigned long start,
1340 unsigned long end, int node, struct vmem_altmap *altmap)
1349 for (addr = start; addr < end; addr = next) {
1350 next = pmd_addr_end(addr, end);
1352 pgd = vmemmap_pgd_populate(addr, node);
1356 p4d = vmemmap_p4d_populate(pgd, addr, node);
1360 pud = vmemmap_pud_populate(p4d, addr, node);
1364 pmd = pmd_offset(pud, addr);
1365 if (pmd_none(*pmd)) {
1368 p = __vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
1372 entry = pfn_pte(__pa(p) >> PAGE_SHIFT,
1374 set_pmd(pmd, __pmd(pte_val(entry)));
1376 /* check to see if we have contiguous blocks */
1377 if (p_end != p || node_start != node) {
1379 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1380 addr_start, addr_end-1, p_start, p_end-1, node_start);
1386 addr_end = addr + PMD_SIZE;
1387 p_end = p + PMD_SIZE;
1390 return -ENOMEM; /* no fallback */
1391 } else if (pmd_large(*pmd)) {
1392 vmemmap_verify((pte_t *)pmd, node, addr, next);
1395 pr_warn_once("vmemmap: falling back to regular page backing\n");
1396 if (vmemmap_populate_basepages(addr, next, node))
1402 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
1404 struct vmem_altmap *altmap = to_vmem_altmap(start);
1407 if (boot_cpu_has(X86_FEATURE_PSE))
1408 err = vmemmap_populate_hugepages(start, end, node, altmap);
1410 pr_err_once("%s: no cpu support for altmap allocations\n",
1414 err = vmemmap_populate_basepages(start, end, node);
1416 sync_global_pgds(start, end - 1);
1420 #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE)
1421 void register_page_bootmem_memmap(unsigned long section_nr,
1422 struct page *start_page, unsigned long nr_pages)
1424 unsigned long addr = (unsigned long)start_page;
1425 unsigned long end = (unsigned long)(start_page + nr_pages);
1431 unsigned int nr_pmd_pages;
1434 for (; addr < end; addr = next) {
1437 pgd = pgd_offset_k(addr);
1438 if (pgd_none(*pgd)) {
1439 next = (addr + PAGE_SIZE) & PAGE_MASK;
1442 get_page_bootmem(section_nr, pgd_page(*pgd), MIX_SECTION_INFO);
1444 p4d = p4d_offset(pgd, addr);
1445 if (p4d_none(*p4d)) {
1446 next = (addr + PAGE_SIZE) & PAGE_MASK;
1449 get_page_bootmem(section_nr, p4d_page(*p4d), MIX_SECTION_INFO);
1451 pud = pud_offset(p4d, addr);
1452 if (pud_none(*pud)) {
1453 next = (addr + PAGE_SIZE) & PAGE_MASK;
1456 get_page_bootmem(section_nr, pud_page(*pud), MIX_SECTION_INFO);
1458 if (!boot_cpu_has(X86_FEATURE_PSE)) {
1459 next = (addr + PAGE_SIZE) & PAGE_MASK;
1460 pmd = pmd_offset(pud, addr);
1463 get_page_bootmem(section_nr, pmd_page(*pmd),
1466 pte = pte_offset_kernel(pmd, addr);
1469 get_page_bootmem(section_nr, pte_page(*pte),
1472 next = pmd_addr_end(addr, end);
1474 pmd = pmd_offset(pud, addr);
1478 nr_pmd_pages = 1 << get_order(PMD_SIZE);
1479 page = pmd_page(*pmd);
1480 while (nr_pmd_pages--)
1481 get_page_bootmem(section_nr, page++,
1488 void __meminit vmemmap_populate_print_last(void)
1491 pr_debug(" [%lx-%lx] PMD -> [%p-%p] on node %d\n",
1492 addr_start, addr_end-1, p_start, p_end-1, node_start);