xref: /openbmc/linux/mm/kasan/init.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1e886bf9dSAndrey Konovalov // SPDX-License-Identifier: GPL-2.0
2b938fcf4SAndrey Konovalov /*
3b266e8feSAndrey Konovalov  * This file contains KASAN shadow initialization code.
4b938fcf4SAndrey Konovalov  *
5b938fcf4SAndrey Konovalov  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6b938fcf4SAndrey Konovalov  * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7b938fcf4SAndrey Konovalov  */
8b938fcf4SAndrey Konovalov 
9b938fcf4SAndrey Konovalov #include <linux/memblock.h>
10b938fcf4SAndrey Konovalov #include <linux/init.h>
11b938fcf4SAndrey Konovalov #include <linux/kasan.h>
12b938fcf4SAndrey Konovalov #include <linux/kernel.h>
13b938fcf4SAndrey Konovalov #include <linux/mm.h>
14b938fcf4SAndrey Konovalov #include <linux/pfn.h>
15b938fcf4SAndrey Konovalov #include <linux/slab.h>
16b938fcf4SAndrey Konovalov 
17b938fcf4SAndrey Konovalov #include <asm/page.h>
18b938fcf4SAndrey Konovalov #include <asm/pgalloc.h>
19b938fcf4SAndrey Konovalov 
20b938fcf4SAndrey Konovalov #include "kasan.h"
21b938fcf4SAndrey Konovalov 
22b938fcf4SAndrey Konovalov /*
23b938fcf4SAndrey Konovalov  * This page serves two purposes:
24b938fcf4SAndrey Konovalov  *   - It used as early shadow memory. The entire shadow region populated
25b938fcf4SAndrey Konovalov  *     with this page, before we will be able to setup normal shadow memory.
26b938fcf4SAndrey Konovalov  *   - Latter it reused it as zero shadow to cover large ranges of memory
27b938fcf4SAndrey Konovalov  *     that allowed to access, but not handled by kasan (vmalloc/vmemmap ...).
28b938fcf4SAndrey Konovalov  */
299577dd74SAndrey Konovalov unsigned char kasan_early_shadow_page[PAGE_SIZE] __page_aligned_bss;
30b938fcf4SAndrey Konovalov 
31b938fcf4SAndrey Konovalov #if CONFIG_PGTABLE_LEVELS > 4
329577dd74SAndrey Konovalov p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D] __page_aligned_bss;
kasan_p4d_table(pgd_t pgd)33b938fcf4SAndrey Konovalov static inline bool kasan_p4d_table(pgd_t pgd)
34b938fcf4SAndrey Konovalov {
359577dd74SAndrey Konovalov 	return pgd_page(pgd) == virt_to_page(lm_alias(kasan_early_shadow_p4d));
36b938fcf4SAndrey Konovalov }
37b938fcf4SAndrey Konovalov #else
kasan_p4d_table(pgd_t pgd)38b938fcf4SAndrey Konovalov static inline bool kasan_p4d_table(pgd_t pgd)
39b938fcf4SAndrey Konovalov {
405c0198b6SAndrey Konovalov 	return false;
41b938fcf4SAndrey Konovalov }
42b938fcf4SAndrey Konovalov #endif
43b938fcf4SAndrey Konovalov #if CONFIG_PGTABLE_LEVELS > 3
44cb32c9c5SDaniel Axtens pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss;
kasan_pud_table(p4d_t p4d)45b938fcf4SAndrey Konovalov static inline bool kasan_pud_table(p4d_t p4d)
46b938fcf4SAndrey Konovalov {
479577dd74SAndrey Konovalov 	return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud));
48b938fcf4SAndrey Konovalov }
49b938fcf4SAndrey Konovalov #else
kasan_pud_table(p4d_t p4d)50b938fcf4SAndrey Konovalov static inline bool kasan_pud_table(p4d_t p4d)
51b938fcf4SAndrey Konovalov {
525c0198b6SAndrey Konovalov 	return false;
53b938fcf4SAndrey Konovalov }
54b938fcf4SAndrey Konovalov #endif
55b938fcf4SAndrey Konovalov #if CONFIG_PGTABLE_LEVELS > 2
56cb32c9c5SDaniel Axtens pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss;
kasan_pmd_table(pud_t pud)57b938fcf4SAndrey Konovalov static inline bool kasan_pmd_table(pud_t pud)
58b938fcf4SAndrey Konovalov {
599577dd74SAndrey Konovalov 	return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd));
60b938fcf4SAndrey Konovalov }
61b938fcf4SAndrey Konovalov #else
kasan_pmd_table(pud_t pud)62b938fcf4SAndrey Konovalov static inline bool kasan_pmd_table(pud_t pud)
63b938fcf4SAndrey Konovalov {
645c0198b6SAndrey Konovalov 	return false;
65b938fcf4SAndrey Konovalov }
66b938fcf4SAndrey Konovalov #endif
67cb32c9c5SDaniel Axtens pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]
6829970dc2SHailong Liu 	__page_aligned_bss;
69b938fcf4SAndrey Konovalov 
kasan_pte_table(pmd_t pmd)70b938fcf4SAndrey Konovalov static inline bool kasan_pte_table(pmd_t pmd)
71b938fcf4SAndrey Konovalov {
729577dd74SAndrey Konovalov 	return pmd_page(pmd) == virt_to_page(lm_alias(kasan_early_shadow_pte));
73b938fcf4SAndrey Konovalov }
74b938fcf4SAndrey Konovalov 
kasan_early_shadow_page_entry(pte_t pte)759577dd74SAndrey Konovalov static inline bool kasan_early_shadow_page_entry(pte_t pte)
76b938fcf4SAndrey Konovalov {
779577dd74SAndrey Konovalov 	return pte_page(pte) == virt_to_page(lm_alias(kasan_early_shadow_page));
78b938fcf4SAndrey Konovalov }
79b938fcf4SAndrey Konovalov 
early_alloc(size_t size,int node)80b938fcf4SAndrey Konovalov static __init void *early_alloc(size_t size, int node)
81b938fcf4SAndrey Konovalov {
828a7f97b9SMike Rapoport 	void *ptr = memblock_alloc_try_nid(size, size, __pa(MAX_DMA_ADDRESS),
83b938fcf4SAndrey Konovalov 					   MEMBLOCK_ALLOC_ACCESSIBLE, node);
848a7f97b9SMike Rapoport 
858a7f97b9SMike Rapoport 	if (!ptr)
868a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %zu bytes align=%zx nid=%d from=%llx\n",
878a7f97b9SMike Rapoport 		      __func__, size, size, node, (u64)__pa(MAX_DMA_ADDRESS));
888a7f97b9SMike Rapoport 
898a7f97b9SMike Rapoport 	return ptr;
90b938fcf4SAndrey Konovalov }
91b938fcf4SAndrey Konovalov 
zero_pte_populate(pmd_t * pmd,unsigned long addr,unsigned long end)92b938fcf4SAndrey Konovalov static void __ref zero_pte_populate(pmd_t *pmd, unsigned long addr,
93b938fcf4SAndrey Konovalov 				unsigned long end)
94b938fcf4SAndrey Konovalov {
95b938fcf4SAndrey Konovalov 	pte_t *pte = pte_offset_kernel(pmd, addr);
96b938fcf4SAndrey Konovalov 	pte_t zero_pte;
97b938fcf4SAndrey Konovalov 
989577dd74SAndrey Konovalov 	zero_pte = pfn_pte(PFN_DOWN(__pa_symbol(kasan_early_shadow_page)),
999577dd74SAndrey Konovalov 				PAGE_KERNEL);
100b938fcf4SAndrey Konovalov 	zero_pte = pte_wrprotect(zero_pte);
101b938fcf4SAndrey Konovalov 
102b938fcf4SAndrey Konovalov 	while (addr + PAGE_SIZE <= end) {
103b938fcf4SAndrey Konovalov 		set_pte_at(&init_mm, addr, pte, zero_pte);
104b938fcf4SAndrey Konovalov 		addr += PAGE_SIZE;
105b938fcf4SAndrey Konovalov 		pte = pte_offset_kernel(pmd, addr);
106b938fcf4SAndrey Konovalov 	}
107b938fcf4SAndrey Konovalov }
108b938fcf4SAndrey Konovalov 
zero_pmd_populate(pud_t * pud,unsigned long addr,unsigned long end)109b938fcf4SAndrey Konovalov static int __ref zero_pmd_populate(pud_t *pud, unsigned long addr,
110b938fcf4SAndrey Konovalov 				unsigned long end)
111b938fcf4SAndrey Konovalov {
112b938fcf4SAndrey Konovalov 	pmd_t *pmd = pmd_offset(pud, addr);
113b938fcf4SAndrey Konovalov 	unsigned long next;
114b938fcf4SAndrey Konovalov 
115b938fcf4SAndrey Konovalov 	do {
116b938fcf4SAndrey Konovalov 		next = pmd_addr_end(addr, end);
117b938fcf4SAndrey Konovalov 
118b938fcf4SAndrey Konovalov 		if (IS_ALIGNED(addr, PMD_SIZE) && end - addr >= PMD_SIZE) {
1199577dd74SAndrey Konovalov 			pmd_populate_kernel(&init_mm, pmd,
1209577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pte));
121b938fcf4SAndrey Konovalov 			continue;
122b938fcf4SAndrey Konovalov 		}
123b938fcf4SAndrey Konovalov 
124b938fcf4SAndrey Konovalov 		if (pmd_none(*pmd)) {
125b938fcf4SAndrey Konovalov 			pte_t *p;
126b938fcf4SAndrey Konovalov 
127b938fcf4SAndrey Konovalov 			if (slab_is_available())
1284cf58924SJoel Fernandes (Google) 				p = pte_alloc_one_kernel(&init_mm);
129b938fcf4SAndrey Konovalov 			else
130b938fcf4SAndrey Konovalov 				p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
131b938fcf4SAndrey Konovalov 			if (!p)
132b938fcf4SAndrey Konovalov 				return -ENOMEM;
133b938fcf4SAndrey Konovalov 
134b938fcf4SAndrey Konovalov 			pmd_populate_kernel(&init_mm, pmd, p);
135b938fcf4SAndrey Konovalov 		}
136b938fcf4SAndrey Konovalov 		zero_pte_populate(pmd, addr, next);
137b938fcf4SAndrey Konovalov 	} while (pmd++, addr = next, addr != end);
138b938fcf4SAndrey Konovalov 
139b938fcf4SAndrey Konovalov 	return 0;
140b938fcf4SAndrey Konovalov }
141b938fcf4SAndrey Konovalov 
pmd_init(void * addr)142*fb6d5c1dSQing Zhang void __weak __meminit pmd_init(void *addr)
143*fb6d5c1dSQing Zhang {
144*fb6d5c1dSQing Zhang }
145*fb6d5c1dSQing Zhang 
zero_pud_populate(p4d_t * p4d,unsigned long addr,unsigned long end)146b938fcf4SAndrey Konovalov static int __ref zero_pud_populate(p4d_t *p4d, unsigned long addr,
147b938fcf4SAndrey Konovalov 				unsigned long end)
148b938fcf4SAndrey Konovalov {
149b938fcf4SAndrey Konovalov 	pud_t *pud = pud_offset(p4d, addr);
150b938fcf4SAndrey Konovalov 	unsigned long next;
151b938fcf4SAndrey Konovalov 
152b938fcf4SAndrey Konovalov 	do {
153b938fcf4SAndrey Konovalov 		next = pud_addr_end(addr, end);
154b938fcf4SAndrey Konovalov 		if (IS_ALIGNED(addr, PUD_SIZE) && end - addr >= PUD_SIZE) {
155b938fcf4SAndrey Konovalov 			pmd_t *pmd;
156b938fcf4SAndrey Konovalov 
1579577dd74SAndrey Konovalov 			pud_populate(&init_mm, pud,
1589577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pmd));
159b938fcf4SAndrey Konovalov 			pmd = pmd_offset(pud, addr);
1609577dd74SAndrey Konovalov 			pmd_populate_kernel(&init_mm, pmd,
1619577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pte));
162b938fcf4SAndrey Konovalov 			continue;
163b938fcf4SAndrey Konovalov 		}
164b938fcf4SAndrey Konovalov 
165b938fcf4SAndrey Konovalov 		if (pud_none(*pud)) {
166b938fcf4SAndrey Konovalov 			pmd_t *p;
167b938fcf4SAndrey Konovalov 
168b938fcf4SAndrey Konovalov 			if (slab_is_available()) {
169b938fcf4SAndrey Konovalov 				p = pmd_alloc(&init_mm, pud, addr);
170b938fcf4SAndrey Konovalov 				if (!p)
171b938fcf4SAndrey Konovalov 					return -ENOMEM;
172b938fcf4SAndrey Konovalov 			} else {
173*fb6d5c1dSQing Zhang 				p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
174*fb6d5c1dSQing Zhang 				pmd_init(p);
175*fb6d5c1dSQing Zhang 				pud_populate(&init_mm, pud, p);
176b938fcf4SAndrey Konovalov 			}
177b938fcf4SAndrey Konovalov 		}
178b938fcf4SAndrey Konovalov 		zero_pmd_populate(pud, addr, next);
179b938fcf4SAndrey Konovalov 	} while (pud++, addr = next, addr != end);
180b938fcf4SAndrey Konovalov 
181b938fcf4SAndrey Konovalov 	return 0;
182b938fcf4SAndrey Konovalov }
183b938fcf4SAndrey Konovalov 
pud_init(void * addr)184*fb6d5c1dSQing Zhang void __weak __meminit pud_init(void *addr)
185*fb6d5c1dSQing Zhang {
186*fb6d5c1dSQing Zhang }
187*fb6d5c1dSQing Zhang 
zero_p4d_populate(pgd_t * pgd,unsigned long addr,unsigned long end)188b938fcf4SAndrey Konovalov static int __ref zero_p4d_populate(pgd_t *pgd, unsigned long addr,
189b938fcf4SAndrey Konovalov 				unsigned long end)
190b938fcf4SAndrey Konovalov {
191b938fcf4SAndrey Konovalov 	p4d_t *p4d = p4d_offset(pgd, addr);
192b938fcf4SAndrey Konovalov 	unsigned long next;
193b938fcf4SAndrey Konovalov 
194b938fcf4SAndrey Konovalov 	do {
195b938fcf4SAndrey Konovalov 		next = p4d_addr_end(addr, end);
196b938fcf4SAndrey Konovalov 		if (IS_ALIGNED(addr, P4D_SIZE) && end - addr >= P4D_SIZE) {
197b938fcf4SAndrey Konovalov 			pud_t *pud;
198b938fcf4SAndrey Konovalov 			pmd_t *pmd;
199b938fcf4SAndrey Konovalov 
2009577dd74SAndrey Konovalov 			p4d_populate(&init_mm, p4d,
2019577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pud));
202b938fcf4SAndrey Konovalov 			pud = pud_offset(p4d, addr);
2039577dd74SAndrey Konovalov 			pud_populate(&init_mm, pud,
2049577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pmd));
205b938fcf4SAndrey Konovalov 			pmd = pmd_offset(pud, addr);
206b938fcf4SAndrey Konovalov 			pmd_populate_kernel(&init_mm, pmd,
2079577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pte));
208b938fcf4SAndrey Konovalov 			continue;
209b938fcf4SAndrey Konovalov 		}
210b938fcf4SAndrey Konovalov 
211b938fcf4SAndrey Konovalov 		if (p4d_none(*p4d)) {
212b938fcf4SAndrey Konovalov 			pud_t *p;
213b938fcf4SAndrey Konovalov 
214b938fcf4SAndrey Konovalov 			if (slab_is_available()) {
215b938fcf4SAndrey Konovalov 				p = pud_alloc(&init_mm, p4d, addr);
216b938fcf4SAndrey Konovalov 				if (!p)
217b938fcf4SAndrey Konovalov 					return -ENOMEM;
218b938fcf4SAndrey Konovalov 			} else {
219*fb6d5c1dSQing Zhang 				p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
220*fb6d5c1dSQing Zhang 				pud_init(p);
221*fb6d5c1dSQing Zhang 				p4d_populate(&init_mm, p4d, p);
222b938fcf4SAndrey Konovalov 			}
223b938fcf4SAndrey Konovalov 		}
224b938fcf4SAndrey Konovalov 		zero_pud_populate(p4d, addr, next);
225b938fcf4SAndrey Konovalov 	} while (p4d++, addr = next, addr != end);
226b938fcf4SAndrey Konovalov 
227b938fcf4SAndrey Konovalov 	return 0;
228b938fcf4SAndrey Konovalov }
229b938fcf4SAndrey Konovalov 
230b938fcf4SAndrey Konovalov /**
2319577dd74SAndrey Konovalov  * kasan_populate_early_shadow - populate shadow memory region with
2329577dd74SAndrey Konovalov  *                               kasan_early_shadow_page
2337b6889f5SYu Kuai  * @shadow_start: start of the memory range to populate
2347b6889f5SYu Kuai  * @shadow_end: end of the memory range to populate
235b938fcf4SAndrey Konovalov  */
kasan_populate_early_shadow(const void * shadow_start,const void * shadow_end)2369577dd74SAndrey Konovalov int __ref kasan_populate_early_shadow(const void *shadow_start,
237b938fcf4SAndrey Konovalov 					const void *shadow_end)
238b938fcf4SAndrey Konovalov {
239b938fcf4SAndrey Konovalov 	unsigned long addr = (unsigned long)shadow_start;
240b938fcf4SAndrey Konovalov 	unsigned long end = (unsigned long)shadow_end;
241b938fcf4SAndrey Konovalov 	pgd_t *pgd = pgd_offset_k(addr);
242b938fcf4SAndrey Konovalov 	unsigned long next;
243b938fcf4SAndrey Konovalov 
244b938fcf4SAndrey Konovalov 	do {
245b938fcf4SAndrey Konovalov 		next = pgd_addr_end(addr, end);
246b938fcf4SAndrey Konovalov 
247b938fcf4SAndrey Konovalov 		if (IS_ALIGNED(addr, PGDIR_SIZE) && end - addr >= PGDIR_SIZE) {
248b938fcf4SAndrey Konovalov 			p4d_t *p4d;
249b938fcf4SAndrey Konovalov 			pud_t *pud;
250b938fcf4SAndrey Konovalov 			pmd_t *pmd;
251b938fcf4SAndrey Konovalov 
252b938fcf4SAndrey Konovalov 			/*
2539577dd74SAndrey Konovalov 			 * kasan_early_shadow_pud should be populated with pmds
254b938fcf4SAndrey Konovalov 			 * at this moment.
255b938fcf4SAndrey Konovalov 			 * [pud,pmd]_populate*() below needed only for
256b938fcf4SAndrey Konovalov 			 * 3,2 - level page tables where we don't have
257b938fcf4SAndrey Konovalov 			 * puds,pmds, so pgd_populate(), pud_populate()
258b938fcf4SAndrey Konovalov 			 * is noops.
259b938fcf4SAndrey Konovalov 			 */
2609577dd74SAndrey Konovalov 			pgd_populate(&init_mm, pgd,
2619577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_p4d));
262b938fcf4SAndrey Konovalov 			p4d = p4d_offset(pgd, addr);
2639577dd74SAndrey Konovalov 			p4d_populate(&init_mm, p4d,
2649577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pud));
265b938fcf4SAndrey Konovalov 			pud = pud_offset(p4d, addr);
2669577dd74SAndrey Konovalov 			pud_populate(&init_mm, pud,
2679577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pmd));
268b938fcf4SAndrey Konovalov 			pmd = pmd_offset(pud, addr);
2699577dd74SAndrey Konovalov 			pmd_populate_kernel(&init_mm, pmd,
2709577dd74SAndrey Konovalov 					lm_alias(kasan_early_shadow_pte));
271b938fcf4SAndrey Konovalov 			continue;
272b938fcf4SAndrey Konovalov 		}
273b938fcf4SAndrey Konovalov 
274b938fcf4SAndrey Konovalov 		if (pgd_none(*pgd)) {
275b938fcf4SAndrey Konovalov 			p4d_t *p;
276b938fcf4SAndrey Konovalov 
277b938fcf4SAndrey Konovalov 			if (slab_is_available()) {
278b938fcf4SAndrey Konovalov 				p = p4d_alloc(&init_mm, pgd, addr);
279b938fcf4SAndrey Konovalov 				if (!p)
280b938fcf4SAndrey Konovalov 					return -ENOMEM;
281b938fcf4SAndrey Konovalov 			} else {
282b938fcf4SAndrey Konovalov 				pgd_populate(&init_mm, pgd,
283b938fcf4SAndrey Konovalov 					early_alloc(PAGE_SIZE, NUMA_NO_NODE));
284b938fcf4SAndrey Konovalov 			}
285b938fcf4SAndrey Konovalov 		}
286b938fcf4SAndrey Konovalov 		zero_p4d_populate(pgd, addr, next);
287b938fcf4SAndrey Konovalov 	} while (pgd++, addr = next, addr != end);
288b938fcf4SAndrey Konovalov 
289b938fcf4SAndrey Konovalov 	return 0;
290b938fcf4SAndrey Konovalov }
291b938fcf4SAndrey Konovalov 
kasan_free_pte(pte_t * pte_start,pmd_t * pmd)292b938fcf4SAndrey Konovalov static void kasan_free_pte(pte_t *pte_start, pmd_t *pmd)
293b938fcf4SAndrey Konovalov {
294b938fcf4SAndrey Konovalov 	pte_t *pte;
295b938fcf4SAndrey Konovalov 	int i;
296b938fcf4SAndrey Konovalov 
297b938fcf4SAndrey Konovalov 	for (i = 0; i < PTRS_PER_PTE; i++) {
298b938fcf4SAndrey Konovalov 		pte = pte_start + i;
299c33c7948SRyan Roberts 		if (!pte_none(ptep_get(pte)))
300b938fcf4SAndrey Konovalov 			return;
301b938fcf4SAndrey Konovalov 	}
302b938fcf4SAndrey Konovalov 
303b938fcf4SAndrey Konovalov 	pte_free_kernel(&init_mm, (pte_t *)page_to_virt(pmd_page(*pmd)));
304b938fcf4SAndrey Konovalov 	pmd_clear(pmd);
305b938fcf4SAndrey Konovalov }
306b938fcf4SAndrey Konovalov 
kasan_free_pmd(pmd_t * pmd_start,pud_t * pud)307b938fcf4SAndrey Konovalov static void kasan_free_pmd(pmd_t *pmd_start, pud_t *pud)
308b938fcf4SAndrey Konovalov {
309b938fcf4SAndrey Konovalov 	pmd_t *pmd;
310b938fcf4SAndrey Konovalov 	int i;
311b938fcf4SAndrey Konovalov 
312b938fcf4SAndrey Konovalov 	for (i = 0; i < PTRS_PER_PMD; i++) {
313b938fcf4SAndrey Konovalov 		pmd = pmd_start + i;
314b938fcf4SAndrey Konovalov 		if (!pmd_none(*pmd))
315b938fcf4SAndrey Konovalov 			return;
316b938fcf4SAndrey Konovalov 	}
317b938fcf4SAndrey Konovalov 
318b938fcf4SAndrey Konovalov 	pmd_free(&init_mm, (pmd_t *)page_to_virt(pud_page(*pud)));
319b938fcf4SAndrey Konovalov 	pud_clear(pud);
320b938fcf4SAndrey Konovalov }
321b938fcf4SAndrey Konovalov 
kasan_free_pud(pud_t * pud_start,p4d_t * p4d)322b938fcf4SAndrey Konovalov static void kasan_free_pud(pud_t *pud_start, p4d_t *p4d)
323b938fcf4SAndrey Konovalov {
324b938fcf4SAndrey Konovalov 	pud_t *pud;
325b938fcf4SAndrey Konovalov 	int i;
326b938fcf4SAndrey Konovalov 
327b938fcf4SAndrey Konovalov 	for (i = 0; i < PTRS_PER_PUD; i++) {
328b938fcf4SAndrey Konovalov 		pud = pud_start + i;
329b938fcf4SAndrey Konovalov 		if (!pud_none(*pud))
330b938fcf4SAndrey Konovalov 			return;
331b938fcf4SAndrey Konovalov 	}
332b938fcf4SAndrey Konovalov 
333b938fcf4SAndrey Konovalov 	pud_free(&init_mm, (pud_t *)page_to_virt(p4d_page(*p4d)));
334b938fcf4SAndrey Konovalov 	p4d_clear(p4d);
335b938fcf4SAndrey Konovalov }
336b938fcf4SAndrey Konovalov 
kasan_free_p4d(p4d_t * p4d_start,pgd_t * pgd)337b938fcf4SAndrey Konovalov static void kasan_free_p4d(p4d_t *p4d_start, pgd_t *pgd)
338b938fcf4SAndrey Konovalov {
339b938fcf4SAndrey Konovalov 	p4d_t *p4d;
340b938fcf4SAndrey Konovalov 	int i;
341b938fcf4SAndrey Konovalov 
342b938fcf4SAndrey Konovalov 	for (i = 0; i < PTRS_PER_P4D; i++) {
343b938fcf4SAndrey Konovalov 		p4d = p4d_start + i;
344b938fcf4SAndrey Konovalov 		if (!p4d_none(*p4d))
345b938fcf4SAndrey Konovalov 			return;
346b938fcf4SAndrey Konovalov 	}
347b938fcf4SAndrey Konovalov 
348b938fcf4SAndrey Konovalov 	p4d_free(&init_mm, (p4d_t *)page_to_virt(pgd_page(*pgd)));
349b938fcf4SAndrey Konovalov 	pgd_clear(pgd);
350b938fcf4SAndrey Konovalov }
351b938fcf4SAndrey Konovalov 
kasan_remove_pte_table(pte_t * pte,unsigned long addr,unsigned long end)352b938fcf4SAndrey Konovalov static void kasan_remove_pte_table(pte_t *pte, unsigned long addr,
353b938fcf4SAndrey Konovalov 				unsigned long end)
354b938fcf4SAndrey Konovalov {
355b938fcf4SAndrey Konovalov 	unsigned long next;
356c33c7948SRyan Roberts 	pte_t ptent;
357b938fcf4SAndrey Konovalov 
358b938fcf4SAndrey Konovalov 	for (; addr < end; addr = next, pte++) {
359b938fcf4SAndrey Konovalov 		next = (addr + PAGE_SIZE) & PAGE_MASK;
360b938fcf4SAndrey Konovalov 		if (next > end)
361b938fcf4SAndrey Konovalov 			next = end;
362b938fcf4SAndrey Konovalov 
363c33c7948SRyan Roberts 		ptent = ptep_get(pte);
364c33c7948SRyan Roberts 
365c33c7948SRyan Roberts 		if (!pte_present(ptent))
366b938fcf4SAndrey Konovalov 			continue;
367b938fcf4SAndrey Konovalov 
368c33c7948SRyan Roberts 		if (WARN_ON(!kasan_early_shadow_page_entry(ptent)))
369b938fcf4SAndrey Konovalov 			continue;
370b938fcf4SAndrey Konovalov 		pte_clear(&init_mm, addr, pte);
371b938fcf4SAndrey Konovalov 	}
372b938fcf4SAndrey Konovalov }
373b938fcf4SAndrey Konovalov 
kasan_remove_pmd_table(pmd_t * pmd,unsigned long addr,unsigned long end)374b938fcf4SAndrey Konovalov static void kasan_remove_pmd_table(pmd_t *pmd, unsigned long addr,
375b938fcf4SAndrey Konovalov 				unsigned long end)
376b938fcf4SAndrey Konovalov {
377b938fcf4SAndrey Konovalov 	unsigned long next;
378b938fcf4SAndrey Konovalov 
379b938fcf4SAndrey Konovalov 	for (; addr < end; addr = next, pmd++) {
380b938fcf4SAndrey Konovalov 		pte_t *pte;
381b938fcf4SAndrey Konovalov 
382b938fcf4SAndrey Konovalov 		next = pmd_addr_end(addr, end);
383b938fcf4SAndrey Konovalov 
384b938fcf4SAndrey Konovalov 		if (!pmd_present(*pmd))
385b938fcf4SAndrey Konovalov 			continue;
386b938fcf4SAndrey Konovalov 
387b938fcf4SAndrey Konovalov 		if (kasan_pte_table(*pmd)) {
388b938fcf4SAndrey Konovalov 			if (IS_ALIGNED(addr, PMD_SIZE) &&
389a11a496eSLecopzer Chen 			    IS_ALIGNED(next, PMD_SIZE)) {
390b938fcf4SAndrey Konovalov 				pmd_clear(pmd);
391b938fcf4SAndrey Konovalov 				continue;
392b938fcf4SAndrey Konovalov 			}
393a11a496eSLecopzer Chen 		}
394b938fcf4SAndrey Konovalov 		pte = pte_offset_kernel(pmd, addr);
395b938fcf4SAndrey Konovalov 		kasan_remove_pte_table(pte, addr, next);
396b938fcf4SAndrey Konovalov 		kasan_free_pte(pte_offset_kernel(pmd, 0), pmd);
397b938fcf4SAndrey Konovalov 	}
398b938fcf4SAndrey Konovalov }
399b938fcf4SAndrey Konovalov 
kasan_remove_pud_table(pud_t * pud,unsigned long addr,unsigned long end)400b938fcf4SAndrey Konovalov static void kasan_remove_pud_table(pud_t *pud, unsigned long addr,
401b938fcf4SAndrey Konovalov 				unsigned long end)
402b938fcf4SAndrey Konovalov {
403b938fcf4SAndrey Konovalov 	unsigned long next;
404b938fcf4SAndrey Konovalov 
405b938fcf4SAndrey Konovalov 	for (; addr < end; addr = next, pud++) {
406b938fcf4SAndrey Konovalov 		pmd_t *pmd, *pmd_base;
407b938fcf4SAndrey Konovalov 
408b938fcf4SAndrey Konovalov 		next = pud_addr_end(addr, end);
409b938fcf4SAndrey Konovalov 
410b938fcf4SAndrey Konovalov 		if (!pud_present(*pud))
411b938fcf4SAndrey Konovalov 			continue;
412b938fcf4SAndrey Konovalov 
413b938fcf4SAndrey Konovalov 		if (kasan_pmd_table(*pud)) {
414b938fcf4SAndrey Konovalov 			if (IS_ALIGNED(addr, PUD_SIZE) &&
415a11a496eSLecopzer Chen 			    IS_ALIGNED(next, PUD_SIZE)) {
416b938fcf4SAndrey Konovalov 				pud_clear(pud);
417b938fcf4SAndrey Konovalov 				continue;
418b938fcf4SAndrey Konovalov 			}
419a11a496eSLecopzer Chen 		}
420b938fcf4SAndrey Konovalov 		pmd = pmd_offset(pud, addr);
421b938fcf4SAndrey Konovalov 		pmd_base = pmd_offset(pud, 0);
422b938fcf4SAndrey Konovalov 		kasan_remove_pmd_table(pmd, addr, next);
423b938fcf4SAndrey Konovalov 		kasan_free_pmd(pmd_base, pud);
424b938fcf4SAndrey Konovalov 	}
425b938fcf4SAndrey Konovalov }
426b938fcf4SAndrey Konovalov 
kasan_remove_p4d_table(p4d_t * p4d,unsigned long addr,unsigned long end)427b938fcf4SAndrey Konovalov static void kasan_remove_p4d_table(p4d_t *p4d, unsigned long addr,
428b938fcf4SAndrey Konovalov 				unsigned long end)
429b938fcf4SAndrey Konovalov {
430b938fcf4SAndrey Konovalov 	unsigned long next;
431b938fcf4SAndrey Konovalov 
432b938fcf4SAndrey Konovalov 	for (; addr < end; addr = next, p4d++) {
433b938fcf4SAndrey Konovalov 		pud_t *pud;
434b938fcf4SAndrey Konovalov 
435b938fcf4SAndrey Konovalov 		next = p4d_addr_end(addr, end);
436b938fcf4SAndrey Konovalov 
437b938fcf4SAndrey Konovalov 		if (!p4d_present(*p4d))
438b938fcf4SAndrey Konovalov 			continue;
439b938fcf4SAndrey Konovalov 
440b938fcf4SAndrey Konovalov 		if (kasan_pud_table(*p4d)) {
441b938fcf4SAndrey Konovalov 			if (IS_ALIGNED(addr, P4D_SIZE) &&
442a11a496eSLecopzer Chen 			    IS_ALIGNED(next, P4D_SIZE)) {
443b938fcf4SAndrey Konovalov 				p4d_clear(p4d);
444b938fcf4SAndrey Konovalov 				continue;
445b938fcf4SAndrey Konovalov 			}
446a11a496eSLecopzer Chen 		}
447b938fcf4SAndrey Konovalov 		pud = pud_offset(p4d, addr);
448b938fcf4SAndrey Konovalov 		kasan_remove_pud_table(pud, addr, next);
449b938fcf4SAndrey Konovalov 		kasan_free_pud(pud_offset(p4d, 0), p4d);
450b938fcf4SAndrey Konovalov 	}
451b938fcf4SAndrey Konovalov }
452b938fcf4SAndrey Konovalov 
kasan_remove_zero_shadow(void * start,unsigned long size)453b938fcf4SAndrey Konovalov void kasan_remove_zero_shadow(void *start, unsigned long size)
454b938fcf4SAndrey Konovalov {
455b938fcf4SAndrey Konovalov 	unsigned long addr, end, next;
456b938fcf4SAndrey Konovalov 	pgd_t *pgd;
457b938fcf4SAndrey Konovalov 
458b938fcf4SAndrey Konovalov 	addr = (unsigned long)kasan_mem_to_shadow(start);
459b938fcf4SAndrey Konovalov 	end = addr + (size >> KASAN_SHADOW_SCALE_SHIFT);
460b938fcf4SAndrey Konovalov 
461affc3f07SAndrey Konovalov 	if (WARN_ON((unsigned long)start % KASAN_MEMORY_PER_SHADOW_PAGE) ||
462affc3f07SAndrey Konovalov 	    WARN_ON(size % KASAN_MEMORY_PER_SHADOW_PAGE))
463b938fcf4SAndrey Konovalov 		return;
464b938fcf4SAndrey Konovalov 
465b938fcf4SAndrey Konovalov 	for (; addr < end; addr = next) {
466b938fcf4SAndrey Konovalov 		p4d_t *p4d;
467b938fcf4SAndrey Konovalov 
468b938fcf4SAndrey Konovalov 		next = pgd_addr_end(addr, end);
469b938fcf4SAndrey Konovalov 
470b938fcf4SAndrey Konovalov 		pgd = pgd_offset_k(addr);
471b938fcf4SAndrey Konovalov 		if (!pgd_present(*pgd))
472b938fcf4SAndrey Konovalov 			continue;
473b938fcf4SAndrey Konovalov 
474b938fcf4SAndrey Konovalov 		if (kasan_p4d_table(*pgd)) {
475b938fcf4SAndrey Konovalov 			if (IS_ALIGNED(addr, PGDIR_SIZE) &&
476a11a496eSLecopzer Chen 			    IS_ALIGNED(next, PGDIR_SIZE)) {
477b938fcf4SAndrey Konovalov 				pgd_clear(pgd);
478b938fcf4SAndrey Konovalov 				continue;
479b938fcf4SAndrey Konovalov 			}
480a11a496eSLecopzer Chen 		}
481b938fcf4SAndrey Konovalov 
482b938fcf4SAndrey Konovalov 		p4d = p4d_offset(pgd, addr);
483b938fcf4SAndrey Konovalov 		kasan_remove_p4d_table(p4d, addr, next);
484b938fcf4SAndrey Konovalov 		kasan_free_p4d(p4d_offset(pgd, 0), pgd);
485b938fcf4SAndrey Konovalov 	}
486b938fcf4SAndrey Konovalov }
487b938fcf4SAndrey Konovalov 
kasan_add_zero_shadow(void * start,unsigned long size)488b938fcf4SAndrey Konovalov int kasan_add_zero_shadow(void *start, unsigned long size)
489b938fcf4SAndrey Konovalov {
490b938fcf4SAndrey Konovalov 	int ret;
491b938fcf4SAndrey Konovalov 	void *shadow_start, *shadow_end;
492b938fcf4SAndrey Konovalov 
493b938fcf4SAndrey Konovalov 	shadow_start = kasan_mem_to_shadow(start);
494b938fcf4SAndrey Konovalov 	shadow_end = shadow_start + (size >> KASAN_SHADOW_SCALE_SHIFT);
495b938fcf4SAndrey Konovalov 
496affc3f07SAndrey Konovalov 	if (WARN_ON((unsigned long)start % KASAN_MEMORY_PER_SHADOW_PAGE) ||
497affc3f07SAndrey Konovalov 	    WARN_ON(size % KASAN_MEMORY_PER_SHADOW_PAGE))
498b938fcf4SAndrey Konovalov 		return -EINVAL;
499b938fcf4SAndrey Konovalov 
5009577dd74SAndrey Konovalov 	ret = kasan_populate_early_shadow(shadow_start, shadow_end);
501b938fcf4SAndrey Konovalov 	if (ret)
5025dabd171SLecopzer Chen 		kasan_remove_zero_shadow(start, size);
503b938fcf4SAndrey Konovalov 	return ret;
504b938fcf4SAndrey Konovalov }
505