xref: /linux/arch/arm64/mm/contpte.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2023 ARM Ltd.
4  */
5 
6 #include <linux/mm.h>
7 #include <linux/efi.h>
8 #include <linux/export.h>
9 #include <asm/tlbflush.h>
10 
11 static inline bool mm_is_user(struct mm_struct *mm)
12 {
13 	/*
14 	 * Don't attempt to apply the contig bit to kernel mappings, because
15 	 * dynamically adding/removing the contig bit can cause page faults.
16 	 * These racing faults are ok for user space, since they get serialized
17 	 * on the PTL. But kernel mappings can't tolerate faults.
18 	 */
19 	if (unlikely(mm_is_efi(mm)))
20 		return false;
21 	return mm != &init_mm;
22 }
23 
24 static inline pte_t *contpte_align_down(pte_t *ptep)
25 {
26 	return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
27 }
28 
29 static inline pte_t *contpte_align_addr_ptep(unsigned long *start,
30 					     unsigned long *end, pte_t *ptep,
31 					     unsigned int nr)
32 {
33 	/*
34 	 * Note: caller must ensure these nr PTEs are consecutive (present)
35 	 * PTEs that map consecutive pages of the same large folio within a
36 	 * single VMA and a single page table.
37 	 */
38 	if (pte_cont(__ptep_get(ptep + nr - 1)))
39 		*end = ALIGN(*end, CONT_PTE_SIZE);
40 
41 	if (pte_cont(__ptep_get(ptep))) {
42 		*start = ALIGN_DOWN(*start, CONT_PTE_SIZE);
43 		ptep = contpte_align_down(ptep);
44 	}
45 
46 	return ptep;
47 }
48 
49 static void contpte_try_unfold_partial(struct mm_struct *mm, unsigned long addr,
50 					pte_t *ptep, unsigned int nr)
51 {
52 	/*
53 	 * Unfold any partially covered contpte block at the beginning and end
54 	 * of the range.
55 	 */
56 
57 	if (ptep != contpte_align_down(ptep) || nr < CONT_PTES)
58 		contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
59 
60 	if (ptep + nr != contpte_align_down(ptep + nr)) {
61 		unsigned long last_addr = addr + PAGE_SIZE * (nr - 1);
62 		pte_t *last_ptep = ptep + nr - 1;
63 
64 		contpte_try_unfold(mm, last_addr, last_ptep,
65 				   __ptep_get(last_ptep));
66 	}
67 }
68 
69 static void contpte_convert(struct mm_struct *mm, unsigned long addr,
70 			    pte_t *ptep, pte_t pte)
71 {
72 	struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
73 	unsigned long start_addr;
74 	pte_t *start_ptep;
75 	int i;
76 
77 	start_ptep = ptep = contpte_align_down(ptep);
78 	start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
79 	pte = pfn_pte(ALIGN_DOWN(pte_pfn(pte), CONT_PTES), pte_pgprot(pte));
80 
81 	for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE) {
82 		pte_t ptent = __ptep_get_and_clear(mm, addr, ptep);
83 
84 		if (pte_dirty(ptent))
85 			pte = pte_mkdirty(pte);
86 
87 		if (pte_young(ptent))
88 			pte = pte_mkyoung(pte);
89 	}
90 
91 	/*
92 	 * On eliding the __tlb_flush_range() under BBML3:
93 	 *
94 	 * NOTE: Instead of using N=16 as the contiguous block length, we use
95 	 *       N=4 for clarity.
96 	 *
97 	 * NOTE: 'n' and 'c' are used to denote the "contiguous bit" being
98 	 *       unset and set, respectively.
99 	 *
100 	 * We worry about two cases where contiguous bit is used:
101 	 *  - When folding N smaller non-contiguous ptes as 1 contiguous block.
102 	 *  - When unfolding a contiguous block into N smaller non-contiguous ptes.
103 	 *
104 	 * Currently, the BBML0 folding case looks as follows:
105 	 *
106 	 *  0) Initial page-table layout:
107 	 *
108 	 *   +----+----+----+----+
109 	 *   |RO,n|RO,n|RO,n|RW,n| <--- last page being set as RO
110 	 *   +----+----+----+----+
111 	 *
112 	 *  1) Aggregate AF + dirty flags using __ptep_get_and_clear():
113 	 *
114 	 *   +----+----+----+----+
115 	 *   |  0 |  0 |  0 |  0 |
116 	 *   +----+----+----+----+
117 	 *
118 	 *  2) __flush_tlb_range():
119 	 *
120 	 *   |____ tlbi + dsb ____|
121 	 *
122 	 *  3) __set_ptes() to repaint contiguous block:
123 	 *
124 	 *   +----+----+----+----+
125 	 *   |RO,c|RO,c|RO,c|RO,c|
126 	 *   +----+----+----+----+
127 	 *
128 	 *  4) The kernel will eventually __flush_tlb() for changed page:
129 	 *
130 	 *                  |____| <--- tlbi + dsb
131 	 *
132 	 * As expected, the intermediate tlbi+dsb ensures that other PEs
133 	 * only ever see an invalid (0) entry, or the new contiguous TLB entry.
134 	 * The final tlbi+dsb will always throw away the newly installed
135 	 * contiguous TLB entry, which is a micro-optimisation opportunity,
136 	 * but does not affect correctness.
137 	 *
138 	 * In the BBML3 case, the change is avoiding the intermediate tlbi+dsb.
139 	 * This means a few things, but notably other PEs will still "see" any
140 	 * stale cached TLB entries. This could lead to a "contiguous bit
141 	 * misprogramming" issue until the final tlbi+dsb of the changed page,
142 	 * which would clear out both the stale (RW,n) entry and the new (RO,c)
143 	 * contiguous entry installed in its place.
144 	 *
145 	 * What this is saying, is the following:
146 	 *
147 	 *  +----+----+----+----+
148 	 *  |RO,n|RO,n|RO,n|RW,n| <--- old page tables, all non-contiguous
149 	 *  +----+----+----+----+
150 	 *
151 	 *  +----+----+----+----+
152 	 *  |RO,c|RO,c|RO,c|RO,c| <--- new page tables, all contiguous
153 	 *  +----+----+----+----+
154 	 *   /\
155 	 *   ||
156 	 *
157 	 *  If both the old single (RW,n) and new contiguous (RO,c) TLB entries
158 	 *  are present, and a write is made to this address, do we fault or
159 	 *  is the write permitted (via amalgamation)?
160 	 *
161 	 * With BBML3 implemented, no TLB conflict abort is raised and the OA,
162 	 * access permissions and memory attributes produced is one of the cached
163 	 * TLB entries, but never amalgamate.
164 	 *
165 	 * Thus, as the page tables are only considered "consistent" after
166 	 * the final tlbi+dsb (which evicts both the single stale (RW,n) TLB
167 	 * entry as well as the new contiguous (RO,c) TLB entry), omitting the
168 	 * initial tlbi+dsb is correct.
169 	 *
170 	 * It is also important to note that at the end of the BBML3 folding
171 	 * case, we are still left with potentially all N TLB entries still
172 	 * cached (the N-1 non-contiguous ptes, and the single contiguous
173 	 * block). However, over time, natural TLB pressure will cause the
174 	 * non-contiguous pte TLB entries to be flushed, leaving only the
175 	 * contiguous block TLB entry. This means that omitting the tlbi+dsb is
176 	 * not only correct, but also keeps our eventual performance benefits.
177 	 *
178 	 * For the unfolding case, BBML0 looks as follows:
179 	 *
180 	 *  0) Initial page-table layout:
181 	 *
182 	 *   +----+----+----+----+
183 	 *   |RW,c|RW,c|RW,c|RW,c| <--- last page being set as RO
184 	 *   +----+----+----+----+
185 	 *
186 	 *  1) Aggregate AF + dirty flags using __ptep_get_and_clear():
187 	 *
188 	 *   +----+----+----+----+
189 	 *   |  0 |  0 |  0 |  0 |
190 	 *   +----+----+----+----+
191 	 *
192 	 *  2) __flush_tlb_range():
193 	 *
194 	 *   |____ tlbi + dsb ____|
195 	 *
196 	 *  3) __set_ptes() to repaint as non-contiguous:
197 	 *
198 	 *   +----+----+----+----+
199 	 *   |RW,n|RW,n|RW,n|RW,n|
200 	 *   +----+----+----+----+
201 	 *
202 	 *  4) Update changed page permissions:
203 	 *
204 	 *   +----+----+----+----+
205 	 *   |RW,n|RW,n|RW,n|RO,n| <--- last page permissions set
206 	 *   +----+----+----+----+
207 	 *
208 	 *  5) The kernel will eventually __flush_tlb() for changed page:
209 	 *
210 	 *                  |____| <--- tlbi + dsb
211 	 *
212 	 * For BBML3, we again remove the intermediate tlbi+dsb. Here, there
213 	 * are no issues, as the final tlbi+dsb covering the changed page is
214 	 * guaranteed to remove the original large contiguous (RW,c) TLB entry,
215 	 * as well as the intermediate (RW,n) TLB entry; the next access will
216 	 * install the new (RO,n) TLB entry and the page tables are only
217 	 * considered "consistent" after the final tlbi+dsb, so software must
218 	 * be prepared for this inconsistency prior to finishing the mm dance
219 	 * regardless.
220 	 */
221 
222 	if (!system_supports_bbml3())
223 		__flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, 3,
224 				  TLBF_NOWALKCACHE);
225 
226 	__set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES);
227 }
228 
229 void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
230 			pte_t *ptep, pte_t pte)
231 {
232 	/*
233 	 * We have already checked that the virtual and pysical addresses are
234 	 * correctly aligned for a contpte mapping in contpte_try_fold() so the
235 	 * remaining checks are to ensure that the contpte range is fully
236 	 * covered by a single folio, and ensure that all the ptes are valid
237 	 * with contiguous PFNs and matching prots. We ignore the state of the
238 	 * access and dirty bits for the purpose of deciding if its a contiguous
239 	 * range; the folding process will generate a single contpte entry which
240 	 * has a single access and dirty bit. Those 2 bits are the logical OR of
241 	 * their respective bits in the constituent pte entries. In order to
242 	 * ensure the contpte range is covered by a single folio, we must
243 	 * recover the folio from the pfn, but special mappings don't have a
244 	 * folio backing them. Fortunately contpte_try_fold() already checked
245 	 * that the pte is not special - we never try to fold special mappings.
246 	 * Note we can't use vm_normal_page() for this since we don't have the
247 	 * vma.
248 	 */
249 
250 	unsigned long folio_start, folio_end;
251 	unsigned long cont_start, cont_end;
252 	pte_t expected_pte, subpte;
253 	struct folio *folio;
254 	struct page *page;
255 	unsigned long pfn;
256 	pte_t *orig_ptep;
257 	pgprot_t prot;
258 
259 	int i;
260 
261 	if (!mm_is_user(mm))
262 		return;
263 
264 	page = pte_page(pte);
265 	folio = page_folio(page);
266 	folio_start = addr - (page - &folio->page) * PAGE_SIZE;
267 	folio_end = folio_start + folio_nr_pages(folio) * PAGE_SIZE;
268 	cont_start = ALIGN_DOWN(addr, CONT_PTE_SIZE);
269 	cont_end = cont_start + CONT_PTE_SIZE;
270 
271 	if (folio_start > cont_start || folio_end < cont_end)
272 		return;
273 
274 	pfn = ALIGN_DOWN(pte_pfn(pte), CONT_PTES);
275 	prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
276 	expected_pte = pfn_pte(pfn, prot);
277 	orig_ptep = ptep;
278 	ptep = contpte_align_down(ptep);
279 
280 	for (i = 0; i < CONT_PTES; i++) {
281 		subpte = pte_mkold(pte_mkclean(__ptep_get(ptep)));
282 		if (!pte_same(subpte, expected_pte))
283 			return;
284 		expected_pte = pte_advance_pfn(expected_pte, 1);
285 		ptep++;
286 	}
287 
288 	pte = pte_mkcont(pte);
289 	contpte_convert(mm, addr, orig_ptep, pte);
290 }
291 EXPORT_SYMBOL_GPL(__contpte_try_fold);
292 
293 void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
294 			pte_t *ptep, pte_t pte)
295 {
296 	/*
297 	 * We have already checked that the ptes are contiguous in
298 	 * contpte_try_unfold(), so just check that the mm is user space.
299 	 */
300 	if (!mm_is_user(mm))
301 		return;
302 
303 	pte = pte_mknoncont(pte);
304 	contpte_convert(mm, addr, ptep, pte);
305 }
306 EXPORT_SYMBOL_GPL(__contpte_try_unfold);
307 
308 pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
309 {
310 	/*
311 	 * Gather access/dirty bits, which may be populated in any of the ptes
312 	 * of the contig range. We are guaranteed to be holding the PTL, so any
313 	 * contiguous range cannot be unfolded or otherwise modified under our
314 	 * feet.
315 	 */
316 
317 	pte_t pte;
318 	int i;
319 
320 	ptep = contpte_align_down(ptep);
321 
322 	for (i = 0; i < CONT_PTES; i++, ptep++) {
323 		pte = __ptep_get(ptep);
324 
325 		if (pte_dirty(pte)) {
326 			orig_pte = pte_mkdirty(orig_pte);
327 			for (; i < CONT_PTES; i++, ptep++) {
328 				pte = __ptep_get(ptep);
329 				if (pte_young(pte)) {
330 					orig_pte = pte_mkyoung(orig_pte);
331 					break;
332 				}
333 			}
334 			break;
335 		}
336 
337 		if (pte_young(pte)) {
338 			orig_pte = pte_mkyoung(orig_pte);
339 			i++;
340 			ptep++;
341 			for (; i < CONT_PTES; i++, ptep++) {
342 				pte = __ptep_get(ptep);
343 				if (pte_dirty(pte)) {
344 					orig_pte = pte_mkdirty(orig_pte);
345 					break;
346 				}
347 			}
348 			break;
349 		}
350 	}
351 
352 	return orig_pte;
353 }
354 EXPORT_SYMBOL_GPL(contpte_ptep_get);
355 
356 static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,
357 					pgprot_t orig_prot)
358 {
359 	pgprot_t prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
360 
361 	return pte_valid_cont(pte) && pte_pfn(pte) == pfn &&
362 			pgprot_val(prot) == pgprot_val(orig_prot);
363 }
364 
365 pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
366 {
367 	/*
368 	 * The ptep_get_lockless() API requires us to read and return *orig_ptep
369 	 * so that it is self-consistent, without the PTL held, so we may be
370 	 * racing with other threads modifying the pte. Usually a READ_ONCE()
371 	 * would suffice, but for the contpte case, we also need to gather the
372 	 * access and dirty bits from across all ptes in the contiguous block,
373 	 * and we can't read all of those neighbouring ptes atomically, so any
374 	 * contiguous range may be unfolded/modified/refolded under our feet.
375 	 * Therefore we ensure we read a _consistent_ contpte range by checking
376 	 * that all ptes in the range are valid and have CONT_PTE set, that all
377 	 * pfns are contiguous and that all pgprots are the same (ignoring
378 	 * access/dirty). If we find a pte that is not consistent, then we must
379 	 * be racing with an update so start again. If the target pte does not
380 	 * have CONT_PTE set then that is considered consistent on its own
381 	 * because it is not part of a contpte range.
382 	 */
383 
384 	pgprot_t orig_prot;
385 	unsigned long pfn;
386 	pte_t orig_pte;
387 	pte_t *ptep;
388 	pte_t pte;
389 	int i;
390 
391 retry:
392 	orig_pte = __ptep_get(orig_ptep);
393 
394 	if (!pte_valid_cont(orig_pte))
395 		return orig_pte;
396 
397 	orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte)));
398 	ptep = contpte_align_down(orig_ptep);
399 	pfn = pte_pfn(orig_pte) - (orig_ptep - ptep);
400 
401 	for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) {
402 		pte = __ptep_get(ptep);
403 
404 		if (!contpte_is_consistent(pte, pfn, orig_prot))
405 			goto retry;
406 
407 		if (pte_dirty(pte)) {
408 			orig_pte = pte_mkdirty(orig_pte);
409 			for (; i < CONT_PTES; i++, ptep++, pfn++) {
410 				pte = __ptep_get(ptep);
411 
412 				if (!contpte_is_consistent(pte, pfn, orig_prot))
413 					goto retry;
414 
415 				if (pte_young(pte)) {
416 					orig_pte = pte_mkyoung(orig_pte);
417 					break;
418 				}
419 			}
420 			break;
421 		}
422 
423 		if (pte_young(pte)) {
424 			orig_pte = pte_mkyoung(orig_pte);
425 			i++;
426 			ptep++;
427 			pfn++;
428 			for (; i < CONT_PTES; i++, ptep++, pfn++) {
429 				pte = __ptep_get(ptep);
430 
431 				if (!contpte_is_consistent(pte, pfn, orig_prot))
432 					goto retry;
433 
434 				if (pte_dirty(pte)) {
435 					orig_pte = pte_mkdirty(orig_pte);
436 					break;
437 				}
438 			}
439 			break;
440 		}
441 	}
442 
443 	return orig_pte;
444 }
445 EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless);
446 
447 void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
448 					pte_t *ptep, pte_t pte, unsigned int nr)
449 {
450 	unsigned long next;
451 	unsigned long end;
452 	unsigned long pfn;
453 	pgprot_t prot;
454 
455 	/*
456 	 * The set_ptes() spec guarantees that when nr > 1, the initial state of
457 	 * all ptes is not-present. Therefore we never need to unfold or
458 	 * otherwise invalidate a range before we set the new ptes.
459 	 * contpte_set_ptes() should never be called for nr < 2.
460 	 */
461 	VM_WARN_ON(nr == 1);
462 
463 	if (!mm_is_user(mm))
464 		return __set_ptes(mm, addr, ptep, pte, nr);
465 
466 	end = addr + (nr << PAGE_SHIFT);
467 	pfn = pte_pfn(pte);
468 	prot = pte_pgprot(pte);
469 
470 	do {
471 		next = pte_cont_addr_end(addr, end);
472 		nr = (next - addr) >> PAGE_SHIFT;
473 		pte = pfn_pte(pfn, prot);
474 
475 		if (((addr | next | (pfn << PAGE_SHIFT)) & ~CONT_PTE_MASK) == 0)
476 			pte = pte_mkcont(pte);
477 		else
478 			pte = pte_mknoncont(pte);
479 
480 		__set_ptes(mm, addr, ptep, pte, nr);
481 
482 		addr = next;
483 		ptep += nr;
484 		pfn += nr;
485 
486 	} while (addr != end);
487 }
488 EXPORT_SYMBOL_GPL(contpte_set_ptes);
489 
490 void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
491 				pte_t *ptep, unsigned int nr, int full)
492 {
493 	contpte_try_unfold_partial(mm, addr, ptep, nr);
494 	__clear_full_ptes(mm, addr, ptep, nr, full);
495 }
496 EXPORT_SYMBOL_GPL(contpte_clear_full_ptes);
497 
498 pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm,
499 				unsigned long addr, pte_t *ptep,
500 				unsigned int nr, int full)
501 {
502 	contpte_try_unfold_partial(mm, addr, ptep, nr);
503 	return __get_and_clear_full_ptes(mm, addr, ptep, nr, full);
504 }
505 EXPORT_SYMBOL_GPL(contpte_get_and_clear_full_ptes);
506 
507 bool contpte_test_and_clear_young_ptes(struct vm_area_struct *vma,
508 		unsigned long addr, pte_t *ptep, unsigned int nr)
509 {
510 	/*
511 	 * ptep_clear_flush_young() technically requires us to clear the access
512 	 * flag for a _single_ pte. However, the core-mm code actually tracks
513 	 * access/dirty per folio, not per page. And since we only create a
514 	 * contig range when the range is covered by a single folio, we can get
515 	 * away with clearing young for the whole contig range here, so we avoid
516 	 * having to unfold.
517 	 *
518 	 * The 'nr' means consecutive (present) PTEs that map consecutive pages
519 	 * of the same large folio in a single VMA and a single page table.
520 	 */
521 
522 	unsigned long end = addr + nr * PAGE_SIZE;
523 	bool young = false;
524 
525 	ptep = contpte_align_addr_ptep(&addr, &end, ptep, nr);
526 	for (; addr != end; ptep++, addr += PAGE_SIZE)
527 		young |= __ptep_test_and_clear_young(vma, addr, ptep);
528 
529 	return young;
530 }
531 EXPORT_SYMBOL_GPL(contpte_test_and_clear_young_ptes);
532 
533 bool contpte_clear_flush_young_ptes(struct vm_area_struct *vma,
534 		unsigned long addr, pte_t *ptep, unsigned int nr)
535 {
536 	bool young;
537 
538 	young = contpte_test_and_clear_young_ptes(vma, addr, ptep, nr);
539 
540 	if (young) {
541 		unsigned long end = addr + nr * PAGE_SIZE;
542 
543 		contpte_align_addr_ptep(&addr, &end, ptep, nr);
544 		/*
545 		 * See comment in __ptep_clear_flush_young(); same rationale for
546 		 * eliding the trailing DSB applies here.
547 		 */
548 		__flush_tlb_range(vma, addr, end, PAGE_SIZE, 3,
549 				  TLBF_NOWALKCACHE | TLBF_NOSYNC);
550 	}
551 
552 	return young;
553 }
554 EXPORT_SYMBOL_GPL(contpte_clear_flush_young_ptes);
555 
556 void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
557 					pte_t *ptep, unsigned int nr)
558 {
559 	/*
560 	 * If wrprotecting an entire contig range, we can avoid unfolding. Just
561 	 * set wrprotect and wait for the later mmu_gather flush to invalidate
562 	 * the tlb. Until the flush, the page may or may not be wrprotected.
563 	 * After the flush, it is guaranteed wrprotected. If it's a partial
564 	 * range though, we must unfold, because we can't have a case where
565 	 * CONT_PTE is set but wrprotect applies to a subset of the PTEs; this
566 	 * would cause it to continue to be unpredictable after the flush.
567 	 */
568 
569 	contpte_try_unfold_partial(mm, addr, ptep, nr);
570 	__wrprotect_ptes(mm, addr, ptep, nr);
571 }
572 EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);
573 
574 void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
575 				    unsigned long addr, pte_t *ptep,
576 				    unsigned int nr, cydp_t flags)
577 {
578 	/*
579 	 * We can safely clear access/dirty without needing to unfold from
580 	 * the architectures perspective, even when contpte is set. If the
581 	 * range starts or ends midway through a contpte block, we can just
582 	 * expand to include the full contpte block. While this is not
583 	 * exactly what the core-mm asked for, it tracks access/dirty per
584 	 * folio, not per page. And since we only create a contpte block
585 	 * when it is covered by a single folio, we can get away with
586 	 * clearing access/dirty for the whole block.
587 	 */
588 	unsigned long start = addr;
589 	unsigned long end = start + nr * PAGE_SIZE;
590 
591 	ptep = contpte_align_addr_ptep(&start, &end, ptep, nr);
592 	__clear_young_dirty_ptes(vma, start, ptep, (end - start) / PAGE_SIZE, flags);
593 }
594 EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);
595 
596 static bool contpte_all_subptes_match_access_flags(pte_t *ptep, pte_t entry)
597 {
598 	pte_t *cont_ptep = contpte_align_down(ptep);
599 	/*
600 	 * PFNs differ per sub-PTE. Match only bits consumed by
601 	 * __ptep_set_access_flags(): AF, DIRTY and write permission.
602 	 */
603 	const pteval_t cmp_mask = PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY;
604 	pteval_t entry_cmp = pte_val(entry) & cmp_mask;
605 	int i;
606 
607 	for (i = 0; i < CONT_PTES; i++) {
608 		pteval_t pte_cmp = pte_val(__ptep_get(cont_ptep + i)) & cmp_mask;
609 
610 		if (pte_cmp != entry_cmp)
611 			return false;
612 	}
613 
614 	return true;
615 }
616 
617 int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
618 					unsigned long addr, pte_t *ptep,
619 					pte_t entry, int dirty)
620 {
621 	unsigned long start_addr;
622 	pte_t orig_pte;
623 	int i;
624 
625 	/*
626 	 * Check whether all sub-PTEs in the CONT block already match the
627 	 * requested access flags/write permission, using raw per-PTE values
628 	 * rather than the gathered ptep_get() view.
629 	 *
630 	 * __ptep_set_access_flags() can update AF, dirty and write
631 	 * permission, but only to make the mapping more permissive.
632 	 *
633 	 * ptep_get() gathers AF/dirty state across the whole CONT block,
634 	 * which is correct for a CPU with FEAT_HAFDBS. But page-table
635 	 * walkers that evaluate each descriptor individually (e.g. a CPU
636 	 * without DBM support, or an SMMU without HTTU, or with HA/HD
637 	 * disabled in CD.TCR) can keep faulting on the target sub-PTE if
638 	 * only a sibling has been updated. Gathering can therefore cause
639 	 * false no-ops when only a sibling has been updated:
640 	 *  - write faults: target still has PTE_RDONLY (needs PTE_RDONLY cleared)
641 	 *  - read faults:  target still lacks PTE_AF
642 	 *
643 	 * Per Arm ARM (DDI 0487) D8.7.1, any sub-PTE in a CONT range may
644 	 * become the effective cached translation, so all entries must have
645 	 * consistent attributes. Check the full CONT block before returning
646 	 * no-op, and when any sub-PTE mismatches, proceed to update the whole
647 	 * range.
648 	 */
649 	if (contpte_all_subptes_match_access_flags(ptep, entry))
650 		return 0;
651 
652 	/*
653 	 * Use raw target pte (not gathered) for write-bit unfold decision.
654 	 */
655 	orig_pte = pte_mknoncont(__ptep_get(ptep));
656 
657 	/*
658 	 * We can fix up access/dirty bits without having to unfold the contig
659 	 * range. But if the write bit is changing, we must unfold.
660 	 */
661 	if (pte_write(orig_pte) == pte_write(entry)) {
662 		/*
663 		 * For HW access management, we technically only need to update
664 		 * the flag on a single pte in the range. But for SW access
665 		 * management, we need to update all the ptes to prevent extra
666 		 * faults. Avoid per-page tlb flush in __ptep_set_access_flags()
667 		 * and instead flush the whole range at the end.
668 		 */
669 		ptep = contpte_align_down(ptep);
670 		start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
671 
672 		/*
673 		 * We are not advancing entry because __ptep_set_access_flags()
674 		 * only consumes access flags from entry. And since we have checked
675 		 * for the whole contpte block and returned early, pte_same()
676 		 * within __ptep_set_access_flags() is likely false.
677 		 */
678 		for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
679 			__ptep_set_access_flags(vma, addr, ptep, entry, 0);
680 
681 		if (dirty)
682 			__flush_tlb_range(vma, start_addr,
683 					  start_addr + CONT_PTE_SIZE,
684 					  PAGE_SIZE, 3,
685 					  TLBF_NOWALKCACHE | TLBF_NOBROADCAST);
686 	} else {
687 		__contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte);
688 		__ptep_set_access_flags(vma, addr, ptep, entry, dirty);
689 	}
690 
691 	return 1;
692 }
693 EXPORT_SYMBOL_GPL(contpte_ptep_set_access_flags);
694