1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2c1cc1552SCatalin Marinas /* 3c1cc1552SCatalin Marinas * Based on arch/arm/mm/mmu.c 4c1cc1552SCatalin Marinas * 5c1cc1552SCatalin Marinas * Copyright (C) 1995-2005 Russell King 6c1cc1552SCatalin Marinas * Copyright (C) 2012 ARM Ltd. 7c1cc1552SCatalin Marinas */ 8c1cc1552SCatalin Marinas 95a9e3e15SJisheng Zhang #include <linux/cache.h> 10c1cc1552SCatalin Marinas #include <linux/export.h> 11c1cc1552SCatalin Marinas #include <linux/kernel.h> 12c1cc1552SCatalin Marinas #include <linux/errno.h> 13c1cc1552SCatalin Marinas #include <linux/init.h> 1498d2e153STakahiro Akashi #include <linux/ioport.h> 1598d2e153STakahiro Akashi #include <linux/kexec.h> 1661bd93ceSArd Biesheuvel #include <linux/libfdt.h> 17c1cc1552SCatalin Marinas #include <linux/mman.h> 18c1cc1552SCatalin Marinas #include <linux/nodemask.h> 19c1cc1552SCatalin Marinas #include <linux/memblock.h> 20dc90f084SChristoph Hellwig #include <linux/memremap.h> 21bbd6ec60SAnshuman Khandual #include <linux/memory.h> 22c1cc1552SCatalin Marinas #include <linux/fs.h> 232475ff9dSCatalin Marinas #include <linux/io.h> 242077be67SLaura Abbott #include <linux/mm.h> 256efd8499STobias Klauser #include <linux/vmalloc.h> 266d47c23bSMike Rapoport #include <linux/set_memory.h> 27c1cc1552SCatalin Marinas 2821ab99c2SMark Rutland #include <asm/barrier.h> 29c1cc1552SCatalin Marinas #include <asm/cputype.h> 30af86e597SLaura Abbott #include <asm/fixmap.h> 31068a17a5SMark Rutland #include <asm/kasan.h> 32b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h> 33c1cc1552SCatalin Marinas #include <asm/sections.h> 34c1cc1552SCatalin Marinas #include <asm/setup.h> 3587dfb311SMasahiro Yamada #include <linux/sizes.h> 36c1cc1552SCatalin Marinas #include <asm/tlb.h> 37c1cc1552SCatalin Marinas #include <asm/mmu_context.h> 381404d6f1SLaura Abbott #include <asm/ptdump.h> 39ec28bb9cSChintan Pandya #include <asm/tlbflush.h> 40ca15ca40SMike Rapoport #include <asm/pgalloc.h> 41c1cc1552SCatalin Marinas 42c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS BIT(0) 43d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS BIT(1) 4487143f40SArd Biesheuvel #define NO_EXEC_MAPPINGS BIT(2) /* assumes FEAT_HPDS is not used */ 45c0951366SArd Biesheuvel 46e8d13cceSArd Biesheuvel int idmap_t0sz __ro_after_init; 47dd006da2SArd Biesheuvel 480d9b1ffeSArd Biesheuvel #if VA_BITS > 48 490d9b1ffeSArd Biesheuvel u64 vabits_actual __ro_after_init = VA_BITS_MIN; 505383cc6eSSteve Capper EXPORT_SYMBOL(vabits_actual); 510d9b1ffeSArd Biesheuvel #endif 52c1cc1552SCatalin Marinas 53475031b6SArd Biesheuvel u64 kimage_vaddr __ro_after_init = (u64)&_text; 54475031b6SArd Biesheuvel EXPORT_SYMBOL(kimage_vaddr); 55475031b6SArd Biesheuvel 565a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init; 57a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset); 58a7f8de16SArd Biesheuvel 59*005e1267SArd Biesheuvel u32 __boot_cpu_mode[] = { BOOT_CPU_MODE_EL2, BOOT_CPU_MODE_EL1 }; 60*005e1267SArd Biesheuvel 61*005e1267SArd Biesheuvel /* 62*005e1267SArd Biesheuvel * The booting CPU updates the failed status @__early_cpu_boot_status, 63*005e1267SArd Biesheuvel * with MMU turned off. 64*005e1267SArd Biesheuvel */ 65*005e1267SArd Biesheuvel long __section(".mmuoff.data.write") __early_cpu_boot_status; 66*005e1267SArd Biesheuvel 67c1cc1552SCatalin Marinas /* 68c1cc1552SCatalin Marinas * Empty_zero_page is a special page that is used for zero-initialized data 69c1cc1552SCatalin Marinas * and COW. 70c1cc1552SCatalin Marinas */ 715227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss; 72c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page); 73c1cc1552SCatalin Marinas 74f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; 75f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; 76f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused; 77f9040773SArd Biesheuvel 782330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock); 79ee017ee3SJianyong Wu static DEFINE_MUTEX(fixmap_lock); 802330b7caSJun Yao 812330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) 822330b7caSJun Yao { 832330b7caSJun Yao pgd_t *fixmap_pgdp; 842330b7caSJun Yao 852330b7caSJun Yao spin_lock(&swapper_pgdir_lock); 8626a6f87eSJames Morse fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp)); 872330b7caSJun Yao WRITE_ONCE(*fixmap_pgdp, pgd); 882330b7caSJun Yao /* 892330b7caSJun Yao * We need dsb(ishst) here to ensure the page-table-walker sees 902330b7caSJun Yao * our new entry before set_p?d() returns. The fixmap's 912330b7caSJun Yao * flush_tlb_kernel_range() via clear_fixmap() does this for us. 922330b7caSJun Yao */ 932330b7caSJun Yao pgd_clear_fixmap(); 942330b7caSJun Yao spin_unlock(&swapper_pgdir_lock); 952330b7caSJun Yao } 962330b7caSJun Yao 97c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 98c1cc1552SCatalin Marinas unsigned long size, pgprot_t vma_prot) 99c1cc1552SCatalin Marinas { 100873ba463SMike Rapoport if (!pfn_is_map_memory(pfn)) 101c1cc1552SCatalin Marinas return pgprot_noncached(vma_prot); 102c1cc1552SCatalin Marinas else if (file->f_flags & O_SYNC) 103c1cc1552SCatalin Marinas return pgprot_writecombine(vma_prot); 104c1cc1552SCatalin Marinas return vma_prot; 105c1cc1552SCatalin Marinas } 106c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot); 107c1cc1552SCatalin Marinas 10890292acaSYu Zhao static phys_addr_t __init early_pgtable_alloc(int shift) 109c1cc1552SCatalin Marinas { 1107142392dSSuzuki K. Poulose phys_addr_t phys; 1117142392dSSuzuki K. Poulose void *ptr; 1127142392dSSuzuki K. Poulose 113c6975d7cSQian Cai phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0, 114c6975d7cSQian Cai MEMBLOCK_ALLOC_NOLEAKTRACE); 115ecc3e771SMike Rapoport if (!phys) 116ecc3e771SMike Rapoport panic("Failed to allocate page table page\n"); 117f4710445SMark Rutland 118f4710445SMark Rutland /* 119f4710445SMark Rutland * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE 120f4710445SMark Rutland * slot will be free, so we can (ab)use the FIX_PTE slot to initialise 121f4710445SMark Rutland * any level of table. 122f4710445SMark Rutland */ 123f4710445SMark Rutland ptr = pte_set_fixmap(phys); 124f4710445SMark Rutland 12521ab99c2SMark Rutland memset(ptr, 0, PAGE_SIZE); 12621ab99c2SMark Rutland 127f4710445SMark Rutland /* 128f4710445SMark Rutland * Implicit barriers also ensure the zeroed page is visible to the page 129f4710445SMark Rutland * table walker 130f4710445SMark Rutland */ 131f4710445SMark Rutland pte_clear_fixmap(); 132f4710445SMark Rutland 133f4710445SMark Rutland return phys; 134c1cc1552SCatalin Marinas } 135c1cc1552SCatalin Marinas 136e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new) 137e98216b5SArd Biesheuvel { 138e98216b5SArd Biesheuvel /* 139e98216b5SArd Biesheuvel * The following mapping attributes may be updated in live 140e98216b5SArd Biesheuvel * kernel mappings without the need for break-before-make. 141e98216b5SArd Biesheuvel */ 1420178dc76SCatalin Marinas pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG; 143e98216b5SArd Biesheuvel 144141d1497SArd Biesheuvel /* creating or taking down mappings is always safe */ 145141d1497SArd Biesheuvel if (old == 0 || new == 0) 146141d1497SArd Biesheuvel return true; 147141d1497SArd Biesheuvel 148141d1497SArd Biesheuvel /* live contiguous mappings may not be manipulated at all */ 149141d1497SArd Biesheuvel if ((old | new) & PTE_CONT) 150141d1497SArd Biesheuvel return false; 151141d1497SArd Biesheuvel 152753e8abcSArd Biesheuvel /* Transitioning from Non-Global to Global is unsafe */ 153753e8abcSArd Biesheuvel if (old & ~new & PTE_NG) 154753e8abcSArd Biesheuvel return false; 1554e602056SWill Deacon 1560178dc76SCatalin Marinas /* 1570178dc76SCatalin Marinas * Changing the memory type between Normal and Normal-Tagged is safe 1580178dc76SCatalin Marinas * since Tagged is considered a permission attribute from the 1590178dc76SCatalin Marinas * mismatched attribute aliases perspective. 1600178dc76SCatalin Marinas */ 1610178dc76SCatalin Marinas if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1620178dc76SCatalin Marinas (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) && 1630178dc76SCatalin Marinas ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) || 1640178dc76SCatalin Marinas (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED))) 1650178dc76SCatalin Marinas mask |= PTE_ATTRINDX_MASK; 1660178dc76SCatalin Marinas 167141d1497SArd Biesheuvel return ((old ^ new) & ~mask) == 0; 168e98216b5SArd Biesheuvel } 169e98216b5SArd Biesheuvel 17020a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, 171d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot) 172c1cc1552SCatalin Marinas { 17320a004e7SWill Deacon pte_t *ptep; 174c1cc1552SCatalin Marinas 17520a004e7SWill Deacon ptep = pte_set_fixmap_offset(pmdp, addr); 176c1cc1552SCatalin Marinas do { 17720a004e7SWill Deacon pte_t old_pte = READ_ONCE(*ptep); 178e98216b5SArd Biesheuvel 17920a004e7SWill Deacon set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot)); 180e98216b5SArd Biesheuvel 181e98216b5SArd Biesheuvel /* 182e98216b5SArd Biesheuvel * After the PTE entry has been populated once, we 183e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 184e98216b5SArd Biesheuvel */ 18520a004e7SWill Deacon BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), 18620a004e7SWill Deacon READ_ONCE(pte_val(*ptep)))); 187e98216b5SArd Biesheuvel 188e393cf40SArd Biesheuvel phys += PAGE_SIZE; 18920a004e7SWill Deacon } while (ptep++, addr += PAGE_SIZE, addr != end); 190f4710445SMark Rutland 191f4710445SMark Rutland pte_clear_fixmap(); 192c1cc1552SCatalin Marinas } 193c1cc1552SCatalin Marinas 19420a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, 195d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 196d27cfa1fSArd Biesheuvel pgprot_t prot, 19790292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 198c0951366SArd Biesheuvel int flags) 199c1cc1552SCatalin Marinas { 200c1cc1552SCatalin Marinas unsigned long next; 20120a004e7SWill Deacon pmd_t pmd = READ_ONCE(*pmdp); 202c1cc1552SCatalin Marinas 20320a004e7SWill Deacon BUG_ON(pmd_sect(pmd)); 20420a004e7SWill Deacon if (pmd_none(pmd)) { 20587143f40SArd Biesheuvel pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN; 206d27cfa1fSArd Biesheuvel phys_addr_t pte_phys; 20787143f40SArd Biesheuvel 20887143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 20987143f40SArd Biesheuvel pmdval |= PMD_TABLE_PXN; 210132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 21190292acaSYu Zhao pte_phys = pgtable_alloc(PAGE_SHIFT); 21287143f40SArd Biesheuvel __pmd_populate(pmdp, pte_phys, pmdval); 21320a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 214c1cc1552SCatalin Marinas } 21520a004e7SWill Deacon BUG_ON(pmd_bad(pmd)); 216d27cfa1fSArd Biesheuvel 217d27cfa1fSArd Biesheuvel do { 218d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 219d27cfa1fSArd Biesheuvel 220d27cfa1fSArd Biesheuvel next = pte_cont_addr_end(addr, end); 221d27cfa1fSArd Biesheuvel 222d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 223d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) && 224d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 225d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 226d27cfa1fSArd Biesheuvel 22720a004e7SWill Deacon init_pte(pmdp, addr, next, phys, __prot); 228d27cfa1fSArd Biesheuvel 229d27cfa1fSArd Biesheuvel phys += next - addr; 230d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 231d27cfa1fSArd Biesheuvel } 232d27cfa1fSArd Biesheuvel 23320a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end, 234d27cfa1fSArd Biesheuvel phys_addr_t phys, pgprot_t prot, 23590292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 236d27cfa1fSArd Biesheuvel { 237d27cfa1fSArd Biesheuvel unsigned long next; 23820a004e7SWill Deacon pmd_t *pmdp; 239c1cc1552SCatalin Marinas 24020a004e7SWill Deacon pmdp = pmd_set_fixmap_offset(pudp, addr); 241c1cc1552SCatalin Marinas do { 24220a004e7SWill Deacon pmd_t old_pmd = READ_ONCE(*pmdp); 243e98216b5SArd Biesheuvel 244c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 245e98216b5SArd Biesheuvel 246c1cc1552SCatalin Marinas /* try section mapping first */ 2474aaa87abSAnshuman Khandual if (((addr | next | phys) & ~PMD_MASK) == 0 && 248c0951366SArd Biesheuvel (flags & NO_BLOCK_MAPPINGS) == 0) { 24920a004e7SWill Deacon pmd_set_huge(pmdp, phys, prot); 250e98216b5SArd Biesheuvel 251a55f9929SCatalin Marinas /* 252e98216b5SArd Biesheuvel * After the PMD entry has been populated once, we 253e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 254a55f9929SCatalin Marinas */ 255e98216b5SArd Biesheuvel BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd), 25620a004e7SWill Deacon READ_ONCE(pmd_val(*pmdp)))); 257a55f9929SCatalin Marinas } else { 25820a004e7SWill Deacon alloc_init_cont_pte(pmdp, addr, next, phys, prot, 259d27cfa1fSArd Biesheuvel pgtable_alloc, flags); 260e98216b5SArd Biesheuvel 261e98216b5SArd Biesheuvel BUG_ON(pmd_val(old_pmd) != 0 && 26220a004e7SWill Deacon pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp))); 263a55f9929SCatalin Marinas } 264c1cc1552SCatalin Marinas phys += next - addr; 26520a004e7SWill Deacon } while (pmdp++, addr = next, addr != end); 266f4710445SMark Rutland 267f4710445SMark Rutland pmd_clear_fixmap(); 268c1cc1552SCatalin Marinas } 269c1cc1552SCatalin Marinas 27020a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, 271d27cfa1fSArd Biesheuvel unsigned long end, phys_addr_t phys, 272d27cfa1fSArd Biesheuvel pgprot_t prot, 27390292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), int flags) 274d27cfa1fSArd Biesheuvel { 275d27cfa1fSArd Biesheuvel unsigned long next; 27620a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 277d27cfa1fSArd Biesheuvel 278d27cfa1fSArd Biesheuvel /* 279d27cfa1fSArd Biesheuvel * Check for initial section mappings in the pgd/pud. 280d27cfa1fSArd Biesheuvel */ 28120a004e7SWill Deacon BUG_ON(pud_sect(pud)); 28220a004e7SWill Deacon if (pud_none(pud)) { 28387143f40SArd Biesheuvel pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN; 284d27cfa1fSArd Biesheuvel phys_addr_t pmd_phys; 28587143f40SArd Biesheuvel 28687143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 28787143f40SArd Biesheuvel pudval |= PUD_TABLE_PXN; 288d27cfa1fSArd Biesheuvel BUG_ON(!pgtable_alloc); 28990292acaSYu Zhao pmd_phys = pgtable_alloc(PMD_SHIFT); 29087143f40SArd Biesheuvel __pud_populate(pudp, pmd_phys, pudval); 29120a004e7SWill Deacon pud = READ_ONCE(*pudp); 292d27cfa1fSArd Biesheuvel } 29320a004e7SWill Deacon BUG_ON(pud_bad(pud)); 294d27cfa1fSArd Biesheuvel 295d27cfa1fSArd Biesheuvel do { 296d27cfa1fSArd Biesheuvel pgprot_t __prot = prot; 297d27cfa1fSArd Biesheuvel 298d27cfa1fSArd Biesheuvel next = pmd_cont_addr_end(addr, end); 299d27cfa1fSArd Biesheuvel 300d27cfa1fSArd Biesheuvel /* use a contiguous mapping if the range is suitably aligned */ 301d27cfa1fSArd Biesheuvel if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) && 302d27cfa1fSArd Biesheuvel (flags & NO_CONT_MAPPINGS) == 0) 303d27cfa1fSArd Biesheuvel __prot = __pgprot(pgprot_val(prot) | PTE_CONT); 304d27cfa1fSArd Biesheuvel 30520a004e7SWill Deacon init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags); 306d27cfa1fSArd Biesheuvel 307d27cfa1fSArd Biesheuvel phys += next - addr; 308d27cfa1fSArd Biesheuvel } while (addr = next, addr != end); 309d27cfa1fSArd Biesheuvel } 310d27cfa1fSArd Biesheuvel 31120a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, 312da141706SLaura Abbott phys_addr_t phys, pgprot_t prot, 31390292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 314c0951366SArd Biesheuvel int flags) 315c1cc1552SCatalin Marinas { 316c1cc1552SCatalin Marinas unsigned long next; 31720a004e7SWill Deacon pud_t *pudp; 318e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 319e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 320c1cc1552SCatalin Marinas 321e9f63768SMike Rapoport if (p4d_none(p4d)) { 32287143f40SArd Biesheuvel p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN; 323132233a7SLaura Abbott phys_addr_t pud_phys; 32487143f40SArd Biesheuvel 32587143f40SArd Biesheuvel if (flags & NO_EXEC_MAPPINGS) 32687143f40SArd Biesheuvel p4dval |= P4D_TABLE_PXN; 327132233a7SLaura Abbott BUG_ON(!pgtable_alloc); 32890292acaSYu Zhao pud_phys = pgtable_alloc(PUD_SHIFT); 32987143f40SArd Biesheuvel __p4d_populate(p4dp, pud_phys, p4dval); 330e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 331c79b954bSJungseok Lee } 332e9f63768SMike Rapoport BUG_ON(p4d_bad(p4d)); 333c79b954bSJungseok Lee 334ee017ee3SJianyong Wu /* 335ee017ee3SJianyong Wu * No need for locking during early boot. And it doesn't work as 336ee017ee3SJianyong Wu * expected with KASLR enabled. 337ee017ee3SJianyong Wu */ 338ee017ee3SJianyong Wu if (system_state != SYSTEM_BOOTING) 339ee017ee3SJianyong Wu mutex_lock(&fixmap_lock); 340e9f63768SMike Rapoport pudp = pud_set_fixmap_offset(p4dp, addr); 341c1cc1552SCatalin Marinas do { 34220a004e7SWill Deacon pud_t old_pud = READ_ONCE(*pudp); 343e98216b5SArd Biesheuvel 344c1cc1552SCatalin Marinas next = pud_addr_end(addr, end); 345206a2a73SSteve Capper 346206a2a73SSteve Capper /* 347206a2a73SSteve Capper * For 4K granule only, attempt to put down a 1GB block 348206a2a73SSteve Capper */ 3491310222cSAnshuman Khandual if (pud_sect_supported() && 3501310222cSAnshuman Khandual ((addr | next | phys) & ~PUD_MASK) == 0 && 351c0951366SArd Biesheuvel (flags & NO_BLOCK_MAPPINGS) == 0) { 35220a004e7SWill Deacon pud_set_huge(pudp, phys, prot); 353206a2a73SSteve Capper 354206a2a73SSteve Capper /* 355e98216b5SArd Biesheuvel * After the PUD entry has been populated once, we 356e98216b5SArd Biesheuvel * only allow updates to the permission attributes. 357206a2a73SSteve Capper */ 358e98216b5SArd Biesheuvel BUG_ON(!pgattr_change_is_safe(pud_val(old_pud), 35920a004e7SWill Deacon READ_ONCE(pud_val(*pudp)))); 360206a2a73SSteve Capper } else { 36120a004e7SWill Deacon alloc_init_cont_pmd(pudp, addr, next, phys, prot, 362c0951366SArd Biesheuvel pgtable_alloc, flags); 363e98216b5SArd Biesheuvel 364e98216b5SArd Biesheuvel BUG_ON(pud_val(old_pud) != 0 && 36520a004e7SWill Deacon pud_val(old_pud) != READ_ONCE(pud_val(*pudp))); 366206a2a73SSteve Capper } 367c1cc1552SCatalin Marinas phys += next - addr; 36820a004e7SWill Deacon } while (pudp++, addr = next, addr != end); 369f4710445SMark Rutland 370f4710445SMark Rutland pud_clear_fixmap(); 371ee017ee3SJianyong Wu if (system_state != SYSTEM_BOOTING) 372ee017ee3SJianyong Wu mutex_unlock(&fixmap_lock); 373c1cc1552SCatalin Marinas } 374c1cc1552SCatalin Marinas 37540f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, 37640f87d31SArd Biesheuvel unsigned long virt, phys_addr_t size, 37740f87d31SArd Biesheuvel pgprot_t prot, 37890292acaSYu Zhao phys_addr_t (*pgtable_alloc)(int), 379c0951366SArd Biesheuvel int flags) 380c1cc1552SCatalin Marinas { 38132d18708SMasahiro Yamada unsigned long addr, end, next; 382974b9b2cSMike Rapoport pgd_t *pgdp = pgd_offset_pgd(pgdir, virt); 383c1cc1552SCatalin Marinas 384cc5d2b3bSMark Rutland /* 385cc5d2b3bSMark Rutland * If the virtual and physical address don't have the same offset 386cc5d2b3bSMark Rutland * within a page, we cannot map the region as the caller expects. 387cc5d2b3bSMark Rutland */ 388cc5d2b3bSMark Rutland if (WARN_ON((phys ^ virt) & ~PAGE_MASK)) 389cc5d2b3bSMark Rutland return; 390cc5d2b3bSMark Rutland 3919c4e08a3SMark Rutland phys &= PAGE_MASK; 392c1cc1552SCatalin Marinas addr = virt & PAGE_MASK; 39332d18708SMasahiro Yamada end = PAGE_ALIGN(virt + size); 394c1cc1552SCatalin Marinas 395c1cc1552SCatalin Marinas do { 396c1cc1552SCatalin Marinas next = pgd_addr_end(addr, end); 39720a004e7SWill Deacon alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc, 398c0951366SArd Biesheuvel flags); 399c1cc1552SCatalin Marinas phys += next - addr; 40020a004e7SWill Deacon } while (pgdp++, addr = next, addr != end); 401c1cc1552SCatalin Marinas } 402c1cc1552SCatalin Marinas 403475ba3fcSWill Deacon static phys_addr_t __pgd_pgtable_alloc(int shift) 404369aaab8SYu Zhao { 40550f11a8aSMike Rapoport void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL); 406369aaab8SYu Zhao BUG_ON(!ptr); 407369aaab8SYu Zhao 408369aaab8SYu Zhao /* Ensure the zeroed page is visible to the page table walker */ 409369aaab8SYu Zhao dsb(ishst); 410369aaab8SYu Zhao return __pa(ptr); 411369aaab8SYu Zhao } 412369aaab8SYu Zhao 41390292acaSYu Zhao static phys_addr_t pgd_pgtable_alloc(int shift) 414da141706SLaura Abbott { 415475ba3fcSWill Deacon phys_addr_t pa = __pgd_pgtable_alloc(shift); 41690292acaSYu Zhao 41790292acaSYu Zhao /* 41890292acaSYu Zhao * Call proper page table ctor in case later we need to 41990292acaSYu Zhao * call core mm functions like apply_to_page_range() on 42090292acaSYu Zhao * this pre-allocated page table. 42190292acaSYu Zhao * 42290292acaSYu Zhao * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is 42390292acaSYu Zhao * folded, and if so pgtable_pmd_page_ctor() becomes nop. 42490292acaSYu Zhao */ 42590292acaSYu Zhao if (shift == PAGE_SHIFT) 426b4ed71f5SMark Rutland BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa))); 42790292acaSYu Zhao else if (shift == PMD_SHIFT) 428475ba3fcSWill Deacon BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa))); 42921ab99c2SMark Rutland 430475ba3fcSWill Deacon return pa; 431da141706SLaura Abbott } 432da141706SLaura Abbott 433132233a7SLaura Abbott /* 434132233a7SLaura Abbott * This function can only be used to modify existing table entries, 435132233a7SLaura Abbott * without allocating new levels of table. Note that this permits the 436132233a7SLaura Abbott * creation of new section or page entries. 437132233a7SLaura Abbott */ 438132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt, 439da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 440d7ecbddfSMark Salter { 44177ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 442d7ecbddfSMark Salter pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n", 443d7ecbddfSMark Salter &phys, virt); 444d7ecbddfSMark Salter return; 445d7ecbddfSMark Salter } 446d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 447d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 448d7ecbddfSMark Salter } 449d7ecbddfSMark Salter 4508ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, 4518ce837ceSArd Biesheuvel unsigned long virt, phys_addr_t size, 452f14c66ceSArd Biesheuvel pgprot_t prot, bool page_mappings_only) 4538ce837ceSArd Biesheuvel { 454c0951366SArd Biesheuvel int flags = 0; 455c0951366SArd Biesheuvel 4561378dc3dSArd Biesheuvel BUG_ON(mm == &init_mm); 4571378dc3dSArd Biesheuvel 458c0951366SArd Biesheuvel if (page_mappings_only) 459d27cfa1fSArd Biesheuvel flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 460c0951366SArd Biesheuvel 46111509a30SMark Rutland __create_pgd_mapping(mm->pgd, phys, virt, size, prot, 462c0951366SArd Biesheuvel pgd_pgtable_alloc, flags); 463d7ecbddfSMark Salter } 464d7ecbddfSMark Salter 465aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt, 466da141706SLaura Abbott phys_addr_t size, pgprot_t prot) 467da141706SLaura Abbott { 46877ad4ce6SMark Rutland if ((virt >= PAGE_END) && (virt < VMALLOC_START)) { 469aa8c09beSArd Biesheuvel pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n", 470da141706SLaura Abbott &phys, virt); 471da141706SLaura Abbott return; 472da141706SLaura Abbott } 473da141706SLaura Abbott 474d27cfa1fSArd Biesheuvel __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL, 475d27cfa1fSArd Biesheuvel NO_CONT_MAPPINGS); 476aa8c09beSArd Biesheuvel 477aa8c09beSArd Biesheuvel /* flush the TLBs after updating live kernel mappings */ 478aa8c09beSArd Biesheuvel flush_tlb_kernel_range(virt, virt + size); 479da141706SLaura Abbott } 480da141706SLaura Abbott 48120a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, 48298d2e153STakahiro Akashi phys_addr_t end, pgprot_t prot, int flags) 483da141706SLaura Abbott { 48420a004e7SWill Deacon __create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start, 48598d2e153STakahiro Akashi prot, early_pgtable_alloc, flags); 486da141706SLaura Abbott } 487da141706SLaura Abbott 4885ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void) 4895ea5306cSArd Biesheuvel { 4905ea5306cSArd Biesheuvel /* 4915ea5306cSArd Biesheuvel * Remove the write permissions from the linear alias of .text/.rodata 4925ea5306cSArd Biesheuvel */ 493e2a073ddSArd Biesheuvel update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext), 494e2a073ddSArd Biesheuvel (unsigned long)__init_begin - (unsigned long)_stext, 4955ea5306cSArd Biesheuvel PAGE_KERNEL_RO); 4965ea5306cSArd Biesheuvel } 4975ea5306cSArd Biesheuvel 4982687275aSCatalin Marinas static bool crash_mem_map __initdata; 4992687275aSCatalin Marinas 5002687275aSCatalin Marinas static int __init enable_crash_mem_map(char *arg) 5012687275aSCatalin Marinas { 5022687275aSCatalin Marinas /* 5032687275aSCatalin Marinas * Proper parameter parsing is done by reserve_crashkernel(). We only 5042687275aSCatalin Marinas * need to know if the linear map has to avoid block mappings so that 5052687275aSCatalin Marinas * the crashkernel reservations can be unmapped later. 5062687275aSCatalin Marinas */ 5072687275aSCatalin Marinas crash_mem_map = true; 5082687275aSCatalin Marinas 5092687275aSCatalin Marinas return 0; 5102687275aSCatalin Marinas } 5112687275aSCatalin Marinas early_param("crashkernel", enable_crash_mem_map); 5122687275aSCatalin Marinas 51320a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp) 514c1cc1552SCatalin Marinas { 51587143f40SArd Biesheuvel static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN); 516e2a073ddSArd Biesheuvel phys_addr_t kernel_start = __pa_symbol(_stext); 51798d2e153STakahiro Akashi phys_addr_t kernel_end = __pa_symbol(__init_begin); 518b10d6bcaSMike Rapoport phys_addr_t start, end; 51987143f40SArd Biesheuvel int flags = NO_EXEC_MAPPINGS; 520b10d6bcaSMike Rapoport u64 i; 52198d2e153STakahiro Akashi 52287143f40SArd Biesheuvel /* 52387143f40SArd Biesheuvel * Setting hierarchical PXNTable attributes on table entries covering 52487143f40SArd Biesheuvel * the linear region is only possible if it is guaranteed that no table 52587143f40SArd Biesheuvel * entries at any level are being shared between the linear region and 52687143f40SArd Biesheuvel * the vmalloc region. Check whether this is true for the PGD level, in 52787143f40SArd Biesheuvel * which case it is guaranteed to be true for all other levels as well. 52887143f40SArd Biesheuvel */ 52987143f40SArd Biesheuvel BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end)); 53087143f40SArd Biesheuvel 53103149563SVijay Balakrishna if (can_set_direct_map() || IS_ENABLED(CONFIG_KFENCE)) 53287143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 53398d2e153STakahiro Akashi 53498d2e153STakahiro Akashi /* 53598d2e153STakahiro Akashi * Take care not to create a writable alias for the 53698d2e153STakahiro Akashi * read-only text and rodata sections of the kernel image. 53798d2e153STakahiro Akashi * So temporarily mark them as NOMAP to skip mappings in 53898d2e153STakahiro Akashi * the following for-loop 53998d2e153STakahiro Akashi */ 54098d2e153STakahiro Akashi memblock_mark_nomap(kernel_start, kernel_end - kernel_start); 541f6bc87c3SSteve Capper 54203149563SVijay Balakrishna #ifdef CONFIG_KEXEC_CORE 54303149563SVijay Balakrishna if (crash_mem_map) { 54403149563SVijay Balakrishna if (IS_ENABLED(CONFIG_ZONE_DMA) || 54503149563SVijay Balakrishna IS_ENABLED(CONFIG_ZONE_DMA32)) 54603149563SVijay Balakrishna flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 54703149563SVijay Balakrishna else if (crashk_res.end) 54803149563SVijay Balakrishna memblock_mark_nomap(crashk_res.start, 54903149563SVijay Balakrishna resource_size(&crashk_res)); 55003149563SVijay Balakrishna } 55103149563SVijay Balakrishna #endif 55203149563SVijay Balakrishna 553c1cc1552SCatalin Marinas /* map all the memory banks */ 554b10d6bcaSMike Rapoport for_each_mem_range(i, &start, &end) { 555c1cc1552SCatalin Marinas if (start >= end) 556c1cc1552SCatalin Marinas break; 5570178dc76SCatalin Marinas /* 5580178dc76SCatalin Marinas * The linear map must allow allocation tags reading/writing 5590178dc76SCatalin Marinas * if MTE is present. Otherwise, it has the same attributes as 5600178dc76SCatalin Marinas * PAGE_KERNEL. 5610178dc76SCatalin Marinas */ 562d15dfd31SCatalin Marinas __map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL), 563d15dfd31SCatalin Marinas flags); 564c1cc1552SCatalin Marinas } 56598d2e153STakahiro Akashi 56698d2e153STakahiro Akashi /* 567e2a073ddSArd Biesheuvel * Map the linear alias of the [_stext, __init_begin) interval 56898d2e153STakahiro Akashi * as non-executable now, and remove the write permission in 56998d2e153STakahiro Akashi * mark_linear_text_alias_ro() below (which will be called after 57098d2e153STakahiro Akashi * alternative patching has completed). This makes the contents 57198d2e153STakahiro Akashi * of the region accessible to subsystems such as hibernate, 57298d2e153STakahiro Akashi * but protects it from inadvertent modification or execution. 57398d2e153STakahiro Akashi * Note that contiguous mappings cannot be remapped in this way, 57498d2e153STakahiro Akashi * so we should avoid them here. 57598d2e153STakahiro Akashi */ 57620a004e7SWill Deacon __map_memblock(pgdp, kernel_start, kernel_end, 57798d2e153STakahiro Akashi PAGE_KERNEL, NO_CONT_MAPPINGS); 57898d2e153STakahiro Akashi memblock_clear_nomap(kernel_start, kernel_end - kernel_start); 57903149563SVijay Balakrishna 58003149563SVijay Balakrishna /* 58103149563SVijay Balakrishna * Use page-level mappings here so that we can shrink the region 58203149563SVijay Balakrishna * in page granularity and put back unused memory to buddy system 58303149563SVijay Balakrishna * through /sys/kernel/kexec_crash_size interface. 58403149563SVijay Balakrishna */ 58503149563SVijay Balakrishna #ifdef CONFIG_KEXEC_CORE 58603149563SVijay Balakrishna if (crash_mem_map && 58703149563SVijay Balakrishna !IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32)) { 58803149563SVijay Balakrishna if (crashk_res.end) { 58903149563SVijay Balakrishna __map_memblock(pgdp, crashk_res.start, 59003149563SVijay Balakrishna crashk_res.end + 1, 59103149563SVijay Balakrishna PAGE_KERNEL, 59203149563SVijay Balakrishna NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS); 59303149563SVijay Balakrishna memblock_clear_nomap(crashk_res.start, 59403149563SVijay Balakrishna resource_size(&crashk_res)); 59503149563SVijay Balakrishna } 59603149563SVijay Balakrishna } 59703149563SVijay Balakrishna #endif 598c1cc1552SCatalin Marinas } 599c1cc1552SCatalin Marinas 600da141706SLaura Abbott void mark_rodata_ro(void) 601da141706SLaura Abbott { 6022f39b5f9SJeremy Linton unsigned long section_size; 603f9040773SArd Biesheuvel 6042f39b5f9SJeremy Linton /* 6059fdc14c5SArd Biesheuvel * mark .rodata as read only. Use __init_begin rather than __end_rodata 6069fdc14c5SArd Biesheuvel * to cover NOTES and EXCEPTION_TABLE. 6072f39b5f9SJeremy Linton */ 6089fdc14c5SArd Biesheuvel section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata; 609aa8c09beSArd Biesheuvel update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata, 6102f39b5f9SJeremy Linton section_size, PAGE_KERNEL_RO); 611e98216b5SArd Biesheuvel 6121404d6f1SLaura Abbott debug_checkwx(); 613da141706SLaura Abbott } 614da141706SLaura Abbott 61520a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end, 616d27cfa1fSArd Biesheuvel pgprot_t prot, struct vm_struct *vma, 61792bbd16eSWill Deacon int flags, unsigned long vm_flags) 618068a17a5SMark Rutland { 6192077be67SLaura Abbott phys_addr_t pa_start = __pa_symbol(va_start); 620068a17a5SMark Rutland unsigned long size = va_end - va_start; 621068a17a5SMark Rutland 622068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(pa_start)); 623068a17a5SMark Rutland BUG_ON(!PAGE_ALIGNED(size)); 624068a17a5SMark Rutland 62520a004e7SWill Deacon __create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot, 626d27cfa1fSArd Biesheuvel early_pgtable_alloc, flags); 627f9040773SArd Biesheuvel 62892bbd16eSWill Deacon if (!(vm_flags & VM_NO_GUARD)) 62992bbd16eSWill Deacon size += PAGE_SIZE; 63092bbd16eSWill Deacon 631f9040773SArd Biesheuvel vma->addr = va_start; 632f9040773SArd Biesheuvel vma->phys_addr = pa_start; 633f9040773SArd Biesheuvel vma->size = size; 63492bbd16eSWill Deacon vma->flags = VM_MAP | vm_flags; 635f9040773SArd Biesheuvel vma->caller = __builtin_return_address(0); 636f9040773SArd Biesheuvel 637f9040773SArd Biesheuvel vm_area_add_early(vma); 638068a17a5SMark Rutland } 639068a17a5SMark Rutland 64028b066daSArd Biesheuvel static int __init parse_rodata(char *arg) 64128b066daSArd Biesheuvel { 642c55191e9SArd Biesheuvel int ret = strtobool(arg, &rodata_enabled); 643c55191e9SArd Biesheuvel if (!ret) { 644c55191e9SArd Biesheuvel rodata_full = false; 645c55191e9SArd Biesheuvel return 0; 646c55191e9SArd Biesheuvel } 647c55191e9SArd Biesheuvel 648c55191e9SArd Biesheuvel /* permit 'full' in addition to boolean options */ 649c55191e9SArd Biesheuvel if (strcmp(arg, "full")) 650c55191e9SArd Biesheuvel return -EINVAL; 651c55191e9SArd Biesheuvel 652c55191e9SArd Biesheuvel rodata_enabled = true; 653c55191e9SArd Biesheuvel rodata_full = true; 654c55191e9SArd Biesheuvel return 0; 65528b066daSArd Biesheuvel } 65628b066daSArd Biesheuvel early_param("rodata", parse_rodata); 65728b066daSArd Biesheuvel 65851a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 65951a0048bSWill Deacon static int __init map_entry_trampoline(void) 66051a0048bSWill Deacon { 661a9c406e6SJames Morse int i; 662a9c406e6SJames Morse 66351a0048bSWill Deacon pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 66451a0048bSWill Deacon phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start); 66551a0048bSWill Deacon 66651a0048bSWill Deacon /* The trampoline is always mapped and can therefore be global */ 66751a0048bSWill Deacon pgprot_val(prot) &= ~PTE_NG; 66851a0048bSWill Deacon 66951a0048bSWill Deacon /* Map only the text into the trampoline page table */ 67051a0048bSWill Deacon memset(tramp_pg_dir, 0, PGD_SIZE); 671a9c406e6SJames Morse __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, 672a9c406e6SJames Morse entry_tramp_text_size(), prot, 673a9c406e6SJames Morse __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS); 67451a0048bSWill Deacon 6756c27c408SWill Deacon /* Map both the text and data into the kernel page table */ 676a9c406e6SJames Morse for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++) 677a9c406e6SJames Morse __set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i, 678a9c406e6SJames Morse pa_start + i * PAGE_SIZE, prot); 679a9c406e6SJames Morse 6806c27c408SWill Deacon if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 6816c27c408SWill Deacon extern char __entry_tramp_data_start[]; 6826c27c408SWill Deacon 6836c27c408SWill Deacon __set_fixmap(FIX_ENTRY_TRAMP_DATA, 6846c27c408SWill Deacon __pa_symbol(__entry_tramp_data_start), 6856c27c408SWill Deacon PAGE_KERNEL_RO); 6866c27c408SWill Deacon } 6876c27c408SWill Deacon 68851a0048bSWill Deacon return 0; 68951a0048bSWill Deacon } 69051a0048bSWill Deacon core_initcall(map_entry_trampoline); 69151a0048bSWill Deacon #endif 69251a0048bSWill Deacon 693068a17a5SMark Rutland /* 694c8027285SMark Brown * Open coded check for BTI, only for use to determine configuration 695c8027285SMark Brown * for early mappings for before the cpufeature code has run. 696c8027285SMark Brown */ 697c8027285SMark Brown static bool arm64_early_this_cpu_has_bti(void) 698c8027285SMark Brown { 699c8027285SMark Brown u64 pfr1; 700c8027285SMark Brown 701c8027285SMark Brown if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)) 702c8027285SMark Brown return false; 703c8027285SMark Brown 70493ad55b7SMarc Zyngier pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1); 705c8027285SMark Brown return cpuid_feature_extract_unsigned_field(pfr1, 706c8027285SMark Brown ID_AA64PFR1_BT_SHIFT); 707c8027285SMark Brown } 708c8027285SMark Brown 709c8027285SMark Brown /* 710068a17a5SMark Rutland * Create fine-grained mappings for the kernel. 711068a17a5SMark Rutland */ 71220a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp) 713068a17a5SMark Rutland { 7142ebe088bSArd Biesheuvel static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext, 7152ebe088bSArd Biesheuvel vmlinux_initdata, vmlinux_data; 716068a17a5SMark Rutland 71728b066daSArd Biesheuvel /* 71828b066daSArd Biesheuvel * External debuggers may need to write directly to the text 71928b066daSArd Biesheuvel * mapping to install SW breakpoints. Allow this (only) when 72028b066daSArd Biesheuvel * explicitly requested with rodata=off. 72128b066daSArd Biesheuvel */ 72228b066daSArd Biesheuvel pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC; 72328b066daSArd Biesheuvel 724d27cfa1fSArd Biesheuvel /* 725c8027285SMark Brown * If we have a CPU that supports BTI and a kernel built for 726c8027285SMark Brown * BTI then mark the kernel executable text as guarded pages 727c8027285SMark Brown * now so we don't have to rewrite the page tables later. 728c8027285SMark Brown */ 729c8027285SMark Brown if (arm64_early_this_cpu_has_bti()) 730c8027285SMark Brown text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP); 731c8027285SMark Brown 732c8027285SMark Brown /* 733d27cfa1fSArd Biesheuvel * Only rodata will be remapped with different permissions later on, 734d27cfa1fSArd Biesheuvel * all other segments are allowed to use contiguous mappings. 735d27cfa1fSArd Biesheuvel */ 736e2a073ddSArd Biesheuvel map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0, 73792bbd16eSWill Deacon VM_NO_GUARD); 73820a004e7SWill Deacon map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL, 73992bbd16eSWill Deacon &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD); 74020a004e7SWill Deacon map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot, 74192bbd16eSWill Deacon &vmlinux_inittext, 0, VM_NO_GUARD); 74220a004e7SWill Deacon map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL, 74392bbd16eSWill Deacon &vmlinux_initdata, 0, VM_NO_GUARD); 74420a004e7SWill Deacon map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0); 745068a17a5SMark Rutland 746974b9b2cSMike Rapoport if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) { 747068a17a5SMark Rutland /* 748f9040773SArd Biesheuvel * The fixmap falls in a separate pgd to the kernel, and doesn't 749f9040773SArd Biesheuvel * live in the carveout for the swapper_pg_dir. We can simply 750f9040773SArd Biesheuvel * re-use the existing dir for the fixmap. 751068a17a5SMark Rutland */ 752974b9b2cSMike Rapoport set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START), 75320a004e7SWill Deacon READ_ONCE(*pgd_offset_k(FIXADDR_START))); 754f9040773SArd Biesheuvel } else if (CONFIG_PGTABLE_LEVELS > 3) { 755b333b0baSMark Rutland pgd_t *bm_pgdp; 756e9f63768SMike Rapoport p4d_t *bm_p4dp; 757b333b0baSMark Rutland pud_t *bm_pudp; 758f9040773SArd Biesheuvel /* 759f9040773SArd Biesheuvel * The fixmap shares its top level pgd entry with the kernel 760f9040773SArd Biesheuvel * mapping. This can really only occur when we are running 761f9040773SArd Biesheuvel * with 16k/4 levels, so we can simply reuse the pud level 762f9040773SArd Biesheuvel * entry instead. 763f9040773SArd Biesheuvel */ 764f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 765974b9b2cSMike Rapoport bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START); 766e9f63768SMike Rapoport bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START); 767e9f63768SMike Rapoport bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START); 768b333b0baSMark Rutland pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd)); 769f9040773SArd Biesheuvel pud_clear_fixmap(); 770f9040773SArd Biesheuvel } else { 771f9040773SArd Biesheuvel BUG(); 772f9040773SArd Biesheuvel } 773068a17a5SMark Rutland 77420a004e7SWill Deacon kasan_copy_shadow(pgdp); 775068a17a5SMark Rutland } 776068a17a5SMark Rutland 777c3cee924SArd Biesheuvel static void __init create_idmap(void) 778c3cee924SArd Biesheuvel { 779c3cee924SArd Biesheuvel u64 start = __pa_symbol(__idmap_text_start); 780c3cee924SArd Biesheuvel u64 size = __pa_symbol(__idmap_text_end) - start; 781c3cee924SArd Biesheuvel pgd_t *pgd = idmap_pg_dir; 782c3cee924SArd Biesheuvel u64 pgd_phys; 783c3cee924SArd Biesheuvel 784c3cee924SArd Biesheuvel /* check if we need an additional level of translation */ 785c3cee924SArd Biesheuvel if (VA_BITS < 48 && idmap_t0sz < (64 - VA_BITS_MIN)) { 786c3cee924SArd Biesheuvel pgd_phys = early_pgtable_alloc(PAGE_SHIFT); 787c3cee924SArd Biesheuvel set_pgd(&idmap_pg_dir[start >> VA_BITS], 788c3cee924SArd Biesheuvel __pgd(pgd_phys | P4D_TYPE_TABLE)); 789c3cee924SArd Biesheuvel pgd = __va(pgd_phys); 790c3cee924SArd Biesheuvel } 791c3cee924SArd Biesheuvel __create_pgd_mapping(pgd, start, start, size, PAGE_KERNEL_ROX, 792c3cee924SArd Biesheuvel early_pgtable_alloc, 0); 793c3cee924SArd Biesheuvel 794c3cee924SArd Biesheuvel if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) { 795c3cee924SArd Biesheuvel extern u32 __idmap_kpti_flag; 796c3cee924SArd Biesheuvel u64 pa = __pa_symbol(&__idmap_kpti_flag); 797c3cee924SArd Biesheuvel 798c3cee924SArd Biesheuvel /* 799c3cee924SArd Biesheuvel * The KPTI G-to-nG conversion code needs a read-write mapping 800c3cee924SArd Biesheuvel * of its synchronization flag in the ID map. 801c3cee924SArd Biesheuvel */ 802c3cee924SArd Biesheuvel __create_pgd_mapping(pgd, pa, pa, sizeof(u32), PAGE_KERNEL, 803c3cee924SArd Biesheuvel early_pgtable_alloc, 0); 804c3cee924SArd Biesheuvel } 805c3cee924SArd Biesheuvel } 806c3cee924SArd Biesheuvel 807c1cc1552SCatalin Marinas void __init paging_init(void) 808c1cc1552SCatalin Marinas { 8092330b7caSJun Yao pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir)); 810c3cee924SArd Biesheuvel extern pgd_t init_idmap_pg_dir[]; 811068a17a5SMark Rutland 812e8d13cceSArd Biesheuvel idmap_t0sz = 63UL - __fls(__pa_symbol(_end) | GENMASK(VA_BITS_MIN - 1, 0)); 813e8d13cceSArd Biesheuvel 81420a004e7SWill Deacon map_kernel(pgdp); 81520a004e7SWill Deacon map_mem(pgdp); 816068a17a5SMark Rutland 817068a17a5SMark Rutland pgd_clear_fixmap(); 818068a17a5SMark Rutland 819c3cee924SArd Biesheuvel cpu_replace_ttbr1(lm_alias(swapper_pg_dir), init_idmap_pg_dir); 8202b5548b6SJun Yao init_mm.pgd = swapper_pg_dir; 821068a17a5SMark Rutland 8223ecc6834SMike Rapoport memblock_phys_free(__pa_symbol(init_pg_dir), 8232b5548b6SJun Yao __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir)); 82424cc61d8SArd Biesheuvel 82524cc61d8SArd Biesheuvel memblock_allow_resize(); 826c3cee924SArd Biesheuvel 827c3cee924SArd Biesheuvel create_idmap(); 828c1cc1552SCatalin Marinas } 829c1cc1552SCatalin Marinas 830c1cc1552SCatalin Marinas /* 831c1cc1552SCatalin Marinas * Check whether a kernel address is valid (derived from arch/x86/). 832c1cc1552SCatalin Marinas */ 833c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr) 834c1cc1552SCatalin Marinas { 83520a004e7SWill Deacon pgd_t *pgdp; 836e9f63768SMike Rapoport p4d_t *p4dp; 83720a004e7SWill Deacon pud_t *pudp, pud; 83820a004e7SWill Deacon pmd_t *pmdp, pmd; 83920a004e7SWill Deacon pte_t *ptep, pte; 840c1cc1552SCatalin Marinas 8418dd4daa0SShyam Thombre addr = arch_kasan_reset_tag(addr); 842c1cc1552SCatalin Marinas if ((((long)addr) >> VA_BITS) != -1UL) 843c1cc1552SCatalin Marinas return 0; 844c1cc1552SCatalin Marinas 84520a004e7SWill Deacon pgdp = pgd_offset_k(addr); 84620a004e7SWill Deacon if (pgd_none(READ_ONCE(*pgdp))) 847c1cc1552SCatalin Marinas return 0; 848c1cc1552SCatalin Marinas 849e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 850e9f63768SMike Rapoport if (p4d_none(READ_ONCE(*p4dp))) 851e9f63768SMike Rapoport return 0; 852e9f63768SMike Rapoport 853e9f63768SMike Rapoport pudp = pud_offset(p4dp, addr); 85420a004e7SWill Deacon pud = READ_ONCE(*pudp); 85520a004e7SWill Deacon if (pud_none(pud)) 856c1cc1552SCatalin Marinas return 0; 857c1cc1552SCatalin Marinas 85820a004e7SWill Deacon if (pud_sect(pud)) 85920a004e7SWill Deacon return pfn_valid(pud_pfn(pud)); 860206a2a73SSteve Capper 86120a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 86220a004e7SWill Deacon pmd = READ_ONCE(*pmdp); 86320a004e7SWill Deacon if (pmd_none(pmd)) 864c1cc1552SCatalin Marinas return 0; 865c1cc1552SCatalin Marinas 86620a004e7SWill Deacon if (pmd_sect(pmd)) 86720a004e7SWill Deacon return pfn_valid(pmd_pfn(pmd)); 868da6e4cb6SDave Anderson 86920a004e7SWill Deacon ptep = pte_offset_kernel(pmdp, addr); 87020a004e7SWill Deacon pte = READ_ONCE(*ptep); 87120a004e7SWill Deacon if (pte_none(pte)) 872c1cc1552SCatalin Marinas return 0; 873c1cc1552SCatalin Marinas 87420a004e7SWill Deacon return pfn_valid(pte_pfn(pte)); 875c1cc1552SCatalin Marinas } 876bbd6ec60SAnshuman Khandual 877bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 878eee07935SAnshuman Khandual static void free_hotplug_page_range(struct page *page, size_t size, 879eee07935SAnshuman Khandual struct vmem_altmap *altmap) 880bbd6ec60SAnshuman Khandual { 881eee07935SAnshuman Khandual if (altmap) { 882eee07935SAnshuman Khandual vmem_altmap_free(altmap, size >> PAGE_SHIFT); 883eee07935SAnshuman Khandual } else { 884bbd6ec60SAnshuman Khandual WARN_ON(PageReserved(page)); 885bbd6ec60SAnshuman Khandual free_pages((unsigned long)page_address(page), get_order(size)); 886bbd6ec60SAnshuman Khandual } 887eee07935SAnshuman Khandual } 888bbd6ec60SAnshuman Khandual 889bbd6ec60SAnshuman Khandual static void free_hotplug_pgtable_page(struct page *page) 890bbd6ec60SAnshuman Khandual { 891eee07935SAnshuman Khandual free_hotplug_page_range(page, PAGE_SIZE, NULL); 892bbd6ec60SAnshuman Khandual } 893bbd6ec60SAnshuman Khandual 894bbd6ec60SAnshuman Khandual static bool pgtable_range_aligned(unsigned long start, unsigned long end, 895bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling, 896bbd6ec60SAnshuman Khandual unsigned long mask) 897bbd6ec60SAnshuman Khandual { 898bbd6ec60SAnshuman Khandual start &= mask; 899bbd6ec60SAnshuman Khandual if (start < floor) 900bbd6ec60SAnshuman Khandual return false; 901bbd6ec60SAnshuman Khandual 902bbd6ec60SAnshuman Khandual if (ceiling) { 903bbd6ec60SAnshuman Khandual ceiling &= mask; 904bbd6ec60SAnshuman Khandual if (!ceiling) 905bbd6ec60SAnshuman Khandual return false; 906bbd6ec60SAnshuman Khandual } 907bbd6ec60SAnshuman Khandual 908bbd6ec60SAnshuman Khandual if (end - 1 > ceiling - 1) 909bbd6ec60SAnshuman Khandual return false; 910bbd6ec60SAnshuman Khandual return true; 911bbd6ec60SAnshuman Khandual } 912bbd6ec60SAnshuman Khandual 913bbd6ec60SAnshuman Khandual static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, 914eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 915eee07935SAnshuman Khandual struct vmem_altmap *altmap) 916bbd6ec60SAnshuman Khandual { 917bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 918bbd6ec60SAnshuman Khandual 919bbd6ec60SAnshuman Khandual do { 920bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 921bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 922bbd6ec60SAnshuman Khandual if (pte_none(pte)) 923bbd6ec60SAnshuman Khandual continue; 924bbd6ec60SAnshuman Khandual 925bbd6ec60SAnshuman Khandual WARN_ON(!pte_present(pte)); 926bbd6ec60SAnshuman Khandual pte_clear(&init_mm, addr, ptep); 927bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 928bbd6ec60SAnshuman Khandual if (free_mapped) 929eee07935SAnshuman Khandual free_hotplug_page_range(pte_page(pte), 930eee07935SAnshuman Khandual PAGE_SIZE, altmap); 931bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 932bbd6ec60SAnshuman Khandual } 933bbd6ec60SAnshuman Khandual 934bbd6ec60SAnshuman Khandual static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr, 935eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 936eee07935SAnshuman Khandual struct vmem_altmap *altmap) 937bbd6ec60SAnshuman Khandual { 938bbd6ec60SAnshuman Khandual unsigned long next; 939bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 940bbd6ec60SAnshuman Khandual 941bbd6ec60SAnshuman Khandual do { 942bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 943bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 944bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 945bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 946bbd6ec60SAnshuman Khandual continue; 947bbd6ec60SAnshuman Khandual 948bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd)); 949bbd6ec60SAnshuman Khandual if (pmd_sect(pmd)) { 950bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 951bbd6ec60SAnshuman Khandual 952bbd6ec60SAnshuman Khandual /* 953bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PMD_SIZE 954bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 955bbd6ec60SAnshuman Khandual */ 956bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 957bbd6ec60SAnshuman Khandual if (free_mapped) 958bbd6ec60SAnshuman Khandual free_hotplug_page_range(pmd_page(pmd), 959eee07935SAnshuman Khandual PMD_SIZE, altmap); 960bbd6ec60SAnshuman Khandual continue; 961bbd6ec60SAnshuman Khandual } 962bbd6ec60SAnshuman Khandual WARN_ON(!pmd_table(pmd)); 963eee07935SAnshuman Khandual unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap); 964bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 965bbd6ec60SAnshuman Khandual } 966bbd6ec60SAnshuman Khandual 967bbd6ec60SAnshuman Khandual static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr, 968eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 969eee07935SAnshuman Khandual struct vmem_altmap *altmap) 970bbd6ec60SAnshuman Khandual { 971bbd6ec60SAnshuman Khandual unsigned long next; 972bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 973bbd6ec60SAnshuman Khandual 974bbd6ec60SAnshuman Khandual do { 975bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 976bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 977bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 978bbd6ec60SAnshuman Khandual if (pud_none(pud)) 979bbd6ec60SAnshuman Khandual continue; 980bbd6ec60SAnshuman Khandual 981bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud)); 982bbd6ec60SAnshuman Khandual if (pud_sect(pud)) { 983bbd6ec60SAnshuman Khandual pud_clear(pudp); 984bbd6ec60SAnshuman Khandual 985bbd6ec60SAnshuman Khandual /* 986bbd6ec60SAnshuman Khandual * One TLBI should be sufficient here as the PUD_SIZE 987bbd6ec60SAnshuman Khandual * range is mapped with a single block entry. 988bbd6ec60SAnshuman Khandual */ 989bbd6ec60SAnshuman Khandual flush_tlb_kernel_range(addr, addr + PAGE_SIZE); 990bbd6ec60SAnshuman Khandual if (free_mapped) 991bbd6ec60SAnshuman Khandual free_hotplug_page_range(pud_page(pud), 992eee07935SAnshuman Khandual PUD_SIZE, altmap); 993bbd6ec60SAnshuman Khandual continue; 994bbd6ec60SAnshuman Khandual } 995bbd6ec60SAnshuman Khandual WARN_ON(!pud_table(pud)); 996eee07935SAnshuman Khandual unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap); 997bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 998bbd6ec60SAnshuman Khandual } 999bbd6ec60SAnshuman Khandual 1000bbd6ec60SAnshuman Khandual static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr, 1001eee07935SAnshuman Khandual unsigned long end, bool free_mapped, 1002eee07935SAnshuman Khandual struct vmem_altmap *altmap) 1003bbd6ec60SAnshuman Khandual { 1004bbd6ec60SAnshuman Khandual unsigned long next; 1005bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 1006bbd6ec60SAnshuman Khandual 1007bbd6ec60SAnshuman Khandual do { 1008bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 1009bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 1010bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 1011bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 1012bbd6ec60SAnshuman Khandual continue; 1013bbd6ec60SAnshuman Khandual 1014bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 1015eee07935SAnshuman Khandual unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap); 1016bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1017bbd6ec60SAnshuman Khandual } 1018bbd6ec60SAnshuman Khandual 1019bbd6ec60SAnshuman Khandual static void unmap_hotplug_range(unsigned long addr, unsigned long end, 1020eee07935SAnshuman Khandual bool free_mapped, struct vmem_altmap *altmap) 1021bbd6ec60SAnshuman Khandual { 1022bbd6ec60SAnshuman Khandual unsigned long next; 1023bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 1024bbd6ec60SAnshuman Khandual 1025eee07935SAnshuman Khandual /* 1026eee07935SAnshuman Khandual * altmap can only be used as vmemmap mapping backing memory. 1027eee07935SAnshuman Khandual * In case the backing memory itself is not being freed, then 1028eee07935SAnshuman Khandual * altmap is irrelevant. Warn about this inconsistency when 1029eee07935SAnshuman Khandual * encountered. 1030eee07935SAnshuman Khandual */ 1031eee07935SAnshuman Khandual WARN_ON(!free_mapped && altmap); 1032eee07935SAnshuman Khandual 1033bbd6ec60SAnshuman Khandual do { 1034bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 1035bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 1036bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 1037bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 1038bbd6ec60SAnshuman Khandual continue; 1039bbd6ec60SAnshuman Khandual 1040bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 1041eee07935SAnshuman Khandual unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap); 1042bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1043bbd6ec60SAnshuman Khandual } 1044bbd6ec60SAnshuman Khandual 1045bbd6ec60SAnshuman Khandual static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, 1046bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1047bbd6ec60SAnshuman Khandual unsigned long ceiling) 1048bbd6ec60SAnshuman Khandual { 1049bbd6ec60SAnshuman Khandual pte_t *ptep, pte; 1050bbd6ec60SAnshuman Khandual unsigned long i, start = addr; 1051bbd6ec60SAnshuman Khandual 1052bbd6ec60SAnshuman Khandual do { 1053bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, addr); 1054bbd6ec60SAnshuman Khandual pte = READ_ONCE(*ptep); 1055bbd6ec60SAnshuman Khandual 1056bbd6ec60SAnshuman Khandual /* 1057bbd6ec60SAnshuman Khandual * This is just a sanity check here which verifies that 1058bbd6ec60SAnshuman Khandual * pte clearing has been done by earlier unmap loops. 1059bbd6ec60SAnshuman Khandual */ 1060bbd6ec60SAnshuman Khandual WARN_ON(!pte_none(pte)); 1061bbd6ec60SAnshuman Khandual } while (addr += PAGE_SIZE, addr < end); 1062bbd6ec60SAnshuman Khandual 1063bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK)) 1064bbd6ec60SAnshuman Khandual return; 1065bbd6ec60SAnshuman Khandual 1066bbd6ec60SAnshuman Khandual /* 1067bbd6ec60SAnshuman Khandual * Check whether we can free the pte page if the rest of the 1068bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1069bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1070bbd6ec60SAnshuman Khandual */ 1071bbd6ec60SAnshuman Khandual ptep = pte_offset_kernel(pmdp, 0UL); 1072bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PTE; i++) { 1073bbd6ec60SAnshuman Khandual if (!pte_none(READ_ONCE(ptep[i]))) 1074bbd6ec60SAnshuman Khandual return; 1075bbd6ec60SAnshuman Khandual } 1076bbd6ec60SAnshuman Khandual 1077bbd6ec60SAnshuman Khandual pmd_clear(pmdp); 1078bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1079bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(ptep)); 1080bbd6ec60SAnshuman Khandual } 1081bbd6ec60SAnshuman Khandual 1082bbd6ec60SAnshuman Khandual static void free_empty_pmd_table(pud_t *pudp, unsigned long addr, 1083bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1084bbd6ec60SAnshuman Khandual unsigned long ceiling) 1085bbd6ec60SAnshuman Khandual { 1086bbd6ec60SAnshuman Khandual pmd_t *pmdp, pmd; 1087bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1088bbd6ec60SAnshuman Khandual 1089bbd6ec60SAnshuman Khandual do { 1090bbd6ec60SAnshuman Khandual next = pmd_addr_end(addr, end); 1091bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, addr); 1092bbd6ec60SAnshuman Khandual pmd = READ_ONCE(*pmdp); 1093bbd6ec60SAnshuman Khandual if (pmd_none(pmd)) 1094bbd6ec60SAnshuman Khandual continue; 1095bbd6ec60SAnshuman Khandual 1096bbd6ec60SAnshuman Khandual WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd)); 1097bbd6ec60SAnshuman Khandual free_empty_pte_table(pmdp, addr, next, floor, ceiling); 1098bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1099bbd6ec60SAnshuman Khandual 1100bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 2) 1101bbd6ec60SAnshuman Khandual return; 1102bbd6ec60SAnshuman Khandual 1103bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK)) 1104bbd6ec60SAnshuman Khandual return; 1105bbd6ec60SAnshuman Khandual 1106bbd6ec60SAnshuman Khandual /* 1107bbd6ec60SAnshuman Khandual * Check whether we can free the pmd page if the rest of the 1108bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1109bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1110bbd6ec60SAnshuman Khandual */ 1111bbd6ec60SAnshuman Khandual pmdp = pmd_offset(pudp, 0UL); 1112bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PMD; i++) { 1113bbd6ec60SAnshuman Khandual if (!pmd_none(READ_ONCE(pmdp[i]))) 1114bbd6ec60SAnshuman Khandual return; 1115bbd6ec60SAnshuman Khandual } 1116bbd6ec60SAnshuman Khandual 1117bbd6ec60SAnshuman Khandual pud_clear(pudp); 1118bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1119bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pmdp)); 1120bbd6ec60SAnshuman Khandual } 1121bbd6ec60SAnshuman Khandual 1122bbd6ec60SAnshuman Khandual static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr, 1123bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1124bbd6ec60SAnshuman Khandual unsigned long ceiling) 1125bbd6ec60SAnshuman Khandual { 1126bbd6ec60SAnshuman Khandual pud_t *pudp, pud; 1127bbd6ec60SAnshuman Khandual unsigned long i, next, start = addr; 1128bbd6ec60SAnshuman Khandual 1129bbd6ec60SAnshuman Khandual do { 1130bbd6ec60SAnshuman Khandual next = pud_addr_end(addr, end); 1131bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, addr); 1132bbd6ec60SAnshuman Khandual pud = READ_ONCE(*pudp); 1133bbd6ec60SAnshuman Khandual if (pud_none(pud)) 1134bbd6ec60SAnshuman Khandual continue; 1135bbd6ec60SAnshuman Khandual 1136bbd6ec60SAnshuman Khandual WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud)); 1137bbd6ec60SAnshuman Khandual free_empty_pmd_table(pudp, addr, next, floor, ceiling); 1138bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1139bbd6ec60SAnshuman Khandual 1140bbd6ec60SAnshuman Khandual if (CONFIG_PGTABLE_LEVELS <= 3) 1141bbd6ec60SAnshuman Khandual return; 1142bbd6ec60SAnshuman Khandual 1143bbd6ec60SAnshuman Khandual if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK)) 1144bbd6ec60SAnshuman Khandual return; 1145bbd6ec60SAnshuman Khandual 1146bbd6ec60SAnshuman Khandual /* 1147bbd6ec60SAnshuman Khandual * Check whether we can free the pud page if the rest of the 1148bbd6ec60SAnshuman Khandual * entries are empty. Overlap with other regions have been 1149bbd6ec60SAnshuman Khandual * handled by the floor/ceiling check. 1150bbd6ec60SAnshuman Khandual */ 1151bbd6ec60SAnshuman Khandual pudp = pud_offset(p4dp, 0UL); 1152bbd6ec60SAnshuman Khandual for (i = 0; i < PTRS_PER_PUD; i++) { 1153bbd6ec60SAnshuman Khandual if (!pud_none(READ_ONCE(pudp[i]))) 1154bbd6ec60SAnshuman Khandual return; 1155bbd6ec60SAnshuman Khandual } 1156bbd6ec60SAnshuman Khandual 1157bbd6ec60SAnshuman Khandual p4d_clear(p4dp); 1158bbd6ec60SAnshuman Khandual __flush_tlb_kernel_pgtable(start); 1159bbd6ec60SAnshuman Khandual free_hotplug_pgtable_page(virt_to_page(pudp)); 1160bbd6ec60SAnshuman Khandual } 1161bbd6ec60SAnshuman Khandual 1162bbd6ec60SAnshuman Khandual static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr, 1163bbd6ec60SAnshuman Khandual unsigned long end, unsigned long floor, 1164bbd6ec60SAnshuman Khandual unsigned long ceiling) 1165bbd6ec60SAnshuman Khandual { 1166bbd6ec60SAnshuman Khandual unsigned long next; 1167bbd6ec60SAnshuman Khandual p4d_t *p4dp, p4d; 1168bbd6ec60SAnshuman Khandual 1169bbd6ec60SAnshuman Khandual do { 1170bbd6ec60SAnshuman Khandual next = p4d_addr_end(addr, end); 1171bbd6ec60SAnshuman Khandual p4dp = p4d_offset(pgdp, addr); 1172bbd6ec60SAnshuman Khandual p4d = READ_ONCE(*p4dp); 1173bbd6ec60SAnshuman Khandual if (p4d_none(p4d)) 1174bbd6ec60SAnshuman Khandual continue; 1175bbd6ec60SAnshuman Khandual 1176bbd6ec60SAnshuman Khandual WARN_ON(!p4d_present(p4d)); 1177bbd6ec60SAnshuman Khandual free_empty_pud_table(p4dp, addr, next, floor, ceiling); 1178bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1179bbd6ec60SAnshuman Khandual } 1180bbd6ec60SAnshuman Khandual 1181bbd6ec60SAnshuman Khandual static void free_empty_tables(unsigned long addr, unsigned long end, 1182bbd6ec60SAnshuman Khandual unsigned long floor, unsigned long ceiling) 1183bbd6ec60SAnshuman Khandual { 1184bbd6ec60SAnshuman Khandual unsigned long next; 1185bbd6ec60SAnshuman Khandual pgd_t *pgdp, pgd; 1186bbd6ec60SAnshuman Khandual 1187bbd6ec60SAnshuman Khandual do { 1188bbd6ec60SAnshuman Khandual next = pgd_addr_end(addr, end); 1189bbd6ec60SAnshuman Khandual pgdp = pgd_offset_k(addr); 1190bbd6ec60SAnshuman Khandual pgd = READ_ONCE(*pgdp); 1191bbd6ec60SAnshuman Khandual if (pgd_none(pgd)) 1192bbd6ec60SAnshuman Khandual continue; 1193bbd6ec60SAnshuman Khandual 1194bbd6ec60SAnshuman Khandual WARN_ON(!pgd_present(pgd)); 1195bbd6ec60SAnshuman Khandual free_empty_p4d_table(pgdp, addr, next, floor, ceiling); 1196bbd6ec60SAnshuman Khandual } while (addr = next, addr < end); 1197bbd6ec60SAnshuman Khandual } 1198bbd6ec60SAnshuman Khandual #endif 1199bbd6ec60SAnshuman Khandual 12002062d44dSAnshuman Khandual #if !ARM64_KERNEL_USES_PMD_MAPS 12017b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 12027b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1203c1cc1552SCatalin Marinas { 1204edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1205eee07935SAnshuman Khandual return vmemmap_populate_basepages(start, end, node, altmap); 1206c1cc1552SCatalin Marinas } 12072062d44dSAnshuman Khandual #else /* !ARM64_KERNEL_USES_PMD_MAPS */ 12087b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 12097b73d978SChristoph Hellwig struct vmem_altmap *altmap) 1210c1cc1552SCatalin Marinas { 12110aad818bSJohannes Weiner unsigned long addr = start; 1212c1cc1552SCatalin Marinas unsigned long next; 121320a004e7SWill Deacon pgd_t *pgdp; 1214e9f63768SMike Rapoport p4d_t *p4dp; 121520a004e7SWill Deacon pud_t *pudp; 121620a004e7SWill Deacon pmd_t *pmdp; 1217c1cc1552SCatalin Marinas 1218edb739eeSAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1219c1cc1552SCatalin Marinas do { 1220c1cc1552SCatalin Marinas next = pmd_addr_end(addr, end); 1221c1cc1552SCatalin Marinas 122220a004e7SWill Deacon pgdp = vmemmap_pgd_populate(addr, node); 122320a004e7SWill Deacon if (!pgdp) 1224c1cc1552SCatalin Marinas return -ENOMEM; 1225c1cc1552SCatalin Marinas 1226e9f63768SMike Rapoport p4dp = vmemmap_p4d_populate(pgdp, addr, node); 1227e9f63768SMike Rapoport if (!p4dp) 1228e9f63768SMike Rapoport return -ENOMEM; 1229e9f63768SMike Rapoport 1230e9f63768SMike Rapoport pudp = vmemmap_pud_populate(p4dp, addr, node); 123120a004e7SWill Deacon if (!pudp) 1232c1cc1552SCatalin Marinas return -ENOMEM; 1233c1cc1552SCatalin Marinas 123420a004e7SWill Deacon pmdp = pmd_offset(pudp, addr); 123520a004e7SWill Deacon if (pmd_none(READ_ONCE(*pmdp))) { 1236c1cc1552SCatalin Marinas void *p = NULL; 1237c1cc1552SCatalin Marinas 1238eee07935SAnshuman Khandual p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap); 12399f84f39fSSudarshan Rajagopalan if (!p) { 12409f84f39fSSudarshan Rajagopalan if (vmemmap_populate_basepages(addr, next, node, altmap)) 1241c1cc1552SCatalin Marinas return -ENOMEM; 12429f84f39fSSudarshan Rajagopalan continue; 12439f84f39fSSudarshan Rajagopalan } 1244c1cc1552SCatalin Marinas 124520a004e7SWill Deacon pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL)); 1246c1cc1552SCatalin Marinas } else 124720a004e7SWill Deacon vmemmap_verify((pte_t *)pmdp, node, addr, next); 1248c1cc1552SCatalin Marinas } while (addr = next, addr != end); 1249c1cc1552SCatalin Marinas 1250c1cc1552SCatalin Marinas return 0; 1251c1cc1552SCatalin Marinas } 12522062d44dSAnshuman Khandual #endif /* !ARM64_KERNEL_USES_PMD_MAPS */ 125340221c73SAnshuman Khandual 125440221c73SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG 125524b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end, 125624b6d416SChristoph Hellwig struct vmem_altmap *altmap) 12570197518cSTang Chen { 1258bbd6ec60SAnshuman Khandual WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END)); 1259bbd6ec60SAnshuman Khandual 1260eee07935SAnshuman Khandual unmap_hotplug_range(start, end, true, altmap); 1261bbd6ec60SAnshuman Khandual free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END); 12620197518cSTang Chen } 126340221c73SAnshuman Khandual #endif /* CONFIG_MEMORY_HOTPLUG */ 1264af86e597SLaura Abbott 1265af86e597SLaura Abbott static inline pud_t *fixmap_pud(unsigned long addr) 1266af86e597SLaura Abbott { 126720a004e7SWill Deacon pgd_t *pgdp = pgd_offset_k(addr); 1268e9f63768SMike Rapoport p4d_t *p4dp = p4d_offset(pgdp, addr); 1269e9f63768SMike Rapoport p4d_t p4d = READ_ONCE(*p4dp); 1270af86e597SLaura Abbott 1271e9f63768SMike Rapoport BUG_ON(p4d_none(p4d) || p4d_bad(p4d)); 1272af86e597SLaura Abbott 1273e9f63768SMike Rapoport return pud_offset_kimg(p4dp, addr); 1274af86e597SLaura Abbott } 1275af86e597SLaura Abbott 1276af86e597SLaura Abbott static inline pmd_t *fixmap_pmd(unsigned long addr) 1277af86e597SLaura Abbott { 127820a004e7SWill Deacon pud_t *pudp = fixmap_pud(addr); 127920a004e7SWill Deacon pud_t pud = READ_ONCE(*pudp); 1280af86e597SLaura Abbott 128120a004e7SWill Deacon BUG_ON(pud_none(pud) || pud_bad(pud)); 1282af86e597SLaura Abbott 128320a004e7SWill Deacon return pmd_offset_kimg(pudp, addr); 1284af86e597SLaura Abbott } 1285af86e597SLaura Abbott 1286af86e597SLaura Abbott static inline pte_t *fixmap_pte(unsigned long addr) 1287af86e597SLaura Abbott { 1288157962f5SArd Biesheuvel return &bm_pte[pte_index(addr)]; 1289af86e597SLaura Abbott } 1290af86e597SLaura Abbott 12912077be67SLaura Abbott /* 12922077be67SLaura Abbott * The p*d_populate functions call virt_to_phys implicitly so they can't be used 12932077be67SLaura Abbott * directly on kernel symbols (bm_p*d). This function is called too early to use 12942077be67SLaura Abbott * lm_alias so __p*d_populate functions must be used to populate with the 12952077be67SLaura Abbott * physical address from __pa_symbol. 12962077be67SLaura Abbott */ 1297af86e597SLaura Abbott void __init early_fixmap_init(void) 1298af86e597SLaura Abbott { 1299e9f63768SMike Rapoport pgd_t *pgdp; 1300e9f63768SMike Rapoport p4d_t *p4dp, p4d; 130120a004e7SWill Deacon pud_t *pudp; 130220a004e7SWill Deacon pmd_t *pmdp; 1303af86e597SLaura Abbott unsigned long addr = FIXADDR_START; 1304af86e597SLaura Abbott 130520a004e7SWill Deacon pgdp = pgd_offset_k(addr); 1306e9f63768SMike Rapoport p4dp = p4d_offset(pgdp, addr); 1307e9f63768SMike Rapoport p4d = READ_ONCE(*p4dp); 1308f80fb3a3SArd Biesheuvel if (CONFIG_PGTABLE_LEVELS > 3 && 1309e9f63768SMike Rapoport !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) { 1310f9040773SArd Biesheuvel /* 1311f9040773SArd Biesheuvel * We only end up here if the kernel mapping and the fixmap 1312f9040773SArd Biesheuvel * share the top level pgd entry, which should only happen on 1313f9040773SArd Biesheuvel * 16k/4 levels configurations. 1314f9040773SArd Biesheuvel */ 1315f9040773SArd Biesheuvel BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES)); 1316e9f63768SMike Rapoport pudp = pud_offset_kimg(p4dp, addr); 1317f9040773SArd Biesheuvel } else { 1318e9f63768SMike Rapoport if (p4d_none(p4d)) 1319c1fd78a7SArd Biesheuvel __p4d_populate(p4dp, __pa_symbol(bm_pud), P4D_TYPE_TABLE); 132020a004e7SWill Deacon pudp = fixmap_pud(addr); 1321f9040773SArd Biesheuvel } 132220a004e7SWill Deacon if (pud_none(READ_ONCE(*pudp))) 1323c1fd78a7SArd Biesheuvel __pud_populate(pudp, __pa_symbol(bm_pmd), PUD_TYPE_TABLE); 132420a004e7SWill Deacon pmdp = fixmap_pmd(addr); 132520a004e7SWill Deacon __pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE); 1326af86e597SLaura Abbott 1327af86e597SLaura Abbott /* 1328af86e597SLaura Abbott * The boot-ioremap range spans multiple pmds, for which 1329157962f5SArd Biesheuvel * we are not prepared: 1330af86e597SLaura Abbott */ 1331af86e597SLaura Abbott BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) 1332af86e597SLaura Abbott != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); 1333af86e597SLaura Abbott 133420a004e7SWill Deacon if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN))) 133520a004e7SWill Deacon || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) { 1336af86e597SLaura Abbott WARN_ON(1); 133720a004e7SWill Deacon pr_warn("pmdp %p != %p, %p\n", 133820a004e7SWill Deacon pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)), 1339af86e597SLaura Abbott fixmap_pmd(fix_to_virt(FIX_BTMAP_END))); 1340af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", 1341af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_BEGIN)); 1342af86e597SLaura Abbott pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", 1343af86e597SLaura Abbott fix_to_virt(FIX_BTMAP_END)); 1344af86e597SLaura Abbott 1345af86e597SLaura Abbott pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); 1346af86e597SLaura Abbott pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); 1347af86e597SLaura Abbott } 1348af86e597SLaura Abbott } 1349af86e597SLaura Abbott 135018b4b276SJames Morse /* 135118b4b276SJames Morse * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we 135218b4b276SJames Morse * ever need to use IPIs for TLB broadcasting, then we're in trouble here. 135318b4b276SJames Morse */ 1354af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx, 1355af86e597SLaura Abbott phys_addr_t phys, pgprot_t flags) 1356af86e597SLaura Abbott { 1357af86e597SLaura Abbott unsigned long addr = __fix_to_virt(idx); 135820a004e7SWill Deacon pte_t *ptep; 1359af86e597SLaura Abbott 1360b63dbef9SMark Rutland BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 1361af86e597SLaura Abbott 136220a004e7SWill Deacon ptep = fixmap_pte(addr); 1363af86e597SLaura Abbott 1364af86e597SLaura Abbott if (pgprot_val(flags)) { 136520a004e7SWill Deacon set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags)); 1366af86e597SLaura Abbott } else { 136720a004e7SWill Deacon pte_clear(&init_mm, addr, ptep); 1368af86e597SLaura Abbott flush_tlb_kernel_range(addr, addr+PAGE_SIZE); 1369af86e597SLaura Abbott } 1370af86e597SLaura Abbott } 137161bd93ceSArd Biesheuvel 1372e112b032SHsin-Yi Wang void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot) 137361bd93ceSArd Biesheuvel { 137461bd93ceSArd Biesheuvel const u64 dt_virt_base = __fix_to_virt(FIX_FDT); 1375f80fb3a3SArd Biesheuvel int offset; 137661bd93ceSArd Biesheuvel void *dt_virt; 137761bd93ceSArd Biesheuvel 137861bd93ceSArd Biesheuvel /* 137961bd93ceSArd Biesheuvel * Check whether the physical FDT address is set and meets the minimum 138061bd93ceSArd Biesheuvel * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be 138104a84810SArd Biesheuvel * at least 8 bytes so that we can always access the magic and size 138204a84810SArd Biesheuvel * fields of the FDT header after mapping the first chunk, double check 138304a84810SArd Biesheuvel * here if that is indeed the case. 138461bd93ceSArd Biesheuvel */ 138561bd93ceSArd Biesheuvel BUILD_BUG_ON(MIN_FDT_ALIGN < 8); 138661bd93ceSArd Biesheuvel if (!dt_phys || dt_phys % MIN_FDT_ALIGN) 138761bd93ceSArd Biesheuvel return NULL; 138861bd93ceSArd Biesheuvel 138961bd93ceSArd Biesheuvel /* 139061bd93ceSArd Biesheuvel * Make sure that the FDT region can be mapped without the need to 139161bd93ceSArd Biesheuvel * allocate additional translation table pages, so that it is safe 1392132233a7SLaura Abbott * to call create_mapping_noalloc() this early. 139361bd93ceSArd Biesheuvel * 139461bd93ceSArd Biesheuvel * On 64k pages, the FDT will be mapped using PTEs, so we need to 139561bd93ceSArd Biesheuvel * be in the same PMD as the rest of the fixmap. 139661bd93ceSArd Biesheuvel * On 4k pages, we'll use section mappings for the FDT so we only 139761bd93ceSArd Biesheuvel * have to be in the same PUD. 139861bd93ceSArd Biesheuvel */ 139961bd93ceSArd Biesheuvel BUILD_BUG_ON(dt_virt_base % SZ_2M); 140061bd93ceSArd Biesheuvel 1401b433dce0SSuzuki K. Poulose BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT != 1402b433dce0SSuzuki K. Poulose __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT); 140361bd93ceSArd Biesheuvel 1404b433dce0SSuzuki K. Poulose offset = dt_phys % SWAPPER_BLOCK_SIZE; 140561bd93ceSArd Biesheuvel dt_virt = (void *)dt_virt_base + offset; 140661bd93ceSArd Biesheuvel 140761bd93ceSArd Biesheuvel /* map the first chunk so we can read the size from the header */ 1408132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), 1409132233a7SLaura Abbott dt_virt_base, SWAPPER_BLOCK_SIZE, prot); 141061bd93ceSArd Biesheuvel 141104a84810SArd Biesheuvel if (fdt_magic(dt_virt) != FDT_MAGIC) 141261bd93ceSArd Biesheuvel return NULL; 141361bd93ceSArd Biesheuvel 1414f80fb3a3SArd Biesheuvel *size = fdt_totalsize(dt_virt); 1415f80fb3a3SArd Biesheuvel if (*size > MAX_FDT_SIZE) 141661bd93ceSArd Biesheuvel return NULL; 141761bd93ceSArd Biesheuvel 1418f80fb3a3SArd Biesheuvel if (offset + *size > SWAPPER_BLOCK_SIZE) 1419132233a7SLaura Abbott create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base, 1420f80fb3a3SArd Biesheuvel round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot); 1421f80fb3a3SArd Biesheuvel 1422f80fb3a3SArd Biesheuvel return dt_virt; 1423f80fb3a3SArd Biesheuvel } 1424f80fb3a3SArd Biesheuvel 142520a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot) 1426324420bfSArd Biesheuvel { 1427f7f0097aSAnshuman Khandual pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot)); 142815122ee2SWill Deacon 142982034c23SLaura Abbott /* Only allow permission changes for now */ 143082034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)), 143182034c23SLaura Abbott pud_val(new_pud))) 143215122ee2SWill Deacon return 0; 143315122ee2SWill Deacon 143487dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PUD_MASK); 143582034c23SLaura Abbott set_pud(pudp, new_pud); 1436324420bfSArd Biesheuvel return 1; 1437324420bfSArd Biesheuvel } 1438324420bfSArd Biesheuvel 143920a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot) 1440324420bfSArd Biesheuvel { 1441f7f0097aSAnshuman Khandual pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot)); 144215122ee2SWill Deacon 144382034c23SLaura Abbott /* Only allow permission changes for now */ 144482034c23SLaura Abbott if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)), 144582034c23SLaura Abbott pmd_val(new_pmd))) 144615122ee2SWill Deacon return 0; 144715122ee2SWill Deacon 144887dedf7cSAnshuman Khandual VM_BUG_ON(phys & ~PMD_MASK); 144982034c23SLaura Abbott set_pmd(pmdp, new_pmd); 1450324420bfSArd Biesheuvel return 1; 1451324420bfSArd Biesheuvel } 1452324420bfSArd Biesheuvel 1453d8a71905SJonathan Marek int pud_clear_huge(pud_t *pudp) 1454d8a71905SJonathan Marek { 1455d8a71905SJonathan Marek if (!pud_sect(READ_ONCE(*pudp))) 1456d8a71905SJonathan Marek return 0; 1457d8a71905SJonathan Marek pud_clear(pudp); 1458d8a71905SJonathan Marek return 1; 1459d8a71905SJonathan Marek } 1460d8a71905SJonathan Marek 146120a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp) 1462324420bfSArd Biesheuvel { 146320a004e7SWill Deacon if (!pmd_sect(READ_ONCE(*pmdp))) 1464324420bfSArd Biesheuvel return 0; 146520a004e7SWill Deacon pmd_clear(pmdp); 1466324420bfSArd Biesheuvel return 1; 1467324420bfSArd Biesheuvel } 1468b6bdb751SToshi Kani 1469ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr) 1470b6bdb751SToshi Kani { 1471ec28bb9cSChintan Pandya pte_t *table; 1472ec28bb9cSChintan Pandya pmd_t pmd; 1473ec28bb9cSChintan Pandya 1474ec28bb9cSChintan Pandya pmd = READ_ONCE(*pmdp); 1475ec28bb9cSChintan Pandya 1476fac880c7SMark Rutland if (!pmd_table(pmd)) { 14779c006972SWill Deacon VM_WARN_ON(1); 1478ec28bb9cSChintan Pandya return 1; 1479b6bdb751SToshi Kani } 1480b6bdb751SToshi Kani 1481ec28bb9cSChintan Pandya table = pte_offset_kernel(pmdp, addr); 1482ec28bb9cSChintan Pandya pmd_clear(pmdp); 1483ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1484ec28bb9cSChintan Pandya pte_free_kernel(NULL, table); 1485ec28bb9cSChintan Pandya return 1; 1486ec28bb9cSChintan Pandya } 1487ec28bb9cSChintan Pandya 1488ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr) 1489b6bdb751SToshi Kani { 1490ec28bb9cSChintan Pandya pmd_t *table; 1491ec28bb9cSChintan Pandya pmd_t *pmdp; 1492ec28bb9cSChintan Pandya pud_t pud; 1493ec28bb9cSChintan Pandya unsigned long next, end; 1494ec28bb9cSChintan Pandya 1495ec28bb9cSChintan Pandya pud = READ_ONCE(*pudp); 1496ec28bb9cSChintan Pandya 1497fac880c7SMark Rutland if (!pud_table(pud)) { 14989c006972SWill Deacon VM_WARN_ON(1); 1499ec28bb9cSChintan Pandya return 1; 1500ec28bb9cSChintan Pandya } 1501ec28bb9cSChintan Pandya 1502ec28bb9cSChintan Pandya table = pmd_offset(pudp, addr); 1503ec28bb9cSChintan Pandya pmdp = table; 1504ec28bb9cSChintan Pandya next = addr; 1505ec28bb9cSChintan Pandya end = addr + PUD_SIZE; 1506ec28bb9cSChintan Pandya do { 1507ec28bb9cSChintan Pandya pmd_free_pte_page(pmdp, next); 1508ec28bb9cSChintan Pandya } while (pmdp++, next += PMD_SIZE, next != end); 1509ec28bb9cSChintan Pandya 1510ec28bb9cSChintan Pandya pud_clear(pudp); 1511ec28bb9cSChintan Pandya __flush_tlb_kernel_pgtable(addr); 1512ec28bb9cSChintan Pandya pmd_free(NULL, table); 1513ec28bb9cSChintan Pandya return 1; 1514b6bdb751SToshi Kani } 15154ab21506SRobin Murphy 15164ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG 1517bbd6ec60SAnshuman Khandual static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size) 1518bbd6ec60SAnshuman Khandual { 1519bbd6ec60SAnshuman Khandual unsigned long end = start + size; 1520bbd6ec60SAnshuman Khandual 1521bbd6ec60SAnshuman Khandual WARN_ON(pgdir != init_mm.pgd); 1522bbd6ec60SAnshuman Khandual WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END)); 1523bbd6ec60SAnshuman Khandual 1524eee07935SAnshuman Khandual unmap_hotplug_range(start, end, false, NULL); 1525bbd6ec60SAnshuman Khandual free_empty_tables(start, end, PAGE_OFFSET, PAGE_END); 1526bbd6ec60SAnshuman Khandual } 1527bbd6ec60SAnshuman Khandual 152803aaf83fSAnshuman Khandual struct range arch_get_mappable_range(void) 152958284a90SAnshuman Khandual { 153003aaf83fSAnshuman Khandual struct range mhp_range; 1531ee7febceSPavel Tatashin u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1532ee7febceSPavel Tatashin u64 end_linear_pa = __pa(PAGE_END - 1); 1533ee7febceSPavel Tatashin 1534ee7febceSPavel Tatashin if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 1535ee7febceSPavel Tatashin /* 1536ee7febceSPavel Tatashin * Check for a wrap, it is possible because of randomized linear 1537ee7febceSPavel Tatashin * mapping the start physical address is actually bigger than 1538ee7febceSPavel Tatashin * the end physical address. In this case set start to zero 1539ee7febceSPavel Tatashin * because [0, end_linear_pa] range must still be able to cover 1540ee7febceSPavel Tatashin * all addressable physical addresses. 1541ee7febceSPavel Tatashin */ 1542ee7febceSPavel Tatashin if (start_linear_pa > end_linear_pa) 1543ee7febceSPavel Tatashin start_linear_pa = 0; 1544ee7febceSPavel Tatashin } 1545ee7febceSPavel Tatashin 1546ee7febceSPavel Tatashin WARN_ON(start_linear_pa > end_linear_pa); 154703aaf83fSAnshuman Khandual 154858284a90SAnshuman Khandual /* 154958284a90SAnshuman Khandual * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)] 155058284a90SAnshuman Khandual * accommodating both its ends but excluding PAGE_END. Max physical 155158284a90SAnshuman Khandual * range which can be mapped inside this linear mapping range, must 155258284a90SAnshuman Khandual * also be derived from its end points. 155358284a90SAnshuman Khandual */ 1554ee7febceSPavel Tatashin mhp_range.start = start_linear_pa; 1555ee7febceSPavel Tatashin mhp_range.end = end_linear_pa; 1556ee7febceSPavel Tatashin 155703aaf83fSAnshuman Khandual return mhp_range; 155858284a90SAnshuman Khandual } 155958284a90SAnshuman Khandual 1560940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size, 1561f5637d3bSLogan Gunthorpe struct mhp_params *params) 15624ab21506SRobin Murphy { 156387143f40SArd Biesheuvel int ret, flags = NO_EXEC_MAPPINGS; 15644ab21506SRobin Murphy 156503aaf83fSAnshuman Khandual VM_BUG_ON(!mhp_range_allowed(start, size, true)); 1566840b2398SMarco Elver 1567840b2398SMarco Elver /* 1568840b2398SMarco Elver * KFENCE requires linear map to be mapped at page granularity, so that 1569840b2398SMarco Elver * it is possible to protect/unprotect single pages in the KFENCE pool. 1570840b2398SMarco Elver */ 15716d47c23bSMike Rapoport if (can_set_direct_map() || IS_ENABLED(CONFIG_KFENCE)) 157287143f40SArd Biesheuvel flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; 15734ab21506SRobin Murphy 15744ab21506SRobin Murphy __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start), 1575bfeb022fSLogan Gunthorpe size, params->pgprot, __pgd_pgtable_alloc, 1576bfeb022fSLogan Gunthorpe flags); 15774ab21506SRobin Murphy 157816993c0fSDan Williams memblock_clear_nomap(start, size); 157916993c0fSDan Williams 1580bbd6ec60SAnshuman Khandual ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, 1581f5637d3bSLogan Gunthorpe params); 1582bbd6ec60SAnshuman Khandual if (ret) 1583bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, 1584bbd6ec60SAnshuman Khandual __phys_to_virt(start), size); 15858fac67caSSudarshan Rajagopalan else { 15868fac67caSSudarshan Rajagopalan max_pfn = PFN_UP(start + size); 15878fac67caSSudarshan Rajagopalan max_low_pfn = max_pfn; 15888fac67caSSudarshan Rajagopalan } 15898fac67caSSudarshan Rajagopalan 1590bbd6ec60SAnshuman Khandual return ret; 15914ab21506SRobin Murphy } 1592bbd6ec60SAnshuman Khandual 159365a2aa5fSDavid Hildenbrand void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) 159422eb6346SDavid Hildenbrand { 159522eb6346SDavid Hildenbrand unsigned long start_pfn = start >> PAGE_SHIFT; 159622eb6346SDavid Hildenbrand unsigned long nr_pages = size >> PAGE_SHIFT; 159722eb6346SDavid Hildenbrand 1598feee6b29SDavid Hildenbrand __remove_pages(start_pfn, nr_pages, altmap); 1599bbd6ec60SAnshuman Khandual __remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size); 160022eb6346SDavid Hildenbrand } 1601bbd6ec60SAnshuman Khandual 1602bbd6ec60SAnshuman Khandual /* 1603bbd6ec60SAnshuman Khandual * This memory hotplug notifier helps prevent boot memory from being 1604bbd6ec60SAnshuman Khandual * inadvertently removed as it blocks pfn range offlining process in 1605bbd6ec60SAnshuman Khandual * __offline_pages(). Hence this prevents both offlining as well as 1606bbd6ec60SAnshuman Khandual * removal process for boot memory which is initially always online. 1607bbd6ec60SAnshuman Khandual * In future if and when boot memory could be removed, this notifier 1608bbd6ec60SAnshuman Khandual * should be dropped and free_hotplug_page_range() should handle any 1609bbd6ec60SAnshuman Khandual * reserved pages allocated during boot. 1610bbd6ec60SAnshuman Khandual */ 1611bbd6ec60SAnshuman Khandual static int prevent_bootmem_remove_notifier(struct notifier_block *nb, 1612bbd6ec60SAnshuman Khandual unsigned long action, void *data) 1613bbd6ec60SAnshuman Khandual { 1614bbd6ec60SAnshuman Khandual struct mem_section *ms; 1615bbd6ec60SAnshuman Khandual struct memory_notify *arg = data; 1616bbd6ec60SAnshuman Khandual unsigned long end_pfn = arg->start_pfn + arg->nr_pages; 1617bbd6ec60SAnshuman Khandual unsigned long pfn = arg->start_pfn; 1618bbd6ec60SAnshuman Khandual 16199fb3d4a3SAnshuman Khandual if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE)) 1620bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1621bbd6ec60SAnshuman Khandual 1622bbd6ec60SAnshuman Khandual for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 16239fb3d4a3SAnshuman Khandual unsigned long start = PFN_PHYS(pfn); 16249fb3d4a3SAnshuman Khandual unsigned long end = start + (1UL << PA_SECTION_SHIFT); 16259fb3d4a3SAnshuman Khandual 1626bbd6ec60SAnshuman Khandual ms = __pfn_to_section(pfn); 16279fb3d4a3SAnshuman Khandual if (!early_section(ms)) 16289fb3d4a3SAnshuman Khandual continue; 16299fb3d4a3SAnshuman Khandual 16309fb3d4a3SAnshuman Khandual if (action == MEM_GOING_OFFLINE) { 16319fb3d4a3SAnshuman Khandual /* 16329fb3d4a3SAnshuman Khandual * Boot memory removal is not supported. Prevent 16339fb3d4a3SAnshuman Khandual * it via blocking any attempted offline request 16349fb3d4a3SAnshuman Khandual * for the boot memory and just report it. 16359fb3d4a3SAnshuman Khandual */ 16369fb3d4a3SAnshuman Khandual pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end); 1637bbd6ec60SAnshuman Khandual return NOTIFY_BAD; 16389fb3d4a3SAnshuman Khandual } else if (action == MEM_OFFLINE) { 16399fb3d4a3SAnshuman Khandual /* 16409fb3d4a3SAnshuman Khandual * This should have never happened. Boot memory 16419fb3d4a3SAnshuman Khandual * offlining should have been prevented by this 16429fb3d4a3SAnshuman Khandual * very notifier. Probably some memory removal 16439fb3d4a3SAnshuman Khandual * procedure might have changed which would then 16449fb3d4a3SAnshuman Khandual * require further debug. 16459fb3d4a3SAnshuman Khandual */ 16469fb3d4a3SAnshuman Khandual pr_err("Boot memory [%lx %lx] offlined\n", start, end); 16479fb3d4a3SAnshuman Khandual 16489fb3d4a3SAnshuman Khandual /* 16499fb3d4a3SAnshuman Khandual * Core memory hotplug does not process a return 16509fb3d4a3SAnshuman Khandual * code from the notifier for MEM_OFFLINE events. 16519fb3d4a3SAnshuman Khandual * The error condition has been reported. Return 16529fb3d4a3SAnshuman Khandual * from here as if ignored. 16539fb3d4a3SAnshuman Khandual */ 16549fb3d4a3SAnshuman Khandual return NOTIFY_DONE; 16559fb3d4a3SAnshuman Khandual } 1656bbd6ec60SAnshuman Khandual } 1657bbd6ec60SAnshuman Khandual return NOTIFY_OK; 1658bbd6ec60SAnshuman Khandual } 1659bbd6ec60SAnshuman Khandual 1660bbd6ec60SAnshuman Khandual static struct notifier_block prevent_bootmem_remove_nb = { 1661bbd6ec60SAnshuman Khandual .notifier_call = prevent_bootmem_remove_notifier, 1662bbd6ec60SAnshuman Khandual }; 1663bbd6ec60SAnshuman Khandual 1664fdd99a41SAnshuman Khandual /* 1665fdd99a41SAnshuman Khandual * This ensures that boot memory sections on the platform are online 1666fdd99a41SAnshuman Khandual * from early boot. Memory sections could not be prevented from being 1667fdd99a41SAnshuman Khandual * offlined, unless for some reason they are not online to begin with. 1668fdd99a41SAnshuman Khandual * This helps validate the basic assumption on which the above memory 1669fdd99a41SAnshuman Khandual * event notifier works to prevent boot memory section offlining and 1670fdd99a41SAnshuman Khandual * its possible removal. 1671fdd99a41SAnshuman Khandual */ 1672fdd99a41SAnshuman Khandual static void validate_bootmem_online(void) 1673fdd99a41SAnshuman Khandual { 1674fdd99a41SAnshuman Khandual phys_addr_t start, end, addr; 1675fdd99a41SAnshuman Khandual struct mem_section *ms; 1676fdd99a41SAnshuman Khandual u64 i; 1677fdd99a41SAnshuman Khandual 1678fdd99a41SAnshuman Khandual /* 1679fdd99a41SAnshuman Khandual * Scanning across all memblock might be expensive 1680fdd99a41SAnshuman Khandual * on some big memory systems. Hence enable this 1681fdd99a41SAnshuman Khandual * validation only with DEBUG_VM. 1682fdd99a41SAnshuman Khandual */ 1683fdd99a41SAnshuman Khandual if (!IS_ENABLED(CONFIG_DEBUG_VM)) 1684fdd99a41SAnshuman Khandual return; 1685fdd99a41SAnshuman Khandual 1686fdd99a41SAnshuman Khandual for_each_mem_range(i, &start, &end) { 1687fdd99a41SAnshuman Khandual for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) { 1688fdd99a41SAnshuman Khandual ms = __pfn_to_section(PHYS_PFN(addr)); 1689fdd99a41SAnshuman Khandual 1690fdd99a41SAnshuman Khandual /* 1691fdd99a41SAnshuman Khandual * All memory ranges in the system at this point 1692fdd99a41SAnshuman Khandual * should have been marked as early sections. 1693fdd99a41SAnshuman Khandual */ 1694fdd99a41SAnshuman Khandual WARN_ON(!early_section(ms)); 1695fdd99a41SAnshuman Khandual 1696fdd99a41SAnshuman Khandual /* 1697fdd99a41SAnshuman Khandual * Memory notifier mechanism here to prevent boot 1698fdd99a41SAnshuman Khandual * memory offlining depends on the fact that each 1699fdd99a41SAnshuman Khandual * early section memory on the system is initially 1700fdd99a41SAnshuman Khandual * online. Otherwise a given memory section which 1701fdd99a41SAnshuman Khandual * is already offline will be overlooked and can 1702fdd99a41SAnshuman Khandual * be removed completely. Call out such sections. 1703fdd99a41SAnshuman Khandual */ 1704fdd99a41SAnshuman Khandual if (!online_section(ms)) 1705fdd99a41SAnshuman Khandual pr_err("Boot memory [%llx %llx] is offline, can be removed\n", 1706fdd99a41SAnshuman Khandual addr, addr + (1UL << PA_SECTION_SHIFT)); 1707fdd99a41SAnshuman Khandual } 1708fdd99a41SAnshuman Khandual } 1709fdd99a41SAnshuman Khandual } 1710fdd99a41SAnshuman Khandual 1711bbd6ec60SAnshuman Khandual static int __init prevent_bootmem_remove_init(void) 1712bbd6ec60SAnshuman Khandual { 1713cb45babeSAnshuman Khandual int ret = 0; 1714cb45babeSAnshuman Khandual 1715cb45babeSAnshuman Khandual if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE)) 1716cb45babeSAnshuman Khandual return ret; 1717cb45babeSAnshuman Khandual 1718fdd99a41SAnshuman Khandual validate_bootmem_online(); 1719cb45babeSAnshuman Khandual ret = register_memory_notifier(&prevent_bootmem_remove_nb); 1720cb45babeSAnshuman Khandual if (ret) 1721cb45babeSAnshuman Khandual pr_err("%s: Notifier registration failed %d\n", __func__, ret); 1722cb45babeSAnshuman Khandual 1723cb45babeSAnshuman Khandual return ret; 1724bbd6ec60SAnshuman Khandual } 1725cb45babeSAnshuman Khandual early_initcall(prevent_bootmem_remove_init); 172622eb6346SDavid Hildenbrand #endif 1727