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
mm_is_user(struct mm_struct * mm)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
contpte_align_down(pte_t * ptep)24 static inline pte_t *contpte_align_down(pte_t *ptep)
25 {
26 return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
27 }
28
contpte_align_addr_ptep(unsigned long * start,unsigned long * end,pte_t * ptep,unsigned int nr)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
contpte_try_unfold_partial(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr)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
contpte_convert(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)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 BBML2+noabort:
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 BBML2 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 * The relevant Arm ARM DDI 0487L.a requirements are RNGLXZ and RJQQTC,
162 * and together state that when BBML1 or BBML2 are implemented, either
163 * a TLB conflict abort is raised (which we expressly forbid), or will
164 * "produce an OA, access permissions, and memory attributes that are
165 * consistent with any of the programmed translation table values".
166 *
167 * That is to say, will either raise a TLB conflict, or produce one of
168 * the cached TLB entries, but never amalgamate.
169 *
170 * Thus, as the page tables are only considered "consistent" after
171 * the final tlbi+dsb (which evicts both the single stale (RW,n) TLB
172 * entry as well as the new contiguous (RO,c) TLB entry), omitting the
173 * initial tlbi+dsb is correct.
174 *
175 * It is also important to note that at the end of the BBML2 folding
176 * case, we are still left with potentially all N TLB entries still
177 * cached (the N-1 non-contiguous ptes, and the single contiguous
178 * block). However, over time, natural TLB pressure will cause the
179 * non-contiguous pte TLB entries to be flushed, leaving only the
180 * contiguous block TLB entry. This means that omitting the tlbi+dsb is
181 * not only correct, but also keeps our eventual performance benefits.
182 *
183 * For the unfolding case, BBML0 looks as follows:
184 *
185 * 0) Initial page-table layout:
186 *
187 * +----+----+----+----+
188 * |RW,c|RW,c|RW,c|RW,c| <--- last page being set as RO
189 * +----+----+----+----+
190 *
191 * 1) Aggregate AF + dirty flags using __ptep_get_and_clear():
192 *
193 * +----+----+----+----+
194 * | 0 | 0 | 0 | 0 |
195 * +----+----+----+----+
196 *
197 * 2) __flush_tlb_range():
198 *
199 * |____ tlbi + dsb ____|
200 *
201 * 3) __set_ptes() to repaint as non-contiguous:
202 *
203 * +----+----+----+----+
204 * |RW,n|RW,n|RW,n|RW,n|
205 * +----+----+----+----+
206 *
207 * 4) Update changed page permissions:
208 *
209 * +----+----+----+----+
210 * |RW,n|RW,n|RW,n|RO,n| <--- last page permissions set
211 * +----+----+----+----+
212 *
213 * 5) The kernel will eventually __flush_tlb() for changed page:
214 *
215 * |____| <--- tlbi + dsb
216 *
217 * For BBML2, we again remove the intermediate tlbi+dsb. Here, there
218 * are no issues, as the final tlbi+dsb covering the changed page is
219 * guaranteed to remove the original large contiguous (RW,c) TLB entry,
220 * as well as the intermediate (RW,n) TLB entry; the next access will
221 * install the new (RO,n) TLB entry and the page tables are only
222 * considered "consistent" after the final tlbi+dsb, so software must
223 * be prepared for this inconsistency prior to finishing the mm dance
224 * regardless.
225 */
226
227 if (!system_supports_bbml2_noabort())
228 __flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, true, 3);
229
230 __set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES);
231 }
232
__contpte_try_fold(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)233 void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
234 pte_t *ptep, pte_t pte)
235 {
236 /*
237 * We have already checked that the virtual and pysical addresses are
238 * correctly aligned for a contpte mapping in contpte_try_fold() so the
239 * remaining checks are to ensure that the contpte range is fully
240 * covered by a single folio, and ensure that all the ptes are valid
241 * with contiguous PFNs and matching prots. We ignore the state of the
242 * access and dirty bits for the purpose of deciding if its a contiguous
243 * range; the folding process will generate a single contpte entry which
244 * has a single access and dirty bit. Those 2 bits are the logical OR of
245 * their respective bits in the constituent pte entries. In order to
246 * ensure the contpte range is covered by a single folio, we must
247 * recover the folio from the pfn, but special mappings don't have a
248 * folio backing them. Fortunately contpte_try_fold() already checked
249 * that the pte is not special - we never try to fold special mappings.
250 * Note we can't use vm_normal_page() for this since we don't have the
251 * vma.
252 */
253
254 unsigned long folio_start, folio_end;
255 unsigned long cont_start, cont_end;
256 pte_t expected_pte, subpte;
257 struct folio *folio;
258 struct page *page;
259 unsigned long pfn;
260 pte_t *orig_ptep;
261 pgprot_t prot;
262
263 int i;
264
265 if (!mm_is_user(mm))
266 return;
267
268 page = pte_page(pte);
269 folio = page_folio(page);
270 folio_start = addr - (page - &folio->page) * PAGE_SIZE;
271 folio_end = folio_start + folio_nr_pages(folio) * PAGE_SIZE;
272 cont_start = ALIGN_DOWN(addr, CONT_PTE_SIZE);
273 cont_end = cont_start + CONT_PTE_SIZE;
274
275 if (folio_start > cont_start || folio_end < cont_end)
276 return;
277
278 pfn = ALIGN_DOWN(pte_pfn(pte), CONT_PTES);
279 prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
280 expected_pte = pfn_pte(pfn, prot);
281 orig_ptep = ptep;
282 ptep = contpte_align_down(ptep);
283
284 for (i = 0; i < CONT_PTES; i++) {
285 subpte = pte_mkold(pte_mkclean(__ptep_get(ptep)));
286 if (!pte_same(subpte, expected_pte))
287 return;
288 expected_pte = pte_advance_pfn(expected_pte, 1);
289 ptep++;
290 }
291
292 pte = pte_mkcont(pte);
293 contpte_convert(mm, addr, orig_ptep, pte);
294 }
295 EXPORT_SYMBOL_GPL(__contpte_try_fold);
296
__contpte_try_unfold(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)297 void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
298 pte_t *ptep, pte_t pte)
299 {
300 /*
301 * We have already checked that the ptes are contiguous in
302 * contpte_try_unfold(), so just check that the mm is user space.
303 */
304 if (!mm_is_user(mm))
305 return;
306
307 pte = pte_mknoncont(pte);
308 contpte_convert(mm, addr, ptep, pte);
309 }
310 EXPORT_SYMBOL_GPL(__contpte_try_unfold);
311
contpte_ptep_get(pte_t * ptep,pte_t orig_pte)312 pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
313 {
314 /*
315 * Gather access/dirty bits, which may be populated in any of the ptes
316 * of the contig range. We are guaranteed to be holding the PTL, so any
317 * contiguous range cannot be unfolded or otherwise modified under our
318 * feet.
319 */
320
321 pte_t pte;
322 int i;
323
324 ptep = contpte_align_down(ptep);
325
326 for (i = 0; i < CONT_PTES; i++, ptep++) {
327 pte = __ptep_get(ptep);
328
329 if (pte_dirty(pte)) {
330 orig_pte = pte_mkdirty(orig_pte);
331 for (; i < CONT_PTES; i++, ptep++) {
332 pte = __ptep_get(ptep);
333 if (pte_young(pte)) {
334 orig_pte = pte_mkyoung(orig_pte);
335 break;
336 }
337 }
338 break;
339 }
340
341 if (pte_young(pte)) {
342 orig_pte = pte_mkyoung(orig_pte);
343 i++;
344 ptep++;
345 for (; i < CONT_PTES; i++, ptep++) {
346 pte = __ptep_get(ptep);
347 if (pte_dirty(pte)) {
348 orig_pte = pte_mkdirty(orig_pte);
349 break;
350 }
351 }
352 break;
353 }
354 }
355
356 return orig_pte;
357 }
358 EXPORT_SYMBOL_GPL(contpte_ptep_get);
359
contpte_is_consistent(pte_t pte,unsigned long pfn,pgprot_t orig_prot)360 static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,
361 pgprot_t orig_prot)
362 {
363 pgprot_t prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
364
365 return pte_valid_cont(pte) && pte_pfn(pte) == pfn &&
366 pgprot_val(prot) == pgprot_val(orig_prot);
367 }
368
contpte_ptep_get_lockless(pte_t * orig_ptep)369 pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
370 {
371 /*
372 * The ptep_get_lockless() API requires us to read and return *orig_ptep
373 * so that it is self-consistent, without the PTL held, so we may be
374 * racing with other threads modifying the pte. Usually a READ_ONCE()
375 * would suffice, but for the contpte case, we also need to gather the
376 * access and dirty bits from across all ptes in the contiguous block,
377 * and we can't read all of those neighbouring ptes atomically, so any
378 * contiguous range may be unfolded/modified/refolded under our feet.
379 * Therefore we ensure we read a _consistent_ contpte range by checking
380 * that all ptes in the range are valid and have CONT_PTE set, that all
381 * pfns are contiguous and that all pgprots are the same (ignoring
382 * access/dirty). If we find a pte that is not consistent, then we must
383 * be racing with an update so start again. If the target pte does not
384 * have CONT_PTE set then that is considered consistent on its own
385 * because it is not part of a contpte range.
386 */
387
388 pgprot_t orig_prot;
389 unsigned long pfn;
390 pte_t orig_pte;
391 pte_t *ptep;
392 pte_t pte;
393 int i;
394
395 retry:
396 orig_pte = __ptep_get(orig_ptep);
397
398 if (!pte_valid_cont(orig_pte))
399 return orig_pte;
400
401 orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte)));
402 ptep = contpte_align_down(orig_ptep);
403 pfn = pte_pfn(orig_pte) - (orig_ptep - ptep);
404
405 for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) {
406 pte = __ptep_get(ptep);
407
408 if (!contpte_is_consistent(pte, pfn, orig_prot))
409 goto retry;
410
411 if (pte_dirty(pte)) {
412 orig_pte = pte_mkdirty(orig_pte);
413 for (; i < CONT_PTES; i++, ptep++, pfn++) {
414 pte = __ptep_get(ptep);
415
416 if (!contpte_is_consistent(pte, pfn, orig_prot))
417 goto retry;
418
419 if (pte_young(pte)) {
420 orig_pte = pte_mkyoung(orig_pte);
421 break;
422 }
423 }
424 break;
425 }
426
427 if (pte_young(pte)) {
428 orig_pte = pte_mkyoung(orig_pte);
429 i++;
430 ptep++;
431 pfn++;
432 for (; i < CONT_PTES; i++, ptep++, pfn++) {
433 pte = __ptep_get(ptep);
434
435 if (!contpte_is_consistent(pte, pfn, orig_prot))
436 goto retry;
437
438 if (pte_dirty(pte)) {
439 orig_pte = pte_mkdirty(orig_pte);
440 break;
441 }
442 }
443 break;
444 }
445 }
446
447 return orig_pte;
448 }
449 EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless);
450
contpte_set_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte,unsigned int nr)451 void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
452 pte_t *ptep, pte_t pte, unsigned int nr)
453 {
454 unsigned long next;
455 unsigned long end;
456 unsigned long pfn;
457 pgprot_t prot;
458
459 /*
460 * The set_ptes() spec guarantees that when nr > 1, the initial state of
461 * all ptes is not-present. Therefore we never need to unfold or
462 * otherwise invalidate a range before we set the new ptes.
463 * contpte_set_ptes() should never be called for nr < 2.
464 */
465 VM_WARN_ON(nr == 1);
466
467 if (!mm_is_user(mm))
468 return __set_ptes(mm, addr, ptep, pte, nr);
469
470 end = addr + (nr << PAGE_SHIFT);
471 pfn = pte_pfn(pte);
472 prot = pte_pgprot(pte);
473
474 do {
475 next = pte_cont_addr_end(addr, end);
476 nr = (next - addr) >> PAGE_SHIFT;
477 pte = pfn_pte(pfn, prot);
478
479 if (((addr | next | (pfn << PAGE_SHIFT)) & ~CONT_PTE_MASK) == 0)
480 pte = pte_mkcont(pte);
481 else
482 pte = pte_mknoncont(pte);
483
484 __set_ptes(mm, addr, ptep, pte, nr);
485
486 addr = next;
487 ptep += nr;
488 pfn += nr;
489
490 } while (addr != end);
491 }
492 EXPORT_SYMBOL_GPL(contpte_set_ptes);
493
contpte_clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)494 void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
495 pte_t *ptep, unsigned int nr, int full)
496 {
497 contpte_try_unfold_partial(mm, addr, ptep, nr);
498 __clear_full_ptes(mm, addr, ptep, nr, full);
499 }
500 EXPORT_SYMBOL_GPL(contpte_clear_full_ptes);
501
contpte_get_and_clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)502 pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm,
503 unsigned long addr, pte_t *ptep,
504 unsigned int nr, int full)
505 {
506 contpte_try_unfold_partial(mm, addr, ptep, nr);
507 return __get_and_clear_full_ptes(mm, addr, ptep, nr, full);
508 }
509 EXPORT_SYMBOL_GPL(contpte_get_and_clear_full_ptes);
510
contpte_test_and_clear_young_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr)511 int contpte_test_and_clear_young_ptes(struct vm_area_struct *vma,
512 unsigned long addr, pte_t *ptep,
513 unsigned int nr)
514 {
515 /*
516 * ptep_clear_flush_young() technically requires us to clear the access
517 * flag for a _single_ pte. However, the core-mm code actually tracks
518 * access/dirty per folio, not per page. And since we only create a
519 * contig range when the range is covered by a single folio, we can get
520 * away with clearing young for the whole contig range here, so we avoid
521 * having to unfold.
522 *
523 * The 'nr' means consecutive (present) PTEs that map consecutive pages
524 * of the same large folio in a single VMA and a single page table.
525 */
526
527 unsigned long end = addr + nr * PAGE_SIZE;
528 int young = 0;
529
530 ptep = contpte_align_addr_ptep(&addr, &end, ptep, nr);
531 for (; addr != end; ptep++, addr += PAGE_SIZE)
532 young |= __ptep_test_and_clear_young(vma, addr, ptep);
533
534 return young;
535 }
536 EXPORT_SYMBOL_GPL(contpte_test_and_clear_young_ptes);
537
contpte_clear_flush_young_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr)538 int contpte_clear_flush_young_ptes(struct vm_area_struct *vma,
539 unsigned long addr, pte_t *ptep,
540 unsigned int nr)
541 {
542 int young;
543
544 young = contpte_test_and_clear_young_ptes(vma, addr, ptep, nr);
545
546 if (young) {
547 unsigned long end = addr + nr * PAGE_SIZE;
548
549 contpte_align_addr_ptep(&addr, &end, ptep, nr);
550 /*
551 * See comment in __ptep_clear_flush_young(); same rationale for
552 * eliding the trailing DSB applies here.
553 */
554 __flush_tlb_range_nosync(vma->vm_mm, addr, end,
555 PAGE_SIZE, true, 3);
556 }
557
558 return young;
559 }
560 EXPORT_SYMBOL_GPL(contpte_clear_flush_young_ptes);
561
contpte_wrprotect_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr)562 void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
563 pte_t *ptep, unsigned int nr)
564 {
565 /*
566 * If wrprotecting an entire contig range, we can avoid unfolding. Just
567 * set wrprotect and wait for the later mmu_gather flush to invalidate
568 * the tlb. Until the flush, the page may or may not be wrprotected.
569 * After the flush, it is guaranteed wrprotected. If it's a partial
570 * range though, we must unfold, because we can't have a case where
571 * CONT_PTE is set but wrprotect applies to a subset of the PTEs; this
572 * would cause it to continue to be unpredictable after the flush.
573 */
574
575 contpte_try_unfold_partial(mm, addr, ptep, nr);
576 __wrprotect_ptes(mm, addr, ptep, nr);
577 }
578 EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);
579
contpte_clear_young_dirty_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr,cydp_t flags)580 void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
581 unsigned long addr, pte_t *ptep,
582 unsigned int nr, cydp_t flags)
583 {
584 /*
585 * We can safely clear access/dirty without needing to unfold from
586 * the architectures perspective, even when contpte is set. If the
587 * range starts or ends midway through a contpte block, we can just
588 * expand to include the full contpte block. While this is not
589 * exactly what the core-mm asked for, it tracks access/dirty per
590 * folio, not per page. And since we only create a contpte block
591 * when it is covered by a single folio, we can get away with
592 * clearing access/dirty for the whole block.
593 */
594 unsigned long start = addr;
595 unsigned long end = start + nr * PAGE_SIZE;
596
597 ptep = contpte_align_addr_ptep(&start, &end, ptep, nr);
598 __clear_young_dirty_ptes(vma, start, ptep, (end - start) / PAGE_SIZE, flags);
599 }
600 EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);
601
contpte_all_subptes_match_access_flags(pte_t * ptep,pte_t entry)602 static bool contpte_all_subptes_match_access_flags(pte_t *ptep, pte_t entry)
603 {
604 pte_t *cont_ptep = contpte_align_down(ptep);
605 /*
606 * PFNs differ per sub-PTE. Match only bits consumed by
607 * __ptep_set_access_flags(): AF, DIRTY and write permission.
608 */
609 const pteval_t cmp_mask = PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY;
610 pteval_t entry_cmp = pte_val(entry) & cmp_mask;
611 int i;
612
613 for (i = 0; i < CONT_PTES; i++) {
614 pteval_t pte_cmp = pte_val(__ptep_get(cont_ptep + i)) & cmp_mask;
615
616 if (pte_cmp != entry_cmp)
617 return false;
618 }
619
620 return true;
621 }
622
contpte_ptep_set_access_flags(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t entry,int dirty)623 int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
624 unsigned long addr, pte_t *ptep,
625 pte_t entry, int dirty)
626 {
627 unsigned long start_addr;
628 pte_t orig_pte;
629 int i;
630
631 /*
632 * Check whether all sub-PTEs in the CONT block already match the
633 * requested access flags/write permission, using raw per-PTE values
634 * rather than the gathered ptep_get() view.
635 *
636 * __ptep_set_access_flags() can update AF, dirty and write
637 * permission, but only to make the mapping more permissive.
638 *
639 * ptep_get() gathers AF/dirty state across the whole CONT block,
640 * which is correct for a CPU with FEAT_HAFDBS. But page-table
641 * walkers that evaluate each descriptor individually (e.g. a CPU
642 * without DBM support, or an SMMU without HTTU, or with HA/HD
643 * disabled in CD.TCR) can keep faulting on the target sub-PTE if
644 * only a sibling has been updated. Gathering can therefore cause
645 * false no-ops when only a sibling has been updated:
646 * - write faults: target still has PTE_RDONLY (needs PTE_RDONLY cleared)
647 * - read faults: target still lacks PTE_AF
648 *
649 * Per Arm ARM (DDI 0487) D8.7.1, any sub-PTE in a CONT range may
650 * become the effective cached translation, so all entries must have
651 * consistent attributes. Check the full CONT block before returning
652 * no-op, and when any sub-PTE mismatches, proceed to update the whole
653 * range.
654 */
655 if (contpte_all_subptes_match_access_flags(ptep, entry))
656 return 0;
657
658 /*
659 * Use raw target pte (not gathered) for write-bit unfold decision.
660 */
661 orig_pte = pte_mknoncont(__ptep_get(ptep));
662
663 /*
664 * We can fix up access/dirty bits without having to unfold the contig
665 * range. But if the write bit is changing, we must unfold.
666 */
667 if (pte_write(orig_pte) == pte_write(entry)) {
668 /*
669 * For HW access management, we technically only need to update
670 * the flag on a single pte in the range. But for SW access
671 * management, we need to update all the ptes to prevent extra
672 * faults. Avoid per-page tlb flush in __ptep_set_access_flags()
673 * and instead flush the whole range at the end.
674 */
675 ptep = contpte_align_down(ptep);
676 start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
677
678 /*
679 * We are not advancing entry because __ptep_set_access_flags()
680 * only consumes access flags from entry. And since we have checked
681 * for the whole contpte block and returned early, pte_same()
682 * within __ptep_set_access_flags() is likely false.
683 */
684 for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
685 __ptep_set_access_flags(vma, addr, ptep, entry, 0);
686
687 if (dirty)
688 local_flush_tlb_contpte(vma, start_addr);
689 } else {
690 __contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte);
691 __ptep_set_access_flags(vma, addr, ptep, entry, dirty);
692 }
693
694 return 1;
695 }
696 EXPORT_SYMBOL_GPL(contpte_ptep_set_access_flags);
697