xref: /linux/arch/arm64/mm/contpte.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
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 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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
602 int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
603 					unsigned long addr, pte_t *ptep,
604 					pte_t entry, int dirty)
605 {
606 	unsigned long start_addr;
607 	pte_t orig_pte;
608 	int i;
609 
610 	/*
611 	 * Gather the access/dirty bits for the contiguous range. If nothing has
612 	 * changed, its a noop.
613 	 */
614 	orig_pte = pte_mknoncont(ptep_get(ptep));
615 	if (pte_val(orig_pte) == pte_val(entry))
616 		return 0;
617 
618 	/*
619 	 * We can fix up access/dirty bits without having to unfold the contig
620 	 * range. But if the write bit is changing, we must unfold.
621 	 */
622 	if (pte_write(orig_pte) == pte_write(entry)) {
623 		/*
624 		 * For HW access management, we technically only need to update
625 		 * the flag on a single pte in the range. But for SW access
626 		 * management, we need to update all the ptes to prevent extra
627 		 * faults. Avoid per-page tlb flush in __ptep_set_access_flags()
628 		 * and instead flush the whole range at the end.
629 		 */
630 		ptep = contpte_align_down(ptep);
631 		start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
632 
633 		/*
634 		 * We are not advancing entry because __ptep_set_access_flags()
635 		 * only consumes access flags from entry. And since we have checked
636 		 * for the whole contpte block and returned early, pte_same()
637 		 * within __ptep_set_access_flags() is likely false.
638 		 */
639 		for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
640 			__ptep_set_access_flags(vma, addr, ptep, entry, 0);
641 
642 		if (dirty)
643 			local_flush_tlb_contpte(vma, start_addr);
644 	} else {
645 		__contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte);
646 		__ptep_set_access_flags(vma, addr, ptep, entry, dirty);
647 	}
648 
649 	return 1;
650 }
651 EXPORT_SYMBOL_GPL(contpte_ptep_set_access_flags);
652