• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/mm/mmu.c
4  *
5  * Copyright (C) 1995-2005 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  */
8 
9 #include <linux/cache.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/kexec.h>
16 #include <linux/libfdt.h>
17 #include <linux/mman.h>
18 #include <linux/nodemask.h>
19 #include <linux/memblock.h>
20 #include <linux/memory.h>
21 #include <linux/fs.h>
22 #include <linux/io.h>
23 #include <linux/mm.h>
24 #include <linux/vmalloc.h>
25 #include <linux/set_memory.h>
26 #include <linux/kfence.h>
27 
28 #include <asm/barrier.h>
29 #include <asm/cputype.h>
30 #include <asm/fixmap.h>
31 #include <asm/kasan.h>
32 #include <asm/kernel-pgtable.h>
33 #include <asm/sections.h>
34 #include <asm/setup.h>
35 #include <linux/sizes.h>
36 #include <asm/tlb.h>
37 #include <asm/mmu_context.h>
38 #include <asm/ptdump.h>
39 #include <asm/tlbflush.h>
40 #include <asm/pgalloc.h>
41 #include <asm/kfence.h>
42 
43 #include <trace/hooks/mm.h>
44 
45 #define NO_BLOCK_MAPPINGS	BIT(0)
46 #define NO_CONT_MAPPINGS	BIT(1)
47 #define NO_EXEC_MAPPINGS	BIT(2)	/* assumes FEAT_HPDS is not used */
48 
49 u64 idmap_t0sz = TCR_T0SZ(VA_BITS_MIN);
50 u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
51 
52 u64 __section(".mmuoff.data.write") vabits_actual;
53 EXPORT_SYMBOL(vabits_actual);
54 
55 u64 kimage_voffset __ro_after_init;
56 EXPORT_SYMBOL(kimage_voffset);
57 
58 /*
59  * Empty_zero_page is a special page that is used for zero-initialized data
60  * and COW.
61  */
62 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
63 EXPORT_SYMBOL(empty_zero_page);
64 
65 static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
66 static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
67 static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
68 
69 static DEFINE_SPINLOCK(swapper_pgdir_lock);
70 static DEFINE_MUTEX(fixmap_lock);
71 
set_swapper_pgd(pgd_t * pgdp,pgd_t pgd)72 void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
73 {
74 	pgd_t *fixmap_pgdp;
75 
76 	spin_lock(&swapper_pgdir_lock);
77 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
78 	WRITE_ONCE(*fixmap_pgdp, pgd);
79 	/*
80 	 * We need dsb(ishst) here to ensure the page-table-walker sees
81 	 * our new entry before set_p?d() returns. The fixmap's
82 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
83 	 */
84 	pgd_clear_fixmap();
85 	spin_unlock(&swapper_pgdir_lock);
86 }
87 
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)88 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
89 			      unsigned long size, pgprot_t vma_prot)
90 {
91 	if (!pfn_is_map_memory(pfn))
92 		return pgprot_noncached(vma_prot);
93 	else if (file->f_flags & O_SYNC)
94 		return pgprot_writecombine(vma_prot);
95 	return vma_prot;
96 }
97 EXPORT_SYMBOL(phys_mem_access_prot);
98 
early_pgtable_alloc(int shift)99 static phys_addr_t __init early_pgtable_alloc(int shift)
100 {
101 	phys_addr_t phys;
102 	void *ptr;
103 
104 	phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0,
105 					 MEMBLOCK_ALLOC_NOLEAKTRACE);
106 	if (!phys)
107 		panic("Failed to allocate page table page\n");
108 
109 	/*
110 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
111 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
112 	 * any level of table.
113 	 */
114 	ptr = pte_set_fixmap(phys);
115 
116 	memset(ptr, 0, PAGE_SIZE);
117 
118 	/*
119 	 * Implicit barriers also ensure the zeroed page is visible to the page
120 	 * table walker
121 	 */
122 	pte_clear_fixmap();
123 
124 	return phys;
125 }
126 
pgattr_change_is_safe(u64 old,u64 new)127 static bool pgattr_change_is_safe(u64 old, u64 new)
128 {
129 	/*
130 	 * The following mapping attributes may be updated in live
131 	 * kernel mappings without the need for break-before-make.
132 	 */
133 	pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
134 
135 	/* creating or taking down mappings is always safe */
136 	if (old == 0 || new == 0)
137 		return true;
138 
139 	/* live contiguous mappings may not be manipulated at all */
140 	if ((old | new) & PTE_CONT)
141 		return false;
142 
143 	/* Transitioning from Non-Global to Global is unsafe */
144 	if (old & ~new & PTE_NG)
145 		return false;
146 
147 	/*
148 	 * Changing the memory type between Normal and Normal-Tagged is safe
149 	 * since Tagged is considered a permission attribute from the
150 	 * mismatched attribute aliases perspective.
151 	 */
152 	if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
153 	     (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) &&
154 	    ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
155 	     (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)))
156 		mask |= PTE_ATTRINDX_MASK;
157 
158 	return ((old ^ new) & ~mask) == 0;
159 }
160 
init_pte(pmd_t * pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot)161 static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
162 		     phys_addr_t phys, pgprot_t prot)
163 {
164 	pte_t *ptep;
165 
166 	ptep = pte_set_fixmap_offset(pmdp, addr);
167 	do {
168 		pte_t old_pte = READ_ONCE(*ptep);
169 
170 		set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
171 
172 		/*
173 		 * After the PTE entry has been populated once, we
174 		 * only allow updates to the permission attributes.
175 		 */
176 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
177 					      READ_ONCE(pte_val(*ptep))));
178 
179 		phys += PAGE_SIZE;
180 	} while (ptep++, addr += PAGE_SIZE, addr != end);
181 
182 	pte_clear_fixmap();
183 }
184 
alloc_init_cont_pte(pmd_t * pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)185 static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
186 				unsigned long end, phys_addr_t phys,
187 				pgprot_t prot,
188 				phys_addr_t (*pgtable_alloc)(int),
189 				int flags)
190 {
191 	unsigned long next;
192 	pmd_t pmd = READ_ONCE(*pmdp);
193 
194 	BUG_ON(pmd_sect(pmd));
195 	if (pmd_none(pmd)) {
196 		pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN;
197 		phys_addr_t pte_phys;
198 
199 		if (flags & NO_EXEC_MAPPINGS)
200 			pmdval |= PMD_TABLE_PXN;
201 		BUG_ON(!pgtable_alloc);
202 		pte_phys = pgtable_alloc(PAGE_SHIFT);
203 		__pmd_populate(pmdp, pte_phys, pmdval);
204 		pmd = READ_ONCE(*pmdp);
205 	}
206 	BUG_ON(pmd_bad(pmd));
207 
208 	do {
209 		pgprot_t __prot = prot;
210 
211 		next = pte_cont_addr_end(addr, end);
212 
213 		/* use a contiguous mapping if the range is suitably aligned */
214 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
215 		    (flags & NO_CONT_MAPPINGS) == 0)
216 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
217 
218 		init_pte(pmdp, addr, next, phys, __prot);
219 
220 		phys += next - addr;
221 	} while (addr = next, addr != end);
222 }
223 
init_pmd(pud_t * pudp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)224 static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
225 		     phys_addr_t phys, pgprot_t prot,
226 		     phys_addr_t (*pgtable_alloc)(int), int flags)
227 {
228 	unsigned long next;
229 	pmd_t *pmdp;
230 
231 	pmdp = pmd_set_fixmap_offset(pudp, addr);
232 	do {
233 		pmd_t old_pmd = READ_ONCE(*pmdp);
234 
235 		next = pmd_addr_end(addr, end);
236 
237 		/* try section mapping first */
238 		if (((addr | next | phys) & ~PMD_MASK) == 0 &&
239 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
240 			pmd_set_huge(pmdp, phys, prot);
241 
242 			/*
243 			 * After the PMD entry has been populated once, we
244 			 * only allow updates to the permission attributes.
245 			 */
246 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
247 						      READ_ONCE(pmd_val(*pmdp))));
248 		} else {
249 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
250 					    pgtable_alloc, flags);
251 
252 			BUG_ON(pmd_val(old_pmd) != 0 &&
253 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
254 		}
255 		phys += next - addr;
256 	} while (pmdp++, addr = next, addr != end);
257 
258 	pmd_clear_fixmap();
259 }
260 
alloc_init_cont_pmd(pud_t * pudp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)261 static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
262 				unsigned long end, phys_addr_t phys,
263 				pgprot_t prot,
264 				phys_addr_t (*pgtable_alloc)(int), int flags)
265 {
266 	unsigned long next;
267 	pud_t pud = READ_ONCE(*pudp);
268 
269 	/*
270 	 * Check for initial section mappings in the pgd/pud.
271 	 */
272 	BUG_ON(pud_sect(pud));
273 	if (pud_none(pud)) {
274 		pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN;
275 		phys_addr_t pmd_phys;
276 
277 		if (flags & NO_EXEC_MAPPINGS)
278 			pudval |= PUD_TABLE_PXN;
279 		BUG_ON(!pgtable_alloc);
280 		pmd_phys = pgtable_alloc(PMD_SHIFT);
281 		__pud_populate(pudp, pmd_phys, pudval);
282 		pud = READ_ONCE(*pudp);
283 	}
284 	BUG_ON(pud_bad(pud));
285 
286 	do {
287 		pgprot_t __prot = prot;
288 
289 		next = pmd_cont_addr_end(addr, end);
290 
291 		/* use a contiguous mapping if the range is suitably aligned */
292 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
293 		    (flags & NO_CONT_MAPPINGS) == 0)
294 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
295 
296 		init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
297 
298 		phys += next - addr;
299 	} while (addr = next, addr != end);
300 }
301 
use_1G_block(unsigned long addr,unsigned long next,unsigned long phys)302 static inline bool use_1G_block(unsigned long addr, unsigned long next,
303 			unsigned long phys)
304 {
305 	if (PAGE_SHIFT != 12)
306 		return false;
307 
308 	if (((addr | next | phys) & ~PUD_MASK) != 0)
309 		return false;
310 
311 	return true;
312 }
313 
alloc_init_pud(pgd_t * pgdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)314 static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
315 			   phys_addr_t phys, pgprot_t prot,
316 			   phys_addr_t (*pgtable_alloc)(int),
317 			   int flags)
318 {
319 	unsigned long next;
320 	pud_t *pudp;
321 	p4d_t *p4dp = p4d_offset(pgdp, addr);
322 	p4d_t p4d = READ_ONCE(*p4dp);
323 
324 	if (p4d_none(p4d)) {
325 		p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN;
326 		phys_addr_t pud_phys;
327 
328 		if (flags & NO_EXEC_MAPPINGS)
329 			p4dval |= P4D_TABLE_PXN;
330 		BUG_ON(!pgtable_alloc);
331 		pud_phys = pgtable_alloc(PUD_SHIFT);
332 		__p4d_populate(p4dp, pud_phys, p4dval);
333 		p4d = READ_ONCE(*p4dp);
334 	}
335 	BUG_ON(p4d_bad(p4d));
336 
337 	/*
338 	 * No need for locking during early boot. And it doesn't work as
339 	 * expected with KASLR enabled.
340 	 */
341 	if (system_state != SYSTEM_BOOTING)
342 		mutex_lock(&fixmap_lock);
343 	pudp = pud_set_fixmap_offset(p4dp, addr);
344 	do {
345 		pud_t old_pud = READ_ONCE(*pudp);
346 
347 		next = pud_addr_end(addr, end);
348 
349 		/*
350 		 * For 4K granule only, attempt to put down a 1GB block
351 		 */
352 		if (use_1G_block(addr, next, phys) &&
353 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
354 			pud_set_huge(pudp, phys, prot);
355 
356 			/*
357 			 * After the PUD entry has been populated once, we
358 			 * only allow updates to the permission attributes.
359 			 */
360 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
361 						      READ_ONCE(pud_val(*pudp))));
362 		} else {
363 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
364 					    pgtable_alloc, flags);
365 
366 			BUG_ON(pud_val(old_pud) != 0 &&
367 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
368 		}
369 		phys += next - addr;
370 	} while (pudp++, addr = next, addr != end);
371 
372 	pud_clear_fixmap();
373 	if (system_state != SYSTEM_BOOTING)
374 		mutex_unlock(&fixmap_lock);
375 }
376 
__create_pgd_mapping(pgd_t * pgdir,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (* pgtable_alloc)(int),int flags)377 static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
378 				 unsigned long virt, phys_addr_t size,
379 				 pgprot_t prot,
380 				 phys_addr_t (*pgtable_alloc)(int),
381 				 int flags)
382 {
383 	unsigned long addr, end, next;
384 	pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
385 
386 	/*
387 	 * If the virtual and physical address don't have the same offset
388 	 * within a page, we cannot map the region as the caller expects.
389 	 */
390 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
391 		return;
392 
393 	phys &= PAGE_MASK;
394 	addr = virt & PAGE_MASK;
395 	end = PAGE_ALIGN(virt + size);
396 
397 	do {
398 		next = pgd_addr_end(addr, end);
399 		alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
400 			       flags);
401 		phys += next - addr;
402 	} while (pgdp++, addr = next, addr != end);
403 }
404 
__pgd_pgtable_alloc(int shift)405 static phys_addr_t __pgd_pgtable_alloc(int shift)
406 {
407 	void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL);
408 	BUG_ON(!ptr);
409 
410 	/* Ensure the zeroed page is visible to the page table walker */
411 	dsb(ishst);
412 	return __pa(ptr);
413 }
414 
pgd_pgtable_alloc(int shift)415 static phys_addr_t pgd_pgtable_alloc(int shift)
416 {
417 	phys_addr_t pa = __pgd_pgtable_alloc(shift);
418 
419 	/*
420 	 * Call proper page table ctor in case later we need to
421 	 * call core mm functions like apply_to_page_range() on
422 	 * this pre-allocated page table.
423 	 *
424 	 * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is
425 	 * folded, and if so pgtable_pmd_page_ctor() becomes nop.
426 	 */
427 	if (shift == PAGE_SHIFT)
428 		BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa)));
429 	else if (shift == PMD_SHIFT)
430 		BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa)));
431 
432 	return pa;
433 }
434 
435 /*
436  * This function can only be used to modify existing table entries,
437  * without allocating new levels of table. Note that this permits the
438  * creation of new section or page entries.
439  */
create_mapping_noalloc(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)440 static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
441 				  phys_addr_t size, pgprot_t prot)
442 {
443 	if (virt < PAGE_OFFSET) {
444 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
445 			&phys, virt);
446 		return;
447 	}
448 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
449 			     NO_CONT_MAPPINGS);
450 }
451 
create_pgd_mapping(struct mm_struct * mm,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,bool page_mappings_only)452 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
453 			       unsigned long virt, phys_addr_t size,
454 			       pgprot_t prot, bool page_mappings_only)
455 {
456 	int flags = 0;
457 
458 	BUG_ON(mm == &init_mm);
459 
460 	if (page_mappings_only)
461 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
462 
463 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
464 			     pgd_pgtable_alloc, flags);
465 }
466 
update_mapping_prot(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)467 static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
468 				phys_addr_t size, pgprot_t prot)
469 {
470 	if (virt < PAGE_OFFSET) {
471 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
472 			&phys, virt);
473 		return;
474 	}
475 
476 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
477 			     NO_CONT_MAPPINGS);
478 
479 	/* flush the TLBs after updating live kernel mappings */
480 	flush_tlb_kernel_range(virt, virt + size);
481 }
482 
__map_memblock(pgd_t * pgdp,phys_addr_t start,phys_addr_t end,pgprot_t prot,int flags)483 static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
484 				  phys_addr_t end, pgprot_t prot, int flags)
485 {
486 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
487 			     prot, early_pgtable_alloc, flags);
488 }
489 
mark_linear_text_alias_ro(void)490 void __init mark_linear_text_alias_ro(void)
491 {
492 	/*
493 	 * Remove the write permissions from the linear alias of .text/.rodata
494 	 */
495 	update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext),
496 			    (unsigned long)__init_begin - (unsigned long)_stext,
497 			    PAGE_KERNEL_RO);
498 }
499 
500 static bool crash_mem_map __initdata;
501 
enable_crash_mem_map(char * arg)502 static int __init enable_crash_mem_map(char *arg)
503 {
504 	/*
505 	 * Proper parameter parsing is done by reserve_crashkernel(). We only
506 	 * need to know if the linear map has to avoid block mappings so that
507 	 * the crashkernel reservations can be unmapped later.
508 	 */
509 	crash_mem_map = true;
510 
511 	return 0;
512 }
513 early_param("crashkernel", enable_crash_mem_map);
514 
515 #ifdef CONFIG_KFENCE
516 
517 bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL;
518 
519 /* early_param() will be parsed before map_mem() below. */
parse_kfence_early_init(char * arg)520 static int __init parse_kfence_early_init(char *arg)
521 {
522 	int val;
523 
524 	if (get_option(&arg, &val))
525 		kfence_early_init = !!val;
526 	return 0;
527 }
528 early_param("kfence.sample_interval", parse_kfence_early_init);
529 
arm64_kfence_alloc_pool(void)530 static phys_addr_t __init arm64_kfence_alloc_pool(void)
531 {
532 	phys_addr_t kfence_pool;
533 
534 	if (!kfence_early_init)
535 		return 0;
536 
537 	kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
538 	if (!kfence_pool) {
539 		pr_err("failed to allocate kfence pool\n");
540 		kfence_early_init = false;
541 		return 0;
542 	}
543 
544 	/* Temporarily mark as NOMAP. */
545 	memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
546 
547 	return kfence_pool;
548 }
549 
arm64_kfence_map_pool(phys_addr_t kfence_pool,pgd_t * pgdp)550 static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp)
551 {
552 	if (!kfence_pool)
553 		return;
554 
555 	/* KFENCE pool needs page-level mapping. */
556 	__map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE,
557 			pgprot_tagged(PAGE_KERNEL),
558 			NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
559 	memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
560 	__kfence_pool = phys_to_virt(kfence_pool);
561 }
562 #else /* CONFIG_KFENCE */
563 
arm64_kfence_alloc_pool(void)564 static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; }
arm64_kfence_map_pool(phys_addr_t kfence_pool,pgd_t * pgdp)565 static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { }
566 
567 #endif /* CONFIG_KFENCE */
568 
map_mem(pgd_t * pgdp)569 static void __init map_mem(pgd_t *pgdp)
570 {
571 	static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
572 	phys_addr_t kernel_start = __pa_symbol(_stext);
573 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
574 	phys_addr_t start, end;
575 	phys_addr_t early_kfence_pool;
576 	int flags = NO_EXEC_MAPPINGS;
577 	u64 i;
578 
579 	/*
580 	 * Setting hierarchical PXNTable attributes on table entries covering
581 	 * the linear region is only possible if it is guaranteed that no table
582 	 * entries at any level are being shared between the linear region and
583 	 * the vmalloc region. Check whether this is true for the PGD level, in
584 	 * which case it is guaranteed to be true for all other levels as well.
585 	 */
586 	BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end));
587 
588 	early_kfence_pool = arm64_kfence_alloc_pool();
589 
590 	if (can_set_direct_map())
591 		flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
592 
593 	/*
594 	 * Take care not to create a writable alias for the
595 	 * read-only text and rodata sections of the kernel image.
596 	 * So temporarily mark them as NOMAP to skip mappings in
597 	 * the following for-loop
598 	 */
599 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
600 
601 #ifdef CONFIG_KEXEC_CORE
602 	if (crash_mem_map) {
603 		if (IS_ENABLED(CONFIG_ZONE_DMA) ||
604 		    IS_ENABLED(CONFIG_ZONE_DMA32))
605 			flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
606 		else if (crashk_res.end)
607 			memblock_mark_nomap(crashk_res.start,
608 			    resource_size(&crashk_res));
609 	}
610 #endif
611 
612 	/* map all the memory banks */
613 	for_each_mem_range(i, &start, &end) {
614 		if (start >= end)
615 			break;
616 		/*
617 		 * The linear map must allow allocation tags reading/writing
618 		 * if MTE is present. Otherwise, it has the same attributes as
619 		 * PAGE_KERNEL.
620 		 */
621 		__map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL),
622 			       flags);
623 	}
624 
625 	/*
626 	 * Map the linear alias of the [_stext, __init_begin) interval
627 	 * as non-executable now, and remove the write permission in
628 	 * mark_linear_text_alias_ro() below (which will be called after
629 	 * alternative patching has completed). This makes the contents
630 	 * of the region accessible to subsystems such as hibernate,
631 	 * but protects it from inadvertent modification or execution.
632 	 * Note that contiguous mappings cannot be remapped in this way,
633 	 * so we should avoid them here.
634 	 */
635 	__map_memblock(pgdp, kernel_start, kernel_end,
636 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
637 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
638 
639 	/*
640 	 * Use page-level mappings here so that we can shrink the region
641 	 * in page granularity and put back unused memory to buddy system
642 	 * through /sys/kernel/kexec_crash_size interface.
643 	 */
644 #ifdef CONFIG_KEXEC_CORE
645 	if (crash_mem_map &&
646 	    !IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32)) {
647 		if (crashk_res.end) {
648 			__map_memblock(pgdp, crashk_res.start,
649 				       crashk_res.end + 1,
650 				       PAGE_KERNEL,
651 				       NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
652 			memblock_clear_nomap(crashk_res.start,
653 					     resource_size(&crashk_res));
654 		}
655 	}
656 #endif
657 
658 	arm64_kfence_map_pool(early_kfence_pool, pgdp);
659 }
660 
mark_rodata_ro(void)661 void mark_rodata_ro(void)
662 {
663 	unsigned long section_size;
664 
665 	/*
666 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
667 	 * to cover NOTES and EXCEPTION_TABLE.
668 	 */
669 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
670 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
671 			    section_size, PAGE_KERNEL_RO);
672 
673 	debug_checkwx();
674 }
675 
map_kernel_segment(pgd_t * pgdp,void * va_start,void * va_end,pgprot_t prot,struct vm_struct * vma,int flags,unsigned long vm_flags)676 static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
677 				      pgprot_t prot, struct vm_struct *vma,
678 				      int flags, unsigned long vm_flags)
679 {
680 	phys_addr_t pa_start = __pa_symbol(va_start);
681 	unsigned long size = va_end - va_start;
682 
683 	BUG_ON(!PAGE_ALIGNED(pa_start));
684 	BUG_ON(!PAGE_ALIGNED(size));
685 
686 	__create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
687 			     early_pgtable_alloc, flags);
688 
689 	if (!(vm_flags & VM_NO_GUARD))
690 		size += PAGE_SIZE;
691 
692 	vma->addr	= va_start;
693 	vma->phys_addr	= pa_start;
694 	vma->size	= size;
695 	vma->flags	= VM_MAP | vm_flags;
696 	vma->caller	= __builtin_return_address(0);
697 
698 	vm_area_add_early(vma);
699 }
700 
parse_rodata(char * arg)701 static int __init parse_rodata(char *arg)
702 {
703 	int ret = strtobool(arg, &rodata_enabled);
704 	if (!ret) {
705 		rodata_full = false;
706 		return 0;
707 	}
708 
709 	/* permit 'full' in addition to boolean options */
710 	if (strcmp(arg, "full"))
711 		return -EINVAL;
712 
713 	rodata_enabled = true;
714 	rodata_full = true;
715 	return 0;
716 }
717 early_param("rodata", parse_rodata);
718 
719 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
map_entry_trampoline(void)720 static int __init map_entry_trampoline(void)
721 {
722 	int i;
723 
724 	pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
725 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
726 
727 	/* The trampoline is always mapped and can therefore be global */
728 	pgprot_val(prot) &= ~PTE_NG;
729 
730 	/* Map only the text into the trampoline page table */
731 	memset(tramp_pg_dir, 0, PGD_SIZE);
732 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
733 			     entry_tramp_text_size(), prot,
734 			     __pgd_pgtable_alloc, NO_BLOCK_MAPPINGS);
735 
736 	/* Map both the text and data into the kernel page table */
737 	for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
738 		__set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
739 			     pa_start + i * PAGE_SIZE, prot);
740 
741 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
742 		extern char __entry_tramp_data_start[];
743 
744 		__set_fixmap(FIX_ENTRY_TRAMP_DATA,
745 			     __pa_symbol(__entry_tramp_data_start),
746 			     PAGE_KERNEL_RO);
747 	}
748 
749 	return 0;
750 }
751 core_initcall(map_entry_trampoline);
752 #endif
753 
754 /*
755  * Open coded check for BTI, only for use to determine configuration
756  * for early mappings for before the cpufeature code has run.
757  */
arm64_early_this_cpu_has_bti(void)758 static bool arm64_early_this_cpu_has_bti(void)
759 {
760 	u64 pfr1;
761 
762 	if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
763 		return false;
764 
765 	pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
766 	return cpuid_feature_extract_unsigned_field(pfr1,
767 						    ID_AA64PFR1_EL1_BT_SHIFT);
768 }
769 
770 /*
771  * Create fine-grained mappings for the kernel.
772  */
map_kernel(pgd_t * pgdp)773 static void __init map_kernel(pgd_t *pgdp)
774 {
775 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
776 				vmlinux_initdata, vmlinux_data;
777 
778 	/*
779 	 * External debuggers may need to write directly to the text
780 	 * mapping to install SW breakpoints. Allow this (only) when
781 	 * explicitly requested with rodata=off.
782 	 */
783 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
784 
785 	/*
786 	 * If we have a CPU that supports BTI and a kernel built for
787 	 * BTI then mark the kernel executable text as guarded pages
788 	 * now so we don't have to rewrite the page tables later.
789 	 */
790 	if (arm64_early_this_cpu_has_bti())
791 		text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
792 
793 	/*
794 	 * Only rodata will be remapped with different permissions later on,
795 	 * all other segments are allowed to use contiguous mappings.
796 	 */
797 	map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0,
798 			   VM_NO_GUARD);
799 	map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
800 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
801 	map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
802 			   &vmlinux_inittext, 0, VM_NO_GUARD);
803 	map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
804 			   &vmlinux_initdata, 0, VM_NO_GUARD);
805 	map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
806 
807 	if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) {
808 		/*
809 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
810 		 * live in the carveout for the swapper_pg_dir. We can simply
811 		 * re-use the existing dir for the fixmap.
812 		 */
813 		set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START),
814 			READ_ONCE(*pgd_offset_k(FIXADDR_START)));
815 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
816 		pgd_t *bm_pgdp;
817 		p4d_t *bm_p4dp;
818 		pud_t *bm_pudp;
819 		/*
820 		 * The fixmap shares its top level pgd entry with the kernel
821 		 * mapping. This can really only occur when we are running
822 		 * with 16k/4 levels, so we can simply reuse the pud level
823 		 * entry instead.
824 		 */
825 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
826 		bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START);
827 		bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START);
828 		bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START);
829 		pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd));
830 		pud_clear_fixmap();
831 	} else {
832 		BUG();
833 	}
834 
835 	kasan_copy_shadow(pgdp);
836 }
837 
paging_init(void)838 void __init paging_init(void)
839 {
840 	pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
841 
842 	map_kernel(pgdp);
843 	map_mem(pgdp);
844 
845 	pgd_clear_fixmap();
846 
847 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
848 	init_mm.pgd = swapper_pg_dir;
849 
850 	memblock_free(__pa_symbol(init_pg_dir),
851 		      __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
852 
853 	memblock_allow_resize();
854 }
855 
856 /*
857  * Check whether a kernel address is valid (derived from arch/x86/).
858  */
kern_addr_valid(unsigned long addr)859 int kern_addr_valid(unsigned long addr)
860 {
861 	pgd_t *pgdp;
862 	p4d_t *p4dp;
863 	pud_t *pudp, pud;
864 	pmd_t *pmdp, pmd;
865 	pte_t *ptep, pte;
866 
867 	addr = arch_kasan_reset_tag(addr);
868 	if ((((long)addr) >> VA_BITS) != -1UL)
869 		return 0;
870 
871 	pgdp = pgd_offset_k(addr);
872 	if (pgd_none(READ_ONCE(*pgdp)))
873 		return 0;
874 
875 	p4dp = p4d_offset(pgdp, addr);
876 	if (p4d_none(READ_ONCE(*p4dp)))
877 		return 0;
878 
879 	pudp = pud_offset(p4dp, addr);
880 	pud = READ_ONCE(*pudp);
881 	if (pud_none(pud))
882 		return 0;
883 
884 	if (pud_sect(pud))
885 		return pfn_valid(pud_pfn(pud));
886 
887 	pmdp = pmd_offset(pudp, addr);
888 	pmd = READ_ONCE(*pmdp);
889 	if (pmd_none(pmd))
890 		return 0;
891 
892 	if (pmd_sect(pmd))
893 		return pfn_valid(pmd_pfn(pmd));
894 
895 	ptep = pte_offset_kernel(pmdp, addr);
896 	pte = READ_ONCE(*ptep);
897 	if (pte_none(pte))
898 		return 0;
899 
900 	return pfn_valid(pte_pfn(pte));
901 }
902 
903 #ifdef CONFIG_MEMORY_HOTPLUG
free_hotplug_page_range(struct page * page,size_t size,struct vmem_altmap * altmap)904 static void free_hotplug_page_range(struct page *page, size_t size,
905 				    struct vmem_altmap *altmap)
906 {
907 	if (altmap) {
908 		vmem_altmap_free(altmap, size >> PAGE_SHIFT);
909 	} else {
910 		WARN_ON(PageReserved(page));
911 		free_pages((unsigned long)page_address(page), get_order(size));
912 	}
913 }
914 
free_hotplug_pgtable_page(struct page * page)915 static void free_hotplug_pgtable_page(struct page *page)
916 {
917 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
918 }
919 
pgtable_range_aligned(unsigned long start,unsigned long end,unsigned long floor,unsigned long ceiling,unsigned long mask)920 static bool pgtable_range_aligned(unsigned long start, unsigned long end,
921 				  unsigned long floor, unsigned long ceiling,
922 				  unsigned long mask)
923 {
924 	start &= mask;
925 	if (start < floor)
926 		return false;
927 
928 	if (ceiling) {
929 		ceiling &= mask;
930 		if (!ceiling)
931 			return false;
932 	}
933 
934 	if (end - 1 > ceiling - 1)
935 		return false;
936 	return true;
937 }
938 
unmap_hotplug_pte_range(pmd_t * pmdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)939 static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
940 				    unsigned long end, bool free_mapped,
941 				    struct vmem_altmap *altmap)
942 {
943 	pte_t *ptep, pte;
944 
945 	do {
946 		ptep = pte_offset_kernel(pmdp, addr);
947 		pte = READ_ONCE(*ptep);
948 		if (pte_none(pte))
949 			continue;
950 
951 		WARN_ON(!pte_present(pte));
952 		pte_clear(&init_mm, addr, ptep);
953 		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
954 		if (free_mapped)
955 			free_hotplug_page_range(pte_page(pte),
956 						PAGE_SIZE, altmap);
957 	} while (addr += PAGE_SIZE, addr < end);
958 }
959 
unmap_hotplug_pmd_range(pud_t * pudp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)960 static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
961 				    unsigned long end, bool free_mapped,
962 				    struct vmem_altmap *altmap)
963 {
964 	unsigned long next;
965 	pmd_t *pmdp, pmd;
966 
967 	do {
968 		next = pmd_addr_end(addr, end);
969 		pmdp = pmd_offset(pudp, addr);
970 		pmd = READ_ONCE(*pmdp);
971 		if (pmd_none(pmd))
972 			continue;
973 
974 		WARN_ON(!pmd_present(pmd));
975 		if (pmd_sect(pmd)) {
976 			pmd_clear(pmdp);
977 
978 			/*
979 			 * One TLBI should be sufficient here as the PMD_SIZE
980 			 * range is mapped with a single block entry.
981 			 */
982 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
983 			if (free_mapped)
984 				free_hotplug_page_range(pmd_page(pmd),
985 							PMD_SIZE, altmap);
986 			continue;
987 		}
988 		WARN_ON(!pmd_table(pmd));
989 		unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
990 	} while (addr = next, addr < end);
991 }
992 
unmap_hotplug_pud_range(p4d_t * p4dp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)993 static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
994 				    unsigned long end, bool free_mapped,
995 				    struct vmem_altmap *altmap)
996 {
997 	unsigned long next;
998 	pud_t *pudp, pud;
999 
1000 	do {
1001 		next = pud_addr_end(addr, end);
1002 		pudp = pud_offset(p4dp, addr);
1003 		pud = READ_ONCE(*pudp);
1004 		if (pud_none(pud))
1005 			continue;
1006 
1007 		WARN_ON(!pud_present(pud));
1008 		if (pud_sect(pud)) {
1009 			pud_clear(pudp);
1010 
1011 			/*
1012 			 * One TLBI should be sufficient here as the PUD_SIZE
1013 			 * range is mapped with a single block entry.
1014 			 */
1015 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1016 			if (free_mapped)
1017 				free_hotplug_page_range(pud_page(pud),
1018 							PUD_SIZE, altmap);
1019 			continue;
1020 		}
1021 		WARN_ON(!pud_table(pud));
1022 		unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
1023 	} while (addr = next, addr < end);
1024 }
1025 
unmap_hotplug_p4d_range(pgd_t * pgdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1026 static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
1027 				    unsigned long end, bool free_mapped,
1028 				    struct vmem_altmap *altmap)
1029 {
1030 	unsigned long next;
1031 	p4d_t *p4dp, p4d;
1032 
1033 	do {
1034 		next = p4d_addr_end(addr, end);
1035 		p4dp = p4d_offset(pgdp, addr);
1036 		p4d = READ_ONCE(*p4dp);
1037 		if (p4d_none(p4d))
1038 			continue;
1039 
1040 		WARN_ON(!p4d_present(p4d));
1041 		unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
1042 	} while (addr = next, addr < end);
1043 }
1044 
unmap_hotplug_range(unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1045 static void unmap_hotplug_range(unsigned long addr, unsigned long end,
1046 				bool free_mapped, struct vmem_altmap *altmap)
1047 {
1048 	unsigned long next;
1049 	pgd_t *pgdp, pgd;
1050 
1051 	/*
1052 	 * altmap can only be used as vmemmap mapping backing memory.
1053 	 * In case the backing memory itself is not being freed, then
1054 	 * altmap is irrelevant. Warn about this inconsistency when
1055 	 * encountered.
1056 	 */
1057 	WARN_ON(!free_mapped && altmap);
1058 
1059 	do {
1060 		next = pgd_addr_end(addr, end);
1061 		pgdp = pgd_offset_k(addr);
1062 		pgd = READ_ONCE(*pgdp);
1063 		if (pgd_none(pgd))
1064 			continue;
1065 
1066 		WARN_ON(!pgd_present(pgd));
1067 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
1068 	} while (addr = next, addr < end);
1069 }
1070 
free_empty_pte_table(pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1071 static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
1072 				 unsigned long end, unsigned long floor,
1073 				 unsigned long ceiling)
1074 {
1075 	pte_t *ptep, pte;
1076 	unsigned long i, start = addr;
1077 
1078 	do {
1079 		ptep = pte_offset_kernel(pmdp, addr);
1080 		pte = READ_ONCE(*ptep);
1081 
1082 		/*
1083 		 * This is just a sanity check here which verifies that
1084 		 * pte clearing has been done by earlier unmap loops.
1085 		 */
1086 		WARN_ON(!pte_none(pte));
1087 	} while (addr += PAGE_SIZE, addr < end);
1088 
1089 	if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
1090 		return;
1091 
1092 	/*
1093 	 * Check whether we can free the pte page if the rest of the
1094 	 * entries are empty. Overlap with other regions have been
1095 	 * handled by the floor/ceiling check.
1096 	 */
1097 	ptep = pte_offset_kernel(pmdp, 0UL);
1098 	for (i = 0; i < PTRS_PER_PTE; i++) {
1099 		if (!pte_none(READ_ONCE(ptep[i])))
1100 			return;
1101 	}
1102 
1103 	pmd_clear(pmdp);
1104 	__flush_tlb_kernel_pgtable(start);
1105 	free_hotplug_pgtable_page(virt_to_page(ptep));
1106 }
1107 
free_empty_pmd_table(pud_t * pudp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1108 static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
1109 				 unsigned long end, unsigned long floor,
1110 				 unsigned long ceiling)
1111 {
1112 	pmd_t *pmdp, pmd;
1113 	unsigned long i, next, start = addr;
1114 
1115 	do {
1116 		next = pmd_addr_end(addr, end);
1117 		pmdp = pmd_offset(pudp, addr);
1118 		pmd = READ_ONCE(*pmdp);
1119 		if (pmd_none(pmd))
1120 			continue;
1121 
1122 		WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
1123 		free_empty_pte_table(pmdp, addr, next, floor, ceiling);
1124 	} while (addr = next, addr < end);
1125 
1126 	if (CONFIG_PGTABLE_LEVELS <= 2)
1127 		return;
1128 
1129 	if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
1130 		return;
1131 
1132 	/*
1133 	 * Check whether we can free the pmd page if the rest of the
1134 	 * entries are empty. Overlap with other regions have been
1135 	 * handled by the floor/ceiling check.
1136 	 */
1137 	pmdp = pmd_offset(pudp, 0UL);
1138 	for (i = 0; i < PTRS_PER_PMD; i++) {
1139 		if (!pmd_none(READ_ONCE(pmdp[i])))
1140 			return;
1141 	}
1142 
1143 	pud_clear(pudp);
1144 	__flush_tlb_kernel_pgtable(start);
1145 	free_hotplug_pgtable_page(virt_to_page(pmdp));
1146 }
1147 
free_empty_pud_table(p4d_t * p4dp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1148 static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
1149 				 unsigned long end, unsigned long floor,
1150 				 unsigned long ceiling)
1151 {
1152 	pud_t *pudp, pud;
1153 	unsigned long i, next, start = addr;
1154 
1155 	do {
1156 		next = pud_addr_end(addr, end);
1157 		pudp = pud_offset(p4dp, addr);
1158 		pud = READ_ONCE(*pudp);
1159 		if (pud_none(pud))
1160 			continue;
1161 
1162 		WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
1163 		free_empty_pmd_table(pudp, addr, next, floor, ceiling);
1164 	} while (addr = next, addr < end);
1165 
1166 	if (CONFIG_PGTABLE_LEVELS <= 3)
1167 		return;
1168 
1169 	if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1170 		return;
1171 
1172 	/*
1173 	 * Check whether we can free the pud page if the rest of the
1174 	 * entries are empty. Overlap with other regions have been
1175 	 * handled by the floor/ceiling check.
1176 	 */
1177 	pudp = pud_offset(p4dp, 0UL);
1178 	for (i = 0; i < PTRS_PER_PUD; i++) {
1179 		if (!pud_none(READ_ONCE(pudp[i])))
1180 			return;
1181 	}
1182 
1183 	p4d_clear(p4dp);
1184 	__flush_tlb_kernel_pgtable(start);
1185 	free_hotplug_pgtable_page(virt_to_page(pudp));
1186 }
1187 
free_empty_p4d_table(pgd_t * pgdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1188 static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
1189 				 unsigned long end, unsigned long floor,
1190 				 unsigned long ceiling)
1191 {
1192 	unsigned long next;
1193 	p4d_t *p4dp, p4d;
1194 
1195 	do {
1196 		next = p4d_addr_end(addr, end);
1197 		p4dp = p4d_offset(pgdp, addr);
1198 		p4d = READ_ONCE(*p4dp);
1199 		if (p4d_none(p4d))
1200 			continue;
1201 
1202 		WARN_ON(!p4d_present(p4d));
1203 		free_empty_pud_table(p4dp, addr, next, floor, ceiling);
1204 	} while (addr = next, addr < end);
1205 }
1206 
free_empty_tables(unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1207 static void free_empty_tables(unsigned long addr, unsigned long end,
1208 			      unsigned long floor, unsigned long ceiling)
1209 {
1210 	unsigned long next;
1211 	pgd_t *pgdp, pgd;
1212 
1213 	do {
1214 		next = pgd_addr_end(addr, end);
1215 		pgdp = pgd_offset_k(addr);
1216 		pgd = READ_ONCE(*pgdp);
1217 		if (pgd_none(pgd))
1218 			continue;
1219 
1220 		WARN_ON(!pgd_present(pgd));
1221 		free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
1222 	} while (addr = next, addr < end);
1223 }
1224 #endif
1225 
1226 #if !ARM64_KERNEL_USES_PMD_MAPS
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1227 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1228 		struct vmem_altmap *altmap)
1229 {
1230 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1231 	return vmemmap_populate_basepages(start, end, node, altmap);
1232 }
1233 #else	/* !ARM64_KERNEL_USES_PMD_MAPS */
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1234 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1235 		struct vmem_altmap *altmap)
1236 {
1237 	unsigned long addr = start;
1238 	unsigned long next;
1239 	pgd_t *pgdp;
1240 	p4d_t *p4dp;
1241 	pud_t *pudp;
1242 	pmd_t *pmdp;
1243 
1244 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1245 	do {
1246 		next = pmd_addr_end(addr, end);
1247 
1248 		pgdp = vmemmap_pgd_populate(addr, node);
1249 		if (!pgdp)
1250 			return -ENOMEM;
1251 
1252 		p4dp = vmemmap_p4d_populate(pgdp, addr, node);
1253 		if (!p4dp)
1254 			return -ENOMEM;
1255 
1256 		pudp = vmemmap_pud_populate(p4dp, addr, node);
1257 		if (!pudp)
1258 			return -ENOMEM;
1259 
1260 		pmdp = pmd_offset(pudp, addr);
1261 		if (pmd_none(READ_ONCE(*pmdp))) {
1262 			void *p = NULL;
1263 
1264 			p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
1265 			if (!p) {
1266 				if (vmemmap_populate_basepages(addr, next, node, altmap))
1267 					return -ENOMEM;
1268 				continue;
1269 			}
1270 
1271 			pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
1272 		} else
1273 			vmemmap_verify((pte_t *)pmdp, node, addr, next);
1274 	} while (addr = next, addr != end);
1275 
1276 	return 0;
1277 }
1278 #endif	/* !ARM64_KERNEL_USES_PMD_MAPS */
1279 
1280 #ifdef CONFIG_MEMORY_HOTPLUG
vmemmap_free(unsigned long start,unsigned long end,struct vmem_altmap * altmap)1281 void vmemmap_free(unsigned long start, unsigned long end,
1282 		struct vmem_altmap *altmap)
1283 {
1284 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1285 
1286 	unmap_hotplug_range(start, end, true, altmap);
1287 	free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
1288 }
1289 #endif /* CONFIG_MEMORY_HOTPLUG */
1290 
fixmap_pud(unsigned long addr)1291 static inline pud_t *fixmap_pud(unsigned long addr)
1292 {
1293 	pgd_t *pgdp = pgd_offset_k(addr);
1294 	p4d_t *p4dp = p4d_offset(pgdp, addr);
1295 	p4d_t p4d = READ_ONCE(*p4dp);
1296 
1297 	BUG_ON(p4d_none(p4d) || p4d_bad(p4d));
1298 
1299 	return pud_offset_kimg(p4dp, addr);
1300 }
1301 
fixmap_pmd(unsigned long addr)1302 static inline pmd_t *fixmap_pmd(unsigned long addr)
1303 {
1304 	pud_t *pudp = fixmap_pud(addr);
1305 	pud_t pud = READ_ONCE(*pudp);
1306 
1307 	BUG_ON(pud_none(pud) || pud_bad(pud));
1308 
1309 	return pmd_offset_kimg(pudp, addr);
1310 }
1311 
fixmap_pte(unsigned long addr)1312 static inline pte_t *fixmap_pte(unsigned long addr)
1313 {
1314 	return &bm_pte[pte_index(addr)];
1315 }
1316 
1317 /*
1318  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
1319  * directly on kernel symbols (bm_p*d). This function is called too early to use
1320  * lm_alias so __p*d_populate functions must be used to populate with the
1321  * physical address from __pa_symbol.
1322  */
early_fixmap_init(void)1323 void __init early_fixmap_init(void)
1324 {
1325 	pgd_t *pgdp;
1326 	p4d_t *p4dp, p4d;
1327 	pud_t *pudp;
1328 	pmd_t *pmdp;
1329 	unsigned long addr = FIXADDR_START;
1330 
1331 	pgdp = pgd_offset_k(addr);
1332 	p4dp = p4d_offset(pgdp, addr);
1333 	p4d = READ_ONCE(*p4dp);
1334 	if (CONFIG_PGTABLE_LEVELS > 3 &&
1335 	    !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) {
1336 		/*
1337 		 * We only end up here if the kernel mapping and the fixmap
1338 		 * share the top level pgd entry, which should only happen on
1339 		 * 16k/4 levels configurations.
1340 		 */
1341 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
1342 		pudp = pud_offset_kimg(p4dp, addr);
1343 	} else {
1344 		if (p4d_none(p4d))
1345 			__p4d_populate(p4dp, __pa_symbol(bm_pud), P4D_TYPE_TABLE);
1346 		pudp = fixmap_pud(addr);
1347 	}
1348 	if (pud_none(READ_ONCE(*pudp)))
1349 		__pud_populate(pudp, __pa_symbol(bm_pmd), PUD_TYPE_TABLE);
1350 	pmdp = fixmap_pmd(addr);
1351 	__pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
1352 
1353 	/*
1354 	 * The boot-ioremap range spans multiple pmds, for which
1355 	 * we are not prepared:
1356 	 */
1357 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1358 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1359 
1360 	if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
1361 	     || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
1362 		WARN_ON(1);
1363 		pr_warn("pmdp %p != %p, %p\n",
1364 			pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
1365 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
1366 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1367 			fix_to_virt(FIX_BTMAP_BEGIN));
1368 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
1369 			fix_to_virt(FIX_BTMAP_END));
1370 
1371 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
1372 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
1373 	}
1374 }
1375 
1376 /*
1377  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
1378  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
1379  */
__set_fixmap(enum fixed_addresses idx,phys_addr_t phys,pgprot_t flags)1380 void __set_fixmap(enum fixed_addresses idx,
1381 			       phys_addr_t phys, pgprot_t flags)
1382 {
1383 	unsigned long addr = __fix_to_virt(idx);
1384 	pte_t *ptep;
1385 
1386 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
1387 
1388 	ptep = fixmap_pte(addr);
1389 
1390 	if (pgprot_val(flags)) {
1391 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
1392 	} else {
1393 		pte_clear(&init_mm, addr, ptep);
1394 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
1395 	}
1396 }
1397 
__get_fixmap_pte(enum fixed_addresses idx)1398 pte_t *__get_fixmap_pte(enum fixed_addresses idx)
1399 {
1400 	unsigned long addr = __fix_to_virt(idx);
1401 	pte_t *ptep;
1402 
1403 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
1404 
1405 	ptep = fixmap_pte(addr);
1406 
1407 	if (!pte_valid(*ptep))
1408 		return NULL;
1409 
1410 	return ptep;
1411 }
1412 
fixmap_remap_fdt(phys_addr_t dt_phys,int * size,pgprot_t prot)1413 void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
1414 {
1415 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
1416 	int offset;
1417 	void *dt_virt;
1418 
1419 	/*
1420 	 * Check whether the physical FDT address is set and meets the minimum
1421 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
1422 	 * at least 8 bytes so that we can always access the magic and size
1423 	 * fields of the FDT header after mapping the first chunk, double check
1424 	 * here if that is indeed the case.
1425 	 */
1426 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
1427 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
1428 		return NULL;
1429 
1430 	/*
1431 	 * Make sure that the FDT region can be mapped without the need to
1432 	 * allocate additional translation table pages, so that it is safe
1433 	 * to call create_mapping_noalloc() this early.
1434 	 *
1435 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
1436 	 * be in the same PMD as the rest of the fixmap.
1437 	 * On 4k pages, we'll use section mappings for the FDT so we only
1438 	 * have to be in the same PUD.
1439 	 */
1440 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
1441 
1442 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
1443 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
1444 
1445 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
1446 	dt_virt = (void *)dt_virt_base + offset;
1447 
1448 	/* map the first chunk so we can read the size from the header */
1449 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
1450 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
1451 
1452 	if (fdt_magic(dt_virt) != FDT_MAGIC)
1453 		return NULL;
1454 
1455 	*size = fdt_totalsize(dt_virt);
1456 	if (*size > MAX_FDT_SIZE)
1457 		return NULL;
1458 
1459 	if (offset + *size > SWAPPER_BLOCK_SIZE)
1460 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
1461 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
1462 
1463 	return dt_virt;
1464 }
1465 
pud_set_huge(pud_t * pudp,phys_addr_t phys,pgprot_t prot)1466 int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
1467 {
1468 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
1469 
1470 	/* Only allow permission changes for now */
1471 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
1472 				   pud_val(new_pud)))
1473 		return 0;
1474 
1475 	VM_BUG_ON(phys & ~PUD_MASK);
1476 	set_pud(pudp, new_pud);
1477 	return 1;
1478 }
1479 
pmd_set_huge(pmd_t * pmdp,phys_addr_t phys,pgprot_t prot)1480 int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
1481 {
1482 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
1483 
1484 	/* Only allow permission changes for now */
1485 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
1486 				   pmd_val(new_pmd)))
1487 		return 0;
1488 
1489 	VM_BUG_ON(phys & ~PMD_MASK);
1490 	set_pmd(pmdp, new_pmd);
1491 	return 1;
1492 }
1493 
pud_clear_huge(pud_t * pudp)1494 int pud_clear_huge(pud_t *pudp)
1495 {
1496 	if (!pud_sect(READ_ONCE(*pudp)))
1497 		return 0;
1498 	pud_clear(pudp);
1499 	return 1;
1500 }
1501 
pmd_clear_huge(pmd_t * pmdp)1502 int pmd_clear_huge(pmd_t *pmdp)
1503 {
1504 	if (!pmd_sect(READ_ONCE(*pmdp)))
1505 		return 0;
1506 	pmd_clear(pmdp);
1507 	return 1;
1508 }
1509 
pmd_free_pte_page(pmd_t * pmdp,unsigned long addr)1510 int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
1511 {
1512 	pte_t *table;
1513 	pmd_t pmd;
1514 
1515 	pmd = READ_ONCE(*pmdp);
1516 
1517 	if (!pmd_table(pmd)) {
1518 		VM_WARN_ON(1);
1519 		return 1;
1520 	}
1521 
1522 	table = pte_offset_kernel(pmdp, addr);
1523 	pmd_clear(pmdp);
1524 	__flush_tlb_kernel_pgtable(addr);
1525 	pte_free_kernel(NULL, table);
1526 	return 1;
1527 }
1528 
pud_free_pmd_page(pud_t * pudp,unsigned long addr)1529 int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1530 {
1531 	pmd_t *table;
1532 	pmd_t *pmdp;
1533 	pud_t pud;
1534 	unsigned long next, end;
1535 
1536 	pud = READ_ONCE(*pudp);
1537 
1538 	if (!pud_table(pud)) {
1539 		VM_WARN_ON(1);
1540 		return 1;
1541 	}
1542 
1543 	table = pmd_offset(pudp, addr);
1544 	pmdp = table;
1545 	next = addr;
1546 	end = addr + PUD_SIZE;
1547 	do {
1548 		pmd_free_pte_page(pmdp, next);
1549 	} while (pmdp++, next += PMD_SIZE, next != end);
1550 
1551 	pud_clear(pudp);
1552 	__flush_tlb_kernel_pgtable(addr);
1553 	pmd_free(NULL, table);
1554 	return 1;
1555 }
1556 
should_flush_tlb_when_young(void)1557 bool should_flush_tlb_when_young(void)
1558 {
1559 	bool skip = false;
1560 
1561 	trace_android_vh_ptep_clear_flush_young(&skip);
1562 	return !skip;
1563 }
1564 
1565 #ifdef CONFIG_MEMORY_HOTPLUG
__remove_pgd_mapping(pgd_t * pgdir,unsigned long start,u64 size)1566 static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
1567 {
1568 	unsigned long end = start + size;
1569 
1570 	WARN_ON(pgdir != init_mm.pgd);
1571 	WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
1572 
1573 	unmap_hotplug_range(start, end, false, NULL);
1574 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
1575 }
1576 
arch_get_mappable_range(void)1577 struct range arch_get_mappable_range(void)
1578 {
1579 	struct range mhp_range;
1580 	u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual));
1581 	u64 end_linear_pa = __pa(PAGE_END - 1);
1582 
1583 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
1584 		/*
1585 		 * Check for a wrap, it is possible because of randomized linear
1586 		 * mapping the start physical address is actually bigger than
1587 		 * the end physical address. In this case set start to zero
1588 		 * because [0, end_linear_pa] range must still be able to cover
1589 		 * all addressable physical addresses.
1590 		 */
1591 		if (start_linear_pa > end_linear_pa)
1592 			start_linear_pa = 0;
1593 	}
1594 
1595 	WARN_ON(start_linear_pa > end_linear_pa);
1596 
1597 	/*
1598 	 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
1599 	 * accommodating both its ends but excluding PAGE_END. Max physical
1600 	 * range which can be mapped inside this linear mapping range, must
1601 	 * also be derived from its end points.
1602 	 */
1603 	mhp_range.start = start_linear_pa;
1604 	mhp_range.end =  end_linear_pa;
1605 
1606 	return mhp_range;
1607 }
1608 
arch_add_memory(int nid,u64 start,u64 size,struct mhp_params * params)1609 int arch_add_memory(int nid, u64 start, u64 size,
1610 		    struct mhp_params *params)
1611 {
1612 	int ret, flags = NO_EXEC_MAPPINGS;
1613 
1614 	VM_BUG_ON(!mhp_range_allowed(start, size, true));
1615 
1616 	if (can_set_direct_map())
1617 		flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
1618 
1619 	__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
1620 			     size, params->pgprot, __pgd_pgtable_alloc,
1621 			     flags);
1622 
1623 	memblock_clear_nomap(start, size);
1624 
1625 	ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
1626 			   params);
1627 	if (ret)
1628 		__remove_pgd_mapping(swapper_pg_dir,
1629 				     __phys_to_virt(start), size);
1630 	else {
1631 		max_pfn = PFN_UP(start + size);
1632 		max_low_pfn = max_pfn;
1633 	}
1634 
1635 	return ret;
1636 }
1637 
arch_remove_memory(u64 start,u64 size,struct vmem_altmap * altmap)1638 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
1639 {
1640 	unsigned long start_pfn = start >> PAGE_SHIFT;
1641 	unsigned long nr_pages = size >> PAGE_SHIFT;
1642 
1643 	__remove_pages(start_pfn, nr_pages, altmap);
1644 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
1645 }
1646 
1647 /*
1648  * This memory hotplug notifier helps prevent boot memory from being
1649  * inadvertently removed as it blocks pfn range offlining process in
1650  * __offline_pages(). Hence this prevents both offlining as well as
1651  * removal process for boot memory which is initially always online.
1652  * In future if and when boot memory could be removed, this notifier
1653  * should be dropped and free_hotplug_page_range() should handle any
1654  * reserved pages allocated during boot.
1655  */
prevent_bootmem_remove_notifier(struct notifier_block * nb,unsigned long action,void * data)1656 static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
1657 					   unsigned long action, void *data)
1658 {
1659 	struct mem_section *ms;
1660 	struct memory_notify *arg = data;
1661 	unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
1662 	unsigned long pfn = arg->start_pfn;
1663 
1664 	if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE))
1665 		return NOTIFY_OK;
1666 
1667 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1668 		unsigned long start = PFN_PHYS(pfn);
1669 		unsigned long end = start + (1UL << PA_SECTION_SHIFT);
1670 
1671 		ms = __pfn_to_section(pfn);
1672 		if (!early_section(ms))
1673 			continue;
1674 
1675 		if (action == MEM_GOING_OFFLINE) {
1676 			/*
1677 			 * Boot memory removal is not supported. Prevent
1678 			 * it via blocking any attempted offline request
1679 			 * for the boot memory and just report it.
1680 			 */
1681 			pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end);
1682 			return NOTIFY_BAD;
1683 		} else if (action == MEM_OFFLINE) {
1684 			/*
1685 			 * This should have never happened. Boot memory
1686 			 * offlining should have been prevented by this
1687 			 * very notifier. Probably some memory removal
1688 			 * procedure might have changed which would then
1689 			 * require further debug.
1690 			 */
1691 			pr_err("Boot memory [%lx %lx] offlined\n", start, end);
1692 
1693 			/*
1694 			 * Core memory hotplug does not process a return
1695 			 * code from the notifier for MEM_OFFLINE events.
1696 			 * The error condition has been reported. Return
1697 			 * from here as if ignored.
1698 			 */
1699 			return NOTIFY_DONE;
1700 		}
1701 	}
1702 	return NOTIFY_OK;
1703 }
1704 
1705 static struct notifier_block prevent_bootmem_remove_nb = {
1706 	.notifier_call = prevent_bootmem_remove_notifier,
1707 };
1708 
1709 /*
1710  * This ensures that boot memory sections on the platform are online
1711  * from early boot. Memory sections could not be prevented from being
1712  * offlined, unless for some reason they are not online to begin with.
1713  * This helps validate the basic assumption on which the above memory
1714  * event notifier works to prevent boot memory section offlining and
1715  * its possible removal.
1716  */
validate_bootmem_online(void)1717 static void validate_bootmem_online(void)
1718 {
1719 	phys_addr_t start, end, addr;
1720 	struct mem_section *ms;
1721 	u64 i;
1722 
1723 	/*
1724 	 * Scanning across all memblock might be expensive
1725 	 * on some big memory systems. Hence enable this
1726 	 * validation only with DEBUG_VM.
1727 	 */
1728 	if (!IS_ENABLED(CONFIG_DEBUG_VM))
1729 		return;
1730 
1731 	for_each_mem_range(i, &start, &end) {
1732 		for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) {
1733 			ms = __pfn_to_section(PHYS_PFN(addr));
1734 
1735 			/*
1736 			 * All memory ranges in the system at this point
1737 			 * should have been marked as early sections.
1738 			 */
1739 			WARN_ON(!early_section(ms));
1740 
1741 			/*
1742 			 * Memory notifier mechanism here to prevent boot
1743 			 * memory offlining depends on the fact that each
1744 			 * early section memory on the system is initially
1745 			 * online. Otherwise a given memory section which
1746 			 * is already offline will be overlooked and can
1747 			 * be removed completely. Call out such sections.
1748 			 */
1749 			if (!online_section(ms))
1750 				pr_err("Boot memory [%llx %llx] is offline, can be removed\n",
1751 					addr, addr + (1UL << PA_SECTION_SHIFT));
1752 		}
1753 	}
1754 }
1755 
prevent_bootmem_remove_init(void)1756 static int __init prevent_bootmem_remove_init(void)
1757 {
1758 	int ret = 0;
1759 
1760 	if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE))
1761 		return ret;
1762 
1763 	validate_bootmem_online();
1764 	ret = register_memory_notifier(&prevent_bootmem_remove_nb);
1765 	if (ret)
1766 		pr_err("%s: Notifier registration failed %d\n", __func__, ret);
1767 
1768 	return ret;
1769 }
1770 early_initcall(prevent_bootmem_remove_init);
1771 #endif
1772