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
714602e575SRyan Roberts __flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, true, 3);
724602e575SRyan Roberts
734602e575SRyan Roberts __set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES);
744602e575SRyan Roberts }
754602e575SRyan Roberts
__contpte_try_fold(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)76f0c22649SRyan Roberts void __contpte_try_fold(struct mm_struct *mm, unsigned long addr,
77f0c22649SRyan Roberts pte_t *ptep, pte_t pte)
78f0c22649SRyan Roberts {
79f0c22649SRyan Roberts /*
80f0c22649SRyan Roberts * We have already checked that the virtual and pysical addresses are
81f0c22649SRyan Roberts * correctly aligned for a contpte mapping in contpte_try_fold() so the
82f0c22649SRyan Roberts * remaining checks are to ensure that the contpte range is fully
83f0c22649SRyan Roberts * covered by a single folio, and ensure that all the ptes are valid
84f0c22649SRyan Roberts * with contiguous PFNs and matching prots. We ignore the state of the
85f0c22649SRyan Roberts * access and dirty bits for the purpose of deciding if its a contiguous
86f0c22649SRyan Roberts * range; the folding process will generate a single contpte entry which
87f0c22649SRyan Roberts * has a single access and dirty bit. Those 2 bits are the logical OR of
88f0c22649SRyan Roberts * their respective bits in the constituent pte entries. In order to
89f0c22649SRyan Roberts * ensure the contpte range is covered by a single folio, we must
90f0c22649SRyan Roberts * recover the folio from the pfn, but special mappings don't have a
91f0c22649SRyan Roberts * folio backing them. Fortunately contpte_try_fold() already checked
92f0c22649SRyan Roberts * that the pte is not special - we never try to fold special mappings.
93f0c22649SRyan Roberts * Note we can't use vm_normal_page() for this since we don't have the
94f0c22649SRyan Roberts * vma.
95f0c22649SRyan Roberts */
96f0c22649SRyan Roberts
97f0c22649SRyan Roberts unsigned long folio_start, folio_end;
98f0c22649SRyan Roberts unsigned long cont_start, cont_end;
99f0c22649SRyan Roberts pte_t expected_pte, subpte;
100f0c22649SRyan Roberts struct folio *folio;
101f0c22649SRyan Roberts struct page *page;
102f0c22649SRyan Roberts unsigned long pfn;
103f0c22649SRyan Roberts pte_t *orig_ptep;
104f0c22649SRyan Roberts pgprot_t prot;
105f0c22649SRyan Roberts
106f0c22649SRyan Roberts int i;
107f0c22649SRyan Roberts
108f0c22649SRyan Roberts if (!mm_is_user(mm))
109f0c22649SRyan Roberts return;
110f0c22649SRyan Roberts
111f0c22649SRyan Roberts page = pte_page(pte);
112f0c22649SRyan Roberts folio = page_folio(page);
113f0c22649SRyan Roberts folio_start = addr - (page - &folio->page) * PAGE_SIZE;
114f0c22649SRyan Roberts folio_end = folio_start + folio_nr_pages(folio) * PAGE_SIZE;
115f0c22649SRyan Roberts cont_start = ALIGN_DOWN(addr, CONT_PTE_SIZE);
116f0c22649SRyan Roberts cont_end = cont_start + CONT_PTE_SIZE;
117f0c22649SRyan Roberts
118f0c22649SRyan Roberts if (folio_start > cont_start || folio_end < cont_end)
119f0c22649SRyan Roberts return;
120f0c22649SRyan Roberts
121f0c22649SRyan Roberts pfn = ALIGN_DOWN(pte_pfn(pte), CONT_PTES);
122f0c22649SRyan Roberts prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
123f0c22649SRyan Roberts expected_pte = pfn_pte(pfn, prot);
124f0c22649SRyan Roberts orig_ptep = ptep;
125f0c22649SRyan Roberts ptep = contpte_align_down(ptep);
126f0c22649SRyan Roberts
127f0c22649SRyan Roberts for (i = 0; i < CONT_PTES; i++) {
128f0c22649SRyan Roberts subpte = pte_mkold(pte_mkclean(__ptep_get(ptep)));
129f0c22649SRyan Roberts if (!pte_same(subpte, expected_pte))
130f0c22649SRyan Roberts return;
131f0c22649SRyan Roberts expected_pte = pte_advance_pfn(expected_pte, 1);
132f0c22649SRyan Roberts ptep++;
133f0c22649SRyan Roberts }
134f0c22649SRyan Roberts
135f0c22649SRyan Roberts pte = pte_mkcont(pte);
136f0c22649SRyan Roberts contpte_convert(mm, addr, orig_ptep, pte);
137f0c22649SRyan Roberts }
138912609e9SRyan Roberts EXPORT_SYMBOL_GPL(__contpte_try_fold);
139f0c22649SRyan Roberts
__contpte_try_unfold(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte)1404602e575SRyan Roberts void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr,
1414602e575SRyan Roberts pte_t *ptep, pte_t pte)
1424602e575SRyan Roberts {
1434602e575SRyan Roberts /*
1444602e575SRyan Roberts * We have already checked that the ptes are contiguous in
1454602e575SRyan Roberts * contpte_try_unfold(), so just check that the mm is user space.
1464602e575SRyan Roberts */
1474602e575SRyan Roberts if (!mm_is_user(mm))
1484602e575SRyan Roberts return;
1494602e575SRyan Roberts
1504602e575SRyan Roberts pte = pte_mknoncont(pte);
1514602e575SRyan Roberts contpte_convert(mm, addr, ptep, pte);
1524602e575SRyan Roberts }
153912609e9SRyan Roberts EXPORT_SYMBOL_GPL(__contpte_try_unfold);
1544602e575SRyan Roberts
contpte_ptep_get(pte_t * ptep,pte_t orig_pte)1554602e575SRyan Roberts pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte)
1564602e575SRyan Roberts {
1574602e575SRyan Roberts /*
1584602e575SRyan Roberts * Gather access/dirty bits, which may be populated in any of the ptes
1594602e575SRyan Roberts * of the contig range. We are guaranteed to be holding the PTL, so any
1604602e575SRyan Roberts * contiguous range cannot be unfolded or otherwise modified under our
1614602e575SRyan Roberts * feet.
1624602e575SRyan Roberts */
1634602e575SRyan Roberts
1644602e575SRyan Roberts pte_t pte;
1654602e575SRyan Roberts int i;
1664602e575SRyan Roberts
1674602e575SRyan Roberts ptep = contpte_align_down(ptep);
1684602e575SRyan Roberts
1694602e575SRyan Roberts for (i = 0; i < CONT_PTES; i++, ptep++) {
1704602e575SRyan Roberts pte = __ptep_get(ptep);
1714602e575SRyan Roberts
1724602e575SRyan Roberts if (pte_dirty(pte))
1734602e575SRyan Roberts orig_pte = pte_mkdirty(orig_pte);
1744602e575SRyan Roberts
1754602e575SRyan Roberts if (pte_young(pte))
1764602e575SRyan Roberts orig_pte = pte_mkyoung(orig_pte);
1774602e575SRyan Roberts }
1784602e575SRyan Roberts
1794602e575SRyan Roberts return orig_pte;
1804602e575SRyan Roberts }
181912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_get);
1824602e575SRyan Roberts
contpte_ptep_get_lockless(pte_t * orig_ptep)1834602e575SRyan Roberts pte_t contpte_ptep_get_lockless(pte_t *orig_ptep)
1844602e575SRyan Roberts {
1854602e575SRyan Roberts /*
18694c18d5fSRyan Roberts * The ptep_get_lockless() API requires us to read and return *orig_ptep
18794c18d5fSRyan Roberts * so that it is self-consistent, without the PTL held, so we may be
18894c18d5fSRyan Roberts * racing with other threads modifying the pte. Usually a READ_ONCE()
18994c18d5fSRyan Roberts * would suffice, but for the contpte case, we also need to gather the
19094c18d5fSRyan Roberts * access and dirty bits from across all ptes in the contiguous block,
19194c18d5fSRyan Roberts * and we can't read all of those neighbouring ptes atomically, so any
19294c18d5fSRyan Roberts * contiguous range may be unfolded/modified/refolded under our feet.
19394c18d5fSRyan Roberts * Therefore we ensure we read a _consistent_ contpte range by checking
19494c18d5fSRyan Roberts * that all ptes in the range are valid and have CONT_PTE set, that all
19594c18d5fSRyan Roberts * pfns are contiguous and that all pgprots are the same (ignoring
19694c18d5fSRyan Roberts * access/dirty). If we find a pte that is not consistent, then we must
19794c18d5fSRyan Roberts * be racing with an update so start again. If the target pte does not
19894c18d5fSRyan Roberts * have CONT_PTE set then that is considered consistent on its own
19994c18d5fSRyan Roberts * because it is not part of a contpte range.
2004602e575SRyan Roberts */
2014602e575SRyan Roberts
2024602e575SRyan Roberts pgprot_t orig_prot;
2034602e575SRyan Roberts unsigned long pfn;
2044602e575SRyan Roberts pte_t orig_pte;
2054602e575SRyan Roberts pgprot_t prot;
2064602e575SRyan Roberts pte_t *ptep;
2074602e575SRyan Roberts pte_t pte;
2084602e575SRyan Roberts int i;
2094602e575SRyan Roberts
2104602e575SRyan Roberts retry:
2114602e575SRyan Roberts orig_pte = __ptep_get(orig_ptep);
2124602e575SRyan Roberts
2134602e575SRyan Roberts if (!pte_valid_cont(orig_pte))
2144602e575SRyan Roberts return orig_pte;
2154602e575SRyan Roberts
2164602e575SRyan Roberts orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte)));
2174602e575SRyan Roberts ptep = contpte_align_down(orig_ptep);
2184602e575SRyan Roberts pfn = pte_pfn(orig_pte) - (orig_ptep - ptep);
2194602e575SRyan Roberts
2204602e575SRyan Roberts for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) {
2214602e575SRyan Roberts pte = __ptep_get(ptep);
2224602e575SRyan Roberts prot = pte_pgprot(pte_mkold(pte_mkclean(pte)));
2234602e575SRyan Roberts
2244602e575SRyan Roberts if (!pte_valid_cont(pte) ||
2254602e575SRyan Roberts pte_pfn(pte) != pfn ||
2264602e575SRyan Roberts pgprot_val(prot) != pgprot_val(orig_prot))
2274602e575SRyan Roberts goto retry;
2284602e575SRyan Roberts
2294602e575SRyan Roberts if (pte_dirty(pte))
2304602e575SRyan Roberts orig_pte = pte_mkdirty(orig_pte);
2314602e575SRyan Roberts
2324602e575SRyan Roberts if (pte_young(pte))
2334602e575SRyan Roberts orig_pte = pte_mkyoung(orig_pte);
2344602e575SRyan Roberts }
2354602e575SRyan Roberts
2364602e575SRyan Roberts return orig_pte;
2374602e575SRyan Roberts }
238912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_get_lockless);
2394602e575SRyan Roberts
contpte_set_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte,unsigned int nr)2404602e575SRyan Roberts void contpte_set_ptes(struct mm_struct *mm, unsigned long addr,
2414602e575SRyan Roberts pte_t *ptep, pte_t pte, unsigned int nr)
2424602e575SRyan Roberts {
2434602e575SRyan Roberts unsigned long next;
2444602e575SRyan Roberts unsigned long end;
2454602e575SRyan Roberts unsigned long pfn;
2464602e575SRyan Roberts pgprot_t prot;
2474602e575SRyan Roberts
2484602e575SRyan Roberts /*
2494602e575SRyan Roberts * The set_ptes() spec guarantees that when nr > 1, the initial state of
2504602e575SRyan Roberts * all ptes is not-present. Therefore we never need to unfold or
2514602e575SRyan Roberts * otherwise invalidate a range before we set the new ptes.
2524602e575SRyan Roberts * contpte_set_ptes() should never be called for nr < 2.
2534602e575SRyan Roberts */
2544602e575SRyan Roberts VM_WARN_ON(nr == 1);
2554602e575SRyan Roberts
2564602e575SRyan Roberts if (!mm_is_user(mm))
2574602e575SRyan Roberts return __set_ptes(mm, addr, ptep, pte, nr);
2584602e575SRyan Roberts
2594602e575SRyan Roberts end = addr + (nr << PAGE_SHIFT);
2604602e575SRyan Roberts pfn = pte_pfn(pte);
2614602e575SRyan Roberts prot = pte_pgprot(pte);
2624602e575SRyan Roberts
2634602e575SRyan Roberts do {
2644602e575SRyan Roberts next = pte_cont_addr_end(addr, end);
2654602e575SRyan Roberts nr = (next - addr) >> PAGE_SHIFT;
2664602e575SRyan Roberts pte = pfn_pte(pfn, prot);
2674602e575SRyan Roberts
2684602e575SRyan Roberts if (((addr | next | (pfn << PAGE_SHIFT)) & ~CONT_PTE_MASK) == 0)
2694602e575SRyan Roberts pte = pte_mkcont(pte);
2704602e575SRyan Roberts else
2714602e575SRyan Roberts pte = pte_mknoncont(pte);
2724602e575SRyan Roberts
2734602e575SRyan Roberts __set_ptes(mm, addr, ptep, pte, nr);
2744602e575SRyan Roberts
2754602e575SRyan Roberts addr = next;
2764602e575SRyan Roberts ptep += nr;
2774602e575SRyan Roberts pfn += nr;
2784602e575SRyan Roberts
2794602e575SRyan Roberts } while (addr != end);
2804602e575SRyan Roberts }
281912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_set_ptes);
2824602e575SRyan Roberts
contpte_clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)2836b1e4efbSRyan Roberts void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr,
2846b1e4efbSRyan Roberts pte_t *ptep, unsigned int nr, int full)
2856b1e4efbSRyan Roberts {
2866b1e4efbSRyan Roberts contpte_try_unfold_partial(mm, addr, ptep, nr);
2876b1e4efbSRyan Roberts __clear_full_ptes(mm, addr, ptep, nr, full);
2886b1e4efbSRyan Roberts }
289912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_clear_full_ptes);
2906b1e4efbSRyan Roberts
contpte_get_and_clear_full_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr,int full)2916b1e4efbSRyan Roberts pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm,
2926b1e4efbSRyan Roberts unsigned long addr, pte_t *ptep,
2936b1e4efbSRyan Roberts unsigned int nr, int full)
2946b1e4efbSRyan Roberts {
2956b1e4efbSRyan Roberts contpte_try_unfold_partial(mm, addr, ptep, nr);
2966b1e4efbSRyan Roberts return __get_and_clear_full_ptes(mm, addr, ptep, nr, full);
2976b1e4efbSRyan Roberts }
298912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_get_and_clear_full_ptes);
2996b1e4efbSRyan Roberts
contpte_ptep_test_and_clear_young(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)3004602e575SRyan Roberts int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma,
3014602e575SRyan Roberts unsigned long addr, pte_t *ptep)
3024602e575SRyan Roberts {
3034602e575SRyan Roberts /*
3044602e575SRyan Roberts * ptep_clear_flush_young() technically requires us to clear the access
3054602e575SRyan Roberts * flag for a _single_ pte. However, the core-mm code actually tracks
3064602e575SRyan Roberts * access/dirty per folio, not per page. And since we only create a
3074602e575SRyan Roberts * contig range when the range is covered by a single folio, we can get
3084602e575SRyan Roberts * away with clearing young for the whole contig range here, so we avoid
3094602e575SRyan Roberts * having to unfold.
3104602e575SRyan Roberts */
3114602e575SRyan Roberts
3124602e575SRyan Roberts int young = 0;
3134602e575SRyan Roberts int i;
3144602e575SRyan Roberts
3154602e575SRyan Roberts ptep = contpte_align_down(ptep);
3164602e575SRyan Roberts addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
3174602e575SRyan Roberts
3184602e575SRyan Roberts for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
3194602e575SRyan Roberts young |= __ptep_test_and_clear_young(vma, addr, ptep);
3204602e575SRyan Roberts
3214602e575SRyan Roberts return young;
3224602e575SRyan Roberts }
323912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_test_and_clear_young);
3244602e575SRyan Roberts
contpte_ptep_clear_flush_young(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)3254602e575SRyan Roberts int contpte_ptep_clear_flush_young(struct vm_area_struct *vma,
3264602e575SRyan Roberts unsigned long addr, pte_t *ptep)
3274602e575SRyan Roberts {
3284602e575SRyan Roberts int young;
3294602e575SRyan Roberts
3304602e575SRyan Roberts young = contpte_ptep_test_and_clear_young(vma, addr, ptep);
3314602e575SRyan Roberts
3324602e575SRyan Roberts if (young) {
3334602e575SRyan Roberts /*
3344602e575SRyan Roberts * See comment in __ptep_clear_flush_young(); same rationale for
3354602e575SRyan Roberts * eliding the trailing DSB applies here.
3364602e575SRyan Roberts */
3374602e575SRyan Roberts addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
3384602e575SRyan Roberts __flush_tlb_range_nosync(vma, addr, addr + CONT_PTE_SIZE,
3394602e575SRyan Roberts PAGE_SIZE, true, 3);
3404602e575SRyan Roberts }
3414602e575SRyan Roberts
3424602e575SRyan Roberts return young;
3434602e575SRyan Roberts }
344912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_clear_flush_young);
3454602e575SRyan Roberts
contpte_wrprotect_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,unsigned int nr)346311a6cf2SRyan Roberts void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
347311a6cf2SRyan Roberts pte_t *ptep, unsigned int nr)
348311a6cf2SRyan Roberts {
349311a6cf2SRyan Roberts /*
350311a6cf2SRyan Roberts * If wrprotecting an entire contig range, we can avoid unfolding. Just
351311a6cf2SRyan Roberts * set wrprotect and wait for the later mmu_gather flush to invalidate
352311a6cf2SRyan Roberts * the tlb. Until the flush, the page may or may not be wrprotected.
353311a6cf2SRyan Roberts * After the flush, it is guaranteed wrprotected. If it's a partial
354311a6cf2SRyan Roberts * range though, we must unfold, because we can't have a case where
355311a6cf2SRyan Roberts * CONT_PTE is set but wrprotect applies to a subset of the PTEs; this
356311a6cf2SRyan Roberts * would cause it to continue to be unpredictable after the flush.
357311a6cf2SRyan Roberts */
358311a6cf2SRyan Roberts
359311a6cf2SRyan Roberts contpte_try_unfold_partial(mm, addr, ptep, nr);
360311a6cf2SRyan Roberts __wrprotect_ptes(mm, addr, ptep, nr);
361311a6cf2SRyan Roberts }
362912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);
363311a6cf2SRyan Roberts
contpte_clear_young_dirty_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr,cydp_t flags)36489e86854SLance Yang void contpte_clear_young_dirty_ptes(struct vm_area_struct *vma,
36589e86854SLance Yang unsigned long addr, pte_t *ptep,
36689e86854SLance Yang unsigned int nr, cydp_t flags)
36789e86854SLance Yang {
36889e86854SLance Yang /*
36989e86854SLance Yang * We can safely clear access/dirty without needing to unfold from
37089e86854SLance Yang * the architectures perspective, even when contpte is set. If the
37189e86854SLance Yang * range starts or ends midway through a contpte block, we can just
37289e86854SLance Yang * expand to include the full contpte block. While this is not
37389e86854SLance Yang * exactly what the core-mm asked for, it tracks access/dirty per
37489e86854SLance Yang * folio, not per page. And since we only create a contpte block
37589e86854SLance Yang * when it is covered by a single folio, we can get away with
37689e86854SLance Yang * clearing access/dirty for the whole block.
37789e86854SLance Yang */
37889e86854SLance Yang unsigned long start = addr;
3796434e698SBarry Song unsigned long end = start + nr * PAGE_SIZE;
38089e86854SLance Yang
38189e86854SLance Yang if (pte_cont(__ptep_get(ptep + nr - 1)))
38289e86854SLance Yang end = ALIGN(end, CONT_PTE_SIZE);
38389e86854SLance Yang
38489e86854SLance Yang if (pte_cont(__ptep_get(ptep))) {
38589e86854SLance Yang start = ALIGN_DOWN(start, CONT_PTE_SIZE);
38689e86854SLance Yang ptep = contpte_align_down(ptep);
38789e86854SLance Yang }
38889e86854SLance Yang
3896434e698SBarry Song __clear_young_dirty_ptes(vma, start, ptep, (end - start) / PAGE_SIZE, flags);
39089e86854SLance Yang }
39189e86854SLance Yang EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);
39289e86854SLance Yang
contpte_ptep_set_access_flags(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t entry,int dirty)3934602e575SRyan Roberts int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
3944602e575SRyan Roberts unsigned long addr, pte_t *ptep,
3954602e575SRyan Roberts pte_t entry, int dirty)
3964602e575SRyan Roberts {
3974602e575SRyan Roberts unsigned long start_addr;
3984602e575SRyan Roberts pte_t orig_pte;
3994602e575SRyan Roberts int i;
4004602e575SRyan Roberts
4014602e575SRyan Roberts /*
4024602e575SRyan Roberts * Gather the access/dirty bits for the contiguous range. If nothing has
4034602e575SRyan Roberts * changed, its a noop.
4044602e575SRyan Roberts */
4054602e575SRyan Roberts orig_pte = pte_mknoncont(ptep_get(ptep));
4064602e575SRyan Roberts if (pte_val(orig_pte) == pte_val(entry))
4074602e575SRyan Roberts return 0;
4084602e575SRyan Roberts
4094602e575SRyan Roberts /*
4104602e575SRyan Roberts * We can fix up access/dirty bits without having to unfold the contig
4114602e575SRyan Roberts * range. But if the write bit is changing, we must unfold.
4124602e575SRyan Roberts */
4134602e575SRyan Roberts if (pte_write(orig_pte) == pte_write(entry)) {
4144602e575SRyan Roberts /*
4154602e575SRyan Roberts * For HW access management, we technically only need to update
4164602e575SRyan Roberts * the flag on a single pte in the range. But for SW access
4174602e575SRyan Roberts * management, we need to update all the ptes to prevent extra
4184602e575SRyan Roberts * faults. Avoid per-page tlb flush in __ptep_set_access_flags()
4194602e575SRyan Roberts * and instead flush the whole range at the end.
4204602e575SRyan Roberts */
4214602e575SRyan Roberts ptep = contpte_align_down(ptep);
4224602e575SRyan Roberts start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE);
4234602e575SRyan Roberts
424*70565f2bSBarry Song /*
425*70565f2bSBarry Song * We are not advancing entry because __ptep_set_access_flags()
426*70565f2bSBarry Song * only consumes access flags from entry. And since we have checked
427*70565f2bSBarry Song * for the whole contpte block and returned early, pte_same()
428*70565f2bSBarry Song * within __ptep_set_access_flags() is likely false.
429*70565f2bSBarry Song */
4304602e575SRyan Roberts for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE)
4314602e575SRyan Roberts __ptep_set_access_flags(vma, addr, ptep, entry, 0);
4324602e575SRyan Roberts
4334602e575SRyan Roberts if (dirty)
4344602e575SRyan Roberts __flush_tlb_range(vma, start_addr, addr,
4354602e575SRyan Roberts PAGE_SIZE, true, 3);
4364602e575SRyan Roberts } else {
4374602e575SRyan Roberts __contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte);
4384602e575SRyan Roberts __ptep_set_access_flags(vma, addr, ptep, entry, dirty);
4394602e575SRyan Roberts }
4404602e575SRyan Roberts
4414602e575SRyan Roberts return 1;
4424602e575SRyan Roberts }
443912609e9SRyan Roberts EXPORT_SYMBOL_GPL(contpte_ptep_set_access_flags);
444