xref: /linux/arch/arm64/mm/pageattr.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4  */
5 #include <linux/kernel.h>
6 #include <linux/mm.h>
7 #include <linux/module.h>
8 #include <linux/mem_encrypt.h>
9 #include <linux/sched.h>
10 #include <linux/vmalloc.h>
11 #include <linux/pagewalk.h>
12 
13 #include <asm/cacheflush.h>
14 #include <asm/pgtable-prot.h>
15 #include <asm/set_memory.h>
16 #include <asm/tlbflush.h>
17 #include <asm/kfence.h>
18 
19 struct page_change_data {
20 	pgprot_t set_mask;
21 	pgprot_t clear_mask;
22 };
23 
24 static ptdesc_t set_pageattr_masks(ptdesc_t val, struct mm_walk *walk)
25 {
26 	struct page_change_data *masks = walk->private;
27 
28 	val &= ~(pgprot_val(masks->clear_mask));
29 	val |= (pgprot_val(masks->set_mask));
30 
31 	return val;
32 }
33 
34 static int pageattr_pud_entry(pud_t *pud, unsigned long addr,
35 			      unsigned long next, struct mm_walk *walk)
36 {
37 	pud_t val = pudp_get(pud);
38 
39 	if (pud_sect(val)) {
40 		if (WARN_ON_ONCE((next - addr) != PUD_SIZE))
41 			return -EINVAL;
42 		val = __pud(set_pageattr_masks(pud_val(val), walk));
43 		set_pud(pud, val);
44 		walk->action = ACTION_CONTINUE;
45 	}
46 
47 	return 0;
48 }
49 
50 static int pageattr_pmd_entry(pmd_t *pmd, unsigned long addr,
51 			      unsigned long next, struct mm_walk *walk)
52 {
53 	pmd_t val = pmdp_get(pmd);
54 
55 	if (pmd_sect(val)) {
56 		if (WARN_ON_ONCE((next - addr) != PMD_SIZE))
57 			return -EINVAL;
58 		val = __pmd(set_pageattr_masks(pmd_val(val), walk));
59 		set_pmd(pmd, val);
60 		walk->action = ACTION_CONTINUE;
61 	}
62 
63 	return 0;
64 }
65 
66 static int pageattr_pte_entry(pte_t *pte, unsigned long addr,
67 			      unsigned long next, struct mm_walk *walk)
68 {
69 	pte_t val = __ptep_get(pte);
70 
71 	val = __pte(set_pageattr_masks(pte_val(val), walk));
72 	__set_pte(pte, val);
73 
74 	return 0;
75 }
76 
77 static const struct mm_walk_ops pageattr_ops = {
78 	.pud_entry	= pageattr_pud_entry,
79 	.pmd_entry	= pageattr_pmd_entry,
80 	.pte_entry	= pageattr_pte_entry,
81 };
82 
83 bool rodata_full __ro_after_init = true;
84 
85 bool can_set_direct_map(void)
86 {
87 	/*
88 	 * rodata_full, DEBUG_PAGEALLOC and a Realm guest all require linear
89 	 * map to be mapped at page granularity, so that it is possible to
90 	 * protect/unprotect single pages.
91 	 *
92 	 * KFENCE pool requires page-granular mapping if initialized late.
93 	 *
94 	 * Realms need to make pages shared/protected at page granularity.
95 	 */
96 	return rodata_full || debug_pagealloc_enabled() ||
97 		arm64_kfence_can_set_direct_map() || is_realm_world();
98 }
99 
100 static int update_range_prot(unsigned long start, unsigned long size,
101 			     pgprot_t set_mask, pgprot_t clear_mask)
102 {
103 	struct page_change_data data;
104 	int ret;
105 
106 	data.set_mask = set_mask;
107 	data.clear_mask = clear_mask;
108 
109 	ret = split_kernel_leaf_mapping(start, start + size);
110 	if (WARN_ON_ONCE(ret))
111 		return ret;
112 
113 	arch_enter_lazy_mmu_mode();
114 
115 	/*
116 	 * The caller must ensure that the range we are operating on does not
117 	 * partially overlap a block mapping, or a cont mapping. Any such case
118 	 * must be eliminated by splitting the mapping.
119 	 */
120 	ret = walk_kernel_page_table_range_lockless(start, start + size,
121 						    &pageattr_ops, NULL, &data);
122 	arch_leave_lazy_mmu_mode();
123 
124 	return ret;
125 }
126 
127 static int __change_memory_common(unsigned long start, unsigned long size,
128 				  pgprot_t set_mask, pgprot_t clear_mask)
129 {
130 	int ret;
131 
132 	ret = update_range_prot(start, size, set_mask, clear_mask);
133 
134 	/*
135 	 * If the memory is being made valid without changing any other bits
136 	 * then a TLBI isn't required as a non-valid entry cannot be cached in
137 	 * the TLB.
138 	 */
139 	if (pgprot_val(set_mask) != PTE_VALID || pgprot_val(clear_mask))
140 		flush_tlb_kernel_range(start, start + size);
141 	return ret;
142 }
143 
144 static int change_memory_common(unsigned long addr, int numpages,
145 				pgprot_t set_mask, pgprot_t clear_mask)
146 {
147 	unsigned long start = addr;
148 	unsigned long size = PAGE_SIZE * numpages;
149 	unsigned long end = start + size;
150 	struct vm_struct *area;
151 	int ret;
152 
153 	if (!PAGE_ALIGNED(addr)) {
154 		start &= PAGE_MASK;
155 		end = start + size;
156 		WARN_ON_ONCE(1);
157 	}
158 
159 	/*
160 	 * Kernel VA mappings are always live, and splitting live section
161 	 * mappings into page mappings may cause TLB conflicts. This means
162 	 * we have to ensure that changing the permission bits of the range
163 	 * we are operating on does not result in such splitting.
164 	 *
165 	 * Let's restrict ourselves to mappings created by vmalloc (or vmap).
166 	 * Disallow VM_ALLOW_HUGE_VMAP mappings to guarantee that only page
167 	 * mappings are updated and splitting is never needed.
168 	 *
169 	 * So check whether the [addr, addr + size) interval is entirely
170 	 * covered by precisely one VM area that has the VM_ALLOC flag set.
171 	 */
172 	area = find_vm_area((void *)addr);
173 	if (!area ||
174 	    end > (unsigned long)kasan_reset_tag(area->addr) + area->size ||
175 	    ((area->flags & (VM_ALLOC | VM_ALLOW_HUGE_VMAP)) != VM_ALLOC))
176 		return -EINVAL;
177 
178 	if (!numpages)
179 		return 0;
180 
181 	/*
182 	 * If we are manipulating read-only permissions, apply the same
183 	 * change to the linear mapping of the pages that back this VM area.
184 	 */
185 	if (rodata_full && (pgprot_val(set_mask) == PTE_RDONLY ||
186 			    pgprot_val(clear_mask) == PTE_RDONLY)) {
187 		unsigned long idx = (start - (unsigned long)kasan_reset_tag(area->addr))
188 				    >> PAGE_SHIFT;
189 		for (; numpages; idx++, numpages--) {
190 			ret = __change_memory_common((u64)page_address(area->pages[idx]),
191 						     PAGE_SIZE, set_mask, clear_mask);
192 			if (ret)
193 				return ret;
194 		}
195 	}
196 
197 	/*
198 	 * Get rid of potentially aliasing lazily unmapped vm areas that may
199 	 * have permissions set that deviate from the ones we are setting here.
200 	 */
201 	vm_unmap_aliases();
202 
203 	return __change_memory_common(start, size, set_mask, clear_mask);
204 }
205 
206 int set_memory_ro(unsigned long addr, int numpages)
207 {
208 	return change_memory_common(addr, numpages,
209 					__pgprot(PTE_RDONLY),
210 					__pgprot(PTE_WRITE));
211 }
212 
213 int set_memory_rw(unsigned long addr, int numpages)
214 {
215 	return change_memory_common(addr, numpages,
216 					__pgprot(PTE_WRITE),
217 					__pgprot(PTE_RDONLY));
218 }
219 
220 int set_memory_nx(unsigned long addr, int numpages)
221 {
222 	return change_memory_common(addr, numpages,
223 					__pgprot(PTE_PXN),
224 					__pgprot(PTE_MAYBE_GP));
225 }
226 
227 int set_memory_x(unsigned long addr, int numpages)
228 {
229 	return change_memory_common(addr, numpages,
230 					__pgprot(PTE_MAYBE_GP),
231 					__pgprot(PTE_PXN));
232 }
233 
234 int set_memory_valid(unsigned long addr, int numpages, int enable)
235 {
236 	if (enable)
237 		return __change_memory_common(addr, PAGE_SIZE * numpages,
238 					__pgprot(PTE_VALID),
239 					__pgprot(0));
240 	else
241 		return __change_memory_common(addr, PAGE_SIZE * numpages,
242 					__pgprot(0),
243 					__pgprot(PTE_VALID));
244 }
245 
246 int set_direct_map_invalid_noflush(struct page *page)
247 {
248 	pgprot_t clear_mask = __pgprot(PTE_VALID);
249 	pgprot_t set_mask = __pgprot(0);
250 
251 	if (!can_set_direct_map())
252 		return 0;
253 
254 	return update_range_prot((unsigned long)page_address(page),
255 				 PAGE_SIZE, set_mask, clear_mask);
256 }
257 
258 int set_direct_map_default_noflush(struct page *page)
259 {
260 	pgprot_t set_mask = __pgprot(PTE_VALID | PTE_WRITE);
261 	pgprot_t clear_mask = __pgprot(PTE_RDONLY);
262 
263 	if (!can_set_direct_map())
264 		return 0;
265 
266 	return update_range_prot((unsigned long)page_address(page),
267 				 PAGE_SIZE, set_mask, clear_mask);
268 }
269 
270 static int __set_memory_enc_dec(unsigned long addr,
271 				int numpages,
272 				bool encrypt)
273 {
274 	unsigned long set_prot = 0, clear_prot = 0;
275 	phys_addr_t start, end;
276 	int ret;
277 
278 	if (!is_realm_world())
279 		return 0;
280 
281 	if (!__is_lm_address(addr))
282 		return -EINVAL;
283 
284 	start = __virt_to_phys(addr);
285 	end = start + numpages * PAGE_SIZE;
286 
287 	if (encrypt)
288 		clear_prot = PROT_NS_SHARED;
289 	else
290 		set_prot = PROT_NS_SHARED;
291 
292 	/*
293 	 * Break the mapping before we make any changes to avoid stale TLB
294 	 * entries or Synchronous External Aborts caused by RIPAS_EMPTY
295 	 */
296 	ret = __change_memory_common(addr, PAGE_SIZE * numpages,
297 				     __pgprot(set_prot),
298 				     __pgprot(clear_prot | PTE_VALID));
299 
300 	if (ret)
301 		return ret;
302 
303 	if (encrypt)
304 		ret = rsi_set_memory_range_protected(start, end);
305 	else
306 		ret = rsi_set_memory_range_shared(start, end);
307 
308 	if (ret)
309 		return ret;
310 
311 	return __change_memory_common(addr, PAGE_SIZE * numpages,
312 				      __pgprot(PTE_VALID),
313 				      __pgprot(0));
314 }
315 
316 static int realm_set_memory_encrypted(unsigned long addr, int numpages)
317 {
318 	int ret = __set_memory_enc_dec(addr, numpages, true);
319 
320 	/*
321 	 * If the request to change state fails, then the only sensible cause
322 	 * of action for the caller is to leak the memory
323 	 */
324 	WARN(ret, "Failed to encrypt memory, %d pages will be leaked",
325 	     numpages);
326 
327 	return ret;
328 }
329 
330 static int realm_set_memory_decrypted(unsigned long addr, int numpages)
331 {
332 	int ret = __set_memory_enc_dec(addr, numpages, false);
333 
334 	WARN(ret, "Failed to decrypt memory, %d pages will be leaked",
335 	     numpages);
336 
337 	return ret;
338 }
339 
340 static const struct arm64_mem_crypt_ops realm_crypt_ops = {
341 	.encrypt = realm_set_memory_encrypted,
342 	.decrypt = realm_set_memory_decrypted,
343 };
344 
345 int realm_register_memory_enc_ops(void)
346 {
347 	return arm64_mem_crypt_ops_register(&realm_crypt_ops);
348 }
349 
350 int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)
351 {
352 	unsigned long addr = (unsigned long)page_address(page);
353 
354 	if (!can_set_direct_map())
355 		return 0;
356 
357 	return set_memory_valid(addr, nr, valid);
358 }
359 
360 #ifdef CONFIG_DEBUG_PAGEALLOC
361 /*
362  * This is - apart from the return value - doing the same
363  * thing as the new set_direct_map_valid_noflush() function.
364  *
365  * Unify? Explain the conceptual differences?
366  */
367 void __kernel_map_pages(struct page *page, int numpages, int enable)
368 {
369 	if (!can_set_direct_map())
370 		return;
371 
372 	set_memory_valid((unsigned long)page_address(page), numpages, enable);
373 }
374 #endif /* CONFIG_DEBUG_PAGEALLOC */
375 
376 /*
377  * This function is used to determine if a linear map page has been marked as
378  * not-valid. Walk the page table and check the PTE_VALID bit.
379  *
380  * Because this is only called on the kernel linear map,  p?d_sect() implies
381  * p?d_present(). When debug_pagealloc is enabled, sections mappings are
382  * disabled.
383  */
384 bool kernel_page_present(struct page *page)
385 {
386 	pgd_t *pgdp;
387 	p4d_t *p4dp;
388 	pud_t *pudp, pud;
389 	pmd_t *pmdp, pmd;
390 	pte_t *ptep;
391 	unsigned long addr = (unsigned long)page_address(page);
392 
393 	pgdp = pgd_offset_k(addr);
394 	if (pgd_none(READ_ONCE(*pgdp)))
395 		return false;
396 
397 	p4dp = p4d_offset(pgdp, addr);
398 	if (p4d_none(READ_ONCE(*p4dp)))
399 		return false;
400 
401 	pudp = pud_offset(p4dp, addr);
402 	pud = READ_ONCE(*pudp);
403 	if (pud_none(pud))
404 		return false;
405 	if (pud_sect(pud))
406 		return true;
407 
408 	pmdp = pmd_offset(pudp, addr);
409 	pmd = READ_ONCE(*pmdp);
410 	if (pmd_none(pmd))
411 		return false;
412 	if (pmd_sect(pmd))
413 		return true;
414 
415 	ptep = pte_offset_kernel(pmdp, addr);
416 	return pte_valid(__ptep_get(ptep));
417 }
418