xref: /linux/arch/arm64/mm/contpte.c (revision 6fb44438a5e1897a72dd11139274735256be8069)
14602e575SRyan Roberts // SPDX-License-Identifier: GPL-2.0-only
24602e575SRyan Roberts /*
34602e575SRyan Roberts  * Copyright (C) 2023 ARM Ltd.
44602e575SRyan Roberts  */
54602e575SRyan Roberts 
64602e575SRyan Roberts #include <linux/mm.h>
74602e575SRyan Roberts #include <linux/efi.h>
84602e575SRyan Roberts #include <linux/export.h>
94602e575SRyan Roberts #include <asm/tlbflush.h>
104602e575SRyan Roberts 
mm_is_user(struct mm_struct * mm)114602e575SRyan Roberts static inline bool mm_is_user(struct mm_struct *mm)
124602e575SRyan Roberts {
134602e575SRyan Roberts 	/*
144602e575SRyan Roberts 	 * Don't attempt to apply the contig bit to kernel mappings, because
154602e575SRyan Roberts 	 * dynamically adding/removing the contig bit can cause page faults.
164602e575SRyan Roberts 	 * These racing faults are ok for user space, since they get serialized
174602e575SRyan Roberts 	 * on the PTL. But kernel mappings can't tolerate faults.
184602e575SRyan Roberts 	 */
194602e575SRyan Roberts 	if (unlikely(mm_is_efi(mm)))
204602e575SRyan Roberts 		return false;
214602e575SRyan Roberts 	return mm != &init_mm;
224602e575SRyan Roberts }
234602e575SRyan Roberts 
contpte_align_down(pte_t * ptep)244602e575SRyan Roberts static inline pte_t *contpte_align_down(pte_t *ptep)
254602e575SRyan Roberts {
264602e575SRyan Roberts 	return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
274602e575SRyan Roberts }
284602e575SRyan Roberts 
contpte_try_unfold_partial(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr)29311a6cf2SRyan Roberts static void contpte_try_unfold_partial(struct mm_struct *mm, unsigned long addr,
30311a6cf2SRyan Roberts 					pte_t *ptep, unsigned int nr)
31311a6cf2SRyan Roberts {
32311a6cf2SRyan Roberts 	/*
33311a6cf2SRyan Roberts 	 * Unfold any partially covered contpte block at the beginning and end
34311a6cf2SRyan Roberts 	 * of the range.
35311a6cf2SRyan Roberts 	 */
36311a6cf2SRyan Roberts 
37311a6cf2SRyan Roberts 	if (ptep != contpte_align_down(ptep) || nr < CONT_PTES)
38311a6cf2SRyan Roberts 		contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep));
39311a6cf2SRyan Roberts 
40311a6cf2SRyan Roberts 	if (ptep + nr != contpte_align_down(ptep + nr)) {
41311a6cf2SRyan Roberts 		unsigned long last_addr = addr + PAGE_SIZE * (nr - 1);
42311a6cf2SRyan Roberts 		pte_t *last_ptep = ptep + nr - 1;
43311a6cf2SRyan Roberts 
44311a6cf2SRyan Roberts 		contpte_try_unfold(mm, last_addr, last_ptep,
45311a6cf2SRyan Roberts 				   __ptep_get(last_ptep));
46311a6cf2SRyan Roberts 	}
47311a6cf2SRyan Roberts }
48311a6cf2SRyan Roberts 
contpte_convert(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)494602e575SRyan Roberts static void contpte_convert(struct mm_struct *mm, unsigned long addr,
504602e575SRyan Roberts 			    pte_t *ptep, pte_t pte)
514602e575SRyan Roberts {
524602e575SRyan Roberts 	struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
534602e575SRyan Roberts 	unsigned long start_addr;
544602e575SRyan Roberts 	pte_t *start_ptep;
554602e575SRyan Roberts 	int i;
564602e575SRyan Roberts 
574602e575SRyan Roberts 	start_ptep = ptep = contpte_align_down(ptep);
584602e575SRyan Roberts 	start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
594602e575SRyan Roberts 	pte = pfn_pte(ALIGN_DOWN(pte_pfn(pte), CONT_PTES), pte_pgprot(pte));
604602e575SRyan Roberts 
614602e575SRyan Roberts 	for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE) {
624602e575SRyan Roberts 		pte_t ptent = __ptep_get_and_clear(mm, addr, ptep);
634602e575SRyan Roberts 
644602e575SRyan Roberts 		if (pte_dirty(ptent))
654602e575SRyan Roberts 			pte = pte_mkdirty(pte);
664602e575SRyan Roberts 
674602e575SRyan Roberts 		if (pte_young(ptent))
684602e575SRyan Roberts 			pte = pte_mkyoung(pte);
694602e575SRyan Roberts 	}
704602e575SRyan Roberts 
7183bbd6beSMikołaj Lenczewski 	/*
7283bbd6beSMikołaj Lenczewski 	 * On eliding the __tlb_flush_range() under BBML2+noabort:
7383bbd6beSMikołaj Lenczewski 	 *
7483bbd6beSMikołaj Lenczewski 	 * NOTE: Instead of using N=16 as the contiguous block length, we use
7583bbd6beSMikołaj Lenczewski 	 *       N=4 for clarity.
7683bbd6beSMikołaj Lenczewski 	 *
7783bbd6beSMikołaj Lenczewski 	 * NOTE: 'n' and 'c' are used to denote the "contiguous bit" being
7883bbd6beSMikołaj Lenczewski 	 *       unset and set, respectively.
7983bbd6beSMikołaj Lenczewski 	 *
8083bbd6beSMikołaj Lenczewski 	 * We worry about two cases where contiguous bit is used:
8183bbd6beSMikołaj Lenczewski 	 *  - When folding N smaller non-contiguous ptes as 1 contiguous block.
8283bbd6beSMikołaj Lenczewski 	 *  - When unfolding a contiguous block into N smaller non-contiguous ptes.
8383bbd6beSMikołaj Lenczewski 	 *
8483bbd6beSMikołaj Lenczewski 	 * Currently, the BBML0 folding case looks as follows:
8583bbd6beSMikołaj Lenczewski 	 *
8683bbd6beSMikołaj Lenczewski 	 *  0) Initial page-table layout:
8783bbd6beSMikołaj Lenczewski 	 *
8883bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
8983bbd6beSMikołaj Lenczewski 	 *   |RO,n|RO,n|RO,n|RW,n| <--- last page being set as RO
9083bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
9183bbd6beSMikołaj Lenczewski 	 *
9283bbd6beSMikołaj Lenczewski 	 *  1) Aggregate AF + dirty flags using __ptep_get_and_clear():
9383bbd6beSMikołaj Lenczewski 	 *
9483bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
9583bbd6beSMikołaj Lenczewski 	 *   |  0 |  0 |  0 |  0 |
9683bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
9783bbd6beSMikołaj Lenczewski 	 *
9883bbd6beSMikołaj Lenczewski 	 *  2) __flush_tlb_range():
9983bbd6beSMikołaj Lenczewski 	 *
10083bbd6beSMikołaj Lenczewski 	 *   |____ tlbi + dsb ____|
10183bbd6beSMikołaj Lenczewski 	 *
10283bbd6beSMikołaj Lenczewski 	 *  3) __set_ptes() to repaint contiguous block:
10383bbd6beSMikołaj Lenczewski 	 *
10483bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
10583bbd6beSMikołaj Lenczewski 	 *   |RO,c|RO,c|RO,c|RO,c|
10683bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
10783bbd6beSMikołaj Lenczewski 	 *
10883bbd6beSMikołaj Lenczewski 	 *  4) The kernel will eventually __flush_tlb() for changed page:
10983bbd6beSMikołaj Lenczewski 	 *
11083bbd6beSMikołaj Lenczewski 	 *                  |____| <--- tlbi + dsb
11183bbd6beSMikołaj Lenczewski 	 *
11283bbd6beSMikołaj Lenczewski 	 * As expected, the intermediate tlbi+dsb ensures that other PEs
11383bbd6beSMikołaj Lenczewski 	 * only ever see an invalid (0) entry, or the new contiguous TLB entry.
11483bbd6beSMikołaj Lenczewski 	 * The final tlbi+dsb will always throw away the newly installed
11583bbd6beSMikołaj Lenczewski 	 * contiguous TLB entry, which is a micro-optimisation opportunity,
11683bbd6beSMikołaj Lenczewski 	 * but does not affect correctness.
11783bbd6beSMikołaj Lenczewski 	 *
11883bbd6beSMikołaj Lenczewski 	 * In the BBML2 case, the change is avoiding the intermediate tlbi+dsb.
11983bbd6beSMikołaj Lenczewski 	 * This means a few things, but notably other PEs will still "see" any
12083bbd6beSMikołaj Lenczewski 	 * stale cached TLB entries. This could lead to a "contiguous bit
12183bbd6beSMikołaj Lenczewski 	 * misprogramming" issue until the final tlbi+dsb of the changed page,
12283bbd6beSMikołaj Lenczewski 	 * which would clear out both the stale (RW,n) entry and the new (RO,c)
12383bbd6beSMikołaj Lenczewski 	 * contiguous entry installed in its place.
12483bbd6beSMikołaj Lenczewski 	 *
12583bbd6beSMikołaj Lenczewski 	 * What this is saying, is the following:
12683bbd6beSMikołaj Lenczewski 	 *
12783bbd6beSMikołaj Lenczewski 	 *  +----+----+----+----+
12883bbd6beSMikołaj Lenczewski 	 *  |RO,n|RO,n|RO,n|RW,n| <--- old page tables, all non-contiguous
12983bbd6beSMikołaj Lenczewski 	 *  +----+----+----+----+
13083bbd6beSMikołaj Lenczewski 	 *
13183bbd6beSMikołaj Lenczewski 	 *  +----+----+----+----+
13283bbd6beSMikołaj Lenczewski 	 *  |RO,c|RO,c|RO,c|RO,c| <--- new page tables, all contiguous
13383bbd6beSMikołaj Lenczewski 	 *  +----+----+----+----+
13483bbd6beSMikołaj Lenczewski 	 *   /\
13583bbd6beSMikołaj Lenczewski 	 *   ||
13683bbd6beSMikołaj Lenczewski 	 *
13783bbd6beSMikołaj Lenczewski 	 *  If both the old single (RW,n) and new contiguous (RO,c) TLB entries
13883bbd6beSMikołaj Lenczewski 	 *  are present, and a write is made to this address, do we fault or
13983bbd6beSMikołaj Lenczewski 	 *  is the write permitted (via amalgamation)?
14083bbd6beSMikołaj Lenczewski 	 *
14183bbd6beSMikołaj Lenczewski 	 * The relevant Arm ARM DDI 0487L.a requirements are RNGLXZ and RJQQTC,
14283bbd6beSMikołaj Lenczewski 	 * and together state that when BBML1 or BBML2 are implemented, either
14383bbd6beSMikołaj Lenczewski 	 * a TLB conflict abort is raised (which we expressly forbid), or will
14483bbd6beSMikołaj Lenczewski 	 * "produce an OA, access permissions, and memory attributes that are
14583bbd6beSMikołaj Lenczewski 	 * consistent with any of the programmed translation table values".
14683bbd6beSMikołaj Lenczewski 	 *
14783bbd6beSMikołaj Lenczewski 	 * That is to say, will either raise a TLB conflict, or produce one of
14883bbd6beSMikołaj Lenczewski 	 * the cached TLB entries, but never amalgamate.
14983bbd6beSMikołaj Lenczewski 	 *
15083bbd6beSMikołaj Lenczewski 	 * Thus, as the page tables are only considered "consistent" after
15183bbd6beSMikołaj Lenczewski 	 * the final tlbi+dsb (which evicts both the single stale (RW,n) TLB
15283bbd6beSMikołaj Lenczewski 	 * entry as well as the new contiguous (RO,c) TLB entry), omitting the
15383bbd6beSMikołaj Lenczewski 	 * initial tlbi+dsb is correct.
15483bbd6beSMikołaj Lenczewski 	 *
15583bbd6beSMikołaj Lenczewski 	 * It is also important to note that at the end of the BBML2 folding
15683bbd6beSMikołaj Lenczewski 	 * case, we are still left with potentially all N TLB entries still
15783bbd6beSMikołaj Lenczewski 	 * cached (the N-1 non-contiguous ptes, and the single contiguous
15883bbd6beSMikołaj Lenczewski 	 * block). However, over time, natural TLB pressure will cause the
15983bbd6beSMikołaj Lenczewski 	 * non-contiguous pte TLB entries to be flushed, leaving only the
16083bbd6beSMikołaj Lenczewski 	 * contiguous block TLB entry. This means that omitting the tlbi+dsb is
16183bbd6beSMikołaj Lenczewski 	 * not only correct, but also keeps our eventual performance benefits.
16283bbd6beSMikołaj Lenczewski 	 *
16383bbd6beSMikołaj Lenczewski 	 * For the unfolding case, BBML0 looks as follows:
16483bbd6beSMikołaj Lenczewski 	 *
16583bbd6beSMikołaj Lenczewski 	 *  0) Initial page-table layout:
16683bbd6beSMikołaj Lenczewski 	 *
16783bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
16883bbd6beSMikołaj Lenczewski 	 *   |RW,c|RW,c|RW,c|RW,c| <--- last page being set as RO
16983bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
17083bbd6beSMikołaj Lenczewski 	 *
17183bbd6beSMikołaj Lenczewski 	 *  1) Aggregate AF + dirty flags using __ptep_get_and_clear():
17283bbd6beSMikołaj Lenczewski 	 *
17383bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
17483bbd6beSMikołaj Lenczewski 	 *   |  0 |  0 |  0 |  0 |
17583bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
17683bbd6beSMikołaj Lenczewski 	 *
17783bbd6beSMikołaj Lenczewski 	 *  2) __flush_tlb_range():
17883bbd6beSMikołaj Lenczewski 	 *
17983bbd6beSMikołaj Lenczewski 	 *   |____ tlbi + dsb ____|
18083bbd6beSMikołaj Lenczewski 	 *
18183bbd6beSMikołaj Lenczewski 	 *  3) __set_ptes() to repaint as non-contiguous:
18283bbd6beSMikołaj Lenczewski 	 *
18383bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
18483bbd6beSMikołaj Lenczewski 	 *   |RW,n|RW,n|RW,n|RW,n|
18583bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
18683bbd6beSMikołaj Lenczewski 	 *
18783bbd6beSMikołaj Lenczewski 	 *  4) Update changed page permissions:
18883bbd6beSMikołaj Lenczewski 	 *
18983bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
19083bbd6beSMikołaj Lenczewski 	 *   |RW,n|RW,n|RW,n|RO,n| <--- last page permissions set
19183bbd6beSMikołaj Lenczewski 	 *   +----+----+----+----+
19283bbd6beSMikołaj Lenczewski 	 *
19383bbd6beSMikołaj Lenczewski 	 *  5) The kernel will eventually __flush_tlb() for changed page:
19483bbd6beSMikołaj Lenczewski 	 *
19583bbd6beSMikołaj Lenczewski 	 *                  |____| <--- tlbi + dsb
19683bbd6beSMikołaj Lenczewski 	 *
19783bbd6beSMikołaj Lenczewski 	 * For BBML2, we again remove the intermediate tlbi+dsb. Here, there
19883bbd6beSMikołaj Lenczewski 	 * are no issues, as the final tlbi+dsb covering the changed page is
19983bbd6beSMikołaj Lenczewski 	 * guaranteed to remove the original large contiguous (RW,c) TLB entry,
20083bbd6beSMikołaj Lenczewski 	 * as well as the intermediate (RW,n) TLB entry; the next access will
20183bbd6beSMikołaj Lenczewski 	 * install the new (RO,n) TLB entry and the page tables are only
20283bbd6beSMikołaj Lenczewski 	 * considered "consistent" after the final tlbi+dsb, so software must
20383bbd6beSMikołaj Lenczewski 	 * be prepared for this inconsistency prior to finishing the mm dance
20483bbd6beSMikołaj Lenczewski 	 * regardless.
20583bbd6beSMikołaj Lenczewski 	 */
20683bbd6beSMikołaj Lenczewski 
20783bbd6beSMikołaj Lenczewski 	if (!system_supports_bbml2_noabort())
2084602e575SRyan Roberts 		__flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, true, 3);
2094602e575SRyan Roberts 
2104602e575SRyan Roberts 	__set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES);
2114602e575SRyan Roberts }
2124602e575SRyan Roberts 
__contpte_try_fold(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)213f0c22649SRyan Roberts void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
214f0c22649SRyan Roberts 			pte_t *ptep, pte_t pte)
215f0c22649SRyan Roberts {
216f0c22649SRyan Roberts 	/*
217f0c22649SRyan Roberts 	 * We have already checked that the virtual and pysical addresses are
218f0c22649SRyan Roberts 	 * correctly aligned for a contpte mapping in contpte_try_fold() so the
219f0c22649SRyan Roberts 	 * remaining checks are to ensure that the contpte range is fully
220f0c22649SRyan Roberts 	 * covered by a single folio, and ensure that all the ptes are valid
221f0c22649SRyan Roberts 	 * with contiguous PFNs and matching prots. We ignore the state of the
222f0c22649SRyan Roberts 	 * access and dirty bits for the purpose of deciding if its a contiguous
223f0c22649SRyan Roberts 	 * range; the folding process will generate a single contpte entry which
224f0c22649SRyan Roberts 	 * has a single access and dirty bit. Those 2 bits are the logical OR of
225f0c22649SRyan Roberts 	 * their respective bits in the constituent pte entries. In order to
226f0c22649SRyan Roberts 	 * ensure the contpte range is covered by a single folio, we must
227f0c22649SRyan Roberts 	 * recover the folio from the pfn, but special mappings don't have a
228f0c22649SRyan Roberts 	 * folio backing them. Fortunately contpte_try_fold() already checked
229f0c22649SRyan Roberts 	 * that the pte is not special - we never try to fold special mappings.
230f0c22649SRyan Roberts 	 * Note we can't use vm_normal_page() for this since we don't have the
231f0c22649SRyan Roberts 	 * vma.
232f0c22649SRyan Roberts 	 */
233f0c22649SRyan Roberts 
234f0c22649SRyan Roberts 	unsigned long folio_start, folio_end;
235f0c22649SRyan Roberts 	unsigned long cont_start, cont_end;
236f0c22649SRyan Roberts 	pte_t expected_pte, subpte;
237f0c22649SRyan Roberts 	struct folio *folio;
238f0c22649SRyan Roberts 	struct page *page;
239f0c22649SRyan Roberts 	unsigned long pfn;
240f0c22649SRyan Roberts 	pte_t *orig_ptep;
241f0c22649SRyan Roberts 	pgprot_t prot;
242f0c22649SRyan Roberts 
243f0c22649SRyan Roberts 	int i;
244f0c22649SRyan Roberts 
245f0c22649SRyan Roberts 	if (!mm_is_user(mm))
246f0c22649SRyan Roberts 		return;
247f0c22649SRyan Roberts 
248f0c22649SRyan Roberts 	page = pte_page(pte);
249f0c22649SRyan Roberts 	folio = page_folio(page);
250f0c22649SRyan Roberts 	folio_start = addr - (page - &folio->page) * PAGE_SIZE;
251f0c22649SRyan Roberts 	folio_end = folio_start + folio_nr_pages(folio) * PAGE_SIZE;
252f0c22649SRyan Roberts 	cont_start = ALIGN_DOWN(addr, CONT_PTE_SIZE);
253f0c22649SRyan Roberts 	cont_end = cont_start + CONT_PTE_SIZE;
254f0c22649SRyan Roberts 
255f0c22649SRyan Roberts 	if (folio_start > cont_start || folio_end < cont_end)
256f0c22649SRyan Roberts 		return;
257f0c22649SRyan Roberts 
258f0c22649SRyan Roberts 	pfn = ALIGN_DOWN(pte_pfn(pte), CONT_PTES);
259f0c22649SRyan Roberts 	prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
260f0c22649SRyan Roberts 	expected_pte = pfn_pte(pfn, prot);
261f0c22649SRyan Roberts 	orig_ptep = ptep;
262f0c22649SRyan Roberts 	ptep = contpte_align_down(ptep);
263f0c22649SRyan Roberts 
264f0c22649SRyan Roberts 	for (i = 0; i < CONT_PTES; i++) {
265f0c22649SRyan Roberts 		subpte = pte_mkold(pte_mkclean(__ptep_get(ptep)));
266f0c22649SRyan Roberts 		if (!pte_same(subpte, expected_pte))
267f0c22649SRyan Roberts 			return;
268f0c22649SRyan Roberts 		expected_pte = pte_advance_pfn(expected_pte, 1);
269f0c22649SRyan Roberts 		ptep++;
270f0c22649SRyan Roberts 	}
271f0c22649SRyan Roberts 
272f0c22649SRyan Roberts 	pte = pte_mkcont(pte);
273f0c22649SRyan Roberts 	contpte_convert(mm, addr, orig_ptep, pte);
274f0c22649SRyan Roberts }
275912609e9SRyan Roberts EXPORT_SYMBOL_GPL(__contpte_try_fold);
276f0c22649SRyan Roberts 
__contpte_try_unfold(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)2774602e575SRyan Roberts void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
2784602e575SRyan Roberts 			pte_t *ptep, pte_t pte)
2794602e575SRyan Roberts {
2804602e575SRyan Roberts 	/*
2814602e575SRyan Roberts 	 * We have already checked that the ptes are contiguous in
2824602e575SRyan Roberts 	 * contpte_try_unfold(), so just check that the mm is user space.
2834602e575SRyan Roberts 	 */
2844602e575SRyan Roberts 	if (!mm_is_user(mm))
2854602e575SRyan Roberts 		return;
2864602e575SRyan Roberts 
2874602e575SRyan Roberts 	pte = pte_mknoncont(pte);
2884602e575SRyan Roberts 	contpte_convert(mm, addr, ptep, pte);
2894602e575SRyan Roberts }
290912609e9SRyan Roberts EXPORT_SYMBOL_GPL(__contpte_try_unfold);
2914602e575SRyan Roberts 
contpte_ptep_get(pte_t * ptep,pte_t orig_pte)2924602e575SRyan Roberts pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
2934602e575SRyan Roberts {
2944602e575SRyan Roberts 	/*
2954602e575SRyan Roberts 	 * Gather access/dirty bits, which may be populated in any of the ptes
2964602e575SRyan Roberts 	 * of the contig range. We are guaranteed to be holding the PTL, so any
2974602e575SRyan Roberts 	 * contiguous range cannot be unfolded or otherwise modified under our
2984602e575SRyan Roberts 	 * feet.
2994602e575SRyan Roberts 	 */
3004602e575SRyan Roberts 
3014602e575SRyan Roberts 	pte_t pte;
3024602e575SRyan Roberts 	int i;
3034602e575SRyan Roberts 
3044602e575SRyan Roberts 	ptep = contpte_align_down(ptep);
3054602e575SRyan Roberts 
3064602e575SRyan Roberts 	for (i = 0; i < CONT_PTES; i++, ptep++) {
3074602e575SRyan Roberts 		pte = __ptep_get(ptep);
3084602e575SRyan Roberts 
309*093ae7a0SXavier Xia 		if (pte_dirty(pte)) {
3104602e575SRyan Roberts 			orig_pte = pte_mkdirty(orig_pte);
311*093ae7a0SXavier Xia 			for (; i < CONT_PTES; i++, ptep++) {
312*093ae7a0SXavier Xia 				pte = __ptep_get(ptep);
313*093ae7a0SXavier Xia 				if (pte_young(pte)) {
3144602e575SRyan Roberts 					orig_pte = pte_mkyoung(orig_pte);
315*093ae7a0SXavier Xia 					break;
316*093ae7a0SXavier Xia 				}
317*093ae7a0SXavier Xia 			}
318*093ae7a0SXavier Xia 			break;
319*093ae7a0SXavier Xia 		}
320*093ae7a0SXavier Xia 
321*093ae7a0SXavier Xia 		if (pte_young(pte)) {
322*093ae7a0SXavier Xia 			orig_pte = pte_mkyoung(orig_pte);
323*093ae7a0SXavier Xia 			i++;
324*093ae7a0SXavier Xia 			ptep++;
325*093ae7a0SXavier Xia 			for (; i < CONT_PTES; i++, ptep++) {
326*093ae7a0SXavier Xia 				pte = __ptep_get(ptep);
327*093ae7a0SXavier Xia 				if (pte_dirty(pte)) {
328*093ae7a0SXavier Xia 					orig_pte = pte_mkdirty(orig_pte);
329*093ae7a0SXavier Xia 					break;
330*093ae7a0SXavier Xia 				}
331*093ae7a0SXavier Xia 			}
332*093ae7a0SXavier Xia 			break;
333*093ae7a0SXavier Xia 		}
3344602e575SRyan Roberts 	}
3354602e575SRyan Roberts 
3364602e575SRyan Roberts 	return orig_pte;
3374602e575SRyan Roberts }
338912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_get);
3394602e575SRyan Roberts 
contpte_is_consistent(pte_t pte,unsigned long pfn,pgprot_t orig_prot)340*093ae7a0SXavier Xia static inline bool contpte_is_consistent(pte_t pte, unsigned long pfn,
341*093ae7a0SXavier Xia 					pgprot_t orig_prot)
342*093ae7a0SXavier Xia {
343*093ae7a0SXavier Xia 	pgprot_t prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
344*093ae7a0SXavier Xia 
345*093ae7a0SXavier Xia 	return pte_valid_cont(pte) && pte_pfn(pte) == pfn &&
346*093ae7a0SXavier Xia 			pgprot_val(prot) == pgprot_val(orig_prot);
347*093ae7a0SXavier Xia }
348*093ae7a0SXavier Xia 
contpte_ptep_get_lockless(pte_t * orig_ptep)3494602e575SRyan Roberts pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
3504602e575SRyan Roberts {
3514602e575SRyan Roberts 	/*
35294c18d5fSRyan Roberts 	 * The ptep_get_lockless() API requires us to read and return *orig_ptep
35394c18d5fSRyan Roberts 	 * so that it is self-consistent, without the PTL held, so we may be
35494c18d5fSRyan Roberts 	 * racing with other threads modifying the pte. Usually a READ_ONCE()
35594c18d5fSRyan Roberts 	 * would suffice, but for the contpte case, we also need to gather the
35694c18d5fSRyan Roberts 	 * access and dirty bits from across all ptes in the contiguous block,
35794c18d5fSRyan Roberts 	 * and we can't read all of those neighbouring ptes atomically, so any
35894c18d5fSRyan Roberts 	 * contiguous range may be unfolded/modified/refolded under our feet.
35994c18d5fSRyan Roberts 	 * Therefore we ensure we read a _consistent_ contpte range by checking
36094c18d5fSRyan Roberts 	 * that all ptes in the range are valid and have CONT_PTE set, that all
36194c18d5fSRyan Roberts 	 * pfns are contiguous and that all pgprots are the same (ignoring
36294c18d5fSRyan Roberts 	 * access/dirty). If we find a pte that is not consistent, then we must
36394c18d5fSRyan Roberts 	 * be racing with an update so start again. If the target pte does not
36494c18d5fSRyan Roberts 	 * have CONT_PTE set then that is considered consistent on its own
36594c18d5fSRyan Roberts 	 * because it is not part of a contpte range.
3664602e575SRyan Roberts 	 */
3674602e575SRyan Roberts 
3684602e575SRyan Roberts 	pgprot_t orig_prot;
3694602e575SRyan Roberts 	unsigned long pfn;
3704602e575SRyan Roberts 	pte_t orig_pte;
3714602e575SRyan Roberts 	pte_t *ptep;
3724602e575SRyan Roberts 	pte_t pte;
3734602e575SRyan Roberts 	int i;
3744602e575SRyan Roberts 
3754602e575SRyan Roberts retry:
3764602e575SRyan Roberts 	orig_pte = __ptep_get(orig_ptep);
3774602e575SRyan Roberts 
3784602e575SRyan Roberts 	if (!pte_valid_cont(orig_pte))
3794602e575SRyan Roberts 		return orig_pte;
3804602e575SRyan Roberts 
3814602e575SRyan Roberts 	orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte)));
3824602e575SRyan Roberts 	ptep = contpte_align_down(orig_ptep);
3834602e575SRyan Roberts 	pfn = pte_pfn(orig_pte) - (orig_ptep - ptep);
3844602e575SRyan Roberts 
3854602e575SRyan Roberts 	for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) {
3864602e575SRyan Roberts 		pte = __ptep_get(ptep);
3874602e575SRyan Roberts 
388*093ae7a0SXavier Xia 		if (!contpte_is_consistent(pte, pfn, orig_prot))
3894602e575SRyan Roberts 			goto retry;
3904602e575SRyan Roberts 
391*093ae7a0SXavier Xia 		if (pte_dirty(pte)) {
3924602e575SRyan Roberts 			orig_pte = pte_mkdirty(orig_pte);
393*093ae7a0SXavier Xia 			for (; i < CONT_PTES; i++, ptep++, pfn++) {
394*093ae7a0SXavier Xia 				pte = __ptep_get(ptep);
3954602e575SRyan Roberts 
396*093ae7a0SXavier Xia 				if (!contpte_is_consistent(pte, pfn, orig_prot))
397*093ae7a0SXavier Xia 					goto retry;
398*093ae7a0SXavier Xia 
399*093ae7a0SXavier Xia 				if (pte_young(pte)) {
4004602e575SRyan Roberts 					orig_pte = pte_mkyoung(orig_pte);
401*093ae7a0SXavier Xia 					break;
402*093ae7a0SXavier Xia 				}
403*093ae7a0SXavier Xia 			}
404*093ae7a0SXavier Xia 			break;
405*093ae7a0SXavier Xia 		}
406*093ae7a0SXavier Xia 
407*093ae7a0SXavier Xia 		if (pte_young(pte)) {
408*093ae7a0SXavier Xia 			orig_pte = pte_mkyoung(orig_pte);
409*093ae7a0SXavier Xia 			i++;
410*093ae7a0SXavier Xia 			ptep++;
411*093ae7a0SXavier Xia 			pfn++;
412*093ae7a0SXavier Xia 			for (; i < CONT_PTES; i++, ptep++, pfn++) {
413*093ae7a0SXavier Xia 				pte = __ptep_get(ptep);
414*093ae7a0SXavier Xia 
415*093ae7a0SXavier Xia 				if (!contpte_is_consistent(pte, pfn, orig_prot))
416*093ae7a0SXavier Xia 					goto retry;
417*093ae7a0SXavier Xia 
418*093ae7a0SXavier Xia 				if (pte_dirty(pte)) {
419*093ae7a0SXavier Xia 					orig_pte = pte_mkdirty(orig_pte);
420*093ae7a0SXavier Xia 					break;
421*093ae7a0SXavier Xia 				}
422*093ae7a0SXavier Xia 			}
423*093ae7a0SXavier Xia 			break;
424*093ae7a0SXavier Xia 		}
4254602e575SRyan Roberts 	}
4264602e575SRyan Roberts 
4274602e575SRyan Roberts 	return orig_pte;
4284602e575SRyan Roberts }
429912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless);
4304602e575SRyan Roberts 
contpte_set_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte,unsigned int nr)4314602e575SRyan Roberts void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
4324602e575SRyan Roberts 					pte_t *ptep, pte_t pte, unsigned int nr)
4334602e575SRyan Roberts {
4344602e575SRyan Roberts 	unsigned long next;
4354602e575SRyan Roberts 	unsigned long end;
4364602e575SRyan Roberts 	unsigned long pfn;
4374602e575SRyan Roberts 	pgprot_t prot;
4384602e575SRyan Roberts 
4394602e575SRyan Roberts 	/*
4404602e575SRyan Roberts 	 * The set_ptes() spec guarantees that when nr > 1, the initial state of
4414602e575SRyan Roberts 	 * all ptes is not-present. Therefore we never need to unfold or
4424602e575SRyan Roberts 	 * otherwise invalidate a range before we set the new ptes.
4434602e575SRyan Roberts 	 * contpte_set_ptes() should never be called for nr < 2.
4444602e575SRyan Roberts 	 */
4454602e575SRyan Roberts 	VM_WARN_ON(nr == 1);
4464602e575SRyan Roberts 
4474602e575SRyan Roberts 	if (!mm_is_user(mm))
4484602e575SRyan Roberts 		return __set_ptes(mm, addr, ptep, pte, nr);
4494602e575SRyan Roberts 
4504602e575SRyan Roberts 	end = addr + (nr << PAGE_SHIFT);
4514602e575SRyan Roberts 	pfn = pte_pfn(pte);
4524602e575SRyan Roberts 	prot = pte_pgprot(pte);
4534602e575SRyan Roberts 
4544602e575SRyan Roberts 	do {
4554602e575SRyan Roberts 		next = pte_cont_addr_end(addr, end);
4564602e575SRyan Roberts 		nr = (next - addr) >> PAGE_SHIFT;
4574602e575SRyan Roberts 		pte = pfn_pte(pfn, prot);
4584602e575SRyan Roberts 
4594602e575SRyan Roberts 		if (((addr | next | (pfn << PAGE_SHIFT)) & ~CONT_PTE_MASK) == 0)
4604602e575SRyan Roberts 			pte = pte_mkcont(pte);
4614602e575SRyan Roberts 		else
4624602e575SRyan Roberts 			pte = pte_mknoncont(pte);
4634602e575SRyan Roberts 
4644602e575SRyan Roberts 		__set_ptes(mm, addr, ptep, pte, nr);
4654602e575SRyan Roberts 
4664602e575SRyan Roberts 		addr = next;
4674602e575SRyan Roberts 		ptep += nr;
4684602e575SRyan Roberts 		pfn += nr;
4694602e575SRyan Roberts 
4704602e575SRyan Roberts 	} while (addr != end);
4714602e575SRyan Roberts }
472912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_set_ptes);
4734602e575SRyan Roberts 
contpte_clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)4746b1e4efbSRyan Roberts void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
4756b1e4efbSRyan Roberts 				pte_t *ptep, unsigned int nr, int full)
4766b1e4efbSRyan Roberts {
4776b1e4efbSRyan Roberts 	contpte_try_unfold_partial(mm, addr, ptep, nr);
4786b1e4efbSRyan Roberts 	__clear_full_ptes(mm, addr, ptep, nr, full);
4796b1e4efbSRyan Roberts }
480912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_clear_full_ptes);
4816b1e4efbSRyan Roberts 
contpte_get_and_clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)4826b1e4efbSRyan Roberts pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm,
4836b1e4efbSRyan Roberts 				unsigned long addr, pte_t *ptep,
4846b1e4efbSRyan Roberts 				unsigned int nr, int full)
4856b1e4efbSRyan Roberts {
4866b1e4efbSRyan Roberts 	contpte_try_unfold_partial(mm, addr, ptep, nr);
4876b1e4efbSRyan Roberts 	return __get_and_clear_full_ptes(mm, addr, ptep, nr, full);
4886b1e4efbSRyan Roberts }
489912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_get_and_clear_full_ptes);
4906b1e4efbSRyan Roberts 
contpte_ptep_test_and_clear_young(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)4914602e575SRyan Roberts int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma,
4924602e575SRyan Roberts 					unsigned long addr, pte_t *ptep)
4934602e575SRyan Roberts {
4944602e575SRyan Roberts 	/*
4954602e575SRyan Roberts 	 * ptep_clear_flush_young() technically requires us to clear the access
4964602e575SRyan Roberts 	 * flag for a _single_ pte. However, the core-mm code actually tracks
4974602e575SRyan Roberts 	 * access/dirty per folio, not per page. And since we only create a
4984602e575SRyan Roberts 	 * contig range when the range is covered by a single folio, we can get
4994602e575SRyan Roberts 	 * away with clearing young for the whole contig range here, so we avoid
5004602e575SRyan Roberts 	 * having to unfold.
5014602e575SRyan Roberts 	 */
5024602e575SRyan Roberts 
5034602e575SRyan Roberts 	int young = 0;
5044602e575SRyan Roberts 	int i;
5054602e575SRyan Roberts 
5064602e575SRyan Roberts 	ptep = contpte_align_down(ptep);
5074602e575SRyan Roberts 	addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
5084602e575SRyan Roberts 
5094602e575SRyan Roberts 	for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
5104602e575SRyan Roberts 		young |= __ptep_test_and_clear_young(vma, addr, ptep);
5114602e575SRyan Roberts 
5124602e575SRyan Roberts 	return young;
5134602e575SRyan Roberts }
514912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_test_and_clear_young);
5154602e575SRyan Roberts 
contpte_ptep_clear_flush_young(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)5164602e575SRyan Roberts int contpte_ptep_clear_flush_young(struct vm_area_struct *vma,
5174602e575SRyan Roberts 					unsigned long addr, pte_t *ptep)
5184602e575SRyan Roberts {
5194602e575SRyan Roberts 	int young;
5204602e575SRyan Roberts 
5214602e575SRyan Roberts 	young = contpte_ptep_test_and_clear_young(vma, addr, ptep);
5224602e575SRyan Roberts 
5234602e575SRyan Roberts 	if (young) {
5244602e575SRyan Roberts 		/*
5254602e575SRyan Roberts 		 * See comment in __ptep_clear_flush_young(); same rationale for
5264602e575SRyan Roberts 		 * eliding the trailing DSB applies here.
5274602e575SRyan Roberts 		 */
5284602e575SRyan Roberts 		addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
5292f4ab3acSBarry Song 		__flush_tlb_range_nosync(vma->vm_mm, addr, addr + CONT_PTE_SIZE,
5304602e575SRyan Roberts 					 PAGE_SIZE, true, 3);
5314602e575SRyan Roberts 	}
5324602e575SRyan Roberts 
5334602e575SRyan Roberts 	return young;
5344602e575SRyan Roberts }
535912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_clear_flush_young);
5364602e575SRyan Roberts 
contpte_wrprotect_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr)537311a6cf2SRyan Roberts void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
538311a6cf2SRyan Roberts 					pte_t *ptep, unsigned int nr)
539311a6cf2SRyan Roberts {
540311a6cf2SRyan Roberts 	/*
541311a6cf2SRyan Roberts 	 * If wrprotecting an entire contig range, we can avoid unfolding. Just
542311a6cf2SRyan Roberts 	 * set wrprotect and wait for the later mmu_gather flush to invalidate
543311a6cf2SRyan Roberts 	 * the tlb. Until the flush, the page may or may not be wrprotected.
544311a6cf2SRyan Roberts 	 * After the flush, it is guaranteed wrprotected. If it's a partial
545311a6cf2SRyan Roberts 	 * range though, we must unfold, because we can't have a case where
546311a6cf2SRyan Roberts 	 * CONT_PTE is set but wrprotect applies to a subset of the PTEs; this
547311a6cf2SRyan Roberts 	 * would cause it to continue to be unpredictable after the flush.
548311a6cf2SRyan Roberts 	 */
549311a6cf2SRyan Roberts 
550311a6cf2SRyan Roberts 	contpte_try_unfold_partial(mm, addr, ptep, nr);
551311a6cf2SRyan Roberts 	__wrprotect_ptes(mm, addr, ptep, nr);
552311a6cf2SRyan Roberts }
553912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);
554311a6cf2SRyan Roberts 
contpte_clear_young_dirty_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr,cydp_t flags)55589e86854SLance Yang void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
55689e86854SLance Yang 				    unsigned long addr, pte_t *ptep,
55789e86854SLance Yang 				    unsigned int nr, cydp_t flags)
55889e86854SLance Yang {
55989e86854SLance Yang 	/*
56089e86854SLance Yang 	 * We can safely clear access/dirty without needing to unfold from
56189e86854SLance Yang 	 * the architectures perspective, even when contpte is set. If the
56289e86854SLance Yang 	 * range starts or ends midway through a contpte block, we can just
56389e86854SLance Yang 	 * expand to include the full contpte block. While this is not
56489e86854SLance Yang 	 * exactly what the core-mm asked for, it tracks access/dirty per
56589e86854SLance Yang 	 * folio, not per page. And since we only create a contpte block
56689e86854SLance Yang 	 * when it is covered by a single folio, we can get away with
56789e86854SLance Yang 	 * clearing access/dirty for the whole block.
56889e86854SLance Yang 	 */
56989e86854SLance Yang 	unsigned long start = addr;
5706434e698SBarry Song 	unsigned long end = start + nr * PAGE_SIZE;
57189e86854SLance Yang 
57289e86854SLance Yang 	if (pte_cont(__ptep_get(ptep + nr - 1)))
57389e86854SLance Yang 		end = ALIGN(end, CONT_PTE_SIZE);
57489e86854SLance Yang 
57589e86854SLance Yang 	if (pte_cont(__ptep_get(ptep))) {
57689e86854SLance Yang 		start = ALIGN_DOWN(start, CONT_PTE_SIZE);
57789e86854SLance Yang 		ptep = contpte_align_down(ptep);
57889e86854SLance Yang 	}
57989e86854SLance Yang 
5806434e698SBarry Song 	__clear_young_dirty_ptes(vma, start, ptep, (end - start) / PAGE_SIZE, flags);
58189e86854SLance Yang }
58289e86854SLance Yang EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);
58389e86854SLance Yang 
contpte_ptep_set_access_flags(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t entry,int dirty)5844602e575SRyan Roberts int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
5854602e575SRyan Roberts 					unsigned long addr, pte_t *ptep,
5864602e575SRyan Roberts 					pte_t entry, int dirty)
5874602e575SRyan Roberts {
5884602e575SRyan Roberts 	unsigned long start_addr;
5894602e575SRyan Roberts 	pte_t orig_pte;
5904602e575SRyan Roberts 	int i;
5914602e575SRyan Roberts 
5924602e575SRyan Roberts 	/*
5934602e575SRyan Roberts 	 * Gather the access/dirty bits for the contiguous range. If nothing has
5944602e575SRyan Roberts 	 * changed, its a noop.
5954602e575SRyan Roberts 	 */
5964602e575SRyan Roberts 	orig_pte = pte_mknoncont(ptep_get(ptep));
5974602e575SRyan Roberts 	if (pte_val(orig_pte) == pte_val(entry))
5984602e575SRyan Roberts 		return 0;
5994602e575SRyan Roberts 
6004602e575SRyan Roberts 	/*
6014602e575SRyan Roberts 	 * We can fix up access/dirty bits without having to unfold the contig
6024602e575SRyan Roberts 	 * range. But if the write bit is changing, we must unfold.
6034602e575SRyan Roberts 	 */
6044602e575SRyan Roberts 	if (pte_write(orig_pte) == pte_write(entry)) {
6054602e575SRyan Roberts 		/*
6064602e575SRyan Roberts 		 * For HW access management, we technically only need to update
6074602e575SRyan Roberts 		 * the flag on a single pte in the range. But for SW access
6084602e575SRyan Roberts 		 * management, we need to update all the ptes to prevent extra
6094602e575SRyan Roberts 		 * faults. Avoid per-page tlb flush in __ptep_set_access_flags()
6104602e575SRyan Roberts 		 * and instead flush the whole range at the end.
6114602e575SRyan Roberts 		 */
6124602e575SRyan Roberts 		ptep = contpte_align_down(ptep);
6134602e575SRyan Roberts 		start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
6144602e575SRyan Roberts 
61570565f2bSBarry Song 		/*
61670565f2bSBarry Song 		 * We are not advancing entry because __ptep_set_access_flags()
61770565f2bSBarry Song 		 * only consumes access flags from entry. And since we have checked
61870565f2bSBarry Song 		 * for the whole contpte block and returned early, pte_same()
61970565f2bSBarry Song 		 * within __ptep_set_access_flags() is likely false.
62070565f2bSBarry Song 		 */
6214602e575SRyan Roberts 		for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
6224602e575SRyan Roberts 			__ptep_set_access_flags(vma, addr, ptep, entry, 0);
6234602e575SRyan Roberts 
6244602e575SRyan Roberts 		if (dirty)
6254602e575SRyan Roberts 			__flush_tlb_range(vma, start_addr, addr,
6264602e575SRyan Roberts 							PAGE_SIZE, true, 3);
6274602e575SRyan Roberts 	} else {
6284602e575SRyan Roberts 		__contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte);
6294602e575SRyan Roberts 		__ptep_set_access_flags(vma, addr, ptep, entry, dirty);
6304602e575SRyan Roberts 	}
6314602e575SRyan Roberts 
6324602e575SRyan Roberts 	return 1;
6334602e575SRyan Roberts }
634912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_set_access_flags);
635